OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MEDIA_BASE_PIPELINE_H_ | 5 #ifndef MEDIA_BASE_PIPELINE_H_ |
6 #define MEDIA_BASE_PIPELINE_H_ | 6 #define MEDIA_BASE_PIPELINE_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/gtest_prod_util.h" | 10 #include "base/gtest_prod_util.h" |
(...skipping 16 matching lines...) Expand all Loading... |
27 | 27 |
28 namespace media { | 28 namespace media { |
29 | 29 |
30 class Clock; | 30 class Clock; |
31 class FilterCollection; | 31 class FilterCollection; |
32 class MediaLog; | 32 class MediaLog; |
33 class TextRenderer; | 33 class TextRenderer; |
34 class TextTrackConfig; | 34 class TextTrackConfig; |
35 class VideoRenderer; | 35 class VideoRenderer; |
36 | 36 |
| 37 // Metadata describing a pipeline once it has been initialized. |
| 38 struct PipelineMetadata { |
| 39 PipelineMetadata() : has_audio(false), has_video(false) {} |
| 40 |
| 41 bool has_audio; |
| 42 bool has_video; |
| 43 gfx::Size natural_size; |
| 44 }; |
| 45 |
| 46 typedef base::Callback<void(PipelineMetadata)> PipelineMetadataCB; |
| 47 |
37 // Pipeline runs the media pipeline. Filters are created and called on the | 48 // Pipeline runs the media pipeline. Filters are created and called on the |
38 // task runner injected into this object. Pipeline works like a state | 49 // task runner injected into this object. Pipeline works like a state |
39 // machine to perform asynchronous initialization, pausing, seeking and playing. | 50 // machine to perform asynchronous initialization, pausing, seeking and playing. |
40 // | 51 // |
41 // Here's a state diagram that describes the lifetime of this object. | 52 // Here's a state diagram that describes the lifetime of this object. |
42 // | 53 // |
43 // [ *Created ] [ Any State ] | 54 // [ *Created ] [ Any State ] |
44 // | Start() | Stop() / SetError() | 55 // | Start() | Stop() / SetError() |
45 // V V | 56 // V V |
46 // [ InitXXX (for each filter) ] [ Stopping ] | 57 // [ InitXXX (for each filter) ] [ Stopping ] |
(...skipping 13 matching lines...) Expand all Loading... |
60 // completed, we are implicitly in a "Paused" state. At that point we simulate | 71 // completed, we are implicitly in a "Paused" state. At that point we simulate |
61 // a Seek() to the beginning of the media to give filters a chance to preroll. | 72 // a Seek() to the beginning of the media to give filters a chance to preroll. |
62 // From then on the normal Seek() transitions are carried out and we start | 73 // From then on the normal Seek() transitions are carried out and we start |
63 // playing the media. | 74 // playing the media. |
64 // | 75 // |
65 // If any error ever happens, this object will transition to the "Error" state | 76 // If any error ever happens, this object will transition to the "Error" state |
66 // from any state. If Stop() is ever called, this object will transition to | 77 // from any state. If Stop() is ever called, this object will transition to |
67 // "Stopped" state. | 78 // "Stopped" state. |
68 class MEDIA_EXPORT Pipeline : public DemuxerHost { | 79 class MEDIA_EXPORT Pipeline : public DemuxerHost { |
69 public: | 80 public: |
70 // Buffering states the pipeline transitions between during playback. | |
71 // kHaveMetadata: | |
72 // Indicates that the following things are known: | |
73 // content duration, container video size, start time, and whether the | |
74 // content has audio and/or video in supported formats. | |
75 // kPrerollCompleted: | |
76 // All renderers have buffered enough data to satisfy preroll and are ready | |
77 // to start playback. | |
78 enum BufferingState { | |
79 kHaveMetadata, | |
80 kPrerollCompleted, | |
81 }; | |
82 | |
83 typedef base::Callback<void(BufferingState)> BufferingStateCB; | |
84 | |
85 // Constructs a media pipeline that will execute on |task_runner|. | 81 // Constructs a media pipeline that will execute on |task_runner|. |
86 Pipeline(const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, | 82 Pipeline(const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, |
87 MediaLog* media_log); | 83 MediaLog* media_log); |
88 virtual ~Pipeline(); | 84 virtual ~Pipeline(); |
89 | 85 |
90 // Build a pipeline to using the given filter collection to construct a filter | 86 // Build a pipeline to using the given filter collection to construct a filter |
91 // chain, executing |seek_cb| when the initial seek/preroll has completed. | 87 // chain, executing |seek_cb| when the initial seek/preroll has completed. |
92 // | 88 // |
93 // |filter_collection| must be a complete collection containing a demuxer, | 89 // |filter_collection| must be a complete collection containing a demuxer, |
94 // audio/video decoders, and audio/video renderers. Failing to do so will | 90 // audio/video decoders, and audio/video renderers. Failing to do so will |
95 // result in a crash. | 91 // result in a crash. |
96 // | 92 // |
97 // The following permanent callbacks will be executed as follows up until | 93 // The following permanent callbacks will be executed as follows up until |
98 // Stop() has completed: | 94 // Stop() has completed: |
99 // |ended_cb| will be executed whenever the media reaches the end. | 95 // |ended_cb| will be executed whenever the media reaches the end. |
100 // |error_cb| will be executed whenever an error occurs but hasn't | 96 // |error_cb| will be executed whenever an error occurs but hasn't been |
101 // been reported already through another callback. | 97 // reported already through another callback. |
102 // |buffering_state_cb| Optional callback that will be executed whenever the | 98 // |metadata_cb| will be executed when the content duration, container video |
103 // pipeline's buffering state changes. | 99 // size, start time, and whether the content has audio and/or |
104 // |duration_change_cb| Optional callback that will be executed whenever the | 100 // video in supported formats are known. |
| 101 // |preroll_completed_cb| will be executed when all renderers have buffered |
| 102 // enough data to satisfy preroll and are ready to |
| 103 // start playback. |
| 104 // |duration_change_cb| optional callback that will be executed whenever the |
105 // presentation duration changes. | 105 // presentation duration changes. |
106 // It is an error to call this method after the pipeline has already started. | 106 // It is an error to call this method after the pipeline has already started. |
107 void Start(scoped_ptr<FilterCollection> filter_collection, | 107 void Start(scoped_ptr<FilterCollection> filter_collection, |
108 const base::Closure& ended_cb, | 108 const base::Closure& ended_cb, |
109 const PipelineStatusCB& error_cb, | 109 const PipelineStatusCB& error_cb, |
110 const PipelineStatusCB& seek_cb, | 110 const PipelineStatusCB& seek_cb, |
111 const BufferingStateCB& buffering_state_cb, | 111 const PipelineMetadataCB& metadata_cb, |
| 112 const base::Closure& preroll_completed_cb, |
112 const base::Closure& duration_change_cb); | 113 const base::Closure& duration_change_cb); |
113 | 114 |
114 // Asynchronously stops the pipeline, executing |stop_cb| when the pipeline | 115 // Asynchronously stops the pipeline, executing |stop_cb| when the pipeline |
115 // teardown has completed. | 116 // teardown has completed. |
116 // | 117 // |
117 // Stop() must complete before destroying the pipeline. It it permissible to | 118 // Stop() must complete before destroying the pipeline. It it permissible to |
118 // call Stop() at any point during the lifetime of the pipeline. | 119 // call Stop() at any point during the lifetime of the pipeline. |
119 // | 120 // |
120 // It is safe to delete the pipeline during the execution of |stop_cb|. | 121 // It is safe to delete the pipeline during the execution of |stop_cb|. |
121 void Stop(const base::Closure& stop_cb); | 122 void Stop(const base::Closure& stop_cb); |
122 | 123 |
123 // Attempt to seek to the position specified by time. |seek_cb| will be | 124 // Attempt to seek to the position specified by time. |seek_cb| will be |
124 // executed when the all filters in the pipeline have processed the seek. | 125 // executed when the all filters in the pipeline have processed the seek. |
125 // | 126 // |
126 // Clients are expected to call GetMediaTime() to check whether the seek | 127 // Clients are expected to call GetMediaTime() to check whether the seek |
127 // succeeded. | 128 // succeeded. |
128 // | 129 // |
129 // It is an error to call this method if the pipeline has not started. | 130 // It is an error to call this method if the pipeline has not started. |
130 void Seek(base::TimeDelta time, const PipelineStatusCB& seek_cb); | 131 void Seek(base::TimeDelta time, const PipelineStatusCB& seek_cb); |
131 | 132 |
132 // Returns true if the pipeline has been started via Start(). If IsRunning() | 133 // Returns true if the pipeline has been started via Start(). If IsRunning() |
133 // returns true, it is expected that Stop() will be called before destroying | 134 // returns true, it is expected that Stop() will be called before destroying |
134 // the pipeline. | 135 // the pipeline. |
135 bool IsRunning() const; | 136 bool IsRunning() const; |
136 | 137 |
137 // Returns true if the media has audio. | |
138 bool HasAudio() const; | |
139 | |
140 // Returns true if the media has video. | |
141 bool HasVideo() const; | |
142 | |
143 // Gets the current playback rate of the pipeline. When the pipeline is | 138 // Gets the current playback rate of the pipeline. When the pipeline is |
144 // started, the playback rate will be 0.0f. A rate of 1.0f indicates | 139 // started, the playback rate will be 0.0f. A rate of 1.0f indicates |
145 // that the pipeline is rendering the media at the standard rate. Valid | 140 // that the pipeline is rendering the media at the standard rate. Valid |
146 // values for playback rate are >= 0.0f. | 141 // values for playback rate are >= 0.0f. |
147 float GetPlaybackRate() const; | 142 float GetPlaybackRate() const; |
148 | 143 |
149 // Attempt to adjust the playback rate. Setting a playback rate of 0.0f pauses | 144 // Attempt to adjust the playback rate. Setting a playback rate of 0.0f pauses |
150 // all rendering of the media. A rate of 1.0f indicates a normal playback | 145 // all rendering of the media. A rate of 1.0f indicates a normal playback |
151 // rate. Values for the playback rate must be greater than or equal to 0.0f. | 146 // rate. Values for the playback rate must be greater than or equal to 0.0f. |
152 // | 147 // |
(...skipping 18 matching lines...) Expand all Loading... |
171 Ranges<base::TimeDelta> GetBufferedTimeRanges(); | 166 Ranges<base::TimeDelta> GetBufferedTimeRanges(); |
172 | 167 |
173 // Get the duration of the media in microseconds. If the duration has not | 168 // Get the duration of the media in microseconds. If the duration has not |
174 // been determined yet, then returns 0. | 169 // been determined yet, then returns 0. |
175 base::TimeDelta GetMediaDuration() const; | 170 base::TimeDelta GetMediaDuration() const; |
176 | 171 |
177 // Get the total size of the media file. If the size has not yet been | 172 // Get the total size of the media file. If the size has not yet been |
178 // determined or can not be determined, this value is 0. | 173 // determined or can not be determined, this value is 0. |
179 int64 GetTotalBytes() const; | 174 int64 GetTotalBytes() const; |
180 | 175 |
181 // Get the video's initial natural size as reported by the container. Note | |
182 // that the natural size can change during playback. | |
183 gfx::Size GetInitialNaturalSize() const; | |
184 | |
185 // Return true if loading progress has been made since the last time this | 176 // Return true if loading progress has been made since the last time this |
186 // method was called. | 177 // method was called. |
187 bool DidLoadingProgress() const; | 178 bool DidLoadingProgress() const; |
188 | 179 |
189 // Gets the current pipeline statistics. | 180 // Gets the current pipeline statistics. |
190 PipelineStatistics GetStatistics() const; | 181 PipelineStatistics GetStatistics() const; |
191 | 182 |
192 void SetClockForTesting(Clock* clock); | 183 void SetClockForTesting(Clock* clock); |
193 void SetErrorForTesting(PipelineStatus status); | 184 void SetErrorForTesting(PipelineStatus status); |
194 | 185 |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
255 | 246 |
256 // Callback executed by audio renderer to update clock time. | 247 // Callback executed by audio renderer to update clock time. |
257 void OnAudioTimeUpdate(base::TimeDelta time, base::TimeDelta max_time); | 248 void OnAudioTimeUpdate(base::TimeDelta time, base::TimeDelta max_time); |
258 | 249 |
259 // Callback executed by video renderer to update clock time. | 250 // Callback executed by video renderer to update clock time. |
260 void OnVideoTimeUpdate(base::TimeDelta max_time); | 251 void OnVideoTimeUpdate(base::TimeDelta max_time); |
261 | 252 |
262 // The following "task" methods correspond to the public methods, but these | 253 // The following "task" methods correspond to the public methods, but these |
263 // methods are run as the result of posting a task to the Pipeline's | 254 // methods are run as the result of posting a task to the Pipeline's |
264 // task runner. | 255 // task runner. |
265 void StartTask(scoped_ptr<FilterCollection> filter_collection, | 256 void StartTask(); |
266 const base::Closure& ended_cb, | |
267 const PipelineStatusCB& error_cb, | |
268 const PipelineStatusCB& seek_cb, | |
269 const BufferingStateCB& buffering_state_cb, | |
270 const base::Closure& duration_change_cb); | |
271 | 257 |
272 // Stops and destroys all filters, placing the pipeline in the kStopped state. | 258 // Stops and destroys all filters, placing the pipeline in the kStopped state. |
273 void StopTask(const base::Closure& stop_cb); | 259 void StopTask(const base::Closure& stop_cb); |
274 | 260 |
275 // Carries out stopping and destroying all filters, placing the pipeline in | 261 // Carries out stopping and destroying all filters, placing the pipeline in |
276 // the kStopped state. | 262 // the kStopped state. |
277 void ErrorChangedTask(PipelineStatus error); | 263 void ErrorChangedTask(PipelineStatus error); |
278 | 264 |
279 // Carries out notifying filters that the playback rate has changed. | 265 // Carries out notifying filters that the playback rate has changed. |
280 void PlaybackRateChangedTask(float playback_rate); | 266 void PlaybackRateChangedTask(float playback_rate); |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
363 Ranges<int64> buffered_byte_ranges_; | 349 Ranges<int64> buffered_byte_ranges_; |
364 Ranges<base::TimeDelta> buffered_time_ranges_; | 350 Ranges<base::TimeDelta> buffered_time_ranges_; |
365 | 351 |
366 // True when AddBufferedByteRange() has been called more recently than | 352 // True when AddBufferedByteRange() has been called more recently than |
367 // DidLoadingProgress(). | 353 // DidLoadingProgress(). |
368 mutable bool did_loading_progress_; | 354 mutable bool did_loading_progress_; |
369 | 355 |
370 // Total size of the media. Set by filters. | 356 // Total size of the media. Set by filters. |
371 int64 total_bytes_; | 357 int64 total_bytes_; |
372 | 358 |
373 // The initial natural size of the video as reported by the container. | |
374 gfx::Size initial_natural_size_; | |
375 | |
376 // Current volume level (from 0.0f to 1.0f). This value is set immediately | 359 // Current volume level (from 0.0f to 1.0f). This value is set immediately |
377 // via SetVolume() and a task is dispatched on the task runner to notify the | 360 // via SetVolume() and a task is dispatched on the task runner to notify the |
378 // filters. | 361 // filters. |
379 float volume_; | 362 float volume_; |
380 | 363 |
381 // Current playback rate (>= 0.0f). This value is set immediately via | 364 // Current playback rate (>= 0.0f). This value is set immediately via |
382 // SetPlaybackRate() and a task is dispatched on the task runner to notify | 365 // SetPlaybackRate() and a task is dispatched on the task runner to notify |
383 // the filters. | 366 // the filters. |
384 float playback_rate_; | 367 float playback_rate_; |
385 | 368 |
386 // base::TickClock used by |clock_|. | 369 // base::TickClock used by |clock_|. |
387 base::DefaultTickClock default_tick_clock_; | 370 base::DefaultTickClock default_tick_clock_; |
388 | 371 |
389 // Reference clock. Keeps track of current playback time. Uses system | 372 // Reference clock. Keeps track of current playback time. Uses system |
390 // clock and linear interpolation, but can have its time manually set | 373 // clock and linear interpolation, but can have its time manually set |
391 // by filters. | 374 // by filters. |
392 scoped_ptr<Clock> clock_; | 375 scoped_ptr<Clock> clock_; |
393 | 376 |
394 // If this value is set to true, then |clock_| is paused and we are waiting | 377 // If this value is set to true, then |clock_| is paused and we are waiting |
395 // for an update of the clock greater than or equal to the elapsed time to | 378 // for an update of the clock greater than or equal to the elapsed time to |
396 // start the clock. | 379 // start the clock. |
397 bool waiting_for_clock_update_; | 380 bool waiting_for_clock_update_; |
398 | 381 |
399 // Status of the pipeline. Initialized to PIPELINE_OK which indicates that | 382 // Status of the pipeline. Initialized to PIPELINE_OK which indicates that |
400 // the pipeline is operating correctly. Any other value indicates that the | 383 // the pipeline is operating correctly. Any other value indicates that the |
401 // pipeline is stopped or is stopping. Clients can call the Stop() method to | 384 // pipeline is stopped or is stopping. Clients can call the Stop() method to |
402 // reset the pipeline state, and restore this to PIPELINE_OK. | 385 // reset the pipeline state, and restore this to PIPELINE_OK. |
403 PipelineStatus status_; | 386 PipelineStatus status_; |
404 | 387 |
405 // Whether the media contains rendered audio or video streams. | |
406 // TODO(fischman,scherkus): replace these with checks for | |
407 // {audio,video}_decoder_ once extraction of {Audio,Video}Decoder from the | |
408 // Filter heirarchy is done. | |
409 bool has_audio_; | |
410 bool has_video_; | |
411 | |
412 // The following data members are only accessed by tasks posted to | 388 // The following data members are only accessed by tasks posted to |
413 // |task_runner_|. | 389 // |task_runner_|. |
414 | 390 |
415 // Member that tracks the current state. | 391 // Member that tracks the current state. |
416 State state_; | 392 State state_; |
417 | 393 |
418 // Whether we've received the audio/video/text ended events. | 394 // Whether we've received the audio/video/text ended events. |
419 bool audio_ended_; | 395 bool audio_ended_; |
420 bool video_ended_; | 396 bool video_ended_; |
421 bool text_ended_; | 397 bool text_ended_; |
422 | 398 |
423 // Set to true in DisableAudioRendererTask(). | 399 // Set to true in DisableAudioRendererTask(). |
424 bool audio_disabled_; | 400 bool audio_disabled_; |
425 | 401 |
426 // Temporary callback used for Start() and Seek(). | 402 // Temporary callback used for Start() and Seek(). |
427 PipelineStatusCB seek_cb_; | 403 PipelineStatusCB seek_cb_; |
428 | 404 |
429 // Temporary callback used for Stop(). | 405 // Temporary callback used for Stop(). |
430 base::Closure stop_cb_; | 406 base::Closure stop_cb_; |
431 | 407 |
432 // Permanent callbacks passed in via Start(). | 408 // Permanent callbacks passed in via Start(). |
433 base::Closure ended_cb_; | 409 base::Closure ended_cb_; |
434 PipelineStatusCB error_cb_; | 410 PipelineStatusCB error_cb_; |
435 BufferingStateCB buffering_state_cb_; | 411 PipelineMetadataCB metadata_cb_; |
| 412 base::Closure preroll_completed_cb_; |
436 base::Closure duration_change_cb_; | 413 base::Closure duration_change_cb_; |
437 | 414 |
438 // Contains the demuxer and renderers to use when initializing. | 415 // Contains the demuxer and renderers to use when initializing. |
439 scoped_ptr<FilterCollection> filter_collection_; | 416 scoped_ptr<FilterCollection> filter_collection_; |
440 | 417 |
441 // Holds the initialized demuxer. Used for seeking. Owned by client. | 418 // Holds the initialized demuxer. Used for seeking. Owned by client. |
442 Demuxer* demuxer_; | 419 Demuxer* demuxer_; |
443 | 420 |
444 // Holds the initialized renderers. Used for setting the volume, | 421 // Holds the initialized renderers. Used for setting the volume, |
445 // playback rate, and determining when playback has finished. | 422 // playback rate, and determining when playback has finished. |
(...skipping 10 matching lines...) Expand all Loading... |
456 scoped_ptr<SerialRunner> pending_callbacks_; | 433 scoped_ptr<SerialRunner> pending_callbacks_; |
457 | 434 |
458 base::ThreadChecker thread_checker_; | 435 base::ThreadChecker thread_checker_; |
459 | 436 |
460 DISALLOW_COPY_AND_ASSIGN(Pipeline); | 437 DISALLOW_COPY_AND_ASSIGN(Pipeline); |
461 }; | 438 }; |
462 | 439 |
463 } // namespace media | 440 } // namespace media |
464 | 441 |
465 #endif // MEDIA_BASE_PIPELINE_H_ | 442 #endif // MEDIA_BASE_PIPELINE_H_ |
OLD | NEW |