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