summaryrefslogtreecommitdiff
path: root/ext/jack/gstjack.c
blob: 4cf4df5877794f38d942f66e02f7533507cc591f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
/* -*- Mode: C; c-basic-offset: 4 -*- */
/*
    Copyright (C) 2002 Andy Wingo <wingo@pobox.com>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    General Public License for more details.

    You should have received a copy of the GNU General Public
    License along with this library; if not, write to the Free
    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include <stdlib.h>
#include <string.h>
#include "gstjack.h"
#include <gst/audio/audio.h>

/* TODO:
   
   this element is still nonfunctional

   - work out the src side (caps setting, etc)

   future core TODO:
   - make a jack clock provider
   - add GST_ELEMENT_FIXED_DATA_RATE, GST_ELEMENT_QOS,
     GST_ELEMENT_CHANGES_DATA_RATE element flags, and make the scheduler
     sensitive to them
*/


static GstElementDetails gst_jack_bin_details = {  
    "Jack Bin",
    "Generic/Bin",
    "Jack processing bin: see README for more info",
    VERSION,
    "Andy Wingo <wingo@pobox.com>",
    "(C) 2002 "
};

static GstElementDetails gst_jack_sink_details = {  
    "Jack Sink",
    "Sink/Audio",
    "Output to a Jack processing network: see README for more info",
    VERSION,
    "Andy Wingo <wingo@pobox.com>",
    "(C) 2002 "
};

static GstElementDetails gst_jack_src_details = {  
    "Jack Src",
    "Source/Audio",
    "Input from a Jack processing network: see README for more info",
    VERSION,
    "Andy Wingo <wingo@pobox.com>",
    "(C) 2002",
};


static GstElementClass *parent_class = NULL;

static void gst_jack_init(GstJack *this);
static void gst_jack_class_init(GstJackClass *klass);

static GstPadTemplate *gst_jack_src_request_pad_factory();
static GstPadTemplate *gst_jack_sink_request_pad_factory();

static GstPad* gst_jack_request_new_pad (GstElement *element, GstPadTemplate *templ, const
                                         gchar *name);

static void gst_jack_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
static void gst_jack_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
static GstElementStateReturn gst_jack_change_state(GstElement *element);
static GstPadConnectReturn gst_jack_connect (GstPad *pad, GstCaps *caps);

static void gst_jack_loop (GstElement *element);


enum {
    ARG_0,
    ARG_PORT_NAME_PREFIX,
};


GType
gst_jack_get_type (void) 
{
  static GType jack_type = 0;

  if (!jack_type) {
    static const GTypeInfo jack_info = {
      sizeof(GstJackClass),
      NULL,
      NULL,
      NULL,
      NULL,
      NULL,
      sizeof(GstJack),
      0,
      NULL,
    };
    jack_type = g_type_register_static (GST_TYPE_ELEMENT, "GstJack", &jack_info, 0);
  }
  return jack_type;
}

GType
gst_jack_sink_get_type (void) 
{
    static GType jack_type = 0;

    if (!jack_type) {
        static const GTypeInfo jack_info = {
            sizeof(GstJackClass),
            NULL,
            NULL,
            (GClassInitFunc)gst_jack_class_init,
            NULL,
            NULL,
            sizeof(GstJack),
            0,
            (GInstanceInitFunc)gst_jack_init,
        };
        jack_type = g_type_register_static (GST_TYPE_JACK, "GstJackSink", &jack_info, 0);
    }
    return jack_type;
}

GType
gst_jack_src_get_type (void) 
{
    static GType jack_type = 0;
    
    if (!jack_type) {
        static const GTypeInfo jack_info = {
            sizeof(GstJackClass),
            NULL,
            NULL,
            (GClassInitFunc)gst_jack_class_init,
            NULL,
            NULL,
            sizeof(GstJack),
            0,
            (GInstanceInitFunc)gst_jack_init,
        };
        jack_type = g_type_register_static (GST_TYPE_JACK, "GstJackSrc", &jack_info, 0);
    }
    return jack_type;
}

static GstPadTemplate*
gst_jack_src_request_pad_factory(void)
{
    static GstPadTemplate *template = NULL;
    
    if (!template)
        template = gst_pad_template_new("%s", GST_PAD_SRC, GST_PAD_REQUEST, 
                                       gst_caps_new("src", "audio/raw",
                                                    GST_AUDIO_FLOAT_MONO_PAD_TEMPLATE_PROPS),
                                       NULL);
    
    return template;
}

static GstPadTemplate*
gst_jack_sink_request_pad_factory(void)
{
    static GstPadTemplate *template = NULL;
    
    if (!template)
        template = gst_pad_template_new("%s", GST_PAD_SINK, GST_PAD_REQUEST, 
                                       gst_caps_new("sink", "audio/raw",
                                                    GST_AUDIO_FLOAT_MONO_PAD_TEMPLATE_PROPS),
                                       NULL);
    
    return template;
}

