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

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: Removed Stop call in destructor. Added DCHECK instead. Created 9 years, 4 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 kMaxTimeOutMs =
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 // Make sure we have been shut down.
36 DCHECK_EQ(0, stream_id_); 43 DCHECK_EQ(0, stream_id_);
wjia(left Chromium) 2011/08/19 18:19:10 Does this mean client has to call Stop() before de
henrika (OOO until Aug 14) 2011/08/19 22:07:05 Yes, that is the current design criteria. If we a
37 Stop();
38 for (int i = 0; i < channels_; ++i) 44 for (int i = 0; i < channels_; ++i)
39 delete [] audio_data_[i]; 45 delete [] audio_data_[i];
40 } 46 }
41 47
42 bool AudioInputDevice::Start() { 48 void AudioInputDevice::Start() {
43 // Make sure we don't call Start() more than once.
44 DCHECK_EQ(0, stream_id_);
45 if (stream_id_)
46 return false;
47
48 AudioParameters params; 49 AudioParameters params;
49 // TODO(henrika): add support for low-latency mode? 50 // TODO(henrika): add support for low-latency mode?
50 params.format = AudioParameters::AUDIO_PCM_LINEAR; 51 params.format = AudioParameters::AUDIO_PCM_LINEAR;
51 params.channels = channels_; 52 params.channels = channels_;
52 params.sample_rate = static_cast<int>(sample_rate_); 53 params.sample_rate = static_cast<int>(sample_rate_);
53 params.bits_per_sample = bits_per_sample_; 54 params.bits_per_sample = bits_per_sample_;
54 params.samples_per_packet = buffer_size_; 55 params.samples_per_packet = buffer_size_;
55 56
56 ChildProcess::current()->io_message_loop()->PostTask( 57 ChildProcess::current()->io_message_loop()->PostTask(
57 FROM_HERE, 58 FROM_HERE,
58 NewRunnableMethod(this, &AudioInputDevice::InitializeOnIOThread, params)); 59 NewRunnableMethod(this, &AudioInputDevice::InitializeOnIOThread, params));
59
60 return true;
61 } 60 }
62 61
63 bool AudioInputDevice::Stop() { 62 bool AudioInputDevice::Stop() {
64 if (!stream_id_) 63 base::WaitableEvent completion(false, false);
65 return false;
66 64
67 ChildProcess::current()->io_message_loop()->PostTask( 65 ChildProcess::current()->io_message_loop()->PostTask(
68 FROM_HERE, 66 FROM_HERE,
69 NewRunnableMethod(this, &AudioInputDevice::ShutDownOnIOThread)); 67 NewRunnableMethod(this, &AudioInputDevice::ShutDownOnIOThread,
68 &completion));
70 69
71 if (audio_thread_.get()) { 70 // We wait here for the IO task to be completed to remove race conflicts
72 socket_->Close(); 71 // with OnLowLatencyCreated and to ensure that Stop can be called from
73 audio_thread_->Join(); 72 // the destructor.
73 if (completion.TimedWait(kMaxTimeOutMs)) {
74 if (audio_thread_.get()) {
75 // Terminate the main thread function in the audio thread.
76 socket_->Close();
77 // Wait for the audio thread to exit.
78 audio_thread_->Join();
79 // Ensures that we can call Stop multiple times.
80 audio_thread_.reset(NULL);
81 }
82 } else {
83 DLOG(ERROR) << "Failed to shut down audio input on IO thread";
84 return false;
74 } 85 }
75 86
76 return true; 87 return true;
77 } 88 }
78 89
79 bool AudioInputDevice::SetVolume(double volume) { 90 bool AudioInputDevice::SetVolume(double volume) {
80 NOTIMPLEMENTED(); 91 NOTIMPLEMENTED();
81 return false; 92 return false;
82 } 93 }
83 94
84 bool AudioInputDevice::GetVolume(double* volume) { 95 bool AudioInputDevice::GetVolume(double* volume) {
85 NOTIMPLEMENTED(); 96 NOTIMPLEMENTED();
86 return false; 97 return false;
87 } 98 }
88 99
89 void AudioInputDevice::InitializeOnIOThread(const AudioParameters& params) { 100 void AudioInputDevice::InitializeOnIOThread(const AudioParameters& params) {
101 DCHECK(MessageLoop::current() == ChildProcess::current()->io_message_loop());
102 // Make sure we don't call Start() more than once.
103 DCHECK_EQ(0, stream_id_);
104 if (stream_id_)
105 return;
106
90 stream_id_ = filter_->AddDelegate(this); 107 stream_id_ = filter_->AddDelegate(this);
91 Send(new AudioInputHostMsg_CreateStream(stream_id_, params, true)); 108 Send(new AudioInputHostMsg_CreateStream(stream_id_, params, true));
92 } 109 }
93 110
94 void AudioInputDevice::StartOnIOThread() { 111 void AudioInputDevice::StartOnIOThread() {
112 DCHECK(MessageLoop::current() == ChildProcess::current()->io_message_loop());
95 if (stream_id_) 113 if (stream_id_)
96 Send(new AudioInputHostMsg_RecordStream(stream_id_)); 114 Send(new AudioInputHostMsg_RecordStream(stream_id_));
97 } 115 }
98 116
99 void AudioInputDevice::ShutDownOnIOThread() { 117 void AudioInputDevice::ShutDownOnIOThread(base::WaitableEvent* completion) {
118 DCHECK(MessageLoop::current() == ChildProcess::current()->io_message_loop());
100 // Make sure we don't call shutdown more than once. 119 // Make sure we don't call shutdown more than once.
101 if (!stream_id_) 120 if (!stream_id_) {
121 completion->Signal();
102 return; 122 return;
123 }
103 124
104 filter_->RemoveDelegate(stream_id_); 125 filter_->RemoveDelegate(stream_id_);
105 Send(new AudioInputHostMsg_CloseStream(stream_id_)); 126 Send(new AudioInputHostMsg_CloseStream(stream_id_));
106 stream_id_ = 0; 127 stream_id_ = 0;
128
129 completion->Signal();
107 } 130 }
108 131
109 void AudioInputDevice::SetVolumeOnIOThread(double volume) { 132 void AudioInputDevice::SetVolumeOnIOThread(double volume) {
133 DCHECK(MessageLoop::current() == ChildProcess::current()->io_message_loop());
110 if (stream_id_) 134 if (stream_id_)
111 Send(new AudioInputHostMsg_SetVolume(stream_id_, volume)); 135 Send(new AudioInputHostMsg_SetVolume(stream_id_, volume));
112 } 136 }
113 137
114 void AudioInputDevice::OnLowLatencyCreated( 138 void AudioInputDevice::OnLowLatencyCreated(
115 base::SharedMemoryHandle handle, 139 base::SharedMemoryHandle handle,
116 base::SyncSocket::Handle socket_handle, 140 base::SyncSocket::Handle socket_handle,
117 uint32 length) { 141 uint32 length) {
118 DCHECK(MessageLoop::current() == ChildProcess::current()->io_message_loop()); 142 DCHECK(MessageLoop::current() == ChildProcess::current()->io_message_loop());
119 #if defined(OS_WIN) 143 #if defined(OS_WIN)
120 DCHECK(handle); 144 DCHECK(handle);
121 DCHECK(socket_handle); 145 DCHECK(socket_handle);
122 #else 146 #else
123 DCHECK_GE(handle.fd, 0); 147 DCHECK_GE(handle.fd, 0);
124 DCHECK_GE(socket_handle, 0); 148 DCHECK_GE(socket_handle, 0);
125 #endif 149 #endif
126 DCHECK(length); 150 DCHECK(length);
127 151
152 // Takes care of the case when Stop is called before OnLowLatencyCreated.
153 if (!stream_id_) {
154 base::SharedMemory::CloseHandle(handle);
155 base::SyncSocket socket(socket_handle);
156 return;
157 }
158
128 shared_memory_.reset(new base::SharedMemory(handle, false)); 159 shared_memory_.reset(new base::SharedMemory(handle, false));
129 shared_memory_->Map(length); 160 shared_memory_->Map(length);
130 161
131 socket_.reset(new base::SyncSocket(socket_handle)); 162 socket_.reset(new base::SyncSocket(socket_handle));
132 163
133 audio_thread_.reset( 164 audio_thread_.reset(
134 new base::DelegateSimpleThread(this, "renderer_audio_input_thread")); 165 new base::DelegateSimpleThread(this, "RendererAudioInputThread"));
135 audio_thread_->Start(); 166 audio_thread_->Start();
136 167
137 MessageLoop::current()->PostTask( 168 MessageLoop::current()->PostTask(
138 FROM_HERE, 169 FROM_HERE,
139 NewRunnableMethod(this, &AudioInputDevice::StartOnIOThread)); 170 NewRunnableMethod(this, &AudioInputDevice::StartOnIOThread));
140 } 171 }
141 172
142 void AudioInputDevice::OnVolume(double volume) { 173 void AudioInputDevice::OnVolume(double volume) {
143 NOTIMPLEMENTED(); 174 NOTIMPLEMENTED();
144 } 175 }
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
190 bytes_per_sample, 221 bytes_per_sample,
191 number_of_frames); 222 number_of_frames);
192 } 223 }
193 224
194 // Deliver captured data to the client in floating point format 225 // Deliver captured data to the client in floating point format
195 // and update the audio-delay measurement. 226 // and update the audio-delay measurement.
196 callback_->Capture(audio_data_, 227 callback_->Capture(audio_data_,
197 number_of_frames, 228 number_of_frames,
198 audio_delay_milliseconds_); 229 audio_delay_milliseconds_);
199 } 230 }
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