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