summaryrefslogtreecommitdiff
path: root/tools/gst-transcoder.c
blob: 9fcac13e699ab45383cc4ee2b9fc06eb4009ee42 (plain)
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/* GStreamer
 *
 * Copyright (C) 2015 Thibault Saunier <tsaunier@gnome.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include <string.h>

#include "utils.h"
#include <gst/transcoder/gsttranscoder.h>
#ifdef G_OS_UNIX
#include <glib-unix.h>
#endif

static const gchar *HELP_SUMMARY =
    "gst-transcoder-1.0 transcodes a stream defined by its first <input-uri>\n"
    "argument to the place defined by its second <output-uri> argument\n"
    "into the format described in its third <encoding-format> argument,\n"
    "or using the given <output-uri> file extension.\n"
    "\n"
    "The <encoding-format> argument:\n"
    "===============================\n"
    "\n"
    "If the encoding format is not defined, it will be guessed with\n"
    "the given <output-uri> file extension."
    "\n"
    "<encoding-format> describe the media format into which the\n"
    "input stream is going to be transcoded. We have two different\n"
    "ways of describing the format:\n"
    "\n"
    "GstEncodingProfile serialization format\n"
    "---------------------------------------\n"
    "\n"
    "GStreamer encoding profiles can be described with a quite extensive\n"
    "syntax which is described in the GstEncodingProfile documentation.\n"
    "\n"
    "The simple case looks like:\n"
    "\n"
    "    muxer_source_caps:videoencoder_source_caps:audioencoder_source_caps\n"
    "\n"
    "Name and category of serialized GstEncodingTarget\n"
    "-------------------------------------------------\n"
    "\n"
    "Encoding targets describe well known formats which\n"
    "those are provided in '.gep' files. You can list\n"
    "available ones using the `--list-targets` argument.\n";

typedef struct
{
  gint cpu_usage, rate;
  gboolean list;
  GstEncodingProfile *profile;
  gchar *src_uri, *dest_uri, *encoding_format, *size;
  gchar *framerate;
} Settings;

#ifdef G_OS_UNIX
static guint signal_watch_hup_id;
static guint signal_watch_intr_id;

static gboolean
intr_handler (gpointer user_data)
{
  GstTranscoder *self = GST_TRANSCODER (user_data);
  GstElement *pipeline = gst_transcoder_get_pipeline (self);

  g_print ("handling interrupt.\n");

  if (pipeline) {
    gst_element_send_event (pipeline, gst_event_new_eos ());
    g_object_unref (pipeline);
  }

  signal_watch_intr_id = 0;
  return G_SOURCE_REMOVE;
}

static gboolean
hup_handler (gpointer user_data)
{
  GstTranscoder *self = GST_TRANSCODER (user_data);
  GstElement *pipeline = gst_transcoder_get_pipeline (self);

  g_print ("handling hang up.\n");

  if (pipeline) {
    gst_element_send_event (pipeline, gst_event_new_eos ());
    g_object_unref (pipeline);
  }

  signal_watch_intr_id = 0;
  return G_SOURCE_REMOVE;
}
#endif /* G_OS_UNIX */

static void
position_updated_cb (GstTranscoder * transcoder, GstClockTime pos)
{
  GstClockTime dur = -1;
  gchar status[64] = { 0, };

  g_object_get (transcoder, "duration", &dur, NULL);

  memset (status, ' ', sizeof (status) - 1);

  if (pos != -1 && dur > 0 && dur != -1) {
    gchar dstr[32], pstr[32];

    /* FIXME: pretty print in nicer format */
    g_snprintf (pstr, 32, "%" GST_TIME_FORMAT, GST_TIME_ARGS (pos));
    pstr[9] = '\0';
    g_snprintf (dstr, 32, "%" GST_TIME_FORMAT, GST_TIME_ARGS (dur));
    dstr[9] = '\0';
    g_print ("%s / %s %s\r", pstr, dstr, status);
  }
}

static GList *
get_profiles_of_type (GstEncodingProfile * profile, GType profile_type)
{
  GList *tmp, *profiles = NULL;

  if (GST_IS_ENCODING_CONTAINER_PROFILE (profile)) {
    for (tmp = (GList *)
        gst_encoding_container_profile_get_profiles
        (GST_ENCODING_CONTAINER_PROFILE (profile)); tmp; tmp = tmp->next) {
      if (G_OBJECT_TYPE (tmp->data) == profile_type)
        profiles = g_list_prepend (profiles, tmp->data);
    }
  } else if (GST_IS_ENCODING_VIDEO_PROFILE (profile)) {
    profiles = g_list_prepend (profiles, profile);
  }

  return profiles;
}

