typefind: Add _with_extension() variants for typefinding data or a buffer
[platform/upstream/gstreamer.git] / plugins / elements / gsttypefindelement.c
1 /* GStreamer
2  * Copyright (C) 2003 Benjamin Otte <in7y118@public.uni-hamburg.de>
3  *
4  * gsttypefindelement.c: element that detects type of stream
5  *
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.
10  *
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.
15  *
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.
20  */
21 /**
22  * SECTION:element-typefind
23  * @title: typefind
24  *
25  * Determines the media-type of a stream. It applies typefind functions in the
26  * order of their rank. Once the type has been detected it sets its src pad caps
27  * to the found media type.
28  *
29  * Whenever a type is found the #GstTypeFindElement::have-type signal is
30  * emitted, either from the streaming thread or the application thread
31  * (the latter may happen when typefinding is done pull-based from the
32  * state change function).
33  *
34  * Plugins can register custom typefinders by using #GstTypeFindFactory.
35  */
36
37 /* FIXME: need a better solution for non-seekable streams */
38
39 /* way of operation:
40  * 1) get a list of all typefind functions sorted best to worst
41  * 2) if all elements have been called with all requested data goto 8
42  * 3) call all functions once with all available data
43  * 4) if a function returns a value >= PROP_MAXIMUM goto 8 (never implemented))
44  * 5) all functions with a result > PROP_MINIMUM or functions that did not get
45  *    all requested data (where peek returned NULL) stay in list
46  * 6) seek to requested offset of best function that still has open data
47  *    requests
48  * 7) goto 2
49  * 8) take best available result and use its caps
50  *
51  * The element has two scheduling modes:
52  *
53  * 1) chain based, it will collect buffers and run the typefind function on
54  *    the buffer until something is found.
55  * 2) getrange based, it will proxy the getrange function to the sinkpad. It
56  *    is assumed that the peer element is happy with whatever format we
57  *    eventually read.
58  *
59  * By default it tries to do pull based typefinding (this avoids joining
60  * received buffers and holding them back in store.)
61  *
62  * When the element has no connected srcpad, and the sinkpad can operate in
63  * getrange based mode, the element starts its own task to figure out the
64  * type of the stream.
65  *
66  * Most of the actual implementation is in libs/gst/base/gsttypefindhelper.c.
67  */
68
69 #ifdef HAVE_CONFIG_H
70 #  include "config.h"
71 #endif
72
73 #include "gst/gst_private.h"
74
75 #include "gsttypefindelement.h"
76 #include "gst/gst-i18n-lib.h"
77 #include "gst/base/gsttypefindhelper.h"
78
79 #include <gst/gsttypefind.h>
80 #include <gst/gstutils.h>
81 #include <gst/gsterror.h>
82
83 GST_DEBUG_CATEGORY_STATIC (gst_type_find_element_debug);
84 #define GST_CAT_DEFAULT gst_type_find_element_debug
85
86 /* generic templates */
87 static GstStaticPadTemplate type_find_element_sink_template =
88 GST_STATIC_PAD_TEMPLATE ("sink",
89     GST_PAD_SINK,
90     GST_PAD_ALWAYS,
91     GST_STATIC_CAPS_ANY);
92
93 static GstStaticPadTemplate type_find_element_src_template =
94 GST_STATIC_PAD_TEMPLATE ("src",
95     GST_PAD_SRC,
96     GST_PAD_ALWAYS,
97     GST_STATIC_CAPS_ANY);
98
99 /* Require at least 2kB of data before we attempt typefinding in chain-mode.
100  * 128kB is massive overkill for the maximum, but doesn't do any harm */
101 #define TYPE_FIND_MIN_SIZE   (2*1024)
102 #define TYPE_FIND_MAX_SIZE (128*1024)
103
104 /* TypeFind signals and args */
105 enum
106 {
107   HAVE_TYPE,
108   LAST_SIGNAL
109 };
110 enum
111 {
112   PROP_0,
113   PROP_CAPS,
114   PROP_MINIMUM,
115   PROP_FORCE_CAPS,
116   PROP_LAST
117 };
118 enum
119 {
120   MODE_NORMAL,                  /* act as identity */
121   MODE_TYPEFIND,                /* do typefinding  */
122   MODE_ERROR                    /* had fatal error */
123 };
124
125
126 #define _do_init \
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);
132
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);
138
139 static gboolean gst_type_find_element_src_event (GstPad * pad,
140     GstObject * parent, GstEvent * event);
141 static gboolean gst_type_find_handle_src_query (GstPad * pad,
142     GstObject * parent, GstQuery * query);
143
144 static gboolean gst_type_find_element_sink_event (GstPad * pad,
145     GstObject * parent, GstEvent * event);
146 static gboolean gst_type_find_element_setcaps (GstTypeFindElement * typefind,
147     GstCaps * caps);
148 static GstFlowReturn gst_type_find_element_chain (GstPad * sinkpad,
149     GstObject * parent, GstBuffer * buffer);
150 static GstFlowReturn gst_type_find_element_getrange (GstPad * srcpad,
151     GstObject * parent, guint64 offset, guint length, GstBuffer ** buffer);
152
153 static GstStateChangeReturn
154 gst_type_find_element_change_state (GstElement * element,
155     GstStateChange transition);
156 static gboolean gst_type_find_element_activate_sink (GstPad * pad,
157     GstObject * parent);
158 static gboolean gst_type_find_element_activate_sink_mode (GstPad * pad,
159     GstObject * parent, GstPadMode mode, gboolean active);
160 static gboolean gst_type_find_element_activate_src_mode (GstPad * pad,
161     GstObject * parent, GstPadMode mode, gboolean active);
162 static GstFlowReturn
163 gst_type_find_element_chain_do_typefinding (GstTypeFindElement * typefind,
164     gboolean check_avail, gboolean at_eos);
165 static void gst_type_find_element_send_cached_events (GstTypeFindElement *
166     typefind);
167
168 static void gst_type_find_element_loop (GstPad * pad);
169
170 static guint gst_type_find_element_signals[LAST_SIGNAL] = { 0 };
171
172 static void
173 gst_type_find_element_have_type (GstTypeFindElement * typefind,
174     guint probability, GstCaps * caps)
175 {
176   GstEvent *event;
177
178   g_assert (caps != NULL);
179
180   GST_INFO_OBJECT (typefind, "found caps %" GST_PTR_FORMAT ", probability=%u",
181       caps, probability);
182
183   /* Do nothing if downstream is pulling from us */
184   if (GST_PAD_MODE (typefind->src) == GST_PAD_MODE_PULL)
185     return;
186
187   GST_OBJECT_LOCK (typefind);
188
189   /* Now actually send the CAPS event downstream.
190    *
191    * Try to directly send the CAPS event downstream that we created in
192    * gst_type_find_element_emit_have_type() if it is still there, instead
193    * of creating a new one. No need to create an equivalent one, replacing
194    * it in the sticky event list and possibly causing renegotiation
195    */
196   event = gst_pad_get_sticky_event (typefind->src, GST_EVENT_CAPS, 0);
197   if (event) {
198     GstCaps *event_caps;
199
200     gst_event_parse_caps (event, &event_caps);
201     if (caps != event_caps) {
202       gst_event_unref (event);
203       event = gst_event_new_caps (caps);
204     }
205   } else {
206     event = gst_event_new_caps (caps);
207   }
208
209   GST_OBJECT_UNLOCK (typefind);
210
211   gst_pad_push_event (typefind->src, event);
212 }
213
214 static void
215 gst_type_find_element_emit_have_type (GstTypeFindElement * typefind,
216     guint probability, GstCaps * caps)
217 {
218   GstEvent *event;
219
220   /* Update caps field immediately so that caps queries and properties can be
221    * honored in all "have-type" signal handlers.
222    */
223   GST_OBJECT_LOCK (typefind);
224   if (typefind->caps)
225     gst_caps_unref (typefind->caps);
226   typefind->caps = gst_caps_ref (caps);
227   GST_OBJECT_UNLOCK (typefind);
228
229   /* Only store the caps event at this point. We give signal handlers
230    * the chance to look at the caps before they are sent downstream.
231    * They are only forwarded downstream later in the default signal
232    * handler after all application signal handlers
233    */
234   event = gst_event_new_caps (caps);
235   gst_pad_store_sticky_event (typefind->src, event);
236   gst_event_unref (event);
237
238   g_signal_emit (typefind, gst_type_find_element_signals[HAVE_TYPE], 0,
239       probability, caps);
240 }
241
242 static void
243 gst_type_find_element_class_init (GstTypeFindElementClass * typefind_class)
244 {
245   GObjectClass *gobject_class = G_OBJECT_CLASS (typefind_class);
246   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (typefind_class);
247
248   gobject_class->set_property = gst_type_find_element_set_property;
249   gobject_class->get_property = gst_type_find_element_get_property;
250   gobject_class->dispose = gst_type_find_element_dispose;
251
252   g_object_class_install_property (gobject_class, PROP_CAPS,
253       g_param_spec_boxed ("caps", _("caps"),
254           _("detected capabilities in stream"), GST_TYPE_CAPS,
255           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
256   g_object_class_install_property (gobject_class, PROP_MINIMUM,
257       g_param_spec_uint ("minimum", _("minimum"),
258           "minimum probability required to accept caps", GST_TYPE_FIND_MINIMUM,
259           GST_TYPE_FIND_MAXIMUM, GST_TYPE_FIND_MINIMUM,
260           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
261   g_object_class_install_property (gobject_class, PROP_FORCE_CAPS,
262       g_param_spec_boxed ("force-caps", _("force caps"),
263           _("force caps without doing a typefind"), GST_TYPE_CAPS,
264           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
265   /**
266    * GstTypeFindElement::have-type:
267    * @typefind: the typefind instance
268    * @probability: the probability of the type found
269    * @caps: the caps of the type found
270    *
271    * This signal gets emitted when the type and its probability has
272    * been found.
273    */
274   gst_type_find_element_signals[HAVE_TYPE] = g_signal_new ("have-type",
275       G_TYPE_FROM_CLASS (typefind_class), G_SIGNAL_RUN_LAST,
276       G_STRUCT_OFFSET (GstTypeFindElementClass, have_type), NULL, NULL,
277       g_cclosure_marshal_generic, G_TYPE_NONE, 2,
278       G_TYPE_UINT, GST_TYPE_CAPS | G_SIGNAL_TYPE_STATIC_SCOPE);
279
280   typefind_class->have_type =
281       GST_DEBUG_FUNCPTR (gst_type_find_element_have_type);
282
283   gst_element_class_set_static_metadata (gstelement_class,
284       "TypeFind",
285       "Generic",
286       "Finds the media type of a stream",
287       "Benjamin Otte <in7y118@public.uni-hamburg.de>");
288   gst_element_class_add_static_pad_template (gstelement_class,
289       &type_find_element_src_template);
290   gst_element_class_add_static_pad_template (gstelement_class,
291       &type_find_element_sink_template);
292
293   gstelement_class->change_state =
294       GST_DEBUG_FUNCPTR (gst_type_find_element_change_state);
295 }
296
297 static void
298 gst_type_find_element_init (GstTypeFindElement * typefind)
299 {
300   /* sinkpad */
301   typefind->sink =
302       gst_pad_new_from_static_template (&type_find_element_sink_template,
303       "sink");
304
305   gst_pad_set_activate_function (typefind->sink,
306       GST_DEBUG_FUNCPTR (gst_type_find_element_activate_sink));
307   gst_pad_set_activatemode_function (typefind->sink,
308       GST_DEBUG_FUNCPTR (gst_type_find_element_activate_sink_mode));
309   gst_pad_set_chain_function (typefind->sink,
310       GST_DEBUG_FUNCPTR (gst_type_find_element_chain));
311   gst_pad_set_event_function (typefind->sink,
312       GST_DEBUG_FUNCPTR (gst_type_find_element_sink_event));
313   GST_PAD_SET_PROXY_ALLOCATION (typefind->sink);
314   gst_element_add_pad (GST_ELEMENT (typefind), typefind->sink);
315
316   /* srcpad */
317   typefind->src =
318       gst_pad_new_from_static_template (&type_find_element_src_template, "src");
319
320   gst_pad_set_activatemode_function (typefind->src,
321       GST_DEBUG_FUNCPTR (gst_type_find_element_activate_src_mode));
322   gst_pad_set_getrange_function (typefind->src,
323       GST_DEBUG_FUNCPTR (gst_type_find_element_getrange));
324   gst_pad_set_event_function (typefind->src,
325       GST_DEBUG_FUNCPTR (gst_type_find_element_src_event));
326   gst_pad_set_query_function (typefind->src,
327       GST_DEBUG_FUNCPTR (gst_type_find_handle_src_query));
328   gst_pad_use_fixed_caps (typefind->src);
329   gst_element_add_pad (GST_ELEMENT (typefind), typefind->src);
330
331   typefind->mode = MODE_TYPEFIND;
332   typefind->caps = NULL;
333   typefind->min_probability = 1;
334
335   typefind->adapter = gst_adapter_new ();
336 }
337
338 static void
339 gst_type_find_element_dispose (GObject * object)
340 {
341   GstTypeFindElement *typefind = GST_TYPE_FIND_ELEMENT (object);
342
343   if (typefind->adapter) {
344     g_object_unref (typefind->adapter);
345     typefind->adapter = NULL;
346   }
347
348   if (typefind->force_caps) {
349     gst_caps_unref (typefind->force_caps);
350     typefind->force_caps = NULL;
351   }
352
353   G_OBJECT_CLASS (parent_class)->dispose (object);
354 }
355
356 static void
357 gst_type_find_element_set_property (GObject * object, guint prop_id,
358     const GValue * value, GParamSpec * pspec)
359 {
360   GstTypeFindElement *typefind;
361
362   typefind = GST_TYPE_FIND_ELEMENT (object);
363
364   switch (prop_id) {
365     case PROP_MINIMUM:
366       typefind->min_probability = g_value_get_uint (value);
367       break;
368     case PROP_FORCE_CAPS:
369       GST_OBJECT_LOCK (typefind);
370       if (typefind->force_caps)
371         gst_caps_unref (typefind->force_caps);
372       typefind->force_caps = g_value_dup_boxed (value);
373       GST_OBJECT_UNLOCK (typefind);
374       break;
375     default:
376       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
377       break;
378   }
379 }
380
381 static void
382 gst_type_find_element_get_property (GObject * object, guint prop_id,
383     GValue * value, GParamSpec * pspec)
384 {
385   GstTypeFindElement *typefind;
386
387   typefind = GST_TYPE_FIND_ELEMENT (object);
388
389   switch (prop_id) {
390     case PROP_CAPS:
391       GST_OBJECT_LOCK (typefind);
392       g_value_set_boxed (value, typefind->caps);
393       GST_OBJECT_UNLOCK (typefind);
394       break;
395     case PROP_MINIMUM:
396       g_value_set_uint (value, typefind->min_probability);
397       break;
398     case PROP_FORCE_CAPS:
399       GST_OBJECT_LOCK (typefind);
400       g_value_set_boxed (value, typefind->force_caps);
401       GST_OBJECT_UNLOCK (typefind);
402       break;
403     default:
404       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
405       break;
406   }
407 }
408
409 static gboolean
410 gst_type_find_handle_src_query (GstPad * pad, GstObject * parent,
411     GstQuery * query)
412 {
413   GstTypeFindElement *typefind;
414   gboolean res = FALSE;
415
416   typefind = GST_TYPE_FIND_ELEMENT (parent);
417   GST_DEBUG_OBJECT (typefind, "Handling src query %s",
418       GST_QUERY_TYPE_NAME (query));
419
420   switch (GST_QUERY_TYPE (query)) {
421     case GST_QUERY_SCHEDULING:
422       /* FIXME, filter out the scheduling modes that we understand */
423       res = gst_pad_peer_query (typefind->sink, query);
424       break;
425     case GST_QUERY_CAPS:
426     {
427       GST_DEBUG_OBJECT (typefind,
428           "Got caps query, our caps are %" GST_PTR_FORMAT, typefind->caps);
429
430       /* We can hijack caps query if we typefind already */
431       if (typefind->caps) {
432         gst_query_set_caps_result (query, typefind->caps);
433         res = TRUE;
434       } else {
435         res = gst_pad_peer_query (typefind->sink, query);
436       }
437       break;
438     }
439     case GST_QUERY_POSITION:
440     {
441       gint64 peer_pos;
442       GstFormat format;
443
444       if (!(res = gst_pad_peer_query (typefind->sink, query)))
445         goto out;
446
447       gst_query_parse_position (query, &format, &peer_pos);
448
449       GST_OBJECT_LOCK (typefind);
450       /* FIXME: this code assumes that there's no discont in the queue */
451       switch (format) {
452         case GST_FORMAT_BYTES:
453           peer_pos -= gst_adapter_available (typefind->adapter);
454           if (peer_pos < 0)     /* Clamp result to 0 */
455             peer_pos = 0;
456           break;
457         default:
458           /* FIXME */
459           break;
460       }
461       GST_OBJECT_UNLOCK (typefind);
462       gst_query_set_position (query, format, peer_pos);
463       break;
464     }
465     default:
466       res = gst_pad_query_default (pad, parent, query);
467       break;
468   }
469 out:
470   return res;
471 }
472
473 static gboolean
474 gst_type_find_element_seek (GstTypeFindElement * typefind, GstEvent * event)
475 {
476   GstSeekFlags flags;
477   GstSeekType start_type, stop_type;
478   GstFormat format;
479   gboolean flush;
480   gdouble rate;
481   gint64 start, stop;
482   GstSegment seeksegment = { 0, };
483
484   gst_event_parse_seek (event, &rate, &format, &flags, &start_type, &start,
485       &stop_type, &stop);
486
487   /* we can only seek on bytes */
488   if (format != GST_FORMAT_BYTES) {
489     GST_DEBUG_OBJECT (typefind, "Can only seek on BYTES");
490     return FALSE;
491   }
492
493   /* copy segment, we need this because we still need the old
494    * segment when we close the current segment. */
495   memcpy (&seeksegment, &typefind->segment, sizeof (GstSegment));
496
497   GST_DEBUG_OBJECT (typefind, "configuring seek");
498   gst_segment_do_seek (&seeksegment, rate, format, flags,
499       start_type, start, stop_type, stop, NULL);
500
501   flush = ! !(flags & GST_SEEK_FLAG_FLUSH);
502
503   GST_DEBUG_OBJECT (typefind, "New segment %" GST_SEGMENT_FORMAT, &seeksegment);
504
505   if (flush) {
506     GST_DEBUG_OBJECT (typefind, "Starting flush");
507     gst_pad_push_event (typefind->sink, gst_event_new_flush_start ());
508     gst_pad_push_event (typefind->src, gst_event_new_flush_start ());
509   } else {
510     GST_DEBUG_OBJECT (typefind, "Non-flushing seek, pausing task");
511     gst_pad_pause_task (typefind->sink);
512   }
513
514   /* now grab the stream lock so that streaming cannot continue, for
515    * non flushing seeks when the element is in PAUSED this could block
516    * forever. */
517   GST_DEBUG_OBJECT (typefind, "Waiting for streaming to stop");
518   GST_PAD_STREAM_LOCK (typefind->sink);
519
520   if (flush) {
521     GST_DEBUG_OBJECT (typefind, "Stopping flush");
522     gst_pad_push_event (typefind->sink, gst_event_new_flush_stop (TRUE));
523     gst_pad_push_event (typefind->src, gst_event_new_flush_stop (TRUE));
524   }
525
526   /* now update the real segment info */
527   GST_DEBUG_OBJECT (typefind, "Committing new seek segment");
528   memcpy (&typefind->segment, &seeksegment, sizeof (GstSegment));
529   typefind->offset = typefind->segment.start;
530
531   /* notify start of new segment */
532   if (typefind->segment.flags & GST_SEGMENT_FLAG_SEGMENT) {
533     GstMessage *msg;
534
535     msg = gst_message_new_segment_start (GST_OBJECT (typefind),
536         GST_FORMAT_BYTES, typefind->segment.start);
537     gst_element_post_message (GST_ELEMENT (typefind), msg);
538   }
539
540   typefind->need_segment = TRUE;
541
542   /* restart our task since it might have been stopped when we did the
543    * flush. */
544   gst_pad_start_task (typefind->sink,
545       (GstTaskFunction) gst_type_find_element_loop, typefind->sink, NULL);
546
547   /* streaming can continue now */
548   GST_PAD_STREAM_UNLOCK (typefind->sink);
549
550   return TRUE;
551 }
552
553 static gboolean
554 gst_type_find_element_src_event (GstPad * pad, GstObject * parent,
555     GstEvent * event)
556 {
557   GstTypeFindElement *typefind = GST_TYPE_FIND_ELEMENT (parent);
558   gboolean result;
559
560   if (typefind->mode != MODE_NORMAL) {
561     /* need to do more? */
562     GST_LOG_OBJECT (typefind, "Still typefinding. Not passing event upstream");
563     gst_event_unref (event);
564     return FALSE;
565   }
566
567   /* Only handle seeks here if driving the pipeline */
568   if (typefind->segment.format != GST_FORMAT_UNDEFINED &&
569       GST_EVENT_TYPE (event) == GST_EVENT_SEEK) {
570     result = gst_type_find_element_seek (typefind, event);
571     gst_event_unref (event);
572     return result;
573   } else {
574     return gst_pad_push_event (typefind->sink, event);
575   }
576 }
577
578 static void
579 start_typefinding (GstTypeFindElement * typefind)
580 {
581   GST_DEBUG_OBJECT (typefind, "starting typefinding");
582
583   GST_OBJECT_LOCK (typefind);
584   if (typefind->caps)
585     gst_caps_replace (&typefind->caps, NULL);
586   typefind->initial_offset = GST_BUFFER_OFFSET_NONE;
587   GST_OBJECT_UNLOCK (typefind);
588
589   typefind->mode = MODE_TYPEFIND;
590 }
591
592 static void
593 stop_typefinding (GstTypeFindElement * typefind)
594 {
595   GstState state;
596   gboolean push_cached_buffers;
597   gsize avail;
598   GstBuffer *buffer;
599   GstClockTime pts, dts;
600
601   gst_element_get_state (GST_ELEMENT (typefind), &state, NULL, 0);
602
603   push_cached_buffers = (state >= GST_STATE_PAUSED && typefind->caps);
604
605   GST_DEBUG_OBJECT (typefind, "stopping typefinding%s",
606       push_cached_buffers ? " and pushing cached events and buffers" : "");
607
608   typefind->mode = MODE_NORMAL;
609   if (push_cached_buffers)
610     gst_type_find_element_send_cached_events (typefind);
611
612   GST_OBJECT_LOCK (typefind);
613   avail = gst_adapter_available (typefind->adapter);
614   if (avail == 0)
615     goto no_data;
616
617   pts = gst_adapter_prev_pts (typefind->adapter, NULL);
618   dts = gst_adapter_prev_dts (typefind->adapter, NULL);
619   buffer = gst_adapter_take_buffer (typefind->adapter, avail);
620   GST_BUFFER_PTS (buffer) = pts;
621   GST_BUFFER_DTS (buffer) = dts;
622   GST_BUFFER_OFFSET (buffer) = typefind->initial_offset;
623   GST_OBJECT_UNLOCK (typefind);
624
625   if (!push_cached_buffers) {
626     gst_buffer_unref (buffer);
627   } else {
628     GstPad *peer = gst_pad_get_peer (typefind->src);
629
630     /* make sure the user gets a meaningful error message in this case,
631      * which is not a core bug or bug of any kind (as the default error
632      * message emitted by gstpad.c otherwise would make you think) */
633     if (peer && GST_PAD_CHAINFUNC (peer) == NULL) {
634       GST_DEBUG_OBJECT (typefind, "upstream only supports push mode, while "
635           "downstream element only works in pull mode, erroring out");
636       GST_ELEMENT_ERROR (typefind, STREAM, FAILED,
637           ("%s cannot work in push mode. The operation is not supported "
638               "with this source element or protocol.",
639               G_OBJECT_TYPE_NAME (GST_PAD_PARENT (peer))),
640           ("Downstream pad %s:%s has no chainfunction, and the upstream "
641               "element does not support pull mode", GST_DEBUG_PAD_NAME (peer)));
642       typefind->mode = MODE_ERROR;      /* make the chain function error out */
643       gst_buffer_unref (buffer);
644     } else {
645       gst_pad_push (typefind->src, buffer);
646     }
647     if (peer)
648       gst_object_unref (peer);
649   }
650   return;
651
652   /* ERRORS */
653 no_data:
654   {
655     GST_DEBUG_OBJECT (typefind, "we have no data to typefind");
656     GST_OBJECT_UNLOCK (typefind);
657     return;
658   }
659 }
660
661 static gboolean
662 gst_type_find_element_sink_event (GstPad * pad, GstObject * parent,
663     GstEvent * event)
664 {
665   gboolean res = FALSE;
666   GstTypeFindElement *typefind = GST_TYPE_FIND_ELEMENT (parent);
667
668   GST_DEBUG_OBJECT (typefind, "got %s event in mode %d",
669       GST_EVENT_TYPE_NAME (event), typefind->mode);
670
671   switch (typefind->mode) {
672     case MODE_TYPEFIND:
673       switch (GST_EVENT_TYPE (event)) {
674         case GST_EVENT_CAPS:
675         {
676           GstCaps *caps;
677
678           /* Parse and push out our caps and data */
679           gst_event_parse_caps (event, &caps);
680           res = gst_type_find_element_setcaps (typefind, caps);
681
682           gst_event_unref (event);
683           break;
684         }
685         case GST_EVENT_GAP:
686         {
687           GST_FIXME_OBJECT (typefind,
688               "GAP events during typefinding not handled properly");
689
690           /* FIXME: These would need to be inserted in the stream at
691            * the right position between buffers, but we combine all
692            * buffers with a GstAdapter. Drop the GAP event for now,
693            * which will only cause an implicit GAP between buffers.
694            */
695           gst_event_unref (event);
696           res = TRUE;
697           break;
698         }
699         case GST_EVENT_EOS:
700         {
701           GST_INFO_OBJECT (typefind, "Got EOS and no type found yet");
702           gst_type_find_element_chain_do_typefinding (typefind, FALSE, TRUE);
703
704           res = gst_pad_push_event (typefind->src, event);
705           break;
706         }
707         case GST_EVENT_FLUSH_STOP:{
708           GList *l;
709
710           GST_OBJECT_LOCK (typefind);
711
712           for (l = typefind->cached_events; l; l = l->next) {
713             if (GST_EVENT_IS_STICKY (l->data) &&
714                 GST_EVENT_TYPE (l->data) != GST_EVENT_SEGMENT &&
715                 GST_EVENT_TYPE (l->data) != GST_EVENT_EOS) {
716               gst_pad_store_sticky_event (typefind->src, l->data);
717             }
718             gst_event_unref (l->data);
719           }
720
721           g_list_free (typefind->cached_events);
722           typefind->cached_events = NULL;
723           gst_adapter_clear (typefind->adapter);
724           GST_OBJECT_UNLOCK (typefind);
725           /* fall through */
726         }
727         case GST_EVENT_FLUSH_START:
728           res = gst_pad_push_event (typefind->src, event);
729           break;
730         default:
731           /* Forward events that would happen before the caps event
732            * directly instead of storing them. There's no reason not
733            * to send them directly and we should only store events
734            * for later sending that would need to come after the caps
735            * event */
736           if (GST_EVENT_TYPE (event) < GST_EVENT_CAPS) {
737             res = gst_pad_push_event (typefind->src, event);
738           } else {
739             GST_DEBUG_OBJECT (typefind, "Saving %s event to send later",
740                 GST_EVENT_TYPE_NAME (event));
741             GST_OBJECT_LOCK (typefind);
742             typefind->cached_events =
743                 g_list_append (typefind->cached_events, event);
744             GST_OBJECT_UNLOCK (typefind);
745             res = TRUE;
746           }
747           break;
748       }
749       break;
750     case MODE_NORMAL:
751       res = gst_pad_push_event (typefind->src, event);
752       break;
753     case MODE_ERROR:
754       break;
755     default:
756       g_assert_not_reached ();
757   }
758   return res;
759 }
760
761 static void
762 gst_type_find_element_send_cached_events (GstTypeFindElement * typefind)
763 {
764   GList *l, *cached_events;
765
766   GST_OBJECT_LOCK (typefind);
767   cached_events = typefind->cached_events;
768   typefind->cached_events = NULL;
769   GST_OBJECT_UNLOCK (typefind);
770
771   for (l = cached_events; l != NULL; l = l->next) {
772     GstEvent *event = GST_EVENT (l->data);
773
774     GST_DEBUG_OBJECT (typefind, "sending cached %s event",
775         GST_EVENT_TYPE_NAME (event));
776     gst_pad_push_event (typefind->src, event);
777   }
778   g_list_free (cached_events);
779 }
780
781 static gboolean
782 gst_type_find_element_setcaps (GstTypeFindElement * typefind, GstCaps * caps)
783 {
784   /* don't operate on ANY caps */
785   if (gst_caps_is_any (caps))
786     return TRUE;
787
788   /* Set to MODE_NORMAL before emitting have-type, in case it triggers a seek */
789   typefind->mode = MODE_NORMAL;
790   gst_type_find_element_emit_have_type (typefind, GST_TYPE_FIND_MAXIMUM, caps);
791
792   /* Shortcircuit typefinding if we get caps */
793   GST_DEBUG_OBJECT (typefind, "Skipping typefinding, using caps from "
794       "upstream: %" GST_PTR_FORMAT, caps);
795
796   stop_typefinding (typefind);
797
798   return TRUE;
799 }
800
801 static gchar *
802 gst_type_find_get_extension (GstTypeFindElement * typefind, GstPad * pad)
803 {
804   GstQuery *query;
805   gchar *uri, *result;
806   size_t len;
807   gint find;
808
809   query = gst_query_new_uri ();
810
811   /* try getting the caps with an uri query and from the extension */
812   if (!gst_pad_peer_query (pad, query))
813     goto peer_query_failed;
814
815   gst_query_parse_uri (query, &uri);
816   if (uri == NULL)
817     goto no_uri;
818
819   GST_DEBUG_OBJECT (typefind, "finding extension of %s", uri);
820
821   /* find the extension on the uri, this is everything after a '.' */
822   len = strlen (uri);
823   find = len - 1;
824
825   while (find >= 0) {
826     if (uri[find] == '.')
827       break;
828     find--;
829   }
830   if (find < 0)
831     goto no_extension;
832
833   result = g_strdup (&uri[find + 1]);
834
835   GST_DEBUG_OBJECT (typefind, "found extension %s", result);
836   gst_query_unref (query);
837   g_free (uri);
838
839   return result;
840
841   /* ERRORS */
842 peer_query_failed:
843   {
844     GST_WARNING_OBJECT (typefind, "failed to query peer uri");
845     gst_query_unref (query);
846     return NULL;
847   }
848 no_uri:
849   {
850     GST_WARNING_OBJECT (typefind, "could not parse the peer uri");
851     gst_query_unref (query);
852     return NULL;
853   }
854 no_extension:
855   {
856     GST_WARNING_OBJECT (typefind, "could not find uri extension in %s", uri);
857     gst_query_unref (query);
858     g_free (uri);
859     return NULL;
860   }
861 }
862
863 static GstCaps *
864 gst_type_find_guess_by_extension (GstTypeFindElement * typefind, GstPad * pad,
865     GstTypeFindProbability * probability)
866 {
867   gchar *ext;
868   GstCaps *caps;
869
870   ext = gst_type_find_get_extension (typefind, pad);
871   if (!ext)
872     return NULL;
873
874   caps = gst_type_find_helper_for_extension (GST_OBJECT_CAST (typefind), ext);
875   if (caps)
876     *probability = GST_TYPE_FIND_MAXIMUM;
877
878   g_free (ext);
879
880   return caps;
881 }
882
883 static GstFlowReturn
884 gst_type_find_element_chain (GstPad * pad, GstObject * parent,
885     GstBuffer * buffer)
886 {
887   GstTypeFindElement *typefind;
888   GstFlowReturn res = GST_FLOW_OK;
889
890   typefind = GST_TYPE_FIND_ELEMENT (parent);
891
892   GST_LOG_OBJECT (typefind, "handling buffer in mode %d", typefind->mode);
893
894   switch (typefind->mode) {
895     case MODE_ERROR:
896       /* we should already have called GST_ELEMENT_ERROR */
897       return GST_FLOW_ERROR;
898     case MODE_NORMAL:
899       /* don't take object lock as typefind->caps should not change anymore */
900       return gst_pad_push (typefind->src, buffer);
901     case MODE_TYPEFIND:
902     {
903       GST_OBJECT_LOCK (typefind);
904       if (typefind->initial_offset == GST_BUFFER_OFFSET_NONE)
905         typefind->initial_offset = GST_BUFFER_OFFSET (buffer);
906       gst_adapter_push (typefind->adapter, buffer);
907       GST_OBJECT_UNLOCK (typefind);
908
909       res = gst_type_find_element_chain_do_typefinding (typefind, TRUE, FALSE);
910
911       if (typefind->mode == MODE_ERROR)
912         res = GST_FLOW_ERROR;
913
914       break;
915     }
916     default:
917       g_assert_not_reached ();
918       return GST_FLOW_ERROR;
919   }
920
921   return res;
922 }
923
924 static GstFlowReturn
925 gst_type_find_element_chain_do_typefinding (GstTypeFindElement * typefind,
926     gboolean check_avail, gboolean at_eos)
927 {
928   GstTypeFindProbability probability;
929   GstCaps *caps = NULL;
930   gsize avail;
931   const guint8 *data;
932   gboolean have_min, have_max;
933   gchar *ext;
934
935   GST_OBJECT_LOCK (typefind);
936   if (typefind->force_caps) {
937     caps = gst_caps_ref (typefind->force_caps);
938     probability = GST_TYPE_FIND_MAXIMUM;
939   }
940
941   if (!caps) {
942     avail = gst_adapter_available (typefind->adapter);
943
944     if (check_avail) {
945       have_min = avail >= TYPE_FIND_MIN_SIZE;
946       have_max = avail >= TYPE_FIND_MAX_SIZE;
947     } else {
948       have_min = avail > 0;
949       have_max = TRUE;
950     }
951
952     if (!have_min)
953       goto not_enough_data;
954
955     ext = gst_type_find_get_extension (typefind, typefind->sink);
956     /* map all available data */
957     data = gst_adapter_map (typefind->adapter, avail);
958     caps = gst_type_find_helper_for_data_with_extension (GST_OBJECT (typefind),
959         data, avail, ext, &probability);
960     gst_adapter_unmap (typefind->adapter);
961     g_free (ext);
962
963     if (caps == NULL && have_max)
964       goto no_type_found;
965     else if (caps == NULL)
966       goto wait_for_data;
967
968     /* found a type */
969     if (probability < typefind->min_probability)
970       goto low_probability;
971   }
972
973   GST_OBJECT_UNLOCK (typefind);
974
975   /* probability is good enough too, so let's make it known ... emitting this
976    * signal calls our object handler which sets the caps. */
977   /* Set to MODE_NORMAL before emitting have-type, in case it triggers a seek */
978   typefind->mode = MODE_NORMAL;
979   gst_type_find_element_emit_have_type (typefind, probability, caps);
980
981   /* .. and send out the accumulated data */
982   stop_typefinding (typefind);
983   gst_caps_unref (caps);
984
985   return GST_FLOW_OK;
986
987 not_enough_data:
988   {
989     GST_OBJECT_UNLOCK (typefind);
990
991     if (at_eos) {
992       GST_ELEMENT_ERROR (typefind, STREAM, TYPE_NOT_FOUND,
993           (_("Stream doesn't contain enough data.")),
994           ("Can't typefind stream"));
995       return GST_FLOW_ERROR;
996     } else {
997       GST_DEBUG_OBJECT (typefind, "not enough data for typefinding yet "
998           "(%" G_GSIZE_FORMAT " bytes)", avail);
999       return GST_FLOW_OK;
1000     }
1001   }
1002 no_type_found:
1003   {
1004     GST_OBJECT_UNLOCK (typefind);
1005     GST_ELEMENT_ERROR (typefind, STREAM, TYPE_NOT_FOUND, (NULL), (NULL));
1006     stop_typefinding (typefind);
1007     return GST_FLOW_ERROR;
1008   }
1009 wait_for_data:
1010   {
1011     GST_OBJECT_UNLOCK (typefind);
1012
1013     if (at_eos) {
1014       GST_ELEMENT_ERROR (typefind, STREAM, TYPE_NOT_FOUND,
1015           (_("Stream doesn't contain enough data.")),
1016           ("Can't typefind stream"));
1017       return GST_FLOW_ERROR;
1018     } else {
1019       GST_DEBUG_OBJECT (typefind,
1020           "no caps found with %" G_GSIZE_FORMAT " bytes of data, "
1021           "waiting for more data", avail);
1022       return GST_FLOW_OK;
1023     }
1024   }
1025 low_probability:
1026   {
1027     GST_DEBUG_OBJECT (typefind, "found caps %" GST_PTR_FORMAT ", but "
1028         "probability is %u which is lower than the required minimum of %u",
1029         caps, probability, typefind->min_probability);
1030
1031     gst_caps_unref (caps);
1032
1033     if (have_max)
1034       goto no_type_found;
1035
1036     GST_OBJECT_UNLOCK (typefind);
1037     GST_DEBUG_OBJECT (typefind, "waiting for more data to try again");
1038     return GST_FLOW_OK;
1039   }
1040 }
1041
1042 static GstFlowReturn
1043 gst_type_find_element_getrange (GstPad * srcpad, GstObject * parent,
1044     guint64 offset, guint length, GstBuffer ** buffer)
1045 {
1046   GstTypeFindElement *typefind;
1047   GstFlowReturn ret;
1048
1049   typefind = GST_TYPE_FIND_ELEMENT (parent);
1050
1051   ret = gst_pad_pull_range (typefind->sink, offset, length, buffer);
1052
1053   return ret;
1054 }
1055
1056 static gboolean
1057 gst_type_find_element_activate_src_mode (GstPad * pad, GstObject * parent,
1058     GstPadMode mode, gboolean active)
1059 {
1060   gboolean res;
1061   GstTypeFindElement *typefind;
1062
1063   typefind = GST_TYPE_FIND_ELEMENT (parent);
1064
1065   switch (mode) {
1066     case GST_PAD_MODE_PULL:
1067       /* make sure our task stops pushing, we can't call _stop here because this
1068        * activation might happen from the streaming thread. */
1069       gst_pad_pause_task (typefind->sink);
1070       res = gst_pad_activate_mode (typefind->sink, mode, active);
1071       break;
1072     default:
1073       res = TRUE;
1074       break;
1075   }
1076   return res;
1077 }
1078
1079 static void
1080 gst_type_find_element_loop (GstPad * pad)
1081 {
1082   GstTypeFindElement *typefind;
1083   GstFlowReturn ret = GST_FLOW_OK;
1084
1085   typefind = GST_TYPE_FIND_ELEMENT (GST_PAD_PARENT (pad));
1086
1087   if (typefind->need_stream_start) {
1088     gchar *stream_id;
1089     GstEvent *event;
1090
1091     stream_id = gst_pad_create_stream_id (typefind->src,
1092         GST_ELEMENT_CAST (typefind), NULL);
1093
1094     GST_DEBUG_OBJECT (typefind, "Pushing STREAM_START");
1095     event = gst_event_new_stream_start (stream_id);
1096     gst_event_set_group_id (event, gst_util_group_id_next ());
1097     gst_pad_push_event (typefind->src, event);
1098
1099     typefind->need_stream_start = FALSE;
1100     g_free (stream_id);
1101   }
1102
1103   if (typefind->mode == MODE_TYPEFIND) {
1104     GstPad *peer = NULL;
1105     GstCaps *found_caps = NULL;
1106     GstTypeFindProbability probability = GST_TYPE_FIND_NONE;
1107
1108     GST_DEBUG_OBJECT (typefind, "find type in pull mode");
1109
1110     GST_OBJECT_LOCK (typefind);
1111     if (typefind->force_caps) {
1112       found_caps = gst_caps_ref (typefind->force_caps);
1113       probability = GST_TYPE_FIND_MAXIMUM;
1114     }
1115     GST_OBJECT_UNLOCK (typefind);
1116
1117     if (!found_caps) {
1118       peer = gst_pad_get_peer (pad);
1119       if (peer) {
1120         gint64 size;
1121         gchar *ext;
1122
1123         if (!gst_pad_query_duration (peer, GST_FORMAT_BYTES, &size)) {
1124           GST_WARNING_OBJECT (typefind, "Could not query upstream length!");
1125           gst_object_unref (peer);
1126
1127           ret = GST_FLOW_ERROR;
1128           goto pause;
1129         }
1130
1131         /* the size if 0, we cannot continue */
1132         if (size == 0) {
1133           /* keep message in sync with message in sink event handler */
1134           GST_ELEMENT_ERROR (typefind, STREAM, TYPE_NOT_FOUND,
1135               (_("Stream contains no data.")), ("Can't typefind empty stream"));
1136           gst_object_unref (peer);
1137           ret = GST_FLOW_ERROR;
1138           goto pause;
1139         }
1140         ext = gst_type_find_get_extension (typefind, pad);
1141
1142         found_caps =
1143             gst_type_find_helper_get_range (GST_OBJECT_CAST (peer),
1144             GST_OBJECT_PARENT (peer),
1145             (GstTypeFindHelperGetRangeFunction) (GST_PAD_GETRANGEFUNC (peer)),
1146             (guint64) size, ext, &probability);
1147         g_free (ext);
1148
1149         GST_DEBUG ("Found caps %" GST_PTR_FORMAT, found_caps);
1150
1151         gst_object_unref (peer);
1152       }
1153     }
1154
1155     if (!found_caps || probability < typefind->min_probability) {
1156       GST_DEBUG ("Trying to guess using extension");
1157       gst_caps_replace (&found_caps, NULL);
1158       found_caps =
1159           gst_type_find_guess_by_extension (typefind, pad, &probability);
1160     }
1161
1162     if (!found_caps || probability < typefind->min_probability) {
1163       GST_ELEMENT_ERROR (typefind, STREAM, TYPE_NOT_FOUND, (NULL), (NULL));
1164       gst_caps_replace (&found_caps, NULL);
1165       ret = GST_FLOW_ERROR;
1166       goto pause;
1167     }
1168
1169     GST_DEBUG ("Emitting found caps %" GST_PTR_FORMAT, found_caps);
1170     /* Set to MODE_NORMAL before emitting have-type, in case it triggers a seek */
1171     typefind->mode = MODE_NORMAL;
1172     gst_type_find_element_emit_have_type (typefind, probability, found_caps);
1173     gst_caps_unref (found_caps);
1174   } else if (typefind->mode == MODE_NORMAL) {
1175     GstBuffer *outbuf = NULL;
1176
1177     if (typefind->need_segment) {
1178       typefind->need_segment = FALSE;
1179       gst_pad_push_event (typefind->src,
1180           gst_event_new_segment (&typefind->segment));
1181     }
1182
1183     /* Pull 4k blocks and send downstream */
1184     ret = gst_pad_pull_range (typefind->sink, typefind->offset, 4096, &outbuf);
1185     if (ret != GST_FLOW_OK)
1186       goto pause;
1187
1188     typefind->offset += gst_buffer_get_size (outbuf);
1189
1190     ret = gst_pad_push (typefind->src, outbuf);
1191     if (ret != GST_FLOW_OK)
1192       goto pause;
1193   } else {
1194     /* Error out */
1195     ret = GST_FLOW_ERROR;
1196     goto pause;
1197   }
1198
1199   return;
1200
1201 pause:
1202   {
1203     const gchar *reason = gst_flow_get_name (ret);
1204     gboolean push_eos = FALSE;
1205
1206     GST_LOG_OBJECT (typefind, "pausing task, reason %s", reason);
1207     gst_pad_pause_task (typefind->sink);
1208
1209     if (ret == GST_FLOW_EOS) {
1210       /* perform EOS logic */
1211
1212       if (typefind->segment.flags & GST_SEGMENT_FLAG_SEGMENT) {
1213         gint64 stop;
1214
1215         /* for segment playback we need to post when (in stream time)
1216          * we stopped, this is either stop (when set) or the duration. */
1217         if ((stop = typefind->segment.stop) == -1)
1218           stop = typefind->offset;
1219
1220         GST_LOG_OBJECT (typefind, "Sending segment done, at end of segment");
1221         gst_element_post_message (GST_ELEMENT (typefind),
1222             gst_message_new_segment_done (GST_OBJECT (typefind),
1223                 GST_FORMAT_BYTES, stop));
1224         gst_pad_push_event (typefind->src,
1225             gst_event_new_segment_done (GST_FORMAT_BYTES, stop));
1226       } else {
1227         push_eos = TRUE;
1228       }
1229     } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
1230       /* for fatal errors we post an error message */
1231       GST_ELEMENT_FLOW_ERROR (typefind, ret);
1232       push_eos = TRUE;
1233     }
1234     if (push_eos) {
1235       /* send EOS, and prevent hanging if no streams yet */
1236       GST_LOG_OBJECT (typefind, "Sending EOS, at end of stream");
1237       gst_pad_push_event (typefind->src, gst_event_new_eos ());
1238     }
1239     return;
1240   }
1241 }
1242
1243 static gboolean
1244 gst_type_find_element_activate_sink_mode (GstPad * pad, GstObject * parent,
1245     GstPadMode mode, gboolean active)
1246 {
1247   gboolean res;
1248   GstTypeFindElement *typefind;
1249
1250   typefind = GST_TYPE_FIND_ELEMENT (parent);
1251
1252   switch (mode) {
1253     case GST_PAD_MODE_PULL:
1254       if (active) {
1255         gst_segment_init (&typefind->segment, GST_FORMAT_BYTES);
1256         typefind->need_segment = TRUE;
1257         typefind->need_stream_start = TRUE;
1258         typefind->offset = 0;
1259         res = TRUE;
1260       } else {
1261         res = gst_pad_stop_task (pad);
1262         gst_segment_init (&typefind->segment, GST_FORMAT_UNDEFINED);
1263       }
1264       break;
1265     case GST_PAD_MODE_PUSH:
1266       if (active) {
1267         gst_segment_init (&typefind->segment, GST_FORMAT_UNDEFINED);
1268         start_typefinding (typefind);
1269       } else {
1270         stop_typefinding (typefind);
1271         gst_segment_init (&typefind->segment, GST_FORMAT_UNDEFINED);
1272       }
1273       res = TRUE;
1274       break;
1275     default:
1276       res = FALSE;
1277       break;
1278   }
1279   return res;
1280 }
1281
1282 static gboolean
1283 gst_type_find_element_activate_sink (GstPad * pad, GstObject * parent)
1284 {
1285   GstQuery *query;
1286   gboolean pull_mode;
1287
1288   query = gst_query_new_scheduling ();
1289
1290   if (!gst_pad_peer_query (pad, query)) {
1291     gst_query_unref (query);
1292     goto typefind_push;
1293   }
1294
1295   pull_mode = gst_query_has_scheduling_mode_with_flags (query,
1296       GST_PAD_MODE_PULL, GST_SCHEDULING_FLAG_SEEKABLE);
1297
1298   gst_query_unref (query);
1299
1300   if (!pull_mode)
1301     goto typefind_push;
1302
1303   if (!gst_pad_activate_mode (pad, GST_PAD_MODE_PULL, TRUE))
1304     goto typefind_push;
1305
1306   /* only start our task if we ourselves decide to start in pull mode */
1307   return gst_pad_start_task (pad, (GstTaskFunction) gst_type_find_element_loop,
1308       pad, NULL);
1309
1310 typefind_push:
1311   {
1312     return gst_pad_activate_mode (pad, GST_PAD_MODE_PUSH, TRUE);
1313   }
1314 }
1315
1316 static GstStateChangeReturn
1317 gst_type_find_element_change_state (GstElement * element,
1318     GstStateChange transition)
1319 {
1320   GstStateChangeReturn ret;
1321   GstTypeFindElement *typefind;
1322
1323   typefind = GST_TYPE_FIND_ELEMENT (element);
1324
1325
1326   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1327
1328   switch (transition) {
1329     case GST_STATE_CHANGE_PAUSED_TO_READY:
1330     case GST_STATE_CHANGE_READY_TO_NULL:
1331       GST_OBJECT_LOCK (typefind);
1332       gst_caps_replace (&typefind->caps, NULL);
1333
1334       g_list_foreach (typefind->cached_events,
1335           (GFunc) gst_mini_object_unref, NULL);
1336       g_list_free (typefind->cached_events);
1337       typefind->cached_events = NULL;
1338       typefind->mode = MODE_TYPEFIND;
1339       GST_OBJECT_UNLOCK (typefind);
1340       break;
1341     default:
1342       break;
1343   }
1344
1345   return ret;
1346 }