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_
10 #include "base/memory/ref_counted.h"
11 #include "base/synchronization/lock.h"
12 #include "base/threading/non_thread_safe.h"
13 #include "base/threading/thread_checker.h"
14 #include "content/public/renderer/media_stream_audio_renderer.h"
15 #include "content/renderer/media/webrtc_audio_device_impl.h"
16 #include "media/base/audio_decoder.h"
17 #include "media/base/audio_pull_fifo.h"
18 #include "media/base/audio_renderer_sink.h"
19 #include "media/base/channel_layout.h"
22 class AudioOutputDevice
;
26 class AudioSourceInterface
;
27 class MediaStreamInterface
;
32 class WebRtcAudioRendererSource
;
34 // This renderer handles calls from the pipeline and WebRtc ADM. It is used
35 // for connecting WebRtc MediaStream with the audio pipeline.
36 class CONTENT_EXPORT WebRtcAudioRenderer
37 : NON_EXPORTED_BASE(public media::AudioRendererSink::RenderCallback
),
38 NON_EXPORTED_BASE(public MediaStreamAudioRenderer
) {
40 // This is a little utility class that holds the configured state of an audio
42 // It is used by both WebRtcAudioRenderer and SharedAudioRenderer (see cc
43 // file) so a part of why it exists is to avoid code duplication and track
44 // the state in the same way in WebRtcAudioRenderer and SharedAudioRenderer.
45 class PlayingState
: public base::NonThreadSafe
{
47 PlayingState() : playing_(false), volume_(1.0f
) {}
49 bool playing() const {
50 DCHECK(CalledOnValidThread());
54 void set_playing(bool playing
) {
55 DCHECK(CalledOnValidThread());
59 float volume() const {
60 DCHECK(CalledOnValidThread());
64 void set_volume(float volume
) {
65 DCHECK(CalledOnValidThread());
75 // Returns platform specific optimal buffer size for rendering audio.
76 static int GetOptimalBufferSize(int sample_rate
, int hardware_buffer_size
);
79 const scoped_refptr
<base::SingleThreadTaskRunner
>& signaling_thread
,
80 const scoped_refptr
<webrtc::MediaStreamInterface
>& media_stream
,
81 int source_render_frame_id
,
84 int frames_per_buffer
);
86 // Initialize function called by clients like WebRtcAudioDeviceImpl.
87 // Stop() has to be called before |source| is deleted.
88 bool Initialize(WebRtcAudioRendererSource
* source
);
90 // When sharing a single instance of WebRtcAudioRenderer between multiple
91 // users (e.g. WebMediaPlayerMS), call this method to create a proxy object
92 // that maintains the Play and Stop states per caller.
93 // The wrapper ensures that Play() won't be called when the caller's state
94 // is "playing", Pause() won't be called when the state already is "paused"
95 // etc and similarly maintains the same state for Stop().
96 // When Stop() is called or when the proxy goes out of scope, the proxy
97 // will ensure that Pause() is called followed by a call to Stop(), which
98 // is the usage pattern that WebRtcAudioRenderer requires.
99 scoped_refptr
<MediaStreamAudioRenderer
> CreateSharedAudioRendererProxy(
100 const scoped_refptr
<webrtc::MediaStreamInterface
>& media_stream
);
102 // Used to DCHECK on the expected state.
103 bool IsStarted() const;
105 // Accessors to the sink audio parameters.
106 int channels() const { return sink_params_
.channels(); }
107 int sample_rate() const { return sink_params_
.sample_rate(); }
108 int frames_per_buffer() const { return sink_params_
.frames_per_buffer(); }
111 // MediaStreamAudioRenderer implementation. This is private since we want
112 // callers to use proxy objects.
113 // TODO(tommi): Make the MediaStreamAudioRenderer implementation a pimpl?
114 void Start() override
;
115 void Play() override
;
116 void Pause() override
;
117 void Stop() override
;
118 void SetVolume(float volume
) override
;
119 void SwitchOutputDevice(const std::string
& device_id
,
120 const GURL
& security_origin
,
121 const media::SwitchOutputDeviceCB
& callback
) override
;
122 base::TimeDelta
GetCurrentRenderTime() const override
;
123 bool IsLocalRenderer() const override
;
125 // Called when an audio renderer, either the main or a proxy, starts playing.
126 // Here we maintain a reference count of how many renderers are currently
127 // playing so that the shared play state of all the streams can be reflected
129 void EnterPlayState();
131 // Called when an audio renderer, either the main or a proxy, is paused.
132 // See EnterPlayState for more details.
133 void EnterPauseState();
136 ~WebRtcAudioRenderer() override
;
145 // Holds raw pointers to PlaingState objects. Ownership is managed outside
147 typedef std::vector
<PlayingState
*> PlayingStates
;
148 // Maps an audio source to a list of playing states that collectively hold
149 // volume information for that source.
150 typedef std::map
<webrtc::AudioSourceInterface
*, PlayingStates
>
153 // Used to DCHECK that we are called on the correct thread.
154 base::ThreadChecker thread_checker_
;
156 // Flag to keep track the state of the renderer.
159 // media::AudioRendererSink::RenderCallback implementation.
160 // These two methods are called on the AudioOutputDevice worker thread.
161 int Render(media::AudioBus
* audio_bus
, int audio_delay_milliseconds
) override
;
162 void OnRenderError() override
;
164 // Called by AudioPullFifo when more data is necessary.
165 // This method is called on the AudioOutputDevice worker thread.
166 void SourceCallback(int fifo_frame_delay
, media::AudioBus
* audio_bus
);
168 // Goes through all renderers for the |source| and applies the proper
169 // volume scaling for the source based on the volume(s) of the renderer(s).
170 void UpdateSourceVolume(webrtc::AudioSourceInterface
* source
);
172 // Tracks a playing state. The state must be playing when this method
174 // Returns true if the state was added, false if it was already being tracked.
175 bool AddPlayingState(webrtc::AudioSourceInterface
* source
,
176 PlayingState
* state
);
177 // Removes a playing state for an audio source.
178 // Returns true if the state was removed from the internal map, false if
179 // it had already been removed or if the source isn't being rendered.
180 bool RemovePlayingState(webrtc::AudioSourceInterface
* source
,
181 PlayingState
* state
);
183 // Called whenever the Play/Pause state changes of any of the renderers
184 // or if the volume of any of them is changed.
185 // Here we update the shared Play state and apply volume scaling to all audio
186 // sources associated with the |media_stream| based on the collective volume
187 // of playing renderers.
188 void OnPlayStateChanged(
189 const scoped_refptr
<webrtc::MediaStreamInterface
>& media_stream
,
190 PlayingState
* state
);
192 // The RenderFrame in which the audio is rendered into |sink_|.
193 const int source_render_frame_id_
;
194 const int session_id_
;
196 const scoped_refptr
<base::SingleThreadTaskRunner
> signaling_thread_
;
198 // The sink (destination) for rendered audio.
199 scoped_refptr
<media::AudioOutputDevice
> sink_
;
201 // The media stream that holds the audio tracks that this renderer renders.
202 const scoped_refptr
<webrtc::MediaStreamInterface
> media_stream_
;
204 // Audio data source from the browser process.
205 WebRtcAudioRendererSource
* source_
;
207 // Protects access to |state_|, |source_|, |sink_| and |current_time_|.
208 mutable base::Lock lock_
;
210 // Ref count for the MediaPlayers which are playing audio.
213 // Ref count for the MediaPlayers which have called Start() but not Stop().
214 int start_ref_count_
;
216 // Used to buffer data between the client and the output device in cases where
217 // the client buffer size is not the same as the output device buffer size.
218 scoped_ptr
<media::AudioPullFifo
> audio_fifo_
;
220 // Contains the accumulated delay estimate which is provided to the WebRTC
222 int audio_delay_milliseconds_
;
224 // Delay due to the FIFO in milliseconds.
225 int fifo_delay_milliseconds_
;
227 base::TimeDelta current_time_
;
229 // Saved volume and playing state of the root renderer.
230 PlayingState playing_state_
;
232 // Audio params used by the sink of the renderer.
233 media::AudioParameters sink_params_
;
235 // Maps audio sources to a list of active audio renderers.
236 // Pointers to PlayingState objects are only kept in this map while the
237 // associated renderer is actually playing the stream. Ownership of the
238 // state objects lies with the renderers and they must leave the playing state
239 // before being destructed (PlayingState object goes out of scope).
240 SourcePlayingStates source_playing_states_
;
242 // Used for triggering new UMA histogram. Counts number of render
243 // callbacks modulo |kNumCallbacksBetweenRenderTimeHistograms|.
244 int render_callback_count_
;
246 DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioRenderer
);
249 } // namespace content
251 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_RENDERER_H_