1 /* -*- Mode: C; c-basic-offset: 4 -*- */
3 Copyright (C) 2002 Andy Wingo <wingo@pobox.com>
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.
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.
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.
26 #include <gst/audio/audio.h>
30 this element is still nonfunctional
32 - work out the src side (caps setting, etc)
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
41 /* elementfactory information */
42 static GstElementDetails gst_jack_bin_details = {
46 "Jack processing bin: see README for more info",
48 "Andy Wingo <wingo@pobox.com>",
52 static GstElementDetails gst_jack_sink_details = {
56 "Output to a Jack processing network: see README for more info",
58 "Andy Wingo <wingo@pobox.com>",
62 static GstElementDetails gst_jack_src_details = {
66 "Input from a Jack processing network: see README for more info",
68 "Andy Wingo <wingo@pobox.com>",
73 static GHashTable *port_name_counts = NULL;
74 static GstElementClass *parent_class = NULL;
76 static void gst_jack_init(GstJack *this);
77 static void gst_jack_class_init(GstJackClass *klass);
79 static GstPadTemplate *gst_jack_src_request_pad_factory();
80 static GstPadTemplate *gst_jack_sink_request_pad_factory();
82 static GstPad* gst_jack_request_new_pad (GstElement *element, GstPadTemplate *templ, const
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);
90 static void gst_jack_loop (GstElement *element);
100 gst_jack_get_type (void)
102 static GType jack_type = 0;
105 static const GTypeInfo jack_info = {
106 sizeof(GstJackClass),
116 jack_type = g_type_register_static (GST_TYPE_ELEMENT, "GstJack", &jack_info, 0);
122 gst_jack_sink_get_type (void)
124 static GType jack_type = 0;
127 static const GTypeInfo jack_info = {
128 sizeof(GstJackClass),
131 (GClassInitFunc)gst_jack_class_init,
136 (GInstanceInitFunc)gst_jack_init,
138 jack_type = g_type_register_static (GST_TYPE_JACK, "GstJackSink", &jack_info, 0);
144 gst_jack_src_get_type (void)
146 static GType jack_type = 0;
149 static const GTypeInfo jack_info = {
150 sizeof(GstJackClass),
153 (GClassInitFunc)gst_jack_class_init,
158 (GInstanceInitFunc)gst_jack_init,
160 jack_type = g_type_register_static (GST_TYPE_JACK, "GstJackSrc", &jack_info, 0);
165 static GstPadTemplate*
166 gst_jack_src_request_pad_factory(void)
168 static GstPadTemplate *template = NULL;
172 caps = gst_caps_new("src",
174 GST_AUDIO_FLOAT_STANDARD_PAD_TEMPLATE_PROPS);
175 template = gst_pad_template_new("%s", GST_PAD_SRC,
176 GST_PAD_REQUEST, caps, NULL);
182 static GstPadTemplate*
183 gst_jack_sink_request_pad_factory(void)
185 static GstPadTemplate *template = NULL;
189 caps = gst_caps_new ("sink",
191 GST_AUDIO_FLOAT_STANDARD_PAD_TEMPLATE_PROPS);
192 template = gst_pad_template_new("%s", GST_PAD_SINK,
193 GST_PAD_REQUEST, caps, NULL);
200 gst_jack_class_init(GstJackClass *klass)
202 GObjectClass *object_class;
203 GstElementClass *element_class;
206 object_class = (GObjectClass *)klass;
207 element_class = (GstElementClass *)klass;
209 if (parent_class == NULL)
210 parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
212 object_class->get_property = gst_jack_get_property;
213 object_class->set_property = gst_jack_set_property;
215 if (GST_IS_JACK_SINK_CLASS (klass))
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",
224 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
226 element_class->change_state = gst_jack_change_state;
228 element_class->request_new_pad = gst_jack_request_new_pad;
232 gst_jack_init(GstJack *this)
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;
239 g_assert_not_reached ();
242 gst_element_set_loop_function(GST_ELEMENT(this), gst_jack_loop);
246 gst_jack_request_new_pad (GstElement *element, GstPadTemplate *templ, const gchar *name)
250 GList *l, **pad_list;
254 g_return_val_if_fail ((this = GST_JACK (element)), NULL);
257 pad_list = &this->pads;
258 else if (this->direction == GST_PAD_SRC)
259 pad_list = &this->bin->src_pads;
261 pad_list = &this->bin->sink_pads;
266 if (strcmp (GST_JACK_PAD(l)->name, name) == 0) {
267 g_warning("requested port name %s already in use.", name);
272 newname = g_strdup (name);
274 if (this->direction == GST_PAD_SINK)
275 newname = g_strdup ("alsa_pcm:playback_1");
277 newname = g_strdup ("alsa_pcm:capture_1");
280 pad = g_new0(GstJackPad, 1);
282 if (!port_name_counts)
283 port_name_counts = g_hash_table_new (g_str_hash, g_str_equal);
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));
288 pad->name = g_strdup_printf ("%s%d", this->port_name_prefix, count);
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);
295 this->pads = g_list_append (this->pads, pad);
297 g_print ("returning from request_new_pad, pad %s created, to connect to %s\n", pad->name, pad->peer_name);
302 gst_jack_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
306 this = (GstJack *)object;
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));
314 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
320 gst_jack_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
324 this = (GstJack *)object;
327 case ARG_PORT_NAME_PREFIX:
328 g_value_set_string (value, this->port_name_prefix);
331 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
336 static GstElementStateReturn
337 gst_jack_change_state (GstElement *element)
340 GList *l = NULL, **pads;
344 g_return_val_if_fail (element != NULL, FALSE);
345 this = GST_JACK (element);
347 switch (GST_STATE_PENDING (element)) {
349 JACK_DEBUG ("%s: NULL", GST_OBJECT_NAME (GST_OBJECT (this)));
353 case GST_STATE_READY:
354 JACK_DEBUG ("%s: READY", GST_OBJECT_NAME (GST_OBJECT (this)));
357 if (!(this->bin = (GstJackBin*)gst_element_get_managing_bin (element))
358 || !GST_IS_JACK_BIN (this->bin)) {
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;
365 /* fixme: verify that all names are unique */
367 pads = (this->direction == GST_PAD_SRC) ? &this->bin->src_pads : &this->bin->sink_pads;
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);
377 case GST_STATE_PAUSED:
378 JACK_DEBUG ("%s: PAUSED", GST_OBJECT_NAME (GST_OBJECT (this)));
380 if (GST_STATE (element) == GST_STATE_READY) {
381 /* we're in READY->PAUSED */
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;
393 case GST_STATE_PLAYING:
394 JACK_DEBUG ("%s: PLAYING", GST_OBJECT_NAME (GST_OBJECT (this)));
398 JACK_DEBUG ("%s: state change finished", GST_OBJECT_NAME (this));
400 if (GST_ELEMENT_CLASS (parent_class)->change_state)
401 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
403 return GST_STATE_SUCCESS;
406 static GstPadLinkReturn
407 gst_jack_connect (GstPad *pad, GstCaps *caps)
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);
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;
421 return GST_PAD_LINK_OK;
424 return GST_PAD_LINK_DELAYED;
428 gst_jack_loop (GstElement *element)
432 gint len, peeked_len;
439 this = GST_JACK (element);
441 g_return_if_fail(this != NULL);
442 len = this->bin->nframes * sizeof (jack_default_audio_sample_t);
447 pad = GST_JACK_PAD (pads);
449 if (this->direction == GST_PAD_SINK) {
451 pad->bs = gst_bytestream_new (pad->pad);
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);
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
464 gst_element_set_eos (element);
465 gst_event_unref (event);
466 // gst_element_yield (element); /* shouldn't return */
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();
478 memcpy (pad->data, peeked, peeked_len);
479 gst_bytestream_flush (pad->bs, peeked_len);
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);
487 gst_pad_push (pad->pad, buffer);
489 pads = g_list_next (pads);
493 // gst_element_yield (element);
498 plugin_init (GModule *module, GstPlugin *plugin)
500 GstElementFactory *factory;
502 if (!gst_library_load ("gstbytestream"))
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));
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));
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));
519 gst_plugin_set_longname(plugin, "JACK plugin library");
524 GstPluginDesc plugin_desc = {