Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qaudiodecoder.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
6
7#include <QtMultimedia/private/qmultimediautils_p.h>
8#include <QtMultimedia/private/qplatformaudiodecoder_p.h>
9#include <QtMultimedia/private/qplatformmediaintegration_p.h>
10#include <QtCore/qcoreevent.h>
11#include <QtCore/qdebug.h>
12#include <QtCore/qmetaobject.h>
13#include <QtCore/qpointer.h>
14#include <QtCore/qtimer.h>
15#include <QtCore/qurl.h>
16
18
19/*!
20 \class QAudioDecoder
21 \brief The QAudioDecoder class implements decoding audio.
22 \inmodule QtMultimedia
23 \ingroup multimedia
24 \ingroup multimedia_audio
25
26 \preliminary
27
28 The QAudioDecoder class is a high level class for decoding
29 audio media files. It is similar to the QMediaPlayer class except
30 that audio is provided back through this API rather than routed
31 directly to audio hardware.
32
33 \sa QAudioBuffer
34*/
35
36/*!
37 Construct an QAudioDecoder instance with \a parent.
38*/
39QAudioDecoder::QAudioDecoder(QObject *parent) : QObject{ *new QAudioDecoderPrivate, parent }
40{
41 Q_D(QAudioDecoder);
42
43 auto maybeDecoder = QPlatformMediaIntegration::instance()->createAudioDecoder(this);
44 if (maybeDecoder) {
45 d->decoder.reset(maybeDecoder.value());
46 } else {
47 qWarning() << "Failed to initialize QAudioDecoder" << maybeDecoder.error();
48 }
49}
50
51/*!
52 Destroys the audio decoder object.
53*/
54QAudioDecoder::~QAudioDecoder() = default;
55
56/*!
57 Returns true is audio decoding is supported on this platform.
58*/
59bool QAudioDecoder::isSupported() const
60{
61 Q_D(const QAudioDecoder);
62
63 return bool(d->decoder);
64}
65
66/*!
67 \property QAudioDecoder::isDecoding
68 \brief \c true if the decoder is currently running and decoding audio data.
69*/
70bool QAudioDecoder::isDecoding() const
71{
72 Q_D(const QAudioDecoder);
73
74 return d->decoder && d->decoder->isDecoding();
75}
76
77/*!
78
79 Returns the current error state of the QAudioDecoder.
80*/
81QAudioDecoder::Error QAudioDecoder::error() const
82{
83 Q_D(const QAudioDecoder);
84 return d->decoder ? d->decoder->error() : NotSupportedError;
85}
86
87/*!
88 \property QAudioDecoder::error
89
90 Returns a human readable description of the current error, or
91 an empty string is there is no error.
92*/
93QString QAudioDecoder::errorString() const
94{
95 Q_D(const QAudioDecoder);
96 if (!d->decoder)
97 return tr("QAudioDecoder not supported.");
98 return d->decoder->errorString();
99}
100
101/*!
102 Starts decoding the audio resource.
103
104 As data gets decoded, the \l bufferReady() signal will be emitted
105 when enough data has been decoded. Calling \l read() will then return
106 an audio buffer without blocking.
107
108 If you call read() before a buffer is ready, an invalid buffer will
109 be returned, again without blocking.
110
111 \sa read()
112*/
113void QAudioDecoder::start()
114{
115 Q_D(QAudioDecoder);
116
117 if (!d->decoder)
118 return;
119
120 // Reset error conditions
121 d->decoder->clearError();
122 d->decoder->start();
123}
124
125/*!
126 Stop decoding audio. Calling \l start() again will resume decoding from the beginning.
127*/
128void QAudioDecoder::stop()
129{
130 Q_D(QAudioDecoder);
131
132 if (d->decoder)
133 d->decoder->stop();
134}
135
136/*!
137 Returns the current file name to decode.
138 If \l setSourceDevice was called, this will
139 be empty.
140*/
141QUrl QAudioDecoder::source() const
142{
143 Q_D(const QAudioDecoder);
144 return d->unresolvedUrl;
145}
146
147/*!
148 Sets the current audio file name to \a fileName.
149
150 When this property is set any current decoding is stopped,
151 and any audio buffers are discarded.
152
153 You can only specify either a source filename or
154 a source QIODevice. Setting one will unset the other.
155*/
156void QAudioDecoder::setSource(const QUrl &fileName)
157{
158 Q_D(QAudioDecoder);
159
160 if (!d->decoder)
161 return;
162
163 d->decoder->clearError();
164 d->unresolvedUrl = fileName;
165 d->decoder->setSourceDevice(nullptr);
166 QUrl url = qMediaFromUserInput(fileName);
167 d->decoder->setSource(url);
168}
169
170/*!
171 Returns the current source QIODevice, if one was set.
172 If \l setSource() was called, this will be a nullptr.
173*/
174QIODevice *QAudioDecoder::sourceDevice() const
175{
176 Q_D(const QAudioDecoder);
177 return d->decoder ? d->decoder->sourceDevice() : nullptr;
178}
179
180/*!
181 Sets the current audio QIODevice to \a device.
182
183 When this property is set any current decoding is stopped,
184 and any audio buffers are discarded.
185
186 You can only specify either a source filename or
187 a source QIODevice. Setting one will unset the other.
188*/
189void QAudioDecoder::setSourceDevice(QIODevice *device)
190{
191 Q_D(QAudioDecoder);
192 if (d->decoder) {
193 d->unresolvedUrl = QUrl{};
194 d->decoder->setSourceDevice(device);
195 }
196}
197
198/*!
199 Returns the audio format the decoder is set to.
200
201 \note This may be different than the format of the decoded
202 samples, if the audio format was set to an invalid one.
203
204 \sa setAudioFormat(), formatChanged()
205*/
206QAudioFormat QAudioDecoder::audioFormat() const
207{
208 Q_D(const QAudioDecoder);
209 return d->decoder ? d->decoder->audioFormat() : QAudioFormat{};
210}
211
212/*!
213 Set the desired audio format for decoded samples to \a format.
214
215 This property can only be set while the decoder is stopped.
216 Setting this property at other times will be ignored.
217
218 If the decoder does not support this format, \l error() will
219 be set to \c FormatError.
220
221 If you do not specify a format, the format of the decoded
222 audio itself will be used. Otherwise, some format conversion
223 will be applied.
224
225 If you wish to reset the decoded format to that of the original
226 audio file, you can specify an invalid \a format.
227
228 \warning Setting a desired audio format is not yet supported
229 on the Android backend. It does work with the default FFMPEG
230 backend.
231*/
232void QAudioDecoder::setAudioFormat(const QAudioFormat &format)
233{
234 if (isDecoding())
235 return;
236
237 Q_D(QAudioDecoder);
238
239 if (d->decoder)
240 d->decoder->setAudioFormat(format);
241}
242
243/*!
244 Returns true if a buffer is available to be read,
245 and false otherwise. If there is no buffer available, calling
246 the \l read() function will return an invalid buffer.
247*/
248bool QAudioDecoder::bufferAvailable() const
249{
250 Q_D(const QAudioDecoder);
251 return d->decoder && d->decoder->bufferAvailable();
252}
253
254/*!
255 Returns position (in milliseconds) of the last buffer read from
256 the decoder or -1 if no buffers have been read.
257*/
258
259qint64 QAudioDecoder::position() const
260{
261 Q_D(const QAudioDecoder);
262 return d->decoder ? d->decoder->position() : -1;
263}
264
265/*!
266 Returns total duration (in milliseconds) of the audio stream or -1
267 if not available.
268*/
269
270qint64 QAudioDecoder::duration() const
271{
272 Q_D(const QAudioDecoder);
273 return d->decoder ? d->decoder->duration() : -1;
274}
275
276/*!
277 Read a buffer from the decoder, if one is available. Returns an invalid buffer
278 if there are no decoded buffers currently available, or on failure. In both cases
279 this function will not block.
280
281 You should either respond to the \l bufferReady() signal or check the
282 \l bufferAvailable() function before calling read() to make sure
283 you get useful data.
284*/
285
286QAudioBuffer QAudioDecoder::read() const
287{
288 Q_D(const QAudioDecoder);
289 return d->decoder ? d->decoder->read() : QAudioBuffer{};
290}
291
292// Enums
293/*!
294 \enum QAudioDecoder::Error
295
296 Defines a media player error condition.
297
298 \value NoError No error has occurred.
299 \value ResourceError A media resource couldn't be resolved.
300 \value FormatError The format of a media resource isn't supported.
301 \value AccessDeniedError There are not the appropriate permissions to play a media resource.
302 \value NotSupportedError QAudioDecoder is not supported on this platform
303*/
304
305// Signals
306/*!
307 \fn void QAudioDecoder::error(QAudioDecoder::Error error)
308
309 Signals that an \a error condition has occurred.
310
311 \sa errorString()
312*/
313
314/*!
315 \fn void QAudioDecoder::sourceChanged()
316
317 Signals that the current source of the decoder has changed.
318
319 \sa source(), sourceDevice()
320*/
321
322/*!
323 \fn void QAudioDecoder::formatChanged(const QAudioFormat &format)
324
325 Signals that the current audio format of the decoder has changed to \a format.
326
327 \sa audioFormat(), setAudioFormat()
328*/
329
330/*!
331 \fn void QAudioDecoder::bufferReady()
332
333 Signals that a new decoded audio buffer is available to be read.
334
335 \sa read(), bufferAvailable()
336*/
337
338/*!
339 \fn void QAudioDecoder::bufferAvailableChanged(bool available)
340
341 Signals the availability (if \a available is true) of a new buffer.
342
343 If \a available is false, there are no buffers available.
344
345 \sa bufferAvailable(), bufferReady()
346*/
347
348/*!
349 \fn void QAudioDecoder::finished()
350
351 Signals that the decoding has finished successfully.
352 If decoding fails, error signal is emitted instead.
353
354 \sa start(), stop(), error()
355*/
356
357/*!
358 \fn void QAudioDecoder::positionChanged(qint64 position)
359
360 Signals that the current \a position of the decoder has changed.
361
362 \sa durationChanged()
363*/
364
365/*!
366 \fn void QAudioDecoder::durationChanged(qint64 duration)
367
368 Signals that the estimated \a duration of the decoded data has changed.
369
370 \sa positionChanged()
371*/
372
373// Properties
374/*!
375 \property QAudioDecoder::source
376 \brief the active filename being decoded by the decoder object.
377*/
378
379/*!
380 \property QAudioDecoder::bufferAvailable
381 \brief whether there is a decoded audio buffer available
382*/
383
384QT_END_NAMESPACE
385
386#include "moc_qaudiodecoder.cpp"
Combined button and popup list for selecting options.