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