static gboolean
set_video_settings (Settings * settings)
{
  GList *video_profiles, *tmp;
  gchar *p, *tmpstr, **vsize;
  gint width = 0, height = 0;
  GValue framerate = G_VALUE_INIT;

  if (!settings->size && !settings->framerate)
    return TRUE;

  if (settings->size) {
    p = tmpstr = g_strdup (settings->size);

    for (; *p; ++p)
      *p = g_ascii_tolower (*p);

    vsize = g_strsplit (tmpstr, "x", -1);
    g_free (tmpstr);

    if (!vsize[1] || vsize[2]) {
      g_strfreev (vsize);
      error ("Video size should be in the form: WxH, got %s", settings->size);

      return FALSE;
    }

    width = g_ascii_strtoull (vsize[0], NULL, 0);
    height = g_ascii_strtoull (vsize[1], NULL, 10);
    g_strfreev (vsize);
  }

  if (settings->framerate) {
    g_value_init (&framerate, GST_TYPE_FRACTION);
    if (!gst_value_deserialize (&framerate, settings->framerate)) {
      error ("Video framerate should be either a fraction or an integer"
          " not: %s", settings->framerate);
      return FALSE;
    }
  }

  video_profiles = get_profiles_of_type (settings->profile,
      GST_TYPE_ENCODING_VIDEO_PROFILE);
  for (tmp = video_profiles; tmp; tmp = tmp->next) {
    GstCaps *rest = gst_encoding_profile_get_restriction (tmp->data);

    if (!rest)
      rest = gst_caps_new_empty_simple ("video/x-raw");
    else
      rest = gst_caps_copy (rest);

    if (settings->size) {
      gst_caps_set_simple (rest, "width", G_TYPE_INT, width,
          "height", G_TYPE_INT, height, NULL);
    }
    if (settings->framerate)
      gst_caps_set_value (rest, "framerate", &framerate);

    gst_encoding_profile_set_restriction (tmp->data, rest);
  }

  return TRUE;
}

static gboolean
set_audio_settings (Settings * settings)
{
  GList *audio_profiles, *tmp;

  if (settings->rate < 0)
    return TRUE;

  audio_profiles =
      get_profiles_of_type (settings->profile, GST_TYPE_ENCODING_AUDIO_PROFILE);
  for (tmp = audio_profiles; tmp; tmp = tmp->next) {
    GstCaps *rest = gst_encoding_profile_get_restriction (tmp->data);

    if (!rest)
      rest = gst_caps_new_empty_simple ("audio/x-raw");
    else
      rest = gst_caps_copy (rest);

    gst_caps_set_simple (rest, "rate", G_TYPE_INT, settings->rate, NULL);
    gst_encoding_profile_set_restriction (tmp->data, rest);
  }

  return TRUE;
}

static void
list_encoding_targets (void)
{
  GList *tmp, *targets = gst_encoding_list_all_targets (NULL);

  for (tmp = targets; tmp; tmp = tmp->next) {
    GstEncodingTarget *target = tmp->data;
    GList *usable_profiles = get_usable_profiles (target);

    if (usable_profiles) {
      GList *tmpprof;

      g_print ("\n%s (%s): %s\n * Profiles:\n",
          gst_encoding_target_get_name (target),
          gst_encoding_target_get_category (target),
          gst_encoding_target_get_description (target));

      for (tmpprof = usable_profiles; tmpprof; tmpprof = tmpprof->next)
        g_print ("     - %s: %s",
            gst_encoding_profile_get_name (tmpprof->data),
            gst_encoding_profile_get_description (tmpprof->data));

      g_print ("\n");
      g_list_free (usable_profiles);
    }
  }

  g_list_free_full (targets, (GDestroyNotify) g_object_unref);
}

static void
_error_cb (GstTranscoder * transcoder, GError * err, GstStructure * details)
{
  if (g_error_matches (err, GST_CORE_ERROR, GST_CORE_ERROR_PAD)) {
    GstPadLinkReturn lret;
    GType type;

    if (details && gst_structure_get (details, "linking-error",
            GST_TYPE_PAD_LINK_RETURN, &lret,
            "msg-source-type", G_TYPE_GTYPE, &type, NULL) &&
        type == g_type_from_name ("GstTranscodeBin")) {
      const gchar *debug = gst_structure_get_string (details, "debug");

      error ("\nCould not setup transcoding pipeline,"
          " make sure that your transcoding format parameters"
          " are compatible with the input stream.\n\n%s", debug);
      return;
    }
  }

  error ("\nFAILURE: %s", err->message);
}

static void
_warning_cb (GstTranscoder * transcoder, GError * error, GstStructure * details)
{
  gboolean cant_encode;
  GstCaps *caps = NULL;
  gchar *stream_id = NULL;

  if (details && gst_structure_get (details, "can-t-encode-stream",
          G_TYPE_BOOLEAN, &cant_encode, "stream-caps", GST_TYPE_CAPS,
          &caps, "stream-id", G_TYPE_STRING, &stream_id, NULL)) {
    gchar *source_uri = gst_transcoder_get_source_uri (transcoder);

    warn ("WARNING: Input stream %s: WON'T BE ENCODED.\n"
        "Make sure the encoding settings are valid and that"
        " any preset you set actually exists.\n"
        "For more information about that stream, you can inspect"
        " the source stream with:\n\n"
        "    gst-discoverer-1.0 -v %s\n", stream_id, source_uri);
    gst_caps_unref (caps);
    g_free (stream_id);;
    g_free (source_uri);;

    return;
  }
  warn ("Got warning: %s", error->message);
}

