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., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
22 * SECTION:element-typefind
24 * Determines the media-type of a stream. It applies typefind functions in the
25 * order of their rank. One the type has been deteted 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);
139 static const GstEventMask *gst_type_find_element_src_event_mask (GstPad * pad);
142 static gboolean gst_type_find_element_src_event (GstPad * pad,
143 GstObject * parent, GstEvent * event);
144 static gboolean gst_type_find_handle_src_query (GstPad * pad,
145 GstObject * parent, GstQuery * query);
147 static gboolean gst_type_find_element_sink_event (GstPad * pad,
148 GstObject * parent, GstEvent * event);
149 static gboolean gst_type_find_element_setcaps (GstTypeFindElement * typefind,
151 static GstFlowReturn gst_type_find_element_chain (GstPad * sinkpad,
152 GstObject * parent, GstBuffer * buffer);
153 static GstFlowReturn gst_type_find_element_getrange (GstPad * srcpad,
154 GstObject * parent, guint64 offset, guint length, GstBuffer ** buffer);
156 static GstStateChangeReturn
157 gst_type_find_element_change_state (GstElement * element,
158 GstStateChange transition);
159 static gboolean gst_type_find_element_activate_sink (GstPad * pad,
161 static gboolean gst_type_find_element_activate_sink_mode (GstPad * pad,
162 GstObject * parent, GstPadMode mode, gboolean active);
163 static gboolean gst_type_find_element_activate_src_mode (GstPad * pad,
164 GstObject * parent, GstPadMode mode, gboolean active);
166 gst_type_find_element_chain_do_typefinding (GstTypeFindElement * typefind,
167 gboolean check_avail);
168 static void gst_type_find_element_send_cached_events (GstTypeFindElement *
171 static void gst_type_find_element_loop (GstPad * pad);
173 static guint gst_type_find_element_signals[LAST_SIGNAL] = { 0 };
176 gst_type_find_element_have_type (GstTypeFindElement * typefind,
177 guint probability, GstCaps * caps)
179 g_assert (caps != NULL);
181 GST_INFO_OBJECT (typefind, "found caps %" GST_PTR_FORMAT ", probability=%u",
184 GST_OBJECT_LOCK (typefind);
186 gst_caps_unref (typefind->caps);
187 typefind->caps = gst_caps_ref (caps);
188 GST_OBJECT_UNLOCK (typefind);
190 gst_pad_set_caps (typefind->src, caps);
194 gst_type_find_element_class_init (GstTypeFindElementClass * typefind_class)
196 GObjectClass *gobject_class = G_OBJECT_CLASS (typefind_class);
197 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (typefind_class);
199 gobject_class->set_property = gst_type_find_element_set_property;
200 gobject_class->get_property = gst_type_find_element_get_property;
201 gobject_class->dispose = gst_type_find_element_dispose;
203 g_object_class_install_property (gobject_class, PROP_CAPS,
204 g_param_spec_boxed ("caps", _("caps"),
205 _("detected capabilities in stream"), GST_TYPE_CAPS,
206 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
207 g_object_class_install_property (gobject_class, PROP_MINIMUM,
208 g_param_spec_uint ("minimum", _("minimum"),
209 "minimum probability required to accept caps", GST_TYPE_FIND_MINIMUM,
210 GST_TYPE_FIND_MAXIMUM, GST_TYPE_FIND_MINIMUM,
211 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
212 g_object_class_install_property (gobject_class, PROP_FORCE_CAPS,
213 g_param_spec_boxed ("force-caps", _("force caps"),
214 _("force caps without doing a typefind"), GST_TYPE_CAPS,
215 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
217 * GstTypeFindElement::have-type:
218 * @typefind: the typefind instance
219 * @probability: the probability of the type found
220 * @caps: the caps of the type found
222 * This signal gets emitted when the type and its probability has
225 gst_type_find_element_signals[HAVE_TYPE] = g_signal_new ("have-type",
226 G_TYPE_FROM_CLASS (typefind_class), G_SIGNAL_RUN_FIRST,
227 G_STRUCT_OFFSET (GstTypeFindElementClass, have_type), NULL, NULL,
228 g_cclosure_marshal_generic, G_TYPE_NONE, 2,
229 G_TYPE_UINT, GST_TYPE_CAPS | G_SIGNAL_TYPE_STATIC_SCOPE);
231 typefind_class->have_type =
232 GST_DEBUG_FUNCPTR (gst_type_find_element_have_type);
234 gst_element_class_set_static_metadata (gstelement_class,
237 "Finds the media type of a stream",
238 "Benjamin Otte <in7y118@public.uni-hamburg.de>");
239 gst_element_class_add_pad_template (gstelement_class,
240 gst_static_pad_template_get (&type_find_element_src_template));
241 gst_element_class_add_pad_template (gstelement_class,
242 gst_static_pad_template_get (&type_find_element_sink_template));
244 gstelement_class->change_state =
245 GST_DEBUG_FUNCPTR (gst_type_find_element_change_state);
249 gst_type_find_element_init (GstTypeFindElement * typefind)
253 gst_pad_new_from_static_template (&type_find_element_sink_template,
256 gst_pad_set_activate_function (typefind->sink,
257 GST_DEBUG_FUNCPTR (gst_type_find_element_activate_sink));
258 gst_pad_set_activatemode_function (typefind->sink,
259 GST_DEBUG_FUNCPTR (gst_type_find_element_activate_sink_mode));
260 gst_pad_set_chain_function (typefind->sink,
261 GST_DEBUG_FUNCPTR (gst_type_find_element_chain));
262 gst_pad_set_event_function (typefind->sink,
263 GST_DEBUG_FUNCPTR (gst_type_find_element_sink_event));
264 GST_PAD_SET_PROXY_ALLOCATION (typefind->sink);
265 gst_element_add_pad (GST_ELEMENT (typefind), typefind->sink);
269 gst_pad_new_from_static_template (&type_find_element_src_template, "src");
271 gst_pad_set_activatemode_function (typefind->src,
272 GST_DEBUG_FUNCPTR (gst_type_find_element_activate_src_mode));
273 gst_pad_set_getrange_function (typefind->src,
274 GST_DEBUG_FUNCPTR (gst_type_find_element_getrange));
275 gst_pad_set_event_function (typefind->src,
276 GST_DEBUG_FUNCPTR (gst_type_find_element_src_event));
277 gst_pad_set_query_function (typefind->src,
278 GST_DEBUG_FUNCPTR (gst_type_find_handle_src_query));
279 gst_pad_use_fixed_caps (typefind->src);
280 gst_element_add_pad (GST_ELEMENT (typefind), typefind->src);
282 typefind->mode = MODE_TYPEFIND;
283 typefind->caps = NULL;
284 typefind->min_probability = 1;
286 typefind->adapter = gst_adapter_new ();
290 gst_type_find_element_dispose (GObject * object)
292 GstTypeFindElement *typefind = GST_TYPE_FIND_ELEMENT (object);
294 if (typefind->adapter) {
295 g_object_unref (typefind->adapter);
296 typefind->adapter = NULL;
299 if (typefind->force_caps) {
300 gst_caps_unref (typefind->force_caps);
301 typefind->force_caps = NULL;
304 G_OBJECT_CLASS (parent_class)->dispose (object);
308 gst_type_find_element_set_property (GObject * object, guint prop_id,
309 const GValue * value, GParamSpec * pspec)
311 GstTypeFindElement *typefind;
313 typefind = GST_TYPE_FIND_ELEMENT (object);
317 typefind->min_probability = g_value_get_uint (value);
319 case PROP_FORCE_CAPS:
320 GST_OBJECT_LOCK (typefind);
321 if (typefind->force_caps)
322 gst_caps_unref (typefind->force_caps);
323 typefind->force_caps = g_value_dup_boxed (value);
324 GST_OBJECT_UNLOCK (typefind);
327 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
333 gst_type_find_element_get_property (GObject * object, guint prop_id,
334 GValue * value, GParamSpec * pspec)
336 GstTypeFindElement *typefind;
338 typefind = GST_TYPE_FIND_ELEMENT (object);
342 GST_OBJECT_LOCK (typefind);
343 g_value_set_boxed (value, typefind->caps);
344 GST_OBJECT_UNLOCK (typefind);
347 g_value_set_uint (value, typefind->min_probability);
349 case PROP_FORCE_CAPS:
350 GST_OBJECT_LOCK (typefind);
351 g_value_set_boxed (value, typefind->force_caps);
352 GST_OBJECT_UNLOCK (typefind);
355 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
361 gst_type_find_handle_src_query (GstPad * pad, GstObject * parent,
364 GstTypeFindElement *typefind;
365 gboolean res = FALSE;
367 typefind = GST_TYPE_FIND_ELEMENT (parent);
368 GST_DEBUG_OBJECT (typefind, "Handling src query %s",
369 GST_QUERY_TYPE_NAME (query));
371 switch (GST_QUERY_TYPE (query)) {
372 case GST_QUERY_SCHEDULING:
373 /* FIXME, filter out the scheduling modes that we understand */
374 res = gst_pad_peer_query (typefind->sink, query);
378 GST_DEBUG_OBJECT (typefind,
379 "Got caps query, our caps are %" GST_PTR_FORMAT, typefind->caps);
381 /* We can hijack caps query if we typefind already */
382 if (typefind->caps) {
383 gst_query_set_caps_result (query, typefind->caps);
386 res = gst_pad_peer_query (typefind->sink, query);
390 case GST_QUERY_POSITION:
395 if (!(res = gst_pad_peer_query (typefind->sink, query)))
398 gst_query_parse_position (query, &format, &peer_pos);
400 GST_OBJECT_LOCK (typefind);
401 /* FIXME: this code assumes that there's no discont in the queue */
403 case GST_FORMAT_BYTES:
404 peer_pos -= gst_adapter_available (typefind->adapter);
410 GST_OBJECT_UNLOCK (typefind);
411 gst_query_set_position (query, format, peer_pos);
415 res = gst_pad_query_default (pad, parent, query);
423 gst_type_find_element_seek (GstTypeFindElement * typefind, GstEvent * event)
426 GstSeekType start_type, stop_type;
431 GstSegment seeksegment = { 0, };
433 gst_event_parse_seek (event, &rate, &format, &flags, &start_type, &start,
436 /* we can only seek on bytes */
437 if (format != GST_FORMAT_BYTES) {
438 GST_DEBUG_OBJECT (typefind, "Can only seek on BYTES");
442 /* copy segment, we need this because we still need the old
443 * segment when we close the current segment. */
444 memcpy (&seeksegment, &typefind->segment, sizeof (GstSegment));
446 GST_DEBUG_OBJECT (typefind, "configuring seek");
447 gst_segment_do_seek (&seeksegment, rate, format, flags,
448 start_type, start, stop_type, stop, NULL);
450 flush = ! !(flags & GST_SEEK_FLAG_FLUSH);
452 GST_DEBUG_OBJECT (typefind, "New segment %" GST_SEGMENT_FORMAT, &seeksegment);
455 GST_DEBUG_OBJECT (typefind, "Starting flush");
456 gst_pad_push_event (typefind->sink, gst_event_new_flush_start ());
457 gst_pad_push_event (typefind->src, gst_event_new_flush_start ());
459 GST_DEBUG_OBJECT (typefind, "Non-flushing seek, pausing task");
460 gst_pad_pause_task (typefind->sink);
463 /* now grab the stream lock so that streaming cannot continue, for
464 * non flushing seeks when the element is in PAUSED this could block
466 GST_DEBUG_OBJECT (typefind, "Waiting for streaming to stop");
467 GST_PAD_STREAM_LOCK (typefind->sink);
470 GST_DEBUG_OBJECT (typefind, "Stopping flush");
471 gst_pad_push_event (typefind->sink, gst_event_new_flush_stop (TRUE));
472 gst_pad_push_event (typefind->src, gst_event_new_flush_stop (TRUE));
475 /* now update the real segment info */
476 GST_DEBUG_OBJECT (typefind, "Committing new seek segment");
477 memcpy (&typefind->segment, &seeksegment, sizeof (GstSegment));
478 typefind->offset = typefind->segment.start;
480 /* notify start of new segment */
481 if (typefind->segment.flags & GST_SEGMENT_FLAG_SEGMENT) {
484 msg = gst_message_new_segment_start (GST_OBJECT (typefind),
485 GST_FORMAT_BYTES, typefind->segment.start);
486 gst_element_post_message (GST_ELEMENT (typefind), msg);
489 typefind->need_segment = TRUE;
491 /* restart our task since it might have been stopped when we did the
493 gst_pad_start_task (typefind->sink,
494 (GstTaskFunction) gst_type_find_element_loop, typefind->sink, NULL);
496 /* streaming can continue now */
497 GST_PAD_STREAM_UNLOCK (typefind->sink);
503 gst_type_find_element_src_event (GstPad * pad, GstObject * parent,
506 GstTypeFindElement *typefind = GST_TYPE_FIND_ELEMENT (parent);
508 if (typefind->mode != MODE_NORMAL) {
509 /* need to do more? */
510 gst_mini_object_unref (GST_MINI_OBJECT_CAST (event));
514 /* Only handle seeks here if driving the pipeline */
515 if (typefind->segment.format != GST_FORMAT_UNDEFINED &&
516 GST_EVENT_TYPE (event) == GST_EVENT_SEEK) {
517 return gst_type_find_element_seek (typefind, event);
519 return gst_pad_push_event (typefind->sink, event);
524 start_typefinding (GstTypeFindElement * typefind)
526 GST_DEBUG_OBJECT (typefind, "starting typefinding");
528 GST_OBJECT_LOCK (typefind);
530 gst_caps_replace (&typefind->caps, NULL);
531 GST_OBJECT_UNLOCK (typefind);
533 typefind->mode = MODE_TYPEFIND;
537 stop_typefinding (GstTypeFindElement * typefind)
540 gboolean push_cached_buffers;
544 gst_element_get_state (GST_ELEMENT (typefind), &state, NULL, 0);
546 push_cached_buffers = (state >= GST_STATE_PAUSED);
548 GST_DEBUG_OBJECT (typefind, "stopping typefinding%s",
549 push_cached_buffers ? " and pushing cached buffers" : "");
551 GST_OBJECT_LOCK (typefind);
552 avail = gst_adapter_available (typefind->adapter);
556 buffer = gst_adapter_take_buffer (typefind->adapter, avail);
557 GST_OBJECT_UNLOCK (typefind);
559 if (!push_cached_buffers) {
560 gst_buffer_unref (buffer);
562 GstPad *peer = gst_pad_get_peer (typefind->src);
564 typefind->mode = MODE_NORMAL;
566 /* make sure the user gets a meaningful error message in this case,
567 * which is not a core bug or bug of any kind (as the default error
568 * message emitted by gstpad.c otherwise would make you think) */
569 if (peer && GST_PAD_CHAINFUNC (peer) == NULL) {
570 GST_DEBUG_OBJECT (typefind, "upstream only supports push mode, while "
571 "downstream element only works in pull mode, erroring out");
572 GST_ELEMENT_ERROR (typefind, STREAM, FAILED,
573 ("%s cannot work in push mode. The operation is not supported "
574 "with this source element or protocol.",
575 G_OBJECT_TYPE_NAME (GST_PAD_PARENT (peer))),
576 ("Downstream pad %s:%s has no chainfunction, and the upstream "
577 "element does not support pull mode", GST_DEBUG_PAD_NAME (peer)));
578 typefind->mode = MODE_ERROR; /* make the chain function error out */
579 gst_buffer_unref (buffer);
581 gst_type_find_element_send_cached_events (typefind);
582 gst_pad_push (typefind->src, buffer);
585 gst_object_unref (peer);
592 GST_DEBUG_OBJECT (typefind, "we have no data to typefind");
593 GST_OBJECT_UNLOCK (typefind);
599 gst_type_find_element_sink_event (GstPad * pad, GstObject * parent,
602 gboolean res = FALSE;
603 GstTypeFindElement *typefind = GST_TYPE_FIND_ELEMENT (parent);
605 GST_DEBUG_OBJECT (typefind, "got %s event in mode %d",
606 GST_EVENT_TYPE_NAME (event), typefind->mode);
608 switch (typefind->mode) {
610 switch (GST_EVENT_TYPE (event)) {
615 /* first pass the caps event downstream */
616 res = gst_pad_push_event (typefind->src, gst_event_ref (event));
618 /* then parse and push out our data */
619 gst_event_parse_caps (event, &caps);
620 res = gst_type_find_element_setcaps (typefind, caps);
622 gst_event_unref (event);
627 GST_INFO_OBJECT (typefind, "Got EOS and no type found yet");
628 gst_type_find_element_chain_do_typefinding (typefind, FALSE);
630 res = gst_pad_push_event (typefind->src, event);
633 case GST_EVENT_FLUSH_STOP:
634 GST_OBJECT_LOCK (typefind);
635 g_list_foreach (typefind->cached_events,
636 (GFunc) gst_mini_object_unref, NULL);
637 g_list_free (typefind->cached_events);
638 typefind->cached_events = NULL;
639 gst_adapter_clear (typefind->adapter);
640 GST_OBJECT_UNLOCK (typefind);
642 case GST_EVENT_FLUSH_START:
643 res = gst_pad_push_event (typefind->src, event);
646 GST_DEBUG_OBJECT (typefind, "Saving %s event to send later",
647 GST_EVENT_TYPE_NAME (event));
648 GST_OBJECT_LOCK (typefind);
649 typefind->cached_events =
650 g_list_append (typefind->cached_events, event);
651 GST_OBJECT_UNLOCK (typefind);
657 res = gst_pad_push_event (typefind->src, event);
662 g_assert_not_reached ();
668 gst_type_find_element_send_cached_events (GstTypeFindElement * typefind)
670 GList *l, *cached_events;
672 GST_OBJECT_LOCK (typefind);
673 cached_events = typefind->cached_events;
674 typefind->cached_events = NULL;
675 GST_OBJECT_UNLOCK (typefind);
677 for (l = cached_events; l != NULL; l = l->next) {
678 GstEvent *event = GST_EVENT (l->data);
680 GST_DEBUG_OBJECT (typefind, "sending cached %s event",
681 GST_EVENT_TYPE_NAME (event));
682 gst_pad_push_event (typefind->src, event);
684 g_list_free (cached_events);
688 gst_type_find_element_setcaps (GstTypeFindElement * typefind, GstCaps * caps)
690 /* don't operate on ANY caps */
691 if (gst_caps_is_any (caps))
694 g_signal_emit (typefind, gst_type_find_element_signals[HAVE_TYPE], 0,
695 GST_TYPE_FIND_MAXIMUM, caps);
697 /* Shortcircuit typefinding if we get caps */
698 if (typefind->mode == MODE_TYPEFIND) {
702 GST_DEBUG_OBJECT (typefind, "Skipping typefinding, using caps from "
703 "upstream buffer: %" GST_PTR_FORMAT, caps);
704 typefind->mode = MODE_NORMAL;
706 gst_type_find_element_send_cached_events (typefind);
707 GST_OBJECT_LOCK (typefind);
708 avail = gst_adapter_available (typefind->adapter);
712 buffer = gst_adapter_take_buffer (typefind->adapter, avail);
713 GST_OBJECT_UNLOCK (typefind);
715 GST_DEBUG_OBJECT (typefind, "Pushing buffer: %" G_GSIZE_FORMAT, avail);
716 gst_pad_push (typefind->src, buffer);
723 GST_DEBUG_OBJECT (typefind, "no data to push");
724 GST_OBJECT_UNLOCK (typefind);
730 gst_type_find_get_extension (GstTypeFindElement * typefind, GstPad * pad)
737 query = gst_query_new_uri ();
739 /* try getting the caps with an uri query and from the extension */
740 if (!gst_pad_peer_query (pad, query))
741 goto peer_query_failed;
743 gst_query_parse_uri (query, &uri);
747 GST_DEBUG_OBJECT (typefind, "finding extension of %s", uri);
749 /* find the extension on the uri, this is everything after a '.' */
754 if (uri[find] == '.')
761 result = g_strdup (&uri[find + 1]);
763 GST_DEBUG_OBJECT (typefind, "found extension %s", result);
764 gst_query_unref (query);
772 GST_WARNING_OBJECT (typefind, "failed to query peer uri");
773 gst_query_unref (query);
778 GST_WARNING_OBJECT (typefind, "could not parse the peer uri");
779 gst_query_unref (query);
784 GST_WARNING_OBJECT (typefind, "could not find uri extension in %s", uri);
785 gst_query_unref (query);
792 gst_type_find_guess_by_extension (GstTypeFindElement * typefind, GstPad * pad,
793 GstTypeFindProbability * probability)
798 ext = gst_type_find_get_extension (typefind, pad);
802 caps = gst_type_find_helper_for_extension (GST_OBJECT_CAST (typefind), ext);
804 *probability = GST_TYPE_FIND_MAXIMUM;
812 gst_type_find_element_chain (GstPad * pad, GstObject * parent,
815 GstTypeFindElement *typefind;
816 GstFlowReturn res = GST_FLOW_OK;
818 typefind = GST_TYPE_FIND_ELEMENT (parent);
820 GST_LOG_OBJECT (typefind, "handling buffer in mode %d", typefind->mode);
822 switch (typefind->mode) {
824 /* we should already have called GST_ELEMENT_ERROR */
825 return GST_FLOW_ERROR;
827 /* don't take object lock as typefind->caps should not change anymore */
828 return gst_pad_push (typefind->src, buffer);
831 GST_OBJECT_LOCK (typefind);
832 gst_adapter_push (typefind->adapter, buffer);
833 GST_OBJECT_UNLOCK (typefind);
835 res = gst_type_find_element_chain_do_typefinding (typefind, TRUE);
837 if (typefind->mode == MODE_ERROR)
838 res = GST_FLOW_ERROR;
843 g_assert_not_reached ();
844 return GST_FLOW_ERROR;
851 gst_type_find_element_chain_do_typefinding (GstTypeFindElement * typefind,
852 gboolean check_avail)
854 GstTypeFindProbability probability;
858 gboolean have_min, have_max;
860 GST_OBJECT_LOCK (typefind);
861 avail = gst_adapter_available (typefind->adapter);
864 have_min = avail >= TYPE_FIND_MIN_SIZE;
865 have_max = avail >= TYPE_FIND_MAX_SIZE;
867 have_min = avail > 0;
872 goto not_enough_data;
874 /* map all available data */
875 data = gst_adapter_map (typefind->adapter, avail);
876 caps = gst_type_find_helper_for_data (GST_OBJECT (typefind),
877 data, avail, &probability);
878 gst_adapter_unmap (typefind->adapter);
880 if (caps == NULL && have_max)
882 else if (caps == NULL)
886 if (probability < typefind->min_probability)
887 goto low_probability;
888 GST_OBJECT_UNLOCK (typefind);
890 /* probability is good enough too, so let's make it known ... emiting this
891 * signal calls our object handler which sets the caps. */
892 g_signal_emit (typefind, gst_type_find_element_signals[HAVE_TYPE], 0,
895 /* .. and send out the accumulated data */
896 stop_typefinding (typefind);
897 gst_caps_unref (caps);
903 GST_DEBUG_OBJECT (typefind, "not enough data for typefinding yet "
904 "(%" G_GSIZE_FORMAT " bytes)", avail);
905 GST_OBJECT_UNLOCK (typefind);
910 GST_OBJECT_UNLOCK (typefind);
911 GST_ELEMENT_ERROR (typefind, STREAM, TYPE_NOT_FOUND, (NULL), (NULL));
912 stop_typefinding (typefind);
913 return GST_FLOW_ERROR;
917 GST_DEBUG_OBJECT (typefind,
918 "no caps found with %" G_GSIZE_FORMAT " bytes of data, "
919 "waiting for more data", avail);
920 GST_OBJECT_UNLOCK (typefind);
925 GST_DEBUG_OBJECT (typefind, "found caps %" GST_PTR_FORMAT ", but "
926 "probability is %u which is lower than the required minimum of %u",
927 caps, probability, typefind->min_probability);
929 gst_caps_unref (caps);
934 GST_OBJECT_UNLOCK (typefind);
935 GST_DEBUG_OBJECT (typefind, "waiting for more data to try again");
941 gst_type_find_element_getrange (GstPad * srcpad, GstObject * parent,
942 guint64 offset, guint length, GstBuffer ** buffer)
944 GstTypeFindElement *typefind;
947 typefind = GST_TYPE_FIND_ELEMENT (parent);
949 ret = gst_pad_pull_range (typefind->sink, offset, length, buffer);
955 gst_type_find_element_activate_src_mode (GstPad * pad, GstObject * parent,
956 GstPadMode mode, gboolean active)
959 GstTypeFindElement *typefind;
961 typefind = GST_TYPE_FIND_ELEMENT (parent);
964 case GST_PAD_MODE_PULL:
965 /* make sure our task stops pushing, we can't call _stop here because this
966 * activation might happen from the streaming thread. */
967 gst_pad_pause_task (typefind->sink);
968 res = gst_pad_activate_mode (typefind->sink, mode, active);
969 if (active && res && typefind->caps) {
971 GST_OBJECT_LOCK (typefind);
972 caps = gst_caps_ref (typefind->caps);
973 GST_OBJECT_UNLOCK (typefind);
974 res = gst_pad_set_caps (typefind->src, caps);
975 gst_caps_unref (caps);
986 gst_type_find_element_loop (GstPad * pad)
988 GstTypeFindElement *typefind;
989 GstFlowReturn ret = GST_FLOW_OK;
991 typefind = GST_TYPE_FIND_ELEMENT (GST_PAD_PARENT (pad));
993 if (typefind->mode == MODE_TYPEFIND) {
995 GstCaps *found_caps = NULL;
996 GstTypeFindProbability probability = GST_TYPE_FIND_NONE;
998 GST_DEBUG_OBJECT (typefind, "find type in pull mode");
1000 peer = gst_pad_get_peer (pad);
1005 if (!gst_pad_query_duration (peer, GST_FORMAT_BYTES, &size)) {
1006 GST_WARNING_OBJECT (typefind, "Could not query upstream length!");
1007 gst_object_unref (peer);
1009 ret = GST_FLOW_ERROR;
1013 /* the size if 0, we cannot continue */
1015 /* keep message in sync with message in sink event handler */
1016 GST_ELEMENT_ERROR (typefind, STREAM, TYPE_NOT_FOUND,
1017 (_("Stream contains no data.")), ("Can't typefind empty stream"));
1018 gst_object_unref (peer);
1019 ret = GST_FLOW_ERROR;
1022 ext = gst_type_find_get_extension (typefind, pad);
1025 gst_type_find_helper_get_range (GST_OBJECT_CAST (peer),
1026 GST_OBJECT_PARENT (peer),
1027 (GstTypeFindHelperGetRangeFunction) (GST_PAD_GETRANGEFUNC (peer)),
1028 (guint64) size, ext, &probability);
1031 GST_DEBUG ("Found caps %" GST_PTR_FORMAT, found_caps);
1033 gst_object_unref (peer);
1036 if (!found_caps || probability < typefind->min_probability) {
1037 GST_DEBUG ("Trying to guess using extension");
1038 gst_caps_replace (&found_caps, NULL);
1040 gst_type_find_guess_by_extension (typefind, pad, &probability);
1043 if (!found_caps || probability < typefind->min_probability) {
1044 GST_ELEMENT_ERROR (typefind, STREAM, TYPE_NOT_FOUND, (NULL), (NULL));
1045 gst_caps_replace (&found_caps, NULL);
1046 ret = GST_FLOW_ERROR;
1050 GST_DEBUG ("Emiting found caps %" GST_PTR_FORMAT, found_caps);
1051 g_signal_emit (typefind, gst_type_find_element_signals[HAVE_TYPE],
1052 0, probability, found_caps);
1053 typefind->mode = MODE_NORMAL;
1054 gst_caps_unref (found_caps);
1055 } else if (typefind->mode == MODE_NORMAL) {
1056 GstBuffer *outbuf = NULL;
1058 if (typefind->need_segment) {
1059 typefind->need_segment = FALSE;
1060 gst_pad_push_event (typefind->src,
1061 gst_event_new_segment (&typefind->segment));
1064 /* Pull 4k blocks and send downstream */
1065 ret = gst_pad_pull_range (typefind->sink, typefind->offset, 4096, &outbuf);
1066 if (ret != GST_FLOW_OK)
1069 typefind->offset += 4096;
1071 ret = gst_pad_push (typefind->src, outbuf);
1072 if (ret != GST_FLOW_OK)
1076 ret = GST_FLOW_ERROR;
1084 const gchar *reason = gst_flow_get_name (ret);
1085 gboolean push_eos = FALSE;
1087 GST_LOG_OBJECT (typefind, "pausing task, reason %s", reason);
1088 gst_pad_pause_task (typefind->sink);
1090 if (ret == GST_FLOW_EOS) {
1091 /* perform EOS logic */
1093 if (typefind->segment.flags & GST_SEGMENT_FLAG_SEGMENT) {
1096 /* for segment playback we need to post when (in stream time)
1097 * we stopped, this is either stop (when set) or the duration. */
1098 if ((stop = typefind->segment.stop) == -1)
1099 stop = typefind->offset;
1101 GST_LOG_OBJECT (typefind, "Sending segment done, at end of segment");
1102 gst_element_post_message (GST_ELEMENT (typefind),
1103 gst_message_new_segment_done (GST_OBJECT (typefind),
1104 GST_FORMAT_BYTES, stop));
1105 gst_pad_push_event (typefind->src,
1106 gst_event_new_segment_done (GST_FORMAT_BYTES, stop));
1110 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
1111 /* for fatal errors we post an error message */
1112 GST_ELEMENT_ERROR (typefind, STREAM, FAILED, (NULL),
1113 ("stream stopped, reason %s", reason));
1117 /* send EOS, and prevent hanging if no streams yet */
1118 GST_LOG_OBJECT (typefind, "Sending EOS, at end of stream");
1119 gst_pad_push_event (typefind->src, gst_event_new_eos ());
1126 gst_type_find_element_activate_sink_mode (GstPad * pad, GstObject * parent,
1127 GstPadMode mode, gboolean active)
1130 GstTypeFindElement *typefind;
1132 typefind = GST_TYPE_FIND_ELEMENT (parent);
1135 case GST_PAD_MODE_PULL:
1137 gst_segment_init (&typefind->segment, GST_FORMAT_BYTES);
1138 typefind->need_segment = TRUE;
1139 typefind->offset = 0;
1142 res = gst_pad_stop_task (pad);
1145 case GST_PAD_MODE_PUSH:
1147 start_typefinding (typefind);
1149 stop_typefinding (typefind);
1161 gst_type_find_element_activate_sink (GstPad * pad, GstObject * parent)
1163 GstTypeFindElement *typefind;
1166 GstCaps *found_caps = NULL;
1167 GstTypeFindProbability probability = GST_TYPE_FIND_NONE;
1169 typefind = GST_TYPE_FIND_ELEMENT (parent);
1171 /* if we have force caps, use those */
1172 GST_OBJECT_LOCK (typefind);
1173 if (typefind->force_caps) {
1174 found_caps = gst_caps_ref (typefind->force_caps);
1175 probability = GST_TYPE_FIND_MAXIMUM;
1176 GST_OBJECT_UNLOCK (typefind);
1178 GST_DEBUG ("Emiting found caps %" GST_PTR_FORMAT, found_caps);
1179 g_signal_emit (typefind, gst_type_find_element_signals[HAVE_TYPE],
1180 0, probability, found_caps);
1181 typefind->mode = MODE_NORMAL;
1182 /* the signal above could have made a downstream element activate
1183 * the pad in pull mode, we check if the pad is already active now and if
1184 * so, we are done */
1185 if (gst_pad_is_active (pad))
1190 GST_OBJECT_UNLOCK (typefind);
1192 query = gst_query_new_scheduling ();
1194 if (!gst_pad_peer_query (pad, query)) {
1195 gst_query_unref (query);
1199 pull_mode = gst_query_has_scheduling_mode (query, GST_PAD_MODE_PULL);
1200 gst_query_unref (query);
1205 if (!gst_pad_activate_mode (pad, GST_PAD_MODE_PULL, TRUE))
1208 /* only start our task if we ourselves decide to start in pull mode */
1209 return gst_pad_start_task (pad, (GstTaskFunction) gst_type_find_element_loop,
1214 return gst_pad_activate_mode (pad, GST_PAD_MODE_PUSH, TRUE);
1218 static GstStateChangeReturn
1219 gst_type_find_element_change_state (GstElement * element,
1220 GstStateChange transition)
1222 GstStateChangeReturn ret;
1223 GstTypeFindElement *typefind;
1225 typefind = GST_TYPE_FIND_ELEMENT (element);
1228 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1230 switch (transition) {
1231 case GST_STATE_CHANGE_PAUSED_TO_READY:
1232 case GST_STATE_CHANGE_READY_TO_NULL:
1233 GST_OBJECT_LOCK (typefind);
1234 gst_caps_replace (&typefind->caps, NULL);
1236 g_list_foreach (typefind->cached_events,
1237 (GFunc) gst_mini_object_unref, NULL);
1238 g_list_free (typefind->cached_events);
1239 typefind->cached_events = NULL;
1240 typefind->mode = MODE_TYPEFIND;
1241 GST_OBJECT_UNLOCK (typefind);