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