QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
UVCCameraController Class Reference

#include <UVCCameraController.h>

Inheritance diagram for UVCCameraController:
Collaboration diagram for UVCCameraController:

Classes

struct  Controls

Signals

void camerasChanged ()
 A camera was plugged in or unplugged.
void formatsChanged ()
void controlsChanged ()
 The camera's adjustable controls, or its zoom range, changed.
void transformChanged ()
Signals inherited from VideoReceiver
void stateChanged (VideoReceiver::State state)
void streamingChanged (bool streaming)
void decodingChanged (bool decoding)
void recordingChanged (bool recording)
void recordingStarted (const QString &file)
 A recording file opened: the first one after startRecording(), or the recording's next part.
void recordingFinished (const QString &file, const QString &error)
 A recording file closed. error is empty when the file was finalized cleanly.
void videoSizeChanged (QSize size)
void statsChanged (const VideoStreamStats &stats)
void errorOccurred (const QString &message)

Public Member Functions

 UVCCameraController (QObject *parent=nullptr)
 ~UVCCameraController () override
UVCStreamMonitor * monitor () const
QString device () const
 The selected camera's description, the config's URI; empty while none is.
void watchCameras ()
 Emits camerasChanged() from now on when a camera is plugged in or unplugged.
QStringList formats () const
 The formats the selected camera offers, as UVCDevices::formatNames(); empty without a camera.
QString format () const
void setFormat (const QString &name)
 Runs the camera in format name, recreating it: not every platform switches a running camera's format.
void setControls (const Controls &controls)
 Applies controls to the camera, and to each camera from now on as it starts.
const Controls & controls () const
bool whiteBalanceAdjustable () const
 Whether the camera has a white balance besides automatic.
qreal minimumZoom () const
 The camera's zoom range; both 1 for a camera that doesn't zoom, or without a camera.
qreal maximumZoom () const
bool exposureAdjustable () const
bool focusAdjustable () const
Q_INVOKABLE void registerVideoOutput (QQuickItem *videoOutput)
Q_INVOKABLE void unregisterVideoOutput (QQuickItem *videoOutput)
void setTransform (QtVideo::Rotation rotation, bool mirrored)
int rotation () const
bool mirrored () const
bool encodesRecordings () const override
void shutDown () override
Public Member Functions inherited from VideoReceiver
 VideoReceiver (QObject *parent=nullptr)
 ~VideoReceiver () override
QString name () const
void setName (const QString &name)
 Names the stream in logs and pipeline dumps. Call once, before the first setActive(true).
const Config & config () const
const DisplayConfig & displayConfig () const
bool active () const
QQuickItem * videoOutput () const
State state () const
bool streaming () const
bool decoding () const
bool recording () const
QSize videoSize () const
 The size of the frames as they are presented, before the orientation the VideoOutput adds.
VideoStreamStats stats () const
 The stream's health, measured about once a second while it streams; invalid while it doesn't.
void setConfig (const Config &config)
void setDisplayConfig (const DisplayConfig &config)
 Stores config for the display sink restart() builds next.
const RecordingEncoding & recordingEncoding () const
void setRecordingEncoding (const RecordingEncoding &encoding)
 Stores encoding for the recordings that start from now on; one in progress keeps its own.
void setActive (bool active)
 Streams while true and the URI is set. False stops the stream, any pending reconnect and any recording.
void restart ()
void reconnectNow ()
void setVideoOutput (QQuickItem *videoOutput)
void startRecording (const QString &file, FileFormat format)
void stopRecording ()
bool takeScreenshot (const QString &file, const VideoScreenshot::Exif &exif={})

Static Public Member Functions

static QStringList recordingCodecs ()
 For QML: QtVideoRecorder::recordingCodecs() and recordingCodecName().
static Q_INVOKABLE QString recordingCodecName (const QString &key)
Static Public Member Functions inherited from VideoReceiver
static QString fileExtension (FileFormat format)
 "mkv", "mov" or "mp4".
