-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
115 lines (94 loc) · 2.7 KB
/
main.cpp
File metadata and controls
115 lines (94 loc) · 2.7 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
#define STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include <iostream>
#include <cstring>
#include <csignal>
#include <fcntl.h>
#include "image/ImagePipeline.h"
#include "PhysicalCamera.h"
#include "VirtualCamera.h"
#include "chrono"
volatile sig_atomic_t keepRunning = 1;
void signalHandler(int)
{
keepRunning = 0;
}
void printConfig(ConfigCamera c)
{
char formatStr[5] = {0};
*reinterpret_cast<uint32_t *>(formatStr) = c.pixelFormat;
std::cout << "\n========== Applied Camera Config ==========\n";
std::cout << "[Resolution] " << c.width << " x " << c.height << "\n";
std::cout << "[Framerate] " << c.fps << " FPS\n";
std::cout << "[Pixel Format] " << formatStr << "\n";
std::cout << "===========================================\n\n";
}
int main(int argc, char *argv[])
{
// signal shutdown
signal(SIGINT, signalHandler);
signal(SIGTERM, signalHandler);
std::cout << "[INFO] Starting Camera System..." << std::endl;
// init camera
PhysicalCamera pc;
VirtualCamera vc;
if (!pc.openDevice("/dev/video0"))
{
return -1;
}
if (!vc.openDevice("/dev/video2"))
{
return -1;
}
ConfigCamera cfg = pc.getHighestConfig();
// config -> format
v4l2_format fmt{};
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
fmt.fmt.pix.width = cfg.width;
fmt.fmt.pix.height = cfg.height;
fmt.fmt.pix.pixelformat = cfg.pixelFormat;
fmt.fmt.pix.field = V4L2_FIELD_NONE;
if (!pc.setFormat(fmt))
{
return -1;
}
if (!vc.setFormat(fmt))
{
return -1;
}
printConfig(cfg);
if (!pc.setFramerate(cfg.fps))
{
return -1;
}
if (!pc.initMemory())
{
return -1;
}
ImagePipeline ip(cfg.pixelFormat, cfg.width, cfg.height);
ip.readArgs(argc, argv);
if (!pc.startStreaming())
{
return -1;
}
std::cout << "[INFO] Streaming started. Press Ctrl+C to stop.\n";
while (keepRunning)
{
auto start = std::chrono::high_resolution_clock::now();
void *frame = pc.getFrame();
if (!frame)
{
continue;
}
size_t bufferSize = pc.getBufferSize();
std::vector<uint8_t> processedBuffer = ip.process(frame, bufferSize, pc.getBufferMaxSize());
vc.writeBuffer(processedBuffer.data(), processedBuffer.size());
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::milli> delay = end - start;
std::cout << "\r[Pipeline Delay] " << delay.count() << " ms" << std::flush;
pc.returnBuffer();
}
std::cout << std::endl;
std::cout << "[SUCCESS] System stopped safely.\n";
return 0;
}