summaryrefslogtreecommitdiffstats
path: root/src/multimedia/qnx/qqnxaudiosource.cpp
blob: 1726a82d8fdc72fff79745b6acdfaf9bb3e6afc3 (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
// Copyright (C) 2016 Research In Motion
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "qqnxaudiosource_p.h"

#include <private/qaudiohelpers_p.h>

#include <QDebug>

QT_BEGIN_NAMESPACE

QQnxAudioSource::QQnxAudioSource(const QAudioDevice &deviceInfo, QObject *parent)
    : QPlatformAudioSource(parent)
    , m_audioSource(0)
    , m_pcmNotifier(0)
    , m_error(QAudio::NoError)
    , m_state(QAudio::StoppedState)
    , m_bytesRead(0)
    , m_elapsedTimeOffset(0)
    , m_totalTimeValue(0)
    , m_volume(qreal(1.0f))
    , m_bytesAvailable(0)
    , m_bufferSize(0)
    , m_periodSize(0)
    , m_deviceInfo(deviceInfo)
    , m_pullMode(true)
{
}

QQnxAudioSource::~QQnxAudioSource()
{
    close();
}

void QQnxAudioSource::start(QIODevice *device)
{
    if (m_state != QAudio::StoppedState)
        close();

    if (!m_pullMode && m_audioSource)
        delete m_audioSource;

    m_pullMode = true;
    m_audioSource = device;

    if (open())
        changeState(QAudio::ActiveState, QAudio::NoError);
    else
        changeState(QAudio::StoppedState, QAudio::OpenError);
}

QIODevice *QQnxAudioSource::start()
{
    if (m_state != QAudio::StoppedState)
        close();

    if (!m_pullMode && m_audioSource)
        delete m_audioSource;

    m_pullMode = false;
    m_audioSource = new InputPrivate(this);
    m_audioSource->open(QIODevice::ReadOnly | QIODevice::Unbuffered);

    if (open()) {
        changeState(QAudio::IdleState, QAudio::NoError);
    } else {
        delete m_audioSource;
        m_audioSource = 0;

        changeState(QAudio::StoppedState, QAudio::OpenError);
    }

    return m_audioSource;
}

void QQnxAudioSource::stop()
{
    if (m_state == QAudio::StoppedState)
        return;

    changeState(QAudio::StoppedState, QAudio::NoError);
    close();
}

void QQnxAudioSource::reset()
{
    stop();
    m_bytesAvailable = 0;
}

void QQnxAudioSource::suspend()
{
    if (m_state == QAudio::StoppedState)
        return;

    snd_pcm_capture_pause(m_pcmHandle.get());

    if (m_pcmNotifier)
        m_pcmNotifier->setEnabled(false);

    changeState(QAudio::SuspendedState, QAudio::NoError);
}

void QQnxAudioSource::resume()
{
    if (m_state == QAudio::StoppedState)
        return;

    snd_pcm_capture_resume(m_pcmHandle.get());

    if (m_pcmNotifier)
        m_pcmNotifier->setEnabled(true);

    if (m_pullMode)
        changeState(QAudio::ActiveState, QAudio::NoError);
    else
        changeState(QAudio::IdleState, QAudio::NoError);
}

qsizetype QQnxAudioSource::bytesReady() const
{
    return qMax(m_bytesAvailable, 0);
}

void QQnxAudioSource::setBufferSize(qsizetype bufferSize)
{
    m_bufferSize = bufferSize;
}

qsizetype QQnxAudioSource::bufferSize() const
{
    return m_bufferSize;
}

qint64 QQnxAudioSource::processedUSecs() const
{
    return qint64(1000000) * m_format.framesForBytes(m_bytesRead) / m_format.sampleRate();
}

QAudio::Error QQnxAudioSource::error() const
{
    return m_error;
}

QAudio::State QQnxAudioSource::state() const
{
    return m_state;
}

void QQnxAudioSource::setFormat(const QAudioFormat &format)
{
    if (m_state == QAudio::StoppedState)
        m_format = format;
}

QAudioFormat QQnxAudioSource::format() const
{
    return m_format;
}

void QQnxAudioSource::setVolume(qreal volume)
{
    m_volume = qBound(qreal(0.0), volume, qreal(1.0));
}

qreal QQnxAudioSource::volume() const
{
    return m_volume;
}

void QQnxAudioSource::userFeed()
{
    if (m_state == QAudio::StoppedState || m_state == QAudio::SuspendedState)
        return;

    deviceReady();
}

bool QQnxAudioSource::deviceReady()
{
    if (m_pullMode) {
        // reads some audio data and writes it to QIODevice
        read(0, 0);
    } else {
        m_bytesAvailable = m_periodSize;

        // emits readyRead() so user will call read() on QIODevice to get some audio data
        if (m_audioSource != 0) {
            InputPrivate *input = qobject_cast<InputPrivate*>(m_audioSource);
            input->trigger();
        }
    }

    if (m_state != QAudio::ActiveState)
        return true;

    return true;
}

bool QQnxAudioSource::open()
{
    if (!m_format.isValid() || m_format.sampleRate() <= 0) {
        if (!m_format.isValid())
            qWarning("QQnxAudioSource: open error, invalid format.");
        else
            qWarning("QQnxAudioSource: open error, invalid sample rate (%d).", m_format.sampleRate());

        return false;
    }

    m_pcmHandle = QnxAudioUtils::openPcmDevice(m_deviceInfo.id(), QAudioDevice::Input);

    if (!m_pcmHandle)
        return false;

    int errorCode = 0;

    // Necessary so that bytesFree() which uses the "free" member of the status struct works
    snd_pcm_plugin_set_disable(m_pcmHandle.get(), PLUGIN_MMAP);

    const std::optional<snd_pcm_channel_info_t> info = QnxAudioUtils::pcmChannelInfo(
            m_pcmHandle.get(), QAudioDevice::Input);

    if (!info) {
        close();
        return false;
    }

    snd_pcm_channel_params_t params = QnxAudioUtils::formatToChannelParams(m_format,
            QAudioDevice::Input, info->max_fragment_size);

    if ((errorCode = snd_pcm_plugin_params(m_pcmHandle.get(), &params)) < 0) {
        qWarning("QQnxAudioSource: open error, couldn't set channel params (0x%x)", -errorCode);
        close();
        return false;
    }

    if ((errorCode = snd_pcm_plugin_prepare(m_pcmHandle.get(), SND_PCM_CHANNEL_CAPTURE)) < 0) {
        qWarning("QQnxAudioSource: open error, couldn't prepare channel (0x%x)", -errorCode);
        close();
        return false;
    }

    const std::optional<snd_pcm_channel_setup_t> setup = QnxAudioUtils::pcmChannelSetup(
            m_pcmHandle.get(), QAudioDevice::Input);

    m_periodSize = qMin(2048, setup->buf.block.frag_size);

    m_elapsedTimeOffset = 0;
    m_totalTimeValue = 0;
    m_bytesRead = 0;

    m_pcmNotifier = new QSocketNotifier(snd_pcm_file_descriptor(m_pcmHandle.get(), SND_PCM_CHANNEL_CAPTURE),
                                        QSocketNotifier::Read, this);
    connect(m_pcmNotifier, SIGNAL(activated(QSocketDescriptor)), SLOT(userFeed()));

    return true;
}

void QQnxAudioSource::close()
{
    if (m_pcmHandle) {
#if SND_PCM_VERSION < SND_PROTOCOL_VERSION('P',3,0,2)
        snd_pcm_plugin_flush(m_pcmHandle.get(), SND_PCM_CHANNEL_CAPTURE);
#else
        snd_pcm_plugin_drop(m_pcmHandle.get(), SND_PCM_CHANNEL_CAPTURE);
#endif
        m_pcmHandle = nullptr;
    }

    if (m_pcmNotifier) {
        delete m_pcmNotifier;
        m_pcmNotifier = 0;
    }

    if (!m_pullMode && m_audioSource) {
        delete m_audioSource;
        m_audioSource = 0;
    }
}

qint64 QQnxAudioSource::read(char *data, qint64 len)
{
    if (!m_pullMode && m_bytesAvailable == 0)
        return 0;

    int errorCode = 0;
    QByteArray tempBuffer(m_periodSize, 0);

    const int actualRead = snd_pcm_plugin_read(m_pcmHandle.get(), tempBuffer.data(), m_periodSize);
    if (actualRead < 1) {
        snd_pcm_channel_status_t status;
        memset(&status, 0, sizeof(status));
        status.channel = SND_PCM_CHANNEL_CAPTURE;
        if ((errorCode = snd_pcm_plugin_status(m_pcmHandle.get(), &status)) < 0) {
            qWarning("QQnxAudioSource: read error, couldn't get plugin status (0x%x)", -errorCode);
            close();
            changeState(QAudio::StoppedState, QAudio::FatalError);
            return -1;
        }

        if (status.status == SND_PCM_STATUS_READY
            || status.status == SND_PCM_STATUS_OVERRUN) {
            if ((errorCode = snd_pcm_plugin_prepare(m_pcmHandle.get(), SND_PCM_CHANNEL_CAPTURE)) < 0) {
                qWarning("QQnxAudioSource: read error, couldn't prepare plugin (0x%x)", -errorCode);
                close();
                changeState(QAudio::StoppedState, QAudio::FatalError);
                return -1;
            }
        }
    } else {
        changeState(QAudio::ActiveState, QAudio::NoError);
    }

    if (m_volume < 1.0f)
        QAudioHelperInternal::qMultiplySamples(m_volume, m_format, tempBuffer.data(), tempBuffer.data(), actualRead);

    m_bytesRead += actualRead;

    if (m_pullMode) {
        m_audioSource->write(tempBuffer.data(), actualRead);
    } else {
        memcpy(data, tempBuffer.data(), qMin(static_cast<qint64>(actualRead), len));
    }

    m_bytesAvailable = 0;

    return actualRead;
}

void QQnxAudioSource::changeState(QAudio::State state, QAudio::Error error)
{
    if (m_state != state) {
        m_state = state;
        emit stateChanged(state);
    }

    if (m_error != error) {
        m_error = error;
        emit errorChanged(error);
    }
}

InputPrivate::InputPrivate(QQnxAudioSource *audio)
    : m_audioDevice(audio)
{
}

qint64 InputPrivate::readData(char *data, qint64 len)
{
    return m_audioDevice->read(data, len);
}

qint64 InputPrivate::writeData(const char *data, qint64 len)
{
    Q_UNUSED(data);
    Q_UNUSED(len);
    return 0;
}

qint64 InputPrivate::bytesAvailable() const
{
    return m_audioDevice->m_bytesAvailable + QIODevice::bytesAvailable();
}

bool InputPrivate::isSequential() const
{
    return true;
}

void InputPrivate::trigger()
{
    emit readyRead();
}

QT_END_NAMESPACE