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" | 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 | |
|
scherkus (not reviewing)
2011/08/23 16:54:19
Stop -> Stop()
henrika (OOO until Aug 14)
2011/08/24 15:03:55
Done.
| |
| 17 // the calling thread forever. | |
| 18 static const base::TimeDelta kMaxTimeOutMs = | |
|
scherkus (not reviewing)
2011/08/23 16:54:19
nit: drop Ms suffix since TimeDelats are unitless
henrika (OOO until Aug 14)
2011/08/24 15:03:55
Thanks. Done.
| |
| 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 |
|
scherkus (not reviewing)
2011/08/23 16:54:19
don't forget () suffix on function call names in c
henrika (OOO until Aug 14)
2011/08/24 15:03:55
Done.
| |
| 73 audio_thread_->Join(); | 75 // function call. |
| 76 if (completion.TimedWait(kMaxTimeOutMs)) { | |
|
scherkus (not reviewing)
2011/08/23 16:54:19
out of curiosity who is usually the calling thread
henrika (OOO until Aug 14)
2011/08/24 15:03:55
The calling client will be calling from the main r
| |
| 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. | |
|
scherkus (not reviewing)
2011/08/23 16:54:19
nit: most of these comments are useless as you hav
| |
| 81 audio_thread_->Join(); | |
| 82 // Ensures that we can call Stop multiple times. | |
|
scherkus (not reviewing)
2011/08/23 16:54:19
who's calling Stop() multiple times?
henrika (OOO until Aug 14)
2011/08/24 15:03:55
Well, we just want to ensure that the design can d
| |
| 83 audio_thread_.reset(NULL); | |
| 84 } | |
| 85 } else { | |
| 86 DLOG(ERROR) << "Failed to shut down audio input on IO thread"; | |
|
scherkus (not reviewing)
2011/08/23 16:54:19
how often do we expect to see this in practice?
m
henrika (OOO until Aug 14)
2011/08/24 15:03:55
Very good question. Classic comment: "should never
| |
| 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) << __FUNCTION__ << "[stream_id=" << stream_id_ << "]"; | |
|
scherkus (not reviewing)
2011/08/23 16:54:19
use OnLowLatencyCreated ?
henrika (OOO until Aug 14)
2011/08/24 15:03:55
Done.
| |
| 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 } |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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 } |
| OLD | NEW |