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 "base/logging.h" | |
8 #include "media/audio/linux/audio_manager_linux.h" | |
9 #include "media/audio/pulse/pulse_util.h" | |
10 #include "media/base/seekable_buffer.h" | |
11 | |
12 namespace media { | |
13 | |
14 PulseAudioInputStream::PulseAudioInputStream(AudioManagerLinux* audio_manager, | |
15 const std::string& device_name, | |
16 const AudioParameters& params, | |
17 pa_threaded_mainloop* mainloop, | |
18 pa_context* context) | |
19 : audio_manager_(audio_manager), | |
20 callback_(NULL), | |
21 device_name_(device_name), | |
22 params_(params), | |
23 channels_(0), | |
24 volume_(0.0), | |
25 stream_started_(false), | |
26 pa_mainloop_(mainloop), | |
27 pa_context_(context), | |
28 handle_(NULL) { | |
29 DCHECK(audio_manager_->GetMessageLoop()->BelongsToCurrentThread()); | |
30 DCHECK(mainloop); | |
31 DCHECK(context); | |
32 } | |
33 | |
34 PulseAudioInputStream::~PulseAudioInputStream() { | |
35 // All internal structures should already have been freed in Close(), | |
36 // which calls AudioManagerPulse::Release which deletes this object. | |
37 DCHECK(!handle_); | |
38 } | |
39 | |
40 bool PulseAudioInputStream::Open() { | |
41 DCHECK(audio_manager_->GetMessageLoop()->BelongsToCurrentThread()); | |
42 AutoPulseLock auto_lock(pa_mainloop_); | |
43 | |
44 // Set sample specifications. | |
45 pa_sample_spec pa_sample_specifications; | |
46 pa_sample_specifications.format = BitsToPASampleFormat( | |
47 params_.bits_per_sample()); | |
48 pa_sample_specifications.rate = params_.sample_rate(); | |
49 pa_sample_specifications.channels = params_.channels(); | |
50 | |
51 // Get channel mapping and open recording stream. | |
52 pa_channel_map source_channel_map = ChannelLayoutToPAChannelMap( | |
53 params_.channel_layout()); | |
54 pa_channel_map* map = (source_channel_map.channels != 0)? | |
55 &source_channel_map : NULL; | |
56 | |
57 // Create a new recording stream. | |
58 handle_ = pa_stream_new(pa_context_, "RecordStream", | |
59 &pa_sample_specifications, map); | |
60 if (!handle_) { | |
61 DLOG(ERROR) << "Open: failed to create PA stream"; | |
62 return false; | |
63 } | |
64 | |
65 pa_stream_set_state_callback(handle_, &StreamNotifyCallback, this); | |
66 pa_stream_set_read_callback(handle_, &ReadCallback, this); | |
67 pa_stream_readable_size(handle_); | |
68 | |
69 // Set server-side capture buffer metrics. Detailed documentation on what | |
70 // values should be chosen can be found at | |
71 // freedesktop.org/software/pulseaudio/doxygen/structpa__buffer__attr.html. | |
72 pa_buffer_attr buffer_attributes; | |
73 const unsigned int buffer_size = params_.GetBytesPerBuffer(); | |
74 buffer_attributes.maxlength = static_cast<uint32_t>(-1); | |
75 buffer_attributes.tlength = buffer_size; | |
76 buffer_attributes.minreq = buffer_size; | |
77 buffer_attributes.prebuf = static_cast<uint32_t>(-1); | |
78 buffer_attributes.fragsize = buffer_size; | |
79 int flags = PA_STREAM_AUTO_TIMING_UPDATE | | |
80 PA_STREAM_INTERPOLATE_TIMING | | |
81 PA_STREAM_ADJUST_LATENCY | | |
82 PA_STREAM_START_CORKED; | |
83 int err = pa_stream_connect_record( | |
84 handle_, | |
85 device_name_ == AudioManagerBase::kDefaultDeviceId ? | |
86 NULL : device_name_.c_str(), | |
87 &buffer_attributes, | |
88 static_cast<pa_stream_flags_t>(flags)); | |
89 if (err) { | |
90 DLOG(ERROR) << "pa_stream_connect_playback FAILED " << err; | |
91 return false; | |
92 } | |
93 | |
94 // Wait for the stream to be ready. | |
95 while (true) { | |
96 pa_stream_state_t stream_state = pa_stream_get_state(handle_); | |
97 if(!PA_STREAM_IS_GOOD(stream_state)) { | |
98 DLOG(ERROR) << "Invalid PulseAudio stream state"; | |
99 return false; | |
100 } | |
101 | |
102 if (stream_state == PA_STREAM_READY) | |
103 break; | |
104 pa_threaded_mainloop_wait(pa_mainloop_); | |
105 } | |
106 | |
107 pa_stream_set_read_callback(handle_, &ReadCallback, this); | |
108 pa_stream_readable_size(handle_); | |
109 | |
110 buffer_.reset(new media::SeekableBuffer(0, 2 * params_.GetBytesPerBuffer())); | |
111 audio_data_buffer_.reset(new uint8[params_.GetBytesPerBuffer()]); | |
112 return true; | |
113 } | |
114 | |
115 void PulseAudioInputStream::Start(AudioInputCallback* callback) { | |
116 DCHECK(audio_manager_->GetMessageLoop()->BelongsToCurrentThread()); | |
117 DCHECK(callback); | |
118 DCHECK(handle_); | |
119 AutoPulseLock auto_lock(pa_mainloop_); | |
120 | |
121 if (stream_started_) | |
122 return; | |
123 | |
124 // Clean up the old buffer. | |
125 pa_stream_drop(handle_); | |
126 | |
127 // Start the streaming. | |
128 stream_started_ = true; | |
129 callback_ = callback; | |
130 | |
131 pa_operation* operation = pa_stream_cork(handle_, 0, NULL, NULL); | |
DaleCurtis
2013/01/30 02:54:30
Do you need to wait for this? I do in PulseOutput.
no longer working on chromium
2013/02/12 17:35:59
I think both work, but make more sense to wait her
| |
132 if (!operation) { | |
133 DLOG(ERROR) << "Failed to start the recording stream"; | |
134 return; | |
135 } | |
136 pa_operation_unref(operation); | |
137 } | |
138 | |
139 void PulseAudioInputStream::Stop() { | |
140 DCHECK(audio_manager_->GetMessageLoop()->BelongsToCurrentThread()); | |
141 AutoPulseLock auto_lock(pa_mainloop_); | |
142 if (!stream_started_) | |
143 return; | |
144 | |
145 // Set the flag to false to stop filling new data to soundcard. | |
146 stream_started_ = false; | |
147 | |
DaleCurtis
2013/01/30 02:54:30
Need to flush?
no longer working on chromium
2013/02/12 17:35:59
Done.
| |
148 // Stop the stream. | |
149 pa_stream_set_read_callback(handle_, NULL, NULL); | |
150 pa_operation* operation = pa_stream_cork(handle_, 1, &StreamSuccessCallback, | |
151 pa_mainloop_); | |
152 if (!operation) { | |
153 DLOG(ERROR) << "PulseAudioInputStream: failed to stop the recording"; | |
154 return; | |
155 } | |
156 | |
157 WaitForOperationCompletion(pa_mainloop_, operation); | |
158 } | |
159 | |
160 void PulseAudioInputStream::Close() { | |
161 DCHECK(audio_manager_->GetMessageLoop()->BelongsToCurrentThread()); | |
162 { | |
163 AutoPulseLock auto_lock(pa_mainloop_); | |
164 if (handle_) { | |
165 // Disable all the callbacks before disconnecting. | |
166 pa_stream_set_state_callback(handle_, NULL, NULL); | |
167 pa_stream_flush(handle_, NULL, NULL); | |
168 | |
169 if (pa_stream_get_state(handle_) != PA_STREAM_UNCONNECTED) | |
170 pa_stream_disconnect(handle_); | |
171 | |
172 // Release PulseAudio structures. | |
173 pa_stream_unref(handle_); | |
174 handle_ = NULL; | |
175 } | |
176 } | |
177 | |
178 if (callback_) | |
179 callback_->OnClose(this); | |
180 | |
181 // Signal to the manager that we're closed and can be removed. | |
182 // This should be the last call in the function as it deletes "this". | |
183 audio_manager_->ReleaseInputStream(this); | |
184 } | |
185 | |
186 double PulseAudioInputStream::GetMaxVolume() { | |
187 return static_cast<double>(PA_VOLUME_NORM); | |
188 } | |
189 | |
190 void PulseAudioInputStream::SetVolume(double volume) { | |
DaleCurtis
2013/01/30 02:54:30
Slightly off topic, but does WebRTC or anything ac
no longer working on chromium
2013/02/12 17:35:59
Yes, WebRtc analog AGC needs these analog volume c
| |
191 AutoPulseLock auto_lock(pa_mainloop_); | |
192 if (!handle_) | |
193 return; | |
194 | |
195 size_t index = pa_stream_get_device_index(handle_); | |
196 pa_operation* operation = NULL; | |
197 if (!channels_) { | |
198 // Get the number of channels for the source only when the |channels_| is 0. | |
199 // We are assuming the stream source is not changed on the fly here. | |
200 operation = pa_context_get_source_info_by_index( | |
201 pa_context_, index, &VolumeCallback, this); | |
202 WaitForOperationCompletion(pa_mainloop_, operation); | |
203 if (!channels_) { | |
204 DLOG(WARNING) << "Failed to get the number of channels for the source"; | |
205 return; | |
206 } | |
207 } | |
208 | |
209 pa_cvolume pa_volume; | |
210 pa_cvolume_set(&pa_volume, channels_, volume); | |
DaleCurtis
2013/01/30 02:54:30
Weird that you need the channels_ here. Do you kno
no longer working on chromium
2013/02/12 17:35:59
The API description looks like this:
Set the volum
| |
211 operation = pa_context_set_source_volume_by_index( | |
212 pa_context_, index, &pa_volume, NULL, NULL); | |
213 | |
214 // Don't need to wait for this task to complete. | |
215 pa_operation_unref(operation); | |
216 } | |
217 | |
218 double PulseAudioInputStream::GetVolume() { | |
219 AutoPulseLock auto_lock(pa_mainloop_); | |
220 if (!handle_) | |
221 return 0.0; | |
222 | |
223 size_t index = pa_stream_get_device_index(handle_); | |
224 pa_operation* operation = pa_context_get_source_info_by_index( | |
225 pa_context_, index, &VolumeCallback, this); | |
226 WaitForOperationCompletion(pa_mainloop_, operation); | |
227 | |
228 return volume_; | |
229 } | |
230 | |
231 // static, used by pa_stream_set_read_callback. | |
232 void PulseAudioInputStream::ReadCallback(pa_stream* handle, | |
233 size_t length, | |
234 void* user_data) { | |
235 PulseAudioInputStream* stream = | |
236 reinterpret_cast<PulseAudioInputStream*>(user_data); | |
237 | |
238 stream->ReadData(); | |
239 } | |
240 | |
241 // static, used by pa_context_get_source_info_by_index. | |
242 void PulseAudioInputStream::VolumeCallback(pa_context* context, | |
243 const pa_source_info* info, | |
244 int error, void* user_data) { | |
245 PulseAudioInputStream* stream = | |
246 reinterpret_cast<PulseAudioInputStream*>(user_data); | |
247 | |
248 if (error) { | |
249 pa_threaded_mainloop_signal(stream->pa_mainloop_, 0); | |
250 return; | |
251 } | |
252 | |
253 if (stream->channels_ != info->channel_map.channels) | |
254 stream->channels_ = info->channel_map.channels; | |
255 | |
256 pa_volume_t volume = PA_VOLUME_MUTED; // Minimum possible value. | |
257 // Use the max volume of any channel as the volume. | |
258 for (int i = 0; i < stream->channels_; ++i) { | |
259 if (volume < info->volume.values[i]) | |
260 volume = info->volume.values[i]; | |
261 } | |
262 | |
263 stream->volume_ = static_cast<double>(volume); | |
264 } | |
265 | |
266 // static, used by pa_stream_set_state_callback. | |
267 void PulseAudioInputStream::StreamNotifyCallback(pa_stream* stream, | |
268 void* user_data) { | |
269 PulseAudioInputStream* pulse_stream = | |
270 reinterpret_cast<PulseAudioInputStream*>(user_data); | |
271 if (stream && pulse_stream->callback_ && | |
272 pa_stream_get_state(stream) == PA_STREAM_FAILED) { | |
273 pulse_stream->callback_->OnError( | |
274 pulse_stream, pa_context_errno(pulse_stream->pa_context_)); | |
275 } | |
276 | |
277 pa_threaded_mainloop_signal(pulse_stream->pa_mainloop_, 0); | |
278 } | |
279 | |
280 int PulseAudioInputStream::GetHardwareLatencyInBytes() { | |
DaleCurtis
2013/01/30 02:54:30
Is this something that should be in PulseUtil?
no longer working on chromium
2013/02/12 17:35:59
Done.
| |
281 int negative = 0; | |
282 pa_usec_t latency_micros = 0; | |
283 if (pa_stream_get_latency(handle_, &latency_micros, &negative) != 0) | |
284 return 0; | |
285 | |
286 if (negative) | |
287 return 0; | |
288 | |
289 return (latency_micros * params_.sample_rate() * | |
290 params_.GetBytesPerFrame()) / base::Time::kMicrosecondsPerSecond; | |
291 } | |
292 | |
293 void PulseAudioInputStream::ReadData() { | |
294 uint32 hardware_delay = GetHardwareLatencyInBytes(); | |
295 | |
296 // Update the AGC volume level once every second. Note that, | |
297 // |volume| is also updated each time SetVolume() is called | |
298 // through IPC by the render-side AGC. | |
299 double normalized_volume = 0.0; | |
300 QueryAgcVolume(&normalized_volume); | |
301 | |
302 while (true) { | |
303 size_t length = 0; | |
304 const void* data = NULL; | |
305 pa_stream_peek(handle_, &data, &length); | |
DaleCurtis
2013/01/30 02:54:30
Does this length correlate well with the requested
no longer working on chromium
2013/02/12 17:35:59
most of the cases it is the same as the requested
| |
306 if (!data || length == 0) | |
307 break; | |
308 | |
309 buffer_->Append(reinterpret_cast<const uint8*>(data), length); | |
310 | |
311 // Checks if we still have data. | |
312 pa_stream_drop(handle_); | |
313 if (pa_stream_readable_size(handle_) <= 0) | |
314 break; | |
315 } | |
316 | |
317 int packet_size = params_.GetBytesPerBuffer(); | |
318 while (buffer_->forward_bytes() >= packet_size) { | |
DaleCurtis
2013/01/30 02:54:30
Following up on the last comment, for this to work
no longer working on chromium
2013/02/12 17:35:59
Right, if this can happen, we might need something
| |
319 buffer_->Read(audio_data_buffer_.get(), packet_size); | |
320 callback_->OnData(this, audio_data_buffer_.get(), packet_size, | |
321 hardware_delay, normalized_volume); | |
322 } | |
323 | |
324 pa_threaded_mainloop_signal(pa_mainloop_, 0); | |
325 } | |
326 | |
327 } // namespace media | |
OLD | NEW |