summaryrefslogtreecommitdiffstats
path: root/src/multimediakit/audio/qaudio_symbian_p.cpp
blob: 08f3297903bad32e339de43140f13f1aefe308b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the Qt Mobility Components.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qaudio_symbian_p.h"
#include <mmffourcc.h>

QT_BEGIN_NAMESPACE

namespace SymbianAudio {
namespace Utils {

//-----------------------------------------------------------------------------
// Static data
//-----------------------------------------------------------------------------

// Sample rate / frequency

typedef TMMFSampleRate SampleRateNative;
typedef int SampleRateQt;

const int SampleRateCount = 12;

const SampleRateNative SampleRateListNative[SampleRateCount] = {
        EMMFSampleRate8000Hz
    ,   EMMFSampleRate11025Hz
    ,   EMMFSampleRate12000Hz
    ,   EMMFSampleRate16000Hz
    ,   EMMFSampleRate22050Hz
    ,   EMMFSampleRate24000Hz
    ,   EMMFSampleRate32000Hz
    ,   EMMFSampleRate44100Hz
    ,   EMMFSampleRate48000Hz
    ,   EMMFSampleRate64000Hz
    ,   EMMFSampleRate88200Hz
    ,   EMMFSampleRate96000Hz
};

const SampleRateQt SampleRateListQt[SampleRateCount] = {
        8000
    ,   11025
    ,   12000
    ,   16000
    ,   22050
    ,   24000
    ,   32000
    ,   44100
    ,   48000
    ,   64000
    ,   88200
    ,   96000
};

// Channels

typedef TMMFMonoStereo ChannelsNative;
typedef int ChannelsQt;

const int ChannelsCount = 2;

const ChannelsNative ChannelsListNative[ChannelsCount] = {
        EMMFMono
    ,   EMMFStereo
};

const ChannelsQt ChannelsListQt[ChannelsCount] = {
        1
    ,   2
};

// Encoding

const int EncodingCount = 6;

const TUint32 EncodingFourCC[EncodingCount] = {
        KMMFFourCCCodePCM8              // 0
    ,   KMMFFourCCCodePCMU8             // 1
    ,   KMMFFourCCCodePCM16             // 2
    ,   KMMFFourCCCodePCMU16            // 3
    ,   KMMFFourCCCodePCM16B            // 4
    ,   KMMFFourCCCodePCMU16B           // 5
};

// The characterised DevSound API specification states that the iEncoding
// field in TMMFCapabilities is ignored, and that the FourCC should be used
// to specify the PCM encoding.
// See "SGL.GT0287.102 Multimedia DevSound Baseline Compatibility.doc" in the
// mm_info/mm_docs repository.
const TMMFSoundEncoding EncodingNative[EncodingCount] = {
        EMMFSoundEncoding16BitPCM       // 0
    ,   EMMFSoundEncoding16BitPCM       // 1
    ,   EMMFSoundEncoding16BitPCM       // 2
    ,   EMMFSoundEncoding16BitPCM       // 3
    ,   EMMFSoundEncoding16BitPCM       // 4
    ,   EMMFSoundEncoding16BitPCM       // 5
};


const int EncodingSampleSize[EncodingCount] = {
        8                               // 0
    ,   8                               // 1
    ,   16                              // 2
    ,   16                              // 3
    ,   16                              // 4
    ,   16                              // 5
};

const QAudioFormat::Endian EncodingByteOrder[EncodingCount] = {
        QAudioFormat::LittleEndian      // 0
    ,   QAudioFormat::LittleEndian      // 1
    ,   QAudioFormat::LittleEndian      // 2
    ,   QAudioFormat::LittleEndian      // 3
    ,   QAudioFormat::BigEndian         // 4
    ,   QAudioFormat::BigEndian         // 5
};

const QAudioFormat::SampleType EncodingSampleType[EncodingCount] = {
        QAudioFormat::SignedInt         // 0
    ,   QAudioFormat::UnSignedInt       // 1
    ,   QAudioFormat::SignedInt         // 2
    ,   QAudioFormat::UnSignedInt       // 3
    ,   QAudioFormat::SignedInt         // 4
    ,   QAudioFormat::UnSignedInt       // 5
};


//-----------------------------------------------------------------------------
// Private functions
//-----------------------------------------------------------------------------

// Helper functions for implementing parameter conversions

template<typename Input>
bool findValue(const Input *inputArray, int length, Input input, int &index) {
    bool result = false;
    for (int i=0; !result && i<length; ++i)
        if (inputArray[i] == input) {
            index = i;
            result = true;
        }
    return result;
}

template<typename Input, typename Output>
bool convertValue(const Input *inputArray, const Output *outputArray,
    int length, Input input, Output &output) {
    int index;
    const bool result = findValue<Input>(inputArray, length, input, index);
    if (result)
        output = outputArray[index];
    return result;
}

/**
 * Macro which is used to generate the implementation of the conversion
 * functions.  The implementation is just a wrapper around the templated
 * convertValue function, e.g.
 *
 * CONVERSION_FUNCTION_IMPL(SampleRate, Qt, Native)
 *
 * expands to
 *
 * bool SampleRateQtToNative(int input, TMMFSampleRate &output) {
 *      return convertValue<SampleRateQt, SampleRateNative>
 *          (SampleRateListQt, SampleRateListNative, SampleRateCount,
 *          input, output);
 * }
 */
#define CONVERSION_FUNCTION_IMPL(FieldLc, Field, Input, Output)               \
bool FieldLc##Input##To##Output(Field##Input input, Field##Output &output) {  \
    return convertValue<Field##Input, Field##Output>(Field##List##Input,      \
        Field##List##Output, Field##Count, input, output);                    \
}

//-----------------------------------------------------------------------------
// Local helper functions
//-----------------------------------------------------------------------------

CONVERSION_FUNCTION_IMPL(sampleRate, SampleRate, Qt, Native)
CONVERSION_FUNCTION_IMPL(sampleRate, SampleRate, Native, Qt)
CONVERSION_FUNCTION_IMPL(channels, Channels, Qt, Native)
CONVERSION_FUNCTION_IMPL(channels, Channels, Native, Qt)

bool sampleInfoQtToNative(int inputSampleSize,
                          QAudioFormat::Endian inputByteOrder,
                          QAudioFormat::SampleType inputSampleType,
                          TUint32 &outputFourCC,
                          TMMFSoundEncoding &outputEncoding) {

    bool found = false;

    for (int i=0; i<EncodingCount && !found; ++i) {
        if (    EncodingSampleSize[i] == inputSampleSize
            &&  EncodingByteOrder[i] == inputByteOrder
            &&  EncodingSampleType[i] == inputSampleType) {
            outputFourCC = EncodingFourCC[i];
            outputEncoding = EncodingNative[i]; // EMMFSoundEncoding16BitPCM
            found = true;
        }
    }

    return found;
}

void capabilitiesNativeToQt(const TMMFCapabilities &caps,
                            const TFourCC &fourcc,
                            QList<int> &frequencies,
                            QList<int> &channels,
                            QList<int> &sampleSizes,
                            QList<QAudioFormat::Endian> &byteOrders,
                            QList<QAudioFormat::SampleType> &sampleTypes) {

    frequencies.clear();
    sampleSizes.clear();
    byteOrders.clear();
    sampleTypes.clear();
    channels.clear();

    for (int i=0; i<SampleRateCount; ++i)
        if (caps.iRate & SampleRateListNative[i])
            frequencies += SampleRateListQt[i];

    for (int i=0; i<ChannelsCount; ++i)
        if (caps.iChannels & ChannelsListNative[i])
            channels += ChannelsListQt[i];

    for (int i=0; i<EncodingCount; ++i) {
        if (fourcc == EncodingFourCC[i]) {
            sampleSizes += EncodingSampleSize[i];
            byteOrders += EncodingByteOrder[i];
            sampleTypes += EncodingSampleType[i];
        }
    }
}

bool formatQtToNative(const QAudioFormat &inputFormat,
                      TUint32 &outputFourCC,
                      TMMFCapabilities &outputFormat) {

    bool result = false;

    // Need to use temporary variables because TMMFCapabilities fields are all
    // TInt, rather than MMF enumerated types.
    TMMFSampleRate outputSampleRate;
    TMMFMonoStereo outputChannels;
    TMMFSoundEncoding outputEncoding;

    if (inputFormat.codec() == QLatin1String("audio/pcm")) {
        result =
                sampleRateQtToNative(inputFormat.frequency(), outputSampleRate)
            &&  channelsQtToNative(inputFormat.channels(), outputChannels)
            &&  sampleInfoQtToNative(inputFormat.sampleSize(),
                                     inputFormat.byteOrder(),
                                     inputFormat.sampleType(),
                                     outputFourCC,
                                     outputEncoding);
    }

    if (result) {
        outputFormat.iRate = outputSampleRate;
        outputFormat.iChannels = outputChannels;
        outputFormat.iEncoding = outputEncoding;
    }

    return result;
}

QAudio::State stateNativeToQt(State nativeState)
{
    switch (nativeState) {
    case ClosedState:
        return QAudio::StoppedState;
    case InitializingState:
        return QAudio::StoppedState;
    case ActiveState:
        return QAudio::ActiveState;
    case IdleState:
        return QAudio::IdleState;
    case SuspendedPausedState:
    case SuspendedStoppedState:
        return QAudio::SuspendedState;
    default:
        Q_ASSERT_X(false, Q_FUNC_INFO, "Invalid state");
        return QAudio::StoppedState; // suppress compiler warning
    }
}

qint64 bytesToSamples(const QAudioFormat &format, qint64 length)
{
    return length / ((format.sampleSize() / 8) * format.channels());
}

qint64 samplesToBytes(const QAudioFormat &format, qint64 samples)
{
    return samples * (format.sampleSize() / 8) * format.channels();
}

} // namespace Utils


