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