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