summaryrefslogtreecommitdiff
path: root/src/options.c
blob: 48a0f209fbfcb4e39edd2001f58111c627771cf2 (plain) (blame)
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
/*
 *  sydra
 *
 *  sydra is a toolbox which allows you to set up multiple bidirectional
 *  Video/Audio streams from external locations.
 *  sydra has been written to be used for the Elevate Festival in Graz
 *  Austria in order to involve external locations to present themselves
 *  at the festival.
 *  Sydra is based on GStreamer and is written in C.
 *
 *
 *  Copyright (C) 2014 Christian Pointner <equinox@spreadspace.org>
 *
 *  This file is part of sydra.
 *
 *  sydra is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  any later version.
 *
 *  sydra is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with sydra. If not, see <http://www.gnu.org/licenses/>.
 *
 *  In addition, as a special exception, the copyright holders hereby
 *  grant permission for non-GPL-compatible GStreamer plugins to be used
 *  and distributed together with GStreamer and sydra.
 *  This permission goes above and beyond the permissions granted by the
 *  GPL license sydra is covered by.
 */

#include "datatypes.h"
#include "config.h"

#include "options.h"
#include "log.h"

#include <stdio.h>
#include <string.h>
#include <glib.h>
#include <gst/gst.h>

static void options_defaults(options_t* opt)
{
  if(!opt)
    return;

  opt->progname_ = g_strdup("sydra");
  opt->daemonize_ = 1;
  opt->username_ = NULL;
  opt->groupname_ = NULL;
  opt->chroot_dir_ = NULL;
  opt->pid_file_ = NULL;
  opt->log_targets_ = NULL;
  opt->debug_ = 0;

  opt->appname_ = NULL;
  opt->mode_ = SENDER;

  opt->video_src_ = g_strdup("v4l2src ! videoconvert ! videoscale ! video/x-raw,format=I420,width=864,height=480,framerate=25/1,pixel-aspect-ratio=1/1 ! identity");
  opt->video_enc_ = g_strdup("vp8enc keyframe-max-dist=25 error-resilient=2 end-usage=1 target-bitrate=1200000 cpu-used=4 deadline=1000000 threads=2");
  opt->video_payloader_ = g_strdup("rtpvp8pay");

  opt->audio_src_ = g_strdup("autoaudiosrc ! audio/x-raw,format=S16LE,channels=1,rate=48000 ! identity");
  opt->audio_enc_ = g_strdup("opusenc bitrate=64000 cbr=true packet-loss-percentage=0 inband-fec=false");
  opt->audio_payloader_ = g_strdup("rtpopuspay");

  opt->rtp_host_ = NULL;
  opt->rtp_port_base_ = 5000;
  opt->rtp_addr_local_ = NULL;
  opt->rtp_port_base_local_ = 5000;
  opt->timeout_ = 30;
  opt->keepalive_int_ = 10;

  opt->preview_videosink_ = NULL;

  opt->video_enc_rec_ = NULL;
  opt->audio_enc_rec_ = NULL;
  opt->rec_mux_ = NULL;
  opt->rec_name_format_ = g_strdup("./recordings/%Y-%m-%d_%H-%M-%S");
}

static GQuark options_error_quark()
{
  static GQuark quark = 0;
  if (!quark)
    quark = g_quark_from_static_string("sydra_options_error");

  return quark;
}

static GOptionGroup* options_get_av_group(options_t* opt)
{
  GOptionEntry av_entries[] = {
    { "video-source", 0, 0, G_OPTION_ARG_STRING, &opt->video_src_,
      "pipeline for raw video (i.e. videotestsrc)", "BIN DESCRIPTION" },
    { "video-encoder", 0, 0, G_OPTION_ARG_STRING, &opt->video_enc_,
      "pipeline for video encoder (i.e. videoconvert ! vp8enc)", "BIN DESCRIPTION" },
    { "video-payloader", 0, 0, G_OPTION_ARG_STRING, &opt->video_payloader_,
      "video RTP payloader element (i.e. rtpvp8pay)", "ELEMENT" },
    { "audio-source", 0, 0, G_OPTION_ARG_STRING, &opt->audio_src_,
      "pipeline for raw audio (i.e. audiotestsrc)", "BIN DESCRIPTION" },
    { "audio-encoder", 0, 0, G_OPTION_ARG_STRING, &opt->audio_enc_,
      "pipeline for audio encoder (i.e. audioconvert ! opusenc)", "BIN DESCRIPTION" },
    { "audio-payloader", 0, 0, G_OPTION_ARG_STRING, &opt->audio_payloader_,
      "audio RTP payloader element (i.e. rtpopuspay)", "ELEMENT" },
    { NULL }
  };
  GOptionGroup* av_group = g_option_group_new ("av", "Audio/Video Options",
                                                  "Show Audio/Video Options", opt, NULL);
  if(!av_group) return NULL;
  g_option_group_add_entries(av_group, av_entries);

  return av_group;
}

