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
qwasmaudiodecoder_p.h
Go to the documentation of this file.
1// Copyright (C) 2026 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
4#ifndef QWASMAUDIODECODER_P_H
5#define QWASMAUDIODECODER_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <private/qplatformaudiodecoder_p.h>
19#include <QtMultimedia/private/qaudio_parsing_support_p.h>
20#include <QtCore/qqueue.h>
21#include <QtCore/qtimer.h>
22#include <QtCore/qurl.h>
23#include <QtMultimedia/qaudiobuffer.h>
24
25#include <emscripten/val.h>
26#include <memory>
27
28QT_BEGIN_NAMESPACE
29
30class QWasmAudioDecoder final : public QPlatformAudioDecoder
31{
32 Q_OBJECT
33
34public:
35 explicit QWasmAudioDecoder(QAudioDecoder *parent);
36 ~QWasmAudioDecoder() override;
37
38 QUrl source() const override;
39 void setSource(const QUrl &url) override;
40 QIODevice *sourceDevice() const override;
41 void setSourceDevice(QIODevice *device) override;
42 void start() override;
43 void stop() override;
44 QAudioFormat audioFormat() const override;
45 void setAudioFormat(const QAudioFormat &format) override;
46 QAudioBuffer read() override;
47 bool canReadQrc() const override { return true; }
48
49private:
50 using Codec = QtMultimediaPrivate::ParsingSupport::AudioCodec;
51
52 void feedChunk(const QByteArray &chunk);
53 void feedAvailableFrames(bool endOfInput);
54 void beginDecoding(Codec codec, const QByteArray &rawHeader);
55 void onSourceReadyRead();
56 void onSourceDone();
57 void startFlush();
58 void deliverOutputs();
59 void tryDecodeAudioData();
60 QString decorateWithFallbackReason(const QString &message) const;
61 void closeDecoder();
62 void resetDecodingState();
63 void handleError(QAudioDecoder::Error code, const QString &message);
64
65 QUrl m_source;
66 QIODevice *m_sourceDevice = nullptr;
67 QAudioFormat m_audioFormat;
68 QQueue<QAudioBuffer> m_pendingBuffers;
69 emscripten::val m_decoder = emscripten::val::undefined();
70 const std::shared_ptr<int> m_generationPtr = std::make_shared<int>(0);
71 QByteArray m_inputBuffer;
72 QByteArray m_rawData;
73 QString m_fallbackReason;
74 // Drains decoded outputs while the decoder runs, so buffers reach the
75 // caller as they are decoded instead of all at once when flush() resolves.
76 QTimer m_outputDrainTimer;
77 Codec m_codec = Codec::Unknown;
78 double m_byteOffset = 0.0;
79 // Total PCM frames delivered so far, used to derive each buffer's
80 // presentation time. The timestamps fed to EncodedAudioChunk are synthetic
81 // byte offsets, so the value that comes back on the AudioData is not a
82 // usable presentation time.
83 qint64 m_decodedFrames = 0;
84 qsizetype m_inputPos = 0;
85 bool m_checkingSupport = false;
86 bool m_configured = false;
87 bool m_flushing = false;
88 bool m_formatEmitted = false;
89 bool m_useDecodeAudioData = false;
90 // Set while deliverOutputs() is running, so a nested event loop started
91 // from a bufferReady() slot cannot process the same outputs twice.
92 bool m_deliveringOutputs = false;
93 // FLAC only: whether m_inputPos has been moved past the container header
94 // and metadata blocks, which must not be scanned for frame sync codes.
95 bool m_flacHeaderSkipped = false;
96};
97
98QT_END_NAMESPACE
99
100#endif // QWASMAUDIODECODER_P_H