Chromium Code Reviews| 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" | |
| 8 #include "content/common/child_process.h" | 7 #include "content/common/child_process.h" |
| 9 #include "content/common/media/audio_messages.h" | 8 #include "content/common/media/audio_messages.h" |
| 10 #include "content/common/view_messages.h" | 9 #include "content/common/view_messages.h" |
| 11 #include "content/renderer/render_thread.h" | 10 #include "content/renderer/render_thread.h" |
| 11 #include "content/renderer/media/audio_input_device_event_handler.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 base::MessageLoopProxy* message_loop_proxy, | |
| 18 AudioInputDeviceEventHandler* handler, | |
|
henrika_dont_use
2011/08/07 16:52:27
Just an idea; could it be useful to allow NULL her
wjia(left Chromium)
2011/08/09 01:40:36
The client is required to wait for "stopped" signa
| |
| 17 CaptureCallback* callback) | 19 CaptureCallback* callback) |
| 18 : buffer_size_(buffer_size), | 20 : buffer_size_(buffer_size), |
| 19 channels_(channels), | 21 channels_(channels), |
| 20 bits_per_sample_(16), | 22 bits_per_sample_(16), |
| 21 sample_rate_(sample_rate), | 23 sample_rate_(sample_rate), |
| 22 callback_(callback), | 24 callback_(callback), |
| 25 event_handler_(handler), | |
| 23 audio_delay_milliseconds_(0), | 26 audio_delay_milliseconds_(0), |
| 24 volume_(1.0), | 27 volume_(1.0), |
| 28 capture_message_loop_proxy_(message_loop_proxy), | |
| 29 state_(kStopped), | |
| 25 stream_id_(0) { | 30 stream_id_(0) { |
| 31 DCHECK(handler); | |
| 26 filter_ = RenderThread::current()->audio_input_message_filter(); | 32 filter_ = RenderThread::current()->audio_input_message_filter(); |
| 27 audio_data_.reserve(channels); | 33 audio_data_.reserve(channels); |
| 28 for (int i = 0; i < channels; ++i) { | 34 for (int i = 0; i < channels; ++i) { |
| 29 float* channel_data = new float[buffer_size]; | 35 float* channel_data = new float[buffer_size]; |
| 30 audio_data_.push_back(channel_data); | 36 audio_data_.push_back(channel_data); |
| 31 } | 37 } |
| 32 } | 38 } |
| 33 | 39 |
| 34 AudioInputDevice::~AudioInputDevice() { | 40 AudioInputDevice::~AudioInputDevice() { |
| 35 // Make sure we have been shut down. | |
| 36 DCHECK_EQ(0, stream_id_); | |
| 37 Stop(); | |
| 38 for (int i = 0; i < channels_; ++i) | 41 for (int i = 0; i < channels_; ++i) |
| 39 delete [] audio_data_[i]; | 42 delete [] audio_data_[i]; |
| 40 } | 43 } |
| 41 | 44 |
| 42 bool AudioInputDevice::Start() { | 45 bool AudioInputDevice::Start() { |
| 43 // Make sure we don't call Start() more than once. | 46 capture_message_loop_proxy_->PostTask(FROM_HERE, |
| 44 DCHECK_EQ(0, stream_id_); | 47 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; | 48 return true; |
| 61 } | 49 } |
| 62 | 50 |
| 63 bool AudioInputDevice::Stop() { | 51 bool AudioInputDevice::Stop() { |
| 64 if (!stream_id_) | 52 capture_message_loop_proxy_->PostTask(FROM_HERE, |
| 65 return false; | 53 NewRunnableMethod(this, &AudioInputDevice::StopOnCaptureThread)); |
| 66 | |
| 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; | 54 return true; |
| 77 } | 55 } |
| 78 | 56 |
| 79 bool AudioInputDevice::SetVolume(double volume) { | 57 bool AudioInputDevice::SetVolume(double volume) { |
| 80 NOTIMPLEMENTED(); | 58 NOTIMPLEMENTED(); |
| 81 return false; | 59 return false; |
| 82 } | 60 } |
| 83 | 61 |
| 84 bool AudioInputDevice::GetVolume(double* volume) { | 62 bool AudioInputDevice::GetVolume(double* volume) { |
| 85 NOTIMPLEMENTED(); | 63 NOTIMPLEMENTED(); |
| 86 return false; | 64 return false; |
| 87 } | 65 } |
| 88 | 66 |
| 89 void AudioInputDevice::InitializeOnIOThread(const AudioParameters& params) { | 67 void AudioInputDevice::StartOnCaptureThread() { |
| 90 stream_id_ = filter_->AddDelegate(this); | 68 // Client should call Start() after fully stopped. |
| 69 DCHECK_NE(state_, kStopping); | |
| 70 // Make sure we don't call Start() more than once. | |
| 71 if (state_ == kStarting || state_ == kStarted) | |
| 72 return; | |
| 73 | |
| 74 state_ = kStarting; | |
| 75 ChildProcess::current()->io_message_loop_proxy()->PostTask(FROM_HERE, | |
| 76 NewRunnableMethod(this, &AudioInputDevice::AddDelegateOnIOThread)); | |
| 77 } | |
| 78 | |
| 79 void AudioInputDevice::OnDelegateAddedOnCaptureThread(int32 stream_id) { | |
| 80 stream_id_ = stream_id; | |
| 81 | |
| 82 // Got Stop() during starting. | |
| 83 if (state_ == kStopping) { | |
| 84 event_handler_->OnRecordingStarted(); | |
|
henrika_dont_use
2011/08/07 16:52:27
Why do we want to signal RecordingStarted here?
wjia(left Chromium)
2011/08/09 01:40:36
The interface is: client calls Start() and expects
| |
| 85 ChildProcess::current()->io_message_loop_proxy()->PostTask(FROM_HERE, | |
| 86 NewRunnableMethod(this, &AudioInputDevice::RemoveDelegateOnIOThread, | |
| 87 stream_id_)); | |
| 88 stream_id_ = 0; | |
| 89 return; | |
| 90 } | |
| 91 | |
| 92 AudioParameters params; | |
| 93 // TODO(henrika): add support for low-latency mode? | |
| 94 params.format = AudioParameters::AUDIO_PCM_LINEAR; | |
| 95 params.channels = channels_; | |
| 96 params.sample_rate = static_cast<int>(sample_rate_); | |
| 97 params.bits_per_sample = bits_per_sample_; | |
| 98 params.samples_per_packet = buffer_size_; | |
| 99 | |
| 91 Send(new AudioInputHostMsg_CreateStream(stream_id_, params, true)); | 100 Send(new AudioInputHostMsg_CreateStream(stream_id_, params, true)); |
| 92 } | 101 } |
| 93 | 102 |
| 94 void AudioInputDevice::StartOnIOThread() { | 103 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, | 104 base::SharedMemoryHandle handle, |
| 116 base::SyncSocket::Handle socket_handle, | 105 base::SyncSocket::Handle socket_handle, |
| 117 uint32 length) { | 106 uint32 length) { |
| 118 DCHECK(MessageLoop::current() == ChildProcess::current()->io_message_loop()); | |
| 119 #if defined(OS_WIN) | 107 #if defined(OS_WIN) |
| 120 DCHECK(handle); | 108 DCHECK(handle); |
| 121 DCHECK(socket_handle); | 109 DCHECK(socket_handle); |
| 122 #else | 110 #else |
| 123 DCHECK_GE(handle.fd, 0); | 111 DCHECK_GE(handle.fd, 0); |
| 124 DCHECK_GE(socket_handle, 0); | 112 DCHECK_GE(socket_handle, 0); |
| 125 #endif | 113 #endif |
| 126 DCHECK(length); | 114 DCHECK(length); |
| 127 | 115 |
| 116 // Got Stop() during starting. | |
| 117 if (state_ == kStopping) { | |
| 118 base::SharedMemory::CloseHandle(handle); | |
| 119 base::SyncSocket socket(socket_handle); | |
| 120 event_handler_->OnRecordingStarted(); | |
|
henrika_dont_use
2011/08/07 16:52:27
Why?
wjia(left Chromium)
2011/08/09 01:40:36
Same as above.
| |
| 121 ChildProcess::current()->io_message_loop_proxy()->PostTask(FROM_HERE, | |
| 122 NewRunnableMethod(this, &AudioInputDevice::RemoveDelegateOnIOThread, | |
| 123 stream_id_)); | |
| 124 return; | |
| 125 } | |
| 126 | |
| 128 shared_memory_.reset(new base::SharedMemory(handle, false)); | 127 shared_memory_.reset(new base::SharedMemory(handle, false)); |
| 129 shared_memory_->Map(length); | 128 shared_memory_->Map(length); |
| 130 | 129 |
| 131 socket_.reset(new base::SyncSocket(socket_handle)); | 130 socket_.reset(new base::SyncSocket(socket_handle)); |
| 132 | 131 |
| 133 audio_thread_.reset( | 132 audio_thread_.reset( |
| 134 new base::DelegateSimpleThread(this, "renderer_audio_input_thread")); | 133 new base::DelegateSimpleThread(this, "RendererAudioInputThread")); |
| 135 audio_thread_->Start(); | 134 audio_thread_->Start(); |
| 136 | 135 |
| 137 MessageLoop::current()->PostTask( | 136 Send(new AudioInputHostMsg_RecordStream(stream_id_)); |
| 138 FROM_HERE, | 137 state_ = kStarted; |
| 139 NewRunnableMethod(this, &AudioInputDevice::StartOnIOThread)); | 138 event_handler_->OnRecordingStarted(); |
| 139 } | |
| 140 | |
| 141 void AudioInputDevice::StopOnCaptureThread() { | |
| 142 // Make sure we don't call shutdown more than once. | |
| 143 if (state_ == kStopping || state_ == kStopped) | |
| 144 return; | |
| 145 | |
| 146 // Got Stop() during starting. | |
| 147 if (state_ == kStarting) { | |
| 148 state_ = kStopping; | |
| 149 return; | |
| 150 } | |
| 151 | |
| 152 ChildProcess::current()->io_message_loop_proxy()->PostTask(FROM_HERE, | |
| 153 NewRunnableMethod(this, &AudioInputDevice::RemoveDelegateOnIOThread, | |
| 154 stream_id_)); | |
| 155 } | |
| 156 | |
| 157 void AudioInputDevice::OnDelegateRemovedOnCaptureThread() { | |
| 158 if (stream_id_) { | |
| 159 Send(new AudioInputHostMsg_CloseStream(stream_id_)); | |
| 160 stream_id_ = 0; | |
| 161 } | |
| 162 | |
| 163 if (audio_thread_.get()) { | |
| 164 socket_->Close(); | |
| 165 audio_thread_->Join(); | |
| 166 audio_thread_.reset(NULL); | |
| 167 } | |
| 168 | |
| 169 state_ = kStopped; | |
| 170 event_handler_->OnRecordingStopped(); | |
| 171 } | |
| 172 | |
| 173 | |
| 174 void AudioInputDevice::SetVolumeOnCaptureThread(double volume) { | |
| 175 if (stream_id_) | |
| 176 Send(new AudioInputHostMsg_SetVolume(stream_id_, volume)); | |
| 177 } | |
| 178 | |
| 179 void AudioInputDevice::OnLowLatencyCreated( | |
| 180 base::SharedMemoryHandle handle, | |
| 181 base::SyncSocket::Handle socket_handle, | |
| 182 uint32 length) { | |
| 183 DCHECK(ChildProcess::current()->io_message_loop_proxy()-> | |
|
henrika_dont_use
2011/08/07 16:52:27
Why is this test needed?
wjia(left Chromium)
2011/08/09 01:40:36
This is a public function.
| |
| 184 BelongsToCurrentThread()); | |
| 185 capture_message_loop_proxy_->PostTask(FROM_HERE, | |
| 186 NewRunnableMethod(this, &AudioInputDevice::StartRecordingOnCaptureThread, | |
| 187 handle, socket_handle, length)); | |
| 140 } | 188 } |
| 141 | 189 |
| 142 void AudioInputDevice::OnVolume(double volume) { | 190 void AudioInputDevice::OnVolume(double volume) { |
| 143 NOTIMPLEMENTED(); | 191 NOTIMPLEMENTED(); |
| 144 } | 192 } |
| 145 | 193 |
| 146 void AudioInputDevice::Send(IPC::Message* message) { | 194 void AudioInputDevice::Send(IPC::Message* message) { |
| 147 filter_->Send(message); | 195 ChildProcess::current()->io_message_loop_proxy()->PostTask(FROM_HERE, |
| 196 NewRunnableMethod(filter_.get(), | |
| 197 &AudioInputMessageFilter::Send, message)); | |
| 148 } | 198 } |
| 149 | 199 |
| 150 // Our audio thread runs here. We receive captured audio samples on | 200 // Our audio thread runs here. We receive captured audio samples on |
| 151 // this thread. | 201 // this thread. |
| 152 void AudioInputDevice::Run() { | 202 void AudioInputDevice::Run() { |
| 153 audio_thread_->SetThreadPriority(base::kThreadPriority_RealtimeAudio); | 203 audio_thread_->SetThreadPriority(base::kThreadPriority_RealtimeAudio); |
| 154 | 204 |
| 155 int pending_data; | 205 int pending_data; |
| 156 const int samples_per_ms = static_cast<int>(sample_rate_) / 1000; | 206 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; | 207 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, | 240 bytes_per_sample, |
| 191 number_of_frames); | 241 number_of_frames); |
| 192 } | 242 } |
| 193 | 243 |
| 194 // Deliver captured data to the client in floating point format | 244 // Deliver captured data to the client in floating point format |
| 195 // and update the audio-delay measurement. | 245 // and update the audio-delay measurement. |
| 196 callback_->Capture(audio_data_, | 246 callback_->Capture(audio_data_, |
| 197 number_of_frames, | 247 number_of_frames, |
| 198 audio_delay_milliseconds_); | 248 audio_delay_milliseconds_); |
| 199 } | 249 } |
| 250 | |
| 251 void AudioInputDevice::AddDelegateOnIOThread() { | |
| 252 int32 stream_id = filter_->AddDelegate(this); | |
| 253 VLOG(1) << "add stream_id = " << stream_id; | |
| 254 capture_message_loop_proxy_->PostTask(FROM_HERE, | |
| 255 NewRunnableMethod(this, &AudioInputDevice::OnDelegateAddedOnCaptureThread, | |
| 256 stream_id)); | |
| 257 } | |
| 258 | |
| 259 void AudioInputDevice::RemoveDelegateOnIOThread(int32 stream_id) { | |
| 260 filter_->RemoveDelegate(stream_id); | |
| 261 VLOG(1) << "remove stream_id = " << stream_id; | |
| 262 capture_message_loop_proxy_->PostTask(FROM_HERE, | |
| 263 NewRunnableMethod(this, | |
| 264 &AudioInputDevice::OnDelegateRemovedOnCaptureThread)); | |
| 265 } | |
| OLD | NEW |