2 * Copyright (C) 2003 Benjamin Otte <in7y118@public.uni-hamburg.de>
4 * gsttypefindelement.c: element that detects type of stream
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.
22 * SECTION:element-typefind
24 * Determines the media-type of a stream. It applies typefind functions in the
25 * order of their rank. Once the type has been detected it sets its src pad caps
26 * to the found media type.
28 * Whenever a type is found the #GstTypeFindElement::have-type signal is
29 * emitted, either from the streaming thread or the application thread
30 * (the latter may happen when typefinding is done pull-based from the
31 * state change function).
33 * Plugins can register custom typefinders by using #GstTypeFindFactory.
36 /* FIXME: need a better solution for non-seekable streams */
39 * 1) get a list of all typefind functions sorted best to worst
40 * 2) if all elements have been called with all requested data goto 8
41 * 3) call all functions once with all available data
42 * 4) if a function returns a value >= PROP_MAXIMUM goto 8 (never implemented))
43 * 5) all functions with a result > PROP_MINIMUM or functions that did not get
44 * all requested data (where peek returned NULL) stay in list
45 * 6) seek to requested offset of best function that still has open data
48 * 8) take best available result and use its caps
50 * The element has two scheduling modes:
52 * 1) chain based, it will collect buffers and run the typefind function on
53 * the buffer until something is found.
54 * 2) getrange based, it will proxy the getrange function to the sinkpad. It
55 * is assumed that the peer element is happy with whatever format we
58 * By default it tries to do pull based typefinding (this avoids joining
59 * received buffers and holding them back in store.)
61 * When the element has no connected srcpad, and the sinkpad can operate in
62 * getrange based mode, the element starts its own task to figure out the
65 * Most of the actual implementation is in libs/gst/base/gsttypefindhelper.c.
72 #include "gst/gst_private.h"
74 #include "gsttypefindelement.h"
75 #include "gst/gst-i18n-lib.h"
76 #include "gst/base/gsttypefindhelper.h"
78 #include <gst/gsttypefind.h>
79 #include <gst/gstutils.h>
80 #include <gst/gsterror.h>
82 GST_DEBUG_CATEGORY_STATIC (gst_type_find_element_debug);
83 #define GST_CAT_DEFAULT gst_type_find_element_debug
85 /* generic templates */
86 static GstStaticPadTemplate type_find_element_sink_template =
87 GST_STATIC_PAD_TEMPLATE ("sink",
92 static GstStaticPadTemplate type_find_element_src_template =
93 GST_STATIC_PAD_TEMPLATE ("src",
98 /* Require at least 2kB of data before we attempt typefinding in chain-mode.
99 * 128kB is massive overkill for the maximum, but doesn't do any harm */
100 #ifdef TIZEN_PROFILE_TV
101 /* TV demuxer needs 4*1024 as min size to match demuxer, if not
102 * sometimes can't find correct demuxer
104 #define TYPE_FIND_MIN_SIZE (4*1024)
106 #define TYPE_FIND_MIN_SIZE (2*1024)
108 #define TYPE_FIND_MAX_SIZE (128*1024)
110 /* TypeFind signals and args */
126 MODE_NORMAL, /* act as identity */
127 MODE_TYPEFIND, /* do typefinding */
128 MODE_ERROR /* had fatal error */
133 GST_DEBUG_CATEGORY_INIT (gst_type_find_element_debug, "typefind", \
134 GST_DEBUG_BG_YELLOW | GST_DEBUG_FG_GREEN, "type finding element");
135 #define gst_type_find_element_parent_class parent_class
136 G_DEFINE_TYPE_WITH_CODE (GstTypeFindElement, gst_type_find_element,
137 GST_TYPE_ELEMENT, _do_init);
139 static void gst_type_find_element_dispose (GObject * object);
140 static void gst_type_find_element_set_property (GObject * object,
141 guint prop_id, const GValue * value, GParamSpec * pspec);
142 static void gst_type_find_element_get_property (GObject * object,
143 guint prop_id, GValue * value, GParamSpec * pspec);
145 static gboolean gst_type_find_element_src_event (GstPad * pad,
146 GstObject * parent, GstEvent * event);
147 static gboolean gst_type_find_handle_src_query (GstPad * pad,
148 GstObject * parent, GstQuery * query);
150 static gboolean gst_type_find_element_sink_event (GstPad * pad,
151 GstObject * parent, GstEvent * event);
152 static gboolean gst_type_find_element_setcaps (GstTypeFindElement * typefind,
154 static GstFlowReturn gst_type_find_element_chain (GstPad * sinkpad,
155 GstObject * parent, GstBuffer * buffer);
156 static GstFlowReturn gst_type_find_element_getrange (GstPad * srcpad,
157 GstObject * parent, guint64 offset, guint length, GstBuffer ** buffer);
159 static GstStateChangeReturn
160 gst_type_find_element_change_state (GstElement * element,
161 GstStateChange transition);
162 static gboolean gst_type_find_element_activate_sink (GstPad * pad,
164 static gboolean gst_type_find_element_activate_sink_mode (GstPad * pad,
165 GstObject * parent, GstPadMode mode, gboolean active);
166 static gboolean gst_type_find_element_activate_src_mode (GstPad * pad,
167 GstObject * parent, GstPadMode mode, gboolean active);
169 gst_type_find_element_chain_do_typefinding (GstTypeFindElement * typefind,
170 gboolean check_avail, gboolean at_eos);
171 static void gst_type_find_element_send_cached_events (GstTypeFindElement *
174 static void gst_type_find_element_loop (GstPad * pad);
176 static guint gst_type_find_element_signals[LAST_SIGNAL] = { 0 };
179 gst_type_find_element_have_type (GstTypeFindElement * typefind,
180 guint probability, GstCaps * caps)
182 g_assert (caps != NULL);
184 GST_INFO_OBJECT (typefind, "found caps %" GST_PTR_FORMAT ", probability=%u",
187 GST_OBJECT_LOCK (typefind);
189 gst_caps_unref (typefind->caps);
190 typefind->caps = gst_caps_ref (caps);
191 GST_OBJECT_UNLOCK (typefind);
193 gst_pad_set_caps (typefind->src, caps);
197 gst_type_find_element_class_init (GstTypeFindElementClass * typefind_class)
199 GObjectClass *gobject_class = G_OBJECT_CLASS (typefind_class);
200 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (typefind_class);
202 gobject_class->set_property = gst_type_find_element_set_property;
203 gobject_class->get_property = gst_type_find_element_get_property;
204 gobject_class->dispose = gst_type_find_element_dispose;
206 g_object_class_install_property (gobject_class, PROP_CAPS,
207 g_param_spec_boxed ("caps", _("caps"),
208 _("detected capabilities in stream"), GST_TYPE_CAPS,
209 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
210 g_object_class_install_property (gobject_class, PROP_MINIMUM,
211 g_param_spec_uint ("minimum", _("minimum"),
212 "minimum probability required to accept caps", GST_TYPE_FIND_MINIMUM,
213 GST_TYPE_FIND_MAXIMUM, GST_TYPE_FIND_MINIMUM,
214 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
215 g_object_class_install_property (gobject_class, PROP_FORCE_CAPS,
216 g_param_spec_boxed ("force-caps", _("force caps"),
217 _("force caps without doing a typefind"), GST_TYPE_CAPS,
218 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
220 * GstTypeFindElement::have-type:
221 * @typefind: the typefind instance
222 * @probability: the probability of the type found
223 * @caps: the caps of the type found
225 * This signal gets emitted when the type and its probability has
228 gst_type_find_element_signals[HAVE_TYPE] = g_signal_new ("have-type",
229 G_TYPE_FROM_CLASS (typefind_class), G_SIGNAL_RUN_LAST,
230 G_STRUCT_OFFSET (GstTypeFindElementClass, have_type), NULL, NULL,
231 g_cclosure_marshal_generic, G_TYPE_NONE, 2,
232 G_TYPE_UINT, GST_TYPE_CAPS | G_SIGNAL_TYPE_STATIC_SCOPE);
234 typefind_class->have_type =
235 GST_DEBUG_FUNCPTR (gst_type_find_element_have_type);
237 gst_element_class_set_static_metadata (gstelement_class,
240 "Finds the media type of a stream",
241 "Benjamin Otte <in7y118@public.uni-hamburg.de>");
242 gst_element_class_add_pad_template (gstelement_class,
243 gst_static_pad_template_get (&type_find_element_src_template));
244 gst_element_class_add_pad_template (gstelement_class,
245 gst_static_pad_template_get (&type_find_element_sink_template));
247 gstelement_class->change_state =
248 GST_DEBUG_FUNCPTR (gst_type_find_element_change_state);
252 gst_type_find_element_init (GstTypeFindElement * typefind)
256 gst_pad_new_from_static_template (&type_find_element_sink_template,
259 gst_pad_set_activate_function (typefind->sink,
260 GST_DEBUG_FUNCPTR (gst_type_find_element_activate_sink));
261 gst_pad_set_activatemode_function (typefind->sink,
262 GST_DEBUG_FUNCPTR (gst_type_find_element_activate_sink_mode));
263 gst_pad_set_chain_function (typefind->sink,
264 GST_DEBUG_FUNCPTR (gst_type_find_element_chain));
265 gst_pad_set_event_function (typefind->sink,
266 GST_DEBUG_FUNCPTR (gst_type_find_element_sink_event));
267 GST_PAD_SET_PROXY_ALLOCATION (typefind->sink);
268 gst_element_add_pad (GST_ELEMENT (typefind), typefind->sink);
272 gst_pad_new_from_static_template (&type_find_element_src_template, "src");
274 gst_pad_set_activatemode_function (typefind->src,
275 GST_DEBUG_FUNCPTR (gst_type_find_element_activate_src_mode));
276 gst_pad_set_getrange_function (typefind->src,
277 GST_DEBUG_FUNCPTR (gst_type_find_element_getrange));
278 gst_pad_set_event_function (typefind->src,
279 GST_DEBUG_FUNCPTR (gst_type_find_element_src_event));
280 gst_pad_set_query_function (typefind->src,
281 GST_DEBUG_FUNCPTR (gst_type_find_handle_src_query));
282 gst_pad_use_fixed_caps (typefind->src);
283 gst_element_add_pad (GST_ELEMENT (typefind), typefind->src);
285 typefind->mode = MODE_TYPEFIND;
286 typefind->caps = NULL;
287 typefind->min_probability = 1;
289 typefind->adapter = gst_adapter_new ();
293 gst_type_find_element_dispose (GObject * object)
295 GstTypeFindElement *typefind = GST_TYPE_FIND_ELEMENT (object);
297 if (typefind->adapter) {
298 g_object_unref (typefind->adapter);
299 typefind->adapter = NULL;
302 if (typefind->force_caps) {
303 gst_caps_unref (typefind->force_caps);
304 typefind->force_caps = NULL;
307 G_OBJECT_CLASS (parent_class)->dispose (object);
311 gst_type_find_element_set_property (GObject * object, guint prop_id,
312 const GValue * value, GParamSpec * pspec)
314 GstTypeFindElement *typefind;
316 typefind = GST_TYPE_FIND_ELEMENT (object);
320 typefind->min_probability = g_value_get_uint (value);
322 case PROP_FORCE_CAPS:
323 GST_OBJECT_LOCK (typefind);
324 if (typefind->force_caps)
325 gst_caps_unref (typefind->force_caps);
326 typefind->force_caps = g_value_dup_boxed (value);
327 GST_OBJECT_UNLOCK (typefind);
330 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
336 gst_type_find_element_get_property (GObject * object, guint prop_id,
337 GValue * value, GParamSpec * pspec)
339 GstTypeFindElement *typefind;
341 typefind = GST_TYPE_FIND_ELEMENT (object);
345 GST_OBJECT_LOCK (typefind);
346 g_value_set_boxed (value, typefind->caps);
347 GST_OBJECT_UNLOCK (typefind);
350 g_value_set_uint (value, typefind->min_probability);
352 case PROP_FORCE_CAPS:
353 GST_OBJECT_LOCK (typefind);
354 g_value_set_boxed (value, typefind->force_caps);
355 GST_OBJECT_UNLOCK (typefind);
358 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
364 gst_type_find_handle_src_query (GstPad * pad, GstObject * parent,
367 GstTypeFindElement *typefind;
368 gboolean res = FALSE;
370 typefind = GST_TYPE_FIND_ELEMENT (parent);
371 GST_DEBUG_OBJECT (typefind, "Handling src query %s",
372 GST_QUERY_TYPE_NAME (query));
374 switch (GST_QUERY_TYPE (query)) {
375 case GST_QUERY_SCHEDULING:
376 /* FIXME, filter out the scheduling modes that we understand */
377 res = gst_pad_peer_query (typefind->sink, query);
381 GST_DEBUG_OBJECT (typefind,
382 "Got caps query, our caps are %" GST_PTR_FORMAT, typefind->caps);
384 /* We can hijack caps query if we typefind already */
385 if (typefind->caps) {
386 gst_query_set_caps_result (query, typefind->caps);
389 res = gst_pad_peer_query (typefind->sink, query);
393 case GST_QUERY_POSITION:
398 if (!(res = gst_pad_peer_query (typefind->sink, query)))
401 gst_query_parse_position (query, &format, &peer_pos);
403 GST_OBJECT_LOCK (typefind);
404 /* FIXME: this code assumes that there's no discont in the queue */
406 case GST_FORMAT_BYTES:
407 peer_pos -= gst_adapter_available (typefind->adapter);
413 GST_OBJECT_UNLOCK (typefind);
414 gst_query_set_position (query, format, peer_pos);
418 res = gst_pad_query_default (pad, parent, query);
426 gst_type_find_element_seek (GstTypeFindElement * typefind, GstEvent * event)
429 GstSeekType start_type, stop_type;
434 GstSegment seeksegment = { 0, };
436 gst_event_parse_seek (event, &rate, &format, &flags, &start_type, &start,
439 /* we can only seek on bytes */
440 if (format != GST_FORMAT_BYTES) {
441 GST_DEBUG_OBJECT (typefind, "Can only seek on BYTES");
445 /* copy segment, we need this because we still need the old
446 * segment when we close the current segment. */
447 memcpy (&seeksegment, &typefind->segment, sizeof (GstSegment));
449 GST_DEBUG_OBJECT (typefind, "configuring seek");
450 gst_segment_do_seek (&seeksegment, rate, format, flags,
451 start_type, start, stop_type, stop, NULL);
453 flush = ! !(flags & GST_SEEK_FLAG_FLUSH);
455 GST_DEBUG_OBJECT (typefind, "New segment %" GST_SEGMENT_FORMAT, &seeksegment);
458 GST_DEBUG_OBJECT (typefind, "Starting flush");
459 gst_pad_push_event (typefind->sink, gst_event_new_flush_start ());
460 gst_pad_push_event (typefind->src, gst_event_new_flush_start ());
462 GST_DEBUG_OBJECT (typefind, "Non-flushing seek, pausing task");
463 gst_pad_pause_task (typefind->sink);
466 /* now grab the stream lock so that streaming cannot continue, for
467 * non flushing seeks when the element is in PAUSED this could block
469 GST_DEBUG_OBJECT (typefind, "Waiting for streaming to stop");
470 GST_PAD_STREAM_LOCK (typefind->sink);
473 GST_DEBUG_OBJECT (typefind, "Stopping flush");
474 gst_pad_push_event (typefind->sink, gst_event_new_flush_stop (TRUE));
475 gst_pad_push_event (typefind->src, gst_event_new_flush_stop (TRUE));
478 /* now update the real segment info */
479 GST_DEBUG_OBJECT (typefind, "Committing new seek segment");
480 memcpy (&typefind->segment, &seeksegment, sizeof (GstSegment));
481 typefind->offset = typefind->segment.start;
483 /* notify start of new segment */
484 if (typefind->segment.flags & GST_SEGMENT_FLAG_SEGMENT) {
487 msg = gst_message_new_segment_start (GST_OBJECT (typefind),
488 GST_FORMAT_BYTES, typefind->segment.start);
489 gst_element_post_message (GST_ELEMENT (typefind), msg);
492 typefind->need_segment = TRUE;
494 /* restart our task since it might have been stopped when we did the
496 gst_pad_start_task (typefind->sink,
497 (GstTaskFunction) gst_type_find_element_loop, typefind->sink, NULL);
499 /* streaming can continue now */
500 GST_PAD_STREAM_UNLOCK (typefind->sink);
506 gst_type_find_element_src_event (GstPad * pad, GstObject * parent,
509 GstTypeFindElement *typefind = GST_TYPE_FIND_ELEMENT (parent);
512 if (typefind->mode != MODE_NORMAL) {
513 /* need to do more? */
514 gst_event_unref (event);
518 /* Only handle seeks here if driving the pipeline */
519 if (typefind->segment.format != GST_FORMAT_UNDEFINED &&
520 GST_EVENT_TYPE (event) == GST_EVENT_SEEK) {
521 result = gst_type_find_element_seek (typefind, event);
522 gst_event_unref (event);
525 return gst_pad_push_event (typefind->sink, event);
530 start_typefinding (GstTypeFindElement * typefind)
532 GST_DEBUG_OBJECT (typefind, "starting typefinding");
534 GST_OBJECT_LOCK (typefind);
536 gst_caps_replace (&typefind->caps, NULL);
537 typefind->initial_offset = GST_BUFFER_OFFSET_NONE;
538 GST_OBJECT_UNLOCK (typefind);
540 typefind->mode = MODE_TYPEFIND;
544 stop_typefinding (GstTypeFindElement * typefind)
547 gboolean push_cached_buffers;
550 GstClockTime pts, dts;
552 gst_element_get_state (GST_ELEMENT (typefind), &state, NULL, 0);
554 push_cached_buffers = (state >= GST_STATE_PAUSED && typefind->caps);
556 GST_DEBUG_OBJECT (typefind, "stopping typefinding%s",
557 push_cached_buffers ? " and pushing cached events and buffers" : "");
559 typefind->mode = MODE_NORMAL;
560 if (push_cached_buffers)
561 gst_type_find_element_send_cached_events (typefind);
563 GST_OBJECT_LOCK (typefind);
564 avail = gst_adapter_available (typefind->adapter);
568 pts = gst_adapter_prev_pts (typefind->adapter, NULL);
569 dts = gst_adapter_prev_dts (typefind->adapter, NULL);
570 buffer = gst_adapter_take_buffer (typefind->adapter, avail);
571 GST_BUFFER_PTS (buffer) = pts;
572 GST_BUFFER_DTS (buffer) = dts;
573 GST_BUFFER_OFFSET (buffer) = typefind->initial_offset;
574 GST_OBJECT_UNLOCK (typefind);
576 if (!push_cached_buffers) {
577 gst_buffer_unref (buffer);
579 GstPad *peer = gst_pad_get_peer (typefind->src);
581 /* make sure the user gets a meaningful error message in this case,
582 * which is not a core bug or bug of any kind (as the default error
583 * message emitted by gstpad.c otherwise would make you think) */
584 if (peer && GST_PAD_CHAINFUNC (peer) == NULL) {
585 GST_DEBUG_OBJECT (typefind, "upstream only supports push mode, while "
586 "downstream element only works in pull mode, erroring out");
587 GST_ELEMENT_ERROR (typefind, STREAM, FAILED,
588 ("%s cannot work in push mode. The operation is not supported "
589 "with this source element or protocol.",
590 G_OBJECT_TYPE_NAME (GST_PAD_PARENT (peer))),
591 ("Downstream pad %s:%s has no chainfunction, and the upstream "
592 "element does not support pull mode", GST_DEBUG_PAD_NAME (peer)));
593 typefind->mode = MODE_ERROR; /* make the chain function error out */
594 gst_buffer_unref (buffer);
596 gst_pad_push (typefind->src, buffer);
599 gst_object_unref (peer);
606 GST_DEBUG_OBJECT (typefind, "we have no data to typefind");
607 GST_OBJECT_UNLOCK (typefind);
613 gst_type_find_element_sink_event (GstPad * pad, GstObject * parent,
616 gboolean res = FALSE;
617 GstTypeFindElement *typefind = GST_TYPE_FIND_ELEMENT (parent);
619 GST_DEBUG_OBJECT (typefind, "got %s event in mode %d",
620 GST_EVENT_TYPE_NAME (event), typefind->mode);
622 switch (typefind->mode) {
624 switch (GST_EVENT_TYPE (event)) {
629 /* Parse and push out our caps and data */
630 gst_event_parse_caps (event, &caps);
631 res = gst_type_find_element_setcaps (typefind, caps);
633 gst_event_unref (event);
638 GST_FIXME_OBJECT (typefind,
639 "GAP events during typefinding not handled properly");
641 /* FIXME: These would need to be inserted in the stream at
642 * the right position between buffers, but we combine all
643 * buffers with a GstAdapter. Drop the GAP event for now,
644 * which will only cause an implicit GAP between buffers.
646 gst_event_unref (event);
652 GST_INFO_OBJECT (typefind, "Got EOS and no type found yet");
653 gst_type_find_element_chain_do_typefinding (typefind, FALSE, TRUE);
655 res = gst_pad_push_event (typefind->src, event);
658 case GST_EVENT_FLUSH_STOP:{
661 GST_OBJECT_LOCK (typefind);
663 for (l = typefind->cached_events; l; l = l->next) {
664 if (GST_EVENT_IS_STICKY (l->data) &&
665 GST_EVENT_TYPE (l->data) != GST_EVENT_SEGMENT &&
666 GST_EVENT_TYPE (l->data) != GST_EVENT_EOS) {
667 gst_pad_store_sticky_event (typefind->src, l->data);
669 gst_event_unref (l->data);
672 g_list_free (typefind->cached_events);
673 typefind->cached_events = NULL;
674 gst_adapter_clear (typefind->adapter);
675 GST_OBJECT_UNLOCK (typefind);
678 case GST_EVENT_FLUSH_START:
679 res = gst_pad_push_event (typefind->src, event);
682 /* Forward events that would happen before the caps event
683 * directly instead of storing them. There's no reason not
684 * to send them directly and we should only store events
685 * for later sending that would need to come after the caps
687 if (GST_EVENT_TYPE (event) < GST_EVENT_CAPS) {
688 res = gst_pad_push_event (typefind->src, event);
690 GST_DEBUG_OBJECT (typefind, "Saving %s event to send later",
691 GST_EVENT_TYPE_NAME (event));
692 GST_OBJECT_LOCK (typefind);
693 typefind->cached_events =
694 g_list_append (typefind->cached_events, event);
695 GST_OBJECT_UNLOCK (typefind);
702 res = gst_pad_push_event (typefind->src, event);
707 g_assert_not_reached ();
713 gst_type_find_element_send_cached_events (GstTypeFindElement * typefind)
715 GList *l, *cached_events;
717 GST_OBJECT_LOCK (typefind);
718 cached_events = typefind->cached_events;
719 typefind->cached_events = NULL;
720 GST_OBJECT_UNLOCK (typefind);
722 for (l = cached_events; l != NULL; l = l->next) {
723 GstEvent *event = GST_EVENT (l->data);
725 GST_DEBUG_OBJECT (typefind, "sending cached %s event",
726 GST_EVENT_TYPE_NAME (event));
727 gst_pad_push_event (typefind->src, event);
729 g_list_free (cached_events);
733 gst_type_find_element_setcaps (GstTypeFindElement * typefind, GstCaps * caps)
735 /* don't operate on ANY caps */
736 if (gst_caps_is_any (caps))
739 g_signal_emit (typefind, gst_type_find_element_signals[HAVE_TYPE], 0,
740 GST_TYPE_FIND_MAXIMUM, caps);
742 /* Shortcircuit typefinding if we get caps */
743 GST_DEBUG_OBJECT (typefind, "Skipping typefinding, using caps from "
744 "upstream: %" GST_PTR_FORMAT, caps);
746 stop_typefinding (typefind);
752 gst_type_find_get_extension (GstTypeFindElement * typefind, GstPad * pad)
759 query = gst_query_new_uri ();
761 /* try getting the caps with an uri query and from the extension */
762 if (!gst_pad_peer_query (pad, query))
763 goto peer_query_failed;
765 gst_query_parse_uri (query, &uri);
769 GST_DEBUG_OBJECT (typefind, "finding extension of %s", uri);
771 /* find the extension on the uri, this is everything after a '.' */
776 if (uri[find] == '.')
783 result = g_strdup (&uri[find + 1]);
785 GST_DEBUG_OBJECT (typefind, "found extension %s", result);
786 gst_query_unref (query);
794 GST_WARNING_OBJECT (typefind, "failed to query peer uri");
795 gst_query_unref (query);
800 GST_WARNING_OBJECT (typefind, "could not parse the peer uri");
801 gst_query_unref (query);
806 GST_WARNING_OBJECT (typefind, "could not find uri extension in %s", uri);
807 gst_query_unref (query);
814 gst_type_find_guess_by_extension (GstTypeFindElement * typefind, GstPad * pad,
815 GstTypeFindProbability * probability)
820 ext = gst_type_find_get_extension (typefind, pad);
824 caps = gst_type_find_helper_for_extension (GST_OBJECT_CAST (typefind), ext);
826 *probability = GST_TYPE_FIND_MAXIMUM;
834 gst_type_find_element_chain (GstPad * pad, GstObject * parent,
837 GstTypeFindElement *typefind;
838 GstFlowReturn res = GST_FLOW_OK;
840 typefind = GST_TYPE_FIND_ELEMENT (parent);
842 GST_LOG_OBJECT (typefind, "handling buffer in mode %d", typefind->mode);
844 switch (typefind->mode) {
846 /* we should already have called GST_ELEMENT_ERROR */
847 return GST_FLOW_ERROR;
849 /* don't take object lock as typefind->caps should not change anymore */
850 return gst_pad_push (typefind->src, buffer);
853 GST_OBJECT_LOCK (typefind);
854 if (typefind->initial_offset == GST_BUFFER_OFFSET_NONE)
855 typefind->initial_offset = GST_BUFFER_OFFSET (buffer);
856 gst_adapter_push (typefind->adapter, buffer);
857 GST_OBJECT_UNLOCK (typefind);
859 res = gst_type_find_element_chain_do_typefinding (typefind, TRUE, FALSE);
861 if (typefind->mode == MODE_ERROR)
862 res = GST_FLOW_ERROR;
867 g_assert_not_reached ();
868 return GST_FLOW_ERROR;
875 gst_type_find_element_chain_do_typefinding (GstTypeFindElement * typefind,
876 gboolean check_avail, gboolean at_eos)
878 GstTypeFindProbability probability;
879 GstCaps *caps = NULL;
882 gboolean have_min, have_max;
884 GST_OBJECT_LOCK (typefind);
885 if (typefind->force_caps) {
886 caps = gst_caps_ref (typefind->force_caps);
887 probability = GST_TYPE_FIND_MAXIMUM;
891 avail = gst_adapter_available (typefind->adapter);
894 have_min = avail >= TYPE_FIND_MIN_SIZE;
895 have_max = avail >= TYPE_FIND_MAX_SIZE;
897 have_min = avail > 0;
902 goto not_enough_data;
904 /* map all available data */
905 data = gst_adapter_map (typefind->adapter, avail);
906 caps = gst_type_find_helper_for_data (GST_OBJECT (typefind),
907 data, avail, &probability);
908 gst_adapter_unmap (typefind->adapter);
910 if (caps == NULL && have_max)
912 else if (caps == NULL)
916 if (probability < typefind->min_probability)
917 goto low_probability;
920 GST_OBJECT_UNLOCK (typefind);
922 /* probability is good enough too, so let's make it known ... emiting this
923 * signal calls our object handler which sets the caps. */
924 g_signal_emit (typefind, gst_type_find_element_signals[HAVE_TYPE], 0,
927 /* .. and send out the accumulated data */
928 stop_typefinding (typefind);
929 gst_caps_unref (caps);
935 GST_OBJECT_UNLOCK (typefind);
938 GST_ELEMENT_ERROR (typefind, STREAM, TYPE_NOT_FOUND,
939 (_("Stream contains not enough data.")), ("Can't typefind stream"));
940 return GST_FLOW_ERROR;
942 GST_DEBUG_OBJECT (typefind, "not enough data for typefinding yet "
943 "(%" G_GSIZE_FORMAT " bytes)", avail);
949 GST_OBJECT_UNLOCK (typefind);
950 GST_ELEMENT_ERROR (typefind, STREAM, TYPE_NOT_FOUND, (NULL), (NULL));
951 stop_typefinding (typefind);
952 return GST_FLOW_ERROR;
956 GST_OBJECT_UNLOCK (typefind);
959 GST_ELEMENT_ERROR (typefind, STREAM, TYPE_NOT_FOUND,
960 (_("Stream contains not enough data.")), ("Can't typefind stream"));
961 return GST_FLOW_ERROR;
963 GST_DEBUG_OBJECT (typefind,
964 "no caps found with %" G_GSIZE_FORMAT " bytes of data, "
965 "waiting for more data", avail);
971 GST_DEBUG_OBJECT (typefind, "found caps %" GST_PTR_FORMAT ", but "
972 "probability is %u which is lower than the required minimum of %u",
973 caps, probability, typefind->min_probability);
975 gst_caps_unref (caps);
980 GST_OBJECT_UNLOCK (typefind);
981 GST_DEBUG_OBJECT (typefind, "waiting for more data to try again");
987 gst_type_find_element_getrange (GstPad * srcpad, GstObject * parent,
988 guint64 offset, guint length, GstBuffer ** buffer)
990 GstTypeFindElement *typefind;
993 typefind = GST_TYPE_FIND_ELEMENT (parent);
995 ret = gst_pad_pull_range (typefind->sink, offset, length, buffer);
1001 gst_type_find_element_activate_src_mode (GstPad * pad, GstObject * parent,
1002 GstPadMode mode, gboolean active)
1005 GstTypeFindElement *typefind;
1007 typefind = GST_TYPE_FIND_ELEMENT (parent);
1010 case GST_PAD_MODE_PULL:
1011 /* make sure our task stops pushing, we can't call _stop here because this
1012 * activation might happen from the streaming thread. */
1013 gst_pad_pause_task (typefind->sink);
1014 res = gst_pad_activate_mode (typefind->sink, mode, active);
1024 gst_type_find_element_loop (GstPad * pad)
1026 GstTypeFindElement *typefind;
1027 GstFlowReturn ret = GST_FLOW_OK;
1029 typefind = GST_TYPE_FIND_ELEMENT (GST_PAD_PARENT (pad));
1031 if (typefind->need_stream_start) {
1035 stream_id = gst_pad_create_stream_id (typefind->src,
1036 GST_ELEMENT_CAST (typefind), NULL);
1038 GST_DEBUG_OBJECT (typefind, "Pushing STREAM_START");
1039 event = gst_event_new_stream_start (stream_id);
1040 gst_event_set_group_id (event, gst_util_group_id_next ());
1041 gst_pad_push_event (typefind->src, event);
1043 typefind->need_stream_start = FALSE;
1047 if (typefind->mode == MODE_TYPEFIND) {
1048 GstPad *peer = NULL;
1049 GstCaps *found_caps = NULL;
1050 GstTypeFindProbability probability = GST_TYPE_FIND_NONE;
1052 GST_DEBUG_OBJECT (typefind, "find type in pull mode");
1054 GST_OBJECT_LOCK (typefind);
1055 if (typefind->force_caps) {
1056 found_caps = gst_caps_ref (typefind->force_caps);
1057 probability = GST_TYPE_FIND_MAXIMUM;
1059 GST_OBJECT_UNLOCK (typefind);
1062 peer = gst_pad_get_peer (pad);
1067 if (!gst_pad_query_duration (peer, GST_FORMAT_BYTES, &size)) {
1068 GST_WARNING_OBJECT (typefind, "Could not query upstream length!");
1069 gst_object_unref (peer);
1071 ret = GST_FLOW_ERROR;
1075 /* the size if 0, we cannot continue */
1077 /* keep message in sync with message in sink event handler */
1078 GST_ELEMENT_ERROR (typefind, STREAM, TYPE_NOT_FOUND,
1079 (_("Stream contains no data.")), ("Can't typefind empty stream"));
1080 gst_object_unref (peer);
1081 ret = GST_FLOW_ERROR;
1084 ext = gst_type_find_get_extension (typefind, pad);
1087 gst_type_find_helper_get_range (GST_OBJECT_CAST (peer),
1088 GST_OBJECT_PARENT (peer),
1089 (GstTypeFindHelperGetRangeFunction) (GST_PAD_GETRANGEFUNC (peer)),
1090 (guint64) size, ext, &probability);
1093 GST_DEBUG ("Found caps %" GST_PTR_FORMAT, found_caps);
1095 gst_object_unref (peer);
1099 if (!found_caps || probability < typefind->min_probability) {
1100 GST_DEBUG ("Trying to guess using extension");
1101 gst_caps_replace (&found_caps, NULL);
1103 gst_type_find_guess_by_extension (typefind, pad, &probability);
1106 if (!found_caps || probability < typefind->min_probability) {
1107 GST_ELEMENT_ERROR (typefind, STREAM, TYPE_NOT_FOUND, (NULL), (NULL));
1108 gst_caps_replace (&found_caps, NULL);
1109 ret = GST_FLOW_ERROR;
1113 GST_DEBUG ("Emiting found caps %" GST_PTR_FORMAT, found_caps);
1114 g_signal_emit (typefind, gst_type_find_element_signals[HAVE_TYPE],
1115 0, probability, found_caps);
1116 typefind->mode = MODE_NORMAL;
1117 gst_caps_unref (found_caps);
1118 } else if (typefind->mode == MODE_NORMAL) {
1119 GstBuffer *outbuf = NULL;
1121 if (typefind->need_segment) {
1122 typefind->need_segment = FALSE;
1123 gst_pad_push_event (typefind->src,
1124 gst_event_new_segment (&typefind->segment));
1127 /* Pull 4k blocks and send downstream */
1128 ret = gst_pad_pull_range (typefind->sink, typefind->offset, 4096, &outbuf);
1129 if (ret != GST_FLOW_OK)
1132 typefind->offset += gst_buffer_get_size (outbuf);
1134 ret = gst_pad_push (typefind->src, outbuf);
1135 if (ret != GST_FLOW_OK)
1139 ret = GST_FLOW_ERROR;
1147 const gchar *reason = gst_flow_get_name (ret);
1148 gboolean push_eos = FALSE;
1150 GST_LOG_OBJECT (typefind, "pausing task, reason %s", reason);
1151 gst_pad_pause_task (typefind->sink);
1153 if (ret == GST_FLOW_EOS) {
1154 /* perform EOS logic */
1156 if (typefind->segment.flags & GST_SEGMENT_FLAG_SEGMENT) {
1159 /* for segment playback we need to post when (in stream time)
1160 * we stopped, this is either stop (when set) or the duration. */
1161 if ((stop = typefind->segment.stop) == -1)
1162 stop = typefind->offset;
1164 GST_LOG_OBJECT (typefind, "Sending segment done, at end of segment");
1165 gst_element_post_message (GST_ELEMENT (typefind),
1166 gst_message_new_segment_done (GST_OBJECT (typefind),
1167 GST_FORMAT_BYTES, stop));
1168 gst_pad_push_event (typefind->src,
1169 gst_event_new_segment_done (GST_FORMAT_BYTES, stop));
1173 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
1174 /* for fatal errors we post an error message */
1175 GST_ELEMENT_ERROR (typefind, STREAM, FAILED, (NULL),
1176 ("stream stopped, reason %s", reason));
1180 /* send EOS, and prevent hanging if no streams yet */
1181 GST_LOG_OBJECT (typefind, "Sending EOS, at end of stream");
1182 gst_pad_push_event (typefind->src, gst_event_new_eos ());
1189 gst_type_find_element_activate_sink_mode (GstPad * pad, GstObject * parent,
1190 GstPadMode mode, gboolean active)
1193 GstTypeFindElement *typefind;
1195 typefind = GST_TYPE_FIND_ELEMENT (parent);
1198 case GST_PAD_MODE_PULL:
1200 gst_segment_init (&typefind->segment, GST_FORMAT_BYTES);
1201 typefind->need_segment = TRUE;
1202 typefind->need_stream_start = TRUE;
1203 typefind->offset = 0;
1206 res = gst_pad_stop_task (pad);
1207 gst_segment_init (&typefind->segment, GST_FORMAT_UNDEFINED);
1210 case GST_PAD_MODE_PUSH:
1212 gst_segment_init (&typefind->segment, GST_FORMAT_UNDEFINED);
1213 start_typefinding (typefind);
1215 stop_typefinding (typefind);
1216 gst_segment_init (&typefind->segment, GST_FORMAT_UNDEFINED);
1228 gst_type_find_element_activate_sink (GstPad * pad, GstObject * parent)
1232 GstSchedulingFlags sched_flags;
1234 query = gst_query_new_scheduling ();
1236 if (!gst_pad_peer_query (pad, query)) {
1237 gst_query_unref (query);
1241 gst_query_parse_scheduling (query, &sched_flags, NULL, NULL, NULL);
1243 pull_mode = gst_query_has_scheduling_mode (query, GST_PAD_MODE_PULL)
1244 && ((sched_flags & GST_SCHEDULING_FLAG_SEEKABLE) != 0);
1246 gst_query_unref (query);
1251 if (!gst_pad_activate_mode (pad, GST_PAD_MODE_PULL, TRUE))
1254 /* only start our task if we ourselves decide to start in pull mode */
1255 return gst_pad_start_task (pad, (GstTaskFunction) gst_type_find_element_loop,
1260 return gst_pad_activate_mode (pad, GST_PAD_MODE_PUSH, TRUE);
1264 static GstStateChangeReturn
1265 gst_type_find_element_change_state (GstElement * element,
1266 GstStateChange transition)
1268 GstStateChangeReturn ret;
1269 GstTypeFindElement *typefind;
1271 typefind = GST_TYPE_FIND_ELEMENT (element);
1274 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1276 switch (transition) {
1277 case GST_STATE_CHANGE_PAUSED_TO_READY:
1278 case GST_STATE_CHANGE_READY_TO_NULL:
1279 GST_OBJECT_LOCK (typefind);
1280 gst_caps_replace (&typefind->caps, NULL);
1282 g_list_foreach (typefind->cached_events,
1283 (GFunc) gst_mini_object_unref, NULL);
1284 g_list_free (typefind->cached_events);
1285 typefind->cached_events = NULL;
1286 typefind->mode = MODE_TYPEFIND;
1287 GST_OBJECT_UNLOCK (typefind);