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, gboolean at_eos);
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)
177 g_assert (caps != NULL);
179 GST_INFO_OBJECT (typefind, "found caps %" GST_PTR_FORMAT ", probability=%u",
182 GST_OBJECT_LOCK (typefind);
184 /* Now actually send the CAPS event downstream.
186 * Try to directly send the CAPS event downstream that we created in
187 * gst_type_find_element_emit_have_type() if it is still there, instead
188 * of creating a new one. No need to create an equivalent one, replacing
189 * it in the sticky event list and possibly causing renegotiation
191 event = gst_pad_get_sticky_event (typefind->src, GST_EVENT_CAPS, 0);
195 gst_event_parse_caps (event, &event_caps);
196 if (caps == event_caps) {
199 gst_event_unref (event);
200 event = gst_event_new_caps (caps);
203 event = gst_event_new_caps (caps);
207 gst_caps_unref (typefind->caps);
208 typefind->caps = gst_caps_ref (caps);
209 GST_OBJECT_UNLOCK (typefind);
211 gst_pad_push_event (typefind->src, event);
215 gst_type_find_element_emit_have_type (GstTypeFindElement * typefind,
216 guint probability, GstCaps * caps)
220 /* Only store the caps event at this point. We give signal handlers
221 * the chance to look at the caps before they are sent downstream.
222 * They are only forwarded downstream later in the default signal
223 * handler after all application signal handlers
225 event = gst_event_new_caps (caps);
226 gst_pad_store_sticky_event (typefind->src, event);
227 gst_event_unref (event);
229 g_signal_emit (typefind, gst_type_find_element_signals[HAVE_TYPE], 0,
234 gst_type_find_element_class_init (GstTypeFindElementClass * typefind_class)
236 GObjectClass *gobject_class = G_OBJECT_CLASS (typefind_class);
237 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (typefind_class);
239 gobject_class->set_property = gst_type_find_element_set_property;
240 gobject_class->get_property = gst_type_find_element_get_property;
241 gobject_class->dispose = gst_type_find_element_dispose;
243 g_object_class_install_property (gobject_class, PROP_CAPS,
244 g_param_spec_boxed ("caps", _("caps"),
245 _("detected capabilities in stream"), GST_TYPE_CAPS,
246 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
247 g_object_class_install_property (gobject_class, PROP_MINIMUM,
248 g_param_spec_uint ("minimum", _("minimum"),
249 "minimum probability required to accept caps", GST_TYPE_FIND_MINIMUM,
250 GST_TYPE_FIND_MAXIMUM, GST_TYPE_FIND_MINIMUM,
251 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
252 g_object_class_install_property (gobject_class, PROP_FORCE_CAPS,
253 g_param_spec_boxed ("force-caps", _("force caps"),
254 _("force caps without doing a typefind"), GST_TYPE_CAPS,
255 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
257 * GstTypeFindElement::have-type:
258 * @typefind: the typefind instance
259 * @probability: the probability of the type found
260 * @caps: the caps of the type found
262 * This signal gets emitted when the type and its probability has
265 gst_type_find_element_signals[HAVE_TYPE] = g_signal_new ("have-type",
266 G_TYPE_FROM_CLASS (typefind_class), G_SIGNAL_RUN_LAST,
267 G_STRUCT_OFFSET (GstTypeFindElementClass, have_type), NULL, NULL,
268 g_cclosure_marshal_generic, G_TYPE_NONE, 2,
269 G_TYPE_UINT, GST_TYPE_CAPS | G_SIGNAL_TYPE_STATIC_SCOPE);
271 typefind_class->have_type =
272 GST_DEBUG_FUNCPTR (gst_type_find_element_have_type);
274 gst_element_class_set_static_metadata (gstelement_class,
277 "Finds the media type of a stream",
278 "Benjamin Otte <in7y118@public.uni-hamburg.de>");
279 gst_element_class_add_static_pad_template (gstelement_class,
280 &type_find_element_src_template);
281 gst_element_class_add_static_pad_template (gstelement_class,
282 &type_find_element_sink_template);
284 gstelement_class->change_state =
285 GST_DEBUG_FUNCPTR (gst_type_find_element_change_state);
289 gst_type_find_element_init (GstTypeFindElement * typefind)
293 gst_pad_new_from_static_template (&type_find_element_sink_template,
296 gst_pad_set_activate_function (typefind->sink,
297 GST_DEBUG_FUNCPTR (gst_type_find_element_activate_sink));
298 gst_pad_set_activatemode_function (typefind->sink,
299 GST_DEBUG_FUNCPTR (gst_type_find_element_activate_sink_mode));
300 gst_pad_set_chain_function (typefind->sink,
301 GST_DEBUG_FUNCPTR (gst_type_find_element_chain));
302 gst_pad_set_event_function (typefind->sink,
303 GST_DEBUG_FUNCPTR (gst_type_find_element_sink_event));
304 GST_PAD_SET_PROXY_ALLOCATION (typefind->sink);
305 gst_element_add_pad (GST_ELEMENT (typefind), typefind->sink);
309 gst_pad_new_from_static_template (&type_find_element_src_template, "src");
311 gst_pad_set_activatemode_function (typefind->src,
312 GST_DEBUG_FUNCPTR (gst_type_find_element_activate_src_mode));
313 gst_pad_set_getrange_function (typefind->src,
314 GST_DEBUG_FUNCPTR (gst_type_find_element_getrange));
315 gst_pad_set_event_function (typefind->src,
316 GST_DEBUG_FUNCPTR (gst_type_find_element_src_event));
317 gst_pad_set_query_function (typefind->src,
318 GST_DEBUG_FUNCPTR (gst_type_find_handle_src_query));
319 gst_pad_use_fixed_caps (typefind->src);
320 gst_element_add_pad (GST_ELEMENT (typefind), typefind->src);
322 typefind->mode = MODE_TYPEFIND;
323 typefind->caps = NULL;
324 typefind->min_probability = 1;
326 typefind->adapter = gst_adapter_new ();
330 gst_type_find_element_dispose (GObject * object)
332 GstTypeFindElement *typefind = GST_TYPE_FIND_ELEMENT (object);
334 if (typefind->adapter) {
335 g_object_unref (typefind->adapter);
336 typefind->adapter = NULL;
339 if (typefind->force_caps) {
340 gst_caps_unref (typefind->force_caps);
341 typefind->force_caps = NULL;
344 G_OBJECT_CLASS (parent_class)->dispose (object);
348 gst_type_find_element_set_property (GObject * object, guint prop_id,
349 const GValue * value, GParamSpec * pspec)
351 GstTypeFindElement *typefind;
353 typefind = GST_TYPE_FIND_ELEMENT (object);
357 typefind->min_probability = g_value_get_uint (value);
359 case PROP_FORCE_CAPS:
360 GST_OBJECT_LOCK (typefind);
361 if (typefind->force_caps)
362 gst_caps_unref (typefind->force_caps);
363 typefind->force_caps = g_value_dup_boxed (value);
364 GST_OBJECT_UNLOCK (typefind);
367 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
373 gst_type_find_element_get_property (GObject * object, guint prop_id,
374 GValue * value, GParamSpec * pspec)
376 GstTypeFindElement *typefind;
378 typefind = GST_TYPE_FIND_ELEMENT (object);
382 GST_OBJECT_LOCK (typefind);
383 g_value_set_boxed (value, typefind->caps);
384 GST_OBJECT_UNLOCK (typefind);
387 g_value_set_uint (value, typefind->min_probability);
389 case PROP_FORCE_CAPS:
390 GST_OBJECT_LOCK (typefind);
391 g_value_set_boxed (value, typefind->force_caps);
392 GST_OBJECT_UNLOCK (typefind);
395 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
401 gst_type_find_handle_src_query (GstPad * pad, GstObject * parent,
404 GstTypeFindElement *typefind;
405 gboolean res = FALSE;
407 typefind = GST_TYPE_FIND_ELEMENT (parent);
408 GST_DEBUG_OBJECT (typefind, "Handling src query %s",
409 GST_QUERY_TYPE_NAME (query));
411 switch (GST_QUERY_TYPE (query)) {
412 case GST_QUERY_SCHEDULING:
413 /* FIXME, filter out the scheduling modes that we understand */
414 res = gst_pad_peer_query (typefind->sink, query);
418 GST_DEBUG_OBJECT (typefind,
419 "Got caps query, our caps are %" GST_PTR_FORMAT, typefind->caps);
421 /* We can hijack caps query if we typefind already */
422 if (typefind->caps) {
423 gst_query_set_caps_result (query, typefind->caps);
426 res = gst_pad_peer_query (typefind->sink, query);
430 case GST_QUERY_POSITION:
435 if (!(res = gst_pad_peer_query (typefind->sink, query)))
438 gst_query_parse_position (query, &format, &peer_pos);
440 GST_OBJECT_LOCK (typefind);
441 /* FIXME: this code assumes that there's no discont in the queue */
443 case GST_FORMAT_BYTES:
444 peer_pos -= gst_adapter_available (typefind->adapter);
450 GST_OBJECT_UNLOCK (typefind);
451 gst_query_set_position (query, format, peer_pos);
455 res = gst_pad_query_default (pad, parent, query);
463 gst_type_find_element_seek (GstTypeFindElement * typefind, GstEvent * event)
466 GstSeekType start_type, stop_type;
471 GstSegment seeksegment = { 0, };
473 gst_event_parse_seek (event, &rate, &format, &flags, &start_type, &start,
476 /* we can only seek on bytes */
477 if (format != GST_FORMAT_BYTES) {
478 GST_DEBUG_OBJECT (typefind, "Can only seek on BYTES");
482 /* copy segment, we need this because we still need the old
483 * segment when we close the current segment. */
484 memcpy (&seeksegment, &typefind->segment, sizeof (GstSegment));
486 GST_DEBUG_OBJECT (typefind, "configuring seek");
487 gst_segment_do_seek (&seeksegment, rate, format, flags,
488 start_type, start, stop_type, stop, NULL);
490 flush = ! !(flags & GST_SEEK_FLAG_FLUSH);
492 GST_DEBUG_OBJECT (typefind, "New segment %" GST_SEGMENT_FORMAT, &seeksegment);
495 GST_DEBUG_OBJECT (typefind, "Starting flush");
496 gst_pad_push_event (typefind->sink, gst_event_new_flush_start ());
497 gst_pad_push_event (typefind->src, gst_event_new_flush_start ());
499 GST_DEBUG_OBJECT (typefind, "Non-flushing seek, pausing task");
500 gst_pad_pause_task (typefind->sink);
503 /* now grab the stream lock so that streaming cannot continue, for
504 * non flushing seeks when the element is in PAUSED this could block
506 GST_DEBUG_OBJECT (typefind, "Waiting for streaming to stop");
507 GST_PAD_STREAM_LOCK (typefind->sink);
510 GST_DEBUG_OBJECT (typefind, "Stopping flush");
511 gst_pad_push_event (typefind->sink, gst_event_new_flush_stop (TRUE));
512 gst_pad_push_event (typefind->src, gst_event_new_flush_stop (TRUE));
515 /* now update the real segment info */
516 GST_DEBUG_OBJECT (typefind, "Committing new seek segment");
517 memcpy (&typefind->segment, &seeksegment, sizeof (GstSegment));
518 typefind->offset = typefind->segment.start;
520 /* notify start of new segment */
521 if (typefind->segment.flags & GST_SEGMENT_FLAG_SEGMENT) {
524 msg = gst_message_new_segment_start (GST_OBJECT (typefind),
525 GST_FORMAT_BYTES, typefind->segment.start);
526 gst_element_post_message (GST_ELEMENT (typefind), msg);
529 typefind->need_segment = TRUE;
531 /* restart our task since it might have been stopped when we did the
533 gst_pad_start_task (typefind->sink,
534 (GstTaskFunction) gst_type_find_element_loop, typefind->sink, NULL);
536 /* streaming can continue now */
537 GST_PAD_STREAM_UNLOCK (typefind->sink);
543 gst_type_find_element_src_event (GstPad * pad, GstObject * parent,
546 GstTypeFindElement *typefind = GST_TYPE_FIND_ELEMENT (parent);
549 if (typefind->mode != MODE_NORMAL) {
550 /* need to do more? */
551 gst_event_unref (event);
555 /* Only handle seeks here if driving the pipeline */
556 if (typefind->segment.format != GST_FORMAT_UNDEFINED &&
557 GST_EVENT_TYPE (event) == GST_EVENT_SEEK) {
558 result = gst_type_find_element_seek (typefind, event);
559 gst_event_unref (event);
562 return gst_pad_push_event (typefind->sink, event);
567 start_typefinding (GstTypeFindElement * typefind)
569 GST_DEBUG_OBJECT (typefind, "starting typefinding");
571 GST_OBJECT_LOCK (typefind);
573 gst_caps_replace (&typefind->caps, NULL);
574 typefind->initial_offset = GST_BUFFER_OFFSET_NONE;
575 GST_OBJECT_UNLOCK (typefind);
577 typefind->mode = MODE_TYPEFIND;
581 stop_typefinding (GstTypeFindElement * typefind)
584 gboolean push_cached_buffers;
587 GstClockTime pts, dts;
589 gst_element_get_state (GST_ELEMENT (typefind), &state, NULL, 0);
591 push_cached_buffers = (state >= GST_STATE_PAUSED && typefind->caps);
593 GST_DEBUG_OBJECT (typefind, "stopping typefinding%s",
594 push_cached_buffers ? " and pushing cached events and buffers" : "");
596 typefind->mode = MODE_NORMAL;
597 if (push_cached_buffers)
598 gst_type_find_element_send_cached_events (typefind);
600 GST_OBJECT_LOCK (typefind);
601 avail = gst_adapter_available (typefind->adapter);
605 pts = gst_adapter_prev_pts (typefind->adapter, NULL);
606 dts = gst_adapter_prev_dts (typefind->adapter, NULL);
607 buffer = gst_adapter_take_buffer (typefind->adapter, avail);
608 GST_BUFFER_PTS (buffer) = pts;
609 GST_BUFFER_DTS (buffer) = dts;
610 GST_BUFFER_OFFSET (buffer) = typefind->initial_offset;
611 GST_OBJECT_UNLOCK (typefind);
613 if (!push_cached_buffers) {
614 gst_buffer_unref (buffer);
616 GstPad *peer = gst_pad_get_peer (typefind->src);
618 /* make sure the user gets a meaningful error message in this case,
619 * which is not a core bug or bug of any kind (as the default error
620 * message emitted by gstpad.c otherwise would make you think) */
621 if (peer && GST_PAD_CHAINFUNC (peer) == NULL) {
622 GST_DEBUG_OBJECT (typefind, "upstream only supports push mode, while "
623 "downstream element only works in pull mode, erroring out");
624 GST_ELEMENT_ERROR (typefind, STREAM, FAILED,
625 ("%s cannot work in push mode. The operation is not supported "
626 "with this source element or protocol.",
627 G_OBJECT_TYPE_NAME (GST_PAD_PARENT (peer))),
628 ("Downstream pad %s:%s has no chainfunction, and the upstream "
629 "element does not support pull mode", GST_DEBUG_PAD_NAME (peer)));
630 typefind->mode = MODE_ERROR; /* make the chain function error out */
631 gst_buffer_unref (buffer);
633 gst_pad_push (typefind->src, buffer);
636 gst_object_unref (peer);
643 GST_DEBUG_OBJECT (typefind, "we have no data to typefind");
644 GST_OBJECT_UNLOCK (typefind);
650 gst_type_find_element_sink_event (GstPad * pad, GstObject * parent,
653 gboolean res = FALSE;
654 GstTypeFindElement *typefind = GST_TYPE_FIND_ELEMENT (parent);
656 GST_DEBUG_OBJECT (typefind, "got %s event in mode %d",
657 GST_EVENT_TYPE_NAME (event), typefind->mode);
659 switch (typefind->mode) {
661 switch (GST_EVENT_TYPE (event)) {
666 /* Parse and push out our caps and data */
667 gst_event_parse_caps (event, &caps);
668 res = gst_type_find_element_setcaps (typefind, caps);
670 gst_event_unref (event);
675 GST_FIXME_OBJECT (typefind,
676 "GAP events during typefinding not handled properly");
678 /* FIXME: These would need to be inserted in the stream at
679 * the right position between buffers, but we combine all
680 * buffers with a GstAdapter. Drop the GAP event for now,
681 * which will only cause an implicit GAP between buffers.
683 gst_event_unref (event);
689 GST_INFO_OBJECT (typefind, "Got EOS and no type found yet");
690 gst_type_find_element_chain_do_typefinding (typefind, FALSE, TRUE);
692 res = gst_pad_push_event (typefind->src, event);
695 case GST_EVENT_FLUSH_STOP:{
698 GST_OBJECT_LOCK (typefind);
700 for (l = typefind->cached_events; l; l = l->next) {
701 if (GST_EVENT_IS_STICKY (l->data) &&
702 GST_EVENT_TYPE (l->data) != GST_EVENT_SEGMENT &&
703 GST_EVENT_TYPE (l->data) != GST_EVENT_EOS) {
704 gst_pad_store_sticky_event (typefind->src, l->data);
706 gst_event_unref (l->data);
709 g_list_free (typefind->cached_events);
710 typefind->cached_events = NULL;
711 gst_adapter_clear (typefind->adapter);
712 GST_OBJECT_UNLOCK (typefind);
715 case GST_EVENT_FLUSH_START:
716 res = gst_pad_push_event (typefind->src, event);
719 /* Forward events that would happen before the caps event
720 * directly instead of storing them. There's no reason not
721 * to send them directly and we should only store events
722 * for later sending that would need to come after the caps
724 if (GST_EVENT_TYPE (event) < GST_EVENT_CAPS) {
725 res = gst_pad_push_event (typefind->src, event);
727 GST_DEBUG_OBJECT (typefind, "Saving %s event to send later",
728 GST_EVENT_TYPE_NAME (event));
729 GST_OBJECT_LOCK (typefind);
730 typefind->cached_events =
731 g_list_append (typefind->cached_events, event);
732 GST_OBJECT_UNLOCK (typefind);
739 res = gst_pad_push_event (typefind->src, event);
744 g_assert_not_reached ();
750 gst_type_find_element_send_cached_events (GstTypeFindElement * typefind)
752 GList *l, *cached_events;
754 GST_OBJECT_LOCK (typefind);
755 cached_events = typefind->cached_events;
756 typefind->cached_events = NULL;
757 GST_OBJECT_UNLOCK (typefind);
759 for (l = cached_events; l != NULL; l = l->next) {
760 GstEvent *event = GST_EVENT (l->data);
762 GST_DEBUG_OBJECT (typefind, "sending cached %s event",
763 GST_EVENT_TYPE_NAME (event));
764 gst_pad_push_event (typefind->src, event);
766 g_list_free (cached_events);
770 gst_type_find_element_setcaps (GstTypeFindElement * typefind, GstCaps * caps)
772 /* don't operate on ANY caps */
773 if (gst_caps_is_any (caps))
776 gst_type_find_element_emit_have_type (typefind, GST_TYPE_FIND_MAXIMUM, caps);
778 /* Shortcircuit typefinding if we get caps */
779 GST_DEBUG_OBJECT (typefind, "Skipping typefinding, using caps from "
780 "upstream: %" GST_PTR_FORMAT, caps);
782 stop_typefinding (typefind);
788 gst_type_find_get_extension (GstTypeFindElement * typefind, GstPad * pad)
795 query = gst_query_new_uri ();
797 /* try getting the caps with an uri query and from the extension */
798 if (!gst_pad_peer_query (pad, query))
799 goto peer_query_failed;
801 gst_query_parse_uri (query, &uri);
805 GST_DEBUG_OBJECT (typefind, "finding extension of %s", uri);
807 /* find the extension on the uri, this is everything after a '.' */
812 if (uri[find] == '.')
819 result = g_strdup (&uri[find + 1]);
821 GST_DEBUG_OBJECT (typefind, "found extension %s", result);
822 gst_query_unref (query);
830 GST_WARNING_OBJECT (typefind, "failed to query peer uri");
831 gst_query_unref (query);
836 GST_WARNING_OBJECT (typefind, "could not parse the peer uri");
837 gst_query_unref (query);
842 GST_WARNING_OBJECT (typefind, "could not find uri extension in %s", uri);
843 gst_query_unref (query);
850 gst_type_find_guess_by_extension (GstTypeFindElement * typefind, GstPad * pad,
851 GstTypeFindProbability * probability)
856 ext = gst_type_find_get_extension (typefind, pad);
860 caps = gst_type_find_helper_for_extension (GST_OBJECT_CAST (typefind), ext);
862 *probability = GST_TYPE_FIND_MAXIMUM;
870 gst_type_find_element_chain (GstPad * pad, GstObject * parent,
873 GstTypeFindElement *typefind;
874 GstFlowReturn res = GST_FLOW_OK;
876 typefind = GST_TYPE_FIND_ELEMENT (parent);
878 GST_LOG_OBJECT (typefind, "handling buffer in mode %d", typefind->mode);
880 switch (typefind->mode) {
882 /* we should already have called GST_ELEMENT_ERROR */
883 return GST_FLOW_ERROR;
885 /* don't take object lock as typefind->caps should not change anymore */
886 return gst_pad_push (typefind->src, buffer);
889 GST_OBJECT_LOCK (typefind);
890 if (typefind->initial_offset == GST_BUFFER_OFFSET_NONE)
891 typefind->initial_offset = GST_BUFFER_OFFSET (buffer);
892 gst_adapter_push (typefind->adapter, buffer);
893 GST_OBJECT_UNLOCK (typefind);
895 res = gst_type_find_element_chain_do_typefinding (typefind, TRUE, FALSE);
897 if (typefind->mode == MODE_ERROR)
898 res = GST_FLOW_ERROR;
903 g_assert_not_reached ();
904 return GST_FLOW_ERROR;
911 gst_type_find_element_chain_do_typefinding (GstTypeFindElement * typefind,
912 gboolean check_avail, gboolean at_eos)
914 GstTypeFindProbability probability;
915 GstCaps *caps = NULL;
918 gboolean have_min, have_max;
920 GST_OBJECT_LOCK (typefind);
921 if (typefind->force_caps) {
922 caps = gst_caps_ref (typefind->force_caps);
923 probability = GST_TYPE_FIND_MAXIMUM;
927 avail = gst_adapter_available (typefind->adapter);
930 have_min = avail >= TYPE_FIND_MIN_SIZE;
931 have_max = avail >= TYPE_FIND_MAX_SIZE;
933 have_min = avail > 0;
938 goto not_enough_data;
940 /* map all available data */
941 data = gst_adapter_map (typefind->adapter, avail);
942 caps = gst_type_find_helper_for_data (GST_OBJECT (typefind),
943 data, avail, &probability);
944 gst_adapter_unmap (typefind->adapter);
946 if (caps == NULL && have_max)
948 else if (caps == NULL)
952 if (probability < typefind->min_probability)
953 goto low_probability;
956 GST_OBJECT_UNLOCK (typefind);
958 /* probability is good enough too, so let's make it known ... emiting this
959 * signal calls our object handler which sets the caps. */
960 gst_type_find_element_emit_have_type (typefind, probability, caps);
962 /* .. and send out the accumulated data */
963 stop_typefinding (typefind);
964 gst_caps_unref (caps);
970 GST_OBJECT_UNLOCK (typefind);
973 GST_ELEMENT_ERROR (typefind, STREAM, TYPE_NOT_FOUND,
974 (_("Stream doesn't contain enough data.")),
975 ("Can't typefind stream"));
976 return GST_FLOW_ERROR;
978 GST_DEBUG_OBJECT (typefind, "not enough data for typefinding yet "
979 "(%" G_GSIZE_FORMAT " bytes)", avail);
985 GST_OBJECT_UNLOCK (typefind);
986 GST_ELEMENT_ERROR (typefind, STREAM, TYPE_NOT_FOUND, (NULL), (NULL));
987 stop_typefinding (typefind);
988 return GST_FLOW_ERROR;
992 GST_OBJECT_UNLOCK (typefind);
995 GST_ELEMENT_ERROR (typefind, STREAM, TYPE_NOT_FOUND,
996 (_("Stream doesn't contain enough data.")),
997 ("Can't typefind stream"));
998 return GST_FLOW_ERROR;
1000 GST_DEBUG_OBJECT (typefind,
1001 "no caps found with %" G_GSIZE_FORMAT " bytes of data, "
1002 "waiting for more data", avail);
1008 GST_DEBUG_OBJECT (typefind, "found caps %" GST_PTR_FORMAT ", but "
1009 "probability is %u which is lower than the required minimum of %u",
1010 caps, probability, typefind->min_probability);
1012 gst_caps_unref (caps);
1017 GST_OBJECT_UNLOCK (typefind);
1018 GST_DEBUG_OBJECT (typefind, "waiting for more data to try again");
1023 static GstFlowReturn
1024 gst_type_find_element_getrange (GstPad * srcpad, GstObject * parent,
1025 guint64 offset, guint length, GstBuffer ** buffer)
1027 GstTypeFindElement *typefind;
1030 typefind = GST_TYPE_FIND_ELEMENT (parent);
1032 ret = gst_pad_pull_range (typefind->sink, offset, length, buffer);
1038 gst_type_find_element_activate_src_mode (GstPad * pad, GstObject * parent,
1039 GstPadMode mode, gboolean active)
1042 GstTypeFindElement *typefind;
1044 typefind = GST_TYPE_FIND_ELEMENT (parent);
1047 case GST_PAD_MODE_PULL:
1048 /* make sure our task stops pushing, we can't call _stop here because this
1049 * activation might happen from the streaming thread. */
1050 gst_pad_pause_task (typefind->sink);
1051 res = gst_pad_activate_mode (typefind->sink, mode, active);
1061 gst_type_find_element_loop (GstPad * pad)
1063 GstTypeFindElement *typefind;
1064 GstFlowReturn ret = GST_FLOW_OK;
1066 typefind = GST_TYPE_FIND_ELEMENT (GST_PAD_PARENT (pad));
1068 if (typefind->need_stream_start) {
1072 stream_id = gst_pad_create_stream_id (typefind->src,
1073 GST_ELEMENT_CAST (typefind), NULL);
1075 GST_DEBUG_OBJECT (typefind, "Pushing STREAM_START");
1076 event = gst_event_new_stream_start (stream_id);
1077 gst_event_set_group_id (event, gst_util_group_id_next ());
1078 gst_pad_push_event (typefind->src, event);
1080 typefind->need_stream_start = FALSE;
1084 if (typefind->mode == MODE_TYPEFIND) {
1085 GstPad *peer = NULL;
1086 GstCaps *found_caps = NULL;
1087 GstTypeFindProbability probability = GST_TYPE_FIND_NONE;
1089 GST_DEBUG_OBJECT (typefind, "find type in pull mode");
1091 GST_OBJECT_LOCK (typefind);
1092 if (typefind->force_caps) {
1093 found_caps = gst_caps_ref (typefind->force_caps);
1094 probability = GST_TYPE_FIND_MAXIMUM;
1096 GST_OBJECT_UNLOCK (typefind);
1099 peer = gst_pad_get_peer (pad);
1104 if (!gst_pad_query_duration (peer, GST_FORMAT_BYTES, &size)) {
1105 GST_WARNING_OBJECT (typefind, "Could not query upstream length!");
1106 gst_object_unref (peer);
1108 ret = GST_FLOW_ERROR;
1112 /* the size if 0, we cannot continue */
1114 /* keep message in sync with message in sink event handler */
1115 GST_ELEMENT_ERROR (typefind, STREAM, TYPE_NOT_FOUND,
1116 (_("Stream contains no data.")), ("Can't typefind empty stream"));
1117 gst_object_unref (peer);
1118 ret = GST_FLOW_ERROR;
1121 ext = gst_type_find_get_extension (typefind, pad);
1124 gst_type_find_helper_get_range (GST_OBJECT_CAST (peer),
1125 GST_OBJECT_PARENT (peer),
1126 (GstTypeFindHelperGetRangeFunction) (GST_PAD_GETRANGEFUNC (peer)),
1127 (guint64) size, ext, &probability);
1130 GST_DEBUG ("Found caps %" GST_PTR_FORMAT, found_caps);
1132 gst_object_unref (peer);
1136 if (!found_caps || probability < typefind->min_probability) {
1137 GST_DEBUG ("Trying to guess using extension");
1138 gst_caps_replace (&found_caps, NULL);
1140 gst_type_find_guess_by_extension (typefind, pad, &probability);
1143 if (!found_caps || probability < typefind->min_probability) {
1144 GST_ELEMENT_ERROR (typefind, STREAM, TYPE_NOT_FOUND, (NULL), (NULL));
1145 gst_caps_replace (&found_caps, NULL);
1146 ret = GST_FLOW_ERROR;
1150 GST_DEBUG ("Emiting found caps %" GST_PTR_FORMAT, found_caps);
1151 gst_type_find_element_emit_have_type (typefind, probability, found_caps);
1152 typefind->mode = MODE_NORMAL;
1153 gst_caps_unref (found_caps);
1154 } else if (typefind->mode == MODE_NORMAL) {
1155 GstBuffer *outbuf = NULL;
1157 if (typefind->need_segment) {
1158 typefind->need_segment = FALSE;
1159 gst_pad_push_event (typefind->src,
1160 gst_event_new_segment (&typefind->segment));
1163 /* Pull 4k blocks and send downstream */
1164 ret = gst_pad_pull_range (typefind->sink, typefind->offset, 4096, &outbuf);
1165 if (ret != GST_FLOW_OK)
1168 typefind->offset += gst_buffer_get_size (outbuf);
1170 ret = gst_pad_push (typefind->src, outbuf);
1171 if (ret != GST_FLOW_OK)
1175 ret = GST_FLOW_ERROR;
1183 const gchar *reason = gst_flow_get_name (ret);
1184 gboolean push_eos = FALSE;
1186 GST_LOG_OBJECT (typefind, "pausing task, reason %s", reason);
1187 gst_pad_pause_task (typefind->sink);
1189 if (ret == GST_FLOW_EOS) {
1190 /* perform EOS logic */
1192 if (typefind->segment.flags & GST_SEGMENT_FLAG_SEGMENT) {
1195 /* for segment playback we need to post when (in stream time)
1196 * we stopped, this is either stop (when set) or the duration. */
1197 if ((stop = typefind->segment.stop) == -1)
1198 stop = typefind->offset;
1200 GST_LOG_OBJECT (typefind, "Sending segment done, at end of segment");
1201 gst_element_post_message (GST_ELEMENT (typefind),
1202 gst_message_new_segment_done (GST_OBJECT (typefind),
1203 GST_FORMAT_BYTES, stop));
1204 gst_pad_push_event (typefind->src,
1205 gst_event_new_segment_done (GST_FORMAT_BYTES, stop));
1209 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
1210 /* for fatal errors we post an error message */
1211 GST_ELEMENT_ERROR (typefind, STREAM, FAILED, (NULL),
1212 ("stream stopped, reason %s", reason));
1216 /* send EOS, and prevent hanging if no streams yet */
1217 GST_LOG_OBJECT (typefind, "Sending EOS, at end of stream");
1218 gst_pad_push_event (typefind->src, gst_event_new_eos ());
1225 gst_type_find_element_activate_sink_mode (GstPad * pad, GstObject * parent,
1226 GstPadMode mode, gboolean active)
1229 GstTypeFindElement *typefind;
1231 typefind = GST_TYPE_FIND_ELEMENT (parent);
1234 case GST_PAD_MODE_PULL:
1236 gst_segment_init (&typefind->segment, GST_FORMAT_BYTES);
1237 typefind->need_segment = TRUE;
1238 typefind->need_stream_start = TRUE;
1239 typefind->offset = 0;
1242 res = gst_pad_stop_task (pad);
1243 gst_segment_init (&typefind->segment, GST_FORMAT_UNDEFINED);
1246 case GST_PAD_MODE_PUSH:
1248 gst_segment_init (&typefind->segment, GST_FORMAT_UNDEFINED);
1249 start_typefinding (typefind);
1251 stop_typefinding (typefind);
1252 gst_segment_init (&typefind->segment, GST_FORMAT_UNDEFINED);
1264 gst_type_find_element_activate_sink (GstPad * pad, GstObject * parent)
1268 GstSchedulingFlags sched_flags;
1270 query = gst_query_new_scheduling ();
1272 if (!gst_pad_peer_query (pad, query)) {
1273 gst_query_unref (query);
1277 gst_query_parse_scheduling (query, &sched_flags, NULL, NULL, NULL);
1279 pull_mode = gst_query_has_scheduling_mode (query, GST_PAD_MODE_PULL)
1280 && ((sched_flags & GST_SCHEDULING_FLAG_SEEKABLE) != 0);
1282 gst_query_unref (query);
1287 if (!gst_pad_activate_mode (pad, GST_PAD_MODE_PULL, TRUE))
1290 /* only start our task if we ourselves decide to start in pull mode */
1291 return gst_pad_start_task (pad, (GstTaskFunction) gst_type_find_element_loop,
1296 return gst_pad_activate_mode (pad, GST_PAD_MODE_PUSH, TRUE);
1300 static GstStateChangeReturn
1301 gst_type_find_element_change_state (GstElement * element,
1302 GstStateChange transition)
1304 GstStateChangeReturn ret;
1305 GstTypeFindElement *typefind;
1307 typefind = GST_TYPE_FIND_ELEMENT (element);
1310 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1312 switch (transition) {
1313 case GST_STATE_CHANGE_PAUSED_TO_READY:
1314 case GST_STATE_CHANGE_READY_TO_NULL:
1315 GST_OBJECT_LOCK (typefind);
1316 gst_caps_replace (&typefind->caps, NULL);
1318 g_list_foreach (typefind->cached_events,
1319 (GFunc) gst_mini_object_unref, NULL);
1320 g_list_free (typefind->cached_events);
1321 typefind->cached_events = NULL;
1322 typefind->mode = MODE_TYPEFIND;
1323 GST_OBJECT_UNLOCK (typefind);