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