3 * Copyright (C) 2014-2015 Sebastian Dröge <sebastian@centricular.com>
4 * Copyright (C) 2015 Thibault Saunier <tsaunier@gnome.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
23 * SECTION:gsttranscoder
24 * @short_description: High level API to transcode media files
25 * from one format to any other format using the GStreamer framework.
27 * - gst_transcoder_error_quark
30 #include "gsttranscoder.h"
31 #include "gsttranscoder-private.h"
33 static GOnce once = G_ONCE_INIT;
35 GST_DEBUG_CATEGORY_STATIC (gst_transcoder_debug);
36 #define GST_CAT_DEFAULT gst_transcoder_debug
38 #define DEFAULT_URI NULL
39 #define DEFAULT_POSITION GST_CLOCK_TIME_NONE
40 #define DEFAULT_DURATION GST_CLOCK_TIME_NONE
41 #define DEFAULT_POSITION_UPDATE_INTERVAL_MS 100
42 #define DEFAULT_AVOID_REENCODING FALSE
45 gst_transcoder_error_quark (void)
50 quark = g_quark_from_static_string ("gst-transcoder-error-quark");
64 PROP_POSITION_UPDATE_INTERVAL,
65 PROP_AVOID_REENCODING,
73 GstEncodingProfile *profile;
79 GMainContext *context;
82 GstElement *transcodebin;
84 GstState target_state, current_state;
85 gboolean is_live, is_eos;
86 GSource *tick_source, *ready_timeout_source;
88 guint position_update_interval_ms;
89 gint wanted_cpu_usage;
91 GstClockTime last_duration;
93 GstTranscoderState app_state;
96 GstTranscoderSignalAdapter *signal_adapter;
97 GstTranscoderSignalAdapter *sync_signal_adapter;
100 struct _GstTranscoderClass
102 GstObjectClass parent_class;
105 #define parent_class gst_transcoder_parent_class
106 G_DEFINE_TYPE (GstTranscoder, gst_transcoder, GST_TYPE_OBJECT);
108 static GParamSpec *param_specs[PROP_LAST] = { NULL, };
110 static void gst_transcoder_dispose (GObject * object);
111 static void gst_transcoder_finalize (GObject * object);
112 static void gst_transcoder_set_property (GObject * object, guint prop_id,
113 const GValue * value, GParamSpec * pspec);
114 static void gst_transcoder_get_property (GObject * object, guint prop_id,
115 GValue * value, GParamSpec * pspec);
116 static void gst_transcoder_constructed (GObject * object);
118 static gpointer gst_transcoder_main (gpointer data);
120 static gboolean gst_transcoder_set_position_update_interval_internal (gpointer
125 * gst_transcoder_set_cpu_usage:
126 * @self: The GstTranscoder to limit CPU usage on.
127 * @cpu_usage: The percentage of the CPU the process running the transcoder
128 * should try to use. It takes into account the number of cores available.
130 * Sets @cpu_usage as target percentage CPU usage of the process running the
131 * transcoding task. It will modulate the transcoding speed to reach that target
135 gst_transcoder_set_cpu_usage (GstTranscoder * self, gint cpu_usage)
137 GST_OBJECT_LOCK (self);
138 self->wanted_cpu_usage = cpu_usage;
139 if (self->transcodebin)
140 g_object_set (self->transcodebin, "cpu-usage", cpu_usage, NULL);
141 GST_OBJECT_UNLOCK (self);
145 gst_transcoder_init (GstTranscoder * self)
147 GST_TRACE_OBJECT (self, "Initializing");
149 self = gst_transcoder_get_instance_private (self);
151 g_cond_init (&self->cond);
153 self->context = g_main_context_new ();
154 self->loop = g_main_loop_new (self->context, FALSE);
155 self->api_bus = gst_bus_new ();
156 self->wanted_cpu_usage = 100;
158 self->position_update_interval_ms = DEFAULT_POSITION_UPDATE_INTERVAL_MS;
160 GST_TRACE_OBJECT (self, "Initialized");
164 gst_transcoder_class_init (GstTranscoderClass * klass)
166 GObjectClass *gobject_class = (GObjectClass *) klass;
168 gobject_class->set_property = gst_transcoder_set_property;
169 gobject_class->get_property = gst_transcoder_get_property;
170 gobject_class->dispose = gst_transcoder_dispose;
171 gobject_class->finalize = gst_transcoder_finalize;
172 gobject_class->constructed = gst_transcoder_constructed;
174 param_specs[PROP_SRC_URI] =
175 g_param_spec_string ("src-uri", "URI", "Source URI", DEFAULT_URI,
176 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
178 param_specs[PROP_DEST_URI] =
179 g_param_spec_string ("dest-uri", "URI", "Source URI", DEFAULT_URI,
180 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
182 param_specs[PROP_PROFILE] =
183 g_param_spec_object ("profile", "Profile",
184 "The GstEncodingProfile to use", GST_TYPE_ENCODING_PROFILE,
185 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
187 param_specs[PROP_POSITION] =
188 g_param_spec_uint64 ("position", "Position", "Current Position",
189 0, G_MAXUINT64, DEFAULT_POSITION,
190 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
192 param_specs[PROP_DURATION] =
193 g_param_spec_uint64 ("duration", "Duration", "Duration",
194 0, G_MAXUINT64, DEFAULT_DURATION,
195 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
197 param_specs[PROP_PIPELINE] =
198 g_param_spec_object ("pipeline", "Pipeline",
199 "GStreamer pipeline that is used",
200 GST_TYPE_ELEMENT, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
202 param_specs[PROP_POSITION_UPDATE_INTERVAL] =
203 g_param_spec_uint ("position-update-interval", "Position update interval",
204 "Interval in milliseconds between two position-updated signals."
205 "Pass 0 to stop updating the position.",
206 0, 10000, DEFAULT_POSITION_UPDATE_INTERVAL_MS,
207 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
210 * GstTranscoder:avoid-reencoding:
212 * See #encodebin:avoid-reencoding
214 param_specs[PROP_AVOID_REENCODING] =
215 g_param_spec_boolean ("avoid-reencoding", "Avoid re-encoding",
216 "Whether to re-encode portions of compatible video streams that lay on segment boundaries",
217 DEFAULT_AVOID_REENCODING, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
219 g_object_class_install_properties (gobject_class, PROP_LAST, param_specs);
223 gst_transcoder_dispose (GObject * object)
225 GstTranscoder *self = GST_TRANSCODER (object);
227 GST_TRACE_OBJECT (self, "Stopping main thread");
229 GST_OBJECT_LOCK (self);
231 g_main_loop_quit (self->loop);
232 GST_OBJECT_UNLOCK (self);
234 g_thread_join (self->thread);
236 GST_OBJECT_LOCK (self);
239 g_main_loop_unref (self->loop);
242 g_main_context_unref (self->context);
243 self->context = NULL;
245 gst_clear_object (&self->signal_adapter);
246 gst_clear_object (&self->sync_signal_adapter);
247 GST_OBJECT_UNLOCK (self);
249 GST_OBJECT_UNLOCK (self);
252 G_OBJECT_CLASS (parent_class)->dispose (object);
256 gst_transcoder_finalize (GObject * object)
258 GstTranscoder *self = GST_TRANSCODER (object);
260 GST_TRACE_OBJECT (self, "Finalizing");
262 g_free (self->source_uri);
263 g_free (self->dest_uri);
264 g_cond_clear (&self->cond);
266 G_OBJECT_CLASS (parent_class)->finalize (object);
270 gst_transcoder_constructed (GObject * object)
272 GstTranscoder *self = GST_TRANSCODER (object);
274 GST_TRACE_OBJECT (self, "Constructed");
277 gst_element_factory_make ("uritranscodebin", "uritranscodebin");
279 g_object_set (self->transcodebin, "source-uri", self->source_uri,
280 "dest-uri", self->dest_uri, "profile", self->profile,
281 "cpu-usage", self->wanted_cpu_usage, NULL);
283 GST_OBJECT_LOCK (self);
284 self->thread = g_thread_new ("GstTranscoder", gst_transcoder_main, self);
285 while (!self->loop || !g_main_loop_is_running (self->loop))
286 g_cond_wait (&self->cond, GST_OBJECT_GET_LOCK (self));
287 GST_OBJECT_UNLOCK (self);
289 G_OBJECT_CLASS (parent_class)->constructed (object);
293 gst_transcoder_set_property (GObject * object, guint prop_id,
294 const GValue * value, GParamSpec * pspec)
296 GstTranscoder *self = GST_TRANSCODER (object);
300 GST_OBJECT_LOCK (self);
301 g_free (self->source_uri);
302 self->source_uri = g_value_dup_string (value);
303 GST_DEBUG_OBJECT (self, "Set source_uri=%s", self->source_uri);
304 GST_OBJECT_UNLOCK (self);
308 GST_OBJECT_LOCK (self);
309 g_free (self->dest_uri);
310 self->dest_uri = g_value_dup_string (value);
311 GST_DEBUG_OBJECT (self, "Set dest_uri=%s", self->dest_uri);
312 GST_OBJECT_UNLOCK (self);
315 case PROP_POSITION_UPDATE_INTERVAL:
316 GST_OBJECT_LOCK (self);
317 self->position_update_interval_ms = g_value_get_uint (value);
318 GST_DEBUG_OBJECT (self, "Set position update interval=%u ms",
319 g_value_get_uint (value));
320 GST_OBJECT_UNLOCK (self);
322 gst_transcoder_set_position_update_interval_internal (self);
325 GST_OBJECT_LOCK (self);
326 self->profile = g_value_dup_object (value);
327 GST_OBJECT_UNLOCK (self);
329 case PROP_AVOID_REENCODING:
330 g_object_set (self->transcodebin, "avoid-reencoding",
331 g_value_get_boolean (value), NULL);
334 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
340 gst_transcoder_get_property (GObject * object, guint prop_id,
341 GValue * value, GParamSpec * pspec)
343 GstTranscoder *self = GST_TRANSCODER (object);
347 GST_OBJECT_LOCK (self);
348 g_value_set_string (value, self->source_uri);
349 GST_OBJECT_UNLOCK (self);
352 GST_OBJECT_LOCK (self);
353 g_value_set_string (value, self->dest_uri);
354 GST_OBJECT_UNLOCK (self);
360 position = self->last_duration;
362 gst_element_query_position (self->transcodebin, GST_FORMAT_TIME,
364 g_value_set_uint64 (value, position);
365 GST_TRACE_OBJECT (self, "Returning position=%" GST_TIME_FORMAT,
366 GST_TIME_ARGS (g_value_get_uint64 (value)));
372 gst_element_query_duration (self->transcodebin, GST_FORMAT_TIME,
374 g_value_set_uint64 (value, duration);
375 GST_TRACE_OBJECT (self, "Returning duration=%" GST_TIME_FORMAT,
376 GST_TIME_ARGS (g_value_get_uint64 (value)));
380 g_value_set_object (value, self->transcodebin);
382 case PROP_POSITION_UPDATE_INTERVAL:
383 GST_OBJECT_LOCK (self);
384 g_value_set_uint (value,
385 gst_transcoder_get_position_update_interval (self));
386 GST_OBJECT_UNLOCK (self);
389 GST_OBJECT_LOCK (self);
390 g_value_set_object (value, self->profile);
391 GST_OBJECT_UNLOCK (self);
393 case PROP_AVOID_REENCODING:
395 gboolean avoid_reencoding;
397 g_object_get (self->transcodebin, "avoid-reencoding", &avoid_reencoding,
399 g_value_set_boolean (value, avoid_reencoding);
403 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
409 * Works same as gst_structure_set to set field/type/value triplets on message data
412 api_bus_post_message (GstTranscoder * self, GstTranscoderMessage message_type,
413 const gchar * firstfield, ...)
415 GstStructure *message_data = NULL;
416 GstMessage *msg = NULL;
419 GST_INFO ("Posting API-bus message-type: %s",
420 gst_transcoder_message_get_name (message_type));
421 message_data = gst_structure_new (GST_TRANSCODER_MESSAGE_DATA,
422 GST_TRANSCODER_MESSAGE_DATA_TYPE, GST_TYPE_TRANSCODER_MESSAGE,
425 va_start (varargs, firstfield);
426 gst_structure_set_valist (message_data, firstfield, varargs);
429 msg = gst_message_new_custom (GST_MESSAGE_APPLICATION,
430 GST_OBJECT (self), message_data);
431 GST_DEBUG ("Created message with payload: [ %" GST_PTR_FORMAT " ]",
433 gst_bus_post (self->api_bus, msg);
437 main_loop_running_cb (gpointer user_data)
439 GstTranscoder *self = GST_TRANSCODER (user_data);
441 GST_TRACE_OBJECT (self, "Main loop running now");
443 GST_OBJECT_LOCK (self);
444 g_cond_signal (&self->cond);
445 GST_OBJECT_UNLOCK (self);
447 return G_SOURCE_REMOVE;
451 tick_cb (gpointer user_data)
453 GstTranscoder *self = GST_TRANSCODER (user_data);
456 if (self->target_state < GST_STATE_PAUSED)
457 return G_SOURCE_CONTINUE;
459 if (!gst_element_query_position (self->transcodebin, GST_FORMAT_TIME,
461 GST_LOG_OBJECT (self, "Could not query position");
462 return G_SOURCE_CONTINUE;
465 GST_LOG_OBJECT (self, "Position %" GST_TIME_FORMAT, GST_TIME_ARGS (position));
467 api_bus_post_message (self, GST_TRANSCODER_MESSAGE_POSITION_UPDATED,
468 GST_TRANSCODER_MESSAGE_DATA_POSITION, GST_TYPE_CLOCK_TIME, position,
471 return G_SOURCE_CONTINUE;
475 add_tick_source (GstTranscoder * self)
477 if (self->tick_source)
480 if (!self->position_update_interval_ms)
483 self->tick_source = g_timeout_source_new (self->position_update_interval_ms);
484 g_source_set_callback (self->tick_source, (GSourceFunc) tick_cb, self, NULL);
485 g_source_attach (self->tick_source, self->context);
489 remove_tick_source (GstTranscoder * self)
491 if (!self->tick_source)
494 g_source_destroy (self->tick_source);
495 g_source_unref (self->tick_source);
496 self->tick_source = NULL;
500 dump_dot_file (GstTranscoder * self, const gchar * name)
504 full_name = g_strdup_printf ("gst-transcoder.%p.%s", self, name);
506 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (self->transcodebin),
507 GST_DEBUG_GRAPH_SHOW_ALL, full_name);
513 error_cb (G_GNUC_UNUSED GstBus * bus, GstMessage * msg, gpointer user_data)
516 GstTranscoder *self = GST_TRANSCODER (user_data);
517 gchar *name, *debug, *message;
518 GstStructure *details = NULL;
520 dump_dot_file (self, "error");
522 gst_message_parse_error (msg, &err, &debug);
523 gst_message_parse_error_details (msg, (const GstStructure **) &details);
526 details = gst_structure_new_empty ("details");
528 details = gst_structure_copy (details);
530 name = gst_object_get_path_string (msg->src);
531 message = gst_error_get_message (err->domain, err->code);
533 gst_structure_set (details, "debug", G_TYPE_STRING, debug,
534 "msg-source-element-name", G_TYPE_STRING, "name",
535 "msg-source-type", G_TYPE_GTYPE, G_OBJECT_TYPE (msg->src),
536 "msg-error", G_TYPE_STRING, message, NULL);
538 api_bus_post_message (self, GST_TRANSCODER_MESSAGE_ERROR,
539 GST_TRANSCODER_MESSAGE_DATA_ERROR, G_TYPE_ERROR, err,
540 GST_TRANSCODER_MESSAGE_DATA_ISSUE_DETAILS, GST_TYPE_STRUCTURE, details,
543 gst_structure_free (details);
544 g_clear_error (&err);
551 warning_cb (G_GNUC_UNUSED GstBus * bus, GstMessage * msg, gpointer user_data)
553 GstTranscoder *self = GST_TRANSCODER (user_data);
554 GError *err, *transcoder_err;
555 gchar *name, *debug, *message, *full_message;
556 const GstStructure *details = NULL;
558 dump_dot_file (self, "warning");
560 gst_message_parse_warning (msg, &err, &debug);
561 gst_message_parse_warning_details (msg, &details);
563 name = gst_object_get_path_string (msg->src);
564 message = gst_error_get_message (err->domain, err->code);
568 g_strdup_printf ("Warning from element %s: %s\n%s\n%s", name, message,
569 err->message, debug);
572 g_strdup_printf ("Warning from element %s: %s\n%s", name, message,
575 GST_WARNING_OBJECT (self, "WARNING: from element %s: %s", name, err->message);
577 GST_WARNING_OBJECT (self, "Additional debug info: %s", debug);
580 g_error_new_literal (GST_TRANSCODER_ERROR, GST_TRANSCODER_ERROR_FAILED,
583 api_bus_post_message (self, GST_TRANSCODER_MESSAGE_WARNING,
584 GST_TRANSCODER_MESSAGE_DATA_WARNING, G_TYPE_ERROR, transcoder_err,
585 GST_TRANSCODER_MESSAGE_DATA_ISSUE_DETAILS, GST_TYPE_STRUCTURE, details,
588 g_clear_error (&transcoder_err);
589 g_clear_error (&err);
592 g_free (full_message);
597 notify_state_changed (GstTranscoder * self, GstTranscoderState new_state)
599 if (new_state == self->app_state)
602 GST_DEBUG_OBJECT (self, "Notifying new state: %s",
603 gst_transcoder_state_get_name (new_state));
604 self->app_state = new_state;
605 api_bus_post_message (self, GST_TRANSCODER_MESSAGE_STATE_CHANGED,
606 GST_TRANSCODER_MESSAGE_DATA_STATE, GST_TYPE_TRANSCODER_STATE, new_state,
611 eos_cb (G_GNUC_UNUSED GstBus * bus, G_GNUC_UNUSED GstMessage * msg,
614 GstTranscoder *self = GST_TRANSCODER (user_data);
616 GST_DEBUG_OBJECT (self, "End of stream");
618 gst_element_query_duration (self->transcodebin, GST_FORMAT_TIME,
619 (gint64 *) & self->last_duration);
621 remove_tick_source (self);
623 notify_state_changed (self, GST_TRANSCODER_STATE_STOPPED);
624 api_bus_post_message (self, GST_TRANSCODER_MESSAGE_DONE, NULL, NULL);
629 clock_lost_cb (G_GNUC_UNUSED GstBus * bus, G_GNUC_UNUSED GstMessage * msg,
632 GstTranscoder *self = GST_TRANSCODER (user_data);
633 GstStateChangeReturn state_ret;
635 GST_DEBUG_OBJECT (self, "Clock lost");
636 if (self->target_state >= GST_STATE_PLAYING) {
637 state_ret = gst_element_set_state (self->transcodebin, GST_STATE_PAUSED);
638 if (state_ret != GST_STATE_CHANGE_FAILURE)
639 state_ret = gst_element_set_state (self->transcodebin, GST_STATE_PLAYING);
641 if (state_ret == GST_STATE_CHANGE_FAILURE) {
642 GError *err = g_error_new (GST_TRANSCODER_ERROR,
643 GST_TRANSCODER_ERROR_FAILED, "Failed to handle clock loss");
644 api_bus_post_message (self, GST_TRANSCODER_MESSAGE_ERROR,
645 GST_TRANSCODER_MESSAGE_DATA_ERROR, G_TYPE_ERROR, err, NULL);
652 state_changed_cb (G_GNUC_UNUSED GstBus * bus, GstMessage * msg,
655 GstTranscoder *self = GST_TRANSCODER (user_data);
656 GstState old_state, new_state, pending_state;
658 gst_message_parse_state_changed (msg, &old_state, &new_state, &pending_state);
660 if (GST_MESSAGE_SRC (msg) == GST_OBJECT (self->transcodebin)) {
661 gchar *transition_name;
663 GST_DEBUG_OBJECT (self, "Changed state old: %s new: %s pending: %s",
664 gst_element_state_get_name (old_state),
665 gst_element_state_get_name (new_state),
666 gst_element_state_get_name (pending_state));
668 transition_name = g_strdup_printf ("%s_%s",
669 gst_element_state_get_name (old_state),
670 gst_element_state_get_name (new_state));
671 dump_dot_file (self, transition_name);
672 g_free (transition_name);
674 self->current_state = new_state;
676 if (new_state == GST_STATE_PAUSED
677 && pending_state == GST_STATE_VOID_PENDING) {
678 remove_tick_source (self);
679 notify_state_changed (self, GST_TRANSCODER_STATE_PAUSED);
682 if (new_state == GST_STATE_PLAYING
683 && pending_state == GST_STATE_VOID_PENDING) {
684 add_tick_source (self);
685 notify_state_changed (self, GST_TRANSCODER_STATE_PLAYING);
691 duration_changed_cb (G_GNUC_UNUSED GstBus * bus, G_GNUC_UNUSED GstMessage * msg,
694 GstTranscoder *self = GST_TRANSCODER (user_data);
697 if (gst_element_query_duration (self->transcodebin, GST_FORMAT_TIME,
699 api_bus_post_message (self, GST_TRANSCODER_MESSAGE_DURATION_CHANGED,
700 GST_TRANSCODER_MESSAGE_DATA_DURATION, GST_TYPE_CLOCK_TIME,
706 latency_cb (G_GNUC_UNUSED GstBus * bus, G_GNUC_UNUSED GstMessage * msg,
709 GstTranscoder *self = GST_TRANSCODER (user_data);
711 GST_DEBUG_OBJECT (self, "Latency changed");
713 gst_bin_recalculate_latency (GST_BIN (self->transcodebin));
717 request_state_cb (G_GNUC_UNUSED GstBus * bus, GstMessage * msg,
720 GstTranscoder *self = GST_TRANSCODER (user_data);
722 GstStateChangeReturn state_ret;
724 gst_message_parse_request_state (msg, &state);
726 GST_DEBUG_OBJECT (self, "State %s requested",
727 gst_element_state_get_name (state));
729 self->target_state = state;
730 state_ret = gst_element_set_state (self->transcodebin, state);
731 if (state_ret == GST_STATE_CHANGE_FAILURE) {
732 GError *err = g_error_new (GST_TRANSCODER_ERROR,
733 GST_TRANSCODER_ERROR_FAILED,
734 "Failed to change to requested state %s",
735 gst_element_state_get_name (state));
737 api_bus_post_message (self, GST_TRANSCODER_MESSAGE_ERROR,
738 GST_TRANSCODER_MESSAGE_DATA_ERROR, G_TYPE_ERROR, err, NULL);
744 element_cb (G_GNUC_UNUSED GstBus * bus, GstMessage * msg, gpointer user_data)
746 GstTranscoder *self = GST_TRANSCODER (user_data);
747 const GstStructure *s;
749 s = gst_message_get_structure (msg);
750 if (gst_structure_has_name (s, "redirect")) {
751 const gchar *new_location;
753 new_location = gst_structure_get_string (s, "new-location");
755 const GValue *locations_list, *location_val;
758 locations_list = gst_structure_get_value (s, "locations");
759 size = gst_value_list_get_size (locations_list);
760 for (i = 0; i < size; ++i) {
761 const GstStructure *location_s;
763 location_val = gst_value_list_get_value (locations_list, i);
764 if (!GST_VALUE_HOLDS_STRUCTURE (location_val))
767 location_s = (const GstStructure *) g_value_get_boxed (location_val);
768 if (!gst_structure_has_name (location_s, "redirect"))
771 new_location = gst_structure_get_string (location_s, "new-location");
778 GST_FIXME_OBJECT (self, "Handle redirection to '%s'", new_location);
785 gst_transcoder_main (gpointer data)
787 GstTranscoder *self = GST_TRANSCODER (data);
791 GST_TRACE_OBJECT (self, "Starting main thread");
793 g_main_context_push_thread_default (self->context);
795 source = g_idle_source_new ();
796 g_source_set_callback (source, (GSourceFunc) main_loop_running_cb, self,
798 g_source_attach (source, self->context);
799 g_source_unref (source);
801 self->bus = bus = gst_element_get_bus (self->transcodebin);
802 gst_bus_add_signal_watch (bus);
804 g_signal_connect (G_OBJECT (bus), "message::error", G_CALLBACK (error_cb),
806 g_signal_connect (G_OBJECT (bus), "message::warning", G_CALLBACK (warning_cb),
808 g_signal_connect (G_OBJECT (bus), "message::eos", G_CALLBACK (eos_cb), self);
809 g_signal_connect (G_OBJECT (bus), "message::state-changed",
810 G_CALLBACK (state_changed_cb), self);
811 g_signal_connect (G_OBJECT (bus), "message::clock-lost",
812 G_CALLBACK (clock_lost_cb), self);
813 g_signal_connect (G_OBJECT (bus), "message::duration-changed",
814 G_CALLBACK (duration_changed_cb), self);
815 g_signal_connect (G_OBJECT (bus), "message::latency",
816 G_CALLBACK (latency_cb), self);
817 g_signal_connect (G_OBJECT (bus), "message::request-state",
818 G_CALLBACK (request_state_cb), self);
819 g_signal_connect (G_OBJECT (bus), "message::element",
820 G_CALLBACK (element_cb), self);
822 self->target_state = GST_STATE_NULL;
823 self->current_state = GST_STATE_NULL;
824 self->is_eos = FALSE;
825 self->is_live = FALSE;
826 self->app_state = GST_TRANSCODER_STATE_STOPPED;
828 GST_TRACE_OBJECT (self, "Starting main loop");
829 g_main_loop_run (self->loop);
830 GST_TRACE_OBJECT (self, "Stopped main loop");
832 gst_bus_remove_signal_watch (bus);
833 gst_object_unref (bus);
835 remove_tick_source (self);
837 g_main_context_pop_thread_default (self->context);
839 self->target_state = GST_STATE_NULL;
840 self->current_state = GST_STATE_NULL;
841 if (self->transcodebin) {
842 gst_element_set_state (self->transcodebin, GST_STATE_NULL);
843 g_clear_object (&self->transcodebin);
846 GST_TRACE_OBJECT (self, "Stopped main thread");
852 gst_transcoder_init_once (G_GNUC_UNUSED gpointer user_data)
854 GST_DEBUG_CATEGORY_INIT (gst_transcoder_debug, "gst-transcoder", 0,
856 gst_transcoder_error_quark ();
861 static GstEncodingProfile *
862 create_encoding_profile (const gchar * pname)
864 GstEncodingProfile *profile;
865 GValue value = G_VALUE_INIT;
867 g_value_init (&value, GST_TYPE_ENCODING_PROFILE);
869 if (!gst_value_deserialize (&value, pname)) {
870 g_value_reset (&value);
875 profile = g_value_dup_object (&value);
876 g_value_reset (&value);
882 * gst_transcoder_new:
883 * @source_uri: The URI of the media stream to transcode
884 * @dest_uri: The URI of the destination of the transcoded stream
885 * @encoding_profile: The serialized #GstEncodingProfile defining the output
886 * format. Have a look at the #GstEncodingProfile documentation to find more
887 * about the serialization format.
889 * Returns: a new #GstTranscoder instance
892 gst_transcoder_new (const gchar * source_uri,
893 const gchar * dest_uri, const gchar * encoding_profile)
895 GstEncodingProfile *profile;
897 g_once (&once, gst_transcoder_init_once, NULL);
899 g_return_val_if_fail (source_uri, NULL);
900 g_return_val_if_fail (dest_uri, NULL);
901 g_return_val_if_fail (encoding_profile, NULL);
903 profile = create_encoding_profile (encoding_profile);
905 return gst_transcoder_new_full (source_uri, dest_uri, profile);
909 * gst_transcoder_new_full:
910 * @source_uri: The URI of the media stream to transcode
911 * @dest_uri: The URI of the destination of the transcoded stream
912 * @profile: The #GstEncodingProfile defining the output format
913 * have a look at the #GstEncodingProfile documentation to find more
914 * about the serialization format.
916 * Returns: a new #GstTranscoder instance
919 gst_transcoder_new_full (const gchar * source_uri,
920 const gchar * dest_uri, GstEncodingProfile * profile)
922 g_once (&once, gst_transcoder_init_once, NULL);
924 g_return_val_if_fail (source_uri, NULL);
925 g_return_val_if_fail (dest_uri, NULL);
927 return g_object_new (GST_TYPE_TRANSCODER, "src-uri", source_uri,
928 "dest-uri", dest_uri, "profile", profile, NULL);
938 _error_cb (RunSyncData * data, GError * error, GstStructure * details)
940 if (data->error == NULL)
941 data->error = g_error_copy (error);
944 g_main_loop_quit (data->loop);
950 _done_cb (RunSyncData * data)
953 g_main_loop_quit (data->loop);
959 * gst_transcoder_run:
960 * @self: The GstTranscoder to run
961 * @error: (allow-none): An error to be set if transcoding fails
963 * Run the transcoder task synchonously. You can connect
964 * to the 'position' signal to get information about the
965 * progress of the transcoding.
968 gst_transcoder_run (GstTranscoder * self, GError ** error)
970 RunSyncData data = { 0, };
971 GstTranscoderSignalAdapter *signal_adapter;
973 g_return_val_if_fail (GST_IS_TRANSCODER (self), FALSE);
975 signal_adapter = gst_transcoder_get_signal_adapter (self, NULL);
977 data.loop = g_main_loop_new (NULL, FALSE);
978 g_signal_connect_swapped (signal_adapter, "error", G_CALLBACK (_error_cb),
980 g_signal_connect_swapped (signal_adapter, "done", G_CALLBACK (_done_cb),
982 gst_transcoder_run_async (self);
985 g_main_loop_run (data.loop);
987 gst_element_set_state (self->transcodebin, GST_STATE_NULL);
988 g_object_unref (signal_adapter);
992 g_propagate_error (error, data.error);
1001 * gst_transcoder_run_async:
1002 * @self: The GstTranscoder to run
1004 * Run the transcoder task asynchronously. You should connect
1005 * to the 'done' signal to be notified about when the
1006 * transcoding is done, and to the 'error' signal to be
1007 * notified about any error.
1010 gst_transcoder_run_async (GstTranscoder * self)
1012 GstStateChangeReturn state_ret;
1014 g_return_if_fail (GST_IS_TRANSCODER (self));
1016 GST_DEBUG_OBJECT (self, "Play");
1018 if (!self->profile) {
1019 GError *err = g_error_new (GST_TRANSCODER_ERROR,
1020 GST_TRANSCODER_ERROR_FAILED, "No \"profile\" provided");
1022 api_bus_post_message (self, GST_TRANSCODER_MESSAGE_ERROR,
1023 GST_TRANSCODER_MESSAGE_DATA_ERROR, G_TYPE_ERROR, err, NULL);
1029 self->target_state = GST_STATE_PLAYING;
1030 state_ret = gst_element_set_state (self->transcodebin, GST_STATE_PLAYING);
1032 if (state_ret == GST_STATE_CHANGE_FAILURE) {
1033 GError *err = g_error_new (GST_TRANSCODER_ERROR,
1034 GST_TRANSCODER_ERROR_FAILED, "Could not start transcoding");
1035 api_bus_post_message (self, GST_TRANSCODER_MESSAGE_ERROR,
1036 GST_TRANSCODER_MESSAGE_DATA_ERROR, G_TYPE_ERROR, err, NULL);
1040 } else if (state_ret == GST_STATE_CHANGE_NO_PREROLL) {
1041 self->is_live = TRUE;
1042 GST_DEBUG_OBJECT (self, "Pipeline is live");
1049 gst_transcoder_set_position_update_interval_internal (gpointer user_data)
1051 GstTranscoder *self = user_data;
1053 GST_OBJECT_LOCK (self);
1055 if (self->tick_source) {
1056 remove_tick_source (self);
1057 add_tick_source (self);
1060 GST_OBJECT_UNLOCK (self);
1062 return G_SOURCE_REMOVE;
1066 * gst_transcoder_set_position_update_interval:
1067 * @self: #GstTranscoder instance
1068 * @interval: interval in ms
1070 * Set interval in milliseconds between two position-updated signals.
1071 * Pass 0 to stop updating the position.
1074 gst_transcoder_set_position_update_interval (GstTranscoder * self,
1077 g_return_if_fail (GST_IS_TRANSCODER (self));
1078 g_return_if_fail (interval <= 10000);
1080 GST_OBJECT_LOCK (self);
1081 self->position_update_interval_ms = interval;
1082 GST_OBJECT_UNLOCK (self);
1084 gst_transcoder_set_position_update_interval_internal (self);
1088 * gst_transcoder_get_position_update_interval:
1089 * @self: #GstTranscoder instance
1091 * Returns: current position update interval in milliseconds
1094 gst_transcoder_get_position_update_interval (GstTranscoder * self)
1096 g_return_val_if_fail (GST_IS_TRANSCODER (self),
1097 DEFAULT_POSITION_UPDATE_INTERVAL_MS);
1099 return self->position_update_interval_ms;
1103 * gst_transcoder_get_source_uri:
1104 * @self: #GstTranscoder instance
1106 * Gets the URI of the currently-transcoding stream.
1108 * Returns: (transfer full): a string containing the URI of the
1109 * source stream. g_free() after usage.
1112 gst_transcoder_get_source_uri (GstTranscoder * self)
1116 g_return_val_if_fail (GST_IS_TRANSCODER (self), DEFAULT_URI);
1118 g_object_get (self, "src-uri", &val, NULL);
1124 * gst_transcoder_get_dest_uri:
1125 * @self: #GstTranscoder instance
1127 * Gets the URI of the destination of the transcoded stream.
1129 * Returns: (transfer full): a string containing the URI of the
1130 * destination of the transcoded stream. g_free() after usage.
1133 gst_transcoder_get_dest_uri (GstTranscoder * self)
1137 g_return_val_if_fail (GST_IS_TRANSCODER (self), DEFAULT_URI);
1139 g_object_get (self, "dest-uri", &val, NULL);
1145 * gst_transcoder_get_position:
1146 * @self: #GstTranscoder instance
1148 * Returns: the absolute position time, in nanoseconds, of the
1149 * transcoding stream.
1152 gst_transcoder_get_position (GstTranscoder * self)
1156 g_return_val_if_fail (GST_IS_TRANSCODER (self), DEFAULT_POSITION);
1158 g_object_get (self, "position", &val, NULL);
1164 * gst_transcoder_get_duration:
1165 * @self: #GstTranscoder instance
1167 * Retrieves the duration of the media stream that self represents.
1169 * Returns: the duration of the transcoding media stream, in
1173 gst_transcoder_get_duration (GstTranscoder * self)
1177 g_return_val_if_fail (GST_IS_TRANSCODER (self), DEFAULT_DURATION);
1179 g_object_get (self, "duration", &val, NULL);
1185 * gst_transcoder_get_pipeline:
1186 * @self: #GstTranscoder instance
1188 * Returns: (transfer full): The internal uritranscodebin instance
1191 gst_transcoder_get_pipeline (GstTranscoder * self)
1195 g_return_val_if_fail (GST_IS_TRANSCODER (self), NULL);
1197 g_object_get (self, "pipeline", &val, NULL);
1203 * gst_transcoder_get_avoid_reencoding:
1204 * @self: The #GstTranscoder to check whether reencoding is avoided or not.
1206 * Returns: %TRUE if the transcoder tries to avoid reencoding streams where
1207 * reencoding is not strictly needed, %FALSE otherwise.
1210 gst_transcoder_get_avoid_reencoding (GstTranscoder * self)
1214 g_return_val_if_fail (GST_IS_TRANSCODER (self), FALSE);
1216 g_object_get (self->transcodebin, "avoid-reencoding", &val, NULL);
1222 * gst_transcoder_set_avoid_reencoding:
1223 * @self: The #GstTranscoder to set whether reencoding should be avoided or not.
1224 * @avoid_reencoding: %TRUE if the transcoder should try to avoid reencoding
1225 * streams where * reencoding is not strictly needed, %FALSE otherwise.
1228 gst_transcoder_set_avoid_reencoding (GstTranscoder * self,
1229 gboolean avoid_reencoding)
1231 g_return_if_fail (GST_IS_TRANSCODER (self));
1233 g_object_set (self->transcodebin, "avoid-reencoding", avoid_reencoding, NULL);
1237 * gst_transcoder_error_get_name:
1238 * @error: a #GstTranscoderError
1240 * Gets a string representing the given error.
1242 * Returns: (transfer none): a string with the given error.
1245 gst_transcoder_error_get_name (GstTranscoderError error)
1248 case GST_TRANSCODER_ERROR_FAILED:
1252 g_assert_not_reached ();
1257 * gst_transcoder_get_message_bus:
1258 * @transcoder: #GstTranscoder instance
1260 * GstTranscoder API exposes a #GstBus instance which purpose is to provide data
1261 * structures representing transcoder-internal events in form of #GstMessage-s of
1262 * type GST_MESSAGE_APPLICATION.
1264 * Each message carries a "transcoder-message" field of type #GstTranscoderMessage.
1265 * Further fields of the message data are specific to each possible value of
1268 * Applications can consume the messages asynchronously within their own
1269 * event-loop / UI-thread etc. Note that in case the application does not
1270 * consume the messages, the bus will accumulate these internally and eventually
1271 * fill memory. To avoid that, the bus has to be set "flushing".
1273 * Returns: (transfer full): The transcoder message bus instance
1278 gst_transcoder_get_message_bus (GstTranscoder * self)
1280 g_return_val_if_fail (GST_IS_TRANSCODER (self), NULL);
1282 return g_object_ref (self->api_bus);
1286 * gst_transcoder_get_sync_signal_adapter:
1287 * @self: (transfer none): #GstTranscoder instance to emit signals synchronously
1290 * Gets the #GstTranscoderSignalAdapter attached to @self to emit signals from
1291 * its thread of emission.
1293 * Returns: (transfer full): The #GstTranscoderSignalAdapter to connect signal
1298 GstTranscoderSignalAdapter *
1299 gst_transcoder_get_sync_signal_adapter (GstTranscoder * self)
1301 g_return_val_if_fail (GST_IS_TRANSCODER (self), NULL);
1303 GST_OBJECT_LOCK (self);
1304 if (!self->sync_signal_adapter)
1305 self->sync_signal_adapter =
1306 gst_transcoder_signal_adapter_new_sync_emit (self);
1307 GST_OBJECT_UNLOCK (self);
1309 return g_object_ref (self->sync_signal_adapter);
1313 * gst_transcoder_get_signal_adapter:
1314 * @self: (transfer none): #GstTranscoder instance to emit signals for.
1315 * @context: (nullable): A #GMainContext on which the main-loop will process
1316 * transcoder bus messages on. Can be NULL (thread-default
1317 * context will be used then).
1319 * Gets the #GstTranscoderSignalAdapter attached to @self if it is attached to
1320 * the right #GMainContext. If no #GstTranscoderSignalAdapter has been created
1321 * yet, it will be created and returned, other calls will return that same
1322 * adapter until it is destroyed, at which point, a new one can be attached the
1325 * Returns: (transfer full)(nullable): The #GstTranscoderSignalAdapter to
1326 * connect signal handlers to.
1330 GstTranscoderSignalAdapter *
1331 gst_transcoder_get_signal_adapter (GstTranscoder * self, GMainContext * context)
1333 g_return_val_if_fail (GST_IS_TRANSCODER (self), NULL);
1336 context = g_main_context_get_thread_default ();
1338 context = g_main_context_default ();
1340 GST_OBJECT_LOCK (self);
1341 if (!self->signal_adapter) {
1342 self->signal_adapter = gst_transcoder_signal_adapter_new (self, context);
1343 } else if (g_source_get_context (self->signal_adapter->source) != context) {
1344 GST_WARNING_OBJECT (self, "Trying to get an adapter for a different "
1345 "GMainContext than the one attached, this is not possible");
1346 GST_OBJECT_UNLOCK (self);
1350 GST_OBJECT_UNLOCK (self);
1352 return g_object_ref (self->signal_adapter);
1356 * gst_transcoder_message_get_name:
1357 * @message: a #GstTranscoderMessage
1359 * Returns (transfer none): The message name
1364 gst_transcoder_message_get_name (GstTranscoderMessage message)
1366 GEnumClass *enum_class;
1367 GEnumValue *enum_value;
1368 enum_class = g_type_class_ref (GST_TYPE_TRANSCODER_MESSAGE);
1369 enum_value = g_enum_get_value (enum_class, message);
1370 g_assert (enum_value != NULL);
1371 g_type_class_unref (enum_class);
1372 return enum_value->value_name;
1376 #define PARSE_MESSAGE_FIELD(msg, field, value_type, value) G_STMT_START { \
1377 const GstStructure *data = NULL; \
1378 g_return_if_fail (gst_transcoder_is_transcoder_message (msg)); \
1379 data = gst_message_get_structure (msg); \
1380 if (!gst_structure_get (data, field, value_type, value, NULL)) { \
1381 g_error ("Could not parse field from structure: %s", field); \
1386 * gst_transcoder_is_transcoder_message:
1387 * @msg: A #GstMessage
1389 * Returns: A #gboolean indicating whether the passes message represents a #GstTranscoder message or not.
1394 gst_transcoder_is_transcoder_message (GstMessage * msg)
1396 const GstStructure *data = NULL;
1397 g_return_val_if_fail (GST_IS_MESSAGE (msg), FALSE);
1399 data = gst_message_get_structure (msg);
1400 g_return_val_if_fail (data, FALSE);
1402 return g_str_equal (gst_structure_get_name (data),
1403 GST_TRANSCODER_MESSAGE_DATA);
1407 * gst_transcoder_message_parse_duration:
1408 * @msg: A #GstMessage
1409 * @duration: (out): the resulting duration
1411 * Parse the given duration @msg and extract the corresponding #GstClockTime
1416 gst_transcoder_message_parse_duration (GstMessage * msg,
1417 GstClockTime * duration)
1419 PARSE_MESSAGE_FIELD (msg, GST_TRANSCODER_MESSAGE_DATA_DURATION,
1420 GST_TYPE_CLOCK_TIME, duration);
1424 * gst_transcoder_message_parse_position:
1425 * @msg: A #GstMessage
1426 * @position: (out): the resulting position
1428 * Parse the given position @msg and extract the corresponding #GstClockTime
1433 gst_transcoder_message_parse_position (GstMessage * msg,
1434 GstClockTime * position)
1436 PARSE_MESSAGE_FIELD (msg, GST_TRANSCODER_MESSAGE_DATA_POSITION,
1437 GST_TYPE_CLOCK_TIME, position);
1441 * gst_transcoder_message_parse_state:
1442 * @msg: A #GstMessage
1443 * @state: (out): the resulting state
1445 * Parse the given state @msg and extract the corresponding #GstTranscoderState
1450 gst_transcoder_message_parse_state (GstMessage * msg,
1451 GstTranscoderState * state)
1453 PARSE_MESSAGE_FIELD (msg, GST_TRANSCODER_MESSAGE_DATA_STATE,
1454 GST_TYPE_TRANSCODER_STATE, state);
1458 * gst_transcoder_message_parse_error:
1459 * @msg: A #GstMessage
1460 * @error: (out) (optional) (transfer full): the resulting error
1461 * @details: (out): (transfer full): A GstStructure containing extra details about the error
1463 * Parse the given error @msg and extract the corresponding #GError
1468 gst_transcoder_message_parse_error (GstMessage * msg, GError * error,
1469 GstStructure ** details)
1471 PARSE_MESSAGE_FIELD (msg, GST_TRANSCODER_MESSAGE_DATA_ERROR, G_TYPE_ERROR,
1473 PARSE_MESSAGE_FIELD (msg, GST_TRANSCODER_MESSAGE_DATA_ISSUE_DETAILS,
1474 GST_TYPE_STRUCTURE, details);
1478 * gst_transcoder_message_parse_warning:
1479 * @msg: A #GstMessage
1480 * @error: (out) (optional) (transfer full): the resulting warning
1481 * @details: (out): (transfer full): A GstStructure containing extra details about the warning
1483 * Parse the given error @msg and extract the corresponding #GError warning
1488 gst_transcoder_message_parse_warning (GstMessage * msg, GError * error,
1489 GstStructure ** details)
1491 PARSE_MESSAGE_FIELD (msg, GST_TRANSCODER_MESSAGE_DATA_WARNING, G_TYPE_ERROR,
1493 PARSE_MESSAGE_FIELD (msg, GST_TRANSCODER_MESSAGE_DATA_ISSUE_DETAILS,
1494 GST_TYPE_STRUCTURE, details);
1498 * gst_transcoder_state_get_name:
1499 * @state: a #GstTranscoderState
1501 * Gets a string representing the given state.
1503 * Returns: (transfer none): a string with the name of the state.
1508 gst_transcoder_state_get_name (GstTranscoderState state)
1511 case GST_TRANSCODER_STATE_STOPPED:
1513 case GST_TRANSCODER_STATE_PAUSED:
1515 case GST_TRANSCODER_STATE_PLAYING:
1519 g_assert_not_reached ();