Chromium Code Reviews| Index: media/audio/audio_input_device.cc |
| =================================================================== |
| --- media/audio/audio_input_device.cc (revision 186364) |
| +++ media/audio/audio_input_device.cc (working copy) |
| @@ -11,6 +11,14 @@ |
| #include "media/audio/audio_manager_base.h" |
| #include "media/base/audio_bus.h" |
| +namespace { |
| +// The number of shared memory buffer segments indicated to browser process |
| +// in order to avoid data overwriting. This number can be any positive number, |
| +// dependent how fast the renderer process can pick up captured data from |
| +// shared memory. |
| +int kRequestedSharedMemoryCount = 10; |
|
DaleCurtis
2013/03/07 02:04:37
static const int or make it an enum. media/ code
wjia(left Chromium)
2013/03/10 18:19:49
anonymous namespace is better than static since it
DaleCurtis
2013/03/11 20:00:47
While the Chromium style guide explicitly says unn
wjia(left Chromium)
2013/03/11 20:34:27
Done.
|
| +} |
| + |
| namespace media { |
| // Takes care of invoking the capture callback on the audio thread. |
| @@ -22,6 +30,7 @@ |
| AudioThreadCallback(const AudioParameters& audio_parameters, |
| base::SharedMemoryHandle memory, |
| int memory_length, |
| + int total_segments, |
| CaptureCallback* capture_callback); |
| virtual ~AudioThreadCallback(); |
| @@ -31,6 +40,7 @@ |
| virtual void Process(int pending_data) OVERRIDE; |
| private: |
| + int current_segment_id_; |
| CaptureCallback* capture_callback_; |
| scoped_ptr<AudioBus> audio_bus_; |
| DISALLOW_COPY_AND_ASSIGN(AudioThreadCallback); |
| @@ -104,7 +114,8 @@ |
| void AudioInputDevice::OnStreamCreated( |
| base::SharedMemoryHandle handle, |
| base::SyncSocket::Handle socket_handle, |
| - int length) { |
| + int length, |
| + int total_segments) { |
| DCHECK(message_loop()->BelongsToCurrentThread()); |
| #if defined(OS_WIN) |
| DCHECK(handle); |
| @@ -127,8 +138,8 @@ |
| DCHECK(audio_thread_.IsStopped()); |
| audio_callback_.reset( |
| - new AudioInputDevice::AudioThreadCallback(audio_parameters_, handle, |
| - length, callback_)); |
| + new AudioInputDevice::AudioThreadCallback( |
| + audio_parameters_, handle, length, total_segments, callback_)); |
| audio_thread_.Start(audio_callback_.get(), socket_handle, "AudioInputDevice"); |
| MessageLoop::current()->PostTask(FROM_HERE, |
| @@ -196,7 +207,7 @@ |
| stream_id_ = 0; |
| } else { |
| ipc_->CreateStream(stream_id_, audio_parameters_, device_id, |
| - agc_is_enabled_); |
| + agc_is_enabled_, kRequestedSharedMemoryCount); |
| } |
| pending_device_ready_ = false; |
| @@ -228,7 +239,8 @@ |
| // and create the stream when getting a OnDeviceReady() callback. |
| if (!session_id_) { |
| ipc_->CreateStream(stream_id_, audio_parameters_, |
| - AudioManagerBase::kDefaultDeviceId, agc_is_enabled_); |
| + AudioManagerBase::kDefaultDeviceId, agc_is_enabled_, |
| + kRequestedSharedMemoryCount); |
| } else { |
| ipc_->StartDevice(stream_id_, session_id_); |
| pending_device_ready_ = true; |
| @@ -302,8 +314,11 @@ |
| const AudioParameters& audio_parameters, |
| base::SharedMemoryHandle memory, |
| int memory_length, |
| + int total_segments, |
| CaptureCallback* capture_callback) |
| - : AudioDeviceThread::Callback(audio_parameters, memory, memory_length), |
| + : AudioDeviceThread::Callback(audio_parameters, memory, memory_length, |
| + total_segments), |
| + current_segment_id_(0), |
| capture_callback_(capture_callback) { |
| audio_bus_ = AudioBus::Create(audio_parameters_); |
| } |
| @@ -319,16 +334,20 @@ |
| // The shared memory represents parameters, size of the data buffer and the |
| // actual data buffer containing audio data. Map the memory into this |
| // structure and parse out parameters and the data area. |
| - AudioInputBuffer* buffer = |
| - reinterpret_cast<AudioInputBuffer*>(shared_memory_.memory()); |
| + uint8* ptr = reinterpret_cast<uint8*>(shared_memory_.memory()); |
|
DaleCurtis
2013/03/07 02:04:37
static_cast ?
wjia(left Chromium)
2013/03/10 18:19:49
Done.
|
| + ptr += current_segment_id_ * segment_length_; |
| + AudioInputBuffer* buffer = reinterpret_cast<AudioInputBuffer*>(ptr); |
| DCHECK_EQ(buffer->params.size, |
| - memory_length_ - sizeof(AudioInputBufferParameters)); |
| + segment_length_ - sizeof(AudioInputBufferParameters)); |
| double volume = buffer->params.volume; |
| int audio_delay_milliseconds = pending_data / bytes_per_ms_; |
| int16* memory = reinterpret_cast<int16*>(&buffer->audio[0]); |
| const int bytes_per_sample = sizeof(memory[0]); |
| + if (++current_segment_id_ >= total_segments_) |
|
DaleCurtis
2013/03/07 02:04:37
Same modulus comment.
wjia(left Chromium)
2013/03/10 18:19:49
Same reply. :-)
|
| + current_segment_id_ = 0; |
| + |
| // Deinterleave each channel and convert to 32-bit floating-point |
| // with nominal range -1.0 -> +1.0. |
| audio_bus_->FromInterleaved(memory, audio_bus_->frames(), bytes_per_sample); |