698cda631d1ca08ff044acc8adbe05ff50dc2ef1
[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
24 #include "gstspideridentity.h"
25
26 #include "gstspider.h"
27
28 GstElementDetails gst_spider_identity_details = {
29   "SpiderIdentity",
30   "Generic",
31   "LGPL",
32   "Connection between spider and outside elements",
33   VERSION,
34   "Benjamin Otte <in7y118@public.uni-hamburg.de>",
35   "(C) 2002",
36 };
37
38
39 /* generic templates 
40  * delete me when meging with spider.c
41  */
42 GST_PAD_TEMPLATE_FACTORY (spider_src_factory,
43   "src",
44   GST_PAD_SRC,
45   GST_PAD_REQUEST,
46   NULL      /* no caps */
47 );
48
49 GST_PAD_TEMPLATE_FACTORY (spider_sink_factory,
50   "sink",
51   GST_PAD_SINK,
52   GST_PAD_REQUEST,
53   NULL      /* no caps */
54 );
55
56 /* SpiderIdentity signals and args */
57 enum {
58   /* FILL ME */
59   LAST_SIGNAL
60 };
61
62 enum {
63   ARG_0,
64   /* FILL ME */
65 };
66
67 /* GObject stuff */
68 static void                     gst_spider_identity_class_init          (GstSpiderIdentityClass *klass);
69 static void                     gst_spider_identity_init                (GstSpiderIdentity *spider_identity);
70
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);
76 /* loop functions */
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);
81
82 static gboolean                 gst_spider_identity_handle_src_event    (GstPad *pad, GstEvent *event);
83
84 /* set/get functions */
85 static void                     gst_spider_identity_set_caps            (GstSpiderIdentity *identity, GstCaps *caps);
86
87 /* callback */
88 static void                     callback_type_find_have_type            (GstElement *typefind, GstCaps *caps, GstSpiderIdentity *identity);
89
90 /* other functions */
91 static void                     gst_spider_identity_start_type_finding  (GstSpiderIdentity *ident);
92
93 static GstElementClass *        parent_class                            = NULL;
94 /* no signals
95 static guint gst_spider_identity_signals[LAST_SIGNAL] = { 0 }; */
96
97 GType
98 gst_spider_identity_get_type (void) 
99 {
100   static GType spider_identity_type = 0;
101
102   if (!spider_identity_type) {
103     static const GTypeInfo spider_identity_info = {
104       sizeof(GstSpiderIdentityClass),      NULL,
105       NULL,
106       (GClassInitFunc)gst_spider_identity_class_init,
107       NULL,
108       NULL,
109       sizeof(GstSpiderIdentity),
110       0,
111       (GInstanceInitFunc)gst_spider_identity_init,
112     };
113     spider_identity_type = g_type_register_static (GST_TYPE_ELEMENT, "GstSpiderIdentity", &spider_identity_info, 0);
114   }
115   return spider_identity_type;
116 }
117
118 static void 
119 gst_spider_identity_class_init (GstSpiderIdentityClass *klass) 
120 {
121   GstElementClass *gstelement_class = (GstElementClass *) klass;
122   
123   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
124   
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));
128   
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);
131 }
132
133 static GstBufferPool*
134 gst_spider_identity_get_bufferpool (GstPad *pad)
135 {
136   GstSpiderIdentity *ident;
137
138   ident = GST_SPIDER_IDENTITY (gst_pad_get_parent (pad));
139
140   return gst_pad_get_bufferpool (ident->src);
141 }
142
143 static void 
144 gst_spider_identity_init (GstSpiderIdentity *ident) 
145 {
146   /* sink */
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));
152   /* src */
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));
158
159   /* variables */
160   ident->plugged = FALSE;
161   
162   /* caching */
163   ident->cache_start = NULL;
164   ident->cache_end = NULL;
165   
166 }
167
168 static void 
169 gst_spider_identity_chain (GstPad *pad, GstBuffer *buf) 
170 {
171   GstSpiderIdentity *ident;
172   
173   /* g_print ("chaining on pad %s:%s with buffer %p\n", GST_DEBUG_PAD_NAME (pad), buf); */
174
175   g_return_if_fail (pad != NULL);
176   g_return_if_fail (GST_IS_PAD (pad));
177   if (buf == NULL) return;
178
179   ident = GST_SPIDER_IDENTITY (gst_pad_get_parent (pad));
180
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)
185     {
186       GstSpider *spider = (GstSpider *) GST_OBJECT_PARENT (ident);
187       GList *list = spider->connections;
188       while (list)
189       {
190         GstSpiderConnection *conn = (GstSpiderConnection *) list->data;
191         list = g_list_next (list);
192         if (conn->sink == ident)
193         {
194           gst_element_set_eos (GST_ELEMENT (conn->src));
195           gst_pad_push (conn->src->src, GST_BUFFER (gst_event_new (GST_EVENT_EOS)));  
196         }
197       }
198     }
199     /* end hack for current event stuff here */
200
201     gst_pad_event_default (ident->sink, GST_EVENT (buf));
202     return;
203   }
204
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);
210   }
211 }
212 GstSpiderIdentity*           
213 gst_spider_identity_new_src (gchar *name)
214 {
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));
220   
221   return ret;
222 }
223 GstSpiderIdentity*           
224 gst_spider_identity_new_sink (gchar *name)
225 {
226   //GstSpiderIdentity *ret = (GstSpiderIdentity *) g_object_new (gst_spider_identity_get_type (), NULL);
227   
228   //GST_ELEMENT_NAME (ret) = name;
229   GstSpiderIdentity *ret = (GstSpiderIdentity *) gst_element_factory_make ("spideridentity", name);
230
231   /* set the right functions */
232   gst_element_set_loop_function (GST_ELEMENT (ret), (GstElementLoopFunction) GST_DEBUG_FUNCPTR (gst_spider_identity_dumb_loop));
233
234   return ret;
235 }
236
237 /* shamelessly stolen from gstqueue.c to get proxy connections */
238 static GstPadConnectReturn
239 gst_spider_identity_connect (GstPad *pad, GstCaps *caps)
240 {
241   GstSpiderIdentity *spider_identity = GST_SPIDER_IDENTITY (gst_pad_get_parent (pad));
242   GstPad *otherpad;
243
244   if (pad == spider_identity->src) 
245     otherpad = spider_identity->sink;
246   else
247     otherpad = spider_identity->src;
248
249   if (otherpad != NULL)
250     return gst_pad_proxy_connect (otherpad, caps);
251   
252   return GST_PAD_CONNECT_OK;
253 }
254
255 static GstCaps*
256 gst_spider_identity_getcaps (GstPad *pad, GstCaps *caps)
257 {
258   GstSpiderIdentity *spider_identity = GST_SPIDER_IDENTITY (gst_pad_get_parent (pad));
259   GstPad *otherpad;
260
261   if (pad == spider_identity->src) 
262     otherpad = spider_identity->sink;
263   else
264     otherpad = spider_identity->src;
265
266   if (otherpad != NULL)
267     return gst_pad_get_allowed_caps (otherpad);
268   return NULL;
269 }
270
271 GstPad*
272 gst_spider_identity_request_new_pad  (GstElement *element, GstPadTemplate *templ, const gchar *name)
273 {
274   GstSpiderIdentity *ident;
275   
276   /*checks */
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);
282   
283   switch (GST_PAD_TEMPLATE_DIRECTION (templ))
284   {
285     case GST_PAD_SINK:
286       if (ident->sink != NULL) break;
287       /* sink */
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));
294       return ident->sink;
295     case GST_PAD_SRC:
296       /* src */
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));
304       return ident->src;
305     default:
306       break;
307   }
308   
309   GST_DEBUG(0, "element %s requested a new pad but none could be created", GST_ELEMENT_NAME(ident));
310   return NULL;
311 }
312
313 /* this function has to
314  * - start the autoplugger
315  * - start type finding
316  * ...
317  */
318 static GstElementStateReturn
319 gst_spider_identity_change_state (GstElement *element)
320 {
321   GstSpiderIdentity *ident;
322   GstSpider *spider;
323   GstElementStateReturn ret = GST_STATE_SUCCESS;
324   
325   /* element check */
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);
329   
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);
336   
337       /* start typefinding or plugging */
338       if ((GST_RPAD_PEER (ident->sink) != NULL) && (GST_RPAD_PEER (ident->src) == NULL))
339       {
340         if (gst_pad_get_caps ((GstPad *) GST_PAD_PEER (ident->sink)) == NULL)
341         {
342           gst_spider_identity_start_type_finding (ident);
343           break;
344         } else {
345           gst_spider_identity_plug (ident);
346         }
347       }
348       /* autoplug on src */
349       if ((GST_RPAD_PEER (ident->src) != NULL) && (GST_RPAD_PEER (ident->sink) == NULL))
350       {
351         gst_spider_identity_plug (ident);
352       }
353     default:
354       break;
355   }
356   
357   if ((ret != GST_STATE_FAILURE) && (GST_ELEMENT_CLASS (parent_class)->change_state))
358     ret = GST_ELEMENT_CLASS (parent_class)->change_state (element);
359   
360   return ret;
361 }
362
363 static void
364 gst_spider_identity_start_type_finding (GstSpiderIdentity *ident)
365 {
366   GstElement* typefind;
367   gchar *name;
368   gboolean restart = FALSE;
369   
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)
372   {
373     gst_element_set_state (GST_ELEMENT (GST_ELEMENT_PARENT (ident)), GST_STATE_PAUSED);
374     restart = TRUE;
375   }
376   
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);
380   g_free (name);
381   
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"));
386   
387   gst_element_set_loop_function (GST_ELEMENT (ident), (GstElementLoopFunction) GST_DEBUG_FUNCPTR (gst_spider_identity_sink_loop_type_finding));
388
389   if (restart)
390   {
391     gst_element_set_state (GST_ELEMENT (GST_ELEMENT_PARENT (ident)), GST_STATE_PLAYING);
392   }
393 }
394 /* waiting for a good suggestion on where to set the caps from typefinding
395  * Caps must be cleared when pad is disconnected
396  * 
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.
399  */
400 void
401 gst_spider_identity_set_caps (GstSpiderIdentity *ident, GstCaps *caps)
402 {
403   if (ident->src)
404   {
405     gst_pad_try_set_caps (ident->src, caps);
406   }
407 }
408
409
410 static void
411 callback_type_find_have_type (GstElement *typefind, GstCaps *caps, GstSpiderIdentity *ident)
412 {
413   gboolean restart_spider = FALSE;
414   
415   GST_INFO (GST_CAT_AUTOPLUG, "element %s has found caps\n", GST_ELEMENT_NAME(ident));
416
417   /* checks */  
418   g_assert (GST_IS_ELEMENT (typefind));
419   g_assert (GST_IS_SPIDER_IDENTITY (ident));
420   
421   /* pause the autoplugger */
422   if (gst_element_get_state (GST_ELEMENT (GST_ELEMENT_PARENT(ident))) == GST_STATE_PLAYING)
423   {
424     gst_element_set_state (GST_ELEMENT (GST_ELEMENT_PARENT(ident)), GST_STATE_PAUSED);
425     restart_spider = TRUE;
426   }
427
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);
431   
432   /* set caps */
433   gst_spider_identity_set_caps (ident, caps);
434   
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));
437   
438   /* autoplug this pad */
439   gst_spider_identity_plug (ident);  
440   
441   /* restart autoplugger */
442   if (restart_spider)
443     gst_element_set_state (GST_ELEMENT (GST_ELEMENT_PARENT(ident)), GST_STATE_PLAYING);
444   
445 }
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
448  */
449 static void
450 gst_spider_identity_dumb_loop  (GstSpiderIdentity *ident)
451 {
452   GstBuffer *buf;
453
454   g_return_if_fail (ident != NULL);
455   g_return_if_fail (GST_IS_SPIDER_IDENTITY (ident));
456   g_assert (ident->sink != NULL);
457
458   buf = gst_pad_pull (ident->sink);
459
460   gst_spider_identity_chain (ident->sink, buf);
461 }
462 /* do nothing until we're connected - then disable yourself
463  */
464 static void
465 gst_spider_identity_src_loop (GstSpiderIdentity *ident)
466 {
467   /* checks - disable for speed */
468   g_return_if_fail (ident != NULL);
469   g_return_if_fail (GST_IS_SPIDER_IDENTITY (ident));
470   
471   /* we don't want a loop function if we're plugged */
472   if (ident->sink && GST_PAD_PEER (ident->sink))
473   {
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);
476     return;
477   }
478   
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
482    */
483   gst_element_interrupt (GST_ELEMENT (ident));
484   return;  
485 }
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.
489  */
490 static void
491 gst_spider_identity_sink_loop_type_finding (GstSpiderIdentity *ident)
492 {
493   GstBuffer *buf;
494   
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);
499   
500   /* get buffer */
501   buf = gst_pad_pull (ident->sink);
502   
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);
508   } 
509
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;
516   
517   /* push the buffer */
518   gst_spider_identity_chain (ident->sink, buf);
519 }
520 /* this function is needed after typefinding:
521  * empty the cache and when the cache is empty - remove this function
522  */
523 static void
524 gst_spider_identity_sink_loop_emptycache (GstSpiderIdentity *ident)
525 {
526   GstBuffer *buf;
527   
528   /* get the buffer and push it */
529   buf = GST_BUFFER (ident->cache_start->data);
530   gst_spider_identity_chain (ident->sink, buf);
531   
532   ident->cache_start = g_list_previous (ident->cache_start);
533
534   /* now check if we have more buffers to push */
535   if (ident->cache_start == NULL)
536   {
537     GST_DEBUG(0, "cache from %s is empty, changing loop function", GST_ELEMENT_NAME(ident));
538     /* free cache */
539     g_list_free (ident->cache_end);
540     ident->cache_end = NULL;
541     
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));
545   }  
546 }
547
548 static gboolean
549 gst_spider_identity_handle_src_event (GstPad *pad, GstEvent *event)
550 {
551   gboolean res = TRUE;
552   GstSpiderIdentity *ident;
553
554   GST_DEBUG (0, "spider_identity src_event\n");
555
556   ident = GST_SPIDER_IDENTITY (gst_pad_get_parent (pad));
557
558   switch (GST_EVENT_TYPE (event)) {
559     case GST_EVENT_FLUSH:
560     case GST_EVENT_SEEK:
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 
566          * next buffer */
567         return TRUE;
568       }
569     default:
570       res = gst_pad_event_default (pad, event);
571       break;
572   }
573
574   return res;
575 }
576
577