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
qdrawavaudiodecoder_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 QDRAWAVAUDIODECODER_P_H
5#define QDRAWAVAUDIODECODER_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 <QtMultimedia/private/qplatformaudiodecoder_p.h>
19#include <QtCore/qbytearray.h>
20#include <QtCore/qspan.h>
21#include <QtCore/qurl.h>
22#include <QtCore/private/qexpected_p.h>
23
24#include <algorithm>
25#include <atomic>
26#include <cstdint>
27#include <memory>
28#include <new>
29#include <optional>
30#include <type_traits>
31
32QT_BEGIN_NAMESPACE
33
34class QAudioDecoder;
35class QIODevice;
36
37namespace QtMultimediaPrivate {
38
41 const QAudioFormat &requestedFormat);
42
43#ifdef __cpp_concepts
44template <typename Reader>
47#endif
48
49#ifdef __cpp_concepts
50template <FrameReader Reader>
51#else
52template <typename Reader>
53#endif
54std::optional<QByteArray> readFramesInChunks(uint64_t declaredFrameCount, size_t bytesPerFrame,
55 Reader &&readFrames)
56{
57 constexpr size_t chunkBytes = 10 * 1024 * 1024; // 10 MB
58 if (bytesPerFrame == 0)
59 return std::nullopt;
60
61 const uint64_t chunkFrames = std::max<uint64_t>(1, chunkBytes / bytesPerFrame);
62
63 QT_TRY
64 {
65 QByteArray result;
66 uint64_t totalFramesRead = 0;
67 while (totalFramesRead < declaredFrameCount) {
68 const uint64_t framesToRead =
69 std::min(chunkFrames, declaredFrameCount - totalFramesRead);
70 const uint64_t bytesToRead = framesToRead * bytesPerFrame;
71 const qsizetype oldSize = result.size();
72 if (bytesToRead > uint64_t(QByteArray::maxSize() - oldSize))
73 return std::nullopt;
74
75 result.resizeForOverwrite(oldSize + qsizetype(bytesToRead));
76 const uint64_t framesRead = readFrames(framesToRead, result.data() + oldSize);
77 Q_ASSERT(framesRead <= framesToRead);
78 result.resize(oldSize + qsizetype(framesRead * bytesPerFrame));
79
80 totalFramesRead += framesRead;
81 if (framesRead < framesToRead)
82 break;
83 }
84 if (totalFramesRead != declaredFrameCount)
85 return std::nullopt;
86 return result;
87 }
88 QT_CATCH(const std::bad_alloc &)
89 {
90 return std::nullopt;
91 }
92}
93
94} // namespace QtMultimediaPrivate
95
96class QDrWavAudioDecoder final : public QPlatformAudioDecoder
97{
98public:
99 explicit QDrWavAudioDecoder(QAudioDecoder *parent);
101
102 // Source management
103 QUrl source() const override { return m_source; }
104 void setSource(const QUrl &) override;
105
106 QIODevice *sourceDevice() const override { return m_sourceDevice; }
107 void setSourceDevice(QIODevice *) override;
108
109 // Lifecycle
110 void start() override;
111 void stop() override;
112
113 // Format
114 QAudioFormat audioFormat() const override { return m_audioFormat; }
115 void setAudioFormat(const QAudioFormat &) override;
116
117 // Buffer pull model
118 QAudioBuffer read() override;
119
120 // Metadata
121 bool canReadQrc() const override { return true; }
122
123private:
124 using QDrWavDecodeResult = QtMultimediaPrivate::QDrWavDecodeResult;
125
126 QDrWavDecodeResult loadAndDecodeFile(const QUrl &, QIODevice *, const QAudioFormat &,
127 std::shared_ptr<std::atomic_bool> decodingStopped);
128
129 // Handle decode completion
130 void onDecodeFinished(QDrWavDecodeResult, const std::atomic_bool &decodingStopped);
131
132 QUrl m_source;
133 QIODevice *m_sourceDevice = nullptr;
134 QAudioFormat m_audioFormat;
135
136 // Decoded result
137 QAudioBuffer m_buffer;
138
139 std::shared_ptr<std::atomic_bool> m_decodingStopped;
140};
141
142QT_END_NAMESPACE
143
144#endif // QDRAWAVAUDIODECODER_P_H
bool canReadQrc() const override
QAudioFormat audioFormat() const override
void setSource(const QUrl &) override
void setAudioFormat(const QAudioFormat &) override
QAudioBuffer read() override
QIODevice * sourceDevice() const override
QUrl source() const override
void setSourceDevice(QIODevice *) override
QDrWavAudioDecoder(QAudioDecoder *parent)
Combined button and popup list for selecting options.
static QDrWavDecodeResult makeResourceError(const QString &message)
std::optional< QByteArray > readFramesInChunks(uint64_t declaredFrameCount, size_t bytesPerFrame, Reader &&readFrames)
static QDrWavDecodeResult makeFormatError(const QString &message)
QDrWavDecodeResult loadWaveAndDecodeData(QSpan< const std::byte > data, const QAudioFormat &requestedFormat)