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_renderer.h" | 5 #include "content/renderer/media/webrtc_local_audio_renderer.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/location.h" | 9 #include "base/location.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
12 #include "base/synchronization/lock.h" | 12 #include "base/synchronization/lock.h" |
13 #include "base/thread_task_runner_handle.h" | 13 #include "base/thread_task_runner_handle.h" |
14 #include "base/trace_event/trace_event.h" | 14 #include "base/trace_event/trace_event.h" |
15 #include "content/renderer/media/audio_device_factory.h" | |
16 #include "content/renderer/media/media_stream_dispatcher.h" | 15 #include "content/renderer/media/media_stream_dispatcher.h" |
| 16 #include "content/renderer/media/restartable_audio_output_device_factory.h" |
17 #include "content/renderer/media/webrtc_audio_capturer.h" | 17 #include "content/renderer/media/webrtc_audio_capturer.h" |
18 #include "content/renderer/media/webrtc_audio_renderer.h" | 18 #include "content/renderer/media/webrtc_audio_renderer.h" |
19 #include "content/renderer/render_frame_impl.h" | 19 #include "content/renderer/render_frame_impl.h" |
20 #include "media/audio/audio_output_device.h" | |
21 #include "media/base/audio_bus.h" | 20 #include "media/base/audio_bus.h" |
22 #include "media/base/audio_shifter.h" | 21 #include "media/base/audio_shifter.h" |
23 | 22 |
24 namespace content { | 23 namespace content { |
25 | 24 |
26 namespace { | 25 namespace { |
27 | 26 |
28 enum LocalRendererSinkStates { | 27 enum LocalRendererSinkStates { |
29 kSinkStarted = 0, | 28 kSinkStarted = 0, |
30 kSinkNeverStarted, | 29 kSinkNeverStarted, |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 } | 118 } |
120 | 119 |
121 void WebRtcLocalAudioRenderer::Start() { | 120 void WebRtcLocalAudioRenderer::Start() { |
122 DVLOG(1) << "WebRtcLocalAudioRenderer::Start()"; | 121 DVLOG(1) << "WebRtcLocalAudioRenderer::Start()"; |
123 DCHECK(task_runner_->BelongsToCurrentThread()); | 122 DCHECK(task_runner_->BelongsToCurrentThread()); |
124 | 123 |
125 // We get audio data from |audio_track_|... | 124 // We get audio data from |audio_track_|... |
126 MediaStreamAudioSink::AddToAudioTrack(this, audio_track_); | 125 MediaStreamAudioSink::AddToAudioTrack(this, audio_track_); |
127 // ...and |sink_| will get audio data from us. | 126 // ...and |sink_| will get audio data from us. |
128 DCHECK(!sink_.get()); | 127 DCHECK(!sink_.get()); |
129 sink_ = | 128 sink_ = RestartableAudioOutputDeviceFactory::NewOutputDevice( |
130 AudioDeviceFactory::NewOutputDevice(source_render_frame_id_, session_id_, | 129 RestartableAudioOutputDeviceFactory::kSourceLocalUserMedia, |
131 output_device_id_, security_origin_); | 130 source_render_frame_id_, session_id_, output_device_id_, |
| 131 security_origin_); |
132 | 132 |
133 base::AutoLock auto_lock(thread_lock_); | 133 base::AutoLock auto_lock(thread_lock_); |
134 last_render_time_ = base::TimeTicks::Now(); | 134 last_render_time_ = base::TimeTicks::Now(); |
135 playing_ = false; | 135 playing_ = false; |
136 } | 136 } |
137 | 137 |
138 void WebRtcLocalAudioRenderer::Stop() { | 138 void WebRtcLocalAudioRenderer::Stop() { |
139 DVLOG(1) << "WebRtcLocalAudioRenderer::Stop()"; | 139 DVLOG(1) << "WebRtcLocalAudioRenderer::Stop()"; |
140 DCHECK(task_runner_->BelongsToCurrentThread()); | 140 DCHECK(task_runner_->BelongsToCurrentThread()); |
141 | 141 |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
231 return true; | 231 return true; |
232 } | 232 } |
233 | 233 |
234 void WebRtcLocalAudioRenderer::SwitchOutputDevice( | 234 void WebRtcLocalAudioRenderer::SwitchOutputDevice( |
235 const std::string& device_id, | 235 const std::string& device_id, |
236 const url::Origin& security_origin, | 236 const url::Origin& security_origin, |
237 const media::SwitchOutputDeviceCB& callback) { | 237 const media::SwitchOutputDeviceCB& callback) { |
238 DVLOG(1) << "WebRtcLocalAudioRenderer::SwitchOutputDevice()"; | 238 DVLOG(1) << "WebRtcLocalAudioRenderer::SwitchOutputDevice()"; |
239 DCHECK(task_runner_->BelongsToCurrentThread()); | 239 DCHECK(task_runner_->BelongsToCurrentThread()); |
240 | 240 |
241 scoped_refptr<media::AudioOutputDevice> new_sink = | 241 scoped_refptr<media::RestartableAudioRendererSink> new_sink = |
242 AudioDeviceFactory::NewOutputDevice(source_render_frame_id_, session_id_, | 242 RestartableAudioOutputDeviceFactory::NewOutputDevice( |
243 device_id, security_origin); | 243 RestartableAudioOutputDeviceFactory::kSourceLocalUserMedia, |
244 if (new_sink->GetDeviceStatus() != media::OUTPUT_DEVICE_STATUS_OK) { | 244 source_render_frame_id_, session_id_, device_id, security_origin); |
245 callback.Run(new_sink->GetDeviceStatus()); | 245 media::OutputDeviceStatus new_sink_status = |
| 246 new_sink->GetOutputDevice()->GetDeviceStatus(); |
| 247 if (new_sink_status != media::OUTPUT_DEVICE_STATUS_OK) { |
| 248 callback.Run(new_sink_status); |
246 return; | 249 return; |
247 } | 250 } |
248 | 251 |
249 output_device_id_ = device_id; | 252 output_device_id_ = device_id; |
250 security_origin_ = security_origin; | 253 security_origin_ = security_origin; |
251 bool was_sink_started = sink_started_; | 254 bool was_sink_started = sink_started_; |
252 | 255 |
253 if (sink_.get()) | 256 if (sink_.get()) |
254 sink_->Stop(); | 257 sink_->Stop(); |
255 | 258 |
256 sink_started_ = false; | 259 sink_started_ = false; |
257 sink_ = new_sink; | 260 sink_ = new_sink; |
258 int frames_per_buffer = sink_->GetOutputParameters().frames_per_buffer(); | 261 int frames_per_buffer = |
| 262 sink_->GetOutputDevice()->GetOutputParameters().frames_per_buffer(); |
259 sink_params_ = source_params_; | 263 sink_params_ = source_params_; |
260 sink_params_.set_frames_per_buffer(WebRtcAudioRenderer::GetOptimalBufferSize( | 264 sink_params_.set_frames_per_buffer(WebRtcAudioRenderer::GetOptimalBufferSize( |
261 source_params_.sample_rate(), frames_per_buffer)); | 265 source_params_.sample_rate(), frames_per_buffer)); |
262 | 266 |
263 if (was_sink_started) | 267 if (was_sink_started) |
264 MaybeStartSink(); | 268 MaybeStartSink(); |
265 | 269 |
266 callback.Run(media::OUTPUT_DEVICE_STATUS_OK); | 270 callback.Run(media::OUTPUT_DEVICE_STATUS_OK); |
267 } | 271 } |
268 | 272 |
269 media::AudioParameters WebRtcLocalAudioRenderer::GetOutputParameters() { | 273 media::AudioParameters WebRtcLocalAudioRenderer::GetOutputParameters() { |
270 DCHECK(task_runner_->BelongsToCurrentThread()); | 274 DCHECK(task_runner_->BelongsToCurrentThread()); |
271 if (!sink_.get()) | 275 if (!sink_.get()) |
272 return media::AudioParameters(); | 276 return media::AudioParameters(); |
273 | 277 |
274 return sink_->GetOutputParameters(); | 278 return sink_->GetOutputDevice()->GetOutputParameters(); |
275 } | 279 } |
276 | 280 |
277 media::OutputDeviceStatus WebRtcLocalAudioRenderer::GetDeviceStatus() { | 281 media::OutputDeviceStatus WebRtcLocalAudioRenderer::GetDeviceStatus() { |
278 DCHECK(task_runner_->BelongsToCurrentThread()); | 282 DCHECK(task_runner_->BelongsToCurrentThread()); |
279 if (!sink_.get()) | 283 if (!sink_.get()) |
280 return media::OUTPUT_DEVICE_STATUS_ERROR_INTERNAL; | 284 return media::OUTPUT_DEVICE_STATUS_ERROR_INTERNAL; |
281 | 285 |
282 return sink_->GetDeviceStatus(); | 286 return sink_->GetOutputDevice()->GetDeviceStatus(); |
283 } | 287 } |
284 | 288 |
285 void WebRtcLocalAudioRenderer::MaybeStartSink() { | 289 void WebRtcLocalAudioRenderer::MaybeStartSink() { |
286 DCHECK(task_runner_->BelongsToCurrentThread()); | 290 DCHECK(task_runner_->BelongsToCurrentThread()); |
287 DVLOG(1) << "WebRtcLocalAudioRenderer::MaybeStartSink()"; | 291 DVLOG(1) << "WebRtcLocalAudioRenderer::MaybeStartSink()"; |
288 | 292 |
289 if (!sink_.get() || !source_params_.IsValid()) | 293 if (!sink_.get() || !source_params_.IsValid()) |
290 return; | 294 return; |
291 | 295 |
292 { | 296 { |
293 // Clear up the old data in the FIFO. | 297 // Clear up the old data in the FIFO. |
294 base::AutoLock auto_lock(thread_lock_); | 298 base::AutoLock auto_lock(thread_lock_); |
295 audio_shifter_->Flush(); | 299 audio_shifter_->Flush(); |
296 } | 300 } |
297 | 301 |
298 if (!sink_params_.IsValid() || !playing_ || !volume_ || sink_started_ || | 302 if (!sink_params_.IsValid() || !playing_ || !volume_ || sink_started_ || |
299 sink_->GetDeviceStatus() != media::OUTPUT_DEVICE_STATUS_OK) | 303 sink_->GetOutputDevice()->GetDeviceStatus() != |
| 304 media::OUTPUT_DEVICE_STATUS_OK) |
300 return; | 305 return; |
301 | 306 |
302 DVLOG(1) << "WebRtcLocalAudioRenderer::MaybeStartSink() -- Starting sink_."; | 307 DVLOG(1) << "WebRtcLocalAudioRenderer::MaybeStartSink() -- Starting sink_."; |
303 sink_->Initialize(sink_params_, this); | 308 sink_->Initialize(sink_params_, this); |
304 sink_->Start(); | 309 sink_->Start(); |
| 310 sink_->Play(); // It used to be AudioOutputDevice which plays on start. |
305 sink_started_ = true; | 311 sink_started_ = true; |
306 UMA_HISTOGRAM_ENUMERATION("Media.LocalRendererSinkStates", | 312 UMA_HISTOGRAM_ENUMERATION("Media.LocalRendererSinkStates", |
307 kSinkStarted, kSinkStatesMax); | 313 kSinkStarted, kSinkStatesMax); |
308 } | 314 } |
309 | 315 |
310 void WebRtcLocalAudioRenderer::ReconfigureSink( | 316 void WebRtcLocalAudioRenderer::ReconfigureSink( |
311 const media::AudioParameters& params) { | 317 const media::AudioParameters& params) { |
312 DCHECK(task_runner_->BelongsToCurrentThread()); | 318 DCHECK(task_runner_->BelongsToCurrentThread()); |
313 | 319 |
314 DVLOG(1) << "WebRtcLocalAudioRenderer::ReconfigureSink()"; | 320 DVLOG(1) << "WebRtcLocalAudioRenderer::ReconfigureSink()"; |
(...skipping 21 matching lines...) Expand all Loading... |
336 audio_shifter_.reset(new_shifter); | 342 audio_shifter_.reset(new_shifter); |
337 } | 343 } |
338 | 344 |
339 if (!sink_.get()) | 345 if (!sink_.get()) |
340 return; // WebRtcLocalAudioRenderer has not yet been started. | 346 return; // WebRtcLocalAudioRenderer has not yet been started. |
341 | 347 |
342 // Stop |sink_| and re-create a new one to be initialized with different audio | 348 // Stop |sink_| and re-create a new one to be initialized with different audio |
343 // parameters. Then, invoke MaybeStartSink() to restart everything again. | 349 // parameters. Then, invoke MaybeStartSink() to restart everything again. |
344 sink_->Stop(); | 350 sink_->Stop(); |
345 sink_started_ = false; | 351 sink_started_ = false; |
346 sink_ = | 352 sink_ = RestartableAudioOutputDeviceFactory::NewOutputDevice( |
347 AudioDeviceFactory::NewOutputDevice(source_render_frame_id_, session_id_, | 353 RestartableAudioOutputDeviceFactory::kSourceLocalUserMedia, |
348 output_device_id_, security_origin_); | 354 source_render_frame_id_, session_id_, output_device_id_, |
349 int frames_per_buffer = sink_->GetOutputParameters().frames_per_buffer(); | 355 security_origin_); |
| 356 int frames_per_buffer = |
| 357 sink_->GetOutputDevice()->GetOutputParameters().frames_per_buffer(); |
350 sink_params_ = source_params_; | 358 sink_params_ = source_params_; |
351 sink_params_.set_frames_per_buffer(WebRtcAudioRenderer::GetOptimalBufferSize( | 359 sink_params_.set_frames_per_buffer(WebRtcAudioRenderer::GetOptimalBufferSize( |
352 source_params_.sample_rate(), frames_per_buffer)); | 360 source_params_.sample_rate(), frames_per_buffer)); |
353 MaybeStartSink(); | 361 MaybeStartSink(); |
354 } | 362 } |
355 | 363 |
356 } // namespace content | 364 } // namespace content |
OLD | NEW |