1 // Copyright 2014 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef MEDIA_FILTERS_RENDERER_IMPL_H_ 6 #define MEDIA_FILTERS_RENDERER_IMPL_H_ 7 8 #include "base/memory/ref_counted.h" 9 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/weak_ptr.h" 11 #include "base/synchronization/lock.h" 12 #include "base/time/clock.h" 13 #include "base/time/default_tick_clock.h" 14 #include "base/time/time.h" 15 #include "media/base/buffering_state.h" 16 #include "media/base/media_export.h" 17 #include "media/base/pipeline_status.h" 18 #include "media/base/renderer.h" 19 20 namespace base { 21 class SingleThreadTaskRunner; 22 } 23 24 namespace media { 25 26 class AudioRenderer; 27 class DemuxerStreamProvider; 28 class TimeSource; 29 class VideoRenderer; 30 class WallClockTimeSource; 31 32 class MEDIA_EXPORT RendererImpl : public Renderer { 33 public: 34 // Renders audio/video streams in |demuxer_stream_provider| using 35 // |audio_renderer| and |video_renderer| provided. All methods except for 36 // GetMediaTime() run on the |task_runner|. GetMediaTime() runs on the render 37 // main thread because it's part of JS sync API. 38 RendererImpl(const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, 39 DemuxerStreamProvider* demuxer_stream_provider, 40 scoped_ptr<AudioRenderer> audio_renderer, 41 scoped_ptr<VideoRenderer> video_renderer); 42 43 virtual ~RendererImpl(); 44 45 // Renderer implementation. 46 virtual void Initialize(const base::Closure& init_cb, 47 const StatisticsCB& statistics_cb, 48 const base::Closure& ended_cb, 49 const PipelineStatusCB& error_cb, 50 const BufferingStateCB& buffering_state_cb) OVERRIDE; 51 virtual void Flush(const base::Closure& flush_cb) OVERRIDE; 52 virtual void StartPlayingFrom(base::TimeDelta time) OVERRIDE; 53 virtual void SetPlaybackRate(float playback_rate) OVERRIDE; 54 virtual void SetVolume(float volume) OVERRIDE; 55 virtual base::TimeDelta GetMediaTime() OVERRIDE; 56 virtual bool HasAudio() OVERRIDE; 57 virtual bool HasVideo() OVERRIDE; 58 virtual void SetCdm(MediaKeys* cdm) OVERRIDE; 59 60 // Helper functions for testing purposes. Must be called before Initialize(). 61 void DisableUnderflowForTesting(); 62 void EnableClocklessVideoPlaybackForTesting(); 63 64 private: 65 enum State { 66 STATE_UNINITIALIZED, 67 STATE_INITIALIZING, 68 STATE_FLUSHING, 69 STATE_PLAYING, 70 STATE_ERROR 71 }; 72 73 base::TimeDelta GetMediaTimeForSyncingVideo(); 74 75 // Helper functions and callbacks for Initialize(). 76 void InitializeAudioRenderer(); 77 void OnAudioRendererInitializeDone(PipelineStatus status); 78 void InitializeVideoRenderer(); 79 void OnVideoRendererInitializeDone(PipelineStatus status); 80 81 // Helper functions and callbacks for Flush(). 82 void FlushAudioRenderer(); 83 void OnAudioRendererFlushDone(); 84 void FlushVideoRenderer(); 85 void OnVideoRendererFlushDone(); 86 87 // Callback executed by filters to update statistics. 88 void OnUpdateStatistics(const PipelineStatistics& stats); 89 90 // Collection of callback methods and helpers for tracking changes in 91 // buffering state and transition from paused/underflow states and playing 92 // states. 93 // 94 // While in the kPlaying state: 95 // - A waiting to non-waiting transition indicates preroll has completed 96 // and StartPlayback() should be called 97 // - A non-waiting to waiting transition indicates underflow has occurred 98 // and PausePlayback() should be called 99 void OnBufferingStateChanged(BufferingState* buffering_state, 100 BufferingState new_buffering_state); 101 bool WaitingForEnoughData() const; 102 void PausePlayback(); 103 void StartPlayback(); 104 105 // Callbacks executed when a renderer has ended. 106 void OnAudioRendererEnded(); 107 void OnVideoRendererEnded(); 108 bool PlaybackHasEnded() const; 109 void RunEndedCallbackIfNeeded(); 110 111 // Callback executed when a runtime error happens. 112 void OnError(PipelineStatus error); 113 114 void FireAllPendingCallbacks(); 115 116 State state_; 117 118 // Task runner used to execute pipeline tasks. 119 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; 120 121 DemuxerStreamProvider* demuxer_stream_provider_; 122 123 // Permanent callbacks to notify various renderer states/stats. 124 StatisticsCB statistics_cb_; 125 base::Closure ended_cb_; 126 PipelineStatusCB error_cb_; 127 BufferingStateCB buffering_state_cb_; 128 129 // Temporary callback used for Initialize() and Flush(). 130 base::Closure init_cb_; 131 base::Closure flush_cb_; 132 133 scoped_ptr<AudioRenderer> audio_renderer_; 134 scoped_ptr<VideoRenderer> video_renderer_; 135 136 // Renderer-provided time source used to control playback. 137 TimeSource* time_source_; 138 scoped_ptr<WallClockTimeSource> wall_clock_time_source_; 139 bool time_ticking_; 140 141 // The time to start playback from after starting/seeking has completed. 142 base::TimeDelta start_time_; 143 144 BufferingState audio_buffering_state_; 145 BufferingState video_buffering_state_; 146 147 // Whether we've received the audio/video ended events. 148 bool audio_ended_; 149 bool video_ended_; 150 151 bool underflow_disabled_for_testing_; 152 bool clockless_video_playback_enabled_for_testing_; 153 154 // NOTE: Weak pointers must be invalidated before all other member variables. 155 base::WeakPtrFactory<RendererImpl> weak_factory_; 156 base::WeakPtr<RendererImpl> weak_this_; 157 158 DISALLOW_COPY_AND_ASSIGN(RendererImpl); 159 }; 160 161 } // namespace media 162 163 #endif // MEDIA_FILTERS_RENDERER_IMPL_H_ 164