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