-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrecorder.cpp
311 lines (278 loc) · 10.5 KB
/
recorder.cpp
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
#ifdef LINUX
#include <X11/Xlib.h>
#include <X11/cursorfont.h>
#endif
#ifdef WINDOWS
#include "conio.h"
#endif
#include <libcapture/capturer.h>
#include <libcapture/video_parameters.h>
#ifndef WINDOWS
#include <poll.h>
#endif
#include <unistd.h>
#include <cassert>
#include <chrono>
#include <cstdio>
#include <filesystem>
#include <iostream>
#include <stdexcept>
#include <string>
#include <thread>
#include <vector>
#ifndef WINDOWS
#include "console_setter.h"
#endif
#include "thread_guard.h"
#define DEFAULT_DEVICES 0
VideoParameters parseVideoSize(const std::string &str) {
VideoParameters dims;
auto main_delim_pos = str.find(':');
{
std::string video_size = str.substr(0, main_delim_pos);
auto delim_pos = video_size.find('x');
if (delim_pos == std::string::npos) throw std::runtime_error("Wrong video-size format");
int width = std::stoi(video_size.substr(0, delim_pos));
int height = std::stoi(video_size.substr(delim_pos + 1));
dims.setVideoSize(width, height);
}
if (main_delim_pos != std::string::npos) {
std::string offsets = str.substr(main_delim_pos + 1);
auto delim_pos = offsets.find(',');
if (delim_pos == std::string::npos) throw std::runtime_error("Wrong offsets");
int offset_x = std::stoi(offsets.substr(0, delim_pos));
int offset_y = std::stoi(offsets.substr(delim_pos + 1));
dims.setVideoOffset(offset_x, offset_y);
}
return dims;
}
/**
* Parse arguments
* @param args a vector containing the arguments to parse
* @return a tuple containing the video device name, audio device name, video parameters, output file and verboseness,
* in this order
*/
std::tuple<std::string, std::string, VideoParameters, std::string, bool> parseArgs(std::vector<std::string> args) {
VideoParameters video_params;
int framerate = 30;
std::string video_device;
std::string audio_device;
std::string output_file;
#if DEFAULT_DEVICES
#if defined(WINDOWS)
video_device = "screen-capture-recorder";
audio_device = "Gruppo microfoni (Realtek High Definition Audio(SST))";
#elif defined(LINUX)
{
std::stringstream ss;
ss << getenv("DISPLAY") << ".0";
video_device = ss.str();
}
audio_device = "hw:0,0";
#else
video_device = "1";
audio_device = "0";
#endif
#endif
bool video_device_set = false;
bool audio_device_set = false;
bool video_size_set = false;
bool framerate_set = false;
bool output_set = false;
bool verbose = false;
std::string wrong_args_msg("Wrong arguments");
for (auto it = args.begin(); it != args.end(); it++) {
if (*it == "-h") {
throw std::runtime_error("");
} else if (*it == "-video_device") {
if (video_device_set || ++it == args.end()) throw std::runtime_error(wrong_args_msg);
video_device = *it;
video_device_set = true;
} else if (*it == "-audio_device") {
if (audio_device_set || ++it == args.end()) throw std::runtime_error(wrong_args_msg);
audio_device = *it;
audio_device_set = true;
} else if (*it == "-video_size") {
if (video_size_set || ++it == args.end()) throw std::runtime_error(wrong_args_msg);
video_params = parseVideoSize(*it);
video_size_set = true;
} else if (*it == "-framerate") {
if (framerate_set || ++it == args.end()) throw std::runtime_error(wrong_args_msg);
framerate = std::stoi(*it);
framerate_set = true;
} else if (*it == "-o") {
if (output_set || ++it == args.end()) throw std::runtime_error(wrong_args_msg);
output_file = *it;
output_set = true;
} else if (*it == "-v") {
verbose = true;
} else {
throw std::runtime_error("Unknown arg: " + *it);
}
}
video_params.setFramerate(framerate);
if (!output_set) {
output_file = "output.mp4";
std::cout << "No output file specified, saving to '" << output_file << "'" << std::endl;
}
if (verbose) {
std::cout << "Parsed video device: " << video_device << std::endl;
std::cout << "Parsed audio device: " << audio_device;
#if DEFAULT_DEVICES
if (audio_device == "none") {
audio_device = "";
std::cout << " (mute)";
}
#endif
std::cout << std::endl;
if (framerate_set) {
std::cout << "Parsed framerate: " << framerate << std::endl;
}
if (video_size_set) {
auto [width, height] = video_params.getVideoSize();
auto [offset_x, offset_y] = video_params.getVideoOffset();
std::cout << "Parsed video size: " << width << "x" << height << std::endl;
std::cout << "Parsed video offset: " << offset_x << "," << offset_y << std::endl;
}
if (output_set) {
std::cout << "Parsed output file: " << output_file << std::endl;
}
}
return std::make_tuple(video_device, audio_device, video_params, output_file, verbose);
}
static void printStatus(bool paused) {
std::cout << std::endl;
if (paused) {
std::cout << "Paused";
} else {
std::cout << "Recording...";
}
std::cout << std::endl;
}
static void printMenu(bool paused) {
if (paused) {
std::cout << "[r]esume";
} else {
std::cout << "[p]ause";
}
std::cout << ", [s]top: " << std::flush;
}
int main(int argc, char **argv) {
VideoParameters video_params;
std::string output_file;
std::string video_device;
std::string audio_device;
bool verbose = false;
try {
std::vector<std::string> args;
for (int i = 1; i < argc; i++) args.emplace_back(argv[i]);
std::tie(video_device, audio_device, video_params, output_file, verbose) = parseArgs(args);
} catch (const std::exception &e) {
std::string msg(e.what());
if (msg != "") std::cerr << "ERROR: " << msg << std::endl;
std::cerr << "Usage: " << argv[0] << std::endl;
std::cerr << "\t[-h]" << std::endl;
std::cerr << "\t[-video_device <device_name>]" << std::endl;
std::cerr << "\t[-audio_device <device_name>]" << std::endl;
std::cerr << "\t[-video_size <width>x<height>:<offset_x>,<offset_y>]" << std::endl;
std::cerr << "\t[-framerate <framerate>]" << std::endl;
std::cerr << "\t[-o <output_file>]" << std::endl;
std::cerr << "\t[-v]" << std::endl;
return 1;
}
try {
Capturer capturer(verbose);
if (video_device.empty()) {
std::cerr << "ERROR: No video device specified" << std::endl << std::endl;
capturer.listAvailableDevices();
return 1;
}
if (std::filesystem::exists(output_file)) {
std::cout << "The output file '" << output_file << "' already exists, overwrite it? [y/N] ";
std::string answer;
std::getline(std::cin, answer);
if (answer != "y" && answer != "Y") return 0;
}
std::atomic<bool> stopped = false;
std::exception_ptr e_ptr;
std::thread future_waiter;
{ // ThreadGuard scope
ThreadGuard tg(future_waiter);
std::chrono::milliseconds poll_interval(50);
auto f = capturer.start(video_device, audio_device, output_file, video_params);
future_waiter = std::thread([f = std::move(f), &stopped, &e_ptr, poll_interval]() mutable {
try {
while (!stopped) {
if (f.wait_for(poll_interval) == std::future_status::ready) {
f.get();
assert(stopped); // if we arrived here, no exception was thrown by get()
break;
};
}
} catch (...) {
e_ptr = std::current_exception();
stopped = true;
}
});
/* Poll STDIN to read commands */
try {
#ifndef WINDOWS
ConsoleSetter cs;
struct pollfd stdin_poll = {.fd = STDIN_FILENO, .events = POLLIN};
#endif
bool paused = false;
bool print_status = true;
bool print_menu = true;
while (!stopped) {
if (print_status) printStatus(paused);
if (print_menu) printMenu(paused);
print_status = false;
print_menu = false;
#ifndef WINDOWS
int poll_ret = poll(&stdin_poll, 1, 0);
#else
int poll_ret = _kbhit();
#endif
if (poll_ret > 0) { // there's something to read
print_status = true;
print_menu = true;
#ifndef WINDOWS
int command = std::tolower(getchar());
#else
int command = std::tolower(getch());
#endif
if (command == 'p' && !paused) {
capturer.pause();
paused = true;
} else if (command == 'r' && paused) {
capturer.resume();
paused = false;
} else if (command == 's') {
std::cout << "\n\nStopping..." << std::flush;
stopped = true;
capturer.stop();
std::cout << " done";
} else {
if (command == '\n') command = ' ';
std::cerr << " Invalid command '" << (char)command << "'";
print_status = false;
}
std::cout << std::endl;
} else if (poll_ret == 0) { // nothing to read, sleep...
std::this_thread::sleep_for(poll_interval);
} else { // error
throw std::runtime_error("Failed to poll stdin");
}
}
} catch (...) {
stopped = true; // tell future_waiter to stop
throw;
}
} // end ThreadGuard scope: join future_waiter in any case
if (e_ptr) std::rethrow_exception(e_ptr);
} catch (const std::exception &e) {
std::cerr << "\nERROR: " << e.what() << ", terminating..." << std::endl;
return 1;
}
return 0;
}