6e057f6cd492e18b17fd51d92b3c1556e5f19df8
[platform/upstream/gst-plugins-good.git] / ext / jack / gstjack.c
1 /* -*- Mode: C; c-basic-offset: 4 -*- */
2 /*
3     Copyright (C) 2002 Andy Wingo <wingo@pobox.com>
4
5     This library is free software; you can redistribute it and/or
6     modify it under the terms of the GNU General Public
7     License as published by the Free Software Foundation; either
8     version 2 of the License, or (at your option) any later version.
9
10     This library is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13     General Public License for more details.
14
15     You should have received a copy of the GNU General Public
16     License along with this library; if not, write to the Free
17     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 #include <stdlib.h>
24 #include <string.h>
25 #include "gstjack.h"
26 #include <gst/audio/audio.h>
27
28 /* TODO:
29
30    this element is still nonfunctional
31
32    - work out the src side (caps setting, etc)
33
34    future core TODO:
35    - make a jack clock provider
36    - add GST_ELEMENT_FIXED_DATA_RATE, GST_ELEMENT_QOS,
37      GST_ELEMENT_CHANGES_DATA_RATE element flags, and make the scheduler
38      sensitive to them
39 */
40
41 /* elementfactory information */
42 static GstElementDetails gst_jack_bin_details = {
43     "Jack Bin",
44     "Generic/Bin",
45     "GPL",
46     "Jack processing bin: see README for more info",
47     VERSION,
48     "Andy Wingo <wingo@pobox.com>",
49     "(C) 2002 "
50 };
51
52 static GstElementDetails gst_jack_sink_details = {  
53     "Jack Sink",
54     "Sink/Audio",
55     "GPL",
56     "Output to a Jack processing network: see README for more info",
57     VERSION,
58     "Andy Wingo <wingo@pobox.com>",
59     "(C) 2002 "
60 };
61
62 static GstElementDetails gst_jack_src_details = {  
63     "Jack Src",
64     "Source/Audio",
65     "GPL",
66     "Input from a Jack processing network: see README for more info",
67     VERSION,
68     "Andy Wingo <wingo@pobox.com>",
69     "(C) 2002",
70 };
71
72
73 static GHashTable *port_name_counts = NULL;
74 static GstElementClass *parent_class = NULL;
75
76 static void gst_jack_init(GstJack *this);
77 static void gst_jack_class_init(GstJackClass *klass);
78
79 static GstPadTemplate *gst_jack_src_request_pad_factory();
80 static GstPadTemplate *gst_jack_sink_request_pad_factory();
81
82 static GstPad* gst_jack_request_new_pad (GstElement *element, GstPadTemplate *templ, const
83                                          gchar *name);
84
85 static void gst_jack_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
86 static void gst_jack_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
87 static GstElementStateReturn gst_jack_change_state(GstElement *element);
88 static GstPadLinkReturn gst_jack_connect (GstPad *pad, GstCaps *caps);
89
90 static void gst_jack_loop (GstElement *element);
91
92
93 enum {
94     ARG_0,
95     ARG_PORT_NAME_PREFIX,
96 };
97
98
99 GType
100 gst_jack_get_type (void) 
101 {
102   static GType jack_type = 0;
103
104   if (!jack_type) {
105     static const GTypeInfo jack_info = {
106       sizeof(GstJackClass),
107       NULL,
108       NULL,
109       NULL,
110       NULL,
111       NULL,
112       sizeof(GstJack),
113       0,
114       NULL,
115     };
116     jack_type = g_type_register_static (GST_TYPE_ELEMENT, "GstJack", &jack_info, 0);
117   }
118   return jack_type;
119 }
120
121 GType
122 gst_jack_sink_get_type (void) 
123 {
124     static GType jack_type = 0;
125
126     if (!jack_type) {
127         static const GTypeInfo jack_info = {
128             sizeof(GstJackClass),
129             NULL,
130             NULL,
131             (GClassInitFunc)gst_jack_class_init,
132             NULL,
133             NULL,
134             sizeof(GstJack),
135             0,
136             (GInstanceInitFunc)gst_jack_init,
137         };
138         jack_type = g_type_register_static (GST_TYPE_JACK, "GstJackSink", &jack_info, 0);
139     }
140     return jack_type;
141 }
142
143 GType
144 gst_jack_src_get_type (void) 
145 {
146     static GType jack_type = 0;
147     
148     if (!jack_type) {
149         static const GTypeInfo jack_info = {
150             sizeof(GstJackClass),
151             NULL,
152             NULL,
153             (GClassInitFunc)gst_jack_class_init,
154             NULL,
155             NULL,
156             sizeof(GstJack),
157             0,
158             (GInstanceInitFunc)gst_jack_init,
159         };
160         jack_type = g_type_register_static (GST_TYPE_JACK, "GstJackSrc", &jack_info, 0);
161     }
162     return jack_type;
163 }
164
165 static GstPadTemplate*
166 gst_jack_src_request_pad_factory(void)
167 {
168     static GstPadTemplate *template = NULL;
169     
170     if (!template) {
171         GstCaps *caps;
172         caps = gst_caps_new("src",
173                             "audio/x-raw-float",
174                             GST_AUDIO_FLOAT_STANDARD_PAD_TEMPLATE_PROPS);
175         template = gst_pad_template_new("%s", GST_PAD_SRC,
176                                         GST_PAD_REQUEST, caps, NULL);
177     }
178     
179     return template;
180 }
181
182 static GstPadTemplate*
183 gst_jack_sink_request_pad_factory(void)
184 {
185     static GstPadTemplate *template = NULL;
186     
187     if (!template) {
188         GstCaps *caps;
189         caps = gst_caps_new ("sink",
190                              "audio/x-raw-float",
191                              GST_AUDIO_FLOAT_STANDARD_PAD_TEMPLATE_PROPS);
192         template = gst_pad_template_new("%s", GST_PAD_SINK,
193                                         GST_PAD_REQUEST, caps, NULL);
194     }
195     
196     return template;
197 }
198
199 static void
200 gst_jack_class_init(GstJackClass *klass)
201 {
202     GObjectClass *object_class;
203     GstElementClass *element_class;
204     gchar *prefix;
205     
206     object_class = (GObjectClass *)klass;
207     element_class = (GstElementClass *)klass;
208     
209     if (parent_class == NULL)
210         parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
211     
212     object_class->get_property = gst_jack_get_property;
213     object_class->set_property = gst_jack_set_property;
214     
215     if (GST_IS_JACK_SINK_CLASS (klass))
216         prefix = "gst-out-";
217     else
218         prefix = "gst-in-";
219     
220     g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_PORT_NAME_PREFIX,
221                                     g_param_spec_string("port-name-prefix","Port name prefix",
222                                                         "String to prepend to jack port names",
223                                                         prefix,
224                                                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
225
226     element_class->change_state = gst_jack_change_state;
227     
228     element_class->request_new_pad = gst_jack_request_new_pad;
229 }
230
231 static void
232 gst_jack_init(GstJack *this)
233 {
234     if (G_OBJECT_TYPE (this) == GST_TYPE_JACK_SRC) {
235         this->direction = GST_PAD_SRC;
236     } else if (G_OBJECT_TYPE (this) == GST_TYPE_JACK_SINK) {
237         this->direction = GST_PAD_SINK;
238     } else {
239         g_assert_not_reached ();
240     }
241     
242     gst_element_set_loop_function(GST_ELEMENT(this), gst_jack_loop);
243 }
244
245 static GstPad*
246 gst_jack_request_new_pad (GstElement *element, GstPadTemplate *templ, const gchar *name) 
247 {
248     GstJack *this;
249     gchar *newname;
250     GList *l, **pad_list;
251     GstJackPad *pad;
252     gint count;
253     
254     g_return_val_if_fail ((this = GST_JACK (element)), NULL);
255     
256     if (!this->bin)
257         pad_list = &this->pads;
258     else if (this->direction == GST_PAD_SRC)
259         pad_list = &this->bin->src_pads;
260     else
261         pad_list = &this->bin->sink_pads;
262     
263     if (name) {
264         l = *pad_list;
265         while (l) {
266             if (strcmp (GST_JACK_PAD(l)->name, name) == 0) {
267                 g_warning("requested port name %s already in use.", name);
268                 return NULL;
269             }
270             l = l->next;
271         }
272         newname = g_strdup (name);
273     } else {
274         if (this->direction == GST_PAD_SINK)
275             newname = g_strdup ("alsa_pcm:playback_1");
276         else
277             newname = g_strdup ("alsa_pcm:capture_1");
278     }
279     
280     pad = g_new0(GstJackPad, 1);
281     
282     if (!port_name_counts)
283         port_name_counts = g_hash_table_new (g_str_hash, g_str_equal);
284
285     count = GPOINTER_TO_INT (g_hash_table_lookup (port_name_counts, this->port_name_prefix));
286     g_hash_table_insert (port_name_counts, g_strdup (this->port_name_prefix), GINT_TO_POINTER (count+1));
287
288     pad->name = g_strdup_printf ("%s%d", this->port_name_prefix, count);
289
290     pad->peer_name = newname;
291     pad->pad = gst_pad_new_from_template (templ, newname);
292     gst_element_add_pad (GST_ELEMENT (this), pad->pad);
293     gst_pad_set_link_function (pad->pad, gst_jack_connect);
294     
295     this->pads = g_list_append (this->pads, pad);
296     
297     g_print ("returning from request_new_pad, pad %s created, to connect to %s\n", pad->name, pad->peer_name);
298     return pad->pad;
299 }
300
301 static void
302 gst_jack_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
303 {
304     GstJack *this;
305     
306     this = (GstJack *)object;
307     switch (prop_id) {
308     case ARG_PORT_NAME_PREFIX:
309         if (this->port_name_prefix)
310             g_free (this->port_name_prefix);
311         this->port_name_prefix = g_strdup (g_value_get_string (value));
312         break;
313     default:
314         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
315         return;
316     }
317 }
318
319 static void
320 gst_jack_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
321 {
322     GstJack *this;
323
324     this = (GstJack *)object;
325
326     switch (prop_id) {
327     case ARG_PORT_NAME_PREFIX:
328         g_value_set_string (value, this->port_name_prefix);
329         break;
330     default:
331         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
332         break;
333     }
334 }
335
336 static GstElementStateReturn
337 gst_jack_change_state (GstElement *element)
338 {
339     GstJack *this;
340     GList *l = NULL, **pads;
341     GstJackPad *pad;
342     GstCaps *caps;
343     
344     g_return_val_if_fail (element != NULL, FALSE);
345     this = GST_JACK (element);
346     
347     switch (GST_STATE_PENDING (element)) {
348     case GST_STATE_NULL:
349         JACK_DEBUG ("%s: NULL", GST_OBJECT_NAME (GST_OBJECT (this)));
350
351         break;
352         
353     case GST_STATE_READY:
354         JACK_DEBUG ("%s: READY", GST_OBJECT_NAME (GST_OBJECT (this)));
355
356         if (!this->bin) {
357             if (!(this->bin = (GstJackBin*)gst_element_get_managing_bin (element))
358                 || !GST_IS_JACK_BIN (this->bin)) {
359                 this->bin = NULL;
360                 g_warning ("jack element %s needs to be contained in a jack bin.",
361                            GST_OBJECT_NAME (GST_OBJECT (element)));
362                 return GST_STATE_FAILURE;
363             }
364
365             /* fixme: verify that all names are unique */
366             l = this->pads;
367             pads = (this->direction == GST_PAD_SRC) ? &this->bin->src_pads : &this->bin->sink_pads;
368             while (l) {
369                 pad = GST_JACK_PAD (l);
370                 JACK_DEBUG ("%s: appending pad %s:%s to list", GST_OBJECT_NAME (this), pad->name, pad->peer_name);
371                 *pads = g_list_append (*pads, pad);
372                 l = g_list_next (l);
373             }
374         }
375         break;
376         
377     case GST_STATE_PAUSED:
378         JACK_DEBUG ("%s: PAUSED", GST_OBJECT_NAME (GST_OBJECT (this)));
379
380         if (GST_STATE (element) == GST_STATE_READY) {
381             /* we're in READY->PAUSED */
382             l = this->pads;
383             while (l) {
384                 pad = GST_JACK_PAD (l);
385                 caps = gst_pad_get_caps (pad->pad);
386                 gst_caps_set (caps, "rate", GST_PROPS_INT_TYPE, (gint) this->bin->rate, NULL);
387                 if (gst_pad_try_set_caps (pad->pad, caps) <= 0)
388                     return GST_STATE_FAILURE;
389                 l = g_list_next (l);
390             }
391         }
392         break;
393     case GST_STATE_PLAYING:
394         JACK_DEBUG ("%s: PLAYING", GST_OBJECT_NAME (GST_OBJECT (this)));
395         break;
396     }
397     
398     JACK_DEBUG ("%s: state change finished", GST_OBJECT_NAME (this));
399     
400     if (GST_ELEMENT_CLASS (parent_class)->change_state)
401         return GST_ELEMENT_CLASS (parent_class)->change_state (element);
402
403     return GST_STATE_SUCCESS;
404 }
405
406 static GstPadLinkReturn
407 gst_jack_connect (GstPad *pad, GstCaps *caps)
408 {
409   GstJack *this;
410   gint rate;
411   
412   this = GST_JACK (gst_pad_get_parent (pad));
413   g_return_val_if_fail (this != NULL, GST_PAD_LINK_REFUSED);
414   g_return_val_if_fail (GST_IS_JACK (this), GST_PAD_LINK_REFUSED);
415   
416   if (GST_CAPS_IS_FIXED (caps)) {
417       gst_caps_get_int (caps, "rate", &rate);
418       if (this->bin && rate != this->bin->rate)
419           return GST_PAD_LINK_REFUSED;
420       
421       return GST_PAD_LINK_OK;
422   }
423   
424   return GST_PAD_LINK_DELAYED;
425 }
426
427 static void
428 gst_jack_loop (GstElement *element)
429 {
430     GstJack *this;
431     GList *pads;
432     gint len, peeked_len;
433     guint8 *peeked;
434     gint avail;
435     GstEvent *event;
436     GstJackPad *pad;
437     GstBuffer *buffer;
438     
439     this = GST_JACK (element);
440     
441     g_return_if_fail(this != NULL);
442     len = this->bin->nframes * sizeof (jack_default_audio_sample_t);
443     
444     do {
445         pads = this->pads;
446         while (pads) {
447             pad = GST_JACK_PAD (pads);
448             
449             if (this->direction == GST_PAD_SINK) {
450                 if (!pad->bs)
451                     pad->bs = gst_bytestream_new (pad->pad);
452                 
453             read:
454                 peeked_len = gst_bytestream_peek_bytes (pad->bs, &peeked, len);
455                 if (peeked_len < len) {
456                     gst_bytestream_get_status(pad->bs, &avail, &event);
457                     if (event) {
458                         g_warning("got an event on jacksink");
459                         if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
460                             /* really, we should just cut this pad out of the graph. let
461                              * me know when this is needed ;)
462                              * also, for sample accuracy etc, we should play avail
463                              * bytes, but hey. */
464                             gst_element_set_eos (element);
465                             gst_event_unref (event);
466 //                            gst_element_yield (element); /* shouldn't return */
467                             return;
468                         }
469                         goto read;
470                     } else {
471                         /* the element at the top of the chain did not emit an eos
472                          * event. this is a Bug(tm) */
473                         g_assert_not_reached();
474                     }
475                 }
476                 
477                 
478                 memcpy (pad->data, peeked, peeked_len);
479                 gst_bytestream_flush (pad->bs, peeked_len);
480             } else {
481                 buffer = gst_buffer_new ();
482                 GST_BUFFER_DATA (buffer)    = pad->data;
483                 GST_BUFFER_SIZE (buffer)    = len;
484                 GST_BUFFER_MAXSIZE (buffer) = len;
485                 GST_BUFFER_FLAG_SET(buffer, GST_BUFFER_DONTFREE);
486                 
487                 gst_pad_push (pad->pad, buffer);
488             }
489             pads = g_list_next (pads);
490         }
491         
492         return;
493 //      gst_element_yield (element);
494     } while (TRUE);
495 }
496
497 static gboolean
498 plugin_init (GModule *module, GstPlugin *plugin)
499 {
500     GstElementFactory *factory;
501     
502     if (!gst_library_load ("gstbytestream"))
503         return FALSE;
504     
505     factory = gst_element_factory_new ("jackbin", GST_TYPE_JACK_BIN, &gst_jack_bin_details);
506     g_return_val_if_fail (factory != NULL, FALSE);
507     gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
508     
509     factory = gst_element_factory_new ("jacksrc", GST_TYPE_JACK_SRC, &gst_jack_src_details);
510     g_return_val_if_fail (factory != NULL, FALSE);
511     gst_element_factory_add_pad_template (factory, gst_jack_src_request_pad_factory());
512     gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
513     
514     factory = gst_element_factory_new ("jacksink", GST_TYPE_JACK_SINK, &gst_jack_sink_details);
515     g_return_val_if_fail (factory != NULL, FALSE);
516     gst_element_factory_add_pad_template (factory, gst_jack_sink_request_pad_factory());
517     gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
518     
519     gst_plugin_set_longname(plugin, "JACK plugin library");
520     
521     return TRUE;
522 }
523
524 GstPluginDesc plugin_desc = {
525     GST_VERSION_MAJOR,
526     GST_VERSION_MINOR,
527     "jack",
528     plugin_init
529 };
530
531