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" |
(...skipping 14 matching lines...) Expand all Loading... | |
25 stream_id_(0) { | 25 stream_id_(0) { |
26 filter_ = RenderThread::current()->audio_input_message_filter(); | 26 filter_ = RenderThread::current()->audio_input_message_filter(); |
27 audio_data_.reserve(channels); | 27 audio_data_.reserve(channels); |
28 for (int i = 0; i < channels; ++i) { | 28 for (int i = 0; i < channels; ++i) { |
29 float* channel_data = new float[buffer_size]; | 29 float* channel_data = new float[buffer_size]; |
30 audio_data_.push_back(channel_data); | 30 audio_data_.push_back(channel_data); |
31 } | 31 } |
32 } | 32 } |
33 | 33 |
34 AudioInputDevice::~AudioInputDevice() { | 34 AudioInputDevice::~AudioInputDevice() { |
35 // Make sure we have been shut down. | |
36 DCHECK_EQ(0, stream_id_); | |
37 Stop(); | 35 Stop(); |
38 for (int i = 0; i < channels_; ++i) | 36 for (int i = 0; i < channels_; ++i) |
39 delete [] audio_data_[i]; | 37 delete [] audio_data_[i]; |
40 } | 38 } |
41 | 39 |
42 bool AudioInputDevice::Start() { | 40 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; | 41 AudioParameters params; |
49 // TODO(henrika): add support for low-latency mode? | 42 // TODO(henrika): add support for low-latency mode? |
50 params.format = AudioParameters::AUDIO_PCM_LINEAR; | 43 params.format = AudioParameters::AUDIO_PCM_LINEAR; |
51 params.channels = channels_; | 44 params.channels = channels_; |
52 params.sample_rate = static_cast<int>(sample_rate_); | 45 params.sample_rate = static_cast<int>(sample_rate_); |
53 params.bits_per_sample = bits_per_sample_; | 46 params.bits_per_sample = bits_per_sample_; |
54 params.samples_per_packet = buffer_size_; | 47 params.samples_per_packet = buffer_size_; |
55 | 48 |
56 ChildProcess::current()->io_message_loop()->PostTask( | 49 ChildProcess::current()->io_message_loop()->PostTask( |
57 FROM_HERE, | 50 FROM_HERE, |
58 NewRunnableMethod(this, &AudioInputDevice::InitializeOnIOThread, params)); | 51 NewRunnableMethod(this, &AudioInputDevice::InitializeOnIOThread, params)); |
59 | |
60 return true; | |
61 } | 52 } |
62 | 53 |
63 bool AudioInputDevice::Stop() { | 54 bool AudioInputDevice::Stop() { |
64 if (!stream_id_) | 55 base::WaitableEvent completion(false, false); |
65 return false; | |
66 | 56 |
67 ChildProcess::current()->io_message_loop()->PostTask( | 57 ChildProcess::current()->io_message_loop()->PostTask( |
68 FROM_HERE, | 58 FROM_HERE, |
69 NewRunnableMethod(this, &AudioInputDevice::ShutDownOnIOThread)); | 59 NewRunnableMethod(this, &AudioInputDevice::ShutDownOnIOThread, |
60 &completion)); | |
70 | 61 |
71 if (audio_thread_.get()) { | 62 // We wait here for the IO task to be completed to remove race conflicts |
72 socket_->Close(); | 63 // with OnLowLatencyCreated and to ensure that Stop can be called from |
73 audio_thread_->Join(); | 64 // the destructor. |
65 if (completion.Wait()) { | |
66 if (audio_thread_.get()) { | |
67 // Terminate the main thread function in the audio thread. | |
68 socket_->Close(); | |
69 // Wait for the audio thread to exit. | |
70 audio_thread_->Join(); | |
71 // Ensures that we can call Stop multiple times. | |
72 audio_thread_.reset(NULL); | |
73 } | |
74 } else { | |
75 DLOG(ERROR) << "failed to shut down audio input on IO thread"; | |
76 return false; | |
74 } | 77 } |
75 | 78 |
76 return true; | 79 return true; |
77 } | 80 } |
78 | 81 |
79 bool AudioInputDevice::SetVolume(double volume) { | 82 bool AudioInputDevice::SetVolume(double volume) { |
80 NOTIMPLEMENTED(); | 83 NOTIMPLEMENTED(); |
81 return false; | 84 return false; |
82 } | 85 } |
83 | 86 |
84 bool AudioInputDevice::GetVolume(double* volume) { | 87 bool AudioInputDevice::GetVolume(double* volume) { |
85 NOTIMPLEMENTED(); | 88 NOTIMPLEMENTED(); |
86 return false; | 89 return false; |
87 } | 90 } |
88 | 91 |
89 void AudioInputDevice::InitializeOnIOThread(const AudioParameters& params) { | 92 void AudioInputDevice::InitializeOnIOThread(const AudioParameters& params) { |
tommi (sloooow) - chröme
2011/08/17 08:35:18
DCHECK(MessageLoop::current() == ChildProcess::cur
henrika (OOO until Aug 14)
2011/08/17 15:34:13
Done.
| |
93 // Make sure we don't call Start() more than once. | |
94 DCHECK_EQ(0, stream_id_); | |
95 if (stream_id_) | |
96 return; | |
97 | |
90 stream_id_ = filter_->AddDelegate(this); | 98 stream_id_ = filter_->AddDelegate(this); |
91 Send(new AudioInputHostMsg_CreateStream(stream_id_, params, true)); | 99 Send(new AudioInputHostMsg_CreateStream(stream_id_, params, true)); |
92 } | 100 } |
93 | 101 |
94 void AudioInputDevice::StartOnIOThread() { | 102 void AudioInputDevice::StartOnIOThread() { |
95 if (stream_id_) | 103 if (stream_id_) |
tommi (sloooow) - chröme
2011/08/17 08:35:18
DCHECK(MessageLoop::current() == ChildProcess::cur
henrika (OOO until Aug 14)
2011/08/17 15:34:13
Done.
| |
96 Send(new AudioInputHostMsg_RecordStream(stream_id_)); | 104 Send(new AudioInputHostMsg_RecordStream(stream_id_)); |
97 } | 105 } |
98 | 106 |
99 void AudioInputDevice::ShutDownOnIOThread() { | 107 void AudioInputDevice::ShutDownOnIOThread(base::WaitableEvent* completion) { |
100 // Make sure we don't call shutdown more than once. | 108 // Make sure we don't call shutdown more than once. |
tommi (sloooow) - chröme
2011/08/17 08:35:18
DCHECK(MessageLoop::current() == ChildProcess::cur
henrika (OOO until Aug 14)
2011/08/17 15:34:13
Done.
| |
101 if (!stream_id_) | 109 if (!stream_id_) { |
110 completion->Signal(); | |
102 return; | 111 return; |
112 } | |
103 | 113 |
104 filter_->RemoveDelegate(stream_id_); | 114 filter_->RemoveDelegate(stream_id_); |
105 Send(new AudioInputHostMsg_CloseStream(stream_id_)); | 115 Send(new AudioInputHostMsg_CloseStream(stream_id_)); |
106 stream_id_ = 0; | 116 stream_id_ = 0; |
117 | |
118 completion->Signal(); | |
107 } | 119 } |
108 | 120 |
109 void AudioInputDevice::SetVolumeOnIOThread(double volume) { | 121 void AudioInputDevice::SetVolumeOnIOThread(double volume) { |
110 if (stream_id_) | 122 if (stream_id_) |
tommi (sloooow) - chröme
2011/08/17 08:35:18
DCHECK(MessageLoop::current() == ChildProcess::cur
henrika (OOO until Aug 14)
2011/08/17 15:34:13
Done.
| |
111 Send(new AudioInputHostMsg_SetVolume(stream_id_, volume)); | 123 Send(new AudioInputHostMsg_SetVolume(stream_id_, volume)); |
112 } | 124 } |
113 | 125 |
114 void AudioInputDevice::OnLowLatencyCreated( | 126 void AudioInputDevice::OnLowLatencyCreated( |
115 base::SharedMemoryHandle handle, | 127 base::SharedMemoryHandle handle, |
116 base::SyncSocket::Handle socket_handle, | 128 base::SyncSocket::Handle socket_handle, |
117 uint32 length) { | 129 uint32 length) { |
118 DCHECK(MessageLoop::current() == ChildProcess::current()->io_message_loop()); | 130 DCHECK(MessageLoop::current() == ChildProcess::current()->io_message_loop()); |
119 #if defined(OS_WIN) | 131 #if defined(OS_WIN) |
120 DCHECK(handle); | 132 DCHECK(handle); |
121 DCHECK(socket_handle); | 133 DCHECK(socket_handle); |
122 #else | 134 #else |
123 DCHECK_GE(handle.fd, 0); | 135 DCHECK_GE(handle.fd, 0); |
124 DCHECK_GE(socket_handle, 0); | 136 DCHECK_GE(socket_handle, 0); |
125 #endif | 137 #endif |
126 DCHECK(length); | 138 DCHECK(length); |
127 | 139 |
140 // Takes care of the case when Stop is called before OnLowLatencyCreated. | |
141 if (!stream_id_) { | |
142 base::SharedMemory::CloseHandle(handle); | |
143 base::SyncSocket socket(socket_handle); | |
tommi (sloooow) - chröme
2011/08/17 08:35:18
is this the normal way of closing a socket? i.e.
henrika (OOO until Aug 14)
2011/08/17 15:34:13
Not sure if it is normal but it works ;-) And, it
| |
144 return; | |
145 } | |
146 | |
128 shared_memory_.reset(new base::SharedMemory(handle, false)); | 147 shared_memory_.reset(new base::SharedMemory(handle, false)); |
129 shared_memory_->Map(length); | 148 shared_memory_->Map(length); |
130 | 149 |
131 socket_.reset(new base::SyncSocket(socket_handle)); | 150 socket_.reset(new base::SyncSocket(socket_handle)); |
132 | 151 |
133 audio_thread_.reset( | 152 audio_thread_.reset( |
134 new base::DelegateSimpleThread(this, "renderer_audio_input_thread")); | 153 new base::DelegateSimpleThread(this, "RendererAudioInputThread")); |
135 audio_thread_->Start(); | 154 audio_thread_->Start(); |
136 | 155 |
137 MessageLoop::current()->PostTask( | 156 MessageLoop::current()->PostTask( |
138 FROM_HERE, | 157 FROM_HERE, |
139 NewRunnableMethod(this, &AudioInputDevice::StartOnIOThread)); | 158 NewRunnableMethod(this, &AudioInputDevice::StartOnIOThread)); |
140 } | 159 } |
141 | 160 |
142 void AudioInputDevice::OnVolume(double volume) { | 161 void AudioInputDevice::OnVolume(double volume) { |
143 NOTIMPLEMENTED(); | 162 NOTIMPLEMENTED(); |
144 } | 163 } |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
190 bytes_per_sample, | 209 bytes_per_sample, |
191 number_of_frames); | 210 number_of_frames); |
192 } | 211 } |
193 | 212 |
194 // Deliver captured data to the client in floating point format | 213 // Deliver captured data to the client in floating point format |
195 // and update the audio-delay measurement. | 214 // and update the audio-delay measurement. |
196 callback_->Capture(audio_data_, | 215 callback_->Capture(audio_data_, |
197 number_of_frames, | 216 number_of_frames, |
198 audio_delay_milliseconds_); | 217 audio_delay_milliseconds_); |
199 } | 218 } |
OLD | NEW |