parts of the patch submitted in bug #113913
[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_STATIC (gst_spider_identity_debug);
31 #define GST_CAT_DEFAULT gst_spider_identity_debug
32
33 static GstElementDetails gst_spider_identity_details = GST_ELEMENT_DETAILS (
34   "SpiderIdentity",
35   "Generic",
36   "Link between spider and outside elements",
37   "Benjamin Otte <in7y118@public.uni-hamburg.de>"
38 );
39
40
41 /* generic templates 
42  * delete me when meging with spider.c
43  */
44 static GstStaticPadTemplate spider_src_factory =
45 GST_STATIC_PAD_TEMPLATE (
46   "src",
47   GST_PAD_SRC,
48   GST_PAD_ALWAYS,
49   GST_STATIC_CAPS_ANY
50 );
51
52 static GstStaticPadTemplate spider_sink_factory =
53 GST_STATIC_PAD_TEMPLATE (
54   "sink",
55   GST_PAD_SINK,
56   GST_PAD_ALWAYS,
57   GST_STATIC_CAPS_ANY
58 );
59
60 /* SpiderIdentity signals and args */
61 enum {
62   /* FILL ME */
63   LAST_SIGNAL
64 };
65
66 enum {
67   ARG_0
68   /* FILL ME */
69 };
70
71 /* GObject stuff */
72 static void                     gst_spider_identity_class_init          (GstSpiderIdentityClass *klass);
73 static void                     gst_spider_identity_init                (GstSpiderIdentity *spider_identity);
74
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);
80 /* loop functions */
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);
84
85 static gboolean                 gst_spider_identity_handle_src_event    (GstPad *pad, GstEvent *event);
86
87 /* other functions */
88 static void                     gst_spider_identity_start_type_finding  (GstSpiderIdentity *ident);
89
90 static GstElementClass *        parent_class                            = NULL;
91 /* no signals
92 static guint gst_spider_identity_signals[LAST_SIGNAL] = { 0 }; */
93
94 GType
95 gst_spider_identity_get_type (void) 
96 {
97   static GType spider_identity_type = 0;
98
99   if (!spider_identity_type) {
100     static const GTypeInfo spider_identity_info = {
101       sizeof(GstSpiderIdentityClass),      NULL,
102       NULL,
103       (GClassInitFunc)gst_spider_identity_class_init,
104       NULL,
105       NULL,
106       sizeof(GstSpiderIdentity),
107       0,
108       (GInstanceInitFunc)gst_spider_identity_init,
109     };
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");
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,
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);
131   
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);
134 }
135
136 static void 
137 gst_spider_identity_init (GstSpiderIdentity *ident) 
138 {
139   /* sink */
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));
145   /* src */
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));
152
153   /* variables */
154   ident->plugged = FALSE;
155 }
156
157 static void 
158 gst_spider_identity_chain (GstPad *pad, GstBuffer *buf) 
159 {
160   GstSpiderIdentity *ident;
161   
162   /*g_print ("chaining on pad %s:%s with buffer %p\n", GST_DEBUG_PAD_NAME (pad), buf);*/
163
164   g_return_if_fail (pad != NULL);
165   g_return_if_fail (GST_IS_PAD (pad));
166   if (buf == NULL) return;
167
168   ident = GST_SPIDER_IDENTITY (gst_pad_get_parent (pad));
169
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)
174     {
175       GstSpider *spider = (GstSpider *) GST_OBJECT_PARENT (ident);
176       GList *list = spider->links;
177       while (list)
178       {
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));
186         }
187       }
188     }
189     /* end hack for current event stuff here */
190
191     gst_pad_event_default (pad, GST_EVENT (buf));
192     return;
193   }
194
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);
201   }
202 }
203 GstSpiderIdentity*           
204 gst_spider_identity_new_src (gchar *name)
205 {
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));
209   
210   return ret;
211 }
212 GstSpiderIdentity*           
213 gst_spider_identity_new_sink (gchar *name)
214 {
215   GstSpiderIdentity *ret = (GstSpiderIdentity *) gst_element_factory_make ("spideridentity", name);
216
217   /* set the right functions */
218   gst_element_set_loop_function (GST_ELEMENT (ret), (GstElementLoopFunction) GST_DEBUG_FUNCPTR (gst_spider_identity_dumb_loop));
219
220   return ret;
221 }
222
223 /* shamelessly stolen from gstqueue.c to get proxy links */
224 static GstPadLinkReturn
225 gst_spider_identity_link (GstPad *pad, const GstCaps *caps)
226 {
227   GstSpiderIdentity *spider_identity = GST_SPIDER_IDENTITY (gst_pad_get_parent (pad));
228   GstPad *otherpad;
229
230   if (pad == spider_identity->src) 
231     otherpad = spider_identity->sink;
232   else
233     otherpad = spider_identity->src;
234
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;
238
239   return gst_pad_try_set_caps (otherpad, gst_caps_copy (caps));
240 }
241
242 static GstCaps*
243 gst_spider_identity_getcaps (GstPad *pad)
244 {
245   GstSpiderIdentity *ident = GST_SPIDER_IDENTITY (gst_pad_get_parent (pad));
246   GstPad *otherpad;
247
248   if (pad == ident->src) 
249     otherpad = ident->sink;
250   else
251     otherpad = ident->src;
252
253   if (otherpad != NULL) {
254     if (GST_PAD_PEER (otherpad)) {
255       GstCaps *ret = gst_pad_get_allowed_caps (otherpad);
256       if (ident->caps) {
257         GstCaps *ret2 = gst_caps_intersect (ident->caps, ret);
258         gst_caps_free (ret);
259         ret = ret2;
260       }
261       return ret;
262     }
263   }
264   if (ident->caps)
265     return gst_caps_copy (ident->caps);
266
267   return gst_caps_new_any ();
268 }
269
270 GstPad*
271 gst_spider_identity_request_new_pad  (GstElement *element, GstPadTemplate *templ, const gchar *name)
272 {
273   GstSpiderIdentity *ident;
274   
275   /*checks */
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);
281   
282   switch (GST_PAD_TEMPLATE_DIRECTION (templ))
283   {
284     case GST_PAD_SINK:
285       if (ident->sink != NULL) break;
286       /* sink */
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));
292       return ident->sink;
293     case GST_PAD_SRC:
294       /* src */
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));
302       return ident->src;
303     default:
304       break;
305   }
306   
307   GST_DEBUG ( "element %s requested a new pad but none could be created", GST_ELEMENT_NAME(ident));
308   return NULL;
309 }
310
311 /* this function has to
312  * - start the autoplugger
313  * - start type finding
314  * ...
315  */
316 static GstElementStateReturn
317 gst_spider_identity_change_state (GstElement *element)
318 {
319   GstSpiderIdentity *ident;
320   GstSpider *spider;
321   GstElementStateReturn ret = GST_STATE_SUCCESS;
322   
323   /* element check */
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);
327   
328   switch (GST_STATE_TRANSITION (element)) {
329     case GST_STATE_PAUSED_TO_READY:
330       gst_caps_replace (&ident->caps, NULL);
331       break;
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);
337   
338       /* start typefinding or plugging */
339       if ((GST_RPAD_PEER (ident->sink) != NULL) && (GST_RPAD_PEER (ident->src) == NULL))
340       {
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))
343         {
344           gst_spider_identity_start_type_finding (ident);
345           gst_caps_free (caps);
346           break;
347         } else {
348           gst_spider_identity_plug (ident);
349         }
350         gst_caps_free (caps);
351       }
352       /* autoplug on src */
353       if ((GST_RPAD_PEER (ident->src) != NULL) && (GST_RPAD_PEER (ident->sink) == NULL))
354       {
355         gst_spider_identity_plug (ident);
356       }
357     default:
358       break;
359   }
360   
361   if ((ret != GST_STATE_FAILURE) && (GST_ELEMENT_CLASS (parent_class)->change_state))
362     ret = GST_ELEMENT_CLASS (parent_class)->change_state (element);
363   
364   return ret;
365 }
366
367 static void
368 gst_spider_identity_start_type_finding (GstSpiderIdentity *ident)
369 {
370 /*  GstElement* typefind;
371   gchar *name;*/
372   gboolean restart = FALSE;
373   
374   GST_DEBUG ("element %s starts typefinding", GST_ELEMENT_NAME(ident));
375   if (GST_STATE (GST_ELEMENT_PARENT (ident)) == GST_STATE_PLAYING)
376   {
377     gst_element_set_state (GST_ELEMENT (GST_ELEMENT_PARENT (ident)), GST_STATE_PAUSED);
378     restart = TRUE;
379   }
380
381   gst_element_set_loop_function (GST_ELEMENT (ident), (GstElementLoopFunction) GST_DEBUG_FUNCPTR (gst_spider_identity_sink_loop_type_finding));
382
383   if (restart)
384   {
385     gst_element_set_state (GST_ELEMENT (GST_ELEMENT_PARENT (ident)), GST_STATE_PLAYING);
386   }
387 }
388
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
391  */
392 static void
393 gst_spider_identity_dumb_loop  (GstSpiderIdentity *ident)
394 {
395   GstBuffer *buf;
396
397   g_return_if_fail (ident != NULL);
398   g_return_if_fail (GST_IS_SPIDER_IDENTITY (ident));
399   g_assert (ident->sink != NULL);
400
401   buf = GST_BUFFER (gst_pad_pull (ident->sink));
402
403   gst_spider_identity_chain (ident->sink, buf);
404 }
405 /* do nothing until we're linked - then disable yourself
406  */
407 static void
408 gst_spider_identity_src_loop (GstSpiderIdentity *ident)
409 {
410   /* checks - disable for speed */
411   g_return_if_fail (ident != NULL);
412   g_return_if_fail (GST_IS_SPIDER_IDENTITY (ident));
413   
414   /* we don't want a loop function if we're plugged */
415   if (ident->sink && GST_PAD_PEER (ident->sink))
416   {
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);
419     return;
420   }
421   gst_element_interrupt (GST_ELEMENT (ident));
422 }
423 /* This loop function is only needed when typefinding.
424  */
425 typedef struct {
426   GstBuffer *buffer;
427   guint best_probability;
428   GstCaps *caps;
429 } SpiderTypeFind;
430 guint8 *
431 spider_find_peek (gpointer data, gint64 offset, guint size)
432 {
433   SpiderTypeFind *find = (SpiderTypeFind *) data;
434   gint64 buffer_offset = GST_BUFFER_OFFSET_IS_VALID (find->buffer) ? 
435                          GST_BUFFER_OFFSET (find->buffer) : 0;
436   
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;
440   } else {
441     GST_LOG ("peek %"G_GINT64_FORMAT", %u failed", offset, size);
442     return NULL;
443   }
444 }
445 static void
446 spider_find_suggest (gpointer data, guint probability, const GstCaps *caps)
447 {
448   SpiderTypeFind *find = (SpiderTypeFind *) data;
449
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;
454   }
455 }
456 static void
457 gst_spider_identity_sink_loop_type_finding (GstSpiderIdentity *ident)
458 {
459   GstData *data;
460   GstTypeFind gst_find;
461   SpiderTypeFind find;
462   GList *walk, *type_list = NULL;
463
464   g_return_if_fail (GST_IS_SPIDER_IDENTITY (ident));
465
466   data = gst_pad_pull (ident->sink);
467   if (!GST_IS_BUFFER (data)) {
468     gst_spider_identity_chain (ident->sink, GST_BUFFER (data));
469     return;
470   }
471   
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)) {
476     goto plug;
477   } else {
478     gst_caps_free (find.caps);
479     find.caps = NULL;
480   }
481   
482   /* now do the actual typefinding with the supplied buffer */
483   walk = type_list = gst_type_find_factory_get_list ();
484     
485   find.best_probability = 0;
486   find.caps = NULL;
487   gst_find.data = &find;
488   gst_find.peek = spider_find_peek;
489   gst_find.suggest = spider_find_suggest;
490   while (walk) {
491     GstTypeFindFactory *factory = GST_TYPE_FIND_FACTORY (walk->data);
492
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)
496       goto plug;
497     walk = g_list_next (walk);
498   }
499   if (find.best_probability > 0)
500     goto plug;
501   GST_ELEMENT_ERROR (ident, STREAM, TYPE_NOT_FOUND, NULL, NULL);
502   find.buffer = GST_BUFFER (gst_event_new (GST_EVENT_EOS));
503
504 end:
505   /* remove loop function */
506   gst_element_set_loop_function (GST_ELEMENT (ident), 
507                                 (GstElementLoopFunction) GST_DEBUG_FUNCPTR (gst_spider_identity_dumb_loop));
508   
509   /* push the buffer */
510   gst_spider_identity_chain (ident->sink, find.buffer);
511   
512   return;
513
514 plug:
515   GST_INFO ("typefind function found caps"); 
516   ident->caps = find.caps;
517   if (GST_PAD_IS_LINKED (ident->src)) {
518     GstPadLinkReturn ret;
519
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");
523     }
524   }
525   GST_LOG_OBJECT (ident, "spider starting caps: %" GST_PTR_FORMAT, find.caps);
526   if (type_list)
527     g_list_free (type_list);
528
529   gst_spider_identity_plug (ident);
530
531   goto end;
532 }
533
534 static gboolean
535 gst_spider_identity_handle_src_event (GstPad *pad, GstEvent *event)
536 {
537   gboolean res = TRUE;
538   GstSpiderIdentity *ident;
539
540   GST_DEBUG ( "spider_identity src_event");
541
542   ident = GST_SPIDER_IDENTITY (gst_pad_get_parent (pad));
543
544   switch (GST_EVENT_TYPE (event)) {
545     case GST_EVENT_FLUSH:
546     case GST_EVENT_SEEK:
547     default:
548       res = gst_pad_event_default (pad, event);
549       break;
550   }
551
552   return res;
553 }