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.
5 #ifndef CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_RENDERER_H_
6 #define CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_RENDERER_H_
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"
20 class AudioOutputDevice
;
24 class AudioSourceInterface
;
25 class MediaStreamInterface
;
30 class WebRtcAudioRendererSource
;
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
) {
38 // This is a little utility class that holds the configured state of an audio
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
{
45 PlayingState() : playing_(false), volume_(1.0f
) {}
47 bool playing() const {
48 DCHECK(CalledOnValidThread());
52 void set_playing(bool playing
) {
53 DCHECK(CalledOnValidThread());
57 float volume() const {
58 DCHECK(CalledOnValidThread());
62 void set_volume(float volume
) {
63 DCHECK(CalledOnValidThread());
73 const scoped_refptr
<webrtc::MediaStreamInterface
>& media_stream
,
74 int source_render_view_id
,
75 int source_render_frame_id
,
78 int frames_per_buffer
);
80 // Initialize function called by clients like WebRtcAudioDeviceImpl.
81 // Stop() has to be called before |source| is deleted.
82 bool Initialize(WebRtcAudioRendererSource
* source
);
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
);
96 // Used to DCHECK on the expected state.
97 bool IsStarted() const;
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(); }
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
;
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
119 void EnterPlayState();
121 // Called when an audio renderer, either the main or a proxy, is paused.
122 // See EnterPlayState for more details.
123 void EnterPauseState();
126 virtual ~WebRtcAudioRenderer();
135 // Holds raw pointers to PlaingState objects. Ownership is managed outside
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
>
143 // Used to DCHECK that we are called on the correct thread.
144 base::ThreadChecker thread_checker_
;
146 // Flag to keep track the state of the renderer.
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
;
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
);
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
);
163 // Tracks a playing state. The state must be playing when this method
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
);
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
);
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_
;
188 // The sink (destination) for rendered audio.
189 scoped_refptr
<media::AudioOutputDevice
> sink_
;
191 // The media stream that holds the audio tracks that this renderer renders.
192 const scoped_refptr
<webrtc::MediaStreamInterface
> media_stream_
;
194 // Audio data source from the browser process.
195 WebRtcAudioRendererSource
* source_
;
197 // Protects access to |state_|, |source_|, |sink_| and |current_time_|.
198 mutable base::Lock lock_
;
200 // Ref count for the MediaPlayers which are playing audio.
203 // Ref count for the MediaPlayers which have called Start() but not Stop().
204 int start_ref_count_
;
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_
;
210 // Contains the accumulated delay estimate which is provided to the WebRTC
212 int audio_delay_milliseconds_
;
214 // Delay due to the FIFO in milliseconds.
215 int fifo_delay_milliseconds_
;
217 base::TimeDelta current_time_
;
219 // Saved volume and playing state of the root renderer.
220 PlayingState playing_state_
;
222 // Audio params used by the sink of the renderer.
223 media::AudioParameters sink_params_
;
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_
;
232 DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioRenderer
);
235 } // namespace content
237 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_RENDERER_H_