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_output_controller.h" |
15 #include "media/audio/audio_util.h" | 16 #include "media/audio/audio_util.h" |
16 | 17 |
| 18 AudioDevice::AudioDevice() |
| 19 : buffer_size_(0), |
| 20 channels_(0), |
| 21 bits_per_sample_(16), |
| 22 sample_rate_(0), |
| 23 latency_format_(AudioParameters::AUDIO_PCM_LOW_LATENCY), |
| 24 callback_(0), |
| 25 is_initialized_(false), |
| 26 audio_delay_milliseconds_(0), |
| 27 volume_(1.0), |
| 28 stream_id_(0), |
| 29 play_on_start_(true), |
| 30 is_started_(false), |
| 31 shared_memory_size_(0) { |
| 32 filter_ = RenderThreadImpl::current()->audio_message_filter(); |
| 33 } |
| 34 |
17 AudioDevice::AudioDevice(size_t buffer_size, | 35 AudioDevice::AudioDevice(size_t buffer_size, |
18 int channels, | 36 int channels, |
19 double sample_rate, | 37 double sample_rate, |
20 RenderCallback* callback) | 38 RenderCallback* callback) |
21 : buffer_size_(buffer_size), | 39 : bits_per_sample_(16), |
22 channels_(channels), | 40 is_initialized_(false), |
23 bits_per_sample_(16), | |
24 sample_rate_(sample_rate), | |
25 callback_(callback), | |
26 audio_delay_milliseconds_(0), | 41 audio_delay_milliseconds_(0), |
27 volume_(1.0), | 42 volume_(1.0), |
28 stream_id_(0) { | 43 stream_id_(0), |
| 44 play_on_start_(true), |
| 45 is_started_(false), |
| 46 shared_memory_size_(0) { |
29 filter_ = RenderThreadImpl::current()->audio_message_filter(); | 47 filter_ = RenderThreadImpl::current()->audio_message_filter(); |
| 48 Initialize(buffer_size, |
| 49 channels, |
| 50 sample_rate, |
| 51 AudioParameters::AUDIO_PCM_LOW_LATENCY, |
| 52 callback); |
| 53 } |
| 54 |
| 55 void AudioDevice::Initialize(size_t buffer_size, |
| 56 int channels, |
| 57 double sample_rate, |
| 58 AudioParameters::Format latency_format, |
| 59 RenderCallback* callback) { |
| 60 CHECK_EQ(0, stream_id_) << |
| 61 "AudioDevice::Initialize() must be called before Start()"; |
| 62 |
| 63 buffer_size_ = buffer_size; |
| 64 channels_ = channels; |
| 65 sample_rate_ = sample_rate; |
| 66 latency_format_ = latency_format; |
| 67 callback_ = callback; |
| 68 |
| 69 // Cleanup from any previous initialization. |
| 70 for (size_t i = 0; i < audio_data_.size(); ++i) |
| 71 delete [] audio_data_[i]; |
| 72 |
30 audio_data_.reserve(channels); | 73 audio_data_.reserve(channels); |
31 for (int i = 0; i < channels; ++i) { | 74 for (int i = 0; i < channels; ++i) { |
32 float* channel_data = new float[buffer_size]; | 75 float* channel_data = new float[buffer_size]; |
33 audio_data_.push_back(channel_data); | 76 audio_data_.push_back(channel_data); |
34 } | 77 } |
| 78 |
| 79 is_initialized_ = true; |
| 80 } |
| 81 |
| 82 bool AudioDevice::IsInitialized() { |
| 83 return is_initialized_; |
35 } | 84 } |
36 | 85 |
37 AudioDevice::~AudioDevice() { | 86 AudioDevice::~AudioDevice() { |
38 // The current design requires that the user calls Stop() before deleting | 87 // The current design requires that the user calls Stop() before deleting |
39 // this class. | 88 // this class. |
40 CHECK_EQ(0, stream_id_); | 89 CHECK_EQ(0, stream_id_); |
41 for (int i = 0; i < channels_; ++i) | 90 for (int i = 0; i < channels_; ++i) |
42 delete [] audio_data_[i]; | 91 delete [] audio_data_[i]; |
43 } | 92 } |
44 | 93 |
45 void AudioDevice::Start() { | 94 void AudioDevice::Start() { |
46 AudioParameters params; | 95 AudioParameters params; |
47 params.format = AudioParameters::AUDIO_PCM_LOW_LATENCY; | 96 params.format = latency_format_; |
48 params.channels = channels_; | 97 params.channels = channels_; |
49 params.sample_rate = static_cast<int>(sample_rate_); | 98 params.sample_rate = static_cast<int>(sample_rate_); |
50 params.bits_per_sample = bits_per_sample_; | 99 params.bits_per_sample = bits_per_sample_; |
51 params.samples_per_packet = buffer_size_; | 100 params.samples_per_packet = buffer_size_; |
52 | 101 |
53 ChildProcess::current()->io_message_loop()->PostTask( | 102 ChildProcess::current()->io_message_loop()->PostTask( |
54 FROM_HERE, | 103 FROM_HERE, |
55 base::Bind(&AudioDevice::InitializeOnIOThread, this, params)); | 104 base::Bind(&AudioDevice::InitializeOnIOThread, this, params)); |
56 } | 105 } |
57 | 106 |
58 bool AudioDevice::Stop() { | 107 bool AudioDevice::Stop() { |
59 // Max waiting time for Stop() to complete. If this time limit is passed, | 108 // 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 | 109 // we will stop waiting and return false. It ensures that Stop() can't block |
61 // the calling thread forever. | 110 // the calling thread forever. |
62 const base::TimeDelta kMaxTimeOut = base::TimeDelta::FromMilliseconds(1000); | 111 const base::TimeDelta kMaxTimeOut = base::TimeDelta::FromMilliseconds(1000); |
63 | 112 |
64 base::WaitableEvent completion(false, false); | 113 base::WaitableEvent completion(false, false); |
65 | 114 |
66 ChildProcess::current()->io_message_loop()->PostTask( | 115 ChildProcess::current()->io_message_loop()->PostTask( |
67 FROM_HERE, | 116 FROM_HERE, |
68 base::Bind(&AudioDevice::ShutDownOnIOThread, this, &completion)); | 117 base::Bind(&AudioDevice::ShutDownOnIOThread, this, &completion)); |
69 | 118 |
70 // We wait here for the IO task to be completed to remove race conflicts | 119 // 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 | 120 // with OnLowLatencyCreated() and to ensure that Stop() acts as a synchronous |
72 // function call. | 121 // function call. |
73 if (completion.TimedWait(kMaxTimeOut)) { | 122 if (completion.TimedWait(kMaxTimeOut)) { |
74 if (audio_thread_.get()) { | 123 ShutDownAudioThread(); |
75 socket_->Close(); | |
76 audio_thread_->Join(); | |
77 audio_thread_.reset(NULL); | |
78 } | |
79 } else { | 124 } else { |
80 LOG(ERROR) << "Failed to shut down audio output on IO thread"; | 125 LOG(ERROR) << "Failed to shut down audio output on IO thread"; |
81 return false; | 126 return false; |
82 } | 127 } |
83 | 128 |
84 return true; | 129 return true; |
85 } | 130 } |
86 | 131 |
| 132 void AudioDevice::Play() { |
| 133 ChildProcess::current()->io_message_loop()->PostTask( |
| 134 FROM_HERE, |
| 135 base::Bind(&AudioDevice::PlayOnIOThread, this)); |
| 136 } |
| 137 |
| 138 void AudioDevice::Pause(bool flush) { |
| 139 ChildProcess::current()->io_message_loop()->PostTask( |
| 140 FROM_HERE, |
| 141 base::Bind(&AudioDevice::PauseOnIOThread, this, flush)); |
| 142 } |
| 143 |
87 bool AudioDevice::SetVolume(double volume) { | 144 bool AudioDevice::SetVolume(double volume) { |
88 if (volume < 0 || volume > 1.0) | 145 if (volume < 0 || volume > 1.0) |
89 return false; | 146 return false; |
90 | 147 |
91 ChildProcess::current()->io_message_loop()->PostTask( | 148 ChildProcess::current()->io_message_loop()->PostTask( |
92 FROM_HERE, | 149 FROM_HERE, |
93 base::Bind(&AudioDevice::SetVolumeOnIOThread, this, volume)); | 150 base::Bind(&AudioDevice::SetVolumeOnIOThread, this, volume)); |
94 | 151 |
95 volume_ = volume; | 152 volume_ = volume; |
96 | 153 |
97 return true; | 154 return true; |
98 } | 155 } |
99 | 156 |
100 void AudioDevice::GetVolume(double* volume) { | 157 void AudioDevice::GetVolume(double* volume) { |
101 // Return a locally cached version of the current scaling factor. | 158 // Return a locally cached version of the current scaling factor. |
102 *volume = volume_; | 159 *volume = volume_; |
103 } | 160 } |
104 | 161 |
105 void AudioDevice::InitializeOnIOThread(const AudioParameters& params) { | 162 void AudioDevice::InitializeOnIOThread(const AudioParameters& params) { |
106 // Make sure we don't call Start() more than once. | 163 DCHECK(MessageLoop::current() == ChildProcess::current()->io_message_loop()); |
| 164 // Make sure we don't create the stream more than once. |
107 DCHECK_EQ(0, stream_id_); | 165 DCHECK_EQ(0, stream_id_); |
108 if (stream_id_) | 166 if (stream_id_) |
109 return; | 167 return; |
110 | 168 |
111 stream_id_ = filter_->AddDelegate(this); | 169 stream_id_ = filter_->AddDelegate(this); |
112 Send(new AudioHostMsg_CreateStream(stream_id_, params, true)); | 170 Send(new AudioHostMsg_CreateStream(stream_id_, params, true)); |
113 } | 171 } |
114 | 172 |
115 void AudioDevice::StartOnIOThread() { | 173 void AudioDevice::PlayOnIOThread() { |
116 if (stream_id_) | 174 DCHECK(MessageLoop::current() == ChildProcess::current()->io_message_loop()); |
| 175 if (stream_id_ && is_started_) |
117 Send(new AudioHostMsg_PlayStream(stream_id_)); | 176 Send(new AudioHostMsg_PlayStream(stream_id_)); |
| 177 else |
| 178 play_on_start_ = true; |
| 179 } |
| 180 |
| 181 void AudioDevice::PauseOnIOThread(bool flush) { |
| 182 DCHECK(MessageLoop::current() == ChildProcess::current()->io_message_loop()); |
| 183 if (stream_id_ && is_started_) { |
| 184 Send(new AudioHostMsg_PauseStream(stream_id_)); |
| 185 if (flush) |
| 186 Send(new AudioHostMsg_FlushStream(stream_id_)); |
| 187 } else { |
| 188 // Note that |flush| isn't relevant here since this is the case where |
| 189 // the stream is first starting. |
| 190 play_on_start_ = false; |
| 191 } |
118 } | 192 } |
119 | 193 |
120 void AudioDevice::ShutDownOnIOThread(base::WaitableEvent* completion) { | 194 void AudioDevice::ShutDownOnIOThread(base::WaitableEvent* completion) { |
| 195 DCHECK(MessageLoop::current() == ChildProcess::current()->io_message_loop()); |
| 196 is_started_ = false; |
| 197 |
121 // Make sure we don't call shutdown more than once. | 198 // Make sure we don't call shutdown more than once. |
122 if (!stream_id_) { | 199 if (!stream_id_) { |
123 completion->Signal(); | 200 if (completion) |
| 201 completion->Signal(); |
124 return; | 202 return; |
125 } | 203 } |
126 | 204 |
127 filter_->RemoveDelegate(stream_id_); | 205 filter_->RemoveDelegate(stream_id_); |
128 Send(new AudioHostMsg_CloseStream(stream_id_)); | 206 Send(new AudioHostMsg_CloseStream(stream_id_)); |
129 stream_id_ = 0; | 207 stream_id_ = 0; |
130 | 208 |
131 completion->Signal(); | 209 if (completion) |
| 210 completion->Signal(); |
132 } | 211 } |
133 | 212 |
134 void AudioDevice::SetVolumeOnIOThread(double volume) { | 213 void AudioDevice::SetVolumeOnIOThread(double volume) { |
| 214 DCHECK(MessageLoop::current() == ChildProcess::current()->io_message_loop()); |
135 if (stream_id_) | 215 if (stream_id_) |
136 Send(new AudioHostMsg_SetVolume(stream_id_, volume)); | 216 Send(new AudioHostMsg_SetVolume(stream_id_, volume)); |
137 } | 217 } |
138 | 218 |
139 void AudioDevice::OnRequestPacket(AudioBuffersState buffers_state) { | 219 void AudioDevice::OnRequestPacket(AudioBuffersState buffers_state) { |
140 // This method does not apply to the low-latency system. | 220 // This method does not apply to the low-latency system. |
141 NOTIMPLEMENTED(); | |
142 } | 221 } |
143 | 222 |
144 void AudioDevice::OnStateChanged(AudioStreamState state) { | 223 void AudioDevice::OnStateChanged(AudioStreamState state) { |
145 if (state == kAudioStreamError) { | 224 if (state == kAudioStreamError) { |
146 DLOG(WARNING) << "AudioDevice::OnStateChanged(kError)"; | 225 DLOG(WARNING) << "AudioDevice::OnStateChanged(kError)"; |
147 } | 226 } |
148 NOTIMPLEMENTED(); | |
149 } | 227 } |
150 | 228 |
151 void AudioDevice::OnCreated( | 229 void AudioDevice::OnCreated( |
152 base::SharedMemoryHandle handle, uint32 length) { | 230 base::SharedMemoryHandle handle, uint32 length) { |
153 // Not needed in this simple implementation. | 231 // Not needed in this simple implementation. |
154 NOTIMPLEMENTED(); | |
155 } | 232 } |
156 | 233 |
157 void AudioDevice::OnLowLatencyCreated( | 234 void AudioDevice::OnLowLatencyCreated( |
158 base::SharedMemoryHandle handle, | 235 base::SharedMemoryHandle handle, |
159 base::SyncSocket::Handle socket_handle, | 236 base::SyncSocket::Handle socket_handle, |
160 uint32 length) { | 237 uint32 length) { |
161 DCHECK(MessageLoop::current() == ChildProcess::current()->io_message_loop()); | 238 DCHECK(MessageLoop::current() == ChildProcess::current()->io_message_loop()); |
162 #if defined(OS_WIN) | 239 #if defined(OS_WIN) |
163 DCHECK(handle); | 240 DCHECK(handle); |
164 DCHECK(socket_handle); | 241 DCHECK(socket_handle); |
165 #else | 242 #else |
166 DCHECK_GE(handle.fd, 0); | 243 DCHECK_GE(handle.fd, 0); |
167 DCHECK_GE(socket_handle, 0); | 244 DCHECK_GE(socket_handle, 0); |
168 #endif | 245 #endif |
169 DCHECK(length); | 246 DCHECK(length); |
170 | 247 |
171 // Takes care of the case when Stop() is called before OnLowLatencyCreated(). | 248 // Takes care of the case when Stop() is called before OnLowLatencyCreated(). |
172 if (!stream_id_) { | 249 if (!stream_id_) { |
173 base::SharedMemory::CloseHandle(handle); | 250 base::SharedMemory::CloseHandle(handle); |
174 // Close the socket handler. | 251 // Close the socket handler. |
175 base::SyncSocket socket(socket_handle); | 252 base::SyncSocket socket(socket_handle); |
176 return; | 253 return; |
177 } | 254 } |
178 | 255 |
179 shared_memory_.reset(new base::SharedMemory(handle, false)); | 256 shared_memory_.reset(new base::SharedMemory(handle, false)); |
180 shared_memory_->Map(length); | 257 shared_memory_->Map(length); |
| 258 shared_memory_size_ = length; |
181 | 259 |
182 DCHECK_GE(length, buffer_size_ * sizeof(int16) * channels_); | 260 DCHECK_GE(length, buffer_size_ * sizeof(int16) * channels_); |
183 | 261 |
184 socket_.reset(new base::SyncSocket(socket_handle)); | 262 { |
185 // Allow the client to pre-populate the buffer. | 263 // Synchronize with ShutDownAudioThread(). |
186 FireRenderCallback(); | 264 base::AutoLock auto_lock(lock_); |
187 | 265 |
188 audio_thread_.reset( | 266 socket_.reset(new base::SyncSocket(socket_handle)); |
189 new base::DelegateSimpleThread(this, "renderer_audio_thread")); | 267 // Allow the client to pre-populate the buffer. |
190 audio_thread_->Start(); | 268 FireRenderCallback(); |
191 | 269 |
192 MessageLoop::current()->PostTask( | 270 DCHECK(!audio_thread_.get()); |
193 FROM_HERE, | 271 audio_thread_.reset( |
194 base::Bind(&AudioDevice::StartOnIOThread, this)); | 272 new base::DelegateSimpleThread(this, "renderer_audio_thread")); |
| 273 audio_thread_->Start(); |
| 274 } |
| 275 |
| 276 // We handle the case where Play() and/or Pause() may have been called |
| 277 // multiple times before OnLowLatencyCreated() gets called. |
| 278 is_started_ = true; |
| 279 if (play_on_start_) |
| 280 PlayOnIOThread(); |
195 } | 281 } |
196 | 282 |
197 void AudioDevice::OnVolume(double volume) { | 283 void AudioDevice::OnVolume(double volume) { |
198 NOTIMPLEMENTED(); | 284 NOTIMPLEMENTED(); |
199 } | 285 } |
200 | 286 |
201 void AudioDevice::Send(IPC::Message* message) { | 287 void AudioDevice::Send(IPC::Message* message) { |
202 filter_->Send(message); | 288 filter_->Send(message); |
203 } | 289 } |
204 | 290 |
205 // Our audio thread runs here. | 291 // Our audio thread runs here. |
206 void AudioDevice::Run() { | 292 void AudioDevice::Run() { |
207 audio_thread_->SetThreadPriority(base::kThreadPriority_RealtimeAudio); | 293 audio_thread_->SetThreadPriority(base::kThreadPriority_RealtimeAudio); |
208 | 294 |
209 int pending_data; | 295 int pending_data; |
210 const int samples_per_ms = static_cast<int>(sample_rate_) / 1000; | 296 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; | 297 const int bytes_per_ms = channels_ * (bits_per_sample_ / 8) * samples_per_ms; |
212 | 298 |
213 while ((sizeof(pending_data) == socket_->Receive(&pending_data, | 299 while (sizeof(pending_data) == |
214 sizeof(pending_data))) && | 300 socket_->Receive(&pending_data, sizeof(pending_data))) { |
215 (pending_data >= 0)) { | 301 if (pending_data == media::AudioOutputController::kPauseMark) { |
| 302 memset(shared_memory_data(), 0, shared_memory_size_); |
| 303 continue; |
| 304 } else if (pending_data < 0) { |
| 305 break; |
| 306 } |
216 // Convert the number of pending bytes in the render buffer | 307 // Convert the number of pending bytes in the render buffer |
217 // into milliseconds. | 308 // into milliseconds. |
218 audio_delay_milliseconds_ = pending_data / bytes_per_ms; | 309 audio_delay_milliseconds_ = pending_data / bytes_per_ms; |
219 FireRenderCallback(); | 310 FireRenderCallback(); |
220 } | 311 } |
221 } | 312 } |
222 | 313 |
223 void AudioDevice::FireRenderCallback() { | 314 void AudioDevice::FireRenderCallback() { |
224 TRACE_EVENT0("audio", "AudioDevice::FireRenderCallback"); | 315 TRACE_EVENT0("audio", "AudioDevice::FireRenderCallback"); |
225 | 316 |
226 if (callback_) { | 317 if (callback_) { |
227 // Update the audio-delay measurement then ask client to render audio. | 318 // Update the audio-delay measurement then ask client to render audio. |
228 callback_->Render(audio_data_, buffer_size_, audio_delay_milliseconds_); | 319 callback_->Render(audio_data_, buffer_size_, audio_delay_milliseconds_); |
229 | 320 |
230 // Interleave, scale, and clip to int16. | 321 // Interleave, scale, and clip to int16. |
| 322 // TODO(crogers): avoid converting to integer here, and pass the data |
| 323 // to the browser process as float, so we don't lose precision for |
| 324 // audio hardware which has better than 16bit precision. |
231 media::InterleaveFloatToInt16(audio_data_, | 325 media::InterleaveFloatToInt16(audio_data_, |
232 static_cast<int16*>(shared_memory_data()), | 326 static_cast<int16*>(shared_memory_data()), |
233 buffer_size_); | 327 buffer_size_); |
234 } | 328 } |
235 } | 329 } |
| 330 |
| 331 void AudioDevice::ShutDownAudioThread() { |
| 332 // Synchronize with OnLowLatencyCreated(). |
| 333 base::AutoLock auto_lock(lock_); |
| 334 |
| 335 if (socket_.get()) { |
| 336 socket_->Close(); |
| 337 } |
| 338 if (audio_thread_.get()) { |
| 339 audio_thread_->Join(); |
| 340 audio_thread_.reset(NULL); |
| 341 } |
| 342 // Note that the socket can't be reset until *after* joining the thread. |
| 343 socket_.reset(NULL); |
| 344 } |
OLD | NEW |