int
main (int argc, char *argv[])
{
  gint res = 0;
  GError *err = NULL;
  GstTranscoder *transcoder;
  GOptionContext *ctx;
  GstTranscoderSignalAdapter *signal_adapter;
  Settings settings = {
    .cpu_usage = 100,
    .rate = -1,
    .encoding_format = NULL,
    .size = NULL,
    .framerate = NULL,
  };
  GOptionEntry options[] = {
    {"cpu-usage", 'c', 0, G_OPTION_ARG_INT, &settings.cpu_usage,
        "The CPU usage to target in the transcoding process", NULL},
    {"list-targets", 'l', G_OPTION_ARG_NONE, 0, &settings.list,
        "List all encoding targets", NULL},
    {"size", 's', 0, G_OPTION_ARG_STRING, &settings.size,
        "set frame size (WxH or abbreviation)", NULL},
    {"audio-rate", 'r', 0, G_OPTION_ARG_INT, &settings.rate,
        "set audio sampling rate (in Hz)", NULL},
    {"framerate", 'f', 0, G_OPTION_ARG_STRING, &settings.framerate,
        "set video framerate as a fraction (24/1 for 24fps)"
          " or a single number (24 for 24fps))", NULL},
    {"video-encoder", 'v', 0, G_OPTION_ARG_STRING, &settings.size,
        "The video encoder to use.", NULL},
    {NULL}
  };

  g_set_prgname ("gst-transcoder");

  ctx = g_option_context_new ("<source uri> <destination uri> "
      "[<encoding format>[/<encoding profile name>]]");
  g_option_context_set_summary (ctx, HELP_SUMMARY);

  g_option_context_add_main_entries (ctx, options, NULL);
  g_option_context_add_group (ctx, gst_init_get_option_group ());

  if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
    g_print ("Error initializing: %s\n", GST_STR_NULL (err->message));
    g_clear_error (&err);
    g_option_context_free (ctx);
    return 1;
  }
  gst_pb_utils_init ();

  if (settings.list) {
    list_encoding_targets ();
    return 0;
  }

  if (argc < 3 || argc > 4) {
    g_print ("%s", g_option_context_get_help (ctx, TRUE, NULL));
    g_option_context_free (ctx);

    return -1;
  }
  g_option_context_free (ctx);

  settings.src_uri = ensure_uri (argv[1]);
  settings.dest_uri = ensure_uri (argv[2]);

  if (argc == 3) {
    settings.encoding_format = get_file_extension (settings.dest_uri);

    if (!settings.encoding_format)
      goto no_extension;
  } else {
    settings.encoding_format = argv[3];
  }

  settings.profile = create_encoding_profile (settings.encoding_format);

  if (!settings.profile) {
    error ("Could not find any encoding format for %s\n",
        settings.encoding_format);
    warn ("You can list available targets using %s --list-targets", argv[0]);
    res = 1;
    goto done;
  }

  g_print ("Encoding to:\n\n");
  describe_encoding_profile (settings.profile);
  if (!set_video_settings (&settings)) {
    res = -1;
    goto done;
  }

  if (!set_audio_settings (&settings)) {
    res = -1;
    goto done;
  }

  transcoder = gst_transcoder_new_full (settings.src_uri, settings.dest_uri,
      settings.profile);
  gst_transcoder_set_avoid_reencoding (transcoder, TRUE);
  gst_transcoder_set_cpu_usage (transcoder, settings.cpu_usage);

  signal_adapter = gst_transcoder_get_signal_adapter (transcoder, NULL);
  g_signal_connect_swapped (signal_adapter, "position-updated",
      G_CALLBACK (position_updated_cb), transcoder);
  g_signal_connect_swapped (signal_adapter, "warning", G_CALLBACK (_warning_cb),
      transcoder);
  g_signal_connect_swapped (signal_adapter, "error", G_CALLBACK (_error_cb),
      transcoder);


#ifdef G_OS_UNIX
  signal_watch_intr_id =
      g_unix_signal_add (SIGINT, (GSourceFunc) intr_handler, transcoder);
  signal_watch_hup_id =
      g_unix_signal_add (SIGHUP, (GSourceFunc) hup_handler, transcoder);
#endif

  ok ("Starting transcoding...");
  gst_transcoder_run (transcoder, &err);
  g_object_unref (signal_adapter);
  if (!err)
    ok ("\nDONE.");

#ifdef G_OS_UNIX
  if (signal_watch_intr_id > 0)
    g_source_remove (signal_watch_intr_id);
  if (signal_watch_hup_id > 0)
    g_source_remove (signal_watch_hup_id);
#endif

done:
  g_free (settings.dest_uri);
  g_free (settings.src_uri);

  return res;

no_extension:
  error ("No <encoding-format> specified and no extension"
      " available in the output target: %s", settings.dest_uri);
  res = 1;

  goto done;
}