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 (gst_spider_identity_debug);
31 #define GST_CAT_DEFAULT gst_spider_identity_debug
33 GstElementDetails gst_spider_identity_details = {
37 "Link between spider and outside elements",
39 "Benjamin Otte <in7y118@public.uni-hamburg.de>",
45 * delete me when meging with spider.c
47 GST_PAD_TEMPLATE_FACTORY (spider_src_factory,
54 GST_PAD_TEMPLATE_FACTORY (spider_sink_factory,
61 /* SpiderIdentity signals and args */
73 static void gst_spider_identity_class_init (GstSpiderIdentityClass *klass);
74 static void gst_spider_identity_init (GstSpiderIdentity *spider_identity);
76 /* functions set in pads, elements and stuff */
77 static void gst_spider_identity_chain (GstPad *pad, GstBuffer *buf);
78 static GstElementStateReturn gst_spider_identity_change_state (GstElement *element);
79 static GstPadLinkReturn gst_spider_identity_link (GstPad *pad, GstCaps *caps);
80 static GstCaps * gst_spider_identity_getcaps (GstPad *pad, GstCaps *caps);
82 static void gst_spider_identity_dumb_loop (GstSpiderIdentity *ident);
83 static void gst_spider_identity_src_loop (GstSpiderIdentity *ident);
84 static void gst_spider_identity_sink_loop_type_finding (GstSpiderIdentity *ident);
86 static gboolean gst_spider_identity_handle_src_event (GstPad *pad, GstEvent *event);
89 static void gst_spider_identity_start_type_finding (GstSpiderIdentity *ident);
91 static GstElementClass * parent_class = NULL;
93 static guint gst_spider_identity_signals[LAST_SIGNAL] = { 0 }; */
96 gst_spider_identity_get_type (void)
98 static GType spider_identity_type = 0;
100 if (!spider_identity_type) {
101 static const GTypeInfo spider_identity_info = {
102 sizeof(GstSpiderIdentityClass), NULL,
104 (GClassInitFunc)gst_spider_identity_class_init,
107 sizeof(GstSpiderIdentity),
109 (GInstanceInitFunc)gst_spider_identity_init,
111 spider_identity_type = g_type_register_static (GST_TYPE_ELEMENT, "GstSpiderIdentity", &spider_identity_info, 0);
113 return spider_identity_type;
117 gst_spider_identity_class_init (GstSpiderIdentityClass *klass)
119 GstElementClass *gstelement_class = (GstElementClass *) klass;
121 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
123 /* add our two pad templates */
124 gst_element_class_add_pad_template (gstelement_class, GST_PAD_TEMPLATE_GET (spider_src_factory));
125 gst_element_class_add_pad_template (gstelement_class, GST_PAD_TEMPLATE_GET (spider_sink_factory));
127 gstelement_class->change_state = GST_DEBUG_FUNCPTR(gst_spider_identity_change_state);
128 gstelement_class->request_new_pad = GST_DEBUG_FUNCPTR(gst_spider_identity_request_new_pad);
131 static GstBufferPool*
132 gst_spider_identity_get_bufferpool (GstPad *pad)
134 GstSpiderIdentity *ident;
136 ident = GST_SPIDER_IDENTITY (gst_pad_get_parent (pad));
138 return gst_pad_get_bufferpool (ident->src);
142 gst_spider_identity_init (GstSpiderIdentity *ident)
145 ident->sink = gst_pad_new_from_template (GST_PAD_TEMPLATE_GET (spider_sink_factory), "sink");
146 gst_element_add_pad (GST_ELEMENT (ident), ident->sink);
147 gst_pad_set_link_function (ident->sink, GST_DEBUG_FUNCPTR (gst_spider_identity_link));
148 gst_pad_set_getcaps_function (ident->sink, GST_DEBUG_FUNCPTR (gst_spider_identity_getcaps));
149 gst_pad_set_bufferpool_function (ident->sink, GST_DEBUG_FUNCPTR (gst_spider_identity_get_bufferpool));
151 ident->src = gst_pad_new_from_template (GST_PAD_TEMPLATE_GET (spider_src_factory), "src");
152 gst_element_add_pad (GST_ELEMENT (ident), ident->src);
153 gst_pad_set_link_function (ident->src, GST_DEBUG_FUNCPTR (gst_spider_identity_link));
154 gst_pad_set_getcaps_function (ident->src, GST_DEBUG_FUNCPTR (gst_spider_identity_getcaps));
155 gst_pad_set_event_function (ident->src, GST_DEBUG_FUNCPTR (gst_spider_identity_handle_src_event));
158 ident->plugged = FALSE;
163 gst_spider_identity_chain (GstPad *pad, GstBuffer *buf)
165 GstSpiderIdentity *ident;
167 /*g_print ("chaining on pad %s:%s with buffer %p\n", GST_DEBUG_PAD_NAME (pad), buf);*/
169 g_return_if_fail (pad != NULL);
170 g_return_if_fail (GST_IS_PAD (pad));
171 if (buf == NULL) return;
173 ident = GST_SPIDER_IDENTITY (gst_pad_get_parent (pad));
175 if (GST_IS_EVENT (buf)) {
176 /* start hack for current event stuff here */
177 /* check for unlinked elements and send them the EOS event, too */
178 if (GST_EVENT_TYPE (GST_EVENT (buf)) == GST_EVENT_EOS)
180 GstSpider *spider = (GstSpider *) GST_OBJECT_PARENT (ident);
181 GList *list = spider->links;
184 GstSpiderConnection *conn = (GstSpiderConnection *) list->data;
185 list = g_list_next (list);
186 if (conn->current != (GstElement *) conn->src) {
187 GST_DEBUG ("sending EOS to unconnected element %s from %s",
188 GST_ELEMENT_NAME (conn->src), GST_ELEMENT_NAME (ident));
189 gst_pad_push (conn->src->src, GST_BUFFER (gst_event_new (GST_EVENT_EOS)));
190 gst_element_set_eos (GST_ELEMENT (conn->src));
194 /* end hack for current event stuff here */
196 gst_pad_event_default (pad, GST_EVENT (buf));
200 if ((ident->src != NULL) && (GST_PAD_PEER (ident->src) != NULL)) {
201 /* 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)); */
202 GST_LOG ( "push %p %" G_GINT64_FORMAT, buf, GST_BUFFER_OFFSET (buf));
203 gst_pad_push (ident->src, buf);
204 } else if (GST_IS_BUFFER (buf)) {
205 gst_buffer_unref (buf);
209 gst_spider_identity_new_src (gchar *name)
211 GstSpiderIdentity *ret = (GstSpiderIdentity *) gst_element_factory_make ("spideridentity", name);
212 /* set the right functions */
213 gst_element_set_loop_function (GST_ELEMENT (ret), (GstElementLoopFunction) GST_DEBUG_FUNCPTR (gst_spider_identity_src_loop));
218 gst_spider_identity_new_sink (gchar *name)
220 GstSpiderIdentity *ret = (GstSpiderIdentity *) gst_element_factory_make ("spideridentity", name);
222 /* set the right functions */
223 gst_element_set_loop_function (GST_ELEMENT (ret), (GstElementLoopFunction) GST_DEBUG_FUNCPTR (gst_spider_identity_dumb_loop));
228 /* shamelessly stolen from gstqueue.c to get proxy links */
229 static GstPadLinkReturn
230 gst_spider_identity_link (GstPad *pad, GstCaps *caps)
232 GstSpiderIdentity *spider_identity = GST_SPIDER_IDENTITY (gst_pad_get_parent (pad));
235 if (pad == spider_identity->src)
236 otherpad = spider_identity->sink;
238 otherpad = spider_identity->src;
240 if (otherpad != NULL)
241 return gst_pad_proxy_link (otherpad, caps);
243 return GST_PAD_LINK_OK;
247 gst_spider_identity_getcaps (GstPad *pad, GstCaps *caps)
249 GstSpiderIdentity *spider_identity = GST_SPIDER_IDENTITY (gst_pad_get_parent (pad));
252 if (pad == spider_identity->src)
253 otherpad = spider_identity->sink;
255 otherpad = spider_identity->src;
257 if (otherpad != NULL)
258 return gst_pad_get_allowed_caps (otherpad);
264 gst_spider_identity_request_new_pad (GstElement *element, GstPadTemplate *templ, const gchar *name)
266 GstSpiderIdentity *ident;
269 g_return_val_if_fail (templ != NULL, NULL);
270 g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
271 ident = GST_SPIDER_IDENTITY (element);
272 g_return_val_if_fail (ident != NULL, NULL);
273 g_return_val_if_fail (GST_IS_SPIDER_IDENTITY (ident), NULL);
275 switch (GST_PAD_TEMPLATE_DIRECTION (templ))
278 if (ident->sink != NULL) break;
280 GST_DEBUG ( "element %s requests new sink pad", GST_ELEMENT_NAME(ident));
281 ident->sink = gst_pad_new ("sink", GST_PAD_SINK);
282 gst_element_add_pad (GST_ELEMENT (ident), ident->sink);
283 gst_pad_set_link_function (ident->sink, GST_DEBUG_FUNCPTR (gst_spider_identity_link));
284 gst_pad_set_getcaps_function (ident->sink, GST_DEBUG_FUNCPTR (gst_spider_identity_getcaps));
285 gst_pad_set_bufferpool_function (ident->sink, GST_DEBUG_FUNCPTR (gst_spider_identity_get_bufferpool));
289 if (ident->src != NULL) break;
290 GST_DEBUG ( "element %s requests new src pad", GST_ELEMENT_NAME(ident));
291 ident->src = gst_pad_new ("src", GST_PAD_SRC);
292 gst_element_add_pad (GST_ELEMENT (ident), ident->src);
293 gst_pad_set_link_function (ident->src, GST_DEBUG_FUNCPTR (gst_spider_identity_link));
294 gst_pad_set_getcaps_function (ident->src, GST_DEBUG_FUNCPTR (gst_spider_identity_getcaps));
295 gst_pad_set_event_function (ident->src, GST_DEBUG_FUNCPTR (gst_spider_identity_handle_src_event));
301 GST_DEBUG ( "element %s requested a new pad but none could be created", GST_ELEMENT_NAME(ident));
305 /* this function has to
306 * - start the autoplugger
307 * - start type finding
310 static GstElementStateReturn
311 gst_spider_identity_change_state (GstElement *element)
313 GstSpiderIdentity *ident;
315 GstElementStateReturn ret = GST_STATE_SUCCESS;
318 ident = GST_SPIDER_IDENTITY (element);
319 g_return_val_if_fail (ident != NULL, GST_STATE_FAILURE);
320 g_return_val_if_fail (GST_IS_SPIDER_IDENTITY (ident), GST_STATE_FAILURE);
322 switch (GST_STATE_TRANSITION (element)) {
323 case GST_STATE_PAUSED_TO_PLAYING:
324 /* autoplugger check */
325 spider = GST_SPIDER (GST_ELEMENT_PARENT (ident));
326 g_return_val_if_fail (spider != NULL, GST_STATE_FAILURE);
327 g_return_val_if_fail (GST_IS_SPIDER (spider), GST_STATE_FAILURE);
329 /* start typefinding or plugging */
330 if ((GST_RPAD_PEER (ident->sink) != NULL) && (GST_RPAD_PEER (ident->src) == NULL))
332 if (gst_pad_get_caps ((GstPad *) GST_PAD_PEER (ident->sink)) == NULL)
334 gst_spider_identity_start_type_finding (ident);
337 gst_spider_identity_plug (ident);
340 /* autoplug on src */
341 if ((GST_RPAD_PEER (ident->src) != NULL) && (GST_RPAD_PEER (ident->sink) == NULL))
343 gst_spider_identity_plug (ident);
349 if ((ret != GST_STATE_FAILURE) && (GST_ELEMENT_CLASS (parent_class)->change_state))
350 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element);
356 gst_spider_identity_start_type_finding (GstSpiderIdentity *ident)
358 /* GstElement* typefind;
360 gboolean restart = FALSE;
362 GST_DEBUG ("element %s starts typefinding", GST_ELEMENT_NAME(ident));
363 if (GST_STATE (GST_ELEMENT_PARENT (ident)) == GST_STATE_PLAYING)
365 gst_element_set_state (GST_ELEMENT (GST_ELEMENT_PARENT (ident)), GST_STATE_PAUSED);
369 gst_element_set_loop_function (GST_ELEMENT (ident), (GstElementLoopFunction) GST_DEBUG_FUNCPTR (gst_spider_identity_sink_loop_type_finding));
373 gst_element_set_state (GST_ELEMENT (GST_ELEMENT_PARENT (ident)), GST_STATE_PLAYING);
377 /* since we can't set the loop function to NULL if there's a cothread for us,
378 * we have to use a dumb one
381 gst_spider_identity_dumb_loop (GstSpiderIdentity *ident)
385 g_return_if_fail (ident != NULL);
386 g_return_if_fail (GST_IS_SPIDER_IDENTITY (ident));
387 g_assert (ident->sink != NULL);
389 buf = gst_pad_pull (ident->sink);
391 gst_spider_identity_chain (ident->sink, buf);
393 /* do nothing until we're linked - then disable yourself
396 gst_spider_identity_src_loop (GstSpiderIdentity *ident)
398 /* checks - disable for speed */
399 g_return_if_fail (ident != NULL);
400 g_return_if_fail (GST_IS_SPIDER_IDENTITY (ident));
402 /* we don't want a loop function if we're plugged */
403 if (ident->sink && GST_PAD_PEER (ident->sink))
405 gst_element_set_loop_function (GST_ELEMENT (ident), (GstElementLoopFunction) GST_DEBUG_FUNCPTR (gst_spider_identity_dumb_loop));
406 gst_spider_identity_dumb_loop (ident);
409 gst_element_interrupt (GST_ELEMENT (ident));
411 /* This loop function is only needed when typefinding.
414 gst_spider_identity_sink_loop_type_finding (GstSpiderIdentity *ident)
416 GstBuffer *buf = NULL;
421 g_return_if_fail (GST_IS_SPIDER_IDENTITY (ident));
423 /* get a bytestream object */
424 bs = gst_bytestream_new (ident->sink);
425 if (gst_bytestream_peek (bs, &buf, 1) != 1 || !buf) {
427 g_warning ("Failed to read fake buffer - serious idiocy going on here");
430 gst_buffer_unref (buf);
434 /* maybe there are already valid caps now? */
435 if ((caps = gst_pad_get_caps (ident->sink)) != NULL) {
439 /* now do the actual typefinding with the supplied buffer */
440 type_list = (GList *) gst_type_get_list ();
443 GstType *type = (GstType *) type_list->data;
444 GSList *factories = type->factories;
447 GstTypeFactory *factory = GST_TYPE_FACTORY (factories->data);
448 GstTypeFindFunc typefindfunc = (GstTypeFindFunc)factory->typefindfunc;
450 GST_DEBUG ("trying typefind function %s", GST_PLUGIN_FEATURE_NAME (factory));
451 if (typefindfunc && (caps = typefindfunc (bs, factory))) {
452 GST_INFO ("typefind function %s found caps", GST_PLUGIN_FEATURE_NAME (factory));
453 if (gst_pad_try_set_caps (ident->src, caps) <= 0) {
454 g_warning ("typefind: found type but peer didn't accept it");
455 gst_caps_sink (caps);
460 factories = g_slist_next (factories);
462 type_list = g_list_next (type_list);
464 gst_element_error(GST_ELEMENT(ident), "Could not find media type", NULL);
465 buf = GST_BUFFER (gst_event_new (GST_EVENT_EOS));
469 /* remove loop function */
470 gst_element_set_loop_function (GST_ELEMENT (ident),
471 (GstElementLoopFunction) GST_DEBUG_FUNCPTR (gst_spider_identity_dumb_loop));
473 /* push the buffer */
474 gst_spider_identity_chain (ident->sink, buf);
476 /* bytestream no longer needed */
477 gst_bytestream_destroy (bs);
482 gst_caps_debug (caps, "spider starting caps");
483 gst_caps_sink (caps);
485 gst_spider_identity_plug (ident);
487 gst_bytestream_read (bs, &buf, bs->listavail);
493 gst_spider_identity_handle_src_event (GstPad *pad, GstEvent *event)
496 GstSpiderIdentity *ident;
498 GST_DEBUG ( "spider_identity src_event");
500 ident = GST_SPIDER_IDENTITY (gst_pad_get_parent (pad));
502 switch (GST_EVENT_TYPE (event)) {
503 case GST_EVENT_FLUSH:
506 res = gst_pad_event_default (pad, event);