static GOptionGroup* options_get_rtp_group(options_t* opt)
{
  GOptionEntry rtp_entries[] = {
    { "rtp-host", 'a', 0, G_OPTION_ARG_STRING, &opt->rtp_host_,
      "remote host for RTP packets", "HOST" },
    { "rtp-port-base", 'o', 0, G_OPTION_ARG_INT, &opt->rtp_port_base_,
      "base number for remote ports", "PORT" },
    { "rtp-addr-local", 'A', 0, G_OPTION_ARG_STRING, &opt->rtp_addr_local_,
      "local address to bind to", "ADDRESS" },
    { "rtp-port-base-local", 'O', 0, G_OPTION_ARG_INT, &opt->rtp_port_base_local_,
      "base number for local ports to bind to", "PORT" },
    { "timeout", 't', 0, G_OPTION_ARG_INT, &opt->timeout_,
      "client timeout in seconds (0 means no timeout) - sender mode only!", "VALUE" },
    { "keepalive-interval", 'k', 0, G_OPTION_ARG_INT, &opt->keepalive_int_,
      "interval in seconds for sending out keep alive messages (0 means no keep alives) - receiver mode only!", "VALUE" },
    { NULL }
  };
  GOptionGroup* rtp_group = g_option_group_new ("rtp", "RTP Options",
                                                  "Show RTP Options", opt, NULL);
  if(!rtp_group) return NULL;
  g_option_group_add_entries(rtp_group, rtp_entries);

  return rtp_group;
}

static GOptionGroup* options_get_rec_group(options_t* opt)
{
  GOptionEntry rec_entries[] = {
    { "rec-video-encoder", 0, 0, G_OPTION_ARG_STRING, &opt->video_enc_rec_,
      "pipeline for video encoder for recording (i.e. videoconvert ! jpegenc) - leave empty to use same as for RTP", "BIN DESCRIPTION" },
    { "rec-audio-encoder", 0, 0, G_OPTION_ARG_STRING, &opt->audio_enc_rec_,
      "pipeline for audio encoder for recording (i.e. audioconvert ! vorbisenc) - leave empty to use same as for RTP", "BIN DESCRIPTION" },
    { "rec-mux", 0, 0, G_OPTION_ARG_STRING, &opt->rec_mux_,
      "muxer elemenent for recording (i.e. matroskamux) - leave empty to disable recording", "ELEMENT" },
    { "rec-name-format", 0, 0, G_OPTION_ARG_STRING, &opt->rec_name_format_,
      "the recording file name format string, see manpage of strftime for the syntax", "FORMATSTRING" },
    { NULL }
  };
  GOptionGroup* rec_group = g_option_group_new ("rec", "Recording Options",
                                                  "Show Recording Options", opt, NULL);
  if(!rec_group) return NULL;
  g_option_group_add_entries(rec_group, rec_entries);

  return rec_group;
}

static gboolean options_parse_mode(const gchar *option_name, const gchar *value, gpointer data, GError **error)
{
  options_t* opt = (options_t*)data;

  if(!strcmp(value, "sender"))
    opt->mode_ = SENDER;
  else if(!strcmp(value, "receiver"))
    opt->mode_ = RECEIVER;
  else {
    g_set_error(error, options_error_quark(), G_OPTION_ERROR_FAILED,
                "Invalid value for mode parameter '%s' - allowed values are: sender, receiver", value);
    return FALSE;
  }
  return TRUE;
}

static int options_parse_post(options_t* opt);

