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