| 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) { | |
| 182 underflow_disabled_for_testing_ = disabled; | |
| 183 } | |
| 184 void SetTimeDeltaInterpolatorForTesting(TimeDeltaInterpolator* interpolator); | |
| 185 void SetErrorForTesting(PipelineStatus status); | 176 void SetErrorForTesting(PipelineStatus status); |
| 186 bool HasWeakPtrsForTesting() const; | 177 bool HasWeakPtrsForTesting() const; |
| 187 | 178 |
| 188 private: | 179 private: |
| 189 FRIEND_TEST_ALL_PREFIXES(PipelineTest, GetBufferedTimeRanges); | 180 FRIEND_TEST_ALL_PREFIXES(PipelineTest, GetBufferedTimeRanges); |
| 190 FRIEND_TEST_ALL_PREFIXES(PipelineTest, EndedCallback); | 181 FRIEND_TEST_ALL_PREFIXES(PipelineTest, EndedCallback); |
| 191 FRIEND_TEST_ALL_PREFIXES(PipelineTest, AudioStreamShorterThanVideo); | 182 FRIEND_TEST_ALL_PREFIXES(PipelineTest, AudioStreamShorterThanVideo); |
| 192 friend class MediaLog; | 183 friend class MediaLog; |
| 193 | 184 |
| 194 // Pipeline states, as described above. | 185 // Pipeline states, as described above. |
| 195 enum State { | 186 enum State { |
| 196 kCreated, | 187 kCreated, |
| 197 kInitDemuxer, | 188 kInitDemuxer, |
| 198 kInitAudioRenderer, | 189 kInitRenderer, |
| 199 kInitVideoRenderer, | |
| 200 kSeeking, | 190 kSeeking, |
| 201 kPlaying, | 191 kPlaying, |
| 202 kStopping, | 192 kStopping, |
| 203 kStopped, | 193 kStopped, |
| 204 }; | 194 }; |
| 205 | 195 |
| 206 // Updates |state_|. All state transitions should use this call. | 196 // Updates |state_|. All state transitions should use this call. |
| 207 void SetState(State next_state); | 197 void SetState(State next_state); |
| 208 | 198 |
| 209 static const char* GetStateString(State state); | 199 static const char* GetStateString(State state); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 222 const TextTrackConfig& config) OVERRIDE; | 212 const TextTrackConfig& config) OVERRIDE; |
| 223 virtual void RemoveTextStream(DemuxerStream* text_stream) OVERRIDE; | 213 virtual void RemoveTextStream(DemuxerStream* text_stream) OVERRIDE; |
| 224 | 214 |
| 225 // Callback executed when a rendering error happened, initiating the teardown | 215 // Callback executed when a rendering error happened, initiating the teardown |
| 226 // sequence. | 216 // sequence. |
| 227 void OnError(PipelineStatus error); | 217 void OnError(PipelineStatus error); |
| 228 | 218 |
| 229 // Callback executed by filters to update statistics. | 219 // Callback executed by filters to update statistics. |
| 230 void OnUpdateStatistics(const PipelineStatistics& stats); | 220 void OnUpdateStatistics(const PipelineStatistics& stats); |
| 231 | 221 |
| 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 | 222 // 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 | 223 // methods are run as the result of posting a task to the Pipeline's |
| 240 // task runner. | 224 // task runner. |
| 241 void StartTask(); | 225 void StartTask(); |
| 242 | 226 |
| 243 // Stops and destroys all filters, placing the pipeline in the kStopped state. | 227 // Stops and destroys all filters, placing the pipeline in the kStopped state. |
| 244 void StopTask(const base::Closure& stop_cb); | 228 void StopTask(const base::Closure& stop_cb); |
| 245 | 229 |
| 246 // Carries out stopping and destroying all filters, placing the pipeline in | 230 // Carries out stopping and destroying all filters, placing the pipeline in |
| 247 // the kStopped state. | 231 // the kStopped state. |
| 248 void ErrorChangedTask(PipelineStatus error); | 232 void ErrorChangedTask(PipelineStatus error); |
| 249 | 233 |
| 250 // Carries out notifying filters that the playback rate has changed. | 234 // Carries out notifying filters that the playback rate has changed. |
| 251 void PlaybackRateChangedTask(float playback_rate); | 235 void PlaybackRateChangedTask(float playback_rate); |
| 252 | 236 |
| 253 // Carries out notifying filters that the volume has changed. | 237 // Carries out notifying filters that the volume has changed. |
| 254 void VolumeChangedTask(float volume); | 238 void VolumeChangedTask(float volume); |
| 255 | 239 |
| 256 // Carries out notifying filters that we are seeking to a new timestamp. | 240 // Carries out notifying filters that we are seeking to a new timestamp. |
| 257 void SeekTask(base::TimeDelta time, const PipelineStatusCB& seek_cb); | 241 void SeekTask(base::TimeDelta time, const PipelineStatusCB& seek_cb); |
| 258 | 242 |
| 259 // Callbacks executed when a renderer has ended. | 243 // Callbacks executed when a renderer has ended. |
| 260 void OnAudioRendererEnded(); | 244 void OnRendererEnded(); |
| 261 void OnVideoRendererEnded(); | |
| 262 void OnTextRendererEnded(); | 245 void OnTextRendererEnded(); |
| 263 void RunEndedCallbackIfNeeded(); | 246 void RunEndedCallbackIfNeeded(); |
| 264 | 247 |
| 265 // Carries out adding a new text stream to the text renderer. | 248 // Carries out adding a new text stream to the text renderer. |
| 266 void AddTextStreamTask(DemuxerStream* text_stream, | 249 void AddTextStreamTask(DemuxerStream* text_stream, |
| 267 const TextTrackConfig& config); | 250 const TextTrackConfig& config); |
| 268 | 251 |
| 269 // Carries out removing a text stream from the text renderer. | 252 // Carries out removing a text stream from the text renderer. |
| 270 void RemoveTextStreamTask(DemuxerStream* text_stream); | 253 void RemoveTextStreamTask(DemuxerStream* text_stream); |
| 271 | 254 |
| 272 // Kicks off initialization for each media object, executing |done_cb| with | 255 // Kicks off initialization for each media object, executing |done_cb| with |
| 273 // the result when completed. | 256 // the result when completed. |
| 274 void InitializeDemuxer(const PipelineStatusCB& done_cb); | 257 void InitializeDemuxer(const PipelineStatusCB& done_cb); |
| 275 void InitializeAudioRenderer(const PipelineStatusCB& done_cb); | 258 void InitializeRenderer(const PipelineStatusCB& done_cb); |
| 276 void InitializeVideoRenderer(const PipelineStatusCB& done_cb); | |
| 277 | |
| 278 // Kicks off destroying filters. Called by StopTask() and ErrorChangedTask(). | |
| 279 // 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 | |
| 281 // state first. | |
| 282 // 2. when pipeline has been initialized, we will first transit to pausing | |
| 283 // => flushing => stopping => stopped state. | |
| 284 // This will remove the race condition during stop between filters. | |
| 285 void TearDownPipeline(); | |
| 286 | |
| 287 // Compute the time corresponding to a byte offset. | |
| 288 base::TimeDelta TimeForByteOffset_Locked(int64 byte_offset) const; | |
| 289 | 259 |
| 290 void OnStateTransition(PipelineStatus status); | 260 void OnStateTransition(PipelineStatus status); |
| 291 void StateTransitionTask(PipelineStatus status); | 261 void StateTransitionTask(PipelineStatus status); |
| 292 | 262 |
| 293 // Initiates an asynchronous pause-flush-seek-preroll call sequence | 263 // Initiates an asynchronous pause-flush-seek-preroll call sequence |
| 294 // executing |done_cb| with the final status when completed. | 264 // executing |done_cb| with the final status when completed. |
| 295 void DoSeek(base::TimeDelta seek_timestamp, const PipelineStatusCB& done_cb); | 265 void DoSeek(base::TimeDelta seek_timestamp, const PipelineStatusCB& done_cb); |
| 296 | 266 |
| 297 // Initiates an asynchronous pause-flush-stop call sequence executing | 267 // Initiates an asynchronous pause-flush-stop call sequence executing |
| 298 // |done_cb| when completed. | 268 // |done_cb| when completed. |
| 299 void DoStop(const PipelineStatusCB& done_cb); | 269 void DoStop(const PipelineStatusCB& done_cb); |
| 300 void OnStopCompleted(PipelineStatus status); | 270 void OnStopCompleted(PipelineStatus status); |
| 301 | 271 |
| 302 // Collection of callback methods and helpers for tracking changes in | 272 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 | 273 |
| 320 // Task runner used to execute pipeline tasks. | 274 // Task runner used to execute pipeline tasks. |
| 321 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 275 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 322 | 276 |
| 323 // MediaLog to which to log events. | 277 // MediaLog to which to log events. |
| 324 scoped_refptr<MediaLog> media_log_; | 278 scoped_refptr<MediaLog> media_log_; |
| 325 | 279 |
| 326 // Lock used to serialize access for the following data members. | 280 // Lock used to serialize access for the following data members. |
| 327 mutable base::Lock lock_; | 281 mutable base::Lock lock_; |
| 328 | 282 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 342 float volume_; | 296 float volume_; |
| 343 | 297 |
| 344 // Current playback rate (>= 0.0f). This value is set immediately via | 298 // Current playback rate (>= 0.0f). This value is set immediately via |
| 345 // SetPlaybackRate() and a task is dispatched on the task runner to notify | 299 // SetPlaybackRate() and a task is dispatched on the task runner to notify |
| 346 // the filters. | 300 // the filters. |
| 347 float playback_rate_; | 301 float playback_rate_; |
| 348 | 302 |
| 349 // Current duration as reported by |demuxer_|. | 303 // Current duration as reported by |demuxer_|. |
| 350 base::TimeDelta duration_; | 304 base::TimeDelta duration_; |
| 351 | 305 |
| 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 | 306 // Status of the pipeline. Initialized to PIPELINE_OK which indicates that |
| 373 // the pipeline is operating correctly. Any other value indicates that the | 307 // 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 | 308 // pipeline is stopped or is stopping. Clients can call the Stop() method to |
| 375 // reset the pipeline state, and restore this to PIPELINE_OK. | 309 // reset the pipeline state, and restore this to PIPELINE_OK. |
| 376 PipelineStatus status_; | 310 PipelineStatus status_; |
| 377 | 311 |
| 378 // The following data members are only accessed by tasks posted to | 312 // The following data members are only accessed by tasks posted to |
| 379 // |task_runner_|. | 313 // |task_runner_|. |
| 380 | 314 |
| 315 bool is_initialized_; |
| 316 |
| 381 // Member that tracks the current state. | 317 // Member that tracks the current state. |
| 382 State state_; | 318 State state_; |
| 383 | 319 |
| 384 // The timestamp to start playback from after starting/seeking has completed. | 320 // The timestamp to start playback from after starting/seeking has completed. |
| 385 base::TimeDelta start_timestamp_; | 321 base::TimeDelta start_timestamp_; |
| 386 | 322 |
| 387 // Whether we've received the audio/video/text ended events. | 323 // Whether we've received the audio/video/text ended events. |
| 388 bool audio_ended_; | 324 bool renderer_ended_; |
| 389 bool video_ended_; | 325 bool text_renderer_ended_; |
| 390 bool text_ended_; | |
| 391 | |
| 392 BufferingState audio_buffering_state_; | |
| 393 BufferingState video_buffering_state_; | |
| 394 | 326 |
| 395 // Temporary callback used for Start() and Seek(). | 327 // Temporary callback used for Start() and Seek(). |
| 396 PipelineStatusCB seek_cb_; | 328 PipelineStatusCB seek_cb_; |
| 397 | 329 |
| 398 // Temporary callback used for Stop(). | 330 // Temporary callback used for Stop(). |
| 399 base::Closure stop_cb_; | 331 base::Closure stop_cb_; |
| 400 | 332 |
| 401 // Permanent callbacks passed in via Start(). | 333 // Permanent callbacks passed in via Start(). |
| 402 base::Closure ended_cb_; | 334 base::Closure ended_cb_; |
| 403 PipelineStatusCB error_cb_; | 335 PipelineStatusCB error_cb_; |
| 404 PipelineMetadataCB metadata_cb_; | 336 PipelineMetadataCB metadata_cb_; |
| 405 BufferingStateCB buffering_state_cb_; | 337 BufferingStateCB buffering_state_cb_; |
| 406 base::Closure duration_change_cb_; | 338 base::Closure duration_change_cb_; |
| 407 | 339 |
| 408 // Contains the demuxer and renderers to use when initializing. | 340 // Contains the demuxer and renderers to use when initializing. |
| 409 scoped_ptr<FilterCollection> filter_collection_; | 341 scoped_ptr<FilterCollection> filter_collection_; |
| 410 | 342 |
| 411 // Holds the initialized demuxer. Used for seeking. Owned by client. | 343 // Holds the initialized demuxer. Used for seeking. Owned by client. |
| 412 Demuxer* demuxer_; | 344 Demuxer* demuxer_; |
| 413 | 345 |
| 414 // Holds the initialized renderers. Used for setting the volume, | 346 // Holds the initialized renderers. Used for setting the volume, |
| 415 // playback rate, and determining when playback has finished. | 347 // playback rate, and determining when playback has finished. |
| 416 scoped_ptr<AudioRenderer> audio_renderer_; | 348 scoped_ptr<Renderer> renderer_; |
| 417 scoped_ptr<VideoRenderer> video_renderer_; | |
| 418 scoped_ptr<TextRenderer> text_renderer_; | 349 scoped_ptr<TextRenderer> text_renderer_; |
| 419 | 350 |
| 420 // Renderer-provided time source used to control playback. | |
| 421 TimeSource* time_source_; | |
| 422 | |
| 423 PipelineStatistics statistics_; | 351 PipelineStatistics statistics_; |
| 424 | 352 |
| 425 scoped_ptr<SerialRunner> pending_callbacks_; | 353 scoped_ptr<SerialRunner> pending_callbacks_; |
| 426 | 354 |
| 427 bool underflow_disabled_for_testing_; | |
| 428 | |
| 429 base::ThreadChecker thread_checker_; | 355 base::ThreadChecker thread_checker_; |
| 430 | 356 |
| 431 // NOTE: Weak pointers must be invalidated before all other member variables. | 357 // NOTE: Weak pointers must be invalidated before all other member variables. |
| 432 base::WeakPtrFactory<Pipeline> weak_factory_; | 358 base::WeakPtrFactory<Pipeline> weak_factory_; |
| 433 | 359 |
| 434 DISALLOW_COPY_AND_ASSIGN(Pipeline); | 360 DISALLOW_COPY_AND_ASSIGN(Pipeline); |
| 435 }; | 361 }; |
| 436 | 362 |
| 437 } // namespace media | 363 } // namespace media |
| 438 | 364 |
| 439 #endif // MEDIA_BASE_PIPELINE_H_ | 365 #endif // MEDIA_BASE_PIPELINE_H_ |
| OLD | NEW |