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 3 // found in the LICENSE file. 4 5 #ifndef CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_RENDERER_H_ 6 #define CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_RENDERER_H_ 7 8 #include "base/memory/ref_counted.h" 9 #include "base/synchronization/lock.h" 10 #include "base/threading/non_thread_safe.h" 11 #include "base/threading/thread_checker.h" 12 #include "content/renderer/media/media_stream_audio_renderer.h" 13 #include "content/renderer/media/webrtc_audio_device_impl.h" 14 #include "media/base/audio_decoder.h" 15 #include "media/base/audio_pull_fifo.h" 16 #include "media/base/audio_renderer_sink.h" 17 #include "media/base/channel_layout.h" 18 19 namespace media { 20 class AudioOutputDevice; 21 } // namespace media 22 23 namespace webrtc { 24 class AudioSourceInterface; 25 class MediaStreamInterface; 26 } // namespace webrtc 27 28 namespace content { 29 30 class WebRtcAudioRendererSource; 31 32 // This renderer handles calls from the pipeline and WebRtc ADM. It is used 33 // for connecting WebRtc MediaStream with the audio pipeline. 34 class CONTENT_EXPORT WebRtcAudioRenderer 35 : NON_EXPORTED_BASE(public media::AudioRendererSink::RenderCallback), 36 NON_EXPORTED_BASE(public MediaStreamAudioRenderer) { 37 public: 38 // This is a little utility class that holds the configured state of an audio 39 // stream. 40 // It is used by both WebRtcAudioRenderer and SharedAudioRenderer (see cc 41 // file) so a part of why it exists is to avoid code duplication and track 42 // the state in the same way in WebRtcAudioRenderer and SharedAudioRenderer. 43 class PlayingState : public base::NonThreadSafe { 44 public: 45 PlayingState() : playing_(false), volume_(1.0f) {} 46 47 bool playing() const { 48 DCHECK(CalledOnValidThread()); 49 return playing_; 50 } 51 52 void set_playing(bool playing) { 53 DCHECK(CalledOnValidThread()); 54 playing_ = playing; 55 } 56 57 float volume() const { 58 DCHECK(CalledOnValidThread()); 59 return volume_; 60 } 61 62 void set_volume(float volume) { 63 DCHECK(CalledOnValidThread()); 64 volume_ = volume; 65 } 66 67 private: 68 bool playing_; 69 float volume_; 70 }; 71 72 WebRtcAudioRenderer( 73 const scoped_refptr<webrtc::MediaStreamInterface>& media_stream, 74 int source_render_view_id, 75 int source_render_frame_id, 76 int session_id, 77 int sample_rate, 78 int frames_per_buffer); 79 80 // Initialize function called by clients like WebRtcAudioDeviceImpl. 81 // Stop() has to be called before |source| is deleted. 82 bool Initialize(WebRtcAudioRendererSource* source); 83 84 // When sharing a single instance of WebRtcAudioRenderer between multiple 85 // users (e.g. WebMediaPlayerMS), call this method to create a proxy object 86 // that maintains the Play and Stop states per caller. 87 // The wrapper ensures that Play() won't be called when the caller's state 88 // is "playing", Pause() won't be called when the state already is "paused" 89 // etc and similarly maintains the same state for Stop(). 90 // When Stop() is called or when the proxy goes out of scope, the proxy 91 // will ensure that Pause() is called followed by a call to Stop(), which 92 // is the usage pattern that WebRtcAudioRenderer requires. 93 scoped_refptr<MediaStreamAudioRenderer> CreateSharedAudioRendererProxy( 94 const scoped_refptr<webrtc::MediaStreamInterface>& media_stream); 95 96 // Used to DCHECK on the expected state. 97 bool IsStarted() const; 98 99 // Accessors to the sink audio parameters. 100 int channels() const { return sink_params_.channels(); } 101 int sample_rate() const { return sink_params_.sample_rate(); } 102 103 private: 104 // MediaStreamAudioRenderer implementation. This is private since we want 105 // callers to use proxy objects. 106 // TODO(tommi): Make the MediaStreamAudioRenderer implementation a pimpl? 107 virtual void Start() OVERRIDE; 108 virtual void Play() OVERRIDE; 109 virtual void Pause() OVERRIDE; 110 virtual void Stop() OVERRIDE; 111 virtual void SetVolume(float volume) OVERRIDE; 112 virtual base::TimeDelta GetCurrentRenderTime() const OVERRIDE; 113 virtual bool IsLocalRenderer() const OVERRIDE; 114 115 // Called when an audio renderer, either the main or a proxy, starts playing. 116 // Here we maintain a reference count of how many renderers are currently 117 // playing so that the shared play state of all the streams can be reflected 118 // correctly. 119 void EnterPlayState(); 120 121 // Called when an audio renderer, either the main or a proxy, is paused. 122 // See EnterPlayState for more details. 123 void EnterPauseState(); 124 125 protected: 126 virtual ~WebRtcAudioRenderer(); 127 128 private: 129 enum State { 130 UNINITIALIZED, 131 PLAYING, 132 PAUSED, 133 }; 134 135 // Holds raw pointers to PlaingState objects. Ownership is managed outside 136 // of this type. 137 typedef std::vector<PlayingState*> PlayingStates; 138 // Maps an audio source to a list of playing states that collectively hold 139 // volume information for that source. 140 typedef std::map<webrtc::AudioSourceInterface*, PlayingStates> 141 SourcePlayingStates; 142 143 // Used to DCHECK that we are called on the correct thread. 144 base::ThreadChecker thread_checker_; 145 146 // Flag to keep track the state of the renderer. 147 State state_; 148 149 // media::AudioRendererSink::RenderCallback implementation. 150 // These two methods are called on the AudioOutputDevice worker thread. 151 virtual int Render(media::AudioBus* audio_bus, 152 int audio_delay_milliseconds) OVERRIDE; 153 virtual void OnRenderError() OVERRIDE; 154 155 // Called by AudioPullFifo when more data is necessary. 156 // This method is called on the AudioOutputDevice worker thread. 157 void SourceCallback(int fifo_frame_delay, media::AudioBus* audio_bus); 158 159 // Goes through all renderers for the |source| and applies the proper 160 // volume scaling for the source based on the volume(s) of the renderer(s). 161 void UpdateSourceVolume(webrtc::AudioSourceInterface* source); 162 163 // Tracks a playing state. The state must be playing when this method 164 // is called. 165 // Returns true if the state was added, false if it was already being tracked. 166 bool AddPlayingState(webrtc::AudioSourceInterface* source, 167 PlayingState* state); 168 // Removes a playing state for an audio source. 169 // Returns true if the state was removed from the internal map, false if 170 // it had already been removed or if the source isn't being rendered. 171 bool RemovePlayingState(webrtc::AudioSourceInterface* source, 172 PlayingState* state); 173 174 // Called whenever the Play/Pause state changes of any of the renderers 175 // or if the volume of any of them is changed. 176 // Here we update the shared Play state and apply volume scaling to all audio 177 // sources associated with the |media_stream| based on the collective volume 178 // of playing renderers. 179 void OnPlayStateChanged( 180 const scoped_refptr<webrtc::MediaStreamInterface>& media_stream, 181 PlayingState* state); 182 183 // The render view and frame in which the audio is rendered into |sink_|. 184 const int source_render_view_id_; 185 const int source_render_frame_id_; 186 const int session_id_; 187 188 // The sink (destination) for rendered audio. 189 scoped_refptr<media::AudioOutputDevice> sink_; 190 191 // The media stream that holds the audio tracks that this renderer renders. 192 const scoped_refptr<webrtc::MediaStreamInterface> media_stream_; 193 194 // Audio data source from the browser process. 195 WebRtcAudioRendererSource* source_; 196 197 // Protects access to |state_|, |source_|, |sink_| and |current_time_|. 198 mutable base::Lock lock_; 199 200 // Ref count for the MediaPlayers which are playing audio. 201 int play_ref_count_; 202 203 // Ref count for the MediaPlayers which have called Start() but not Stop(). 204 int start_ref_count_; 205 206 // Used to buffer data between the client and the output device in cases where 207 // the client buffer size is not the same as the output device buffer size. 208 scoped_ptr<media::AudioPullFifo> audio_fifo_; 209 210 // Contains the accumulated delay estimate which is provided to the WebRTC 211 // AEC. 212 int audio_delay_milliseconds_; 213 214 // Delay due to the FIFO in milliseconds. 215 int fifo_delay_milliseconds_; 216 217 base::TimeDelta current_time_; 218 219 // Saved volume and playing state of the root renderer. 220 PlayingState playing_state_; 221 222 // Audio params used by the sink of the renderer. 223 media::AudioParameters sink_params_; 224 225 // Maps audio sources to a list of active audio renderers. 226 // Pointers to PlayingState objects are only kept in this map while the 227 // associated renderer is actually playing the stream. Ownership of the 228 // state objects lies with the renderers and they must leave the playing state 229 // before being destructed (PlayingState object goes out of scope). 230 SourcePlayingStates source_playing_states_; 231 232 DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioRenderer); 233 }; 234 235 } // namespace content 236 237 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_RENDERER_H_ 238