1 /*****************************************************************************
2 * jack : JACK audio output module
3 *****************************************************************************
4 * Copyright (C) 2006 the VideoLAN team
7 * Authors: Cyril Deguet <asmax _at_ videolan.org>
8 * Jon Griffiths <jon_p_griffiths _At_ yahoo _DOT_ com>
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.
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.
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 *****************************************************************************/
25 * \file modules/audio_output/jack.c
26 * \brief JACK audio output functions
28 /*****************************************************************************
30 *****************************************************************************/
31 #include <unistd.h> /* write(), close() */
37 #include <vlc_common.h>
38 #include <vlc_plugin.h>
41 #include <jack/jack.h>
43 typedef jack_default_audio_sample_t jack_sample_t;
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 *****************************************************************************/
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;
59 /*****************************************************************************
61 *****************************************************************************/
62 static int Open ( vlc_object_t * );
63 static void Close ( vlc_object_t * );
64 static void Play ( aout_instance_t * );
65 static int Process ( jack_nframes_t i_frames, void *p_arg );
67 #define AUTO_CONNECT_OPTION "jack-auto-connect"
68 #define AUTO_CONNECT_TEXT N_("Automatically connect to writable clients")
69 #define AUTO_CONNECT_LONGTEXT N_( \
70 "If enabled, this option will automatically connect sound output to the " \
71 "first writable JACK clients found." )
73 #define CONNECT_REGEX_OPTION "jack-connect-regex"
74 #define CONNECT_REGEX_TEXT N_("Connect to clients matching")
75 #define CONNECT_REGEX_LONGTEXT N_( \
76 "If automatic connection is enabled, only JACK clients whose names " \
77 "match this regular expression will be considered for connection." )
79 /*****************************************************************************
81 *****************************************************************************/
83 set_shortname( "JACK" )
84 set_description( N_("JACK audio output") )
85 set_capability( "audio output", 100 )
86 set_category( CAT_AUDIO )
87 set_subcategory( SUBCAT_AUDIO_AOUT )
88 add_bool( AUTO_CONNECT_OPTION, false, NULL, AUTO_CONNECT_TEXT,
89 AUTO_CONNECT_LONGTEXT, true )
90 add_string( CONNECT_REGEX_OPTION, NULL, NULL, CONNECT_REGEX_TEXT,
91 CONNECT_REGEX_LONGTEXT, true )
92 set_callbacks( Open, Close )
95 /*****************************************************************************
96 * Open: create a JACK client
97 *****************************************************************************/
98 static int Open( vlc_object_t *p_this )
101 aout_instance_t *p_aout = (aout_instance_t *)p_this;
102 struct aout_sys_t *p_sys = NULL;
103 int status = VLC_SUCCESS;
107 /* Allocate structure */
108 p_sys = calloc( 1, sizeof( aout_sys_t ) );
114 p_aout->output.p_sys = p_sys;
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_open( psz_name,
120 JackNullOption | JackNoStartServer,
122 if( p_sys->p_jack_client == NULL )
124 msg_Err( p_aout, "failed to connect to JACK server" );
125 status = VLC_EGENERIC;
129 /* Set the process callback */
130 jack_set_process_callback( p_sys->p_jack_client, Process, p_aout );
132 p_aout->output.pf_play = Play;
133 aout_VolumeSoftInit( p_aout );
135 /* JACK only supports fl32 format */
136 p_aout->output.output.i_format = VLC_CODEC_FL32;
137 // TODO add buffer size callback
138 p_aout->output.i_nb_samples = jack_get_buffer_size( p_sys->p_jack_client );
139 p_aout->output.output.i_rate = jack_get_sample_rate( p_sys->p_jack_client );
141 p_sys->i_channels = aout_FormatNbChannels( &p_aout->output.output );
143 p_sys->p_jack_ports = malloc( p_sys->i_channels *
144 sizeof(jack_port_t *) );
145 if( p_sys->p_jack_ports == NULL )
151 p_sys->p_jack_buffers = malloc( p_sys->i_channels *
152 sizeof(jack_sample_t *) );
153 if( p_sys->p_jack_buffers == NULL )
159 /* Create the output ports */
160 for( i = 0; i < p_sys->i_channels; i++ )
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 );
167 if( p_sys->p_jack_ports[i] == NULL )
169 msg_Err( p_aout, "failed to register a JACK port" );
170 status = VLC_EGENERIC;
175 /* Tell the JACK server we are ready */
176 i_error = jack_activate( p_sys->p_jack_client );
179 msg_Err( p_aout, "failed to activate JACK client (error %d)", i_error );
180 status = VLC_EGENERIC;
184 /* Auto connect ports if we were asked to */
185 if( config_GetInt( p_aout, AUTO_CONNECT_OPTION ) )
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,
193 /* Count the number of returned ports */
195 while( pp_in_ports && pp_in_ports[i_in_ports] )
200 /* Tie the output ports to JACK input ports */
201 for( i = 0; i_in_ports > 0 && i < p_sys->i_channels; i++ )
203 const char* psz_in = pp_in_ports[i % i_in_ports];
204 const char* psz_out = jack_port_name( p_sys->p_jack_ports[i] );
206 i_error = jack_connect( p_sys->p_jack_client, psz_out, psz_in );
209 msg_Err( p_aout, "failed to connect port %s to port %s (error %d)",
210 psz_out, psz_in, i_error );
214 msg_Dbg( p_aout, "connecting port %s to port %s",
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 );
226 /* Clean up, if an error occurred */
227 if( status != VLC_SUCCESS && p_sys != NULL)
229 if( p_sys->p_jack_client )
231 jack_deactivate( p_sys->p_jack_client );
232 jack_client_close( p_sys->p_jack_client );
234 free( p_sys->p_jack_ports );
235 free( p_sys->p_jack_buffers );
242 /*****************************************************************************
243 * Process: callback for JACK
244 *****************************************************************************/
245 int Process( jack_nframes_t i_frames, void *p_arg )
247 unsigned int i, j, i_nb_samples = 0;
248 aout_instance_t *p_aout = (aout_instance_t*) p_arg;
249 struct aout_sys_t *p_sys = p_aout->output.p_sys;
250 jack_sample_t *p_src = NULL;
252 /* we assume that data we send to jack now will be played one buffer size samples
253 after the start of this jack cycle */
254 jack_nframes_t dframes = i_frames - jack_frames_since_cycle_start( p_sys->p_jack_client );
255 jack_time_t dtime = dframes * 1000 * 1000 / jack_get_sample_rate( p_sys->p_jack_client );
256 mtime_t play_date = mdate() + (mtime_t) ( dtime );
258 /* Get the next audio data buffer */
259 aout_buffer_t *p_buffer = aout_OutputNextBuffer( p_aout, play_date, false );
261 if( p_buffer != NULL )
263 p_src = (jack_sample_t *)p_buffer->p_buffer;
264 i_nb_samples = p_buffer->i_nb_samples;
267 /* Get the JACK buffers to write to */
268 for( i = 0; i < p_sys->i_channels; i++ )
270 p_sys->p_jack_buffers[i] = jack_port_get_buffer( p_sys->p_jack_ports[i],
274 /* Copy in the audio data */
275 for( j = 0; j < i_nb_samples; j++ )
277 for( i = 0; i < p_sys->i_channels; i++ )
279 jack_sample_t *p_dst = p_sys->p_jack_buffers[i];
285 /* Fill any remaining buffer with silence */
286 if( i_nb_samples < i_frames )
288 for( i = 0; i < p_sys->i_channels; i++ )
290 memset( p_sys->p_jack_buffers[i] + i_nb_samples, 0,
291 sizeof( jack_sample_t ) * (i_frames - i_nb_samples) );
297 aout_BufferFree( p_buffer );
303 /*****************************************************************************
304 * Play: nothing to do
305 *****************************************************************************/
306 static void Play( aout_instance_t *p_aout )
308 VLC_UNUSED( p_aout );
311 /*****************************************************************************
312 * Close: close the JACK client
313 *****************************************************************************/
314 static void Close( vlc_object_t *p_this )
317 aout_instance_t *p_aout = (aout_instance_t *)p_this;
318 struct aout_sys_t *p_sys = p_aout->output.p_sys;
320 i_error = jack_deactivate( p_sys->p_jack_client );
323 msg_Err( p_aout, "jack_deactivate failed (error %d)", i_error );
326 i_error = jack_client_close( p_sys->p_jack_client );
329 msg_Err( p_aout, "jack_client_close failed (error %d)", i_error );
331 free( p_sys->p_jack_ports );
332 free( p_sys->p_jack_buffers );