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_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 "content/common/child_process.h" | 8 #include "content/common/child_process.h" |
9 #include "content/common/media/audio_messages.h" | 9 #include "content/common/media/audio_messages.h" |
10 #include "content/common/view_messages.h" | 10 #include "content/common/view_messages.h" |
11 #include "content/renderer/render_thread.h" | 11 #include "content/renderer/render_thread.h" |
12 #include "media/audio/audio_util.h" | 12 #include "media/audio/audio_util.h" |
13 | 13 |
14 AudioInputDevice::AudioInputDevice(size_t buffer_size, | 14 AudioInputDevice::AudioInputDevice(size_t buffer_size, |
15 int channels, | 15 int channels, |
16 double sample_rate, | 16 double sample_rate, |
17 CaptureCallback* callback) | 17 CaptureCallback* callback) |
18 : buffer_size_(buffer_size), | 18 : buffer_size_(buffer_size), |
19 channels_(channels), | 19 channels_(channels), |
20 bits_per_sample_(16), | 20 bits_per_sample_(16), |
21 sample_rate_(sample_rate), | 21 sample_rate_(sample_rate), |
22 callback_(callback), | 22 callback_(callback), |
23 audio_delay_milliseconds_(0), | 23 audio_delay_milliseconds_(0), |
24 volume_(1.0), | 24 volume_(1.0), |
25 capture_thread_("AudioInputDevice"), | |
henrika_dont_use
2011/07/30 15:50:59
Do we use camel-back style for thread names?
wjia(left Chromium)
2011/08/01 22:40:13
Yes.
| |
26 delegate_event_(false, false), | |
27 stop_event_(false, false), | |
25 stream_id_(0) { | 28 stream_id_(0) { |
26 filter_ = RenderThread::current()->audio_input_message_filter(); | 29 filter_ = RenderThread::current()->audio_input_message_filter(); |
30 capture_thread_.Start(); | |
31 message_loop_proxy_ = capture_thread_.message_loop_proxy(); | |
henrika_dont_use
2011/07/30 15:50:59
Sorry, again, why not message_loop()?
Also, why is
wjia(left Chromium)
2011/08/01 22:40:13
In audio_input_device.h, the difference between Me
| |
27 audio_data_.reserve(channels); | 32 audio_data_.reserve(channels); |
28 for (int i = 0; i < channels; ++i) { | 33 for (int i = 0; i < channels; ++i) { |
29 float* channel_data = new float[buffer_size]; | 34 float* channel_data = new float[buffer_size]; |
30 audio_data_.push_back(channel_data); | 35 audio_data_.push_back(channel_data); |
31 } | 36 } |
32 } | 37 } |
33 | 38 |
34 AudioInputDevice::~AudioInputDevice() { | 39 AudioInputDevice::~AudioInputDevice() { |
35 // Make sure we have been shut down. | |
36 DCHECK_EQ(0, stream_id_); | |
37 Stop(); | 40 Stop(); |
41 capture_thread_.Stop(); | |
henrika_dont_use
2011/07/30 15:50:59
You did not like DCHECK_EQ(0, stream_id_)?
xians
2011/08/01 11:41:23
It is for thread safety, stream_id_ is only access
wjia(left Chromium)
2011/08/01 22:40:13
Shijing has the answer.
| |
38 for (int i = 0; i < channels_; ++i) | 42 for (int i = 0; i < channels_; ++i) |
39 delete [] audio_data_[i]; | 43 delete [] audio_data_[i]; |
40 } | 44 } |
41 | 45 |
42 bool AudioInputDevice::Start() { | 46 bool AudioInputDevice::Start() { |
43 // Make sure we don't call Start() more than once. | 47 message_loop_proxy_->PostTask(FROM_HERE, |
henrika_dont_use
2011/07/30 15:50:59
Why bool if false is not an option?
wjia(left Chromium)
2011/08/01 22:40:13
I didn't change ADM. ADM should be refactored as w
| |
44 DCHECK_EQ(0, stream_id_); | 48 NewRunnableMethod(this, &AudioInputDevice::StartOnCaptureThread)); |
45 if (stream_id_) | |
46 return false; | |
47 | |
48 AudioParameters params; | |
49 // TODO(henrika): add support for low-latency mode? | |
50 params.format = AudioParameters::AUDIO_PCM_LINEAR; | |
51 params.channels = channels_; | |
52 params.sample_rate = static_cast<int>(sample_rate_); | |
53 params.bits_per_sample = bits_per_sample_; | |
54 params.samples_per_packet = buffer_size_; | |
55 | |
56 ChildProcess::current()->io_message_loop()->PostTask( | |
57 FROM_HERE, | |
58 NewRunnableMethod(this, &AudioInputDevice::InitializeOnIOThread, params)); | |
59 | |
60 return true; | 49 return true; |
61 } | 50 } |
62 | 51 |
63 bool AudioInputDevice::Stop() { | 52 bool AudioInputDevice::Stop() { |
64 if (!stream_id_) | 53 message_loop_proxy_->PostTask(FROM_HERE, |
henrika_dont_use
2011/07/30 15:50:59
Why bool?
wjia(left Chromium)
2011/08/01 22:40:13
Same reason for Start().
| |
65 return false; | 54 NewRunnableMethod(this, &AudioInputDevice::ShutDownOnCaptureThread)); |
66 | 55 stop_event_.Wait(); |
67 ChildProcess::current()->io_message_loop()->PostTask( | |
68 FROM_HERE, | |
69 NewRunnableMethod(this, &AudioInputDevice::ShutDownOnIOThread)); | |
70 | |
71 if (audio_thread_.get()) { | |
72 socket_->Close(); | |
73 audio_thread_->Join(); | |
74 } | |
75 | |
76 return true; | 56 return true; |
77 } | 57 } |
78 | 58 |
79 bool AudioInputDevice::SetVolume(double volume) { | 59 bool AudioInputDevice::SetVolume(double volume) { |
80 NOTIMPLEMENTED(); | 60 NOTIMPLEMENTED(); |
81 return false; | 61 return false; |
82 } | 62 } |
83 | 63 |
84 bool AudioInputDevice::GetVolume(double* volume) { | 64 bool AudioInputDevice::GetVolume(double* volume) { |
85 NOTIMPLEMENTED(); | 65 NOTIMPLEMENTED(); |
86 return false; | 66 return false; |
87 } | 67 } |
88 | 68 |
89 void AudioInputDevice::InitializeOnIOThread(const AudioParameters& params) { | 69 void AudioInputDevice::StartOnCaptureThread() { |
90 stream_id_ = filter_->AddDelegate(this); | 70 // Make sure we don't call Start() more than once. |
henrika_dont_use
2011/07/30 15:50:59
Must we check stream_id on the capture thread? Cou
xians
2011/08/01 11:41:23
ditto, thread safety.
On 2011/07/30 15:50:59, hen
| |
71 if (stream_id_) | |
72 return; | |
73 | |
74 AudioParameters params; | |
75 // TODO(henrika): add support for low-latency mode? | |
76 params.format = AudioParameters::AUDIO_PCM_LINEAR; | |
77 params.channels = channels_; | |
78 params.sample_rate = static_cast<int>(sample_rate_); | |
79 params.bits_per_sample = bits_per_sample_; | |
80 params.samples_per_packet = buffer_size_; | |
81 | |
82 ChildProcess::current()->io_message_loop_proxy()->PostTask(FROM_HERE, | |
henrika_dont_use
2011/07/30 15:50:59
why proxy (again) ;-) ? It was jam who initially s
wjia(left Chromium)
2011/08/01 22:40:13
MessageLoopProxy is preferred over MessageLoop.
| |
83 NewRunnableMethod(this, &AudioInputDevice::AddDelegateOnIOThread)); | |
84 delegate_event_.Wait(); | |
henrika_dont_use
2011/07/30 15:50:59
I have only done a quick check so far but it feels
xians
2011/08/01 11:41:23
My understanding is that this delegate_event.Wait(
wjia(left Chromium)
2011/08/01 22:40:13
Shijing has the answer. The delegate has to be add
| |
85 | |
91 Send(new AudioInputHostMsg_CreateStream(stream_id_, params, true)); | 86 Send(new AudioInputHostMsg_CreateStream(stream_id_, params, true)); |
henrika_dont_use
2011/07/30 15:50:59
Add comment about that true<=>low-latency audio st
wjia(left Chromium)
2011/08/01 22:40:13
?? There was no comment for this.
| |
92 } | 87 } |
93 | 88 |
94 void AudioInputDevice::StartOnIOThread() { | 89 void AudioInputDevice::StartRecordingOnCaptureThread( |
95 if (stream_id_) | |
96 Send(new AudioInputHostMsg_RecordStream(stream_id_)); | |
97 } | |
98 | |
99 void AudioInputDevice::ShutDownOnIOThread() { | |
100 // Make sure we don't call shutdown more than once. | |
101 if (!stream_id_) | |
102 return; | |
103 | |
104 filter_->RemoveDelegate(stream_id_); | |
105 Send(new AudioInputHostMsg_CloseStream(stream_id_)); | |
106 stream_id_ = 0; | |
107 } | |
108 | |
109 void AudioInputDevice::SetVolumeOnIOThread(double volume) { | |
110 if (stream_id_) | |
111 Send(new AudioInputHostMsg_SetVolume(stream_id_, volume)); | |
112 } | |
113 | |
114 void AudioInputDevice::OnLowLatencyCreated( | |
115 base::SharedMemoryHandle handle, | 90 base::SharedMemoryHandle handle, |
116 base::SyncSocket::Handle socket_handle, | 91 base::SyncSocket::Handle socket_handle, |
117 uint32 length) { | 92 uint32 length) { |
118 DCHECK(MessageLoop::current() == ChildProcess::current()->io_message_loop()); | |
119 #if defined(OS_WIN) | 93 #if defined(OS_WIN) |
120 DCHECK(handle); | 94 DCHECK(handle); |
121 DCHECK(socket_handle); | 95 DCHECK(socket_handle); |
122 #else | 96 #else |
123 DCHECK_GE(handle.fd, 0); | 97 DCHECK_GE(handle.fd, 0); |
124 DCHECK_GE(socket_handle, 0); | 98 DCHECK_GE(socket_handle, 0); |
125 #endif | 99 #endif |
126 DCHECK(length); | 100 DCHECK(length); |
127 | 101 |
102 if (!stream_id_) { | |
henrika_dont_use
2011/07/30 15:50:59
How could this happen? If so, can't we detect it e
wjia(left Chromium)
2011/08/01 22:40:13
When Stop is called before OnLowLatencyCreated is
| |
103 base::SharedMemory::CloseHandle(handle); | |
104 base::SyncSocket socket(socket_handle); | |
xians
2011/08/01 11:41:23
Sorry, the same question as Henrik has, + why do w
wjia(left Chromium)
2011/08/01 22:40:13
both handles should be closed.
| |
105 return; | |
106 } | |
107 | |
128 shared_memory_.reset(new base::SharedMemory(handle, false)); | 108 shared_memory_.reset(new base::SharedMemory(handle, false)); |
129 shared_memory_->Map(length); | 109 shared_memory_->Map(length); |
130 | 110 |
131 socket_.reset(new base::SyncSocket(socket_handle)); | 111 socket_.reset(new base::SyncSocket(socket_handle)); |
132 | 112 |
133 audio_thread_.reset( | 113 audio_thread_.reset( |
134 new base::DelegateSimpleThread(this, "renderer_audio_input_thread")); | 114 new base::DelegateSimpleThread(this, "renderer_audio_input_thread")); |
henrika_dont_use
2011/07/30 15:50:59
Note style for thread name and compare with AudioI
wjia(left Chromium)
2011/08/01 22:40:13
I missed this one copied from previous code. Chang
| |
135 audio_thread_->Start(); | 115 audio_thread_->Start(); |
136 | 116 |
137 MessageLoop::current()->PostTask( | 117 Send(new AudioInputHostMsg_RecordStream(stream_id_)); |
138 FROM_HERE, | 118 } |
139 NewRunnableMethod(this, &AudioInputDevice::StartOnIOThread)); | 119 |
120 void AudioInputDevice::ShutDownOnCaptureThread() { | |
121 // Make sure we don't call shutdown more than once. | |
122 if (!stream_id_) { | |
123 stop_event_.Signal(); | |
124 return; | |
125 } | |
126 | |
127 ChildProcess::current()->io_message_loop_proxy()->PostTask(FROM_HERE, | |
128 NewRunnableMethod(this, &AudioInputDevice::RemoveDelegateOnIOThread, | |
129 stream_id_)); | |
130 Send(new AudioInputHostMsg_CloseStream(stream_id_)); | |
henrika_dont_use
2011/07/30 15:50:59
We wait here on Start. Is it not suitable to do so
wjia(left Chromium)
2011/08/01 22:40:13
I moved Wait to a little bit later to allow IO thr
| |
131 stream_id_ = 0; | |
132 | |
133 if (audio_thread_.get()) { | |
xians
2011/08/01 11:41:23
shall we have a state here instead?
Think about th
wjia(left Chromium)
2011/08/01 22:40:13
Immediately after audio_thread_ is joined, it's se
| |
134 socket_->Close(); | |
135 audio_thread_->Join(); | |
136 audio_thread_.reset(NULL); | |
henrika_dont_use
2011/07/30 15:50:59
Is NULL really needed here?
wjia(left Chromium)
2011/08/01 22:40:13
yes, in Shijing's exemplary use case, 2nd Shutdown
| |
137 } | |
138 | |
139 delegate_event_.Wait(); | |
henrika_dont_use
2011/07/30 15:50:59
OK, you wait here instead. Hmm. Not sure why.
wjia(left Chromium)
2011/08/01 22:40:13
Explained as above.
| |
140 stop_event_.Signal(); | |
141 } | |
142 | |
143 void AudioInputDevice::SetVolumeOnCaptureThread(double volume) { | |
144 if (stream_id_) | |
145 Send(new AudioInputHostMsg_SetVolume(stream_id_, volume)); | |
146 } | |
147 | |
148 void AudioInputDevice::OnLowLatencyCreated( | |
149 base::SharedMemoryHandle handle, | |
150 base::SyncSocket::Handle socket_handle, | |
151 uint32 length) { | |
152 DCHECK(MessageLoop::current() == ChildProcess::current()->io_message_loop()); | |
153 message_loop_proxy_->PostTask(FROM_HERE, | |
154 NewRunnableMethod(this, &AudioInputDevice::StartRecordingOnCaptureThread, | |
155 handle, socket_handle, length)); | |
140 } | 156 } |
141 | 157 |
142 void AudioInputDevice::OnVolume(double volume) { | 158 void AudioInputDevice::OnVolume(double volume) { |
143 NOTIMPLEMENTED(); | 159 NOTIMPLEMENTED(); |
144 } | 160 } |
145 | 161 |
146 void AudioInputDevice::Send(IPC::Message* message) { | 162 void AudioInputDevice::Send(IPC::Message* message) { |
147 filter_->Send(message); | 163 ChildProcess::current()->io_message_loop_proxy()->PostTask(FROM_HERE, |
henrika_dont_use
2011/07/30 15:50:59
It feels more clear to name this SendOnIOThread()
wjia(left Chromium)
2011/08/01 22:40:13
Done.
| |
164 NewRunnableMethod(filter_.get(), | |
165 &AudioInputMessageFilter::Send, message)); | |
148 } | 166 } |
149 | 167 |
150 // Our audio thread runs here. We receive captured audio samples on | 168 // Our audio thread runs here. We receive captured audio samples on |
151 // this thread. | 169 // this thread. |
152 void AudioInputDevice::Run() { | 170 void AudioInputDevice::Run() { |
153 audio_thread_->SetThreadPriority(base::kThreadPriority_RealtimeAudio); | 171 audio_thread_->SetThreadPriority(base::kThreadPriority_RealtimeAudio); |
154 | 172 |
155 int pending_data; | 173 int pending_data; |
156 const int samples_per_ms = static_cast<int>(sample_rate_) / 1000; | 174 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; | 175 const int bytes_per_ms = channels_ * (bits_per_sample_ / 8) * samples_per_ms; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
190 bytes_per_sample, | 208 bytes_per_sample, |
191 number_of_frames); | 209 number_of_frames); |
192 } | 210 } |
193 | 211 |
194 // Deliver captured data to the client in floating point format | 212 // Deliver captured data to the client in floating point format |
195 // and update the audio-delay measurement. | 213 // and update the audio-delay measurement. |
196 callback_->Capture(audio_data_, | 214 callback_->Capture(audio_data_, |
197 number_of_frames, | 215 number_of_frames, |
198 audio_delay_milliseconds_); | 216 audio_delay_milliseconds_); |
199 } | 217 } |
218 | |
219 void AudioInputDevice::AddDelegateOnIOThread() { | |
220 stream_id_ = filter_->AddDelegate(this); | |
henrika_dont_use
2011/07/30 15:50:59
Would be nice with a VLOG(1) here. Feels great to
wjia(left Chromium)
2011/08/01 22:40:13
Done.
| |
221 delegate_event_.Signal(); | |
222 } | |
223 | |
224 void AudioInputDevice::RemoveDelegateOnIOThread(int32 stream_id) { | |
225 filter_->RemoveDelegate(stream_id); | |
henrika_dont_use
2011/07/30 15:50:59
VLOG(1) please.
wjia(left Chromium)
2011/08/01 22:40:13
Done.
| |
226 delegate_event_.Signal(); | |
227 } | |
OLD | NEW |