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