]> git.sesse.net Git - nageru/blob - flags.cpp
Support searching for the theme in multiple directories.
[nageru] / flags.cpp
1 #include "flags.h"
2
3 #include <getopt.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7
8 #include <utility>
9
10 using namespace std;
11
12 Flags global_flags;
13
14 // Long options that have no corresponding short option.
15 enum LongOption {
16         OPTION_VA_DISPLAY = 1000,
17         OPTION_HTTP_UNCOMPRESSED_VIDEO,
18         OPTION_HTTP_X264_VIDEO,
19         OPTION_X264_PRESET,
20         OPTION_X264_TUNE,
21         OPTION_X264_SPEEDCONTROL,
22         OPTION_X264_SPEEDCONTROL_VERBOSE,
23         OPTION_X264_BITRATE,
24         OPTION_X264_VBV_BUFSIZE,
25         OPTION_X264_VBV_MAX_BITRATE,
26         OPTION_X264_PARAM,
27         OPTION_HTTP_MUX,
28         OPTION_HTTP_COARSE_TIMEBASE,
29         OPTION_HTTP_AUDIO_CODEC,
30         OPTION_HTTP_AUDIO_BITRATE,
31         OPTION_FLAT_AUDIO,
32         OPTION_GAIN_STAGING,
33         OPTION_DISABLE_LOCUT,
34         OPTION_ENABLE_LOCUT,
35         OPTION_DISABLE_GAIN_STAGING_AUTO,
36         OPTION_ENABLE_GAIN_STAGING_AUTO,
37         OPTION_DISABLE_COMPRESSOR,
38         OPTION_ENABLE_COMPRESSOR,
39         OPTION_DISABLE_LIMITER,
40         OPTION_ENABLE_LIMITER,
41         OPTION_DISABLE_MAKEUP_GAIN_AUTO,
42         OPTION_ENABLE_MAKEUP_GAIN_AUTO,
43         OPTION_DISABLE_ALSA_OUTPUT,
44         OPTION_NO_FLUSH_PBOS
45 };
46
47 void usage()
48 {
49         fprintf(stderr, "Usage: nageru [OPTION]...\n");
50         fprintf(stderr, "\n");
51         fprintf(stderr, "  -h, --help                      print usage information\n");
52         fprintf(stderr, "  -c, --num-cards                 set number of input cards, including fake cards (default 2)\n");
53         fprintf(stderr, "  -C, --num-fake-cards            set number of fake cards (default 0)\n");
54         fprintf(stderr, "  -t, --theme=FILE                choose theme (default theme.lua)\n");
55         fprintf(stderr, "  -I, --theme-dir=DIR             search for theme in this directory (can be given multiple times)\n");
56         fprintf(stderr, "  -v, --va-display=SPEC           VA-API device for H.264 encoding\n");
57         fprintf(stderr, "                                    ($DISPLAY spec or /dev/dri/render* path)\n");
58         fprintf(stderr, "  -m, --map-signal=SIGNAL,CARD    set a default card mapping (can be given multiple times)\n");
59         fprintf(stderr, "      --http-uncompressed-video   send uncompressed NV12 video to HTTP clients\n");
60         fprintf(stderr, "      --http-x264-video           send x264-compressed video to HTTP clients\n");
61         fprintf(stderr, "      --x264-preset               x264 quality preset (default " X264_DEFAULT_PRESET ")\n");
62         fprintf(stderr, "      --x264-tune                 x264 tuning (default " X264_DEFAULT_TUNE ", can be blank)\n");
63         fprintf(stderr, "      --x264-speedcontrol         try to match x264 preset to available CPU speed\n");
64         fprintf(stderr, "      --x264-speedcontrol-verbose  output speedcontrol debugging statistics\n");
65         fprintf(stderr, "      --x264-bitrate              x264 bitrate (in kilobit/sec, default %d)\n",
66                 DEFAULT_X264_OUTPUT_BIT_RATE);
67         fprintf(stderr, "      --x264-vbv-bufsize          x264 VBV size (in kilobits, 0 = one-frame VBV,\n");
68         fprintf(stderr, "                                  default: same as --x264-bitrate, that is, one-second VBV)\n");
69         fprintf(stderr, "      --x264-vbv-max-bitrate      x264 local max bitrate (in kilobit/sec per --vbv-bufsize,\n");
70         fprintf(stderr, "                                  0 = no limit, default: same as --x264-bitrate, i.e., CBR)\n");
71         fprintf(stderr, "      --x264-param=NAME[,VALUE]   set any x264 parameter, for fine tuning\n");
72         fprintf(stderr, "      --http-mux=NAME             mux to use for HTTP streams (default " DEFAULT_STREAM_MUX_NAME ")\n");
73         fprintf(stderr, "      --http-audio-codec=NAME     audio codec to use for HTTP streams\n");
74         fprintf(stderr, "                                  (default is to use the same as for the recording)\n");
75         fprintf(stderr, "      --http-audio-bitrate=KBITS  audio codec bit rate to use for HTTP streams\n");
76         fprintf(stderr, "                                  (default is %d, ignored unless --http-audio-codec is set)\n",
77                 DEFAULT_AUDIO_OUTPUT_BIT_RATE / 1000);
78         fprintf(stderr, "      --http-coarse-timebase      use less timebase for HTTP (recommended for muxers\n");
79         fprintf(stderr, "                                  that handle large pts poorly, like e.g. MP4)\n");
80         fprintf(stderr, "      --flat-audio                start with most audio processing turned off\n");
81         fprintf(stderr, "                                    (can be overridden by e.g. --enable-limiter)\n");
82         fprintf(stderr, "      --gain-staging=DB           set initial gain staging to the given value\n");
83         fprintf(stderr, "                                    (--disable-gain-staging-auto)\n");
84         fprintf(stderr, "      --disable-locut             turn off locut filter (also --enable)\n");
85         fprintf(stderr, "      --disable-gain-staging-auto  turn off automatic gain staging (also --enable)\n");
86         fprintf(stderr, "      --disable-compressor        turn off regular compressor (also --enable)\n");
87         fprintf(stderr, "      --disable-limiter           turn off limiter (also --enable)\n");
88         fprintf(stderr, "      --disable-makeup-gain-auto  turn off auto-adjustment of final makeup gain (also --enable)\n");
89         fprintf(stderr, "      --disable-alsa-output       disable audio monitoring via ALSA\n");
90         fprintf(stderr, "      --no-flush-pbos             do not explicitly signal texture data uploads\n");
91         fprintf(stderr, "                                    (will give display corruption, but makes it\n");
92         fprintf(stderr, "                                    possible to run with apitrace in real time)\n");
93 }
94
95 void parse_flags(int argc, char * const argv[])
96 {
97         static const option long_options[] = {
98                 { "help", no_argument, 0, 'h' },
99                 { "num-cards", required_argument, 0, 'c' },
100                 { "num-fake-cards", required_argument, 0, 'C' },
101                 { "theme", required_argument, 0, 't' },
102                 { "theme-dir", required_argument, 0, 'I' },
103                 { "map-signal", required_argument, 0, 'm' },
104                 { "va-display", required_argument, 0, OPTION_VA_DISPLAY },
105                 { "http-uncompressed-video", no_argument, 0, OPTION_HTTP_UNCOMPRESSED_VIDEO },
106                 { "http-x264-video", no_argument, 0, OPTION_HTTP_X264_VIDEO },
107                 { "x264-preset", required_argument, 0, OPTION_X264_PRESET },
108                 { "x264-tune", required_argument, 0, OPTION_X264_TUNE },
109                 { "x264-speedcontrol", no_argument, 0, OPTION_X264_SPEEDCONTROL },
110                 { "x264-speedcontrol-verbose", no_argument, 0, OPTION_X264_SPEEDCONTROL_VERBOSE },
111                 { "x264-bitrate", required_argument, 0, OPTION_X264_BITRATE },
112                 { "x264-vbv-bufsize", required_argument, 0, OPTION_X264_VBV_BUFSIZE },
113                 { "x264-vbv-max-bitrate", required_argument, 0, OPTION_X264_VBV_MAX_BITRATE },
114                 { "x264-param", required_argument, 0, OPTION_X264_PARAM },
115                 { "http-mux", required_argument, 0, OPTION_HTTP_MUX },
116                 { "http-coarse-timebase", no_argument, 0, OPTION_HTTP_COARSE_TIMEBASE },
117                 { "http-audio-codec", required_argument, 0, OPTION_HTTP_AUDIO_CODEC },
118                 { "http-audio-bitrate", required_argument, 0, OPTION_HTTP_AUDIO_BITRATE },
119                 { "flat-audio", no_argument, 0, OPTION_FLAT_AUDIO },
120                 { "gain-staging", required_argument, 0, OPTION_GAIN_STAGING },
121                 { "disable-locut", no_argument, 0, OPTION_DISABLE_LOCUT },
122                 { "enable-locut", no_argument, 0, OPTION_ENABLE_LOCUT },
123                 { "disable-gain-staging-auto", no_argument, 0, OPTION_DISABLE_GAIN_STAGING_AUTO },
124                 { "enable-gain-staging-auto", no_argument, 0, OPTION_ENABLE_GAIN_STAGING_AUTO },
125                 { "disable-compressor", no_argument, 0, OPTION_DISABLE_COMPRESSOR },
126                 { "enable-compressor", no_argument, 0, OPTION_ENABLE_COMPRESSOR },
127                 { "disable-limiter", no_argument, 0, OPTION_DISABLE_LIMITER },
128                 { "enable-limiter", no_argument, 0, OPTION_ENABLE_LIMITER },
129                 { "disable-makeup-gain-auto", no_argument, 0, OPTION_DISABLE_MAKEUP_GAIN_AUTO },
130                 { "enable-makeup-gain-auto", no_argument, 0, OPTION_ENABLE_MAKEUP_GAIN_AUTO },
131                 { "disable-alsa-output", no_argument, 0, OPTION_DISABLE_ALSA_OUTPUT },
132                 { "no-flush-pbos", no_argument, 0, OPTION_NO_FLUSH_PBOS },
133                 { 0, 0, 0, 0 }
134         };
135         vector<string> theme_dirs;
136         for ( ;; ) {
137                 int option_index = 0;
138                 int c = getopt_long(argc, argv, "c:C:t:v:m:", long_options, &option_index);
139
140                 if (c == -1) {
141                         break;
142                 }
143                 switch (c) {
144                 case 'c':
145                         global_flags.num_cards = atoi(optarg);
146                         break;
147                 case 'C':
148                         global_flags.num_fake_cards = atoi(optarg);
149                         break;
150                 case 't':
151                         global_flags.theme_filename = optarg;
152                         break;
153                 case 'I':
154                         theme_dirs.push_back(optarg);
155                         break;
156                 case 'm': {
157                         char *ptr = strchr(optarg, ',');
158                         if (ptr == nullptr) {
159                                 fprintf(stderr, "ERROR: Invalid argument '%s' to --map-signal (needs a signal and a card number, separated by comma)\n", optarg);
160                                 exit(1);
161                         }
162                         *ptr = '\0';
163                         const int signal_num = atoi(optarg);
164                         const int card_num = atoi(ptr + 1);
165                         if (global_flags.default_stream_mapping.count(signal_num)) {
166                                 fprintf(stderr, "ERROR: Signal %d already mapped to card %d\n",
167                                         signal_num, global_flags.default_stream_mapping[signal_num]);
168                                 exit(1);
169                         }
170                         global_flags.default_stream_mapping[signal_num] = card_num;
171                         break;
172                 }
173                 case OPTION_VA_DISPLAY:
174                         global_flags.va_display = optarg;
175                         break;
176                 case OPTION_HTTP_UNCOMPRESSED_VIDEO:
177                         global_flags.uncompressed_video_to_http = true;
178                         break;
179                 case OPTION_HTTP_MUX:
180                         global_flags.stream_mux_name = optarg;
181                         break;
182                 case OPTION_HTTP_COARSE_TIMEBASE:
183                         global_flags.stream_coarse_timebase = true;
184                         break;
185                 case OPTION_HTTP_AUDIO_CODEC:
186                         global_flags.stream_audio_codec_name = optarg;
187                         break;
188                 case OPTION_HTTP_AUDIO_BITRATE:
189                         global_flags.stream_audio_codec_bitrate = atoi(optarg) * 1000;
190                         break;
191                 case OPTION_HTTP_X264_VIDEO:
192                         global_flags.x264_video_to_http = true;
193                         break;
194                 case OPTION_X264_PRESET:
195                         global_flags.x264_preset = optarg;
196                         break;
197                 case OPTION_X264_TUNE:
198                         global_flags.x264_tune = optarg;
199                         break;
200                 case OPTION_X264_SPEEDCONTROL:
201                         global_flags.x264_speedcontrol = true;
202                         break;
203                 case OPTION_X264_SPEEDCONTROL_VERBOSE:
204                         global_flags.x264_speedcontrol_verbose = true;
205                         break;
206                 case OPTION_X264_BITRATE:
207                         global_flags.x264_bitrate = atoi(optarg);
208                         break;
209                 case OPTION_X264_VBV_BUFSIZE:
210                         global_flags.x264_vbv_buffer_size = atoi(optarg);
211                         break;
212                 case OPTION_X264_VBV_MAX_BITRATE:
213                         global_flags.x264_vbv_max_bitrate = atoi(optarg);
214                         break;
215                 case OPTION_X264_PARAM:
216                         global_flags.x264_extra_param.push_back(optarg);
217                         break;
218                 case OPTION_FLAT_AUDIO:
219                         // If --flat-audio is given, turn off everything that messes with the sound,
220                         // except the final makeup gain.
221                         global_flags.locut_enabled = false;
222                         global_flags.gain_staging_auto = false;
223                         global_flags.compressor_enabled = false;
224                         global_flags.limiter_enabled = false;
225                         break;
226                 case OPTION_GAIN_STAGING:
227                         global_flags.initial_gain_staging_db = atof(optarg);
228                         global_flags.gain_staging_auto = false;
229                         break;
230                 case OPTION_DISABLE_LOCUT:
231                         global_flags.locut_enabled = false;
232                         break;
233                 case OPTION_ENABLE_LOCUT:
234                         global_flags.locut_enabled = true;
235                         break;
236                 case OPTION_DISABLE_GAIN_STAGING_AUTO:
237                         global_flags.gain_staging_auto = false;
238                         break;
239                 case OPTION_ENABLE_GAIN_STAGING_AUTO:
240                         global_flags.gain_staging_auto = true;
241                         break;
242                 case OPTION_DISABLE_COMPRESSOR:
243                         global_flags.compressor_enabled = false;
244                         break;
245                 case OPTION_ENABLE_COMPRESSOR:
246                         global_flags.compressor_enabled = true;
247                         break;
248                 case OPTION_DISABLE_LIMITER:
249                         global_flags.limiter_enabled = false;
250                         break;
251                 case OPTION_ENABLE_LIMITER:
252                         global_flags.limiter_enabled = true;
253                         break;
254                 case OPTION_DISABLE_MAKEUP_GAIN_AUTO:
255                         global_flags.final_makeup_gain_auto = false;
256                         break;
257                 case OPTION_ENABLE_MAKEUP_GAIN_AUTO:
258                         global_flags.final_makeup_gain_auto = true;
259                         break;
260                 case OPTION_DISABLE_ALSA_OUTPUT:
261                         global_flags.enable_alsa_output = false;
262                         break;
263                 case OPTION_NO_FLUSH_PBOS:
264                         global_flags.flush_pbos = false;
265                         break;
266                 case 'h':
267                         usage();
268                         exit(0);
269                 default:
270                         fprintf(stderr, "Unknown option '%s'\n", argv[option_index]);
271                         fprintf(stderr, "\n");
272                         usage();
273                         exit(1);
274                 }
275         }
276
277         if (global_flags.uncompressed_video_to_http &&
278             global_flags.x264_video_to_http) {
279                 fprintf(stderr, "ERROR: --http-uncompressed-video and --http-x264-video are mutually incompatible\n");
280                 exit(1);
281         }
282         if (global_flags.num_fake_cards > global_flags.num_cards) {
283                 fprintf(stderr, "ERROR: More fake cards then total cards makes no sense\n");
284                 exit(1);
285         }
286         if (global_flags.num_cards <= 0) {
287                 fprintf(stderr, "ERROR: --num-cards must be at least 1\n");
288                 exit(1);
289         }
290         if (global_flags.num_fake_cards < 0) {
291                 fprintf(stderr, "ERROR: --num-fake-cards cannot be negative\n");
292                 exit(1);
293         }
294         if (global_flags.x264_speedcontrol) {
295                 if (!global_flags.x264_preset.empty() && global_flags.x264_preset != "faster") {
296                         fprintf(stderr, "WARNING: --x264-preset is overridden by --x264-speedcontrol (implicitly uses \"faster\" as base preset)\n");
297                 }
298                 global_flags.x264_preset = "faster";
299         } else if (global_flags.x264_preset.empty()) {
300                 global_flags.x264_preset = X264_DEFAULT_PRESET;
301         }
302         if (!theme_dirs.empty()) {
303                 global_flags.theme_dirs = theme_dirs;
304         }
305
306         for (pair<int, int> mapping : global_flags.default_stream_mapping) {
307                 if (mapping.second >= global_flags.num_cards) {
308                         fprintf(stderr, "ERROR: Signal %d mapped to card %d, which doesn't exist (try adjusting --num-cards)\n",
309                                 mapping.first, mapping.second);
310                         exit(1);
311                 }
312         }
313 }