diff options
author | Piotr Srebrny <piotr.srebrny@qt.io> | 2021-09-17 17:34:30 +0200 |
---|---|---|
committer | Piotr Srebrny <piotr.srebrny@qt.io> | 2021-09-18 11:07:31 +0000 |
commit | 6b4792e63e0dfe6c568f0a79b426f004386ebe96 (patch) | |
tree | 185213286cc52bca15d7592a262d495ec528663b /src/multimedia | |
parent | 119d54fabda01bef7b7c95d68729746e667d62cf (diff) |
Set default camera when calling setCameraDevice with null device
setCameraDevice should behave in a similar way to
QAudioInput/Output::setAudioDevice, which select default device when
null device is provided as an argument.
Pick-to: 6.2
Change-Id: I8ac5710ebef1ed655726014fd06ff2481cea95aa
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
Diffstat (limited to 'src/multimedia')
-rw-r--r-- | src/multimedia/camera/qcamera.cpp | 37 | ||||
-rw-r--r-- | src/multimedia/camera/qcamera_p.h | 2 |
2 files changed, 17 insertions, 22 deletions
diff --git a/src/multimedia/camera/qcamera.cpp b/src/multimedia/camera/qcamera.cpp index 6fa9079e9..22abc4f75 100644 --- a/src/multimedia/camera/qcamera.cpp +++ b/src/multimedia/camera/qcamera.cpp @@ -109,7 +109,7 @@ void QCameraPrivate::_q_error(int error, const QString &errorString) emit q->errorOccurred(this->error, errorString); } -void QCameraPrivate::init() +void QCameraPrivate::init(const QCameraDevice &device) { Q_Q(QCamera); @@ -119,8 +119,9 @@ void QCameraPrivate::init() return; } + cameraDevice = !device.isNull() ? device : QMediaDevices::defaultVideoInput(); if (cameraDevice.isNull()) - _q_error(QCamera::CameraError, QString::fromUtf8("Invalid camera specified")); + _q_error(QCamera::CameraError, QString::fromUtf8("No camera detected")); control->setCamera(cameraDevice); q->connect(control, SIGNAL(activeChanged(bool)), q, SIGNAL(activeChanged(bool))); q->connect(control, SIGNAL(error(int,QString)), q, SLOT(_q_error(int,QString))); @@ -147,10 +148,7 @@ QCamera::QCamera(const QCameraDevice &cameraDevice, QObject *parent) : QObject(*new QCameraPrivate, parent) { Q_D(QCamera); - - d->cameraDevice = cameraDevice; - d->init(); - setCameraDevice(cameraDevice); + d->init(cameraDevice); } /*! @@ -170,18 +168,15 @@ QCamera::QCamera(QCameraDevice::Position position, QObject *parent) { Q_D(QCamera); - QCameraDevice info; + QCameraDevice device; auto cameras = QMediaDevices::videoInputs(); for (const auto &c : cameras) { if (c.position() == position) { - info = c; + device = c; break; } } - if (info.isNull()) - info = QMediaDevices::defaultVideoInput(); - d->cameraDevice = info; - d->init(); + d->init(device); } /*! @@ -307,19 +302,19 @@ QCameraDevice QCamera::cameraDevice() const } /*! - Sets the camera object to use the physical camera described by - \a cameraDevice. + Connects the camera object to the physical camera device described by + \a device. Using a default constructed QCameraDevice object as \a device + will connect the camera to the system default camera device. */ void QCamera::setCameraDevice(const QCameraDevice &cameraDevice) { Q_D(QCamera); - if (d->cameraDevice == cameraDevice) + auto dev = cameraDevice; + if (dev.isNull()) + dev = QMediaDevices::defaultVideoInput(); + if (d->cameraDevice == dev) return; - d->cameraDevice = cameraDevice; - if (d->cameraDevice.isNull()) - d->_q_error(QCamera::CameraError, QString::fromUtf8("Invalid camera specified")); - else - d->_q_error(QCamera::NoError, QString()); + d->cameraDevice = dev; if (d->control) d->control->setCamera(d->cameraDevice); emit cameraDeviceChanged(); @@ -336,7 +331,7 @@ QCameraFormat QCamera::cameraFormat() const } /*! - Tells the camera to use the format described by \a format. This can be used to define + Tells the camera to use the format desribed by \a format. This can be used to define as specific resolution and frame rate to be used for recording and image capture. */ void QCamera::setCameraFormat(const QCameraFormat &format) diff --git a/src/multimedia/camera/qcamera_p.h b/src/multimedia/camera/qcamera_p.h index cb940eed2..c0d457649 100644 --- a/src/multimedia/camera/qcamera_p.h +++ b/src/multimedia/camera/qcamera_p.h @@ -70,7 +70,7 @@ public: { } - void init(); + void init(const QCameraDevice &device); QMediaCaptureSession *captureSession = nullptr; QPlatformMediaCaptureSession *captureInterface = nullptr; |