1 /* GStreamer
2 *
3 * Copyright (C) 2015 Thibault Saunier <tsaunier@gnome.org>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21 #include <string.h>
22
23 #include "utils.h"
24 #include <gst/transcoder/gsttranscoder.h>
25 #ifdef G_OS_UNIX
26 #include <glib-unix.h>
27 #endif
28
29 static const gchar *HELP_SUMMARY =
30 "gst-transcoder-1.0 transcodes a stream defined by its first <input-uri>\n"
31 "argument to the place defined by its second <output-uri> argument\n"
32 "into the format described in its third <encoding-format> argument,\n"
33 "or using the given <output-uri> file extension.\n"
34 "\n"
35 "The <encoding-format> argument:\n"
36 "===============================\n"
37 "\n"
38 "If the encoding format is not defined, it will be guessed with\n"
39 "the given <output-uri> file extension."
40 "\n"
41 "<encoding-format> describe the media format into which the\n"
42 "input stream is going to be transcoded. We have two different\n"
43 "ways of describing the format:\n"
44 "\n"
45 "GstEncodingProfile serialization format\n"
46 "---------------------------------------\n"
47 "\n"
48 "GStreamer encoding profiles can be described with a quite extensive\n"
49 "syntax which is described in the GstEncodingProfile documentation.\n"
50 "\n"
51 "The simple case looks like:\n"
52 "\n"
53 " muxer_source_caps:videoencoder_source_caps:audioencoder_source_caps\n"
54 "\n"
55 "Name and category of serialized GstEncodingTarget\n"
56 "-------------------------------------------------\n"
57 "\n"
58 "Encoding targets describe well known formats which\n"
59 "those are provided in '.gep' files. You can list\n"
60 "available ones using the `--list-targets` argument.\n";
61
62 typedef struct
63 {
64 gint cpu_usage, rate;
65 gboolean list;
66 GstEncodingProfile *profile;
67 gchar *src_uri, *dest_uri, *encoding_format, *size;
68 gchar *framerate;
69 } Settings;
70
71 #ifdef G_OS_UNIX
72 static guint signal_watch_hup_id;
73 static guint signal_watch_intr_id;
74
75 static gboolean
intr_handler(gpointer user_data)76 intr_handler (gpointer user_data)
77 {
78 GstTranscoder *self = GST_TRANSCODER (user_data);
79 GstElement *pipeline = gst_transcoder_get_pipeline (self);
80
81 g_print ("handling interrupt.\n");
82
83 if (pipeline) {
84 gst_element_send_event (pipeline, gst_event_new_eos ());
85 g_object_unref (pipeline);
86 }
87
88 signal_watch_intr_id = 0;
89 return G_SOURCE_REMOVE;
90 }
91
92 static gboolean
hup_handler(gpointer user_data)93 hup_handler (gpointer user_data)
94 {
95 GstTranscoder *self = GST_TRANSCODER (user_data);
96 GstElement *pipeline = gst_transcoder_get_pipeline (self);
97
98 g_print ("handling hang up.\n");
99
100 if (pipeline) {
101 gst_element_send_event (pipeline, gst_event_new_eos ());
102 g_object_unref (pipeline);
103 }
104
105 signal_watch_intr_id = 0;
106 return G_SOURCE_REMOVE;
107 }
108 #endif /* G_OS_UNIX */
109
110 static void
position_updated_cb(GstTranscoder * transcoder,GstClockTime pos)111 position_updated_cb (GstTranscoder * transcoder, GstClockTime pos)
112 {
113 GstClockTime dur = -1;
114 gchar status[64] = { 0, };
115
116 g_object_get (transcoder, "duration", &dur, NULL);
117
118 memset (status, ' ', sizeof (status) - 1);
119
120 if (pos != -1 && dur > 0 && dur != -1) {
121 gchar dstr[32], pstr[32];
122
123 /* FIXME: pretty print in nicer format */
124 g_snprintf (pstr, 32, "%" GST_TIME_FORMAT, GST_TIME_ARGS (pos));
125 pstr[9] = '\0';
126 g_snprintf (dstr, 32, "%" GST_TIME_FORMAT, GST_TIME_ARGS (dur));
127 dstr[9] = '\0';
128 g_print ("%s / %s %s\r", pstr, dstr, status);
129 }
130 }
131
132 static GList *
get_profiles_of_type(GstEncodingProfile * profile,GType profile_type)133 get_profiles_of_type (GstEncodingProfile * profile, GType profile_type)
134 {
135 GList *tmp, *profiles = NULL;
136
137 if (GST_IS_ENCODING_CONTAINER_PROFILE (profile)) {
138 for (tmp = (GList *)
139 gst_encoding_container_profile_get_profiles
140 (GST_ENCODING_CONTAINER_PROFILE (profile)); tmp; tmp = tmp->next) {
141 if (G_OBJECT_TYPE (tmp->data) == profile_type)
142 profiles = g_list_prepend (profiles, tmp->data);
143 }
144 } else if (GST_IS_ENCODING_VIDEO_PROFILE (profile)) {
145 profiles = g_list_prepend (profiles, profile);
146 }
147
148 return profiles;
149 }
150
151 static gboolean
set_video_settings(Settings * settings)152 set_video_settings (Settings * settings)
153 {
154 GList *video_profiles, *tmp;
155 gchar *p, *tmpstr, **vsize;
156 gint width = 0, height = 0;
157 GValue framerate = G_VALUE_INIT;
158
159 if (!settings->size && !settings->framerate)
160 return TRUE;
161
162 if (settings->size) {
163 p = tmpstr = g_strdup (settings->size);
164
165 for (; *p; ++p)
166 *p = g_ascii_tolower (*p);
167
168 vsize = g_strsplit (tmpstr, "x", -1);
169 g_free (tmpstr);
170
171 if (!vsize[1] || vsize[2]) {
172 g_strfreev (vsize);
173 error ("Video size should be in the form: WxH, got %s", settings->size);
174
175 return FALSE;
176 }
177
178 width = g_ascii_strtoull (vsize[0], NULL, 0);
179 height = g_ascii_strtoull (vsize[1], NULL, 10);
180 g_strfreev (vsize);
181 }
182
183 if (settings->framerate) {
184 g_value_init (&framerate, GST_TYPE_FRACTION);
185 if (!gst_value_deserialize (&framerate, settings->framerate)) {
186 error ("Video framerate should be either a fraction or an integer"
187 " not: %s", settings->framerate);
188 return FALSE;
189 }
190 }
191
192 video_profiles = get_profiles_of_type (settings->profile,
193 GST_TYPE_ENCODING_VIDEO_PROFILE);
194 for (tmp = video_profiles; tmp; tmp = tmp->next) {
195 GstCaps *rest = gst_encoding_profile_get_restriction (tmp->data);
196
197 if (!rest)
198 rest = gst_caps_new_empty_simple ("video/x-raw");
199 else
200 rest = gst_caps_copy (rest);
201
202 if (settings->size) {
203 gst_caps_set_simple (rest, "width", G_TYPE_INT, width,
204 "height", G_TYPE_INT, height, NULL);
205 }
206 if (settings->framerate)
207 gst_caps_set_value (rest, "framerate", &framerate);
208
209 gst_encoding_profile_set_restriction (tmp->data, rest);
210 }
211
212 return TRUE;
213 }
214
215 static gboolean
set_audio_settings(Settings * settings)216 set_audio_settings (Settings * settings)
217 {
218 GList *audio_profiles, *tmp;
219
220 if (settings->rate < 0)
221 return TRUE;
222
223 audio_profiles =
224 get_profiles_of_type (settings->profile, GST_TYPE_ENCODING_AUDIO_PROFILE);
225 for (tmp = audio_profiles; tmp; tmp = tmp->next) {
226 GstCaps *rest = gst_encoding_profile_get_restriction (tmp->data);
227
228 if (!rest)
229 rest = gst_caps_new_empty_simple ("audio/x-raw");
230 else
231 rest = gst_caps_copy (rest);
232
233 gst_caps_set_simple (rest, "rate", G_TYPE_INT, settings->rate, NULL);
234 gst_encoding_profile_set_restriction (tmp->data, rest);
235 }
236
237 return TRUE;
238 }
239
240 static void
list_encoding_targets(void)241 list_encoding_targets (void)
242 {
243 GList *tmp, *targets = gst_encoding_list_all_targets (NULL);
244
245 for (tmp = targets; tmp; tmp = tmp->next) {
246 GstEncodingTarget *target = tmp->data;
247 GList *usable_profiles = get_usable_profiles (target);
248
249 if (usable_profiles) {
250 GList *tmpprof;
251
252 g_print ("\n%s (%s): %s\n * Profiles:\n",
253 gst_encoding_target_get_name (target),
254 gst_encoding_target_get_category (target),
255 gst_encoding_target_get_description (target));
256
257 for (tmpprof = usable_profiles; tmpprof; tmpprof = tmpprof->next)
258 g_print (" - %s: %s",
259 gst_encoding_profile_get_name (tmpprof->data),
260 gst_encoding_profile_get_description (tmpprof->data));
261
262 g_print ("\n");
263 g_list_free (usable_profiles);
264 }
265 }
266
267 g_list_free_full (targets, (GDestroyNotify) g_object_unref);
268 }
269
270 static void
_error_cb(GstTranscoder * transcoder,GError * err,GstStructure * details)271 _error_cb (GstTranscoder * transcoder, GError * err, GstStructure * details)
272 {
273 if (g_error_matches (err, GST_CORE_ERROR, GST_CORE_ERROR_PAD)) {
274 GstPadLinkReturn lret;
275 GType type;
276
277 if (details && gst_structure_get (details, "linking-error",
278 GST_TYPE_PAD_LINK_RETURN, &lret,
279 "msg-source-type", G_TYPE_GTYPE, &type, NULL) &&
280 type == g_type_from_name ("GstTranscodeBin")) {
281 const gchar *debug = gst_structure_get_string (details, "debug");
282
283 error ("\nCould not setup transcoding pipeline,"
284 " make sure that your transcoding format parameters"
285 " are compatible with the input stream.\n\n%s", debug);
286 return;
287 }
288 }
289
290 error ("\nFAILURE: %s", err->message);
291 }
292
293 static void
_warning_cb(GstTranscoder * transcoder,GError * error,GstStructure * details)294 _warning_cb (GstTranscoder * transcoder, GError * error, GstStructure * details)
295 {
296 gboolean cant_encode;
297 GstCaps *caps = NULL;
298 gchar *stream_id = NULL;
299
300 if (details && gst_structure_get (details, "can-t-encode-stream",
301 G_TYPE_BOOLEAN, &cant_encode, "stream-caps", GST_TYPE_CAPS,
302 &caps, "stream-id", G_TYPE_STRING, &stream_id, NULL)) {
303 gchar *source_uri = gst_transcoder_get_source_uri (transcoder);
304
305 warn ("WARNING: Input stream %s: WON'T BE ENCODED.\n"
306 "Make sure the encoding settings are valid and that"
307 " any preset you set actually exists.\n"
308 "For more information about that stream, you can inspect"
309 " the source stream with:\n\n"
310 " gst-discoverer-1.0 -v %s\n", stream_id, source_uri);
311 gst_caps_unref (caps);
312 g_free (stream_id);;
313 g_free (source_uri);;
314
315 return;
316 }
317 warn ("Got warning: %s", error->message);
318 }
319
320 int
main(int argc,char * argv[])321 main (int argc, char *argv[])
322 {
323 gint res = 0;
324 GError *err = NULL;
325 GstTranscoder *transcoder;
326 GOptionContext *ctx;
327 GstTranscoderSignalAdapter *signal_adapter;
328 Settings settings = {
329 .cpu_usage = 100,
330 .rate = -1,
331 .encoding_format = NULL,
332 .size = NULL,
333 .framerate = NULL,
334 };
335 GOptionEntry options[] = {
336 {"cpu-usage", 'c', 0, G_OPTION_ARG_INT, &settings.cpu_usage,
337 "The CPU usage to target in the transcoding process", NULL},
338 {"list-targets", 'l', G_OPTION_ARG_NONE, 0, &settings.list,
339 "List all encoding targets", NULL},
340 {"size", 's', 0, G_OPTION_ARG_STRING, &settings.size,
341 "set frame size (WxH or abbreviation)", NULL},
342 {"audio-rate", 'r', 0, G_OPTION_ARG_INT, &settings.rate,
343 "set audio sampling rate (in Hz)", NULL},
344 {"framerate", 'f', 0, G_OPTION_ARG_STRING, &settings.framerate,
345 "set video framerate as a fraction (24/1 for 24fps)"
346 " or a single number (24 for 24fps))", NULL},
347 {"video-encoder", 'v', 0, G_OPTION_ARG_STRING, &settings.size,
348 "The video encoder to use.", NULL},
349 {NULL}
350 };
351
352 g_set_prgname ("gst-transcoder");
353
354 ctx = g_option_context_new ("<source uri> <destination uri> "
355 "[<encoding format>[/<encoding profile name>]]");
356 g_option_context_set_summary (ctx, HELP_SUMMARY);
357
358 g_option_context_add_main_entries (ctx, options, NULL);
359 g_option_context_add_group (ctx, gst_init_get_option_group ());
360
361 if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
362 g_print ("Error initializing: %s\n", GST_STR_NULL (err->message));
363 g_clear_error (&err);
364 g_option_context_free (ctx);
365 return 1;
366 }
367 gst_pb_utils_init ();
368
369 if (settings.list) {
370 list_encoding_targets ();
371 return 0;
372 }
373
374 if (argc < 3 || argc > 4) {
375 g_print ("%s", g_option_context_get_help (ctx, TRUE, NULL));
376 g_option_context_free (ctx);
377
378 return -1;
379 }
380 g_option_context_free (ctx);
381
382 settings.src_uri = ensure_uri (argv[1]);
383 settings.dest_uri = ensure_uri (argv[2]);
384
385 if (argc == 3) {
386 settings.encoding_format = get_file_extension (settings.dest_uri);
387
388 if (!settings.encoding_format)
389 goto no_extension;
390 } else {
391 settings.encoding_format = argv[3];
392 }
393
394 settings.profile = create_encoding_profile (settings.encoding_format);
395
396 if (!settings.profile) {
397 error ("Could not find any encoding format for %s\n",
398 settings.encoding_format);
399 warn ("You can list available targets using %s --list-targets", argv[0]);
400 res = 1;
401 goto done;
402 }
403
404 g_print ("Encoding to:\n\n");
405 describe_encoding_profile (settings.profile);
406 if (!set_video_settings (&settings)) {
407 res = -1;
408 goto done;
409 }
410
411 if (!set_audio_settings (&settings)) {
412 res = -1;
413 goto done;
414 }
415
416 transcoder = gst_transcoder_new_full (settings.src_uri, settings.dest_uri,
417 settings.profile);
418 gst_transcoder_set_avoid_reencoding (transcoder, TRUE);
419 gst_transcoder_set_cpu_usage (transcoder, settings.cpu_usage);
420
421 signal_adapter = gst_transcoder_get_signal_adapter (transcoder, NULL);
422 g_signal_connect_swapped (signal_adapter, "position-updated",
423 G_CALLBACK (position_updated_cb), transcoder);
424 g_signal_connect_swapped (signal_adapter, "warning", G_CALLBACK (_warning_cb),
425 transcoder);
426 g_signal_connect_swapped (signal_adapter, "error", G_CALLBACK (_error_cb),
427 transcoder);
428
429
430 #ifdef G_OS_UNIX
431 signal_watch_intr_id =
432 g_unix_signal_add (SIGINT, (GSourceFunc) intr_handler, transcoder);
433 signal_watch_hup_id =
434 g_unix_signal_add (SIGHUP, (GSourceFunc) hup_handler, transcoder);
435 #endif
436
437 ok ("Starting transcoding...");
438 gst_transcoder_run (transcoder, &err);
439 g_object_unref (signal_adapter);
440 if (!err)
441 ok ("\nDONE.");
442
443 #ifdef G_OS_UNIX
444 if (signal_watch_intr_id > 0)
445 g_source_remove (signal_watch_intr_id);
446 if (signal_watch_hup_id > 0)
447 g_source_remove (signal_watch_hup_id);
448 #endif
449
450 done:
451 g_free (settings.dest_uri);
452 g_free (settings.src_uri);
453
454 return res;
455
456 no_extension:
457 error ("No <encoding-format> specified and no extension"
458 " available in the output target: %s", settings.dest_uri);
459 res = 1;
460
461 goto done;
462 }
463