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