| 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_audio_device_impl.h" | 5 #include "content/renderer/media/webrtc_audio_device_impl.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "base/win/windows_version.h" | 10 #include "base/win/windows_version.h" |
| 11 #include "content/renderer/media/audio_device_factory.h" |
| 11 #include "content/renderer/media/audio_hardware.h" | 12 #include "content/renderer/media/audio_hardware.h" |
| 12 #include "content/renderer/render_thread_impl.h" | 13 #include "content/renderer/render_thread_impl.h" |
| 13 #include "media/audio/audio_util.h" | 14 #include "media/audio/audio_util.h" |
| 14 #include "media/audio/audio_parameters.h" | 15 #include "media/audio/audio_parameters.h" |
| 15 #include "media/audio/sample_rates.h" | 16 #include "media/audio/sample_rates.h" |
| 16 | 17 |
| 17 using media::AudioParameters; | 18 using media::AudioParameters; |
| 18 | 19 |
| 19 static const int64 kMillisecondsBetweenProcessCalls = 5000; | 20 static const int64 kMillisecondsBetweenProcessCalls = 5000; |
| 20 static const double kMaxVolumeLevel = 255.0; | 21 static const double kMaxVolumeLevel = 255.0; |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 output_delay_ms_(0), | 135 output_delay_ms_(0), |
| 135 last_error_(AudioDeviceModule::kAdmErrNone), | 136 last_error_(AudioDeviceModule::kAdmErrNone), |
| 136 last_process_time_(base::TimeTicks::Now()), | 137 last_process_time_(base::TimeTicks::Now()), |
| 137 session_id_(0), | 138 session_id_(0), |
| 138 bytes_per_sample_(0), | 139 bytes_per_sample_(0), |
| 139 initialized_(false), | 140 initialized_(false), |
| 140 playing_(false), | 141 playing_(false), |
| 141 recording_(false), | 142 recording_(false), |
| 142 agc_is_enabled_(false) { | 143 agc_is_enabled_(false) { |
| 143 DVLOG(1) << "WebRtcAudioDeviceImpl::WebRtcAudioDeviceImpl()"; | 144 DVLOG(1) << "WebRtcAudioDeviceImpl::WebRtcAudioDeviceImpl()"; |
| 145 // TODO(henrika): remove this restriction when factory is used for the |
| 146 // input side as well. |
| 144 DCHECK(RenderThreadImpl::current()) << | 147 DCHECK(RenderThreadImpl::current()) << |
| 145 "WebRtcAudioDeviceImpl must be constructed on the render thread"; | 148 "WebRtcAudioDeviceImpl must be constructed on the render thread"; |
| 149 audio_output_device_ = AudioDeviceFactory::Create(); |
| 150 DCHECK(audio_output_device_); |
| 146 } | 151 } |
| 147 | 152 |
| 148 WebRtcAudioDeviceImpl::~WebRtcAudioDeviceImpl() { | 153 WebRtcAudioDeviceImpl::~WebRtcAudioDeviceImpl() { |
| 149 DVLOG(1) << "WebRtcAudioDeviceImpl::~WebRtcAudioDeviceImpl()"; | 154 DVLOG(1) << "WebRtcAudioDeviceImpl::~WebRtcAudioDeviceImpl()"; |
| 150 if (playing_) | 155 if (playing_) |
| 151 StopPlayout(); | 156 StopPlayout(); |
| 152 if (recording_) | 157 if (recording_) |
| 153 StopRecording(); | 158 StopRecording(); |
| 154 if (initialized_) | 159 if (initialized_) |
| 155 Terminate(); | 160 Terminate(); |
| (...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 403 this, &error, &event)); | 408 this, &error, &event)); |
| 404 event.Wait(); | 409 event.Wait(); |
| 405 return error; | 410 return error; |
| 406 } | 411 } |
| 407 | 412 |
| 408 // Calling Init() multiple times in a row is OK. | 413 // Calling Init() multiple times in a row is OK. |
| 409 if (initialized_) | 414 if (initialized_) |
| 410 return 0; | 415 return 0; |
| 411 | 416 |
| 412 DCHECK(!audio_input_device_); | 417 DCHECK(!audio_input_device_); |
| 413 DCHECK(!audio_output_device_); | |
| 414 DCHECK(!input_buffer_.get()); | 418 DCHECK(!input_buffer_.get()); |
| 415 DCHECK(!output_buffer_.get()); | 419 DCHECK(!output_buffer_.get()); |
| 416 | 420 |
| 417 // TODO(henrika): it could be possible to allow one of the directions (input | 421 // TODO(henrika): it could be possible to allow one of the directions (input |
| 418 // or output) to use a non-supported rate. As an example: if only the | 422 // or output) to use a non-supported rate. As an example: if only the |
| 419 // output rate is OK, we could finalize Init() and only set up an AudioDevice. | 423 // output rate is OK, we could finalize Init() and only set up an AudioDevice. |
| 420 | 424 |
| 421 // Ask the browser for the default audio output hardware sample-rate. | 425 // Ask the browser for the default audio output hardware sample-rate. |
| 422 // This request is based on a synchronous IPC message. | 426 // This request is based on a synchronous IPC message. |
| 423 int out_sample_rate = audio_hardware::GetOutputSampleRate(); | 427 int out_sample_rate = audio_hardware::GetOutputSampleRate(); |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 576 audio_input_device_ = new AudioInputDevice( | 580 audio_input_device_ = new AudioInputDevice( |
| 577 input_audio_parameters_, this, this); | 581 input_audio_parameters_, this, this); |
| 578 | 582 |
| 579 UMA_HISTOGRAM_ENUMERATION("WebRTC.AudioOutputChannelLayout", | 583 UMA_HISTOGRAM_ENUMERATION("WebRTC.AudioOutputChannelLayout", |
| 580 out_channel_layout, CHANNEL_LAYOUT_MAX); | 584 out_channel_layout, CHANNEL_LAYOUT_MAX); |
| 581 UMA_HISTOGRAM_ENUMERATION("WebRTC.AudioInputChannelLayout", | 585 UMA_HISTOGRAM_ENUMERATION("WebRTC.AudioInputChannelLayout", |
| 582 in_channel_layout, CHANNEL_LAYOUT_MAX); | 586 in_channel_layout, CHANNEL_LAYOUT_MAX); |
| 583 AddHistogramFramesPerBuffer(kAudioOutput, out_buffer_size); | 587 AddHistogramFramesPerBuffer(kAudioOutput, out_buffer_size); |
| 584 AddHistogramFramesPerBuffer(kAudioInput, in_buffer_size); | 588 AddHistogramFramesPerBuffer(kAudioInput, in_buffer_size); |
| 585 | 589 |
| 586 // Create and configure the audio rendering client. | 590 // Configure the audio rendering client. |
| 587 audio_output_device_ = new AudioDevice(output_audio_parameters_, this); | 591 audio_output_device_->Initialize(output_audio_parameters_, this); |
| 588 | 592 |
| 589 DCHECK(audio_input_device_); | 593 DCHECK(audio_input_device_); |
| 590 DCHECK(audio_output_device_); | |
| 591 | 594 |
| 592 // Allocate local audio buffers based on the parameters above. | 595 // Allocate local audio buffers based on the parameters above. |
| 593 // It is assumed that each audio sample contains 16 bits and each | 596 // It is assumed that each audio sample contains 16 bits and each |
| 594 // audio frame contains one or two audio samples depending on the | 597 // audio frame contains one or two audio samples depending on the |
| 595 // number of channels. | 598 // number of channels. |
| 596 input_buffer_.reset(new int16[input_buffer_size() * input_channels()]); | 599 input_buffer_.reset(new int16[input_buffer_size() * input_channels()]); |
| 597 output_buffer_.reset(new int16[output_buffer_size() * output_channels()]); | 600 output_buffer_.reset(new int16[output_buffer_size() * output_channels()]); |
| 598 | 601 |
| 599 DCHECK(input_buffer_.get()); | 602 DCHECK(input_buffer_.get()); |
| 600 DCHECK(output_buffer_.get()); | 603 DCHECK(output_buffer_.get()); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 620 } | 623 } |
| 621 | 624 |
| 622 int32_t WebRtcAudioDeviceImpl::Terminate() { | 625 int32_t WebRtcAudioDeviceImpl::Terminate() { |
| 623 DVLOG(1) << "Terminate()"; | 626 DVLOG(1) << "Terminate()"; |
| 624 | 627 |
| 625 // Calling Terminate() multiple times in a row is OK. | 628 // Calling Terminate() multiple times in a row is OK. |
| 626 if (!initialized_) | 629 if (!initialized_) |
| 627 return 0; | 630 return 0; |
| 628 | 631 |
| 629 DCHECK(audio_input_device_); | 632 DCHECK(audio_input_device_); |
| 630 DCHECK(audio_output_device_); | |
| 631 DCHECK(input_buffer_.get()); | 633 DCHECK(input_buffer_.get()); |
| 632 DCHECK(output_buffer_.get()); | 634 DCHECK(output_buffer_.get()); |
| 633 | 635 |
| 634 // Release all resources allocated in Init(). | 636 // Release all resources allocated in Init(). |
| 635 audio_input_device_ = NULL; | 637 audio_input_device_ = NULL; |
| 636 audio_output_device_ = NULL; | |
| 637 input_buffer_.reset(); | 638 input_buffer_.reset(); |
| 638 output_buffer_.reset(); | 639 output_buffer_.reset(); |
| 639 | 640 |
| 640 initialized_ = false; | 641 initialized_ = false; |
| 641 return 0; | 642 return 0; |
| 642 } | 643 } |
| 643 | 644 |
| 644 bool WebRtcAudioDeviceImpl::Initialized() const { | 645 bool WebRtcAudioDeviceImpl::Initialized() const { |
| 645 return initialized_; | 646 return initialized_; |
| 646 } | 647 } |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 690 } | 691 } |
| 691 | 692 |
| 692 int32_t WebRtcAudioDeviceImpl::SetRecordingDevice(WindowsDeviceType device) { | 693 int32_t WebRtcAudioDeviceImpl::SetRecordingDevice(WindowsDeviceType device) { |
| 693 DVLOG(2) << "WARNING: WebRtcAudioDeviceImpl::SetRecordingDevice() " | 694 DVLOG(2) << "WARNING: WebRtcAudioDeviceImpl::SetRecordingDevice() " |
| 694 << "NOT IMPLEMENTED"; | 695 << "NOT IMPLEMENTED"; |
| 695 return 0; | 696 return 0; |
| 696 } | 697 } |
| 697 | 698 |
| 698 int32_t WebRtcAudioDeviceImpl::PlayoutIsAvailable(bool* available) { | 699 int32_t WebRtcAudioDeviceImpl::PlayoutIsAvailable(bool* available) { |
| 699 DVLOG(1) << "PlayoutIsAvailable()"; | 700 DVLOG(1) << "PlayoutIsAvailable()"; |
| 700 *available = (audio_output_device_ != NULL); | 701 *available = initialized(); |
| 701 return 0; | 702 return 0; |
| 702 } | 703 } |
| 703 | 704 |
| 704 int32_t WebRtcAudioDeviceImpl::InitPlayout() { | 705 int32_t WebRtcAudioDeviceImpl::InitPlayout() { |
| 705 DVLOG(2) << "WARNING: WebRtcAudioDeviceImpl::InitPlayout() " | 706 DVLOG(2) << "WARNING: WebRtcAudioDeviceImpl::InitPlayout() " |
| 706 << "NOT IMPLEMENTED"; | 707 << "NOT IMPLEMENTED"; |
| 707 return 0; | 708 return 0; |
| 708 } | 709 } |
| 709 | 710 |
| 710 bool WebRtcAudioDeviceImpl::PlayoutIsInitialized() const { | 711 bool WebRtcAudioDeviceImpl::PlayoutIsInitialized() const { |
| 711 DVLOG(1) << "PlayoutIsInitialized()"; | 712 DVLOG(1) << "PlayoutIsInitialized()"; |
| 712 return (audio_output_device_ != NULL); | 713 return initialized(); |
| 713 } | 714 } |
| 714 | 715 |
| 715 int32_t WebRtcAudioDeviceImpl::RecordingIsAvailable(bool* available) { | 716 int32_t WebRtcAudioDeviceImpl::RecordingIsAvailable(bool* available) { |
| 716 DVLOG(1) << "RecordingIsAvailable()"; | 717 DVLOG(1) << "RecordingIsAvailable()"; |
| 717 *available = (audio_input_device_ != NULL); | 718 *available = (audio_input_device_ != NULL); |
| 718 return 0; | 719 return 0; |
| 719 } | 720 } |
| 720 | 721 |
| 721 int32_t WebRtcAudioDeviceImpl::InitRecording() { | 722 int32_t WebRtcAudioDeviceImpl::InitRecording() { |
| 722 DVLOG(2) << "WARNING: WebRtcAudioDeviceImpl::InitRecording() " | 723 DVLOG(2) << "WARNING: WebRtcAudioDeviceImpl::InitRecording() " |
| (...skipping 440 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1163 } | 1164 } |
| 1164 | 1165 |
| 1165 int32_t WebRtcAudioDeviceImpl::GetLoudspeakerStatus(bool* enabled) const { | 1166 int32_t WebRtcAudioDeviceImpl::GetLoudspeakerStatus(bool* enabled) const { |
| 1166 NOTIMPLEMENTED(); | 1167 NOTIMPLEMENTED(); |
| 1167 return -1; | 1168 return -1; |
| 1168 } | 1169 } |
| 1169 | 1170 |
| 1170 void WebRtcAudioDeviceImpl::SetSessionId(int session_id) { | 1171 void WebRtcAudioDeviceImpl::SetSessionId(int session_id) { |
| 1171 session_id_ = session_id; | 1172 session_id_ = session_id; |
| 1172 } | 1173 } |
| OLD | NEW |