1 /*****************************************************************************
2 * jack.c : JACK audio output module
3 *****************************************************************************
4 * Copyright (C) 2006 VLC authors and VideoLAN
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 it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * 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>
42 #include <jack/ringbuffer.h>
44 typedef jack_default_audio_sample_t jack_sample_t;
46 /*****************************************************************************
47 * aout_sys_t: JACK audio output method descriptor
48 *****************************************************************************
49 * This structure is part of the audio output thread descriptor.
50 * It describes some JACK specific variables.
51 *****************************************************************************/
54 jack_ringbuffer_t *p_jack_ringbuffer;
55 jack_client_t *p_jack_client;
56 jack_port_t **p_jack_ports;
57 jack_sample_t **p_jack_buffers;
58 unsigned int i_channels;
60 jack_nframes_t latency;
63 mtime_t paused; /**< Time when (last) paused */
66 /*****************************************************************************
68 *****************************************************************************/
69 static int Open ( vlc_object_t * );
70 static void Close ( vlc_object_t * );
71 static void Play ( audio_output_t * p_aout, block_t * p_block );
72 static void Pause ( audio_output_t *aout, bool paused, mtime_t date );
73 static void Flush ( audio_output_t *p_aout, bool wait );
74 static int TimeGet ( audio_output_t *, mtime_t * );
75 static int Process ( jack_nframes_t i_frames, void *p_arg );
76 static int GraphChange ( void *p_arg );
78 #include "audio_output/volume.h"
80 #define AUTO_CONNECT_OPTION "jack-auto-connect"
81 #define AUTO_CONNECT_TEXT N_("Automatically connect to writable clients")
82 #define AUTO_CONNECT_LONGTEXT N_( \
83 "If enabled, this option will automatically connect sound output to the " \
84 "first writable JACK clients found." )
86 #define CONNECT_REGEX_OPTION "jack-connect-regex"
87 #define CONNECT_REGEX_TEXT N_("Connect to clients matching")
88 #define CONNECT_REGEX_LONGTEXT N_( \
89 "If automatic connection is enabled, only JACK clients whose names " \
90 "match this regular expression will be considered for connection." )
92 /*****************************************************************************
94 *****************************************************************************/
96 set_shortname( "JACK" )
97 set_description( N_("JACK audio output") )
98 set_capability( "audio output", 100 )
99 set_category( CAT_AUDIO )
100 set_subcategory( SUBCAT_AUDIO_AOUT )
101 add_bool( AUTO_CONNECT_OPTION, true, AUTO_CONNECT_TEXT,
102 AUTO_CONNECT_LONGTEXT, false )
103 add_string( CONNECT_REGEX_OPTION, "system", CONNECT_REGEX_TEXT,
104 CONNECT_REGEX_LONGTEXT, false )
106 set_callbacks( Open, Close )
110 static int Start( audio_output_t *p_aout, audio_sample_format_t *restrict fmt )
113 struct aout_sys_t *p_sys = p_aout->sys;
114 int status = VLC_SUCCESS;
119 p_sys->paused = VLC_TS_INVALID;
121 /* Connect to the JACK server */
122 snprintf( psz_name, sizeof(psz_name), "vlc_%d", getpid());
123 psz_name[sizeof(psz_name) - 1] = '\0';
124 p_sys->p_jack_client = jack_client_open( psz_name,
125 JackNullOption | JackNoStartServer,
127 if( p_sys->p_jack_client == NULL )
129 msg_Err( p_aout, "failed to connect to JACK server" );
130 status = VLC_EGENERIC;
134 /* Set the process callback */
135 jack_set_process_callback( p_sys->p_jack_client, Process, p_aout );
136 jack_set_graph_order_callback ( p_sys->p_jack_client, GraphChange, p_aout );
138 /* JACK only supports fl32 format */
139 fmt->i_format = VLC_CODEC_FL32;
140 // TODO add buffer size callback
141 p_sys->i_rate = fmt->i_rate = jack_get_sample_rate( p_sys->p_jack_client );
144 p_aout->pause = Pause;
145 p_aout->flush = Flush;
146 p_aout->time_get = TimeGet;
147 aout_SoftVolumeStart( p_aout );
149 p_sys->i_channels = aout_FormatNbChannels( fmt );
150 aout_FormatPrepare(fmt);
152 p_sys->p_jack_ports = malloc( p_sys->i_channels *
153 sizeof(jack_port_t *) );
154 if( p_sys->p_jack_ports == NULL )
160 p_sys->p_jack_buffers = malloc( p_sys->i_channels *
161 sizeof(jack_sample_t *) );
162 if( p_sys->p_jack_buffers == NULL )
168 const size_t buf_sz = AOUT_MAX_ADVANCE_TIME * fmt->i_rate *
169 fmt->i_bytes_per_frame / CLOCK_FREQ;
170 p_sys->p_jack_ringbuffer = jack_ringbuffer_create( buf_sz );
172 if( p_sys->p_jack_ringbuffer == NULL )
178 if( jack_ringbuffer_mlock( p_sys->p_jack_ringbuffer ))
180 msg_Warn( p_aout, "failed to lock JACK ringbuffer in memory" );
183 /* Create the output ports */
184 for( i = 0; i < p_sys->i_channels; i++ )
186 snprintf( psz_name, sizeof(psz_name), "out_%d", i + 1);
187 psz_name[sizeof(psz_name) - 1] = '\0';
188 p_sys->p_jack_ports[i] = jack_port_register( p_sys->p_jack_client,
189 psz_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0 );
191 if( p_sys->p_jack_ports[i] == NULL )
193 msg_Err( p_aout, "failed to register a JACK port" );
194 status = VLC_EGENERIC;
199 /* Tell the JACK server we are ready */
200 i_error = jack_activate( p_sys->p_jack_client );
203 msg_Err( p_aout, "failed to activate JACK client (error %d)", i_error );
204 status = VLC_EGENERIC;
208 /* Auto connect ports if we were asked to */
209 if( var_InheritBool( p_aout, AUTO_CONNECT_OPTION ) )
211 unsigned int i_in_ports;
212 char *psz_regex = var_InheritString( p_aout, CONNECT_REGEX_OPTION );
213 const char **pp_in_ports = jack_get_ports( p_sys->p_jack_client,
217 /* Count the number of returned ports */
219 while( pp_in_ports && pp_in_ports[i_in_ports] )
224 /* Tie the output ports to JACK input ports */
225 for( i = 0; i_in_ports > 0 && i < p_sys->i_channels; i++ )
227 const char* psz_in = pp_in_ports[i % i_in_ports];
228 const char* psz_out = jack_port_name( p_sys->p_jack_ports[i] );
230 i_error = jack_connect( p_sys->p_jack_client, psz_out, psz_in );
233 msg_Err( p_aout, "failed to connect port %s to port %s (error %d)",
234 psz_out, psz_in, i_error );
238 msg_Dbg( p_aout, "connecting port %s to port %s",
245 msg_Dbg( p_aout, "JACK audio output initialized (%d channels, rate=%d)",
246 p_sys->i_channels, fmt->i_rate );
249 /* Clean up, if an error occurred */
250 if( status != VLC_SUCCESS )
252 if( p_sys->p_jack_client )
254 jack_deactivate( p_sys->p_jack_client );
255 jack_client_close( p_sys->p_jack_client );
257 if( p_sys->p_jack_ringbuffer )
258 jack_ringbuffer_free( p_sys->p_jack_ringbuffer );
260 free( p_sys->p_jack_ports );
261 free( p_sys->p_jack_buffers );
266 static void Play (audio_output_t * p_aout, block_t * p_block)
268 struct aout_sys_t *p_sys = p_aout->sys;
269 jack_ringbuffer_t *rb = p_sys->p_jack_ringbuffer;
270 const size_t bytes_per_frame = p_sys->i_channels * sizeof(jack_sample_t);
272 while (p_block->i_buffer > 0) {
274 /* move data to buffer */
275 const size_t write_space = jack_ringbuffer_write_space(rb);
276 const size_t bytes = p_block->i_buffer < write_space ?
277 p_block->i_buffer : write_space;
279 /* If our audio thread is not reading fast enough */
280 if( unlikely( bytes == 0 ) ) {
281 msg_Warn( p_aout, "%"PRIuPTR " frames of audio dropped",
282 p_block->i_buffer / bytes_per_frame );
286 jack_ringbuffer_write( rb, (const char *) p_block->p_buffer, bytes );
288 p_block->p_buffer += bytes;
289 p_block->i_buffer -= bytes;
292 block_Release(p_block);
296 * Pause or unpause playback
298 static void Pause(audio_output_t *aout, bool paused, mtime_t date)
300 aout_sys_t *sys = aout->sys;
306 msg_Dbg(aout, "resuming after %"PRId64" us", date);
307 sys->paused = VLC_TS_INVALID;
311 static void Flush(audio_output_t *p_aout, bool wait)
313 struct aout_sys_t * p_sys = p_aout->sys;
314 jack_ringbuffer_t *rb = p_sys->p_jack_ringbuffer;
316 /* Sleep if wait was requested */
320 if (!TimeGet(p_aout, &delay))
324 /* reset ringbuffer read and write pointers */
325 jack_ringbuffer_reset(rb);
328 static int TimeGet(audio_output_t *p_aout, mtime_t *delay)
330 struct aout_sys_t * p_sys = p_aout->sys;
331 jack_ringbuffer_t *rb = p_sys->p_jack_ringbuffer;
332 const size_t bytes_per_frame = p_sys->i_channels * sizeof(jack_sample_t);
334 *delay = (p_sys->latency +
335 (jack_ringbuffer_read_space(rb) / bytes_per_frame)) *
336 CLOCK_FREQ / p_sys->i_rate;
341 /*****************************************************************************
342 * Process: callback for JACK
343 *****************************************************************************/
344 int Process( jack_nframes_t i_frames, void *p_arg )
346 unsigned int i, j, frames_from_rb = 0;
347 size_t bytes_read = 0;
349 audio_output_t *p_aout = (audio_output_t*) p_arg;
350 struct aout_sys_t *p_sys = p_aout->sys;
352 /* Get the next audio data buffer unless paused */
354 if( p_sys->paused == VLC_TS_INVALID )
355 frames_from_rb = i_frames;
357 /* Get the JACK buffers to write to */
358 for( i = 0; i < p_sys->i_channels; i++ )
360 p_sys->p_jack_buffers[i] = jack_port_get_buffer( p_sys->p_jack_ports[i],
364 /* Copy in the audio data */
365 for( j = 0; j < frames_from_rb; j++ )
367 for( i = 0; i < p_sys->i_channels; i++ )
369 jack_sample_t *p_dst = p_sys->p_jack_buffers[i] + j;
370 bytes_read += jack_ringbuffer_read( p_sys->p_jack_ringbuffer,
371 (char *) p_dst, sizeof(jack_sample_t) );
375 /* Fill any remaining buffer with silence */
376 frames_read = (bytes_read / sizeof(jack_sample_t)) / p_sys->i_channels;
377 if( frames_read < i_frames )
379 for( i = 0; i < p_sys->i_channels; i++ )
381 memset( p_sys->p_jack_buffers[i] + frames_read, 0,
382 sizeof( jack_sample_t ) * (i_frames - frames_read) );
389 /*****************************************************************************
390 * GraphChange: callback when JACK reorders it's process graph.
391 We update latency information.
392 *****************************************************************************/
394 static int GraphChange( void *p_arg )
396 audio_output_t *p_aout = (audio_output_t*) p_arg;
397 struct aout_sys_t *p_sys = p_aout->sys;
399 jack_latency_range_t port_latency;
403 for( i = 0; i < p_sys->i_channels; ++i )
405 jack_port_get_latency_range( p_sys->p_jack_ports[i], JackPlaybackLatency,
407 p_sys->latency = __MAX( p_sys->latency, port_latency.max );
410 msg_Dbg(p_aout, "JACK graph reordered. Our maximum latency=%d.",
416 /*****************************************************************************
417 * Close: close the JACK client
418 *****************************************************************************/
419 static void Stop( audio_output_t *p_aout )
422 struct aout_sys_t *p_sys = p_aout->sys;
424 i_error = jack_deactivate( p_sys->p_jack_client );
427 msg_Err( p_aout, "jack_deactivate failed (error %d)", i_error );
430 i_error = jack_client_close( p_sys->p_jack_client );
433 msg_Err( p_aout, "jack_client_close failed (error %d)", i_error );
435 free( p_sys->p_jack_ports );
436 free( p_sys->p_jack_buffers );
437 jack_ringbuffer_free( p_sys->p_jack_ringbuffer );
440 static int Open(vlc_object_t *obj)
442 audio_output_t *aout = (audio_output_t *)obj;
443 aout_sys_t *sys = calloc(1, sizeof (*sys));
445 if( unlikely( sys == NULL ) )
450 aout_SoftVolumeInit(aout);
454 static void Close(vlc_object_t *obj)
456 audio_output_t *aout = (audio_output_t *)obj;
457 aout_sys_t *sys = aout->sys;