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/string_util.h" | 8 #include "base/string_util.h" |
9 #include "base/win/windows_version.h" | 9 #include "base/win/windows_version.h" |
10 #include "content/renderer/media/audio_hardware.h" | 10 #include "content/renderer/media/audio_hardware.h" |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 } | 63 } |
64 | 64 |
65 int32_t WebRtcAudioDeviceImpl::Release() { | 65 int32_t WebRtcAudioDeviceImpl::Release() { |
66 int ret = base::subtle::Barrier_AtomicIncrement(&ref_count_, -1); | 66 int ret = base::subtle::Barrier_AtomicIncrement(&ref_count_, -1); |
67 if (ret == 0) { | 67 if (ret == 0) { |
68 delete this; | 68 delete this; |
69 } | 69 } |
70 return ret; | 70 return ret; |
71 } | 71 } |
72 | 72 |
73 size_t WebRtcAudioDeviceImpl::Render( | 73 int WebRtcAudioDeviceImpl::Render( |
74 const std::vector<float*>& audio_data, | 74 const std::vector<float*>& audio_data, |
75 size_t number_of_frames, | 75 int number_of_frames, |
76 size_t audio_delay_milliseconds) { | 76 int audio_delay_milliseconds) { |
77 DCHECK_LE(number_of_frames, output_buffer_size()); | 77 DCHECK_LE(number_of_frames, output_buffer_size()); |
78 | 78 |
79 { | 79 { |
80 base::AutoLock auto_lock(lock_); | 80 base::AutoLock auto_lock(lock_); |
81 // Store the reported audio delay locally. | 81 // Store the reported audio delay locally. |
82 output_delay_ms_ = audio_delay_milliseconds; | 82 output_delay_ms_ = audio_delay_milliseconds; |
83 } | 83 } |
84 | 84 |
85 const int channels = audio_data.size(); | 85 const int channels = audio_data.size(); |
86 DCHECK_LE(channels, output_channels()); | 86 DCHECK_LE(channels, output_channels()); |
87 | 87 |
88 int samples_per_sec = output_sample_rate(); | 88 int samples_per_sec = output_sample_rate(); |
89 if (samples_per_sec == 44100) { | 89 if (samples_per_sec == 44100) { |
90 // Even if the hardware runs at 44.1kHz, we use 44.0 internally. | 90 // Even if the hardware runs at 44.1kHz, we use 44.0 internally. |
91 samples_per_sec = 44000; | 91 samples_per_sec = 44000; |
92 } | 92 } |
93 uint32_t samples_per_10_msec = (samples_per_sec / 100); | 93 int samples_per_10_msec = (samples_per_sec / 100); |
94 const int bytes_per_10_msec = | 94 const int bytes_per_10_msec = |
95 channels * samples_per_10_msec * bytes_per_sample_; | 95 channels * samples_per_10_msec * bytes_per_sample_; |
96 | 96 |
97 uint32_t num_audio_samples = 0; | 97 uint32_t num_audio_samples = 0; |
98 size_t accumulated_audio_samples = 0; | 98 int accumulated_audio_samples = 0; |
99 | 99 |
100 char* audio_byte_buffer = reinterpret_cast<char*>(output_buffer_.get()); | 100 char* audio_byte_buffer = reinterpret_cast<char*>(output_buffer_.get()); |
101 | 101 |
102 // Get audio samples in blocks of 10 milliseconds from the registered | 102 // Get audio samples in blocks of 10 milliseconds from the registered |
103 // webrtc::AudioTransport source. Keep reading until our internal buffer | 103 // webrtc::AudioTransport source. Keep reading until our internal buffer |
104 // is full. | 104 // is full. |
105 while (accumulated_audio_samples < number_of_frames) { | 105 while (accumulated_audio_samples < number_of_frames) { |
106 // Get 10ms and append output to temporary byte buffer. | 106 // Get 10ms and append output to temporary byte buffer. |
107 audio_transport_callback_->NeedMorePlayData(samples_per_10_msec, | 107 audio_transport_callback_->NeedMorePlayData(samples_per_10_msec, |
108 bytes_per_sample_, | 108 bytes_per_sample_, |
(...skipping 20 matching lines...) Expand all Loading... |
129 } | 129 } |
130 | 130 |
131 void WebRtcAudioDeviceImpl::OnRenderError() { | 131 void WebRtcAudioDeviceImpl::OnRenderError() { |
132 DCHECK_EQ(MessageLoop::current(), ChildProcess::current()->io_message_loop()); | 132 DCHECK_EQ(MessageLoop::current(), ChildProcess::current()->io_message_loop()); |
133 // TODO(henrika): Implement error handling. | 133 // TODO(henrika): Implement error handling. |
134 LOG(ERROR) << "OnRenderError()"; | 134 LOG(ERROR) << "OnRenderError()"; |
135 } | 135 } |
136 | 136 |
137 void WebRtcAudioDeviceImpl::Capture( | 137 void WebRtcAudioDeviceImpl::Capture( |
138 const std::vector<float*>& audio_data, | 138 const std::vector<float*>& audio_data, |
139 size_t number_of_frames, | 139 int number_of_frames, |
140 size_t audio_delay_milliseconds) { | 140 int audio_delay_milliseconds) { |
141 DCHECK_LE(number_of_frames, input_buffer_size()); | 141 DCHECK_LE(number_of_frames, input_buffer_size()); |
142 | 142 |
143 int output_delay_ms = 0; | 143 int output_delay_ms = 0; |
144 { | 144 { |
145 base::AutoLock auto_lock(lock_); | 145 base::AutoLock auto_lock(lock_); |
146 // Store the reported audio delay locally. | 146 // Store the reported audio delay locally. |
147 input_delay_ms_ = audio_delay_milliseconds; | 147 input_delay_ms_ = audio_delay_milliseconds; |
148 output_delay_ms = output_delay_ms_; | 148 output_delay_ms = output_delay_ms_; |
149 } | 149 } |
150 | 150 |
151 const int channels = audio_data.size(); | 151 const int channels = audio_data.size(); |
152 DCHECK_LE(channels, input_channels()); | 152 DCHECK_LE(channels, input_channels()); |
153 uint32_t new_mic_level = 0; | 153 uint32_t new_mic_level = 0; |
154 | 154 |
155 // Interleave, scale, and clip input to int16 and store result in | 155 // Interleave, scale, and clip input to int16 and store result in |
156 // a local byte buffer. | 156 // a local byte buffer. |
157 media::InterleaveFloatToInt16(audio_data, | 157 media::InterleaveFloatToInt16(audio_data, |
158 input_buffer_.get(), | 158 input_buffer_.get(), |
159 number_of_frames); | 159 number_of_frames); |
160 | 160 |
161 int samples_per_sec = input_sample_rate(); | 161 int samples_per_sec = input_sample_rate(); |
162 if (samples_per_sec == 44100) { | 162 if (samples_per_sec == 44100) { |
163 // Even if the hardware runs at 44.1kHz, we use 44.0 internally. | 163 // Even if the hardware runs at 44.1kHz, we use 44.0 internally. |
164 samples_per_sec = 44000; | 164 samples_per_sec = 44000; |
165 } | 165 } |
166 const int samples_per_10_msec = (samples_per_sec / 100); | 166 const int samples_per_10_msec = (samples_per_sec / 100); |
167 const int bytes_per_10_msec = | 167 const int bytes_per_10_msec = |
168 channels * samples_per_10_msec * bytes_per_sample_; | 168 channels * samples_per_10_msec * bytes_per_sample_; |
169 size_t accumulated_audio_samples = 0; | 169 int accumulated_audio_samples = 0; |
170 | 170 |
171 char* audio_byte_buffer = reinterpret_cast<char*>(input_buffer_.get()); | 171 char* audio_byte_buffer = reinterpret_cast<char*>(input_buffer_.get()); |
172 | 172 |
173 // Write audio samples in blocks of 10 milliseconds to the registered | 173 // Write audio samples in blocks of 10 milliseconds to the registered |
174 // webrtc::AudioTransport sink. Keep writing until our internal byte | 174 // webrtc::AudioTransport sink. Keep writing until our internal byte |
175 // buffer is empty. | 175 // buffer is empty. |
176 while (accumulated_audio_samples < number_of_frames) { | 176 while (accumulated_audio_samples < number_of_frames) { |
177 // Deliver 10ms of recorded PCM audio. | 177 // Deliver 10ms of recorded PCM audio. |
178 // TODO(henrika): add support for analog AGC? | 178 // TODO(henrika): add support for analog AGC? |
179 audio_transport_callback_->RecordedDataIsAvailable( | 179 audio_transport_callback_->RecordedDataIsAvailable( |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
326 } | 326 } |
327 | 327 |
328 // Ask the browser for the default number of audio input channels. | 328 // Ask the browser for the default number of audio input channels. |
329 // This request is based on a synchronous IPC message. | 329 // This request is based on a synchronous IPC message. |
330 ChannelLayout input_channel_layout = | 330 ChannelLayout input_channel_layout = |
331 audio_hardware::GetInputChannelLayout(); | 331 audio_hardware::GetInputChannelLayout(); |
332 DVLOG(1) << "Audio input hardware channels: " << input_channel_layout; | 332 DVLOG(1) << "Audio input hardware channels: " << input_channel_layout; |
333 | 333 |
334 ChannelLayout out_channel_layout = CHANNEL_LAYOUT_MONO; | 334 ChannelLayout out_channel_layout = CHANNEL_LAYOUT_MONO; |
335 AudioParameters::Format in_format = AudioParameters::AUDIO_PCM_LINEAR; | 335 AudioParameters::Format in_format = AudioParameters::AUDIO_PCM_LINEAR; |
336 size_t in_buffer_size = 0; | 336 int in_buffer_size = 0; |
337 size_t out_buffer_size = 0; | 337 int out_buffer_size = 0; |
338 | 338 |
339 // TODO(henrika): factor out all platform specific parts in separate | 339 // TODO(henrika): factor out all platform specific parts in separate |
340 // functions. Code is a bit messy right now. | 340 // functions. Code is a bit messy right now. |
341 | 341 |
342 // Windows | 342 // Windows |
343 #if defined(OS_WIN) | 343 #if defined(OS_WIN) |
344 // Always use stereo rendering on Windows. | 344 // Always use stereo rendering on Windows. |
345 out_channel_layout = CHANNEL_LAYOUT_STEREO; | 345 out_channel_layout = CHANNEL_LAYOUT_STEREO; |
346 | 346 |
347 DVLOG(1) << "Using AUDIO_PCM_LOW_LATENCY as input mode on Windows."; | 347 DVLOG(1) << "Using AUDIO_PCM_LOW_LATENCY as input mode on Windows."; |
(...skipping 642 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
990 } | 990 } |
991 | 991 |
992 int32_t WebRtcAudioDeviceImpl::GetLoudspeakerStatus(bool* enabled) const { | 992 int32_t WebRtcAudioDeviceImpl::GetLoudspeakerStatus(bool* enabled) const { |
993 NOTIMPLEMENTED(); | 993 NOTIMPLEMENTED(); |
994 return -1; | 994 return -1; |
995 } | 995 } |
996 | 996 |
997 void WebRtcAudioDeviceImpl::SetSessionId(int session_id) { | 997 void WebRtcAudioDeviceImpl::SetSessionId(int session_id) { |
998 session_id_ = session_id; | 998 session_id_ = session_id; |
999 } | 999 } |
OLD | NEW |