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

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

Issue 22886005: Switch audio synchronization from sleep() based to select() based. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Comments. Split out base/ changes. Created 7 years, 3 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_output_device.h" 5 #include "media/audio/audio_output_device.h"
6 6
7 #include "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "base/debug/trace_event.h" 8 #include "base/debug/trace_event.h"
9 #include "base/message_loop/message_loop.h" 9 #include "base/message_loop/message_loop.h"
10 #include "base/threading/thread_restrictions.h" 10 #include "base/threading/thread_restrictions.h"
11 #include "base/time/time.h" 11 #include "base/time/time.h"
12 #include "media/audio/audio_output_controller.h" 12 #include "media/audio/audio_output_controller.h"
13 #include "media/audio/audio_util.h" 13 #include "media/audio/audio_util.h"
14 #include "media/audio/shared_memory_util.h"
15 #include "media/base/limits.h" 14 #include "media/base/limits.h"
16 15
17 namespace media { 16 namespace media {
18 17
19 // Takes care of invoking the render callback on the audio thread. 18 // Takes care of invoking the render callback on the audio thread.
20 // An instance of this class is created for each capture stream in 19 // An instance of this class is created for each capture stream in
21 // OnStreamCreated(). 20 // OnStreamCreated().
22 class AudioOutputDevice::AudioThreadCallback 21 class AudioOutputDevice::AudioThreadCallback
23 : public AudioDeviceThread::Callback { 22 : public AudioDeviceThread::Callback {
24 public: 23 public:
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after
238 // delete as they see fit. AudioOutputDevice should internally use WeakPtr 237 // delete as they see fit. AudioOutputDevice should internally use WeakPtr
239 // to handle teardown and thread hopping. See http://crbug.com/151051 for 238 // to handle teardown and thread hopping. See http://crbug.com/151051 for
240 // details. 239 // details.
241 base::AutoLock auto_lock(audio_thread_lock_); 240 base::AutoLock auto_lock(audio_thread_lock_);
242 if (stopping_hack_) 241 if (stopping_hack_)
243 return; 242 return;
244 243
245 DCHECK(audio_thread_.IsStopped()); 244 DCHECK(audio_thread_.IsStopped());
246 audio_callback_.reset(new AudioOutputDevice::AudioThreadCallback( 245 audio_callback_.reset(new AudioOutputDevice::AudioThreadCallback(
247 audio_parameters_, handle, length, callback_)); 246 audio_parameters_, handle, length, callback_));
248 audio_thread_.Start(audio_callback_.get(), socket_handle, 247 audio_thread_.Start(
249 "AudioOutputDevice"); 248 audio_callback_.get(), socket_handle, "AudioOutputDevice", true);
250 state_ = PAUSED; 249 state_ = PAUSED;
251 250
252 // We handle the case where Play() and/or Pause() may have been called 251 // We handle the case where Play() and/or Pause() may have been called
253 // multiple times before OnStreamCreated() gets called. 252 // multiple times before OnStreamCreated() gets called.
254 if (play_on_start_) 253 if (play_on_start_)
255 PlayOnIOThread(); 254 PlayOnIOThread();
256 } 255 }
257 256
258 void AudioOutputDevice::OnIPCClosed() { 257 void AudioOutputDevice::OnIPCClosed() {
259 DCHECK(message_loop()->BelongsToCurrentThread()); 258 DCHECK(message_loop()->BelongsToCurrentThread());
260 state_ = IPC_CLOSED; 259 state_ = IPC_CLOSED;
261 ipc_.reset(); 260 ipc_.reset();
262 } 261 }
263 262
264 void AudioOutputDevice::WillDestroyCurrentMessageLoop() { 263 void AudioOutputDevice::WillDestroyCurrentMessageLoop() {
265 LOG(ERROR) << "IO loop going away before the audio device has been stopped"; 264 LOG(ERROR) << "IO loop going away before the audio device has been stopped";
266 ShutDownOnIOThread(); 265 ShutDownOnIOThread();
267 } 266 }
268 267
269 // AudioOutputDevice::AudioThreadCallback 268 // AudioOutputDevice::AudioThreadCallback
270 269
271 AudioOutputDevice::AudioThreadCallback::AudioThreadCallback( 270 AudioOutputDevice::AudioThreadCallback::AudioThreadCallback(
272 const AudioParameters& audio_parameters, 271 const AudioParameters& audio_parameters,
273 base::SharedMemoryHandle memory, 272 base::SharedMemoryHandle memory,
274 int memory_length, 273 int memory_length,
275 AudioRendererSink::RenderCallback* render_callback) 274 AudioRendererSink::RenderCallback* render_callback)
276 : AudioDeviceThread::Callback(audio_parameters, 275 : AudioDeviceThread::Callback(audio_parameters, memory, memory_length, 1),
277 memory, 276 render_callback_(render_callback) {}
278 memory_length,
279 1),
280 render_callback_(render_callback) {
281 }
282 277
283 AudioOutputDevice::AudioThreadCallback::~AudioThreadCallback() { 278 AudioOutputDevice::AudioThreadCallback::~AudioThreadCallback() {
284 } 279 }
285 280
286 void AudioOutputDevice::AudioThreadCallback::MapSharedMemory() { 281 void AudioOutputDevice::AudioThreadCallback::MapSharedMemory() {
287 CHECK_EQ(total_segments_, 1); 282 CHECK_EQ(total_segments_, 1);
288 CHECK(shared_memory_.Map(TotalSharedMemorySizeInBytes(memory_length_))); 283 CHECK(shared_memory_.Map(memory_length_));
289 284
290 // Calculate output and input memory size. 285 // Calculate output and input memory size.
291 int output_memory_size = AudioBus::CalculateMemorySize(audio_parameters_); 286 int output_memory_size = AudioBus::CalculateMemorySize(audio_parameters_);
292 int input_channels = audio_parameters_.input_channels(); 287 int input_channels = audio_parameters_.input_channels();
293 int frames = audio_parameters_.frames_per_buffer(); 288 int frames = audio_parameters_.frames_per_buffer();
294 int input_memory_size = 289 int input_memory_size = AudioBus::CalculateMemorySize(input_channels, frames);
295 AudioBus::CalculateMemorySize(input_channels, frames);
296 290
297 int io_size = output_memory_size + input_memory_size; 291 int io_size = output_memory_size + input_memory_size;
298 292
299 DCHECK_EQ(memory_length_, io_size); 293 DCHECK_EQ(memory_length_, io_size);
300 294
301 output_bus_ = 295 output_bus_ =
302 AudioBus::WrapMemory(audio_parameters_, shared_memory_.memory()); 296 AudioBus::WrapMemory(audio_parameters_, shared_memory_.memory());
303 297
304 if (input_channels > 0) { 298 if (input_channels > 0) {
305 // The input data is after the output data. 299 // The input data is after the output data.
306 char* input_data = 300 char* input_data =
307 static_cast<char*>(shared_memory_.memory()) + output_memory_size; 301 static_cast<char*>(shared_memory_.memory()) + output_memory_size;
308 input_bus_ = 302 input_bus_ = AudioBus::WrapMemory(input_channels, frames, input_data);
309 AudioBus::WrapMemory(input_channels, frames, input_data);
310 } 303 }
311 } 304 }
312 305
313 // Called whenever we receive notifications about pending data. 306 // Called whenever we receive notifications about pending data.
314 void AudioOutputDevice::AudioThreadCallback::Process(int pending_data) { 307 void AudioOutputDevice::AudioThreadCallback::Process(int pending_data) {
315 if (pending_data == kPauseMark) { 308 // Negative |pending_data| indicates the browser side stream has stopped.
316 memset(shared_memory_.memory(), 0, memory_length_); 309 if (pending_data < 0)
317 SetActualDataSizeInBytes(&shared_memory_, memory_length_, 0);
318 return; 310 return;
319 }
320 311
321 // Convert the number of pending bytes in the render buffer 312 // Convert the number of pending bytes in the render buffer into milliseconds.
322 // into milliseconds.
323 int audio_delay_milliseconds = pending_data / bytes_per_ms_; 313 int audio_delay_milliseconds = pending_data / bytes_per_ms_;
324 314
325 TRACE_EVENT0("audio", "AudioOutputDevice::FireRenderCallback"); 315 TRACE_EVENT0("audio", "AudioOutputDevice::FireRenderCallback");
326 316
327 // Update the audio-delay measurement then ask client to render audio. Since 317 // Update the audio-delay measurement then ask client to render audio. Since
328 // |output_bus_| is wrapping the shared memory the Render() call is writing 318 // |output_bus_| is wrapping the shared memory the Render() call is writing
329 // directly into the shared memory. 319 // directly into the shared memory.
330 int input_channels = audio_parameters_.input_channels(); 320 int input_channels = audio_parameters_.input_channels();
331 size_t num_frames = audio_parameters_.frames_per_buffer(); 321 if (input_bus_ && input_channels > 0) {
332 322 render_callback_->RenderIO(
333 if (input_bus_.get() && input_channels > 0) { 323 input_bus_.get(), output_bus_.get(), audio_delay_milliseconds);
334 render_callback_->RenderIO(input_bus_.get(),
335 output_bus_.get(),
336 audio_delay_milliseconds);
337 } else { 324 } else {
338 num_frames = render_callback_->Render(output_bus_.get(), 325 render_callback_->Render(output_bus_.get(), audio_delay_milliseconds);
339 audio_delay_milliseconds);
340 } 326 }
341
342 // Let the host know we are done.
343 // TODO(dalecurtis): Technically this is not always correct. Due to channel
344 // padding for alignment, there may be more data available than this. We're
345 // relying on AudioSyncReader::Read() to parse this with that in mind. Rename
346 // these methods to Set/GetActualFrameCount().
347 SetActualDataSizeInBytes(
348 &shared_memory_, memory_length_,
349 num_frames * sizeof(*output_bus_->channel(0)) * output_bus_->channels());
350 } 327 }
351 328
352 } // namespace media. 329 } // namespace media.
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698