2 * Copyright (C) 2006 Wim Taymans <wim@fluendo.com>
4 * gstjackaudioclient.c: jack audio client implementation
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
24 #include "gstjackaudioclient.h"
26 GST_DEBUG_CATEGORY_STATIC (gst_jack_audio_client_debug);
27 #define GST_CAT_DEFAULT gst_jack_audio_client_debug
30 gst_jack_audio_client_init (void)
32 GST_DEBUG_CATEGORY_INIT (gst_jack_audio_client_debug, "jackclient", 0,
33 "jackclient helpers");
36 /* a list of global connections indexed by id and server. */
37 G_LOCK_DEFINE_STATIC (connections_lock);
38 static GList *connections;
40 /* the connection to a server */
47 /* id/server pair and the connection */
50 jack_client_t *client;
52 /* lists of GstJackAudioClients */
56 } GstJackAudioConnection;
58 /* an object sharing a jack_client_t connection. */
59 struct _GstJackAudioClient
61 GstJackAudioConnection *conn;
63 GstJackClientType type;
67 void (*shutdown) (void *arg);
68 JackProcessCallback process;
69 JackBufferSizeCallback buffer_size;
70 JackSampleRateCallback sample_rate;
74 typedef jack_default_audio_sample_t sample_t;
78 jack_nframes_t nframes;
83 jack_process_cb (jack_nframes_t nframes, void *arg)
85 GstJackAudioConnection *conn = (GstJackAudioConnection *) arg;
89 g_mutex_lock (conn->lock);
90 /* call sources first, then sinks. Sources will either push data into the
91 * ringbuffer of the sinks, which will then pull the data out of it, or
92 * sinks will pull the data from the sources. */
93 for (walk = conn->src_clients; walk; walk = g_list_next (walk)) {
94 GstJackAudioClient *client = (GstJackAudioClient *) walk->data;
96 /* only call active clients */
97 if ((client->active || client->deactivate) && client->process) {
98 res = client->process (nframes, client->user_data);
99 if (client->deactivate) {
100 client->deactivate = FALSE;
101 g_cond_signal (conn->flush_cond);
105 for (walk = conn->sink_clients; walk; walk = g_list_next (walk)) {
106 GstJackAudioClient *client = (GstJackAudioClient *) walk->data;
108 /* only call active clients */
109 if ((client->active || client->deactivate) && client->process) {
110 res = client->process (nframes, client->user_data);
111 if (client->deactivate) {
112 client->deactivate = FALSE;
113 g_cond_signal (conn->flush_cond);
117 g_mutex_unlock (conn->lock);
124 jack_sample_rate_cb (jack_nframes_t nframes, void *arg)
131 jack_buffer_size_cb (jack_nframes_t nframes, void *arg)
137 jack_shutdown_cb (void *arg)
139 GstJackAudioConnection *conn = (GstJackAudioConnection *) arg;
142 GST_DEBUG ("disconnect client %s from server %s", conn->id,
143 GST_STR_NULL (conn->server));
145 g_mutex_lock (conn->lock);
146 for (walk = conn->src_clients; walk; walk = g_list_next (walk)) {
147 GstJackAudioClient *client = (GstJackAudioClient *) walk->data;
149 if (client->shutdown)
150 client->shutdown (client->user_data);
152 for (walk = conn->sink_clients; walk; walk = g_list_next (walk)) {
153 GstJackAudioClient *client = (GstJackAudioClient *) walk->data;
155 if (client->shutdown)
156 client->shutdown (client->user_data);
158 g_mutex_unlock (conn->lock);
168 connection_find (GstJackAudioConnection * conn, FindData * data)
170 /* id's must match */
171 if (strcmp (conn->id, data->id))
174 /* both the same or NULL */
175 if (conn->server == data->server)
178 /* we cannot compare NULL */
179 if (conn->server == NULL || data->server == NULL)
182 if (strcmp (conn->server, data->server))
188 /* make a connection with @id and @server. Returns NULL on failure with the
190 static GstJackAudioConnection *
191 gst_jack_audio_make_connection (const gchar * id, const gchar * server,
192 jack_client_t * jclient, jack_status_t * status)
194 GstJackAudioConnection *conn;
195 jack_options_t options;
200 GST_DEBUG ("new client %s, connecting to server %s", id,
201 GST_STR_NULL (server));
203 /* never start a server */
204 options = JackNoStartServer;
205 /* if we have a servername, use it */
207 options |= JackServerName;
208 /* open the client */
210 jclient = jack_client_open (id, options, status, server);
214 /* now create object */
215 conn = g_new (GstJackAudioConnection, 1);
217 conn->lock = g_mutex_new ();
218 conn->flush_cond = g_cond_new ();
219 conn->id = g_strdup (id);
220 conn->server = g_strdup (server);
221 conn->client = jclient;
223 conn->src_clients = NULL;
224 conn->sink_clients = NULL;
226 /* set our callbacks */
227 jack_set_process_callback (jclient, jack_process_cb, conn);
228 /* these callbacks cause us to error */
229 jack_set_buffer_size_callback (jclient, jack_buffer_size_cb, conn);
230 jack_set_sample_rate_callback (jclient, jack_sample_rate_cb, conn);
231 jack_on_shutdown (jclient, jack_shutdown_cb, conn);
233 /* all callbacks are set, activate the client */
234 if ((res = jack_activate (jclient)))
235 goto could_not_activate;
237 GST_DEBUG ("opened connection %p", conn);
244 GST_DEBUG ("failed to open jack client, %d", *status);
249 GST_ERROR ("Could not activate client (%d)", res);
250 *status = JackFailure;
251 g_mutex_free (conn->lock);
253 g_free (conn->server);
259 static GstJackAudioConnection *
260 gst_jack_audio_get_connection (const gchar * id, const gchar * server,
261 jack_client_t * jclient, jack_status_t * status)
263 GstJackAudioConnection *conn;
267 GST_DEBUG ("getting connection for id %s, server %s", id,
268 GST_STR_NULL (server));
271 data.server = server;
273 G_LOCK (connections_lock);
275 g_list_find_custom (connections, &data, (GCompareFunc) connection_find);
276 if (found != NULL && jclient != NULL) {
277 /* we found it, increase refcount and return it */
278 conn = (GstJackAudioConnection *) found->data;
281 GST_DEBUG ("found connection %p", conn);
283 /* make new connection */
284 conn = gst_jack_audio_make_connection (id, server, jclient, status);
286 GST_DEBUG ("created connection %p", conn);
287 /* add to list on success */
288 connections = g_list_prepend (connections, conn);
290 GST_WARNING ("could not create connection");
293 G_UNLOCK (connections_lock);
299 gst_jack_audio_unref_connection (GstJackAudioConnection * conn)
304 GST_DEBUG ("unref connection %p refcnt %d", conn, conn->refcount);
306 G_LOCK (connections_lock);
308 if ((zero = (conn->refcount == 0))) {
309 GST_DEBUG ("closing connection %p", conn);
310 /* remove from list, we can release the mutex after removing the connection
311 * from the list because after that, nobody can access the connection anymore. */
312 connections = g_list_remove (connections, conn);
314 G_UNLOCK (connections_lock);
316 /* if we are zero, close and cleanup the connection */
318 /* don't use conn->lock here. two reasons:
320 * 1) its not necessary: jack_deactivate() will not return until the JACK thread
321 * associated with this connection is cleaned up by a thread join, hence
322 * no more callbacks can occur or be in progress.
324 * 2) it would deadlock anyway, because jack_deactivate() will sleep
325 * waiting for the JACK thread, and can thus cause deadlock in
328 if ((res = jack_deactivate (conn->client))) {
329 /* we only warn, this means the server is probably shut down and the client
331 GST_WARNING ("Could not deactivate Jack client (%d)", res);
333 /* close connection */
334 if ((res = jack_client_close (conn->client))) {
335 /* we assume the client is gone. */
336 GST_WARNING ("close failed (%d)", res);
340 g_mutex_free (conn->lock);
341 g_cond_free (conn->flush_cond);
343 g_free (conn->server);
349 gst_jack_audio_connection_add_client (GstJackAudioConnection * conn,
350 GstJackAudioClient * client)
352 g_mutex_lock (conn->lock);
353 switch (client->type) {
354 case GST_JACK_CLIENT_SOURCE:
355 conn->src_clients = g_list_append (conn->src_clients, client);
358 case GST_JACK_CLIENT_SINK:
359 conn->sink_clients = g_list_append (conn->sink_clients, client);
363 g_warning ("trying to add unknown client type");
366 g_mutex_unlock (conn->lock);
370 gst_jack_audio_connection_remove_client (GstJackAudioConnection * conn,
371 GstJackAudioClient * client)
373 g_mutex_lock (conn->lock);
374 switch (client->type) {
375 case GST_JACK_CLIENT_SOURCE:
376 conn->src_clients = g_list_remove (conn->src_clients, client);
379 case GST_JACK_CLIENT_SINK:
380 conn->sink_clients = g_list_remove (conn->sink_clients, client);
384 g_warning ("trying to remove unknown client type");
387 g_mutex_unlock (conn->lock);
391 * gst_jack_audio_client_get:
393 * @server: the server to connect to or NULL for the default server
394 * @type: the client type
395 * @shutdown: a callback when the jack server shuts down
396 * @process: a callback when samples are available
397 * @buffer_size: a callback when the buffer_size changes
398 * @sample_rate: a callback when the sample_rate changes
399 * @user_data: user data passed to the callbacks
400 * @status: pointer to hold the jack status code in case of errors
402 * Get the jack client connection for @id and @server. Connections to the same
403 * @id and @server will receive the same physical Jack client connection and
404 * will therefore be scheduled in the same process callback.
406 * Returns: a #GstJackAudioClient.
409 gst_jack_audio_client_new (const gchar * id, const gchar * server,
410 jack_client_t * jclient, GstJackClientType type,
411 void (*shutdown) (void *arg), JackProcessCallback process,
412 JackBufferSizeCallback buffer_size, JackSampleRateCallback sample_rate,
413 gpointer user_data, jack_status_t * status)
415 GstJackAudioClient *client;
416 GstJackAudioConnection *conn;
418 g_return_val_if_fail (id != NULL, NULL);
419 g_return_val_if_fail (status != NULL, NULL);
421 /* first get a connection for the id/server pair */
422 conn = gst_jack_audio_get_connection (id, server, jclient, status);
426 GST_INFO ("new client %s", id);
428 /* make new client using the connection */
429 client = g_new (GstJackAudioClient, 1);
430 client->active = client->deactivate = FALSE;
433 client->shutdown = shutdown;
434 client->process = process;
435 client->buffer_size = buffer_size;
436 client->sample_rate = sample_rate;
437 client->user_data = user_data;
439 /* add the client to the connection */
440 gst_jack_audio_connection_add_client (conn, client);
447 GST_DEBUG ("Could not get server connection (%d)", *status);
453 * gst_jack_audio_client_free:
454 * @client: a #GstJackAudioClient
456 * Free the resources used by @client.
459 gst_jack_audio_client_free (GstJackAudioClient * client)
461 GstJackAudioConnection *conn;
463 g_return_if_fail (client != NULL);
465 GST_INFO ("free client");
469 /* remove from connection first so that it's not scheduled anymore after this
471 gst_jack_audio_connection_remove_client (conn, client);
472 gst_jack_audio_unref_connection (conn);
478 * gst_jack_audio_client_get_client:
479 * @client: a #GstJackAudioClient
481 * Get the jack audio client for @client. This function is used to perform
482 * operations on the jack server from this client.
484 * Returns: The jack audio client.
487 gst_jack_audio_client_get_client (GstJackAudioClient * client)
489 g_return_val_if_fail (client != NULL, NULL);
491 /* no lock needed, the connection and the client does not change
492 * once the client is created. */
493 return client->conn->client;
497 * gst_jack_audio_client_set_active:
498 * @client: a #GstJackAudioClient
499 * @active: new mode for the client
501 * Activate or deactive @client. When a client is activated it will receive
502 * callbacks when data should be processed.
504 * Returns: 0 if all ok.
507 gst_jack_audio_client_set_active (GstJackAudioClient * client, gboolean active)
509 g_return_val_if_fail (client != NULL, -1);
511 /* make sure that we are not dispatching the client */
512 g_mutex_lock (client->conn->lock);
513 if (client->active && !active) {
514 /* we need to process once more to flush the port */
515 client->deactivate = TRUE;
517 /* need to wait for process_cb run once more */
518 while (client->deactivate)
519 g_cond_wait (client->conn->flush_cond, client->conn->lock);
521 client->active = active;
522 g_mutex_unlock (client->conn->lock);