rtph264pay: add stream-format and alignment to h264 sink caps
[platform/upstream/gstreamer.git] / ext / jack / gstjackaudioclient.c
1 /* GStreamer
2  * Copyright (C) 2006 Wim Taymans <wim@fluendo.com>
3  *
4  * gstjackaudioclient.c: jack audio client implementation
5  *
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.
10  *
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.
15  *
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.
20  */
21
22 #include <string.h>
23
24 #include "gstjackaudioclient.h"
25 #include "gstjack.h"
26
27 #include <gst/glib-compat-private.h>
28
29 GST_DEBUG_CATEGORY_STATIC (gst_jack_audio_client_debug);
30 #define GST_CAT_DEFAULT gst_jack_audio_client_debug
31
32 void
33 gst_jack_audio_client_init (void)
34 {
35   GST_DEBUG_CATEGORY_INIT (gst_jack_audio_client_debug, "jackclient", 0,
36       "jackclient helpers");
37 }
38
39 /* a list of global connections indexed by id and server. */
40 G_LOCK_DEFINE_STATIC (connections_lock);
41 static GList *connections;
42
43 /* the connection to a server  */
44 typedef struct
45 {
46   gint refcount;
47   GMutex *lock;
48   GCond *flush_cond;
49
50   /* id/server pair and the connection */
51   gchar *id;
52   gchar *server;
53   jack_client_t *client;
54
55   /* lists of GstJackAudioClients */
56   gint n_clients;
57   GList *src_clients;
58   GList *sink_clients;
59
60   /* transport state handling */
61   gint cur_ts;
62   GstState transport_state;
63 } GstJackAudioConnection;
64
65 /* an object sharing a jack_client_t connection. */
66 struct _GstJackAudioClient
67 {
68   GstJackAudioConnection *conn;
69
70   GstJackClientType type;
71   gboolean active;
72   gboolean deactivate;
73
74   JackShutdownCallback shutdown;
75   JackProcessCallback process;
76   JackBufferSizeCallback buffer_size;
77   JackSampleRateCallback sample_rate;
78   gpointer user_data;
79 };
80
81 typedef struct
82 {
83   jack_nframes_t nframes;
84   gpointer user_data;
85 } JackCB;
86
87 static gboolean
88 jack_handle_transport_change (GstJackAudioClient * client, GstState state)
89 {
90   GstObject *obj = GST_OBJECT_PARENT (client->user_data);
91   GstJackTransport mode;
92
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));
99     return TRUE;
100   }
101   return FALSE;
102 }
103
104 static int
105 jack_process_cb (jack_nframes_t nframes, void *arg)
106 {
107   GstJackAudioConnection *conn = (GstJackAudioConnection *) arg;
108   GList *walk;
109   int res = 0;
110   jack_transport_state_t ts = jack_transport_query (conn->client, NULL);
111
112   if (ts != conn->cur_ts) {
113     conn->cur_ts = ts;
114     switch (ts) {
115       case JackTransportStopped:
116         GST_DEBUG ("transport state is 'stopped'");
117         conn->transport_state = GST_STATE_PAUSED;
118         break;
119       case JackTransportStarting:
120         GST_DEBUG ("transport state is 'starting'");
121         conn->transport_state = GST_STATE_READY;
122         break;
123       case JackTransportRolling:
124         GST_DEBUG ("transport state is 'rolling'");
125         conn->transport_state = GST_STATE_PLAYING;
126         break;
127       default:
128         break;
129     }
130     GST_DEBUG ("num of clients: src=%d, sink=%d",
131         g_list_length (conn->src_clients), g_list_length (conn->sink_clients));
132   }
133
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;
140
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);
147       }
148     }
149   }
150   for (walk = conn->sink_clients; walk; walk = g_list_next (walk)) {
151     GstJackAudioClient *client = (GstJackAudioClient *) walk->data;
152
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);
159       }
160     }
161   }
162
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;
170         break;
171       }
172     }
173   }
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;
179         break;
180       }
181     }
182   }
183   g_mutex_unlock (conn->lock);
184   return res;
185 }
186
187 /* we error out */
188 static int
189 jack_sample_rate_cb (jack_nframes_t nframes, void *arg)
190 {
191   return 0;
192 }
193
194 /* we error out */
195 static int
196 jack_buffer_size_cb (jack_nframes_t nframes, void *arg)
197 {
198   return 0;
199 }
200
201 static void
202 jack_shutdown_cb (void *arg)
203 {
204   GstJackAudioConnection *conn = (GstJackAudioConnection *) arg;
205   GList *walk;
206
207   GST_DEBUG ("disconnect client %s from server %s", conn->id,
208       GST_STR_NULL (conn->server));
209
210   g_mutex_lock (conn->lock);
211   for (walk = conn->src_clients; walk; walk = g_list_next (walk)) {
212     GstJackAudioClient *client = (GstJackAudioClient *) walk->data;
213
214     if (client->shutdown)
215       client->shutdown (client->user_data);
216   }
217   for (walk = conn->sink_clients; walk; walk = g_list_next (walk)) {
218     GstJackAudioClient *client = (GstJackAudioClient *) walk->data;
219
220     if (client->shutdown)
221       client->shutdown (client->user_data);
222   }
223   g_mutex_unlock (conn->lock);
224 }
225
226 typedef struct
227 {
228   const gchar *id;
229   const gchar *server;
230 } FindData;
231
232 static gint
233 connection_find (GstJackAudioConnection * conn, FindData * data)
234 {
235   /* id's must match */
236   if (strcmp (conn->id, data->id))
237     return 1;
238
239   /* both the same or NULL */
240   if (conn->server == data->server)
241     return 0;
242
243   /* we cannot compare NULL */
244   if (conn->server == NULL || data->server == NULL)
245     return 1;
246
247   if (strcmp (conn->server, data->server))
248     return 1;
249
250   return 0;
251 }
252
253 /* make a connection with @id and @server. Returns NULL on failure with the
254  * status set. */
255 static GstJackAudioConnection *
256 gst_jack_audio_make_connection (const gchar * id, const gchar * server,
257     jack_client_t * jclient, jack_status_t * status)
258 {
259   GstJackAudioConnection *conn;
260   jack_options_t options;
261   gint res;
262
263   *status = 0;
264
265   GST_DEBUG ("new client %s, connecting to server %s", id,
266       GST_STR_NULL (server));
267
268   /* never start a server */
269   options = JackNoStartServer;
270   /* if we have a servername, use it */
271   if (server != NULL)
272     options |= JackServerName;
273   /* open the client */
274   if (jclient == NULL)
275     jclient = jack_client_open (id, options, status, server);
276   if (jclient == NULL)
277     goto could_not_open;
278
279   /* now create object */
280   conn = g_new (GstJackAudioConnection, 1);
281   conn->refcount = 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;
287   conn->n_clients = 0;
288   conn->src_clients = NULL;
289   conn->sink_clients = NULL;
290   conn->cur_ts = -1;
291   conn->transport_state = GST_STATE_VOID_PENDING;
292
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);
299
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;
304
305   GST_DEBUG ("opened connection %p", conn);
306
307   return conn;
308
309   /* ERRORS */
310 could_not_open:
311   {
312     GST_DEBUG ("failed to open jack client, %d", *status);
313     return NULL;
314   }
315 could_not_activate:
316   {
317     GST_ERROR ("Could not activate client (%d)", res);
318     *status = JackFailure;
319     g_mutex_free (conn->lock);
320     g_free (conn->id);
321     g_free (conn->server);
322     g_free (conn);
323     return NULL;
324   }
325 }
326
327 static GstJackAudioConnection *
328 gst_jack_audio_get_connection (const gchar * id, const gchar * server,
329     jack_client_t * jclient, jack_status_t * status)
330 {
331   GstJackAudioConnection *conn;
332   GList *found;
333   FindData data;
334
335   GST_DEBUG ("getting connection for id %s, server %s", id,
336       GST_STR_NULL (server));
337
338   data.id = id;
339   data.server = server;
340
341   G_LOCK (connections_lock);
342   found =
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;
347     conn->refcount++;
348
349     GST_DEBUG ("found connection %p", conn);
350   } else {
351     /* make new connection */
352     conn = gst_jack_audio_make_connection (id, server, jclient, status);
353     if (conn != NULL) {
354       GST_DEBUG ("created connection %p", conn);
355       /* add to list on success */
356       connections = g_list_prepend (connections, conn);
357     } else {
358       GST_WARNING ("could not create connection");
359     }
360   }
361   G_UNLOCK (connections_lock);
362
363   return conn;
364 }
365
366 static void
367 gst_jack_audio_unref_connection (GstJackAudioConnection * conn)
368 {
369   gint res;
370   gboolean zero;
371
372   GST_DEBUG ("unref connection %p refcnt %d", conn, conn->refcount);
373
374   G_LOCK (connections_lock);
375   conn->refcount--;
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);
381   }
382   G_UNLOCK (connections_lock);
383
384   /* if we are zero, close and cleanup the connection */
385   if (zero) {
386     /* don't use conn->lock here. two reasons:
387      *
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.
391      *
392      * 2) it would deadlock anyway, because jack_deactivate() will sleep
393      *      waiting for the JACK thread, and can thus cause deadlock in 
394      *      jack_process_cb()
395      */
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
399        * is gone anyway. */
400       GST_WARNING ("Could not deactivate Jack client (%d)", res);
401     }
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);
406     }
407
408     /* free resources */
409     g_mutex_free (conn->lock);
410     g_cond_free (conn->flush_cond);
411     g_free (conn->id);
412     g_free (conn->server);
413     g_free (conn);
414   }
415 }
416
417 static void
418 gst_jack_audio_connection_add_client (GstJackAudioConnection * conn,
419     GstJackAudioClient * client)
420 {
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);
425       conn->n_clients++;
426       break;
427     case GST_JACK_CLIENT_SINK:
428       conn->sink_clients = g_list_append (conn->sink_clients, client);
429       conn->n_clients++;
430       break;
431     default:
432       g_warning ("trying to add unknown client type");
433       break;
434   }
435   g_mutex_unlock (conn->lock);
436 }
437
438 static void
439 gst_jack_audio_connection_remove_client (GstJackAudioConnection * conn,
440     GstJackAudioClient * client)
441 {
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);
446       conn->n_clients--;
447       break;
448     case GST_JACK_CLIENT_SINK:
449       conn->sink_clients = g_list_remove (conn->sink_clients, client);
450       conn->n_clients--;
451       break;
452     default:
453       g_warning ("trying to remove unknown client type");
454       break;
455   }
456   g_mutex_unlock (conn->lock);
457 }
458
459 /**
460  * gst_jack_audio_client_get:
461  * @id: the client id
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
470  *
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.
474  * 
475  * Returns: a #GstJackAudioClient.
476  */
477 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)
483 {
484   GstJackAudioClient *client;
485   GstJackAudioConnection *conn;
486
487   g_return_val_if_fail (id != NULL, NULL);
488   g_return_val_if_fail (status != NULL, NULL);
489
490   /* first get a connection for the id/server pair */
491   conn = gst_jack_audio_get_connection (id, server, jclient, status);
492   if (conn == NULL)
493     goto no_connection;
494
495   GST_INFO ("new client %s", id);
496
497   /* make new client using the connection */
498   client = g_new (GstJackAudioClient, 1);
499   client->active = client->deactivate = FALSE;
500   client->conn = conn;
501   client->type = type;
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;
507
508   /* add the client to the connection */
509   gst_jack_audio_connection_add_client (conn, client);
510
511   return client;
512
513   /* ERRORS */
514 no_connection:
515   {
516     GST_DEBUG ("Could not get server connection (%d)", *status);
517     return NULL;
518   }
519 }
520
521 /**
522  * gst_jack_audio_client_free:
523  * @client: a #GstJackAudioClient
524  *
525  * Free the resources used by @client.
526  */
527 void
528 gst_jack_audio_client_free (GstJackAudioClient * client)
529 {
530   GstJackAudioConnection *conn;
531
532   g_return_if_fail (client != NULL);
533
534   GST_INFO ("free client");
535
536   conn = client->conn;
537
538   /* remove from connection first so that it's not scheduled anymore after this
539    * call */
540   gst_jack_audio_connection_remove_client (conn, client);
541   gst_jack_audio_unref_connection (conn);
542
543   g_free (client);
544 }
545
546 /**
547  * gst_jack_audio_client_get_client:
548  * @client: a #GstJackAudioClient
549  *
550  * Get the jack audio client for @client. This function is used to perform
551  * operations on the jack server from this client.
552  *
553  * Returns: The jack audio client.
554  */
555 jack_client_t *
556 gst_jack_audio_client_get_client (GstJackAudioClient * client)
557 {
558   g_return_val_if_fail (client != NULL, NULL);
559
560   /* no lock needed, the connection and the client does not change 
561    * once the client is created. */
562   return client->conn->client;
563 }
564
565 /**
566  * gst_jack_audio_client_set_active:
567  * @client: a #GstJackAudioClient
568  * @active: new mode for the client
569  *
570  * Activate or deactive @client. When a client is activated it will receive
571  * callbacks when data should be processed.
572  *
573  * Returns: 0 if all ok.
574  */
575 gint
576 gst_jack_audio_client_set_active (GstJackAudioClient * client, gboolean active)
577 {
578   g_return_val_if_fail (client != NULL, -1);
579
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;
585
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);
589   }
590   client->active = active;
591   g_mutex_unlock (client->conn->lock);
592
593   return 0;
594 }
595
596 /**
597  * gst_jack_audio_client_get_transport_state:
598  * @client: a #GstJackAudioClient
599  *
600  * Check the current transport state. The client can use this to request a state
601  * change from the application.
602  *
603  * Returns: the state, %GST_STATE_VOID_PENDING for no change in the transport
604  * state
605  */
606 GstState
607 gst_jack_audio_client_get_transport_state (GstJackAudioClient * client)
608 {
609   GstState state = client->conn->transport_state;
610
611   client->conn->transport_state = GST_STATE_VOID_PENDING;
612   return state;
613 }