[M120 Migration][hbbtv] Audio tracks count notification
[platform/framework/web/chromium-efl.git] / media / base / pipeline_impl.h
1 // Copyright 2016 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef MEDIA_BASE_PIPELINE_IMPL_H_
6 #define MEDIA_BASE_PIPELINE_IMPL_H_
7
8 #include <memory>
9
10 #include "base/memory/raw_ptr.h"
11 #include "base/memory/scoped_refptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/task/sequenced_task_runner.h"
14 #include "base/threading/thread_checker.h"
15 #include "base/time/time.h"
16 #include "media/base/media_export.h"
17 #include "media/base/pipeline.h"
18 #include "media/base/renderer.h"
19 #include "media/base/renderer_factory_selector.h"
20 #include "third_party/abseil-cpp/absl/types/optional.h"
21
22 namespace base {
23 class SingleThreadTaskRunner;
24 }
25
26 namespace media {
27
28 class MediaLog;
29
30 // Callbacks used for Renderer creation. When the RendererType is nullopt, the
31 // current base one will be created.
32 using CreateRendererCB = base::RepeatingCallback<std::unique_ptr<Renderer>(
33     absl::optional<RendererType>)>;
34 using RendererCreatedCB = base::OnceCallback<void(std::unique_ptr<Renderer>)>;
35 using AsyncCreateRendererCB =
36     base::RepeatingCallback<void(absl::optional<RendererType>,
37                                  RendererCreatedCB)>;
38
39 // Pipeline runs the media pipeline.  Filters are created and called on the
40 // task runner injected into this object. Pipeline works like a state
41 // machine to perform asynchronous initialization, pausing, seeking and playing.
42 //
43 // Here's a state diagram that describes the lifetime of this object.
44 //
45 //   [ *Created ]                       [ Any State ]
46 //         | Start()                         | Stop()
47 //         V                                 V
48 //   [ Starting ]                       [ Stopping ]
49 //         |                                 |
50 //         V                                 V
51 //   [ Playing ] <---------.            [ Stopped ]
52 //     |  |  | Seek()      |
53 //     |  |  V             |
54 //     |  | [ Seeking ] ---'
55 //     |  |                ^
56 //     |  | *TrackChange() |
57 //     |  V                |
58 //     | [ Switching ] ----'
59 //     |                   ^
60 //     | Suspend()         |
61 //     V                   |
62 //   [ Suspending ]        |
63 //     |                   |
64 //     V                   |
65 //   [ Suspended ]         |
66 //     | Resume()          |
67 //     V                   |
68 //   [ Resuming ] ---------'
69 //
70 // Initialization is a series of state transitions from "Created" through each
71 // filter initialization state.  When all filter initialization states have
72 // completed, we simulate a Seek() to the beginning of the media to give filters
73 // a chance to preroll. From then on the normal Seek() transitions are carried
74 // out and we start playing the media.
75 //
76 // If Stop() is ever called, this object will transition to "Stopped" state.
77 // Pipeline::Stop() is never called from withing PipelineImpl. It's |client_|'s
78 // responsibility to call stop when appropriate.
79 //
80 // TODO(sandersd): It should be possible to pass through Suspended when going
81 // from InitDemuxer to InitRenderer, thereby eliminating the Resuming state.
82 // Some annoying differences between the two paths need to be removed first.
83 class MEDIA_EXPORT PipelineImpl : public Pipeline {
84  public:
85   // Constructs a pipeline that will execute media tasks on |media_task_runner|.
86   // |create_renderer_cb|: to create renderers when starting and resuming.
87   PipelineImpl(scoped_refptr<base::SequencedTaskRunner> media_task_runner,
88                scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
89                CreateRendererCB create_renderer_cb,
90 #if defined(TIZEN_VIDEO_HOLE)
91                bool is_video_hole,
92 #endif
93                MediaLog* media_log);
94
95   PipelineImpl(const PipelineImpl&) = delete;
96   PipelineImpl& operator=(const PipelineImpl&) = delete;
97
98   ~PipelineImpl() override;
99
100   // Pipeline implementation.
101   void Start(StartType start_type,
102              Demuxer* demuxer,
103              Client* client,
104              PipelineStatusCallback seek_cb) override;
105   void Stop() override;
106   void Seek(base::TimeDelta time, PipelineStatusCallback seek_cb) override;
107   void Suspend(PipelineStatusCallback suspend_cb) override;
108   void Resume(base::TimeDelta time, PipelineStatusCallback seek_cb) override;
109   bool IsRunning() const override;
110   bool IsSuspended() const override;
111   double GetPlaybackRate() const override;
112   void SetPlaybackRate(double playback_rate) override;
113   float GetVolume() const override;
114   void SetVolume(float volume) override;
115   void SetLatencyHint(absl::optional<base::TimeDelta> latency_hint) override;
116   void SetPreservesPitch(bool preserves_pitch) override;
117   void SetWasPlayedWithUserActivation(
118       bool was_played_with_user_activation) override;
119   base::TimeDelta GetMediaTime() const override;
120   Ranges<base::TimeDelta> GetBufferedTimeRanges() const override;
121   base::TimeDelta GetMediaDuration() const override;
122   bool DidLoadingProgress() override;
123   PipelineStatistics GetStatistics() const override;
124   void SetCdm(CdmContext* cdm_context, CdmAttachedCB cdm_attached_cb) override;
125
126 #if defined(TIZEN_MULTIMEDIA)
127   void ToggleFullscreenMode(bool is_fullscreen,
128                             ToggledFullscreenCB cb) override;
129 #endif
130 #if defined(TIZEN_VIDEO_HOLE)
131   void SetMediaGeometry(const gfx::RectF rect_f) override;
132 #endif
133
134   // |enabled_track_ids| contains track ids of enabled audio tracks.
135   void OnEnabledAudioTracksChanged(
136       const std::vector<MediaTrack::Id>& enabled_track_ids,
137       base::OnceClosure change_completed_cb) override;
138
139   // |selected_track_id| is either empty, which means no video track is
140   // selected, or contains the selected video track id.
141   void OnSelectedVideoTrackChanged(
142       absl::optional<MediaTrack::Id> selected_track_id,
143       base::OnceClosure change_completed_cb) override;
144
145   void OnExternalVideoFrameRequest() override;
146
147  private:
148   friend class MediaLog;
149   class RendererWrapper;
150
151   // Pipeline states, as described above.
152   // TODO(alokp): Move this to RendererWrapper after removing the references
153   // from MediaLog.
154   enum State {
155     kCreated,
156     kStarting,
157     kSeeking,
158     kPlaying,
159     kStopping,
160     kStopped,
161     kSuspending,
162     kSuspended,
163     kResuming,
164   };
165   static const char* GetStateString(State state);
166
167   // Create a Renderer asynchronously. Must be called on the main task runner
168   // and the callback will be called on the main task runner as well.
169   void AsyncCreateRenderer(absl::optional<RendererType> renderer_type,
170                            RendererCreatedCB renderer_created_cb);
171
172   // Notifications from RendererWrapper.
173   void OnError(PipelineStatus error);
174   void OnFallback(PipelineStatus fallback);
175   void OnEnded();
176   void OnMetadata(const PipelineMetadata& metadata);
177 #if BUILDFLAG(IS_TIZEN_TV)
178   void SetContentMimeType(const std::string& mime_type) override;
179   void AudioTracksCountChanged(unsigned count) override;
180   void NotifyTrackInfoToBrowser(int active_track_id);
181   void SetParentalRatingResult(bool is_pass) override;
182   void AddTrackInfo(media::MediaTrackInfo trackinfo);
183   void SetActiveTextTrack(int id, bool is_in_band) override;
184   void SetActiveAudioTrack(int index) override;
185   void SetActiveVideoTrack(int index) override;
186   void SetPreferTextLanguage(const std::string& lang) override;
187   double GetStartDate() override;
188   void DestroyPlayerSync(base::OnceClosure cb) override;
189 #endif
190   void OnBufferingStateChange(BufferingState state,
191                               BufferingStateChangeReason reason);
192   void OnDurationChange(base::TimeDelta duration);
193   void OnWaiting(WaitingReason reason);
194   void OnAudioConfigChange(const AudioDecoderConfig& config);
195   void OnVideoConfigChange(const VideoDecoderConfig& config);
196   void OnVideoNaturalSizeChange(const gfx::Size& size);
197   void OnVideoOpacityChange(bool opaque);
198   void OnVideoAverageKeyframeDistanceUpdate();
199   void OnAudioPipelineInfoChange(const AudioPipelineInfo& info);
200   void OnVideoPipelineInfoChange(const VideoPipelineInfo& info);
201   void OnRemotePlayStateChange(MediaStatus::State state);
202   void OnVideoFrameRateChange(absl::optional<int> fps);
203 #if defined(TIZEN_MULTIMEDIA)
204   void OnRequestSuspend(bool resource_conflicted);
205   void OnSeekableTimeChange(base::TimeDelta min_time,
206                             base::TimeDelta max_time,
207                             bool is_live);
208   void OnLivePlaybackComplete();
209 #endif
210
211   // Task completion callbacks from RendererWrapper.
212   void OnSeekDone(bool is_suspended);
213   void OnSuspendDone();
214
215   // Parameters passed in the constructor.
216   const scoped_refptr<base::SequencedTaskRunner> media_task_runner_;
217   CreateRendererCB create_renderer_cb_;
218   const raw_ptr<MediaLog> media_log_;
219
220   // Pipeline client. Valid only while the pipeline is running.
221   raw_ptr<Client> client_;
222
223   // RendererWrapper instance that runs on the media thread.
224   std::unique_ptr<RendererWrapper> renderer_wrapper_;
225
226   // Temporary callback used for Start(), Seek(), and Resume().
227   PipelineStatusCallback seek_cb_;
228
229   // Temporary callback used for Suspend().
230   PipelineStatusCallback suspend_cb_;
231
232   // Current playback rate (>= 0.0). This value is set immediately via
233   // SetPlaybackRate() and a task is dispatched on the task runner to notify
234   // the filters.
235   double playback_rate_;
236
237   // Current volume level (from 0.0f to 1.0f). This value is set immediately
238   // via SetVolume() and a task is dispatched on the task runner to notify the
239   // filters.
240   float volume_;
241
242   // Current duration as reported by Demuxer.
243   base::TimeDelta duration_;
244
245   // Set by GetMediaTime(), used to prevent the current media time value as
246   // reported to JavaScript from going backwards in time.
247   mutable base::TimeDelta last_media_time_;
248
249   // Set by Seek(), used in place of asking the renderer for current media time
250   // while a seek is pending. Renderer's time cannot be trusted until the seek
251   // has completed.
252   base::TimeDelta seek_time_;
253
254   // Cached suspension state for the RendererWrapper.
255   bool is_suspended_;
256
257   // 'external_video_frame_request_signaled_' tracks whether we've called
258   // OnExternalVideoFrameRequest on the current renderer. Calls which may
259   // create a new renderer in the RendererWrapper (Start, Resume, SetCdm) will
260   // reset this member. There is no guarantee to the client that
261   // OnExternalVideoFrameRequest will be called only once.
262   bool external_video_frame_request_signaled_ = false;
263
264   base::ThreadChecker thread_checker_;
265   base::WeakPtrFactory<PipelineImpl> weak_factory_{this};
266 };
267
268 }  // namespace media
269
270 #endif  // MEDIA_BASE_PIPELINE_IMPL_H_