summaryrefslogtreecommitdiffstats
path: root/src/multimedia/audio/qaudiodecoder.cpp
blob: f555f46ed2e93656677c5613c20fd8dc8f43c554 (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
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "qaudiodecoder.h"

#include <private/qaudiodecoder_p.h>
#include <private/qmultimediautils_p.h>
#include <private/qplatformaudiodecoder_p.h>
#include <private/qplatformmediaintegration_p.h>

#include <QtCore/qcoreevent.h>
#include <QtCore/qdebug.h>
#include <QtCore/qmetaobject.h>
#include <QtCore/qpointer.h>
#include <QtCore/qtimer.h>
#include <QtCore/qurl.h>

QT_BEGIN_NAMESPACE

/*!
    \class QAudioDecoder
    \brief The QAudioDecoder class implements decoding audio.
    \inmodule QtMultimedia
    \ingroup multimedia
    \ingroup multimedia_audio

    \preliminary

    The QAudioDecoder class is a high level class for decoding
    audio media files.  It is similar to the QMediaPlayer class except
    that audio is provided back through this API rather than routed
    directly to audio hardware.

    \sa QAudioBuffer
*/

/*!
    Construct an QAudioDecoder instance with \a parent.
*/
QAudioDecoder::QAudioDecoder(QObject *parent) : QObject{ *new QAudioDecoderPrivate, parent }
{
    QT6_ONLY(Q_UNUSED(unused))

    Q_D(QAudioDecoder);

    auto maybeDecoder = QPlatformMediaIntegration::instance()->createAudioDecoder(this);
    if (maybeDecoder) {
        d->decoder.reset(maybeDecoder.value());
    } else {
        qWarning() << "Failed to initialize QAudioDecoder" << maybeDecoder.error();
    }
}

/*!
    Destroys the audio decoder object.
*/
QAudioDecoder::~QAudioDecoder() = default;

/*!
    Returns true is audio decoding is supported on this platform.
*/
bool QAudioDecoder::isSupported() const
{
    Q_D(const QAudioDecoder);

    return bool(d->decoder);
}

/*!
    \property QAudioDecoder::isDecoding
    \brief \c true if the decoder is currently running and decoding audio data.
*/
bool QAudioDecoder::isDecoding() const
{
    Q_D(const QAudioDecoder);

    return d->decoder && d->decoder->isDecoding();
}

/*!

    Returns the current error state of the QAudioDecoder.
*/
QAudioDecoder::Error QAudioDecoder::error() const
{
    Q_D(const QAudioDecoder);
    return d->decoder ? d->decoder->error() : NotSupportedError;
}

/*!
    \property QAudioDecoder::error

    Returns a human readable description of the current error, or
    an empty string is there is no error.
*/
QString QAudioDecoder::errorString() const
{
    Q_D(const QAudioDecoder);
    if (!d->decoder)
        return tr("QAudioDecoder not supported.");
    return d->decoder->errorString();
}

/*!
    Starts decoding the audio resource.

    As data gets decoded, the \l bufferReady() signal will be emitted
    when enough data has been decoded.  Calling \l read() will then return
    an audio buffer without blocking.

    If you call read() before a buffer is ready, an invalid buffer will
    be returned, again without blocking.

    \sa read()
*/
void QAudioDecoder::start()
{
    Q_D(QAudioDecoder);

    if (!d->decoder)
        return;

    // Reset error conditions
    d->decoder->clearError();
    d->decoder->start();
}

/*!
    Stop decoding audio.  Calling \l start() again will resume decoding from the beginning.
*/
void QAudioDecoder::stop()
{
    Q_D(QAudioDecoder);

    if (d->decoder)
        d->decoder->stop();
}

/*!
    Returns the current file name to decode.
    If \l setSourceDevice was called, this will
    be empty.
*/
QUrl QAudioDecoder::source() const
{
    Q_D(const QAudioDecoder);
    return d->unresolvedUrl;
}

/*!
    Sets the current audio file name to \a fileName.

    When this property is set any current decoding is stopped,
    and any audio buffers are discarded.

    You can only specify either a source filename or
    a source QIODevice.  Setting one will unset the other.
*/
void QAudioDecoder::setSource(const QUrl &fileName)
{
    Q_D(QAudioDecoder);

    if (!d->decoder)
        return;

    d->decoder->clearError();
    d->unresolvedUrl = fileName;
    d->decoder->setSourceDevice(nullptr);
    QUrl url = qMediaFromUserInput(fileName);
    d->decoder->setSource(url);
}

/*!
    Returns the current source QIODevice, if one was set.
    If \l setSource() was called, this will be a nullptr.
*/
QIODevice *QAudioDecoder::sourceDevice() const
{
    Q_D(const QAudioDecoder);
    return d->decoder ? d->decoder->sourceDevice() : nullptr;
}

/*!
    Sets the current audio QIODevice to \a device.

    When this property is set any current decoding is stopped,
    and any audio buffers are discarded.

    You can only specify either a source filename or
    a source QIODevice.  Setting one will unset the other.
*/
void QAudioDecoder::setSourceDevice(QIODevice *device)
{
    Q_D(QAudioDecoder);
    if (d->decoder) {
        d->unresolvedUrl = QUrl{};
        d->decoder->setSourceDevice(device);
    }
}

/*!
    Returns the audio format the decoder is set to.

    \note This may be different than the format of the decoded
    samples, if the audio format was set to an invalid one.

    \sa setAudioFormat(), formatChanged()
*/
QAudioFormat QAudioDecoder::audioFormat() const
{
    Q_D(const QAudioDecoder);
    return d->decoder ? d->decoder->audioFormat() : QAudioFormat{};
}

/*!
    Set the desired audio format for decoded samples to \a format.

    This property can only be set while the decoder is stopped.
    Setting this property at other times will be ignored.

    If the decoder does not support this format, \l error() will
    be set to \c FormatError.

    If you do not specify a format, the format of the decoded
    audio itself will be used.  Otherwise, some format conversion
    will be applied.

    If you wish to reset the decoded format to that of the original
    audio file, you can specify an invalid \a format.

    \warning Setting a desired audio format is not yet supported
    on the Android backend. It does work with the default FFMPEG
    backend.
*/
void QAudioDecoder::setAudioFormat(const QAudioFormat &format)
{
    if (isDecoding())
        return;

    Q_D(QAudioDecoder);

    if (d->decoder)
        d->decoder->setAudioFormat(format);
}

/*!
    Returns true if a buffer is available to be read,
    and false otherwise.  If there is no buffer available, calling
    the \l read() function will return an invalid buffer.
*/
bool QAudioDecoder::bufferAvailable() const
{
    Q_D(const QAudioDecoder);
    return d->decoder && d->decoder->bufferAvailable();
}

/*!
    Returns position (in milliseconds) of the last buffer read from
    the decoder or -1 if no buffers have been read.
*/

qint64 QAudioDecoder::position() const
{
    Q_D(const QAudioDecoder);
    return d->decoder ? d->decoder->position() : -1;
}

/*!
    Returns total duration (in milliseconds) of the audio stream or -1
    if not available.
*/

qint64 QAudioDecoder::duration() const
{
    Q_D(const QAudioDecoder);
    return d->decoder ? d->decoder->duration() : -1;
}

/*!
    Read a buffer from the decoder, if one is available. Returns an invalid buffer
    if there are no decoded buffers currently available, or on failure.  In both cases
    this function will not block.

    You should either respond to the \l bufferReady() signal or check the
    \l bufferAvailable() function before calling read() to make sure
    you get useful data.
*/

QAudioBuffer QAudioDecoder::read() const
{
    Q_D(const QAudioDecoder);
    return d->decoder ? d->decoder->read() : QAudioBuffer{};
}

// Enums
/*!
    \enum QAudioDecoder::Error

    Defines a media player error condition.

    \value NoError No error has occurred.
    \value ResourceError A media resource couldn't be resolved.
    \value FormatError The format of a media resource isn't supported.
    \value AccessDeniedError There are not the appropriate permissions to play a media resource.
    \value NotSupportedError QAudioDecoder is not supported on this platform
*/

// Signals
/*!
    \fn void QAudioDecoder::error(QAudioDecoder::Error error)

    Signals that an \a error condition has occurred.

    \sa errorString()
*/

/*!
    \fn void QAudioDecoder::sourceChanged()

    Signals that the current source of the decoder has changed.

    \sa source(), sourceDevice()
*/

/*!
    \fn void QAudioDecoder::formatChanged(const QAudioFormat &format)

    Signals that the current audio format of the decoder has changed to \a format.

    \sa audioFormat(), setAudioFormat()
*/

/*!
    \fn void QAudioDecoder::bufferReady()

    Signals that a new decoded audio buffer is available to be read.

    \sa read(), bufferAvailable()
*/

/*!
    \fn void QAudioDecoder::bufferAvailableChanged(bool available)

    Signals the availability (if \a available is true) of a new buffer.

    If \a available is false, there are no buffers available.

    \sa bufferAvailable(), bufferReady()
*/

/*!
    \fn void QAudioDecoder::finished()

    Signals that the decoding has finished successfully.
    If decoding fails, error signal is emitted instead.

    \sa start(), stop(), error()
*/

/*!
    \fn void QAudioDecoder::positionChanged(qint64 position)

    Signals that the current \a position of the decoder has changed.

    \sa durationChanged()
*/

/*!
    \fn void QAudioDecoder::durationChanged(qint64 duration)

    Signals that the estimated \a duration of the decoded data has changed.

    \sa positionChanged()
*/

// Properties
/*!
    \property QAudioDecoder::source
    \brief the active filename being decoded by the decoder object.
*/

/*!
    \property QAudioDecoder::bufferAvailable
    \brief whether there is a decoded audio buffer available
*/

QT_END_NAMESPACE

#include "moc_qaudiodecoder.cpp"