Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(88)

Side by Side Diff: media/audio/audio_input_device.cc

Issue 12379071: Use multiple shared memory buffers cyclically for audio capture. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/message_loop.h" 8 #include "base/message_loop.h"
9 #include "base/threading/thread_restrictions.h" 9 #include "base/threading/thread_restrictions.h"
10 #include "base/time.h" 10 #include "base/time.h"
11 #include "media/audio/audio_manager_base.h" 11 #include "media/audio/audio_manager_base.h"
12 #include "media/base/audio_bus.h" 12 #include "media/base/audio_bus.h"
13 13
14 namespace {
15 int kRequestdSharedMemoryCount = 10;
henrika (OOO until Aug 14) 2013/03/04 13:12:51 Why not "requested"? Also, rather central constant
wjia(left Chromium) 2013/03/05 02:40:13 That's a typo. It should be Requested. Thanks for
16 }
17
14 namespace media { 18 namespace media {
15 19
16 // Takes care of invoking the capture callback on the audio thread. 20 // Takes care of invoking the capture callback on the audio thread.
17 // An instance of this class is created for each capture stream in 21 // An instance of this class is created for each capture stream in
18 // OnLowLatencyCreated(). 22 // OnLowLatencyCreated().
19 class AudioInputDevice::AudioThreadCallback 23 class AudioInputDevice::AudioThreadCallback
20 : public AudioDeviceThread::Callback { 24 : public AudioDeviceThread::Callback {
21 public: 25 public:
22 AudioThreadCallback(const AudioParameters& audio_parameters, 26 AudioThreadCallback(const AudioParameters& audio_parameters,
23 base::SharedMemoryHandle memory, 27 SharedMemoryHandleVector memory,
24 int memory_length, 28 int memory_length,
25 CaptureCallback* capture_callback); 29 CaptureCallback* capture_callback);
26 virtual ~AudioThreadCallback(); 30 virtual ~AudioThreadCallback();
27 31
28 virtual void MapSharedMemory() OVERRIDE; 32 virtual void MapSharedMemory() OVERRIDE;
29 33
30 // Called whenever we receive notifications about pending data. 34 // Called whenever we receive notifications about pending data.
31 virtual void Process(int pending_data) OVERRIDE; 35 virtual void Process(int pending_data, int index) OVERRIDE;
henrika (OOO until Aug 14) 2013/03/04 13:12:51 Comment |index|.
wjia(left Chromium) 2013/03/05 02:40:13 Done.
32 36
33 private: 37 private:
34 CaptureCallback* capture_callback_; 38 CaptureCallback* capture_callback_;
35 scoped_ptr<AudioBus> audio_bus_; 39 scoped_ptr<AudioBus> audio_bus_;
36 DISALLOW_COPY_AND_ASSIGN(AudioThreadCallback); 40 DISALLOW_COPY_AND_ASSIGN(AudioThreadCallback);
37 }; 41 };
38 42
39 AudioInputDevice::AudioInputDevice( 43 AudioInputDevice::AudioInputDevice(
40 AudioInputIPC* ipc, 44 AudioInputIPC* ipc,
41 const scoped_refptr<base::MessageLoopProxy>& io_loop) 45 const scoped_refptr<base::MessageLoopProxy>& io_loop)
42 : ScopedLoopObserver(io_loop), 46 : ScopedLoopObserver(io_loop),
43 callback_(NULL), 47 callback_(NULL),
44 event_handler_(NULL), 48 event_handler_(NULL),
45 ipc_(ipc), 49 ipc_(ipc),
50 socket_handle_(base::SyncSocket::kInvalidHandle),
51 received_shared_memory_count_(0),
46 stream_id_(0), 52 stream_id_(0),
47 session_id_(0), 53 session_id_(0),
48 pending_device_ready_(false), 54 pending_device_ready_(false),
49 agc_is_enabled_(false) { 55 agc_is_enabled_(false),
56 audio_thread_(true) {
50 CHECK(ipc_); 57 CHECK(ipc_);
51 } 58 }
52 59
53 void AudioInputDevice::Initialize(const AudioParameters& params, 60 void AudioInputDevice::Initialize(const AudioParameters& params,
54 CaptureCallback* callback, 61 CaptureCallback* callback,
55 CaptureEventHandler* event_handler) { 62 CaptureEventHandler* event_handler) {
56 DCHECK(!callback_); 63 DCHECK(!callback_);
57 DCHECK(!event_handler_); 64 DCHECK(!event_handler_);
58 audio_parameters_ = params; 65 audio_parameters_ = params;
59 callback_ = callback; 66 callback_ = callback;
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
95 } 102 }
96 103
97 void AudioInputDevice::SetAutomaticGainControl(bool enabled) { 104 void AudioInputDevice::SetAutomaticGainControl(bool enabled) {
98 DVLOG(1) << "SetAutomaticGainControl(enabled=" << enabled << ")"; 105 DVLOG(1) << "SetAutomaticGainControl(enabled=" << enabled << ")";
99 message_loop()->PostTask(FROM_HERE, 106 message_loop()->PostTask(FROM_HERE,
100 base::Bind(&AudioInputDevice::SetAutomaticGainControlOnIOThread, 107 base::Bind(&AudioInputDevice::SetAutomaticGainControlOnIOThread,
101 this, enabled)); 108 this, enabled));
102 } 109 }
103 110
104 void AudioInputDevice::OnStreamCreated( 111 void AudioInputDevice::OnStreamCreated(
112 base::SyncSocket::Handle socket_handle,
113 int total_handles) {
114 DCHECK(message_loop()->BelongsToCurrentThread());
115 DCHECK_NE(socket_handle, base::SyncSocket::kInvalidHandle);
116 DVLOG(1) << "OnStreamCreated (stream_id=" << stream_id_ << ")";
117
118 // We should only get this callback if stream_id_ is valid. If it is not,
119 // the IPC layer should have closed the socket handle for us and not invoked
120 // the callback. The basic assertion is that when
121 // stream_id_ is 0 the AudioInputDevice instance is not registered as a
122 // delegate and hence it should not receive callbacks.
123 DCHECK(stream_id_);
124 DCHECK_GT(total_handles, 0);
125 DCHECK_EQ(shared_memory_handles_.size(), 0u);
126
127 base::AutoLock auto_lock(audio_thread_lock_);
128 DCHECK(audio_thread_.IsStopped());
129
130 socket_handle_ = socket_handle;
131 shared_memory_handles_.resize(total_handles, base::SharedMemory().handle());
132 }
133
134 void AudioInputDevice::OnSharedMemoryCreated(
105 base::SharedMemoryHandle handle, 135 base::SharedMemoryHandle handle,
106 base::SyncSocket::Handle socket_handle, 136 int length,
107 int length) { 137 int index) {
108 DCHECK(message_loop()->BelongsToCurrentThread()); 138 DCHECK(message_loop()->BelongsToCurrentThread());
109 #if defined(OS_WIN) 139 DCHECK_NE(socket_handle_, base::SyncSocket::kInvalidHandle);
110 DCHECK(handle); 140 DCHECK(base::SharedMemory::IsHandleValid(handle));
111 DCHECK(socket_handle);
112 #else
113 DCHECK_GE(handle.fd, 0);
114 DCHECK_GE(socket_handle, 0);
115 #endif
116 DCHECK(length); 141 DCHECK(length);
117 DVLOG(1) << "OnStreamCreated (stream_id=" << stream_id_ << ")"; 142 DVLOG(1) << "OnStreamCreated (stream_id=" << stream_id_ << ")";
118 143
119 // We should only get this callback if stream_id_ is valid. If it is not, 144 // We should only get this callback if stream_id_ is valid. If it is not,
120 // the IPC layer should have closed the shared memory and socket handles 145 // the IPC layer should have closed the shared memory socket handle
121 // for us and not invoked the callback. The basic assertion is that when 146 // for us and not invoked the callback. The basic assertion is that when
122 // stream_id_ is 0 the AudioInputDevice instance is not registered as a 147 // stream_id_ is 0 the AudioInputDevice instance is not registered as a
123 // delegate and hence it should not receive callbacks. 148 // delegate and hence it should not receive callbacks.
124 DCHECK(stream_id_); 149 DCHECK(stream_id_);
125 150
126 base::AutoLock auto_lock(audio_thread_lock_); 151 base::AutoLock auto_lock(audio_thread_lock_);
127 152
128 DCHECK(audio_thread_.IsStopped()); 153 DCHECK(audio_thread_.IsStopped());
129 audio_callback_.reset(
130 new AudioInputDevice::AudioThreadCallback(audio_parameters_, handle,
131 length, callback_));
132 audio_thread_.Start(audio_callback_.get(), socket_handle, "AudioInputDevice");
133 154
134 MessageLoop::current()->PostTask(FROM_HERE, 155 int total_handles = static_cast<int>(shared_memory_handles_.size());
135 base::Bind(&AudioInputDevice::StartOnIOThread, this)); 156 DCHECK_LT(index, total_handles);
157 DCHECK(!base::SharedMemory::IsHandleValid(shared_memory_handles_[index]));
158 shared_memory_handles_[index] = handle;
159 received_shared_memory_count_++;
160 if (received_shared_memory_count_ == total_handles) {
161 audio_callback_.reset(
162 new AudioInputDevice::AudioThreadCallback(
163 audio_parameters_, shared_memory_handles_, length, callback_));
164 audio_thread_.Start(audio_callback_.get(), socket_handle_,
165 "AudioInputDevice");
166
167 MessageLoop::current()->PostTask(FROM_HERE,
168 base::Bind(&AudioInputDevice::StartOnIOThread, this));
169 }
136 } 170 }
137 171
138 void AudioInputDevice::OnVolume(double volume) { 172 void AudioInputDevice::OnVolume(double volume) {
139 NOTIMPLEMENTED(); 173 NOTIMPLEMENTED();
140 } 174 }
141 175
142 void AudioInputDevice::OnStateChanged( 176 void AudioInputDevice::OnStateChanged(
143 AudioInputIPCDelegate::State state) { 177 AudioInputIPCDelegate::State state) {
144 DCHECK(message_loop()->BelongsToCurrentThread()); 178 DCHECK(message_loop()->BelongsToCurrentThread());
145 179
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
189 if (!pending_device_ready_) 223 if (!pending_device_ready_)
190 return; 224 return;
191 225
192 // If AudioInputDeviceManager returns an empty string, it means no device 226 // If AudioInputDeviceManager returns an empty string, it means no device
193 // is ready for start. 227 // is ready for start.
194 if (device_id.empty()) { 228 if (device_id.empty()) {
195 ipc_->RemoveDelegate(stream_id_); 229 ipc_->RemoveDelegate(stream_id_);
196 stream_id_ = 0; 230 stream_id_ = 0;
197 } else { 231 } else {
198 ipc_->CreateStream(stream_id_, audio_parameters_, device_id, 232 ipc_->CreateStream(stream_id_, audio_parameters_, device_id,
199 agc_is_enabled_); 233 agc_is_enabled_, kRequestdSharedMemoryCount);
200 } 234 }
201 235
202 pending_device_ready_ = false; 236 pending_device_ready_ = false;
203 // Notify the client that the device has been started. 237 // Notify the client that the device has been started.
204 if (event_handler_) 238 if (event_handler_)
205 event_handler_->OnDeviceStarted(device_id); 239 event_handler_->OnDeviceStarted(device_id);
206 } 240 }
207 241
208 void AudioInputDevice::OnIPCClosed() { 242 void AudioInputDevice::OnIPCClosed() {
209 ipc_ = NULL; 243 ipc_ = NULL;
(...skipping 11 matching lines...) Expand all
221 DCHECK_EQ(0, stream_id_); 255 DCHECK_EQ(0, stream_id_);
222 if (stream_id_) 256 if (stream_id_)
223 return; 257 return;
224 258
225 stream_id_ = ipc_->AddDelegate(this); 259 stream_id_ = ipc_->AddDelegate(this);
226 // If |session_id_| is not specified, it will directly create the stream; 260 // If |session_id_| is not specified, it will directly create the stream;
227 // otherwise it will send a AudioInputHostMsg_StartDevice msg to the browser 261 // otherwise it will send a AudioInputHostMsg_StartDevice msg to the browser
228 // and create the stream when getting a OnDeviceReady() callback. 262 // and create the stream when getting a OnDeviceReady() callback.
229 if (!session_id_) { 263 if (!session_id_) {
230 ipc_->CreateStream(stream_id_, audio_parameters_, 264 ipc_->CreateStream(stream_id_, audio_parameters_,
231 AudioManagerBase::kDefaultDeviceId, agc_is_enabled_); 265 AudioManagerBase::kDefaultDeviceId, agc_is_enabled_,
266 kRequestdSharedMemoryCount);
232 } else { 267 } else {
233 ipc_->StartDevice(stream_id_, session_id_); 268 ipc_->StartDevice(stream_id_, session_id_);
234 pending_device_ready_ = true; 269 pending_device_ready_ = true;
235 } 270 }
236 } 271 }
237 272
238 void AudioInputDevice::SetSessionIdOnIOThread(int session_id) { 273 void AudioInputDevice::SetSessionIdOnIOThread(int session_id) {
239 DCHECK(message_loop()->BelongsToCurrentThread()); 274 DCHECK(message_loop()->BelongsToCurrentThread());
240 session_id_ = session_id; 275 session_id_ = session_id;
241 } 276 }
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
293 } 328 }
294 329
295 void AudioInputDevice::WillDestroyCurrentMessageLoop() { 330 void AudioInputDevice::WillDestroyCurrentMessageLoop() {
296 LOG(ERROR) << "IO loop going away before the input device has been stopped"; 331 LOG(ERROR) << "IO loop going away before the input device has been stopped";
297 ShutDownOnIOThread(); 332 ShutDownOnIOThread();
298 } 333 }
299 334
300 // AudioInputDevice::AudioThreadCallback 335 // AudioInputDevice::AudioThreadCallback
301 AudioInputDevice::AudioThreadCallback::AudioThreadCallback( 336 AudioInputDevice::AudioThreadCallback::AudioThreadCallback(
302 const AudioParameters& audio_parameters, 337 const AudioParameters& audio_parameters,
303 base::SharedMemoryHandle memory, 338 SharedMemoryHandleVector memory,
304 int memory_length, 339 int memory_length,
305 CaptureCallback* capture_callback) 340 CaptureCallback* capture_callback)
306 : AudioDeviceThread::Callback(audio_parameters, memory, memory_length), 341 : AudioDeviceThread::Callback(audio_parameters, memory, memory_length),
307 capture_callback_(capture_callback) { 342 capture_callback_(capture_callback) {
308 audio_bus_ = AudioBus::Create(audio_parameters_); 343 audio_bus_ = AudioBus::Create(audio_parameters_);
309 } 344 }
310 345
311 AudioInputDevice::AudioThreadCallback::~AudioThreadCallback() { 346 AudioInputDevice::AudioThreadCallback::~AudioThreadCallback() {
312 } 347 }
313 348
314 void AudioInputDevice::AudioThreadCallback::MapSharedMemory() { 349 void AudioInputDevice::AudioThreadCallback::MapSharedMemory() {
315 shared_memory_.Map(memory_length_); 350 for (size_t id = 0; id < shared_memory_.size(); ++id) {
351 shared_memory_[id]->Map(memory_length_);
352 }
316 } 353 }
317 354
318 void AudioInputDevice::AudioThreadCallback::Process(int pending_data) { 355 void AudioInputDevice::AudioThreadCallback::Process(
356 int pending_data, int index) {
319 // The shared memory represents parameters, size of the data buffer and the 357 // The shared memory represents parameters, size of the data buffer and the
320 // actual data buffer containing audio data. Map the memory into this 358 // actual data buffer containing audio data. Map the memory into this
321 // structure and parse out parameters and the data area. 359 // structure and parse out parameters and the data area.
322 AudioInputBuffer* buffer = 360 AudioInputBuffer* buffer =
323 reinterpret_cast<AudioInputBuffer*>(shared_memory_.memory()); 361 reinterpret_cast<AudioInputBuffer*>(shared_memory_[index]->memory());
324 DCHECK_EQ(buffer->params.size, 362 DCHECK_EQ(buffer->params.size,
325 memory_length_ - sizeof(AudioInputBufferParameters)); 363 memory_length_ - sizeof(AudioInputBufferParameters));
326 double volume = buffer->params.volume; 364 double volume = buffer->params.volume;
327 365
328 int audio_delay_milliseconds = pending_data / bytes_per_ms_; 366 int audio_delay_milliseconds = pending_data / bytes_per_ms_;
329 int16* memory = reinterpret_cast<int16*>(&buffer->audio[0]); 367 int16* memory = reinterpret_cast<int16*>(&buffer->audio[0]);
330 const int bytes_per_sample = sizeof(memory[0]); 368 const int bytes_per_sample = sizeof(memory[0]);
331 369
332 // Deinterleave each channel and convert to 32-bit floating-point 370 // Deinterleave each channel and convert to 32-bit floating-point
333 // with nominal range -1.0 -> +1.0. 371 // with nominal range -1.0 -> +1.0.
334 audio_bus_->FromInterleaved(memory, audio_bus_->frames(), bytes_per_sample); 372 audio_bus_->FromInterleaved(memory, audio_bus_->frames(), bytes_per_sample);
335 373
336 // Deliver captured data to the client in floating point format 374 // Deliver captured data to the client in floating point format
337 // and update the audio-delay measurement. 375 // and update the audio-delay measurement.
338 capture_callback_->Capture(audio_bus_.get(), 376 capture_callback_->Capture(audio_bus_.get(),
339 audio_delay_milliseconds, volume); 377 audio_delay_milliseconds, volume);
340 } 378 }
341 379
342 } // namespace media 380 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698