| 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 "content/renderer/media/audio_device.h" | 5 #include "content/renderer/media/audio_device.h" |
| 6 | 6 |
| 7 #include "base/debug/trace_event.h" | 7 #include "base/debug/trace_event.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" |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 } | 62 } |
| 63 | 63 |
| 64 void AudioDevice::Initialize(const media::AudioParameters& params, | 64 void AudioDevice::Initialize(const media::AudioParameters& params, |
| 65 RenderCallback* callback) { | 65 RenderCallback* callback) { |
| 66 CHECK_EQ(0, stream_id_) << | 66 CHECK_EQ(0, stream_id_) << |
| 67 "AudioDevice::Initialize() must be called before Start()"; | 67 "AudioDevice::Initialize() must be called before Start()"; |
| 68 | 68 |
| 69 CHECK(!callback_); // Calling Initialize() twice? | 69 CHECK(!callback_); // Calling Initialize() twice? |
| 70 | 70 |
| 71 audio_parameters_ = params; | 71 audio_parameters_ = params; |
| 72 audio_parameters_.Reset( | |
| 73 params.format(), | |
| 74 params.channel_layout(), params.sample_rate(), params.bits_per_sample(), | |
| 75 params.frames_per_buffer()); | |
| 76 callback_ = callback; | 72 callback_ = callback; |
| 77 } | 73 } |
| 78 | 74 |
| 79 AudioDevice::~AudioDevice() { | 75 AudioDevice::~AudioDevice() { |
| 80 // The current design requires that the user calls Stop() before deleting | 76 // The current design requires that the user calls Stop() before deleting |
| 81 // this class. | 77 // this class. |
| 82 CHECK_EQ(0, stream_id_); | 78 CHECK_EQ(0, stream_id_); |
| 83 } | 79 } |
| 84 | 80 |
| 85 void AudioDevice::Start() { | 81 void AudioDevice::Start() { |
| 86 DCHECK(callback_) << "Initialize hasn't been called"; | 82 DCHECK(callback_) << "Initialize hasn't been called"; |
| 87 message_loop()->PostTask(FROM_HERE, | 83 message_loop()->PostTask(FROM_HERE, |
| 88 base::Bind(&AudioDevice::InitializeOnIOThread, this, audio_parameters_)); | 84 base::Bind(&AudioDevice::CreateStreamOnIOThread, this, |
| 85 audio_parameters_)); |
| 89 } | 86 } |
| 90 | 87 |
| 91 void AudioDevice::Stop() { | 88 void AudioDevice::Stop() { |
| 92 { | 89 { |
| 93 base::AutoLock auto_lock(audio_thread_lock_); | 90 base::AutoLock auto_lock(audio_thread_lock_); |
| 94 audio_thread_.Stop(MessageLoop::current()); | 91 audio_thread_.Stop(MessageLoop::current()); |
| 95 } | 92 } |
| 96 | 93 |
| 97 message_loop()->PostTask(FROM_HERE, | 94 message_loop()->PostTask(FROM_HERE, |
| 98 base::Bind(&AudioDevice::ShutDownOnIOThread, this)); | 95 base::Bind(&AudioDevice::ShutDownOnIOThread, this)); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 120 volume_ = volume; | 117 volume_ = volume; |
| 121 | 118 |
| 122 return true; | 119 return true; |
| 123 } | 120 } |
| 124 | 121 |
| 125 void AudioDevice::GetVolume(double* volume) { | 122 void AudioDevice::GetVolume(double* volume) { |
| 126 // Return a locally cached version of the current scaling factor. | 123 // Return a locally cached version of the current scaling factor. |
| 127 *volume = volume_; | 124 *volume = volume_; |
| 128 } | 125 } |
| 129 | 126 |
| 130 void AudioDevice::InitializeOnIOThread(const media::AudioParameters& params) { | 127 void AudioDevice::CreateStreamOnIOThread(const media::AudioParameters& params) { |
| 131 DCHECK(message_loop()->BelongsToCurrentThread()); | 128 DCHECK(message_loop()->BelongsToCurrentThread()); |
| 132 // Make sure we don't create the stream more than once. | 129 // Make sure we don't create the stream more than once. |
| 133 DCHECK_EQ(0, stream_id_); | 130 DCHECK_EQ(0, stream_id_); |
| 134 if (stream_id_) | 131 if (stream_id_) |
| 135 return; | 132 return; |
| 136 | 133 |
| 137 stream_id_ = filter_->AddDelegate(this); | 134 stream_id_ = filter_->AddDelegate(this); |
| 138 Send(new AudioHostMsg_CreateStream(stream_id_, params)); | 135 Send(new AudioHostMsg_CreateStream(stream_id_, params)); |
| 139 } | 136 } |
| 140 | 137 |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 295 // TODO(crogers/vrk): Figure out a way to avoid the float -> int -> float | 292 // TODO(crogers/vrk): Figure out a way to avoid the float -> int -> float |
| 296 // conversions that happen in the <audio> and WebRTC scenarios. | 293 // conversions that happen in the <audio> and WebRTC scenarios. |
| 297 media::InterleaveFloatToInt(audio_data_, shared_memory_.memory(), | 294 media::InterleaveFloatToInt(audio_data_, shared_memory_.memory(), |
| 298 audio_parameters_.frames_per_buffer(), | 295 audio_parameters_.frames_per_buffer(), |
| 299 audio_parameters_.bits_per_sample() / 8); | 296 audio_parameters_.bits_per_sample() / 8); |
| 300 | 297 |
| 301 // Let the host know we are done. | 298 // Let the host know we are done. |
| 302 media::SetActualDataSizeInBytes(&shared_memory_, memory_length_, | 299 media::SetActualDataSizeInBytes(&shared_memory_, memory_length_, |
| 303 num_frames * audio_parameters_.GetBytesPerFrame()); | 300 num_frames * audio_parameters_.GetBytesPerFrame()); |
| 304 } | 301 } |
| OLD | NEW |