static void
gst_jack_class_init(GstJackClass *klass)
{
    GObjectClass *object_class;
    GstElementClass *element_class;
    
    object_class = (GObjectClass *)klass;
    element_class = (GstElementClass *)klass;
    
    if (parent_class == NULL)
        parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
    
    object_class->get_property = gst_jack_get_property;
    object_class->set_property = gst_jack_set_property;
    
    g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_PORT_NAME_PREFIX,
                                    g_param_spec_string("port-name-prefix","Port name prefix",
                                                        "String to prepend to jack port names",
                                                        "gst-",
                                                        G_PARAM_READWRITE | G_PARAM_CONSTRUCT));

    element_class->change_state = gst_jack_change_state;
    
    element_class->request_new_pad = gst_jack_request_new_pad;
}

static void
gst_jack_init(GstJack *this)
{
    if (G_OBJECT_TYPE (this) == GST_TYPE_JACK_SRC) {
        this->direction = GST_PAD_SRC;
    } else if (G_OBJECT_TYPE (this) == GST_TYPE_JACK_SINK) {
        this->direction = GST_PAD_SINK;
    } else {
        g_assert_not_reached ();
    }
    
    gst_element_set_loop_function(GST_ELEMENT(this), gst_jack_loop);
}

static GstPad*
gst_jack_request_new_pad (GstElement *element, GstPadTemplate *templ, const gchar *name) 
{
    GstJack *this;
    gchar *newname;
    GList *l, **pad_list;
    GstJackPad *pad;
    
    g_return_val_if_fail ((this = GST_JACK (element)), NULL);
    
    if (!this->bin)
        pad_list = &this->pads;
    else if (this->direction == GST_PAD_SRC)
        pad_list = &this->bin->src_pads;
    else
        pad_list = &this->bin->src_pads;
    
    if (name) {
        l = *pad_list;
        while (l) {
            if (strcmp (GST_JACK_PAD(l)->name, name) == 0) {
                g_warning("requested port name %s already in use.", name);
                return NULL;
            }
            l = l->next;
        }
        newname = g_strdup (name);
    } else {
        newname = g_strdup ("alsa_pcm:out_1");
    }
    
    pad = g_new0(GstJackPad, 1);
    
    pad->name = g_strdup_printf ("%s%d", this->port_name_prefix, 1); /* fixme :) */
    pad->peer_name = newname;
    pad->pad = gst_pad_new_from_template (templ, newname);
    gst_element_add_pad (GST_ELEMENT (this), pad->pad);
    gst_pad_set_connect_function (pad->pad, gst_jack_connect);
    
    this->pads = g_list_append (this->pads, pad);
    
    g_print ("returning from request_new_pad, pad %s created, to connect to %s\n", pad->name, pad->peer_name);
    return pad->pad;
}

static void
gst_jack_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
{
    GstJack *this;
    
    this = (GstJack *)object;
    switch (prop_id) {
    case ARG_PORT_NAME_PREFIX:
        if (this->port_name_prefix)
            g_free (this->port_name_prefix);
        this->port_name_prefix = g_strdup (g_value_get_string (value));
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        return;
    }
}

static void
gst_jack_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
{
    GstJack *this;

    this = (GstJack *)object;

    switch (prop_id) {
    case ARG_PORT_NAME_PREFIX:
        g_value_set_string (value, this->port_name_prefix);
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        break;
    }
}

static GstElementStateReturn
gst_jack_change_state (GstElement *element)
{
    GstJack *this;
    GList *l = NULL, **pads;
    GstJackPad *pad;
    GstCaps *caps;
    
    g_return_val_if_fail (element != NULL, FALSE);
    this = GST_JACK (element);
    
    switch (GST_STATE_PENDING (element)) {
    case GST_STATE_NULL:
        g_message ("jack client %s: NULL", GST_OBJECT_NAME (GST_OBJECT (this)));

        break;
        
    case GST_STATE_READY:
        g_message ("jack client %s: READY", GST_OBJECT_NAME (GST_OBJECT (this)));

        if (!this->bin) {
            if (!(this->bin = (GstJackBin*)gst_element_get_managing_bin (element))
                || !GST_IS_JACK_BIN (this->bin)) {
                this->bin = NULL;
                g_warning ("jack element %s needs to be contained in a jack bin.",
                           GST_OBJECT_NAME (GST_OBJECT (element)));
                return GST_STATE_FAILURE;
            }

            /* fixme: verify that all names are unique */
            l = this->pads;
            pads = (this->direction == GST_PAD_SRC) ? &this->bin->src_pads : &this->bin->sink_pads;
            while (l) {
                pad = GST_JACK_PAD (l);
                g_message ("jack: appending pad %s:%s to list", pad->name, pad->peer_name);
                *pads = g_list_append (*pads, pad);
                l = g_list_next (l);
            }
        }
        break;
        
    case GST_STATE_PAUSED:
        g_message ("jack client %s: PAUSED", GST_OBJECT_NAME (GST_OBJECT (this)));

        if (GST_STATE (element) == GST_STATE_READY) {
            /* we're in READY->PAUSED */
            l = this->pads;
            while (l) {
                pad = GST_JACK_PAD (l);
                caps = gst_pad_get_caps (pad->pad);
                gst_caps_set (caps, "rate", GST_PROPS_INT_TYPE, (gint) this->bin->rate, NULL);
                caps->fixed = TRUE; /* we know this to be true */
                if (!gst_pad_try_set_caps (pad->pad, caps))
                    return GST_STATE_FAILURE;
                l = g_list_next (l);
            }
        }
        break;
    case GST_STATE_PLAYING:
        g_message ("jack client %s: PLAYING", GST_OBJECT_NAME (GST_OBJECT (this)));
        break;
    }
    
    g_message ("jack: state change finished");
    
    if (GST_ELEMENT_CLASS (parent_class)->change_state)
        return GST_ELEMENT_CLASS (parent_class)->change_state (element);

    return GST_STATE_SUCCESS;
}

