OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/renderer/media/webrtc_local_audio_track.h" | 5 #include "content/renderer/media/webrtc_local_audio_track.h" |
6 | 6 |
| 7 #include "content/renderer/media/webaudio_capturer_source.h" |
7 #include "content/renderer/media/webrtc_audio_capturer.h" | 8 #include "content/renderer/media/webrtc_audio_capturer.h" |
8 #include "content/renderer/media/webrtc_audio_capturer_sink_owner.h" | 9 #include "content/renderer/media/webrtc_audio_capturer_sink_owner.h" |
| 10 #include "content/renderer/media/webrtc_local_audio_source_provider.h" |
| 11 #include "media/base/audio_fifo.h" |
9 #include "third_party/libjingle/source/talk/media/base/audiorenderer.h" | 12 #include "third_party/libjingle/source/talk/media/base/audiorenderer.h" |
10 | 13 |
11 namespace content { | 14 namespace content { |
12 | 15 |
| 16 static const size_t kMaxNumberOfBuffersInFifo = 2; |
13 static const char kAudioTrackKind[] = "audio"; | 17 static const char kAudioTrackKind[] = "audio"; |
14 | 18 |
15 namespace { | 19 namespace { |
16 | 20 |
17 using webrtc::MediaConstraintsInterface; | 21 using webrtc::MediaConstraintsInterface; |
18 | 22 |
19 // This helper function checks if any audio constraints are set that require | 23 // This helper function checks if any audio constraints are set that require |
20 // audio processing to be applied. Right now this is a big, single switch for | 24 // audio processing to be applied. Right now this is a big, single switch for |
21 // all of the properties, but in the future they'll be handled one by one. | 25 // all of the properties, but in the future they'll be handled one by one. |
22 bool NeedsAudioProcessing( | 26 bool NeedsAudioProcessing( |
(...skipping 17 matching lines...) Expand all Loading... |
40 value) { | 44 value) { |
41 return true; | 45 return true; |
42 } | 46 } |
43 } | 47 } |
44 | 48 |
45 return false; | 49 return false; |
46 } | 50 } |
47 | 51 |
48 } // namespace. | 52 } // namespace. |
49 | 53 |
| 54 // This is a temporary audio buffer with parameters used to send data to |
| 55 // callbacks. |
| 56 class WebRtcLocalAudioTrack::ConfiguredBuffer : |
| 57 public base::RefCounted<WebRtcLocalAudioTrack::ConfiguredBuffer> { |
| 58 public: |
| 59 ConfiguredBuffer() : sink_buffer_size_(0) {} |
| 60 |
| 61 void Initialize(const media::AudioParameters& params) { |
| 62 DCHECK(params.IsValid()); |
| 63 params_ = params; |
| 64 |
| 65 // Use 10ms as the sink buffer size since that is the native packet size |
| 66 // WebRtc is running on. |
| 67 sink_buffer_size_ = params.sample_rate() / 100; |
| 68 audio_wrapper_ = |
| 69 media::AudioBus::Create(params.channels(), sink_buffer_size_); |
| 70 buffer_.reset(new int16[params.frames_per_buffer() * params.channels()]); |
| 71 |
| 72 // The size of the FIFO should be at least twice of the source buffer size |
| 73 // or twice of the sink buffer size. |
| 74 int buffer_size = std::max( |
| 75 kMaxNumberOfBuffersInFifo * params.frames_per_buffer(), |
| 76 kMaxNumberOfBuffersInFifo * sink_buffer_size_); |
| 77 fifo_.reset(new media::AudioFifo(params.channels(), buffer_size)); |
| 78 } |
| 79 |
| 80 void Push(media::AudioBus* audio_source) { |
| 81 DCHECK(fifo_->frames() + audio_source->frames() <= fifo_->max_frames()); |
| 82 fifo_->Push(audio_source); |
| 83 } |
| 84 |
| 85 bool Consume() { |
| 86 if (fifo_->frames() < audio_wrapper_->frames()) |
| 87 return false; |
| 88 |
| 89 fifo_->Consume(audio_wrapper_.get(), 0, audio_wrapper_->frames()); |
| 90 audio_wrapper_->ToInterleaved(audio_wrapper_->frames(), |
| 91 params_.bits_per_sample() / 8, |
| 92 buffer()); |
| 93 return true; |
| 94 } |
| 95 |
| 96 int16* buffer() const { return buffer_.get(); } |
| 97 const media::AudioParameters& params() const { return params_; } |
| 98 int sink_buffer_size() const { return sink_buffer_size_; } |
| 99 |
| 100 private: |
| 101 ~ConfiguredBuffer() {} |
| 102 friend class base::RefCounted<WebRtcLocalAudioTrack::ConfiguredBuffer>; |
| 103 |
| 104 media::AudioParameters params_; |
| 105 scoped_ptr<media::AudioBus> audio_wrapper_; |
| 106 scoped_ptr<media::AudioFifo> fifo_; |
| 107 scoped_ptr<int16[]> buffer_; |
| 108 int sink_buffer_size_; |
| 109 }; |
| 110 |
50 scoped_refptr<WebRtcLocalAudioTrack> WebRtcLocalAudioTrack::Create( | 111 scoped_refptr<WebRtcLocalAudioTrack> WebRtcLocalAudioTrack::Create( |
51 const std::string& id, | 112 const std::string& id, |
52 const scoped_refptr<WebRtcAudioCapturer>& capturer, | 113 const scoped_refptr<WebRtcAudioCapturer>& capturer, |
| 114 WebAudioCapturerSource* webaudio_source, |
53 webrtc::AudioSourceInterface* track_source, | 115 webrtc::AudioSourceInterface* track_source, |
54 const webrtc::MediaConstraintsInterface* constraints) { | 116 const webrtc::MediaConstraintsInterface* constraints) { |
55 talk_base::RefCountedObject<WebRtcLocalAudioTrack>* track = | 117 talk_base::RefCountedObject<WebRtcLocalAudioTrack>* track = |
56 new talk_base::RefCountedObject<WebRtcLocalAudioTrack>( | 118 new talk_base::RefCountedObject<WebRtcLocalAudioTrack>( |
57 id, capturer, track_source, constraints); | 119 id, capturer, webaudio_source, track_source, constraints); |
58 return track; | 120 return track; |
59 } | 121 } |
60 | 122 |
61 WebRtcLocalAudioTrack::WebRtcLocalAudioTrack( | 123 WebRtcLocalAudioTrack::WebRtcLocalAudioTrack( |
62 const std::string& label, | 124 const std::string& label, |
63 const scoped_refptr<WebRtcAudioCapturer>& capturer, | 125 const scoped_refptr<WebRtcAudioCapturer>& capturer, |
| 126 WebAudioCapturerSource* webaudio_source, |
64 webrtc::AudioSourceInterface* track_source, | 127 webrtc::AudioSourceInterface* track_source, |
65 const webrtc::MediaConstraintsInterface* constraints) | 128 const webrtc::MediaConstraintsInterface* constraints) |
66 : webrtc::MediaStreamTrack<webrtc::AudioTrackInterface>(label), | 129 : webrtc::MediaStreamTrack<webrtc::AudioTrackInterface>(label), |
67 capturer_(capturer), | 130 capturer_(capturer), |
| 131 webaudio_source_(webaudio_source), |
68 track_source_(track_source), | 132 track_source_(track_source), |
69 need_audio_processing_(NeedsAudioProcessing(constraints)) { | 133 need_audio_processing_(NeedsAudioProcessing(constraints)) { |
70 // The capturer with a valid device id is using microphone as source, | |
71 // and APM (AudioProcessingModule) is turned on only for microphone data. | |
72 DCHECK(capturer.get()); | 134 DCHECK(capturer.get()); |
73 DVLOG(1) << "WebRtcLocalAudioTrack::WebRtcLocalAudioTrack()"; | 135 DVLOG(1) << "WebRtcLocalAudioTrack::WebRtcLocalAudioTrack()"; |
74 } | 136 } |
75 | 137 |
76 WebRtcLocalAudioTrack::~WebRtcLocalAudioTrack() { | 138 WebRtcLocalAudioTrack::~WebRtcLocalAudioTrack() { |
77 DCHECK(thread_checker_.CalledOnValidThread()); | 139 DCHECK(thread_checker_.CalledOnValidThread()); |
78 DVLOG(1) << "WebRtcLocalAudioTrack::~WebRtcLocalAudioTrack()"; | 140 DVLOG(1) << "WebRtcLocalAudioTrack::~WebRtcLocalAudioTrack()"; |
79 // Users might not call Stop() on the track. | 141 // Users might not call Stop() on the track. |
80 Stop(); | 142 Stop(); |
81 } | 143 } |
82 | 144 |
83 void WebRtcLocalAudioTrack::CaptureData(const int16* audio_data, | 145 void WebRtcLocalAudioTrack::Capture(media::AudioBus* audio_source, |
84 int number_of_channels, | 146 int audio_delay_milliseconds, |
85 int number_of_frames, | 147 int volume, |
86 int audio_delay_milliseconds, | 148 bool key_pressed) { |
87 int volume, | |
88 bool key_pressed) { | |
89 scoped_refptr<WebRtcAudioCapturer> capturer; | 149 scoped_refptr<WebRtcAudioCapturer> capturer; |
90 std::vector<int> voe_channels; | 150 std::vector<int> voe_channels; |
91 int sample_rate = 0; | 151 int sample_rate = 0; |
| 152 int number_of_channels = 0; |
| 153 int number_of_frames = 0; |
92 SinkList sinks; | 154 SinkList sinks; |
| 155 scoped_refptr<ConfiguredBuffer> current_buffer; |
93 { | 156 { |
94 base::AutoLock auto_lock(lock_); | 157 base::AutoLock auto_lock(lock_); |
95 // When the track is diabled, we simply return here. | 158 // When the track is disabled, we simply return here. |
96 // TODO(xians): Figure out if we should feed zero to sinks instead, in | 159 // TODO(xians): Figure out if we should feed zero to sinks instead, in |
97 // order to inject VAD data in such case. | 160 // order to inject VAD data in such case. |
98 if (!enabled()) | 161 if (!enabled()) |
99 return; | 162 return; |
100 | 163 |
101 capturer = capturer_; | 164 capturer = capturer_; |
102 voe_channels = voe_channels_; | 165 voe_channels = voe_channels_; |
103 sample_rate = params_.sample_rate(), | 166 current_buffer = buffer_; |
| 167 sample_rate = current_buffer->params().sample_rate(); |
| 168 number_of_channels = current_buffer->params().channels(); |
| 169 number_of_frames = current_buffer->sink_buffer_size(); |
104 sinks = sinks_; | 170 sinks = sinks_; |
105 } | 171 } |
106 | 172 |
107 // Feed the data to the sinks. | 173 // Push the data to the fifo. |
108 for (SinkList::const_iterator it = sinks.begin(); it != sinks.end(); ++it) { | 174 current_buffer->Push(audio_source); |
109 int new_volume = (*it)->CaptureData(voe_channels, | 175 // Only turn off the audio processing when the constrain is set to false as |
110 audio_data, | 176 // well as there is no correct delay value. |
111 sample_rate, | 177 bool need_audio_processing = need_audio_processing_ ? |
112 number_of_channels, | 178 need_audio_processing_ : (audio_delay_milliseconds != 0); |
113 number_of_frames, | 179 int current_volume = volume; |
114 audio_delay_milliseconds, | 180 while (current_buffer->Consume()) { |
115 volume, | 181 // Feed the data to the sinks. |
116 need_audio_processing_, | 182 for (SinkList::const_iterator it = sinks.begin(); it != sinks.end(); ++it) { |
117 key_pressed); | 183 int new_volume = (*it)->CaptureData(voe_channels, |
118 if (new_volume != 0 && capturer.get()) | 184 current_buffer->buffer(), |
119 capturer->SetVolume(new_volume); | 185 sample_rate, |
| 186 number_of_channels, |
| 187 number_of_frames, |
| 188 audio_delay_milliseconds, |
| 189 current_volume, |
| 190 need_audio_processing, |
| 191 key_pressed); |
| 192 if (new_volume != 0 && capturer.get()) { |
| 193 // Feed the new volume to WebRtc while changing the volume on the |
| 194 // browser. |
| 195 capturer->SetVolume(new_volume); |
| 196 current_volume = new_volume; |
| 197 } |
| 198 } |
120 } | 199 } |
121 } | 200 } |
122 | 201 |
123 void WebRtcLocalAudioTrack::SetCaptureFormat( | 202 void WebRtcLocalAudioTrack::SetCaptureFormat( |
124 const media::AudioParameters& params) { | 203 const media::AudioParameters& params) { |
125 base::AutoLock auto_lock(lock_); | 204 if (!params.IsValid()) |
126 params_ = params; | 205 return; |
| 206 |
| 207 scoped_refptr<ConfiguredBuffer> new_buffer(new ConfiguredBuffer()); |
| 208 new_buffer->Initialize(params); |
| 209 |
| 210 SinkList sinks; |
| 211 { |
| 212 base::AutoLock auto_lock(lock_); |
| 213 buffer_ = new_buffer; |
| 214 sinks = sinks_; |
| 215 } |
127 | 216 |
128 // Update all the existing sinks with the new format. | 217 // Update all the existing sinks with the new format. |
129 for (SinkList::const_iterator it = sinks_.begin(); | 218 for (SinkList::const_iterator it = sinks.begin(); |
130 it != sinks_.end(); ++it) | 219 it != sinks.end(); ++it) { |
131 (*it)->SetCaptureFormat(params); | 220 (*it)->SetCaptureFormat(params); |
| 221 } |
132 } | 222 } |
133 | 223 |
134 void WebRtcLocalAudioTrack::AddChannel(int channel_id) { | 224 void WebRtcLocalAudioTrack::AddChannel(int channel_id) { |
135 DVLOG(1) << "WebRtcLocalAudioTrack::AddChannel(channel_id=" | 225 DVLOG(1) << "WebRtcLocalAudioTrack::AddChannel(channel_id=" |
136 << channel_id << ")"; | 226 << channel_id << ")"; |
137 base::AutoLock auto_lock(lock_); | 227 base::AutoLock auto_lock(lock_); |
138 if (std::find(voe_channels_.begin(), voe_channels_.end(), channel_id) != | 228 if (std::find(voe_channels_.begin(), voe_channels_.end(), channel_id) != |
139 voe_channels_.end()) { | 229 voe_channels_.end()) { |
140 // We need to handle the case when the same channel is connected to the | 230 // We need to handle the case when the same channel is connected to the |
141 // track more than once. | 231 // track more than once. |
(...skipping 23 matching lines...) Expand all Loading... |
165 } | 255 } |
166 | 256 |
167 std::string WebRtcLocalAudioTrack::kind() const { | 257 std::string WebRtcLocalAudioTrack::kind() const { |
168 return kAudioTrackKind; | 258 return kAudioTrackKind; |
169 } | 259 } |
170 | 260 |
171 void WebRtcLocalAudioTrack::AddSink(WebRtcAudioCapturerSink* sink) { | 261 void WebRtcLocalAudioTrack::AddSink(WebRtcAudioCapturerSink* sink) { |
172 DCHECK(thread_checker_.CalledOnValidThread()); | 262 DCHECK(thread_checker_.CalledOnValidThread()); |
173 DVLOG(1) << "WebRtcLocalAudioTrack::AddSink()"; | 263 DVLOG(1) << "WebRtcLocalAudioTrack::AddSink()"; |
174 base::AutoLock auto_lock(lock_); | 264 base::AutoLock auto_lock(lock_); |
175 sink->SetCaptureFormat(params_); | 265 if (buffer_.get()) |
| 266 sink->SetCaptureFormat(buffer_->params()); |
176 | 267 |
177 // Verify that |sink| is not already added to the list. | 268 // Verify that |sink| is not already added to the list. |
178 DCHECK(std::find_if( | 269 DCHECK(std::find_if( |
179 sinks_.begin(), sinks_.end(), | 270 sinks_.begin(), sinks_.end(), |
180 WebRtcAudioCapturerSinkOwner::WrapsSink(sink)) == sinks_.end()); | 271 WebRtcAudioCapturerSinkOwner::WrapsSink(sink)) == sinks_.end()); |
181 | 272 |
182 // Create (and add to the list) a new WebRtcAudioCapturerSinkOwner which owns | 273 // Create (and add to the list) a new WebRtcAudioCapturerSinkOwner which owns |
183 // the |sink| and delagates all calls to the WebRtcAudioCapturerSink | 274 // the |sink| and delagates all calls to the WebRtcAudioCapturerSink |
184 // interface. | 275 // interface. |
185 sinks_.push_back(new WebRtcAudioCapturerSinkOwner(sink)); | 276 sinks_.push_back(new WebRtcAudioCapturerSinkOwner(sink)); |
(...skipping 14 matching lines...) Expand all Loading... |
200 // be sent to this sink. Also avoids a possible crash which can happen | 291 // be sent to this sink. Also avoids a possible crash which can happen |
201 // if this method is called while capturing is active. | 292 // if this method is called while capturing is active. |
202 (*it)->Reset(); | 293 (*it)->Reset(); |
203 sinks_.erase(it); | 294 sinks_.erase(it); |
204 } | 295 } |
205 } | 296 } |
206 | 297 |
207 void WebRtcLocalAudioTrack::Start() { | 298 void WebRtcLocalAudioTrack::Start() { |
208 DCHECK(thread_checker_.CalledOnValidThread()); | 299 DCHECK(thread_checker_.CalledOnValidThread()); |
209 DVLOG(1) << "WebRtcLocalAudioTrack::Start()"; | 300 DVLOG(1) << "WebRtcLocalAudioTrack::Start()"; |
210 if (capturer_.get()) | 301 DCHECK(capturer_.get()); |
211 capturer_->AddTrack(this); | 302 if (webaudio_source_.get()) { |
| 303 // If the track is hooking up with WebAudio, do NOT add the track to the |
| 304 // capturer as its sink otherwise two streams in different clock will be |
| 305 // pushed through the same track. |
| 306 WebRtcLocalAudioSourceProvider* source_provider = |
| 307 static_cast<WebRtcLocalAudioSourceProvider*>( |
| 308 capturer_->audio_source_provider()); |
| 309 webaudio_source_->Start(this, source_provider); |
| 310 return; |
| 311 } |
| 312 |
| 313 capturer_->AddTrack(this); |
212 } | 314 } |
213 | 315 |
214 void WebRtcLocalAudioTrack::Stop() { | 316 void WebRtcLocalAudioTrack::Stop() { |
215 DCHECK(thread_checker_.CalledOnValidThread()); | 317 DCHECK(thread_checker_.CalledOnValidThread()); |
216 DVLOG(1) << "WebRtcLocalAudioTrack::Stop()"; | 318 DVLOG(1) << "WebRtcLocalAudioTrack::Stop()"; |
217 if (!capturer_.get()) | 319 if (!capturer_.get()) |
218 return; | 320 return; |
219 | 321 |
220 capturer_->RemoveTrack(this); | 322 if (webaudio_source_.get()) { |
| 323 // Called Stop() on the |webaudio_source_| explicitly so that |
| 324 // |webaudio_source_| won't push more data to the track anymore. |
| 325 // Also note that the track is not registered as a sink to the |capturer_| |
| 326 // in such case and no need to call RemoveTrack(). |
| 327 webaudio_source_->Stop(); |
| 328 } else { |
| 329 capturer_->RemoveTrack(this); |
| 330 } |
221 | 331 |
222 // Protect the pointers using the lock when accessing |sinks_| and | 332 // Protect the pointers using the lock when accessing |sinks_| and |
223 // setting the |capturer_| to NULL. | 333 // setting the |capturer_| to NULL. |
224 SinkList sinks; | 334 SinkList sinks; |
225 { | 335 { |
226 base::AutoLock auto_lock(lock_); | 336 base::AutoLock auto_lock(lock_); |
227 sinks = sinks_; | 337 sinks = sinks_; |
| 338 webaudio_source_ = NULL; |
228 capturer_ = NULL; | 339 capturer_ = NULL; |
229 } | 340 } |
230 | 341 |
231 for (SinkList::const_iterator it = sinks.begin(); it != sinks.end(); ++it) | 342 for (SinkList::const_iterator it = sinks.begin(); it != sinks.end(); ++it) |
232 (*it)->Reset(); | 343 (*it)->Reset(); |
233 } | 344 } |
234 | 345 |
235 } // namespace content | 346 } // namespace content |
OLD | NEW |