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
qwasmwebaudiosink_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 QWASMWEBAUDIOSINK_P_H
5#define QWASMWEBAUDIOSINK_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/qaudiosystem_p.h>
19#include <QHash>
20
21#include <emscripten/val.h>
22
23#include <atomic>
24#include <optional>
25#include <string>
26
27QT_BEGIN_NAMESPACE
28
29class QWasmAudioSinkDevice;
30
32{
34
35public:
38
39 void start(QIODevice *device) override;
40 QIODevice *start() override;
41 void start(AudioCallback &&callback) override;
42 bool hasCallbackAPI() override { return true; }
43 void stop() override;
44 void reset() override;
45 void suspend() override;
46 void resume() override;
47 qsizetype bytesFree() const override;
48 void setBufferSize(qsizetype value) override;
49 qsizetype bufferSize() const override;
50 qint64 processedUSecs() const override;
51 QAudio::State state() const override;
52 void setVolume(float volume) override;
53 void setError(QAudio::Error) override;
54
55 static void workletReadyCallback(int callbackId);
56 static void deliverDataCallback(int callbackId);
57
58private:
59 void start(bool pullMode);
60 void deliverData();
61 void connectWorklet();
62 void writeToRingBuffer(const char *data, int bytes);
63 void teardownPipeline();
64
65#if !QT_CONFIG(thread)
66 void deliverToWorklet();
67#endif
68
69 static int s_nextId;
70 int m_callbackId = 0;
71
72 emscripten::val m_audioContext = emscripten::val::undefined();
73#if QT_CONFIG(thread)
75#endif
76
77 std::atomic<quint64> m_processed{0};
78 bool m_workletModuleLoaded = false;
79
80 emscripten::val m_workletNode = emscripten::val::undefined();
81
82 // Ring buffer: main thread writes, worklet reads (threaded) or
83 // deliverToWorklet() reads and posts (single-threaded).
84 QByteArray m_ringBuffer;
85 std::atomic<int> m_writePos{0};
86 std::atomic<int> m_readPos{0};
87 std::atomic<float> m_volumeAtomic{1.0f};
88
89 QIODevice *m_device = nullptr;
90 bool m_pullMode = false;
91 bool m_running = false;
92 bool m_suspended = false;
93 std::optional<AudioCallback> m_audioCallback;
94 qsizetype m_bufferSize = 0;
95 std::string m_deviceId;
96
98};
99
100QT_END_NAMESPACE
101
102#endif // QWASMWEBAUDIOSINK_P_H
The QAudioDevice class provides an information about audio devices and their functionality.
The QCameraDevice class provides general information about camera devices.
QPlatformAudioSink * createAudioSink(const QAudioDevice &, const QAudioFormat &, QObject *parent) override
QList< QAudioDevice > findAudioOutputs() const override
QList< QAudioDevice > findAudioInputs() const override
QPlatformAudioSource * createAudioSource(const QAudioDevice &, const QAudioFormat &, QObject *parent) override
void connectNotify(const QMetaMethod &signal) override
QLatin1String backendName() const override
void resume() override
void stop() override
void suspend() override
QIODevice * start() override
void start(AudioCallback &&callback) override
QAudio::State state() const override
qsizetype bufferSize() const override
void setBufferSize(qsizetype value) override
qsizetype bytesFree() const override
void setVolume(float volume) override
qint64 processedUSecs() const override
static void deliverDataCallback(int callbackId)
bool hasCallbackAPI() override
void reset() override
static void workletReadyCallback(int callbackId)
static void workletReadyCallback(int callbackId)
qint64 processedUSecs() const override
QAudio::State state() const override
QIODevice * start() override
qsizetype bufferSize() const override
void setBufferSize(qsizetype value) override
void start(QIODevice *device) override
void setVolume(float volume) override
qint64 readFromBuffer(char *data, qint64 maxlen)
static void audioDataCallback(int callbackId)
qsizetype bytesReady() const override
void connectNotify(const QMetaMethod &signal) override
QList< QCameraDevice > findVideoInputs() const override
QList< QCameraDevice > videoInputs() const
static QWasmMediaDevices * instance()
QList< QAudioDevice > audioOutputs() const
QList< QAudioDevice > audioInputs() const
State
Definition qaudio.h:32
Error
Definition qaudio.h:23
Combined button and popup list for selecting options.
EM_JS(void, setupAudioOutputSelector,(), { const overlay=document.createElement('div');overlay.style.cssText='position:fixed;top:0;left:0;width:100%;height:100%;background:rgba(0, 0, 0, 0.5);z-index:9999;display:flex;align-items:center;justify-content:center;';const dialog=document.createElement('div');dialog.style.cssText='background:white;padding:24px;border-radius:8px;text-align:center;font-family:sans-serif;min-width:240px;';const message=document.createElement('p');message.textContent='Select an audio output device to continue.';message.style.cssText='margin:0 0 16px 0;font-size:14px;';const button=document.createElement('button');button.textContent='Select Audio Output';button.style.cssText='padding:8px 16px;font-size:14px;cursor:pointer;';button.addEventListener('click', async()=> { document.body.removeChild(overlay);try { const deviceInfo=await navigator.mediaDevices.selectAudioOutput();console.log("Selected device: ", deviceInfo.label);Module._qtMediaDevicesOnAudioOutputSelected();} catch(err) { console.error(err);} }, { once:true });dialog.appendChild(message);dialog.appendChild(button);overlay.appendChild(dialog);document.body.appendChild(overlay);})
EMSCRIPTEN_KEEPALIVE void qtMediaDevicesOnAudioOutputSelected()
bool isFirefox()