OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "media/audio/cras/cras_input.h" | 5 #include "media/audio/cras/cras_input.h" |
6 | 6 |
7 #include <math.h> | 7 #include <math.h> |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
152 for (size_t i = 0; i < arraysize(kChannelMap); ++i) { | 152 for (size_t i = 0; i < arraysize(kChannelMap); ++i) { |
153 layout[kChannelMap[i]] = ChannelOrder(params_.channel_layout(), | 153 layout[kChannelMap[i]] = ChannelOrder(params_.channel_layout(), |
154 static_cast<Channels>(i)); | 154 static_cast<Channels>(i)); |
155 } | 155 } |
156 if (cras_audio_format_set_channel_layout(audio_format, layout) != 0) { | 156 if (cras_audio_format_set_channel_layout(audio_format, layout) != 0) { |
157 DLOG(WARNING) << "Error setting channel layout."; | 157 DLOG(WARNING) << "Error setting channel layout."; |
158 callback->OnError(this); | 158 callback->OnError(this); |
159 return; | 159 return; |
160 } | 160 } |
161 | 161 |
162 unsigned int frames_per_packet = params_.frames_per_buffer(); | 162 // For a hotword hardware stream, only wait for a single callback. |
163 cras_stream_params* stream_params = cras_client_stream_params_create( | 163 // Replace the existing callback with the HotwordSampleReady one that should |
164 // only run once. | |
165 if (params_.effects() & AudioParameters::PlatformEffectsMask::HOTWORD) { | |
166 if (cras_client_add_hotword_callback(client_, audio_format, | |
167 CrasInputStream::HotwordSampleReady)) { | |
168 DLOG(WARNING) << "Failed to add the hotword callback."; | |
169 callback_->OnError(this); | |
170 callback_ = NULL; | |
171 } | |
172 }else { | |
173 unsigned int frames_per_packet = params_.frames_per_buffer(); | |
174 cras_stream_params* stream_params = cras_client_stream_params_create( | |
164 stream_direction_, | 175 stream_direction_, |
165 frames_per_packet, // Total latency. | 176 frames_per_packet, // Total latency. |
166 frames_per_packet, // Call back when this many ready. | 177 frames_per_packet, // Call back when this many ready. |
167 frames_per_packet, // Minimum Callback level ignored for capture streams. | 178 frames_per_packet, // Minimum Callback level ignored for capture streams. |
168 CRAS_STREAM_TYPE_DEFAULT, | 179 CRAS_STREAM_TYPE_DEFAULT, |
169 0, // Unused flags. | 180 0, // Unused flags. |
170 this, | 181 this, |
171 CrasInputStream::SamplesReady, | 182 CrasInputStream::SamplesReady, |
172 CrasInputStream::StreamError, | 183 CrasInputStream::StreamError, |
173 audio_format); | 184 audio_format); |
174 if (!stream_params) { | 185 if (!stream_params) { |
175 DLOG(WARNING) << "Error setting up stream parameters."; | 186 DLOG(WARNING) << "Error setting up stream parameters."; |
176 callback_->OnError(this); | 187 callback_->OnError(this); |
177 callback_ = NULL; | 188 callback_ = NULL; |
178 cras_audio_format_destroy(audio_format); | 189 cras_audio_format_destroy(audio_format); |
179 return; | 190 return; |
180 } | 191 } |
181 | 192 |
182 // Before starting the stream, save the number of bytes in a frame for use in | 193 // Before starting the stream, save the number of bytes in a frame for use |
183 // the callback. | 194 // in the callback. |
184 bytes_per_frame_ = cras_client_format_bytes_per_frame(audio_format); | 195 bytes_per_frame_ = cras_client_format_bytes_per_frame(audio_format); |
185 | 196 |
186 // Adding the stream will start the audio callbacks. | 197 // Adding the stream will start the audio callbacks. |
187 if (cras_client_add_stream(client_, &stream_id_, stream_params)) { | 198 if (cras_client_add_stream(client_, &stream_id_, stream_params)) { |
188 DLOG(WARNING) << "Failed to add the stream."; | 199 DLOG(WARNING) << "Failed to add the stream."; |
189 callback_->OnError(this); | 200 callback_->OnError(this); |
190 callback_ = NULL; | 201 callback_ = NULL; |
202 } | |
191 } | 203 } |
192 | 204 |
193 // Done with config params. | 205 // Done with config params. |
194 cras_audio_format_destroy(audio_format); | 206 cras_audio_format_destroy(audio_format); |
195 cras_client_stream_params_destroy(stream_params); | 207 cras_client_stream_params_destroy(stream_params); |
dgreid
2014/09/17 22:03:00
move stream_params_destory into the else as well.
rpetterson
2014/09/17 22:10:46
Done.
| |
196 | 208 |
197 started_ = true; | 209 started_ = true; |
198 } | 210 } |
199 | 211 |
200 void CrasInputStream::Stop() { | 212 void CrasInputStream::Stop() { |
201 DCHECK(client_); | 213 DCHECK(client_); |
202 | 214 |
203 if (!callback_ || !started_) | 215 if (!callback_ || !started_) |
204 return; | 216 return; |
205 | 217 |
(...skipping 11 matching lines...) Expand all Loading... | |
217 cras_stream_id_t stream_id, | 229 cras_stream_id_t stream_id, |
218 uint8* samples, | 230 uint8* samples, |
219 size_t frames, | 231 size_t frames, |
220 const timespec* sample_ts, | 232 const timespec* sample_ts, |
221 void* arg) { | 233 void* arg) { |
222 CrasInputStream* me = static_cast<CrasInputStream*>(arg); | 234 CrasInputStream* me = static_cast<CrasInputStream*>(arg); |
223 me->ReadAudio(frames, samples, sample_ts); | 235 me->ReadAudio(frames, samples, sample_ts); |
224 return frames; | 236 return frames; |
225 } | 237 } |
226 | 238 |
239 // Static callback asking for a single sample. Run on high priority thread. | |
240 int CrasInputStream::HotwordSampleReady(cras_client* client, | |
241 cras_audio_format* fmt, | |
242 uint8* hotword_phrase, | |
243 size_t frames, | |
244 const timespec* sample_ts, | |
245 void* arg) { | |
246 CrasInputStream* me = static_cast<CrasInputStream*>(arg); | |
247 me->ReadAudio(frames, single_sample, sample_ts); | |
248 return frames; | |
249 } | |
250 | |
227 // Static callback for stream errors. | 251 // Static callback for stream errors. |
228 int CrasInputStream::StreamError(cras_client* client, | 252 int CrasInputStream::StreamError(cras_client* client, |
229 cras_stream_id_t stream_id, | 253 cras_stream_id_t stream_id, |
230 int err, | 254 int err, |
231 void* arg) { | 255 void* arg) { |
232 CrasInputStream* me = static_cast<CrasInputStream*>(arg); | 256 CrasInputStream* me = static_cast<CrasInputStream*>(arg); |
233 me->NotifyStreamError(err); | 257 me->NotifyStreamError(err); |
234 return 0; | 258 return 0; |
235 } | 259 } |
236 | 260 |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
303 | 327 |
304 double CrasInputStream::GetVolumeRatioFromDecibels(double dB) const { | 328 double CrasInputStream::GetVolumeRatioFromDecibels(double dB) const { |
305 return pow(10, dB / 20.0); | 329 return pow(10, dB / 20.0); |
306 } | 330 } |
307 | 331 |
308 double CrasInputStream::GetDecibelsFromVolumeRatio(double volume_ratio) const { | 332 double CrasInputStream::GetDecibelsFromVolumeRatio(double volume_ratio) const { |
309 return 20 * log10(volume_ratio); | 333 return 20 * log10(volume_ratio); |
310 } | 334 } |
311 | 335 |
312 } // namespace media | 336 } // namespace media |
OLD | NEW |