OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "media/audio/pulse/pulse_input.h" | |
6 | |
7 #include <pulse/pulseaudio.h> | |
8 | |
9 #include "base/logging.h" | |
10 #include "base/message_loop.h" | |
11 #include "media/audio/pulse/audio_manager_pulse.h" | |
12 #include "media/audio/pulse/pulse_util.h" | |
13 #include "media/base/seekable_buffer.h" | |
14 | |
15 namespace media { | |
16 | |
17 PulseAudioInputStream::PulseAudioInputStream(AudioManagerPulse* audio_manager, | |
18 const std::string& device_name, | |
19 const AudioParameters& params, | |
20 pa_threaded_mainloop* mainloop, | |
21 pa_context* context) | |
22 : audio_manager_(audio_manager), | |
23 callback_(NULL), | |
24 device_name_(device_name), | |
25 params_(params), | |
26 channels_(0), | |
27 volume_(0.0), | |
28 stream_started_(false), | |
29 pa_mainloop_(mainloop), | |
30 pa_context_(context), | |
31 handle_(NULL), | |
32 context_state_changed_(false) { | |
33 DCHECK(audio_manager_->GetMessageLoop()->BelongsToCurrentThread()); | |
34 DCHECK(mainloop); | |
35 DCHECK(context); | |
36 } | |
37 | |
38 PulseAudioInputStream::~PulseAudioInputStream() { | |
39 // All internal structures should already have been freed in Close(), | |
40 // which calls AudioManagerPulse::Release which deletes this object. | |
41 DCHECK(!handle_); | |
42 } | |
43 | |
44 bool PulseAudioInputStream::Open() { | |
45 DCHECK(audio_manager_->GetMessageLoop()->BelongsToCurrentThread()); | |
46 AutoPulseLock auto_lock(pa_mainloop_); | |
47 | |
48 // Set sample specifications. | |
49 pa_sample_spec pa_sample_specifications; | |
50 pa_sample_specifications.format = BitsToPASampleFormat( | |
51 params_.bits_per_sample()); | |
52 pa_sample_specifications.rate = params_.sample_rate(); | |
53 pa_sample_specifications.channels = params_.channels(); | |
54 | |
55 // Get channel mapping and open recording stream. | |
56 pa_channel_map source_channel_map = ChannelLayoutToPAChannelMap( | |
57 params_.channel_layout()); | |
58 pa_channel_map* map = (source_channel_map.channels != 0)? | |
59 &source_channel_map : NULL; | |
60 | |
61 // Create a new recording stream. | |
62 handle_ = pa_stream_new(pa_context_, "RecordStream", | |
63 &pa_sample_specifications, map); | |
64 if (!handle_) { | |
65 DLOG(ERROR) << "Open: failed to create PA stream"; | |
66 return false; | |
67 } | |
68 | |
69 pa_stream_set_state_callback(handle_, &StreamNotifyCallback, this); | |
70 | |
71 // Set server-side capture buffer metrics. Detailed documentation on what | |
72 // values should be chosen can be found at | |
73 // freedesktop.org/software/pulseaudio/doxygen/structpa__buffer__attr.html. | |
74 pa_buffer_attr buffer_attributes; | |
75 const unsigned int buffer_size = params_.GetBytesPerBuffer(); | |
76 buffer_attributes.maxlength = static_cast<uint32_t>(-1); | |
77 buffer_attributes.tlength = buffer_size; | |
78 buffer_attributes.minreq = buffer_size; | |
79 buffer_attributes.prebuf = static_cast<uint32_t>(-1); | |
80 buffer_attributes.fragsize = buffer_size; | |
81 int flags = PA_STREAM_AUTO_TIMING_UPDATE | | |
82 PA_STREAM_INTERPOLATE_TIMING | | |
83 PA_STREAM_ADJUST_LATENCY | | |
84 PA_STREAM_START_CORKED; | |
85 int err = pa_stream_connect_record( | |
DaleCurtis
2013/02/20 00:17:38
There's no supporting documentation on the return
no longer working on chromium
2013/02/20 14:43:38
I did not find the document to explain the returne
| |
86 handle_, | |
87 device_name_ == AudioManagerBase::kDefaultDeviceId ? | |
88 NULL : device_name_.c_str(), | |
89 &buffer_attributes, | |
90 static_cast<pa_stream_flags_t>(flags)); | |
91 if (err) { | |
92 DLOG(ERROR) << "pa_stream_connect_playback FAILED " << err; | |
93 return false; | |
94 } | |
95 | |
96 // Wait for the stream to be ready. | |
97 while (true) { | |
98 pa_stream_state_t stream_state = pa_stream_get_state(handle_); | |
99 if(!PA_STREAM_IS_GOOD(stream_state)) { | |
100 DLOG(ERROR) << "Invalid PulseAudio stream state"; | |
101 return false; | |
102 } | |
103 | |
104 if (stream_state == PA_STREAM_READY) | |
105 break; | |
106 pa_threaded_mainloop_wait(pa_mainloop_); | |
107 } | |
108 | |
109 pa_stream_set_read_callback(handle_, &ReadCallback, this); | |
110 pa_stream_readable_size(handle_); | |
111 | |
112 buffer_.reset(new media::SeekableBuffer(0, 2 * params_.GetBytesPerBuffer())); | |
113 audio_data_buffer_.reset(new uint8[params_.GetBytesPerBuffer()]); | |
114 return true; | |
115 } | |
116 | |
117 void PulseAudioInputStream::Start(AudioInputCallback* callback) { | |
118 DCHECK(audio_manager_->GetMessageLoop()->BelongsToCurrentThread()); | |
119 DCHECK(callback); | |
120 DCHECK(handle_); | |
121 AutoPulseLock auto_lock(pa_mainloop_); | |
122 | |
123 if (stream_started_) | |
124 return; | |
125 | |
126 // Clean up the old buffer. | |
127 pa_stream_drop(handle_); | |
128 | |
DaleCurtis
2013/02/20 00:17:38
Clear buffer_ ?
no longer working on chromium
2013/02/20 14:43:38
True, thanks.
Done.
| |
129 // Start the streaming. | |
130 stream_started_ = true; | |
131 callback_ = callback; | |
132 | |
133 pa_operation* operation = pa_stream_cork(handle_, 0, NULL, NULL); | |
DaleCurtis
2013/02/20 00:17:38
Can this hang or fail w/o a success callback?
no longer working on chromium
2013/02/20 14:43:38
I am not sure if I understand the question.
Are yo
DaleCurtis
2013/02/21 21:54:21
When I was writing PulseOutput I ran into situatio
no longer working on chromium
2013/02/22 13:12:29
I will keep an eye on this.
| |
134 WaitForOperationCompletion(pa_mainloop_, operation); | |
135 } | |
136 | |
137 void PulseAudioInputStream::Stop() { | |
138 DCHECK(audio_manager_->GetMessageLoop()->BelongsToCurrentThread()); | |
139 AutoPulseLock auto_lock(pa_mainloop_); | |
140 if (!stream_started_) | |
141 return; | |
142 | |
143 // Set the flag to false to stop filling new data to soundcard. | |
144 stream_started_ = false; | |
145 | |
DaleCurtis
2013/02/20 00:17:38
Side note: I really dislike that callback_ isn't s
no longer working on chromium
2013/02/20 14:43:38
I remember it was only the speech recognition usin
DaleCurtis
2013/02/21 21:54:21
Awesome, yes please!
| |
146 pa_operation* operation = pa_stream_flush( | |
147 handle_, &StreamSuccessCallback, this); | |
148 WaitForOperationCompletion(pa_mainloop_, operation); | |
149 | |
150 // Stop the stream. | |
151 pa_stream_set_read_callback(handle_, NULL, NULL); | |
152 operation = pa_stream_cork(handle_, 1, &StreamSuccessCallback, this); | |
153 WaitForOperationCompletion(pa_mainloop_, operation); | |
154 } | |
155 | |
156 void PulseAudioInputStream::Close() { | |
157 DCHECK(audio_manager_->GetMessageLoop()->BelongsToCurrentThread()); | |
158 { | |
159 AutoPulseLock auto_lock(pa_mainloop_); | |
160 if (handle_) { | |
161 // Disable all the callbacks before disconnecting. | |
162 pa_stream_set_state_callback(handle_, NULL, NULL); | |
163 pa_stream_flush(handle_, NULL, NULL); | |
164 | |
165 if (pa_stream_get_state(handle_) != PA_STREAM_UNCONNECTED) | |
166 pa_stream_disconnect(handle_); | |
167 | |
168 // Release PulseAudio structures. | |
169 pa_stream_unref(handle_); | |
170 handle_ = NULL; | |
171 } | |
172 } | |
173 | |
174 if (callback_) | |
175 callback_->OnClose(this); | |
176 | |
177 // Signal to the manager that we're closed and can be removed. | |
178 // This should be the last call in the function as it deletes "this". | |
179 audio_manager_->ReleaseInputStream(this); | |
180 } | |
181 | |
182 double PulseAudioInputStream::GetMaxVolume() { | |
183 return static_cast<double>(PA_VOLUME_NORM); | |
184 } | |
185 | |
186 void PulseAudioInputStream::SetVolume(double volume) { | |
187 AutoPulseLock auto_lock(pa_mainloop_); | |
188 if (!handle_) | |
189 return; | |
190 | |
191 size_t index = pa_stream_get_device_index(handle_); | |
DaleCurtis
2013/02/20 00:17:38
Should this just be done during open and the value
no longer working on chromium
2013/02/20 14:43:38
Not really, the device can be changed on the fly.
DaleCurtis
2013/02/21 21:54:21
Hmmm, we'll need an AudioDeviceListenerPulse or so
no longer working on chromium
2013/02/22 13:12:29
Please point it out if I miss something.
We don't
| |
192 pa_operation* operation = NULL; | |
193 if (!channels_) { | |
194 // Get the number of channels for the source only when the |channels_| is 0. | |
195 // We are assuming the stream source is not changed on the fly here. | |
196 operation = pa_context_get_source_info_by_index( | |
197 pa_context_, index, &VolumeCallback, this); | |
198 WaitForOperationCompletion(pa_mainloop_, operation); | |
199 if (!channels_) { | |
200 DLOG(WARNING) << "Failed to get the number of channels for the source"; | |
201 return; | |
202 } | |
203 } | |
204 | |
205 pa_cvolume pa_volume; | |
206 pa_cvolume_set(&pa_volume, channels_, volume); | |
207 operation = pa_context_set_source_volume_by_index( | |
208 pa_context_, index, &pa_volume, NULL, NULL); | |
209 | |
210 // Don't need to wait for this task to complete. | |
211 pa_operation_unref(operation); | |
212 } | |
213 | |
214 double PulseAudioInputStream::GetVolume() { | |
215 AutoPulseLock auto_lock(pa_mainloop_); | |
216 if (!handle_) | |
217 return 0.0; | |
218 | |
219 size_t index = pa_stream_get_device_index(handle_); | |
220 pa_operation* operation = pa_context_get_source_info_by_index( | |
221 pa_context_, index, &VolumeCallback, this); | |
222 WaitForOperationCompletion(pa_mainloop_, operation); | |
DaleCurtis
2013/02/20 00:17:38
Is it worth doing this or should you just cache th
no longer working on chromium
2013/02/20 14:43:38
The volume can be changed by different ways.
| |
223 | |
224 return volume_; | |
225 } | |
226 | |
227 // static, used by pa_stream_set_read_callback. | |
228 void PulseAudioInputStream::ReadCallback(pa_stream* handle, | |
229 size_t length, | |
230 void* user_data) { | |
231 PulseAudioInputStream* stream = | |
232 reinterpret_cast<PulseAudioInputStream*>(user_data); | |
233 | |
234 stream->ReadData(); | |
235 } | |
236 | |
237 // static, used by pa_context_get_source_info_by_index. | |
238 void PulseAudioInputStream::VolumeCallback(pa_context* context, | |
239 const pa_source_info* info, | |
240 int error, void* user_data) { | |
241 PulseAudioInputStream* stream = | |
242 reinterpret_cast<PulseAudioInputStream*>(user_data); | |
243 | |
244 if (error) { | |
DaleCurtis
2013/02/20 00:17:38
The docs indicate that value is "eol", without ela
no longer working on chromium
2013/02/20 14:43:38
pulse has a very poor documentation.
| |
245 pa_threaded_mainloop_signal(stream->pa_mainloop_, 0); | |
246 return; | |
247 } | |
248 | |
249 if (stream->channels_ != info->channel_map.channels) | |
DaleCurtis
2013/02/20 00:17:38
Why would these values differ? This seems sketchy.
no longer working on chromium
2013/02/20 14:43:38
Same reason as the comment in line 191, the device
| |
250 stream->channels_ = info->channel_map.channels; | |
251 | |
252 pa_volume_t volume = PA_VOLUME_MUTED; // Minimum possible value. | |
253 // Use the max volume of any channel as the volume. | |
254 for (int i = 0; i < stream->channels_; ++i) { | |
255 if (volume < info->volume.values[i]) | |
256 volume = info->volume.values[i]; | |
257 } | |
258 | |
259 stream->volume_ = static_cast<double>(volume); | |
260 } | |
261 | |
262 // static, pa_stream_success_cb_t | |
263 void PulseAudioInputStream::StreamSuccessCallback(pa_stream* s, int error, | |
264 void* user_data) { | |
265 PulseAudioInputStream* stream = | |
266 static_cast<PulseAudioInputStream*>(user_data); | |
267 | |
268 pa_threaded_mainloop_signal(stream->pa_mainloop_, 0); | |
269 } | |
270 | |
271 // static, used by pa_stream_set_state_callback. | |
272 void PulseAudioInputStream::StreamNotifyCallback(pa_stream* s, | |
273 void* user_data) { | |
274 PulseAudioInputStream* stream = | |
275 reinterpret_cast<PulseAudioInputStream*>(user_data); | |
276 if (s && stream->callback_ && | |
277 pa_stream_get_state(s) == PA_STREAM_FAILED) { | |
278 stream->callback_->OnError(stream, pa_context_errno(stream->pa_context_)); | |
279 } | |
280 | |
281 pa_threaded_mainloop_signal(stream->pa_mainloop_, 0); | |
282 } | |
283 | |
284 void PulseAudioInputStream::ReadData() { | |
285 uint32 hardware_delay = GetHardwareLatencyInBytes( | |
286 handle_, params_.sample_rate(), params_.GetBytesPerFrame()); | |
287 | |
288 // Update the AGC volume level once every second. Note that, | |
289 // |volume| is also updated each time SetVolume() is called | |
290 // through IPC by the render-side AGC. | |
291 double normalized_volume = 0.0; | |
292 QueryAgcVolume(&normalized_volume); | |
293 | |
294 while (true) { | |
DaleCurtis
2013/02/20 00:17:38
while (pa_stream_readable_size(handle_) > 0) inste
no longer working on chromium
2013/02/20 14:43:38
I changed it to use a
do {
} while (a_stream_read
| |
295 size_t length = 0; | |
296 const void* data = NULL; | |
297 pa_stream_peek(handle_, &data, &length); | |
298 if (!data || length == 0) | |
299 break; | |
300 | |
301 buffer_->Append(reinterpret_cast<const uint8*>(data), length); | |
302 | |
303 // Checks if we still have data. | |
304 pa_stream_drop(handle_); | |
305 if (pa_stream_readable_size(handle_) <= 0) | |
306 break; | |
307 } | |
308 | |
309 int packet_size = params_.GetBytesPerBuffer(); | |
310 while (buffer_->forward_bytes() >= packet_size) { | |
311 buffer_->Read(audio_data_buffer_.get(), packet_size); | |
312 callback_->OnData(this, audio_data_buffer_.get(), packet_size, | |
313 hardware_delay, normalized_volume); | |
314 | |
315 if (buffer_->forward_bytes() < packet_size) | |
316 break; | |
317 | |
318 // TODO(xians): improve the code by implementing a WaitTillDataReady on the | |
319 // input side. | |
320 DLOG(WARNING) << "OnData is being called consecutively, sleep 2ms to " | |
321 << "wait until render consumes the data"; | |
322 base::PlatformThread::Sleep( | |
DaleCurtis
2013/02/20 00:17:38
The general delay for this is 5ms.
no longer working on chromium
2013/02/20 14:43:38
Done.
| |
323 base::TimeDelta::FromMilliseconds(2)); | |
324 } | |
325 | |
326 pa_threaded_mainloop_signal(pa_mainloop_, 0); | |
327 } | |
328 | |
329 } // namespace media | |
OLD | NEW |