static QString recordingComment ()
 The comment recordings carry, of GStreamer and of a USB camera alike: the application and its version.
static bool isValidFileFormat (int format)
static QSize presentedSize (const QVideoFrame &frame, QSize pictureSize={})

Static Public Attributes

static constexpr int kMaxRetries = 3
static constexpr std::chrono::seconds kRetryInterval {2}

Protected Member Functions

void onConfigChanged () override
void onActiveChanged () override
void onRestartRequested () override
void onReconnectRequested () override
void onVideoOutputChanged () override
 Also called when the current output is set again.
void onStartRecording (const QString &file, FileFormat format) override
void onStopRecording () override
bool onTakeScreenshot (const QString &file, const VideoScreenshot::Exif &exif) override
Protected Member Functions inherited from VideoReceiver
QVideoSink * videoSink () const
 The VideoOutput's QVideoSink, or nullptr.
void setState (State state)
void setDecoding (bool decoding)
void setRecording (bool recording)
void setVideoSize (QSize size)
void setStats (const VideoStreamStats &stats)
 Backends publish a new measurement, and an invalid one once the stream stops or reconnects.

Friends

class UVCCameraControllerTest
class VideoManagerTest

Additional Inherited Members

Public Types inherited from VideoReceiver
enum class  State : uint8_t { Stopped , Connecting , Streaming , Reconnecting }
enum class  FileFormat : uint8_t { Mkv = 0 , Mov = 1 , Mp4 = 2 }
 Values are persisted by VideoSettings::recordingFormat. More...

Detailed Description

A USB (UVC) camera as a VideoReceiver: the camera the config's URI names by its description, which is its video source name. Owns its QCamera and capture session, measures its frames, takes its photos and records it. The user hears of camera errors and of the camera being unplugged.

The camera is tracked by its device id, so unplugging it is noticed even while another camera of the same name stays attached; the controller then runs that one. After an error the camera is recreated up to kMaxRetries times.

The camera runs while the receiver is active and has a VideoOutput: a UVCVideoOutput.qml, which registers itself once it is created and turns and mirrors the picture as rotation and mirrored say, with its orientation and mirrored properties. Stopped until then, streaming from the camera's first frame, and reconnecting while it waits to retry after an error.

Definition at line 36 of file UVCCameraController.h.

Constructor & Destructor Documentation

◆ UVCCameraController()

◆ ~UVCCameraController()

UVCCameraController::~UVCCameraController ( )
override

Definition at line 82 of file UVCCameraController.cc.

Member Function Documentation

◆ camerasChanged

void UVCCameraController::camerasChanged ( )
signal

A camera was plugged in or unplugged.

◆ controls()

const Controls & UVCCameraController::controls ( ) const
inline

Definition at line 97 of file UVCCameraController.h.

Referenced by setControls().

◆ controlsChanged

void UVCCameraController::controlsChanged ( )
signal

The camera's adjustable controls, or its zoom range, changed.

◆ device()

QString UVCCameraController::device ( ) const
inline

The selected camera's description, the config's URI; empty while none is.

Definition at line 67 of file UVCCameraController.h.

References VideoReceiver::config(), and VideoReceiver::Config::uri.

Referenced by onConfigChanged().

◆ encodesRecordings()

bool UVCCameraController::encodesRecordings ( ) const
inlineoverridevirtual

True for a receiver that encodes its recordings anew, as recordingEncoding() says: a USB camera, or Qt Multimedia's player. False for one that records the stream as it arrives.

Reimplemented from VideoReceiver.

Definition at line 128 of file UVCCameraController.h.

◆ exposureAdjustable()

bool UVCCameraController::exposureAdjustable ( ) const

Whether the camera's exposure compensation can be set. Never on Linux, where Qt's V4L2 camera reads the focus range into the exposure compensation range.

Definition at line 236 of file UVCCameraController.cc.

