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 /* Do nothing if downstream is pulling from us */
183 if (GST_PAD_MODE (typefind->src) == GST_PAD_MODE_PULL)
186 GST_OBJECT_LOCK (typefind);
188 /* Now actually send the CAPS event downstream.
190 * Try to directly send the CAPS event downstream that we created in
191 * gst_type_find_element_emit_have_type() if it is still there, instead
192 * of creating a new one. No need to create an equivalent one, replacing
193 * it in the sticky event list and possibly causing renegotiation
195 event = gst_pad_get_sticky_event (typefind->src, GST_EVENT_CAPS, 0);
199 gst_event_parse_caps (event, &event_caps);
200 if (caps != event_caps) {
201 gst_event_unref (event);
202 event = gst_event_new_caps (caps);
205 event = gst_event_new_caps (caps);
208 GST_OBJECT_UNLOCK (typefind);
210 gst_pad_push_event (typefind->src, event);
214 gst_type_find_element_emit_have_type (GstTypeFindElement * typefind,
215 guint probability, GstCaps * caps)
219 /* Update caps field immediatly so that caps queries and properties can be
220 * honored in all "have-type" signal handlers.
222 GST_OBJECT_LOCK (typefind);
224 gst_caps_unref (typefind->caps);
225 typefind->caps = gst_caps_ref (caps);
226 GST_OBJECT_UNLOCK (typefind);
228 /* Only store the caps event at this point. We give signal handlers
229 * the chance to look at the caps before they are sent downstream.
230 * They are only forwarded downstream later in the default signal
231 * handler after all application signal handlers
233 event = gst_event_new_caps (caps);
234 gst_pad_store_sticky_event (typefind->src, event);
235 gst_event_unref (event);
237 g_signal_emit (typefind, gst_type_find_element_signals[HAVE_TYPE], 0,
242 gst_type_find_element_class_init (GstTypeFindElementClass * typefind_class)
244 GObjectClass *gobject_class = G_OBJECT_CLASS (typefind_class);
245 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (typefind_class);
247 gobject_class->set_property = gst_type_find_element_set_property;
248 gobject_class->get_property = gst_type_find_element_get_property;
249 gobject_class->dispose = gst_type_find_element_dispose;
251 g_object_class_install_property (gobject_class, PROP_CAPS,
252 g_param_spec_boxed ("caps", _("caps"),
253 _("detected capabilities in stream"), GST_TYPE_CAPS,
254 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
255 g_object_class_install_property (gobject_class, PROP_MINIMUM,
256 g_param_spec_uint ("minimum", _("minimum"),
257 "minimum probability required to accept caps", GST_TYPE_FIND_MINIMUM,
258 GST_TYPE_FIND_MAXIMUM, GST_TYPE_FIND_MINIMUM,
259 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
260 g_object_class_install_property (gobject_class, PROP_FORCE_CAPS,
261 g_param_spec_boxed ("force-caps", _("force caps"),
262 _("force caps without doing a typefind"), GST_TYPE_CAPS,
263 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
265 * GstTypeFindElement::have-type:
266 * @typefind: the typefind instance
267 * @probability: the probability of the type found
268 * @caps: the caps of the type found
270 * This signal gets emitted when the type and its probability has
273 gst_type_find_element_signals[HAVE_TYPE] = g_signal_new ("have-type",
274 G_TYPE_FROM_CLASS (typefind_class), G_SIGNAL_RUN_LAST,
275 G_STRUCT_OFFSET (GstTypeFindElementClass, have_type), NULL, NULL,
276 g_cclosure_marshal_generic, G_TYPE_NONE, 2,
277 G_TYPE_UINT, GST_TYPE_CAPS | G_SIGNAL_TYPE_STATIC_SCOPE);
279 typefind_class->have_type =
280 GST_DEBUG_FUNCPTR (gst_type_find_element_have_type);
282 gst_element_class_set_static_metadata (gstelement_class,
285 "Finds the media type of a stream",
286 "Benjamin Otte <in7y118@public.uni-hamburg.de>");
287 gst_element_class_add_static_pad_template (gstelement_class,
288 &type_find_element_src_template);
289 gst_element_class_add_static_pad_template (gstelement_class,
290 &type_find_element_sink_template);
292 gstelement_class->change_state =
293 GST_DEBUG_FUNCPTR (gst_type_find_element_change_state);
297 gst_type_find_element_init (GstTypeFindElement * typefind)
301 gst_pad_new_from_static_template (&type_find_element_sink_template,
304 gst_pad_set_activate_function (typefind->sink,
305 GST_DEBUG_FUNCPTR (gst_type_find_element_activate_sink));
306 gst_pad_set_activatemode_function (typefind->sink,
307 GST_DEBUG_FUNCPTR (gst_type_find_element_activate_sink_mode));
308 gst_pad_set_chain_function (typefind->sink,
309 GST_DEBUG_FUNCPTR (gst_type_find_element_chain));
310 gst_pad_set_event_function (typefind->sink,
311 GST_DEBUG_FUNCPTR (gst_type_find_element_sink_event));
312 GST_PAD_SET_PROXY_ALLOCATION (typefind->sink);
313 gst_element_add_pad (GST_ELEMENT (typefind), typefind->sink);
317 gst_pad_new_from_static_template (&type_find_element_src_template, "src");
319 gst_pad_set_activatemode_function (typefind->src,
320 GST_DEBUG_FUNCPTR (gst_type_find_element_activate_src_mode));
321 gst_pad_set_getrange_function (typefind->src,
322 GST_DEBUG_FUNCPTR (gst_type_find_element_getrange));
323 gst_pad_set_event_function (typefind->src,
324 GST_DEBUG_FUNCPTR (gst_type_find_element_src_event));
325 gst_pad_set_query_function (typefind->src,
326 GST_DEBUG_FUNCPTR (gst_type_find_handle_src_query));
327 gst_pad_use_fixed_caps (typefind->src);
328 gst_element_add_pad (GST_ELEMENT (typefind), typefind->src);
330 typefind->mode = MODE_TYPEFIND;
331 typefind->caps = NULL;
332 typefind->min_probability = 1;
334 typefind->adapter = gst_adapter_new ();
338 gst_type_find_element_dispose (GObject * object)
340 GstTypeFindElement *typefind = GST_TYPE_FIND_ELEMENT (object);
342 if (typefind->adapter) {
343 g_object_unref (typefind->adapter);
344 typefind->adapter = NULL;
347 if (typefind->force_caps) {
348 gst_caps_unref (typefind->force_caps);
349 typefind->force_caps = NULL;
352 G_OBJECT_CLASS (parent_class)->dispose (object);
356 gst_type_find_element_set_property (GObject * object, guint prop_id,
357 const GValue * value, GParamSpec * pspec)
359 GstTypeFindElement *typefind;
361 typefind = GST_TYPE_FIND_ELEMENT (object);
365 typefind->min_probability = g_value_get_uint (value);
367 case PROP_FORCE_CAPS:
368 GST_OBJECT_LOCK (typefind);
369 if (typefind->force_caps)
370 gst_caps_unref (typefind->force_caps);
371 typefind->force_caps = g_value_dup_boxed (value);
372 GST_OBJECT_UNLOCK (typefind);
375 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
381 gst_type_find_element_get_property (GObject * object, guint prop_id,
382 GValue * value, GParamSpec * pspec)
384 GstTypeFindElement *typefind;
386 typefind = GST_TYPE_FIND_ELEMENT (object);
390 GST_OBJECT_LOCK (typefind);
391 g_value_set_boxed (value, typefind->caps);
392 GST_OBJECT_UNLOCK (typefind);
395 g_value_set_uint (value, typefind->min_probability);
397 case PROP_FORCE_CAPS:
398 GST_OBJECT_LOCK (typefind);
399 g_value_set_boxed (value, typefind->force_caps);
400 GST_OBJECT_UNLOCK (typefind);
403 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
409 gst_type_find_handle_src_query (GstPad * pad, GstObject * parent,
412 GstTypeFindElement *typefind;
413 gboolean res = FALSE;
415 typefind = GST_TYPE_FIND_ELEMENT (parent);
416 GST_DEBUG_OBJECT (typefind, "Handling src query %s",
417 GST_QUERY_TYPE_NAME (query));
419 switch (GST_QUERY_TYPE (query)) {
420 case GST_QUERY_SCHEDULING:
421 /* FIXME, filter out the scheduling modes that we understand */
422 res = gst_pad_peer_query (typefind->sink, query);
426 GST_DEBUG_OBJECT (typefind,
427 "Got caps query, our caps are %" GST_PTR_FORMAT, typefind->caps);
429 /* We can hijack caps query if we typefind already */
430 if (typefind->caps) {
431 gst_query_set_caps_result (query, typefind->caps);
434 res = gst_pad_peer_query (typefind->sink, query);
438 case GST_QUERY_POSITION:
443 if (!(res = gst_pad_peer_query (typefind->sink, query)))
446 gst_query_parse_position (query, &format, &peer_pos);
448 GST_OBJECT_LOCK (typefind);
449 /* FIXME: this code assumes that there's no discont in the queue */
451 case GST_FORMAT_BYTES:
452 peer_pos -= gst_adapter_available (typefind->adapter);
453 if (peer_pos < 0) /* Clamp result to 0 */
460 GST_OBJECT_UNLOCK (typefind);
461 gst_query_set_position (query, format, peer_pos);
465 res = gst_pad_query_default (pad, parent, query);
473 gst_type_find_element_seek (GstTypeFindElement * typefind, GstEvent * event)
476 GstSeekType start_type, stop_type;
481 GstSegment seeksegment = { 0, };
483 gst_event_parse_seek (event, &rate, &format, &flags, &start_type, &start,
486 /* we can only seek on bytes */
487 if (format != GST_FORMAT_BYTES) {
488 GST_DEBUG_OBJECT (typefind, "Can only seek on BYTES");
492 /* copy segment, we need this because we still need the old
493 * segment when we close the current segment. */
494 memcpy (&seeksegment, &typefind->segment, sizeof (GstSegment));
496 GST_DEBUG_OBJECT (typefind, "configuring seek");
497 gst_segment_do_seek (&seeksegment, rate, format, flags,
498 start_type, start, stop_type, stop, NULL);
500 flush = ! !(flags & GST_SEEK_FLAG_FLUSH);
502 GST_DEBUG_OBJECT (typefind, "New segment %" GST_SEGMENT_FORMAT, &seeksegment);
505 GST_DEBUG_OBJECT (typefind, "Starting flush");
506 gst_pad_push_event (typefind->sink, gst_event_new_flush_start ());
507 gst_pad_push_event (typefind->src, gst_event_new_flush_start ());
509 GST_DEBUG_OBJECT (typefind, "Non-flushing seek, pausing task");
510 gst_pad_pause_task (typefind->sink);
513 /* now grab the stream lock so that streaming cannot continue, for
514 * non flushing seeks when the element is in PAUSED this could block
516 GST_DEBUG_OBJECT (typefind, "Waiting for streaming to stop");
517 GST_PAD_STREAM_LOCK (typefind->sink);
520 GST_DEBUG_OBJECT (typefind, "Stopping flush");
521 gst_pad_push_event (typefind->sink, gst_event_new_flush_stop (TRUE));
522 gst_pad_push_event (typefind->src, gst_event_new_flush_stop (TRUE));
525 /* now update the real segment info */
526 GST_DEBUG_OBJECT (typefind, "Committing new seek segment");
527 memcpy (&typefind->segment, &seeksegment, sizeof (GstSegment));
528 typefind->offset = typefind->segment.start;
530 /* notify start of new segment */
531 if (typefind->segment.flags & GST_SEGMENT_FLAG_SEGMENT) {
534 msg = gst_message_new_segment_start (GST_OBJECT (typefind),
535 GST_FORMAT_BYTES, typefind->segment.start);
536 gst_element_post_message (GST_ELEMENT (typefind), msg);
539 typefind->need_segment = TRUE;
541 /* restart our task since it might have been stopped when we did the
543 gst_pad_start_task (typefind->sink,
544 (GstTaskFunction) gst_type_find_element_loop, typefind->sink, NULL);
546 /* streaming can continue now */
547 GST_PAD_STREAM_UNLOCK (typefind->sink);
553 gst_type_find_element_src_event (GstPad * pad, GstObject * parent,
556 GstTypeFindElement *typefind = GST_TYPE_FIND_ELEMENT (parent);
559 if (typefind->mode != MODE_NORMAL) {
560 /* need to do more? */
561 GST_LOG_OBJECT (typefind, "Still typefinding. Not passing event upstream");
562 gst_event_unref (event);
566 /* Only handle seeks here if driving the pipeline */
567 if (typefind->segment.format != GST_FORMAT_UNDEFINED &&
568 GST_EVENT_TYPE (event) == GST_EVENT_SEEK) {
569 result = gst_type_find_element_seek (typefind, event);
570 gst_event_unref (event);
573 return gst_pad_push_event (typefind->sink, event);
578 start_typefinding (GstTypeFindElement * typefind)
580 GST_DEBUG_OBJECT (typefind, "starting typefinding");
582 GST_OBJECT_LOCK (typefind);
584 gst_caps_replace (&typefind->caps, NULL);
585 typefind->initial_offset = GST_BUFFER_OFFSET_NONE;
586 GST_OBJECT_UNLOCK (typefind);
588 typefind->mode = MODE_TYPEFIND;
592 stop_typefinding (GstTypeFindElement * typefind)
595 gboolean push_cached_buffers;
598 GstClockTime pts, dts;
600 gst_element_get_state (GST_ELEMENT (typefind), &state, NULL, 0);
602 push_cached_buffers = (state >= GST_STATE_PAUSED && typefind->caps);
604 GST_DEBUG_OBJECT (typefind, "stopping typefinding%s",
605 push_cached_buffers ? " and pushing cached events and buffers" : "");
607 typefind->mode = MODE_NORMAL;
608 if (push_cached_buffers)
609 gst_type_find_element_send_cached_events (typefind);
611 GST_OBJECT_LOCK (typefind);
612 avail = gst_adapter_available (typefind->adapter);
616 pts = gst_adapter_prev_pts (typefind->adapter, NULL);
617 dts = gst_adapter_prev_dts (typefind->adapter, NULL);
618 buffer = gst_adapter_take_buffer (typefind->adapter, avail);
619 GST_BUFFER_PTS (buffer) = pts;
620 GST_BUFFER_DTS (buffer) = dts;
621 GST_BUFFER_OFFSET (buffer) = typefind->initial_offset;
622 GST_OBJECT_UNLOCK (typefind);
624 if (!push_cached_buffers) {
625 gst_buffer_unref (buffer);
627 GstPad *peer = gst_pad_get_peer (typefind->src);
629 /* make sure the user gets a meaningful error message in this case,
630 * which is not a core bug or bug of any kind (as the default error
631 * message emitted by gstpad.c otherwise would make you think) */
632 if (peer && GST_PAD_CHAINFUNC (peer) == NULL) {
633 GST_DEBUG_OBJECT (typefind, "upstream only supports push mode, while "
634 "downstream element only works in pull mode, erroring out");
635 GST_ELEMENT_ERROR (typefind, STREAM, FAILED,
636 ("%s cannot work in push mode. The operation is not supported "
637 "with this source element or protocol.",
638 G_OBJECT_TYPE_NAME (GST_PAD_PARENT (peer))),
639 ("Downstream pad %s:%s has no chainfunction, and the upstream "
640 "element does not support pull mode", GST_DEBUG_PAD_NAME (peer)));
641 typefind->mode = MODE_ERROR; /* make the chain function error out */
642 gst_buffer_unref (buffer);
644 gst_pad_push (typefind->src, buffer);
647 gst_object_unref (peer);
654 GST_DEBUG_OBJECT (typefind, "we have no data to typefind");
655 GST_OBJECT_UNLOCK (typefind);
661 gst_type_find_element_sink_event (GstPad * pad, GstObject * parent,
664 gboolean res = FALSE;
665 GstTypeFindElement *typefind = GST_TYPE_FIND_ELEMENT (parent);
667 GST_DEBUG_OBJECT (typefind, "got %s event in mode %d",
668 GST_EVENT_TYPE_NAME (event), typefind->mode);
670 switch (typefind->mode) {
672 switch (GST_EVENT_TYPE (event)) {
677 /* Parse and push out our caps and data */
678 gst_event_parse_caps (event, &caps);
679 res = gst_type_find_element_setcaps (typefind, caps);
681 gst_event_unref (event);
686 GST_FIXME_OBJECT (typefind,
687 "GAP events during typefinding not handled properly");
689 /* FIXME: These would need to be inserted in the stream at
690 * the right position between buffers, but we combine all
691 * buffers with a GstAdapter. Drop the GAP event for now,
692 * which will only cause an implicit GAP between buffers.
694 gst_event_unref (event);
700 GST_INFO_OBJECT (typefind, "Got EOS and no type found yet");
701 gst_type_find_element_chain_do_typefinding (typefind, FALSE, TRUE);
703 res = gst_pad_push_event (typefind->src, event);
706 case GST_EVENT_FLUSH_STOP:{
709 GST_OBJECT_LOCK (typefind);
711 for (l = typefind->cached_events; l; l = l->next) {
712 if (GST_EVENT_IS_STICKY (l->data) &&
713 GST_EVENT_TYPE (l->data) != GST_EVENT_SEGMENT &&
714 GST_EVENT_TYPE (l->data) != GST_EVENT_EOS) {
715 gst_pad_store_sticky_event (typefind->src, l->data);
717 gst_event_unref (l->data);
720 g_list_free (typefind->cached_events);
721 typefind->cached_events = NULL;
722 gst_adapter_clear (typefind->adapter);
723 GST_OBJECT_UNLOCK (typefind);
726 case GST_EVENT_FLUSH_START:
727 res = gst_pad_push_event (typefind->src, event);
730 /* Forward events that would happen before the caps event
731 * directly instead of storing them. There's no reason not
732 * to send them directly and we should only store events
733 * for later sending that would need to come after the caps
735 if (GST_EVENT_TYPE (event) < GST_EVENT_CAPS) {
736 res = gst_pad_push_event (typefind->src, event);
738 GST_DEBUG_OBJECT (typefind, "Saving %s event to send later",
739 GST_EVENT_TYPE_NAME (event));
740 GST_OBJECT_LOCK (typefind);
741 typefind->cached_events =
742 g_list_append (typefind->cached_events, event);
743 GST_OBJECT_UNLOCK (typefind);
750 res = gst_pad_push_event (typefind->src, event);
755 g_assert_not_reached ();
761 gst_type_find_element_send_cached_events (GstTypeFindElement * typefind)
763 GList *l, *cached_events;
765 GST_OBJECT_LOCK (typefind);
766 cached_events = typefind->cached_events;
767 typefind->cached_events = NULL;
768 GST_OBJECT_UNLOCK (typefind);
770 for (l = cached_events; l != NULL; l = l->next) {
771 GstEvent *event = GST_EVENT (l->data);
773 GST_DEBUG_OBJECT (typefind, "sending cached %s event",
774 GST_EVENT_TYPE_NAME (event));
775 gst_pad_push_event (typefind->src, event);
777 g_list_free (cached_events);
781 gst_type_find_element_setcaps (GstTypeFindElement * typefind, GstCaps * caps)
783 /* don't operate on ANY caps */
784 if (gst_caps_is_any (caps))
787 /* Set to MODE_NORMAL before emitting have-type, in case it triggers a seek */
788 typefind->mode = MODE_NORMAL;
789 gst_type_find_element_emit_have_type (typefind, GST_TYPE_FIND_MAXIMUM, caps);
791 /* Shortcircuit typefinding if we get caps */
792 GST_DEBUG_OBJECT (typefind, "Skipping typefinding, using caps from "
793 "upstream: %" GST_PTR_FORMAT, caps);
795 stop_typefinding (typefind);
801 gst_type_find_get_extension (GstTypeFindElement * typefind, GstPad * pad)
808 query = gst_query_new_uri ();
810 /* try getting the caps with an uri query and from the extension */
811 if (!gst_pad_peer_query (pad, query))
812 goto peer_query_failed;
814 gst_query_parse_uri (query, &uri);
818 GST_DEBUG_OBJECT (typefind, "finding extension of %s", uri);
820 /* find the extension on the uri, this is everything after a '.' */
825 if (uri[find] == '.')
832 result = g_strdup (&uri[find + 1]);
834 GST_DEBUG_OBJECT (typefind, "found extension %s", result);
835 gst_query_unref (query);
843 GST_WARNING_OBJECT (typefind, "failed to query peer uri");
844 gst_query_unref (query);
849 GST_WARNING_OBJECT (typefind, "could not parse the peer uri");
850 gst_query_unref (query);
855 GST_WARNING_OBJECT (typefind, "could not find uri extension in %s", uri);
856 gst_query_unref (query);
863 gst_type_find_guess_by_extension (GstTypeFindElement * typefind, GstPad * pad,
864 GstTypeFindProbability * probability)
869 ext = gst_type_find_get_extension (typefind, pad);
873 caps = gst_type_find_helper_for_extension (GST_OBJECT_CAST (typefind), ext);
875 *probability = GST_TYPE_FIND_MAXIMUM;
883 gst_type_find_element_chain (GstPad * pad, GstObject * parent,
886 GstTypeFindElement *typefind;
887 GstFlowReturn res = GST_FLOW_OK;
889 typefind = GST_TYPE_FIND_ELEMENT (parent);
891 GST_LOG_OBJECT (typefind, "handling buffer in mode %d", typefind->mode);
893 switch (typefind->mode) {
895 /* we should already have called GST_ELEMENT_ERROR */
896 return GST_FLOW_ERROR;
898 /* don't take object lock as typefind->caps should not change anymore */
899 return gst_pad_push (typefind->src, buffer);
902 GST_OBJECT_LOCK (typefind);
903 if (typefind->initial_offset == GST_BUFFER_OFFSET_NONE)
904 typefind->initial_offset = GST_BUFFER_OFFSET (buffer);
905 gst_adapter_push (typefind->adapter, buffer);
906 GST_OBJECT_UNLOCK (typefind);
908 res = gst_type_find_element_chain_do_typefinding (typefind, TRUE, FALSE);
910 if (typefind->mode == MODE_ERROR)
911 res = GST_FLOW_ERROR;
916 g_assert_not_reached ();
917 return GST_FLOW_ERROR;
924 gst_type_find_element_chain_do_typefinding (GstTypeFindElement * typefind,
925 gboolean check_avail, gboolean at_eos)
927 GstTypeFindProbability probability;
928 GstCaps *caps = NULL;
931 gboolean have_min, have_max;
933 GST_OBJECT_LOCK (typefind);
934 if (typefind->force_caps) {
935 caps = gst_caps_ref (typefind->force_caps);
936 probability = GST_TYPE_FIND_MAXIMUM;
940 avail = gst_adapter_available (typefind->adapter);
943 have_min = avail >= TYPE_FIND_MIN_SIZE;
944 have_max = avail >= TYPE_FIND_MAX_SIZE;
946 have_min = avail > 0;
951 goto not_enough_data;
953 /* map all available data */
954 data = gst_adapter_map (typefind->adapter, avail);
955 caps = gst_type_find_helper_for_data (GST_OBJECT (typefind),
956 data, avail, &probability);
957 gst_adapter_unmap (typefind->adapter);
959 if (caps == NULL && have_max)
961 else if (caps == NULL)
965 if (probability < typefind->min_probability)
966 goto low_probability;
969 GST_OBJECT_UNLOCK (typefind);
971 /* probability is good enough too, so let's make it known ... emiting this
972 * signal calls our object handler which sets the caps. */
973 /* Set to MODE_NORMAL before emitting have-type, in case it triggers a seek */
974 typefind->mode = MODE_NORMAL;
975 gst_type_find_element_emit_have_type (typefind, probability, caps);
977 /* .. and send out the accumulated data */
978 stop_typefinding (typefind);
979 gst_caps_unref (caps);
985 GST_OBJECT_UNLOCK (typefind);
988 GST_ELEMENT_ERROR (typefind, STREAM, TYPE_NOT_FOUND,
989 (_("Stream doesn't contain enough data.")),
990 ("Can't typefind stream"));
991 return GST_FLOW_ERROR;
993 GST_DEBUG_OBJECT (typefind, "not enough data for typefinding yet "
994 "(%" G_GSIZE_FORMAT " bytes)", avail);
1000 GST_OBJECT_UNLOCK (typefind);
1001 GST_ELEMENT_ERROR (typefind, STREAM, TYPE_NOT_FOUND, (NULL), (NULL));
1002 stop_typefinding (typefind);
1003 return GST_FLOW_ERROR;
1007 GST_OBJECT_UNLOCK (typefind);
1010 GST_ELEMENT_ERROR (typefind, STREAM, TYPE_NOT_FOUND,
1011 (_("Stream doesn't contain enough data.")),
1012 ("Can't typefind stream"));
1013 return GST_FLOW_ERROR;
1015 GST_DEBUG_OBJECT (typefind,
1016 "no caps found with %" G_GSIZE_FORMAT " bytes of data, "
1017 "waiting for more data", avail);
1023 GST_DEBUG_OBJECT (typefind, "found caps %" GST_PTR_FORMAT ", but "
1024 "probability is %u which is lower than the required minimum of %u",
1025 caps, probability, typefind->min_probability);
1027 gst_caps_unref (caps);
1032 GST_OBJECT_UNLOCK (typefind);
1033 GST_DEBUG_OBJECT (typefind, "waiting for more data to try again");
1038 static GstFlowReturn
1039 gst_type_find_element_getrange (GstPad * srcpad, GstObject * parent,
1040 guint64 offset, guint length, GstBuffer ** buffer)
1042 GstTypeFindElement *typefind;
1045 typefind = GST_TYPE_FIND_ELEMENT (parent);
1047 ret = gst_pad_pull_range (typefind->sink, offset, length, buffer);
1053 gst_type_find_element_activate_src_mode (GstPad * pad, GstObject * parent,
1054 GstPadMode mode, gboolean active)
1057 GstTypeFindElement *typefind;
1059 typefind = GST_TYPE_FIND_ELEMENT (parent);
1062 case GST_PAD_MODE_PULL:
1063 /* make sure our task stops pushing, we can't call _stop here because this
1064 * activation might happen from the streaming thread. */
1065 gst_pad_pause_task (typefind->sink);
1066 res = gst_pad_activate_mode (typefind->sink, mode, active);
1076 gst_type_find_element_loop (GstPad * pad)
1078 GstTypeFindElement *typefind;
1079 GstFlowReturn ret = GST_FLOW_OK;
1081 typefind = GST_TYPE_FIND_ELEMENT (GST_PAD_PARENT (pad));
1083 if (typefind->need_stream_start) {
1087 stream_id = gst_pad_create_stream_id (typefind->src,
1088 GST_ELEMENT_CAST (typefind), NULL);
1090 GST_DEBUG_OBJECT (typefind, "Pushing STREAM_START");
1091 event = gst_event_new_stream_start (stream_id);
1092 gst_event_set_group_id (event, gst_util_group_id_next ());
1093 gst_pad_push_event (typefind->src, event);
1095 typefind->need_stream_start = FALSE;
1099 if (typefind->mode == MODE_TYPEFIND) {
1100 GstPad *peer = NULL;
1101 GstCaps *found_caps = NULL;
1102 GstTypeFindProbability probability = GST_TYPE_FIND_NONE;
1104 GST_DEBUG_OBJECT (typefind, "find type in pull mode");
1106 GST_OBJECT_LOCK (typefind);
1107 if (typefind->force_caps) {
1108 found_caps = gst_caps_ref (typefind->force_caps);
1109 probability = GST_TYPE_FIND_MAXIMUM;
1111 GST_OBJECT_UNLOCK (typefind);
1114 peer = gst_pad_get_peer (pad);
1119 if (!gst_pad_query_duration (peer, GST_FORMAT_BYTES, &size)) {
1120 GST_WARNING_OBJECT (typefind, "Could not query upstream length!");
1121 gst_object_unref (peer);
1123 ret = GST_FLOW_ERROR;
1127 /* the size if 0, we cannot continue */
1129 /* keep message in sync with message in sink event handler */
1130 GST_ELEMENT_ERROR (typefind, STREAM, TYPE_NOT_FOUND,
1131 (_("Stream contains no data.")), ("Can't typefind empty stream"));
1132 gst_object_unref (peer);
1133 ret = GST_FLOW_ERROR;
1136 ext = gst_type_find_get_extension (typefind, pad);
1139 gst_type_find_helper_get_range (GST_OBJECT_CAST (peer),
1140 GST_OBJECT_PARENT (peer),
1141 (GstTypeFindHelperGetRangeFunction) (GST_PAD_GETRANGEFUNC (peer)),
1142 (guint64) size, ext, &probability);
1145 GST_DEBUG ("Found caps %" GST_PTR_FORMAT, found_caps);
1147 gst_object_unref (peer);
1151 if (!found_caps || probability < typefind->min_probability) {
1152 GST_DEBUG ("Trying to guess using extension");
1153 gst_caps_replace (&found_caps, NULL);
1155 gst_type_find_guess_by_extension (typefind, pad, &probability);
1158 if (!found_caps || probability < typefind->min_probability) {
1159 GST_ELEMENT_ERROR (typefind, STREAM, TYPE_NOT_FOUND, (NULL), (NULL));
1160 gst_caps_replace (&found_caps, NULL);
1161 ret = GST_FLOW_ERROR;
1165 GST_DEBUG ("Emiting found caps %" GST_PTR_FORMAT, found_caps);
1166 /* Set to MODE_NORMAL before emitting have-type, in case it triggers a seek */
1167 typefind->mode = MODE_NORMAL;
1168 gst_type_find_element_emit_have_type (typefind, probability, found_caps);
1169 gst_caps_unref (found_caps);
1170 } else if (typefind->mode == MODE_NORMAL) {
1171 GstBuffer *outbuf = NULL;
1173 if (typefind->need_segment) {
1174 typefind->need_segment = FALSE;
1175 gst_pad_push_event (typefind->src,
1176 gst_event_new_segment (&typefind->segment));
1179 /* Pull 4k blocks and send downstream */
1180 ret = gst_pad_pull_range (typefind->sink, typefind->offset, 4096, &outbuf);
1181 if (ret != GST_FLOW_OK)
1184 typefind->offset += gst_buffer_get_size (outbuf);
1186 ret = gst_pad_push (typefind->src, outbuf);
1187 if (ret != GST_FLOW_OK)
1191 ret = GST_FLOW_ERROR;
1199 const gchar *reason = gst_flow_get_name (ret);
1200 gboolean push_eos = FALSE;
1202 GST_LOG_OBJECT (typefind, "pausing task, reason %s", reason);
1203 gst_pad_pause_task (typefind->sink);
1205 if (ret == GST_FLOW_EOS) {
1206 /* perform EOS logic */
1208 if (typefind->segment.flags & GST_SEGMENT_FLAG_SEGMENT) {
1211 /* for segment playback we need to post when (in stream time)
1212 * we stopped, this is either stop (when set) or the duration. */
1213 if ((stop = typefind->segment.stop) == -1)
1214 stop = typefind->offset;
1216 GST_LOG_OBJECT (typefind, "Sending segment done, at end of segment");
1217 gst_element_post_message (GST_ELEMENT (typefind),
1218 gst_message_new_segment_done (GST_OBJECT (typefind),
1219 GST_FORMAT_BYTES, stop));
1220 gst_pad_push_event (typefind->src,
1221 gst_event_new_segment_done (GST_FORMAT_BYTES, stop));
1225 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
1226 /* for fatal errors we post an error message */
1227 GST_ELEMENT_FLOW_ERROR (typefind, ret);
1231 /* send EOS, and prevent hanging if no streams yet */
1232 GST_LOG_OBJECT (typefind, "Sending EOS, at end of stream");
1233 gst_pad_push_event (typefind->src, gst_event_new_eos ());
1240 gst_type_find_element_activate_sink_mode (GstPad * pad, GstObject * parent,
1241 GstPadMode mode, gboolean active)
1244 GstTypeFindElement *typefind;
1246 typefind = GST_TYPE_FIND_ELEMENT (parent);
1249 case GST_PAD_MODE_PULL:
1251 gst_segment_init (&typefind->segment, GST_FORMAT_BYTES);
1252 typefind->need_segment = TRUE;
1253 typefind->need_stream_start = TRUE;
1254 typefind->offset = 0;
1257 res = gst_pad_stop_task (pad);
1258 gst_segment_init (&typefind->segment, GST_FORMAT_UNDEFINED);
1261 case GST_PAD_MODE_PUSH:
1263 gst_segment_init (&typefind->segment, GST_FORMAT_UNDEFINED);
1264 start_typefinding (typefind);
1266 stop_typefinding (typefind);
1267 gst_segment_init (&typefind->segment, GST_FORMAT_UNDEFINED);
1279 gst_type_find_element_activate_sink (GstPad * pad, GstObject * parent)
1284 query = gst_query_new_scheduling ();
1286 if (!gst_pad_peer_query (pad, query)) {
1287 gst_query_unref (query);
1291 pull_mode = gst_query_has_scheduling_mode_with_flags (query,
1292 GST_PAD_MODE_PULL, GST_SCHEDULING_FLAG_SEEKABLE);
1294 gst_query_unref (query);
1299 if (!gst_pad_activate_mode (pad, GST_PAD_MODE_PULL, TRUE))
1302 /* only start our task if we ourselves decide to start in pull mode */
1303 return gst_pad_start_task (pad, (GstTaskFunction) gst_type_find_element_loop,
1308 return gst_pad_activate_mode (pad, GST_PAD_MODE_PUSH, TRUE);
1312 static GstStateChangeReturn
1313 gst_type_find_element_change_state (GstElement * element,
1314 GstStateChange transition)
1316 GstStateChangeReturn ret;
1317 GstTypeFindElement *typefind;
1319 typefind = GST_TYPE_FIND_ELEMENT (element);
1322 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1324 switch (transition) {
1325 case GST_STATE_CHANGE_PAUSED_TO_READY:
1326 case GST_STATE_CHANGE_READY_TO_NULL:
1327 GST_OBJECT_LOCK (typefind);
1328 gst_caps_replace (&typefind->caps, NULL);
1330 g_list_foreach (typefind->cached_events,
1331 (GFunc) gst_mini_object_unref, NULL);
1332 g_list_free (typefind->cached_events);
1333 typefind->cached_events = NULL;
1334 typefind->mode = MODE_TYPEFIND;
1335 GST_OBJECT_UNLOCK (typefind);