New typefind system: bytestream is now part of the core all plugins have been modifie...
[platform/upstream/gstreamer.git] / gst / autoplug / gstspideridentity.c
1 /* GStreamer
2  * Copyright (C) 2002 Erik Walthinsen <omega@cse.ogi.edu>
3  *               2002 Wim Taymans <wtay@chello.be>
4  *
5  * gstspideridentity.c: identity element for the spider autoplugger
6  *
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.
11  *
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.
16  *
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.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #  include "config.h"
25 #endif
26
27 #include "gstspideridentity.h"
28 #include "gstspider.h"
29
30 GST_DEBUG_CATEGORY (gst_spider_identity_debug);
31 #define GST_CAT_DEFAULT gst_spider_identity_debug
32
33 GstElementDetails gst_spider_identity_details = {
34   "SpiderIdentity",
35   "Generic",
36   "LGPL",
37   "Link between spider and outside elements",
38   VERSION,
39   "Benjamin Otte <in7y118@public.uni-hamburg.de>",
40   "(C) 2002",
41 };
42
43
44 /* generic templates 
45  * delete me when meging with spider.c
46  */
47 GST_PAD_TEMPLATE_FACTORY (spider_src_factory,
48   "src",
49   GST_PAD_SRC,
50   GST_PAD_REQUEST,
51   NULL      /* no caps */
52 );
53
54 GST_PAD_TEMPLATE_FACTORY (spider_sink_factory,
55   "sink",
56   GST_PAD_SINK,
57   GST_PAD_REQUEST,
58   NULL      /* no caps */
59 );
60
61 /* SpiderIdentity signals and args */
62 enum {
63   /* FILL ME */
64   LAST_SIGNAL
65 };
66
67 enum {
68   ARG_0,
69   /* FILL ME */
70 };
71
72 /* GObject stuff */
73 static void                     gst_spider_identity_class_init          (GstSpiderIdentityClass *klass);
74 static void                     gst_spider_identity_init                (GstSpiderIdentity *spider_identity);
75
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);
81 /* loop functions */
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);
85
86 static gboolean                 gst_spider_identity_handle_src_event    (GstPad *pad, GstEvent *event);
87
88 /* other functions */
89 static void                     gst_spider_identity_start_type_finding  (GstSpiderIdentity *ident);
90
91 static GstElementClass *        parent_class                            = NULL;
92 /* no signals
93 static guint gst_spider_identity_signals[LAST_SIGNAL] = { 0 }; */
94
95 GType
96 gst_spider_identity_get_type (void) 
97 {
98   static GType spider_identity_type = 0;
99
100   if (!spider_identity_type) {
101     static const GTypeInfo spider_identity_info = {
102       sizeof(GstSpiderIdentityClass),      NULL,
103       NULL,
104       (GClassInitFunc)gst_spider_identity_class_init,
105       NULL,
106       NULL,
107       sizeof(GstSpiderIdentity),
108       0,
109       (GInstanceInitFunc)gst_spider_identity_init,
110     };
111     spider_identity_type = g_type_register_static (GST_TYPE_ELEMENT, "GstSpiderIdentity", &spider_identity_info, 0);
112   }
113   return spider_identity_type;
114 }
115
116 static void 
117 gst_spider_identity_class_init (GstSpiderIdentityClass *klass) 
118 {
119   GstElementClass *gstelement_class = (GstElementClass *) klass;
120   
121   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
122   
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));
126   
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);
129 }
130
131 static GstBufferPool*
132 gst_spider_identity_get_bufferpool (GstPad *pad)
133 {
134   GstSpiderIdentity *ident;
135
136   ident = GST_SPIDER_IDENTITY (gst_pad_get_parent (pad));
137
138   return gst_pad_get_bufferpool (ident->src);
139 }
140
141 static void 
142 gst_spider_identity_init (GstSpiderIdentity *ident) 
143 {
144   /* sink */
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));
150   /* src */
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));
156
157   /* variables */
158   ident->plugged = FALSE;
159   
160 }
161
162 static void 
163 gst_spider_identity_chain (GstPad *pad, GstBuffer *buf) 
164 {
165   GstSpiderIdentity *ident;
166   
167   /*g_print ("chaining on pad %s:%s with buffer %p\n", GST_DEBUG_PAD_NAME (pad), buf);*/
168
169   g_return_if_fail (pad != NULL);
170   g_return_if_fail (GST_IS_PAD (pad));
171   if (buf == NULL) return;
172
173   ident = GST_SPIDER_IDENTITY (gst_pad_get_parent (pad));
174
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)
179     {
180       GstSpider *spider = (GstSpider *) GST_OBJECT_PARENT (ident);
181       GList *list = spider->links;
182       while (list)
183       {
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));
191         }
192       }
193     }
194     /* end hack for current event stuff here */
195
196     gst_pad_event_default (pad, GST_EVENT (buf));
197     return;
198   }
199
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);
206   }
207 }
208 GstSpiderIdentity*           
209 gst_spider_identity_new_src (gchar *name)
210 {
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));
214   
215   return ret;
216 }
217 GstSpiderIdentity*           
218 gst_spider_identity_new_sink (gchar *name)
219 {
220   GstSpiderIdentity *ret = (GstSpiderIdentity *) gst_element_factory_make ("spideridentity", name);
221
222   /* set the right functions */
223   gst_element_set_loop_function (GST_ELEMENT (ret), (GstElementLoopFunction) GST_DEBUG_FUNCPTR (gst_spider_identity_dumb_loop));
224
225   return ret;
226 }
227
228 /* shamelessly stolen from gstqueue.c to get proxy links */
229 static GstPadLinkReturn
230 gst_spider_identity_link (GstPad *pad, GstCaps *caps)
231 {
232   GstSpiderIdentity *spider_identity = GST_SPIDER_IDENTITY (gst_pad_get_parent (pad));
233   GstPad *otherpad;
234
235   if (pad == spider_identity->src) 
236     otherpad = spider_identity->sink;
237   else
238     otherpad = spider_identity->src;
239
240   if (otherpad != NULL)
241     return gst_pad_proxy_link (otherpad, caps);
242   
243   return GST_PAD_LINK_OK;
244 }
245
246 static GstCaps*
247 gst_spider_identity_getcaps (GstPad *pad, GstCaps *caps)
248 {
249   GstSpiderIdentity *spider_identity = GST_SPIDER_IDENTITY (gst_pad_get_parent (pad));
250   GstPad *otherpad;
251
252   if (pad == spider_identity->src) 
253     otherpad = spider_identity->sink;
254   else
255     otherpad = spider_identity->src;
256
257   if (otherpad != NULL)
258     return gst_pad_get_allowed_caps (otherpad);
259   
260   return NULL;
261 }
262
263 GstPad*
264 gst_spider_identity_request_new_pad  (GstElement *element, GstPadTemplate *templ, const gchar *name)
265 {
266   GstSpiderIdentity *ident;
267   
268   /*checks */
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);
274   
275   switch (GST_PAD_TEMPLATE_DIRECTION (templ))
276   {
277     case GST_PAD_SINK:
278       if (ident->sink != NULL) break;
279       /* sink */
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));
286       return ident->sink;
287     case GST_PAD_SRC:
288       /* src */
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));
296       return ident->src;
297     default:
298       break;
299   }
300   
301   GST_DEBUG ( "element %s requested a new pad but none could be created", GST_ELEMENT_NAME(ident));
302   return NULL;
303 }
304
305 /* this function has to
306  * - start the autoplugger
307  * - start type finding
308  * ...
309  */
310 static GstElementStateReturn
311 gst_spider_identity_change_state (GstElement *element)
312 {
313   GstSpiderIdentity *ident;
314   GstSpider *spider;
315   GstElementStateReturn ret = GST_STATE_SUCCESS;
316   
317   /* element check */
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);
321   
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);
328   
329       /* start typefinding or plugging */
330       if ((GST_RPAD_PEER (ident->sink) != NULL) && (GST_RPAD_PEER (ident->src) == NULL))
331       {
332         if (gst_pad_get_caps ((GstPad *) GST_PAD_PEER (ident->sink)) == NULL)
333         {
334           gst_spider_identity_start_type_finding (ident);
335           break;
336         } else {
337           gst_spider_identity_plug (ident);
338         }
339       }
340       /* autoplug on src */
341       if ((GST_RPAD_PEER (ident->src) != NULL) && (GST_RPAD_PEER (ident->sink) == NULL))
342       {
343         gst_spider_identity_plug (ident);
344       }
345     default:
346       break;
347   }
348   
349   if ((ret != GST_STATE_FAILURE) && (GST_ELEMENT_CLASS (parent_class)->change_state))
350     ret = GST_ELEMENT_CLASS (parent_class)->change_state (element);
351   
352   return ret;
353 }
354
355 static void
356 gst_spider_identity_start_type_finding (GstSpiderIdentity *ident)
357 {
358 /*  GstElement* typefind;
359   gchar *name;*/
360   gboolean restart = FALSE;
361   
362   GST_DEBUG ("element %s starts typefinding", GST_ELEMENT_NAME(ident));
363   if (GST_STATE (GST_ELEMENT_PARENT (ident)) == GST_STATE_PLAYING)
364   {
365     gst_element_set_state (GST_ELEMENT (GST_ELEMENT_PARENT (ident)), GST_STATE_PAUSED);
366     restart = TRUE;
367   }
368
369   gst_element_set_loop_function (GST_ELEMENT (ident), (GstElementLoopFunction) GST_DEBUG_FUNCPTR (gst_spider_identity_sink_loop_type_finding));
370
371   if (restart)
372   {
373     gst_element_set_state (GST_ELEMENT (GST_ELEMENT_PARENT (ident)), GST_STATE_PLAYING);
374   }
375 }
376
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
379  */
380 static void
381 gst_spider_identity_dumb_loop  (GstSpiderIdentity *ident)
382 {
383   GstBuffer *buf;
384
385   g_return_if_fail (ident != NULL);
386   g_return_if_fail (GST_IS_SPIDER_IDENTITY (ident));
387   g_assert (ident->sink != NULL);
388
389   buf = gst_pad_pull (ident->sink);
390
391   gst_spider_identity_chain (ident->sink, buf);
392 }
393 /* do nothing until we're linked - then disable yourself
394  */
395 static void
396 gst_spider_identity_src_loop (GstSpiderIdentity *ident)
397 {
398   /* checks - disable for speed */
399   g_return_if_fail (ident != NULL);
400   g_return_if_fail (GST_IS_SPIDER_IDENTITY (ident));
401   
402   /* we don't want a loop function if we're plugged */
403   if (ident->sink && GST_PAD_PEER (ident->sink))
404   {
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);
407     return;
408   }
409   gst_element_interrupt (GST_ELEMENT (ident));
410 }
411 /* This loop function is only needed when typefinding.
412  */
413 static void
414 gst_spider_identity_sink_loop_type_finding (GstSpiderIdentity *ident)
415 {
416   GstBuffer *buf = NULL;
417   GList *type_list;
418   GstCaps *caps;
419   GstByteStream *bs;
420
421   g_return_if_fail (GST_IS_SPIDER_IDENTITY (ident));
422
423   /* get a bytestream object */
424   bs = gst_bytestream_new (ident->sink);
425   if (gst_bytestream_peek (bs, &buf, 1) != 1 || !buf) {
426     buf = NULL;
427     g_warning ("Failed to read fake buffer - serious idiocy going on here");
428     goto end;
429   } else {
430     gst_buffer_unref (buf);
431     buf = NULL;
432   }
433
434   /* maybe there are already valid caps now? */
435   if ((caps = gst_pad_get_caps (ident->sink)) != NULL) {
436     goto plug;
437   }
438   
439   /* now do the actual typefinding with the supplied buffer */
440   type_list = (GList *) gst_type_get_list ();
441     
442   while (type_list) {
443     GstType *type = (GstType *) type_list->data;
444     GSList *factories = type->factories;
445
446     while (factories) {
447       GstTypeFactory *factory = GST_TYPE_FACTORY (factories->data);
448       GstTypeFindFunc typefindfunc = (GstTypeFindFunc)factory->typefindfunc;
449
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);
456         } else {
457           goto plug;
458         }
459       }
460       factories = g_slist_next (factories);
461     }
462     type_list = g_list_next (type_list);
463   }
464   gst_element_error(GST_ELEMENT(ident), "Could not find media type", NULL);
465   buf = GST_BUFFER (gst_event_new (GST_EVENT_EOS));
466
467 end:
468
469   /* remove loop function */
470   gst_element_set_loop_function (GST_ELEMENT (ident), 
471                                 (GstElementLoopFunction) GST_DEBUG_FUNCPTR (gst_spider_identity_dumb_loop));
472   
473   /* push the buffer */
474   gst_spider_identity_chain (ident->sink, buf);
475
476   /* bytestream no longer needed */
477   gst_bytestream_destroy (bs);
478   
479   return;
480
481 plug:
482   gst_caps_debug (caps, "spider starting caps");
483   gst_caps_sink (caps);
484
485   gst_spider_identity_plug (ident);
486
487   gst_bytestream_read (bs, &buf, bs->listavail);
488
489   goto end;
490 }
491
492 static gboolean
493 gst_spider_identity_handle_src_event (GstPad *pad, GstEvent *event)
494 {
495   gboolean res = TRUE;
496   GstSpiderIdentity *ident;
497
498   GST_DEBUG ( "spider_identity src_event");
499
500   ident = GST_SPIDER_IDENTITY (gst_pad_get_parent (pad));
501
502   switch (GST_EVENT_TYPE (event)) {
503     case GST_EVENT_FLUSH:
504     case GST_EVENT_SEEK:
505     default:
506       res = gst_pad_event_default (pad, event);
507       break;
508   }
509
510   return res;
511 }