◆ focusAdjustable()

bool UVCCameraController::focusAdjustable ( ) const

Whether the camera can focus at infinity, one of the fixed focus modes. Never on Linux, where Qt's V4L2 camera doesn't read the focus range, so it can't set a focus distance.

Definition at line 242 of file UVCCameraController.cc.

◆ format()

QString UVCCameraController::format ( ) const
inline

The format the camera runs in, named as in formats(). Empty, or a format the camera doesn't offer, lets Qt choose.

Definition at line 77 of file UVCCameraController.h.

Referenced by onStartRecording(), and transformChanged().

◆ formats()

QStringList UVCCameraController::formats ( ) const
inline

The formats the selected camera offers, as UVCDevices::formatNames(); empty without a camera.

Definition at line 73 of file UVCCameraController.h.

◆ formatsChanged

void UVCCameraController::formatsChanged ( )
signal

◆ maximumZoom()

qreal UVCCameraController::maximumZoom ( ) const

Definition at line 231 of file UVCCameraController.cc.

◆ minimumZoom()

qreal UVCCameraController::minimumZoom ( ) const

The camera's zoom range; both 1 for a camera that doesn't zoom, or without a camera.

Definition at line 226 of file UVCCameraController.cc.

◆ mirrored()

bool UVCCameraController::mirrored ( ) const
inline

Definition at line 126 of file UVCCameraController.h.

Referenced by setTransform().

◆ monitor()

UVCStreamMonitor * UVCCameraController::monitor ( ) const
inline

Definition at line 64 of file UVCCameraController.h.

◆ onActiveChanged()

void UVCCameraController::onActiveChanged ( )
overrideprotectedvirtual

Implements VideoReceiver.

Definition at line 93 of file UVCCameraController.cc.

References VideoReceiver::active().

◆ onConfigChanged()

void UVCCameraController::onConfigChanged ( )
overrideprotectedvirtual

Implements VideoReceiver.

Definition at line 87 of file UVCCameraController.cc.

References device().

◆ onReconnectRequested()

void UVCCameraController::onReconnectRequested ( )
overrideprotectedvirtual

Implements VideoReceiver.

Definition at line 110 of file UVCCameraController.cc.

◆ onRestartRequested()

void UVCCameraController::onRestartRequested ( )
overrideprotectedvirtual

Implements VideoReceiver.

Definition at line 102 of file UVCCameraController.cc.

◆ onStartRecording()

void UVCCameraController::onStartRecording ( const QString & file,
FileFormat format )
overrideprotectedvirtual

Records the camera's video as QtVideoRecorder::start() does, while it runs. Recreating the camera (another device or format, an unplug, a retry) ends the recording; Qt finishes writing the file.

Implements VideoReceiver.

Definition at line 176 of file UVCCameraController.cc.

References QtVideoRecorder::container(), format(), VideoReceiver::recordingEncoding(), and VideoReceiver::recordingFinished().

◆ onStopRecording()

void UVCCameraController::onStopRecording ( )
overrideprotectedvirtual

Implements VideoReceiver.

Definition at line 190 of file UVCCameraController.cc.

◆ onTakeScreenshot()

bool UVCCameraController::onTakeScreenshot ( const QString & file,
const VideoScreenshot::Exif & exif )
overrideprotectedvirtual

Takes a photo, turned and mirrored as setTransform() says and saved off the GUI thread: a still capture where Qt takes one (Android, Apple), else the frame on display. Saves the frame on display too while the camera can't capture, or when a capture fails, which errorOccurred() reports if there is none by then.

Reimplemented from VideoReceiver.

Definition at line 265 of file UVCCameraController.cc.

References VideoReceiver::onTakeScreenshot().

◆ onVideoOutputChanged()

void UVCCameraController::onVideoOutputChanged ( )
overrideprotectedvirtual

Also called when the current output is set again.

Implements VideoReceiver.

