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

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

Issue 7661017: Refactor AudioInputDevice to remove race conditions and allow more flexible calling sequences. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Minor changes based on review by Andrew Created 9 years, 3 months 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_input_device.h" 5 #include "content/renderer/media/audio_input_device.h"
6 6
7 #include "base/message_loop.h" 7 #include "base/message_loop.h"
8 #include "base/time.h"
8 #include "content/common/child_process.h" 9 #include "content/common/child_process.h"
9 #include "content/common/media/audio_messages.h" 10 #include "content/common/media/audio_messages.h"
10 #include "content/common/view_messages.h" 11 #include "content/common/view_messages.h"
11 #include "content/renderer/render_thread.h" 12 #include "content/renderer/render_thread.h"
12 #include "media/audio/audio_util.h" 13 #include "media/audio/audio_util.h"
13 14
15 // Max waiting time for Stop() to complete. If this time limit is passed,
16 // we will stop waiting and return false. It ensures that Stop() can't block
17 // the calling thread forever.
18 static const base::TimeDelta kMaxTimeOut =
19 base::TimeDelta::FromMilliseconds(1000);
20
14 AudioInputDevice::AudioInputDevice(size_t buffer_size, 21 AudioInputDevice::AudioInputDevice(size_t buffer_size,
15 int channels, 22 int channels,
16 double sample_rate, 23 double sample_rate,
17 CaptureCallback* callback) 24 CaptureCallback* callback)
18 : buffer_size_(buffer_size), 25 : buffer_size_(buffer_size),
19 channels_(channels), 26 channels_(channels),
20 bits_per_sample_(16), 27 bits_per_sample_(16),
21 sample_rate_(sample_rate), 28 sample_rate_(sample_rate),
22 callback_(callback), 29 callback_(callback),
23 audio_delay_milliseconds_(0), 30 audio_delay_milliseconds_(0),
24 volume_(1.0), 31 volume_(1.0),
25 stream_id_(0) { 32 stream_id_(0) {
26 filter_ = RenderThread::current()->audio_input_message_filter(); 33 filter_ = RenderThread::current()->audio_input_message_filter();
27 audio_data_.reserve(channels); 34 audio_data_.reserve(channels);
28 for (int i = 0; i < channels; ++i) { 35 for (int i = 0; i < channels; ++i) {
29 float* channel_data = new float[buffer_size]; 36 float* channel_data = new float[buffer_size];
30 audio_data_.push_back(channel_data); 37 audio_data_.push_back(channel_data);
31 } 38 }
32 } 39 }
33 40
34 AudioInputDevice::~AudioInputDevice() { 41 AudioInputDevice::~AudioInputDevice() {
35 // Make sure we have been shut down. 42 // TODO(henrika): The current design requires that the user calls
36 DCHECK_EQ(0, stream_id_); 43 // Stop before deleting this class.
37 Stop(); 44 CHECK_EQ(0, stream_id_);
38 for (int i = 0; i < channels_; ++i) 45 for (int i = 0; i < channels_; ++i)
39 delete [] audio_data_[i]; 46 delete [] audio_data_[i];
40 } 47 }
41 48
42 bool AudioInputDevice::Start() { 49 void AudioInputDevice::Start() {
43 // Make sure we don't call Start() more than once. 50 VLOG(1) << "Start()";
44 DCHECK_EQ(0, stream_id_);
45 if (stream_id_)
46 return false;
47
48 AudioParameters params; 51 AudioParameters params;
49 // TODO(henrika): add support for low-latency mode? 52 // TODO(henrika): add support for low-latency mode?
50 params.format = AudioParameters::AUDIO_PCM_LINEAR; 53 params.format = AudioParameters::AUDIO_PCM_LINEAR;
51 params.channels = channels_; 54 params.channels = channels_;
52 params.sample_rate = static_cast<int>(sample_rate_); 55 params.sample_rate = static_cast<int>(sample_rate_);
53 params.bits_per_sample = bits_per_sample_; 56 params.bits_per_sample = bits_per_sample_;
54 params.samples_per_packet = buffer_size_; 57 params.samples_per_packet = buffer_size_;
55 58
56 ChildProcess::current()->io_message_loop()->PostTask( 59 ChildProcess::current()->io_message_loop()->PostTask(
57 FROM_HERE, 60 FROM_HERE,
58 NewRunnableMethod(this, &AudioInputDevice::InitializeOnIOThread, params)); 61 NewRunnableMethod(this, &AudioInputDevice::InitializeOnIOThread, params));
59
60 return true;
61 } 62 }
62 63
63 bool AudioInputDevice::Stop() { 64 bool AudioInputDevice::Stop() {
64 if (!stream_id_) 65 VLOG(1) << "Stop()";
65 return false; 66 base::WaitableEvent completion(false, false);
66 67
67 ChildProcess::current()->io_message_loop()->PostTask( 68 ChildProcess::current()->io_message_loop()->PostTask(
68 FROM_HERE, 69 FROM_HERE,
69 NewRunnableMethod(this, &AudioInputDevice::ShutDownOnIOThread)); 70 NewRunnableMethod(this, &AudioInputDevice::ShutDownOnIOThread,
71 &completion));
70 72
71 if (audio_thread_.get()) { 73 // We wait here for the IO task to be completed to remove race conflicts
72 socket_->Close(); 74 // with OnLowLatencyCreated() and to ensure that Stop() acts as a synchronous
73 audio_thread_->Join(); 75 // function call.
76 if (completion.TimedWait(kMaxTimeOut)) {
77 if (audio_thread_.get()) {
78 // Terminate the main thread function in the audio thread.
79 socket_->Close();
80 // Wait for the audio thread to exit.
81 audio_thread_->Join();
82 // Ensures that we can call Stop() multiple times.
83 audio_thread_.reset(NULL);
84 }
85 } else {
86 LOG(ERROR) << "Failed to shut down audio input on IO thread";
87 return false;
74 } 88 }
75 89
76 return true; 90 return true;
77 } 91 }
78 92
79 bool AudioInputDevice::SetVolume(double volume) { 93 bool AudioInputDevice::SetVolume(double volume) {
80 NOTIMPLEMENTED(); 94 NOTIMPLEMENTED();
81 return false; 95 return false;
82 } 96 }
83 97
84 bool AudioInputDevice::GetVolume(double* volume) { 98 bool AudioInputDevice::GetVolume(double* volume) {
85 NOTIMPLEMENTED(); 99 NOTIMPLEMENTED();
86 return false; 100 return false;
87 } 101 }
88 102
89 void AudioInputDevice::InitializeOnIOThread(const AudioParameters& params) { 103 void AudioInputDevice::InitializeOnIOThread(const AudioParameters& params) {
104 DCHECK(MessageLoop::current() == ChildProcess::current()->io_message_loop());
105 // Make sure we don't call Start() more than once.
106 DCHECK_EQ(0, stream_id_);
107 if (stream_id_)
108 return;
109
90 stream_id_ = filter_->AddDelegate(this); 110 stream_id_ = filter_->AddDelegate(this);
91 Send(new AudioInputHostMsg_CreateStream(stream_id_, params, true)); 111 Send(new AudioInputHostMsg_CreateStream(stream_id_, params, true));
92 } 112 }
93 113
94 void AudioInputDevice::StartOnIOThread() { 114 void AudioInputDevice::StartOnIOThread() {
115 DCHECK(MessageLoop::current() == ChildProcess::current()->io_message_loop());
95 if (stream_id_) 116 if (stream_id_)
96 Send(new AudioInputHostMsg_RecordStream(stream_id_)); 117 Send(new AudioInputHostMsg_RecordStream(stream_id_));
97 } 118 }
98 119
99 void AudioInputDevice::ShutDownOnIOThread() { 120 void AudioInputDevice::ShutDownOnIOThread(base::WaitableEvent* completion) {
121 DCHECK(MessageLoop::current() == ChildProcess::current()->io_message_loop());
100 // Make sure we don't call shutdown more than once. 122 // Make sure we don't call shutdown more than once.
101 if (!stream_id_) 123 if (!stream_id_) {
124 completion->Signal();
102 return; 125 return;
126 }
103 127
104 filter_->RemoveDelegate(stream_id_); 128 filter_->RemoveDelegate(stream_id_);
105 Send(new AudioInputHostMsg_CloseStream(stream_id_)); 129 Send(new AudioInputHostMsg_CloseStream(stream_id_));
106 stream_id_ = 0; 130 stream_id_ = 0;
131
132 completion->Signal();
107 } 133 }
108 134
109 void AudioInputDevice::SetVolumeOnIOThread(double volume) { 135 void AudioInputDevice::SetVolumeOnIOThread(double volume) {
136 DCHECK(MessageLoop::current() == ChildProcess::current()->io_message_loop());
110 if (stream_id_) 137 if (stream_id_)
111 Send(new AudioInputHostMsg_SetVolume(stream_id_, volume)); 138 Send(new AudioInputHostMsg_SetVolume(stream_id_, volume));
112 } 139 }
113 140
114 void AudioInputDevice::OnLowLatencyCreated( 141 void AudioInputDevice::OnLowLatencyCreated(
115 base::SharedMemoryHandle handle, 142 base::SharedMemoryHandle handle,
116 base::SyncSocket::Handle socket_handle, 143 base::SyncSocket::Handle socket_handle,
117 uint32 length) { 144 uint32 length) {
118 DCHECK(MessageLoop::current() == ChildProcess::current()->io_message_loop()); 145 DCHECK(MessageLoop::current() == ChildProcess::current()->io_message_loop());
119 #if defined(OS_WIN) 146 #if defined(OS_WIN)
120 DCHECK(handle); 147 DCHECK(handle);
121 DCHECK(socket_handle); 148 DCHECK(socket_handle);
122 #else 149 #else
123 DCHECK_GE(handle.fd, 0); 150 DCHECK_GE(handle.fd, 0);
124 DCHECK_GE(socket_handle, 0); 151 DCHECK_GE(socket_handle, 0);
125 #endif 152 #endif
126 DCHECK(length); 153 DCHECK(length);
127 154
155 VLOG(1) << "OnLowLatencyCreated (stream_id=" << stream_id_ << ")";
156 // Takes care of the case when Stop() is called before OnLowLatencyCreated().
157 if (!stream_id_) {
158 base::SharedMemory::CloseHandle(handle);
159 base::SyncSocket socket(socket_handle);
160 return;
161 }
162
128 shared_memory_.reset(new base::SharedMemory(handle, false)); 163 shared_memory_.reset(new base::SharedMemory(handle, false));
129 shared_memory_->Map(length); 164 shared_memory_->Map(length);
130 165
131 socket_.reset(new base::SyncSocket(socket_handle)); 166 socket_.reset(new base::SyncSocket(socket_handle));
132 167
133 audio_thread_.reset( 168 audio_thread_.reset(
134 new base::DelegateSimpleThread(this, "renderer_audio_input_thread")); 169 new base::DelegateSimpleThread(this, "RendererAudioInputThread"));
135 audio_thread_->Start(); 170 audio_thread_->Start();
136 171
137 MessageLoop::current()->PostTask( 172 MessageLoop::current()->PostTask(
138 FROM_HERE, 173 FROM_HERE,
139 NewRunnableMethod(this, &AudioInputDevice::StartOnIOThread)); 174 NewRunnableMethod(this, &AudioInputDevice::StartOnIOThread));
140 } 175 }
141 176
142 void AudioInputDevice::OnVolume(double volume) { 177 void AudioInputDevice::OnVolume(double volume) {
143 NOTIMPLEMENTED(); 178 NOTIMPLEMENTED();
144 } 179 }
145 180
146 void AudioInputDevice::Send(IPC::Message* message) { 181 void AudioInputDevice::Send(IPC::Message* message) {
147 filter_->Send(message); 182 filter_->Send(message);
148 } 183 }
149 184
150 // Our audio thread runs here. We receive captured audio samples on 185 // Our audio thread runs here. We receive captured audio samples on
151 // this thread. 186 // this thread.
152 void AudioInputDevice::Run() { 187 void AudioInputDevice::Run() {
153 audio_thread_->SetThreadPriority(base::kThreadPriority_RealtimeAudio); 188 audio_thread_->SetThreadPriority(base::kThreadPriority_RealtimeAudio);
154 189
155 int pending_data; 190 int pending_data;
156 const int samples_per_ms = static_cast<int>(sample_rate_) / 1000; 191 const int samples_per_ms = static_cast<int>(sample_rate_) / 1000;
157 const int bytes_per_ms = channels_ * (bits_per_sample_ / 8) * samples_per_ms; 192 const int bytes_per_ms = channels_ * (bits_per_sample_ / 8) * samples_per_ms;
158 193
159 while (sizeof(pending_data) == socket_->Receive(&pending_data, 194 while (sizeof(pending_data) == socket_->Receive(&pending_data,
160 sizeof(pending_data)) && 195 sizeof(pending_data)) &&
161 pending_data >= 0) { 196 pending_data >= 0) {
162 // TODO(henrika): investigate the provided |pending_data| value 197 // TODO(henrika): investigate the provided |pending_data| value
163 // and ensure that it is actually an accurate delay estimation. 198 // and ensure that it is actually an accurate delay estimation.
164 199
165 // Convert the number of pending bytes in the capture buffer 200 // Convert the number of pending bytes in the capture buffer
166 // into milliseconds. 201 // into milliseconds.
167 audio_delay_milliseconds_ = pending_data / bytes_per_ms; 202 audio_delay_milliseconds_ = pending_data / bytes_per_ms;
168 203
169 FireCaptureCallback(); 204 FireCaptureCallback();
170 } 205 }
171 } 206 }
(...skipping 18 matching lines...) Expand all
190 bytes_per_sample, 225 bytes_per_sample,
191 number_of_frames); 226 number_of_frames);
192 } 227 }
193 228
194 // Deliver captured data to the client in floating point format 229 // Deliver captured data to the client in floating point format
195 // and update the audio-delay measurement. 230 // and update the audio-delay measurement.
196 callback_->Capture(audio_data_, 231 callback_->Capture(audio_data_,
197 number_of_frames, 232 number_of_frames,
198 audio_delay_milliseconds_); 233 audio_delay_milliseconds_);
199 } 234 }
OLDNEW
« no previous file with comments | « content/renderer/media/audio_input_device.h ('k') | content/renderer/media/audio_input_message_filter.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698