//-----------------------------------------------------------------------------
// DevSoundWrapper
//-----------------------------------------------------------------------------

DevSoundWrapper::DevSoundWrapper(QAudio::Mode mode, QObject *parent)
    :   QObject(parent)
    ,   m_mode(mode)
    ,   m_state(StateIdle)
    ,   m_devsound(0)
    ,   m_fourcc(0)
{
    QT_TRAP_THROWING(m_devsound = CMMFDevSound::NewL());

    switch (mode) {
    case QAudio::AudioOutput:
        m_nativeMode = EMMFStatePlaying;
        break;

    case QAudio::AudioInput:
        m_nativeMode = EMMFStateRecording;
        break;

    default:
        Q_ASSERT_X(false, Q_FUNC_INFO, "Invalid mode");
    }

    getSupportedCodecs();
}

DevSoundWrapper::~DevSoundWrapper()
{
    delete m_devsound;
}

const QList<QString>& DevSoundWrapper::supportedCodecs() const
{
    return m_supportedCodecs;
}

void DevSoundWrapper::initialize(const QString& codec)
{
    Q_ASSERT(StateInitializing != m_state);
    m_state = StateInitializing;
    if (QLatin1String("audio/pcm") == codec) {
        m_fourcc = KMMFFourCCCodePCM16;
        TRAPD(err, m_devsound->InitializeL(*this, m_fourcc, m_nativeMode));
        if (KErrNone != err) {
            m_state = StateIdle;
            emit initializeComplete(err);
        }
    } else {
        emit initializeComplete(KErrNotSupported);
    }
}

