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
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 */
120 MODE_NORMAL, /* act as identity */
121 MODE_TYPEFIND, /* do typefinding */
122 MODE_ERROR /* had fatal error */
127 GST_DEBUG_CATEGORY_INIT (gst_type_find_element_debug, "typefind", \
128 GST_DEBUG_BG_YELLOW | GST_DEBUG_FG_GREEN, "type finding element");
129 #define gst_type_find_element_parent_class parent_class
130 G_DEFINE_TYPE_WITH_CODE (GstTypeFindElement, gst_type_find_element,
131 GST_TYPE_ELEMENT, _do_init);
133 static void gst_type_find_element_dispose (GObject * object);
134 static void gst_type_find_element_set_property (GObject * object,
135 guint prop_id, const GValue * value, GParamSpec * pspec);
136 static void gst_type_find_element_get_property (GObject * object,
137 guint prop_id, GValue * value, GParamSpec * pspec);
140 static const GstEventMask *gst_type_find_element_src_event_mask (GstPad * pad);
143 static gboolean gst_type_find_element_src_event (GstPad * pad,
144 GstObject * parent, GstEvent * event);
145 static gboolean gst_type_find_handle_src_query (GstPad * pad,
146 GstObject * parent, GstQuery * query);
148 static gboolean gst_type_find_element_sink_event (GstPad * pad,
149 GstObject * parent, GstEvent * event);
150 static gboolean gst_type_find_element_setcaps (GstTypeFindElement * typefind,
152 static GstFlowReturn gst_type_find_element_chain (GstPad * sinkpad,
153 GstObject * parent, GstBuffer * buffer);
154 static GstFlowReturn gst_type_find_element_getrange (GstPad * srcpad,
155 GstObject * parent, guint64 offset, guint length, GstBuffer ** buffer);
157 static GstStateChangeReturn
158 gst_type_find_element_change_state (GstElement * element,
159 GstStateChange transition);
160 static gboolean gst_type_find_element_activate_sink (GstPad * pad,
162 static gboolean gst_type_find_element_activate_sink_mode (GstPad * pad,
163 GstObject * parent, GstPadMode mode, gboolean active);
164 static gboolean gst_type_find_element_activate_src_mode (GstPad * pad,
165 GstObject * parent, GstPadMode mode, gboolean active);
167 gst_type_find_element_chain_do_typefinding (GstTypeFindElement * typefind,
168 gboolean check_avail);
169 static void gst_type_find_element_send_cached_events (GstTypeFindElement *
172 static void gst_type_find_element_loop (GstPad * pad);
174 static guint gst_type_find_element_signals[LAST_SIGNAL] = { 0 };
177 gst_type_find_element_have_type (GstTypeFindElement * typefind,
178 guint probability, GstCaps * caps)
180 g_assert (caps != NULL);
182 GST_INFO_OBJECT (typefind, "found caps %" GST_PTR_FORMAT ", probability=%u",
185 GST_OBJECT_LOCK (typefind);
187 gst_caps_unref (typefind->caps);
188 typefind->caps = gst_caps_ref (caps);
189 GST_OBJECT_UNLOCK (typefind);
191 gst_pad_set_caps (typefind->src, caps);
195 gst_type_find_element_class_init (GstTypeFindElementClass * typefind_class)
197 GObjectClass *gobject_class = G_OBJECT_CLASS (typefind_class);
198 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (typefind_class);
200 gobject_class->set_property = gst_type_find_element_set_property;
201 gobject_class->get_property = gst_type_find_element_get_property;
202 gobject_class->dispose = gst_type_find_element_dispose;
204 g_object_class_install_property (gobject_class, PROP_CAPS,
205 g_param_spec_boxed ("caps", _("caps"),
206 _("detected capabilities in stream"), GST_TYPE_CAPS,
207 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
208 g_object_class_install_property (gobject_class, PROP_MINIMUM,
209 g_param_spec_uint ("minimum", _("minimum"),
210 "minimum probability required to accept caps", GST_TYPE_FIND_MINIMUM,
211 GST_TYPE_FIND_MAXIMUM, GST_TYPE_FIND_MINIMUM,
212 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
213 g_object_class_install_property (gobject_class, PROP_MAXIMUM,
214 g_param_spec_uint ("maximum", _("maximum"),
215 "probability to stop typefinding (deprecated; non-functional)",
216 GST_TYPE_FIND_MINIMUM, GST_TYPE_FIND_MAXIMUM, GST_TYPE_FIND_MAXIMUM,
217 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
218 g_object_class_install_property (gobject_class, PROP_FORCE_CAPS,
219 g_param_spec_boxed ("force-caps", _("force caps"),
220 _("force caps without doing a typefind"), GST_TYPE_CAPS,
221 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
223 * GstTypeFindElement::have-type:
224 * @typefind: the typefind instance
225 * @probability: the probability of the type found
226 * @caps: the caps of the type found
228 * This signal gets emitted when the type and its probability has
231 gst_type_find_element_signals[HAVE_TYPE] = g_signal_new ("have-type",
232 G_TYPE_FROM_CLASS (typefind_class), G_SIGNAL_RUN_FIRST,
233 G_STRUCT_OFFSET (GstTypeFindElementClass, have_type), NULL, NULL,
234 g_cclosure_marshal_generic, G_TYPE_NONE, 2,
235 G_TYPE_UINT, GST_TYPE_CAPS | G_SIGNAL_TYPE_STATIC_SCOPE);
237 typefind_class->have_type =
238 GST_DEBUG_FUNCPTR (gst_type_find_element_have_type);
240 gst_element_class_set_static_metadata (gstelement_class,
243 "Finds the media type of a stream",
244 "Benjamin Otte <in7y118@public.uni-hamburg.de>");
245 gst_element_class_add_pad_template (gstelement_class,
246 gst_static_pad_template_get (&type_find_element_src_template));
247 gst_element_class_add_pad_template (gstelement_class,
248 gst_static_pad_template_get (&type_find_element_sink_template));
250 gstelement_class->change_state =
251 GST_DEBUG_FUNCPTR (gst_type_find_element_change_state);
255 gst_type_find_element_init (GstTypeFindElement * typefind)
259 gst_pad_new_from_static_template (&type_find_element_sink_template,
262 gst_pad_set_activate_function (typefind->sink,
263 GST_DEBUG_FUNCPTR (gst_type_find_element_activate_sink));
264 gst_pad_set_activatemode_function (typefind->sink,
265 GST_DEBUG_FUNCPTR (gst_type_find_element_activate_sink_mode));
266 gst_pad_set_chain_function (typefind->sink,
267 GST_DEBUG_FUNCPTR (gst_type_find_element_chain));
268 gst_pad_set_event_function (typefind->sink,
269 GST_DEBUG_FUNCPTR (gst_type_find_element_sink_event));
270 GST_PAD_SET_PROXY_ALLOCATION (typefind->sink);
271 gst_element_add_pad (GST_ELEMENT (typefind), typefind->sink);
275 gst_pad_new_from_static_template (&type_find_element_src_template, "src");
277 gst_pad_set_activatemode_function (typefind->src,
278 GST_DEBUG_FUNCPTR (gst_type_find_element_activate_src_mode));
279 gst_pad_set_getrange_function (typefind->src,
280 GST_DEBUG_FUNCPTR (gst_type_find_element_getrange));
281 gst_pad_set_event_function (typefind->src,
282 GST_DEBUG_FUNCPTR (gst_type_find_element_src_event));
283 gst_pad_set_query_function (typefind->src,
284 GST_DEBUG_FUNCPTR (gst_type_find_handle_src_query));
285 gst_pad_use_fixed_caps (typefind->src);
286 gst_element_add_pad (GST_ELEMENT (typefind), typefind->src);
288 typefind->mode = MODE_TYPEFIND;
289 typefind->caps = NULL;
290 typefind->min_probability = 1;
291 typefind->max_probability = GST_TYPE_FIND_MAXIMUM;
293 typefind->adapter = gst_adapter_new ();
297 gst_type_find_element_dispose (GObject * object)
299 GstTypeFindElement *typefind = GST_TYPE_FIND_ELEMENT (object);
301 if (typefind->adapter) {
302 g_object_unref (typefind->adapter);
303 typefind->adapter = NULL;
306 if (typefind->force_caps) {
307 gst_caps_unref (typefind->force_caps);
308 typefind->force_caps = NULL;
311 G_OBJECT_CLASS (parent_class)->dispose (object);
315 gst_type_find_element_set_property (GObject * object, guint prop_id,
316 const GValue * value, GParamSpec * pspec)
318 GstTypeFindElement *typefind;
320 typefind = GST_TYPE_FIND_ELEMENT (object);
324 typefind->min_probability = g_value_get_uint (value);
327 typefind->max_probability = g_value_get_uint (value);
329 case PROP_FORCE_CAPS:
330 GST_OBJECT_LOCK (typefind);
331 if (typefind->force_caps)
332 gst_caps_unref (typefind->force_caps);
333 typefind->force_caps = g_value_dup_boxed (value);
334 GST_OBJECT_UNLOCK (typefind);
337 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
343 gst_type_find_element_get_property (GObject * object, guint prop_id,
344 GValue * value, GParamSpec * pspec)
346 GstTypeFindElement *typefind;
348 typefind = GST_TYPE_FIND_ELEMENT (object);
352 GST_OBJECT_LOCK (typefind);
353 g_value_set_boxed (value, typefind->caps);
354 GST_OBJECT_UNLOCK (typefind);
357 g_value_set_uint (value, typefind->min_probability);
360 g_value_set_uint (value, typefind->max_probability);
362 case PROP_FORCE_CAPS:
363 GST_OBJECT_LOCK (typefind);
364 g_value_set_boxed (value, typefind->force_caps);
365 GST_OBJECT_UNLOCK (typefind);
368 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
374 gst_type_find_handle_src_query (GstPad * pad, GstObject * parent,
377 GstTypeFindElement *typefind;
378 gboolean res = FALSE;
380 typefind = GST_TYPE_FIND_ELEMENT (parent);
381 GST_DEBUG_OBJECT (typefind, "Handling src query %s",
382 GST_QUERY_TYPE_NAME (query));
384 switch (GST_QUERY_TYPE (query)) {
385 case GST_QUERY_SCHEDULING:
386 /* FIXME, filter out the scheduling modes that we understand */
387 res = gst_pad_peer_query (typefind->sink, query);
391 GST_DEBUG_OBJECT (typefind,
392 "Got caps query, our caps are %" GST_PTR_FORMAT, typefind->caps);
394 /* We can hijack caps query if we typefind already */
395 if (typefind->caps) {
396 gst_query_set_caps_result (query, typefind->caps);
399 res = gst_pad_peer_query (typefind->sink, query);
403 case GST_QUERY_POSITION:
408 if (!(res = gst_pad_peer_query (typefind->sink, query)))
411 gst_query_parse_position (query, &format, &peer_pos);
413 GST_OBJECT_LOCK (typefind);
414 /* FIXME: this code assumes that there's no discont in the queue */
416 case GST_FORMAT_BYTES:
417 peer_pos -= gst_adapter_available (typefind->adapter);
423 GST_OBJECT_UNLOCK (typefind);
424 gst_query_set_position (query, format, peer_pos);
428 res = gst_pad_query_default (pad, parent, query);
436 gst_type_find_element_seek (GstTypeFindElement * typefind, GstEvent * event)
439 GstSeekType cur_type, stop_type;
444 GstSegment seeksegment = { 0, };
446 gst_event_parse_seek (event, &rate, &format, &flags, &cur_type, &cur,
449 /* we can only seek on bytes */
450 if (format != GST_FORMAT_BYTES) {
451 GST_DEBUG_OBJECT (typefind, "Can only seek on BYTES");
455 /* copy segment, we need this because we still need the old
456 * segment when we close the current segment. */
457 memcpy (&seeksegment, &typefind->segment, sizeof (GstSegment));
459 GST_DEBUG_OBJECT (typefind, "configuring seek");
460 gst_segment_do_seek (&seeksegment, rate, format, flags,
461 cur_type, cur, stop_type, stop, NULL);
463 flush = ! !(flags & GST_SEEK_FLAG_FLUSH);
465 GST_DEBUG_OBJECT (typefind, "New segment %" GST_SEGMENT_FORMAT, &seeksegment);
468 GST_DEBUG_OBJECT (typefind, "Starting flush");
469 gst_pad_push_event (typefind->sink, gst_event_new_flush_start ());
470 gst_pad_push_event (typefind->src, gst_event_new_flush_start ());
472 GST_DEBUG_OBJECT (typefind, "Non-flushing seek, pausing task");
473 gst_pad_pause_task (typefind->sink);
476 /* now grab the stream lock so that streaming cannot continue, for
477 * non flushing seeks when the element is in PAUSED this could block
479 GST_DEBUG_OBJECT (typefind, "Waiting for streaming to stop");
480 GST_PAD_STREAM_LOCK (typefind->sink);
483 GST_DEBUG_OBJECT (typefind, "Stopping flush");
484 gst_pad_push_event (typefind->sink, gst_event_new_flush_stop (TRUE));
485 gst_pad_push_event (typefind->src, gst_event_new_flush_stop (TRUE));
488 /* now update the real segment info */
489 GST_DEBUG_OBJECT (typefind, "Committing new seek segment");
490 memcpy (&typefind->segment, &seeksegment, sizeof (GstSegment));
491 typefind->offset = typefind->segment.start;
493 /* notify start of new segment */
494 if (typefind->segment.flags & GST_SEEK_FLAG_SEGMENT) {
497 msg = gst_message_new_segment_start (GST_OBJECT (typefind),
498 GST_FORMAT_BYTES, typefind->segment.start);
499 gst_element_post_message (GST_ELEMENT (typefind), msg);
502 typefind->need_segment = TRUE;
504 /* restart our task since it might have been stopped when we did the
506 gst_pad_start_task (typefind->sink,
507 (GstTaskFunction) gst_type_find_element_loop, typefind->sink, NULL);
509 /* streaming can continue now */
510 GST_PAD_STREAM_UNLOCK (typefind->sink);
516 gst_type_find_element_src_event (GstPad * pad, GstObject * parent,
519 GstTypeFindElement *typefind = GST_TYPE_FIND_ELEMENT (parent);
521 if (typefind->mode != MODE_NORMAL) {
522 /* need to do more? */
523 gst_mini_object_unref (GST_MINI_OBJECT_CAST (event));
527 /* Only handle seeks here if driving the pipeline */
528 if (typefind->segment.format != GST_FORMAT_UNDEFINED &&
529 GST_EVENT_TYPE (event) == GST_EVENT_SEEK) {
530 return gst_type_find_element_seek (typefind, event);
532 return gst_pad_push_event (typefind->sink, event);
537 start_typefinding (GstTypeFindElement * typefind)
539 GST_DEBUG_OBJECT (typefind, "starting typefinding");
541 GST_OBJECT_LOCK (typefind);
543 gst_caps_replace (&typefind->caps, NULL);
544 GST_OBJECT_UNLOCK (typefind);
546 typefind->mode = MODE_TYPEFIND;
550 stop_typefinding (GstTypeFindElement * typefind)
553 gboolean push_cached_buffers;
557 gst_element_get_state (GST_ELEMENT (typefind), &state, NULL, 0);
559 push_cached_buffers = (state >= GST_STATE_PAUSED);
561 GST_DEBUG_OBJECT (typefind, "stopping typefinding%s",
562 push_cached_buffers ? " and pushing cached buffers" : "");
564 GST_OBJECT_LOCK (typefind);
565 avail = gst_adapter_available (typefind->adapter);
569 buffer = gst_adapter_take_buffer (typefind->adapter, avail);
570 GST_OBJECT_UNLOCK (typefind);
572 if (!push_cached_buffers) {
573 gst_buffer_unref (buffer);
575 GstPad *peer = gst_pad_get_peer (typefind->src);
577 typefind->mode = MODE_NORMAL;
579 /* make sure the user gets a meaningful error message in this case,
580 * which is not a core bug or bug of any kind (as the default error
581 * message emitted by gstpad.c otherwise would make you think) */
582 if (peer && GST_PAD_CHAINFUNC (peer) == NULL) {
583 GST_DEBUG_OBJECT (typefind, "upstream only supports push mode, while "
584 "downstream element only works in pull mode, erroring out");
585 GST_ELEMENT_ERROR (typefind, STREAM, FAILED,
586 ("%s cannot work in push mode. The operation is not supported "
587 "with this source element or protocol.",
588 G_OBJECT_TYPE_NAME (GST_PAD_PARENT (peer))),
589 ("Downstream pad %s:%s has no chainfunction, and the upstream "
590 "element does not support pull mode", GST_DEBUG_PAD_NAME (peer)));
591 typefind->mode = MODE_ERROR; /* make the chain function error out */
592 gst_buffer_unref (buffer);
594 gst_type_find_element_send_cached_events (typefind);
595 gst_pad_push (typefind->src, buffer);
598 gst_object_unref (peer);
605 GST_DEBUG_OBJECT (typefind, "we have no data to typefind");
606 GST_OBJECT_UNLOCK (typefind);
612 gst_type_find_element_sink_event (GstPad * pad, GstObject * parent,
615 gboolean res = FALSE;
616 GstTypeFindElement *typefind = GST_TYPE_FIND_ELEMENT (parent);
618 GST_DEBUG_OBJECT (typefind, "got %s event in mode %d",
619 GST_EVENT_TYPE_NAME (event), typefind->mode);
621 switch (typefind->mode) {
623 switch (GST_EVENT_TYPE (event)) {
628 /* first pass the caps event downstream */
629 res = gst_pad_push_event (typefind->src, gst_event_ref (event));
631 /* then parse and push out our data */
632 gst_event_parse_caps (event, &caps);
633 res = gst_type_find_element_setcaps (typefind, caps);
635 gst_event_unref (event);
640 GST_INFO_OBJECT (typefind, "Got EOS and no type found yet");
641 gst_type_find_element_chain_do_typefinding (typefind, FALSE);
643 res = gst_pad_push_event (typefind->src, event);
646 case GST_EVENT_FLUSH_STOP:
647 GST_OBJECT_LOCK (typefind);
648 g_list_foreach (typefind->cached_events,
649 (GFunc) gst_mini_object_unref, NULL);
650 g_list_free (typefind->cached_events);
651 typefind->cached_events = NULL;
652 gst_adapter_clear (typefind->adapter);
653 GST_OBJECT_UNLOCK (typefind);
655 case GST_EVENT_FLUSH_START:
656 res = gst_pad_push_event (typefind->src, event);
659 GST_DEBUG_OBJECT (typefind, "Saving %s event to send later",
660 GST_EVENT_TYPE_NAME (event));
661 GST_OBJECT_LOCK (typefind);
662 typefind->cached_events =
663 g_list_append (typefind->cached_events, event);
664 GST_OBJECT_UNLOCK (typefind);
670 res = gst_pad_push_event (typefind->src, event);
675 g_assert_not_reached ();
681 gst_type_find_element_send_cached_events (GstTypeFindElement * typefind)
683 GList *l, *cached_events;
685 GST_OBJECT_LOCK (typefind);
686 cached_events = typefind->cached_events;
687 typefind->cached_events = NULL;
688 GST_OBJECT_UNLOCK (typefind);
690 for (l = cached_events; l != NULL; l = l->next) {
691 GstEvent *event = GST_EVENT (l->data);
693 GST_DEBUG_OBJECT (typefind, "sending cached %s event",
694 GST_EVENT_TYPE_NAME (event));
695 gst_pad_push_event (typefind->src, event);
697 g_list_free (cached_events);
701 gst_type_find_element_setcaps (GstTypeFindElement * typefind, GstCaps * caps)
703 /* don't operate on ANY caps */
704 if (gst_caps_is_any (caps))
707 g_signal_emit (typefind, gst_type_find_element_signals[HAVE_TYPE], 0,
708 GST_TYPE_FIND_MAXIMUM, caps);
710 /* Shortcircuit typefinding if we get caps */
711 if (typefind->mode == MODE_TYPEFIND) {
715 GST_DEBUG_OBJECT (typefind, "Skipping typefinding, using caps from "
716 "upstream buffer: %" GST_PTR_FORMAT, caps);
717 typefind->mode = MODE_NORMAL;
719 gst_type_find_element_send_cached_events (typefind);
720 GST_OBJECT_LOCK (typefind);
721 avail = gst_adapter_available (typefind->adapter);
725 buffer = gst_adapter_take_buffer (typefind->adapter, avail);
726 GST_OBJECT_UNLOCK (typefind);
728 GST_DEBUG_OBJECT (typefind, "Pushing buffer: %" G_GSIZE_FORMAT, avail);
729 gst_pad_push (typefind->src, buffer);
736 GST_DEBUG_OBJECT (typefind, "no data to push");
737 GST_OBJECT_UNLOCK (typefind);
743 gst_type_find_get_extension (GstTypeFindElement * typefind, GstPad * pad)
750 query = gst_query_new_uri ();
752 /* try getting the caps with an uri query and from the extension */
753 if (!gst_pad_peer_query (pad, query))
754 goto peer_query_failed;
756 gst_query_parse_uri (query, &uri);
760 GST_DEBUG_OBJECT (typefind, "finding extension of %s", uri);
762 /* find the extension on the uri, this is everything after a '.' */
767 if (uri[find] == '.')
774 result = g_strdup (&uri[find + 1]);
776 GST_DEBUG_OBJECT (typefind, "found extension %s", result);
777 gst_query_unref (query);
785 GST_WARNING_OBJECT (typefind, "failed to query peer uri");
786 gst_query_unref (query);
791 GST_WARNING_OBJECT (typefind, "could not parse the peer uri");
792 gst_query_unref (query);
797 GST_WARNING_OBJECT (typefind, "could not find uri extension in %s", uri);
798 gst_query_unref (query);
805 gst_type_find_guess_by_extension (GstTypeFindElement * typefind, GstPad * pad,
806 GstTypeFindProbability * probability)
811 ext = gst_type_find_get_extension (typefind, pad);
815 caps = gst_type_find_helper_for_extension (GST_OBJECT_CAST (typefind), ext);
817 *probability = GST_TYPE_FIND_MAXIMUM;
825 gst_type_find_element_chain (GstPad * pad, GstObject * parent,
828 GstTypeFindElement *typefind;
829 GstFlowReturn res = GST_FLOW_OK;
831 typefind = GST_TYPE_FIND_ELEMENT (parent);
833 GST_LOG_OBJECT (typefind, "handling buffer in mode %d", typefind->mode);
835 switch (typefind->mode) {
837 /* we should already have called GST_ELEMENT_ERROR */
838 return GST_FLOW_ERROR;
840 /* don't take object lock as typefind->caps should not change anymore */
841 return gst_pad_push (typefind->src, buffer);
844 GST_OBJECT_LOCK (typefind);
845 gst_adapter_push (typefind->adapter, buffer);
846 GST_OBJECT_UNLOCK (typefind);
848 res = gst_type_find_element_chain_do_typefinding (typefind, TRUE);
850 if (typefind->mode == MODE_ERROR)
851 res = GST_FLOW_ERROR;
856 g_assert_not_reached ();
857 return GST_FLOW_ERROR;
864 gst_type_find_element_chain_do_typefinding (GstTypeFindElement * typefind,
865 gboolean check_avail)
867 GstTypeFindProbability probability;
871 gboolean have_min, have_max;
873 GST_OBJECT_LOCK (typefind);
874 avail = gst_adapter_available (typefind->adapter);
877 have_min = avail >= TYPE_FIND_MIN_SIZE;
878 have_max = avail >= TYPE_FIND_MAX_SIZE;
885 goto not_enough_data;
887 /* map all available data */
888 data = gst_adapter_map (typefind->adapter, avail);
889 caps = gst_type_find_helper_for_data (GST_OBJECT (typefind),
890 data, avail, &probability);
891 gst_adapter_unmap (typefind->adapter);
893 if (caps == NULL && have_max)
895 else if (caps == NULL)
899 if (probability < typefind->min_probability)
900 goto low_probability;
901 GST_OBJECT_UNLOCK (typefind);
903 /* probability is good enough too, so let's make it known ... emiting this
904 * signal calls our object handler which sets the caps. */
905 g_signal_emit (typefind, gst_type_find_element_signals[HAVE_TYPE], 0,
908 /* .. and send out the accumulated data */
909 stop_typefinding (typefind);
910 gst_caps_unref (caps);
916 GST_DEBUG_OBJECT (typefind, "not enough data for typefinding yet "
917 "(%" G_GSIZE_FORMAT " bytes)", avail);
918 GST_OBJECT_UNLOCK (typefind);
923 GST_OBJECT_UNLOCK (typefind);
924 GST_ELEMENT_ERROR (typefind, STREAM, TYPE_NOT_FOUND, (NULL), (NULL));
925 stop_typefinding (typefind);
926 return GST_FLOW_ERROR;
930 GST_DEBUG_OBJECT (typefind,
931 "no caps found with %" G_GSIZE_FORMAT " bytes of data, "
932 "waiting for more data", avail);
933 GST_OBJECT_UNLOCK (typefind);
938 GST_DEBUG_OBJECT (typefind, "found caps %" GST_PTR_FORMAT ", but "
939 "probability is %u which is lower than the required minimum of %u",
940 caps, probability, typefind->min_probability);
942 gst_caps_unref (caps);
947 GST_OBJECT_UNLOCK (typefind);
948 GST_DEBUG_OBJECT (typefind, "waiting for more data to try again");
954 gst_type_find_element_getrange (GstPad * srcpad, GstObject * parent,
955 guint64 offset, guint length, GstBuffer ** buffer)
957 GstTypeFindElement *typefind;
960 typefind = GST_TYPE_FIND_ELEMENT (parent);
962 ret = gst_pad_pull_range (typefind->sink, offset, length, buffer);
968 gst_type_find_element_activate_src_mode (GstPad * pad, GstObject * parent,
969 GstPadMode mode, gboolean active)
972 GstTypeFindElement *typefind;
974 typefind = GST_TYPE_FIND_ELEMENT (parent);
977 case GST_PAD_MODE_PULL:
978 /* make sure our task stops pushing, we can't call _stop here because this
979 * activation might happen from the streaming thread. */
980 gst_pad_pause_task (typefind->sink);
981 res = gst_pad_activate_mode (typefind->sink, mode, active);
982 if (active && res && typefind->caps) {
984 GST_OBJECT_LOCK (typefind);
985 caps = gst_caps_ref (typefind->caps);
986 GST_OBJECT_UNLOCK (typefind);
987 res = gst_pad_set_caps (typefind->src, caps);
988 gst_caps_unref (caps);
999 gst_type_find_element_loop (GstPad * pad)
1001 GstTypeFindElement *typefind;
1002 GstFlowReturn ret = GST_FLOW_OK;
1004 typefind = GST_TYPE_FIND_ELEMENT (GST_PAD_PARENT (pad));
1006 if (typefind->mode == MODE_TYPEFIND) {
1008 GstCaps *found_caps = NULL;
1009 GstTypeFindProbability probability = GST_TYPE_FIND_NONE;
1011 GST_DEBUG_OBJECT (typefind, "find type in pull mode");
1013 peer = gst_pad_get_peer (pad);
1018 if (!gst_pad_query_duration (peer, GST_FORMAT_BYTES, &size)) {
1019 GST_WARNING_OBJECT (typefind, "Could not query upstream length!");
1020 gst_object_unref (peer);
1022 ret = GST_FLOW_ERROR;
1026 /* the size if 0, we cannot continue */
1028 /* keep message in sync with message in sink event handler */
1029 GST_ELEMENT_ERROR (typefind, STREAM, TYPE_NOT_FOUND,
1030 (_("Stream contains no data.")), ("Can't typefind empty stream"));
1031 gst_object_unref (peer);
1032 ret = GST_FLOW_ERROR;
1035 ext = gst_type_find_get_extension (typefind, pad);
1038 gst_type_find_helper_get_range (GST_OBJECT_CAST (peer),
1039 GST_OBJECT_PARENT (peer),
1040 (GstTypeFindHelperGetRangeFunction) (GST_PAD_GETRANGEFUNC (peer)),
1041 (guint64) size, ext, &probability);
1044 GST_DEBUG ("Found caps %" GST_PTR_FORMAT, found_caps);
1046 gst_object_unref (peer);
1049 if (!found_caps || probability < typefind->min_probability) {
1050 GST_DEBUG ("Trying to guess using extension");
1051 gst_caps_replace (&found_caps, NULL);
1053 gst_type_find_guess_by_extension (typefind, pad, &probability);
1056 if (!found_caps || probability < typefind->min_probability) {
1057 GST_ELEMENT_ERROR (typefind, STREAM, TYPE_NOT_FOUND, (NULL), (NULL));
1058 gst_caps_replace (&found_caps, NULL);
1059 ret = GST_FLOW_ERROR;
1063 GST_DEBUG ("Emiting found caps %" GST_PTR_FORMAT, found_caps);
1064 g_signal_emit (typefind, gst_type_find_element_signals[HAVE_TYPE],
1065 0, probability, found_caps);
1066 typefind->mode = MODE_NORMAL;
1067 gst_caps_unref (found_caps);
1068 } else if (typefind->mode == MODE_NORMAL) {
1069 GstBuffer *outbuf = NULL;
1071 if (typefind->need_segment) {
1072 typefind->need_segment = FALSE;
1073 gst_pad_push_event (typefind->src,
1074 gst_event_new_segment (&typefind->segment));
1077 /* Pull 4k blocks and send downstream */
1078 ret = gst_pad_pull_range (typefind->sink, typefind->offset, 4096, &outbuf);
1079 if (ret != GST_FLOW_OK)
1082 typefind->offset += 4096;
1084 ret = gst_pad_push (typefind->src, outbuf);
1085 if (ret != GST_FLOW_OK)
1089 ret = GST_FLOW_ERROR;
1097 const gchar *reason = gst_flow_get_name (ret);
1098 gboolean push_eos = FALSE;
1100 GST_LOG_OBJECT (typefind, "pausing task, reason %s", reason);
1101 gst_pad_pause_task (typefind->sink);
1103 if (ret == GST_FLOW_EOS) {
1104 /* perform EOS logic */
1106 if (typefind->segment.flags & GST_SEEK_FLAG_SEGMENT) {
1109 /* for segment playback we need to post when (in stream time)
1110 * we stopped, this is either stop (when set) or the duration. */
1111 if ((stop = typefind->segment.stop) == -1)
1112 stop = typefind->offset;
1114 GST_LOG_OBJECT (typefind, "Sending segment done, at end of segment");
1115 gst_element_post_message (GST_ELEMENT (typefind),
1116 gst_message_new_segment_done (GST_OBJECT (typefind),
1117 GST_FORMAT_BYTES, stop));
1121 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
1122 /* for fatal errors we post an error message */
1123 GST_ELEMENT_ERROR (typefind, STREAM, FAILED, (NULL),
1124 ("stream stopped, reason %s", reason));
1128 /* send EOS, and prevent hanging if no streams yet */
1129 GST_LOG_OBJECT (typefind, "Sending EOS, at end of stream");
1130 gst_pad_push_event (typefind->src, gst_event_new_eos ());
1137 gst_type_find_element_activate_sink_mode (GstPad * pad, GstObject * parent,
1138 GstPadMode mode, gboolean active)
1141 GstTypeFindElement *typefind;
1143 typefind = GST_TYPE_FIND_ELEMENT (parent);
1146 case GST_PAD_MODE_PULL:
1148 gst_segment_init (&typefind->segment, GST_FORMAT_BYTES);
1149 typefind->need_segment = TRUE;
1150 typefind->offset = 0;
1153 res = gst_pad_stop_task (pad);
1156 case GST_PAD_MODE_PUSH:
1158 start_typefinding (typefind);
1160 stop_typefinding (typefind);
1172 gst_type_find_element_activate_sink (GstPad * pad, GstObject * parent)
1174 GstTypeFindElement *typefind;
1177 GstCaps *found_caps = NULL;
1178 GstTypeFindProbability probability = GST_TYPE_FIND_NONE;
1180 typefind = GST_TYPE_FIND_ELEMENT (parent);
1182 /* if we have force caps, use those */
1183 GST_OBJECT_LOCK (typefind);
1184 if (typefind->force_caps) {
1185 found_caps = gst_caps_ref (typefind->force_caps);
1186 probability = GST_TYPE_FIND_MAXIMUM;
1187 GST_OBJECT_UNLOCK (typefind);
1189 GST_DEBUG ("Emiting found caps %" GST_PTR_FORMAT, found_caps);
1190 g_signal_emit (typefind, gst_type_find_element_signals[HAVE_TYPE],
1191 0, probability, found_caps);
1192 typefind->mode = MODE_NORMAL;
1193 /* the signal above could have made a downstream element activate
1194 * the pad in pull mode, we check if the pad is already active now and if
1195 * so, we are done */
1196 if (gst_pad_is_active (pad))
1201 GST_OBJECT_UNLOCK (typefind);
1203 query = gst_query_new_scheduling ();
1205 if (!gst_pad_peer_query (pad, query)) {
1206 gst_query_unref (query);
1210 pull_mode = gst_query_has_scheduling_mode (query, GST_PAD_MODE_PULL);
1211 gst_query_unref (query);
1216 if (!gst_pad_activate_mode (pad, GST_PAD_MODE_PULL, TRUE))
1219 /* only start our task if we ourselves decide to start in pull mode */
1220 return gst_pad_start_task (pad, (GstTaskFunction) gst_type_find_element_loop,
1225 return gst_pad_activate_mode (pad, GST_PAD_MODE_PUSH, TRUE);
1229 static GstStateChangeReturn
1230 gst_type_find_element_change_state (GstElement * element,
1231 GstStateChange transition)
1233 GstStateChangeReturn ret;
1234 GstTypeFindElement *typefind;
1236 typefind = GST_TYPE_FIND_ELEMENT (element);
1239 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1241 switch (transition) {
1242 case GST_STATE_CHANGE_PAUSED_TO_READY:
1243 case GST_STATE_CHANGE_READY_TO_NULL:
1244 GST_OBJECT_LOCK (typefind);
1245 gst_caps_replace (&typefind->caps, NULL);
1247 g_list_foreach (typefind->cached_events,
1248 (GFunc) gst_mini_object_unref, NULL);
1249 g_list_free (typefind->cached_events);
1250 typefind->cached_events = NULL;
1251 typefind->mode = MODE_TYPEFIND;
1252 GST_OBJECT_UNLOCK (typefind);