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