const QList<int>& DevSoundWrapper::supportedFrequencies() const
{
    Q_ASSERT(StateInitialized == m_state);
    return m_supportedFrequencies;
}

const QList<int>& DevSoundWrapper::supportedChannels() const
{
    Q_ASSERT(StateInitialized == m_state);
    return m_supportedChannels;
}

const QList<int>& DevSoundWrapper::supportedSampleSizes() const
{
    Q_ASSERT(StateInitialized == m_state);
    return m_supportedSampleSizes;
}

const QList<QAudioFormat::Endian>& DevSoundWrapper::supportedByteOrders() const
{
    Q_ASSERT(StateInitialized == m_state);
    return m_supportedByteOrders;
}

const QList<QAudioFormat::SampleType>& DevSoundWrapper::supportedSampleTypes() const
{
    Q_ASSERT(StateInitialized == m_state);
    return m_supportedSampleTypes;
}

bool DevSoundWrapper::isFormatSupported(const QAudioFormat &format) const
{
    Q_ASSERT(StateInitialized == m_state);
    return m_supportedCodecs.contains(format.codec())
        && m_supportedFrequencies.contains(format.frequency())
        && m_supportedChannels.contains(format.channels())
        && m_supportedSampleSizes.contains(format.sampleSize())
        && m_supportedSampleTypes.contains(format.sampleType())
        && m_supportedByteOrders.contains(format.byteOrder());
}

