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 #define TYPE_FIND_MIN_SIZE (2*1024)
101 #define TYPE_FIND_MAX_SIZE (128*1024)
103 /* TypeFind signals and args */
119 MODE_NORMAL, /* act as identity */
120 MODE_TYPEFIND, /* do typefinding */
121 MODE_ERROR /* had fatal error */
126 GST_DEBUG_CATEGORY_INIT (gst_type_find_element_debug, "typefind", \
127 GST_DEBUG_BG_YELLOW | GST_DEBUG_FG_GREEN, "type finding element");
128 #define gst_type_find_element_parent_class parent_class
129 G_DEFINE_TYPE_WITH_CODE (GstTypeFindElement, gst_type_find_element,
130 GST_TYPE_ELEMENT, _do_init);
132 static void gst_type_find_element_dispose (GObject * object);
133 static void gst_type_find_element_set_property (GObject * object,
134 guint prop_id, const GValue * value, GParamSpec * pspec);
135 static void gst_type_find_element_get_property (GObject * object,
136 guint prop_id, GValue * value, GParamSpec * pspec);
138 static gboolean gst_type_find_element_src_event (GstPad * pad,
139 GstObject * parent, GstEvent * event);
140 static gboolean gst_type_find_handle_src_query (GstPad * pad,
141 GstObject * parent, GstQuery * query);
143 static gboolean gst_type_find_element_sink_event (GstPad * pad,
144 GstObject * parent, GstEvent * event);
145 static gboolean gst_type_find_element_setcaps (GstTypeFindElement * typefind,
147 static GstFlowReturn gst_type_find_element_chain (GstPad * sinkpad,
148 GstObject * parent, GstBuffer * buffer);
149 static GstFlowReturn gst_type_find_element_getrange (GstPad * srcpad,
150 GstObject * parent, guint64 offset, guint length, GstBuffer ** buffer);
152 static GstStateChangeReturn
153 gst_type_find_element_change_state (GstElement * element,
154 GstStateChange transition);
155 static gboolean gst_type_find_element_activate_sink (GstPad * pad,
157 static gboolean gst_type_find_element_activate_sink_mode (GstPad * pad,
158 GstObject * parent, GstPadMode mode, gboolean active);
159 static gboolean gst_type_find_element_activate_src_mode (GstPad * pad,
160 GstObject * parent, GstPadMode mode, gboolean active);
162 gst_type_find_element_chain_do_typefinding (GstTypeFindElement * typefind,
163 gboolean check_avail);
164 static void gst_type_find_element_send_cached_events (GstTypeFindElement *
167 static void gst_type_find_element_loop (GstPad * pad);
169 static guint gst_type_find_element_signals[LAST_SIGNAL] = { 0 };
172 gst_type_find_element_have_type (GstTypeFindElement * typefind,
173 guint probability, GstCaps * caps)
175 g_assert (caps != NULL);
177 GST_INFO_OBJECT (typefind, "found caps %" GST_PTR_FORMAT ", probability=%u",
180 GST_OBJECT_LOCK (typefind);
182 gst_caps_unref (typefind->caps);
183 typefind->caps = gst_caps_ref (caps);
184 GST_OBJECT_UNLOCK (typefind);
186 gst_pad_set_caps (typefind->src, caps);
190 gst_type_find_element_class_init (GstTypeFindElementClass * typefind_class)
192 GObjectClass *gobject_class = G_OBJECT_CLASS (typefind_class);
193 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (typefind_class);
195 gobject_class->set_property = gst_type_find_element_set_property;
196 gobject_class->get_property = gst_type_find_element_get_property;
197 gobject_class->dispose = gst_type_find_element_dispose;
199 g_object_class_install_property (gobject_class, PROP_CAPS,
200 g_param_spec_boxed ("caps", _("caps"),
201 _("detected capabilities in stream"), GST_TYPE_CAPS,
202 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
203 g_object_class_install_property (gobject_class, PROP_MINIMUM,
204 g_param_spec_uint ("minimum", _("minimum"),
205 "minimum probability required to accept caps", GST_TYPE_FIND_MINIMUM,
206 GST_TYPE_FIND_MAXIMUM, GST_TYPE_FIND_MINIMUM,
207 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
208 g_object_class_install_property (gobject_class, PROP_FORCE_CAPS,
209 g_param_spec_boxed ("force-caps", _("force caps"),
210 _("force caps without doing a typefind"), GST_TYPE_CAPS,
211 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
213 * GstTypeFindElement::have-type:
214 * @typefind: the typefind instance
215 * @probability: the probability of the type found
216 * @caps: the caps of the type found
218 * This signal gets emitted when the type and its probability has
221 gst_type_find_element_signals[HAVE_TYPE] = g_signal_new ("have-type",
222 G_TYPE_FROM_CLASS (typefind_class), G_SIGNAL_RUN_LAST,
223 G_STRUCT_OFFSET (GstTypeFindElementClass, have_type), NULL, NULL,
224 g_cclosure_marshal_generic, G_TYPE_NONE, 2,
225 G_TYPE_UINT, GST_TYPE_CAPS | G_SIGNAL_TYPE_STATIC_SCOPE);
227 typefind_class->have_type =
228 GST_DEBUG_FUNCPTR (gst_type_find_element_have_type);
230 gst_element_class_set_static_metadata (gstelement_class,
233 "Finds the media type of a stream",
234 "Benjamin Otte <in7y118@public.uni-hamburg.de>");
235 gst_element_class_add_pad_template (gstelement_class,
236 gst_static_pad_template_get (&type_find_element_src_template));
237 gst_element_class_add_pad_template (gstelement_class,
238 gst_static_pad_template_get (&type_find_element_sink_template));
240 gstelement_class->change_state =
241 GST_DEBUG_FUNCPTR (gst_type_find_element_change_state);
245 gst_type_find_element_init (GstTypeFindElement * typefind)
249 gst_pad_new_from_static_template (&type_find_element_sink_template,
252 gst_pad_set_activate_function (typefind->sink,
253 GST_DEBUG_FUNCPTR (gst_type_find_element_activate_sink));
254 gst_pad_set_activatemode_function (typefind->sink,
255 GST_DEBUG_FUNCPTR (gst_type_find_element_activate_sink_mode));
256 gst_pad_set_chain_function (typefind->sink,
257 GST_DEBUG_FUNCPTR (gst_type_find_element_chain));
258 gst_pad_set_event_function (typefind->sink,
259 GST_DEBUG_FUNCPTR (gst_type_find_element_sink_event));
260 GST_PAD_SET_PROXY_ALLOCATION (typefind->sink);
261 gst_element_add_pad (GST_ELEMENT (typefind), typefind->sink);
265 gst_pad_new_from_static_template (&type_find_element_src_template, "src");
267 gst_pad_set_activatemode_function (typefind->src,
268 GST_DEBUG_FUNCPTR (gst_type_find_element_activate_src_mode));
269 gst_pad_set_getrange_function (typefind->src,
270 GST_DEBUG_FUNCPTR (gst_type_find_element_getrange));
271 gst_pad_set_event_function (typefind->src,
272 GST_DEBUG_FUNCPTR (gst_type_find_element_src_event));
273 gst_pad_set_query_function (typefind->src,
274 GST_DEBUG_FUNCPTR (gst_type_find_handle_src_query));
275 gst_pad_use_fixed_caps (typefind->src);
276 gst_element_add_pad (GST_ELEMENT (typefind), typefind->src);
278 typefind->mode = MODE_TYPEFIND;
279 typefind->caps = NULL;
280 typefind->min_probability = 1;
282 typefind->adapter = gst_adapter_new ();
286 gst_type_find_element_dispose (GObject * object)
288 GstTypeFindElement *typefind = GST_TYPE_FIND_ELEMENT (object);
290 if (typefind->adapter) {
291 g_object_unref (typefind->adapter);
292 typefind->adapter = NULL;
295 if (typefind->force_caps) {
296 gst_caps_unref (typefind->force_caps);
297 typefind->force_caps = NULL;
300 G_OBJECT_CLASS (parent_class)->dispose (object);
304 gst_type_find_element_set_property (GObject * object, guint prop_id,
305 const GValue * value, GParamSpec * pspec)
307 GstTypeFindElement *typefind;
309 typefind = GST_TYPE_FIND_ELEMENT (object);
313 typefind->min_probability = g_value_get_uint (value);
315 case PROP_FORCE_CAPS:
316 GST_OBJECT_LOCK (typefind);
317 if (typefind->force_caps)
318 gst_caps_unref (typefind->force_caps);
319 typefind->force_caps = g_value_dup_boxed (value);
320 GST_OBJECT_UNLOCK (typefind);
323 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
329 gst_type_find_element_get_property (GObject * object, guint prop_id,
330 GValue * value, GParamSpec * pspec)
332 GstTypeFindElement *typefind;
334 typefind = GST_TYPE_FIND_ELEMENT (object);
338 GST_OBJECT_LOCK (typefind);
339 g_value_set_boxed (value, typefind->caps);
340 GST_OBJECT_UNLOCK (typefind);
343 g_value_set_uint (value, typefind->min_probability);
345 case PROP_FORCE_CAPS:
346 GST_OBJECT_LOCK (typefind);
347 g_value_set_boxed (value, typefind->force_caps);
348 GST_OBJECT_UNLOCK (typefind);
351 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
357 gst_type_find_handle_src_query (GstPad * pad, GstObject * parent,
360 GstTypeFindElement *typefind;
361 gboolean res = FALSE;
363 typefind = GST_TYPE_FIND_ELEMENT (parent);
364 GST_DEBUG_OBJECT (typefind, "Handling src query %s",
365 GST_QUERY_TYPE_NAME (query));
367 switch (GST_QUERY_TYPE (query)) {
368 case GST_QUERY_SCHEDULING:
369 /* FIXME, filter out the scheduling modes that we understand */
370 res = gst_pad_peer_query (typefind->sink, query);
374 GST_DEBUG_OBJECT (typefind,
375 "Got caps query, our caps are %" GST_PTR_FORMAT, typefind->caps);
377 /* We can hijack caps query if we typefind already */
378 if (typefind->caps) {
379 gst_query_set_caps_result (query, typefind->caps);
382 res = gst_pad_peer_query (typefind->sink, query);
386 case GST_QUERY_POSITION:
391 if (!(res = gst_pad_peer_query (typefind->sink, query)))
394 gst_query_parse_position (query, &format, &peer_pos);
396 GST_OBJECT_LOCK (typefind);
397 /* FIXME: this code assumes that there's no discont in the queue */
399 case GST_FORMAT_BYTES:
400 peer_pos -= gst_adapter_available (typefind->adapter);
406 GST_OBJECT_UNLOCK (typefind);
407 gst_query_set_position (query, format, peer_pos);
411 res = gst_pad_query_default (pad, parent, query);
419 gst_type_find_element_seek (GstTypeFindElement * typefind, GstEvent * event)
422 GstSeekType start_type, stop_type;
427 GstSegment seeksegment = { 0, };
429 gst_event_parse_seek (event, &rate, &format, &flags, &start_type, &start,
432 /* we can only seek on bytes */
433 if (format != GST_FORMAT_BYTES) {
434 GST_DEBUG_OBJECT (typefind, "Can only seek on BYTES");
438 /* copy segment, we need this because we still need the old
439 * segment when we close the current segment. */
440 memcpy (&seeksegment, &typefind->segment, sizeof (GstSegment));
442 GST_DEBUG_OBJECT (typefind, "configuring seek");
443 gst_segment_do_seek (&seeksegment, rate, format, flags,
444 start_type, start, stop_type, stop, NULL);
446 flush = ! !(flags & GST_SEEK_FLAG_FLUSH);
448 GST_DEBUG_OBJECT (typefind, "New segment %" GST_SEGMENT_FORMAT, &seeksegment);
451 GST_DEBUG_OBJECT (typefind, "Starting flush");
452 gst_pad_push_event (typefind->sink, gst_event_new_flush_start ());
453 gst_pad_push_event (typefind->src, gst_event_new_flush_start ());
455 GST_DEBUG_OBJECT (typefind, "Non-flushing seek, pausing task");
456 gst_pad_pause_task (typefind->sink);
459 /* now grab the stream lock so that streaming cannot continue, for
460 * non flushing seeks when the element is in PAUSED this could block
462 GST_DEBUG_OBJECT (typefind, "Waiting for streaming to stop");
463 GST_PAD_STREAM_LOCK (typefind->sink);
466 GST_DEBUG_OBJECT (typefind, "Stopping flush");
467 gst_pad_push_event (typefind->sink, gst_event_new_flush_stop (TRUE));
468 gst_pad_push_event (typefind->src, gst_event_new_flush_stop (TRUE));
471 /* now update the real segment info */
472 GST_DEBUG_OBJECT (typefind, "Committing new seek segment");
473 memcpy (&typefind->segment, &seeksegment, sizeof (GstSegment));
474 typefind->offset = typefind->segment.start;
476 /* notify start of new segment */
477 if (typefind->segment.flags & GST_SEGMENT_FLAG_SEGMENT) {
480 msg = gst_message_new_segment_start (GST_OBJECT (typefind),
481 GST_FORMAT_BYTES, typefind->segment.start);
482 gst_element_post_message (GST_ELEMENT (typefind), msg);
485 typefind->need_segment = TRUE;
487 /* restart our task since it might have been stopped when we did the
489 gst_pad_start_task (typefind->sink,
490 (GstTaskFunction) gst_type_find_element_loop, typefind->sink, NULL);
492 /* streaming can continue now */
493 GST_PAD_STREAM_UNLOCK (typefind->sink);
499 gst_type_find_element_src_event (GstPad * pad, GstObject * parent,
502 GstTypeFindElement *typefind = GST_TYPE_FIND_ELEMENT (parent);
504 if (typefind->mode != MODE_NORMAL) {
505 /* need to do more? */
506 gst_mini_object_unref (GST_MINI_OBJECT_CAST (event));
510 /* Only handle seeks here if driving the pipeline */
511 if (typefind->segment.format != GST_FORMAT_UNDEFINED &&
512 GST_EVENT_TYPE (event) == GST_EVENT_SEEK) {
513 return gst_type_find_element_seek (typefind, event);
515 return gst_pad_push_event (typefind->sink, event);
520 start_typefinding (GstTypeFindElement * typefind)
522 GST_DEBUG_OBJECT (typefind, "starting typefinding");
524 GST_OBJECT_LOCK (typefind);
526 gst_caps_replace (&typefind->caps, NULL);
527 typefind->initial_offset = GST_BUFFER_OFFSET_NONE;
528 GST_OBJECT_UNLOCK (typefind);
530 typefind->mode = MODE_TYPEFIND;
534 stop_typefinding (GstTypeFindElement * typefind)
537 gboolean push_cached_buffers;
540 GstClockTime pts, dts;
542 gst_element_get_state (GST_ELEMENT (typefind), &state, NULL, 0);
544 push_cached_buffers = (state >= GST_STATE_PAUSED && typefind->caps);
546 GST_DEBUG_OBJECT (typefind, "stopping typefinding%s",
547 push_cached_buffers ? " and pushing cached events and buffers" : "");
549 typefind->mode = MODE_NORMAL;
550 if (push_cached_buffers)
551 gst_type_find_element_send_cached_events (typefind);
553 GST_OBJECT_LOCK (typefind);
554 avail = gst_adapter_available (typefind->adapter);
558 pts = gst_adapter_prev_pts (typefind->adapter, NULL);
559 dts = gst_adapter_prev_dts (typefind->adapter, NULL);
560 buffer = gst_adapter_take_buffer (typefind->adapter, avail);
561 GST_BUFFER_PTS (buffer) = pts;
562 GST_BUFFER_DTS (buffer) = dts;
563 GST_BUFFER_OFFSET (buffer) = typefind->initial_offset;
564 GST_OBJECT_UNLOCK (typefind);
566 if (!push_cached_buffers) {
567 gst_buffer_unref (buffer);
569 GstPad *peer = gst_pad_get_peer (typefind->src);
571 /* make sure the user gets a meaningful error message in this case,
572 * which is not a core bug or bug of any kind (as the default error
573 * message emitted by gstpad.c otherwise would make you think) */
574 if (peer && GST_PAD_CHAINFUNC (peer) == NULL) {
575 GST_DEBUG_OBJECT (typefind, "upstream only supports push mode, while "
576 "downstream element only works in pull mode, erroring out");
577 GST_ELEMENT_ERROR (typefind, STREAM, FAILED,
578 ("%s cannot work in push mode. The operation is not supported "
579 "with this source element or protocol.",
580 G_OBJECT_TYPE_NAME (GST_PAD_PARENT (peer))),
581 ("Downstream pad %s:%s has no chainfunction, and the upstream "
582 "element does not support pull mode", GST_DEBUG_PAD_NAME (peer)));
583 typefind->mode = MODE_ERROR; /* make the chain function error out */
584 gst_buffer_unref (buffer);
586 gst_pad_push (typefind->src, buffer);
589 gst_object_unref (peer);
596 GST_DEBUG_OBJECT (typefind, "we have no data to typefind");
597 GST_OBJECT_UNLOCK (typefind);
603 gst_type_find_element_sink_event (GstPad * pad, GstObject * parent,
606 gboolean res = FALSE;
607 GstTypeFindElement *typefind = GST_TYPE_FIND_ELEMENT (parent);
609 GST_DEBUG_OBJECT (typefind, "got %s event in mode %d",
610 GST_EVENT_TYPE_NAME (event), typefind->mode);
612 switch (typefind->mode) {
614 switch (GST_EVENT_TYPE (event)) {
619 /* Parse and push out our caps and data */
620 gst_event_parse_caps (event, &caps);
621 res = gst_type_find_element_setcaps (typefind, caps);
623 gst_event_unref (event);
628 GST_FIXME_OBJECT (typefind,
629 "GAP events during typefinding not handled properly");
631 /* FIXME: These would need to be inserted in the stream at
632 * the right position between buffers, but we combine all
633 * buffers with a GstAdapter. Drop the GAP event for now,
634 * which will only cause an implicit GAP between buffers.
636 gst_event_unref (event);
642 GST_INFO_OBJECT (typefind, "Got EOS and no type found yet");
643 gst_type_find_element_chain_do_typefinding (typefind, FALSE);
645 res = gst_pad_push_event (typefind->src, event);
648 case GST_EVENT_FLUSH_STOP:{
651 GST_OBJECT_LOCK (typefind);
653 for (l = typefind->cached_events; l; l = l->next) {
654 if (GST_EVENT_IS_STICKY (l->data) &&
655 GST_EVENT_TYPE (l->data) != GST_EVENT_SEGMENT &&
656 GST_EVENT_TYPE (l->data) != GST_EVENT_EOS) {
657 gst_pad_store_sticky_event (typefind->src, l->data);
659 gst_event_unref (l->data);
662 g_list_free (typefind->cached_events);
663 typefind->cached_events = NULL;
664 gst_adapter_clear (typefind->adapter);
665 GST_OBJECT_UNLOCK (typefind);
668 case GST_EVENT_FLUSH_START:
669 res = gst_pad_push_event (typefind->src, event);
672 /* Forward events that would happen before the caps event
673 * directly instead of storing them. There's no reason not
674 * to send them directly and we should only store events
675 * for later sending that would need to come after the caps
677 if (GST_EVENT_TYPE (event) < GST_EVENT_CAPS) {
678 res = gst_pad_push_event (typefind->src, event);
680 GST_DEBUG_OBJECT (typefind, "Saving %s event to send later",
681 GST_EVENT_TYPE_NAME (event));
682 GST_OBJECT_LOCK (typefind);
683 typefind->cached_events =
684 g_list_append (typefind->cached_events, event);
685 GST_OBJECT_UNLOCK (typefind);
692 res = gst_pad_push_event (typefind->src, event);
697 g_assert_not_reached ();
703 gst_type_find_element_send_cached_events (GstTypeFindElement * typefind)
705 GList *l, *cached_events;
707 GST_OBJECT_LOCK (typefind);
708 cached_events = typefind->cached_events;
709 typefind->cached_events = NULL;
710 GST_OBJECT_UNLOCK (typefind);
712 for (l = cached_events; l != NULL; l = l->next) {
713 GstEvent *event = GST_EVENT (l->data);
715 GST_DEBUG_OBJECT (typefind, "sending cached %s event",
716 GST_EVENT_TYPE_NAME (event));
717 gst_pad_push_event (typefind->src, event);
719 g_list_free (cached_events);
723 gst_type_find_element_setcaps (GstTypeFindElement * typefind, GstCaps * caps)
725 /* don't operate on ANY caps */
726 if (gst_caps_is_any (caps))
729 g_signal_emit (typefind, gst_type_find_element_signals[HAVE_TYPE], 0,
730 GST_TYPE_FIND_MAXIMUM, caps);
732 /* Shortcircuit typefinding if we get caps */
733 GST_DEBUG_OBJECT (typefind, "Skipping typefinding, using caps from "
734 "upstream: %" GST_PTR_FORMAT, caps);
736 stop_typefinding (typefind);
742 gst_type_find_get_extension (GstTypeFindElement * typefind, GstPad * pad)
749 query = gst_query_new_uri ();
751 /* try getting the caps with an uri query and from the extension */
752 if (!gst_pad_peer_query (pad, query))
753 goto peer_query_failed;
755 gst_query_parse_uri (query, &uri);
759 GST_DEBUG_OBJECT (typefind, "finding extension of %s", uri);
761 /* find the extension on the uri, this is everything after a '.' */
766 if (uri[find] == '.')
773 result = g_strdup (&uri[find + 1]);
775 GST_DEBUG_OBJECT (typefind, "found extension %s", result);
776 gst_query_unref (query);
784 GST_WARNING_OBJECT (typefind, "failed to query peer uri");
785 gst_query_unref (query);
790 GST_WARNING_OBJECT (typefind, "could not parse the peer uri");
791 gst_query_unref (query);
796 GST_WARNING_OBJECT (typefind, "could not find uri extension in %s", uri);
797 gst_query_unref (query);
804 gst_type_find_guess_by_extension (GstTypeFindElement * typefind, GstPad * pad,
805 GstTypeFindProbability * probability)
810 ext = gst_type_find_get_extension (typefind, pad);
814 caps = gst_type_find_helper_for_extension (GST_OBJECT_CAST (typefind), ext);
816 *probability = GST_TYPE_FIND_MAXIMUM;
824 gst_type_find_element_chain (GstPad * pad, GstObject * parent,
827 GstTypeFindElement *typefind;
828 GstFlowReturn res = GST_FLOW_OK;
830 typefind = GST_TYPE_FIND_ELEMENT (parent);
832 GST_LOG_OBJECT (typefind, "handling buffer in mode %d", typefind->mode);
834 switch (typefind->mode) {
836 /* we should already have called GST_ELEMENT_ERROR */
837 return GST_FLOW_ERROR;
839 /* don't take object lock as typefind->caps should not change anymore */
840 return gst_pad_push (typefind->src, buffer);
843 GST_OBJECT_LOCK (typefind);
844 if (typefind->initial_offset == GST_BUFFER_OFFSET_NONE)
845 typefind->initial_offset = GST_BUFFER_OFFSET (buffer);
846 gst_adapter_push (typefind->adapter, buffer);
847 GST_OBJECT_UNLOCK (typefind);
849 res = gst_type_find_element_chain_do_typefinding (typefind, TRUE);
851 if (typefind->mode == MODE_ERROR)
852 res = GST_FLOW_ERROR;
857 g_assert_not_reached ();
858 return GST_FLOW_ERROR;
865 gst_type_find_element_chain_do_typefinding (GstTypeFindElement * typefind,
866 gboolean check_avail)
868 GstTypeFindProbability probability;
869 GstCaps *caps = NULL;
872 gboolean have_min, have_max;
874 GST_OBJECT_LOCK (typefind);
875 if (typefind->force_caps) {
876 caps = gst_caps_ref (typefind->force_caps);
877 probability = GST_TYPE_FIND_MAXIMUM;
881 avail = gst_adapter_available (typefind->adapter);
884 have_min = avail >= TYPE_FIND_MIN_SIZE;
885 have_max = avail >= TYPE_FIND_MAX_SIZE;
887 have_min = avail > 0;
892 goto not_enough_data;
894 /* map all available data */
895 data = gst_adapter_map (typefind->adapter, avail);
896 caps = gst_type_find_helper_for_data (GST_OBJECT (typefind),
897 data, avail, &probability);
898 gst_adapter_unmap (typefind->adapter);
900 if (caps == NULL && have_max)
902 else if (caps == NULL)
906 if (probability < typefind->min_probability)
907 goto low_probability;
910 GST_OBJECT_UNLOCK (typefind);
912 /* probability is good enough too, so let's make it known ... emiting this
913 * signal calls our object handler which sets the caps. */
914 g_signal_emit (typefind, gst_type_find_element_signals[HAVE_TYPE], 0,
917 /* .. and send out the accumulated data */
918 stop_typefinding (typefind);
919 gst_caps_unref (caps);
925 GST_DEBUG_OBJECT (typefind, "not enough data for typefinding yet "
926 "(%" G_GSIZE_FORMAT " bytes)", avail);
927 GST_OBJECT_UNLOCK (typefind);
932 GST_OBJECT_UNLOCK (typefind);
933 GST_ELEMENT_ERROR (typefind, STREAM, TYPE_NOT_FOUND, (NULL), (NULL));
934 stop_typefinding (typefind);
935 return GST_FLOW_ERROR;
939 GST_DEBUG_OBJECT (typefind,
940 "no caps found with %" G_GSIZE_FORMAT " bytes of data, "
941 "waiting for more data", avail);
942 GST_OBJECT_UNLOCK (typefind);
947 GST_DEBUG_OBJECT (typefind, "found caps %" GST_PTR_FORMAT ", but "
948 "probability is %u which is lower than the required minimum of %u",
949 caps, probability, typefind->min_probability);
951 gst_caps_unref (caps);
956 GST_OBJECT_UNLOCK (typefind);
957 GST_DEBUG_OBJECT (typefind, "waiting for more data to try again");
963 gst_type_find_element_getrange (GstPad * srcpad, GstObject * parent,
964 guint64 offset, guint length, GstBuffer ** buffer)
966 GstTypeFindElement *typefind;
969 typefind = GST_TYPE_FIND_ELEMENT (parent);
971 ret = gst_pad_pull_range (typefind->sink, offset, length, buffer);
977 gst_type_find_element_activate_src_mode (GstPad * pad, GstObject * parent,
978 GstPadMode mode, gboolean active)
981 GstTypeFindElement *typefind;
983 typefind = GST_TYPE_FIND_ELEMENT (parent);
986 case GST_PAD_MODE_PULL:
987 /* make sure our task stops pushing, we can't call _stop here because this
988 * activation might happen from the streaming thread. */
989 gst_pad_pause_task (typefind->sink);
990 res = gst_pad_activate_mode (typefind->sink, mode, active);
1000 gst_type_find_element_loop (GstPad * pad)
1002 GstTypeFindElement *typefind;
1003 GstFlowReturn ret = GST_FLOW_OK;
1005 typefind = GST_TYPE_FIND_ELEMENT (GST_PAD_PARENT (pad));
1007 if (typefind->need_stream_start) {
1011 stream_id = gst_pad_create_stream_id (typefind->src,
1012 GST_ELEMENT_CAST (typefind), NULL);
1014 GST_DEBUG_OBJECT (typefind, "Pushing STREAM_START");
1015 event = gst_event_new_stream_start (stream_id);
1016 gst_event_set_group_id (event, gst_util_group_id_next ());
1017 gst_pad_push_event (typefind->src, event);
1019 typefind->need_stream_start = FALSE;
1023 if (typefind->mode == MODE_TYPEFIND) {
1024 GstPad *peer = NULL;
1025 GstCaps *found_caps = NULL;
1026 GstTypeFindProbability probability = GST_TYPE_FIND_NONE;
1028 GST_DEBUG_OBJECT (typefind, "find type in pull mode");
1030 GST_OBJECT_LOCK (typefind);
1031 if (typefind->force_caps) {
1032 found_caps = gst_caps_ref (typefind->force_caps);
1033 probability = GST_TYPE_FIND_MAXIMUM;
1035 GST_OBJECT_UNLOCK (typefind);
1038 peer = gst_pad_get_peer (pad);
1043 if (!gst_pad_query_duration (peer, GST_FORMAT_BYTES, &size)) {
1044 GST_WARNING_OBJECT (typefind, "Could not query upstream length!");
1045 gst_object_unref (peer);
1047 ret = GST_FLOW_ERROR;
1051 /* the size if 0, we cannot continue */
1053 /* keep message in sync with message in sink event handler */
1054 GST_ELEMENT_ERROR (typefind, STREAM, TYPE_NOT_FOUND,
1055 (_("Stream contains no data.")), ("Can't typefind empty stream"));
1056 gst_object_unref (peer);
1057 ret = GST_FLOW_ERROR;
1060 ext = gst_type_find_get_extension (typefind, pad);
1063 gst_type_find_helper_get_range (GST_OBJECT_CAST (peer),
1064 GST_OBJECT_PARENT (peer),
1065 (GstTypeFindHelperGetRangeFunction) (GST_PAD_GETRANGEFUNC (peer)),
1066 (guint64) size, ext, &probability);
1069 GST_DEBUG ("Found caps %" GST_PTR_FORMAT, found_caps);
1071 gst_object_unref (peer);
1075 if (!found_caps || probability < typefind->min_probability) {
1076 GST_DEBUG ("Trying to guess using extension");
1077 gst_caps_replace (&found_caps, NULL);
1079 gst_type_find_guess_by_extension (typefind, pad, &probability);
1082 if (!found_caps || probability < typefind->min_probability) {
1083 GST_ELEMENT_ERROR (typefind, STREAM, TYPE_NOT_FOUND, (NULL), (NULL));
1084 gst_caps_replace (&found_caps, NULL);
1085 ret = GST_FLOW_ERROR;
1089 GST_DEBUG ("Emiting found caps %" GST_PTR_FORMAT, found_caps);
1090 g_signal_emit (typefind, gst_type_find_element_signals[HAVE_TYPE],
1091 0, probability, found_caps);
1092 typefind->mode = MODE_NORMAL;
1093 gst_caps_unref (found_caps);
1094 } else if (typefind->mode == MODE_NORMAL) {
1095 GstBuffer *outbuf = NULL;
1097 if (typefind->need_segment) {
1098 typefind->need_segment = FALSE;
1099 gst_pad_push_event (typefind->src,
1100 gst_event_new_segment (&typefind->segment));
1103 /* Pull 4k blocks and send downstream */
1104 ret = gst_pad_pull_range (typefind->sink, typefind->offset, 4096, &outbuf);
1105 if (ret != GST_FLOW_OK)
1108 typefind->offset += gst_buffer_get_size (outbuf);
1110 ret = gst_pad_push (typefind->src, outbuf);
1111 if (ret != GST_FLOW_OK)
1115 ret = GST_FLOW_ERROR;
1123 const gchar *reason = gst_flow_get_name (ret);
1124 gboolean push_eos = FALSE;
1126 GST_LOG_OBJECT (typefind, "pausing task, reason %s", reason);
1127 gst_pad_pause_task (typefind->sink);
1129 if (ret == GST_FLOW_EOS) {
1130 /* perform EOS logic */
1132 if (typefind->segment.flags & GST_SEGMENT_FLAG_SEGMENT) {
1135 /* for segment playback we need to post when (in stream time)
1136 * we stopped, this is either stop (when set) or the duration. */
1137 if ((stop = typefind->segment.stop) == -1)
1138 stop = typefind->offset;
1140 GST_LOG_OBJECT (typefind, "Sending segment done, at end of segment");
1141 gst_element_post_message (GST_ELEMENT (typefind),
1142 gst_message_new_segment_done (GST_OBJECT (typefind),
1143 GST_FORMAT_BYTES, stop));
1144 gst_pad_push_event (typefind->src,
1145 gst_event_new_segment_done (GST_FORMAT_BYTES, stop));
1149 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
1150 /* for fatal errors we post an error message */
1151 GST_ELEMENT_ERROR (typefind, STREAM, FAILED, (NULL),
1152 ("stream stopped, reason %s", reason));
1156 /* send EOS, and prevent hanging if no streams yet */
1157 GST_LOG_OBJECT (typefind, "Sending EOS, at end of stream");
1158 gst_pad_push_event (typefind->src, gst_event_new_eos ());
1165 gst_type_find_element_activate_sink_mode (GstPad * pad, GstObject * parent,
1166 GstPadMode mode, gboolean active)
1169 GstTypeFindElement *typefind;
1171 typefind = GST_TYPE_FIND_ELEMENT (parent);
1174 case GST_PAD_MODE_PULL:
1176 gst_segment_init (&typefind->segment, GST_FORMAT_BYTES);
1177 typefind->need_segment = TRUE;
1178 typefind->need_stream_start = TRUE;
1179 typefind->offset = 0;
1182 gst_segment_init (&typefind->segment, GST_FORMAT_UNDEFINED);
1183 res = gst_pad_stop_task (pad);
1186 case GST_PAD_MODE_PUSH:
1187 gst_segment_init (&typefind->segment, GST_FORMAT_UNDEFINED);
1189 start_typefinding (typefind);
1191 stop_typefinding (typefind);
1203 gst_type_find_element_activate_sink (GstPad * pad, GstObject * parent)
1207 GstSchedulingFlags sched_flags;
1209 query = gst_query_new_scheduling ();
1211 if (!gst_pad_peer_query (pad, query)) {
1212 gst_query_unref (query);
1216 gst_query_parse_scheduling (query, &sched_flags, NULL, NULL, NULL);
1218 pull_mode = gst_query_has_scheduling_mode (query, GST_PAD_MODE_PULL)
1219 && ((sched_flags & GST_SCHEDULING_FLAG_SEEKABLE) != 0);
1221 gst_query_unref (query);
1226 if (!gst_pad_activate_mode (pad, GST_PAD_MODE_PULL, TRUE))
1229 /* only start our task if we ourselves decide to start in pull mode */
1230 return gst_pad_start_task (pad, (GstTaskFunction) gst_type_find_element_loop,
1235 return gst_pad_activate_mode (pad, GST_PAD_MODE_PUSH, TRUE);
1239 static GstStateChangeReturn
1240 gst_type_find_element_change_state (GstElement * element,
1241 GstStateChange transition)
1243 GstStateChangeReturn ret;
1244 GstTypeFindElement *typefind;
1246 typefind = GST_TYPE_FIND_ELEMENT (element);
1249 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1251 switch (transition) {
1252 case GST_STATE_CHANGE_PAUSED_TO_READY:
1253 case GST_STATE_CHANGE_READY_TO_NULL:
1254 GST_OBJECT_LOCK (typefind);
1255 gst_caps_replace (&typefind->caps, NULL);
1257 g_list_foreach (typefind->cached_events,
1258 (GFunc) gst_mini_object_unref, NULL);
1259 g_list_free (typefind->cached_events);
1260 typefind->cached_events = NULL;
1261 typefind->mode = MODE_TYPEFIND;
1262 GST_OBJECT_UNLOCK (typefind);