1 /*****************************************************************************
2 * jack.c : 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 *****************************************************************************/
54 jack_client_t *p_jack_client;
55 jack_port_t **p_jack_ports;
56 jack_sample_t **p_jack_buffers;
57 unsigned int i_channels;
58 jack_nframes_t latency;
63 /*****************************************************************************
65 *****************************************************************************/
66 static int Open ( vlc_object_t * );
67 static void Close ( vlc_object_t * );
68 static int Process ( jack_nframes_t i_frames, void *p_arg );
69 static int GraphChange ( void *p_arg );
73 #define AUTO_CONNECT_OPTION "jack-auto-connect"
74 #define AUTO_CONNECT_TEXT N_("Automatically connect to writable clients")
75 #define AUTO_CONNECT_LONGTEXT N_( \
76 "If enabled, this option will automatically connect sound output to the " \
77 "first writable JACK clients found." )
79 #define CONNECT_REGEX_OPTION "jack-connect-regex"
80 #define CONNECT_REGEX_TEXT N_("Connect to clients matching")
81 #define CONNECT_REGEX_LONGTEXT N_( \
82 "If automatic connection is enabled, only JACK clients whose names " \
83 "match this regular expression will be considered for connection." )
85 /*****************************************************************************
87 *****************************************************************************/
89 set_shortname( "JACK" )
90 set_description( N_("JACK audio output") )
91 set_capability( "audio output", 100 )
92 set_category( CAT_AUDIO )
93 set_subcategory( SUBCAT_AUDIO_AOUT )
94 add_bool( AUTO_CONNECT_OPTION, true, AUTO_CONNECT_TEXT,
95 AUTO_CONNECT_LONGTEXT, false )
96 add_string( CONNECT_REGEX_OPTION, "system", CONNECT_REGEX_TEXT,
97 CONNECT_REGEX_LONGTEXT, false )
99 set_callbacks( Open, Close )
102 /*****************************************************************************
103 * Open: create a JACK client
104 *****************************************************************************/
105 static int Open( vlc_object_t *p_this )
108 audio_output_t *p_aout = (audio_output_t *)p_this;
109 struct aout_sys_t *p_sys = NULL;
110 int status = VLC_SUCCESS;
114 /* Allocate structure */
115 p_sys = calloc( 1, sizeof( aout_sys_t ) );
124 /* Connect to the JACK server */
125 snprintf( psz_name, sizeof(psz_name), "vlc_%d", getpid());
126 psz_name[sizeof(psz_name) - 1] = '\0';
127 p_sys->p_jack_client = jack_client_open( psz_name,
128 JackNullOption | JackNoStartServer,
130 if( p_sys->p_jack_client == NULL )
132 msg_Err( p_aout, "failed to connect to JACK server" );
133 status = VLC_EGENERIC;
137 /* Set the process callback */
138 jack_set_process_callback( p_sys->p_jack_client, Process, p_aout );
139 jack_set_graph_order_callback ( p_sys->p_jack_client, GraphChange, p_aout );
141 /* JACK only supports fl32 format */
142 p_aout->format.i_format = VLC_CODEC_FL32;
143 // TODO add buffer size callback
144 p_aout->format.i_rate = jack_get_sample_rate( p_sys->p_jack_client );
146 p_aout->pf_play = aout_PacketPlay;
147 p_aout->pf_pause = aout_PacketPause;
148 p_aout->pf_flush = aout_PacketFlush;
149 aout_PacketInit( p_aout, &p_sys->packet,
150 jack_get_buffer_size( p_sys->p_jack_client ) );
151 aout_SoftVolumeInit( p_aout );
153 p_sys->i_channels = aout_FormatNbChannels( &p_aout->format );
155 p_sys->p_jack_ports = malloc( p_sys->i_channels *
156 sizeof(jack_port_t *) );
157 if( p_sys->p_jack_ports == NULL )
163 p_sys->p_jack_buffers = malloc( p_sys->i_channels *
164 sizeof(jack_sample_t *) );
165 if( p_sys->p_jack_buffers == NULL )
171 /* Create the output ports */
172 for( i = 0; i < p_sys->i_channels; i++ )
174 snprintf( psz_name, sizeof(psz_name), "out_%d", i + 1);
175 psz_name[sizeof(psz_name) - 1] = '\0';
176 p_sys->p_jack_ports[i] = jack_port_register( p_sys->p_jack_client,
177 psz_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0 );
179 if( p_sys->p_jack_ports[i] == NULL )
181 msg_Err( p_aout, "failed to register a JACK port" );
182 status = VLC_EGENERIC;
187 /* Tell the JACK server we are ready */
188 i_error = jack_activate( p_sys->p_jack_client );
191 msg_Err( p_aout, "failed to activate JACK client (error %d)", i_error );
192 status = VLC_EGENERIC;
196 /* Auto connect ports if we were asked to */
197 if( var_InheritBool( p_aout, AUTO_CONNECT_OPTION ) )
199 unsigned int i_in_ports;
200 char *psz_regex = var_InheritString( p_aout, CONNECT_REGEX_OPTION );
201 const char **pp_in_ports = jack_get_ports( p_sys->p_jack_client,
205 /* Count the number of returned ports */
207 while( pp_in_ports && pp_in_ports[i_in_ports] )
212 /* Tie the output ports to JACK input ports */
213 for( i = 0; i_in_ports > 0 && i < p_sys->i_channels; i++ )
215 const char* psz_in = pp_in_ports[i % i_in_ports];
216 const char* psz_out = jack_port_name( p_sys->p_jack_ports[i] );
218 i_error = jack_connect( p_sys->p_jack_client, psz_out, psz_in );
221 msg_Err( p_aout, "failed to connect port %s to port %s (error %d)",
222 psz_out, psz_in, i_error );
226 msg_Dbg( p_aout, "connecting port %s to port %s",
233 msg_Dbg( p_aout, "JACK audio output initialized (%d channels, rate=%d)",
234 p_sys->i_channels, p_aout->format.i_rate );
237 /* Clean up, if an error occurred */
238 if( status != VLC_SUCCESS && p_sys != NULL)
240 if( p_sys->p_jack_client )
242 jack_deactivate( p_sys->p_jack_client );
243 jack_client_close( p_sys->p_jack_client );
244 aout_PacketDestroy( p_aout );
246 free( p_sys->p_jack_ports );
247 free( p_sys->p_jack_buffers );
254 /*****************************************************************************
255 * Process: callback for JACK
256 *****************************************************************************/
257 int Process( jack_nframes_t i_frames, void *p_arg )
259 unsigned int i, j, i_nb_samples = 0;
260 audio_output_t *p_aout = (audio_output_t*) p_arg;
261 struct aout_sys_t *p_sys = p_aout->sys;
262 jack_sample_t *p_src = NULL;
264 jack_nframes_t dframes = p_sys->latency
265 - jack_frames_since_cycle_start( p_sys->p_jack_client );
267 jack_time_t dtime = dframes * 1000 * 1000 / jack_get_sample_rate( p_sys->p_jack_client );
268 mtime_t play_date = mdate() + (mtime_t) ( dtime );
270 /* Get the next audio data buffer */
271 block_t *p_buffer = aout_PacketNext( p_aout, play_date );
273 if( p_buffer != NULL )
275 p_src = (jack_sample_t *)p_buffer->p_buffer;
276 i_nb_samples = p_buffer->i_nb_samples;
279 /* Get the JACK buffers to write to */
280 for( i = 0; i < p_sys->i_channels; i++ )
282 p_sys->p_jack_buffers[i] = jack_port_get_buffer( p_sys->p_jack_ports[i],
286 /* Copy in the audio data */
287 for( j = 0; j < i_nb_samples; j++ )
289 for( i = 0; i < p_sys->i_channels; i++ )
291 jack_sample_t *p_dst = p_sys->p_jack_buffers[i];
297 /* Fill any remaining buffer with silence */
298 if( i_nb_samples < i_frames )
300 for( i = 0; i < p_sys->i_channels; i++ )
302 memset( p_sys->p_jack_buffers[i] + i_nb_samples, 0,
303 sizeof( jack_sample_t ) * (i_frames - i_nb_samples) );
308 block_Release( p_buffer );
312 /*****************************************************************************
313 * GraphChange: callback when JACK reorders it's process graph.
314 We update latency information.
315 *****************************************************************************/
317 static int GraphChange( void *p_arg )
319 audio_output_t *p_aout = (audio_output_t*) p_arg;
320 struct aout_sys_t *p_sys = p_aout->sys;
322 jack_nframes_t port_latency;
326 for( i = 0; i < p_sys->i_channels; ++i )
328 port_latency = jack_port_get_total_latency( p_sys->p_jack_client,
329 p_sys->p_jack_ports[i] );
330 p_sys->latency = __MAX( p_sys->latency, port_latency );
333 msg_Dbg(p_aout, "JACK graph reordered. Our maximum latency=%d.", p_sys->latency);
338 /*****************************************************************************
339 * Close: close the JACK client
340 *****************************************************************************/
341 static void Close( vlc_object_t *p_this )
344 audio_output_t *p_aout = (audio_output_t *)p_this;
345 struct aout_sys_t *p_sys = p_aout->sys;
347 i_error = jack_deactivate( p_sys->p_jack_client );
350 msg_Err( p_aout, "jack_deactivate failed (error %d)", i_error );
353 i_error = jack_client_close( p_sys->p_jack_client );
356 msg_Err( p_aout, "jack_client_close failed (error %d)", i_error );
358 free( p_sys->p_jack_ports );
359 free( p_sys->p_jack_buffers );
360 aout_PacketDestroy( p_aout );