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