2 * Copyright (C) 2002 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2002 Wim Taymans <wtay@chello.be>
5 * gstspideridentity.c: identity element for the spider autoplugger
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
27 #include "gstspideridentity.h"
28 #include "gstspider.h"
30 GST_DEBUG_CATEGORY_STATIC (gst_spider_identity_debug);
31 #define GST_CAT_DEFAULT gst_spider_identity_debug
33 static GstElementDetails gst_spider_identity_details = GST_ELEMENT_DETAILS (
36 "Link between spider and outside elements",
37 "Benjamin Otte <in7y118@public.uni-hamburg.de>"
42 * delete me when meging with spider.c
44 static GstStaticPadTemplate spider_src_factory =
45 GST_STATIC_PAD_TEMPLATE (
52 static GstStaticPadTemplate spider_sink_factory =
53 GST_STATIC_PAD_TEMPLATE (
60 /* SpiderIdentity signals and args */
72 static void gst_spider_identity_class_init (GstSpiderIdentityClass *klass);
73 static void gst_spider_identity_init (GstSpiderIdentity *spider_identity);
75 /* functions set in pads, elements and stuff */
76 static void gst_spider_identity_chain (GstPad *pad, GstBuffer *buf);
77 static GstElementStateReturn gst_spider_identity_change_state (GstElement *element);
78 static GstPadLinkReturn gst_spider_identity_link (GstPad *pad, const GstCaps *caps);
79 static GstCaps * gst_spider_identity_getcaps (GstPad *pad);
81 static void gst_spider_identity_dumb_loop (GstSpiderIdentity *ident);
82 static void gst_spider_identity_src_loop (GstSpiderIdentity *ident);
83 static void gst_spider_identity_sink_loop_type_finding (GstSpiderIdentity *ident);
85 static gboolean gst_spider_identity_handle_src_event (GstPad *pad, GstEvent *event);
88 static void gst_spider_identity_start_type_finding (GstSpiderIdentity *ident);
90 static GstElementClass * parent_class = NULL;
92 static guint gst_spider_identity_signals[LAST_SIGNAL] = { 0 }; */
95 gst_spider_identity_get_type (void)
97 static GType spider_identity_type = 0;
99 if (!spider_identity_type) {
100 static const GTypeInfo spider_identity_info = {
101 sizeof(GstSpiderIdentityClass), NULL,
103 (GClassInitFunc)gst_spider_identity_class_init,
106 sizeof(GstSpiderIdentity),
108 (GInstanceInitFunc)gst_spider_identity_init,
110 spider_identity_type = g_type_register_static (GST_TYPE_ELEMENT, "GstSpiderIdentity",
111 &spider_identity_info, 0);
112 GST_DEBUG_CATEGORY_INIT (gst_spider_identity_debug, "spideridentity",
113 0, "spider autoplugging proxy element");
115 return spider_identity_type;
119 gst_spider_identity_class_init (GstSpiderIdentityClass *klass)
121 GstElementClass *gstelement_class = (GstElementClass *) klass;
123 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
125 /* add our two pad templates */
126 gst_element_class_add_pad_template (gstelement_class,
127 gst_static_pad_template_get (&spider_src_factory));
128 gst_element_class_add_pad_template (gstelement_class,
129 gst_static_pad_template_get (&spider_sink_factory));
130 gst_element_class_set_details (gstelement_class, &gst_spider_identity_details);
132 gstelement_class->change_state = GST_DEBUG_FUNCPTR(gst_spider_identity_change_state);
133 gstelement_class->request_new_pad = GST_DEBUG_FUNCPTR(gst_spider_identity_request_new_pad);
137 gst_spider_identity_init (GstSpiderIdentity *ident)
140 ident->sink = gst_pad_new_from_template (
141 gst_static_pad_template_get (&spider_sink_factory), "sink");
142 gst_element_add_pad (GST_ELEMENT (ident), ident->sink);
143 gst_pad_set_link_function (ident->sink, GST_DEBUG_FUNCPTR (gst_spider_identity_link));
144 gst_pad_set_getcaps_function (ident->sink, GST_DEBUG_FUNCPTR (gst_spider_identity_getcaps));
146 ident->src = gst_pad_new_from_template (
147 gst_static_pad_template_get (&spider_src_factory), "src");
148 gst_element_add_pad (GST_ELEMENT (ident), ident->src);
149 gst_pad_set_link_function (ident->src, GST_DEBUG_FUNCPTR (gst_spider_identity_link));
150 gst_pad_set_getcaps_function (ident->src, GST_DEBUG_FUNCPTR (gst_spider_identity_getcaps));
151 gst_pad_set_event_function (ident->src, GST_DEBUG_FUNCPTR (gst_spider_identity_handle_src_event));
154 ident->plugged = FALSE;
158 gst_spider_identity_chain (GstPad *pad, GstBuffer *buf)
160 GstSpiderIdentity *ident;
162 /*g_print ("chaining on pad %s:%s with buffer %p\n", GST_DEBUG_PAD_NAME (pad), buf);*/
164 g_return_if_fail (pad != NULL);
165 g_return_if_fail (GST_IS_PAD (pad));
166 if (buf == NULL) return;
168 ident = GST_SPIDER_IDENTITY (gst_pad_get_parent (pad));
170 if (GST_IS_EVENT (buf)) {
171 /* start hack for current event stuff here */
172 /* check for unlinked elements and send them the EOS event, too */
173 if (GST_EVENT_TYPE (GST_EVENT (buf)) == GST_EVENT_EOS)
175 GstSpider *spider = (GstSpider *) GST_OBJECT_PARENT (ident);
176 GList *list = spider->links;
179 GstSpiderConnection *conn = (GstSpiderConnection *) list->data;
180 list = g_list_next (list);
181 if (conn->current != (GstElement *) conn->src) {
182 GST_DEBUG ("sending EOS to unconnected element %s from %s",
183 GST_ELEMENT_NAME (conn->src), GST_ELEMENT_NAME (ident));
184 gst_pad_push (conn->src->src, GST_DATA (GST_BUFFER (gst_event_new (GST_EVENT_EOS))));
185 gst_element_set_eos (GST_ELEMENT (conn->src));
189 /* end hack for current event stuff here */
191 gst_pad_event_default (pad, GST_EVENT (buf));
195 if ((ident->src != NULL) && (GST_PAD_PEER (ident->src) != NULL)) {
196 /* g_print("pushing buffer %p (refcount %d - buffersize %d) to pad %s:%s\n", buf, GST_BUFFER_REFCOUNT (buf), GST_BUFFER_SIZE (buf), GST_DEBUG_PAD_NAME (ident->src)); */
197 GST_LOG ( "push %p %" G_GINT64_FORMAT, buf, GST_BUFFER_OFFSET (buf));
198 gst_pad_push (ident->src, GST_DATA (buf));
199 } else if (GST_IS_BUFFER (buf)) {
200 gst_buffer_unref (buf);
204 gst_spider_identity_new_src (gchar *name)
206 GstSpiderIdentity *ret = (GstSpiderIdentity *) gst_element_factory_make ("spideridentity", name);
207 /* set the right functions */
208 gst_element_set_loop_function (GST_ELEMENT (ret), (GstElementLoopFunction) GST_DEBUG_FUNCPTR (gst_spider_identity_src_loop));
213 gst_spider_identity_new_sink (gchar *name)
215 GstSpiderIdentity *ret = (GstSpiderIdentity *) gst_element_factory_make ("spideridentity", name);
217 /* set the right functions */
218 gst_element_set_loop_function (GST_ELEMENT (ret), (GstElementLoopFunction) GST_DEBUG_FUNCPTR (gst_spider_identity_dumb_loop));
223 /* shamelessly stolen from gstqueue.c to get proxy links */
224 static GstPadLinkReturn
225 gst_spider_identity_link (GstPad *pad, const GstCaps *caps)
227 GstSpiderIdentity *spider_identity = GST_SPIDER_IDENTITY (gst_pad_get_parent (pad));
230 if (pad == spider_identity->src)
231 otherpad = spider_identity->sink;
233 otherpad = spider_identity->src;
235 g_return_val_if_fail (otherpad != NULL, GST_PAD_LINK_REFUSED);
236 if (GST_PAD_PEER (otherpad) == NULL)
237 return GST_PAD_LINK_DELAYED;
239 return gst_pad_try_set_caps (otherpad, gst_caps_copy (caps));
243 gst_spider_identity_getcaps (GstPad *pad)
245 GstSpiderIdentity *ident = GST_SPIDER_IDENTITY (gst_pad_get_parent (pad));
248 if (pad == ident->src)
249 otherpad = ident->sink;
251 otherpad = ident->src;
253 if (otherpad != NULL) {
254 if (GST_PAD_PEER (otherpad)) {
255 GstCaps *ret = gst_pad_get_allowed_caps (otherpad);
257 GstCaps *ret2 = gst_caps_intersect (ident->caps, ret);
265 return gst_caps_copy (ident->caps);
267 return gst_caps_new_any ();
271 gst_spider_identity_request_new_pad (GstElement *element, GstPadTemplate *templ, const gchar *name)
273 GstSpiderIdentity *ident;
276 g_return_val_if_fail (templ != NULL, NULL);
277 g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
278 ident = GST_SPIDER_IDENTITY (element);
279 g_return_val_if_fail (ident != NULL, NULL);
280 g_return_val_if_fail (GST_IS_SPIDER_IDENTITY (ident), NULL);
282 switch (GST_PAD_TEMPLATE_DIRECTION (templ))
285 if (ident->sink != NULL) break;
287 GST_DEBUG ( "element %s requests new sink pad", GST_ELEMENT_NAME(ident));
288 ident->sink = gst_pad_new ("sink", GST_PAD_SINK);
289 gst_element_add_pad (GST_ELEMENT (ident), ident->sink);
290 gst_pad_set_link_function (ident->sink, GST_DEBUG_FUNCPTR (gst_spider_identity_link));
291 gst_pad_set_getcaps_function (ident->sink, GST_DEBUG_FUNCPTR (gst_spider_identity_getcaps));
295 if (ident->src != NULL) break;
296 GST_DEBUG ( "element %s requests new src pad", GST_ELEMENT_NAME(ident));
297 ident->src = gst_pad_new ("src", GST_PAD_SRC);
298 gst_element_add_pad (GST_ELEMENT (ident), ident->src);
299 gst_pad_set_link_function (ident->src, GST_DEBUG_FUNCPTR (gst_spider_identity_link));
300 gst_pad_set_getcaps_function (ident->src, GST_DEBUG_FUNCPTR (gst_spider_identity_getcaps));
301 gst_pad_set_event_function (ident->src, GST_DEBUG_FUNCPTR (gst_spider_identity_handle_src_event));
307 GST_DEBUG ( "element %s requested a new pad but none could be created", GST_ELEMENT_NAME(ident));
311 /* this function has to
312 * - start the autoplugger
313 * - start type finding
316 static GstElementStateReturn
317 gst_spider_identity_change_state (GstElement *element)
319 GstSpiderIdentity *ident;
321 GstElementStateReturn ret = GST_STATE_SUCCESS;
324 ident = GST_SPIDER_IDENTITY (element);
325 g_return_val_if_fail (ident != NULL, GST_STATE_FAILURE);
326 g_return_val_if_fail (GST_IS_SPIDER_IDENTITY (ident), GST_STATE_FAILURE);
328 switch (GST_STATE_TRANSITION (element)) {
329 case GST_STATE_PAUSED_TO_READY:
330 gst_caps_replace (&ident->caps, NULL);
332 case GST_STATE_PAUSED_TO_PLAYING:
333 /* autoplugger check */
334 spider = GST_SPIDER (GST_ELEMENT_PARENT (ident));
335 g_return_val_if_fail (spider != NULL, GST_STATE_FAILURE);
336 g_return_val_if_fail (GST_IS_SPIDER (spider), GST_STATE_FAILURE);
338 /* start typefinding or plugging */
339 if ((GST_RPAD_PEER (ident->sink) != NULL) && (GST_RPAD_PEER (ident->src) == NULL))
341 GstCaps *caps = gst_pad_get_caps ((GstPad *) GST_PAD_PEER (ident->sink));
342 if (gst_caps_is_any (caps) || gst_caps_is_empty (caps))
344 gst_spider_identity_start_type_finding (ident);
345 gst_caps_free (caps);
348 gst_spider_identity_plug (ident);
350 gst_caps_free (caps);
352 /* autoplug on src */
353 if ((GST_RPAD_PEER (ident->src) != NULL) && (GST_RPAD_PEER (ident->sink) == NULL))
355 gst_spider_identity_plug (ident);
361 if ((ret != GST_STATE_FAILURE) && (GST_ELEMENT_CLASS (parent_class)->change_state))
362 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element);
368 gst_spider_identity_start_type_finding (GstSpiderIdentity *ident)
370 /* GstElement* typefind;
372 gboolean restart = FALSE;
374 GST_DEBUG ("element %s starts typefinding", GST_ELEMENT_NAME(ident));
375 if (GST_STATE (GST_ELEMENT_PARENT (ident)) == GST_STATE_PLAYING)
377 gst_element_set_state (GST_ELEMENT (GST_ELEMENT_PARENT (ident)), GST_STATE_PAUSED);
381 gst_element_set_loop_function (GST_ELEMENT (ident), (GstElementLoopFunction) GST_DEBUG_FUNCPTR (gst_spider_identity_sink_loop_type_finding));
385 gst_element_set_state (GST_ELEMENT (GST_ELEMENT_PARENT (ident)), GST_STATE_PLAYING);
389 /* since we can't set the loop function to NULL if there's a cothread for us,
390 * we have to use a dumb one
393 gst_spider_identity_dumb_loop (GstSpiderIdentity *ident)
397 g_return_if_fail (ident != NULL);
398 g_return_if_fail (GST_IS_SPIDER_IDENTITY (ident));
399 g_assert (ident->sink != NULL);
401 buf = GST_BUFFER (gst_pad_pull (ident->sink));
403 gst_spider_identity_chain (ident->sink, buf);
405 /* do nothing until we're linked - then disable yourself
408 gst_spider_identity_src_loop (GstSpiderIdentity *ident)
410 /* checks - disable for speed */
411 g_return_if_fail (ident != NULL);
412 g_return_if_fail (GST_IS_SPIDER_IDENTITY (ident));
414 /* we don't want a loop function if we're plugged */
415 if (ident->sink && GST_PAD_PEER (ident->sink))
417 gst_element_set_loop_function (GST_ELEMENT (ident), (GstElementLoopFunction) GST_DEBUG_FUNCPTR (gst_spider_identity_dumb_loop));
418 gst_spider_identity_dumb_loop (ident);
421 gst_element_interrupt (GST_ELEMENT (ident));
423 /* This loop function is only needed when typefinding.
427 guint best_probability;
431 spider_find_peek (gpointer data, gint64 offset, guint size)
433 SpiderTypeFind *find = (SpiderTypeFind *) data;
434 gint64 buffer_offset = GST_BUFFER_OFFSET_IS_VALID (find->buffer) ?
435 GST_BUFFER_OFFSET (find->buffer) : 0;
437 if (offset >= buffer_offset && offset + size <= buffer_offset + GST_BUFFER_SIZE (find->buffer)) {
438 GST_LOG ("peek %"G_GINT64_FORMAT", %u successful", offset, size);
439 return GST_BUFFER_DATA (find->buffer) + offset - buffer_offset;
441 GST_LOG ("peek %"G_GINT64_FORMAT", %u failed", offset, size);
446 spider_find_suggest (gpointer data, guint probability, const GstCaps *caps)
448 SpiderTypeFind *find = (SpiderTypeFind *) data;
450 GST_INFO ("suggest %u, %" GST_PTR_FORMAT, probability, caps);
451 if (probability > find->best_probability) {
452 gst_caps_replace (&find->caps, gst_caps_copy (caps));
453 find->best_probability = probability;
457 gst_spider_identity_sink_loop_type_finding (GstSpiderIdentity *ident)
460 GstTypeFind gst_find;
462 GList *walk, *type_list = NULL;
464 g_return_if_fail (GST_IS_SPIDER_IDENTITY (ident));
466 data = gst_pad_pull (ident->sink);
467 if (!GST_IS_BUFFER (data)) {
468 gst_spider_identity_chain (ident->sink, GST_BUFFER (data));
472 find.buffer = GST_BUFFER (data);
473 /* maybe there are already valid caps now? */
474 find.caps = gst_pad_get_caps (ident->sink);
475 if (! gst_caps_is_empty (find.caps) && !gst_caps_is_any (find.caps)) {
478 gst_caps_free (find.caps);
482 /* now do the actual typefinding with the supplied buffer */
483 walk = type_list = gst_type_find_factory_get_list ();
485 find.best_probability = 0;
487 gst_find.data = &find;
488 gst_find.peek = spider_find_peek;
489 gst_find.suggest = spider_find_suggest;
491 GstTypeFindFactory *factory = GST_TYPE_FIND_FACTORY (walk->data);
493 GST_DEBUG ("trying typefind function %s", GST_PLUGIN_FEATURE_NAME (factory));
494 gst_type_find_factory_call_function (factory, &gst_find);
495 if (find.best_probability >= GST_TYPE_FIND_MAXIMUM)
497 walk = g_list_next (walk);
499 if (find.best_probability > 0)
501 GST_ELEMENT_ERROR (ident, STREAM, TYPE_NOT_FOUND, NULL, NULL);
502 find.buffer = GST_BUFFER (gst_event_new (GST_EVENT_EOS));
505 /* remove loop function */
506 gst_element_set_loop_function (GST_ELEMENT (ident),
507 (GstElementLoopFunction) GST_DEBUG_FUNCPTR (gst_spider_identity_dumb_loop));
509 /* push the buffer */
510 gst_spider_identity_chain (ident->sink, find.buffer);
515 GST_INFO ("typefind function found caps");
516 ident->caps = find.caps;
517 if (GST_PAD_IS_LINKED (ident->src)) {
518 GstPadLinkReturn ret;
520 ret = gst_pad_try_set_caps (ident->src, find.caps);
521 if (GST_PAD_LINK_FAILED (ret)){
522 g_critical("could not set caps on spideridentity src pad\n");
525 GST_LOG_OBJECT (ident, "spider starting caps: %" GST_PTR_FORMAT, find.caps);
527 g_list_free (type_list);
529 gst_spider_identity_plug (ident);
535 gst_spider_identity_handle_src_event (GstPad *pad, GstEvent *event)
538 GstSpiderIdentity *ident;
540 GST_DEBUG ( "spider_identity src_event");
542 ident = GST_SPIDER_IDENTITY (gst_pad_get_parent (pad));
544 switch (GST_EVENT_TYPE (event)) {
545 case GST_EVENT_FLUSH:
548 res = gst_pad_event_default (pad, event);