3c37970623b54d00b339a7affc893f5ff253c2b0
[framework/multimedia/gst-plugins-good0.10.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-jackaudiosrc
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 #ifdef HAVE_CONFIG_H
78 #include "config.h"
79 #endif
80
81 #include <gst/gst-i18n-plugin.h>
82 #include <stdlib.h>
83 #include <string.h>
84
85 #include "gstjackaudiosrc.h"
86 #include "gstjackringbuffer.h"
87 #include "gstjackutil.h"
88
89 GST_DEBUG_CATEGORY_STATIC (gst_jack_audio_src_debug);
90 #define GST_CAT_DEFAULT gst_jack_audio_src_debug
91
92 static gboolean
93 gst_jack_audio_src_allocate_channels (GstJackAudioSrc * src, gint channels)
94 {
95   jack_client_t *client;
96
97   client = gst_jack_audio_client_get_client (src->client);
98
99   /* remove ports we don't need */
100   while (src->port_count > channels)
101     jack_port_unregister (client, src->ports[--src->port_count]);
102
103   /* alloc enough input ports */
104   src->ports = g_realloc (src->ports, sizeof (jack_port_t *) * channels);
105   src->buffers = g_realloc (src->buffers, sizeof (sample_t *) * channels);
106
107   /* create an input port for each channel */
108   while (src->port_count < channels) {
109     gchar *name;
110
111     /* port names start from 1 and are local to the element */
112     name =
113         g_strdup_printf ("in_%s_%d", GST_ELEMENT_NAME (src),
114         src->port_count + 1);
115     src->ports[src->port_count] =
116         jack_port_register (client, name, JACK_DEFAULT_AUDIO_TYPE,
117         JackPortIsInput, 0);
118     if (src->ports[src->port_count] == NULL)
119       return FALSE;
120
121     src->port_count++;
122
123     g_free (name);
124   }
125   return TRUE;
126 }
127
128 static void
129 gst_jack_audio_src_free_channels (GstJackAudioSrc * src)
130 {
131   gint res, i = 0;
132   jack_client_t *client;
133
134   client = gst_jack_audio_client_get_client (src->client);
135
136   /* get rid of all ports */
137   while (src->port_count) {
138     GST_LOG_OBJECT (src, "unregister port %d", i);
139     if ((res = jack_port_unregister (client, src->ports[i++])))
140       GST_DEBUG_OBJECT (src, "unregister of port failed (%d)", res);
141
142     src->port_count--;
143   }
144   g_free (src->ports);
145   src->ports = NULL;
146   g_free (src->buffers);
147   src->buffers = NULL;
148 }
149
150 /* ringbuffer abstract base class */
151 static GType
152 gst_jack_ring_buffer_get_type (void)
153 {
154   static volatile gsize ringbuffer_type = 0;
155
156   if (g_once_init_enter (&ringbuffer_type)) {
157     static const GTypeInfo ringbuffer_info = { sizeof (GstJackRingBufferClass),
158       NULL,
159       NULL,
160       (GClassInitFunc) gst_jack_ring_buffer_class_init,
161       NULL,
162       NULL,
163       sizeof (GstJackRingBuffer),
164       0,
165       (GInstanceInitFunc) gst_jack_ring_buffer_init,
166       NULL
167     };
168     GType tmp = g_type_register_static (GST_TYPE_RING_BUFFER,
169         "GstJackAudioSrcRingBuffer", &ringbuffer_info, 0);
170     g_once_init_leave (&ringbuffer_type, tmp);
171   }
172
173   return (GType) ringbuffer_type;
174 }
175
176 static void
177 gst_jack_ring_buffer_class_init (GstJackRingBufferClass * klass)
178 {
179   GstRingBufferClass *gstringbuffer_class;
180
181   gstringbuffer_class = (GstRingBufferClass *) klass;
182
183   ring_parent_class = g_type_class_peek_parent (klass);
184
185   gstringbuffer_class->open_device =
186       GST_DEBUG_FUNCPTR (gst_jack_ring_buffer_open_device);
187   gstringbuffer_class->close_device =
188       GST_DEBUG_FUNCPTR (gst_jack_ring_buffer_close_device);
189   gstringbuffer_class->acquire =
190       GST_DEBUG_FUNCPTR (gst_jack_ring_buffer_acquire);
191   gstringbuffer_class->release =
192       GST_DEBUG_FUNCPTR (gst_jack_ring_buffer_release);
193   gstringbuffer_class->start = GST_DEBUG_FUNCPTR (gst_jack_ring_buffer_start);
194   gstringbuffer_class->pause = GST_DEBUG_FUNCPTR (gst_jack_ring_buffer_pause);
195   gstringbuffer_class->resume = GST_DEBUG_FUNCPTR (gst_jack_ring_buffer_start);
196   gstringbuffer_class->stop = GST_DEBUG_FUNCPTR (gst_jack_ring_buffer_stop);
197
198   gstringbuffer_class->delay = GST_DEBUG_FUNCPTR (gst_jack_ring_buffer_delay);
199 }
200
201 /* this is the callback of jack. This should be RT-safe.
202  * Writes samples from the jack input port's buffer to the gst ring buffer.
203  */
204 static int
205 jack_process_cb (jack_nframes_t nframes, void *arg)
206 {
207   GstJackAudioSrc *src;
208   GstRingBuffer *buf;
209   gint len;
210   guint8 *writeptr;
211   gint writeseg;
212   gint channels, i, j, flen;
213   sample_t *data;
214
215   buf = GST_RING_BUFFER_CAST (arg);
216   src = GST_JACK_AUDIO_SRC (GST_OBJECT_PARENT (buf));
217
218   channels = buf->spec.channels;
219
220   /* get input buffers */
221   for (i = 0; i < channels; i++)
222     src->buffers[i] =
223         (sample_t *) jack_port_get_buffer (src->ports[i], nframes);
224
225   if (gst_ring_buffer_prepare_read (buf, &writeseg, &writeptr, &len)) {
226     flen = len / channels;
227
228     /* the number of samples must be exactly the segment size */
229     if (nframes * sizeof (sample_t) != flen)
230       goto wrong_size;
231
232     /* the samples in the jack input buffers have to be interleaved into the
233      * ringbuffer */
234     data = (sample_t *) writeptr;
235     for (i = 0; i < nframes; ++i)
236       for (j = 0; j < channels; ++j)
237         *data++ = src->buffers[j][i];
238
239     GST_DEBUG ("copy %d frames: %p, %d bytes, %d channels", nframes, writeptr,
240         len / channels, channels);
241
242     /* we wrote one segment */
243     gst_ring_buffer_advance (buf, 1);
244   }
245   return 0;
246
247   /* ERRORS */
248 wrong_size:
249   {
250     GST_ERROR_OBJECT (src, "nbytes (%d) != flen (%d)",
251         (gint) (nframes * sizeof (sample_t)), flen);
252     return 1;
253   }
254 }
255
256 /* we error out */
257 static int
258 jack_sample_rate_cb (jack_nframes_t nframes, void *arg)
259 {
260   GstJackAudioSrc *src;
261   GstJackRingBuffer *abuf;
262
263   abuf = GST_JACK_RING_BUFFER_CAST (arg);
264   src = GST_JACK_AUDIO_SRC (GST_OBJECT_PARENT (arg));
265
266   if (abuf->sample_rate != -1 && abuf->sample_rate != nframes)
267     goto not_supported;
268
269   return 0;
270
271   /* ERRORS */
272 not_supported:
273   {
274     GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS,
275         (NULL), ("Jack changed the sample rate, which is not supported"));
276     return 1;
277   }
278 }
279
280 /* we error out */
281 static int
282 jack_buffer_size_cb (jack_nframes_t nframes, void *arg)
283 {
284   GstJackAudioSrc *src;
285   GstJackRingBuffer *abuf;
286
287   abuf = GST_JACK_RING_BUFFER_CAST (arg);
288   src = GST_JACK_AUDIO_SRC (GST_OBJECT_PARENT (arg));
289
290   if (abuf->buffer_size != -1 && abuf->buffer_size != nframes)
291     goto not_supported;
292
293   return 0;
294
295   /* ERRORS */
296 not_supported:
297   {
298     GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS,
299         (NULL), ("Jack changed the buffer size, which is not supported"));
300     return 1;
301   }
302 }
303
304 static void
305 jack_shutdown_cb (void *arg)
306 {
307   GstJackAudioSrc *src;
308
309   src = GST_JACK_AUDIO_SRC (GST_OBJECT_PARENT (arg));
310
311   GST_DEBUG_OBJECT (src, "shutdown");
312
313   GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND,
314       (NULL), ("Jack server shutdown"));
315 }
316
317 static void
318 gst_jack_ring_buffer_init (GstJackRingBuffer * buf,
319     GstJackRingBufferClass * g_class)
320 {
321   buf->channels = -1;
322   buf->buffer_size = -1;
323   buf->sample_rate = -1;
324 }
325
326 /* the _open_device method should make a connection with the server
327 */
328 static gboolean
329 gst_jack_ring_buffer_open_device (GstRingBuffer * buf)
330 {
331   GstJackAudioSrc *src;
332   jack_status_t status = 0;
333   const gchar *name;
334
335   src = GST_JACK_AUDIO_SRC (GST_OBJECT_PARENT (buf));
336
337   GST_DEBUG_OBJECT (src, "open");
338
339   if (src->client_name) {
340     name = src->client_name;
341   } else {
342     name = g_get_application_name ();
343   }
344   if (!name)
345     name = "GStreamer";
346
347   src->client = gst_jack_audio_client_new (name, src->server,
348       src->jclient,
349       GST_JACK_CLIENT_SOURCE,
350       jack_shutdown_cb,
351       jack_process_cb, jack_buffer_size_cb, jack_sample_rate_cb, buf, &status);
352   if (src->client == NULL)
353     goto could_not_open;
354
355   GST_DEBUG_OBJECT (src, "opened");
356
357   return TRUE;
358
359   /* ERRORS */
360 could_not_open:
361   {
362     if (status & (JackServerFailed | JackFailure)) {
363       GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND,
364           (_("Jack server not found")),
365           ("Cannot connect to the Jack server (status %d)", status));
366     } else {
367       GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
368           (NULL), ("Jack client open error (status %d)", status));
369     }
370     return FALSE;
371   }
372 }
373
374 /* close the connection with the server
375 */
376 static gboolean
377 gst_jack_ring_buffer_close_device (GstRingBuffer * buf)
378 {
379   GstJackAudioSrc *src;
380
381   src = GST_JACK_AUDIO_SRC (GST_OBJECT_PARENT (buf));
382
383   GST_DEBUG_OBJECT (src, "close");
384
385   gst_jack_audio_src_free_channels (src);
386   gst_jack_audio_client_free (src->client);
387   src->client = NULL;
388
389   return TRUE;
390 }
391
392
393 /* allocate a buffer and setup resources to process the audio samples of
394  * the format as specified in @spec.
395  *
396  * We allocate N jack ports, one for each channel. If we are asked to
397  * automatically make a connection with physical ports, we connect as many
398  * ports as there are physical ports, leaving leftover ports unconnected.
399  *
400  * It is assumed that samplerate and number of channels are acceptable since our
401  * getcaps method will always provide correct values. If unacceptable caps are
402  * received for some reason, we fail here.
403  */
404 static gboolean
405 gst_jack_ring_buffer_acquire (GstRingBuffer * buf, GstRingBufferSpec * spec)
406 {
407   GstJackAudioSrc *src;
408   GstJackRingBuffer *abuf;
409   const char **ports;
410   gint sample_rate, buffer_size;
411   gint i, channels, res;
412   jack_client_t *client;
413
414   src = GST_JACK_AUDIO_SRC (GST_OBJECT_PARENT (buf));
415   abuf = GST_JACK_RING_BUFFER_CAST (buf);
416
417   GST_DEBUG_OBJECT (src, "acquire");
418
419   client = gst_jack_audio_client_get_client (src->client);
420
421   /* sample rate must be that of the server */
422   sample_rate = jack_get_sample_rate (client);
423   if (sample_rate != spec->rate)
424     goto wrong_samplerate;
425
426   channels = spec->channels;
427
428   if (!gst_jack_audio_src_allocate_channels (src, channels))
429     goto out_of_ports;
430
431   gst_jack_set_layout_on_caps (&spec->caps, channels);
432
433   buffer_size = jack_get_buffer_size (client);
434
435   /* the segment size in bytes, this is large enough to hold a buffer of 32bit floats
436    * for all channels  */
437   spec->segsize = buffer_size * sizeof (gfloat) * channels;
438   spec->latency_time = gst_util_uint64_scale (spec->segsize,
439       (GST_SECOND / GST_USECOND), spec->rate * spec->bytes_per_sample);
440   /* segtotal based on buffer-time latency */
441   spec->segtotal = spec->buffer_time / spec->latency_time;
442   if (spec->segtotal < 2) {
443     spec->segtotal = 2;
444     spec->buffer_time = spec->latency_time * spec->segtotal;
445   }
446
447   GST_DEBUG_OBJECT (src, "buffer time: %" G_GINT64_FORMAT " usec",
448       spec->buffer_time);
449   GST_DEBUG_OBJECT (src, "latency time: %" G_GINT64_FORMAT " usec",
450       spec->latency_time);
451   GST_DEBUG_OBJECT (src, "buffer_size %d, segsize %d, segtotal %d",
452       buffer_size, spec->segsize, spec->segtotal);
453
454   /* allocate the ringbuffer memory now */
455   buf->data = gst_buffer_new_and_alloc (spec->segtotal * spec->segsize);
456   memset (GST_BUFFER_DATA (buf->data), 0, GST_BUFFER_SIZE (buf->data));
457
458   if ((res = gst_jack_audio_client_set_active (src->client, TRUE)))
459     goto could_not_activate;
460
461   /* if we need to automatically connect the ports, do so now. We must do this
462    * after activating the client. */
463   if (src->connect == GST_JACK_CONNECT_AUTO
464       || src->connect == GST_JACK_CONNECT_AUTO_FORCED) {
465     /* find all the physical output ports. A physical output port is a port
466      * associated with a hardware device. Someone needs connect to a physical
467      * port in order to capture something. */
468     ports =
469         jack_get_ports (client, NULL, NULL,
470         JackPortIsPhysical | JackPortIsOutput);
471     if (ports == NULL) {
472       /* no ports? fine then we don't do anything except for posting a warning
473        * message. */
474       GST_ELEMENT_WARNING (src, RESOURCE, NOT_FOUND, (NULL),
475           ("No physical output ports found, leaving ports unconnected"));
476       goto done;
477     }
478
479     for (i = 0; i < channels; i++) {
480       /* stop when all output ports are exhausted */
481       if (ports[i] == NULL) {
482         /* post a warning that we could not connect all ports */
483         GST_ELEMENT_WARNING (src, RESOURCE, NOT_FOUND, (NULL),
484             ("No more physical ports, leaving some ports unconnected"));
485         break;
486       }
487       GST_DEBUG_OBJECT (src, "try connecting to %s",
488           jack_port_name (src->ports[i]));
489
490       /* connect the physical port to a port */
491       res = jack_connect (client, ports[i], jack_port_name (src->ports[i]));
492       if (res != 0 && res != EEXIST)
493         goto cannot_connect;
494     }
495     free (ports);
496   }
497 done:
498
499   abuf->sample_rate = sample_rate;
500   abuf->buffer_size = buffer_size;
501   abuf->channels = spec->channels;
502
503   return TRUE;
504
505   /* ERRORS */
506 wrong_samplerate:
507   {
508     GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
509         ("Wrong samplerate, server is running at %d and we received %d",
510             sample_rate, spec->rate));
511     return FALSE;
512   }
513 out_of_ports:
514   {
515     GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
516         ("Cannot allocate more Jack ports"));
517     return FALSE;
518   }
519 could_not_activate:
520   {
521     GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
522         ("Could not activate client (%d:%s)", res, g_strerror (res)));
523     return FALSE;
524   }
525 cannot_connect:
526   {
527     GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
528         ("Could not connect input ports to physical ports (%d:%s)",
529             res, g_strerror (res)));
530     free (ports);
531     return FALSE;
532   }
533 }
534
535 /* function is called with LOCK */
536 static gboolean
537 gst_jack_ring_buffer_release (GstRingBuffer * buf)
538 {
539   GstJackAudioSrc *src;
540   GstJackRingBuffer *abuf;
541   gint res;
542
543   abuf = GST_JACK_RING_BUFFER_CAST (buf);
544   src = GST_JACK_AUDIO_SRC (GST_OBJECT_PARENT (buf));
545
546   GST_DEBUG_OBJECT (src, "release");
547
548   if ((res = gst_jack_audio_client_set_active (src->client, FALSE))) {
549     /* we only warn, this means the server is probably shut down and the client
550      * is gone anyway. */
551     GST_ELEMENT_WARNING (src, RESOURCE, CLOSE, (NULL),
552         ("Could not deactivate Jack client (%d)", res));
553   }
554
555   abuf->channels = -1;
556   abuf->buffer_size = -1;
557   abuf->sample_rate = -1;
558
559   /* free the buffer */
560   gst_buffer_unref (buf->data);
561   buf->data = NULL;
562
563   return TRUE;
564 }
565
566 static gboolean
567 gst_jack_ring_buffer_start (GstRingBuffer * buf)
568 {
569   GstJackAudioSrc *src;
570
571   src = GST_JACK_AUDIO_SRC (GST_OBJECT_PARENT (buf));
572
573   GST_DEBUG_OBJECT (src, "start");
574
575   return TRUE;
576 }
577
578 static gboolean
579 gst_jack_ring_buffer_pause (GstRingBuffer * buf)
580 {
581   GstJackAudioSrc *src;
582
583   src = GST_JACK_AUDIO_SRC (GST_OBJECT_PARENT (buf));
584
585   GST_DEBUG_OBJECT (src, "pause");
586
587   return TRUE;
588 }
589
590 static gboolean
591 gst_jack_ring_buffer_stop (GstRingBuffer * buf)
592 {
593   GstJackAudioSrc *src;
594
595   src = GST_JACK_AUDIO_SRC (GST_OBJECT_PARENT (buf));
596
597   GST_DEBUG_OBJECT (src, "stop");
598
599   return TRUE;
600 }
601
602 #if defined (HAVE_JACK_0_120_1) || defined(HAVE_JACK_1_9_7)
603 static guint
604 gst_jack_ring_buffer_delay (GstRingBuffer * buf)
605 {
606   GstJackAudioSrc *src;
607   guint i, res = 0;
608   jack_latency_range_t range;
609
610   src = GST_JACK_AUDIO_SRC (GST_OBJECT_PARENT (buf));
611
612   for (i = 0; i < src->port_count; i++) {
613     jack_port_get_latency_range (src->ports[i], JackCaptureLatency, &range);
614     if (range.max > res)
615       res = range.max;
616   }
617
618   GST_DEBUG_OBJECT (src, "delay %u", res);
619
620   return res;
621 }
622 #else /* !(defined (HAVE_JACK_0_120_1) || defined(HAVE_JACK_1_9_7)) */
623 static guint
624 gst_jack_ring_buffer_delay (GstRingBuffer * buf)
625 {
626   GstJackAudioSrc *src;
627   guint i, res = 0;
628   guint latency;
629   jack_client_t *client;
630
631   src = GST_JACK_AUDIO_SRC (GST_OBJECT_PARENT (buf));
632
633   client = gst_jack_audio_client_get_client (src->client);
634
635   for (i = 0; i < src->port_count; i++) {
636     latency = jack_port_get_total_latency (client, src->ports[i]);
637     if (latency > res)
638       res = latency;
639   }
640
641   GST_DEBUG_OBJECT (src, "delay %u", res);
642
643   return res;
644 }
645 #endif
646
647 /* Audiosrc signals and args */
648 enum
649 {
650   /* FILL ME */
651   LAST_SIGNAL
652 };
653
654 #define DEFAULT_PROP_CONNECT            GST_JACK_CONNECT_AUTO
655 #define DEFAULT_PROP_SERVER             NULL
656 #define DEFAULT_PROP_CLIENT_NAME        NULL
657
658 enum
659 {
660   PROP_0,
661   PROP_CONNECT,
662   PROP_SERVER,
663   PROP_CLIENT,
664   PROP_CLIENT_NAME,
665   PROP_LAST
666 };
667
668
669 /* the capabilities of the inputs and outputs.
670  *
671  * describe the real formats here.
672  */
673
674 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
675     GST_PAD_SRC,
676     GST_PAD_ALWAYS,
677     GST_STATIC_CAPS ("audio/x-raw-float, "
678         "endianness = (int) BYTE_ORDER, "
679         "width = (int) 32, "
680         "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, MAX ]")
681     );
682
683 #define _do_init(bla) \
684   GST_DEBUG_CATEGORY_INIT(gst_jack_audio_src_debug, "jacksrc", 0, "jacksrc element");
685
686 GST_BOILERPLATE_FULL (GstJackAudioSrc, gst_jack_audio_src, GstBaseAudioSrc,
687     GST_TYPE_BASE_AUDIO_SRC, _do_init);
688
689 static void gst_jack_audio_src_dispose (GObject * object);
690 static void gst_jack_audio_src_set_property (GObject * object, guint prop_id,
691     const GValue * value, GParamSpec * pspec);
692 static void gst_jack_audio_src_get_property (GObject * object, guint prop_id,
693     GValue * value, GParamSpec * pspec);
694
695 static GstCaps *gst_jack_audio_src_getcaps (GstBaseSrc * bsrc);
696 static GstRingBuffer *gst_jack_audio_src_create_ringbuffer (GstBaseAudioSrc *
697     src);
698
699 /* GObject vmethod implementations */
700
701 static void
702 gst_jack_audio_src_base_init (gpointer gclass)
703 {
704   GstElementClass *element_class = GST_ELEMENT_CLASS (gclass);
705
706   gst_element_class_add_static_pad_template (element_class, &src_factory);
707   gst_element_class_set_details_simple (element_class, "Audio Source (Jack)",
708       "Source/Audio", "Captures audio from a JACK server",
709       "Tristan Matthews <tristan@sat.qc.ca>");
710 }
711
712 /* initialize the jack_audio_src's class */
713 static void
714 gst_jack_audio_src_class_init (GstJackAudioSrcClass * klass)
715 {
716   GObjectClass *gobject_class;
717   GstBaseSrcClass *gstbasesrc_class;
718   GstBaseAudioSrcClass *gstbaseaudiosrc_class;
719
720   gobject_class = (GObjectClass *) klass;
721
722   gstbasesrc_class = (GstBaseSrcClass *) klass;
723   gstbaseaudiosrc_class = (GstBaseAudioSrcClass *) klass;
724
725   gobject_class->dispose = gst_jack_audio_src_dispose;
726   gobject_class->set_property = gst_jack_audio_src_set_property;
727   gobject_class->get_property = gst_jack_audio_src_get_property;
728
729   g_object_class_install_property (gobject_class, PROP_CONNECT,
730       g_param_spec_enum ("connect", "Connect",
731           "Specify how the input ports will be connected",
732           GST_TYPE_JACK_CONNECT, DEFAULT_PROP_CONNECT,
733           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
734
735   g_object_class_install_property (gobject_class, PROP_SERVER,
736       g_param_spec_string ("server", "Server",
737           "The Jack server to connect to (NULL = default)",
738           DEFAULT_PROP_SERVER, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
739
740   /**
741    * GstJackAudioSrc:client-name
742    *
743    * The client name to use.
744    *
745    * Since: 0.10.31
746    */
747   g_object_class_install_property (gobject_class, PROP_CLIENT_NAME,
748       g_param_spec_string ("client-name", "Client name",
749           "The client name of the Jack instance (NULL = default)",
750           DEFAULT_PROP_CLIENT_NAME,
751           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
752
753   g_object_class_install_property (gobject_class, PROP_CLIENT,
754       g_param_spec_boxed ("client", "JackClient", "Handle for jack client",
755           GST_TYPE_JACK_CLIENT,
756           GST_PARAM_MUTABLE_READY | G_PARAM_READWRITE |
757           G_PARAM_STATIC_STRINGS));
758
759   gstbasesrc_class->get_caps = GST_DEBUG_FUNCPTR (gst_jack_audio_src_getcaps);
760   gstbaseaudiosrc_class->create_ringbuffer =
761       GST_DEBUG_FUNCPTR (gst_jack_audio_src_create_ringbuffer);
762
763   /* ref class from a thread-safe context to work around missing bit of
764    * thread-safety in GObject */
765   g_type_class_ref (GST_TYPE_JACK_RING_BUFFER);
766
767   gst_jack_audio_client_init ();
768 }
769
770 /* initialize the new element
771  * instantiate pads and add them to element
772  * set pad calback functions
773  * initialize instance structure
774  */
775 static void
776 gst_jack_audio_src_init (GstJackAudioSrc * src, GstJackAudioSrcClass * gclass)
777 {
778   //gst_base_src_set_live(GST_BASE_SRC (src), TRUE);
779   src->connect = DEFAULT_PROP_CONNECT;
780   src->server = g_strdup (DEFAULT_PROP_SERVER);
781   src->jclient = NULL;
782   src->ports = NULL;
783   src->port_count = 0;
784   src->buffers = NULL;
785   src->client_name = g_strdup (DEFAULT_PROP_CLIENT_NAME);
786 }
787
788 static void
789 gst_jack_audio_src_dispose (GObject * object)
790 {
791   GstJackAudioSrc *src = GST_JACK_AUDIO_SRC (object);
792
793   gst_caps_replace (&src->caps, NULL);
794
795   if (src->client_name != NULL) {
796     g_free (src->client_name);
797     src->client_name = NULL;
798   }
799
800   G_OBJECT_CLASS (parent_class)->dispose (object);
801 }
802
803 static void
804 gst_jack_audio_src_set_property (GObject * object, guint prop_id,
805     const GValue * value, GParamSpec * pspec)
806 {
807   GstJackAudioSrc *src = GST_JACK_AUDIO_SRC (object);
808
809   switch (prop_id) {
810     case PROP_CLIENT_NAME:
811       g_free (src->client_name);
812       src->client_name = g_value_dup_string (value);
813       break;
814     case PROP_CONNECT:
815       src->connect = g_value_get_enum (value);
816       break;
817     case PROP_SERVER:
818       g_free (src->server);
819       src->server = g_value_dup_string (value);
820       break;
821     case PROP_CLIENT:
822       if (GST_STATE (src) == GST_STATE_NULL ||
823           GST_STATE (src) == GST_STATE_READY) {
824         src->jclient = g_value_get_boxed (value);
825       }
826       break;
827     default:
828       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
829       break;
830   }
831 }
832
833 static void
834 gst_jack_audio_src_get_property (GObject * object, guint prop_id,
835     GValue * value, GParamSpec * pspec)
836 {
837   GstJackAudioSrc *src = GST_JACK_AUDIO_SRC (object);
838
839   switch (prop_id) {
840     case PROP_CLIENT_NAME:
841       g_value_set_string (value, src->client_name);
842       break;
843     case PROP_CONNECT:
844       g_value_set_enum (value, src->connect);
845       break;
846     case PROP_SERVER:
847       g_value_set_string (value, src->server);
848       break;
849     case PROP_CLIENT:
850       g_value_set_boxed (value, src->jclient);
851       break;
852     default:
853       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
854       break;
855   }
856 }
857
858 static GstCaps *
859 gst_jack_audio_src_getcaps (GstBaseSrc * bsrc)
860 {
861   GstJackAudioSrc *src = GST_JACK_AUDIO_SRC (bsrc);
862   const char **ports;
863   gint min, max;
864   gint rate;
865   jack_client_t *client;
866
867   if (src->client == NULL)
868     goto no_client;
869
870   client = gst_jack_audio_client_get_client (src->client);
871
872   if (src->connect == GST_JACK_CONNECT_AUTO) {
873     /* get a port count, this is the number of channels we can automatically
874      * connect. */
875     ports = jack_get_ports (client, NULL, NULL,
876         JackPortIsPhysical | JackPortIsOutput);
877     max = 0;
878     if (ports != NULL) {
879       for (; ports[max]; max++);
880
881       free (ports);
882     } else
883       max = 0;
884   } else {
885     /* we allow any number of pads, something else is going to connect the
886      * pads. */
887     max = G_MAXINT;
888   }
889   min = MIN (1, max);
890
891   rate = jack_get_sample_rate (client);
892
893   GST_DEBUG_OBJECT (src, "got %d-%d ports, samplerate: %d", min, max, rate);
894
895   if (!src->caps) {
896     src->caps = gst_caps_new_simple ("audio/x-raw-float",
897         "endianness", G_TYPE_INT, G_BYTE_ORDER,
898         "width", G_TYPE_INT, 32,
899         "rate", G_TYPE_INT, rate,
900         "channels", GST_TYPE_INT_RANGE, min, max, NULL);
901   }
902   GST_INFO_OBJECT (src, "returning caps %" GST_PTR_FORMAT, src->caps);
903
904   return gst_caps_ref (src->caps);
905
906   /* ERRORS */
907 no_client:
908   {
909     GST_DEBUG_OBJECT (src, "device not open, using template caps");
910     /* base class will get template caps for us when we return NULL */
911     return NULL;
912   }
913 }
914
915 static GstRingBuffer *
916 gst_jack_audio_src_create_ringbuffer (GstBaseAudioSrc * src)
917 {
918   GstRingBuffer *buffer;
919
920   buffer = g_object_new (GST_TYPE_JACK_RING_BUFFER, NULL);
921   GST_DEBUG_OBJECT (src, "created ringbuffer @%p", buffer);
922
923   return buffer;
924 }