From f47c1454fcff4d29b62d79a2045e7ebbccf57ccb Mon Sep 17 00:00:00 2001 From: Lorn Potter Date: Fri, 15 Mar 2024 14:23:11 +1000 Subject: wasm: fix camera showing in declarative MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Wait for camera to become found before trying to make it active Fixes: QTBUG-112316 Change-Id: Ic68c48be03909dbac11cf92d75387e25c74d573e Reviewed-by: Morten Johan Sørvig (cherry picked from commit 841fe1e93b38cbfc4af78e6135501cda143161e5) Reviewed-by: Qt Cherry-pick Bot --- .../multimedia/wasm/common/qwasmvideooutput.cpp | 2 +- .../multimedia/wasm/mediacapture/qwasmcamera.cpp | 43 +++++++++++++++++++--- .../multimedia/wasm/mediacapture/qwasmcamera_p.h | 5 ++- 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/src/plugins/multimedia/wasm/common/qwasmvideooutput.cpp b/src/plugins/multimedia/wasm/common/qwasmvideooutput.cpp index 452ea28f8..41b4208d3 100644 --- a/src/plugins/multimedia/wasm/common/qwasmvideooutput.cpp +++ b/src/plugins/multimedia/wasm/common/qwasmvideooutput.cpp @@ -232,7 +232,7 @@ void QWasmVideoOutput::setSource(const QUrl &url) if (url.isLocalFile()) { QFile localFile(url.toLocalFile()); if (localFile.open(QIODevice::ReadOnly)) { - QDataStream buffer(&localFile); // we will serialize the data into the file + QDataStream buffer(&localFile); // we will serialize the data into the file setSource(buffer.device()); } else { qWarning() << "Failed to open file"; diff --git a/src/plugins/multimedia/wasm/mediacapture/qwasmcamera.cpp b/src/plugins/multimedia/wasm/mediacapture/qwasmcamera.cpp index 031cc8dd3..976d94d96 100644 --- a/src/plugins/multimedia/wasm/mediacapture/qwasmcamera.cpp +++ b/src/plugins/multimedia/wasm/mediacapture/qwasmcamera.cpp @@ -8,6 +8,7 @@ #include "private/qplatformvideosink_p.h" #include #include +#include #include "qwasmmediacapturesession_p.h" #include @@ -23,8 +24,33 @@ Q_LOGGING_CATEGORY(qWasmCamera, "qt.multimedia.wasm.camera") QWasmCamera::QWasmCamera(QCamera *camera) - : QPlatformCamera(camera), m_cameraOutput(new QWasmVideoOutput) + : QPlatformCamera(camera), + m_cameraOutput(new QWasmVideoOutput), + m_cameraIsReady(false) { + QWasmMediaDevices *wasmMediaDevices = + static_cast(QPlatformMediaIntegration::instance()->mediaDevices()); + + connect(wasmMediaDevices, &QWasmMediaDevices::videoInputsChanged,this, [this]() { + const QList cameras = QMediaDevices::videoInputs(); + + if (!cameras.isEmpty()) { + if (m_cameraDev.id().isEmpty()) + setCamera(cameras.at(0)); // default camera + else + setCamera(m_cameraDev); + return; + } + }); + + connect(this, &QWasmCamera::cameraIsReady, this, [this]() { + m_cameraIsReady = true; + if (m_cameraShouldStartActive) { + QTimer::singleShot(50, this, [this]() { + setActive(true); + }); + } + }); } QWasmCamera::~QWasmCamera() = default; @@ -41,6 +67,11 @@ void QWasmCamera::setActive(bool active) return; } + if (!m_cameraIsReady) { + m_cameraShouldStartActive = true; + return; + } + m_cameraOutput->setSurface(m_CaptureSession->videoSink()); m_cameraActive = active; @@ -56,6 +87,9 @@ void QWasmCamera::setActive(bool active) void QWasmCamera::setCamera(const QCameraDevice &camera) { + if (!m_cameraDev.id().isEmpty()) + return; + m_cameraOutput->setVideoMode(QWasmVideoOutput::Camera); constexpr QSize initialSize(0, 0); @@ -65,15 +99,18 @@ void QWasmCamera::setCamera(const QCameraDevice &camera) m_cameraOutput->updateVideoElementGeometry(initialRect); const auto cameras = QMediaDevices::videoInputs(); + if (std::find(cameras.begin(), cameras.end(), camera) != cameras.end()) { m_cameraDev = camera; createCamera(m_cameraDev); + emit cameraIsReady(); return; } if (cameras.count() > 0) { m_cameraDev = camera; createCamera(m_cameraDev); + emit cameraIsReady(); } else { emit error(QCamera::CameraError, QStringLiteral("Failed to find a camera")); } @@ -399,10 +436,6 @@ void QWasmCamera::setColorTemperature(int temperature) void QWasmCamera::createCamera(const QCameraDevice &camera) { m_cameraOutput->addCameraSourceElement(camera.id().toStdString()); - // getUserMedia is async - QTimer::singleShot(100, [this] () { - m_cameraIsReady = m_cameraOutput->isCameraReady(); - }); } void QWasmCamera::updateCameraFeatures() diff --git a/src/plugins/multimedia/wasm/mediacapture/qwasmcamera_p.h b/src/plugins/multimedia/wasm/mediacapture/qwasmcamera_p.h index ccf5f99ce..b9399c6de 100644 --- a/src/plugins/multimedia/wasm/mediacapture/qwasmcamera_p.h +++ b/src/plugins/multimedia/wasm/mediacapture/qwasmcamera_p.h @@ -67,7 +67,8 @@ public: void setColorTemperature(int temperature) override; QWasmVideoOutput *cameraOutput() { return m_cameraOutput.data(); } - +Q_SIGNALS: + void cameraIsReady(); private: void createCamera(const QCameraDevice &camera); void updateCameraFeatures(); @@ -89,6 +90,8 @@ private: QCamera::WhiteBalanceMode m_wasmWhiteBalanceMode; int m_wasmColorTemperature; bool m_cameraIsReady = false; + bool m_cameraShouldStartActive = false; + bool m_shouldBeActive = false; }; QT_END_NAMESPACE -- cgit v1.2.3