int options_parse(options_t* opt, int argc, char* argv[])
{
  if(!opt)
    return -1;

  options_defaults(opt);

  g_free(opt->progname_);
  opt->progname_ = g_strdup(argv[0]);
  if(!opt->progname_)
    return -127;

  gboolean show_version = FALSE;
  GOptionEntry main_entries[] = {
    { "version", 'v', 0, G_OPTION_ARG_NONE, &show_version,
      "print version info and exit", NULL },
    { "nodaemonize", 'D', G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &opt->daemonize_,
      "don't run in background", NULL },
    { "username", 'u', 0, G_OPTION_ARG_STRING, &opt->username_,
      "change to this user", "USERNAME" },
    { "group", 'g', 0, G_OPTION_ARG_STRING, &opt->groupname_,
      "change to this group", "GROUP" },
    { "chroot", 'C', 0, G_OPTION_ARG_STRING, &opt->chroot_dir_,
      "chroot to this directory", "PATH" },
    { "write-pid", 'P', 0, G_OPTION_ARG_STRING, &opt->pid_file_,
      "write pid to this file", "PATH" },
    { "log", 'L', 0, G_OPTION_ARG_STRING_ARRAY, &opt->log_targets_,
      "add a log target, can be invoked several times", "<TARGET>:<LEVEL>[,<PARAM1>[,<PARAM2>..]]" },
    { "debug", 'U', 0, G_OPTION_ARG_NONE, &opt->debug_,
      "don't daemonize and log to stdout with maximum log level", NULL },
    { "appname", 'n', 0, G_OPTION_ARG_STRING, &opt->appname_,
      "set the application name (will be used by xvimagesink for window title)", "NAME" },
    { "mode", 'm', 0, G_OPTION_ARG_CALLBACK, options_parse_mode,
      "the main operation mode", "(sender|receiver)" },
    { "videosink", 'V', 0, G_OPTION_ARG_STRING, &opt->preview_videosink_,
      "video sink element for local preview (i.e. textoverlay text=\" preview \" ! xvimagesink) - leave empty to disable preview", "BIN DESCRIPTION" },
    { NULL }
  };
  GOptionContext *ctx = g_option_context_new("- spreadspace streaming hydra ");
  GOptionGroup* main_group = g_option_group_new ("main", "Application Options",
                                                 "Show Application Options", opt, NULL);
  if(main_group)
    g_option_group_add_entries(main_group, main_entries);

  GOptionGroup* av_group = options_get_av_group(opt);
  GOptionGroup* rtp_group = options_get_rtp_group(opt);
  GOptionGroup* rec_group = options_get_rec_group(opt);
  GOptionGroup* gst_group = gst_init_get_option_group();
  if(!main_group || !av_group || !rtp_group || !rec_group || !gst_group) {
    printf("Failed to initialize: memory error\n");
    return -127;
  }
  g_option_context_set_main_group(ctx, main_group);
  g_option_context_add_group(ctx, av_group);
  g_option_context_add_group(ctx, rtp_group);
  g_option_context_add_group(ctx, rec_group);
  g_option_context_add_group(ctx, gst_group);

  GError *err = NULL;
  if(!g_option_context_parse(ctx, &argc, &argv, &err)) {
    printf("Failed to initialize: %s\n", err->message);
    g_error_free(err);
    return 1;
  }

  if(show_version)
    return -1;

  return options_parse_post(opt);
}

static int options_parse_post(options_t* opt)
{
  if(opt->rtp_port_base_ < 1 || opt->rtp_port_base_ > 65535 ||
     opt->rtp_port_base_local_ < 1 || opt->rtp_port_base_local_ > 65535) {
    printf("Failed to initialize: rtp port is invalid\n");
    return -2;
  }

  if(opt->timeout_ < 0) {
    printf("Failed to initialize: timeout is invalid\n");
    return -3;
  }
  if(opt->keepalive_int_ < 0) {
    printf("Failed to initialize: keep alive interval is invalid\n");
    return -3;
  }

  if(opt->debug_) {
    opt->daemonize_ = 0;
    g_strfreev(opt->log_targets_);
    opt->log_targets_ = g_new(gchar*, 2);
    if(!opt->log_targets_) return -127;
    opt->log_targets_[0] = g_strdup("stdout:5");
    if(!(opt->log_targets_[0])) return -127;
    opt->log_targets_[1] = NULL;
  }

  if(!g_strv_length(opt->log_targets_)) {
    opt->log_targets_ = g_new(gchar*, 2);
    if(!opt->log_targets_) return -127;
    opt->log_targets_[0] = g_strdup("syslog:3,sydra,daemon");
    if(!(opt->log_targets_[0])) return -127;
    opt->log_targets_[1] = NULL;
  }

  return 0;
}

