|
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 |
|
226 close(); |
|
227 |
|
228 pullMode = true; |
|
229 audioSource = device; |
|
230 |
|
231 deviceState = QAudio::ActiveState; |
|
232 |
|
233 open(); |
|
234 |
|
235 emit stateChanged(deviceState); |
|
236 } |
|
237 |
|
238 QIODevice* QAudioOutputPrivate::start() |
|
239 { |
|
240 if(deviceState != QAudio::StoppedState) |
|
241 deviceState = QAudio::StoppedState; |
|
242 |
|
243 errorState = QAudio::NoError; |
|
244 |
|
245 // Handle change of mode |
|
246 if(audioSource && !pullMode) |
|
247 delete audioSource; |
|
248 |
|
249 close(); |
|
250 |
|
251 audioSource = new OutputPrivate(this); |
|
252 audioSource->open(QIODevice::WriteOnly|QIODevice::Unbuffered); |
|
253 pullMode = false; |
|
254 |
|
255 deviceState = QAudio::IdleState; |
|
256 |
|
257 open(); |
|
258 |
|
259 emit stateChanged(deviceState); |
|
260 |
|
261 return audioSource; |
|
262 } |
|
263 |
|
264 void QAudioOutputPrivate::stop() |
|
265 { |
|
266 if(deviceState == QAudio::StoppedState) |
|
267 return; |
|
268 errorState = QAudio::NoError; |
|
269 deviceState = QAudio::StoppedState; |
|
270 close(); |
|
271 emit stateChanged(deviceState); |
|
272 } |
|
273 |
|
274 bool QAudioOutputPrivate::open() |
|
275 { |
|
276 if(opened) |
|
277 return true; |
|
278 |
|
279 #ifdef DEBUG_AUDIO |
|
280 QTime now(QTime::currentTime()); |
|
281 qDebug()<<now.second()<<"s "<<now.msec()<<"ms :open()"; |
|
282 #endif |
|
283 timeStamp.restart(); |
|
284 elapsedTimeOffset = 0; |
|
285 |
|
286 int dir; |
|
287 int err=-1; |
|
288 int count=0; |
|
289 unsigned int freakuency=settings.frequency(); |
|
290 |
|
291 QString dev = QString(QLatin1String(m_device.constData())); |
|
292 QList<QByteArray> devices = QAudioDeviceInfoInternal::availableDevices(QAudio::AudioOutput); |
|
293 if(dev.compare(QLatin1String("default")) == 0) { |
|
294 #if(SND_LIB_MAJOR == 1 && SND_LIB_MINOR == 0 && SND_LIB_SUBMINOR >= 14) |
|
295 dev = QLatin1String(devices.first()); |
|
296 #else |
|
297 dev = QLatin1String("hw:0,0"); |
|
298 #endif |
|
299 } else { |
|
300 #if(SND_LIB_MAJOR == 1 && SND_LIB_MINOR == 0 && SND_LIB_SUBMINOR >= 14) |
|
301 dev = QLatin1String(m_device); |
|
302 #else |
|
303 int idx = 0; |
|
304 char *name; |
|
305 |
|
306 QString shortName = QLatin1String(m_device.mid(m_device.indexOf('=',0)+1).constData()); |
|
307 |
|
308 while(snd_card_get_name(idx,&name) == 0) { |
|
309 if(qstrncmp(shortName.toLocal8Bit().constData(),name,shortName.length()) == 0) |
|
310 break; |
|
311 idx++; |
|
312 } |
|
313 dev = QString(QLatin1String("hw:%1,0")).arg(idx); |
|
314 #endif |
|
315 } |
|
316 |
|
317 // Step 1: try and open the device |
|
318 while((count < 5) && (err < 0)) { |
|
319 err=snd_pcm_open(&handle,dev.toLocal8Bit().constData(),SND_PCM_STREAM_PLAYBACK,0); |
|
320 if(err < 0) |
|
321 count++; |
|
322 } |
|
323 if (( err < 0)||(handle == 0)) { |
|
324 errorState = QAudio::OpenError; |
|
325 emit errorChanged(errorState); |
|
326 deviceState = QAudio::StoppedState; |
|
327 return false; |
|
328 } |
|
329 snd_pcm_nonblock( handle, 0 ); |
|
330 |
|
331 // Step 2: Set the desired HW parameters. |
|
332 snd_pcm_hw_params_alloca( &hwparams ); |
|
333 |
|
334 bool fatal = false; |
|
335 QString errMessage; |
|
336 unsigned int chunks = 8; |
|
337 |
|
338 err = snd_pcm_hw_params_any( handle, hwparams ); |
|
339 if ( err < 0 ) { |
|
340 fatal = true; |
|
341 errMessage = QString::fromLatin1("QAudioOutput: snd_pcm_hw_params_any: err = %1").arg(err); |
|
342 } |
|
343 if ( !fatal ) { |
|
344 err = snd_pcm_hw_params_set_rate_resample( handle, hwparams, 1 ); |
|
345 if ( err < 0 ) { |
|
346 fatal = true; |
|
347 errMessage = QString::fromLatin1("QAudioOutput: snd_pcm_hw_params_set_rate_resample: err = %1").arg(err); |
|
348 } |
|
349 } |
|
350 if ( !fatal ) { |
|
351 err = snd_pcm_hw_params_set_access( handle, hwparams, access ); |
|
352 if ( err < 0 ) { |
|
353 fatal = true; |
|
354 errMessage = QString::fromLatin1("QAudioOutput: snd_pcm_hw_params_set_access: err = %1").arg(err); |
|
355 } |
|
356 } |
|
357 if ( !fatal ) { |
|
358 err = setFormat(); |
|
359 if ( err < 0 ) { |
|
360 fatal = true; |
|
361 errMessage = QString::fromLatin1("QAudioOutput: snd_pcm_hw_params_set_format: err = %1").arg(err); |
|
362 } |
|
363 } |
|
364 if ( !fatal ) { |
|
365 err = snd_pcm_hw_params_set_channels( handle, hwparams, (unsigned int)settings.channels() ); |
|
366 if ( err < 0 ) { |
|
367 fatal = true; |
|
368 errMessage = QString::fromLatin1("QAudioOutput: snd_pcm_hw_params_set_channels: err = %1").arg(err); |
|
369 } |
|
370 } |
|
371 if ( !fatal ) { |
|
372 err = snd_pcm_hw_params_set_rate_near( handle, hwparams, &freakuency, 0 ); |
|
373 if ( err < 0 ) { |
|
374 fatal = true; |
|
375 errMessage = QString::fromLatin1("QAudioOutput: snd_pcm_hw_params_set_rate_near: err = %1").arg(err); |
|
376 } |
|
377 } |
|
378 if ( !fatal ) { |
|
379 unsigned int maxBufferTime = 0; |
|
380 unsigned int minBufferTime = 0; |
|
381 unsigned int maxPeriodTime = 0; |
|
382 unsigned int minPeriodTime = 0; |
|
383 |
|
384 err = snd_pcm_hw_params_get_buffer_time_max(hwparams, &maxBufferTime, &dir); |
|
385 if ( err >= 0) |
|
386 err = snd_pcm_hw_params_get_buffer_time_min(hwparams, &minBufferTime, &dir); |
|
387 if ( err >= 0) |
|
388 err = snd_pcm_hw_params_get_period_time_max(hwparams, &maxPeriodTime, &dir); |
|
389 if ( err >= 0) |
|
390 err = snd_pcm_hw_params_get_period_time_min(hwparams, &minPeriodTime, &dir); |
|
391 |
|
392 if ( err < 0 ) { |
|
393 fatal = true; |
|
394 errMessage = QString::fromLatin1("QAudioOutput: buffer/period min and max: err = %1").arg(err); |
|
395 } else { |
|
396 if (maxBufferTime < buffer_time || buffer_time < minBufferTime || maxPeriodTime < period_time || minPeriodTime > period_time) { |
|
397 #ifdef DEBUG_AUDIO |
|
398 qDebug()<<"defaults out of range"; |
|
399 qDebug()<<"pmin="<<minPeriodTime<<", pmax="<<maxPeriodTime<<", bmin="<<minBufferTime<<", bmax="<<maxBufferTime; |
|
400 #endif |
|
401 period_time = minPeriodTime; |
|
402 if (period_time*4 <= maxBufferTime) { |
|
403 // Use 4 periods if possible |
|
404 buffer_time = period_time*4; |
|
405 chunks = 4; |
|
406 } else if (period_time*2 <= maxBufferTime) { |
|
407 // Use 2 periods if possible |
|
408 buffer_time = period_time*2; |
|
409 chunks = 2; |
|
410 } else { |
|
411 qWarning()<<"QAudioOutput: alsa only supports single period!"; |
|
412 fatal = true; |
|
413 } |
|
414 #ifdef DEBUG_AUDIO |
|
415 qDebug()<<"used: buffer_time="<<buffer_time<<", period_time="<<period_time; |
|
416 #endif |
|
417 } |
|
418 } |
|
419 } |
|
420 if ( !fatal ) { |
|
421 err = snd_pcm_hw_params_set_buffer_time_near(handle, hwparams, &buffer_time, &dir); |
|
422 if ( err < 0 ) { |
|
423 fatal = true; |
|
424 errMessage = QString::fromLatin1("QAudioOutput: snd_pcm_hw_params_set_buffer_time_near: err = %1").arg(err); |
|
425 } |
|
426 } |
|
427 if ( !fatal ) { |
|
428 err = snd_pcm_hw_params_set_period_time_near(handle, hwparams, &period_time, &dir); |
|
429 if ( err < 0 ) { |
|
430 fatal = true; |
|
431 errMessage = QString::fromLatin1("QAudioOutput: snd_pcm_hw_params_set_period_time_near: err = %1").arg(err); |
|
432 } |
|
433 } |
|
434 if ( !fatal ) { |
|
435 err = snd_pcm_hw_params_set_periods_near(handle, hwparams, &chunks, &dir); |
|
436 if ( err < 0 ) { |
|
437 fatal = true; |
|
438 errMessage = QString::fromLatin1("QAudioOutput: snd_pcm_hw_params_set_periods_near: err = %1").arg(err); |
|
439 } |
|
440 } |
|
441 if ( !fatal ) { |
|
442 err = snd_pcm_hw_params(handle, hwparams); |
|
443 if ( err < 0 ) { |
|
444 fatal = true; |
|
445 errMessage = QString::fromLatin1("QAudioOutput: snd_pcm_hw_params: err = %1").arg(err); |
|
446 } |
|
447 } |
|
448 if( err < 0) { |
|
449 qWarning()<<errMessage; |
|
450 errorState = QAudio::OpenError; |
|
451 emit errorChanged(errorState); |
|
452 deviceState = QAudio::StoppedState; |
|
453 return false; |
|
454 } |
|
455 snd_pcm_hw_params_get_buffer_size(hwparams,&buffer_frames); |
|
456 buffer_size = snd_pcm_frames_to_bytes(handle,buffer_frames); |
|
457 snd_pcm_hw_params_get_period_size(hwparams,&period_frames, &dir); |
|
458 period_size = snd_pcm_frames_to_bytes(handle,period_frames); |
|
459 snd_pcm_hw_params_get_buffer_time(hwparams,&buffer_time, &dir); |
|
460 snd_pcm_hw_params_get_period_time(hwparams,&period_time, &dir); |
|
461 |
|
462 // Step 3: Set the desired SW parameters. |
|
463 snd_pcm_sw_params_t *swparams; |
|
464 snd_pcm_sw_params_alloca(&swparams); |
|
465 snd_pcm_sw_params_current(handle, swparams); |
|
466 snd_pcm_sw_params_set_start_threshold(handle,swparams,period_frames); |
|
467 snd_pcm_sw_params_set_stop_threshold(handle,swparams,buffer_frames); |
|
468 snd_pcm_sw_params_set_avail_min(handle, swparams,period_frames); |
|
469 snd_pcm_sw_params(handle, swparams); |
|
470 |
|
471 // Step 4: Prepare audio |
|
472 if(audioBuffer == 0) |
|
473 audioBuffer = new char[snd_pcm_frames_to_bytes(handle,buffer_frames)]; |
|
474 snd_pcm_prepare( handle ); |
|
475 snd_pcm_start(handle); |
|
476 |
|
477 // Step 5: Setup callback and timer fallback |
|
478 snd_async_add_pcm_handler(&ahandler, handle, async_callback, this); |
|
479 bytesAvailable = bytesFree(); |
|
480 |
|
481 // Step 6: Start audio processing |
|
482 timer->start(period_time/1000); |
|
483 |
|
484 clockStamp.restart(); |
|
485 timeStamp.restart(); |
|
486 elapsedTimeOffset = 0; |
|
487 errorState = QAudio::NoError; |
|
488 totalTimeValue = 0; |
|
489 opened = true; |
|
490 |
|
491 return true; |
|
492 } |
|
493 |
|
494 void QAudioOutputPrivate::close() |
|
495 { |
|
496 timer->stop(); |
|
497 |
|
498 if ( handle ) { |
|
499 snd_pcm_drain( handle ); |
|
500 snd_pcm_close( handle ); |
|
501 handle = 0; |
|
502 delete [] audioBuffer; |
|
503 audioBuffer=0; |
|
504 } |
|
505 if(!pullMode && audioSource) { |
|
506 delete audioSource; |
|
507 audioSource = 0; |
|
508 } |
|
509 opened = false; |
|
510 } |
|
511 |
|
512 int QAudioOutputPrivate::bytesFree() const |
|
513 { |
|
514 if(resuming) |
|
515 return period_size; |
|
516 |
|
517 if(deviceState != QAudio::ActiveState && deviceState != QAudio::IdleState) |
|
518 return 0; |
|
519 int frames = snd_pcm_avail_update(handle); |
|
520 if((int)frames > (int)buffer_frames) |
|
521 frames = buffer_frames; |
|
522 |
|
523 return snd_pcm_frames_to_bytes(handle, frames); |
|
524 } |
|
525 |
|
526 qint64 QAudioOutputPrivate::write( const char *data, qint64 len ) |
|
527 { |
|
528 // Write out some audio data |
|
529 if ( !handle ) |
|
530 return 0; |
|
531 #ifdef DEBUG_AUDIO |
|
532 qDebug()<<"frames to write out = "<< |
|
533 snd_pcm_bytes_to_frames( handle, (int)len )<<" ("<<len<<") bytes"; |
|
534 #endif |
|
535 int frames, err; |
|
536 int space = bytesFree(); |
|
537 if(len < space) { |
|
538 // Just write it |
|
539 frames = snd_pcm_bytes_to_frames( handle, (int)len ); |
|
540 err = snd_pcm_writei( handle, data, frames ); |
|
541 } else { |
|
542 // Only write space worth |
|
543 frames = snd_pcm_bytes_to_frames( handle, (int)space ); |
|
544 err = snd_pcm_writei( handle, data, frames ); |
|
545 } |
|
546 if(err > 0) { |
|
547 totalTimeValue += err; |
|
548 resuming = false; |
|
549 errorState = QAudio::NoError; |
|
550 if (deviceState != QAudio::ActiveState) { |
|
551 deviceState = QAudio::ActiveState; |
|
552 emit stateChanged(deviceState); |
|
553 } |
|
554 return snd_pcm_frames_to_bytes( handle, err ); |
|
555 } else |
|
556 err = xrun_recovery(err); |
|
557 |
|
558 if(err < 0) { |
|
559 close(); |
|
560 errorState = QAudio::FatalError; |
|
561 emit errorChanged(errorState); |
|
562 deviceState = QAudio::StoppedState; |
|
563 emit stateChanged(deviceState); |
|
564 } |
|
565 return 0; |
|
566 } |
|
567 |
|
568 int QAudioOutputPrivate::periodSize() const |
|
569 { |
|
570 return period_size; |
|
571 } |
|
572 |
|
573 void QAudioOutputPrivate::setBufferSize(int value) |
|
574 { |
|
575 if(deviceState == QAudio::StoppedState) |
|
576 buffer_size = value; |
|
577 } |
|
578 |
|
579 int QAudioOutputPrivate::bufferSize() const |
|
580 { |
|
581 return buffer_size; |
|
582 } |
|
583 |
|
584 void QAudioOutputPrivate::setNotifyInterval(int ms) |
|
585 { |
|
586 intervalTime = qMax(0, ms); |
|
587 } |
|
588 |
|
589 int QAudioOutputPrivate::notifyInterval() const |
|
590 { |
|
591 return intervalTime; |
|
592 } |
|
593 |
|
594 qint64 QAudioOutputPrivate::processedUSecs() const |
|
595 { |
|
596 return qint64(1000000) * totalTimeValue / settings.frequency(); |
|
597 } |
|
598 |
|
599 void QAudioOutputPrivate::resume() |
|
600 { |
|
601 if(deviceState == QAudio::SuspendedState) { |
|
602 int err = 0; |
|
603 |
|
604 if(handle) { |
|
605 err = snd_pcm_prepare( handle ); |
|
606 if(err < 0) |
|
607 xrun_recovery(err); |
|
608 |
|
609 err = snd_pcm_start(handle); |
|
610 if(err < 0) |
|
611 xrun_recovery(err); |
|
612 |
|
613 bytesAvailable = (int)snd_pcm_frames_to_bytes(handle, buffer_frames); |
|
614 } |
|
615 resuming = true; |
|
616 |
|
617 deviceState = QAudio::ActiveState; |
|
618 |
|
619 errorState = QAudio::NoError; |
|
620 timer->start(period_time/1000); |
|
621 emit stateChanged(deviceState); |
|
622 } |
|
623 } |
|
624 |
|
625 void QAudioOutputPrivate::setFormat(const QAudioFormat& fmt) |
|
626 { |
|
627 if (deviceState == QAudio::StoppedState) |
|
628 settings = fmt; |
|
629 } |
|
630 |
|
631 QAudioFormat QAudioOutputPrivate::format() const |
|
632 { |
|
633 return settings; |
|
634 } |
|
635 |
|
636 void QAudioOutputPrivate::suspend() |
|
637 { |
|
638 if(deviceState == QAudio::ActiveState || deviceState == QAudio::IdleState || resuming) { |
|
639 timer->stop(); |
|
640 deviceState = QAudio::SuspendedState; |
|
641 errorState = QAudio::NoError; |
|
642 emit stateChanged(deviceState); |
|
643 } |
|
644 } |
|
645 |
|
646 void QAudioOutputPrivate::userFeed() |
|
647 { |
|
648 if(deviceState == QAudio::StoppedState || deviceState == QAudio::SuspendedState) |
|
649 return; |
|
650 #ifdef DEBUG_AUDIO |
|
651 QTime now(QTime::currentTime()); |
|
652 qDebug()<<now.second()<<"s "<<now.msec()<<"ms :userFeed() OUT"; |
|
653 #endif |
|
654 if(deviceState == QAudio::IdleState) |
|
655 bytesAvailable = bytesFree(); |
|
656 |
|
657 deviceReady(); |
|
658 } |
|
659 |
|
660 void QAudioOutputPrivate::feedback() |
|
661 { |
|
662 updateAvailable(); |
|
663 } |
|
664 |
|
665 |
|
666 void QAudioOutputPrivate::updateAvailable() |
|
667 { |
|
668 #ifdef DEBUG_AUDIO |
|
669 QTime now(QTime::currentTime()); |
|
670 qDebug()<<now.second()<<"s "<<now.msec()<<"ms :updateAvailable()"; |
|
671 #endif |
|
672 bytesAvailable = bytesFree(); |
|
673 } |
|
674 |
|
675 bool QAudioOutputPrivate::deviceReady() |
|
676 { |
|
677 if(pullMode) { |
|
678 int l = 0; |
|
679 int chunks = bytesAvailable/period_size; |
|
680 if(chunks==0) { |
|
681 bytesAvailable = bytesFree(); |
|
682 return false; |
|
683 } |
|
684 #ifdef DEBUG_AUDIO |
|
685 qDebug()<<"deviceReady() avail="<<bytesAvailable<<" bytes, period size="<<period_size<<" bytes"; |
|
686 qDebug()<<"deviceReady() no. of chunks that can fit ="<<chunks<<", chunks in bytes ="<<period_size*chunks; |
|
687 #endif |
|
688 int input = period_frames*chunks; |
|
689 if(input > (int)buffer_frames) |
|
690 input = buffer_frames; |
|
691 l = audioSource->read(audioBuffer,snd_pcm_frames_to_bytes(handle, input)); |
|
692 if(l > 0) { |
|
693 // Got some data to output |
|
694 if(deviceState != QAudio::ActiveState) |
|
695 return true; |
|
696 qint64 bytesWritten = write(audioBuffer,l); |
|
697 if (bytesWritten != l) |
|
698 audioSource->seek(audioSource->pos()-(l-bytesWritten)); |
|
699 bytesAvailable = bytesFree(); |
|
700 |
|
701 } else if(l == 0) { |
|
702 // Did not get any data to output |
|
703 bytesAvailable = bytesFree(); |
|
704 if(bytesAvailable > snd_pcm_frames_to_bytes(handle, buffer_frames-period_frames)) { |
|
705 // Underrun |
|
706 if (deviceState != QAudio::IdleState) { |
|
707 errorState = QAudio::UnderrunError; |
|
708 emit errorChanged(errorState); |
|
709 deviceState = QAudio::IdleState; |
|
710 emit stateChanged(deviceState); |
|
711 } |
|
712 } |
|
713 |
|
714 } else if(l < 0) { |
|
715 close(); |
|
716 deviceState = QAudio::StoppedState; |
|
717 errorState = QAudio::IOError; |
|
718 emit errorChanged(errorState); |
|
719 emit stateChanged(deviceState); |
|
720 } |
|
721 } else { |
|
722 bytesAvailable = bytesFree(); |
|
723 if(bytesAvailable > snd_pcm_frames_to_bytes(handle, buffer_frames-period_frames)) { |
|
724 // Underrun |
|
725 if (deviceState != QAudio::IdleState) { |
|
726 errorState = QAudio::UnderrunError; |
|
727 emit errorChanged(errorState); |
|
728 deviceState = QAudio::IdleState; |
|
729 emit stateChanged(deviceState); |
|
730 } |
|
731 } |
|
732 } |
|
733 |
|
734 if(deviceState != QAudio::ActiveState) |
|
735 return true; |
|
736 |
|
737 if(intervalTime && (timeStamp.elapsed() + elapsedTimeOffset) > intervalTime) { |
|
738 emit notify(); |
|
739 elapsedTimeOffset = timeStamp.elapsed() + elapsedTimeOffset - intervalTime; |
|
740 timeStamp.restart(); |
|
741 } |
|
742 return true; |
|
743 } |
|
744 |
|
745 qint64 QAudioOutputPrivate::elapsedUSecs() const |
|
746 { |
|
747 if (deviceState == QAudio::StoppedState) |
|
748 return 0; |
|
749 |
|
750 return clockStamp.elapsed()*1000; |
|
751 } |
|
752 |
|
753 void QAudioOutputPrivate::reset() |
|
754 { |
|
755 if(handle) |
|
756 snd_pcm_reset(handle); |
|
757 |
|
758 stop(); |
|
759 } |
|
760 |
|
761 OutputPrivate::OutputPrivate(QAudioOutputPrivate* audio) |
|
762 { |
|
763 audioDevice = qobject_cast<QAudioOutputPrivate*>(audio); |
|
764 } |
|
765 |
|
766 OutputPrivate::~OutputPrivate() {} |
|
767 |
|
768 qint64 OutputPrivate::readData( char* data, qint64 len) |
|
769 { |
|
770 Q_UNUSED(data) |
|
771 Q_UNUSED(len) |
|
772 |
|
773 return 0; |
|
774 } |
|
775 |
|
776 qint64 OutputPrivate::writeData(const char* data, qint64 len) |
|
777 { |
|
778 int retry = 0; |
|
779 qint64 written = 0; |
|
780 if((audioDevice->deviceState == QAudio::ActiveState) |
|
781 ||(audioDevice->deviceState == QAudio::IdleState)) { |
|
782 while(written < len) { |
|
783 int chunk = audioDevice->write(data+written,(len-written)); |
|
784 if(chunk <= 0) |
|
785 retry++; |
|
786 written+=chunk; |
|
787 if(retry > 10) |
|
788 return written; |
|
789 } |
|
790 } |
|
791 return written; |
|
792 |
|
793 } |
|
794 |
|
795 QT_END_NAMESPACE |