1 /*****************************************************************************
2 * jack.c: JACK audio input module
3 *****************************************************************************
4 * Copyright (C) 2007-2008 the VideoLAN team
5 * Copyright (C) 2007 Société des arts technologiques
6 * Copyright (C) 2007 Savoir-faire Linux
8 * Authors: Arnaud Sala <arnaud.sala at savoirfairelinux.com>
9 * Julien Plissonneau Duquene <... at savoirfairelinux.com>
10 * Pierre-Luc Beaudoin <pierre-luc.beaudoin at savoirfairelinux.com>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
28 * \file modules/access/jack.c
29 * \brief JACK audio input functions
32 /*****************************************************************************
34 *****************************************************************************/
40 #include <vlc_common.h>
41 #include <vlc_plugin.h>
42 #include <vlc_input.h>
43 #include <vlc_demux.h>
45 #include <vlc_strings.h>
47 #include <jack/jack.h>
48 #include <jack/ringbuffer.h>
50 #include <sys/types.h>
53 /*****************************************************************************
55 *****************************************************************************/
56 static int Open ( vlc_object_t * );
57 static void Close( vlc_object_t * );
59 #define CACHING_TEXT N_("Caching value in ms")
60 #define CACHING_LONGTEXT N_( \
61 "Make VLC buffer audio data captured from jack for the specified " \
62 "length in milliseconds." )
63 #define PACE_TEXT N_( "Pace" )
64 #define PACE_LONGTEXT N_( \
65 "Read the audio stream at VLC pace rather than Jack pace." )
66 #define AUTO_CONNECT_TEXT N_( "Auto Connection" )
67 #define AUTO_CONNECT_LONGTEXT N_( \
68 "Automatically connect VLC input ports to available output ports." )
71 set_description( N_("JACK audio input") )
72 set_capability( "access_demux", 0 )
73 set_shortname( N_( "JACK Input" ) )
74 set_category( CAT_INPUT )
75 set_subcategory( SUBCAT_INPUT_ACCESS )
77 add_integer( "jack-input-caching", DEFAULT_PTS_DELAY / 1000, NULL,
78 CACHING_TEXT, CACHING_LONGTEXT, true )
79 add_bool( "jack-input-use-vlc-pace", false, NULL,
80 PACE_TEXT, PACE_LONGTEXT, true )
81 add_bool( "jack-input-auto-connect", false, NULL,
82 AUTO_CONNECT_TEXT, AUTO_CONNECT_LONGTEXT, true )
84 add_shortcut( "jack" )
85 set_callbacks( Open, Close )
88 /*****************************************************************************
90 *****************************************************************************/
94 /* Audio properties */
95 vlc_fourcc_t i_acodec_raw;
96 unsigned int i_channels;
98 int i_audio_max_frame_size;
100 block_t *p_block_audio;
101 es_out_id_t *p_es_audio;
104 /* Jack properties */
105 jack_client_t *p_jack_client;
106 jack_port_t **pp_jack_port_input;
107 jack_default_audio_sample_t **pp_jack_buffer;
108 jack_ringbuffer_t *p_jack_ringbuffer;
109 jack_nframes_t jack_buffer_size;
110 jack_nframes_t jack_sample_rate;
111 size_t jack_sample_size;
113 char **pp_jack_port_table;
117 static int Demux( demux_t * );
118 static int Control( demux_t *p_demux, int i_query, va_list args );
120 static void Parse ( demux_t * );
121 static void Port_finder( demux_t * );
122 static int Process( jack_nframes_t i_frames, void *p_arg );
124 static block_t *GrabJack( demux_t * );
126 /*****************************************************************************
127 * Open: Connect to the JACK server
128 *****************************************************************************/
129 static int Open( vlc_object_t *p_this )
132 demux_t *p_demux = ( demux_t* )p_this;
137 p_demux->pf_demux = Demux;
138 p_demux->pf_control = Control;
140 /* Allocate structure */
141 p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
149 var_Create( p_demux, "jack-input-caching",
150 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
151 var_Create( p_demux, "jack-input-use-vlc-pace",
152 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
153 var_Create( p_demux, "jack-input-auto-connect",
154 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
156 /* JACK connexions */
157 /* define name and connect to jack server */
158 char p_vlc_client_name[32];
159 sprintf( p_vlc_client_name, "vlc-input-%d", getpid() );
160 p_sys->p_jack_client = jack_client_new( p_vlc_client_name );
161 if( p_sys->p_jack_client == NULL )
163 msg_Err( p_demux, "failed to connect to JACK server" );
168 /* find some specifics ports if user entered a regexp */
169 if( p_sys->psz_ports )
171 Port_finder( p_demux );
172 if( p_sys->i_channels == 0 )
174 p_sys->i_channels = p_sys->i_match_ports;
178 /* allocate input ports */
179 if( p_sys->i_channels == 0 ) p_sys->i_channels = 2 ; /* default number of port */
180 p_sys->pp_jack_port_input = malloc(
181 p_sys->i_channels * sizeof( jack_port_t* ) );
182 if( p_sys->pp_jack_port_input == NULL )
184 jack_client_close( p_sys->p_jack_client );
189 /* allocate ringbuffer */
190 /* The length of the ringbuffer is critical, it must be large enought
191 to keep all data between 2 GrabJack() calls. We assume 1 sec is ok */
192 p_sys->p_jack_ringbuffer = jack_ringbuffer_create( p_sys->i_channels
193 * jack_get_sample_rate( p_sys->p_jack_client )
194 * sizeof( jack_default_audio_sample_t ) );
195 if( p_sys->p_jack_ringbuffer == NULL )
197 free( p_sys->pp_jack_port_input );
198 jack_client_close( p_sys->p_jack_client );
203 /* register input ports */
204 for( i = 0; i < p_sys->i_channels; i++ )
206 char p_input_name[32];
207 snprintf( p_input_name, 32, "vlc_in_%d", i+1 );
208 p_sys->pp_jack_port_input[i] = jack_port_register(
209 p_sys->p_jack_client, p_input_name, JACK_DEFAULT_AUDIO_TYPE,
210 JackPortIsInput, 0 );
211 if( p_sys->pp_jack_port_input[i] == NULL )
213 msg_Err( p_demux, "failed to register a JACK port" );
214 jack_ringbuffer_free( p_sys->p_jack_ringbuffer );
215 free( p_sys->pp_jack_port_input );
216 jack_client_close( p_sys->p_jack_client );
222 /* allocate buffer for input ports */
223 p_sys->pp_jack_buffer = malloc ( p_sys->i_channels
224 * sizeof( jack_default_audio_sample_t * ) );
225 if( p_sys->pp_jack_buffer == NULL )
227 for( i = 0; i < p_sys->i_channels; i++ )
228 jack_port_unregister( p_sys->p_jack_client, p_sys->pp_jack_port_input[i] );
229 jack_ringbuffer_free( p_sys->p_jack_ringbuffer );
230 free( p_sys->pp_jack_port_input );
231 jack_client_close( p_sys->p_jack_client );
236 /* set process callback */
237 jack_set_process_callback( p_sys->p_jack_client, Process, p_demux );
239 /* tell jack server we are ready */
240 if ( jack_activate( p_sys->p_jack_client ) )
242 msg_Err( p_demux, "failed to activate JACK client" );
243 free( p_sys->pp_jack_buffer );
244 for( i = 0; i < p_sys->i_channels; i++ )
245 jack_port_unregister( p_sys->p_jack_client, p_sys->pp_jack_port_input[i] );
246 jack_ringbuffer_free( p_sys->p_jack_ringbuffer );
247 free( p_sys->pp_jack_port_input );
248 jack_client_close( p_sys->p_jack_client );
253 /* connect vlc input to specifics jack output ports if requested */
254 /* if( var_GetBool( p_demux, "jack-input-auto-connect" ) && p_sys->psz_ports ) */
255 if( p_sys->psz_ports )
260 if( p_sys->i_match_ports > 0 )
262 for( j = 0; j < p_sys->i_match_ports; j++ )
264 i_input_ports = j % p_sys->i_channels;
265 jack_connect( p_sys->p_jack_client, p_sys->pp_jack_port_table[j],
266 jack_port_name( p_sys->pp_jack_port_input[i_input_ports] ) );
271 /* connect vlc input to all jack output ports if requested */
272 if( var_GetBool( p_demux, "jack-input-auto-connect" ) && !p_sys->psz_ports )
276 const char **pp_jack_port_output;
278 pp_jack_port_output = jack_get_ports( p_sys->p_jack_client, NULL, NULL, JackPortIsOutput );
280 while( pp_jack_port_output && pp_jack_port_output[i_out_ports] )
284 if( i_out_ports > 0 )
286 for( j = 0; j < i_out_ports; j++ )
288 i_input_ports = j % p_sys->i_channels;
289 jack_connect( p_sys->p_jack_client, pp_jack_port_output[j],
290 jack_port_name( p_sys->pp_jack_port_input[i_input_ports] ) );
293 free( pp_jack_port_output );
296 /* info about jack server */
297 /* get buffers size */
298 p_sys->jack_buffer_size = jack_get_buffer_size( p_sys->p_jack_client );
299 /* get sample rate */
300 p_sys->jack_sample_rate = jack_get_sample_rate( p_sys->p_jack_client );
301 /* get sample size */
302 p_sys->jack_sample_size = sizeof( jack_default_audio_sample_t );
304 /* Define output format */
305 es_format_Init( &fmt, AUDIO_ES, VLC_CODEC_FL32 );
306 fmt.audio.i_channels = p_sys->i_channels;
307 fmt.audio.i_rate = p_sys->jack_sample_rate;
308 fmt.audio.i_bitspersample = p_sys->jack_sample_size * 8;
309 fmt.audio.i_blockalign = fmt.audio.i_bitspersample / 8;
310 fmt.i_bitrate = fmt.audio.i_rate * fmt.audio.i_bitspersample
311 * fmt.audio.i_channels;
313 p_sys->p_es_audio = es_out_Add( p_demux->out, &fmt );
314 date_Init( &p_sys->pts, fmt.audio.i_rate, 1 );
315 date_Set( &p_sys->pts, 1 );
321 /*****************************************************************************
322 * Close: Disconnect from jack server and release associated resources
323 *****************************************************************************/
324 static void Close( vlc_object_t *p_this )
326 demux_t *p_demux = ( demux_t* )p_this;
327 demux_sys_t *p_sys = p_demux->p_sys;
329 msg_Dbg( p_demux,"Module unloaded" );
330 if( p_sys->p_block_audio ) block_Release( p_sys->p_block_audio );
331 if( p_sys->p_jack_client ) jack_client_close( p_sys->p_jack_client );
332 if( p_sys->p_jack_ringbuffer ) jack_ringbuffer_free( p_sys->p_jack_ringbuffer );
333 free( p_sys->pp_jack_port_input );
334 free( p_sys->pp_jack_buffer );
335 free( p_sys->pp_jack_port_table );
340 /*****************************************************************************
342 *****************************************************************************/
343 static int Control( demux_t *p_demux, int i_query, va_list args )
347 demux_sys_t *p_sys = p_demux->p_sys;
351 /* Special for access_demux */
352 case DEMUX_CAN_PAUSE:
354 pb = (bool *)va_arg( args, bool * );
358 case DEMUX_SET_PAUSE_STATE:
360 case DEMUX_CAN_CONTROL_PACE:
361 pb = ( bool* )va_arg( args, bool * );
362 *pb = var_GetBool( p_demux, "jack-input-use-vlc-pace" );
365 case DEMUX_GET_PTS_DELAY:
366 pi64 = ( int64_t* )va_arg( args, int64_t * );
367 *pi64 = var_GetInteger( p_demux, "jack-input-caching" ) * 1000;
371 pi64 = ( int64_t* )va_arg( args, int64_t * );
372 *pi64 = date_Get(&p_sys->pts);
375 /* TODO implement others */
384 /*****************************************************************************
386 *****************************************************************************/
387 static int Demux( demux_t *p_demux )
394 p_sys = p_demux->p_sys;
395 p_es = p_sys->p_es_audio;
396 p_block = GrabJack( p_demux );
400 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
401 es_out_Send( p_demux->out, p_es, p_block );
408 /*****************************************************************************
409 * Process Callback : fill ringbuffer with Jack audio data
410 *****************************************************************************/
411 int Process( jack_nframes_t i_frames, void *p_arg )
413 demux_t *p_demux = ( demux_t* )p_arg;
414 demux_sys_t *p_sys = p_demux->p_sys;
418 /* Get and interlace buffers */
419 for ( i = 0; i < p_sys->i_channels ; i++ )
421 p_sys->pp_jack_buffer[i] = jack_port_get_buffer(
422 p_sys->pp_jack_port_input[i], i_frames );
425 /* fill ring buffer with signal */
426 for( j = 0; j < i_frames; j++ )
428 for( i = 0; i <p_sys->i_channels; i++ )
430 if( jack_ringbuffer_write_space( p_sys->p_jack_ringbuffer ) <
431 p_sys->jack_sample_size ) {
432 msg_Err( p_demux, "buffer overflow");
433 return 0; // buffer overflow
435 i_write = jack_ringbuffer_write( p_sys->p_jack_ringbuffer,
436 ( char * ) (p_sys->pp_jack_buffer[i]+j),
437 p_sys->jack_sample_size );
438 if (i_write != p_sys->jack_sample_size ) {
439 msg_Warn( p_demux, "error writing on ring buffer");
448 /*****************************************************************************
449 * GrabJack: grab audio data in the Jack buffer
450 *****************************************************************************/
451 static block_t *GrabJack( demux_t *p_demux )
454 demux_sys_t *p_sys = p_demux->p_sys;
457 /* read signal from ring buffer */
458 i_read = jack_ringbuffer_read_space( p_sys->p_jack_ringbuffer );
460 if( i_read < 100 ) /* avoid small read */
461 { /* vlc has too much free time on its hands? */
463 #warning Hmm.... looks wrong
468 if( p_sys->p_block_audio )
470 p_block = p_sys->p_block_audio;
474 p_block = block_New( p_demux, i_read );
478 msg_Warn( p_demux, "cannot get block" );
482 //Find the previous power of 2, this algo assumes size_t has the same size on all arch
485 i_read |= i_read >> 1;
486 i_read |= i_read >> 2;
487 i_read |= i_read >> 4;
488 i_read |= i_read >> 8;
489 i_read |= i_read >> 16;
492 i_read = jack_ringbuffer_read( p_sys->p_jack_ringbuffer, ( char * ) p_block->p_buffer, i_read );
494 p_block->i_dts = p_block->i_pts = date_Increment( &p_sys->pts,
495 i_read/(p_sys->i_channels * p_sys->jack_sample_size) );
497 p_sys->p_block_audio = p_block;
498 p_block->i_buffer = i_read;
499 p_sys->p_block_audio = 0;
505 /*****************************************************************************
506 * Port_finder: compare ports with the regexp entered
507 *****************************************************************************/
508 static void Port_finder( demux_t *p_demux )
511 demux_sys_t *p_sys = p_demux->p_sys;
512 char *psz_expr = p_sys->psz_ports;
515 char *psz_uri = NULL;
516 const char **pp_jack_port_output = NULL;
518 int i_total_out_ports =0;
519 p_sys->pp_jack_port_table = NULL;
521 /* parse the ports part of the MRL */
522 for( token = strtok_r( psz_expr, ",", &state ); token;
523 token = strtok_r( NULL, ",", &state ) )
525 psz_uri = decode_URI_duplicate( token );
526 /* get the ports which match the regexp */
527 pp_jack_port_output = jack_get_ports( p_sys->p_jack_client,
528 psz_uri, NULL, JackPortIsOutput );
529 if( pp_jack_port_output == NULL )
531 msg_Err( p_demux, "port(s) asked not found:%s", psz_uri );
532 free( pp_jack_port_output );
536 while( pp_jack_port_output && pp_jack_port_output[i_out_ports] )
540 /* alloc an array to store all the matched ports */
541 p_sys->pp_jack_port_table = xrealloc( p_sys->pp_jack_port_table,
542 (i_out_ports * sizeof( char * ) + i_total_out_ports * sizeof( char * ) ) );
544 for(int i=0; i<i_out_ports;i++)
546 p_sys->pp_jack_port_table[i_total_out_ports+i] = ( char * ) pp_jack_port_output[i];
549 i_total_out_ports += i_out_ports;
553 free( pp_jack_port_output );
554 p_sys->i_match_ports = i_total_out_ports;
558 /*****************************************************************************
559 * Parse: Parse the MRL
560 *****************************************************************************/
561 static void Parse( demux_t *p_demux )
563 demux_sys_t *p_sys = p_demux->p_sys;
564 char *psz_dup = strdup( p_demux->psz_location );
565 char *psz_parser = psz_dup;
567 if( !strncmp( psz_parser, "channels=", strlen( "channels=" ) ) )
569 p_sys->i_channels = abs( strtol( psz_parser + strlen( "channels=" ),
572 else if( !strncmp( psz_parser, "ports=", strlen( "ports=" ) ) )
575 psz_parser += strlen( "ports=" );
576 if( strchr( psz_parser, ':' ) )
578 i_len = strchr( psz_parser, ':' ) - psz_parser;
582 i_len = strlen( psz_parser );
584 p_sys->psz_ports = strndup( psz_parser, i_len );
589 msg_Warn( p_demux, "unknown option" );
592 while( *psz_parser && *psz_parser != ':' )
597 if( *psz_parser == ':' )
601 *psz_parser++ = '\0';
602 if( !strncmp( psz_parser, "channels=", strlen( "channels=" ) ) )
604 p_sys->i_channels = abs( strtol(
605 psz_parser + strlen( "channels=" ), &psz_parser, 0 ) );
607 else if( !strncmp( psz_parser, "ports=", strlen( "ports=" ) ) )
610 psz_parser += strlen( "ports=" );
611 if( strchr( psz_parser, ':' ) )
613 i_len = strchr( psz_parser, ':' ) - psz_parser;
617 i_len = strlen( psz_parser );
619 p_sys->psz_ports = strndup( psz_parser, i_len );
624 msg_Warn( p_demux, "unknown option" );
626 while( *psz_parser && *psz_parser != ':' )
631 if( *psz_parser == '\0' )