Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qwasmaudioinput.cpp
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR
3// GPL-2.0-only OR GPL-3.0-only
4
5#include "qwasmaudioinput_p.h"
6
7#include <qaudioinput.h>
8#include <private/qstdweb_p.h>
9
11
12Q_LOGGING_CATEGORY(qWasmAudioInput, "qt.multimedia.wasm.audioinput")
13
16{
17 m_wasMuted = false;
18 setDeviceSourceStream("");
19}
20
22{
23}
24
26{
27 qCDebug(qWasmAudioInput) << Q_FUNC_INFO << muted;
28 if (muted == m_wasMuted)
29 return;
30 if (m_mediaStream.isNull() || m_mediaStream.isUndefined())
31 return;
32 emscripten::val audioTracks = m_mediaStream.call<emscripten::val>("getAudioTracks");
33 if (audioTracks.isNull() || audioTracks.isUndefined())
34 return;
35 if (audioTracks["length"].as<int>() < 1)
36 return;
37 audioTracks[0].set("muted", muted);
38
40 m_wasMuted = muted;
41
42}
43
45{
46 return m_wasMuted;
47}
48
50{
51 if (device == audioDevice)
52 return;
53
54 device = audioDevice;
55 setDeviceSourceStream(device.id().toStdString());
56}
57
59{
61 // TODO seems no easy way to set input volume
62}
63
64void QWasmAudioInput::setDeviceSourceStream(const std::string &id)
65{
66 qCDebug(qWasmAudioInput) << Q_FUNC_INFO << id;
67 emscripten::val navigator = emscripten::val::global("navigator");
68 emscripten::val mediaDevices = navigator["mediaDevices"];
69
70 if (mediaDevices.isNull() || mediaDevices.isUndefined()) {
71 qWarning() << "No media devices found";
72 return;
73 }
74
75 qstdweb::PromiseCallbacks getUserMediaCallback{
76 // default
77 .thenFunc =
78 [this](emscripten::val stream) {
79 qCDebug(qWasmAudioInput) << "getUserMediaSuccess";
80 m_mediaStream = stream;
81 },
82 .catchFunc =
83 [](emscripten::val error) {
84 qCDebug(qWasmAudioInput)
85 << "addCameraSourceElement getUserMedia fail"
86 << QString::fromStdString(error["name"].as<std::string>())
87 << QString::fromStdString(error["message"].as<std::string>());
88 }
89 };
90
91 emscripten::val constraints = emscripten::val::object();
92 constraints.set("audio", true);
93 if (!id.empty())
94 constraints.set("deviceId", id);
95
96 // we do it this way as this prompts user for mic permissions
97 qstdweb::Promise::make(mediaDevices, QStringLiteral("getUserMedia"),
98 std::move(getUserMediaCallback), constraints);
99}
100
102{
103 return m_mediaStream;
104}
105
107
108#include "moc_qwasmaudioinput_p.cpp"
The QAudioDevice class provides an information about audio devices and their functionality.
QByteArray id
\qmlproperty string QtMultimedia::audioDevice::id
\qmltype AudioInput \instantiates QAudioInput
Definition qaudioinput.h:19
std::string toStdString() const
\inmodule QtCore
Definition qobject.h:90
static QString fromStdString(const std::string &s)
Definition qstring.h:1322
void setMuted(bool muted) override
void mutedChanged(bool muted)
emscripten::val mediaStream()
bool isMuted() const
void setAudioDevice(const QAudioDevice &device) final
void setVolume(float volume) final
Combined button and popup list for selecting options.
void make(emscripten::val target, QString methodName, PromiseCallbacks callbacks, Args... args)
Definition qstdweb_p.h:182
#define Q_FUNC_INFO
DBusConnection const char DBusError * error
EGLStreamKHR stream
#define qWarning
Definition qlogging.h:162
#define Q_LOGGING_CATEGORY(name,...)
#define qCDebug(category,...)
GLenum GLuint id
[7]
#define QStringLiteral(str)
#define emit
#define Q_UNUSED(x)
std::function< void(emscripten::val)> thenFunc
Definition qstdweb_p.h:173
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent