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., 51 Franklin St, Fifth Floor,
40 * Boston, MA 02110-1301, USA.
41 */
42
43 /**
44 * SECTION:element-jackaudiosrc
45 * @title: jackaudiosrc
46 * @see_also: #GstAudioBaseSrc, #GstAudioRingBuffer
47 *
48 * A Src that inputs data from Jack ports.
49 *
50 * It will create N Jack ports named in_<name>_<num> where
51 * <name> is the element name and <num> is starting from 1.
52 * Each port corresponds to a gstreamer channel.
53 *
54 * The samplerate as exposed on the caps is always the same as the samplerate of
55 * the jack server.
56 *
57 * When the #GstJackAudioSrc:connect property is set to auto, this element
58 * will try to connect each input port to a random physical jack output pin.
59 *
60 * When the #GstJackAudioSrc:connect property is set to none, the element will
61 * accept any number of output channels and will create (but not connect) an
62 * input port for each channel.
63 *
64 * The element will generate an error when the Jack server is shut down when it
65 * was PAUSED or PLAYING. This element does not support dynamic rate and buffer
66 * size changes at runtime.
67 *
68 * ## Example launch line
69 * |[
70 * gst-launch-1.0 jackaudiosrc connect=0 ! jackaudiosink connect=0
71 * ]| Get audio input into gstreamer from jack.
72 *
73 */
74
75 #ifdef HAVE_CONFIG_H
76 #include "config.h"
77 #endif
78
79 #include <gst/gst-i18n-plugin.h>
80 #include <stdlib.h>
81 #include <string.h>
82
83 #include <gst/audio/audio.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
gst_jack_audio_src_allocate_channels(GstJackAudioSrc * src,gint channels)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
gst_jack_audio_src_free_channels(GstJackAudioSrc * src)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
gst_jack_ring_buffer_get_type(void)152 gst_jack_ring_buffer_get_type (void)
153 {
154 static 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_AUDIO_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
gst_jack_ring_buffer_class_init(GstJackRingBufferClass * klass)177 gst_jack_ring_buffer_class_init (GstJackRingBufferClass * klass)
178 {
179 GstAudioRingBufferClass *gstringbuffer_class;
180
181 gstringbuffer_class = (GstAudioRingBufferClass *) 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
jack_process_cb(jack_nframes_t nframes,void * arg)205 jack_process_cb (jack_nframes_t nframes, void *arg)
206 {
207 GstJackAudioSrc *src;
208 GstAudioRingBuffer *buf;
209 gint len;
210 guint8 *writeptr;
211 gint writeseg;
212 gint channels, i, j, flen;
213 sample_t *data;
214
215 buf = GST_AUDIO_RING_BUFFER_CAST (arg);
216 src = GST_JACK_AUDIO_SRC (GST_OBJECT_PARENT (buf));
217
218 channels = GST_AUDIO_INFO_CHANNELS (&buf->spec.info);
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_audio_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_audio_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
jack_sample_rate_cb(jack_nframes_t nframes,void * arg)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
jack_buffer_size_cb(jack_nframes_t nframes,void * arg)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
jack_shutdown_cb(void * arg)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
gst_jack_ring_buffer_init(GstJackRingBuffer * buf,GstJackRingBufferClass * g_class)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
gst_jack_ring_buffer_open_device(GstAudioRingBuffer * buf)329 gst_jack_ring_buffer_open_device (GstAudioRingBuffer * 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
gst_jack_ring_buffer_close_device(GstAudioRingBuffer * buf)377 gst_jack_ring_buffer_close_device (GstAudioRingBuffer * 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
gst_jack_ring_buffer_acquire(GstAudioRingBuffer * buf,GstAudioRingBufferSpec * spec)405 gst_jack_ring_buffer_acquire (GstAudioRingBuffer * buf,
406 GstAudioRingBufferSpec * spec)
407 {
408 GstJackAudioSrc *src;
409 GstJackRingBuffer *abuf;
410 gint sample_rate, buffer_size;
411 gint i, bpf, rate, 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 rate = GST_AUDIO_INFO_RATE (&spec->info);
422
423 /* sample rate must be that of the server */
424 sample_rate = jack_get_sample_rate (client);
425 if (sample_rate != rate)
426 goto wrong_samplerate;
427
428 bpf = GST_AUDIO_INFO_BPF (&spec->info);
429 channels = GST_AUDIO_INFO_CHANNELS (&spec->info);
430
431 if (!gst_jack_audio_src_allocate_channels (src, channels))
432 goto out_of_ports;
433
434 gst_jack_set_layout (buf, spec);
435
436 buffer_size = jack_get_buffer_size (client);
437
438 /* the segment size in bytes, this is large enough to hold a buffer of 32bit floats
439 * for all channels */
440 spec->segsize = buffer_size * sizeof (gfloat) * channels;
441 spec->latency_time = gst_util_uint64_scale (spec->segsize,
442 (GST_SECOND / GST_USECOND), rate * bpf);
443 /* segtotal based on buffer-time latency */
444 spec->segtotal = spec->buffer_time / spec->latency_time;
445
446 /* Use small period when low-latency is enabled regardless of buffer-time */
447 if (spec->segtotal < 2 || src->low_latency) {
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->size = spec->segtotal * spec->segsize;
461 buf->memory = g_malloc0 (buf->size);
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 || src->connect == GST_JACK_CONNECT_EXPLICIT) {
471 const char **available_ports = NULL;
472 const char **jack_ports = NULL;
473 char **user_ports = NULL;
474
475 /* find all the physical output ports. A physical output port is a port
476 * associated with a hardware device. Someone needs connect to a physical
477 * port in order to capture something. */
478
479 if (src->port_names) {
480 user_ports = gst_jack_audio_client_get_port_names_from_string (client,
481 src->port_names, JackPortIsOutput);
482
483 if (user_ports)
484 available_ports = (const char **) user_ports;
485 }
486
487 if (!available_ports && src->connect == GST_JACK_CONNECT_EXPLICIT)
488 goto wrong_port_names;
489
490 if (!available_ports) {
491 if (!src->port_pattern) {
492 jack_ports = jack_get_ports (client, NULL, NULL,
493 JackPortIsPhysical | JackPortIsOutput);
494 } else {
495 jack_ports = jack_get_ports (client, src->port_pattern, NULL,
496 JackPortIsOutput);
497 }
498 }
499
500 if (!available_ports) {
501 /* no ports? fine then we don't do anything except for posting a warning
502 * message. */
503 GST_ELEMENT_WARNING (src, RESOURCE, NOT_FOUND, (NULL),
504 ("No physical output ports found, leaving ports unconnected"));
505 goto done;
506 }
507
508 for (i = 0; i < channels; i++) {
509 /* stop when all output ports are exhausted */
510 if (!available_ports[i]) {
511 /* post a warning that we could not connect all ports */
512 GST_ELEMENT_WARNING (src, RESOURCE, NOT_FOUND, (NULL),
513 ("No more physical ports, leaving some ports unconnected"));
514 break;
515 }
516 GST_DEBUG_OBJECT (src, "try connecting to %s",
517 jack_port_name (src->ports[i]));
518
519 /* connect the physical port to a port */
520 res = jack_connect (client,
521 available_ports[i], jack_port_name (src->ports[i]));
522 if (res != 0 && res != EEXIST) {
523 jack_free (jack_ports);
524 g_strfreev (user_ports);
525
526 goto cannot_connect;
527 }
528 }
529
530 jack_free (jack_ports);
531 g_strfreev (user_ports);
532 }
533 done:
534
535 abuf->sample_rate = sample_rate;
536 abuf->buffer_size = buffer_size;
537 abuf->channels = channels;
538
539 return TRUE;
540
541 /* ERRORS */
542 wrong_samplerate:
543 {
544 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
545 ("Wrong samplerate, server is running at %d and we received %d",
546 sample_rate, rate));
547 return FALSE;
548 }
549 out_of_ports:
550 {
551 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
552 ("Cannot allocate more Jack ports"));
553 return FALSE;
554 }
555 could_not_activate:
556 {
557 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
558 ("Could not activate client (%d:%s)", res, g_strerror (res)));
559 return FALSE;
560 }
561 cannot_connect:
562 {
563 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
564 ("Could not connect input ports to physical ports (%d:%s)",
565 res, g_strerror (res)));
566 return FALSE;
567 }
568 wrong_port_names:
569 {
570 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
571 ("Invalid port-names was provided"));
572 return FALSE;
573 }
574 }
575
576 /* function is called with LOCK */
577 static gboolean
gst_jack_ring_buffer_release(GstAudioRingBuffer * buf)578 gst_jack_ring_buffer_release (GstAudioRingBuffer * buf)
579 {
580 GstJackAudioSrc *src;
581 GstJackRingBuffer *abuf;
582 gint res;
583
584 abuf = GST_JACK_RING_BUFFER_CAST (buf);
585 src = GST_JACK_AUDIO_SRC (GST_OBJECT_PARENT (buf));
586
587 GST_DEBUG_OBJECT (src, "release");
588
589 if ((res = gst_jack_audio_client_set_active (src->client, FALSE))) {
590 /* we only warn, this means the server is probably shut down and the client
591 * is gone anyway. */
592 GST_ELEMENT_WARNING (src, RESOURCE, CLOSE, (NULL),
593 ("Could not deactivate Jack client (%d)", res));
594 }
595
596 abuf->channels = -1;
597 abuf->buffer_size = -1;
598 abuf->sample_rate = -1;
599
600 /* free the buffer */
601 g_free (buf->memory);
602 buf->memory = NULL;
603
604 return TRUE;
605 }
606
607 static gboolean
gst_jack_ring_buffer_start(GstAudioRingBuffer * buf)608 gst_jack_ring_buffer_start (GstAudioRingBuffer * buf)
609 {
610 GstJackAudioSrc *src;
611
612 src = GST_JACK_AUDIO_SRC (GST_OBJECT_PARENT (buf));
613
614 GST_DEBUG_OBJECT (src, "start");
615
616 if (src->transport & GST_JACK_TRANSPORT_MASTER) {
617 jack_client_t *client;
618
619 client = gst_jack_audio_client_get_client (src->client);
620 jack_transport_start (client);
621 }
622
623 return TRUE;
624 }
625
626 static gboolean
gst_jack_ring_buffer_pause(GstAudioRingBuffer * buf)627 gst_jack_ring_buffer_pause (GstAudioRingBuffer * buf)
628 {
629 GstJackAudioSrc *src;
630
631 src = GST_JACK_AUDIO_SRC (GST_OBJECT_PARENT (buf));
632
633 GST_DEBUG_OBJECT (src, "pause");
634
635 if (src->transport & GST_JACK_TRANSPORT_MASTER) {
636 jack_client_t *client;
637
638 client = gst_jack_audio_client_get_client (src->client);
639 jack_transport_stop (client);
640 }
641
642 return TRUE;
643 }
644
645 static gboolean
gst_jack_ring_buffer_stop(GstAudioRingBuffer * buf)646 gst_jack_ring_buffer_stop (GstAudioRingBuffer * buf)
647 {
648 GstJackAudioSrc *src;
649
650 src = GST_JACK_AUDIO_SRC (GST_OBJECT_PARENT (buf));
651
652 GST_DEBUG_OBJECT (src, "stop");
653
654 if (src->transport & GST_JACK_TRANSPORT_MASTER) {
655 jack_client_t *client;
656
657 client = gst_jack_audio_client_get_client (src->client);
658 jack_transport_stop (client);
659 }
660
661 return TRUE;
662 }
663
664 #if defined (HAVE_JACK_0_120_1) || defined(HAVE_JACK_1_9_7)
665 static guint
gst_jack_ring_buffer_delay(GstAudioRingBuffer * buf)666 gst_jack_ring_buffer_delay (GstAudioRingBuffer * buf)
667 {
668 GstJackAudioSrc *src;
669 guint i, res = 0;
670 jack_latency_range_t range;
671
672 src = GST_JACK_AUDIO_SRC (GST_OBJECT_PARENT (buf));
673
674 for (i = 0; i < src->port_count; i++) {
675 jack_port_get_latency_range (src->ports[i], JackCaptureLatency, &range);
676 if (range.max > res)
677 res = range.max;
678 }
679
680 GST_DEBUG_OBJECT (src, "delay %u", res);
681
682 return res;
683 }
684 #else /* !(defined (HAVE_JACK_0_120_1) || defined(HAVE_JACK_1_9_7)) */
685 static guint
gst_jack_ring_buffer_delay(GstAudioRingBuffer * buf)686 gst_jack_ring_buffer_delay (GstAudioRingBuffer * buf)
687 {
688 GstJackAudioSrc *src;
689 guint i, res = 0;
690 guint latency;
691 jack_client_t *client;
692
693 src = GST_JACK_AUDIO_SRC (GST_OBJECT_PARENT (buf));
694
695 client = gst_jack_audio_client_get_client (src->client);
696
697 for (i = 0; i < src->port_count; i++) {
698 latency = jack_port_get_total_latency (client, src->ports[i]);
699 if (latency > res)
700 res = latency;
701 }
702
703 GST_DEBUG_OBJECT (src, "delay %u", res);
704
705 return res;
706 }
707 #endif
708
709 /* Audiosrc signals and args */
710 enum
711 {
712 /* FILL ME */
713 LAST_SIGNAL
714 };
715
716 #define DEFAULT_PROP_CONNECT GST_JACK_CONNECT_AUTO
717 #define DEFAULT_PROP_SERVER NULL
718 #define DEFAULT_PROP_CLIENT_NAME NULL
719 #define DEFAULT_PROP_TRANSPORT GST_JACK_TRANSPORT_AUTONOMOUS
720 #define DEFAULT_PROP_PORT_PATTERN NULL
721 #define DEFAULT_PROP_LOW_LATENCY FALSE
722
723 enum
724 {
725 PROP_0,
726 PROP_CONNECT,
727 PROP_SERVER,
728 PROP_CLIENT,
729 PROP_CLIENT_NAME,
730 PROP_PORT_PATTERN,
731 PROP_TRANSPORT,
732 PROP_LOW_LATENCY,
733 PROP_PORT_NAMES,
734 PROP_LAST
735 };
736
737 /* the capabilities of the inputs and outputs.
738 *
739 * describe the real formats here.
740 */
741
742 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
743 GST_PAD_SRC,
744 GST_PAD_ALWAYS,
745 GST_STATIC_CAPS ("audio/x-raw, "
746 "format = (string) " GST_JACK_FORMAT_STR ", "
747 "layout = (string) interleaved, "
748 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, MAX ]")
749 );
750
751 #define gst_jack_audio_src_parent_class parent_class
752 G_DEFINE_TYPE (GstJackAudioSrc, gst_jack_audio_src, GST_TYPE_AUDIO_BASE_SRC);
753 GST_ELEMENT_REGISTER_DEFINE (jackaudiosrc, "jackaudiosrc",
754 GST_RANK_PRIMARY, GST_TYPE_JACK_AUDIO_SRC);
755
756 static void gst_jack_audio_src_dispose (GObject * object);
757 static void gst_jack_audio_src_set_property (GObject * object, guint prop_id,
758 const GValue * value, GParamSpec * pspec);
759 static void gst_jack_audio_src_get_property (GObject * object, guint prop_id,
760 GValue * value, GParamSpec * pspec);
761
762 static GstCaps *gst_jack_audio_src_getcaps (GstBaseSrc * bsrc,
763 GstCaps * filter);
764 static GstAudioRingBuffer *gst_jack_audio_src_create_ringbuffer (GstAudioBaseSrc
765 * src);
766
767 /* GObject vmethod implementations */
768
769 /* initialize the jack_audio_src's class */
770 static void
gst_jack_audio_src_class_init(GstJackAudioSrcClass * klass)771 gst_jack_audio_src_class_init (GstJackAudioSrcClass * klass)
772 {
773 GObjectClass *gobject_class;
774 GstElementClass *gstelement_class;
775 GstBaseSrcClass *gstbasesrc_class;
776 GstAudioBaseSrcClass *gstaudiobasesrc_class;
777
778 GST_DEBUG_CATEGORY_INIT (gst_jack_audio_src_debug, "jacksrc", 0,
779 "jacksrc element");
780
781 gobject_class = (GObjectClass *) klass;
782 gstelement_class = (GstElementClass *) klass;
783 gstbasesrc_class = (GstBaseSrcClass *) klass;
784 gstaudiobasesrc_class = (GstAudioBaseSrcClass *) klass;
785
786 gobject_class->dispose = gst_jack_audio_src_dispose;
787 gobject_class->set_property = gst_jack_audio_src_set_property;
788 gobject_class->get_property = gst_jack_audio_src_get_property;
789
790 g_object_class_install_property (gobject_class, PROP_CONNECT,
791 g_param_spec_enum ("connect", "Connect",
792 "Specify how the input ports will be connected",
793 GST_TYPE_JACK_CONNECT, DEFAULT_PROP_CONNECT,
794 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
795
796 g_object_class_install_property (gobject_class, PROP_SERVER,
797 g_param_spec_string ("server", "Server",
798 "The Jack server to connect to (NULL = default)",
799 DEFAULT_PROP_SERVER, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
800
801 /**
802 * GstJackAudioSrc:client-name:
803 *
804 * The client name to use.
805 */
806 g_object_class_install_property (gobject_class, PROP_CLIENT_NAME,
807 g_param_spec_string ("client-name", "Client name",
808 "The client name of the Jack instance (NULL = default)",
809 DEFAULT_PROP_CLIENT_NAME,
810 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
811
812 g_object_class_install_property (gobject_class, PROP_CLIENT,
813 g_param_spec_boxed ("client", "JackClient", "Handle for jack client",
814 GST_TYPE_JACK_CLIENT,
815 GST_PARAM_MUTABLE_READY | G_PARAM_READWRITE |
816 G_PARAM_STATIC_STRINGS));
817 /**
818 * GstJackAudioSrc:port-pattern
819 *
820 * autoconnect to ports matching pattern, when NULL connect to physical ports
821 *
822 * Since: 1.6
823 */
824 g_object_class_install_property (gobject_class, PROP_PORT_PATTERN,
825 g_param_spec_string ("port-pattern", "port pattern",
826 "A pattern to select which ports to connect to (NULL = first physical ports)",
827 DEFAULT_PROP_PORT_PATTERN,
828 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
829
830 /**
831 * GstJackAudioSink:transport:
832 *
833 * The jack transport behaviour for the client.
834 */
835 g_object_class_install_property (gobject_class, PROP_TRANSPORT,
836 g_param_spec_flags ("transport", "Transport mode",
837 "Jack transport behaviour of the client",
838 GST_TYPE_JACK_TRANSPORT, DEFAULT_PROP_TRANSPORT,
839 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
840
841 /**
842 * GstJackAudioSrc:low-latency:
843 *
844 * Optimize all settings for lowest latency. When enabled,
845 * #GstAudioBaseSrc:buffer-time and #GstAudioBaseSrc:latency-time will be
846 * ignored.
847 *
848 * Since: 1.20
849 */
850 g_object_class_install_property (gobject_class, PROP_LOW_LATENCY,
851 g_param_spec_boolean ("low-latency", "Low latency",
852 "Optimize all settings for lowest latency. When enabled, "
853 "\"buffer-time\" and \"latency-time\" will be ignored",
854 DEFAULT_PROP_LOW_LATENCY,
855 GST_PARAM_MUTABLE_READY | G_PARAM_READWRITE |
856 G_PARAM_STATIC_STRINGS));
857
858 /**
859 * GstJackAudioSrc:port-names:
860 *
861 * Comma-separated list of port name including "client_name:" prefix
862 *
863 * Since: 1.20
864 */
865 g_object_class_install_property (gobject_class, PROP_PORT_NAMES,
866 g_param_spec_string ("port-names", "Port Names",
867 "Comma-separated list of port name including \"client_name:\" prefix",
868 NULL, GST_PARAM_MUTABLE_READY | G_PARAM_READWRITE |
869 G_PARAM_STATIC_STRINGS));
870
871 gst_element_class_add_static_pad_template (gstelement_class, &src_factory);
872
873 gst_element_class_set_static_metadata (gstelement_class,
874 "Audio Source (Jack)", "Source/Audio",
875 "Captures audio from a JACK server",
876 "Tristan Matthews <tristan@sat.qc.ca>");
877
878 gstbasesrc_class->get_caps = GST_DEBUG_FUNCPTR (gst_jack_audio_src_getcaps);
879 gstaudiobasesrc_class->create_ringbuffer =
880 GST_DEBUG_FUNCPTR (gst_jack_audio_src_create_ringbuffer);
881
882 /* ref class from a thread-safe context to work around missing bit of
883 * thread-safety in GObject */
884 g_type_class_ref (GST_TYPE_JACK_RING_BUFFER);
885
886 gst_jack_audio_client_init ();
887 }
888
889 static void
gst_jack_audio_src_init(GstJackAudioSrc * src)890 gst_jack_audio_src_init (GstJackAudioSrc * src)
891 {
892 //gst_base_src_set_live(GST_BASE_SRC (src), TRUE);
893 src->connect = DEFAULT_PROP_CONNECT;
894 src->server = g_strdup (DEFAULT_PROP_SERVER);
895 src->jclient = NULL;
896 src->ports = NULL;
897 src->port_count = 0;
898 src->buffers = NULL;
899 src->client_name = g_strdup (DEFAULT_PROP_CLIENT_NAME);
900 src->transport = DEFAULT_PROP_TRANSPORT;
901 src->low_latency = DEFAULT_PROP_LOW_LATENCY;
902 }
903
904 static void
gst_jack_audio_src_dispose(GObject * object)905 gst_jack_audio_src_dispose (GObject * object)
906 {
907 GstJackAudioSrc *src = GST_JACK_AUDIO_SRC (object);
908
909 gst_caps_replace (&src->caps, NULL);
910
911 if (src->client_name != NULL) {
912 g_free (src->client_name);
913 src->client_name = NULL;
914 }
915
916 if (src->port_pattern != NULL) {
917 g_free (src->port_pattern);
918 src->port_pattern = NULL;
919 }
920
921 g_clear_pointer (&src->port_names, g_free);
922
923 G_OBJECT_CLASS (parent_class)->dispose (object);
924 }
925
926 static void
gst_jack_audio_src_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)927 gst_jack_audio_src_set_property (GObject * object, guint prop_id,
928 const GValue * value, GParamSpec * pspec)
929 {
930 GstJackAudioSrc *src = GST_JACK_AUDIO_SRC (object);
931
932 switch (prop_id) {
933 case PROP_CLIENT_NAME:
934 g_free (src->client_name);
935 src->client_name = g_value_dup_string (value);
936 break;
937 case PROP_PORT_PATTERN:
938 g_free (src->port_pattern);
939 src->port_pattern = g_value_dup_string (value);
940 break;
941 case PROP_CONNECT:
942 src->connect = g_value_get_enum (value);
943 break;
944 case PROP_SERVER:
945 g_free (src->server);
946 src->server = g_value_dup_string (value);
947 break;
948 case PROP_CLIENT:
949 if (GST_STATE (src) == GST_STATE_NULL ||
950 GST_STATE (src) == GST_STATE_READY) {
951 src->jclient = g_value_get_boxed (value);
952 }
953 break;
954 case PROP_TRANSPORT:
955 src->transport = g_value_get_flags (value);
956 break;
957 case PROP_LOW_LATENCY:
958 src->low_latency = g_value_get_boolean (value);
959 break;
960 case PROP_PORT_NAMES:
961 g_free (src->port_names);
962 src->port_names = g_value_dup_string (value);
963 break;
964 default:
965 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
966 break;
967 }
968 }
969
970 static void
gst_jack_audio_src_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)971 gst_jack_audio_src_get_property (GObject * object, guint prop_id,
972 GValue * value, GParamSpec * pspec)
973 {
974 GstJackAudioSrc *src = GST_JACK_AUDIO_SRC (object);
975
976 switch (prop_id) {
977 case PROP_CLIENT_NAME:
978 g_value_set_string (value, src->client_name);
979 break;
980 case PROP_PORT_PATTERN:
981 g_value_set_string (value, src->port_pattern);
982 break;
983 case PROP_CONNECT:
984 g_value_set_enum (value, src->connect);
985 break;
986 case PROP_SERVER:
987 g_value_set_string (value, src->server);
988 break;
989 case PROP_CLIENT:
990 g_value_set_boxed (value, src->jclient);
991 break;
992 case PROP_TRANSPORT:
993 g_value_set_flags (value, src->transport);
994 break;
995 case PROP_LOW_LATENCY:
996 g_value_set_boolean (value, src->low_latency);
997 break;
998 case PROP_PORT_NAMES:
999 g_value_set_string (value, src->port_names);
1000 break;
1001 default:
1002 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1003 break;
1004 }
1005 }
1006
1007 static GstCaps *
gst_jack_audio_src_getcaps(GstBaseSrc * bsrc,GstCaps * filter)1008 gst_jack_audio_src_getcaps (GstBaseSrc * bsrc, GstCaps * filter)
1009 {
1010 GstJackAudioSrc *src = GST_JACK_AUDIO_SRC (bsrc);
1011 const char **ports;
1012 gint min, max;
1013 gint rate;
1014 jack_client_t *client;
1015
1016 if (src->client == NULL)
1017 goto no_client;
1018
1019 if (src->connect == GST_JACK_CONNECT_EXPLICIT && !src->port_names)
1020 goto no_port_names;
1021
1022 client = gst_jack_audio_client_get_client (src->client);
1023
1024 if (src->connect == GST_JACK_CONNECT_AUTO ||
1025 src->connect == GST_JACK_CONNECT_EXPLICIT) {
1026 max = 0;
1027
1028 if (src->port_names) {
1029 gchar **user_ports =
1030 gst_jack_audio_client_get_port_names_from_string (client,
1031 src->port_names, JackPortIsOutput);
1032
1033 if (user_ports) {
1034 max = g_strv_length (user_ports);
1035 } else {
1036 GST_ELEMENT_WARNING (src, RESOURCE, NOT_FOUND,
1037 ("Invalid \"port-names\" was requested"),
1038 ("Requested \"port-names\" %s contains invalid name",
1039 src->port_names));
1040 }
1041
1042 g_strfreev (user_ports);
1043 }
1044
1045 if (max > 0)
1046 goto found;
1047
1048 if (src->connect == GST_JACK_CONNECT_EXPLICIT)
1049 goto no_port_names;
1050
1051 /* get a port count, this is the number of channels we can automatically
1052 * connect. */
1053 ports = jack_get_ports (client, NULL, NULL,
1054 JackPortIsPhysical | JackPortIsOutput);
1055 if (ports != NULL) {
1056 for (; ports[max]; max++);
1057
1058 jack_free (ports);
1059 } else
1060 max = 0;
1061 } else {
1062 /* we allow any number of pads, something else is going to connect the
1063 * pads. */
1064 max = G_MAXINT;
1065 }
1066
1067 found:
1068 if (src->connect == GST_JACK_CONNECT_EXPLICIT) {
1069 min = max;
1070 } else {
1071 min = MIN (1, max);
1072 }
1073
1074 rate = jack_get_sample_rate (client);
1075
1076 GST_DEBUG_OBJECT (src, "got %d-%d ports, samplerate: %d", min, max, rate);
1077
1078 if (!src->caps) {
1079 src->caps = gst_caps_new_simple ("audio/x-raw",
1080 "format", G_TYPE_STRING, GST_JACK_FORMAT_STR,
1081 "layout", G_TYPE_STRING, "interleaved", "rate", G_TYPE_INT, rate, NULL);
1082 if (min == max) {
1083 gst_caps_set_simple (src->caps, "channels", G_TYPE_INT, min, NULL);
1084 } else {
1085 gst_caps_set_simple (src->caps,
1086 "channels", GST_TYPE_INT_RANGE, min, max, NULL);
1087 }
1088 }
1089 GST_INFO_OBJECT (src, "returning caps %" GST_PTR_FORMAT, src->caps);
1090
1091 return gst_caps_ref (src->caps);
1092
1093 /* ERRORS */
1094 no_client:
1095 {
1096 GST_DEBUG_OBJECT (src, "device not open, using template caps");
1097 /* base class will get template caps for us when we return NULL */
1098 return NULL;
1099 }
1100 no_port_names:
1101 {
1102 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS,
1103 ("User must provide valid port names"),
1104 ("\"port-names\" contains invalid name or NULL string"));
1105 return NULL;
1106 }
1107 }
1108
1109 static GstAudioRingBuffer *
gst_jack_audio_src_create_ringbuffer(GstAudioBaseSrc * src)1110 gst_jack_audio_src_create_ringbuffer (GstAudioBaseSrc * src)
1111 {
1112 GstAudioRingBuffer *buffer;
1113
1114 buffer = g_object_new (GST_TYPE_JACK_RING_BUFFER, NULL);
1115 GST_DEBUG_OBJECT (src, "created ringbuffer @%p", buffer);
1116
1117 return buffer;
1118 }
1119