-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathomtsndcli_v01.cpp
More file actions
2494 lines (2242 loc) · 73.6 KB
/
Copy pathomtsndcli_v01.cpp
File metadata and controls
2494 lines (2242 loc) · 73.6 KB
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* SPDX-License-Identifier: MIT */
/*
* omtsndcli_v01.cpp
*
* Canonical GitHub source snapshot for the current OMT sender CLI state.
*
* Licensing note
* --------------
* This repository source is intended to be distributed under the MIT License.
* Project notes indicate the sender lineage began as a derivative of the
* Open Media Transport C++ example sender, whose upstream Examples repository
* is also published under MIT. Third-party libraries and headers used by this
* program retain their own licenses.
*
* Source lineage
* --------------
* This file is the documented v01 snapshot derived directly from the proven
* working source `omtcli06_fix.cpp` and prepared as the canonical repository
* entry point for the Linux sender PC implementation.
*
* Project scope
* -------------
* - Linux OMT sender for Magewell HDMI capture devices
* - Video input via V4L2 (`-t v4l2:...`) or synthetic/original testcard
* - Audio input via ALSA (`-s alsa:...`) or built-in testcard noise
* - OMT network sender over TCP using libomt
* - Runtime stats, sender health, rolling 24 h exit summary, clean signal stop
*
* Validated environment from project handoff
* ------------------------------------------
* - Debian 13 sender PC
* - Host: qduv1
* - Capture hardware: 4x Magewell Pro Capture HDMI
* - Devices observed as /dev/video0 .. /dev/video3
* - OMT libs already built and verified: libvmx, libomtnet, libomt
* - Interop verified against vMix: discovery, test video/audio, live V4L2,
* live ALSA HDMI audio
*
* Architectural intent
* --------------------
* Linux sender PC -> Magewell Pro Capture HDMI -> V4L2 / ALSA -> OMT sender
*
* The sender is intentionally small and direct. Preferred behavior is to pass
* through formats that OMT can send natively and only perform limited local
* conversion where explicitly implemented.
*
* Current CLI surface
* -------------------
* General help:
* ./omtsndcli_v01 -h
*
* Video help:
* ./omtsndcli_v01 -t help
* ./omtsndcli_v01 -t testcard:help
* ./omtsndcli_v01 -t v4l2:help
*
* Audio help:
* ./omtsndcli_v01 -s help
* ./omtsndcli_v01 -s testcard:help
* ./omtsndcli_v01 -s alsa:help
*
* Examples:
* ./omtsndcli_v01 -t testcard
* ./omtsndcli_v01 -t testcard -s testcard
* ./omtsndcli_v01 -t v4l2:device=/dev/video0
* ./omtsndcli_v01 -t v4l2:device=/dev/video0:name=Cam1:format=YUY2:b=mid
* ./omtsndcli_v01 -t v4l2:device=/dev/video0 -s alsa:hw:2,0
*
* Quality mapping
* ---------------
* User-facing bitrate / quality aliases map to OMT quality as follows:
* low | 1 -> OMTQuality_Low
* mid | 2 -> OMTQuality_Medium
* high | 3 -> OMTQuality_High
*
* Operational notes
* -----------------
* - `late` in sender health means `omt_send()` exceeded the frame budget.
* - `late` is not the same as `dropped`.
* - This source already contains V4L2 format discovery/classification,
* optional RGB->BGRA conversion paths, ALSA capture, test generators,
* runtime statistics, and 24 h rolling interval summarization.
*
* Build example
* -------------
* clang++ -O3 -std=c++17 -o omtsndcli_v01 omtsndcli_v01.cpp \
* -L. -lomt -lasound -Wl,-rpath,'$ORIGIN'
*/
#include <iostream>
#include <chrono>
#include <thread>
#include <fstream>
#include <vector>
#include <map>
#include <string>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <algorithm>
#include <cstdint>
#include <cerrno>
#include <stdexcept>
#include <sstream>
#include <filesystem>
#include <memory>
#include <iomanip>
#include <deque>
#include <csignal>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <linux/videodev2.h>
#include <alsa/asoundlib.h>
// The header for the C/C++ wrapper of OMT
#include "libomt.h"
using namespace std;
static volatile sig_atomic_t g_stop_requested = 0;
static void handle_signal(int)
{
g_stop_requested = 1;
}
struct CaptureBuffer
{
void* start = nullptr;
size_t length = 0;
};
struct ParsedSpec
{
std::string driver;
std::map<std::string, std::string> kv;
};
enum class VideoSourceType
{
Testcard,
V4L2,
};
enum class AudioSourceType
{
None,
Testcard,
ALSA,
};
struct VideoConfig
{
VideoSourceType type = VideoSourceType::Testcard;
std::string device = "/dev/video0";
std::string name;
std::string format = "YUY2";
std::string file = "california-1080-uyvy.yuv";
int width = 1920;
int height = 1080;
int fps = 50;
std::string bitrate = "mid";
int qualityCode = 0;
std::map<std::string, std::string> controls;
};
struct AudioConfig
{
AudioSourceType type = AudioSourceType::None;
std::string device = "default";
int rate = 48000;
int channels = 2;
double volumeDb = -20.0;
};
enum class V4L2ConversionMode
{
None,
RGB4_TO_BGRA,
RGB3_TO_BGRA,
BGR3_TO_BGRA,
};
struct V4L2State
{
int fd = -1;
uint32_t pixelformat = 0;
OMTCodec outputCodec = OMTCodec_UYVY;
V4L2ConversionMode conversionMode = V4L2ConversionMode::None;
std::vector<CaptureBuffer> buffers;
size_t inputStride = 0;
size_t inputFrameBytes = 0;
};
struct TestcardVideoState
{
std::vector<uint8_t> baseFrame;
std::vector<uint8_t> movingLines;
size_t linePos = 0;
};
struct ALSAState
{
snd_pcm_t* pcm = nullptr;
std::vector<int16_t> interleaved;
};
struct TestcardAudioState
{
std::vector<float> samples;
float gain = 0.1f;
};
static int xioctl(int fd, unsigned long request, void* arg)
{
int r;
do
{
r = ioctl(fd, request, arg);
} while (r == -1 && errno == EINTR);
return r;
}
static float rand_FloatRange(float a, float b)
{
return ((b - a) * ((float)rand() / (float)RAND_MAX)) + a;
}
static std::vector<std::string> split(const std::string& text, char sep)
{
std::vector<std::string> out;
size_t start = 0;
while (start <= text.size())
{
size_t end = text.find(sep, start);
out.push_back(text.substr(start, end == std::string::npos ? std::string::npos : end - start));
if (end == std::string::npos)
{
break;
}
start = end + 1;
}
return out;
}
static std::string trim(const std::string& s)
{
size_t start = 0;
while (start < s.size() && std::isspace(static_cast<unsigned char>(s[start])))
{
++start;
}
size_t end = s.size();
while (end > start && std::isspace(static_cast<unsigned char>(s[end - 1])))
{
--end;
}
return s.substr(start, end - start);
}
static std::string to_lower(std::string s)
{
std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) { return static_cast<char>(std::tolower(c)); });
return s;
}
static std::string normalize_control_name(const std::string& s)
{
std::string out;
for (unsigned char c : s)
{
if (std::isalnum(c))
{
out.push_back(static_cast<char>(std::tolower(c)));
}
else if (c == ' ' || c == '-' || c == '_' || c == '.')
{
out.push_back('_');
}
}
return out;
}
static std::string fourcc_to_string(uint32_t fourcc)
{
char out[5] = {
static_cast<char>(fourcc & 0xFF),
static_cast<char>((fourcc >> 8) & 0xFF),
static_cast<char>((fourcc >> 16) & 0xFF),
static_cast<char>((fourcc >> 24) & 0xFF),
0
};
return std::string(out);
}
static ParsedSpec parse_spec(const std::string& spec)
{
ParsedSpec out;
size_t firstColon = spec.find(':');
if (firstColon == std::string::npos)
{
out.driver = spec;
return out;
}
out.driver = spec.substr(0, firstColon);
std::string rest = spec.substr(firstColon + 1);
auto tokens = split(rest, ':');
for (const auto& tokenRaw : tokens)
{
std::string token = trim(tokenRaw);
if (token.empty())
{
continue;
}
if (token == "help")
{
out.kv["help"] = "1";
continue;
}
size_t eq = token.find('=');
if (eq != std::string::npos)
{
out.kv[token.substr(0, eq)] = token.substr(eq + 1);
}
else
{
if (!out.kv.count("device"))
{
out.kv["device"] = token;
}
else
{
out.kv["device"] += ":" + token;
}
}
}
return out;
}
static int get_int(const std::map<std::string, std::string>& kv, const std::string& key, int fallback)
{
auto it = kv.find(key);
if (it == kv.end())
{
return fallback;
}
return std::stoi(it->second);
}
static double get_double(const std::map<std::string, std::string>& kv, const std::string& key, double fallback)
{
auto it = kv.find(key);
if (it == kv.end())
{
return fallback;
}
return std::stod(it->second);
}
static std::string get_string(const std::map<std::string, std::string>& kv, const std::string& key, const std::string& fallback)
{
auto it = kv.find(key);
if (it == kv.end())
{
return fallback;
}
return it->second;
}
static std::string get_string_any(const std::map<std::string, std::string>& kv,
std::initializer_list<const char*> keys,
const std::string& fallback)
{
for (const char* key : keys)
{
auto it = kv.find(key);
if (it != kv.end())
{
return it->second;
}
}
return fallback;
}
static int get_int_any(const std::map<std::string, std::string>& kv,
std::initializer_list<const char*> keys,
int fallback)
{
for (const char* key : keys)
{
auto it = kv.find(key);
if (it != kv.end())
{
return std::stoi(it->second);
}
}
return fallback;
}
enum class FormatSupportKind
{
Passthrough,
ConversionOnly,
Unsupported,
};
struct FormatSupport
{
FormatSupportKind kind = FormatSupportKind::Unsupported;
std::string target;
};
static bool stdout_supports_color()
{
return isatty(STDOUT_FILENO) != 0;
}
static std::string colorize_text(const std::string& text, const char* code)
{
if (!stdout_supports_color())
{
return text;
}
return std::string(code) + text + "\033[0m";
}
static std::string yellow_text(const std::string& text)
{
return colorize_text(text, "\033[33m");
}
static std::string white_text(const std::string& text)
{
return colorize_text(text, "\033[97m");
}
static std::string green_text(const std::string& text)
{
return colorize_text(text, "\033[32m");
}
static std::string red_text(const std::string& text)
{
return colorize_text(text, "\033[31m");
}
static std::string cyan_text(const std::string& text)
{
return colorize_text(text, "\033[36m");
}
static FormatSupport classify_v4l2_format(uint32_t pixelformat)
{
if (pixelformat == V4L2_PIX_FMT_YUYV)
{
return {FormatSupportKind::Passthrough, "YUY2"};
}
if (pixelformat == V4L2_PIX_FMT_UYVY)
{
return {FormatSupportKind::Passthrough, "UYVY"};
}
#ifdef V4L2_PIX_FMT_NV12
if (pixelformat == V4L2_PIX_FMT_NV12)
{
return {FormatSupportKind::Passthrough, "NV12"};
}
#endif
#ifdef V4L2_PIX_FMT_YVU420
if (pixelformat == V4L2_PIX_FMT_YVU420)
{
return {FormatSupportKind::Passthrough, "YV12"};
}
#endif
#ifdef V4L2_PIX_FMT_YUV420
if (pixelformat == V4L2_PIX_FMT_YUV420)
{
return {FormatSupportKind::Unsupported, ""};
}
#endif
#ifdef V4L2_PIX_FMT_BGR32
if (pixelformat == V4L2_PIX_FMT_BGR32)
{
return {FormatSupportKind::Passthrough, "BGRA"};
}
#endif
#ifdef V4L2_PIX_FMT_RGB32
if (pixelformat == V4L2_PIX_FMT_RGB32)
{
return {FormatSupportKind::ConversionOnly, "BGRA"};
}
#endif
#ifdef V4L2_PIX_FMT_RGB24
if (pixelformat == V4L2_PIX_FMT_RGB24)
{
return {FormatSupportKind::ConversionOnly, "BGRA"};
}
#endif
#ifdef V4L2_PIX_FMT_BGR24
if (pixelformat == V4L2_PIX_FMT_BGR24)
{
return {FormatSupportKind::ConversionOnly, "BGRA"};
}
#endif
return {FormatSupportKind::Unsupported, ""};
}
static std::string format_support_suffix(const FormatSupport& support)
{
switch (support.kind)
{
case FormatSupportKind::Passthrough:
return "[OMT passthrough: yes -> " + support.target + "]";
case FormatSupportKind::ConversionOnly:
return "[OMT conversion only -> " + support.target + "]";
default:
return "[not sendable by this sender without conversion]";
}
}
static std::string colorized_format_line(const std::string& base, const FormatSupport& support)
{
std::string text = base + " " + format_support_suffix(support);
switch (support.kind)
{
case FormatSupportKind::Passthrough:
return colorize_text(text, "\033[32m");
case FormatSupportKind::ConversionOnly:
return colorize_text(text, "\033[38;5;208m");
default:
return colorize_text(text, "\033[31m");
}
}
static int parse_quality_code(const std::string& value)
{
std::string v = to_lower(trim(value));
if (v.empty() || v == "default" || v == "mid" || v == "medium" || v == "2")
{
return static_cast<int>(OMTQuality_Medium);
}
if (v == "low" || v == "1")
{
return static_cast<int>(OMTQuality_Low);
}
if (v == "high" || v == "3")
{
return static_cast<int>(OMTQuality_High);
}
throw std::runtime_error("Unsupported bitrate/quality value: " + value + " (use low|mid|high or 1|2|3)");
}
static std::string normalize_quality_label(const std::string& value)
{
std::string v = to_lower(trim(value));
if (v.empty() || v == "default" || v == "mid" || v == "medium" || v == "2")
{
return "mid";
}
if (v == "low" || v == "1")
{
return "low";
}
if (v == "high" || v == "3")
{
return "high";
}
throw std::runtime_error("Unsupported bitrate/quality value: " + value + " (use low|mid|high or 1|2|3)");
}
static std::string quality_label_from_code(int qualityCode)
{
if (qualityCode == static_cast<int>(OMTQuality_Low))
{
return "Low";
}
if (qualityCode == static_cast<int>(OMTQuality_Medium))
{
return "Medium";
}
if (qualityCode == static_cast<int>(OMTQuality_High))
{
return "High";
}
if (qualityCode == static_cast<int>(OMTQuality_Default))
{
return "Default";
}
return "Unknown";
}
static std::string format_decimal(double value, int decimals)
{
std::ostringstream oss;
oss << std::fixed << std::setprecision(decimals) << value;
return oss.str();
}
static std::string white_or_red_uint64(uint64_t value)
{
std::string s = std::to_string(value);
return value == 0 ? white_text(s) : red_text(s);
}
static std::string white_or_red_double(double value, int decimals)
{
std::string s = format_decimal(value, decimals);
return value == 0.0 ? white_text(s) : red_text(s);
}
struct RollingInterval
{
std::chrono::steady_clock::time_point endTime;
double elapsedSeconds = 0.0;
uint64_t videoFrames = 0;
uint64_t videoBytes = 0;
uint64_t audioSamplesPerChannel = 0;
uint64_t capturedFrames = 0;
uint64_t sentFrames = 0;
uint64_t droppedFrames = 0;
uint64_t lateFrames = 0;
double sendTimeSumMs = 0.0;
double sendTimeMaxMs = 0.0;
};
static void trim_rolling_intervals(std::deque<RollingInterval>& intervals,
const std::chrono::steady_clock::time_point& now)
{
const auto window = std::chrono::hours(24);
while (!intervals.empty() && (now - intervals.front().endTime) > window)
{
intervals.pop_front();
}
}
static void accumulate_audio_levels(const OMTMediaFrame& audio_frame,
std::vector<double>& sumSquares,
std::vector<double>& peaks)
{
const float* samples = static_cast<const float*>(audio_frame.Data);
if (!samples)
{
return;
}
for (int s = 0; s < audio_frame.SamplesPerChannel; ++s)
{
for (int ch = 0; ch < audio_frame.Channels; ++ch)
{
size_t idx = static_cast<size_t>(s) * static_cast<size_t>(audio_frame.Channels) + static_cast<size_t>(ch);
double v = samples[idx];
sumSquares[ch] += v * v;
peaks[ch] = std::max(peaks[ch], std::abs(v));
}
}
}
static std::string audio_levels_line(const std::vector<double>& sumSquares,
const std::vector<double>& peaks,
uint64_t samplesPerChannelInInterval)
{
std::ostringstream oss;
oss << yellow_text("[Audio sender] Volume: ");
if (samplesPerChannelInInterval == 0 || sumSquares.empty())
{
oss << green_text("(no audio samples)");
return oss.str();
}
for (size_t ch = 0; ch < sumSquares.size(); ++ch)
{
double rms = std::sqrt(sumSquares[ch] / static_cast<double>(samplesPerChannelInInterval));
double peak = peaks[ch];
double rmsDb = 20.0 * std::log10(std::max(rms, 1e-12));
double peakDb = 20.0 * std::log10(std::max(peak, 1e-12));
if (ch > 0)
{
oss << " ";
}
std::ostringstream channel;
channel << "[" << ch << "] "
<< std::fixed << std::setprecision(2)
<< rmsDb << "/" << peakDb << " dBFS RMS/peak";
oss << green_text(channel.str());
}
return oss.str();
}
static std::string sender_health_line(uint64_t capturedFrames,
uint64_t sentFrames,
uint64_t droppedFrames,
uint64_t lateFrames,
double avgSendMs,
double maxSendMs)
{
std::ostringstream oss;
oss << white_text("-----------------------------------------------------------------------------") << "\n";
oss << white_text(" Sender health : ")
<< white_text("captured=") << white_text(std::to_string(capturedFrames))
<< white_text(" sent=") << white_text(std::to_string(sentFrames))
<< white_text(" dropped=") << white_or_red_uint64(droppedFrames)
<< white_text(" late=") << white_or_red_uint64(lateFrames)
<< white_text(" omt_send avg/max=") << white_text(format_decimal(avgSendMs, 3))
<< white_text("/") << white_or_red_double(maxSendMs, 3)
<< white_text(" ms");
return oss.str();
}
static void print_exit_summary(const std::deque<RollingInterval>& intervals,
bool audioEnabled)
{
double seconds = 0.0;
uint64_t videoFrames = 0;
uint64_t videoBytes = 0;
uint64_t audioSamples = 0;
uint64_t capturedFrames = 0;
uint64_t sentFrames = 0;
uint64_t droppedFrames = 0;
uint64_t lateFrames = 0;
double sendTimeSumMs = 0.0;
double sendTimeMaxMs = 0.0;
for (const auto& item : intervals)
{
seconds += item.elapsedSeconds;
videoFrames += item.videoFrames;
videoBytes += item.videoBytes;
audioSamples += item.audioSamplesPerChannel;
capturedFrames += item.capturedFrames;
sentFrames += item.sentFrames;
droppedFrames += item.droppedFrames;
lateFrames += item.lateFrames;
sendTimeSumMs += item.sendTimeSumMs;
sendTimeMaxMs = std::max(sendTimeMaxMs, item.sendTimeMaxMs);
}
std::cout << "\n";
std::cout << yellow_text("Last 24h summary") << "\n";
if (seconds <= 0.0)
{
std::cout << yellow_text(" No interval statistics collected.") << "\n";
return;
}
double avgFps = static_cast<double>(videoFrames) / seconds;
double avgBitrateMbit = (static_cast<double>(videoBytes) * 8.0) / seconds / 1000000.0;
double avgSendMs = sentFrames > 0 ? (sendTimeSumMs / static_cast<double>(sentFrames)) : 0.0;
std::cout << yellow_text(" Window : ")
<< format_decimal(seconds / 3600.0, 2) << " h\n";
std::cout << yellow_text(" Video frames : ")
<< videoFrames << "\n";
std::cout << yellow_text(" Average FPS : ")
<< format_decimal(avgFps, 3) << "\n";
std::cout << yellow_text(" Average bitrate : ")
<< cyan_text(format_decimal(avgBitrateMbit, 3) + " Mbit/s") << "\n";
std::cout << white_text(" Sender health : ")
<< white_text("captured=") << white_text(std::to_string(capturedFrames))
<< white_text(" sent=") << white_text(std::to_string(sentFrames))
<< white_text(" dropped=") << white_or_red_uint64(droppedFrames)
<< white_text(" late=") << white_or_red_uint64(lateFrames)
<< white_text(" omt_send avg/max=") << white_text(format_decimal(avgSendMs, 3))
<< white_text("/") << white_or_red_double(sendTimeMaxMs, 3)
<< white_text(" ms") << "\n";
if (audioEnabled)
{
std::cout << yellow_text(" Audio samples : ")
<< audioSamples << " samples/channel\n";
}
}
static std::string basename_like(const std::string& path)
{
size_t pos = path.find_last_of('/');
if (pos == std::string::npos)
{
return path;
}
if (pos + 1 >= path.size())
{
return path;
}
return path.substr(pos + 1);
}
static VideoConfig build_video_config(const std::string& spec)
{
ParsedSpec parsed = parse_spec(spec);
VideoConfig cfg;
if (parsed.driver.empty() || parsed.driver == "testcard")
{
cfg.type = VideoSourceType::Testcard;
cfg.width = get_int(parsed.kv, "width", 1920);
cfg.height = get_int(parsed.kv, "height", 1080);
cfg.fps = get_int(parsed.kv, "fps", 60);
cfg.format = get_string_any(parsed.kv, {"format", "fmt"}, "UYVY");
cfg.file = get_string(parsed.kv, "file", "california-1080-uyvy.yuv");
cfg.name = get_string(parsed.kv, "name", "Testcard");
cfg.bitrate = normalize_quality_label(get_string_any(parsed.kv, {"bitrate", "b"}, "mid"));
cfg.qualityCode = parse_quality_code(cfg.bitrate);
return cfg;
}
if (parsed.driver == "v4l2")
{
cfg.type = VideoSourceType::V4L2;
cfg.device = get_string_any(parsed.kv, {"device", "d"}, "/dev/video0");
cfg.width = get_int(parsed.kv, "width", 1920);
cfg.height = get_int(parsed.kv, "height", 1080);
cfg.fps = get_int(parsed.kv, "fps", 50);
cfg.format = get_string_any(parsed.kv, {"format", "fmt"}, "YUY2");
cfg.name = get_string(parsed.kv, "name", "Magewell " + basename_like(cfg.device));
cfg.bitrate = normalize_quality_label(get_string_any(parsed.kv, {"bitrate", "b"}, "mid"));
cfg.qualityCode = parse_quality_code(cfg.bitrate);
for (const auto& [key, value] : parsed.kv)
{
if (key.rfind("ctrl.", 0) == 0)
{
cfg.controls[key.substr(5)] = value;
}
else if (key.rfind("control.", 0) == 0)
{
cfg.controls[key.substr(8)] = value;
}
}
return cfg;
}
throw std::runtime_error("Unsupported video transport: " + parsed.driver);
}
static AudioConfig build_audio_config(const std::string& spec)
{
ParsedSpec parsed = parse_spec(spec);
AudioConfig cfg;
if (parsed.driver.empty() || parsed.driver == "testcard")
{
cfg.type = AudioSourceType::Testcard;
cfg.rate = get_int(parsed.kv, "rate", 48000);
cfg.channels = get_int(parsed.kv, "channels", 2);
cfg.volumeDb = get_double(parsed.kv, "volume", -20.0);
return cfg;
}
if (parsed.driver == "alsa")
{
cfg.type = AudioSourceType::ALSA;
cfg.device = get_string(parsed.kv, "device", "default");
cfg.rate = get_int(parsed.kv, "rate", 48000);
cfg.channels = get_int(parsed.kv, "channels", 2);
cfg.volumeDb = get_double(parsed.kv, "volume", 0.0);
return cfg;
}
throw std::runtime_error("Unsupported audio source: " + parsed.driver);
}
static uint32_t v4l2_fourcc_from_string(const std::string& format)
{
std::string f = to_lower(format);
if (f == "yuy2" || f == "yuyv")
{
return V4L2_PIX_FMT_YUYV;
}
if (f == "uyvy")
{
return V4L2_PIX_FMT_UYVY;
}
#ifdef V4L2_PIX_FMT_BGR32
if (f == "bgr4" || f == "bgra")
{
return V4L2_PIX_FMT_BGR32;
}
#endif
#ifdef V4L2_PIX_FMT_RGB32
if (f == "rgb4" || f == "argb" || f == "xrgb")
{
return V4L2_PIX_FMT_RGB32;
}
#endif
#ifdef V4L2_PIX_FMT_NV12
if (f == "nv12")
{
return V4L2_PIX_FMT_NV12;
}
#endif
#ifdef V4L2_PIX_FMT_YVU420
if (f == "yv12")
{
return V4L2_PIX_FMT_YVU420;
}
#endif
#ifdef V4L2_PIX_FMT_YUV420
if (f == "yu12" || f == "i420")
{
return V4L2_PIX_FMT_YUV420;
}
#endif
#ifdef V4L2_PIX_FMT_RGB24
if (f == "rgb3" || f == "rgb24")
{
return V4L2_PIX_FMT_RGB24;
}
#endif
#ifdef V4L2_PIX_FMT_BGR24
if (f == "bgr3" || f == "bgr24")
{
return V4L2_PIX_FMT_BGR24;
}
#endif
#ifdef V4L2_PIX_FMT_GREY
if (f == "grey" || f == "gray")
{
return V4L2_PIX_FMT_GREY;
}
#endif
#ifdef V4L2_PIX_FMT_Y16
if (f == "y16")
{
return V4L2_PIX_FMT_Y16;
}
#endif
#ifdef V4L2_PIX_FMT_YUV32
if (f == "yuv4")
{
return V4L2_PIX_FMT_YUV32;
}
#endif
return 0;
}
static bool omt_codec_from_v4l2(uint32_t pixelformat, OMTCodec& codec)
{
if (pixelformat == V4L2_PIX_FMT_YUYV)
{
codec = OMTCodec_YUY2;
return true;
}
if (pixelformat == V4L2_PIX_FMT_UYVY)
{
codec = OMTCodec_UYVY;
return true;
}
#ifdef V4L2_PIX_FMT_NV12
if (pixelformat == V4L2_PIX_FMT_NV12)
{
codec = OMTCodec_NV12;
return true;
}
#endif
#ifdef V4L2_PIX_FMT_YVU420
if (pixelformat == V4L2_PIX_FMT_YVU420)
{
codec = OMTCodec_YV12;
return true;
}
#endif
#ifdef V4L2_PIX_FMT_BGR32
if (pixelformat == V4L2_PIX_FMT_BGR32)
{
codec = OMTCodec_BGRA;
return true;
}
#endif
return false;
}
static bool omt_codec_from_testcard_format(const std::string& format, OMTCodec& codec)
{
std::string f = to_lower(format);
if (f == "uyvy")
{
codec = OMTCodec_UYVY;
return true;
}
if (f == "yuy2" || f == "yuyv")
{