diff options
author | Lorn Potter <lorn.potter@gmail.com> | 2023-04-10 07:54:41 +1000 |
---|---|---|
committer | Lorn Potter <lorn.potter@gmail.com> | 2023-04-20 05:28:49 +1000 |
commit | 6394ca0ee85695754fe687cd8f1d77e4e5f07283 (patch) | |
tree | b969de777b845fcf89b57202e16233e9bc200a4b | |
parent | 587fb48167cf57d1612338ac0e91ae8136cd76aa (diff) | |
download | qtmultimedia-6394ca0ee85695754fe687cd8f1d77e4e5f07283.tar.gz |
wasm: fix crash when camera is not yet ready
Pick-to: 6.5
Change-Id: I38b27c2295827f7319d9c45f7a12bd45c98bd7d6
Reviewed-by: Morten Johan Sørvig <morten.sorvig@qt.io>
4 files changed, 38 insertions, 38 deletions
diff --git a/src/plugins/multimedia/wasm/common/qwasmvideooutput.cpp b/src/plugins/multimedia/wasm/common/qwasmvideooutput.cpp index ba6127176..94a8173c7 100644 --- a/src/plugins/multimedia/wasm/common/qwasmvideooutput.cpp +++ b/src/plugins/multimedia/wasm/common/qwasmvideooutput.cpp @@ -239,6 +239,7 @@ void QWasmVideoOutput::addSourceElement(const QString &urlString) void QWasmVideoOutput::addCameraSourceElement(const std::string &id) { + m_cameraIsReady = false; emscripten::val navigator = emscripten::val::global("navigator"); emscripten::val mediaDevices = navigator["mediaDevices"]; @@ -254,44 +255,7 @@ void QWasmVideoOutput::addCameraSourceElement(const std::string &id) qCDebug(qWasmMediaVideoOutput) << "getUserMediaSuccess"; m_video.set("srcObject", stream); - - emscripten::val videoTracksObject = stream.call<emscripten::val>("getVideoTracks"); - if (videoTracksObject.isNull() || videoTracksObject.isUndefined()) { - emit errorOccured(QMediaPlayer::ResourceError, QStringLiteral("Resource error")); - return; - } - - if (videoTracksObject.call<emscripten::val>("length").as<int>() == 0) { - emit errorOccured(QMediaPlayer::ResourceError, QStringLiteral("Resource error")); - return; - } - emscripten::val tracks = stream.call<emscripten::val>("getVideoTracks"); - if (tracks.isNull() || tracks.isUndefined()) - return; - if (tracks["length"].as<int>() == 0) - return; - emscripten::val videoTrack = tracks[0]; - if (videoTrack.isNull() || videoTrack.isUndefined()) { - emit errorOccured(QMediaPlayer::ResourceError, QStringLiteral("Resource error")); - return; - } - - emscripten::val currentVideoCapabilities = videoTrack.call<emscripten::val>("getCapabilities"); - if (currentVideoCapabilities.isNull() || currentVideoCapabilities.isUndefined()) { - emit errorOccured(QMediaPlayer::ResourceError, QStringLiteral("Resource error")); - return; - } - - emscripten::val videoSettings = videoTrack.call<emscripten::val>("getSettings"); - if (videoSettings.isNull() || videoSettings.isUndefined()) { - emit errorOccured(QMediaPlayer::ResourceError, QStringLiteral("Resource error")); - return; - } - - // TODO double fRate = videoSettings["frameRate"].as<double>(); - // const int width = videoSettings["width"].as<int>(); - // const int height = videoSettings["height"].as<int>(); - + m_cameraIsReady = true; }, .catchFunc = [](emscripten::val error) { diff --git a/src/plugins/multimedia/wasm/common/qwasmvideooutput_p.h b/src/plugins/multimedia/wasm/common/qwasmvideooutput_p.h index 66ae6fd2b..e47f2030e 100644 --- a/src/plugins/multimedia/wasm/common/qwasmvideooutput_p.h +++ b/src/plugins/multimedia/wasm/common/qwasmvideooutput_p.h @@ -80,6 +80,7 @@ public: bool hasCapability(const QString &cap); emscripten::val getDeviceCapabilities(); bool setDeviceSetting(const std::string &key, emscripten::val value); + bool isCameraReady() { return m_cameraIsReady; } // mediacapturesession has the videosink QVideoSink *m_wasmSink = nullptr; @@ -112,6 +113,8 @@ private: bool m_toBePaused = false; bool m_isSeeking = false; bool m_hasAudio = false; + bool m_cameraIsReady = false; + emscripten::val m_offscreenContext = emscripten::val::undefined(); QSize m_pendingVideoSize; QWasmVideoOutput::WasmVideoMode m_currentVideoMode = QWasmVideoOutput::VideoOutput; diff --git a/src/plugins/multimedia/wasm/mediacapture/qwasmcamera.cpp b/src/plugins/multimedia/wasm/mediacapture/qwasmcamera.cpp index f346ef75b..08d32ac0b 100644 --- a/src/plugins/multimedia/wasm/mediacapture/qwasmcamera.cpp +++ b/src/plugins/multimedia/wasm/mediacapture/qwasmcamera.cpp @@ -16,6 +16,7 @@ #include <emscripten/bind.h> #include <emscripten/html5.h> #include <QUuid> +#include <QTimer> #include <private/qstdweb_p.h> @@ -172,6 +173,9 @@ void QWasmCamera::setTorchMode(QCamera::TorchMode mode) bool QWasmCamera::isTorchModeSupported(QCamera::TorchMode mode) const { + if (!m_cameraIsReady) + return false; + emscripten::val caps = m_cameraOutput->getDeviceCapabilities(); if (caps.isUndefined()) return false; @@ -215,6 +219,9 @@ void QWasmCamera::setExposureMode(QCamera::ExposureMode mode) bool QWasmCamera::isExposureModeSupported(QCamera::ExposureMode mode) const { + if (!m_cameraIsReady) + return false; + emscripten::val caps = m_cameraOutput->getDeviceCapabilities(); if (caps.isUndefined()) return false; @@ -247,6 +254,9 @@ bool QWasmCamera::isExposureModeSupported(QCamera::ExposureMode mode) const void QWasmCamera::setExposureCompensation(float bias) { + if (!m_cameraIsReady) + return; + emscripten::val caps = m_cameraOutput->getDeviceCapabilities(); if (caps.isUndefined()) return; @@ -268,6 +278,9 @@ void QWasmCamera::setManualExposureTime(float secs) if (m_wasmExposureTime == secs) return; + if (!m_cameraIsReady) + return; + emscripten::val caps = m_cameraOutput->getDeviceCapabilities(); emscripten::val exposureTime = caps["exposureTime"]; if (exposureTime.isUndefined()) @@ -280,6 +293,9 @@ void QWasmCamera::setManualExposureTime(float secs) int QWasmCamera::isoSensitivity() const { + if (!m_cameraIsReady) + return 0; + emscripten::val caps = m_cameraOutput->getDeviceCapabilities(); if (caps.isUndefined()) return false; @@ -293,6 +309,9 @@ int QWasmCamera::isoSensitivity() const void QWasmCamera::setManualIsoSensitivity(int sens) { + if (!m_cameraIsReady) + return; + emscripten::val caps = m_cameraOutput->getDeviceCapabilities(); if (caps.isUndefined()) return; @@ -310,6 +329,9 @@ void QWasmCamera::setManualIsoSensitivity(int sens) bool QWasmCamera::isWhiteBalanceModeSupported(QCamera::WhiteBalanceMode mode) const { + if (!m_cameraIsReady) + return false; + emscripten::val caps = m_cameraOutput->getDeviceCapabilities(); if (caps.isUndefined()) return false; @@ -355,6 +377,9 @@ void QWasmCamera::setWhiteBalanceMode(QCamera::WhiteBalanceMode mode) void QWasmCamera::setColorTemperature(int temperature) { + if (!m_cameraIsReady) + return; + emscripten::val caps = m_cameraOutput->getDeviceCapabilities(); if (caps.isUndefined()) return; @@ -374,10 +399,17 @@ void QWasmCamera::setColorTemperature(int temperature) void QWasmCamera::createCamera(const QCameraDevice &camera) { m_cameraOutput->addCameraSourceElement(camera.id().toStdString()); + // getUserMedia is async + QTimer::singleShot(100,[this, &camera] () { + m_cameraIsReady = m_cameraOutput->isCameraReady(); + }); } void QWasmCamera::updateCameraFeatures() { + if (!m_cameraIsReady) + return; + emscripten::val caps = m_cameraOutput->getDeviceCapabilities(); if (caps.isUndefined()) return; diff --git a/src/plugins/multimedia/wasm/mediacapture/qwasmcamera_p.h b/src/plugins/multimedia/wasm/mediacapture/qwasmcamera_p.h index 6f8040dbf..ccf5f99ce 100644 --- a/src/plugins/multimedia/wasm/mediacapture/qwasmcamera_p.h +++ b/src/plugins/multimedia/wasm/mediacapture/qwasmcamera_p.h @@ -88,6 +88,7 @@ private: int m_wasmIsoSensitivity; QCamera::WhiteBalanceMode m_wasmWhiteBalanceMode; int m_wasmColorTemperature; + bool m_cameraIsReady = false; }; QT_END_NAMESPACE |