int DevSoundWrapper::samplesProcessed() const
{
    int result = 0;
    if (StateInitialized == m_state) {
        switch (m_mode) {
        case QAudio::AudioInput:
            result = m_devsound->SamplesRecorded();
            break;
        case QAudio::AudioOutput:
            result = m_devsound->SamplesPlayed();
            break;
        }
    }
    return result;
}

bool DevSoundWrapper::setFormat(const QAudioFormat &format)
{
    Q_ASSERT(StateInitialized == m_state);
    bool result = false;
    TUint32 fourcc;
    TMMFCapabilities nativeFormat;
    if (Utils::formatQtToNative(format, fourcc, nativeFormat)) {
        TMMFCapabilities currentNativeFormat = m_devsound->Config();
        nativeFormat.iBufferSize = currentNativeFormat.iBufferSize;
        TRAPD(err, m_devsound->SetConfigL(nativeFormat));
        result = (KErrNone == err);
    }
    return result;
}

bool DevSoundWrapper::start()
{
    Q_ASSERT(StateInitialized == m_state);
    int err = KErrArgument;
    switch (m_mode) {
    case QAudio::AudioInput:
        TRAP(err, m_devsound->RecordInitL());
        break;
    case QAudio::AudioOutput:
        TRAP(err, m_devsound->PlayInitL());
        break;
    }
    return (KErrNone == err);
}

bool DevSoundWrapper::pause()
{
    Q_ASSERT(StateInitialized == m_state);
#ifndef PRE_S60_52_PLATFORM
    if (m_mode == QAudio::AudioOutput ) {
        m_devsound->Pause();
        return true;
    } else {
        const bool canPause = isResumeSupported();
        if (canPause)
            m_devsound->Pause();
        else
            stop();
        return canPause;
    }
#else
    const bool canPause = isResumeSupported();
    if (canPause)
        m_devsound->Pause();
    else
        stop();
    return canPause;
#endif
}

void DevSoundWrapper::resume()
{
    Q_ASSERT(StateInitialized == m_state);
    Q_ASSERT(isResumeSupported());
    // TODO: QTBUG-13625
}

void DevSoundWrapper::stop()
{
    m_devsound->Stop();
}

void DevSoundWrapper::bufferProcessed()
{
    Q_ASSERT(StateInitialized == m_state);
    switch (m_mode) {
    case QAudio::AudioInput:
        m_devsound->RecordData();
        break;
    case QAudio::AudioOutput:
        m_devsound->PlayData();
        break;
    }
}