void options_clear(options_t* opt)
{
  if(!opt)
    return;

  g_free(opt->progname_);
  g_free(opt->username_);
  g_free(opt->groupname_);
  g_free(opt->chroot_dir_);
  g_free(opt->pid_file_);
  g_strfreev(opt->log_targets_);
  g_free(opt->appname_);
  g_free(opt->video_src_);
  g_free(opt->video_enc_);
  g_free(opt->video_payloader_);
  g_free(opt->audio_src_);
  g_free(opt->audio_enc_);
  g_free(opt->audio_payloader_);
  g_free(opt->rtp_host_);
  g_free(opt->rtp_addr_local_);
  g_free(opt->preview_videosink_);
  g_free(opt->video_enc_rec_);
  g_free(opt->audio_enc_rec_);
  g_free(opt->rec_mux_);
  g_free(opt->rec_name_format_);
}

void options_print_version()
{
  printf("%s\n", VERSION_STRING_0);
#if defined(__clang__)
  printf("%s, using CLANG %s\n", VERSION_STRING_1, __clang_version__);
#elif defined(__GNUC__)
  printf("%s, using GCC %d.%d.%d\n", VERSION_STRING_1, __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__);
#else
  printf("%s\n", VERSION_STRING_1);
#endif
  const gchar *nano_str;
  guint major, minor, micro, nano;
  gst_version(&major, &minor, &micro, &nano);
  if (nano == 1)
    nano_str = " (CVS)";
  else if (nano == 2)
    nano_str = " (Prerelease)";
  else
    nano_str = "";
  printf("  linked against GStreamer %d.%d.%d%s\n", major, minor, micro, nano_str);
}

void options_print(options_t* opt)
{
  if(!opt)
    return;

  printf("  progname: '%s'\n", opt->progname_);
  printf("  daemonize: %s\n", opt->daemonize_ ? "true" : "false");
  printf("  username: '%s'\n", opt->username_);
  printf("  groupname: '%s'\n", opt->groupname_);
  printf("  chroot_dir: '%s'\n", opt->chroot_dir_);
  printf("  pid_file: '%s'\n", opt->pid_file_);
  printf("  log_targets: \n");
  gchar* lt = opt->log_targets_ ? g_strjoinv ("'\n    '",  opt->log_targets_) : NULL;
  if(lt) {
    printf("    '%s'\n",  lt);
    g_free(lt);
  }
  printf("  debug: %s\n", opt->debug_ ? "true" : "false");
  printf("  appname: >>%s<<\n", opt->appname_);
  printf("  mode: >>%s<<\n", opt->mode_ == SENDER ? "sender" : "receiver");
  printf("  video_src: >>%s<<\n", opt->video_src_);
  printf("  video_enc: >>%s<<\n", opt->video_enc_);
  printf("  video_payloader: >>%s<<\n", opt->video_payloader_);
  printf("  audio_src: >>%s<<\n", opt->audio_src_);
  printf("  audio_enc: >>%s<<\n", opt->audio_enc_);
  printf("  audio_payloader: >>%s<<\n", opt->audio_payloader_);
  printf("  rtp_host: >>%s<<\n", opt->rtp_host_);
  printf("  rtp_port_base: %d\n", opt->rtp_port_base_);
  printf("  rtp_addr_local: >>%s<<\n", opt->rtp_addr_local_);
  printf("  rtp_port_base_local: %d\n", opt->rtp_port_base_local_);
  printf("  timeout: %d\n", opt->timeout_);
  printf("  keepalive_int: %d\n", opt->keepalive_int_);
  printf("  preview_video_sink: >>%s<<\n", opt->preview_videosink_);
  printf("  video_enc_rec: >>%s<<\n", opt->video_enc_rec_);
  printf("  audio_enc_rec: >>%s<<\n", opt->audio_enc_rec_);
  printf("  rec_mux: >>%s<<\n", opt->rec_mux_);
  printf("  rec_name_format: '%s'\n", opt->rec_name_format_);
}