Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/debug/trace_event.h" | 8 #include "base/debug/trace_event.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/time.h" | 10 #include "base/time.h" |
| 11 #include "content/common/child_process.h" | 11 #include "content/common/child_process.h" |
| 12 #include "content/common/media/audio_messages.h" | 12 #include "content/common/media/audio_messages.h" |
| 13 #include "content/common/view_messages.h" | 13 #include "content/common/view_messages.h" |
| 14 #include "content/renderer/render_thread_impl.h" | 14 #include "content/renderer/render_thread_impl.h" |
| 15 #include "media/audio/audio_util.h" | 15 #include "media/audio/audio_util.h" |
| 16 | 16 |
| 17 AudioDevice::AudioDevice(size_t buffer_size, | 17 AudioDevice::AudioDevice(size_t buffer_size, |
| 18 int channels, | 18 int channels, |
| 19 double sample_rate, | 19 double sample_rate, |
| 20 RenderCallback* callback) | 20 RenderCallback* callback) |
| 21 : buffer_size_(buffer_size), | 21 : buffer_size_(buffer_size), |
| 22 channels_(channels), | 22 channels_(channels), |
| 23 bits_per_sample_(16), | 23 bits_per_sample_(16), |
| 24 sample_rate_(sample_rate), | 24 sample_rate_(sample_rate), |
| 25 callback_(callback), | 25 callback_(callback), |
| 26 audio_delay_milliseconds_(0), | 26 audio_delay_milliseconds_(0), |
| 27 volume_(1.0), | 27 volume_(1.0), |
| 28 stream_id_(0) { | 28 stream_id_(0), |
| 29 memory_length_(0) { | |
| 29 filter_ = RenderThreadImpl::current()->audio_message_filter(); | 30 filter_ = RenderThreadImpl::current()->audio_message_filter(); |
| 30 audio_data_.reserve(channels); | 31 audio_data_.reserve(channels); |
| 31 for (int i = 0; i < channels; ++i) { | 32 for (int i = 0; i < channels; ++i) { |
| 32 float* channel_data = new float[buffer_size]; | 33 float* channel_data = new float[buffer_size]; |
| 33 audio_data_.push_back(channel_data); | 34 audio_data_.push_back(channel_data); |
| 34 } | 35 } |
| 35 } | 36 } |
| 36 | 37 |
| 37 AudioDevice::~AudioDevice() { | 38 AudioDevice::~AudioDevice() { |
| 38 // The current design requires that the user calls Stop() before deleting | 39 // The current design requires that the user calls Stop() before deleting |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 49 params.sample_rate = static_cast<int>(sample_rate_); | 50 params.sample_rate = static_cast<int>(sample_rate_); |
| 50 params.bits_per_sample = bits_per_sample_; | 51 params.bits_per_sample = bits_per_sample_; |
| 51 params.samples_per_packet = buffer_size_; | 52 params.samples_per_packet = buffer_size_; |
| 52 | 53 |
| 53 ChildProcess::current()->io_message_loop()->PostTask( | 54 ChildProcess::current()->io_message_loop()->PostTask( |
| 54 FROM_HERE, | 55 FROM_HERE, |
| 55 base::Bind(&AudioDevice::InitializeOnIOThread, this, params)); | 56 base::Bind(&AudioDevice::InitializeOnIOThread, this, params)); |
| 56 } | 57 } |
| 57 | 58 |
| 58 bool AudioDevice::Stop() { | 59 bool AudioDevice::Stop() { |
| 60 DCHECK(MessageLoop::current() != ChildProcess::current()->io_message_loop()); | |
| 59 // Max waiting time for Stop() to complete. If this time limit is passed, | 61 // Max waiting time for Stop() to complete. If this time limit is passed, |
| 60 // we will stop waiting and return false. It ensures that Stop() can't block | 62 // we will stop waiting and return false. It ensures that Stop() can't block |
| 61 // the calling thread forever. | 63 // the calling thread forever. |
| 62 const base::TimeDelta kMaxTimeOut = base::TimeDelta::FromMilliseconds(1000); | 64 const base::TimeDelta kMaxTimeOut = base::TimeDelta::FromMilliseconds(1000); |
| 63 | 65 |
| 64 base::WaitableEvent completion(false, false); | 66 base::WaitableEvent completion(false, false); |
| 65 | 67 |
| 66 ChildProcess::current()->io_message_loop()->PostTask( | 68 ChildProcess::current()->io_message_loop()->PostTask( |
| 67 FROM_HERE, | 69 FROM_HERE, |
| 68 base::Bind(&AudioDevice::ShutDownOnIOThread, this, &completion)); | 70 base::Bind(&AudioDevice::ShutDownOnIOThread, this, &completion)); |
| 69 | 71 |
| 70 // We wait here for the IO task to be completed to remove race conflicts | 72 // We wait here for the IO task to be completed to remove race conflicts |
| 71 // with OnLowLatencyCreated() and to ensure that Stop() acts as a synchronous | 73 // with OnLowLatencyCreated() and to ensure that Stop() acts as a synchronous |
| 72 // function call. | 74 // function call. |
| 73 if (completion.TimedWait(kMaxTimeOut)) { | 75 if (!completion.TimedWait(kMaxTimeOut)) { |
| 74 if (audio_thread_.get()) { | 76 LOG(WARNING) << "Failed to shut down audio output on IO thread"; |
|
tommi (sloooow) - chröme
2011/12/01 14:40:45
LOG(ERROR)?
no longer working on chromium
2011/12/02 10:16:30
Done.
| |
| 75 socket_->Close(); | 77 } |
| 76 audio_thread_->Join(); | 78 |
| 77 audio_thread_.reset(NULL); | 79 if (audio_thread_.get()) { |
| 80 { | |
| 81 base::SyncSocket socket(socket_handle_); | |
|
henrika (OOO until Aug 14)
2011/12/01 14:57:13
A comment perhaps?
no longer working on chromium
2011/12/02 10:16:30
Done.
| |
| 78 } | 82 } |
| 79 } else { | 83 audio_thread_->Join(); |
| 80 LOG(ERROR) << "Failed to shut down audio output on IO thread"; | 84 audio_thread_.reset(NULL); |
| 81 return false; | |
| 82 } | 85 } |
| 83 | 86 |
| 84 return true; | 87 return true; |
|
Ami GONE FROM CHROMIUM
2011/12/01 17:25:28
FWIW, this function can't return false anymore aft
no longer working on chromium
2011/12/02 10:16:30
Done.
| |
| 85 } | 88 } |
| 86 | 89 |
| 87 bool AudioDevice::SetVolume(double volume) { | 90 bool AudioDevice::SetVolume(double volume) { |
| 88 if (volume < 0 || volume > 1.0) | 91 if (volume < 0 || volume > 1.0) |
| 89 return false; | 92 return false; |
| 90 | 93 |
| 91 ChildProcess::current()->io_message_loop()->PostTask( | 94 ChildProcess::current()->io_message_loop()->PostTask( |
| 92 FROM_HERE, | 95 FROM_HERE, |
| 93 base::Bind(&AudioDevice::SetVolumeOnIOThread, this, volume)); | 96 base::Bind(&AudioDevice::SetVolumeOnIOThread, this, volume)); |
| 94 | 97 |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 169 DCHECK(length); | 172 DCHECK(length); |
| 170 | 173 |
| 171 // Takes care of the case when Stop() is called before OnLowLatencyCreated(). | 174 // Takes care of the case when Stop() is called before OnLowLatencyCreated(). |
| 172 if (!stream_id_) { | 175 if (!stream_id_) { |
| 173 base::SharedMemory::CloseHandle(handle); | 176 base::SharedMemory::CloseHandle(handle); |
| 174 // Close the socket handler. | 177 // Close the socket handler. |
| 175 base::SyncSocket socket(socket_handle); | 178 base::SyncSocket socket(socket_handle); |
| 176 return; | 179 return; |
| 177 } | 180 } |
| 178 | 181 |
| 179 shared_memory_.reset(new base::SharedMemory(handle, false)); | 182 shared_memory_handle_ = handle; |
| 180 shared_memory_->Map(length); | 183 memory_length_ = length; |
| 181 | 184 |
| 182 DCHECK_GE(length, buffer_size_ * sizeof(int16) * channels_); | 185 DCHECK_GE(length, buffer_size_ * sizeof(int16) * channels_); |
| 183 | 186 |
| 184 socket_.reset(new base::SyncSocket(socket_handle)); | 187 socket_handle_ = socket_handle; |
| 185 // Allow the client to pre-populate the buffer. | |
| 186 FireRenderCallback(); | |
| 187 | 188 |
| 188 audio_thread_.reset( | 189 audio_thread_.reset( |
|
tommi (sloooow) - chröme
2011/12/01 14:40:45
maybe we should dcheck at the top that the audio t
no longer working on chromium
2011/12/02 10:16:30
Done.
| |
| 189 new base::DelegateSimpleThread(this, "renderer_audio_thread")); | 190 new base::DelegateSimpleThread(this, "renderer_audio_thread")); |
| 190 audio_thread_->Start(); | 191 audio_thread_->Start(); |
| 191 | 192 |
| 192 MessageLoop::current()->PostTask( | 193 MessageLoop::current()->PostTask( |
| 193 FROM_HERE, | 194 FROM_HERE, |
| 194 base::Bind(&AudioDevice::StartOnIOThread, this)); | 195 base::Bind(&AudioDevice::StartOnIOThread, this)); |
| 195 } | 196 } |
| 196 | 197 |
| 197 void AudioDevice::OnVolume(double volume) { | 198 void AudioDevice::OnVolume(double volume) { |
| 198 NOTIMPLEMENTED(); | 199 NOTIMPLEMENTED(); |
| 199 } | 200 } |
| 200 | 201 |
| 201 void AudioDevice::Send(IPC::Message* message) { | 202 void AudioDevice::Send(IPC::Message* message) { |
| 202 filter_->Send(message); | 203 filter_->Send(message); |
| 203 } | 204 } |
| 204 | 205 |
| 205 // Our audio thread runs here. | 206 // Our audio thread runs here. |
| 206 void AudioDevice::Run() { | 207 void AudioDevice::Run() { |
| 207 audio_thread_->SetThreadPriority(base::kThreadPriority_RealtimeAudio); | 208 audio_thread_->SetThreadPriority(base::kThreadPriority_RealtimeAudio); |
| 208 | 209 |
| 210 base::SharedMemory shared_memory(shared_memory_handle_, false); | |
| 211 shared_memory.Map(memory_length_); | |
| 212 // Allow the client to pre-populate the buffer. | |
| 213 FireRenderCallback(static_cast<int16*>(shared_memory.memory())); | |
|
tommi (sloooow) - chröme
2011/12/01 14:40:45
reinterpret_cast?
no longer working on chromium
2011/12/02 10:16:30
Done.
| |
| 214 | |
| 215 base::SyncSocket socket(socket_handle_); | |
| 216 | |
| 209 int pending_data; | 217 int pending_data; |
| 210 const int samples_per_ms = static_cast<int>(sample_rate_) / 1000; | 218 const int samples_per_ms = static_cast<int>(sample_rate_) / 1000; |
| 211 const int bytes_per_ms = channels_ * (bits_per_sample_ / 8) * samples_per_ms; | 219 const int bytes_per_ms = channels_ * (bits_per_sample_ / 8) * samples_per_ms; |
| 212 | 220 |
| 213 while ((sizeof(pending_data) == socket_->Receive(&pending_data, | 221 while ((sizeof(pending_data) == socket.Receive(&pending_data, |
| 214 sizeof(pending_data))) && | 222 sizeof(pending_data))) && |
| 215 (pending_data >= 0)) { | 223 (pending_data >= 0)) { |
| 216 // Convert the number of pending bytes in the render buffer | 224 // Convert the number of pending bytes in the render buffer |
| 217 // into milliseconds. | 225 // into milliseconds. |
| 218 audio_delay_milliseconds_ = pending_data / bytes_per_ms; | 226 audio_delay_milliseconds_ = pending_data / bytes_per_ms; |
| 219 FireRenderCallback(); | 227 FireRenderCallback(static_cast<int16*>(shared_memory.memory())); |
|
tommi (sloooow) - chröme
2011/12/01 14:40:45
same here
no longer working on chromium
2011/12/02 10:16:30
Done.
| |
| 220 } | 228 } |
| 221 } | 229 } |
| 222 | 230 |
| 223 void AudioDevice::FireRenderCallback() { | 231 void AudioDevice::FireRenderCallback(int16* data) { |
| 224 TRACE_EVENT0("audio", "AudioDevice::FireRenderCallback"); | 232 TRACE_EVENT0("audio", "AudioDevice::FireRenderCallback"); |
| 225 | 233 |
| 226 if (callback_) { | 234 if (callback_) { |
| 227 // Update the audio-delay measurement then ask client to render audio. | 235 // Update the audio-delay measurement then ask client to render audio. |
| 228 callback_->Render(audio_data_, buffer_size_, audio_delay_milliseconds_); | 236 callback_->Render(audio_data_, buffer_size_, audio_delay_milliseconds_); |
| 229 | 237 |
| 230 // Interleave, scale, and clip to int16. | 238 // Interleave, scale, and clip to int16. |
| 231 media::InterleaveFloatToInt16(audio_data_, | 239 media::InterleaveFloatToInt16(audio_data_, |
| 232 static_cast<int16*>(shared_memory_data()), | 240 data, |
| 233 buffer_size_); | 241 buffer_size_); |
| 234 } | 242 } |
| 235 } | 243 } |
| OLD | NEW |