|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the Qt Mobility Components. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 // |
|
43 // W A R N I N G |
|
44 // ------------- |
|
45 // |
|
46 // This file is not part of the Qt API. It exists for the convenience |
|
47 // of other Qt classes. This header file may change from version to |
|
48 // version without notice, or even be removed. |
|
49 // |
|
50 // We mean it. |
|
51 // |
|
52 |
|
53 #include <QtCore/qcoreapplication.h> |
|
54 #include "qaudiooutput_alsa_p.h" |
|
55 #include "qaudiodeviceinfo_alsa_p.h" |
|
56 |
|
57 QT_BEGIN_NAMESPACE |
|
58 |
|
59 //#define DEBUG_AUDIO 1 |
|
60 |
|
61 QAudioOutputPrivate::QAudioOutputPrivate(const QByteArray &device) |
|
62 { |
|
63 bytesAvailable = 0; |
|
64 handle = 0; |
|
65 ahandler = 0; |
|
66 access = SND_PCM_ACCESS_RW_INTERLEAVED; |
|
67 pcmformat = SND_PCM_FORMAT_S16; |
|
68 buffer_frames = 0; |
|
69 period_frames = 0; |
|
70 buffer_size = 0; |
|
71 period_size = 0; |
|
72 buffer_time = 100000; |
|
73 period_time = 20000; |
|
74 totalTimeValue = 0; |
|
75 intervalTime = 1000; |
|
76 audioBuffer = 0; |
|
77 errorState = QAudio::NoError; |
|
78 deviceState = QAudio::StoppedState; |
|
79 audioSource = 0; |
|
80 pullMode = true; |
|
81 resuming = false; |
|
82 opened = false; |
|
83 |
|
84 m_device = device; |
|
85 |
|
86 timer = new QTimer(this); |
|
87 connect(timer,SIGNAL(timeout()),SLOT(userFeed())); |
|
88 } |
|
89 |
|
90 QAudioOutputPrivate::~QAudioOutputPrivate() |
|
91 { |
|
92 close(); |
|
93 disconnect(timer, SIGNAL(timeout())); |
|
94 QCoreApplication::processEvents(); |
|
95 delete timer; |
|
96 } |
|
97 |
|
98 QAudio::Error QAudioOutputPrivate::error() const |
|
99 { |
|
100 return errorState; |
|
101 } |
|
102 |
|
103 QAudio::State QAudioOutputPrivate::state() const |
|
104 { |
|
105 return deviceState; |
|
106 } |
|
107 |
|
108 void QAudioOutputPrivate::async_callback(snd_async_handler_t *ahandler) |
|
109 { |
|
110 QAudioOutputPrivate* audioOut; |
|
111 |
|
112 audioOut = static_cast<QAudioOutputPrivate*> |
|
113 (snd_async_handler_get_callback_private(ahandler)); |
|
114 |
|
115 if((audioOut->deviceState==QAudio::ActiveState)||(audioOut->resuming)) |
|
116 audioOut->feedback(); |
|
117 } |
|
118 |
|
119 int QAudioOutputPrivate::xrun_recovery(int err) |
|
120 { |
|
121 int count = 0; |
|
122 bool reset = false; |
|
123 |
|
124 if(err == -EPIPE) { |
|
125 errorState = QAudio::UnderrunError; |
|
126 emit errorChanged(errorState); |
|
127 err = snd_pcm_prepare(handle); |
|
128 if(err < 0) |
|
129 reset = true; |
|
130 |
|
131 } else if((err == -ESTRPIPE)||(err == -EIO)) { |
|
132 errorState = QAudio::IOError; |
|
133 emit errorChanged(errorState); |
|
134 while((err = snd_pcm_resume(handle)) == -EAGAIN){ |
|
135 usleep(100); |
|
136 count++; |
|
137 if(count > 5) { |
|
138 reset = true; |
|
139 break; |
|
140 } |
|
141 } |
|
142 if(err < 0) { |
|
143 err = snd_pcm_prepare(handle); |
|
144 if(err < 0) |
|
145 reset = true; |
|
146 } |
|
147 } |
|
148 if(reset) { |
|
149 close(); |
|
150 open(); |
|
151 snd_pcm_prepare(handle); |
|
152 return 0; |
|
153 } |
|
154 return err; |
|
155 } |
|
156 |
|
157 int QAudioOutputPrivate::setFormat() |
|
158 { |
|
159 snd_pcm_format_t pcmformat = SND_PCM_FORMAT_S16; |
|
160 |
|
161 if(settings.sampleSize() == 8) { |
|
162 pcmformat = SND_PCM_FORMAT_U8; |
|
163 |
|
164 } else if(settings.sampleSize() == 16) { |
|
165 if(settings.sampleType() == QAudioFormat::SignedInt) { |
|
166 if(settings.byteOrder() == QAudioFormat::LittleEndian) |
|
167 pcmformat = SND_PCM_FORMAT_S16_LE; |
|
168 else |
|
169 pcmformat = SND_PCM_FORMAT_S16_BE; |
|
170 } else if(settings.sampleType() == QAudioFormat::UnSignedInt) { |
|
171 if(settings.byteOrder() == QAudioFormat::LittleEndian) |
|
172 pcmformat = SND_PCM_FORMAT_U16_LE; |
|
173 else |
|
174 pcmformat = SND_PCM_FORMAT_U16_BE; |
|
175 } |
|
176 } else if(settings.sampleSize() == 24) { |
|
177 if(settings.sampleType() == QAudioFormat::SignedInt) { |
|
178 if(settings.byteOrder() == QAudioFormat::LittleEndian) |
|
179 pcmformat = SND_PCM_FORMAT_S24_LE; |
|
180 else |
|
181 pcmformat = SND_PCM_FORMAT_S24_BE; |
|
182 } else if(settings.sampleType() == QAudioFormat::UnSignedInt) { |
|
183 if(settings.byteOrder() == QAudioFormat::LittleEndian) |
|
184 pcmformat = SND_PCM_FORMAT_U24_LE; |
|
185 else |
|
186 pcmformat = SND_PCM_FORMAT_U24_BE; |
|
187 } |
|
188 } else if(settings.sampleSize() == 32) { |
|
189 if(settings.sampleType() == QAudioFormat::SignedInt) { |
|
190 if(settings.byteOrder() == QAudioFormat::LittleEndian) |
|
191 pcmformat = SND_PCM_FORMAT_S32_LE; |
|
192 else |
|
193 pcmformat = SND_PCM_FORMAT_S32_BE; |
|
194 } else if(settings.sampleType() == QAudioFormat::UnSignedInt) { |
|
195 if(settings.byteOrder() == QAudioFormat::LittleEndian) |
|
196 pcmformat = SND_PCM_FORMAT_U32_LE; |
|
197 else |
|
198 pcmformat = SND_PCM_FORMAT_U32_BE; |
|
199 } else if(settings.sampleType() == QAudioFormat::Float) { |
|
200 if(settings.byteOrder() == QAudioFormat::LittleEndian) |
|
201 pcmformat = SND_PCM_FORMAT_FLOAT_LE; |
|
202 else |
|
203 pcmformat = SND_PCM_FORMAT_FLOAT_BE; |
|
204 } |
|
205 } else if(settings.sampleSize() == 64) { |
|
206 if(settings.byteOrder() == QAudioFormat::LittleEndian) |
|
207 pcmformat = SND_PCM_FORMAT_FLOAT64_LE; |
|
208 else |
|
209 pcmformat = SND_PCM_FORMAT_FLOAT64_BE; |
|
210 } |
|
211 |
|
212 return snd_pcm_hw_params_set_format( handle, hwparams, pcmformat); |
|
213 } |
|
214 |
|
215 void QAudioOutputPrivate::start(QIODevice* device) |
|
216 { |
|
217 if(deviceState != QAudio::StoppedState) |
|
218 deviceState = QAudio::StoppedState; |
|
219 |
|
220 errorState = QAudio::NoError; |
|
221 |
|
222 // Handle change of mode |
|
223 if(audioSource && !pullMode) { |
|
224 delete audioSource; |
|
225 audioSource = 0; |
|
226 } |
|
227 |
|
228 close(); |
|
229 |
|
230 pullMode = true; |
|
231 audioSource = device; |
|
232 |
|
233 deviceState = QAudio::ActiveState; |
|
234 |
|
235 open(); |
|
236 |
|
237 emit stateChanged(deviceState); |
|
238 } |
|
239 |
|
240 QIODevice* QAudioOutputPrivate::start() |
|
241 { |
|
242 if(deviceState != QAudio::StoppedState) |
|
243 deviceState = QAudio::StoppedState; |
|
244 |
|
245 errorState = QAudio::NoError; |
|
246 |
|
247 // Handle change of mode |
|
248 if(audioSource && !pullMode) { |
|
249 delete audioSource; |
|
250 audioSource = 0; |
|
251 } |
|
252 |
|
253 close(); |
|
254 |
|
255 audioSource = new OutputPrivate(this); |
|
256 audioSource->open(QIODevice::WriteOnly|QIODevice::Unbuffered); |
|
257 pullMode = false; |
|
258 |
|
259 deviceState = QAudio::IdleState; |
|
260 |
|
261 open(); |
|
262 |
|
263 emit stateChanged(deviceState); |
|
264 |
|
265 return audioSource; |
|
266 } |
|
267 |
|
268 void QAudioOutputPrivate::stop() |
|
269 { |
|
270 if(deviceState == QAudio::StoppedState) |
|
271 return; |
|
272 errorState = QAudio::NoError; |
|
273 deviceState = QAudio::StoppedState; |
|
274 close(); |
|
275 emit stateChanged(deviceState); |
|
276 } |
|
277 |
|
278 bool QAudioOutputPrivate::open() |
|
279 { |
|
280 if(opened) |
|
281 return true; |
|
282 |
|
283 #ifdef DEBUG_AUDIO |
|
284 QTime now(QTime::currentTime()); |
|
285 qDebug()<<now.second()<<"s "<<now.msec()<<"ms :open()"; |
|
286 #endif |
|
287 timeStamp.restart(); |
|
288 elapsedTimeOffset = 0; |
|
289 |
|
290 int dir; |
|
291 int err=-1; |
|
292 int count=0; |
|
293 unsigned int freakuency=settings.frequency(); |
|
294 |
|
295 QString dev = QString(QLatin1String(m_device.constData())); |
|
296 QList<QByteArray> devices = QAudioDeviceInfoInternal::availableDevices(QAudio::AudioOutput); |
|
297 if(dev.compare(QLatin1String("default")) == 0) { |
|
298 #if(SND_LIB_MAJOR == 1 && SND_LIB_MINOR == 0 && SND_LIB_SUBMINOR >= 14) |
|
299 if (devices.size() > 0) |
|
300 dev = QLatin1String(devices.first()); |
|
301 else |
|
302 return false; |
|
303 #else |
|
304 dev = QLatin1String("hw:0,0"); |
|
305 #endif |
|
306 } else { |
|
307 #if(SND_LIB_MAJOR == 1 && SND_LIB_MINOR == 0 && SND_LIB_SUBMINOR >= 14) |
|
308 dev = QLatin1String(m_device); |
|
309 #else |
|
310 int idx = 0; |
|
311 char *name; |
|
312 |
|
313 QString shortName = QLatin1String(m_device.mid(m_device.indexOf('=',0)+1).constData()); |
|
314 |
|
315 while(snd_card_get_name(idx,&name) == 0) { |
|
316 if(qstrncmp(shortName.toLocal8Bit().constData(),name,shortName.length()) == 0) |
|
317 break; |
|
318 idx++; |
|
319 } |
|
320 dev = QString(QLatin1String("hw:%1,0")).arg(idx); |
|
321 #endif |
|
322 } |
|
323 |
|
324 // Step 1: try and open the device |
|
325 while((count < 5) && (err < 0)) { |
|
326 err=snd_pcm_open(&handle,dev.toLocal8Bit().constData(),SND_PCM_STREAM_PLAYBACK,0); |
|
327 if(err < 0) |
|
328 count++; |
|
329 } |
|
330 if (( err < 0)||(handle == 0)) { |
|
331 errorState = QAudio::OpenError; |
|
332 emit errorChanged(errorState); |
|
333 deviceState = QAudio::StoppedState; |
|
334 return false; |
|
335 } |
|
336 snd_pcm_nonblock( handle, 0 ); |
|
337 |
|
338 // Step 2: Set the desired HW parameters. |
|
339 snd_pcm_hw_params_alloca( &hwparams ); |
|
340 |
|
341 bool fatal = false; |
|
342 QString errMessage; |
|
343 unsigned int chunks = 8; |
|
344 |
|
345 err = snd_pcm_hw_params_any( handle, hwparams ); |
|
346 if ( err < 0 ) { |
|
347 fatal = true; |
|
348 errMessage = QString::fromLatin1("QAudioOutput: snd_pcm_hw_params_any: err = %1").arg(err); |
|
349 } |
|
350 if ( !fatal ) { |
|
351 err = snd_pcm_hw_params_set_rate_resample( handle, hwparams, 1 ); |
|
352 if ( err < 0 ) { |
|
353 fatal = true; |
|
354 errMessage = QString::fromLatin1("QAudioOutput: snd_pcm_hw_params_set_rate_resample: err = %1").arg(err); |
|
355 } |
|
356 } |
|
357 if ( !fatal ) { |
|
358 err = snd_pcm_hw_params_set_access( handle, hwparams, access ); |
|
359 if ( err < 0 ) { |
|
360 fatal = true; |
|
361 errMessage = QString::fromLatin1("QAudioOutput: snd_pcm_hw_params_set_access: err = %1").arg(err); |
|
362 } |
|
363 } |
|
364 if ( !fatal ) { |
|
365 err = setFormat(); |
|
366 if ( err < 0 ) { |
|
367 fatal = true; |
|
368 errMessage = QString::fromLatin1("QAudioOutput: snd_pcm_hw_params_set_format: err = %1").arg(err); |
|
369 } |
|
370 } |
|
371 if ( !fatal ) { |
|
372 err = snd_pcm_hw_params_set_channels( handle, hwparams, (unsigned int)settings.channels() ); |
|
373 if ( err < 0 ) { |
|
374 fatal = true; |
|
375 errMessage = QString::fromLatin1("QAudioOutput: snd_pcm_hw_params_set_channels: err = %1").arg(err); |
|
376 } |
|
377 } |
|
378 if ( !fatal ) { |
|
379 err = snd_pcm_hw_params_set_rate_near( handle, hwparams, &freakuency, 0 ); |
|
380 if ( err < 0 ) { |
|
381 fatal = true; |
|
382 errMessage = QString::fromLatin1("QAudioOutput: snd_pcm_hw_params_set_rate_near: err = %1").arg(err); |
|
383 } |
|
384 } |
|
385 if ( !fatal ) { |
|
386 unsigned int maxBufferTime = 0; |
|
387 unsigned int minBufferTime = 0; |
|
388 unsigned int maxPeriodTime = 0; |
|
389 unsigned int minPeriodTime = 0; |
|
390 |
|
391 err = snd_pcm_hw_params_get_buffer_time_max(hwparams, &maxBufferTime, &dir); |
|
392 if ( err >= 0) |
|
393 err = snd_pcm_hw_params_get_buffer_time_min(hwparams, &minBufferTime, &dir); |
|
394 if ( err >= 0) |
|
395 err = snd_pcm_hw_params_get_period_time_max(hwparams, &maxPeriodTime, &dir); |
|
396 if ( err >= 0) |
|
397 err = snd_pcm_hw_params_get_period_time_min(hwparams, &minPeriodTime, &dir); |
|
398 |
|
399 if ( err < 0 ) { |
|
400 fatal = true; |
|
401 errMessage = QString::fromLatin1("QAudioOutput: buffer/period min and max: err = %1").arg(err); |
|
402 } else { |
|
403 if (maxBufferTime < buffer_time || buffer_time < minBufferTime || maxPeriodTime < period_time || minPeriodTime > period_time) { |
|
404 #ifdef DEBUG_AUDIO |
|
405 qDebug()<<"defaults out of range"; |
|
406 qDebug()<<"pmin="<<minPeriodTime<<", pmax="<<maxPeriodTime<<", bmin="<<minBufferTime<<", bmax="<<maxBufferTime; |
|
407 #endif |
|
408 period_time = minPeriodTime; |
|
409 if (period_time*4 <= maxBufferTime) { |
|
410 // Use 4 periods if possible |
|
411 buffer_time = period_time*4; |
|
412 chunks = 4; |
|
413 } else if (period_time*2 <= maxBufferTime) { |
|
414 // Use 2 periods if possible |
|
415 buffer_time = period_time*2; |
|
416 chunks = 2; |
|
417 } else { |
|
418 qWarning()<<"QAudioOutput: alsa only supports single period!"; |
|
419 fatal = true; |
|
420 } |
|
421 #ifdef DEBUG_AUDIO |
|
422 qDebug()<<"used: buffer_time="<<buffer_time<<", period_time="<<period_time; |
|
423 #endif |
|
424 } |
|
425 } |
|
426 } |
|
427 if ( !fatal ) { |
|
428 err = snd_pcm_hw_params_set_buffer_time_near(handle, hwparams, &buffer_time, &dir); |
|
429 if ( err < 0 ) { |
|
430 fatal = true; |
|
431 errMessage = QString::fromLatin1("QAudioOutput: snd_pcm_hw_params_set_buffer_time_near: err = %1").arg(err); |
|
432 } |
|
433 } |
|
434 if ( !fatal ) { |
|
435 err = snd_pcm_hw_params_set_period_time_near(handle, hwparams, &period_time, &dir); |
|
436 if ( err < 0 ) { |
|
437 fatal = true; |
|
438 errMessage = QString::fromLatin1("QAudioOutput: snd_pcm_hw_params_set_period_time_near: err = %1").arg(err); |
|
439 } |
|
440 } |
|
441 if ( !fatal ) { |
|
442 err = snd_pcm_hw_params_set_periods_near(handle, hwparams, &chunks, &dir); |
|
443 if ( err < 0 ) { |
|
444 fatal = true; |
|
445 errMessage = QString::fromLatin1("QAudioOutput: snd_pcm_hw_params_set_periods_near: err = %1").arg(err); |
|
446 } |
|
447 } |
|
448 if ( !fatal ) { |
|
449 err = snd_pcm_hw_params(handle, hwparams); |
|
450 if ( err < 0 ) { |
|
451 fatal = true; |
|
452 errMessage = QString::fromLatin1("QAudioOutput: snd_pcm_hw_params: err = %1").arg(err); |
|
453 } |
|
454 } |
|
455 if( err < 0) { |
|
456 qWarning()<<errMessage; |
|
457 errorState = QAudio::OpenError; |
|
458 emit errorChanged(errorState); |
|
459 deviceState = QAudio::StoppedState; |
|
460 return false; |
|
461 } |
|
462 snd_pcm_hw_params_get_buffer_size(hwparams,&buffer_frames); |
|
463 buffer_size = snd_pcm_frames_to_bytes(handle,buffer_frames); |
|
464 snd_pcm_hw_params_get_period_size(hwparams,&period_frames, &dir); |
|
465 period_size = snd_pcm_frames_to_bytes(handle,period_frames); |
|
466 snd_pcm_hw_params_get_buffer_time(hwparams,&buffer_time, &dir); |
|
467 snd_pcm_hw_params_get_period_time(hwparams,&period_time, &dir); |
|
468 |
|
469 // Step 3: Set the desired SW parameters. |
|
470 snd_pcm_sw_params_t *swparams; |
|
471 snd_pcm_sw_params_alloca(&swparams); |
|
472 snd_pcm_sw_params_current(handle, swparams); |
|
473 snd_pcm_sw_params_set_start_threshold(handle,swparams,period_frames); |
|
474 snd_pcm_sw_params_set_stop_threshold(handle,swparams,buffer_frames); |
|
475 snd_pcm_sw_params_set_avail_min(handle, swparams,period_frames); |
|
476 snd_pcm_sw_params(handle, swparams); |
|
477 |
|
478 // Step 4: Prepare audio |
|
479 if(audioBuffer == 0) |
|
480 audioBuffer = new char[snd_pcm_frames_to_bytes(handle,buffer_frames)]; |
|
481 snd_pcm_prepare( handle ); |
|
482 snd_pcm_start(handle); |
|
483 |
|
484 // Step 5: Setup callback and timer fallback |
|
485 snd_async_add_pcm_handler(&ahandler, handle, async_callback, this); |
|
486 bytesAvailable = bytesFree(); |
|
487 |
|
488 // Step 6: Start audio processing |
|
489 timer->start(period_time/1000); |
|
490 |
|
491 clockStamp.restart(); |
|
492 timeStamp.restart(); |
|
493 elapsedTimeOffset = 0; |
|
494 errorState = QAudio::NoError; |
|
495 totalTimeValue = 0; |
|
496 opened = true; |
|
497 |
|
498 return true; |
|
499 } |
|
500 |
|
501 void QAudioOutputPrivate::close() |
|
502 { |
|
503 timer->stop(); |
|
504 |
|
505 if ( handle ) { |
|
506 snd_pcm_drain( handle ); |
|
507 snd_pcm_close( handle ); |
|
508 handle = 0; |
|
509 delete [] audioBuffer; |
|
510 audioBuffer=0; |
|
511 } |
|
512 if(!pullMode && audioSource) { |
|
513 delete audioSource; |
|
514 audioSource = 0; |
|
515 } |
|
516 opened = false; |
|
517 } |
|
518 |
|
519 int QAudioOutputPrivate::bytesFree() const |
|
520 { |
|
521 if(resuming) |
|
522 return period_size; |
|
523 |
|
524 if(deviceState != QAudio::ActiveState && deviceState != QAudio::IdleState) |
|
525 return 0; |
|
526 int frames = snd_pcm_avail_update(handle); |
|
527 if((int)frames > (int)buffer_frames) |
|
528 frames = buffer_frames; |
|
529 |
|
530 return snd_pcm_frames_to_bytes(handle, frames); |
|
531 } |
|
532 |
|
533 qint64 QAudioOutputPrivate::write( const char *data, qint64 len ) |
|
534 { |
|
535 // Write out some audio data |
|
536 if ( !handle ) |
|
537 return 0; |
|
538 #ifdef DEBUG_AUDIO |
|
539 qDebug()<<"frames to write out = "<< |
|
540 snd_pcm_bytes_to_frames( handle, (int)len )<<" ("<<len<<") bytes"; |
|
541 #endif |
|
542 int frames, err; |
|
543 int space = bytesFree(); |
|
544 if(len < space) { |
|
545 // Just write it |
|
546 frames = snd_pcm_bytes_to_frames( handle, (int)len ); |
|
547 err = snd_pcm_writei( handle, data, frames ); |
|
548 } else { |
|
549 // Only write space worth |
|
550 frames = snd_pcm_bytes_to_frames( handle, (int)space ); |
|
551 err = snd_pcm_writei( handle, data, frames ); |
|
552 } |
|
553 if(err > 0) { |
|
554 totalTimeValue += err; |
|
555 resuming = false; |
|
556 errorState = QAudio::NoError; |
|
557 if (deviceState != QAudio::ActiveState) { |
|
558 deviceState = QAudio::ActiveState; |
|
559 emit stateChanged(deviceState); |
|
560 } |
|
561 return snd_pcm_frames_to_bytes( handle, err ); |
|
562 } else |
|
563 err = xrun_recovery(err); |
|
564 |
|
565 if(err < 0) { |
|
566 close(); |
|
567 errorState = QAudio::FatalError; |
|
568 emit errorChanged(errorState); |
|
569 deviceState = QAudio::StoppedState; |
|
570 emit stateChanged(deviceState); |
|
571 } |
|
572 return 0; |
|
573 } |
|
574 |
|
575 int QAudioOutputPrivate::periodSize() const |
|
576 { |
|
577 return period_size; |
|
578 } |
|
579 |
|
580 void QAudioOutputPrivate::setBufferSize(int value) |
|
581 { |
|
582 if(deviceState == QAudio::StoppedState) |
|
583 buffer_size = value; |
|
584 } |
|
585 |
|
586 int QAudioOutputPrivate::bufferSize() const |
|
587 { |
|
588 return buffer_size; |
|
589 } |
|
590 |
|
591 void QAudioOutputPrivate::setNotifyInterval(int ms) |
|
592 { |
|
593 intervalTime = qMax(0, ms); |
|
594 } |
|
595 |
|
596 int QAudioOutputPrivate::notifyInterval() const |
|
597 { |
|
598 return intervalTime; |
|
599 } |
|
600 |
|
601 qint64 QAudioOutputPrivate::processedUSecs() const |
|
602 { |
|
603 return qint64(1000000) * totalTimeValue / settings.frequency(); |
|
604 } |
|
605 |
|
606 void QAudioOutputPrivate::resume() |
|
607 { |
|
608 if(deviceState == QAudio::SuspendedState) { |
|
609 int err = 0; |
|
610 |
|
611 if(handle) { |
|
612 err = snd_pcm_prepare( handle ); |
|
613 if(err < 0) |
|
614 xrun_recovery(err); |
|
615 |
|
616 err = snd_pcm_start(handle); |
|
617 if(err < 0) |
|
618 xrun_recovery(err); |
|
619 |
|
620 bytesAvailable = (int)snd_pcm_frames_to_bytes(handle, buffer_frames); |
|
621 } |
|
622 resuming = true; |
|
623 |
|
624 deviceState = QAudio::ActiveState; |
|
625 |
|
626 errorState = QAudio::NoError; |
|
627 timer->start(period_time/1000); |
|
628 emit stateChanged(deviceState); |
|
629 } |
|
630 } |
|
631 |
|
632 void QAudioOutputPrivate::setFormat(const QAudioFormat& fmt) |
|
633 { |
|
634 if (deviceState == QAudio::StoppedState) |
|
635 settings = fmt; |
|
636 } |
|
637 |
|
638 QAudioFormat QAudioOutputPrivate::format() const |
|
639 { |
|
640 return settings; |
|
641 } |
|
642 |
|
643 void QAudioOutputPrivate::suspend() |
|
644 { |
|
645 if(deviceState == QAudio::ActiveState || deviceState == QAudio::IdleState || resuming) { |
|
646 timer->stop(); |
|
647 deviceState = QAudio::SuspendedState; |
|
648 errorState = QAudio::NoError; |
|
649 emit stateChanged(deviceState); |
|
650 } |
|
651 } |
|
652 |
|
653 void QAudioOutputPrivate::userFeed() |
|
654 { |
|
655 if(deviceState == QAudio::StoppedState || deviceState == QAudio::SuspendedState) |
|
656 return; |
|
657 #ifdef DEBUG_AUDIO |
|
658 QTime now(QTime::currentTime()); |
|
659 qDebug()<<now.second()<<"s "<<now.msec()<<"ms :userFeed() OUT"; |
|
660 #endif |
|
661 if(deviceState == QAudio::IdleState) |
|
662 bytesAvailable = bytesFree(); |
|
663 |
|
664 deviceReady(); |
|
665 } |
|
666 |
|
667 void QAudioOutputPrivate::feedback() |
|
668 { |
|
669 updateAvailable(); |
|
670 } |
|
671 |
|
672 |
|
673 void QAudioOutputPrivate::updateAvailable() |
|
674 { |
|
675 #ifdef DEBUG_AUDIO |
|
676 QTime now(QTime::currentTime()); |
|
677 qDebug()<<now.second()<<"s "<<now.msec()<<"ms :updateAvailable()"; |
|
678 #endif |
|
679 bytesAvailable = bytesFree(); |
|
680 } |
|
681 |
|
682 bool QAudioOutputPrivate::deviceReady() |
|
683 { |
|
684 if(pullMode) { |
|
685 int l = 0; |
|
686 int chunks = bytesAvailable/period_size; |
|
687 if(chunks==0) { |
|
688 bytesAvailable = bytesFree(); |
|
689 return false; |
|
690 } |
|
691 #ifdef DEBUG_AUDIO |
|
692 qDebug()<<"deviceReady() avail="<<bytesAvailable<<" bytes, period size="<<period_size<<" bytes"; |
|
693 qDebug()<<"deviceReady() no. of chunks that can fit ="<<chunks<<", chunks in bytes ="<<period_size*chunks; |
|
694 #endif |
|
695 int input = period_frames*chunks; |
|
696 if(input > (int)buffer_frames) |
|
697 input = buffer_frames; |
|
698 l = audioSource->read(audioBuffer,snd_pcm_frames_to_bytes(handle, input)); |
|
699 if(l > 0) { |
|
700 // Got some data to output |
|
701 if(deviceState != QAudio::ActiveState) |
|
702 return true; |
|
703 qint64 bytesWritten = write(audioBuffer,l); |
|
704 if (bytesWritten != l) |
|
705 audioSource->seek(audioSource->pos()-(l-bytesWritten)); |
|
706 bytesAvailable = bytesFree(); |
|
707 |
|
708 } else if(l == 0) { |
|
709 // Did not get any data to output |
|
710 bytesAvailable = bytesFree(); |
|
711 if(bytesAvailable > snd_pcm_frames_to_bytes(handle, buffer_frames-period_frames)) { |
|
712 // Underrun |
|
713 if (deviceState != QAudio::IdleState) { |
|
714 errorState = QAudio::UnderrunError; |
|
715 emit errorChanged(errorState); |
|
716 deviceState = QAudio::IdleState; |
|
717 emit stateChanged(deviceState); |
|
718 } |
|
719 } |
|
720 |
|
721 } else if(l < 0) { |
|
722 close(); |
|
723 deviceState = QAudio::StoppedState; |
|
724 errorState = QAudio::IOError; |
|
725 emit errorChanged(errorState); |
|
726 emit stateChanged(deviceState); |
|
727 } |
|
728 } else { |
|
729 bytesAvailable = bytesFree(); |
|
730 if(bytesAvailable > snd_pcm_frames_to_bytes(handle, buffer_frames-period_frames)) { |
|
731 // Underrun |
|
732 if (deviceState != QAudio::IdleState) { |
|
733 errorState = QAudio::UnderrunError; |
|
734 emit errorChanged(errorState); |
|
735 deviceState = QAudio::IdleState; |
|
736 emit stateChanged(deviceState); |
|
737 } |
|
738 } |
|
739 } |
|
740 |
|
741 if(deviceState != QAudio::ActiveState) |
|
742 return true; |
|
743 |
|
744 if(intervalTime && (timeStamp.elapsed() + elapsedTimeOffset) > intervalTime) { |
|
745 emit notify(); |
|
746 elapsedTimeOffset = timeStamp.elapsed() + elapsedTimeOffset - intervalTime; |
|
747 timeStamp.restart(); |
|
748 } |
|
749 return true; |
|
750 } |
|
751 |
|
752 qint64 QAudioOutputPrivate::elapsedUSecs() const |
|
753 { |
|
754 if (deviceState == QAudio::StoppedState) |
|
755 return 0; |
|
756 |
|
757 return clockStamp.elapsed()*1000; |
|
758 } |
|
759 |
|
760 void QAudioOutputPrivate::reset() |
|
761 { |
|
762 if(handle) |
|
763 snd_pcm_reset(handle); |
|
764 |
|
765 stop(); |
|
766 } |
|
767 |
|
768 OutputPrivate::OutputPrivate(QAudioOutputPrivate* audio) |
|
769 { |
|
770 audioDevice = qobject_cast<QAudioOutputPrivate*>(audio); |
|
771 } |
|
772 |
|
773 OutputPrivate::~OutputPrivate() {} |
|
774 |
|
775 qint64 OutputPrivate::readData( char* data, qint64 len) |
|
776 { |
|
777 Q_UNUSED(data) |
|
778 Q_UNUSED(len) |
|
779 |
|
780 return 0; |
|
781 } |
|
782 |
|
783 qint64 OutputPrivate::writeData(const char* data, qint64 len) |
|
784 { |
|
785 int retry = 0; |
|
786 qint64 written = 0; |
|
787 if((audioDevice->deviceState == QAudio::ActiveState) |
|
788 ||(audioDevice->deviceState == QAudio::IdleState)) { |
|
789 while(written < len) { |
|
790 int chunk = audioDevice->write(data+written,(len-written)); |
|
791 if(chunk <= 0) |
|
792 retry++; |
|
793 written+=chunk; |
|
794 if(retry > 10) |
|
795 return written; |
|
796 } |
|
797 } |
|
798 return written; |
|
799 |
|
800 } |
|
801 |
|
802 QT_END_NAMESPACE |