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