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