Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // Implementation of Pipeline. | 5 // Implementation of Pipeline & PipelineStatusNotification (an async-to-sync |
| 6 // callback adapter). | |
| 6 | 7 |
| 7 #ifndef MEDIA_BASE_PIPELINE_IMPL_H_ | 8 #ifndef MEDIA_BASE_PIPELINE_IMPL_H_ |
| 8 #define MEDIA_BASE_PIPELINE_IMPL_H_ | 9 #define MEDIA_BASE_PIPELINE_IMPL_H_ |
| 9 | 10 |
| 10 #include <set> | 11 #include <set> |
| 11 #include <string> | 12 #include <string> |
| 12 #include <vector> | 13 #include <vector> |
| 13 | 14 |
| 14 #include "base/gtest_prod_util.h" | 15 #include "base/gtest_prod_util.h" |
| 15 #include "base/message_loop.h" | 16 #include "base/message_loop.h" |
| 16 #include "base/ref_counted.h" | 17 #include "base/ref_counted.h" |
| 17 #include "base/scoped_ptr.h" | 18 #include "base/scoped_ptr.h" |
| 19 #include "base/synchronization/condition_variable.h" | |
| 20 #include "base/synchronization/lock.h" | |
| 18 #include "base/threading/thread.h" | 21 #include "base/threading/thread.h" |
| 19 #include "base/time.h" | 22 #include "base/time.h" |
| 20 #include "media/base/clock.h" | 23 #include "media/base/clock.h" |
| 21 #include "media/base/composite_filter.h" | 24 #include "media/base/composite_filter.h" |
| 22 #include "media/base/filter_host.h" | 25 #include "media/base/filter_host.h" |
| 23 #include "media/base/pipeline.h" | 26 #include "media/base/pipeline.h" |
| 24 | 27 |
| 25 namespace media { | 28 namespace media { |
| 26 | 29 |
| 30 // Adapter for using asynchronous Pipeline methods in code that wants to run | |
| 31 // synchronously. To use, construct an instance of this class and pass the | |
| 32 // |Callback()| to the Pipeline method requiring a callback. Then Wait() for | |
| 33 // the callback to get fired and call status() to see what the callback's | |
| 34 // argument was. | |
| 35 class PipelineStatusNotification { | |
| 36 public: | |
| 37 PipelineStatusNotification(); | |
| 38 ~PipelineStatusNotification(); | |
| 39 | |
| 40 // See class-level comment for usage. | |
| 41 media::PipelineStatusCallback* Callback(); | |
| 42 void Notify(media::PipelineStatus status); | |
| 43 void Wait(); | |
| 44 media::PipelineStatus status(); | |
| 45 | |
| 46 private: | |
| 47 base::Lock lock_; | |
| 48 base::ConditionVariable cv_; | |
| 49 media::PipelineStatus status_; | |
| 50 bool notified_; | |
| 51 scoped_ptr<media::PipelineStatusCallback> callback_; | |
| 52 }; | |
| 27 | 53 |
| 28 // PipelineImpl runs the media pipeline. Filters are created and called on the | 54 // PipelineImpl runs the media pipeline. Filters are created and called on the |
| 29 // message loop injected into this object. PipelineImpl works like a state | 55 // message loop injected into this object. PipelineImpl works like a state |
| 30 // machine to perform asynchronous initialization, pausing, seeking and playing. | 56 // machine to perform asynchronous initialization, pausing, seeking and playing. |
| 31 // | 57 // |
| 32 // Here's a state diagram that describes the lifetime of this object. | 58 // Here's a state diagram that describes the lifetime of this object. |
| 33 // | 59 // |
| 34 // [ *Created ] [ Stopped ] | 60 // [ *Created ] [ Stopped ] |
| 35 // | Start() ^ | 61 // | Start() ^ |
| 36 // V SetError() | | 62 // V SetError() | |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 60 // playing the media. | 86 // playing the media. |
| 61 // | 87 // |
| 62 // If any error ever happens, this object will transition to the "Error" state | 88 // If any error ever happens, this object will transition to the "Error" state |
| 63 // from any state. If Stop() is ever called, this object will transition to | 89 // from any state. If Stop() is ever called, this object will transition to |
| 64 // "Stopped" state. | 90 // "Stopped" state. |
| 65 class PipelineImpl : public Pipeline, public FilterHost { | 91 class PipelineImpl : public Pipeline, public FilterHost { |
| 66 public: | 92 public: |
| 67 explicit PipelineImpl(MessageLoop* message_loop); | 93 explicit PipelineImpl(MessageLoop* message_loop); |
| 68 | 94 |
| 69 // Pipeline implementation. | 95 // Pipeline implementation. |
| 70 virtual void Init(PipelineCallback* ended_callback, | 96 virtual void Init(PipelineStatusCallback* ended_callback, |
| 71 PipelineCallback* error_callback, | 97 PipelineStatusCallback* error_callback, |
| 72 PipelineCallback* network_callback); | 98 PipelineStatusCallback* network_callback); |
| 73 virtual bool Start(FilterCollection* filter_collection, | 99 virtual bool Start(FilterCollection* filter_collection, |
| 74 const std::string& uri, | 100 const std::string& uri, |
| 75 PipelineCallback* start_callback); | 101 PipelineStatusCallback* start_callback); |
| 76 virtual void Stop(PipelineCallback* stop_callback); | 102 virtual void Stop(PipelineStatusCallback* stop_callback); |
| 77 virtual void Seek(base::TimeDelta time, PipelineCallback* seek_callback); | 103 virtual void Seek(base::TimeDelta time, PipelineStatusCallback* seek_callback) ; |
|
acolwell GONE FROM CHROMIUM
2011/03/15 03:56:28
over 80 chars.
Ami GONE FROM CHROMIUM
2011/03/15 17:37:18
Done.
| |
| 78 virtual bool IsRunning() const; | 104 virtual bool IsRunning() const; |
| 79 virtual bool IsInitialized() const; | 105 virtual bool IsInitialized() const; |
| 80 virtual bool IsNetworkActive() const; | 106 virtual bool IsNetworkActive() const; |
| 81 virtual bool HasAudio() const; | 107 virtual bool HasAudio() const; |
| 82 virtual bool HasVideo() const; | 108 virtual bool HasVideo() const; |
| 83 virtual float GetPlaybackRate() const; | 109 virtual float GetPlaybackRate() const; |
| 84 virtual void SetPlaybackRate(float playback_rate); | 110 virtual void SetPlaybackRate(float playback_rate); |
| 85 virtual float GetVolume() const; | 111 virtual float GetVolume() const; |
| 86 virtual void SetVolume(float volume); | 112 virtual void SetVolume(float volume); |
| 87 virtual base::TimeDelta GetCurrentTime() const; | 113 virtual base::TimeDelta GetCurrentTime() const; |
| 88 virtual base::TimeDelta GetBufferedTime(); | 114 virtual base::TimeDelta GetBufferedTime(); |
| 89 virtual base::TimeDelta GetMediaDuration() const; | 115 virtual base::TimeDelta GetMediaDuration() const; |
| 90 virtual int64 GetBufferedBytes() const; | 116 virtual int64 GetBufferedBytes() const; |
| 91 virtual int64 GetTotalBytes() const; | 117 virtual int64 GetTotalBytes() const; |
| 92 virtual void GetVideoSize(size_t* width_out, size_t* height_out) const; | 118 virtual void GetVideoSize(size_t* width_out, size_t* height_out) const; |
| 93 virtual bool IsStreaming() const; | 119 virtual bool IsStreaming() const; |
| 94 virtual bool IsLoaded() const; | 120 virtual bool IsLoaded() const; |
| 95 virtual PipelineError GetError() const; | |
| 96 virtual PipelineStatistics GetStatistics() const; | 121 virtual PipelineStatistics GetStatistics() const; |
| 97 | 122 |
| 98 void SetClockForTesting(Clock* clock); | 123 void SetClockForTesting(Clock* clock); |
| 99 | 124 |
| 100 private: | 125 private: |
| 101 // Pipeline states, as described above. | 126 // Pipeline states, as described above. |
| 102 enum State { | 127 enum State { |
| 103 kCreated, | 128 kCreated, |
| 104 kInitDemuxer, | 129 kInitDemuxer, |
| 105 kInitAudioDecoder, | 130 kInitAudioDecoder, |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 148 void FinishInitialization(); | 173 void FinishInitialization(); |
| 149 | 174 |
| 150 // Returns true if the given state is one that transitions to a new state | 175 // Returns true if the given state is one that transitions to a new state |
| 151 // after iterating through each filter. | 176 // after iterating through each filter. |
| 152 static bool TransientState(State state); | 177 static bool TransientState(State state); |
| 153 | 178 |
| 154 // Given the current state, returns the next state. | 179 // Given the current state, returns the next state. |
| 155 State FindNextState(State current); | 180 State FindNextState(State current); |
| 156 | 181 |
| 157 // FilterHost implementation. | 182 // FilterHost implementation. |
| 158 virtual void SetError(PipelineError error); | 183 virtual void SetError(PipelineStatus error); |
| 159 virtual base::TimeDelta GetTime() const; | 184 virtual base::TimeDelta GetTime() const; |
| 160 virtual base::TimeDelta GetDuration() const; | 185 virtual base::TimeDelta GetDuration() const; |
| 161 virtual void SetTime(base::TimeDelta time); | 186 virtual void SetTime(base::TimeDelta time); |
| 162 virtual void SetDuration(base::TimeDelta duration); | 187 virtual void SetDuration(base::TimeDelta duration); |
| 163 virtual void SetBufferedTime(base::TimeDelta buffered_time); | 188 virtual void SetBufferedTime(base::TimeDelta buffered_time); |
| 164 virtual void SetTotalBytes(int64 total_bytes); | 189 virtual void SetTotalBytes(int64 total_bytes); |
| 165 virtual void SetBufferedBytes(int64 buffered_bytes); | 190 virtual void SetBufferedBytes(int64 buffered_bytes); |
| 166 virtual void SetVideoSize(size_t width, size_t height); | 191 virtual void SetVideoSize(size_t width, size_t height); |
| 167 virtual void SetStreaming(bool streamed); | 192 virtual void SetStreaming(bool streamed); |
| 168 virtual void SetLoaded(bool loaded); | 193 virtual void SetLoaded(bool loaded); |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 183 void OnTeardownStateTransition(); | 208 void OnTeardownStateTransition(); |
| 184 | 209 |
| 185 // Callback executed by filters to update statistics. | 210 // Callback executed by filters to update statistics. |
| 186 void OnUpdateStatistics(const PipelineStatistics& stats); | 211 void OnUpdateStatistics(const PipelineStatistics& stats); |
| 187 | 212 |
| 188 // The following "task" methods correspond to the public methods, but these | 213 // The following "task" methods correspond to the public methods, but these |
| 189 // methods are run as the result of posting a task to the PipelineInternal's | 214 // methods are run as the result of posting a task to the PipelineInternal's |
| 190 // message loop. | 215 // message loop. |
| 191 void StartTask(FilterCollection* filter_collection, | 216 void StartTask(FilterCollection* filter_collection, |
| 192 const std::string& url, | 217 const std::string& url, |
| 193 PipelineCallback* start_callback); | 218 PipelineStatusCallback* start_callback); |
| 194 | 219 |
| 195 // InitializeTask() performs initialization in multiple passes. It is executed | 220 // InitializeTask() performs initialization in multiple passes. It is executed |
| 196 // as a result of calling Start() or InitializationComplete() that advances | 221 // as a result of calling Start() or InitializationComplete() that advances |
| 197 // initialization to the next state. It works as a hub of state transition for | 222 // initialization to the next state. It works as a hub of state transition for |
| 198 // initialization. | 223 // initialization. |
| 199 void InitializeTask(); | 224 void InitializeTask(); |
| 200 | 225 |
| 201 // Stops and destroys all filters, placing the pipeline in the kStopped state. | 226 // Stops and destroys all filters, placing the pipeline in the kStopped state. |
| 202 void StopTask(PipelineCallback* stop_callback); | 227 void StopTask(PipelineStatusCallback* stop_callback); |
| 203 | 228 |
| 204 // Carries out stopping and destroying all filters, placing the pipeline in | 229 // Carries out stopping and destroying all filters, placing the pipeline in |
| 205 // the kError state. | 230 // the kError state. |
| 206 void ErrorChangedTask(PipelineError error); | 231 void ErrorChangedTask(PipelineStatus error); |
| 207 | 232 |
| 208 // Carries out notifying filters that the playback rate has changed. | 233 // Carries out notifying filters that the playback rate has changed. |
| 209 void PlaybackRateChangedTask(float playback_rate); | 234 void PlaybackRateChangedTask(float playback_rate); |
| 210 | 235 |
| 211 // Carries out notifying filters that the volume has changed. | 236 // Carries out notifying filters that the volume has changed. |
| 212 void VolumeChangedTask(float volume); | 237 void VolumeChangedTask(float volume); |
| 213 | 238 |
| 214 // Carries out notifying filters that we are seeking to a new timestamp. | 239 // Carries out notifying filters that we are seeking to a new timestamp. |
| 215 void SeekTask(base::TimeDelta time, PipelineCallback* seek_callback); | 240 void SeekTask(base::TimeDelta time, PipelineStatusCallback* seek_callback); |
| 216 | 241 |
| 217 // Carries out handling a notification from a filter that it has ended. | 242 // Carries out handling a notification from a filter that it has ended. |
| 218 void NotifyEndedTask(); | 243 void NotifyEndedTask(); |
| 219 | 244 |
| 220 // Carries out handling a notification of network event. | 245 // Carries out handling a notification of network event. |
| 221 void NotifyNetworkEventTask(); | 246 void NotifyNetworkEventTask(); |
| 222 | 247 |
| 223 // Carries out disabling the audio renderer. | 248 // Carries out disabling the audio renderer. |
| 224 void DisableAudioRendererTask(); | 249 void DisableAudioRendererTask(); |
| 225 | 250 |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 238 // Internal methods used in the implementation of the pipeline thread. All | 263 // Internal methods used in the implementation of the pipeline thread. All |
| 239 // of these methods are only called on the pipeline thread. | 264 // of these methods are only called on the pipeline thread. |
| 240 | 265 |
| 241 // PrepareFilter() creates the filter's thread and injects a FilterHost and | 266 // PrepareFilter() creates the filter's thread and injects a FilterHost and |
| 242 // MessageLoop. | 267 // MessageLoop. |
| 243 bool PrepareFilter(scoped_refptr<Filter> filter); | 268 bool PrepareFilter(scoped_refptr<Filter> filter); |
| 244 | 269 |
| 245 // The following initialize methods are used to select a specific type of | 270 // The following initialize methods are used to select a specific type of |
| 246 // Filter object from FilterCollection and initialize it asynchronously. | 271 // Filter object from FilterCollection and initialize it asynchronously. |
| 247 void InitializeDemuxer(); | 272 void InitializeDemuxer(); |
| 248 void OnDemuxerBuilt(PipelineError error, Demuxer* demuxer); | 273 void OnDemuxerBuilt(PipelineStatus status, Demuxer* demuxer); |
| 249 | 274 |
| 250 // Returns true if the asynchronous action of creating decoder has started. | 275 // Returns true if the asynchronous action of creating decoder has started. |
| 251 // Returns false if this method did nothing because the corresponding | 276 // Returns false if this method did nothing because the corresponding |
| 252 // audio/video stream does not exist. | 277 // audio/video stream does not exist. |
| 253 bool InitializeAudioDecoder(const scoped_refptr<Demuxer>& demuxer); | 278 bool InitializeAudioDecoder(const scoped_refptr<Demuxer>& demuxer); |
| 254 bool InitializeVideoDecoder(const scoped_refptr<Demuxer>& demuxer); | 279 bool InitializeVideoDecoder(const scoped_refptr<Demuxer>& demuxer); |
| 255 | 280 |
| 256 // Initializes a renderer and connects it with decoder. Returns true if the | 281 // Initializes a renderer and connects it with decoder. Returns true if the |
| 257 // asynchronous action of creating renderer has started. Returns | 282 // asynchronous action of creating renderer has started. Returns |
| 258 // false if this method did nothing because the corresponding audio/video | 283 // false if this method did nothing because the corresponding audio/video |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 344 | 369 |
| 345 // If this value is set to true, then |clock_| is paused and we are waiting | 370 // If this value is set to true, then |clock_| is paused and we are waiting |
| 346 // for an update of the clock greater than or equal to the elapsed time to | 371 // for an update of the clock greater than or equal to the elapsed time to |
| 347 // start the clock. | 372 // start the clock. |
| 348 bool waiting_for_clock_update_; | 373 bool waiting_for_clock_update_; |
| 349 | 374 |
| 350 // Status of the pipeline. Initialized to PIPELINE_OK which indicates that | 375 // Status of the pipeline. Initialized to PIPELINE_OK which indicates that |
| 351 // the pipeline is operating correctly. Any other value indicates that the | 376 // the pipeline is operating correctly. Any other value indicates that the |
| 352 // pipeline is stopped or is stopping. Clients can call the Stop() method to | 377 // pipeline is stopped or is stopping. Clients can call the Stop() method to |
| 353 // reset the pipeline state, and restore this to PIPELINE_OK. | 378 // reset the pipeline state, and restore this to PIPELINE_OK. |
| 354 PipelineError error_; | 379 PipelineStatus status_; |
| 355 | 380 |
| 356 // Whether the media contains rendered audio and video streams. | 381 // Whether the media contains rendered audio and video streams. |
| 357 bool has_audio_; | 382 bool has_audio_; |
| 358 bool has_video_; | 383 bool has_video_; |
| 359 | 384 |
| 360 // The following data members are only accessed by tasks posted to | 385 // The following data members are only accessed by tasks posted to |
| 361 // |message_loop_|. | 386 // |message_loop_|. |
| 362 | 387 |
| 363 // Member that tracks the current state. | 388 // Member that tracks the current state. |
| 364 State state_; | 389 State state_; |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 379 // TODO(vrk): This is a hack. | 404 // TODO(vrk): This is a hack. |
| 380 base::TimeDelta max_buffered_time_; | 405 base::TimeDelta max_buffered_time_; |
| 381 | 406 |
| 382 // Filter collection as passed in by Start(). | 407 // Filter collection as passed in by Start(). |
| 383 scoped_ptr<FilterCollection> filter_collection_; | 408 scoped_ptr<FilterCollection> filter_collection_; |
| 384 | 409 |
| 385 // URL for the data source as passed in by Start(). | 410 // URL for the data source as passed in by Start(). |
| 386 std::string url_; | 411 std::string url_; |
| 387 | 412 |
| 388 // Callbacks for various pipeline operations. | 413 // Callbacks for various pipeline operations. |
| 389 scoped_ptr<PipelineCallback> seek_callback_; | 414 scoped_ptr<PipelineStatusCallback> seek_callback_; |
| 390 scoped_ptr<PipelineCallback> stop_callback_; | 415 scoped_ptr<PipelineStatusCallback> stop_callback_; |
| 391 scoped_ptr<PipelineCallback> ended_callback_; | 416 scoped_ptr<PipelineStatusCallback> ended_callback_; |
| 392 scoped_ptr<PipelineCallback> error_callback_; | 417 scoped_ptr<PipelineStatusCallback> error_callback_; |
| 393 scoped_ptr<PipelineCallback> network_callback_; | 418 scoped_ptr<PipelineStatusCallback> network_callback_; |
| 394 | 419 |
| 395 // Reference to the filter(s) that constitute the pipeline. | 420 // Reference to the filter(s) that constitute the pipeline. |
| 396 scoped_refptr<Filter> pipeline_filter_; | 421 scoped_refptr<Filter> pipeline_filter_; |
| 397 | 422 |
| 398 // Renderer references used for setting the volume and determining | 423 // Renderer references used for setting the volume and determining |
| 399 // when playback has finished. | 424 // when playback has finished. |
| 400 scoped_refptr<AudioRenderer> audio_renderer_; | 425 scoped_refptr<AudioRenderer> audio_renderer_; |
| 401 scoped_refptr<VideoRenderer> video_renderer_; | 426 scoped_refptr<VideoRenderer> video_renderer_; |
| 402 | 427 |
| 403 // Helper class that stores filter references during pipeline | 428 // Helper class that stores filter references during pipeline |
| 404 // initialization. | 429 // initialization. |
| 405 class PipelineInitState; | 430 class PipelineInitState; |
| 406 scoped_ptr<PipelineInitState> pipeline_init_state_; | 431 scoped_ptr<PipelineInitState> pipeline_init_state_; |
| 407 | 432 |
| 408 // Statistics. | 433 // Statistics. |
| 409 PipelineStatistics statistics_; | 434 PipelineStatistics statistics_; |
| 410 | 435 |
| 411 FRIEND_TEST_ALL_PREFIXES(PipelineImplTest, GetBufferedTime); | 436 FRIEND_TEST_ALL_PREFIXES(PipelineImplTest, GetBufferedTime); |
| 412 | 437 |
| 413 DISALLOW_COPY_AND_ASSIGN(PipelineImpl); | 438 DISALLOW_COPY_AND_ASSIGN(PipelineImpl); |
| 414 }; | 439 }; |
| 415 | 440 |
| 416 } // namespace media | 441 } // namespace media |
| 417 | 442 |
| 418 #endif // MEDIA_BASE_PIPELINE_IMPL_H_ | 443 #endif // MEDIA_BASE_PIPELINE_IMPL_H_ |
| OLD | NEW |