static GstPadConnectReturn
gst_jack_connect (GstPad *pad, GstCaps *caps)
{
  GstJack *this;
  gint rate;
  
  this = GST_JACK (gst_pad_get_parent (pad));
  g_return_val_if_fail (this != NULL, GST_PAD_CONNECT_REFUSED);
  g_return_val_if_fail (GST_IS_JACK (this), GST_PAD_CONNECT_REFUSED);
  
  if (GST_CAPS_IS_FIXED (caps)) {
      gst_caps_get_int (caps, "rate", &rate);
      if (this->bin && rate != this->bin->rate)
          return GST_PAD_CONNECT_REFUSED;
      
      return GST_PAD_CONNECT_OK;
  }
  
  return GST_PAD_CONNECT_DELAYED;
}

static void
gst_jack_loop (GstElement *element)
{
    GstJack *this;
    GList *pads;
    gint len;
    gchar *peeked;
    gint avail;
    GstEvent *event;
    GstJackPad *pad;
    GstBuffer *buffer;
    
    this = GST_JACK (element);
    
    g_return_if_fail(this != NULL);
    len = this->bin->nframes * sizeof (jack_default_audio_sample_t);
    
    do {
        pads = this->pads;
        while (pads) {
            pad = GST_JACK_PAD (pads);
            
            if (this->direction == GST_PAD_SINK) {
                if (!pad->bs)
                    pad->bs = gst_bytestream_new (pad->pad);
                
                if (gst_bytestream_peek_bytes (pad->bs, (guint8**)&peeked, len) < len) {
                    gst_bytestream_get_status(pad->bs, &avail, &event);
                    if (event) {
                        g_warning("got an event on jacksink");
                        if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
                            /* really, we should just cut this pad out of the graph. let
                             * me know when this is needed ;)
                             * also, for sample accuracy etc, we should play avail
                             * bytes, but hey. */
                            gst_element_set_eos(GST_ELEMENT(this));
                            gst_event_free(event);
                        }
                    } else {
                        /* the element at the top of the chain did not emit an eos
                         * event. this is a Bug(tm) */
                        g_assert_not_reached();
                    }
                }
                
                memcpy (pad->data, peeked, len);
                gst_bytestream_flush (pad->bs, len);
            } else {
                buffer = gst_buffer_new ();
                GST_BUFFER_DATA (buffer)    = pad->data;
                GST_BUFFER_SIZE (buffer)    = len;
                GST_BUFFER_MAXSIZE (buffer) = len;
                
                gst_pad_push (pad->pad, buffer);
            }
            pads = g_list_next (pads);
        }
        
	gst_element_yield (element);
    } while (TRUE);
}

static gboolean
plugin_init (GModule *module, GstPlugin *plugin)
{
    GstElementFactory *factory;
    
    if (!gst_library_load ("gstbytestream")) {
        gst_info("jack: could not load support library: 'gstbytestream'\n");
        return FALSE;
    }
    
    factory = gst_element_factory_new ("jackbin", GST_TYPE_JACK_BIN, &gst_jack_bin_details);
    g_return_val_if_fail (factory != NULL, FALSE);
    gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
    
    factory = gst_element_factory_new ("jacksrc", GST_TYPE_JACK_SRC, &gst_jack_src_details);
    g_return_val_if_fail (factory != NULL, FALSE);
    gst_element_factory_add_pad_template (factory, gst_jack_src_request_pad_factory());
    gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
    
    factory = gst_element_factory_new ("jacksink", GST_TYPE_JACK_SINK, &gst_jack_sink_details);
    g_return_val_if_fail (factory != NULL, FALSE);
    gst_element_factory_add_pad_template (factory, gst_jack_sink_request_pad_factory());
    gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
    
    gst_plugin_set_longname(plugin, "JACK plugin library");
    
    return TRUE;
}

GstPluginDesc plugin_desc = {
    GST_VERSION_MAJOR,
    GST_VERSION_MINOR,
    "jack",
    plugin_init
};