]> git.sesse.net Git - vlc/blob - modules/audio_output/jack.c
Jack patch by Jon Griffiths, fix crashes, improve debuging and use regexps.
[vlc] / modules / audio_output / jack.c
1 /*****************************************************************************
2  * jack : JACK audio output module
3  *****************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
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
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 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 General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, 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 <string.h>                                            /* strerror() */
32 #include <unistd.h>                                      /* write(), close() */
33 #include <stdlib.h>                            /* calloc(), malloc(), free() */
34
35 #include <vlc/vlc.h>
36 #include <vlc_aout.h>
37
38 #include <jack/jack.h>
39
40 typedef jack_default_audio_sample_t jack_sample_t;
41
42 /*****************************************************************************
43  * aout_sys_t: JACK audio output method descriptor
44  *****************************************************************************
45  * This structure is part of the audio output thread descriptor.
46  * It describes some JACK specific variables.
47  *****************************************************************************/
48 struct aout_sys_t
49 {
50     jack_client_t  *p_jack_client;
51     jack_port_t   **p_jack_ports;
52     jack_sample_t **p_jack_buffers;
53     unsigned int    i_channels;
54 };
55
56 /*****************************************************************************
57  * Local prototypes
58  *****************************************************************************/
59 static int  Open         ( vlc_object_t * );
60 static void Close        ( vlc_object_t * );
61 static void Play         ( aout_instance_t * );
62 static int  Process      ( jack_nframes_t i_frames, void *p_arg );
63
64 #define AUTO_CONNECT_OPTION "jack-auto-connect"
65 #define AUTO_CONNECT_TEXT N_("Automatically connect to writable clients")
66 #define AUTO_CONNECT_LONGTEXT N_( \
67     "If enabled, this option will automatically connect sound output to the " \
68     "first writable JACK clients found." )
69
70 #define CONNECT_REGEX_OPTION "jack-connect-regex"
71 #define CONNECT_REGEX_TEXT N_("Connect to clients matching")
72 #define CONNECT_REGEX_LONGTEXT N_( \
73     "If automatic connection is enabled, only JACK clients whose names " \
74     "match this regular expression will be considered for connection." )
75
76 /*****************************************************************************
77  * Module descriptor
78  *****************************************************************************/
79 vlc_module_begin();
80     set_shortname( "JACK" );
81     set_description( _("JACK audio output") );
82     set_capability( "audio output", 100 );
83     set_category( CAT_AUDIO );
84     set_subcategory( SUBCAT_AUDIO_AOUT );
85     add_bool( AUTO_CONNECT_OPTION, 0, NULL, AUTO_CONNECT_TEXT,
86               AUTO_CONNECT_LONGTEXT, VLC_TRUE );
87     add_string( CONNECT_REGEX_OPTION, NULL, NULL, CONNECT_REGEX_TEXT,
88                 CONNECT_REGEX_LONGTEXT, VLC_TRUE );
89     set_callbacks( Open, Close );
90 vlc_module_end();
91
92 /*****************************************************************************
93  * Open: create a JACK client
94  *****************************************************************************/
95 static int Open( vlc_object_t *p_this )
96 {
97     char psz_name[32];
98     aout_instance_t *p_aout = (aout_instance_t *)p_this;
99     struct aout_sys_t *p_sys = NULL;
100     int status = VLC_SUCCESS;
101     unsigned int i;
102     int i_error;
103
104     /* Allocate structure */
105     p_sys = calloc( 1, sizeof( aout_sys_t ) );
106     if( p_sys == NULL )
107     {
108         msg_Err( p_aout, "out of memory" );
109         status = VLC_ENOMEM;
110         goto error_out;
111     }
112     p_aout->output.p_sys = p_sys;
113
114     /* Connect to the JACK server */
115     snprintf( psz_name, sizeof(psz_name), "vlc_%d", getpid());
116     psz_name[sizeof(psz_name) - 1] = '\0';
117     p_sys->p_jack_client = jack_client_new( psz_name );
118     if( p_sys->p_jack_client == NULL )
119     {
120         msg_Err( p_aout, "failed to connect to JACK server" );
121         status = VLC_EGENERIC;
122         goto error_out;
123     }
124
125     /* Set the process callback */
126     jack_set_process_callback( p_sys->p_jack_client, Process, p_aout );
127
128     p_aout->output.pf_play = Play;
129     aout_VolumeSoftInit( p_aout );
130
131     /* JACK only supports fl32 format */
132     p_aout->output.output.i_format = VLC_FOURCC('f','l','3','2');
133     // TODO add buffer size callback
134     p_aout->output.i_nb_samples = jack_get_buffer_size( p_sys->p_jack_client );
135     p_aout->output.output.i_rate = jack_get_sample_rate( p_sys->p_jack_client );
136
137     p_sys->i_channels = aout_FormatNbChannels( &p_aout->output.output );
138
139     p_sys->p_jack_ports = malloc( p_sys->i_channels *
140                                   sizeof(jack_port_t *) );
141     if( p_sys->p_jack_ports == NULL )
142     {
143         msg_Err( p_aout, "out of memory" );
144         status = VLC_ENOMEM;
145         goto error_out;
146     }
147
148     p_sys->p_jack_buffers = malloc( p_sys->i_channels *
149                                     sizeof(jack_sample_t *) );
150     if( p_sys->p_jack_buffers == NULL )
151     {
152         msg_Err( p_aout, "out of memory" );
153         status = VLC_ENOMEM;
154         goto error_out;
155     }
156
157     /* Create the output ports */
158     for( i = 0; i < p_sys->i_channels; i++ )
159     {
160         snprintf( psz_name, sizeof(psz_name), "out_%d", i + 1);
161         psz_name[sizeof(psz_name) - 1] = '\0';
162         p_sys->p_jack_ports[i] = jack_port_register( p_sys->p_jack_client,
163                 psz_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0 );
164
165         if( p_sys->p_jack_ports[i] == NULL )
166         {
167             msg_Err( p_aout, "failed to register a JACK port" );
168             status = VLC_EGENERIC;
169             goto error_out;
170         }
171     }
172
173     /* Tell the JACK server we are ready */
174     i_error = jack_activate( p_sys->p_jack_client );
175     if( i_error )
176     {
177         msg_Err( p_aout, "failed to activate JACK client (error %d)", i_error );
178         status = VLC_EGENERIC;
179         goto error_out;
180     }
181
182     /* Auto connect ports if we were asked to */
183     if( config_GetInt( p_aout, AUTO_CONNECT_OPTION ) )
184     {
185         unsigned int i_in_ports;
186         char *psz_regex = config_GetPsz( p_aout, CONNECT_REGEX_OPTION );
187         const char **pp_in_ports = jack_get_ports( p_sys->p_jack_client,
188                                                    psz_regex, NULL,
189                                                    JackPortIsInput );
190         /* Count the number of returned ports */
191         i_in_ports = 0;
192         while( pp_in_ports && pp_in_ports[i_in_ports] )
193         {
194             i_in_ports++;
195         }
196
197         /* Tie the output ports to JACK input ports */
198         for( i = 0; i_in_ports > 0 && i < p_sys->i_channels; i++ )
199         {
200             const char* psz_in = pp_in_ports[i % i_in_ports];
201             const char* psz_out = jack_port_name( p_sys->p_jack_ports[i] );
202
203             i_error = jack_connect( p_sys->p_jack_client, psz_out, psz_in );
204             if( i_error )
205             {
206                 msg_Err( p_aout, "failed to connect port %s to port %s (error %d)",
207                          psz_out, psz_in, i_error );
208             }
209             else
210             {
211                 msg_Dbg( p_aout, "connecting port %s to port %s",
212                          psz_out, psz_in );
213             }
214         }
215         if( pp_in_ports )
216         {
217             free( pp_in_ports );
218         }
219     }
220
221     msg_Dbg( p_aout, "JACK audio output initialized (%d channels, buffer "
222              "size=%d, rate=%d)", p_sys->i_channels,
223              p_aout->output.i_nb_samples, p_aout->output.output.i_rate );
224
225 error_out:
226     /* Clean up, if an error occurred */
227     if( status != VLC_SUCCESS && p_sys != NULL)
228     {
229         if( p_sys->p_jack_client )
230         {
231             jack_deactivate( p_sys->p_jack_client );
232             jack_client_close( p_sys->p_jack_client );
233         }
234         if( p_sys->p_jack_ports )
235         {
236             free( p_sys->p_jack_ports );
237         }
238         if( p_sys->p_jack_buffers )
239         {
240             free( p_sys->p_jack_buffers );
241         }
242         free( p_sys );
243     }
244     return status;
245 }
246
247
248 /*****************************************************************************
249  * Process: callback for JACK
250  *****************************************************************************/
251 int Process( jack_nframes_t i_frames, void *p_arg )
252 {
253     unsigned int i, j, i_nb_samples = 0;
254     aout_instance_t *p_aout = (aout_instance_t*) p_arg;
255     struct aout_sys_t *p_sys = p_aout->output.p_sys;
256     jack_sample_t *p_src = NULL;
257
258     /* Get the next audio data buffer */
259     aout_buffer_t *p_buffer = aout_FifoPop( p_aout, &p_aout->output.fifo );
260     if( p_buffer != NULL )
261     {
262         p_src = (jack_sample_t *)p_buffer->p_buffer;
263         i_nb_samples = p_buffer->i_nb_samples;
264     }
265
266     /* Get the JACK buffers to write to */
267     for( i = 0; i < p_sys->i_channels; i++ )
268     {
269         p_sys->p_jack_buffers[i] = jack_port_get_buffer( p_sys->p_jack_ports[i],
270                                                          i_frames );
271     }
272
273     /* Copy in the audio data */
274     for( j = 0; j < i_nb_samples; j++ )
275     {
276         for( i = 0; i < p_sys->i_channels; i++ )
277         {
278             jack_sample_t *p_dst = p_sys->p_jack_buffers[i];
279             p_dst[j] = *p_src;
280             p_src++;
281         }
282     }
283
284     /* Fill any remaining buffer with silence */
285     if( i_nb_samples < i_frames )
286     {
287         for( i = 0; i < p_sys->i_channels; i++ )
288         {
289             memset( p_sys->p_jack_buffers[i] + i_nb_samples, 0,
290                     sizeof( jack_sample_t ) * (i_frames - i_nb_samples) );
291         }
292     }
293
294     if( p_buffer )
295     {
296         aout_BufferFree( p_buffer );
297     }
298     return 0;
299 }
300
301
302 /*****************************************************************************
303  * Play: nothing to do
304  *****************************************************************************/
305 static void Play( aout_instance_t *p_aout )
306 {
307     aout_FifoFirstDate( p_aout, &p_aout->output.fifo );
308 }
309
310 /*****************************************************************************
311  * Close: close the JACK client
312  *****************************************************************************/
313 static void Close( vlc_object_t *p_this )
314 {
315     int i_error;
316     aout_instance_t *p_aout = (aout_instance_t *)p_this;
317     struct aout_sys_t *p_sys = p_aout->output.p_sys;
318
319     i_error = jack_deactivate( p_sys->p_jack_client );
320     if( i_error )
321     {
322         msg_Err( p_aout, "jack_deactivate failed (error %d)", i_error );
323     }
324
325     i_error = jack_client_close( p_sys->p_jack_client );
326     if( i_error )
327     {
328         msg_Err( p_aout, "jack_client_close failed (error %d)", i_error );
329     }
330     free( p_sys->p_jack_ports );
331     free( p_sys->p_jack_buffers );
332     free( p_sys );
333 }