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