1 /* GStreamer encoding bin
2 * Copyright (C) 2009 Edward Hervey <edward.hervey@collabora.co.uk>
3 * (C) 2009 Nokia Corporation
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.
26 #include "gstencodebin.h"
27 #include "gstsmartencoder.h"
28 #include "gststreamsplitter.h"
29 #include "gststreamcombiner.h"
30 #include <gst/gst-i18n-plugin.h>
33 * SECTION:element-encodebin
35 * EncodeBin provides a bin for encoding/muxing various streams according to
36 * a specified #GstEncodingProfile.
38 * Based on the profile that was set (via the #GstEncodeBin:profile property),
39 * EncodeBin will internally select and configure the required elements
40 * (encoders, muxers, but also audio and video converters) so that you can
41 * provide it raw or pre-encoded streams of data in input and have your
42 * encoded/muxed/converted stream in output.
45 * <title>Features</title>
48 * Automatic encoder and muxer selection based on elements available on the
52 * Conversion of raw audio/video streams (scaling, framerate conversion,
53 * colorspace conversion, samplerate conversion) to conform to the profile
57 * Variable number of streams. If the presence property for a stream encoding
58 * profile is 0, you can request any number of sink pads for it via the
59 * standard request pad gstreamer API or the #GstEncodeBin::request-pad action
63 * Avoid reencoding (passthrough). If the input stream is already encoded and is
64 * compatible with what the #GstEncodingProfile expects, then the stream won't
65 * be re-encoded but just passed through downstream to the muxer or the output.
68 * Mix pre-encoded and raw streams as input. In addition to the passthrough
69 * feature above, you can feed both raw audio/video *AND* already-encoded data
70 * to a pad. #GstEncodeBin will take care of passing through the compatible
71 * segments and re-encoding the segments of media that need encoding.
74 * Standard behaviour is to use a #GstEncodingContainerProfile to have both
75 * encoding and muxing performed. But you can also provide a single stream
76 * profile (like #GstEncodingAudioProfile) to only have the encoding done and
77 * handle the encoded output yourself.
80 * Audio imperfection corrections. Incoming audio streams can have non perfect
81 * timestamps (jitter), like the streams coming from ASF files. #GstEncodeBin
82 * will automatically fix those imperfections for you. See
83 * #GstEncodeBin:audio-jitter-tolerance for more details.
86 * Variable or Constant video framerate. If your #GstEncodingVideoProfile has
87 * the variableframerate property deactivated (default), then the incoming
88 * raw video stream will be retimestampped in order to produce a constant
92 * Cross-boundary re-encoding. When feeding compatible pre-encoded streams that
93 * fall on segment boundaries, and for supported formats (right now only H263),
94 * the GOP will be decoded/reencoded when needed to produce an encoded output
95 * that fits exactly within the request GstSegment.
98 * Missing plugin support. If a #GstElement is missing to encode/mux to the
99 * request profile formats, a missing-plugin #GstMessage will be posted on the
100 * #GstBus, allowing systems that support the missing-plugin system to offer the
101 * user a way to install the missing element.
110 * Handling mp3!xing!idv3 and theora!ogg tagsetting scenarios:
111 * Once we have chosen a muxer:
112 * When a new stream is requested:
113 * If muxer isn't 'Formatter' OR doesn't have a TagSetter interface:
114 * Find a Formatter for the given stream (preferably with TagSetter)
115 * Insert that before muxer
118 #define fast_pad_link(a,b) gst_pad_link_full((a),(b),GST_PAD_LINK_CHECK_NOTHING)
119 #define fast_element_link(a,b) gst_element_link_pads_full((a),"src",(b),"sink",GST_PAD_LINK_CHECK_NOTHING)
123 GST_ENCODEBIN_FLAG_NO_AUDIO_CONVERSION = (1 << 0),
124 GST_ENCODEBIN_FLAG_NO_VIDEO_CONVERSION = (1 << 1)
127 #define GST_TYPE_ENCODEBIN_FLAGS (gst_encodebin_flags_get_type())
128 GType gst_encodebin_flags_get_type (void);
130 /* generic templates */
131 static GstStaticPadTemplate muxer_src_template =
132 GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
133 GST_STATIC_CAPS_ANY);
135 static GstStaticPadTemplate video_sink_template =
136 GST_STATIC_PAD_TEMPLATE ("video_%u",
139 GST_STATIC_CAPS_ANY);
140 static GstStaticPadTemplate audio_sink_template =
141 GST_STATIC_PAD_TEMPLATE ("audio_%u",
144 GST_STATIC_CAPS_ANY);
145 /* static GstStaticPadTemplate text_sink_template = */
146 /* GST_STATIC_PAD_TEMPLATE ("text_%u", */
148 /* GST_PAD_REQUEST, */
149 /* GST_STATIC_CAPS_ANY); */
150 static GstStaticPadTemplate private_sink_template =
151 GST_STATIC_PAD_TEMPLATE ("private_%u",
154 GST_STATIC_CAPS_ANY);
160 /* the profile field is only valid if it could be entirely setup */
161 GstEncodingProfile *profile;
163 GList *streams; /* List of StreamGroup, not sorted */
166 /* Ghostpad with changing target */
169 /* TRUE if in PAUSED/PLAYING */
172 /* available muxers, encoders and parsers */
178 /* Increasing counter for unique pad name */
181 /* Cached caps for identification */
182 GstCaps *raw_video_caps;
183 GstCaps *raw_audio_caps;
184 /* GstCaps *raw_text_caps; */
186 guint queue_buffers_max;
187 guint queue_bytes_max;
188 guint64 queue_time_max;
191 gboolean avoid_reencoding;
193 GstEncodeBinFlags flags;
196 struct _GstEncodeBinClass
201 GstPad *(*request_pad) (GstEncodeBin * encodebin, GstCaps * caps);
204 typedef struct _StreamGroup StreamGroup;
209 GstEncodingProfile *profile;
210 GstPad *ghostpad; /* Sink ghostpad */
211 GstElement *inqueue; /* Queue just after the ghostpad */
212 GstElement *splitter;
213 GList *converters; /* List of conversion GstElement */
214 GstElement *capsfilter; /* profile->restriction (if non-NULL/ANY) */
215 GstElement *encoder; /* Encoder (can be NULL) */
216 GstElement *combiner;
218 GstElement *smartencoder;
219 GstElement *outfilter; /* Output capsfilter (streamprofile.format) */
220 GstElement *formatter;
221 GstElement *outqueue; /* Queue just before the muxer */
224 /* Default for queues (same defaults as queue element) */
225 #define DEFAULT_QUEUE_BUFFERS_MAX 200
226 #define DEFAULT_QUEUE_BYTES_MAX 10 * 1024 * 1024
227 #define DEFAULT_QUEUE_TIME_MAX GST_SECOND
228 #define DEFAULT_AUDIO_JITTER_TOLERANCE 20 * GST_MSECOND
229 #define DEFAULT_AVOID_REENCODING FALSE
230 #define DEFAULT_FLAGS 0
232 #define DEFAULT_RAW_CAPS \
236 "subpicture/x-dvd; " \
244 PROP_QUEUE_BUFFERS_MAX,
245 PROP_QUEUE_BYTES_MAX,
247 PROP_AUDIO_JITTER_TOLERANCE,
248 PROP_AVOID_REENCODING,
260 #define C_FLAGS(v) ((guint) v)
263 gst_encodebin_flags_get_type (void)
265 static const GFlagsValue values[] = {
266 {C_FLAGS (GST_ENCODEBIN_FLAG_NO_AUDIO_CONVERSION), "Do not use audio "
267 "conversion elements", "no-audio-conversion"},
268 {C_FLAGS (GST_ENCODEBIN_FLAG_NO_VIDEO_CONVERSION), "Do not use video "
269 "conversion elements", "no-video-conversion"},
272 static volatile GType id = 0;
274 if (g_once_init_enter ((gsize *) & id)) {
277 _id = g_flags_register_static ("GstEncodeBinFlags", values);
279 g_once_init_leave ((gsize *) & id, _id);
285 static guint gst_encode_bin_signals[LAST_SIGNAL] = { 0 };
287 static GstStaticCaps default_raw_caps = GST_STATIC_CAPS (DEFAULT_RAW_CAPS);
289 GST_DEBUG_CATEGORY_STATIC (gst_encode_bin_debug);
290 #define GST_CAT_DEFAULT gst_encode_bin_debug
292 G_DEFINE_TYPE (GstEncodeBin, gst_encode_bin, GST_TYPE_BIN);
294 static void gst_encode_bin_dispose (GObject * object);
295 static void gst_encode_bin_set_property (GObject * object, guint prop_id,
296 const GValue * value, GParamSpec * pspec);
297 static void gst_encode_bin_get_property (GObject * object, guint prop_id,
298 GValue * value, GParamSpec * pspec);
299 static GstStateChangeReturn gst_encode_bin_change_state (GstElement * element,
300 GstStateChange transition);
302 static GstPad *gst_encode_bin_request_new_pad (GstElement * element,
303 GstPadTemplate * templ, const gchar * name, const GstCaps * caps);
304 static void gst_encode_bin_release_pad (GstElement * element, GstPad * pad);
307 gst_encode_bin_set_profile (GstEncodeBin * ebin, GstEncodingProfile * profile);
308 static void gst_encode_bin_tear_down_profile (GstEncodeBin * ebin);
309 static gboolean gst_encode_bin_setup_profile (GstEncodeBin * ebin,
310 GstEncodingProfile * profile);
312 static StreamGroup *_create_stream_group (GstEncodeBin * ebin,
313 GstEncodingProfile * sprof, const gchar * sinkpadname, GstCaps * sinkcaps);
314 static void stream_group_remove (GstEncodeBin * ebin, StreamGroup * sgroup);
315 static GstPad *gst_encode_bin_request_pad_signal (GstEncodeBin * encodebin,
318 static inline GstElement *_get_formatter (GstEncodeBin * ebin,
319 GstEncodingProfile * sprof);
322 gst_encode_bin_class_init (GstEncodeBinClass * klass)
324 GObjectClass *gobject_klass;
325 GstElementClass *gstelement_klass;
327 gobject_klass = (GObjectClass *) klass;
328 gstelement_klass = (GstElementClass *) klass;
330 gobject_klass->dispose = gst_encode_bin_dispose;
331 gobject_klass->set_property = gst_encode_bin_set_property;
332 gobject_klass->get_property = gst_encode_bin_get_property;
337 * GstEncodeBin:profile:
339 * The #GstEncodingProfile to use. This property must be set before going
340 * to %GST_STATE_PAUSED or higher.
342 g_object_class_install_property (gobject_klass, PROP_PROFILE,
343 g_param_spec_object ("profile", "Profile",
344 "The GstEncodingProfile to use", GST_TYPE_ENCODING_PROFILE,
345 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
347 g_object_class_install_property (gobject_klass, PROP_QUEUE_BYTES_MAX,
348 g_param_spec_uint ("queue-bytes-max", "Max. size (kB)",
349 "Max. amount of data in the queue (bytes, 0=disable)",
350 0, G_MAXUINT, DEFAULT_QUEUE_BYTES_MAX,
351 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
353 g_object_class_install_property (gobject_klass, PROP_QUEUE_BUFFERS_MAX,
354 g_param_spec_uint ("queue-buffers-max", "Max. size (buffers)",
355 "Max. number of buffers in the queue (0=disable)", 0, G_MAXUINT,
356 DEFAULT_QUEUE_BUFFERS_MAX,
357 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
359 g_object_class_install_property (gobject_klass, PROP_QUEUE_TIME_MAX,
360 g_param_spec_uint64 ("queue-time-max", "Max. size (ns)",
361 "Max. amount of data in the queue (in ns, 0=disable)", 0, G_MAXUINT64,
362 DEFAULT_QUEUE_TIME_MAX, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
364 g_object_class_install_property (gobject_klass, PROP_AUDIO_JITTER_TOLERANCE,
365 g_param_spec_uint64 ("audio-jitter-tolerance", "Audio jitter tolerance",
366 "Amount of timestamp jitter/imperfection to allow on audio streams before inserting/dropping samples (ns)",
367 0, G_MAXUINT64, DEFAULT_AUDIO_JITTER_TOLERANCE,
368 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
370 g_object_class_install_property (gobject_klass, PROP_AVOID_REENCODING,
371 g_param_spec_boolean ("avoid-reencoding", "Avoid re-encoding",
372 "Whether to re-encode portions of compatible video streams that lay on segment boundaries",
373 DEFAULT_AVOID_REENCODING,
374 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
379 * Control the behaviour of encodebin.
381 g_object_class_install_property (gobject_klass, PROP_FLAGS,
382 g_param_spec_flags ("flags", "Flags", "Flags to control behaviour",
383 GST_TYPE_ENCODEBIN_FLAGS, DEFAULT_FLAGS,
384 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
388 * GstEncodeBin::request-pad
389 * @encodebin: a #GstEncodeBin instance
392 * Use this method to request an unused sink request #GstPad that can take the
393 * provided @caps as input. You must release the pad with
394 * gst_element_release_request_pad() when you are done with it.
396 * Returns: A compatible #GstPad, or %NULL if no compatible #GstPad could be
397 * created or is available.
399 gst_encode_bin_signals[SIGNAL_REQUEST_PAD] =
400 g_signal_new ("request-pad", G_TYPE_FROM_CLASS (klass),
401 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstEncodeBinClass,
402 request_pad), NULL, NULL, g_cclosure_marshal_generic,
403 GST_TYPE_PAD, 1, GST_TYPE_CAPS);
405 klass->request_pad = gst_encode_bin_request_pad_signal;
407 gst_element_class_add_pad_template (gstelement_klass,
408 gst_static_pad_template_get (&muxer_src_template));
409 gst_element_class_add_pad_template (gstelement_klass,
410 gst_static_pad_template_get (&video_sink_template));
411 gst_element_class_add_pad_template (gstelement_klass,
412 gst_static_pad_template_get (&audio_sink_template));
413 /* gst_element_class_add_pad_template (gstelement_klass, */
414 /* gst_static_pad_template_get (&text_sink_template)); */
415 gst_element_class_add_pad_template (gstelement_klass,
416 gst_static_pad_template_get (&private_sink_template));
418 gstelement_klass->change_state =
419 GST_DEBUG_FUNCPTR (gst_encode_bin_change_state);
420 gstelement_klass->request_new_pad =
421 GST_DEBUG_FUNCPTR (gst_encode_bin_request_new_pad);
422 gstelement_klass->release_pad =
423 GST_DEBUG_FUNCPTR (gst_encode_bin_release_pad);
425 gst_element_class_set_static_metadata (gstelement_klass,
427 "Generic/Bin/Encoder",
428 "Convenience encoding/muxing element",
429 "Edward Hervey <edward.hervey@collabora.co.uk>");
433 gst_encode_bin_dispose (GObject * object)
435 GstEncodeBin *ebin = (GstEncodeBin *) object;
438 gst_plugin_feature_list_free (ebin->muxers);
440 if (ebin->formatters)
441 gst_plugin_feature_list_free (ebin->formatters);
444 gst_plugin_feature_list_free (ebin->encoders);
447 gst_plugin_feature_list_free (ebin->parsers);
449 gst_encode_bin_tear_down_profile (ebin);
451 if (ebin->raw_video_caps)
452 gst_caps_unref (ebin->raw_video_caps);
453 if (ebin->raw_audio_caps)
454 gst_caps_unref (ebin->raw_audio_caps);
455 /* if (ebin->raw_text_caps) */
456 /* gst_caps_unref (ebin->raw_text_caps); */
458 G_OBJECT_CLASS (gst_encode_bin_parent_class)->dispose (object);
462 gst_encode_bin_init (GstEncodeBin * encode_bin)
464 GstPadTemplate *tmpl;
467 gst_element_factory_list_get_elements (GST_ELEMENT_FACTORY_TYPE_MUXER,
470 encode_bin->formatters =
471 gst_element_factory_list_get_elements (GST_ELEMENT_FACTORY_TYPE_FORMATTER,
474 encode_bin->encoders =
475 gst_element_factory_list_get_elements (GST_ELEMENT_FACTORY_TYPE_ENCODER,
478 encode_bin->parsers =
479 gst_element_factory_list_get_elements (GST_ELEMENT_FACTORY_TYPE_PARSER,
482 encode_bin->raw_video_caps = gst_caps_from_string ("video/x-raw");
483 encode_bin->raw_audio_caps = gst_caps_from_string ("audio/x-raw");
484 /* encode_bin->raw_text_caps = */
485 /* gst_caps_from_string ("text/x-raw"); */
487 encode_bin->queue_buffers_max = DEFAULT_QUEUE_BUFFERS_MAX;
488 encode_bin->queue_bytes_max = DEFAULT_QUEUE_BYTES_MAX;
489 encode_bin->queue_time_max = DEFAULT_QUEUE_TIME_MAX;
490 encode_bin->tolerance = DEFAULT_AUDIO_JITTER_TOLERANCE;
491 encode_bin->avoid_reencoding = DEFAULT_AVOID_REENCODING;
492 encode_bin->flags = DEFAULT_FLAGS;
494 tmpl = gst_static_pad_template_get (&muxer_src_template);
495 encode_bin->srcpad = gst_ghost_pad_new_no_target_from_template ("src", tmpl);
496 gst_object_unref (tmpl);
497 gst_pad_set_active (encode_bin->srcpad, TRUE);
498 gst_element_add_pad (GST_ELEMENT_CAST (encode_bin), encode_bin->srcpad);
502 gst_encode_bin_set_property (GObject * object, guint prop_id,
503 const GValue * value, GParamSpec * pspec)
505 GstEncodeBin *ebin = (GstEncodeBin *) object;
509 gst_encode_bin_set_profile (ebin,
510 (GstEncodingProfile *) g_value_get_object (value));
512 case PROP_QUEUE_BUFFERS_MAX:
513 ebin->queue_buffers_max = g_value_get_uint (value);
515 case PROP_QUEUE_BYTES_MAX:
516 ebin->queue_bytes_max = g_value_get_uint (value);
518 case PROP_QUEUE_TIME_MAX:
519 ebin->queue_time_max = g_value_get_uint64 (value);
521 case PROP_AUDIO_JITTER_TOLERANCE:
522 ebin->tolerance = g_value_get_uint64 (value);
524 case PROP_AVOID_REENCODING:
525 ebin->avoid_reencoding = g_value_get_boolean (value);
528 ebin->flags = g_value_get_flags (value);
531 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
537 gst_encode_bin_get_property (GObject * object, guint prop_id,
538 GValue * value, GParamSpec * pspec)
540 GstEncodeBin *ebin = (GstEncodeBin *) object;
544 g_value_set_object (value, (GObject *) ebin->profile);
546 case PROP_QUEUE_BUFFERS_MAX:
547 g_value_set_uint (value, ebin->queue_buffers_max);
549 case PROP_QUEUE_BYTES_MAX:
550 g_value_set_uint (value, ebin->queue_bytes_max);
552 case PROP_QUEUE_TIME_MAX:
553 g_value_set_uint64 (value, ebin->queue_time_max);
555 case PROP_AUDIO_JITTER_TOLERANCE:
556 g_value_set_uint64 (value, ebin->tolerance);
558 case PROP_AVOID_REENCODING:
559 g_value_set_boolean (value, ebin->avoid_reencoding);
562 g_value_set_flags (value, ebin->flags);
565 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
570 static inline gboolean
571 are_raw_caps (const GstCaps * caps)
573 GstCaps *raw = gst_static_caps_get (&default_raw_caps);
575 if (gst_caps_can_intersect (caps, raw)) {
576 gst_caps_unref (raw);
579 gst_caps_unref (raw);
583 /* Returns the number of time a given stream profile is currently used
586 stream_profile_used_count (GstEncodeBin * ebin, GstEncodingProfile * sprof)
588 guint nbprofused = 0;
591 for (tmp = ebin->streams; tmp; tmp = tmp->next) {
592 StreamGroup *sgroup = (StreamGroup *) tmp->data;
594 if (sgroup->profile == sprof)
601 static inline GstEncodingProfile *
602 next_unused_stream_profile (GstEncodeBin * ebin, GType ptype, GstCaps * caps)
604 GST_DEBUG_OBJECT (ebin, "ptype:%s, caps:%" GST_PTR_FORMAT,
605 g_type_name (ptype), caps);
607 if (G_UNLIKELY (ptype == G_TYPE_NONE && caps != NULL)) {
608 /* Identify the profile type based on raw caps */
609 if (gst_caps_can_intersect (ebin->raw_video_caps, caps))
610 ptype = GST_TYPE_ENCODING_VIDEO_PROFILE;
611 else if (gst_caps_can_intersect (ebin->raw_audio_caps, caps))
612 ptype = GST_TYPE_ENCODING_AUDIO_PROFILE;
613 /* else if (gst_caps_can_intersect (ebin->raw_text_caps, caps)) */
614 /* ptype = GST_TYPE_ENCODING_TEXT_PROFILE; */
615 GST_DEBUG_OBJECT (ebin, "Detected profile type as being %s",
616 g_type_name (ptype));
619 if (GST_IS_ENCODING_CONTAINER_PROFILE (ebin->profile)) {
623 gst_encoding_container_profile_get_profiles
624 (GST_ENCODING_CONTAINER_PROFILE (ebin->profile)); tmp;
626 GstEncodingProfile *sprof = (GstEncodingProfile *) tmp->data;
628 /* Pick an available Stream profile for which:
629 * * either it is of the compatibly raw type,
630 * * OR we can pass it through directly without encoding
632 if (G_TYPE_FROM_INSTANCE (sprof) == ptype) {
633 guint presence = gst_encoding_profile_get_presence (sprof);
634 GST_DEBUG ("Found a stream profile with the same type");
636 || (presence > stream_profile_used_count (ebin, sprof)))
638 } else if ((caps != NULL) && (ptype == G_TYPE_NONE)) {
642 outcaps = gst_encoding_profile_get_input_caps (sprof);
643 GST_DEBUG ("Unknown stream, seeing if it's compatible with %"
644 GST_PTR_FORMAT, outcaps);
645 res = gst_caps_can_intersect (outcaps, caps);
646 gst_caps_unref (outcaps);
658 request_pad_for_stream (GstEncodeBin * encodebin, GType ptype,
659 const gchar * name, GstCaps * caps)
662 GstEncodingProfile *sprof;
664 GST_DEBUG_OBJECT (encodebin, "name:%s caps:%" GST_PTR_FORMAT, name, caps);
666 /* Figure out if we have a unused GstEncodingProfile we can use for
668 sprof = next_unused_stream_profile (encodebin, ptype, caps);
670 if (G_UNLIKELY (sprof == NULL))
671 goto no_stream_profile;
673 sgroup = _create_stream_group (encodebin, sprof, name, caps);
674 if (G_UNLIKELY (sgroup == NULL))
675 goto no_stream_group;
677 return sgroup->ghostpad;
681 GST_WARNING_OBJECT (encodebin, "Couldn't find a compatible stream profile");
687 GST_WARNING_OBJECT (encodebin, "Couldn't create a StreamGroup");
693 gst_encode_bin_request_new_pad (GstElement * element,
694 GstPadTemplate * templ, const gchar * name, const GstCaps * caps)
696 GstEncodeBin *ebin = (GstEncodeBin *) element;
699 GST_DEBUG_OBJECT (element, "templ:%s, name:%s", templ->name_template, name);
701 /* Identify the stream group */
703 res = request_pad_for_stream (ebin, G_TYPE_NONE, name, (GstCaps *) caps);
707 GType ptype = G_TYPE_NONE;
709 if (!strcmp (templ->name_template, "video_%u"))
710 ptype = GST_TYPE_ENCODING_VIDEO_PROFILE;
711 else if (!strcmp (templ->name_template, "audio_%u"))
712 ptype = GST_TYPE_ENCODING_AUDIO_PROFILE;
713 /* else if (!strcmp (templ->name_template, "text_%u")) */
714 /* ptype = GST_TYPE_ENCODING_TEXT_PROFILE; */
716 /* FIXME : Check uniqueness of pad */
717 /* FIXME : Check that the requested number is the last one, and if not,
718 * update the last_pad_id variable so that we don't create a pad with
719 * the same name/number in the future */
721 res = request_pad_for_stream (ebin, ptype, name, NULL);
728 gst_encode_bin_request_pad_signal (GstEncodeBin * encodebin, GstCaps * caps)
730 GstPad *pad = request_pad_for_stream (encodebin, G_TYPE_NONE, NULL, caps);
732 return pad ? GST_PAD_CAST (gst_object_ref (pad)) : NULL;
735 static inline StreamGroup *
736 find_stream_group_from_pad (GstEncodeBin * ebin, GstPad * pad)
740 for (tmp = ebin->streams; tmp; tmp = tmp->next) {
741 StreamGroup *sgroup = (StreamGroup *) tmp->data;
742 if (G_UNLIKELY (sgroup->ghostpad == pad))
750 gst_encode_bin_release_pad (GstElement * element, GstPad * pad)
752 GstEncodeBin *ebin = (GstEncodeBin *) element;
755 /* Find the associated StreamGroup */
757 sgroup = find_stream_group_from_pad (ebin, pad);
758 if (G_UNLIKELY (sgroup == NULL))
759 goto no_stream_group;
761 /* Release objects/data associated with the StreamGroup */
762 stream_group_remove (ebin, sgroup);
768 GST_WARNING_OBJECT (ebin, "Couldn't find corresponding StreamGroup");
773 /* Create a parser for the given stream profile */
774 static inline GstElement *
775 _get_parser (GstEncodeBin * ebin, GstEncodingProfile * sprof)
777 GList *parsers1, *parsers, *tmp;
778 GstElement *parser = NULL;
779 GstElementFactory *parserfact = NULL;
782 format = gst_encoding_profile_get_format (sprof);
784 GST_DEBUG ("Getting list of parsers for format %" GST_PTR_FORMAT, format);
786 /* FIXME : requesting twice the parsers twice is a bit ugly, we should
787 * have a method to request on more than one condition */
789 gst_element_factory_list_filter (ebin->parsers, format,
792 gst_element_factory_list_filter (parsers1, format, GST_PAD_SINK, FALSE);
793 gst_plugin_feature_list_free (parsers1);
795 if (G_UNLIKELY (parsers == NULL)) {
796 GST_DEBUG ("Couldn't find any compatible parsers");
800 for (tmp = parsers; tmp; tmp = tmp->next) {
801 /* FIXME : We're only picking the first one so far */
802 /* FIXME : signal the user if he wants this */
803 parserfact = (GstElementFactory *) tmp->data;
808 parser = gst_element_factory_create (parserfact, NULL);
810 gst_plugin_feature_list_free (parsers);
814 gst_caps_unref (format);
820 _create_element_and_set_preset (GstElementFactory * factory,
821 const gchar * preset, const gchar * name, const gchar * preset_name)
823 GstElement *res = NULL;
825 GST_DEBUG ("Creating element from factory %s (preset factory name: %s"
826 " preset name: %s)", GST_OBJECT_NAME (factory), preset, preset_name);
828 res = gst_element_factory_create (factory, name);
830 if (preset && GST_IS_PRESET (res)) {
831 if (preset_name == NULL ||
832 g_strcmp0 (GST_OBJECT_NAME (factory), preset_name) == 0) {
834 if (!gst_preset_load_preset (GST_PRESET (res), preset)) {
835 GST_WARNING ("Couldn't set preset [%s] on element [%s]",
836 preset, GST_OBJECT_NAME (factory));
837 gst_object_unref (res);
841 GST_DEBUG ("Using a preset with no preset name, making use of the"
842 " proper element without setting any property");
844 } else if (preset_name && g_strcmp0 (GST_OBJECT_NAME (factory), preset_name)) {
845 gst_object_unref (res);
848 /* Else we keep it */
853 /* Create the encoder for the given stream profile */
854 static inline GstElement *
855 _get_encoder (GstEncodeBin * ebin, GstEncodingProfile * sprof)
857 GList *encoders, *tmp;
858 GstElement *encoder = NULL;
859 GstElementFactory *encoderfact = NULL;
861 const gchar *preset, *preset_name;
863 format = gst_encoding_profile_get_format (sprof);
864 preset = gst_encoding_profile_get_preset (sprof);
865 preset_name = gst_encoding_profile_get_preset_name (sprof);
867 GST_DEBUG ("Getting list of encoders for format %" GST_PTR_FORMAT, format);
869 /* If stream caps are raw, return identity */
870 if (G_UNLIKELY (are_raw_caps (format))) {
871 GST_DEBUG ("Stream format is raw, returning identity as the encoder");
872 encoder = gst_element_factory_make ("identity", NULL);
877 gst_element_factory_list_filter (ebin->encoders, format,
880 if (G_UNLIKELY (encoders == NULL)) {
881 GST_DEBUG ("Couldn't find any compatible encoders");
885 for (tmp = encoders; tmp; tmp = tmp->next) {
886 encoderfact = (GstElementFactory *) tmp->data;
887 if ((encoder = _create_element_and_set_preset (encoderfact, preset,
892 gst_plugin_feature_list_free (encoders);
896 gst_caps_unref (format);
902 local_element_request_pad (GstElement * element, GstPadTemplate * templ,
903 const gchar * name, const GstCaps * caps)
905 GstPad *newpad = NULL;
906 GstElementClass *oclass;
908 oclass = GST_ELEMENT_GET_CLASS (element);
910 if (oclass->request_new_pad)
911 newpad = (oclass->request_new_pad) (element, templ, name, caps);
914 gst_object_ref (newpad);
920 gst_element_get_pad_from_template (GstElement * element, GstPadTemplate * templ)
923 GstPadPresence presence;
925 /* If this function is ever exported, we need check the validity of `element'
926 * and `templ', and to make sure the template actually belongs to the
929 presence = GST_PAD_TEMPLATE_PRESENCE (templ);
933 case GST_PAD_SOMETIMES:
934 ret = gst_element_get_static_pad (element, templ->name_template);
935 if (!ret && presence == GST_PAD_ALWAYS)
937 ("Element %s has an ALWAYS template %s, but no pad of the same name",
938 GST_OBJECT_NAME (element), templ->name_template);
941 case GST_PAD_REQUEST:
942 ret = gst_element_request_pad (element, templ, NULL, NULL);
949 /* FIXME : Improve algorithm for finding compatible muxer sink pad */
950 static inline GstPad *
951 get_compatible_muxer_sink_pad (GstEncodeBin * ebin, GstElement * encoder,
955 GstPadTemplate *srctempl = NULL;
956 GstPadTemplate *sinktempl;
960 srcpad = gst_element_get_static_pad (encoder, "src");
962 srctempl = gst_pad_get_pad_template (srcpad);
964 GST_DEBUG_OBJECT (ebin,
965 "Attempting to find pad from muxer %s compatible with %s:%s",
966 GST_ELEMENT_NAME (ebin->muxer), GST_DEBUG_PAD_NAME (srcpad));
968 gst_object_unref (srcpad);
969 sinktempl = gst_element_get_compatible_pad_template (ebin->muxer, srctempl);
970 gst_object_unref (srctempl);
973 gst_pad_template_new ("whatever", GST_PAD_SRC, GST_PAD_ALWAYS,
975 g_assert (srctempl != NULL);
976 sinktempl = gst_element_get_compatible_pad_template (ebin->muxer, srctempl);
977 g_object_unref (srctempl);
980 if (G_UNLIKELY (sinktempl == NULL))
983 sinkpad = gst_element_get_pad_from_template (ebin->muxer, sinktempl);
989 GST_WARNING_OBJECT (ebin, "No compatible pad available on muxer");
995 _has_class (GstElement * element, const gchar * classname)
997 GstElementClass *klass;
1000 klass = GST_ELEMENT_GET_CLASS (element);
1001 value = gst_element_class_get_metadata (klass, GST_ELEMENT_METADATA_KLASS);
1003 return strstr (value, classname) != NULL;
1006 /* FIXME : Add handling of streams that don't need encoding */
1007 /* FIXME : Add handling of streams that don't require conversion elements */
1009 * Create the elements, StreamGroup, add the sink pad, link it to the muxer
1011 * sinkpadname: If non-NULL, that name will be assigned to the sink ghost pad
1012 * sinkcaps: If non-NULL will be used to figure out how to setup the group */
1013 static StreamGroup *
1014 _create_stream_group (GstEncodeBin * ebin, GstEncodingProfile * sprof,
1015 const gchar * sinkpadname, GstCaps * sinkcaps)
1017 StreamGroup *sgroup = NULL;
1018 GstPad *sinkpad, *srcpad, *muxerpad = NULL;
1019 /* Element we will link to the encoder */
1020 GstElement *last = NULL;
1021 GList *tmp, *tosync = NULL;
1022 GstCaps *format, *restriction;
1023 const gchar *missing_element_name;
1025 format = gst_encoding_profile_get_format (sprof);
1026 restriction = gst_encoding_profile_get_restriction (sprof);
1028 GST_DEBUG ("Creating group. format %" GST_PTR_FORMAT ", for caps %"
1029 GST_PTR_FORMAT, format, sinkcaps);
1030 GST_DEBUG ("avoid_reencoding:%d", ebin->avoid_reencoding);
1032 sgroup = g_slice_new0 (StreamGroup);
1033 sgroup->ebin = ebin;
1034 sgroup->profile = sprof;
1036 /* NOTE for people reading this code:
1038 * We construct the group starting by the furthest downstream element
1039 * and making our way up adding/syncing/linking as we go.
1041 * There are two parallel paths:
1042 * * One for raw data which goes through converters and encoders
1043 * * One for already encoded data
1046 /* Exception to the rule above:
1047 * We check if we have an available encoder so we can abort early */
1048 /* FIXME : What if we only want to do passthrough ??? */
1049 GST_LOG ("Checking for encoder availability");
1050 sgroup->encoder = _get_encoder (ebin, sprof);
1051 if (G_UNLIKELY (sgroup->encoder == NULL))
1055 * If we are handling a container profile, figure out if the muxer has a
1056 * sinkpad compatible with the selected profile */
1058 muxerpad = get_compatible_muxer_sink_pad (ebin, NULL, format);
1059 if (G_UNLIKELY (muxerpad == NULL))
1065 * We only use a 1buffer long queue here, the actual queueing will be done
1066 * in the input queue */
1067 last = sgroup->outqueue = gst_element_factory_make ("queue", NULL);
1068 g_object_set (sgroup->outqueue, "max-size-buffers", (guint32) 1,
1069 "max-size-bytes", (guint32) 0, "max-size-time", (guint64) 0,
1070 "silent", TRUE, NULL);
1072 gst_bin_add (GST_BIN (ebin), sgroup->outqueue);
1073 tosync = g_list_append (tosync, sgroup->outqueue);
1074 srcpad = gst_element_get_static_pad (sgroup->outqueue, "src");
1076 if (G_UNLIKELY (fast_pad_link (srcpad, muxerpad) != GST_PAD_LINK_OK)) {
1077 goto muxer_link_failure;
1079 gst_object_unref (muxerpad);
1081 gst_ghost_pad_set_target (GST_GHOST_PAD (ebin->srcpad), srcpad);
1083 gst_object_unref (srcpad);
1085 /* Check if we need a formatter
1086 * If we have no muxer or
1087 * if the muxer isn't a formatter and doesn't implement the tagsetter interface
1089 if (!ebin->muxer || (!GST_IS_TAG_SETTER (ebin->muxer)
1090 && !_has_class (ebin->muxer, "Formatter"))) {
1091 sgroup->formatter = _get_formatter (ebin, sprof);
1092 if (sgroup->formatter) {
1093 GST_DEBUG ("Adding formatter for %" GST_PTR_FORMAT, format);
1095 gst_bin_add (GST_BIN (ebin), sgroup->formatter);
1096 tosync = g_list_append (tosync, sgroup->formatter);
1097 if (G_UNLIKELY (!fast_element_link (sgroup->formatter, last)))
1098 goto formatter_link_failure;
1099 last = sgroup->formatter;
1104 /* Output capsfilter
1105 * This will receive the format caps from the streamprofile */
1106 GST_DEBUG ("Adding output capsfilter for %" GST_PTR_FORMAT, format);
1107 sgroup->outfilter = gst_element_factory_make ("capsfilter", NULL);
1108 g_object_set (sgroup->outfilter, "caps", format, NULL);
1110 gst_bin_add (GST_BIN (ebin), sgroup->outfilter);
1111 tosync = g_list_append (tosync, sgroup->outfilter);
1112 if (G_UNLIKELY (!fast_element_link (sgroup->outfilter, last)))
1113 goto outfilter_link_failure;
1114 last = sgroup->outfilter;
1117 sgroup->parser = _get_parser (ebin, sprof);
1119 if (sgroup->parser != NULL) {
1120 GST_DEBUG ("Got a parser %s", GST_ELEMENT_NAME (sgroup->parser));
1121 gst_bin_add (GST_BIN (ebin), sgroup->parser);
1122 tosync = g_list_append (tosync, sgroup->parser);
1123 if (G_UNLIKELY (!gst_element_link (sgroup->parser, last)))
1124 goto parser_link_failure;
1125 last = sgroup->parser;
1128 /* Stream combiner */
1129 sgroup->combiner = g_object_new (GST_TYPE_STREAM_COMBINER, NULL);
1131 gst_bin_add (GST_BIN (ebin), sgroup->combiner);
1132 tosync = g_list_append (tosync, sgroup->combiner);
1133 if (G_UNLIKELY (!fast_element_link (sgroup->combiner, last)))
1134 goto combiner_link_failure;
1137 /* Stream splitter */
1138 sgroup->splitter = g_object_new (GST_TYPE_STREAM_SPLITTER, NULL);
1140 gst_bin_add (GST_BIN (ebin), sgroup->splitter);
1141 tosync = g_list_append (tosync, sgroup->splitter);
1144 * FIXME : figure out what max-size to use for the input queue */
1145 sgroup->inqueue = gst_element_factory_make ("queue", NULL);
1146 g_object_set (sgroup->inqueue, "max-size-buffers",
1147 (guint32) ebin->queue_buffers_max, "max-size-bytes",
1148 (guint32) ebin->queue_bytes_max, "max-size-time",
1149 (guint64) ebin->queue_time_max, "silent", TRUE, NULL);
1151 gst_bin_add (GST_BIN (ebin), sgroup->inqueue);
1152 tosync = g_list_append (tosync, sgroup->inqueue);
1153 if (G_UNLIKELY (!fast_element_link (sgroup->inqueue, sgroup->splitter)))
1154 goto splitter_link_failure;
1156 /* Expose input queue sink pad as ghostpad */
1157 sinkpad = gst_element_get_static_pad (sgroup->inqueue, "sink");
1158 if (sinkpadname == NULL) {
1160 g_strdup_printf ("%s_%u", gst_encoding_profile_get_type_nick (sprof),
1161 ebin->last_pad_id++);
1162 GST_DEBUG ("Adding ghost pad %s", pname);
1163 sgroup->ghostpad = gst_ghost_pad_new (pname, sinkpad);
1166 sgroup->ghostpad = gst_ghost_pad_new (sinkpadname, sinkpad);
1167 gst_object_unref (sinkpad);
1170 /* Path 1 : Already-encoded data */
1172 local_element_request_pad (sgroup->combiner, NULL, "passthroughsink",
1174 if (G_UNLIKELY (sinkpad == NULL))
1175 goto no_combiner_sinkpad;
1177 if (ebin->avoid_reencoding) {
1180 GST_DEBUG ("Asked to use Smart Encoder");
1181 sgroup->smartencoder = g_object_new (GST_TYPE_SMART_ENCODER, NULL);
1183 /* Check if stream format is compatible */
1184 srcpad = gst_element_get_static_pad (sgroup->smartencoder, "src");
1185 tmpcaps = gst_pad_query_caps (srcpad, NULL);
1186 if (!gst_caps_can_intersect (tmpcaps, format)) {
1187 GST_DEBUG ("We don't have a smart encoder for the stream format");
1188 gst_object_unref (sgroup->smartencoder);
1189 sgroup->smartencoder = NULL;
1191 gst_bin_add ((GstBin *) ebin, sgroup->smartencoder);
1192 fast_pad_link (srcpad, sinkpad);
1193 tosync = g_list_append (tosync, sgroup->smartencoder);
1194 sinkpad = gst_element_get_static_pad (sgroup->smartencoder, "sink");
1196 gst_caps_unref (tmpcaps);
1197 g_object_unref (srcpad);
1201 local_element_request_pad (sgroup->splitter, NULL, "passthroughsrc",
1203 if (G_UNLIKELY (srcpad == NULL))
1204 goto no_splitter_srcpad;
1206 /* Go straight to splitter */
1207 if (G_UNLIKELY (fast_pad_link (srcpad, sinkpad) != GST_PAD_LINK_OK))
1208 goto passthrough_link_failure;
1209 g_object_unref (sinkpad);
1210 g_object_unref (srcpad);
1213 /* Path 2 : Conversion / Encoding */
1215 /* 1. Create the encoder */
1216 GST_LOG ("Adding encoder");
1217 last = sgroup->encoder;
1218 gst_bin_add ((GstBin *) ebin, sgroup->encoder);
1219 tosync = g_list_append (tosync, sgroup->encoder);
1222 local_element_request_pad (sgroup->combiner, NULL, "encodingsink", NULL);
1223 if (G_UNLIKELY (sinkpad == NULL))
1224 goto no_combiner_sinkpad;
1225 srcpad = gst_element_get_static_pad (sgroup->encoder, "src");
1226 if (G_UNLIKELY (fast_pad_link (srcpad, sinkpad) != GST_PAD_LINK_OK))
1227 goto encoder_link_failure;
1228 g_object_unref (sinkpad);
1229 g_object_unref (srcpad);
1232 /* 3. Create the conversion/restriction elements */
1233 /* 3.1. capsfilter */
1234 if (restriction && !gst_caps_is_any (restriction)) {
1235 GST_LOG ("Adding capsfilter for restriction caps : %" GST_PTR_FORMAT,
1238 last = sgroup->capsfilter = gst_element_factory_make ("capsfilter", NULL);
1239 g_object_set (sgroup->capsfilter, "caps", restriction, NULL);
1240 gst_bin_add ((GstBin *) ebin, sgroup->capsfilter);
1241 tosync = g_list_append (tosync, sgroup->capsfilter);
1242 fast_element_link (sgroup->capsfilter, sgroup->encoder);
1245 /* 3.2. restriction elements */
1246 /* FIXME : Once we have properties for specific converters, use those */
1247 if (GST_IS_ENCODING_VIDEO_PROFILE (sprof)) {
1248 const gboolean native_video =
1249 ! !(ebin->flags & GST_ENCODEBIN_FLAG_NO_VIDEO_CONVERSION);
1250 GstElement *cspace = NULL, *scale, *vrate, *cspace2 = NULL;
1252 GST_LOG ("Adding conversion elements for video stream");
1254 if (!native_video) {
1255 cspace = gst_element_factory_make ("videoconvert", NULL);
1256 scale = gst_element_factory_make ("videoscale", NULL);
1258 missing_element_name = "videoscale";
1259 goto missing_element;
1261 /* 4-tap scaling and black borders */
1262 g_object_set (scale, "method", 2, "add-borders", TRUE, NULL);
1263 cspace2 = gst_element_factory_make ("videoconvert", NULL);
1265 if (!cspace || !cspace2) {
1266 missing_element_name = "videoconvert";
1267 goto missing_element;
1270 gst_bin_add_many ((GstBin *) ebin, cspace, scale, cspace2, NULL);
1271 tosync = g_list_append (tosync, cspace);
1272 tosync = g_list_append (tosync, scale);
1273 tosync = g_list_append (tosync, cspace2);
1275 sgroup->converters = g_list_prepend (sgroup->converters, cspace);
1276 sgroup->converters = g_list_prepend (sgroup->converters, scale);
1277 sgroup->converters = g_list_prepend (sgroup->converters, cspace2);
1279 if (!fast_element_link (cspace, scale) ||
1280 !fast_element_link (scale, cspace2))
1281 goto converter_link_failure;
1284 if (!gst_encoding_video_profile_get_variableframerate
1285 (GST_ENCODING_VIDEO_PROFILE (sprof))) {
1286 vrate = gst_element_factory_make ("videorate", NULL);
1288 missing_element_name = "videorate";
1289 goto missing_element;
1292 gst_bin_add ((GstBin *) ebin, vrate);
1293 tosync = g_list_prepend (tosync, vrate);
1294 sgroup->converters = g_list_prepend (sgroup->converters, vrate);
1296 if ((!native_video && !fast_element_link (cspace2, vrate))
1297 || !fast_element_link (vrate, last))
1298 goto converter_link_failure;
1304 } else if (!native_video) {
1305 if (!fast_element_link (cspace2, last))
1306 goto converter_link_failure;
1310 } else if (GST_IS_ENCODING_AUDIO_PROFILE (sprof)
1311 && !(ebin->flags & GST_ENCODEBIN_FLAG_NO_AUDIO_CONVERSION)) {
1312 GstElement *aconv, *ares, *arate, *aconv2;
1314 GST_LOG ("Adding conversion elements for audio stream");
1316 arate = gst_element_factory_make ("audiorate", NULL);
1317 g_object_set (arate, "tolerance", (guint64) ebin->tolerance, NULL);
1319 missing_element_name = "audiorate";
1320 goto missing_element;
1322 aconv = gst_element_factory_make ("audioconvert", NULL);
1323 aconv2 = gst_element_factory_make ("audioconvert", NULL);
1324 ares = gst_element_factory_make ("audioresample", NULL);
1325 if (!aconv || !aconv2) {
1326 missing_element_name = "audioconvert";
1327 goto missing_element;
1330 missing_element_name = "audioresample";
1331 goto missing_element;
1334 gst_bin_add_many ((GstBin *) ebin, arate, aconv, ares, aconv2, NULL);
1335 tosync = g_list_append (tosync, arate);
1336 tosync = g_list_append (tosync, aconv);
1337 tosync = g_list_append (tosync, ares);
1338 tosync = g_list_append (tosync, aconv2);
1339 if (!fast_element_link (arate, aconv) ||
1340 !fast_element_link (aconv, ares) ||
1341 !fast_element_link (ares, aconv2) || !fast_element_link (aconv2, last))
1342 goto converter_link_failure;
1344 sgroup->converters = g_list_prepend (sgroup->converters, arate);
1345 sgroup->converters = g_list_prepend (sgroup->converters, aconv);
1346 sgroup->converters = g_list_prepend (sgroup->converters, ares);
1347 sgroup->converters = g_list_prepend (sgroup->converters, aconv2);
1352 /* Link to stream splitter */
1353 sinkpad = gst_element_get_static_pad (last, "sink");
1355 local_element_request_pad (sgroup->splitter, NULL, "encodingsrc", NULL);
1356 if (G_UNLIKELY (srcpad == NULL))
1357 goto no_splitter_srcpad;
1358 if (G_UNLIKELY (fast_pad_link (srcpad, sinkpad) != GST_PAD_LINK_OK))
1359 goto splitter_encoding_failure;
1360 g_object_unref (sinkpad);
1361 g_object_unref (srcpad);
1363 /* End of Stream 2 setup */
1365 /* Sync all elements to parent state */
1366 for (tmp = tosync; tmp; tmp = tmp->next)
1367 gst_element_sync_state_with_parent ((GstElement *) tmp->data);
1368 g_list_free (tosync);
1371 GST_DEBUG ("Adding ghostpad %s:%s", GST_DEBUG_PAD_NAME (sgroup->ghostpad));
1372 gst_pad_set_active (sgroup->ghostpad, TRUE);
1373 gst_element_add_pad ((GstElement *) ebin, sgroup->ghostpad);
1375 /* Add StreamGroup to our list of streams */
1378 ("Done creating elements, adding StreamGroup to our controlled stream list");
1380 ebin->streams = g_list_prepend (ebin->streams, sgroup);
1383 gst_caps_unref (format);
1385 gst_caps_unref (restriction);
1389 splitter_encoding_failure:
1390 GST_ERROR_OBJECT (ebin, "Error linking splitter to encoding stream");
1394 GST_ERROR_OBJECT (ebin, "Couldn't create encoder for format %" GST_PTR_FORMAT,
1396 /* missing plugin support */
1397 gst_element_post_message (GST_ELEMENT_CAST (ebin),
1398 gst_missing_encoder_message_new (GST_ELEMENT_CAST (ebin), format));
1399 GST_ELEMENT_ERROR (ebin, CORE, MISSING_PLUGIN, (NULL),
1400 ("Couldn't create encoder for format %" GST_PTR_FORMAT, format));
1404 GST_ERROR_OBJECT (ebin,
1405 "Couldn't find a compatible muxer pad to link encoder to");
1409 gst_element_post_message (GST_ELEMENT_CAST (ebin),
1410 gst_missing_element_message_new (GST_ELEMENT_CAST (ebin),
1411 missing_element_name));
1412 GST_ELEMENT_ERROR (ebin, CORE, MISSING_PLUGIN,
1413 (_("Missing element '%s' - check your GStreamer installation."),
1414 missing_element_name), (NULL));
1417 encoder_link_failure:
1418 GST_ERROR_OBJECT (ebin, "Failed to link the encoder");
1422 GST_ERROR_OBJECT (ebin, "Couldn't link encoder to muxer");
1425 formatter_link_failure:
1426 GST_ERROR_OBJECT (ebin, "Couldn't link output filter to output queue");
1429 outfilter_link_failure:
1430 GST_ERROR_OBJECT (ebin,
1431 "Couldn't link output filter to output queue/formatter");
1434 passthrough_link_failure:
1435 GST_ERROR_OBJECT (ebin, "Failed linking splitter in passthrough mode");
1439 GST_ERROR_OBJECT (ebin, "Couldn't get a source pad from the splitter");
1442 no_combiner_sinkpad:
1443 GST_ERROR_OBJECT (ebin, "Couldn't get a sink pad from the combiner");
1446 splitter_link_failure:
1447 GST_ERROR_OBJECT (ebin, "Failure linking to the splitter");
1450 combiner_link_failure:
1451 GST_ERROR_OBJECT (ebin, "Failure linking to the combiner");
1454 parser_link_failure:
1455 GST_ERROR_OBJECT (ebin, "Failure linking the parser");
1458 converter_link_failure:
1459 GST_ERROR_OBJECT (ebin, "Failure linking the video converters");
1463 /* FIXME : Actually properly cleanup everything */
1465 gst_caps_unref (format);
1467 gst_caps_unref (restriction);
1468 g_slice_free (StreamGroup, sgroup);
1473 _gst_caps_match_foreach (GQuark field_id, const GValue * value, gpointer data)
1475 GstStructure *structure = data;
1476 const GValue *other_value = gst_structure_id_get_value (structure, field_id);
1478 if (G_UNLIKELY (other_value == NULL))
1480 if (gst_value_compare (value, other_value) == GST_VALUE_EQUAL) {
1488 * checks that there is at least one structure on caps_a that has
1489 * all its fields exactly the same as one structure on caps_b
1492 _gst_caps_match (const GstCaps * caps_a, const GstCaps * caps_b)
1495 gboolean res = FALSE;
1497 for (i = 0; i < gst_caps_get_size (caps_a); i++) {
1498 GstStructure *structure_a = gst_caps_get_structure (caps_a, i);
1499 for (j = 0; j < gst_caps_get_size (caps_b); j++) {
1500 GstStructure *structure_b = gst_caps_get_structure (caps_b, j);
1502 res = gst_structure_foreach (structure_a, _gst_caps_match_foreach,
1513 _factory_can_handle_caps (GstElementFactory * factory, const GstCaps * caps,
1514 GstPadDirection dir, gboolean exact)
1516 const GList *templates;
1518 templates = gst_element_factory_get_static_pad_templates (factory);
1520 GstStaticPadTemplate *template = (GstStaticPadTemplate *) templates->data;
1522 if (template->direction == dir) {
1523 GstCaps *tmp = gst_static_caps_get (&template->static_caps);
1525 if ((exact && _gst_caps_match (caps, tmp)) ||
1526 (!exact && gst_caps_can_intersect (tmp, caps))) {
1527 gst_caps_unref (tmp);
1530 gst_caps_unref (tmp);
1532 templates = g_list_next (templates);
1538 static inline GstElement *
1539 _get_formatter (GstEncodeBin * ebin, GstEncodingProfile * sprof)
1541 GList *formatters, *tmpfmtr;
1542 GstElement *formatter = NULL;
1543 GstElementFactory *formatterfact = NULL;
1545 const gchar *preset, *preset_name;
1547 format = gst_encoding_profile_get_format (sprof);
1548 preset = gst_encoding_profile_get_preset (sprof);
1549 preset_name = gst_encoding_profile_get_preset_name (sprof);
1551 GST_DEBUG ("Getting list of formatters for format %" GST_PTR_FORMAT, format);
1554 gst_element_factory_list_filter (ebin->formatters, format, GST_PAD_SRC,
1557 if (formatters == NULL)
1560 /* FIXME : signal the user if he wants this */
1561 for (tmpfmtr = formatters; tmpfmtr; tmpfmtr = tmpfmtr->next) {
1562 formatterfact = (GstElementFactory *) tmpfmtr->data;
1564 GST_DEBUG_OBJECT (ebin, "Trying formatter %s",
1565 GST_OBJECT_NAME (formatterfact));
1568 _create_element_and_set_preset (formatterfact, preset,
1569 NULL, preset_name)))
1573 gst_plugin_feature_list_free (formatters);
1577 gst_caps_unref (format);
1582 compare_elements (gconstpointer a, gconstpointer b, gpointer udata)
1584 GstCaps *caps = udata;
1585 GstElementFactory *fac_a = (GstElementFactory *) a;
1586 GstElementFactory *fac_b = (GstElementFactory *) b;
1588 /* FIXME not quite sure this is the best algorithm to order the elements
1589 * Some caps similarity comparison algorithm would fit better than going
1590 * boolean (equals/not equals).
1592 gboolean equals_a = _factory_can_handle_caps (fac_a, caps, GST_PAD_SRC, TRUE);
1593 gboolean equals_b = _factory_can_handle_caps (fac_b, caps, GST_PAD_SRC, TRUE);
1595 if (equals_a == equals_b) {
1596 return gst_plugin_feature_get_rank ((GstPluginFeature *) fac_b) -
1597 gst_plugin_feature_get_rank ((GstPluginFeature *) fac_a);
1598 } else if (equals_a) {
1600 } else if (equals_b) {
1606 static inline GstElement *
1607 _get_muxer (GstEncodeBin * ebin)
1609 GList *muxers, *formatters, *tmpmux;
1610 GstElement *muxer = NULL;
1611 GstElementFactory *muxerfact = NULL;
1614 const gchar *preset, *preset_name;
1616 format = gst_encoding_profile_get_format (ebin->profile);
1617 preset = gst_encoding_profile_get_preset (ebin->profile);
1618 preset_name = gst_encoding_profile_get_preset_name (ebin->profile);
1620 GST_DEBUG ("Getting list of muxers for format %" GST_PTR_FORMAT, format);
1623 gst_element_factory_list_filter (ebin->muxers, format, GST_PAD_SRC, TRUE);
1626 gst_element_factory_list_filter (ebin->formatters, format, GST_PAD_SRC,
1629 muxers = g_list_sort_with_data (muxers, compare_elements, (gpointer) format);
1631 g_list_sort_with_data (formatters, compare_elements, (gpointer) format);
1633 muxers = g_list_concat (muxers, formatters);
1638 /* FIXME : signal the user if he wants this */
1639 for (tmpmux = muxers; tmpmux; tmpmux = tmpmux->next) {
1640 gboolean cansinkstreams = TRUE;
1641 const GList *profiles =
1642 gst_encoding_container_profile_get_profiles
1643 (GST_ENCODING_CONTAINER_PROFILE (ebin->profile));
1645 muxerfact = (GstElementFactory *) tmpmux->data;
1647 GST_DEBUG ("Trying muxer %s", GST_OBJECT_NAME (muxerfact));
1649 /* See if the muxer can sink all of our stream profile caps */
1650 for (tmp = profiles; tmp; tmp = tmp->next) {
1651 GstEncodingProfile *sprof = (GstEncodingProfile *) tmp->data;
1652 GstCaps *sformat = gst_encoding_profile_get_format (sprof);
1654 if (!_factory_can_handle_caps (muxerfact, sformat, GST_PAD_SINK, FALSE)) {
1655 GST_DEBUG ("Skipping muxer because it can't sink caps %"
1656 GST_PTR_FORMAT, sformat);
1657 cansinkstreams = FALSE;
1659 gst_caps_unref (sformat);
1663 gst_caps_unref (sformat);
1666 /* Only use a muxer than can use all streams and than can accept the
1667 * preset (which may be present or not) */
1668 if (cansinkstreams && (muxer =
1669 _create_element_and_set_preset (muxerfact, preset, "muxer",
1674 gst_plugin_feature_list_free (muxers);
1678 gst_caps_unref (format);
1683 create_elements_and_pads (GstEncodeBin * ebin)
1685 gboolean ret = TRUE;
1686 GstElement *muxer = NULL;
1688 const GList *tmp, *profiles;
1689 GstEncodingProfile *sprof;
1691 GST_DEBUG ("Current profile : %s",
1692 gst_encoding_profile_get_name (ebin->profile));
1694 if (GST_IS_ENCODING_CONTAINER_PROFILE (ebin->profile)) {
1695 /* 1. Get the compatible muxer */
1696 muxer = _get_muxer (ebin);
1697 if (G_UNLIKELY (muxer == NULL))
1700 /* Record the muxer */
1701 ebin->muxer = muxer;
1702 gst_bin_add ((GstBin *) ebin, muxer);
1704 /* 2. Ghost the muxer source pad */
1706 /* FIXME : We should figure out if it's a static/request/dyamic pad,
1707 * but for the time being let's assume it's a static pad :) */
1708 muxerpad = gst_element_get_static_pad (muxer, "src");
1709 if (G_UNLIKELY (muxerpad == NULL))
1712 if (!gst_ghost_pad_set_target (GST_GHOST_PAD (ebin->srcpad), muxerpad))
1713 goto no_muxer_ghost_pad;
1715 gst_object_unref (muxerpad);
1716 /* 3. Activate fixed presence streams */
1718 gst_encoding_container_profile_get_profiles
1719 (GST_ENCODING_CONTAINER_PROFILE (ebin->profile));
1720 for (tmp = profiles; tmp; tmp = tmp->next) {
1721 sprof = (GstEncodingProfile *) tmp->data;
1723 GST_DEBUG ("Trying stream profile with presence %d",
1724 gst_encoding_profile_get_presence (sprof));
1726 if (gst_encoding_profile_get_presence (sprof) != 0) {
1727 if (G_UNLIKELY (_create_stream_group (ebin, sprof, NULL, NULL) == NULL))
1731 gst_element_sync_state_with_parent (muxer);
1733 if (G_UNLIKELY (_create_stream_group (ebin, ebin->profile, NULL,
1742 GstCaps *format = gst_encoding_profile_get_format (ebin->profile);
1744 GST_WARNING ("No available muxer for %" GST_PTR_FORMAT, format);
1745 /* missing plugin support */
1746 gst_element_post_message (GST_ELEMENT_CAST (ebin),
1747 gst_missing_encoder_message_new (GST_ELEMENT_CAST (ebin), format));
1748 GST_ELEMENT_ERROR (ebin, CORE, MISSING_PLUGIN, (NULL),
1749 ("No available muxer for format %" GST_PTR_FORMAT, format));
1751 gst_caps_unref (format);
1757 GST_WARNING ("Can't get source pad from muxer (%s)",
1758 GST_ELEMENT_NAME (muxer));
1759 gst_bin_remove (GST_BIN (ebin), muxer);
1765 GST_WARNING ("Couldn't set %s:%s as source ghostpad target",
1766 GST_DEBUG_PAD_NAME (muxerpad));
1767 gst_bin_remove (GST_BIN (ebin), muxer);
1768 gst_object_unref (muxerpad);
1774 GST_WARNING ("Could not create Streams");
1776 gst_bin_remove (GST_BIN (ebin), muxer);
1783 release_pads (const GValue * item, GstElement * elt)
1785 GstPad *pad = g_value_get_object (item);
1786 GstPad *peer = NULL;
1788 GST_DEBUG_OBJECT (elt, "Releasing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1790 /* Unlink from its peer pad */
1791 if ((peer = gst_pad_get_peer (pad))) {
1792 if (GST_PAD_DIRECTION (peer) == GST_PAD_SRC)
1793 gst_pad_unlink (peer, pad);
1795 gst_pad_unlink (pad, peer);
1796 gst_object_unref (peer);
1799 /* Release it from the object */
1800 gst_element_release_request_pad (elt, pad);
1804 stream_group_free (GstEncodeBin * ebin, StreamGroup * sgroup)
1810 GST_DEBUG_OBJECT (ebin, "Freeing StreamGroup %p", sgroup);
1813 /* outqueue - Muxer */
1814 tmppad = gst_element_get_static_pad (sgroup->outqueue, "src");
1815 pad = gst_pad_get_peer (tmppad);
1817 /* Remove muxer request sink pad */
1818 gst_pad_unlink (tmppad, pad);
1819 if (GST_PAD_TEMPLATE_PRESENCE (GST_PAD_PAD_TEMPLATE (pad)) ==
1821 gst_element_release_request_pad (ebin->muxer, pad);
1822 gst_object_unref (tmppad);
1823 gst_object_unref (pad);
1825 if (sgroup->outqueue)
1826 gst_element_set_state (sgroup->outqueue, GST_STATE_NULL);
1828 if (sgroup->formatter) {
1829 /* capsfilter - formatter - outqueue */
1830 gst_element_set_state (sgroup->formatter, GST_STATE_NULL);
1831 gst_element_set_state (sgroup->outfilter, GST_STATE_NULL);
1832 gst_element_unlink (sgroup->formatter, sgroup->outqueue);
1833 gst_element_unlink (sgroup->outfilter, sgroup->formatter);
1835 /* Capsfilter - outqueue */
1836 gst_element_set_state (sgroup->outfilter, GST_STATE_NULL);
1837 gst_element_unlink (sgroup->outfilter, sgroup->outqueue);
1839 gst_element_set_state (sgroup->outqueue, GST_STATE_NULL);
1840 gst_bin_remove (GST_BIN (ebin), sgroup->outqueue);
1842 /* streamcombiner - parser - capsfilter */
1843 if (sgroup->parser) {
1844 gst_element_set_state (sgroup->parser, GST_STATE_NULL);
1845 gst_element_unlink (sgroup->parser, sgroup->outfilter);
1846 gst_element_unlink (sgroup->combiner, sgroup->parser);
1847 gst_bin_remove ((GstBin *) ebin, sgroup->parser);
1851 if (sgroup->ghostpad)
1852 gst_element_remove_pad (GST_ELEMENT_CAST (ebin), sgroup->ghostpad);
1854 if (sgroup->inqueue)
1855 gst_element_set_state (sgroup->inqueue, GST_STATE_NULL);
1857 if (sgroup->encoder)
1858 gst_element_set_state (sgroup->encoder, GST_STATE_NULL);
1859 if (sgroup->outfilter)
1860 gst_element_set_state (sgroup->outfilter, GST_STATE_NULL);
1861 if (sgroup->smartencoder)
1862 gst_element_set_state (sgroup->smartencoder, GST_STATE_NULL);
1864 if (sgroup->capsfilter) {
1865 gst_element_set_state (sgroup->capsfilter, GST_STATE_NULL);
1866 gst_element_unlink (sgroup->capsfilter, sgroup->encoder);
1867 gst_bin_remove ((GstBin *) ebin, sgroup->capsfilter);
1870 for (tmp = sgroup->converters; tmp; tmp = tmp->next) {
1871 GstElement *elt = (GstElement *) tmp->data;
1873 gst_element_set_state (elt, GST_STATE_NULL);
1874 gst_bin_remove ((GstBin *) ebin, elt);
1876 if (sgroup->converters)
1877 g_list_free (sgroup->converters);
1879 if (sgroup->combiner) {
1880 GstIterator *it = gst_element_iterate_sink_pads (sgroup->combiner);
1881 GstIteratorResult itret = GST_ITERATOR_OK;
1883 while (itret == GST_ITERATOR_OK || itret == GST_ITERATOR_RESYNC) {
1885 gst_iterator_foreach (it, (GstIteratorForeachFunction) release_pads,
1887 gst_iterator_resync (it);
1889 gst_iterator_free (it);
1890 gst_element_set_state (sgroup->combiner, GST_STATE_NULL);
1891 gst_bin_remove ((GstBin *) ebin, sgroup->combiner);
1894 if (sgroup->splitter) {
1895 GstIterator *it = gst_element_iterate_src_pads (sgroup->splitter);
1896 GstIteratorResult itret = GST_ITERATOR_OK;
1897 while (itret == GST_ITERATOR_OK || itret == GST_ITERATOR_RESYNC) {
1899 gst_iterator_foreach (it, (GstIteratorForeachFunction) release_pads,
1901 gst_iterator_resync (it);
1903 gst_iterator_free (it);
1905 gst_element_set_state (sgroup->splitter, GST_STATE_NULL);
1906 gst_bin_remove ((GstBin *) ebin, sgroup->splitter);
1909 if (sgroup->inqueue)
1910 gst_bin_remove ((GstBin *) ebin, sgroup->inqueue);
1912 if (sgroup->encoder)
1913 gst_bin_remove ((GstBin *) ebin, sgroup->encoder);
1915 if (sgroup->smartencoder)
1916 gst_bin_remove ((GstBin *) ebin, sgroup->smartencoder);
1918 if (sgroup->outfilter)
1919 gst_bin_remove ((GstBin *) ebin, sgroup->outfilter);
1921 g_slice_free (StreamGroup, sgroup);
1925 stream_group_remove (GstEncodeBin * ebin, StreamGroup * sgroup)
1927 ebin->streams = g_list_remove (ebin->streams, sgroup);
1929 stream_group_free (ebin, sgroup);
1933 gst_encode_bin_tear_down_profile (GstEncodeBin * ebin)
1935 if (G_UNLIKELY (ebin->profile == NULL))
1938 GST_DEBUG ("Tearing down profile %s",
1939 gst_encoding_profile_get_name (ebin->profile));
1941 while (ebin->streams)
1942 stream_group_remove (ebin, (StreamGroup *) ebin->streams->data);
1944 /* Set ghostpad target to NULL */
1945 gst_ghost_pad_set_target (GST_GHOST_PAD (ebin->srcpad), NULL);
1947 /* Remove muxer if present */
1949 gst_element_set_state (ebin->muxer, GST_STATE_NULL);
1950 gst_bin_remove (GST_BIN (ebin), ebin->muxer);
1954 /* free/clear profile */
1955 gst_encoding_profile_unref (ebin->profile);
1956 ebin->profile = NULL;
1960 gst_encode_bin_setup_profile (GstEncodeBin * ebin, GstEncodingProfile * profile)
1964 g_return_val_if_fail (ebin->profile == NULL, FALSE);
1966 GST_DEBUG ("Setting up profile %p:%s (type:%s)", profile,
1967 gst_encoding_profile_get_name (profile),
1968 gst_encoding_profile_get_type_nick (profile));
1970 ebin->profile = profile;
1971 gst_object_ref (ebin->profile);
1973 /* Create elements */
1974 res = create_elements_and_pads (ebin);
1976 gst_encode_bin_tear_down_profile (ebin);
1982 gst_encode_bin_set_profile (GstEncodeBin * ebin, GstEncodingProfile * profile)
1984 g_return_val_if_fail (GST_IS_ENCODING_PROFILE (profile), FALSE);
1986 GST_DEBUG_OBJECT (ebin, "profile (%p) : %s", profile,
1987 gst_encoding_profile_get_name (profile));
1989 if (G_UNLIKELY (ebin->active)) {
1990 GST_WARNING_OBJECT (ebin, "Element already active, can't change profile");
1994 /* If we're not active, we can deactivate the previous profile */
1995 if (ebin->profile) {
1996 gst_encode_bin_tear_down_profile (ebin);
1999 return gst_encode_bin_setup_profile (ebin, profile);
2002 static inline gboolean
2003 gst_encode_bin_activate (GstEncodeBin * ebin)
2005 ebin->active = ebin->profile != NULL;
2006 return ebin->active;
2010 gst_encode_bin_deactivate (GstEncodeBin * ebin)
2012 ebin->active = FALSE;
2015 static GstStateChangeReturn
2016 gst_encode_bin_change_state (GstElement * element, GstStateChange transition)
2018 GstStateChangeReturn ret;
2019 GstEncodeBin *ebin = (GstEncodeBin *) element;
2021 switch (transition) {
2022 case GST_STATE_CHANGE_READY_TO_PAUSED:
2023 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
2024 if (!gst_encode_bin_activate (ebin)) {
2025 ret = GST_STATE_CHANGE_FAILURE;
2034 GST_ELEMENT_CLASS (gst_encode_bin_parent_class)->change_state (element,
2036 if (ret == GST_STATE_CHANGE_FAILURE)
2039 switch (transition) {
2040 case GST_STATE_CHANGE_PAUSED_TO_READY:
2041 gst_encode_bin_deactivate (ebin);
2053 plugin_init (GstPlugin * plugin)
2057 GST_DEBUG_CATEGORY_INIT (gst_encode_bin_debug, "encodebin", 0, "encoder bin");
2060 GST_DEBUG ("binding text domain %s to locale dir %s", GETTEXT_PACKAGE,
2062 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
2063 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
2064 #endif /* ENABLE_NLS */
2067 res = gst_element_register (plugin, "encodebin", GST_RANK_NONE,
2068 GST_TYPE_ENCODE_BIN);
2073 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
2076 "various encoding-related elements", plugin_init, VERSION, GST_LICENSE,
2077 GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)