schedule ws renamed to scheduler.
[platform/upstream/gstreamer.git] / gst / autoplug / gstautoplugger.c
1 /* GStreamer
2  * Copyright (C) 2001 RidgeRun, Inc. (www.ridgerun.com)
3  *
4  * gstautoplugger.c: Data  for the dynamic autopluggerger
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #include <gst/gst.h>
23
24 GstElementDetails gst_autoplugger_details = {
25   "Dynamic autoplugger",
26   "Autoplugger",
27   "Magic element that converts from any type to any other",
28   VERSION,
29   "Erik Walthinsen <omega@temple-baptist.com>",
30   "(C) 2001 RidgeRun, Inc. (www.ridgerun.com)",
31 };
32
33 #define GST_TYPE_AUTOPLUGGER \
34   (gst_autoplugger_get_type())
35 #define GST_AUTOPLUGGER(obj) \
36   (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_AUTOPLUGGER,GstAutoplugger))
37 #define GST_AUTOPLUGGER_CLASS(klass) \
38   (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_AUTOPLUGGER,GstAutopluggerClass))
39 #define GST_IS_AUTOPLUGGER(obj) \
40   (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_AUTOPLUGGER))
41 #define GST_IS_AUTOPLUGGER_CLASS(obj) \
42   (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_AUTOPLUGGER))
43
44 typedef struct _GstAutoplugger GstAutoplugger;
45 typedef struct _GstAutopluggerClass GstAutopluggerClass;
46
47 struct _GstAutoplugger {
48   GstBin bin;
49   gint paused;
50
51   GstElement *cache;
52   gboolean cache_first_buffer;
53   GstPad *cache_sinkpad, *cache_srcpad;
54
55   GstElement *typefind;
56   GstPad *typefind_sinkpad;
57
58   GstPad *sinkpadpeer, *srcpadpeer;
59   GstCaps *sinkcaps, *srccaps;
60
61   GstCaps *sinktemplatecaps;
62
63   GstAutoplug *autoplug;
64   GstElement *autobin;
65
66   gboolean disable_nocaps;
67 };
68
69 struct _GstAutopluggerClass {
70   GstBinClass parent_class;
71 };
72
73
74 /*  signals and args */
75 enum {
76   LAST_SIGNAL
77 };
78
79 enum {
80   ARG_0,
81 };
82
83
84 static void                     gst_autoplugger_class_init      (GstAutopluggerClass *klass);
85 static void                     gst_autoplugger_init            (GstAutoplugger *queue);
86
87 static void                     gst_autoplugger_set_property            (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
88 static void                     gst_autoplugger_get_property            (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
89
90 //static GstElementStateReturn  gst_autoplugger_change_state    (GstElement *element);
91
92
93 static void     gst_autoplugger_external_sink_caps_changed      (GstPad *pad, GstCaps *caps, GstAutoplugger *autoplugger);
94 static void     gst_autoplugger_external_src_caps_changed       (GstPad *pad, GstCaps *caps, GstAutoplugger *autoplugger);
95 static void     gst_autoplugger_external_sink_caps_nego_failed  (GstPad *pad, gboolean *result, GstAutoplugger *autoplugger);
96 static void     gst_autoplugger_external_src_caps_nego_failed   (GstPad *pad, gboolean *result, GstAutoplugger *autoplugger);
97 static void     gst_autoplugger_external_sink_connected         (GstPad *pad, GstPad *peerpad, GstAutoplugger *autoplugger);
98 static void     gst_autoplugger_external_src_connected          (GstPad *pad, GstPad *peerpad, GstAutoplugger *autoplugger);
99
100 static void     gst_autoplugger_cache_first_buffer              (GstElement *element,GstBuffer *buf,GstAutoplugger *autoplugger);
101 static void     gst_autoplugger_cache_empty                     (GstElement *element, GstAutoplugger *autoplugger);
102 static void     gst_autoplugger_typefind_have_type              (GstElement *element, GstCaps *caps, GstAutoplugger *autoplugger);
103
104 static GstElementClass *parent_class = NULL;
105 //static guint gst_autoplugger_signals[LAST_SIGNAL] = { 0 };
106
107 GType
108 gst_autoplugger_get_type(void) {
109   static GType autoplugger_type = 0;
110
111   if (!autoplugger_type) {
112     static const GTypeInfo autoplugger_info = {
113       sizeof(GstAutopluggerClass),
114       NULL,
115       NULL,
116       (GClassInitFunc)gst_autoplugger_class_init,
117       NULL,
118       NULL,
119       sizeof(GstAutoplugger),
120       0,
121       (GInstanceInitFunc)gst_autoplugger_init,
122     };
123     autoplugger_type = g_type_register_static (GST_TYPE_BIN, "GstAutoplugger", &autoplugger_info, 0);
124   }
125   return autoplugger_type;
126 }
127
128 static void
129 gst_autoplugger_class_init (GstAutopluggerClass *klass)
130 {
131   GObjectClass *gobject_class;
132   GstElementClass *gstelement_class;
133
134   gobject_class = (GObjectClass*)klass;
135   gstelement_class = (GstElementClass*)klass;
136
137   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
138
139 /*
140   gst_autoplugger_signals[_EMPTY] =
141     g_signal_new ("_empty", G_OBJECT_TYPE(gobject_class), G_SIGNAL_RUN_LAST,
142                     G_STRUCT_OFFSET (GstAutopluggerClass, _empty), NULL, NULL,
143                     g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
144 */
145
146 /*
147   g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_BUFFER_COUNT,
148     g_param_spec_int("buffer_count","buffer_count","buffer_count",
149                       0,G_MAXINT,0,G_PARAM_READABLE)); // CHECKME!
150   g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_RESET,
151     g_param_spec_boolean("reset","reset","reset",
152                          FALSE,G_PARAM_WRITABLE)); // CHECKME!
153 */
154
155   gobject_class->set_property = gst_autoplugger_set_property;
156   gobject_class->get_property = gst_autoplugger_get_property;
157
158 //  gstelement_class->change_state = gst_autoplugger_change_state;
159 }
160
161 static void
162 gst_autoplugger_init (GstAutoplugger *autoplugger)
163 {
164   // create the autoplugger cache, which is the fundamental unit of the autopluggerger
165   // FIXME we need to find a way to set element's name before _init
166   // FIXME ... so we can name the subelements uniquely
167   autoplugger->cache = gst_elementfactory_make("autoplugcache", "unnamed_autoplugcache");
168   g_return_if_fail (autoplugger->cache != NULL);
169
170   GST_DEBUG(GST_CAT_AUTOPLUG, "turning on caps nego proxying in cache\n");
171   g_object_set(G_OBJECT(autoplugger->cache),"caps_proxy",TRUE,NULL);
172
173   // attach signals to the cache
174   g_signal_connect (G_OBJECT (autoplugger->cache), "first_buffer",
175                      G_CALLBACK (gst_autoplugger_cache_first_buffer), autoplugger);
176
177   // add the cache to self
178   gst_bin_add (GST_BIN(autoplugger), autoplugger->cache);
179
180   // get the cache's pads so we can attach stuff to them
181   autoplugger->cache_sinkpad = gst_element_get_pad (autoplugger->cache, "sink");
182   autoplugger->cache_srcpad = gst_element_get_pad (autoplugger->cache, "src");
183
184   // attach handlers to the typefind pads
185   g_signal_connect (G_OBJECT (autoplugger->cache_sinkpad), "caps_changed",
186                      G_CALLBACK (gst_autoplugger_external_sink_caps_changed), autoplugger);
187   g_signal_connect (G_OBJECT (autoplugger->cache_srcpad), "caps_changed",
188                      G_CALLBACK (gst_autoplugger_external_src_caps_changed), autoplugger);
189   g_signal_connect (G_OBJECT (autoplugger->cache_sinkpad), "caps_nego_failed",
190                      G_CALLBACK (gst_autoplugger_external_sink_caps_nego_failed), autoplugger);
191   g_signal_connect (G_OBJECT (autoplugger->cache_srcpad), "caps_nego_failed",
192                      G_CALLBACK (gst_autoplugger_external_src_caps_nego_failed), autoplugger);
193 //  g_signal_connect (G_OBJECT (autoplugger->cache_sinkpad), "connected",
194 //                     gst_autoplugger_external_sink_connected, autoplugger);
195 //  g_signal_connect (G_OBJECT (autoplugger->cache_srcpad), "connected",
196 //                     gst_autoplugger_external_src_connected, autoplugger);
197
198   // ghost both of these pads to the outside world
199   gst_element_add_ghost_pad (GST_ELEMENT(autoplugger), autoplugger->cache_sinkpad, "sink");
200   gst_element_add_ghost_pad (GST_ELEMENT(autoplugger), autoplugger->cache_srcpad, "src");
201 }
202
203
204 static void
205 gst_autoplugger_external_sink_connected(GstPad *pad, GstPad *peerpad, GstAutoplugger *autoplugger)
206 {
207   GstPadTemplate *peertemplate;
208   GstCaps *peercaps, *peertemplatecaps;
209
210   GST_INFO(GST_CAT_AUTOPLUG, "have cache:sink connected");
211 //  autoplugger->sinkpadpeer = peerpad;
212
213   if (autoplugger->sinkpadpeer) {
214     peercaps = GST_PAD_CAPS(autoplugger->sinkpadpeer);
215     if (peercaps)
216       GST_INFO(GST_CAT_AUTOPLUG, "there are some caps on this pad's peer: %s",
217                gst_caps_get_mime(peercaps));
218     peertemplate = GST_PAD_PADTEMPLATE(autoplugger->sinkpadpeer);
219     if (peertemplate) {
220       peertemplatecaps = GST_PADTEMPLATE_CAPS(peertemplate);
221       if (peertemplatecaps) {
222         GST_INFO(GST_CAT_AUTOPLUG, "there are some caps on this pad's peer's padtemplate %s",
223                  gst_caps_get_mime(peertemplatecaps));
224       }
225     }
226   }
227 }
228
229 static void
230 gst_autoplugger_external_src_connected(GstPad *pad, GstPad *peerpad, GstAutoplugger *autoplugger)
231 {
232   GstPadTemplate *peertemplate;
233   GstCaps *peercaps, *peertemplatecaps;
234
235   GST_INFO(GST_CAT_AUTOPLUG, "have cache:src connected");
236 //  autoplugger->srcpadpeer = peerpad;
237
238   if (autoplugger->srcpadpeer) {
239     peercaps = GST_PAD_CAPS(autoplugger->srcpadpeer);
240     if (peercaps)
241       GST_INFO(GST_CAT_AUTOPLUG, "there are some caps on this pad's peer: %s",
242                gst_caps_get_mime(peercaps));
243     peertemplate = GST_PAD_PADTEMPLATE(autoplugger->srcpadpeer);
244     if (peertemplate) {
245       peertemplatecaps = GST_PADTEMPLATE_CAPS(peertemplate);
246       if (peertemplatecaps) {
247         GST_INFO(GST_CAT_AUTOPLUG, "there are some caps on this pad's peer's padtemplate %s",
248                  gst_caps_get_mime(peertemplatecaps));
249         autoplugger->sinktemplatecaps = peertemplatecaps;
250 //        GST_DEBUG(GST_CAT_AUTOPLUG, "turning on caps nego proxying in cache\n");
251 //        gtk_object_set(G_OBJECT(autoplugger->cache),"caps_proxy",TRUE,NULL);
252       }
253     }
254   }
255 }
256
257
258 static void
259 gst_autoplugger_external_sink_caps_changed(GstPad *pad, GstCaps *caps, GstAutoplugger *autoplugger)
260 {
261   GST_INFO(GST_CAT_AUTOPLUG, "have cache:sink caps of %s\n",gst_caps_get_mime(caps));
262   autoplugger->sinkcaps = caps;
263 }
264
265 static void
266 gst_autoplugger_external_src_caps_changed(GstPad *pad, GstCaps *caps, GstAutoplugger *autoplugger)
267 {
268   GST_INFO(GST_CAT_AUTOPLUG, "have cache:src caps of %s\n",gst_caps_get_mime(caps));
269   autoplugger->srccaps = caps;
270 }
271
272
273 static gboolean
274 gst_autoplugger_autoplug(GstAutoplugger *autoplugger,GstPad *srcpad,GstCaps *srccaps,GstCaps *sinkcaps)
275 {
276   GstPad *sinkpad;
277
278   sinkpad = GST_PAD(GST_PAD_PEER(srcpad));
279   GST_DEBUG(GST_CAT_AUTOPLUG,"disconnecting %s:%s and %s:%s to autoplug between them\n",
280             GST_DEBUG_PAD_NAME(srcpad),GST_DEBUG_PAD_NAME(sinkpad));
281   GST_DEBUG(GST_CAT_AUTOPLUG,"srcpadcaps are of type %s\n",gst_caps_get_mime(srccaps));
282   GST_DEBUG(GST_CAT_AUTOPLUG,"sinkpadcaps are of type %s\n",gst_caps_get_mime(sinkcaps));
283
284   // disconnect the pads
285   GST_DEBUG(GST_CAT_AUTOPLUG, "disconnecting the pads that will be joined by an autobin\n");
286   gst_pad_disconnect(srcpad,sinkpad);
287
288   if (!autoplugger->autoplug) {
289     autoplugger->autoplug = gst_autoplugfactory_make("static");
290     g_return_val_if_fail(autoplugger->autoplug != NULL, FALSE);
291   }
292   GST_DEBUG(GST_CAT_AUTOPLUG, "building autoplugged bin between caps\n");
293   autoplugger->autobin = gst_autoplug_to_caps(autoplugger->autoplug,
294     srccaps,sinkcaps,NULL);
295   g_return_val_if_fail(autoplugger->autobin != NULL, FALSE);
296   gst_bin_add(GST_BIN(autoplugger),autoplugger->autobin);
297
298 gst_scheduler_show(GST_ELEMENT_SCHED(autoplugger));
299
300   // FIXME this is a hack
301 //  GST_DEBUG(GST_CAT_AUTOPLUG, "copying failed caps to srcpad %s:%s to ensure renego\n",GST_DEBUG_PAD_NAME(autoplugger->cache_srcpad));
302 //  gst_pad_set_caps(srcpad,srccaps);
303
304   if (GST_PAD_CAPS(srcpad) == NULL) GST_DEBUG(GST_CAT_AUTOPLUG,"no caps on cache:src!\n");
305
306   // attach the autoplugged bin
307   GST_DEBUG(GST_CAT_AUTOPLUG, "attaching the autoplugged bin between the two pads\n");
308   gst_pad_connect(srcpad,gst_element_get_pad(autoplugger->autobin,"sink"));
309 gst_scheduler_show(GST_ELEMENT_SCHED(autoplugger));
310   gst_pad_connect(gst_element_get_pad(autoplugger->autobin,"src_00"),sinkpad);
311 gst_scheduler_show(GST_ELEMENT_SCHED(autoplugger));
312
313   // FIXME try to force the renego
314 //  GST_DEBUG(GST_CAT_AUTOPLUG, "trying to force everyone to nego\n");
315 //  gst_pad_renegotiate(gst_element_get_pad(autoplugger->autobin,"sink"));
316 //  gst_pad_renegotiate(sinkpad);
317
318   return TRUE;
319 }
320
321 static void
322 gst_autoplugger_external_sink_caps_nego_failed(GstPad *pad, gboolean *result, GstAutoplugger *autoplugger)
323 {
324   GstPad *srcpad_peer;
325   GstPadTemplate *srcpad_peer_template;
326   GstCaps *srcpad_peer_caps;
327   GstPad *sinkpad_peer;
328   GstCaps *sinkpad_peer_caps;
329
330   GST_INFO(GST_CAT_AUTOPLUG, "have caps nego failure on sinkpad %s:%s!!!",GST_DEBUG_PAD_NAME(pad));
331
332   autoplugger->paused++;
333   if (autoplugger->paused == 1)
334     // try to PAUSE the whole thing
335     gst_element_set_state(GST_ELEMENT_SCHED(autoplugger)->parent,GST_STATE_PAUSED);
336
337   srcpad_peer = GST_PAD(GST_PAD_PEER(autoplugger->cache_srcpad));
338   g_return_if_fail(srcpad_peer != NULL);
339   srcpad_peer_template = GST_PAD_PADTEMPLATE(srcpad_peer);
340   g_return_if_fail(srcpad_peer_template != NULL);
341   srcpad_peer_caps = GST_PADTEMPLATE_CAPS(srcpad_peer_template);
342   g_return_if_fail(srcpad_peer_caps != NULL);
343
344   sinkpad_peer = GST_PAD(GST_PAD_PEER(pad));
345   g_return_if_fail(sinkpad_peer != NULL);
346   sinkpad_peer_caps = GST_PAD_CAPS(sinkpad_peer);
347   g_return_if_fail(sinkpad_peer_caps != NULL);
348
349   if (gst_autoplugger_autoplug(autoplugger,autoplugger->cache_srcpad,sinkpad_peer_caps,srcpad_peer_caps))
350     *result = TRUE;
351
352   // force renego
353   gst_pad_renegotiate(GST_PAD(GST_PAD_PEER(autoplugger->cache_sinkpad)));
354
355   autoplugger->paused--;
356   if (autoplugger->paused == 0)
357     // try to PLAY the whole thing
358     gst_element_set_state(GST_ELEMENT_SCHED(autoplugger)->parent,GST_STATE_PLAYING);
359
360   GST_INFO(GST_CAT_AUTOPLUG, "done dealing with caps nego failure on sinkpad %s:%s",GST_DEBUG_PAD_NAME(pad));
361 }
362
363 static void
364 gst_autoplugger_external_src_caps_nego_failed(GstPad *pad, gboolean *result, GstAutoplugger *autoplugger)
365 {
366   GstCaps *srcpad_caps;
367   GstPad *srcpad_peer;
368   GstPadTemplate *srcpad_peer_template;
369   GstCaps *srcpad_peer_caps;
370
371   GST_INFO(GST_CAT_AUTOPLUG, "have caps nego failure on srcpad %s:%s!!!",GST_DEBUG_PAD_NAME(pad));
372
373   autoplugger->paused++;
374   if (autoplugger->paused == 1)
375     // try to PAUSE the whole thing
376     gst_element_set_state(GST_ELEMENT_SCHED(autoplugger)->parent,GST_STATE_PAUSED);
377
378   srcpad_caps = GST_PAD_CAPS(autoplugger->cache_srcpad);
379
380   srcpad_peer = GST_PAD(GST_PAD_PEER(autoplugger->cache_srcpad));
381   g_return_if_fail(srcpad_peer != NULL);
382   srcpad_peer_template = GST_PAD_PADTEMPLATE(srcpad_peer);
383   g_return_if_fail(srcpad_peer_template != NULL);
384   srcpad_peer_caps = GST_PADTEMPLATE_CAPS(srcpad_peer_template);
385   g_return_if_fail(srcpad_peer_caps != NULL);
386
387   if (gst_autoplugger_autoplug(autoplugger,autoplugger->cache_srcpad,srcpad_caps,srcpad_peer_caps))
388     *result = TRUE;
389
390   autoplugger->paused--;
391   if (autoplugger->paused == 0)
392     // try to PLAY the whole thing
393     gst_element_set_state(GST_ELEMENT_SCHED(autoplugger)->parent,GST_STATE_PLAYING);
394
395   autoplugger->disable_nocaps = TRUE;
396
397   GST_INFO(GST_CAT_AUTOPLUG, "done dealing with caps nego failure on srcpad %s:%s",GST_DEBUG_PAD_NAME(pad));
398 }
399
400
401 static void
402 gst_autoplugger_cache_empty(GstElement *element, GstAutoplugger *autoplugger)
403 {
404   GstPad *cache_sinkpad_peer,*cache_srcpad_peer;
405
406   GST_INFO(GST_CAT_AUTOPLUG, "autoplugger cache has hit empty, we can now remove it");
407
408   autoplugger->paused++;
409   if (autoplugger->paused == 1)
410     // try to PAUSE the whole thing
411     gst_element_set_state(GST_ELEMENT_SCHED(autoplugger)->parent,GST_STATE_PAUSED);
412
413   // disconnect the cache from its peers
414   GST_DEBUG(GST_CAT_AUTOPLUG, "disconnecting autoplugcache from its peers\n");
415   cache_sinkpad_peer = GST_PAD (GST_PAD_PEER(autoplugger->cache_sinkpad));
416   cache_srcpad_peer = GST_PAD (GST_PAD_PEER(autoplugger->cache_srcpad));
417   gst_pad_disconnect(cache_sinkpad_peer,autoplugger->cache_sinkpad);
418   gst_pad_disconnect(autoplugger->cache_srcpad,cache_srcpad_peer);
419
420   // remove the cache from self
421   GST_DEBUG(GST_CAT_AUTOPLUG, "removing the cache from the autoplugger\n");
422   gst_bin_remove (GST_BIN(autoplugger), autoplugger->cache);
423
424   // connect the two pads
425   GST_DEBUG(GST_CAT_AUTOPLUG, "reconnecting the autoplugcache's former peers\n");
426   gst_pad_connect(cache_sinkpad_peer,cache_srcpad_peer);
427
428   autoplugger->paused--;
429   if (autoplugger->paused == 0)
430     // try to PLAY the whole thing
431     gst_element_set_state(GST_ELEMENT_SCHED(autoplugger)->parent,GST_STATE_PLAYING);
432
433 //  xmlSaveFile("autoplugger.gst", gst_xml_write(GST_ELEMENT_SCHED(autoplugger)->parent));
434
435   GST_INFO(GST_CAT_AUTOPLUG, "autoplugger_cache_empty finished");
436 }
437
438 static void
439 gst_autoplugger_typefind_have_type(GstElement *element, GstCaps *caps, GstAutoplugger *autoplugger) 
440 {
441   GST_INFO(GST_CAT_AUTOPLUG, "typefind claims to have a type: %s",gst_caps_get_mime(caps));
442
443 gst_scheduler_show(GST_ELEMENT_SCHED(autoplugger));
444
445   autoplugger->paused++;
446   if (autoplugger->paused == 1)
447     // try to PAUSE the whole thing
448     gst_element_set_state(GST_ELEMENT_SCHED(autoplugger)->parent,GST_STATE_PAUSED);
449
450   // first disconnect the typefind and shut it down
451   GST_DEBUG(GST_CAT_AUTOPLUG, "disconnecting typefind from the cache\n");
452   gst_pad_disconnect(autoplugger->cache_srcpad,autoplugger->typefind_sinkpad);
453   gst_bin_remove(GST_BIN(autoplugger),autoplugger->typefind);
454
455   // FIXME FIXME now we'd compare caps and see if we need to autoplug something in the middle, but for 
456   // now we're going to just reconnect where we left off
457   // FIXME FIXME FIXME!!!: this should really be done in the caps failure!!!
458 /*
459   if (!autoplugger->autoplug) {
460     autoplugger->autoplug = gst_autoplugfactory_make("static");
461   }
462   autoplugger->autobin = gst_autoplug_to_caps(autoplugger->autoplug,
463       caps,autoplugger->sinktemplatecaps,NULL);
464   g_return_if_fail(autoplugger->autobin != NULL);
465   gst_bin_add(GST_BIN(autoplugger),autoplugger->autobin);
466
467 //  // re-attach the srcpad's original peer to the cache
468 //  GST_DEBUG(GST_CAT_AUTOPLUG, "reconnecting the cache to the downstream peer\n");
469 //  gst_pad_connect(autoplugger->cache_srcpad,autoplugger->srcpadpeer);
470
471   // attach the autoplugged bin
472   GST_DEBUG(GST_CAT_AUTOPLUG, "attaching the autoplugged bin between cache and downstream peer\n");
473   gst_pad_connect(autoplugger->cache_srcpad,gst_element_get_pad(autoplugger->autobin,"sink"));
474   gst_pad_connect(gst_element_get_pad(autoplugger->autobin,"src_00"),autoplugger->srcpadpeer);
475 */
476
477   // FIXME set the caps on the new connection
478 //  GST_DEBUG(GST_CAT_AUTOPLUG,"forcing caps on the typefound pad\n");
479 //  gst_pad_set_caps(autoplugger->cache_srcpad,caps);
480
481   // reattach the original outside srcpad
482   GST_DEBUG(GST_CAT_AUTOPLUG,"re-attaching downstream peer to autoplugcache\n");
483   gst_pad_connect(autoplugger->cache_srcpad,autoplugger->srcpadpeer);
484
485   // now reset the autoplugcache
486   GST_DEBUG(GST_CAT_AUTOPLUG, "resetting the cache to send first buffer(s) again\n");
487   g_object_set(G_OBJECT(autoplugger->cache),"reset",TRUE,NULL);
488
489   // attach the cache_empty handler
490   // FIXME this is the wrong place, it shouldn't be done until we get successful caps nego!
491   g_signal_connect (G_OBJECT(autoplugger->cache),"cache_empty",
492                      G_CALLBACK (gst_autoplugger_cache_empty), autoplugger);
493
494   autoplugger->paused--;
495   if (autoplugger->paused == 0)
496     // try to PLAY the whole thing
497     gst_element_set_state(GST_ELEMENT_SCHED(autoplugger)->parent,GST_STATE_PLAYING);
498
499   GST_INFO(GST_CAT_AUTOPLUG, "typefind_have_type finished");
500 gst_scheduler_show(GST_ELEMENT_SCHED(autoplugger));
501 }
502
503 static void
504 gst_autoplugger_cache_first_buffer(GstElement *element,GstBuffer *buf,GstAutoplugger *autoplugger)
505 {
506   GST_INFO(GST_CAT_AUTOPLUG, "have first buffer through cache");
507   autoplugger->cache_first_buffer = TRUE;
508
509   // if there are no established caps, worry
510   if (!autoplugger->sinkcaps) {
511     GST_INFO(GST_CAT_AUTOPLUG, "have no caps for the buffer, Danger Will Robinson!");
512
513 if (autoplugger->disable_nocaps) {
514   GST_DEBUG(GST_CAT_AUTOPLUG, "not dealing with lack of caps this time\n");
515   return;
516 }
517
518 gst_scheduler_show(GST_ELEMENT_SCHED(autoplugger));
519
520   autoplugger->paused++;
521   if (autoplugger->paused == 1)
522     // try to PAUSE the whole thing
523     gst_element_set_state(GST_ELEMENT_SCHED(autoplugger)->parent,GST_STATE_PAUSED);
524
525     // detach the srcpad
526     GST_DEBUG(GST_CAT_AUTOPLUG, "disconnecting cache from its downstream peer\n");
527     autoplugger->srcpadpeer = GST_PAD(GST_PAD_PEER(autoplugger->cache_srcpad));
528     gst_pad_disconnect(autoplugger->cache_srcpad,autoplugger->srcpadpeer);
529
530     // instantiate the typefind and set up the signal handlers
531     if (!autoplugger->typefind) {
532       GST_DEBUG(GST_CAT_AUTOPLUG, "creating typefind and setting signal handler\n");
533       autoplugger->typefind = gst_elementfactory_make("typefind","unnamed_typefind");
534       autoplugger->typefind_sinkpad = gst_element_get_pad(autoplugger->typefind,"sink");
535       g_signal_connect (G_OBJECT(autoplugger->typefind),"have_type",
536                          G_CALLBACK (gst_autoplugger_typefind_have_type), autoplugger);
537     }
538     // add it to self and attach it
539     GST_DEBUG(GST_CAT_AUTOPLUG, "adding typefind to self and connecting to cache\n");
540     gst_bin_add(GST_BIN(autoplugger),autoplugger->typefind);
541     gst_pad_connect(autoplugger->cache_srcpad,autoplugger->typefind_sinkpad);
542
543     // bring the typefind into playing state
544     GST_DEBUG(GST_CAT_AUTOPLUG, "setting typefind state to PLAYING\n");
545     gst_element_set_state(autoplugger->cache,GST_STATE_PLAYING);
546
547   autoplugger->paused--;
548   if (autoplugger->paused == 0)
549     // try to PLAY the whole thing
550     gst_element_set_state(GST_ELEMENT_SCHED(autoplugger)->parent,GST_STATE_PLAYING);
551
552     GST_INFO(GST_CAT_AUTOPLUG,"here we go into nothingness, hoping the typefind will return us to safety");
553 gst_scheduler_show(GST_ELEMENT_SCHED(autoplugger));
554   } else {
555 //    // attach the cache_empty handler, since the cache simply isn't needed
556 //    g_signal_connect (G_OBJECT(autoplugger->cache),"cache_empty",
557 //                       gst_autoplugger_cache_empty,autoplugger);
558   }
559 }
560
561 static void
562 gst_autoplugger_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
563 {
564   GstAutoplugger *autoplugger;
565
566   autoplugger = GST_AUTOPLUGGER (object);
567
568   switch (prop_id) {
569     default:
570       break;
571   }
572 }
573
574 static void
575 gst_autoplugger_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
576 {
577   GstAutoplugger *autoplugger;
578
579   autoplugger = GST_AUTOPLUGGER (object);
580
581   switch (prop_id) {
582     default:
583       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
584       break;
585   }
586 }
587
588 static gboolean
589 plugin_init (GModule *module, GstPlugin *plugin)
590 {
591   GstElementFactory *factory;
592
593   factory = gst_elementfactory_new ("autoplugger", GST_TYPE_AUTOPLUGGER,
594                                     &gst_autoplugger_details);
595   g_return_val_if_fail (factory != NULL, FALSE);
596
597   gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
598
599   return TRUE;
600 }
601
602 GstPluginDesc plugin_desc = {
603   GST_VERSION_MAJOR,
604   GST_VERSION_MINOR,
605   "autoplugger",
606   plugin_init
607 };
608