3 * Copyright (C) 2015 Thibault Saunier <tsaunier@gnome.org>
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.
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.
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.
24 #include <gst/transcoder/gsttranscoder.h>
26 #include <glib-unix.h>
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"
35 "The <encoding-format> argument:\n"
36 "===============================\n"
38 "If the encoding format is not defined, it will be guessed with\n"
39 "the given <output-uri> file extension."
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"
45 "GstEncodingProfile serialization format\n"
46 "---------------------------------------\n"
48 "GStreamer encoding profiles can be described with a quite extensive\n"
49 "syntax which is described in the GstEncodingProfile documentation.\n"
51 "The simple case looks like:\n"
53 " muxer_source_caps:videoencoder_source_caps:audioencoder_source_caps\n"
55 "Name and category of serialized GstEncodingTarget\n"
56 "-------------------------------------------------\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";
66 GstEncodingProfile *profile;
67 gchar *src_uri, *dest_uri, *encoding_format, *size;
72 static guint signal_watch_hup_id;
73 static guint signal_watch_intr_id;
76 intr_handler (gpointer user_data)
78 GstTranscoder *self = GST_TRANSCODER (user_data);
79 GstElement *pipeline = gst_transcoder_get_pipeline (self);
81 g_print ("handling interrupt.\n");
84 gst_element_send_event (pipeline, gst_event_new_eos ());
85 g_object_unref (pipeline);
88 signal_watch_intr_id = 0;
89 return G_SOURCE_REMOVE;
93 hup_handler (gpointer user_data)
95 GstTranscoder *self = GST_TRANSCODER (user_data);
96 GstElement *pipeline = gst_transcoder_get_pipeline (self);
98 g_print ("handling hang up.\n");
101 gst_element_send_event (pipeline, gst_event_new_eos ());
102 g_object_unref (pipeline);
105 signal_watch_intr_id = 0;
106 return G_SOURCE_REMOVE;
108 #endif /* G_OS_UNIX */
111 position_updated_cb (GstTranscoder * transcoder, GstClockTime pos)
113 GstClockTime dur = -1;
114 gchar status[64] = { 0, };
116 g_object_get (transcoder, "duration", &dur, NULL);
118 memset (status, ' ', sizeof (status) - 1);
120 if (pos != -1 && dur > 0 && dur != -1) {
121 gchar dstr[32], pstr[32];
123 /* FIXME: pretty print in nicer format */
124 g_snprintf (pstr, 32, "%" GST_TIME_FORMAT, GST_TIME_ARGS (pos));
126 g_snprintf (dstr, 32, "%" GST_TIME_FORMAT, GST_TIME_ARGS (dur));
128 g_print ("%s / %s %s\r", pstr, dstr, status);
133 get_profiles_of_type (GstEncodingProfile * profile, GType profile_type)
135 GList *tmp, *profiles = NULL;
137 if (GST_IS_ENCODING_CONTAINER_PROFILE (profile)) {
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);
144 } else if (GST_IS_ENCODING_VIDEO_PROFILE (profile)) {
145 profiles = g_list_prepend (profiles, profile);
152 set_video_settings (Settings * settings)
154 GList *video_profiles, *tmp;
155 gchar *p, *tmpstr, **vsize;
156 gint width = 0, height = 0;
157 GValue framerate = G_VALUE_INIT;
159 if (!settings->size && !settings->framerate)
162 if (settings->size) {
163 p = tmpstr = g_strdup (settings->size);
166 *p = g_ascii_tolower (*p);
168 vsize = g_strsplit (tmpstr, "x", -1);
171 if (!vsize[1] || vsize[2]) {
173 error ("Video size should be in the form: WxH, got %s", settings->size);
178 width = g_ascii_strtoull (vsize[0], NULL, 0);
179 height = g_ascii_strtoull (vsize[1], NULL, 10);
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);
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);
198 rest = gst_caps_new_empty_simple ("video/x-raw");
200 rest = gst_caps_copy (rest);
202 if (settings->size) {
203 gst_caps_set_simple (rest, "width", G_TYPE_INT, width,
204 "height", G_TYPE_INT, height, NULL);
206 if (settings->framerate)
207 gst_caps_set_value (rest, "framerate", &framerate);
209 gst_encoding_profile_set_restriction (tmp->data, rest);
216 set_audio_settings (Settings * settings)
218 GList *audio_profiles, *tmp;
220 if (settings->rate < 0)
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);
229 rest = gst_caps_new_empty_simple ("audio/x-raw");
231 rest = gst_caps_copy (rest);
233 gst_caps_set_simple (rest, "rate", G_TYPE_INT, settings->rate, NULL);
234 gst_encoding_profile_set_restriction (tmp->data, rest);
241 list_encoding_targets (void)
243 GList *tmp, *targets = gst_encoding_list_all_targets (NULL);
245 for (tmp = targets; tmp; tmp = tmp->next) {
246 GstEncodingTarget *target = tmp->data;
247 GList *usable_profiles = get_usable_profiles (target);
249 if (usable_profiles) {
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));
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));
263 g_list_free (usable_profiles);
267 g_list_free_full (targets, (GDestroyNotify) g_object_unref);
271 _error_cb (GstTranscoder * transcoder, GError * err, GstStructure * details)
273 if (g_error_matches (err, GST_CORE_ERROR, GST_CORE_ERROR_PAD)) {
274 GstPadLinkReturn lret;
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");
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);
290 error ("\nFAILURE: %s", err->message);
294 _warning_cb (GstTranscoder * transcoder, GError * error, GstStructure * details)
296 gboolean cant_encode;
297 GstCaps *caps = NULL;
298 gchar *stream_id = NULL;
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);
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);
313 g_free (source_uri);;
317 warn ("Got warning: %s", error->message);
321 main (int argc, char *argv[])
325 GstTranscoder *transcoder;
327 GstTranscoderSignalAdapter *signal_adapter;
328 Settings settings = {
331 .encoding_format = NULL,
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},
352 g_set_prgname ("gst-transcoder");
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);
358 g_option_context_add_main_entries (ctx, options, NULL);
359 g_option_context_add_group (ctx, gst_init_get_option_group ());
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);
367 gst_pb_utils_init ();
370 list_encoding_targets ();
374 if (argc < 3 || argc > 4) {
375 g_print ("%s", g_option_context_get_help (ctx, TRUE, NULL));
376 g_option_context_free (ctx);
380 g_option_context_free (ctx);
382 settings.src_uri = ensure_uri (argv[1]);
383 settings.dest_uri = ensure_uri (argv[2]);
386 settings.encoding_format = get_file_extension (settings.dest_uri);
388 if (!settings.encoding_format)
391 settings.encoding_format = argv[3];
394 settings.profile = create_encoding_profile (settings.encoding_format);
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]);
404 g_print ("Encoding to:\n\n");
405 describe_encoding_profile (settings.profile);
406 if (!set_video_settings (&settings)) {
411 if (!set_audio_settings (&settings)) {
416 transcoder = gst_transcoder_new_full (settings.src_uri, settings.dest_uri,
418 gst_transcoder_set_avoid_reencoding (transcoder, TRUE);
419 gst_transcoder_set_cpu_usage (transcoder, settings.cpu_usage);
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),
426 g_signal_connect_swapped (signal_adapter, "error", G_CALLBACK (_error_cb),
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);
437 ok ("Starting transcoding...");
438 gst_transcoder_run (transcoder, &err);
439 g_object_unref (signal_adapter);
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);
451 g_free (settings.dest_uri);
452 g_free (settings.src_uri);
457 error ("No <encoding-format> specified and no extension"
458 " available in the output target: %s", settings.dest_uri);