diff options
Diffstat (limited to 'src/plugins/multimedia')
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 <private/qmemoryvideobuffer_p.h> #include <private/qvideotexturehelper_p.h> +#include <private/qwasmmediadevices_p.h> #include "qwasmmediacapturesession_p.h" #include <common/qwasmvideooutput_p.h> @@ -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<QWasmMediaDevices *>(QPlatformMediaIntegration::instance()->mediaDevices()); + + connect(wasmMediaDevices, &QWasmMediaDevices::videoInputsChanged,this, [this]() { + const QList<QCameraDevice> 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 |