]> git.sesse.net Git - vlc/blob - modules/audio_output/jack.c
LGPL
[vlc] / modules / audio_output / jack.c
1 /*****************************************************************************
2  * jack.c : JACK audio output module
3  *****************************************************************************
4  * Copyright (C) 2006 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Cyril Deguet <asmax _at_ videolan.org>
8  *          Jon Griffiths <jon_p_griffiths _At_ yahoo _DOT_ com>
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24 /**
25  * \file modules/audio_output/jack.c
26  * \brief JACK audio output functions
27  */
28 /*****************************************************************************
29  * Preamble
30  *****************************************************************************/
31 #include <unistd.h>                                      /* write(), close() */
32
33 #ifdef HAVE_CONFIG_H
34 # include "config.h"
35 #endif
36
37 #include <vlc_common.h>
38 #include <vlc_plugin.h>
39 #include <vlc_aout.h>
40
41 #include <jack/jack.h>
42
43 typedef jack_default_audio_sample_t jack_sample_t;
44
45 /*****************************************************************************
46  * aout_sys_t: JACK audio output method descriptor
47  *****************************************************************************
48  * This structure is part of the audio output thread descriptor.
49  * It describes some JACK specific variables.
50  *****************************************************************************/
51 struct aout_sys_t
52 {
53     aout_packet_t   packet;
54     jack_client_t  *p_jack_client;
55     jack_port_t   **p_jack_ports;
56     jack_sample_t **p_jack_buffers;
57     unsigned int    i_channels;
58     jack_nframes_t latency;
59     float soft_gain;
60     bool soft_mute;
61 };
62
63 /*****************************************************************************
64  * Local prototypes
65  *****************************************************************************/
66 static int  Open         ( vlc_object_t * );
67 static void Close        ( vlc_object_t * );
68 static int  Process      ( jack_nframes_t i_frames, void *p_arg );
69 static int  GraphChange  ( void *p_arg );
70
71 #include "volume.h"
72
73 #define AUTO_CONNECT_OPTION "jack-auto-connect"
74 #define AUTO_CONNECT_TEXT N_("Automatically connect to writable clients")
75 #define AUTO_CONNECT_LONGTEXT N_( \
76     "If enabled, this option will automatically connect sound output to the " \
77     "first writable JACK clients found." )
78
79 #define CONNECT_REGEX_OPTION "jack-connect-regex"
80 #define CONNECT_REGEX_TEXT N_("Connect to clients matching")
81 #define CONNECT_REGEX_LONGTEXT N_( \
82     "If automatic connection is enabled, only JACK clients whose names " \
83     "match this regular expression will be considered for connection." )
84
85 /*****************************************************************************
86  * Module descriptor
87  *****************************************************************************/
88 vlc_module_begin ()
89     set_shortname( "JACK" )
90     set_description( N_("JACK audio output") )
91     set_capability( "audio output", 100 )
92     set_category( CAT_AUDIO )
93     set_subcategory( SUBCAT_AUDIO_AOUT )
94     add_bool( AUTO_CONNECT_OPTION, true, AUTO_CONNECT_TEXT,
95               AUTO_CONNECT_LONGTEXT, false )
96     add_string( CONNECT_REGEX_OPTION, "system", CONNECT_REGEX_TEXT,
97                 CONNECT_REGEX_LONGTEXT, false )
98     add_sw_gain( )
99     set_callbacks( Open, Close )
100 vlc_module_end ()
101
102
103 static int Start( audio_output_t *p_aout, audio_sample_format_t *restrict fmt )
104 {
105     char psz_name[32];
106     struct aout_sys_t *p_sys = p_aout->sys;
107     int status = VLC_SUCCESS;
108     unsigned int i;
109     int i_error;
110
111     p_sys->latency = 0;
112
113     /* Connect to the JACK server */
114     snprintf( psz_name, sizeof(psz_name), "vlc_%d", getpid());
115     psz_name[sizeof(psz_name) - 1] = '\0';
116     p_sys->p_jack_client = jack_client_open( psz_name,
117                                              JackNullOption | JackNoStartServer,
118                                              NULL );
119     if( p_sys->p_jack_client == NULL )
120     {
121         msg_Err( p_aout, "failed to connect to JACK server" );
122         status = VLC_EGENERIC;
123         goto error_out;
124     }
125
126     /* Set the process callback */
127     jack_set_process_callback( p_sys->p_jack_client, Process, p_aout );
128     jack_set_graph_order_callback ( p_sys->p_jack_client, GraphChange, p_aout );
129
130     /* JACK only supports fl32 format */
131     fmt->i_format = VLC_CODEC_FL32;
132     // TODO add buffer size callback
133     fmt->i_rate = jack_get_sample_rate( p_sys->p_jack_client );
134
135     p_aout->time_get = aout_PacketTimeGet;
136     p_aout->play = aout_PacketPlay;
137     p_aout->pause = aout_PacketPause;
138     p_aout->flush = aout_PacketFlush;
139     aout_PacketInit( p_aout, &p_sys->packet,
140                      jack_get_buffer_size( p_sys->p_jack_client ), fmt );
141     aout_SoftVolumeStart( p_aout );
142
143     p_sys->i_channels = aout_FormatNbChannels( fmt );
144
145     p_sys->p_jack_ports = malloc( p_sys->i_channels *
146                                   sizeof(jack_port_t *) );
147     if( p_sys->p_jack_ports == NULL )
148     {
149         status = VLC_ENOMEM;
150         goto error_out;
151     }
152
153     p_sys->p_jack_buffers = malloc( p_sys->i_channels *
154                                     sizeof(jack_sample_t *) );
155     if( p_sys->p_jack_buffers == NULL )
156     {
157         status = VLC_ENOMEM;
158         goto error_out;
159     }
160
161     /* Create the output ports */
162     for( i = 0; i < p_sys->i_channels; i++ )
163     {
164         snprintf( psz_name, sizeof(psz_name), "out_%d", i + 1);
165         psz_name[sizeof(psz_name) - 1] = '\0';
166         p_sys->p_jack_ports[i] = jack_port_register( p_sys->p_jack_client,
167                 psz_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0 );
168
169         if( p_sys->p_jack_ports[i] == NULL )
170         {
171             msg_Err( p_aout, "failed to register a JACK port" );
172             status = VLC_EGENERIC;
173             goto error_out;
174         }
175     }
176
177     /* Tell the JACK server we are ready */
178     i_error = jack_activate( p_sys->p_jack_client );
179     if( i_error )
180     {
181         msg_Err( p_aout, "failed to activate JACK client (error %d)", i_error );
182         status = VLC_EGENERIC;
183         goto error_out;
184     }
185
186     /* Auto connect ports if we were asked to */
187     if( var_InheritBool( p_aout, AUTO_CONNECT_OPTION ) )
188     {
189         unsigned int i_in_ports;
190         char *psz_regex = var_InheritString( p_aout, CONNECT_REGEX_OPTION );
191         const char **pp_in_ports = jack_get_ports( p_sys->p_jack_client,
192                                                    psz_regex, NULL,
193                                                    JackPortIsInput );
194         free( psz_regex );
195         /* Count the number of returned ports */
196         i_in_ports = 0;
197         while( pp_in_ports && pp_in_ports[i_in_ports] )
198         {
199             i_in_ports++;
200         }
201
202         /* Tie the output ports to JACK input ports */
203         for( i = 0; i_in_ports > 0 && i < p_sys->i_channels; i++ )
204         {
205             const char* psz_in = pp_in_ports[i % i_in_ports];
206             const char* psz_out = jack_port_name( p_sys->p_jack_ports[i] );
207
208             i_error = jack_connect( p_sys->p_jack_client, psz_out, psz_in );
209             if( i_error )
210             {
211                 msg_Err( p_aout, "failed to connect port %s to port %s (error %d)",
212                          psz_out, psz_in, i_error );
213             }
214             else
215             {
216                 msg_Dbg( p_aout, "connecting port %s to port %s",
217                          psz_out, psz_in );
218             }
219         }
220         free( pp_in_ports );
221     }
222
223     msg_Dbg( p_aout, "JACK audio output initialized (%d channels, rate=%d)",
224              p_sys->i_channels, fmt->i_rate );
225
226 error_out:
227     /* Clean up, if an error occurred */
228     if( status != VLC_SUCCESS && p_sys != NULL)
229     {
230         if( p_sys->p_jack_client )
231         {
232             jack_deactivate( p_sys->p_jack_client );
233             jack_client_close( p_sys->p_jack_client );
234             aout_PacketDestroy( p_aout );
235         }
236         free( p_sys->p_jack_ports );
237         free( p_sys->p_jack_buffers );
238     }
239     return status;
240 }
241
242
243 /*****************************************************************************
244  * Process: callback for JACK
245  *****************************************************************************/
246 int Process( jack_nframes_t i_frames, void *p_arg )
247 {
248     unsigned int i, j, i_nb_samples = 0;
249     audio_output_t *p_aout = (audio_output_t*) p_arg;
250     struct aout_sys_t *p_sys = p_aout->sys;
251     jack_sample_t *p_src = NULL;
252
253     jack_nframes_t dframes = p_sys->latency
254                              - jack_frames_since_cycle_start( p_sys->p_jack_client );
255
256     jack_time_t dtime = dframes * 1000 * 1000 / jack_get_sample_rate( p_sys->p_jack_client );
257     mtime_t play_date = mdate() + (mtime_t) ( dtime );
258
259     /* Get the next audio data buffer */
260     block_t *p_buffer = aout_PacketNext( p_aout, play_date );
261
262     if( p_buffer != NULL )
263     {
264         p_src = (jack_sample_t *)p_buffer->p_buffer;
265         i_nb_samples = p_buffer->i_nb_samples;
266     }
267
268     /* Get the JACK buffers to write to */
269     for( i = 0; i < p_sys->i_channels; i++ )
270     {
271         p_sys->p_jack_buffers[i] = jack_port_get_buffer( p_sys->p_jack_ports[i],
272                                                          i_frames );
273     }
274
275     /* Copy in the audio data */
276     for( j = 0; j < i_nb_samples; j++ )
277     {
278         for( i = 0; i < p_sys->i_channels; i++ )
279         {
280             jack_sample_t *p_dst = p_sys->p_jack_buffers[i];
281             p_dst[j] = *p_src;
282             p_src++;
283         }
284     }
285
286     /* Fill any remaining buffer with silence */
287     if( i_nb_samples < i_frames )
288     {
289         for( i = 0; i < p_sys->i_channels; i++ )
290         {
291             memset( p_sys->p_jack_buffers[i] + i_nb_samples, 0,
292                     sizeof( jack_sample_t ) * (i_frames - i_nb_samples) );
293         }
294     }
295
296     if( p_buffer )
297         block_Release( p_buffer );
298     return 0;
299 }
300
301 /*****************************************************************************
302  * GraphChange: callback when JACK reorders it's process graph.
303                 We update latency information.
304  *****************************************************************************/
305
306 static int GraphChange( void *p_arg )
307 {
308   audio_output_t *p_aout = (audio_output_t*) p_arg;
309   struct aout_sys_t *p_sys = p_aout->sys;
310   unsigned int i;
311   jack_nframes_t port_latency;
312
313   p_sys->latency = 0;
314
315   for( i = 0; i < p_sys->i_channels; ++i )
316   {
317     port_latency = jack_port_get_total_latency( p_sys->p_jack_client,
318                                                   p_sys->p_jack_ports[i] );
319     p_sys->latency = __MAX( p_sys->latency, port_latency );
320   }
321
322   msg_Dbg(p_aout, "JACK graph reordered. Our maximum latency=%d.", p_sys->latency);
323
324   return 0;
325 }
326
327 /*****************************************************************************
328  * Close: close the JACK client
329  *****************************************************************************/
330 static void Stop( audio_output_t *p_aout )
331 {
332     int i_error;
333     struct aout_sys_t *p_sys = p_aout->sys;
334
335     i_error = jack_deactivate( p_sys->p_jack_client );
336     if( i_error )
337     {
338         msg_Err( p_aout, "jack_deactivate failed (error %d)", i_error );
339     }
340
341     i_error = jack_client_close( p_sys->p_jack_client );
342     if( i_error )
343     {
344         msg_Err( p_aout, "jack_client_close failed (error %d)", i_error );
345     }
346     free( p_sys->p_jack_ports );
347     free( p_sys->p_jack_buffers );
348     aout_PacketDestroy( p_aout );
349 }
350
351 static int Open(vlc_object_t *obj)
352 {
353     audio_output_t *aout = (audio_output_t *)obj;
354     aout_sys_t *sys = calloc(1, sizeof (*sys));
355
356     if (unlikely(sys == NULL))
357         return VLC_ENOMEM;
358     aout->sys = sys;
359     aout->start = Start;
360     aout->stop = Stop;
361     aout_SoftVolumeInit(aout);
362     return VLC_SUCCESS;
363 }
364
365 static void Close(vlc_object_t *obj)
366 {
367     audio_output_t *aout = (audio_output_t *)obj;
368     aout_sys_t *sys = aout->sys;
369
370     free(sys);
371 }