Definition at line 170 of file UVCCameraController.cc.

References VideoReceiver::videoOutput().

◆ recordingCodecName()

Q_INVOKABLE QString UVCCameraController::recordingCodecName ( const QString & key)
inlinestatic

Definition at line 137 of file UVCCameraController.h.

References QtVideoRecorder::recordingCodecName().

◆ recordingCodecs()

QStringList UVCCameraController::recordingCodecs ( )
inlinestatic

◆ registerVideoOutput()

void UVCCameraController::registerVideoOutput ( QQuickItem * videoOutput)

For QML: setVideoOutput(), with videoOutput to display the camera in it, replacing any other, and with none to stop displaying in videoOutput if it is the output set. That one happens on the next event loop pass, unless another output registered by then: it takes the camera over without the camera stopping.

Definition at line 146 of file UVCCameraController.cc.

References VideoReceiver::setVideoOutput(), and VideoReceiver::videoOutput().

◆ rotation()

int UVCCameraController::rotation ( ) const
inline

The clockwise rotation, in degrees, and the mirroring after it, that a VideoOutput showing the camera applies as its orientation and mirrored properties.

Definition at line 124 of file UVCCameraController.h.

Referenced by setTransform().

◆ setControls()

void UVCCameraController::setControls ( const Controls & controls)

Applies controls to the camera, and to each camera from now on as it starts.

Definition at line 210 of file UVCCameraController.cc.

References controls().

◆ setFormat()

void UVCCameraController::setFormat ( const QString & name)

Runs the camera in format name, recreating it: not every platform switches a running camera's format.

Definition at line 128 of file UVCCameraController.cc.

References VideoReceiver::name().

◆ setTransform()

void UVCCameraController::setTransform ( QtVideo::Rotation rotation,
bool mirrored )

Turns the camera's picture clockwise by rotation and then mirrors it when mirrored, on display and in photos; recordings keep the camera's own picture. On display the VideoOutput does it, on top of the frames' own rotation and mirroring: see rotation() and mirrored().

Definition at line 200 of file UVCCameraController.cc.

References mirrored(), rotation(), and transformChanged().

◆ shutDown()

void UVCCameraController::shutDown ( )
overridevirtual

Waits for a recording's file to be finished first: Qt writes it on its own thread, which the application's exit would cut short.

Reimplemented from VideoReceiver.

Definition at line 115 of file UVCCameraController.cc.

References VideoReceiver::shutDown().

◆ transformChanged

void UVCCameraController::transformChanged ( )
signal

References format().

Referenced by setTransform().

◆ unregisterVideoOutput()

void UVCCameraController::unregisterVideoOutput ( QQuickItem * videoOutput)

◆ watchCameras()

void UVCCameraController::watchCameras ( )

Emits camerasChanged() from now on when a camera is plugged in or unplugged.

Definition at line 140 of file UVCCameraController.cc.

◆ whiteBalanceAdjustable()

bool UVCCameraController::whiteBalanceAdjustable ( ) const

Whether the camera has a white balance besides automatic.

Definition at line 219 of file UVCCameraController.cc.

◆ UVCCameraControllerTest

friend class UVCCameraControllerTest
friend

Definition at line 54 of file UVCCameraController.h.

References UVCCameraControllerTest.

Referenced by UVCCameraControllerTest.

◆ VideoManagerTest

friend class VideoManagerTest
friend

Definition at line 55 of file UVCCameraController.h.

References VideoManagerTest.

Referenced by VideoManagerTest.

Member Data Documentation

◆ kMaxRetries

int UVCCameraController::kMaxRetries = 3
staticconstexpr

Definition at line 58 of file UVCCameraController.h.

◆ kRetryInterval

std::chrono::seconds UVCCameraController::kRetryInterval {2}
staticconstexpr

Definition at line 59 of file UVCCameraController.h.

Referenced by UVCCameraController().


The documentation for this class was generated from the following files: