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