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