7#include <QtCore/QDateTime>
8#include <QtCore/QEventLoop>
10#include <QtCore/QFileInfo>
11#include <QtCore/QMetaEnum>
12#include <QtCore/QMimeType>
13#include <QtCore/QTimer>
15#include <QtMultimedia/QMediaMetaData>
23QString fileSuffix(
const QMediaFormat& format)
25 switch (
format.fileFormat()) {
26 case QMediaFormat::Matroska:
27 return QStringLiteral(
"mkv");
28 case QMediaFormat::QuickTime:
29 return QStringLiteral(
"mov");
30 case QMediaFormat::MPEG4:
31 return QStringLiteral(
"mp4");
33 return format.mimeType().preferredSuffix();
38constexpr QMediaFormat::FileFormat kRecordingContainers[] = {QMediaFormat::MPEG4, QMediaFormat::Matroska,
39 QMediaFormat::QuickTime};
42constexpr QMediaFormat::VideoCodec kFallbackCodecs[] = {
43 QMediaFormat::VideoCodec::H264, QMediaFormat::VideoCodec::H265, QMediaFormat::VideoCodec::AV1,
44 QMediaFormat::VideoCodec::VP9, QMediaFormat::VideoCodec::MPEG4, QMediaFormat::VideoCodec::MPEG2,
47std::optional<QMediaFormat> encodableFormat(QMediaFormat::FileFormat container, QMediaFormat::VideoCodec codec)
49 QMediaFormat
format(container);
50 format.setVideoCodec(codec);
51 return format.isSupported(QMediaFormat::Encode) ? std::optional(format) : std::nullopt;
54void removeIfEmpty(
const QString& file)
56 const QFileInfo info(file);
57 if (info.exists() && (info.size() == 0)) {
58 (void) QFile::remove(file);
66 , _recorder(new QMediaRecorder(this))
68 (void) connect(_recorder, &QMediaRecorder::recorderStateChanged,
this, &QtVideoRecorder::_onRecorderStateChanged);
69 (void) connect(_recorder, &QMediaRecorder::errorOccurred,
this,
70 [
this](QMediaRecorder::Error,
const QString& errorString) { _onRecorderError(errorString); });
72 (void) connect(_recorder, &QMediaRecorder::durationChanged,
this, [
this](qint64 duration) {
73 if (!_recording && (duration > 0) && (_recorder->recorderState() == QMediaRecorder::RecordingState)) {
84 QMediaFormat::VideoCodec codec)
86 QList<QMediaFormat::FileFormat> containers{
container};
87 for (
const QMediaFormat::FileFormat other : kRecordingContainers) {
88 if (!containers.contains(other)) {
89 containers.append(other);
92 QList<QMediaFormat> formats;
93 if (codec != QMediaFormat::VideoCodec::Unspecified) {
94 for (
const QMediaFormat::FileFormat candidate : std::as_const(containers)) {
95 if (
const std::optional<QMediaFormat> format = encodableFormat(candidate, codec)) {
96 formats.append(*format);
101 for (
const QMediaFormat::FileFormat candidate : std::as_const(containers)) {
102 QList<QMediaFormat> fallbacks;
103 for (
const QMediaFormat::VideoCodec fallback : kFallbackCodecs) {
104 if (
const std::optional<QMediaFormat> format = encodableFormat(candidate, fallback)) {
105 fallbacks.append(*format);
108 if (!fallbacks.isEmpty()) {
109 for (
const QMediaFormat& format : std::as_const(fallbacks)) {
110 if (!formats.contains(format)) {
111 formats.append(format);
122 QList<QMediaFormat::VideoCodec> codecs(std::begin(kFallbackCodecs), std::end(kFallbackCodecs));
123 const QMetaEnum codecEnum = QMetaEnum::fromType<QMediaFormat::VideoCodec>();
124 for (
int i = 0; i < codecEnum.keyCount(); i++) {
125 const auto codec =
static_cast<QMediaFormat::VideoCodec
>(codecEnum.value(i));
126 if ((codec != QMediaFormat::VideoCodec::Unspecified) && !codecs.contains(codec)) {
127 codecs.append(codec);
132 for (
const QMediaFormat::VideoCodec codec : std::as_const(codecs)) {
133 const bool encodable = std::ranges::any_of(kRecordingContainers, [codec](QMediaFormat::FileFormat
container) {
134 return encodableFormat(
container, codec).has_value();
137 keys.append(QString::fromLatin1(codecEnum.valueToKey(
static_cast<int>(codec))));
146 const int value = QMetaEnum::fromType<QMediaFormat::VideoCodec>().keyToValue(key.toLatin1().constData(), &ok);
147 return ok ?
static_cast<QMediaFormat::VideoCodec
>(value) : QMediaFormat::VideoCodec::Unspecified;
159 return QMediaFormat::Matroska;
161 return QMediaFormat::QuickTime;
165 return QMediaFormat::MPEG4;
170 const bool valid = (value >= QMediaRecorder::VeryLowQuality) && (value <= QMediaRecorder::VeryHighQuality);
171 return valid ?
static_cast<QMediaRecorder::Quality
>(value) : QMediaRecorder::HighQuality;
175 QMediaRecorder::Quality quality)
178 if (_pendingFormats.isEmpty()) {
179 qCWarning(QtVideoRecorderLog) <<
"No video encoder:" << file;
183 const QFileInfo info(file);
184 _recordingBase = QStringLiteral(
"%1/%2").arg(info.path(), info.completeBaseName());
186 _stopRequested =
false;
190void QtVideoRecorder::_recordNextFormat()
192 const QMediaFormat format = _pendingFormats.takeFirst();
193 _recordingFile = QStringLiteral(
"%1.%2").arg(_recordingBase, fileSuffix(format));
194 _recordingError.clear();
195 qCDebug(QtVideoRecorderLog) <<
"record" << _recordingFile << format.fileFormat() << format.videoCodec();
196 _recorder->setMediaFormat(format);
201 _recorder->setQuality(_quality);
203 QMediaMetaData metaData;
204 metaData.insert(QMediaMetaData::Date, QDateTime::currentDateTimeUtc());
206 _recorder->setMetaData(metaData);
207 _recorder->setOutputLocation(QUrl::fromLocalFile(_recordingFile));
211void QtVideoRecorder::_retryNextFormat()
213 if (_stopRequested ||
active() || _pendingFormats.isEmpty()) {
221 _stopRequested =
true;
232 QTimer::singleShot(timeout, &loop, &QEventLoop::quit);
244void QtVideoRecorder::_onRecorderStateChanged(QMediaRecorder::RecorderState state)
246 if (state != QMediaRecorder::StoppedState) {
254 _onRecordingNotStarted();
258void QtVideoRecorder::_onRecorderError(
const QString& errorString)
260 qCWarning(QtVideoRecorderLog) <<
"Recording error:" << _recordingFile << errorString;
261 _recordingError = errorString;
263 if (!_recording && (_recorder->recorderState() == QMediaRecorder::StoppedState)) {
264 _onRecordingNotStarted();
268void QtVideoRecorder::_onRecordingNotStarted()
270 removeIfEmpty(_recordingFile);
271 if (_recordingError.isEmpty() || _stopRequested) {
274 if (!_pendingFormats.isEmpty() && _recorder->captureSession()) {
275 qCDebug(QtVideoRecorderLog) <<
"Encoder failed, trying the next format:" << _recordingError;
277 QMetaObject::invokeMethod(
this, &QtVideoRecorder::_retryNextFormat, Qt::QueuedConnection);
280 const QString error = std::exchange(_recordingError, QString());
#define QGC_LOGGING_CATEGORY(name, categoryStr)
static QList< QMediaFormat > recordingFormats(QMediaFormat::FileFormat container, QMediaFormat::VideoCodec codec=QMediaFormat::VideoCodec::Unspecified)
bool recording() const
From the first encoded frame until the recorder stops.
void recordingChanged(bool recording)
static QMediaFormat::VideoCodec recordingCodec(const QString &key)
The codec of key key, as recordingCodecs() names it; Unspecified for an empty or unknown key.
static QMediaFormat::FileFormat container(VideoReceiver::FileFormat format)
The container of format.
void recordingFinished(const QString &file, const QString &error)
error is empty for a recording that finished normally.
QtVideoRecorder(QObject *parent=nullptr)
static QStringList recordingCodecs()
void recordingStarted(const QString &file)
void stopAndWait(std::chrono::milliseconds timeout=kFinishTimeout)
static QMediaRecorder::Quality recordingQuality(int value)
The quality of value value, one of VideoSettings::uvcRecordingQuality's; HighQuality for any other.
static QString recordingCodecName(const QString &key)
The name of the codec of key key, such as "H.264", for display.
void start(const QString &file, QMediaFormat::FileFormat container, QMediaFormat::VideoCodec codec=QMediaFormat::VideoCodec::Unspecified, QMediaRecorder::Quality quality=QMediaRecorder::HighQuality)
~QtVideoRecorder() override
bool active() const
Recording, or waiting for the first frame to encode.
FileFormat
Values are persisted by VideoSettings::recordingFormat.
static QString recordingComment()
The comment recordings carry, of GStreamer and of a USB camera alike: the application and its version...