|
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 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 #include "qaudioinput_symbian_p.h" |
|
43 |
|
44 QT_BEGIN_NAMESPACE |
|
45 |
|
46 //----------------------------------------------------------------------------- |
|
47 // Constants |
|
48 //----------------------------------------------------------------------------- |
|
49 |
|
50 const int PushInterval = 50; // ms |
|
51 |
|
52 |
|
53 //----------------------------------------------------------------------------- |
|
54 // Private class |
|
55 //----------------------------------------------------------------------------- |
|
56 |
|
57 SymbianAudioInputPrivate::SymbianAudioInputPrivate( |
|
58 QAudioInputPrivate *audioDevice) |
|
59 : m_audioDevice(audioDevice) |
|
60 { |
|
61 |
|
62 } |
|
63 |
|
64 SymbianAudioInputPrivate::~SymbianAudioInputPrivate() |
|
65 { |
|
66 |
|
67 } |
|
68 |
|
69 qint64 SymbianAudioInputPrivate::readData(char *data, qint64 len) |
|
70 { |
|
71 qint64 totalRead = 0; |
|
72 |
|
73 if (m_audioDevice->state() == QAudio::ActiveState || |
|
74 m_audioDevice->state() == QAudio::IdleState) { |
|
75 |
|
76 while (totalRead < len) { |
|
77 const qint64 read = m_audioDevice->read(data + totalRead, |
|
78 len - totalRead); |
|
79 if (read > 0) |
|
80 totalRead += read; |
|
81 else |
|
82 break; |
|
83 } |
|
84 } |
|
85 |
|
86 return totalRead; |
|
87 } |
|
88 |
|
89 qint64 SymbianAudioInputPrivate::writeData(const char *data, qint64 len) |
|
90 { |
|
91 Q_UNUSED(data) |
|
92 Q_UNUSED(len) |
|
93 return 0; |
|
94 } |
|
95 |
|
96 void SymbianAudioInputPrivate::dataReady() |
|
97 { |
|
98 emit readyRead(); |
|
99 } |
|
100 |
|
101 |
|
102 //----------------------------------------------------------------------------- |
|
103 // Public functions |
|
104 //----------------------------------------------------------------------------- |
|
105 |
|
106 QAudioInputPrivate::QAudioInputPrivate(const QByteArray &device, |
|
107 const QAudioFormat &format) |
|
108 : m_device(device) |
|
109 , m_format(format) |
|
110 , m_clientBufferSize(SymbianAudio::DefaultBufferSize) |
|
111 , m_notifyInterval(SymbianAudio::DefaultNotifyInterval) |
|
112 , m_notifyTimer(new QTimer(this)) |
|
113 , m_error(QAudio::NoError) |
|
114 , m_internalState(SymbianAudio::ClosedState) |
|
115 , m_externalState(QAudio::StoppedState) |
|
116 , m_pullMode(false) |
|
117 , m_sink(0) |
|
118 , m_pullTimer(new QTimer(this)) |
|
119 , m_devSoundBuffer(0) |
|
120 , m_devSoundBufferSize(0) |
|
121 , m_totalBytesReady(0) |
|
122 , m_devSoundBufferPos(0) |
|
123 , m_totalSamplesRecorded(0) |
|
124 { |
|
125 connect(m_notifyTimer.data(), SIGNAL(timeout()), this, SIGNAL(notify())); |
|
126 |
|
127 SymbianAudio::Utils::formatQtToNative(m_format, m_nativeFourCC, |
|
128 m_nativeFormat); |
|
129 |
|
130 m_pullTimer->setInterval(PushInterval); |
|
131 connect(m_pullTimer.data(), SIGNAL(timeout()), this, SLOT(pullData())); |
|
132 } |
|
133 |
|
134 QAudioInputPrivate::~QAudioInputPrivate() |
|
135 { |
|
136 close(); |
|
137 } |
|
138 |
|
139 QIODevice* QAudioInputPrivate::start(QIODevice *device) |
|
140 { |
|
141 stop(); |
|
142 |
|
143 open(); |
|
144 if (SymbianAudio::ClosedState != m_internalState) { |
|
145 if (device) { |
|
146 m_pullMode = true; |
|
147 m_sink = device; |
|
148 } else { |
|
149 m_sink = new SymbianAudioInputPrivate(this); |
|
150 m_sink->open(QIODevice::ReadOnly | QIODevice::Unbuffered); |
|
151 } |
|
152 |
|
153 m_elapsed.restart(); |
|
154 } |
|
155 |
|
156 return m_sink; |
|
157 } |
|
158 |
|
159 void QAudioInputPrivate::stop() |
|
160 { |
|
161 close(); |
|
162 } |
|
163 |
|
164 void QAudioInputPrivate::reset() |
|
165 { |
|
166 m_totalSamplesRecorded += getSamplesRecorded(); |
|
167 m_devSound->Stop(); |
|
168 startRecording(); |
|
169 } |
|
170 |
|
171 void QAudioInputPrivate::suspend() |
|
172 { |
|
173 if (SymbianAudio::ActiveState == m_internalState |
|
174 || SymbianAudio::IdleState == m_internalState) { |
|
175 m_notifyTimer->stop(); |
|
176 m_pullTimer->stop(); |
|
177 m_devSound->Pause(); |
|
178 const qint64 samplesRecorded = getSamplesRecorded(); |
|
179 m_totalSamplesRecorded += samplesRecorded; |
|
180 |
|
181 if (m_devSoundBuffer) { |
|
182 m_devSoundBufferQ.append(m_devSoundBuffer); |
|
183 m_devSoundBuffer = 0; |
|
184 } |
|
185 |
|
186 setState(SymbianAudio::SuspendedState); |
|
187 } |
|
188 } |
|
189 |
|
190 void QAudioInputPrivate::resume() |
|
191 { |
|
192 if (SymbianAudio::SuspendedState == m_internalState) |
|
193 startDataTransfer(); |
|
194 } |
|
195 |
|
196 int QAudioInputPrivate::bytesReady() const |
|
197 { |
|
198 Q_ASSERT(m_devSoundBufferPos <= m_totalBytesReady); |
|
199 return m_totalBytesReady - m_devSoundBufferPos; |
|
200 } |
|
201 |
|
202 int QAudioInputPrivate::periodSize() const |
|
203 { |
|
204 return bufferSize(); |
|
205 } |
|
206 |
|
207 void QAudioInputPrivate::setBufferSize(int value) |
|
208 { |
|
209 // Note that DevSound does not allow its client to specify the buffer size. |
|
210 // This functionality is available via custom interfaces, but since these |
|
211 // cannot be guaranteed to work across all DevSound implementations, we |
|
212 // do not use them here. |
|
213 // In order to comply with the expected bevahiour of QAudioInput, we store |
|
214 // the value and return it from bufferSize(), but the underlying DevSound |
|
215 // buffer size remains unchanged. |
|
216 if (value > 0) |
|
217 m_clientBufferSize = value; |
|
218 } |
|
219 |
|
220 int QAudioInputPrivate::bufferSize() const |
|
221 { |
|
222 return m_devSoundBufferSize ? m_devSoundBufferSize : m_clientBufferSize; |
|
223 } |
|
224 |
|
225 void QAudioInputPrivate::setNotifyInterval(int ms) |
|
226 { |
|
227 if (ms > 0) { |
|
228 const int oldNotifyInterval = m_notifyInterval; |
|
229 m_notifyInterval = ms; |
|
230 if (m_notifyTimer->isActive() && ms != oldNotifyInterval) |
|
231 m_notifyTimer->start(m_notifyInterval); |
|
232 } |
|
233 } |
|
234 |
|
235 int QAudioInputPrivate::notifyInterval() const |
|
236 { |
|
237 return m_notifyInterval; |
|
238 } |
|
239 |
|
240 qint64 QAudioInputPrivate::processedUSecs() const |
|
241 { |
|
242 int samplesPlayed = 0; |
|
243 if (m_devSound && SymbianAudio::SuspendedState != m_internalState) |
|
244 samplesPlayed = getSamplesRecorded(); |
|
245 |
|
246 // Protect against division by zero |
|
247 Q_ASSERT_X(m_format.frequency() > 0, Q_FUNC_INFO, "Invalid frequency"); |
|
248 |
|
249 const qint64 result = qint64(1000000) * |
|
250 (samplesPlayed + m_totalSamplesRecorded) |
|
251 / m_format.frequency(); |
|
252 |
|
253 return result; |
|
254 } |
|
255 |
|
256 qint64 QAudioInputPrivate::elapsedUSecs() const |
|
257 { |
|
258 const qint64 result = (QAudio::StoppedState == state()) ? |
|
259 0 : m_elapsed.elapsed() * 1000; |
|
260 return result; |
|
261 } |
|
262 |
|
263 QAudio::Error QAudioInputPrivate::error() const |
|
264 { |
|
265 return m_error; |
|
266 } |
|
267 |
|
268 QAudio::State QAudioInputPrivate::state() const |
|
269 { |
|
270 return m_externalState; |
|
271 } |
|
272 |
|
273 QAudioFormat QAudioInputPrivate::format() const |
|
274 { |
|
275 return m_format; |
|
276 } |
|
277 |
|
278 //----------------------------------------------------------------------------- |
|
279 // MDevSoundObserver implementation |
|
280 //----------------------------------------------------------------------------- |
|
281 |
|
282 void QAudioInputPrivate::InitializeComplete(TInt aError) |
|
283 { |
|
284 Q_ASSERT_X(SymbianAudio::InitializingState == m_internalState, |
|
285 Q_FUNC_INFO, "Invalid state"); |
|
286 |
|
287 if (KErrNone == aError) |
|
288 startRecording(); |
|
289 } |
|
290 |
|
291 void QAudioInputPrivate::ToneFinished(TInt aError) |
|
292 { |
|
293 Q_UNUSED(aError) |
|
294 // This class doesn't use DevSound's tone playback functions, so should |
|
295 // never receive this callback. |
|
296 Q_ASSERT_X(false, Q_FUNC_INFO, "Unexpected callback"); |
|
297 } |
|
298 |
|
299 void QAudioInputPrivate::BufferToBeFilled(CMMFBuffer *aBuffer) |
|
300 { |
|
301 Q_UNUSED(aBuffer) |
|
302 // This class doesn't use DevSound in play mode, so should never receive |
|
303 // this callback. |
|
304 Q_ASSERT_X(false, Q_FUNC_INFO, "Unexpected callback"); |
|
305 } |
|
306 |
|
307 void QAudioInputPrivate::PlayError(TInt aError) |
|
308 { |
|
309 Q_UNUSED(aError) |
|
310 // This class doesn't use DevSound in play mode, so should never receive |
|
311 // this callback. |
|
312 Q_ASSERT_X(false, Q_FUNC_INFO, "Unexpected callback"); |
|
313 } |
|
314 |
|
315 void QAudioInputPrivate::BufferToBeEmptied(CMMFBuffer *aBuffer) |
|
316 { |
|
317 // Following receipt of this callback, DevSound should not provide another |
|
318 // buffer until we have returned the current one. |
|
319 Q_ASSERT_X(!m_devSoundBuffer, Q_FUNC_INFO, "Buffer already held"); |
|
320 |
|
321 CMMFDataBuffer *const buffer = static_cast<CMMFDataBuffer*>(aBuffer); |
|
322 |
|
323 if (!m_devSoundBufferSize) |
|
324 m_devSoundBufferSize = buffer->Data().MaxLength(); |
|
325 |
|
326 m_totalBytesReady += buffer->Data().Length(); |
|
327 |
|
328 if (SymbianAudio::SuspendedState == m_internalState) { |
|
329 m_devSoundBufferQ.append(buffer); |
|
330 } else { |
|
331 // Will be returned to DevSound by bufferEmptied(). |
|
332 m_devSoundBuffer = buffer; |
|
333 m_devSoundBufferPos = 0; |
|
334 |
|
335 if (bytesReady() && !m_pullMode) |
|
336 pushData(); |
|
337 } |
|
338 } |
|
339 |
|
340 void QAudioInputPrivate::RecordError(TInt aError) |
|
341 { |
|
342 Q_UNUSED(aError) |
|
343 setError(QAudio::IOError); |
|
344 } |
|
345 |
|
346 void QAudioInputPrivate::ConvertError(TInt aError) |
|
347 { |
|
348 Q_UNUSED(aError) |
|
349 // This class doesn't use DevSound's format conversion functions, so |
|
350 // should never receive this callback. |
|
351 Q_ASSERT_X(false, Q_FUNC_INFO, "Unexpected callback"); |
|
352 } |
|
353 |
|
354 void QAudioInputPrivate::DeviceMessage(TUid aMessageType, const TDesC8 &aMsg) |
|
355 { |
|
356 Q_UNUSED(aMessageType) |
|
357 Q_UNUSED(aMsg) |
|
358 // Ignore this callback. |
|
359 } |
|
360 |
|
361 //----------------------------------------------------------------------------- |
|
362 // Private functions |
|
363 //----------------------------------------------------------------------------- |
|
364 |
|
365 void QAudioInputPrivate::open() |
|
366 { |
|
367 Q_ASSERT_X(SymbianAudio::ClosedState == m_internalState, |
|
368 Q_FUNC_INFO, "DevSound already opened"); |
|
369 |
|
370 QT_TRAP_THROWING( m_devSound.reset(CMMFDevSound::NewL()) ) |
|
371 |
|
372 QScopedPointer<SymbianAudio::DevSoundCapabilities> caps( |
|
373 new SymbianAudio::DevSoundCapabilities(*m_devSound, QAudio::AudioInput)); |
|
374 |
|
375 int err = SymbianAudio::Utils::isFormatSupported(m_format, *caps) ? |
|
376 KErrNone : KErrNotSupported; |
|
377 |
|
378 if (KErrNone == err) { |
|
379 setState(SymbianAudio::InitializingState); |
|
380 TRAP(err, m_devSound->InitializeL(*this, m_nativeFourCC, |
|
381 EMMFStateRecording)); |
|
382 } |
|
383 |
|
384 if (KErrNone != err) { |
|
385 setError(QAudio::OpenError); |
|
386 m_devSound.reset(); |
|
387 } |
|
388 } |
|
389 |
|
390 void QAudioInputPrivate::startRecording() |
|
391 { |
|
392 const int samplesRecorded = m_devSound->SamplesRecorded(); |
|
393 Q_ASSERT(samplesRecorded == 0); |
|
394 |
|
395 TRAPD(err, startDevSoundL()); |
|
396 if (KErrNone == err) { |
|
397 startDataTransfer(); |
|
398 } else { |
|
399 setError(QAudio::OpenError); |
|
400 close(); |
|
401 } |
|
402 } |
|
403 |
|
404 void QAudioInputPrivate::startDevSoundL() |
|
405 { |
|
406 TMMFCapabilities nativeFormat = m_devSound->Config(); |
|
407 m_nativeFormat.iBufferSize = nativeFormat.iBufferSize; |
|
408 m_devSound->SetConfigL(m_nativeFormat); |
|
409 m_devSound->RecordInitL(); |
|
410 } |
|
411 |
|
412 void QAudioInputPrivate::startDataTransfer() |
|
413 { |
|
414 m_notifyTimer->start(m_notifyInterval); |
|
415 |
|
416 if (m_pullMode) |
|
417 m_pullTimer->start(); |
|
418 |
|
419 if (bytesReady()) { |
|
420 setState(SymbianAudio::ActiveState); |
|
421 if (!m_pullMode) |
|
422 pushData(); |
|
423 } else { |
|
424 if (SymbianAudio::SuspendedState == m_internalState) |
|
425 setState(SymbianAudio::ActiveState); |
|
426 else |
|
427 setState(SymbianAudio::IdleState); |
|
428 } |
|
429 } |
|
430 |
|
431 CMMFDataBuffer* QAudioInputPrivate::currentBuffer() const |
|
432 { |
|
433 CMMFDataBuffer *result = m_devSoundBuffer; |
|
434 if (!result && !m_devSoundBufferQ.empty()) |
|
435 result = m_devSoundBufferQ.front(); |
|
436 return result; |
|
437 } |
|
438 |
|
439 void QAudioInputPrivate::pushData() |
|
440 { |
|
441 Q_ASSERT_X(bytesReady(), Q_FUNC_INFO, "No data available"); |
|
442 Q_ASSERT_X(!m_pullMode, Q_FUNC_INFO, "pushData called when in pull mode"); |
|
443 qobject_cast<SymbianAudioInputPrivate *>(m_sink)->dataReady(); |
|
444 } |
|
445 |
|
446 qint64 QAudioInputPrivate::read(char *data, qint64 len) |
|
447 { |
|
448 // SymbianAudioInputPrivate is ready to read data |
|
449 |
|
450 Q_ASSERT_X(!m_pullMode, Q_FUNC_INFO, |
|
451 "read called when in pull mode"); |
|
452 |
|
453 qint64 bytesRead = 0; |
|
454 |
|
455 CMMFDataBuffer *buffer = 0; |
|
456 while ((buffer = currentBuffer()) && (bytesRead < len)) { |
|
457 if (SymbianAudio::IdleState == m_internalState) |
|
458 setState(SymbianAudio::ActiveState); |
|
459 |
|
460 TDesC8 &inputBuffer = buffer->Data(); |
|
461 |
|
462 const qint64 inputBytes = bytesReady(); |
|
463 const qint64 outputBytes = len - bytesRead; |
|
464 const qint64 copyBytes = outputBytes < inputBytes ? |
|
465 outputBytes : inputBytes; |
|
466 |
|
467 memcpy(data, inputBuffer.Ptr() + m_devSoundBufferPos, copyBytes); |
|
468 |
|
469 m_devSoundBufferPos += copyBytes; |
|
470 data += copyBytes; |
|
471 bytesRead += copyBytes; |
|
472 |
|
473 if (!bytesReady()) |
|
474 bufferEmptied(); |
|
475 } |
|
476 |
|
477 return bytesRead; |
|
478 } |
|
479 |
|
480 void QAudioInputPrivate::pullData() |
|
481 { |
|
482 Q_ASSERT_X(m_pullMode, Q_FUNC_INFO, |
|
483 "pullData called when in push mode"); |
|
484 |
|
485 CMMFDataBuffer *buffer = 0; |
|
486 while (buffer = currentBuffer()) { |
|
487 if (SymbianAudio::IdleState == m_internalState) |
|
488 setState(SymbianAudio::ActiveState); |
|
489 |
|
490 TDesC8 &inputBuffer = buffer->Data(); |
|
491 |
|
492 const qint64 inputBytes = bytesReady(); |
|
493 const qint64 bytesPushed = m_sink->write( |
|
494 (char*)inputBuffer.Ptr() + m_devSoundBufferPos, inputBytes); |
|
495 |
|
496 m_devSoundBufferPos += bytesPushed; |
|
497 |
|
498 if (!bytesReady()) |
|
499 bufferEmptied(); |
|
500 |
|
501 if (!bytesPushed) |
|
502 break; |
|
503 } |
|
504 } |
|
505 |
|
506 void QAudioInputPrivate::bufferEmptied() |
|
507 { |
|
508 m_devSoundBufferPos = 0; |
|
509 |
|
510 if (m_devSoundBuffer) { |
|
511 m_totalBytesReady -= m_devSoundBuffer->Data().Length(); |
|
512 m_devSoundBuffer = 0; |
|
513 m_devSound->RecordData(); |
|
514 } else { |
|
515 Q_ASSERT(!m_devSoundBufferQ.empty()); |
|
516 m_totalBytesReady -= m_devSoundBufferQ.front()->Data().Length(); |
|
517 m_devSoundBufferQ.erase(m_devSoundBufferQ.begin()); |
|
518 |
|
519 // If the queue has been emptied, resume transfer from the hardware |
|
520 if (m_devSoundBufferQ.empty()) |
|
521 m_devSound->RecordInitL(); |
|
522 } |
|
523 |
|
524 Q_ASSERT(m_totalBytesReady >= 0); |
|
525 } |
|
526 |
|
527 void QAudioInputPrivate::close() |
|
528 { |
|
529 m_notifyTimer->stop(); |
|
530 m_pullTimer->stop(); |
|
531 |
|
532 m_error = QAudio::NoError; |
|
533 |
|
534 if (m_devSound) |
|
535 m_devSound->Stop(); |
|
536 m_devSound.reset(); |
|
537 m_devSoundBuffer = 0; |
|
538 m_devSoundBufferSize = 0; |
|
539 m_totalBytesReady = 0; |
|
540 |
|
541 if (!m_pullMode) // m_sink is owned |
|
542 delete m_sink; |
|
543 m_pullMode = false; |
|
544 m_sink = 0; |
|
545 |
|
546 m_devSoundBufferQ.clear(); |
|
547 m_devSoundBufferPos = 0; |
|
548 m_totalSamplesRecorded = 0; |
|
549 |
|
550 setState(SymbianAudio::ClosedState); |
|
551 } |
|
552 |
|
553 qint64 QAudioInputPrivate::getSamplesRecorded() const |
|
554 { |
|
555 qint64 result = 0; |
|
556 if (m_devSound) |
|
557 result = qint64(m_devSound->SamplesRecorded()); |
|
558 return result; |
|
559 } |
|
560 |
|
561 void QAudioInputPrivate::setError(QAudio::Error error) |
|
562 { |
|
563 m_error = error; |
|
564 |
|
565 // Although no state transition actually occurs here, a stateChanged event |
|
566 // must be emitted to inform the client that the call to start() was |
|
567 // unsuccessful. |
|
568 if (QAudio::OpenError == error) |
|
569 emit stateChanged(QAudio::StoppedState); |
|
570 |
|
571 // Close the DevSound instance. This causes a transition to StoppedState. |
|
572 // This must be done asynchronously in case the current function was called |
|
573 // from a DevSound event handler, in which case deleting the DevSound |
|
574 // instance may cause an exception. |
|
575 QMetaObject::invokeMethod(this, "close", Qt::QueuedConnection); |
|
576 } |
|
577 |
|
578 void QAudioInputPrivate::setState(SymbianAudio::State newInternalState) |
|
579 { |
|
580 const QAudio::State oldExternalState = m_externalState; |
|
581 m_internalState = newInternalState; |
|
582 m_externalState = SymbianAudio::Utils::stateNativeToQt( |
|
583 m_internalState, initializingState()); |
|
584 |
|
585 if (m_externalState != oldExternalState) |
|
586 emit stateChanged(m_externalState); |
|
587 } |
|
588 |
|
589 QAudio::State QAudioInputPrivate::initializingState() const |
|
590 { |
|
591 return QAudio::IdleState; |
|
592 } |
|
593 |
|
594 QT_END_NAMESPACE |