]> git.sesse.net Git - vlc/blob - modules/audio_output/jack.c
aout: pass audio buffer explicitly to pf_play
[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_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     jack_client_t  *p_jack_client;
54     jack_port_t   **p_jack_ports;
55     jack_sample_t **p_jack_buffers;
56     unsigned int    i_channels;
57     jack_nframes_t latency;
58 };
59
60 /*****************************************************************************
61  * Local prototypes
62  *****************************************************************************/
63 static int  Open         ( vlc_object_t * );
64 static void Close        ( vlc_object_t * );
65 static void Play         ( audio_output_t *, block_t * );
66 static int  Process      ( jack_nframes_t i_frames, void *p_arg );
67 static int  GraphChange  ( void *p_arg );
68
69 #define AUTO_CONNECT_OPTION "jack-auto-connect"
70 #define AUTO_CONNECT_TEXT N_("Automatically connect to writable clients")
71 #define AUTO_CONNECT_LONGTEXT N_( \
72     "If enabled, this option will automatically connect sound output to the " \
73     "first writable JACK clients found." )
74
75 #define CONNECT_REGEX_OPTION "jack-connect-regex"
76 #define CONNECT_REGEX_TEXT N_("Connect to clients matching")
77 #define CONNECT_REGEX_LONGTEXT N_( \
78     "If automatic connection is enabled, only JACK clients whose names " \
79     "match this regular expression will be considered for connection." )
80
81 /*****************************************************************************
82  * Module descriptor
83  *****************************************************************************/
84 vlc_module_begin ()
85     set_shortname( "JACK" )
86     set_description( N_("JACK audio output") )
87     set_capability( "audio output", 100 )
88     set_category( CAT_AUDIO )
89     set_subcategory( SUBCAT_AUDIO_AOUT )
90     add_bool( AUTO_CONNECT_OPTION, true, AUTO_CONNECT_TEXT,
91               AUTO_CONNECT_LONGTEXT, false )
92     add_string( CONNECT_REGEX_OPTION, "system", CONNECT_REGEX_TEXT,
93                 CONNECT_REGEX_LONGTEXT, false )
94     set_callbacks( Open, Close )
95 vlc_module_end ()
96
97 /*****************************************************************************
98  * Open: create a JACK client
99  *****************************************************************************/
100 static int Open( vlc_object_t *p_this )
101 {
102     char psz_name[32];
103     audio_output_t *p_aout = (audio_output_t *)p_this;
104     struct aout_sys_t *p_sys = NULL;
105     int status = VLC_SUCCESS;
106     unsigned int i;
107     int i_error;
108
109     /* Allocate structure */
110     p_sys = calloc( 1, sizeof( aout_sys_t ) );
111     if( p_sys == NULL )
112     {
113         status = VLC_ENOMEM;
114         goto error_out;
115     }
116     p_aout->sys = p_sys;
117     p_sys->latency = 0;
118
119     /* Connect to the JACK server */
120     snprintf( psz_name, sizeof(psz_name), "vlc_%d", getpid());
121     psz_name[sizeof(psz_name) - 1] = '\0';
122     p_sys->p_jack_client = jack_client_open( psz_name,
123                                              JackNullOption | JackNoStartServer,
124                                              NULL );
125     if( p_sys->p_jack_client == NULL )
126     {
127         msg_Err( p_aout, "failed to connect to JACK server" );
128         status = VLC_EGENERIC;
129         goto error_out;
130     }
131
132     /* Set the process callback */
133     jack_set_process_callback( p_sys->p_jack_client, Process, p_aout );
134     jack_set_graph_order_callback ( p_sys->p_jack_client, GraphChange, p_aout );
135
136     p_aout->pf_play = Play;
137     p_aout->pf_pause = NULL;
138     p_aout->pf_flush = NULL;
139     aout_VolumeSoftInit( p_aout );
140
141     /* JACK only supports fl32 format */
142     p_aout->format.i_format = VLC_CODEC_FL32;
143     // TODO add buffer size callback
144     p_aout->i_nb_samples = jack_get_buffer_size( p_sys->p_jack_client );
145     p_aout->format.i_rate = jack_get_sample_rate( p_sys->p_jack_client );
146
147     p_sys->i_channels = aout_FormatNbChannels( &p_aout->format );
148
149     p_sys->p_jack_ports = malloc( p_sys->i_channels *
150                                   sizeof(jack_port_t *) );
151     if( p_sys->p_jack_ports == NULL )
152     {
153         status = VLC_ENOMEM;
154         goto error_out;
155     }
156
157     p_sys->p_jack_buffers = malloc( p_sys->i_channels *
158                                     sizeof(jack_sample_t *) );
159     if( p_sys->p_jack_buffers == NULL )
160     {
161         status = VLC_ENOMEM;
162         goto error_out;
163     }
164
165     /* Create the output ports */
166     for( i = 0; i < p_sys->i_channels; i++ )
167     {
168         snprintf( psz_name, sizeof(psz_name), "out_%d", i + 1);
169         psz_name[sizeof(psz_name) - 1] = '\0';
170         p_sys->p_jack_ports[i] = jack_port_register( p_sys->p_jack_client,
171                 psz_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0 );
172
173         if( p_sys->p_jack_ports[i] == NULL )
174         {
175             msg_Err( p_aout, "failed to register a JACK port" );
176             status = VLC_EGENERIC;
177             goto error_out;
178         }
179     }
180
181     /* Tell the JACK server we are ready */
182     i_error = jack_activate( p_sys->p_jack_client );
183     if( i_error )
184     {
185         msg_Err( p_aout, "failed to activate JACK client (error %d)", i_error );
186         status = VLC_EGENERIC;
187         goto error_out;
188     }
189
190     /* Auto connect ports if we were asked to */
191     if( var_InheritBool( p_aout, AUTO_CONNECT_OPTION ) )
192     {
193         unsigned int i_in_ports;
194         char *psz_regex = var_InheritString( p_aout, CONNECT_REGEX_OPTION );
195         const char **pp_in_ports = jack_get_ports( p_sys->p_jack_client,
196                                                    psz_regex, NULL,
197                                                    JackPortIsInput );
198         free( psz_regex );
199         /* Count the number of returned ports */
200         i_in_ports = 0;
201         while( pp_in_ports && pp_in_ports[i_in_ports] )
202         {
203             i_in_ports++;
204         }
205
206         /* Tie the output ports to JACK input ports */
207         for( i = 0; i_in_ports > 0 && i < p_sys->i_channels; i++ )
208         {
209             const char* psz_in = pp_in_ports[i % i_in_ports];
210             const char* psz_out = jack_port_name( p_sys->p_jack_ports[i] );
211
212             i_error = jack_connect( p_sys->p_jack_client, psz_out, psz_in );
213             if( i_error )
214             {
215                 msg_Err( p_aout, "failed to connect port %s to port %s (error %d)",
216                          psz_out, psz_in, i_error );
217             }
218             else
219             {
220                 msg_Dbg( p_aout, "connecting port %s to port %s",
221                          psz_out, psz_in );
222             }
223         }
224         free( pp_in_ports );
225     }
226
227     msg_Dbg( p_aout, "JACK audio output initialized (%d channels, buffer "
228              "size=%d, rate=%d)", p_sys->i_channels,
229              p_aout->i_nb_samples, p_aout->format.i_rate );
230
231 error_out:
232     /* Clean up, if an error occurred */
233     if( status != VLC_SUCCESS && p_sys != NULL)
234     {
235         if( p_sys->p_jack_client )
236         {
237             jack_deactivate( p_sys->p_jack_client );
238             jack_client_close( p_sys->p_jack_client );
239         }
240         free( p_sys->p_jack_ports );
241         free( p_sys->p_jack_buffers );
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     audio_output_t *p_aout = (audio_output_t*) p_arg;
255     struct aout_sys_t *p_sys = p_aout->sys;
256     jack_sample_t *p_src = NULL;
257
258     jack_nframes_t dframes = p_sys->latency
259                              - jack_frames_since_cycle_start( p_sys->p_jack_client );
260
261     jack_time_t dtime = dframes * 1000 * 1000 / jack_get_sample_rate( p_sys->p_jack_client );
262     mtime_t play_date = mdate() + (mtime_t) ( dtime );
263
264     /* Get the next audio data buffer */
265     aout_buffer_t *p_buffer = aout_OutputNextBuffer( p_aout, play_date, false );
266
267     if( p_buffer != NULL )
268     {
269         p_src = (jack_sample_t *)p_buffer->p_buffer;
270         i_nb_samples = p_buffer->i_nb_samples;
271     }
272
273     /* Get the JACK buffers to write to */
274     for( i = 0; i < p_sys->i_channels; i++ )
275     {
276         p_sys->p_jack_buffers[i] = jack_port_get_buffer( p_sys->p_jack_ports[i],
277                                                          i_frames );
278     }
279
280     /* Copy in the audio data */
281     for( j = 0; j < i_nb_samples; j++ )
282     {
283         for( i = 0; i < p_sys->i_channels; i++ )
284         {
285             jack_sample_t *p_dst = p_sys->p_jack_buffers[i];
286             p_dst[j] = *p_src;
287             p_src++;
288         }
289     }
290
291     /* Fill any remaining buffer with silence */
292     if( i_nb_samples < i_frames )
293     {
294         for( i = 0; i < p_sys->i_channels; i++ )
295         {
296             memset( p_sys->p_jack_buffers[i] + i_nb_samples, 0,
297                     sizeof( jack_sample_t ) * (i_frames - i_nb_samples) );
298         }
299     }
300
301     if( p_buffer )
302     {
303         aout_BufferFree( p_buffer );
304     }
305     return 0;
306 }
307
308 /*****************************************************************************
309  * GraphChange: callback when JACK reorders it's process graph.
310                 We update latency information.
311  *****************************************************************************/
312
313 static int GraphChange( void *p_arg )
314 {
315   audio_output_t *p_aout = (audio_output_t*) p_arg;
316   struct aout_sys_t *p_sys = p_aout->sys;
317   unsigned int i;
318   jack_nframes_t port_latency;
319
320   p_sys->latency = 0;
321
322   for( i = 0; i < p_sys->i_channels; ++i )
323   {
324     port_latency = jack_port_get_total_latency( p_sys->p_jack_client,
325                                                   p_sys->p_jack_ports[i] );
326     p_sys->latency = __MAX( p_sys->latency, port_latency );
327   }
328
329   msg_Dbg(p_aout, "JACK graph reordered. Our maximum latency=%d.", p_sys->latency);
330
331   return 0;
332 }
333
334 /*****************************************************************************
335  * Play: nothing to do
336  *****************************************************************************/
337 static void Play( audio_output_t *p_aout, block_t *block )
338 {
339     aout_FifoPush( &p_aout->fifo, block );
340 }
341
342 /*****************************************************************************
343  * Close: close the JACK client
344  *****************************************************************************/
345 static void Close( vlc_object_t *p_this )
346 {
347     int i_error;
348     audio_output_t *p_aout = (audio_output_t *)p_this;
349     struct aout_sys_t *p_sys = p_aout->sys;
350
351     i_error = jack_deactivate( p_sys->p_jack_client );
352     if( i_error )
353     {
354         msg_Err( p_aout, "jack_deactivate failed (error %d)", i_error );
355     }
356
357     i_error = jack_client_close( p_sys->p_jack_client );
358     if( i_error )
359     {
360         msg_Err( p_aout, "jack_client_close failed (error %d)", i_error );
361     }
362     free( p_sys->p_jack_ports );
363     free( p_sys->p_jack_buffers );
364     free( p_sys );
365 }