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