-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
116 lines (99 loc) · 3.47 KB
/
Copy pathmain.cpp
File metadata and controls
116 lines (99 loc) · 3.47 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
#include "HttpServer.h"
#include "WebcamCapture.h"
#include <atomic>
#include <csignal>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#include <thread>
#include <vector>
#include <chrono>
namespace
{
std::atomic<bool> g_running{true};
void signalHandler(int)
{
g_running = false;
}
void printUsage(const char* argv0)
{
std::cerr << "Usage: " << argv0
<< " [device] [width] [height] [fps] [quality] [port] [--reencode]\n"
<< " device V4L2 device path (default /dev/video0)\n"
<< " width capture width (default 640)\n"
<< " height capture height (default 480)\n"
<< " fps capture fps (default 15)\n"
<< " quality MJPEG encoder qscale, only used with --reencode (2=best .. 31=worst, default 5)\n"
<< " port HTTP port (default 8080)\n"
<< " --reencode ask the camera for raw YUYV422 and decode/scale/encode it to\n"
<< " MJPEG ourselves via libavcodec, instead of requesting MJPEG\n"
<< " directly from the camera. Only needed if your webcam\n"
<< " doesn't support MJPEG natively - it costs real CPU.\n";
}
}
int main(int argc, char** argv)
{
std::string device = "/dev/video0";
int width = 640;
int height = 480;
int fps = 15;
int quality = 5;
int port = 8080;
bool reencode = false;
std::vector<std::string> positional;
for (int i = 1; i < argc; ++i)
{
std::string arg = argv[i];
if (arg == "--reencode")
{
reencode = true;
} else if (arg == "-h" || arg == "--help")
{
printUsage(argv[0]);
return 0;
}
else
{
positional.push_back(arg);
}
}
if (positional.size() > 0) device = positional[0];
if (positional.size() > 1) width = std::atoi(positional[1].c_str());
if (positional.size() > 2) height = std::atoi(positional[2].c_str());
if (positional.size() > 3) fps = std::atoi(positional[3].c_str());
if (positional.size() > 4) quality = std::atoi(positional[4].c_str());
if (positional.size() > 5) port = std::atoi(positional[5].c_str());
std::cerr << "usb-camera-web: " << device << " " << width << "x" << height
<< " @ " << fps << "fps, port " << port
<< (reencode ? ", software re-encode (quality " + std::to_string(quality) + ")"
: ", MJPEG passthrough (no re-encode)")
<< "\n";
std::signal(SIGINT, signalHandler);
std::signal(SIGTERM, signalHandler);
WebcamCapture camera;
if (!camera.init(device, width, height, fps, quality, reencode))
{
std::cerr << "Failed to open " << device << "\n";
return 1;
}
HttpServer server(port);
if (!server.start())
{
std::cerr << "Failed to start HTTP server on port " << port << "\n";
return 1;
}
camera.setFrameCallback([&](std::vector<uint8_t> jpegFrame)
{
server.pushFrame(std::move(jpegFrame));
});
camera.start();
std::cerr << "Streaming. Open http://<host-ip>:" << port << "/ in a browser.\n";
std::cerr << "Press Ctrl+C to stop.\n";
while (g_running)
std::this_thread::sleep_for(std::chrono::milliseconds(200));
std::cerr << "\nShutting down...\n";
camera.stop();
server.stop();
return 0;
}