author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Fri, 19 Feb 2010 23:40:16 +0200 | |
branch | RCL_3 |
changeset 4 | 3b1da2848fc7 |
parent 3 | 41300fa6a67c |
child 7 | 3f74d0d4af4c |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the QtMultimedia module of the Qt Toolkit. |
|
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" |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
55 |
#include "qaudiodeviceinfo_alsa_p.h" |
0 | 56 |
|
57 |
QT_BEGIN_NAMESPACE |
|
58 |
||
59 |
//#define DEBUG_AUDIO 1 |
|
60 |
||
61 |
static const int minimumIntervalTime = 50; |
|
62 |
||
63 |
QAudioOutputPrivate::QAudioOutputPrivate(const QByteArray &device, const QAudioFormat& audioFormat): |
|
64 |
settings(audioFormat) |
|
65 |
{ |
|
66 |
bytesAvailable = 0; |
|
67 |
handle = 0; |
|
68 |
ahandler = 0; |
|
69 |
access = SND_PCM_ACCESS_RW_INTERLEAVED; |
|
70 |
pcmformat = SND_PCM_FORMAT_S16; |
|
71 |
buffer_frames = 0; |
|
72 |
period_frames = 0; |
|
73 |
buffer_size = 0; |
|
74 |
period_size = 0; |
|
75 |
buffer_time = 100000; |
|
76 |
period_time = 20000; |
|
77 |
totalTimeValue = 0; |
|
78 |
intervalTime = 1000; |
|
79 |
audioBuffer = 0; |
|
80 |
errorState = QAudio::NoError; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
81 |
deviceState = QAudio::StoppedState; |
0 | 82 |
audioSource = 0; |
83 |
pullMode = true; |
|
84 |
resuming = false; |
|
85 |
opened = false; |
|
86 |
||
87 |
QStringList list1 = QString(QLatin1String(device)).split(QLatin1String(":")); |
|
88 |
m_device = QByteArray(list1.at(0).toLocal8Bit().constData()); |
|
89 |
||
90 |
timer = new QTimer(this); |
|
91 |
connect(timer,SIGNAL(timeout()),SLOT(userFeed())); |
|
92 |
} |
|
93 |
||
94 |
QAudioOutputPrivate::~QAudioOutputPrivate() |
|
95 |
{ |
|
96 |
close(); |
|
97 |
disconnect(timer, SIGNAL(timeout())); |
|
98 |
QCoreApplication::processEvents(); |
|
99 |
delete timer; |
|
100 |
} |
|
101 |
||
102 |
QAudio::Error QAudioOutputPrivate::error() const |
|
103 |
{ |
|
104 |
return errorState; |
|
105 |
} |
|
106 |
||
107 |
QAudio::State QAudioOutputPrivate::state() const |
|
108 |
{ |
|
109 |
return deviceState; |
|
110 |
} |
|
111 |
||
112 |
void QAudioOutputPrivate::async_callback(snd_async_handler_t *ahandler) |
|
113 |
{ |
|
114 |
QAudioOutputPrivate* audioOut; |
|
115 |
||
116 |
audioOut = static_cast<QAudioOutputPrivate*> |
|
117 |
(snd_async_handler_get_callback_private(ahandler)); |
|
118 |
||
119 |
if((audioOut->deviceState==QAudio::ActiveState)||(audioOut->resuming)) |
|
120 |
audioOut->feedback(); |
|
121 |
} |
|
122 |
||
123 |
int QAudioOutputPrivate::xrun_recovery(int err) |
|
124 |
{ |
|
125 |
int count = 0; |
|
126 |
bool reset = false; |
|
127 |
||
128 |
if(err == -EPIPE) { |
|
129 |
errorState = QAudio::UnderrunError; |
|
130 |
err = snd_pcm_prepare(handle); |
|
131 |
if(err < 0) |
|
132 |
reset = true; |
|
133 |
||
134 |
} else if((err == -ESTRPIPE)||(err == -EIO)) { |
|
135 |
errorState = QAudio::IOError; |
|
136 |
while((err = snd_pcm_resume(handle)) == -EAGAIN){ |
|
137 |
usleep(100); |
|
138 |
count++; |
|
139 |
if(count > 5) { |
|
140 |
reset = true; |
|
141 |
break; |
|
142 |
} |
|
143 |
} |
|
144 |
if(err < 0) { |
|
145 |
err = snd_pcm_prepare(handle); |
|
146 |
if(err < 0) |
|
147 |
reset = true; |
|
148 |
} |
|
149 |
} |
|
150 |
if(reset) { |
|
151 |
close(); |
|
152 |
open(); |
|
153 |
snd_pcm_prepare(handle); |
|
154 |
return 0; |
|
155 |
} |
|
156 |
return err; |
|
157 |
} |
|
158 |
||
159 |
int QAudioOutputPrivate::setFormat() |
|
160 |
{ |
|
161 |
snd_pcm_format_t pcmformat = SND_PCM_FORMAT_S16; |
|
162 |
||
163 |
if(settings.sampleSize() == 8) { |
|
164 |
pcmformat = SND_PCM_FORMAT_U8; |
|
165 |
||
166 |
} else if(settings.sampleSize() == 16) { |
|
167 |
if(settings.sampleType() == QAudioFormat::SignedInt) { |
|
168 |
if(settings.byteOrder() == QAudioFormat::LittleEndian) |
|
169 |
pcmformat = SND_PCM_FORMAT_S16_LE; |
|
170 |
else |
|
171 |
pcmformat = SND_PCM_FORMAT_S16_BE; |
|
172 |
} else if(settings.sampleType() == QAudioFormat::UnSignedInt) { |
|
173 |
if(settings.byteOrder() == QAudioFormat::LittleEndian) |
|
174 |
pcmformat = SND_PCM_FORMAT_U16_LE; |
|
175 |
else |
|
176 |
pcmformat = SND_PCM_FORMAT_U16_BE; |
|
177 |
} |
|
178 |
} else if(settings.sampleSize() == 24) { |
|
179 |
if(settings.sampleType() == QAudioFormat::SignedInt) { |
|
180 |
if(settings.byteOrder() == QAudioFormat::LittleEndian) |
|
181 |
pcmformat = SND_PCM_FORMAT_S24_LE; |
|
182 |
else |
|
183 |
pcmformat = SND_PCM_FORMAT_S24_BE; |
|
184 |
} else if(settings.sampleType() == QAudioFormat::UnSignedInt) { |
|
185 |
if(settings.byteOrder() == QAudioFormat::LittleEndian) |
|
186 |
pcmformat = SND_PCM_FORMAT_U24_LE; |
|
187 |
else |
|
188 |
pcmformat = SND_PCM_FORMAT_U24_BE; |
|
189 |
} |
|
190 |
} else if(settings.sampleSize() == 32) { |
|
191 |
if(settings.sampleType() == QAudioFormat::SignedInt) { |
|
192 |
if(settings.byteOrder() == QAudioFormat::LittleEndian) |
|
193 |
pcmformat = SND_PCM_FORMAT_S32_LE; |
|
194 |
else |
|
195 |
pcmformat = SND_PCM_FORMAT_S32_BE; |
|
196 |
} else if(settings.sampleType() == QAudioFormat::UnSignedInt) { |
|
197 |
if(settings.byteOrder() == QAudioFormat::LittleEndian) |
|
198 |
pcmformat = SND_PCM_FORMAT_U32_LE; |
|
199 |
else |
|
200 |
pcmformat = SND_PCM_FORMAT_U32_BE; |
|
201 |
} else if(settings.sampleType() == QAudioFormat::Float) { |
|
202 |
if(settings.byteOrder() == QAudioFormat::LittleEndian) |
|
203 |
pcmformat = SND_PCM_FORMAT_FLOAT_LE; |
|
204 |
else |
|
205 |
pcmformat = SND_PCM_FORMAT_FLOAT_BE; |
|
206 |
} |
|
207 |
} else if(settings.sampleSize() == 64) { |
|
208 |
if(settings.byteOrder() == QAudioFormat::LittleEndian) |
|
209 |
pcmformat = SND_PCM_FORMAT_FLOAT64_LE; |
|
210 |
else |
|
211 |
pcmformat = SND_PCM_FORMAT_FLOAT64_BE; |
|
212 |
} |
|
213 |
||
214 |
return snd_pcm_hw_params_set_format( handle, hwparams, pcmformat); |
|
215 |
} |
|
216 |
||
217 |
QIODevice* QAudioOutputPrivate::start(QIODevice* device) |
|
218 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
219 |
if(deviceState != QAudio::StoppedState) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
220 |
deviceState = QAudio::StoppedState; |
0 | 221 |
|
222 |
errorState = QAudio::NoError; |
|
223 |
||
224 |
// Handle change of mode |
|
225 |
if(audioSource && pullMode && !device) { |
|
226 |
// pull -> push |
|
227 |
close(); |
|
228 |
audioSource = 0; |
|
229 |
} else if(audioSource && !pullMode && device) { |
|
230 |
// push -> pull |
|
231 |
close(); |
|
232 |
delete audioSource; |
|
233 |
audioSource = 0; |
|
234 |
} |
|
235 |
||
236 |
if(device) { |
|
237 |
//set to pull mode |
|
238 |
pullMode = true; |
|
239 |
audioSource = device; |
|
240 |
deviceState = QAudio::ActiveState; |
|
241 |
} else { |
|
242 |
//set to push mode |
|
243 |
if(!audioSource) { |
|
244 |
audioSource = new OutputPrivate(this); |
|
245 |
audioSource->open(QIODevice::WriteOnly|QIODevice::Unbuffered); |
|
246 |
} |
|
247 |
pullMode = false; |
|
248 |
deviceState = QAudio::IdleState; |
|
249 |
} |
|
250 |
||
251 |
open(); |
|
252 |
||
253 |
emit stateChanged(deviceState); |
|
254 |
||
255 |
return audioSource; |
|
256 |
} |
|
257 |
||
258 |
void QAudioOutputPrivate::stop() |
|
259 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
260 |
if(deviceState == QAudio::StoppedState) |
0 | 261 |
return; |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
262 |
deviceState = QAudio::StoppedState; |
0 | 263 |
close(); |
264 |
emit stateChanged(deviceState); |
|
265 |
} |
|
266 |
||
267 |
bool QAudioOutputPrivate::open() |
|
268 |
{ |
|
269 |
if(opened) |
|
270 |
return true; |
|
271 |
||
272 |
#ifdef DEBUG_AUDIO |
|
273 |
QTime now(QTime::currentTime()); |
|
274 |
qDebug()<<now.second()<<"s "<<now.msec()<<"ms :open()"; |
|
275 |
#endif |
|
276 |
timeStamp.restart(); |
|
277 |
elapsedTimeOffset = 0; |
|
278 |
||
279 |
int dir; |
|
280 |
int err=-1; |
|
281 |
int count=0; |
|
282 |
unsigned int freakuency=settings.frequency(); |
|
283 |
||
284 |
QString dev = QLatin1String(m_device.constData()); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
285 |
QList<QByteArray> devices = QAudioDeviceInfoInternal::availableDevices(QAudio::AudioOutput); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
286 |
if(dev.compare(QLatin1String("default")) == 0) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
287 |
#if(SND_LIB_MAJOR == 1 && SND_LIB_MINOR == 0 && SND_LIB_SUBMINOR >= 14) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
288 |
dev = QLatin1String(devices.first().constData()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
289 |
#else |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
290 |
dev = QLatin1String("hw:0,0"); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
291 |
#endif |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
292 |
} else { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
293 |
#if(SND_LIB_MAJOR == 1 && SND_LIB_MINOR == 0 && SND_LIB_SUBMINOR >= 14) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
294 |
dev = QLatin1String(m_device); |
0 | 295 |
#else |
296 |
int idx = 0; |
|
297 |
char *name; |
|
298 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
299 |
QString shortName = QLatin1String(m_device.mid(m_device.indexOf('=',0)+1).constData()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
300 |
|
0 | 301 |
while(snd_card_get_name(idx,&name) == 0) { |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
302 |
if(qstrncmp(shortName.toLocal8Bit().constData(),name,shortName.length()) == 0) |
0 | 303 |
break; |
304 |
idx++; |
|
305 |
} |
|
306 |
dev = QString(QLatin1String("hw:%1,0")).arg(idx); |
|
307 |
#endif |
|
308 |
} |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
309 |
|
0 | 310 |
// Step 1: try and open the device |
311 |
while((count < 5) && (err < 0)) { |
|
312 |
err=snd_pcm_open(&handle,dev.toLocal8Bit().constData(),SND_PCM_STREAM_PLAYBACK,0); |
|
313 |
if(err < 0) |
|
314 |
count++; |
|
315 |
} |
|
316 |
if (( err < 0)||(handle == 0)) { |
|
317 |
errorState = QAudio::OpenError; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
318 |
deviceState = QAudio::StoppedState; |
0 | 319 |
return false; |
320 |
} |
|
321 |
snd_pcm_nonblock( handle, 0 ); |
|
322 |
||
323 |
// Step 2: Set the desired HW parameters. |
|
324 |
snd_pcm_hw_params_alloca( &hwparams ); |
|
325 |
||
326 |
bool fatal = false; |
|
327 |
QString errMessage; |
|
328 |
unsigned int chunks = 8; |
|
329 |
||
330 |
err = snd_pcm_hw_params_any( handle, hwparams ); |
|
331 |
if ( err < 0 ) { |
|
332 |
fatal = true; |
|
333 |
errMessage = QString::fromLatin1("QAudioOutput: snd_pcm_hw_params_any: err = %1").arg(err); |
|
334 |
} |
|
335 |
if ( !fatal ) { |
|
336 |
err = snd_pcm_hw_params_set_rate_resample( handle, hwparams, 1 ); |
|
337 |
if ( err < 0 ) { |
|
338 |
fatal = true; |
|
339 |
errMessage = QString::fromLatin1("QAudioOutput: snd_pcm_hw_params_set_rate_resample: err = %1").arg(err); |
|
340 |
} |
|
341 |
} |
|
342 |
if ( !fatal ) { |
|
343 |
err = snd_pcm_hw_params_set_access( handle, hwparams, access ); |
|
344 |
if ( err < 0 ) { |
|
345 |
fatal = true; |
|
346 |
errMessage = QString::fromLatin1("QAudioOutput: snd_pcm_hw_params_set_access: err = %1").arg(err); |
|
347 |
} |
|
348 |
} |
|
349 |
if ( !fatal ) { |
|
350 |
err = setFormat(); |
|
351 |
if ( err < 0 ) { |
|
352 |
fatal = true; |
|
353 |
errMessage = QString::fromLatin1("QAudioOutput: snd_pcm_hw_params_set_format: err = %1").arg(err); |
|
354 |
} |
|
355 |
} |
|
356 |
if ( !fatal ) { |
|
357 |
err = snd_pcm_hw_params_set_channels( handle, hwparams, (unsigned int)settings.channels() ); |
|
358 |
if ( err < 0 ) { |
|
359 |
fatal = true; |
|
360 |
errMessage = QString::fromLatin1("QAudioOutput: snd_pcm_hw_params_set_channels: err = %1").arg(err); |
|
361 |
} |
|
362 |
} |
|
363 |
if ( !fatal ) { |
|
364 |
err = snd_pcm_hw_params_set_rate_near( handle, hwparams, &freakuency, 0 ); |
|
365 |
if ( err < 0 ) { |
|
366 |
fatal = true; |
|
367 |
errMessage = QString::fromLatin1("QAudioOutput: snd_pcm_hw_params_set_rate_near: err = %1").arg(err); |
|
368 |
} |
|
369 |
} |
|
370 |
if ( !fatal ) { |
|
371 |
err = snd_pcm_hw_params_set_buffer_time_near(handle, hwparams, &buffer_time, &dir); |
|
372 |
if ( err < 0 ) { |
|
373 |
fatal = true; |
|
374 |
errMessage = QString::fromLatin1("QAudioOutput: snd_pcm_hw_params_set_buffer_time_near: err = %1").arg(err); |
|
375 |
} |
|
376 |
} |
|
377 |
if ( !fatal ) { |
|
378 |
err = snd_pcm_hw_params_set_period_time_near(handle, hwparams, &period_time, &dir); |
|
379 |
if ( err < 0 ) { |
|
380 |
fatal = true; |
|
381 |
errMessage = QString::fromLatin1("QAudioOutput: snd_pcm_hw_params_set_period_time_near: err = %1").arg(err); |
|
382 |
} |
|
383 |
} |
|
384 |
if ( !fatal ) { |
|
385 |
err = snd_pcm_hw_params_set_periods_near(handle, hwparams, &chunks, &dir); |
|
386 |
if ( err < 0 ) { |
|
387 |
fatal = true; |
|
388 |
errMessage = QString::fromLatin1("QAudioOutput: snd_pcm_hw_params_set_periods_near: err = %1").arg(err); |
|
389 |
} |
|
390 |
} |
|
391 |
if ( !fatal ) { |
|
392 |
err = snd_pcm_hw_params(handle, hwparams); |
|
393 |
if ( err < 0 ) { |
|
394 |
fatal = true; |
|
395 |
errMessage = QString::fromLatin1("QAudioOutput: snd_pcm_hw_params: err = %1").arg(err); |
|
396 |
} |
|
397 |
} |
|
398 |
if( err < 0) { |
|
399 |
qWarning()<<errMessage; |
|
400 |
errorState = QAudio::OpenError; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
401 |
deviceState = QAudio::StoppedState; |
0 | 402 |
return false; |
403 |
} |
|
404 |
snd_pcm_hw_params_get_buffer_size(hwparams,&buffer_frames); |
|
405 |
buffer_size = snd_pcm_frames_to_bytes(handle,buffer_frames); |
|
406 |
snd_pcm_hw_params_get_period_size(hwparams,&period_frames, &dir); |
|
407 |
period_size = snd_pcm_frames_to_bytes(handle,period_frames); |
|
408 |
snd_pcm_hw_params_get_buffer_time(hwparams,&buffer_time, &dir); |
|
409 |
snd_pcm_hw_params_get_period_time(hwparams,&period_time, &dir); |
|
410 |
||
411 |
// Step 3: Set the desired SW parameters. |
|
412 |
snd_pcm_sw_params_t *swparams; |
|
413 |
snd_pcm_sw_params_alloca(&swparams); |
|
414 |
snd_pcm_sw_params_current(handle, swparams); |
|
415 |
snd_pcm_sw_params_set_start_threshold(handle,swparams,period_frames); |
|
416 |
snd_pcm_sw_params_set_stop_threshold(handle,swparams,buffer_frames); |
|
417 |
snd_pcm_sw_params_set_avail_min(handle, swparams,period_frames); |
|
418 |
snd_pcm_sw_params(handle, swparams); |
|
419 |
||
420 |
// Step 4: Prepare audio |
|
421 |
if(audioBuffer == 0) |
|
422 |
audioBuffer = new char[snd_pcm_frames_to_bytes(handle,buffer_frames)]; |
|
423 |
snd_pcm_prepare( handle ); |
|
424 |
snd_pcm_start(handle); |
|
425 |
||
426 |
// Step 5: Setup callback and timer fallback |
|
427 |
snd_async_add_pcm_handler(&ahandler, handle, async_callback, this); |
|
428 |
bytesAvailable = bytesFree(); |
|
429 |
||
430 |
// Step 6: Start audio processing |
|
431 |
timer->start(period_time/1000); |
|
432 |
||
433 |
clockStamp.restart(); |
|
434 |
timeStamp.restart(); |
|
435 |
elapsedTimeOffset = 0; |
|
436 |
errorState = QAudio::NoError; |
|
437 |
totalTimeValue = 0; |
|
438 |
opened = true; |
|
439 |
||
440 |
return true; |
|
441 |
} |
|
442 |
||
443 |
void QAudioOutputPrivate::close() |
|
444 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
445 |
deviceState = QAudio::StoppedState; |
0 | 446 |
timer->stop(); |
447 |
||
448 |
if ( handle ) { |
|
449 |
snd_pcm_drain( handle ); |
|
450 |
snd_pcm_close( handle ); |
|
451 |
handle = 0; |
|
452 |
delete [] audioBuffer; |
|
453 |
audioBuffer=0; |
|
454 |
} |
|
455 |
if(!pullMode && audioSource) { |
|
456 |
delete audioSource; |
|
457 |
audioSource = 0; |
|
458 |
} |
|
459 |
opened = false; |
|
460 |
} |
|
461 |
||
462 |
int QAudioOutputPrivate::bytesFree() const |
|
463 |
{ |
|
464 |
if(resuming) |
|
465 |
return period_size; |
|
466 |
||
467 |
if(deviceState != QAudio::ActiveState && deviceState != QAudio::IdleState) |
|
468 |
return 0; |
|
469 |
int frames = snd_pcm_avail_update(handle); |
|
470 |
if((int)frames > (int)buffer_frames) |
|
471 |
frames = buffer_frames; |
|
472 |
||
473 |
return snd_pcm_frames_to_bytes(handle, frames); |
|
474 |
} |
|
475 |
||
476 |
qint64 QAudioOutputPrivate::write( const char *data, qint64 len ) |
|
477 |
{ |
|
478 |
// Write out some audio data |
|
479 |
if ( !handle ) |
|
480 |
return 0; |
|
481 |
#ifdef DEBUG_AUDIO |
|
482 |
qDebug()<<"frames to write out = "<< |
|
483 |
snd_pcm_bytes_to_frames( handle, (int)len )<<" ("<<len<<") bytes"; |
|
484 |
#endif |
|
485 |
int frames, err; |
|
486 |
int space = bytesFree(); |
|
487 |
if(len < space) { |
|
488 |
// Just write it |
|
489 |
frames = snd_pcm_bytes_to_frames( handle, (int)len ); |
|
490 |
err = snd_pcm_writei( handle, data, frames ); |
|
491 |
} else { |
|
492 |
// Only write space worth |
|
493 |
frames = snd_pcm_bytes_to_frames( handle, (int)space ); |
|
494 |
err = snd_pcm_writei( handle, data, frames ); |
|
495 |
} |
|
496 |
if(err > 0) { |
|
497 |
totalTimeValue += err*1000000/settings.frequency(); |
|
498 |
resuming = false; |
|
499 |
errorState = QAudio::NoError; |
|
500 |
deviceState = QAudio::ActiveState; |
|
501 |
return snd_pcm_frames_to_bytes( handle, err ); |
|
502 |
} else |
|
503 |
err = xrun_recovery(err); |
|
504 |
||
505 |
if(err < 0) { |
|
506 |
close(); |
|
507 |
errorState = QAudio::FatalError; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
508 |
deviceState = QAudio::StoppedState; |
0 | 509 |
emit stateChanged(deviceState); |
510 |
} |
|
511 |
return 0; |
|
512 |
} |
|
513 |
||
514 |
int QAudioOutputPrivate::periodSize() const |
|
515 |
{ |
|
516 |
return period_size; |
|
517 |
} |
|
518 |
||
519 |
void QAudioOutputPrivate::setBufferSize(int value) |
|
520 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
521 |
if(deviceState == QAudio::StoppedState) |
0 | 522 |
buffer_size = value; |
523 |
} |
|
524 |
||
525 |
int QAudioOutputPrivate::bufferSize() const |
|
526 |
{ |
|
527 |
return buffer_size; |
|
528 |
} |
|
529 |
||
530 |
void QAudioOutputPrivate::setNotifyInterval(int ms) |
|
531 |
{ |
|
532 |
if(ms >= minimumIntervalTime) |
|
533 |
intervalTime = ms; |
|
534 |
else |
|
535 |
intervalTime = minimumIntervalTime; |
|
536 |
} |
|
537 |
||
538 |
int QAudioOutputPrivate::notifyInterval() const |
|
539 |
{ |
|
540 |
return intervalTime; |
|
541 |
} |
|
542 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
543 |
qint64 QAudioOutputPrivate::processedUSecs() const |
0 | 544 |
{ |
545 |
return totalTimeValue; |
|
546 |
} |
|
547 |
||
548 |
void QAudioOutputPrivate::resume() |
|
549 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
550 |
if(deviceState == QAudio::SuspendedState) { |
0 | 551 |
int err = 0; |
552 |
||
553 |
if(handle) { |
|
554 |
err = snd_pcm_prepare( handle ); |
|
555 |
if(err < 0) |
|
556 |
xrun_recovery(err); |
|
557 |
||
558 |
err = snd_pcm_start(handle); |
|
559 |
if(err < 0) |
|
560 |
xrun_recovery(err); |
|
561 |
||
562 |
bytesAvailable = (int)snd_pcm_frames_to_bytes(handle, buffer_frames); |
|
563 |
} |
|
564 |
resuming = true; |
|
565 |
if(pullMode) |
|
566 |
deviceState = QAudio::ActiveState; |
|
567 |
else |
|
568 |
deviceState = QAudio::IdleState; |
|
569 |
||
570 |
errorState = QAudio::NoError; |
|
571 |
timer->start(period_time/1000); |
|
572 |
emit stateChanged(deviceState); |
|
573 |
} |
|
574 |
} |
|
575 |
||
576 |
QAudioFormat QAudioOutputPrivate::format() const |
|
577 |
{ |
|
578 |
return settings; |
|
579 |
} |
|
580 |
||
581 |
void QAudioOutputPrivate::suspend() |
|
582 |
{ |
|
583 |
if(deviceState == QAudio::ActiveState || deviceState == QAudio::IdleState || resuming) { |
|
584 |
timer->stop(); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
585 |
deviceState = QAudio::SuspendedState; |
0 | 586 |
errorState = QAudio::NoError; |
587 |
emit stateChanged(deviceState); |
|
588 |
} |
|
589 |
} |
|
590 |
||
591 |
void QAudioOutputPrivate::userFeed() |
|
592 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
593 |
if(deviceState == QAudio::StoppedState || deviceState == QAudio::SuspendedState) |
0 | 594 |
return; |
595 |
#ifdef DEBUG_AUDIO |
|
596 |
QTime now(QTime::currentTime()); |
|
597 |
qDebug()<<now.second()<<"s "<<now.msec()<<"ms :userFeed() OUT"; |
|
598 |
#endif |
|
599 |
if(deviceState == QAudio::IdleState) |
|
600 |
bytesAvailable = bytesFree(); |
|
601 |
||
602 |
deviceReady(); |
|
603 |
} |
|
604 |
||
605 |
void QAudioOutputPrivate::feedback() |
|
606 |
{ |
|
607 |
QMetaObject::invokeMethod(this, SLOT(updateAvailable()), Qt::QueuedConnection); |
|
608 |
} |
|
609 |
||
610 |
void QAudioOutputPrivate::updateAvailable() |
|
611 |
{ |
|
612 |
#ifdef DEBUG_AUDIO |
|
613 |
QTime now(QTime::currentTime()); |
|
614 |
qDebug()<<now.second()<<"s "<<now.msec()<<"ms :updateAvailable()"; |
|
615 |
#endif |
|
616 |
bytesAvailable = bytesFree(); |
|
617 |
} |
|
618 |
||
619 |
bool QAudioOutputPrivate::deviceReady() |
|
620 |
{ |
|
621 |
if(pullMode) { |
|
622 |
int l = 0; |
|
623 |
int chunks = bytesAvailable/period_size; |
|
624 |
if(chunks==0) { |
|
625 |
bytesAvailable = bytesFree(); |
|
626 |
return false; |
|
627 |
} |
|
628 |
#ifdef DEBUG_AUDIO |
|
629 |
qDebug()<<"deviceReady() avail="<<bytesAvailable<<" bytes, period size="<<period_size<<" bytes"; |
|
630 |
qDebug()<<"deviceReady() no. of chunks that can fit ="<<chunks<<", chunks in bytes ="<<period_size*chunks; |
|
631 |
#endif |
|
632 |
int input = period_frames*chunks; |
|
633 |
if(input > (int)buffer_frames) |
|
634 |
input = buffer_frames; |
|
635 |
l = audioSource->read(audioBuffer,snd_pcm_frames_to_bytes(handle, input)); |
|
636 |
if(l > 0) { |
|
637 |
// Got some data to output |
|
638 |
if(deviceState != QAudio::ActiveState) |
|
639 |
return true; |
|
640 |
write(audioBuffer,l); |
|
641 |
bytesAvailable = bytesFree(); |
|
642 |
||
643 |
} else if(l == 0) { |
|
644 |
// Did not get any data to output |
|
645 |
bytesAvailable = bytesFree(); |
|
646 |
if(bytesAvailable > snd_pcm_frames_to_bytes(handle, buffer_frames-period_frames)) { |
|
647 |
// Underrun |
|
648 |
errorState = QAudio::UnderrunError; |
|
649 |
deviceState = QAudio::IdleState; |
|
650 |
emit stateChanged(deviceState); |
|
651 |
} |
|
652 |
||
653 |
} else if(l < 0) { |
|
654 |
close(); |
|
655 |
errorState = QAudio::IOError; |
|
656 |
emit stateChanged(deviceState); |
|
657 |
} |
|
658 |
} else |
|
659 |
bytesAvailable = bytesFree(); |
|
660 |
||
661 |
if(deviceState != QAudio::ActiveState) |
|
662 |
return true; |
|
663 |
||
664 |
if((timeStamp.elapsed() + elapsedTimeOffset) > intervalTime) { |
|
665 |
emit notify(); |
|
666 |
elapsedTimeOffset = timeStamp.elapsed() + elapsedTimeOffset - intervalTime; |
|
667 |
timeStamp.restart(); |
|
668 |
} |
|
669 |
return true; |
|
670 |
} |
|
671 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
672 |
qint64 QAudioOutputPrivate::elapsedUSecs() const |
0 | 673 |
{ |
674 |
if(!handle) |
|
675 |
return 0; |
|
676 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
677 |
if (deviceState == QAudio::StoppedState) |
0 | 678 |
return 0; |
679 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
680 |
#if(SND_LIB_MAJOR == 1 && SND_LIB_MINOR == 0 && SND_LIB_SUBMINOR >= 14) |
0 | 681 |
snd_pcm_status_t* status; |
682 |
snd_pcm_status_alloca(&status); |
|
683 |
||
684 |
snd_timestamp_t t1,t2; |
|
685 |
if( snd_pcm_status(handle, status) >= 0) { |
|
686 |
snd_pcm_status_get_tstamp(status,&t1); |
|
687 |
snd_pcm_status_get_trigger_tstamp(status,&t2); |
|
688 |
t1.tv_sec-=t2.tv_sec; |
|
689 |
||
690 |
signed long l = (signed long)t1.tv_usec - (signed long)t2.tv_usec; |
|
691 |
if(l < 0) { |
|
692 |
t1.tv_sec--; |
|
693 |
l = -l; |
|
694 |
l %= 1000000; |
|
695 |
} |
|
696 |
return ((t1.tv_sec * 1000000)+l); |
|
697 |
} else |
|
698 |
return 0; |
|
699 |
#else |
|
700 |
return clockStamp.elapsed()*1000; |
|
701 |
#endif |
|
702 |
return 0; |
|
703 |
} |
|
704 |
||
705 |
void QAudioOutputPrivate::reset() |
|
706 |
{ |
|
707 |
if(handle) |
|
708 |
snd_pcm_reset(handle); |
|
709 |
||
710 |
stop(); |
|
711 |
} |
|
712 |
||
713 |
OutputPrivate::OutputPrivate(QAudioOutputPrivate* audio) |
|
714 |
{ |
|
715 |
audioDevice = qobject_cast<QAudioOutputPrivate*>(audio); |
|
716 |
} |
|
717 |
||
718 |
OutputPrivate::~OutputPrivate() {} |
|
719 |
||
720 |
qint64 OutputPrivate::readData( char* data, qint64 len) |
|
721 |
{ |
|
722 |
Q_UNUSED(data) |
|
723 |
Q_UNUSED(len) |
|
724 |
||
725 |
return 0; |
|
726 |
} |
|
727 |
||
728 |
qint64 OutputPrivate::writeData(const char* data, qint64 len) |
|
729 |
{ |
|
730 |
int retry = 0; |
|
731 |
qint64 written = 0; |
|
732 |
if((audioDevice->deviceState == QAudio::ActiveState) |
|
733 |
||(audioDevice->deviceState == QAudio::IdleState)) { |
|
734 |
while(written < len) { |
|
735 |
int chunk = audioDevice->write(data+written,(len-written)); |
|
736 |
if(chunk <= 0) |
|
737 |
retry++; |
|
738 |
written+=chunk; |
|
739 |
if(retry > 10) |
|
740 |
return written; |
|
741 |
} |
|
742 |
} |
|
743 |
return written; |
|
744 |
||
745 |
} |
|
746 |
||
747 |
QT_END_NAMESPACE |