Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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 GPL-2.0-only OR GPL-3.0-only
3
4#include "qwasmaudioinput_p.h"
5
6#include <qaudioinput.h>
7#include <private/qstdweb_p.h>
8
10
11Q_LOGGING_CATEGORY(qWasmAudioInput, "qt.multimedia.wasm.audioinput")
12
14 : QObject(parent), QPlatformAudioInput(parent)
15{
16 m_wasMuted = false;
17 setDeviceSourceStream("");
18}
19
23
25{
26 qCDebug(qWasmAudioInput) << Q_FUNC_INFO << muted;
27 if (muted == m_wasMuted)
28 return;
29 if (m_mediaStream.isNull() || m_mediaStream.isUndefined())
30 return;
31 emscripten::val audioTracks = m_mediaStream.call<emscripten::val>("getAudioTracks");
32 if (audioTracks.isNull() || audioTracks.isUndefined())
33 return;
34 if (audioTracks["length"].as<int>() < 1)
35 return;
36 audioTracks[0].set("muted", muted);
37
39 m_wasMuted = muted;
40
41}
42
44{
45 return m_wasMuted;
46}
47
49{
50 if (device == audioDevice)
51 return;
52
53 device = audioDevice;
54 setDeviceSourceStream(device.id().toStdString());
55}
56
58{
60 // TODO seems no easy way to set input volume
61}
62
63void QWasmAudioInput::setDeviceSourceStream(const std::string &id)
64{
65 qCDebug(qWasmAudioInput) << Q_FUNC_INFO << id;
66 emscripten::val navigator = emscripten::val::global("navigator");
67 emscripten::val mediaDevices = navigator["mediaDevices"];
68
69 if (mediaDevices.isNull() || mediaDevices.isUndefined()) {
70 qWarning() << "No media devices found";
71 return;
72 }
73
74 qstdweb::PromiseCallbacks getUserMediaCallback{
75 // default
76 .thenFunc =
77 [this](emscripten::val stream) {
78 qCDebug(qWasmAudioInput) << "getUserMediaSuccess";
79 m_mediaStream = stream;
80 },
81 .catchFunc =
82 [](emscripten::val error) {
83 qCDebug(qWasmAudioInput)
84 << "addCameraSourceElement getUserMedia fail"
85 << QString::fromStdString(error["name"].as<std::string>())
86 << QString::fromStdString(error["message"].as<std::string>());
87 }
88 };
89
90 emscripten::val constraints = emscripten::val::object();
91 constraints.set("audio", true);
92 if (!id.empty())
93 constraints.set("deviceId", id);
94
95 // we do it this way as this prompts user for mic permissions
96 qstdweb::Promise::make(mediaDevices, QStringLiteral("getUserMedia"),
97 std::move(getUserMediaCallback), constraints);
98}
99
101{
102 return m_mediaStream;
103}
104
106
107#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:103
static QString fromStdString(const std::string &s)
Definition qstring.h:1447
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:221
#define Q_FUNC_INFO
DBusConnection const char DBusError * error
EGLStreamKHR stream
#define qWarning
Definition qlogging.h:166
#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:212