Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / renderer / media / webrtc_local_audio_renderer.h
blob4764e1b8ee4dbf50f76a161eaf83834d9298965a
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_LOCAL_AUDIO_RENDERER_H_
6 #define CONTENT_RENDERER_MEDIA_WEBRTC_LOCAL_AUDIO_RENDERER_H_
8 #include <vector>
10 #include "base/callback.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/message_loop/message_loop_proxy.h"
13 #include "base/synchronization/lock.h"
14 #include "base/threading/thread_checker.h"
15 #include "content/common/content_export.h"
16 #include "content/public/renderer/media_stream_audio_sink.h"
17 #include "content/renderer/media/media_stream_audio_renderer.h"
18 #include "content/renderer/media/webrtc_audio_device_impl.h"
19 #include "content/renderer/media/webrtc_local_audio_track.h"
20 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h"
22 namespace media {
23 class AudioBus;
24 class AudioShifter;
25 class AudioOutputDevice;
26 class AudioParameters;
29 namespace content {
31 class WebRtcAudioCapturer;
33 // WebRtcLocalAudioRenderer is a MediaStreamAudioRenderer designed for rendering
34 // local audio media stream tracks,
35 // http://dev.w3.org/2011/webrtc/editor/getusermedia.html#mediastreamtrack
36 // It also implements media::AudioRendererSink::RenderCallback to render audio
37 // data provided from a WebRtcLocalAudioTrack source.
38 // When the audio layer in the browser process asks for data to render, this
39 // class provides the data by implementing the MediaStreamAudioSink
40 // interface, i.e., we are a sink seen from the WebRtcAudioCapturer perspective.
41 // TODO(henrika): improve by using similar principles as in RTCVideoRenderer
42 // which register itself to the video track when the provider is started and
43 // deregisters itself when it is stopped.
44 // Tracking this at http://crbug.com/164813.
45 class CONTENT_EXPORT WebRtcLocalAudioRenderer
46 : NON_EXPORTED_BASE(public MediaStreamAudioRenderer),
47 NON_EXPORTED_BASE(public MediaStreamAudioSink),
48 NON_EXPORTED_BASE(public media::AudioRendererSink::RenderCallback) {
49 public:
50 // Creates a local renderer and registers a capturing |source| object.
51 // The |source| is owned by the WebRtcAudioDeviceImpl.
52 // Called on the main thread.
53 WebRtcLocalAudioRenderer(const blink::WebMediaStreamTrack& audio_track,
54 int source_render_frame_id,
55 int session_id,
56 int frames_per_buffer);
58 // MediaStreamAudioRenderer implementation.
59 // Called on the main thread.
60 void Start() override;
61 void Stop() override;
62 void Play() override;
63 void Pause() override;
64 void SetVolume(float volume) override;
65 base::TimeDelta GetCurrentRenderTime() const override;
66 bool IsLocalRenderer() const override;
68 const base::TimeDelta& total_render_time() const {
69 return total_render_time_;
72 protected:
73 ~WebRtcLocalAudioRenderer() override;
75 private:
76 // MediaStreamAudioSink implementation.
78 // Called on the AudioInputDevice worker thread.
79 void OnData(const media::AudioBus& audio_bus,
80 base::TimeTicks estimated_capture_time) override;
82 // Called on the AudioInputDevice worker thread.
83 void OnSetFormat(const media::AudioParameters& params) override;
85 // media::AudioRendererSink::RenderCallback implementation.
86 // Render() is called on the AudioOutputDevice thread and OnRenderError()
87 // on the IO thread.
88 int Render(media::AudioBus* audio_bus, int audio_delay_milliseconds) override;
89 void OnRenderError() override;
91 // Initializes and starts the |sink_| if
92 // we have received valid |source_params_| &&
93 // |playing_| has been set to true &&
94 // |volume_| is not zero.
95 void MaybeStartSink();
97 // Sets new |source_params_| and then re-initializes and restarts |sink_|.
98 void ReconfigureSink(const media::AudioParameters& params);
100 // The audio track which provides data to render. Given that this class
101 // implements local loopback, the audio track is getting data from a capture
102 // instance like a selected microphone and forwards the recorded data to its
103 // sinks. The recorded data is stored in a FIFO and consumed
104 // by this class when the sink asks for new data.
105 // This class is calling MediaStreamAudioSink::AddToAudioTrack() and
106 // MediaStreamAudioSink::RemoveFromAudioTrack() to connect and disconnect
107 // with the audio track.
108 blink::WebMediaStreamTrack audio_track_;
110 // The render view and frame in which the audio is rendered into |sink_|.
111 const int source_render_frame_id_;
112 const int session_id_;
114 // MessageLoop associated with the single thread that performs all control
115 // tasks. Set to the MessageLoop that invoked the ctor.
116 const scoped_refptr<base::MessageLoopProxy> message_loop_;
118 // The sink (destination) for rendered audio.
119 scoped_refptr<media::AudioOutputDevice> sink_;
121 // This does all the synchronization/resampling/smoothing.
122 scoped_ptr<media::AudioShifter> audio_shifter_;
124 // Stores last time a render callback was received. The time difference
125 // between a new time stamp and this value can be used to derive the
126 // total render time.
127 base::TimeTicks last_render_time_;
129 // Keeps track of total time audio has been rendered.
130 base::TimeDelta total_render_time_;
132 // The audio parameters of the capture source.
133 // Must only be touched on the main thread.
134 media::AudioParameters source_params_;
136 // The audio parameters used by the sink.
137 // Must only be touched on the main thread.
138 media::AudioParameters sink_params_;
140 // Set when playing, cleared when paused.
141 bool playing_;
143 // Protects |audio_shifter_|, |playing_| and |sink_|.
144 mutable base::Lock thread_lock_;
146 // The preferred buffer size provided via the ctor.
147 const int frames_per_buffer_;
149 // The preferred device id of the output device or empty for the default
150 // output device.
151 const std::string output_device_id_;
153 // Cache value for the volume.
154 float volume_;
156 // Flag to indicate whether |sink_| has been started yet.
157 bool sink_started_;
159 // Used to DCHECK that some methods are called on the capture audio thread.
160 base::ThreadChecker capture_thread_checker_;
162 DISALLOW_COPY_AND_ASSIGN(WebRtcLocalAudioRenderer);
165 } // namespace content
167 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_LOCAL_AUDIO_RENDERER_H_