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

Side by Side Diff: content/renderer/media/audio_device.cc

Issue 8659040: There is a racing between SyncSocket::Receive in audio_thread_ and SyncSocket::Close in renderer ... (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: Created 9 years 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) 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 audio_event_(true, false) {
enal1 2011/11/29 16:14:48 Can you please explain what exactly arguments to e
tommi (sloooow) - chröme 2011/11/29 16:41:53 I think it's ok to set |manual_reset| to false in
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 26 matching lines...) Expand all
65 66
66 ChildProcess::current()->io_message_loop()->PostTask( 67 ChildProcess::current()->io_message_loop()->PostTask(
67 FROM_HERE, 68 FROM_HERE,
68 base::Bind(&AudioDevice::ShutDownOnIOThread, this, &completion)); 69 base::Bind(&AudioDevice::ShutDownOnIOThread, this, &completion));
69 70
70 // We wait here for the IO task to be completed to remove race conflicts 71 // 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 72 // with OnLowLatencyCreated() and to ensure that Stop() acts as a synchronous
72 // function call. 73 // function call.
73 if (completion.TimedWait(kMaxTimeOut)) { 74 if (completion.TimedWait(kMaxTimeOut)) {
74 if (audio_thread_.get()) { 75 if (audio_thread_.get()) {
75 socket_->Close(); 76 audio_event_.Signal();
76 audio_thread_->Join(); 77 audio_thread_->Join();
77 audio_thread_.reset(NULL); 78 audio_thread_.reset(NULL);
78 } 79 }
79 } else { 80 } else {
80 LOG(ERROR) << "Failed to shut down audio output on IO thread"; 81 LOG(ERROR) << "Failed to shut down audio output on IO thread";
81 return false; 82 return false;
82 } 83 }
83 84
84 return true; 85 return true;
85 } 86 }
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
178 179
179 shared_memory_.reset(new base::SharedMemory(handle, false)); 180 shared_memory_.reset(new base::SharedMemory(handle, false));
180 shared_memory_->Map(length); 181 shared_memory_->Map(length);
181 182
182 DCHECK_GE(length, buffer_size_ * sizeof(int16) * channels_); 183 DCHECK_GE(length, buffer_size_ * sizeof(int16) * channels_);
183 184
184 socket_.reset(new base::SyncSocket(socket_handle)); 185 socket_.reset(new base::SyncSocket(socket_handle));
185 // Allow the client to pre-populate the buffer. 186 // Allow the client to pre-populate the buffer.
186 FireRenderCallback(); 187 FireRenderCallback();
187 188
189 audio_event_.Reset();
tommi (sloooow) - chröme 2011/11/29 16:41:53 if you set manual_reset to false, you don't have t
188 audio_thread_.reset( 190 audio_thread_.reset(
189 new base::DelegateSimpleThread(this, "renderer_audio_thread")); 191 new base::DelegateSimpleThread(this, "renderer_audio_thread"));
190 audio_thread_->Start(); 192 audio_thread_->Start();
191 193
192 MessageLoop::current()->PostTask( 194 MessageLoop::current()->PostTask(
193 FROM_HERE, 195 FROM_HERE,
194 base::Bind(&AudioDevice::StartOnIOThread, this)); 196 base::Bind(&AudioDevice::StartOnIOThread, this));
195 } 197 }
196 198
197 void AudioDevice::OnVolume(double volume) { 199 void AudioDevice::OnVolume(double volume) {
198 NOTIMPLEMENTED(); 200 NOTIMPLEMENTED();
199 } 201 }
200 202
201 void AudioDevice::Send(IPC::Message* message) { 203 void AudioDevice::Send(IPC::Message* message) {
202 filter_->Send(message); 204 filter_->Send(message);
203 } 205 }
204 206
205 // Our audio thread runs here. 207 // Our audio thread runs here.
206 void AudioDevice::Run() { 208 void AudioDevice::Run() {
207 audio_thread_->SetThreadPriority(base::kThreadPriority_RealtimeAudio); 209 audio_thread_->SetThreadPriority(base::kThreadPriority_RealtimeAudio);
208 210
209 int pending_data; 211 int pending_data;
210 const int samples_per_ms = static_cast<int>(sample_rate_) / 1000; 212 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; 213 const int bytes_per_ms = channels_ * (bits_per_sample_ / 8) * samples_per_ms;
212 214
213 while ((sizeof(pending_data) == socket_->Receive(&pending_data, 215 while (!audio_event_.IsSignaled() &&
enal1 2011/11/29 16:14:48 I don't fully understand how event helps in a case
tommi (sloooow) - chröme 2011/11/29 16:41:53 good point! Is there a way to read from the socke
no longer working on chromium 2011/11/30 17:02:55 Really good question. No, event does not help at a
216 (sizeof(pending_data) == socket_->Receive(&pending_data,
214 sizeof(pending_data))) && 217 sizeof(pending_data))) &&
215 (pending_data >= 0)) { 218 (pending_data >= 0)) {
216 // Convert the number of pending bytes in the render buffer 219 // Convert the number of pending bytes in the render buffer
217 // into milliseconds. 220 // into milliseconds.
218 audio_delay_milliseconds_ = pending_data / bytes_per_ms; 221 audio_delay_milliseconds_ = pending_data / bytes_per_ms;
219 FireRenderCallback(); 222 FireRenderCallback();
220 } 223 }
224
225 socket_->Close();
221 } 226 }
222 227
223 void AudioDevice::FireRenderCallback() { 228 void AudioDevice::FireRenderCallback() {
224 TRACE_EVENT0("audio", "AudioDevice::FireRenderCallback"); 229 TRACE_EVENT0("audio", "AudioDevice::FireRenderCallback");
225 230
226 if (callback_) { 231 if (callback_) {
227 // Update the audio-delay measurement then ask client to render audio. 232 // Update the audio-delay measurement then ask client to render audio.
228 callback_->Render(audio_data_, buffer_size_, audio_delay_milliseconds_); 233 callback_->Render(audio_data_, buffer_size_, audio_delay_milliseconds_);
229 234
230 // Interleave, scale, and clip to int16. 235 // Interleave, scale, and clip to int16.
231 media::InterleaveFloatToInt16(audio_data_, 236 media::InterleaveFloatToInt16(audio_data_,
232 static_cast<int16*>(shared_memory_data()), 237 static_cast<int16*>(shared_memory_data()),
233 buffer_size_); 238 buffer_size_);
234 } 239 }
235 } 240 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698