211e1d2e6df4b2be26824dff5aa12a166a464043
[platform/upstream/gstreamer.git] / ext / jack / gstjackaudiosrc.c
1 /* GStreamer
2  * Copyright (C) 2008 Tristan Matthews <tristan@sat.qc.ca>
3  * 
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  *
22  * Alternatively, the contents of this file may be used under the
23  * GNU Lesser General Public License Version 2.1 (the "LGPL"), in
24  * which case the following provisions apply instead of the ones
25  * mentioned above:
26  *
27  * This library is free software; you can redistribute it and/or
28  * modify it under the terms of the GNU Library General Public
29  * License as published by the Free Software Foundation; either
30  * version 2 of the License, or (at your option) any later version.
31  *
32  * This library is distributed in the hope that it will be useful,
33  * but WITHOUT ANY WARRANTY; without even the implied warranty of
34  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
35  * Library General Public License for more details.
36  *
37  * You should have received a copy of the GNU Library General Public
38  * License along with this library; if not, write to the
39  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
40  * Boston, MA 02111-1307, USA.
41  */
42
43 /**
44  * SECTION:element-jack_audio_src
45  * @see_also: #GstBaseAudioSrc, #GstRingBuffer
46  *
47  * A Src that inputs data from Jack ports.
48  * 
49  * It will create N Jack ports named in_&lt;name&gt;_&lt;num&gt; where 
50  * &lt;name&gt; is the element name and &lt;num&gt; is starting from 1.
51  * Each port corresponds to a gstreamer channel.
52  * 
53  * The samplerate as exposed on the caps is always the same as the samplerate of
54  * the jack server.
55  * 
56  * When the #GstJackAudioSrc:connect property is set to auto, this element
57  * will try to connect each input port to a random physical jack output pin. 
58  * 
59  * When the #GstJackAudioSrc:connect property is set to none, the element will
60  * accept any number of output channels and will create (but not connect) an
61  * input port for each channel.
62  * 
63  * The element will generate an error when the Jack server is shut down when it
64  * was PAUSED or PLAYING. This element does not support dynamic rate and buffer
65  * size changes at runtime.
66  * 
67  * <refsect2>
68  * <title>Example launch line</title>
69  * |[
70  * gst-launch jackaudiosrc connect=0 ! jackaudiosink connect=0
71  * ]| Get audio input into gstreamer from jack.
72  * </refsect2>
73  *
74  * Last reviewed on 2008-07-22 (0.10.4)
75  */
76
77 #include <gst/gst.h>
78 #include <stdlib.h>
79 #include <string.h>
80
81 #include "gstjackaudiosrc.h"
82 #include "gstjackringbuffer.h"
83
84 GST_DEBUG_CATEGORY_STATIC (gst_jack_audio_src_debug);
85 #define GST_CAT_DEFAULT gst_jack_audio_src_debug
86
87 static gboolean
88 gst_jack_audio_src_allocate_channels (GstJackAudioSrc * src, gint channels)
89 {
90   jack_client_t *client;
91
92   client = gst_jack_audio_client_get_client (src->client);
93
94   /* remove ports we don't need */
95   while (src->port_count > channels)
96     jack_port_unregister (client, src->ports[--src->port_count]);
97
98   /* alloc enough input ports */
99   src->ports = g_realloc (src->ports, sizeof (jack_port_t *) * channels);
100
101   /* create an input port for each channel */
102   while (src->port_count < channels) {
103     gchar *name;
104
105     /* port names start from 1 and are local to the element */
106     name =
107         g_strdup_printf ("in_%s_%d", GST_ELEMENT_NAME (src),
108         src->port_count + 1);
109     src->ports[src->port_count] =
110         jack_port_register (client, name, JACK_DEFAULT_AUDIO_TYPE,
111         JackPortIsInput, 0);
112     if (src->ports[src->port_count] == NULL)
113       return FALSE;
114
115     src->port_count++;
116
117     g_free (name);
118   }
119   return TRUE;
120 }
121
122 static void
123 gst_jack_audio_src_free_channels (GstJackAudioSrc * src)
124 {
125   gint res, i = 0;
126   jack_client_t *client;
127
128   client = gst_jack_audio_client_get_client (src->client);
129
130   /* get rid of all ports */
131   while (src->port_count) {
132     GST_LOG_OBJECT (src, "unregister port %d", i);
133     if ((res = jack_port_unregister (client, src->ports[i++])))
134       GST_DEBUG_OBJECT (src, "unregister of port failed (%d)", res);
135
136     src->port_count--;
137   }
138   g_free (src->ports);
139   src->ports = NULL;
140 }
141
142 /* ringbuffer abstract base class */
143 static GType
144 gst_jack_ring_buffer_get_type ()
145 {
146   static GType ringbuffer_type = 0;
147
148   if (!ringbuffer_type) {
149     static const GTypeInfo ringbuffer_info = { sizeof (GstJackRingBufferClass),
150       NULL,
151       NULL,
152       (GClassInitFunc) gst_jack_ring_buffer_class_init,
153       NULL,
154       NULL,
155       sizeof (GstJackRingBuffer),
156       0,
157       (GInstanceInitFunc) gst_jack_ring_buffer_init,
158       NULL
159     };
160
161     ringbuffer_type =
162         g_type_register_static (GST_TYPE_RING_BUFFER,
163         "GstJackAudioSrcRingBuffer", &ringbuffer_info, 0);
164   }
165   return ringbuffer_type;
166 }
167
168 static void
169 gst_jack_ring_buffer_class_init (GstJackRingBufferClass * klass)
170 {
171   GObjectClass *gobject_class;
172   GstObjectClass *gstobject_class;
173   GstRingBufferClass *gstringbuffer_class;
174
175   gobject_class = (GObjectClass *) klass;
176   gstobject_class = (GstObjectClass *) klass;
177   gstringbuffer_class = (GstRingBufferClass *) klass;
178
179   ring_parent_class = g_type_class_peek_parent (klass);
180
181   gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_jack_ring_buffer_dispose);
182   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_jack_ring_buffer_finalize);
183
184   gstringbuffer_class->open_device =
185       GST_DEBUG_FUNCPTR (gst_jack_ring_buffer_open_device);
186   gstringbuffer_class->close_device =
187       GST_DEBUG_FUNCPTR (gst_jack_ring_buffer_close_device);
188   gstringbuffer_class->acquire =
189       GST_DEBUG_FUNCPTR (gst_jack_ring_buffer_acquire);
190   gstringbuffer_class->release =
191       GST_DEBUG_FUNCPTR (gst_jack_ring_buffer_release);
192   gstringbuffer_class->start = GST_DEBUG_FUNCPTR (gst_jack_ring_buffer_start);
193   gstringbuffer_class->pause = GST_DEBUG_FUNCPTR (gst_jack_ring_buffer_pause);
194   gstringbuffer_class->resume = GST_DEBUG_FUNCPTR (gst_jack_ring_buffer_start);
195   gstringbuffer_class->stop = GST_DEBUG_FUNCPTR (gst_jack_ring_buffer_stop);
196
197   gstringbuffer_class->delay = GST_DEBUG_FUNCPTR (gst_jack_ring_buffer_delay);
198 }
199
200 /* this is the callback of jack. This should be RT-safe.
201  * Writes samples from the jack input port's buffer to the gst ring buffer.
202  */
203 static int
204 jack_process_cb (jack_nframes_t nframes, void *arg)
205 {
206   GstJackAudioSrc *src;
207   GstRingBuffer *buf;
208   GstJackRingBuffer *abuf;
209   gint len, givenLen;
210   guint8 *writeptr, *dataStart;
211   gint writeseg;
212   gint channels, i, j;
213   sample_t **buffers, *data;
214
215   buf = GST_RING_BUFFER_CAST (arg);
216   abuf = GST_JACK_RING_BUFFER_CAST (arg);
217   src = GST_JACK_AUDIO_SRC (GST_OBJECT_PARENT (buf));
218
219   channels = buf->spec.channels;
220   len = sizeof (sample_t) * nframes * channels;
221
222   /* alloc pointers to samples */
223   buffers = g_alloca (sizeof (sample_t *) * channels);
224   data = g_alloca (len);
225
226   /* get input buffers */
227   for (i = 0; i < channels; i++)
228     buffers[i] = (sample_t *) jack_port_get_buffer (src->ports[i], nframes);
229
230   //writeptr = data; 
231   dataStart = (guint8 *) data;
232
233   /* the samples in the jack input buffers have to be interleaved into the 
234    * ringbuffer 
235    */
236   for (i = 0; i < nframes; ++i)
237     for (j = 0; j < channels; ++j)
238       *data++ = buffers[j][i];
239
240   if (gst_ring_buffer_prepare_read (buf, &writeseg, &writeptr, &givenLen)) {
241     memcpy (writeptr, (char *) dataStart, givenLen);
242
243     GST_DEBUG ("copy %d frames: %p, %d bytes, %d channels", nframes, writeptr,
244         len / channels, channels);
245
246     /* clear written samples in the ringbuffer */
247     // gst_ring_buffer_clear(buf, 0);
248
249     /* we wrote one segment */
250     gst_ring_buffer_advance (buf, 1);
251   }
252   return 0;
253 }
254
255 /* we error out */
256 static int
257 jack_sample_rate_cb (jack_nframes_t nframes, void *arg)
258 {
259   GstJackAudioSrc *src;
260   GstJackRingBuffer *abuf;
261
262   abuf = GST_JACK_RING_BUFFER_CAST (arg);
263   src = GST_JACK_AUDIO_SRC (GST_OBJECT_PARENT (arg));
264
265   if (abuf->sample_rate != -1 && abuf->sample_rate != nframes)
266     goto not_supported;
267
268   return 0;
269
270   /* ERRORS */
271 not_supported:
272   {
273     GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS,
274         (NULL), ("Jack changed the sample rate, which is not supported"));
275     return 1;
276   }
277 }
278
279 /* we error out */
280 static int
281 jack_buffer_size_cb (jack_nframes_t nframes, void *arg)
282 {
283   GstJackAudioSrc *src;
284   GstJackRingBuffer *abuf;
285
286   abuf = GST_JACK_RING_BUFFER_CAST (arg);
287   src = GST_JACK_AUDIO_SRC (GST_OBJECT_PARENT (arg));
288
289   if (abuf->buffer_size != -1 && abuf->buffer_size != nframes)
290     goto not_supported;
291
292   return 0;
293
294   /* ERRORS */
295 not_supported:
296   {
297     GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS,
298         (NULL), ("Jack changed the buffer size, which is not supported"));
299     return 1;
300   }
301 }
302
303 static void
304 jack_shutdown_cb (void *arg)
305 {
306   GstJackAudioSrc *src;
307
308   src = GST_JACK_AUDIO_SRC (GST_OBJECT_PARENT (arg));
309
310   GST_DEBUG_OBJECT (src, "shutdown");
311
312   GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND,
313       (NULL), ("Jack server shutdown"));
314 }
315
316 static void
317 gst_jack_ring_buffer_init (GstJackRingBuffer * buf,
318     GstJackRingBufferClass * g_class)
319 {
320   buf->channels = -1;
321   buf->buffer_size = -1;
322   buf->sample_rate = -1;
323 }
324
325 static void
326 gst_jack_ring_buffer_dispose (GObject * object)
327 {
328   G_OBJECT_CLASS (ring_parent_class)->dispose (object);
329 }
330
331 static void
332 gst_jack_ring_buffer_finalize (GObject * object)
333 {
334   GstJackRingBuffer *ringbuffer;
335   ringbuffer = GST_JACK_RING_BUFFER_CAST (object);
336   G_OBJECT_CLASS (ring_parent_class)->finalize (object);
337 }
338
339 /* the _open_device method should make a connection with the server
340 */
341 static gboolean
342 gst_jack_ring_buffer_open_device (GstRingBuffer * buf)
343 {
344   GstJackAudioSrc *src;
345   jack_status_t status = 0;
346   const gchar *name;
347
348   src = GST_JACK_AUDIO_SRC (GST_OBJECT_PARENT (buf));
349
350   GST_DEBUG_OBJECT (src, "open");
351
352   name = g_get_application_name ();
353   if (!name)
354     name = "GStreamer";
355
356   src->client = gst_jack_audio_client_new (name, src->server,
357       GST_JACK_CLIENT_SOURCE,
358       jack_shutdown_cb,
359       jack_process_cb, jack_buffer_size_cb, jack_sample_rate_cb, buf, &status);
360   if (src->client == NULL)
361     goto could_not_open;
362
363   GST_DEBUG_OBJECT (src, "opened");
364
365   return TRUE;
366
367   /* ERRORS */
368 could_not_open:
369   {
370     if (status & JackServerFailed) {
371       GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND,
372           (NULL), ("Cannot connect to the Jack server (status %d)", status));
373     } else {
374       GST_ELEMENT_ERROR (src, RESOURCE, OPEN_WRITE,
375           (NULL), ("Jack client open error (status %d)", status));
376     }
377     return FALSE;
378   }
379 }
380
381 /* close the connection with the server
382 */
383 static gboolean
384 gst_jack_ring_buffer_close_device (GstRingBuffer * buf)
385 {
386   GstJackAudioSrc *src;
387
388   src = GST_JACK_AUDIO_SRC (GST_OBJECT_PARENT (buf));
389
390   GST_DEBUG_OBJECT (src, "close");
391
392   gst_jack_audio_src_free_channels (src);
393   gst_jack_audio_client_free (src->client);
394   src->client = NULL;
395
396   return TRUE;
397 }
398
399
400 /* allocate a buffer and setup resources to process the audio samples of
401  * the format as specified in @spec.
402  *
403  * We allocate N jack ports, one for each channel. If we are asked to
404  * automatically make a connection with physical ports, we connect as many
405  * ports as there are physical ports, leaving leftover ports unconnected.
406  *
407  * It is assumed that samplerate and number of channels are acceptable since our
408  * getcaps method will always provide correct values. If unacceptable caps are
409  * received for some reason, we fail here.
410  */
411 static gboolean
412 gst_jack_ring_buffer_acquire (GstRingBuffer * buf, GstRingBufferSpec * spec)
413 {
414   GstJackAudioSrc *src;
415   GstJackRingBuffer *abuf;
416   const char **ports;
417   gint sample_rate, buffer_size;
418   gint i, channels, res;
419   jack_client_t *client;
420
421   src = GST_JACK_AUDIO_SRC (GST_OBJECT_PARENT (buf));
422   abuf = GST_JACK_RING_BUFFER_CAST (buf);
423
424   GST_DEBUG_OBJECT (src, "acquire");
425
426   client = gst_jack_audio_client_get_client (src->client);
427
428   /* sample rate must be that of the server */
429   sample_rate = jack_get_sample_rate (client);
430   if (sample_rate != spec->rate)
431     goto wrong_samplerate;
432
433   channels = spec->channels;
434
435   if (!gst_jack_audio_src_allocate_channels (src, channels))
436     goto out_of_ports;
437
438   buffer_size = jack_get_buffer_size (client);
439
440   /* the segment size in bytes, this is large enough to hold a buffer of 32bit floats
441    * for all channels  */
442   spec->segsize = buffer_size * sizeof (gfloat) * channels;
443   spec->latency_time = gst_util_uint64_scale (spec->segsize,
444       (GST_SECOND / GST_USECOND), spec->rate * spec->bytes_per_sample);
445   /* segtotal based on buffer-time latency */
446   spec->segtotal = spec->buffer_time / spec->latency_time;
447   if (spec->segtotal < 2) {
448     spec->segtotal = 2;
449     spec->buffer_time = spec->latency_time * spec->segtotal;
450   }
451
452   GST_DEBUG_OBJECT (src, "buffer time: %" G_GINT64_FORMAT " usec",
453       spec->buffer_time);
454   GST_DEBUG_OBJECT (src, "latency time: %" G_GINT64_FORMAT " usec",
455       spec->latency_time);
456   GST_DEBUG_OBJECT (src, "buffer_size %d, segsize %d, segtotal %d",
457       buffer_size, spec->segsize, spec->segtotal);
458
459   /* allocate the ringbuffer memory now */
460   buf->data = gst_buffer_new_and_alloc (spec->segtotal * spec->segsize);
461   memset (GST_BUFFER_DATA (buf->data), 0, GST_BUFFER_SIZE (buf->data));
462
463   if ((res = gst_jack_audio_client_set_active (src->client, TRUE)))
464     goto could_not_activate;
465
466   /* if we need to automatically connect the ports, do so now. We must do this
467    * after activating the client. */
468   if (src->connect == GST_JACK_CONNECT_AUTO
469       || src->connect == GST_JACK_CONNECT_AUTO_FORCED) {
470     /* find all the physical output ports. A physical output port is a port
471      * associated with a hardware device. Someone needs connect to a physical
472      * port in order to capture something. */
473     ports =
474         jack_get_ports (client, NULL, NULL,
475         JackPortIsPhysical | JackPortIsOutput);
476     if (ports == NULL) {
477       /* no ports? fine then we don't do anything except for posting a warning
478        * message. */
479       GST_ELEMENT_WARNING (src, RESOURCE, NOT_FOUND, (NULL),
480           ("No physical output ports found, leaving ports unconnected"));
481       goto done;
482     }
483
484     for (i = 0; i < channels; i++) {
485       /* stop when all output ports are exhausted */
486       if (ports[i] == NULL) {
487         /* post a warning that we could not connect all ports */
488         GST_ELEMENT_WARNING (src, RESOURCE, NOT_FOUND, (NULL),
489             ("No more physical ports, leaving some ports unconnected"));
490         break;
491       }
492       GST_DEBUG_OBJECT (src, "try connecting to %s",
493           jack_port_name (src->ports[i]));
494
495       /* connect the physical port to a port */
496       res = jack_connect (client, ports[i], jack_port_name (src->ports[i]));
497       if (res != 0 && res != EEXIST)
498         goto cannot_connect;
499     }
500     free (ports);
501   }
502 done:
503
504   abuf->sample_rate = sample_rate;
505   abuf->buffer_size = buffer_size;
506   abuf->channels = spec->channels;
507
508   return TRUE;
509
510   /* ERRORS */
511 wrong_samplerate:
512   {
513     GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
514         ("Wrong samplerate, server is running at %d and we received %d",
515             sample_rate, spec->rate));
516     return FALSE;
517   }
518 out_of_ports:
519   {
520     GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
521         ("Cannot allocate more Jack ports"));
522     return FALSE;
523   }
524 could_not_activate:
525   {
526     GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
527         ("Could not activate client (%d:%s)", res, g_strerror (res)));
528     return FALSE;
529   }
530 cannot_connect:
531   {
532     GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
533         ("Could not connect input ports to physical ports (%d:%s)",
534             res, g_strerror (res)));
535     free (ports);
536     return FALSE;
537   }
538 }
539
540 /* function is called with LOCK */
541 static gboolean
542 gst_jack_ring_buffer_release (GstRingBuffer * buf)
543 {
544   GstJackAudioSrc *src;
545   GstJackRingBuffer *abuf;
546   gint res;
547
548   abuf = GST_JACK_RING_BUFFER_CAST (buf);
549   src = GST_JACK_AUDIO_SRC (GST_OBJECT_PARENT (buf));
550
551   GST_DEBUG_OBJECT (src, "release");
552
553   if ((res = gst_jack_audio_client_set_active (src->client, FALSE))) {
554     /* we only warn, this means the server is probably shut down and the client
555      * is gone anyway. */
556     GST_ELEMENT_WARNING (src, RESOURCE, CLOSE, (NULL),
557         ("Could not deactivate Jack client (%d)", res));
558   }
559
560   abuf->channels = -1;
561   abuf->buffer_size = -1;
562   abuf->sample_rate = -1;
563
564   /* free the buffer */
565   gst_buffer_unref (buf->data);
566   buf->data = NULL;
567
568   return TRUE;
569 }
570
571 static gboolean
572 gst_jack_ring_buffer_start (GstRingBuffer * buf)
573 {
574   GstJackAudioSrc *src;
575
576   src = GST_JACK_AUDIO_SRC (GST_OBJECT_PARENT (buf));
577
578   GST_DEBUG_OBJECT (src, "start");
579
580   return TRUE;
581 }
582
583 static gboolean
584 gst_jack_ring_buffer_pause (GstRingBuffer * buf)
585 {
586   GstJackAudioSrc *src;
587
588   src = GST_JACK_AUDIO_SRC (GST_OBJECT_PARENT (buf));
589
590   GST_DEBUG_OBJECT (src, "pause");
591
592   return TRUE;
593 }
594
595 static gboolean
596 gst_jack_ring_buffer_stop (GstRingBuffer * buf)
597 {
598   GstJackAudioSrc *src;
599
600   src = GST_JACK_AUDIO_SRC (GST_OBJECT_PARENT (buf));
601
602   GST_DEBUG_OBJECT (src, "stop");
603
604   return TRUE;
605 }
606
607 static guint
608 gst_jack_ring_buffer_delay (GstRingBuffer * buf)
609 {
610   GstJackAudioSrc *src;
611   guint i, res = 0, latency;
612   jack_client_t *client;
613
614   src = GST_JACK_AUDIO_SRC (GST_OBJECT_PARENT (buf));
615   client = gst_jack_audio_client_get_client (src->client);
616
617   for (i = 0; i < src->port_count; i++) {
618     latency = jack_port_get_total_latency (client, src->ports[i]);
619     if (latency > res)
620       res = latency;
621   }
622
623   GST_DEBUG_OBJECT (src, "delay %u", res);
624
625   return res;
626 }
627
628 /* Audiosrc signals and args */
629 enum
630 {
631   /* FILL ME */
632   LAST_SIGNAL
633 };
634
635 #define DEFAULT_PROP_CONNECT    GST_JACK_CONNECT_AUTO
636 #define DEFAULT_PROP_SERVER     NULL
637
638 enum
639 {
640   PROP_0,
641   PROP_CONNECT,
642   PROP_SERVER,
643   PROP_LAST
644 };
645
646
647 /* the capabilities of the inputs and outputs.
648  *
649  * describe the real formats here.
650  */
651
652 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
653     GST_PAD_SRC,
654     GST_PAD_ALWAYS,
655     GST_STATIC_CAPS ("audio/x-raw-float, "
656         "endianness = (int) { " G_STRINGIFY (G_BYTE_ORDER) " }, "
657         "width = (int) 32, "
658         "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, MAX ]")
659     );
660
661 #define _do_init(bla) \
662   GST_DEBUG_CATEGORY_INIT(gst_jack_audio_src_debug, "jacksrc", 0, "jacksrc element");
663
664 GST_BOILERPLATE_FULL (GstJackAudioSrc, gst_jack_audio_src, GstBaseAudioSrc,
665     GST_TYPE_BASE_AUDIO_SRC, _do_init);
666
667 static void gst_jack_audio_src_set_property (GObject * object, guint prop_id,
668     const GValue * value, GParamSpec * pspec);
669 static void gst_jack_audio_src_get_property (GObject * object, guint prop_id,
670     GValue * value, GParamSpec * pspec);
671
672 static GstCaps *gst_jack_audio_src_getcaps (GstBaseSrc * bsrc);
673 static GstRingBuffer *gst_jack_audio_src_create_ringbuffer (GstBaseAudioSrc *
674     src);
675
676 /* GObject vmethod implementations */
677
678 static void
679 gst_jack_audio_src_base_init (gpointer gclass)
680 {
681   static GstElementDetails gst_jack_audio_src_details = {
682     "Audio Source (Jack)",
683     "Source/Audio",
684     "Input from Jack",
685     "Tristan Matthews <tristan@sat.qc.ca>"
686   };
687   GstElementClass *element_class = GST_ELEMENT_CLASS (gclass);
688
689   gst_element_class_add_pad_template (element_class,
690       gst_static_pad_template_get (&src_factory));
691   gst_element_class_set_details (element_class, &gst_jack_audio_src_details);
692 }
693
694 /* initialize the jack_audio_src's class */
695 static void
696 gst_jack_audio_src_class_init (GstJackAudioSrcClass * klass)
697 {
698   GObjectClass *gobject_class;
699   GstElementClass *gstelement_class;
700   GstBaseSrcClass *gstbasesrc_class;
701   GstBaseAudioSrcClass *gstbaseaudiosrc_class;
702
703   gobject_class = (GObjectClass *) klass;
704   gstelement_class = (GstElementClass *) klass;
705
706   gstbasesrc_class = (GstBaseSrcClass *) klass;
707   gstbaseaudiosrc_class = (GstBaseAudioSrcClass *) klass;
708
709   gobject_class->set_property =
710       GST_DEBUG_FUNCPTR (gst_jack_audio_src_set_property);
711   gobject_class->get_property =
712       GST_DEBUG_FUNCPTR (gst_jack_audio_src_get_property);
713
714   g_object_class_install_property (gobject_class, PROP_CONNECT,
715       g_param_spec_enum ("connect", "Connect",
716           "Specify how the input ports will be connected",
717           GST_TYPE_JACK_CONNECT, DEFAULT_PROP_CONNECT, G_PARAM_READWRITE));
718
719   g_object_class_install_property (gobject_class, PROP_SERVER,
720       g_param_spec_string ("server", "Server",
721           "The Jack server to connect to (NULL = default)",
722           DEFAULT_PROP_SERVER, G_PARAM_READWRITE));
723
724   gstbasesrc_class->get_caps = GST_DEBUG_FUNCPTR (gst_jack_audio_src_getcaps);
725   gstbaseaudiosrc_class->create_ringbuffer =
726       GST_DEBUG_FUNCPTR (gst_jack_audio_src_create_ringbuffer);
727
728   /* ref class from a thread-safe context to work around missing bit of
729    * thread-safety in GObject */
730   g_type_class_ref (GST_TYPE_JACK_RING_BUFFER);
731
732   gst_jack_audio_client_init ();
733 }
734
735 /* initialize the new element
736  * instantiate pads and add them to element
737  * set pad calback functions
738  * initialize instance structure
739  */
740 static void
741 gst_jack_audio_src_init (GstJackAudioSrc * src, GstJackAudioSrcClass * gclass)
742 {
743   //gst_base_src_set_live(GST_BASE_SRC (src), TRUE);
744   src->connect = DEFAULT_PROP_CONNECT;
745   src->server = g_strdup (DEFAULT_PROP_SERVER);
746   src->ports = NULL;
747   src->port_count = 0;
748 }
749
750 static void
751 gst_jack_audio_src_set_property (GObject * object, guint prop_id,
752     const GValue * value, GParamSpec * pspec)
753 {
754   GstJackAudioSrc *src = GST_JACK_AUDIO_SRC (object);
755
756   switch (prop_id) {
757     case PROP_CONNECT:
758       src->connect = g_value_get_enum (value);
759       break;
760     case PROP_SERVER:
761       g_free (src->server);
762       src->server = g_value_dup_string (value);
763       break;
764     default:
765       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
766       break;
767   }
768 }
769
770 static void
771 gst_jack_audio_src_get_property (GObject * object, guint prop_id,
772     GValue * value, GParamSpec * pspec)
773 {
774   GstJackAudioSrc *src = GST_JACK_AUDIO_SRC (object);
775
776   switch (prop_id) {
777     case PROP_CONNECT:
778       g_value_set_enum (value, src->connect);
779       break;
780     case PROP_SERVER:
781       g_value_set_string (value, src->server);
782       break;
783     default:
784       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
785       break;
786   }
787 }
788
789 static GstCaps *
790 gst_jack_audio_src_getcaps (GstBaseSrc * bsrc)
791 {
792   GstJackAudioSrc *src = GST_JACK_AUDIO_SRC (bsrc);
793   const char **ports;
794   gint min, max;
795   gint rate;
796   jack_client_t *client;
797
798   if (src->client == NULL)
799     goto no_client;
800
801   client = gst_jack_audio_client_get_client (src->client);
802
803   if (src->connect == GST_JACK_CONNECT_AUTO) {
804     /* get a port count, this is the number of channels we can automatically
805      * connect. */
806     ports = jack_get_ports (client, NULL, NULL,
807         JackPortIsPhysical | JackPortIsOutput);
808     max = 0;
809     if (ports != NULL) {
810       for (; ports[max]; max++);
811
812       free (ports);
813     } else
814       max = 0;
815   } else {
816     /* we allow any number of pads, something else is going to connect the
817      * pads. */
818     max = G_MAXINT;
819   }
820   min = MIN (1, max);
821
822   rate = jack_get_sample_rate (client);
823
824   GST_DEBUG_OBJECT (src, "got %d-%d ports, samplerate: %d", min, max, rate);
825
826   if (!src->caps) {
827     src->caps = gst_caps_new_simple ("audio/x-raw-float",
828         "endianness", G_TYPE_INT, G_BYTE_ORDER,
829         "width", G_TYPE_INT, 32,
830         "rate", G_TYPE_INT, rate,
831         "channels", GST_TYPE_INT_RANGE, min, max, NULL);
832   }
833   GST_INFO_OBJECT (src, "returning caps %" GST_PTR_FORMAT, src->caps);
834
835   return gst_caps_ref (src->caps);
836
837   /* ERRORS */
838 no_client:
839   {
840     GST_DEBUG_OBJECT (src, "device not open, using template caps");
841     /* base class will get template caps for us when we return NULL */
842     return NULL;
843   }
844 }
845
846 static GstRingBuffer *
847 gst_jack_audio_src_create_ringbuffer (GstBaseAudioSrc * src)
848 {
849   GstRingBuffer *buffer;
850
851   buffer = g_object_new (GST_TYPE_JACK_RING_BUFFER, NULL);
852   GST_DEBUG_OBJECT (src, "created ringbuffer @%p", buffer);
853
854   return buffer;
855 }