1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
|
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include "commandlineparser.h"
#include <QApplication>
namespace {
bool contains(const QStringList &list, const QStringView &str)
{
return list.contains(str, Qt::CaseInsensitive);
}
bool equals(const QStringView &a, const QStringView &b)
{
return a.compare(b, Qt::CaseInsensitive) == 0;
}
template <typename Container, typename Separator, typename ToString>
QString join(const Container &container, const Separator &separator, ToString &&toString)
{
QString result;
for (const auto &element : container) {
if (!result.isEmpty())
result += separator;
result += toString(element);
}
return result;
}
template <typename Numbers, typename Separator>
QString joinNumberElements(const Numbers &numbers, const Separator &separator)
{
return join(numbers, separator, [](auto number) { return QString::number(number); });
}
template <typename Map, typename Separator>
QString joinMapStringValues(const Map &map, const Separator &separator)
{
return join(map, separator, [](const auto &pair) { return pair.second; });
}
auto toUInt(const QString &value, bool &ok)
{
return value.toUInt(&ok);
}
auto toUIntList(const QString &value, bool &ok)
{
std::vector<uint32_t> result;
const QStringList split = value.split(',');
ok = !split.isEmpty();
for (auto it = split.cbegin(); ok && it != split.cend(); ++it)
result.push_back(it->toUInt(&ok));
return result;
}
auto toSize(const QString &value, bool &ok)
{
const QStringList xy = value.split('x');
ok = xy.size() == 2;
QSize result;
if (ok)
result.setWidth(xy.front().toUInt(&ok));
if (ok)
result.setHeight(xy.back().toUInt(&ok));
return result;
}
auto toPositiveFloat(const QString &value, bool &ok)
{
const float result = value.toFloat(&ok);
ok = ok && result > 0;
return result;
}
auto toUrl(const QString &value, [[maybe_unused]] bool &ok)
{
return QUrl::fromLocalFile(value);
}
auto toStreams(const QStringList &streams, const QString &value, bool &ok)
{
const QStringList result = value.split(',');
ok = !result.empty() && std::all_of(result.cbegin(), result.cend(), [&](const QString &s) {
return streams.contains(s, Qt::CaseInsensitive);
});
return result;
}
auto toMode(const QStringList &modes, const QString &value, bool &ok)
{
ok = modes.contains(value, Qt::CaseInsensitive);
return value;
}
auto toEnum = [](const auto &enumMap, const QString &value, bool &ok) {
auto found = std::find_if(enumMap.begin(), enumMap.end(),
[&](const auto &pair) { return equals(pair.second, value); });
ok = found != enumMap.end();
return ok ? found->first : decltype(found->first)();
};
template <typename Duration>
auto toMsCount(Duration duration)
{
return std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
}
const char *noCheck()
{
return nullptr;
};
} // namespace
CommandLineParser::CommandLineParser()
: m_result{ AudioGeneratorSettings{}, VideoGeneratorSettings{}, PushModeSettings{}, {} }
{
m_parser.setApplicationDescription(QStringLiteral(
"The application tests QtMultimedia media frame inputs with media "
"recording and shows a simplistic result preview via the media player."));
m_parser.addHelpOption();
}
CommandLineParser::Result CommandLineParser::process()
{
m_parser.process(*QApplication::instance());
parseCommonSettings(); // parse common settings first
parseAudioGeneratorSettings();
parseVideoGeneratorSettings();
parsePushModeSettings();
parseRecorderSettings();
return std::move(m_result);
}
void CommandLineParser::parseCommonSettings()
{
if (auto duration = parsedValue(m_options.duration, noCheck, toUInt)) {
m_result.audioGeneratorSettings->duration = std::chrono::milliseconds(*duration);
m_result.videoGeneratorSettings->duration = std::chrono::milliseconds(*duration);
}
if (auto streams = parsedValue(m_options.streams, noCheck, toStreams, m_streams)) {
if (!contains(*streams, m_audioStream))
m_result.audioGeneratorSettings.reset();
if (!contains(*streams, m_videoStream))
m_result.videoGeneratorSettings.reset();
}
if (auto mode = parsedValue(m_options.mode, noCheck, toMode, m_modes))
m_mode = *mode;
if (!equals(m_mode, m_pushMode))
m_result.pushModeSettings.reset();
}
void CommandLineParser::parseAudioGeneratorSettings()
{
AudioGeneratorSettingsOpt &settings = m_result.audioGeneratorSettings;
auto check = [&settings]() -> const char * {
return settings ? nullptr : "audio stream is not enabled";
};
if (auto frequencies = parsedValue(m_options.channelFrequencies, check, toUIntList))
settings->channelFrequencies = std::move(*frequencies);
if (auto bufferDuration = parsedValue(m_options.audioBufferDuration, check, toUInt))
settings->bufferDuration = std::chrono::milliseconds(*bufferDuration);
if (auto format = parsedValue(m_options.sampleFormat, check, toEnum, m_sampleFormats))
settings->sampleFormat = *format;
if (auto config = parsedValue(m_options.channelConfig, check, toEnum, m_channelConfigs))
settings->channelConfig = *config;
}
void CommandLineParser::parseVideoGeneratorSettings()
{
VideoGeneratorSettingsOpt &settings = m_result.videoGeneratorSettings;
auto check = [&settings]() -> const char * {
return settings ? nullptr : "video stream is not enabled";
};
if (auto frameRate = parsedValue(m_options.frameRate, check, toUInt))
settings->frameRate = *frameRate;
if (auto resolution = parsedValue(m_options.resolution, check, toSize))
settings->resolution = *resolution;
if (auto patternWidth = parsedValue(m_options.framePatternWidth, check, toUInt))
settings->patternWidth = *patternWidth;
if (auto patternSpeed = parsedValue(m_options.framePatternSpeed, check, toUInt))
settings->patternSpeed = *patternSpeed;
}
void CommandLineParser::parsePushModeSettings()
{
PushModeSettingsOpt &settings = m_result.pushModeSettings;
auto check = [&settings]() -> const char * {
return settings ? nullptr : "push mode is not enabled";
};
if (auto producingRate = parsedValue(m_options.pushModeProducingRate, check, toPositiveFloat))
settings->producingRate = *producingRate;
if (auto maxQueueSize = parsedValue(m_options.pushModeMaxQueueSize, check, toUInt))
settings->maxQueueSize = *maxQueueSize;
}
void CommandLineParser::parseRecorderSettings()
{
RecorderSettings &settings = m_result.recorderSettings;
if (auto location = parsedValue(m_options.outputLocation, noCheck, toUrl))
settings.outputLocation = *location;
if (auto frameRate = parsedValue(m_options.recorderFrameRate, noCheck, toUInt))
settings.frameRate = *frameRate;
if (auto resolution = parsedValue(m_options.recorderResolution, noCheck, toSize))
settings.resolution = *resolution;
settings.quality = parsedValue(m_options.recorderQuality, noCheck, toEnum, m_recorderQualities);
settings.audioCodec = parsedValue(m_options.recorderAudioCodec, noCheck, toEnum, m_audioCodecs);
settings.videoCodec = parsedValue(m_options.recorderVideoCodec, noCheck, toEnum, m_videoCodecs);
settings.fileFormat = parsedValue(m_options.recorderFileFormat, noCheck, toEnum, m_fileFormats);
}
template <typename ContextChecker, typename... ResultGetter>
auto CommandLineParser::parsedValue(const QString &option, ContextChecker &&contextChecker,
ResultGetter &&...resultGetter)
-> std::optional<decltype(std::invoke(resultGetter..., QString(), std::declval<bool &>()))>
{
Q_ASSERT(!option.isEmpty());
if (!m_parser.isSet(option))
return {};
const QString value = m_parser.value(option);
if (value.isEmpty())
return {};
bool ok = true;
auto result = std::invoke(std::forward<ResultGetter>(resultGetter)..., value, ok);
if (!ok) {
qWarning() << "Invalid value for option" << option << ":" << value;
} else if (const auto error = contextChecker()) {
ok = false;
qWarning() << error << ", so the option" << option << "will not be applied";
}
if (!ok)
return {};
return { std::move(result) };
}
QString CommandLineParser::addOption(QCommandLineOption option)
{
m_parser.addOption(option);
return option.names().constFirst();
}
CommandLineParser::Options CommandLineParser::createOptions()
{
Options result;
Q_ASSERT(m_result.audioGeneratorSettings && m_result.videoGeneratorSettings
&& m_result.pushModeSettings);
const AudioGeneratorSettings &audioGeneratorSettings = *m_result.audioGeneratorSettings;
const VideoGeneratorSettings &videoGeneratorSettings = *m_result.videoGeneratorSettings;
const PushModeSettings &pushModeSettings = *m_result.pushModeSettings;
result.streams = addOption({ QStringLiteral("streams"),
QStringLiteral("Types of generated streams (%1).\nDefaults to %2.")
.arg(m_streams.join(u", "), m_streams.join(',')),
QStringLiteral("list of enum") });
result.mode = addOption({ QStringLiteral("mode"),
QStringLiteral("Media frames generation mode (%1).\nDefaults to %2.")
.arg(m_modes.join(u", "), m_pullMode),
QStringLiteral("list of enum") });
result.duration =
addOption({ { QStringLiteral("generator.duration"), QStringLiteral("duration") },
QStringLiteral("Media duration.\nDefaults to %1.")
.arg(toMsCount(audioGeneratorSettings.duration)),
QStringLiteral("ms") });
result.channelFrequencies = addOption(
{ { QStringLiteral("generator.frequencies"), QStringLiteral("frequencies") },
QStringLiteral("Generated audio frequencies for each channel. It's "
"possible to set one or several ones: x,y,z.\nDefaults to %1.")
.arg(joinNumberElements(audioGeneratorSettings.channelFrequencies, u",")),
QStringLiteral("list of hz") });
result.audioBufferDuration =
addOption({ { QStringLiteral("generator.audioBufferDuration"),
QStringLiteral("audioBufferDuration") },
QStringLiteral("Duration of single audio buffer.\nDefaults to %1.")
.arg(toMsCount(audioGeneratorSettings.bufferDuration)),
QStringLiteral("ms") });
result.sampleFormat = addOption(
{ { QStringLiteral("generator.sampleFormat"), QStringLiteral("sampleFormat") },
QStringLiteral("Generated audio sample format (%1).\nDefaults to %2.")
.arg(joinMapStringValues(m_sampleFormats, u", "),
m_sampleFormats.at(audioGeneratorSettings.sampleFormat)),
QStringLiteral("enum") });
result.channelConfig = addOption(
{ { QStringLiteral("generator.channelConfig"), QStringLiteral("channelConfig") },
QStringLiteral("Generated audio channel config (%1).\nDefaults to %2.")
.arg(joinMapStringValues(m_channelConfigs, u", "),
m_channelConfigs.at(audioGeneratorSettings.channelConfig)),
QStringLiteral("enum") });
result.frameRate =
addOption({ { QStringLiteral("generator.frameRate"), QStringLiteral("frameRate") },
QStringLiteral("Generated video frame rate.\nDefaults to %1.")
.arg(videoGeneratorSettings.frameRate),
QStringLiteral("uint") });
result.resolution = addOption(
{ { QStringLiteral("generator.resolution"), QStringLiteral("resolution") },
QStringLiteral("Generated video frame resolution.\nDefaults to %1x%2.")
.arg(QString::number(videoGeneratorSettings.resolution.width()),
QString::number(videoGeneratorSettings.resolution.height())),
QStringLiteral("WxH") });
result.framePatternWidth =
addOption({ { QStringLiteral("generator.framePatternWidth"),
QStringLiteral("framePatternWidth") },
QStringLiteral("Generated video frame patern pixel width.\nDefaults to %1.")
.arg(videoGeneratorSettings.patternWidth),
QStringLiteral("uint") });
result.framePatternSpeed = addOption(
{ { QStringLiteral("generator.framePatternSpeed"),
QStringLiteral("framePatternSpeed") },
QStringLiteral("Relative speed of moving the frame pattern.\nDefaults to %1.")
.arg(videoGeneratorSettings.patternSpeed),
QStringLiteral("float") });
result.pushModeProducingRate = addOption(
{ { QStringLiteral("generator.pushMode.producingRate"),
QStringLiteral("producingRate") },
QStringLiteral(
"Relative factor of media producing rate in push mode.\nDefaults to %1.")
.arg(pushModeSettings.producingRate),
QStringLiteral("positive float") });
result.pushModeMaxQueueSize =
addOption({ {
QStringLiteral("generator.pushMode.maxQueueSize"),
QStringLiteral("maxQueueSize"),
},
QStringLiteral("Max number of generated media frames in the queue on "
"the front of media recorder.\nDefaults to %1.")
.arg(pushModeSettings.maxQueueSize),
QStringLiteral("uint") });
result.outputLocation = addOption(
{ { QStringLiteral("recorder.outputLocation"), QStringLiteral("outputLocation") },
QStringLiteral("Output location for the media."),
QStringLiteral("file or directory") });
result.recorderFrameRate =
addOption({ QStringLiteral("recorder.frameRate"),
QStringLiteral("Video frame rate for media recorder.\nNo default value."),
QStringLiteral("uint") });
result.recorderQuality =
addOption({ { QStringLiteral("recorder.quality"), QStringLiteral("quality") },
QStringLiteral("Quality of media recording (%1).\nNo default value.")
.arg(joinMapStringValues(m_recorderQualities, u", ")),
QStringLiteral("enum") });
result.recorderResolution =
addOption({ QStringLiteral("recorder.resolution"),
QStringLiteral("Video resolution for media recorder.\nNo default value."),
QStringLiteral("WxH") });
result.recorderAudioCodec =
addOption({ { QStringLiteral("recorder.audioCodec"), QStringLiteral("audioCodec") },
QStringLiteral("Audio codec for media recorder. \nNo default value."),
QStringLiteral("enum") });
result.recorderVideoCodec =
addOption({ { QStringLiteral("recorder.videoCodec"), QStringLiteral("videoCodec") },
QStringLiteral("Video codec for media recorder.\nNo default value."),
QStringLiteral("enum") });
result.recorderFileFormat =
addOption({ { QStringLiteral("recorder.fileFormat"), QStringLiteral("fileFormat") },
QStringLiteral("File format for media recorder.\nNo default value."),
QStringLiteral("enum") });
return result;
}
|