void DevSoundWrapper::getSupportedCodecs()
{
/*
 * TODO: once we support formats other than PCM, this function should
 * convert the array of FourCC codes into MIME types for each codec.
 *
    RArray<TFourCC> fourcc;
    QT_TRAP_THROWING(CleanupClosePushL(&fourcc));

    TMMFPrioritySettings settings;
    switch (mode) {
    case QAudio::AudioOutput:
        settings.iState = EMMFStatePlaying;
        m_devsound->GetSupportedInputDataTypesL(fourcc, settings);
        break;

    case QAudio::AudioInput:
        settings.iState = EMMFStateRecording;
        m_devsound->GetSupportedInputDataTypesL(fourcc, settings);
        break;

    default:
        Q_ASSERT_X(false, Q_FUNC_INFO, "Invalid mode");
    }

    CleanupStack::PopAndDestroy(); // fourcc
*/

    m_supportedCodecs.append(QLatin1String("audio/pcm"));
}

void DevSoundWrapper::populateCapabilities()
{
    m_supportedFrequencies.clear();
    m_supportedChannels.clear();
    m_supportedSampleSizes.clear();
    m_supportedByteOrders.clear();
    m_supportedSampleTypes.clear();

    const TMMFCapabilities caps = m_devsound->Capabilities();

    for (int i=0; i<Utils::SampleRateCount; ++i)
        if (caps.iRate & Utils::SampleRateListNative[i])
            m_supportedFrequencies += Utils::SampleRateListQt[i];

    for (int i=0; i<Utils::ChannelsCount; ++i)
        if (caps.iChannels & Utils::ChannelsListNative[i])
            m_supportedChannels += Utils::ChannelsListQt[i];

    for (int i=0; i<Utils::EncodingCount; ++i) {
        if (m_fourcc == Utils::EncodingFourCC[i]) {
            m_supportedSampleSizes += Utils::EncodingSampleSize[i];
            m_supportedByteOrders += Utils::EncodingByteOrder[i];
            m_supportedSampleTypes += Utils::EncodingSampleType[i];
        }
    }
}

bool DevSoundWrapper::isResumeSupported() const
{
    // TODO: QTBUG-13625
    return false;
}

void DevSoundWrapper::InitializeComplete(TInt aError)
{
    Q_ASSERT(StateInitializing == m_state);
    if (KErrNone == aError) {
        m_state = StateInitialized;
        populateCapabilities();
    } else {
        m_state = StateIdle;
    }
    emit initializeComplete(aError);
}

void DevSoundWrapper::ToneFinished(TInt aError)
{
    Q_UNUSED(aError)
    // This class doesn't use DevSound's tone playback functions, so should
    // never receive this callback.
    Q_ASSERT_X(false, Q_FUNC_INFO, "Unexpected callback");
}

void DevSoundWrapper::BufferToBeFilled(CMMFBuffer *aBuffer)
{
    Q_ASSERT(QAudio::AudioOutput == m_mode);
    emit bufferToBeProcessed(aBuffer);
}

void DevSoundWrapper::PlayError(TInt aError)
{
    Q_ASSERT(QAudio::AudioOutput == m_mode);
    emit processingError(aError);
}

void DevSoundWrapper::BufferToBeEmptied(CMMFBuffer *aBuffer)
{
    Q_ASSERT(QAudio::AudioInput == m_mode);
    emit bufferToBeProcessed(aBuffer);
}

void DevSoundWrapper::RecordError(TInt aError)
{
    Q_ASSERT(QAudio::AudioInput == m_mode);
    emit processingError(aError);
}

void DevSoundWrapper::ConvertError(TInt aError)
{
    Q_UNUSED(aError)
    // This class doesn't use DevSound's format conversion functions, so
    // should never receive this callback.
    Q_ASSERT_X(false, Q_FUNC_INFO, "Unexpected callback");
}

void DevSoundWrapper::DeviceMessage(TUid aMessageType, const TDesC8 &aMsg)
{
    Q_UNUSED(aMessageType)
    Q_UNUSED(aMsg)
    // Ignore this callback.
}

#ifndef PRE_S60_52_PLATFORM
int DevSoundWrapper::flush()
{
    return m_devsound->EmptyBuffers();
}
#endif
} // namespace SymbianAudio

QT_END_NAMESPACE