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> | |
9 | |
10 #include "base/gtest_prod_util.h" | 8 #include "base/gtest_prod_util.h" |
11 #include "base/synchronization/condition_variable.h" | |
12 #include "base/synchronization/lock.h" | 9 #include "base/synchronization/lock.h" |
13 #include "base/threading/thread_checker.h" | 10 #include "base/threading/thread_checker.h" |
14 #include "base/time/default_tick_clock.h" | 11 #include "base/time/default_tick_clock.h" |
15 #include "media/base/audio_renderer.h" | |
16 #include "media/base/buffering_state.h" | 12 #include "media/base/buffering_state.h" |
17 #include "media/base/demuxer.h" | 13 #include "media/base/demuxer.h" |
18 #include "media/base/media_export.h" | 14 #include "media/base/media_export.h" |
19 #include "media/base/pipeline_status.h" | 15 #include "media/base/pipeline_status.h" |
20 #include "media/base/ranges.h" | 16 #include "media/base/ranges.h" |
21 #include "media/base/serial_runner.h" | 17 #include "media/base/serial_runner.h" |
22 #include "media/base/video_rotation.h" | 18 #include "media/base/video_rotation.h" |
23 #include "ui/gfx/size.h" | 19 #include "ui/gfx/size.h" |
24 | 20 |
25 namespace base { | 21 namespace base { |
26 class SingleThreadTaskRunner; | 22 class SingleThreadTaskRunner; |
27 class TimeDelta; | 23 class TimeDelta; |
28 } | 24 } |
29 | 25 |
30 namespace media { | 26 namespace media { |
31 | 27 |
32 class FilterCollection; | 28 class FilterCollection; |
33 class MediaLog; | 29 class MediaLog; |
| 30 class Renderer; |
34 class TextRenderer; | 31 class TextRenderer; |
35 class TextTrackConfig; | 32 class TextTrackConfig; |
36 class TimeDeltaInterpolator; | 33 class TimeDeltaInterpolator; |
37 class TimeSource; | |
38 class VideoRenderer; | |
39 | 34 |
40 // Metadata describing a pipeline once it has been initialized. | 35 // Metadata describing a pipeline once it has been initialized. |
41 struct PipelineMetadata { | 36 struct PipelineMetadata { |
42 PipelineMetadata() | 37 PipelineMetadata() |
43 : has_audio(false), has_video(false), video_rotation(VIDEO_ROTATION_0) {} | 38 : has_audio(false), has_video(false), video_rotation(VIDEO_ROTATION_0) {} |
44 | 39 |
45 bool has_audio; | 40 bool has_audio; |
46 bool has_video; | 41 bool has_video; |
47 gfx::Size natural_size; | 42 gfx::Size natural_size; |
48 VideoRotation video_rotation; | 43 VideoRotation video_rotation; |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 // been determined yet, then returns 0. | 165 // been determined yet, then returns 0. |
171 base::TimeDelta GetMediaDuration() const; | 166 base::TimeDelta GetMediaDuration() const; |
172 | 167 |
173 // Return true if loading progress has been made since the last time this | 168 // Return true if loading progress has been made since the last time this |
174 // method was called. | 169 // method was called. |
175 bool DidLoadingProgress(); | 170 bool DidLoadingProgress(); |
176 | 171 |
177 // Gets the current pipeline statistics. | 172 // Gets the current pipeline statistics. |
178 PipelineStatistics GetStatistics() const; | 173 PipelineStatistics GetStatistics() const; |
179 | 174 |
180 void set_underflow_disabled_for_testing(bool disabled) { | 175 void DisableUnderflowForTesting(); |
181 underflow_disabled_for_testing_ = disabled; | |
182 } | |
183 void SetTimeDeltaInterpolatorForTesting(TimeDeltaInterpolator* interpolator); | 176 void SetTimeDeltaInterpolatorForTesting(TimeDeltaInterpolator* interpolator); |
184 void SetErrorForTesting(PipelineStatus status); | 177 void SetErrorForTesting(PipelineStatus status); |
185 | 178 |
186 private: | 179 private: |
187 FRIEND_TEST_ALL_PREFIXES(PipelineTest, GetBufferedTimeRanges); | 180 FRIEND_TEST_ALL_PREFIXES(PipelineTest, GetBufferedTimeRanges); |
188 FRIEND_TEST_ALL_PREFIXES(PipelineTest, EndedCallback); | 181 FRIEND_TEST_ALL_PREFIXES(PipelineTest, EndedCallback); |
189 FRIEND_TEST_ALL_PREFIXES(PipelineTest, AudioStreamShorterThanVideo); | 182 FRIEND_TEST_ALL_PREFIXES(PipelineTest, AudioStreamShorterThanVideo); |
190 friend class MediaLog; | 183 friend class MediaLog; |
191 | 184 |
192 // Pipeline states, as described above. | 185 // Pipeline states, as described above. |
193 enum State { | 186 enum State { |
194 kCreated, | 187 kCreated, |
195 kInitDemuxer, | 188 kInitDemuxer, |
196 kInitAudioRenderer, | 189 kInitRenderer, |
197 kInitVideoRenderer, | |
198 kSeeking, | 190 kSeeking, |
199 kPlaying, | 191 kPlaying, |
200 kStopping, | 192 kStopping, |
201 kStopped, | 193 kStopped, |
202 }; | 194 }; |
203 | 195 |
204 // Updates |state_|. All state transitions should use this call. | 196 // Updates |state_|. All state transitions should use this call. |
205 void SetState(State next_state); | 197 void SetState(State next_state); |
206 | 198 |
207 static const char* GetStateString(State state); | 199 static const char* GetStateString(State state); |
(...skipping 11 matching lines...) Expand all Loading... |
219 virtual void AddTextStream(DemuxerStream* text_stream, | 211 virtual void AddTextStream(DemuxerStream* text_stream, |
220 const TextTrackConfig& config) OVERRIDE; | 212 const TextTrackConfig& config) OVERRIDE; |
221 virtual void RemoveTextStream(DemuxerStream* text_stream) OVERRIDE; | 213 virtual void RemoveTextStream(DemuxerStream* text_stream) OVERRIDE; |
222 | 214 |
223 // Initiates teardown sequence in response to a runtime error. | 215 // Initiates teardown sequence in response to a runtime error. |
224 // | 216 // |
225 // Safe to call from any thread. | 217 // Safe to call from any thread. |
226 void SetError(PipelineStatus error); | 218 void SetError(PipelineStatus error); |
227 | 219 |
228 // Callbacks executed when a renderer has ended. | 220 // Callbacks executed when a renderer has ended. |
229 void OnAudioRendererEnded(); | 221 void OnRendererEnded(); |
230 void OnVideoRendererEnded(); | |
231 void OnTextRendererEnded(); | 222 void OnTextRendererEnded(); |
232 | 223 |
233 // Callback executed by filters to update statistics. | 224 // Callback executed by filters to update statistics. |
234 void OnUpdateStatistics(const PipelineStatistics& stats); | 225 void OnUpdateStatistics(const PipelineStatistics& stats); |
235 | 226 |
236 // Callback executed by audio renderer to update clock time. | |
237 void OnAudioTimeUpdate(base::TimeDelta time, base::TimeDelta max_time); | |
238 | |
239 // Callback executed by video renderer to update clock time. | |
240 void OnVideoTimeUpdate(base::TimeDelta max_time); | |
241 | |
242 // The following "task" methods correspond to the public methods, but these | 227 // The following "task" methods correspond to the public methods, but these |
243 // methods are run as the result of posting a task to the Pipeline's | 228 // methods are run as the result of posting a task to the Pipeline's |
244 // task runner. | 229 // task runner. |
245 void StartTask(); | 230 void StartTask(); |
246 | 231 |
247 // Stops and destroys all filters, placing the pipeline in the kStopped state. | 232 // Stops and destroys all filters, placing the pipeline in the kStopped state. |
248 void StopTask(const base::Closure& stop_cb); | 233 void StopTask(const base::Closure& stop_cb); |
249 | 234 |
250 // Carries out stopping and destroying all filters, placing the pipeline in | 235 // Carries out stopping and destroying all filters, placing the pipeline in |
251 // the kStopped state. | 236 // the kStopped state. |
252 void ErrorChangedTask(PipelineStatus error); | 237 void ErrorChangedTask(PipelineStatus error); |
253 | 238 |
254 // Carries out notifying filters that the playback rate has changed. | 239 // Carries out notifying filters that the playback rate has changed. |
255 void PlaybackRateChangedTask(float playback_rate); | 240 void PlaybackRateChangedTask(float playback_rate); |
256 | 241 |
257 // Carries out notifying filters that the volume has changed. | 242 // Carries out notifying filters that the volume has changed. |
258 void VolumeChangedTask(float volume); | 243 void VolumeChangedTask(float volume); |
259 | 244 |
260 // Carries out notifying filters that we are seeking to a new timestamp. | 245 // Carries out notifying filters that we are seeking to a new timestamp. |
261 void SeekTask(base::TimeDelta time, const PipelineStatusCB& seek_cb); | 246 void SeekTask(base::TimeDelta time, const PipelineStatusCB& seek_cb); |
262 | 247 |
263 // Handles audio/video/text ended logic and running |ended_cb_|. | 248 // Handles audio/video/text ended logic and running |ended_cb_|. |
264 void DoAudioRendererEnded(); | |
265 void DoVideoRendererEnded(); | |
266 void DoTextRendererEnded(); | |
267 void RunEndedCallbackIfNeeded(); | 249 void RunEndedCallbackIfNeeded(); |
268 | 250 |
269 // Carries out adding a new text stream to the text renderer. | 251 // Carries out adding a new text stream to the text renderer. |
270 void AddTextStreamTask(DemuxerStream* text_stream, | 252 void AddTextStreamTask(DemuxerStream* text_stream, |
271 const TextTrackConfig& config); | 253 const TextTrackConfig& config); |
272 | 254 |
273 // Carries out removing a text stream from the text renderer. | 255 // Carries out removing a text stream from the text renderer. |
274 void RemoveTextStreamTask(DemuxerStream* text_stream); | 256 void RemoveTextStreamTask(DemuxerStream* text_stream); |
275 | 257 |
276 // Kicks off initialization for each media object, executing |done_cb| with | 258 // Kicks off initialization for each media object, executing |done_cb| with |
277 // the result when completed. | 259 // the result when completed. |
278 void InitializeDemuxer(const PipelineStatusCB& done_cb); | 260 void InitializeDemuxer(const PipelineStatusCB& done_cb); |
279 void InitializeAudioRenderer(const PipelineStatusCB& done_cb); | 261 void InitializeRenderer(const PipelineStatusCB& done_cb); |
280 void InitializeVideoRenderer(const PipelineStatusCB& done_cb); | |
281 | 262 |
282 // Kicks off destroying filters. Called by StopTask() and ErrorChangedTask(). | 263 // Kicks off destroying filters. Called by StopTask() and ErrorChangedTask(). |
283 // When we start to tear down the pipeline, we will consider two cases: | 264 // When we start to tear down the pipeline, we will consider two cases: |
284 // 1. when pipeline has not been initialized, we will transit to stopping | 265 // 1. when pipeline has not been initialized, we will transit to stopping |
285 // state first. | 266 // state first. |
286 // 2. when pipeline has been initialized, we will first transit to pausing | 267 // 2. when pipeline has been initialized, we will first transit to pausing |
287 // => flushing => stopping => stopped state. | 268 // => flushing => stopping => stopped state. |
288 // This will remove the race condition during stop between filters. | 269 // This will remove the race condition during stop between filters. |
289 void TearDownPipeline(); | 270 void TearDownPipeline(); |
290 | 271 |
291 // Compute the time corresponding to a byte offset. | 272 // Compute the time corresponding to a byte offset. |
292 base::TimeDelta TimeForByteOffset_Locked(int64 byte_offset) const; | 273 base::TimeDelta TimeForByteOffset_Locked(int64 byte_offset) const; |
293 | 274 |
294 void OnStateTransition(PipelineStatus status); | 275 void OnStateTransition(PipelineStatus status); |
295 void StateTransitionTask(PipelineStatus status); | 276 void StateTransitionTask(PipelineStatus status); |
296 | 277 |
297 // Initiates an asynchronous pause-flush-seek-preroll call sequence | 278 // Initiates an asynchronous pause-flush-seek-preroll call sequence |
298 // executing |done_cb| with the final status when completed. | 279 // executing |done_cb| with the final status when completed. |
299 void DoSeek(base::TimeDelta seek_timestamp, const PipelineStatusCB& done_cb); | 280 void DoSeek(base::TimeDelta seek_timestamp, const PipelineStatusCB& done_cb); |
300 | 281 |
301 // Initiates an asynchronous pause-flush-stop call sequence executing | 282 // Initiates an asynchronous pause-flush-stop call sequence executing |
302 // |done_cb| when completed. | 283 // |done_cb| when completed. |
303 void DoStop(const PipelineStatusCB& done_cb); | 284 void DoStop(const PipelineStatusCB& done_cb); |
304 void OnStopCompleted(PipelineStatus status); | 285 void OnStopCompleted(PipelineStatus status); |
305 | 286 |
306 // Collection of callback methods and helpers for tracking changes in | 287 void BufferingStateChanged(BufferingState new_buffering_state); |
307 // buffering state and transition from paused/underflow states and playing | |
308 // states. | |
309 // | |
310 // While in the kPlaying state: | |
311 // - A waiting to non-waiting transition indicates preroll has completed | |
312 // and StartPlayback() should be called | |
313 // - A non-waiting to waiting transition indicates underflow has occurred | |
314 // and PausePlayback() should be called | |
315 void BufferingStateChanged(BufferingState* buffering_state, | |
316 BufferingState new_buffering_state); | |
317 bool WaitingForEnoughData() const; | |
318 void PausePlayback(); | |
319 void StartPlayback(); | |
320 | |
321 void PauseClockAndStopTicking_Locked(); | |
322 void StartClockIfWaitingForTimeUpdate_Locked(); | |
323 | 288 |
324 // Task runner used to execute pipeline tasks. | 289 // Task runner used to execute pipeline tasks. |
325 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 290 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
326 | 291 |
327 // MediaLog to which to log events. | 292 // MediaLog to which to log events. |
328 scoped_refptr<MediaLog> media_log_; | 293 scoped_refptr<MediaLog> media_log_; |
329 | 294 |
330 // Lock used to serialize access for the following data members. | 295 // Lock used to serialize access for the following data members. |
331 mutable base::Lock lock_; | 296 mutable base::Lock lock_; |
332 | 297 |
(...skipping 13 matching lines...) Expand all Loading... |
346 float volume_; | 311 float volume_; |
347 | 312 |
348 // Current playback rate (>= 0.0f). This value is set immediately via | 313 // Current playback rate (>= 0.0f). This value is set immediately via |
349 // SetPlaybackRate() and a task is dispatched on the task runner to notify | 314 // SetPlaybackRate() and a task is dispatched on the task runner to notify |
350 // the filters. | 315 // the filters. |
351 float playback_rate_; | 316 float playback_rate_; |
352 | 317 |
353 // Current duration as reported by |demuxer_|. | 318 // Current duration as reported by |demuxer_|. |
354 base::TimeDelta duration_; | 319 base::TimeDelta duration_; |
355 | 320 |
356 // base::TickClock used by |interpolator_|. | |
357 base::DefaultTickClock default_tick_clock_; | |
358 | |
359 // Tracks the most recent media time update and provides interpolated values | |
360 // as playback progresses. | |
361 scoped_ptr<TimeDeltaInterpolator> interpolator_; | |
362 | |
363 enum InterpolationState { | |
364 // Audio (if present) is not rendering. Time isn't being interpolated. | |
365 INTERPOLATION_STOPPED, | |
366 | |
367 // Audio (if present) is rendering. Time isn't being interpolated. | |
368 INTERPOLATION_WAITING_FOR_AUDIO_TIME_UPDATE, | |
369 | |
370 // Audio (if present) is rendering. Time is being interpolated. | |
371 INTERPOLATION_STARTED, | |
372 }; | |
373 | |
374 InterpolationState interpolation_state_; | |
375 | |
376 // Status of the pipeline. Initialized to PIPELINE_OK which indicates that | 321 // Status of the pipeline. Initialized to PIPELINE_OK which indicates that |
377 // the pipeline is operating correctly. Any other value indicates that the | 322 // the pipeline is operating correctly. Any other value indicates that the |
378 // pipeline is stopped or is stopping. Clients can call the Stop() method to | 323 // pipeline is stopped or is stopping. Clients can call the Stop() method to |
379 // reset the pipeline state, and restore this to PIPELINE_OK. | 324 // reset the pipeline state, and restore this to PIPELINE_OK. |
380 PipelineStatus status_; | 325 PipelineStatus status_; |
381 | 326 |
382 // The following data members are only accessed by tasks posted to | 327 // The following data members are only accessed by tasks posted to |
383 // |task_runner_|. | 328 // |task_runner_|. |
384 | 329 |
| 330 bool is_initialized_; |
| 331 |
385 // Member that tracks the current state. | 332 // Member that tracks the current state. |
386 State state_; | 333 State state_; |
387 | 334 |
388 // The timestamp to start playback from after starting/seeking has completed. | 335 // The timestamp to start playback from after starting/seeking has completed. |
389 base::TimeDelta start_timestamp_; | 336 base::TimeDelta start_timestamp_; |
390 | 337 |
391 // Whether we've received the audio/video/text ended events. | 338 // Whether we've received the audio/video/text ended events. |
392 bool audio_ended_; | 339 bool ended_; |
393 bool video_ended_; | |
394 bool text_ended_; | 340 bool text_ended_; |
395 | 341 |
396 BufferingState audio_buffering_state_; | |
397 BufferingState video_buffering_state_; | |
398 | |
399 // Temporary callback used for Start() and Seek(). | 342 // Temporary callback used for Start() and Seek(). |
400 PipelineStatusCB seek_cb_; | 343 PipelineStatusCB seek_cb_; |
401 | 344 |
402 // Temporary callback used for Stop(). | 345 // Temporary callback used for Stop(). |
403 base::Closure stop_cb_; | 346 base::Closure stop_cb_; |
404 | 347 |
405 // Permanent callbacks passed in via Start(). | 348 // Permanent callbacks passed in via Start(). |
406 base::Closure ended_cb_; | 349 base::Closure ended_cb_; |
407 PipelineStatusCB error_cb_; | 350 PipelineStatusCB error_cb_; |
408 PipelineMetadataCB metadata_cb_; | 351 PipelineMetadataCB metadata_cb_; |
409 BufferingStateCB buffering_state_cb_; | 352 BufferingStateCB buffering_state_cb_; |
410 base::Closure duration_change_cb_; | 353 base::Closure duration_change_cb_; |
411 | 354 |
412 // Contains the demuxer and renderers to use when initializing. | 355 // Contains the demuxer and renderers to use when initializing. |
413 scoped_ptr<FilterCollection> filter_collection_; | 356 scoped_ptr<FilterCollection> filter_collection_; |
414 | 357 |
415 // Holds the initialized demuxer. Used for seeking. Owned by client. | 358 // Holds the initialized demuxer. Used for seeking. Owned by client. |
416 Demuxer* demuxer_; | 359 Demuxer* demuxer_; |
417 | 360 |
418 // Holds the initialized renderers. Used for setting the volume, | 361 // Holds the initialized renderers. Used for setting the volume, |
419 // playback rate, and determining when playback has finished. | 362 // playback rate, and determining when playback has finished. |
420 scoped_ptr<AudioRenderer> audio_renderer_; | 363 scoped_ptr<Renderer> renderer_; |
421 scoped_ptr<VideoRenderer> video_renderer_; | |
422 scoped_ptr<TextRenderer> text_renderer_; | 364 scoped_ptr<TextRenderer> text_renderer_; |
423 | 365 |
424 // Renderer-provided time source used to control playback. | |
425 TimeSource* time_source_; | |
426 | |
427 PipelineStatistics statistics_; | 366 PipelineStatistics statistics_; |
428 | 367 |
429 scoped_ptr<SerialRunner> pending_callbacks_; | 368 scoped_ptr<SerialRunner> pending_callbacks_; |
430 | 369 |
431 bool underflow_disabled_for_testing_; | 370 bool underflow_disabled_for_testing_; |
| 371 TimeDeltaInterpolator* test_interpolator_; |
432 | 372 |
433 base::ThreadChecker thread_checker_; | 373 base::ThreadChecker thread_checker_; |
434 | 374 |
435 DISALLOW_COPY_AND_ASSIGN(Pipeline); | 375 DISALLOW_COPY_AND_ASSIGN(Pipeline); |
436 }; | 376 }; |
437 | 377 |
438 } // namespace media | 378 } // namespace media |
439 | 379 |
440 #endif // MEDIA_BASE_PIPELINE_H_ | 380 #endif // MEDIA_BASE_PIPELINE_H_ |
OLD | NEW |