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.
24 #include "gstspideridentity.h"
26 #include "gstspider.h"
28 GstElementDetails gst_spider_identity_details = {
32 "Connection between spider and outside elements",
34 "Benjamin Otte <in7y118@public.uni-hamburg.de>",
40 * delete me when meging with spider.c
42 GST_PAD_TEMPLATE_FACTORY (spider_src_factory,
49 GST_PAD_TEMPLATE_FACTORY (spider_sink_factory,
56 /* SpiderIdentity signals and args */
68 static void gst_spider_identity_class_init (GstSpiderIdentityClass *klass);
69 static void gst_spider_identity_init (GstSpiderIdentity *spider_identity);
71 /* functions set in pads, elements and stuff */
72 static void gst_spider_identity_chain (GstPad *pad, GstBuffer *buf);
73 static GstElementStateReturn gst_spider_identity_change_state (GstElement *element);
74 static GstPadConnectReturn gst_spider_identity_connect (GstPad *pad, GstCaps *caps);
75 static GstCaps * gst_spider_identity_getcaps (GstPad *pad, GstCaps *caps);
77 static void gst_spider_identity_dumb_loop (GstSpiderIdentity *ident);
78 static void gst_spider_identity_src_loop (GstSpiderIdentity *ident);
79 static void gst_spider_identity_sink_loop_type_finding (GstSpiderIdentity *ident);
80 static void gst_spider_identity_sink_loop_emptycache (GstSpiderIdentity *ident);
82 static gboolean gst_spider_identity_handle_src_event (GstPad *pad, GstEvent *event);
84 /* set/get functions */
85 static void gst_spider_identity_set_caps (GstSpiderIdentity *identity, GstCaps *caps);
88 static void callback_type_find_have_type (GstElement *typefind, GstCaps *caps, GstSpiderIdentity *identity);
91 static void gst_spider_identity_start_type_finding (GstSpiderIdentity *ident);
93 static GstElementClass * parent_class = NULL;
95 static guint gst_spider_identity_signals[LAST_SIGNAL] = { 0 }; */
98 gst_spider_identity_get_type (void)
100 static GType spider_identity_type = 0;
102 if (!spider_identity_type) {
103 static const GTypeInfo spider_identity_info = {
104 sizeof(GstSpiderIdentityClass), NULL,
106 (GClassInitFunc)gst_spider_identity_class_init,
109 sizeof(GstSpiderIdentity),
111 (GInstanceInitFunc)gst_spider_identity_init,
113 spider_identity_type = g_type_register_static (GST_TYPE_ELEMENT, "GstSpiderIdentity", &spider_identity_info, 0);
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, GST_PAD_TEMPLATE_GET (spider_src_factory));
127 gst_element_class_add_pad_template (gstelement_class, GST_PAD_TEMPLATE_GET (spider_sink_factory));
129 gstelement_class->change_state = GST_DEBUG_FUNCPTR(gst_spider_identity_change_state);
130 gstelement_class->request_new_pad = GST_DEBUG_FUNCPTR(gst_spider_identity_request_new_pad);
133 static GstBufferPool*
134 gst_spider_identity_get_bufferpool (GstPad *pad)
136 GstSpiderIdentity *ident;
138 ident = GST_SPIDER_IDENTITY (gst_pad_get_parent (pad));
140 return gst_pad_get_bufferpool (ident->src);
144 gst_spider_identity_init (GstSpiderIdentity *ident)
147 ident->sink = gst_pad_new_from_template (GST_PAD_TEMPLATE_GET (spider_sink_factory), "sink");
148 gst_element_add_pad (GST_ELEMENT (ident), ident->sink);
149 gst_pad_set_connect_function (ident->sink, GST_DEBUG_FUNCPTR (gst_spider_identity_connect));
150 gst_pad_set_getcaps_function (ident->sink, GST_DEBUG_FUNCPTR (gst_spider_identity_getcaps));
151 gst_pad_set_bufferpool_function (ident->sink, GST_DEBUG_FUNCPTR (gst_spider_identity_get_bufferpool));
153 ident->src = gst_pad_new_from_template (GST_PAD_TEMPLATE_GET (spider_src_factory), "src");
154 gst_element_add_pad (GST_ELEMENT (ident), ident->src);
155 gst_pad_set_connect_function (ident->src, GST_DEBUG_FUNCPTR (gst_spider_identity_connect));
156 gst_pad_set_getcaps_function (ident->src, GST_DEBUG_FUNCPTR (gst_spider_identity_getcaps));
157 gst_pad_set_event_function (ident->src, GST_DEBUG_FUNCPTR (gst_spider_identity_handle_src_event));
160 ident->plugged = FALSE;
163 ident->cache_start = NULL;
164 ident->cache_end = NULL;
169 gst_spider_identity_chain (GstPad *pad, GstBuffer *buf)
171 GstSpiderIdentity *ident;
173 /* g_print ("chaining on pad %s:%s with buffer %p\n", GST_DEBUG_PAD_NAME (pad), buf); */
175 g_return_if_fail (pad != NULL);
176 g_return_if_fail (GST_IS_PAD (pad));
177 if (buf == NULL) return;
179 ident = GST_SPIDER_IDENTITY (gst_pad_get_parent (pad));
181 if (GST_IS_EVENT (buf)) {
182 /* start hack for current event stuff here */
183 /* check for unconnected elements and send them the EOS event, too */
184 if (GST_EVENT_TYPE (GST_EVENT (buf)) == GST_EVENT_EOS)
186 GstSpider *spider = (GstSpider *) GST_OBJECT_PARENT (ident);
187 GList *list = spider->connections;
190 GstSpiderConnection *conn = (GstSpiderConnection *) list->data;
191 list = g_list_next (list);
192 if (conn->sink == ident)
194 gst_element_set_eos (GST_ELEMENT (conn->src));
195 gst_pad_push (conn->src->src, GST_BUFFER (gst_event_new (GST_EVENT_EOS)));
199 /* end hack for current event stuff here */
201 gst_pad_event_default (ident->sink, GST_EVENT (buf));
205 if ((ident->src != NULL) && (GST_PAD_PEER (ident->src) != NULL)) {
206 /* 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)); */
207 gst_pad_push (ident->src, buf);
208 } else if (GST_IS_BUFFER (buf)) {
209 gst_buffer_unref (buf);
213 gst_spider_identity_new_src (gchar *name)
215 //GstSpiderIdentity *ret = (GstSpiderIdentity *) g_object_new (gst_spider_identity_get_type (), NULL);
216 //GST_ELEMENT_NAME (ret) = name;
217 GstSpiderIdentity *ret = (GstSpiderIdentity *) gst_element_factory_make ("spideridentity", name);
218 /* set the right functions */
219 gst_element_set_loop_function (GST_ELEMENT (ret), (GstElementLoopFunction) GST_DEBUG_FUNCPTR (gst_spider_identity_src_loop));
224 gst_spider_identity_new_sink (gchar *name)
226 //GstSpiderIdentity *ret = (GstSpiderIdentity *) g_object_new (gst_spider_identity_get_type (), NULL);
228 //GST_ELEMENT_NAME (ret) = name;
229 GstSpiderIdentity *ret = (GstSpiderIdentity *) gst_element_factory_make ("spideridentity", name);
231 /* set the right functions */
232 gst_element_set_loop_function (GST_ELEMENT (ret), (GstElementLoopFunction) GST_DEBUG_FUNCPTR (gst_spider_identity_dumb_loop));
237 /* shamelessly stolen from gstqueue.c to get proxy connections */
238 static GstPadConnectReturn
239 gst_spider_identity_connect (GstPad *pad, GstCaps *caps)
241 GstSpiderIdentity *spider_identity = GST_SPIDER_IDENTITY (gst_pad_get_parent (pad));
244 if (pad == spider_identity->src)
245 otherpad = spider_identity->sink;
247 otherpad = spider_identity->src;
249 if (otherpad != NULL)
250 return gst_pad_proxy_connect (otherpad, caps);
252 return GST_PAD_CONNECT_OK;
256 gst_spider_identity_getcaps (GstPad *pad, GstCaps *caps)
258 GstSpiderIdentity *spider_identity = GST_SPIDER_IDENTITY (gst_pad_get_parent (pad));
261 if (pad == spider_identity->src)
262 otherpad = spider_identity->sink;
264 otherpad = spider_identity->src;
266 if (otherpad != NULL)
267 return gst_pad_get_allowed_caps (otherpad);
272 gst_spider_identity_request_new_pad (GstElement *element, GstPadTemplate *templ, const gchar *name)
274 GstSpiderIdentity *ident;
277 g_return_val_if_fail (templ != NULL, NULL);
278 g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
279 ident = GST_SPIDER_IDENTITY (element);
280 g_return_val_if_fail (ident != NULL, NULL);
281 g_return_val_if_fail (GST_IS_SPIDER_IDENTITY (ident), NULL);
283 switch (GST_PAD_TEMPLATE_DIRECTION (templ))
286 if (ident->sink != NULL) break;
288 GST_DEBUG(0, "element %s requests new sink pad", GST_ELEMENT_NAME(ident));
289 ident->sink = gst_pad_new ("sink", GST_PAD_SINK);
290 gst_element_add_pad (GST_ELEMENT (ident), ident->sink);
291 gst_pad_set_connect_function (ident->sink, GST_DEBUG_FUNCPTR (gst_spider_identity_connect));
292 gst_pad_set_getcaps_function (ident->sink, GST_DEBUG_FUNCPTR (gst_spider_identity_getcaps));
293 gst_pad_set_bufferpool_function (ident->sink, GST_DEBUG_FUNCPTR (gst_spider_identity_get_bufferpool));
297 if (ident->src != NULL) break;
298 GST_DEBUG(0, "element %s requests new src pad", GST_ELEMENT_NAME(ident));
299 ident->src = gst_pad_new ("src", GST_PAD_SRC);
300 gst_element_add_pad (GST_ELEMENT (ident), ident->src);
301 gst_pad_set_connect_function (ident->src, GST_DEBUG_FUNCPTR (gst_spider_identity_connect));
302 gst_pad_set_getcaps_function (ident->src, GST_DEBUG_FUNCPTR (gst_spider_identity_getcaps));
303 gst_pad_set_event_function (ident->src, GST_DEBUG_FUNCPTR (gst_spider_identity_handle_src_event));
309 GST_DEBUG(0, "element %s requested a new pad but none could be created", GST_ELEMENT_NAME(ident));
313 /* this function has to
314 * - start the autoplugger
315 * - start type finding
318 static GstElementStateReturn
319 gst_spider_identity_change_state (GstElement *element)
321 GstSpiderIdentity *ident;
323 GstElementStateReturn ret = GST_STATE_SUCCESS;
326 ident = GST_SPIDER_IDENTITY (element);
327 g_return_val_if_fail (ident != NULL, GST_STATE_FAILURE);
328 g_return_val_if_fail (GST_IS_SPIDER_IDENTITY (ident), GST_STATE_FAILURE);
330 switch (GST_STATE_TRANSITION (element)) {
331 case GST_STATE_PAUSED_TO_PLAYING:
332 /* autoplugger check */
333 spider = GST_SPIDER (GST_ELEMENT_PARENT (ident));
334 g_return_val_if_fail (spider != NULL, GST_STATE_FAILURE);
335 g_return_val_if_fail (GST_IS_SPIDER (spider), GST_STATE_FAILURE);
337 /* start typefinding or plugging */
338 if ((GST_RPAD_PEER (ident->sink) != NULL) && (GST_RPAD_PEER (ident->src) == NULL))
340 if (gst_pad_get_caps ((GstPad *) GST_PAD_PEER (ident->sink)) == NULL)
342 gst_spider_identity_start_type_finding (ident);
345 gst_spider_identity_plug (ident);
348 /* autoplug on src */
349 if ((GST_RPAD_PEER (ident->src) != NULL) && (GST_RPAD_PEER (ident->sink) == NULL))
351 gst_spider_identity_plug (ident);
357 if ((ret != GST_STATE_FAILURE) && (GST_ELEMENT_CLASS (parent_class)->change_state))
358 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element);
364 gst_spider_identity_start_type_finding (GstSpiderIdentity *ident)
366 GstElement* typefind;
368 gboolean restart = FALSE;
370 GST_DEBUG (GST_CAT_AUTOPLUG, "element %s starts typefinding", GST_ELEMENT_NAME(ident));
371 if (GST_STATE (GST_ELEMENT_PARENT (ident)) == GST_STATE_PLAYING)
373 gst_element_set_state (GST_ELEMENT (GST_ELEMENT_PARENT (ident)), GST_STATE_PAUSED);
377 /* create and connect typefind object */
378 name = g_strdup_printf ("%s%s", "typefind", GST_ELEMENT_NAME(ident));
379 typefind = gst_element_factory_make ("typefind", name);
382 g_signal_connect (G_OBJECT (typefind), "have_type",
383 G_CALLBACK (callback_type_find_have_type), ident);
384 gst_bin_add (GST_BIN (GST_ELEMENT_PARENT (ident)), typefind);
385 gst_pad_connect (gst_element_get_compatible_pad ((GstElement *) ident, gst_element_get_pad (typefind, "sink")), gst_element_get_pad (typefind, "sink"));
387 gst_element_set_loop_function (GST_ELEMENT (ident), (GstElementLoopFunction) GST_DEBUG_FUNCPTR (gst_spider_identity_sink_loop_type_finding));
391 gst_element_set_state (GST_ELEMENT (GST_ELEMENT_PARENT (ident)), GST_STATE_PLAYING);
394 /* waiting for a good suggestion on where to set the caps from typefinding
395 * Caps must be cleared when pad is disconnected
397 * Currently we are naive and set the caps on the source of the identity object
398 * directly and hope to avoid any disturbance in the force.
401 gst_spider_identity_set_caps (GstSpiderIdentity *ident, GstCaps *caps)
405 gst_pad_try_set_caps (ident->src, caps);
411 callback_type_find_have_type (GstElement *typefind, GstCaps *caps, GstSpiderIdentity *ident)
413 gboolean restart_spider = FALSE;
415 GST_INFO (GST_CAT_AUTOPLUG, "element %s has found caps\n", GST_ELEMENT_NAME(ident));
418 g_assert (GST_IS_ELEMENT (typefind));
419 g_assert (GST_IS_SPIDER_IDENTITY (ident));
421 /* pause the autoplugger */
422 if (gst_element_get_state (GST_ELEMENT (GST_ELEMENT_PARENT(ident))) == GST_STATE_PLAYING)
424 gst_element_set_state (GST_ELEMENT (GST_ELEMENT_PARENT(ident)), GST_STATE_PAUSED);
425 restart_spider = TRUE;
428 /* remove typefind */
429 gst_pad_disconnect (ident->src, (GstPad*) GST_RPAD_PEER (ident->src));
430 gst_bin_remove (GST_BIN (GST_ELEMENT_PARENT (ident)), typefind);
433 gst_spider_identity_set_caps (ident, caps);
435 /* set new loop function, we gotta empty the cache */
436 gst_element_set_loop_function (GST_ELEMENT (ident), (GstElementLoopFunction) GST_DEBUG_FUNCPTR (gst_spider_identity_sink_loop_emptycache));
438 /* autoplug this pad */
439 gst_spider_identity_plug (ident);
441 /* restart autoplugger */
443 gst_element_set_state (GST_ELEMENT (GST_ELEMENT_PARENT(ident)), GST_STATE_PLAYING);
446 /* since we can't set the loop function to NULL if there's a cothread for us,
447 * we have to use a dumb one
450 gst_spider_identity_dumb_loop (GstSpiderIdentity *ident)
454 g_return_if_fail (ident != NULL);
455 g_return_if_fail (GST_IS_SPIDER_IDENTITY (ident));
456 g_assert (ident->sink != NULL);
458 buf = gst_pad_pull (ident->sink);
460 gst_spider_identity_chain (ident->sink, buf);
462 /* do nothing until we're connected - then disable yourself
465 gst_spider_identity_src_loop (GstSpiderIdentity *ident)
467 /* checks - disable for speed */
468 g_return_if_fail (ident != NULL);
469 g_return_if_fail (GST_IS_SPIDER_IDENTITY (ident));
471 /* we don't want a loop function if we're plugged */
472 if (ident->sink && GST_PAD_PEER (ident->sink))
474 gst_element_set_loop_function (GST_ELEMENT (ident), (GstElementLoopFunction) GST_DEBUG_FUNCPTR (gst_spider_identity_dumb_loop));
475 gst_spider_identity_dumb_loop (ident);
479 /* in any case, we don't want to do anything:
480 * - if we're not plugged, we don't have buffers
481 * - if we're plugged, we wanna be chained please
483 gst_element_interrupt (GST_ELEMENT (ident));
486 /* This loop function is only needed when typefinding.
487 * It works quite simple: get a new buffer, append it to the cache
488 * and push it to the typefinder.
491 gst_spider_identity_sink_loop_type_finding (GstSpiderIdentity *ident)
495 /* checks - disable for speed */
496 g_return_if_fail (ident != NULL);
497 g_return_if_fail (GST_IS_SPIDER_IDENTITY (ident));
498 g_assert (ident->sink != NULL);
501 buf = gst_pad_pull (ident->sink);
503 /* if it's an event... */
504 while (GST_IS_EVENT (buf)) {
505 /* handle DISCONT events, please */
506 gst_pad_event_default (ident->sink, GST_EVENT (buf));
507 buf = gst_pad_pull (ident->sink);
510 /* add it to the end of the cache */
511 gst_buffer_ref (buf);
512 GST_DEBUG (0, "element %s adds buffer %p (size %d) to cache", GST_ELEMENT_NAME(ident), buf, GST_BUFFER_SIZE (buf));
513 ident->cache_end = g_list_prepend (ident->cache_end, buf);
514 if (ident->cache_start == NULL)
515 ident->cache_start = ident->cache_end;
517 /* push the buffer */
518 gst_spider_identity_chain (ident->sink, buf);
520 /* this function is needed after typefinding:
521 * empty the cache and when the cache is empty - remove this function
524 gst_spider_identity_sink_loop_emptycache (GstSpiderIdentity *ident)
528 /* get the buffer and push it */
529 buf = GST_BUFFER (ident->cache_start->data);
530 gst_spider_identity_chain (ident->sink, buf);
532 ident->cache_start = g_list_previous (ident->cache_start);
534 /* now check if we have more buffers to push */
535 if (ident->cache_start == NULL)
537 GST_DEBUG(0, "cache from %s is empty, changing loop function", GST_ELEMENT_NAME(ident));
539 g_list_free (ident->cache_end);
540 ident->cache_end = NULL;
542 /* remove loop function */
543 gst_element_set_loop_function (GST_ELEMENT (ident), (GstElementLoopFunction) GST_DEBUG_FUNCPTR (gst_spider_identity_dumb_loop));
544 gst_element_interrupt (GST_ELEMENT (ident));
549 gst_spider_identity_handle_src_event (GstPad *pad, GstEvent *event)
552 GstSpiderIdentity *ident;
554 GST_DEBUG (0, "spider_identity src_event\n");
556 ident = GST_SPIDER_IDENTITY (gst_pad_get_parent (pad));
558 switch (GST_EVENT_TYPE (event)) {
559 case GST_EVENT_FLUSH:
561 /* see if there's something cached */
562 if (ident->cache_start && ident->cache_start->data) {
563 GST_DEBUG (0, "spider_identity seek in cache\n");
564 /* FIXME we need to find the right position in the cache, make sure we
565 * push from that offset and send out a discont event on the
570 res = gst_pad_event_default (pad, event);