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