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"
27 #include <gst/glib-compat-private.h>
29 GST_DEBUG_CATEGORY_STATIC (gst_jack_audio_client_debug);
30 #define GST_CAT_DEFAULT gst_jack_audio_client_debug
33 gst_jack_audio_client_init (void)
35 GST_DEBUG_CATEGORY_INIT (gst_jack_audio_client_debug, "jackclient", 0,
36 "jackclient helpers");
39 /* a list of global connections indexed by id and server. */
40 G_LOCK_DEFINE_STATIC (connections_lock);
41 static GList *connections;
43 /* the connection to a server */
50 /* id/server pair and the connection */
53 jack_client_t *client;
55 /* lists of GstJackAudioClients */
60 /* transport state handling */
62 GstState transport_state;
63 } GstJackAudioConnection;
65 /* an object sharing a jack_client_t connection. */
66 struct _GstJackAudioClient
68 GstJackAudioConnection *conn;
70 GstJackClientType type;
74 JackShutdownCallback shutdown;
75 JackProcessCallback process;
76 JackBufferSizeCallback buffer_size;
77 JackSampleRateCallback sample_rate;
83 jack_nframes_t nframes;
88 jack_handle_transport_change (GstJackAudioClient * client, GstState state)
90 GstObject *obj = GST_OBJECT_PARENT (client->user_data);
91 GstJackTransport mode;
93 g_object_get (obj, "transport", &mode, NULL);
94 if ((mode == GST_JACK_TRANSPORT_SLAVE) && (GST_STATE (obj) != state)) {
95 GST_INFO_OBJECT (obj, "requesting state change: %s",
96 gst_element_state_get_name (state));
97 gst_element_post_message (GST_ELEMENT (obj),
98 gst_message_new_request_state (obj, state));
105 jack_process_cb (jack_nframes_t nframes, void *arg)
107 GstJackAudioConnection *conn = (GstJackAudioConnection *) arg;
110 jack_transport_state_t ts = jack_transport_query (conn->client, NULL);
112 if (ts != conn->cur_ts) {
115 case JackTransportStopped:
116 GST_DEBUG ("transport state is 'stopped'");
117 conn->transport_state = GST_STATE_PAUSED;
119 case JackTransportStarting:
120 GST_DEBUG ("transport state is 'starting'");
121 conn->transport_state = GST_STATE_READY;
123 case JackTransportRolling:
124 GST_DEBUG ("transport state is 'rolling'");
125 conn->transport_state = GST_STATE_PLAYING;
130 GST_DEBUG ("num of clients: src=%d, sink=%d",
131 g_list_length (conn->src_clients), g_list_length (conn->sink_clients));
134 g_mutex_lock (conn->lock);
135 /* call sources first, then sinks. Sources will either push data into the
136 * ringbuffer of the sinks, which will then pull the data out of it, or
137 * sinks will pull the data from the sources. */
138 for (walk = conn->src_clients; walk; walk = g_list_next (walk)) {
139 GstJackAudioClient *client = (GstJackAudioClient *) walk->data;
141 /* only call active clients */
142 if ((client->active || client->deactivate) && client->process) {
143 res = client->process (nframes, client->user_data);
144 if (client->deactivate) {
145 client->deactivate = FALSE;
146 g_cond_signal (conn->flush_cond);
150 for (walk = conn->sink_clients; walk; walk = g_list_next (walk)) {
151 GstJackAudioClient *client = (GstJackAudioClient *) walk->data;
153 /* only call active clients */
154 if ((client->active || client->deactivate) && client->process) {
155 res = client->process (nframes, client->user_data);
156 if (client->deactivate) {
157 client->deactivate = FALSE;
158 g_cond_signal (conn->flush_cond);
163 /* handle transport state requisition, do sinks first, stop after the first
164 * element that handled it */
165 if (conn->transport_state != GST_STATE_VOID_PENDING) {
166 for (walk = conn->sink_clients; walk; walk = g_list_next (walk)) {
167 if (jack_handle_transport_change ((GstJackAudioClient *) walk->data,
168 conn->transport_state)) {
169 conn->transport_state = GST_STATE_VOID_PENDING;
174 if (conn->transport_state != GST_STATE_VOID_PENDING) {
175 for (walk = conn->src_clients; walk; walk = g_list_next (walk)) {
176 if (jack_handle_transport_change ((GstJackAudioClient *) walk->data,
177 conn->transport_state)) {
178 conn->transport_state = GST_STATE_VOID_PENDING;
183 g_mutex_unlock (conn->lock);
189 jack_sample_rate_cb (jack_nframes_t nframes, void *arg)
196 jack_buffer_size_cb (jack_nframes_t nframes, void *arg)
202 jack_shutdown_cb (void *arg)
204 GstJackAudioConnection *conn = (GstJackAudioConnection *) arg;
207 GST_DEBUG ("disconnect client %s from server %s", conn->id,
208 GST_STR_NULL (conn->server));
210 g_mutex_lock (conn->lock);
211 for (walk = conn->src_clients; walk; walk = g_list_next (walk)) {
212 GstJackAudioClient *client = (GstJackAudioClient *) walk->data;
214 if (client->shutdown)
215 client->shutdown (client->user_data);
217 for (walk = conn->sink_clients; walk; walk = g_list_next (walk)) {
218 GstJackAudioClient *client = (GstJackAudioClient *) walk->data;
220 if (client->shutdown)
221 client->shutdown (client->user_data);
223 g_mutex_unlock (conn->lock);
233 connection_find (GstJackAudioConnection * conn, FindData * data)
235 /* id's must match */
236 if (strcmp (conn->id, data->id))
239 /* both the same or NULL */
240 if (conn->server == data->server)
243 /* we cannot compare NULL */
244 if (conn->server == NULL || data->server == NULL)
247 if (strcmp (conn->server, data->server))
253 /* make a connection with @id and @server. Returns NULL on failure with the
255 static GstJackAudioConnection *
256 gst_jack_audio_make_connection (const gchar * id, const gchar * server,
257 jack_client_t * jclient, jack_status_t * status)
259 GstJackAudioConnection *conn;
260 jack_options_t options;
265 GST_DEBUG ("new client %s, connecting to server %s", id,
266 GST_STR_NULL (server));
268 /* never start a server */
269 options = JackNoStartServer;
270 /* if we have a servername, use it */
272 options |= JackServerName;
273 /* open the client */
275 jclient = jack_client_open (id, options, status, server);
279 /* now create object */
280 conn = g_new (GstJackAudioConnection, 1);
282 conn->lock = g_mutex_new ();
283 conn->flush_cond = g_cond_new ();
284 conn->id = g_strdup (id);
285 conn->server = g_strdup (server);
286 conn->client = jclient;
288 conn->src_clients = NULL;
289 conn->sink_clients = NULL;
291 conn->transport_state = GST_STATE_VOID_PENDING;
293 /* set our callbacks */
294 jack_set_process_callback (jclient, jack_process_cb, conn);
295 /* these callbacks cause us to error */
296 jack_set_buffer_size_callback (jclient, jack_buffer_size_cb, conn);
297 jack_set_sample_rate_callback (jclient, jack_sample_rate_cb, conn);
298 jack_on_shutdown (jclient, jack_shutdown_cb, conn);
300 /* all callbacks are set, activate the client */
301 GST_INFO ("activate jack_client %p", jclient);
302 if ((res = jack_activate (jclient)))
303 goto could_not_activate;
305 GST_DEBUG ("opened connection %p", conn);
312 GST_DEBUG ("failed to open jack client, %d", *status);
317 GST_ERROR ("Could not activate client (%d)", res);
318 *status = JackFailure;
319 g_mutex_free (conn->lock);
321 g_free (conn->server);
327 static GstJackAudioConnection *
328 gst_jack_audio_get_connection (const gchar * id, const gchar * server,
329 jack_client_t * jclient, jack_status_t * status)
331 GstJackAudioConnection *conn;
335 GST_DEBUG ("getting connection for id %s, server %s", id,
336 GST_STR_NULL (server));
339 data.server = server;
341 G_LOCK (connections_lock);
343 g_list_find_custom (connections, &data, (GCompareFunc) connection_find);
344 if (found != NULL && jclient != NULL) {
345 /* we found it, increase refcount and return it */
346 conn = (GstJackAudioConnection *) found->data;
349 GST_DEBUG ("found connection %p", conn);
351 /* make new connection */
352 conn = gst_jack_audio_make_connection (id, server, jclient, status);
354 GST_DEBUG ("created connection %p", conn);
355 /* add to list on success */
356 connections = g_list_prepend (connections, conn);
358 GST_WARNING ("could not create connection");
361 G_UNLOCK (connections_lock);
367 gst_jack_audio_unref_connection (GstJackAudioConnection * conn)
372 GST_DEBUG ("unref connection %p refcnt %d", conn, conn->refcount);
374 G_LOCK (connections_lock);
376 if ((zero = (conn->refcount == 0))) {
377 GST_DEBUG ("closing connection %p", conn);
378 /* remove from list, we can release the mutex after removing the connection
379 * from the list because after that, nobody can access the connection anymore. */
380 connections = g_list_remove (connections, conn);
382 G_UNLOCK (connections_lock);
384 /* if we are zero, close and cleanup the connection */
386 /* don't use conn->lock here. two reasons:
388 * 1) its not necessary: jack_deactivate() will not return until the JACK thread
389 * associated with this connection is cleaned up by a thread join, hence
390 * no more callbacks can occur or be in progress.
392 * 2) it would deadlock anyway, because jack_deactivate() will sleep
393 * waiting for the JACK thread, and can thus cause deadlock in
396 GST_INFO ("deactivate jack_client %p", conn->client);
397 if ((res = jack_deactivate (conn->client))) {
398 /* we only warn, this means the server is probably shut down and the client
400 GST_WARNING ("Could not deactivate Jack client (%d)", res);
402 /* close connection */
403 if ((res = jack_client_close (conn->client))) {
404 /* we assume the client is gone. */
405 GST_WARNING ("close failed (%d)", res);
409 g_mutex_free (conn->lock);
410 g_cond_free (conn->flush_cond);
412 g_free (conn->server);
418 gst_jack_audio_connection_add_client (GstJackAudioConnection * conn,
419 GstJackAudioClient * client)
421 g_mutex_lock (conn->lock);
422 switch (client->type) {
423 case GST_JACK_CLIENT_SOURCE:
424 conn->src_clients = g_list_append (conn->src_clients, client);
427 case GST_JACK_CLIENT_SINK:
428 conn->sink_clients = g_list_append (conn->sink_clients, client);
432 g_warning ("trying to add unknown client type");
435 g_mutex_unlock (conn->lock);
439 gst_jack_audio_connection_remove_client (GstJackAudioConnection * conn,
440 GstJackAudioClient * client)
442 g_mutex_lock (conn->lock);
443 switch (client->type) {
444 case GST_JACK_CLIENT_SOURCE:
445 conn->src_clients = g_list_remove (conn->src_clients, client);
448 case GST_JACK_CLIENT_SINK:
449 conn->sink_clients = g_list_remove (conn->sink_clients, client);
453 g_warning ("trying to remove unknown client type");
456 g_mutex_unlock (conn->lock);
460 * gst_jack_audio_client_get:
462 * @server: the server to connect to or NULL for the default server
463 * @type: the client type
464 * @shutdown: a callback when the jack server shuts down
465 * @process: a callback when samples are available
466 * @buffer_size: a callback when the buffer_size changes
467 * @sample_rate: a callback when the sample_rate changes
468 * @user_data: user data passed to the callbacks
469 * @status: pointer to hold the jack status code in case of errors
471 * Get the jack client connection for @id and @server. Connections to the same
472 * @id and @server will receive the same physical Jack client connection and
473 * will therefore be scheduled in the same process callback.
475 * Returns: a #GstJackAudioClient.
478 gst_jack_audio_client_new (const gchar * id, const gchar * server,
479 jack_client_t * jclient, GstJackClientType type,
480 void (*shutdown) (void *arg), JackProcessCallback process,
481 JackBufferSizeCallback buffer_size, JackSampleRateCallback sample_rate,
482 gpointer user_data, jack_status_t * status)
484 GstJackAudioClient *client;
485 GstJackAudioConnection *conn;
487 g_return_val_if_fail (id != NULL, NULL);
488 g_return_val_if_fail (status != NULL, NULL);
490 /* first get a connection for the id/server pair */
491 conn = gst_jack_audio_get_connection (id, server, jclient, status);
495 GST_INFO ("new client %s", id);
497 /* make new client using the connection */
498 client = g_new (GstJackAudioClient, 1);
499 client->active = client->deactivate = FALSE;
502 client->shutdown = shutdown;
503 client->process = process;
504 client->buffer_size = buffer_size;
505 client->sample_rate = sample_rate;
506 client->user_data = user_data;
508 /* add the client to the connection */
509 gst_jack_audio_connection_add_client (conn, client);
516 GST_DEBUG ("Could not get server connection (%d)", *status);
522 * gst_jack_audio_client_free:
523 * @client: a #GstJackAudioClient
525 * Free the resources used by @client.
528 gst_jack_audio_client_free (GstJackAudioClient * client)
530 GstJackAudioConnection *conn;
532 g_return_if_fail (client != NULL);
534 GST_INFO ("free client");
538 /* remove from connection first so that it's not scheduled anymore after this
540 gst_jack_audio_connection_remove_client (conn, client);
541 gst_jack_audio_unref_connection (conn);
547 * gst_jack_audio_client_get_client:
548 * @client: a #GstJackAudioClient
550 * Get the jack audio client for @client. This function is used to perform
551 * operations on the jack server from this client.
553 * Returns: The jack audio client.
556 gst_jack_audio_client_get_client (GstJackAudioClient * client)
558 g_return_val_if_fail (client != NULL, NULL);
560 /* no lock needed, the connection and the client does not change
561 * once the client is created. */
562 return client->conn->client;
566 * gst_jack_audio_client_set_active:
567 * @client: a #GstJackAudioClient
568 * @active: new mode for the client
570 * Activate or deactive @client. When a client is activated it will receive
571 * callbacks when data should be processed.
573 * Returns: 0 if all ok.
576 gst_jack_audio_client_set_active (GstJackAudioClient * client, gboolean active)
578 g_return_val_if_fail (client != NULL, -1);
580 /* make sure that we are not dispatching the client */
581 g_mutex_lock (client->conn->lock);
582 if (client->active && !active) {
583 /* we need to process once more to flush the port */
584 client->deactivate = TRUE;
586 /* need to wait for process_cb run once more */
587 while (client->deactivate)
588 g_cond_wait (client->conn->flush_cond, client->conn->lock);
590 client->active = active;
591 g_mutex_unlock (client->conn->lock);
597 * gst_jack_audio_client_get_transport_state:
598 * @client: a #GstJackAudioClient
600 * Check the current transport state. The client can use this to request a state
601 * change from the application.
603 * Returns: the state, %GST_STATE_VOID_PENDING for no change in the transport
607 gst_jack_audio_client_get_transport_state (GstJackAudioClient * client)
609 GstState state = client->conn->transport_state;
611 client->conn->transport_state = GST_STATE_VOID_PENDING;