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 "media/audio/audio_input_device.h" | 5 #include "media/audio/audio_input_device.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/memory/scoped_vector.h" | 8 #include "base/memory/scoped_vector.h" |
| 9 #include "base/strings/stringprintf.h" |
9 #include "base/threading/thread_restrictions.h" | 10 #include "base/threading/thread_restrictions.h" |
10 #include "base/time/time.h" | 11 #include "base/time/time.h" |
11 #include "media/audio/audio_manager_base.h" | 12 #include "media/audio/audio_manager_base.h" |
12 #include "media/base/audio_bus.h" | 13 #include "media/base/audio_bus.h" |
13 | 14 |
14 namespace media { | 15 namespace media { |
15 | 16 |
16 // The number of shared memory buffer segments indicated to browser process | 17 // The number of shared memory buffer segments indicated to browser process |
17 // in order to avoid data overwriting. This number can be any positive number, | 18 // in order to avoid data overwriting. This number can be any positive number, |
18 // dependent how fast the renderer process can pick up captured data from | 19 // dependent how fast the renderer process can pick up captured data from |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 break; | 171 break; |
171 case AUDIO_INPUT_IPC_DELEGATE_STATE_ERROR: | 172 case AUDIO_INPUT_IPC_DELEGATE_STATE_ERROR: |
172 DLOG(WARNING) << "AudioInputDevice::OnStateChanged(ERROR)"; | 173 DLOG(WARNING) << "AudioInputDevice::OnStateChanged(ERROR)"; |
173 // Don't dereference the callback object if the audio thread | 174 // Don't dereference the callback object if the audio thread |
174 // is stopped or stopping. That could mean that the callback | 175 // is stopped or stopping. That could mean that the callback |
175 // object has been deleted. | 176 // object has been deleted. |
176 // TODO(tommi): Add an explicit contract for clearing the callback | 177 // TODO(tommi): Add an explicit contract for clearing the callback |
177 // object. Possibly require calling Initialize again or provide | 178 // object. Possibly require calling Initialize again or provide |
178 // a callback object via Start() and clear it in Stop(). | 179 // a callback object via Start() and clear it in Stop(). |
179 if (!audio_thread_.IsStopped()) | 180 if (!audio_thread_.IsStopped()) |
180 callback_->OnCaptureError(); | 181 callback_->OnCaptureError( |
| 182 "AudioInputDevice::OnStateChanged - audio thread still running"); |
181 break; | 183 break; |
182 default: | 184 default: |
183 NOTREACHED(); | 185 NOTREACHED(); |
184 break; | 186 break; |
185 } | 187 } |
186 } | 188 } |
187 | 189 |
188 void AudioInputDevice::OnIPCClosed() { | 190 void AudioInputDevice::OnIPCClosed() { |
189 DCHECK(task_runner()->BelongsToCurrentThread()); | 191 DCHECK(task_runner()->BelongsToCurrentThread()); |
190 state_ = IPC_CLOSED; | 192 state_ = IPC_CLOSED; |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
289 media::AudioInputBuffer* buffer = | 291 media::AudioInputBuffer* buffer = |
290 reinterpret_cast<media::AudioInputBuffer*>(ptr); | 292 reinterpret_cast<media::AudioInputBuffer*>(ptr); |
291 scoped_ptr<media::AudioBus> audio_bus = | 293 scoped_ptr<media::AudioBus> audio_bus = |
292 media::AudioBus::WrapMemory(audio_parameters_, buffer->audio); | 294 media::AudioBus::WrapMemory(audio_parameters_, buffer->audio); |
293 audio_buses_.push_back(audio_bus.Pass()); | 295 audio_buses_.push_back(audio_bus.Pass()); |
294 ptr += segment_length_; | 296 ptr += segment_length_; |
295 } | 297 } |
296 } | 298 } |
297 | 299 |
298 void AudioInputDevice::AudioThreadCallback::Process(uint32 pending_data) { | 300 void AudioInputDevice::AudioThreadCallback::Process(uint32 pending_data) { |
299 CHECK_EQ(current_segment_id_, static_cast<int>(pending_data)); | |
300 | |
301 // The shared memory represents parameters, size of the data buffer and the | 301 // The shared memory represents parameters, size of the data buffer and the |
302 // actual data buffer containing audio data. Map the memory into this | 302 // actual data buffer containing audio data. Map the memory into this |
303 // structure and parse out parameters and the data area. | 303 // structure and parse out parameters and the data area. |
304 uint8* ptr = static_cast<uint8*>(shared_memory_.memory()); | 304 uint8* ptr = static_cast<uint8*>(shared_memory_.memory()); |
305 ptr += current_segment_id_ * segment_length_; | 305 ptr += current_segment_id_ * segment_length_; |
306 AudioInputBuffer* buffer = reinterpret_cast<AudioInputBuffer*>(ptr); | 306 AudioInputBuffer* buffer = reinterpret_cast<AudioInputBuffer*>(ptr); |
307 | 307 |
308 // Usually this will be equal but in the case of low sample rate (e.g. 8kHz, | 308 // Usually this will be equal but in the case of low sample rate (e.g. 8kHz, |
309 // the buffer may be bigger (on mac at least)). | 309 // the buffer may be bigger (on mac at least)). |
310 DCHECK_GE(buffer->params.size, | 310 DCHECK_GE(buffer->params.size, |
311 segment_length_ - sizeof(AudioInputBufferParameters)); | 311 segment_length_ - sizeof(AudioInputBufferParameters)); |
312 | 312 |
313 // Verify correct sequence. | 313 // Verify correct sequence. |
314 CHECK_EQ(last_buffer_id_ + 1, buffer->params.id); | 314 if (buffer->params.id != last_buffer_id_ + 1) { |
| 315 std::string message = base::StringPrintf( |
| 316 "Incorrect buffer sequence. Expected = %u. Actual = %u.", |
| 317 last_buffer_id_ + 1, buffer->params.id); |
| 318 LOG(ERROR) << message; |
| 319 capture_callback_->OnCaptureError(message); |
| 320 } |
| 321 if (current_segment_id_ != static_cast<int>(pending_data)) { |
| 322 std::string message = base::StringPrintf( |
| 323 "Segment id not matching. Remote = %u. Local = %d.", |
| 324 pending_data, current_segment_id_); |
| 325 LOG(ERROR) << message; |
| 326 capture_callback_->OnCaptureError(message); |
| 327 } |
315 last_buffer_id_ = buffer->params.id; | 328 last_buffer_id_ = buffer->params.id; |
316 | 329 |
317 // Use pre-allocated audio bus wrapping existing block of shared memory. | 330 // Use pre-allocated audio bus wrapping existing block of shared memory. |
318 media::AudioBus* audio_bus = audio_buses_[current_segment_id_]; | 331 media::AudioBus* audio_bus = audio_buses_[current_segment_id_]; |
319 | 332 |
320 // Deliver captured data to the client in floating point format and update | 333 // Deliver captured data to the client in floating point format and update |
321 // the audio delay measurement. | 334 // the audio delay measurement. |
322 capture_callback_->Capture( | 335 capture_callback_->Capture( |
323 audio_bus, | 336 audio_bus, |
324 buffer->params.hardware_delay_bytes / bytes_per_ms_, // Delay in ms | 337 buffer->params.hardware_delay_bytes / bytes_per_ms_, // Delay in ms |
325 buffer->params.volume, | 338 buffer->params.volume, |
326 buffer->params.key_pressed); | 339 buffer->params.key_pressed); |
327 | 340 |
328 if (++current_segment_id_ >= total_segments_) | 341 if (++current_segment_id_ >= total_segments_) |
329 current_segment_id_ = 0; | 342 current_segment_id_ = 0; |
330 } | 343 } |
331 | 344 |
332 } // namespace media | 345 } // namespace media |
OLD | NEW |