1 /*****************************************************************************
2 * jack.c: JACK audio input module
3 *****************************************************************************
4 * Copyright (C) 2007-2008 VLC authors and VideoLAN
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 it
13 * under the terms of the GNU Lesser General Public License as published by
14 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public License
23 * along with this program; if not, write to the Free Software Foundation,
24 * 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 PACE_TEXT N_( "Pace" )
60 #define PACE_LONGTEXT N_( \
61 "Read the audio stream at VLC pace rather than Jack pace." )
62 #define AUTO_CONNECT_TEXT N_( "Auto connection" )
63 #define AUTO_CONNECT_LONGTEXT N_( \
64 "Automatically connect VLC input ports to available output ports." )
67 set_description( N_("JACK audio input") )
68 set_capability( "access_demux", 0 )
69 set_shortname( N_( "JACK Input" ) )
70 set_category( CAT_INPUT
)
71 set_subcategory( SUBCAT_INPUT_ACCESS
)
73 add_bool( "jack-input-use-vlc-pace", false,
74 PACE_TEXT
, PACE_LONGTEXT
, true )
75 add_bool( "jack-input-auto-connect", false,
76 AUTO_CONNECT_TEXT
, AUTO_CONNECT_LONGTEXT
, false )
78 add_shortcut( "jack" )
79 set_callbacks( Open
, Close
)
82 /*****************************************************************************
84 *****************************************************************************/
88 /* Audio properties */
89 vlc_fourcc_t i_acodec_raw
;
90 unsigned int i_channels
;
92 int i_audio_max_frame_size
;
94 block_t
*p_block_audio
;
95 es_out_id_t
*p_es_audio
;
99 jack_client_t
*p_jack_client
;
100 jack_port_t
**pp_jack_port_input
;
101 jack_default_audio_sample_t
**pp_jack_buffer
;
102 jack_ringbuffer_t
*p_jack_ringbuffer
;
103 jack_nframes_t jack_buffer_size
;
104 jack_nframes_t jack_sample_rate
;
105 size_t jack_sample_size
;
107 char **pp_jack_port_table
;
111 static int Demux( demux_t
* );
112 static int Control( demux_t
*p_demux
, int i_query
, va_list args
);
114 static void Parse ( demux_t
* );
115 static void Port_finder( demux_t
* );
116 static int Process( jack_nframes_t i_frames
, void *p_arg
);
118 static block_t
*GrabJack( demux_t
* );
120 /*****************************************************************************
121 * Open: Connect to the JACK server
122 *****************************************************************************/
123 static int Open( vlc_object_t
*p_this
)
125 demux_t
*p_demux
= ( demux_t
* )p_this
;
130 p_demux
->pf_demux
= Demux
;
131 p_demux
->pf_control
= Control
;
133 /* Allocate structure */
134 p_demux
->p_sys
= p_sys
= calloc( 1, sizeof( demux_sys_t
) );
142 var_Create( p_demux
, "jack-input-use-vlc-pace",
143 VLC_VAR_BOOL
| VLC_VAR_DOINHERIT
);
144 var_Create( p_demux
, "jack-input-auto-connect",
145 VLC_VAR_BOOL
| VLC_VAR_DOINHERIT
);
147 /* JACK connexions */
148 /* define name and connect to jack server */
149 char p_vlc_client_name
[32];
150 sprintf( p_vlc_client_name
, "vlc-input-%d", getpid() );
151 p_sys
->p_jack_client
= jack_client_open( p_vlc_client_name
, JackNullOption
, NULL
);
152 if( p_sys
->p_jack_client
== NULL
)
154 msg_Err( p_demux
, "failed to connect to JACK server" );
159 /* find some specifics ports if user entered a regexp */
160 if( p_sys
->psz_ports
)
162 Port_finder( p_demux
);
163 if( p_sys
->i_channels
== 0 )
165 p_sys
->i_channels
= p_sys
->i_match_ports
;
169 /* allocate input ports */
170 if( p_sys
->i_channels
== 0 ) p_sys
->i_channels
= 2 ; /* default number of port */
171 p_sys
->pp_jack_port_input
= malloc(
172 p_sys
->i_channels
* sizeof( jack_port_t
* ) );
173 if( p_sys
->pp_jack_port_input
== NULL
)
175 jack_client_close( p_sys
->p_jack_client
);
180 /* allocate ringbuffer */
181 /* The length of the ringbuffer is critical, it must be large enought
182 to keep all data between 2 GrabJack() calls. We assume 1 sec is ok */
183 p_sys
->p_jack_ringbuffer
= jack_ringbuffer_create( p_sys
->i_channels
184 * jack_get_sample_rate( p_sys
->p_jack_client
)
185 * sizeof( jack_default_audio_sample_t
) );
186 if( p_sys
->p_jack_ringbuffer
== NULL
)
188 free( p_sys
->pp_jack_port_input
);
189 jack_client_close( p_sys
->p_jack_client
);
194 /* register input ports */
195 for( unsigned i
= 0; i
< p_sys
->i_channels
; i
++ )
197 char p_input_name
[32];
198 snprintf( p_input_name
, 32, "vlc_in_%d", i
+1 );
199 p_sys
->pp_jack_port_input
[i
] = jack_port_register(
200 p_sys
->p_jack_client
, p_input_name
, JACK_DEFAULT_AUDIO_TYPE
,
201 JackPortIsInput
, 0 );
202 if( p_sys
->pp_jack_port_input
[i
] == NULL
)
204 msg_Err( p_demux
, "failed to register a JACK port" );
205 jack_ringbuffer_free( p_sys
->p_jack_ringbuffer
);
206 free( p_sys
->pp_jack_port_input
);
207 jack_client_close( p_sys
->p_jack_client
);
213 /* allocate buffer for input ports */
214 p_sys
->pp_jack_buffer
= malloc ( p_sys
->i_channels
215 * sizeof( jack_default_audio_sample_t
* ) );
216 if( p_sys
->pp_jack_buffer
== NULL
)
218 for( unsigned i
= 0; i
< p_sys
->i_channels
; i
++ )
219 jack_port_unregister( p_sys
->p_jack_client
, p_sys
->pp_jack_port_input
[i
] );
220 jack_ringbuffer_free( p_sys
->p_jack_ringbuffer
);
221 free( p_sys
->pp_jack_port_input
);
222 jack_client_close( p_sys
->p_jack_client
);
227 /* set process callback */
228 jack_set_process_callback( p_sys
->p_jack_client
, Process
, p_demux
);
230 /* tell jack server we are ready */
231 if ( jack_activate( p_sys
->p_jack_client
) )
233 msg_Err( p_demux
, "failed to activate JACK client" );
234 free( p_sys
->pp_jack_buffer
);
235 for( unsigned i
= 0; i
< p_sys
->i_channels
; i
++ )
236 jack_port_unregister( p_sys
->p_jack_client
, p_sys
->pp_jack_port_input
[i
] );
237 jack_ringbuffer_free( p_sys
->p_jack_ringbuffer
);
238 free( p_sys
->pp_jack_port_input
);
239 jack_client_close( p_sys
->p_jack_client
);
244 /* connect vlc input to specifics jack output ports if requested */
245 /* if( var_GetBool( p_demux, "jack-input-auto-connect" ) && p_sys->psz_ports ) */
246 if( p_sys
->psz_ports
)
248 for( int j
= 0; j
< p_sys
->i_match_ports
; j
++ )
250 int i_input_ports
= j
% p_sys
->i_channels
;
251 jack_connect( p_sys
->p_jack_client
, p_sys
->pp_jack_port_table
[j
],
252 jack_port_name( p_sys
->pp_jack_port_input
[i_input_ports
] ) );
256 /* connect vlc input to all jack output ports if requested */
257 if( var_GetBool( p_demux
, "jack-input-auto-connect" ) && !p_sys
->psz_ports
)
259 const char **pp_jack_port_output
= jack_get_ports( p_sys
->p_jack_client
,
260 NULL
, NULL
, JackPortIsOutput
);
262 while( pp_jack_port_output
&& pp_jack_port_output
[i_out_ports
] )
267 for( int j
= 0; j
< i_out_ports
; j
++ )
269 int i_input_ports
= j
% p_sys
->i_channels
;
270 jack_connect( p_sys
->p_jack_client
, pp_jack_port_output
[j
],
271 jack_port_name( p_sys
->pp_jack_port_input
[i_input_ports
] ) );
274 free( pp_jack_port_output
);
277 /* info about jack server */
278 /* get buffers size */
279 p_sys
->jack_buffer_size
= jack_get_buffer_size( p_sys
->p_jack_client
);
280 /* get sample rate */
281 p_sys
->jack_sample_rate
= jack_get_sample_rate( p_sys
->p_jack_client
);
282 /* get sample size */
283 p_sys
->jack_sample_size
= sizeof( jack_default_audio_sample_t
);
285 /* Define output format */
286 es_format_Init( &fmt
, AUDIO_ES
, VLC_CODEC_FL32
);
287 fmt
.audio
.i_channels
= p_sys
->i_channels
;
288 fmt
.audio
.i_rate
= p_sys
->jack_sample_rate
;
289 fmt
.audio
.i_bitspersample
= p_sys
->jack_sample_size
* 8;
290 fmt
.audio
.i_blockalign
= fmt
.audio
.i_bitspersample
/ 8;
291 fmt
.i_bitrate
= fmt
.audio
.i_rate
* fmt
.audio
.i_bitspersample
292 * fmt
.audio
.i_channels
;
294 p_sys
->p_es_audio
= es_out_Add( p_demux
->out
, &fmt
);
295 date_Init( &p_sys
->pts
, fmt
.audio
.i_rate
, 1 );
296 date_Set( &p_sys
->pts
, 1 );
302 /*****************************************************************************
303 * Close: Disconnect from jack server and release associated resources
304 *****************************************************************************/
305 static void Close( vlc_object_t
*p_this
)
307 demux_t
*p_demux
= ( demux_t
* )p_this
;
308 demux_sys_t
*p_sys
= p_demux
->p_sys
;
310 msg_Dbg( p_demux
,"Module unloaded" );
311 if( p_sys
->p_block_audio
) block_Release( p_sys
->p_block_audio
);
312 if( p_sys
->p_jack_client
) jack_client_close( p_sys
->p_jack_client
);
313 if( p_sys
->p_jack_ringbuffer
) jack_ringbuffer_free( p_sys
->p_jack_ringbuffer
);
314 free( p_sys
->pp_jack_port_input
);
315 free( p_sys
->pp_jack_buffer
);
316 free( p_sys
->pp_jack_port_table
);
321 /*****************************************************************************
323 *****************************************************************************/
324 static int Control( demux_t
*p_demux
, int i_query
, va_list args
)
328 demux_sys_t
*p_sys
= p_demux
->p_sys
;
332 /* Special for access_demux */
333 case DEMUX_CAN_PAUSE
:
335 pb
= va_arg( args
, bool * );
339 case DEMUX_SET_PAUSE_STATE
:
341 case DEMUX_CAN_CONTROL_PACE
:
342 pb
= va_arg( args
, bool * );
343 *pb
= var_GetBool( p_demux
, "jack-input-use-vlc-pace" );
346 case DEMUX_GET_PTS_DELAY
:
347 pi64
= va_arg( args
, int64_t * );
348 *pi64
= INT64_C(1000) * var_InheritInteger( p_demux
, "live-caching" );
352 pi64
= va_arg( args
, int64_t * );
353 *pi64
= date_Get(&p_sys
->pts
);
356 /* TODO implement others */
365 /*****************************************************************************
367 *****************************************************************************/
368 static int Demux( demux_t
*p_demux
)
374 p_sys
= p_demux
->p_sys
;
375 p_es
= p_sys
->p_es_audio
;
376 p_block
= GrabJack( p_demux
);
380 es_out_Control( p_demux
->out
, ES_OUT_SET_PCR
, p_block
->i_pts
);
381 es_out_Send( p_demux
->out
, p_es
, p_block
);
388 /*****************************************************************************
389 * Process Callback : fill ringbuffer with Jack audio data
390 *****************************************************************************/
391 int Process( jack_nframes_t i_frames
, void *p_arg
)
393 demux_t
*p_demux
= ( demux_t
* )p_arg
;
394 demux_sys_t
*p_sys
= p_demux
->p_sys
;
396 /* Get and interlace buffers */
397 for ( unsigned i
= 0; i
< p_sys
->i_channels
; i
++ )
399 p_sys
->pp_jack_buffer
[i
] = jack_port_get_buffer(
400 p_sys
->pp_jack_port_input
[i
], i_frames
);
403 /* fill ring buffer with signal */
404 for( unsigned j
= 0; j
< i_frames
; j
++ )
406 for( unsigned i
= 0; i
<p_sys
->i_channels
; i
++ )
408 if( jack_ringbuffer_write_space( p_sys
->p_jack_ringbuffer
) <
409 p_sys
->jack_sample_size
) {
410 msg_Err( p_demux
, "buffer overflow");
411 return 0; // buffer overflow
413 size_t i_write
= jack_ringbuffer_write( p_sys
->p_jack_ringbuffer
,
414 ( char * ) (p_sys
->pp_jack_buffer
[i
]+j
),
415 p_sys
->jack_sample_size
);
416 if (i_write
!= p_sys
->jack_sample_size
) {
417 msg_Warn( p_demux
, "error writing on ring buffer");
426 /*****************************************************************************
427 * GrabJack: grab audio data in the Jack buffer
428 *****************************************************************************/
429 static block_t
*GrabJack( demux_t
*p_demux
)
431 demux_sys_t
*p_sys
= p_demux
->p_sys
;
434 /* read signal from ring buffer */
435 size_t i_read
= jack_ringbuffer_read_space( p_sys
->p_jack_ringbuffer
);
437 if( i_read
< 100 ) /* avoid small read */
438 { /* vlc has too much free time on its hands? */
440 #warning Hmm.... looks wrong
445 if( p_sys
->p_block_audio
)
447 p_block
= p_sys
->p_block_audio
;
451 p_block
= block_Alloc( i_read
);
455 msg_Warn( p_demux
, "cannot get block" );
459 //Find the previous power of 2, this algo assumes size_t has the same size on all arch
462 i_read
|= i_read
>> 1;
463 i_read
|= i_read
>> 2;
464 i_read
|= i_read
>> 4;
465 i_read
|= i_read
>> 8;
466 i_read
|= i_read
>> 16;
469 i_read
= jack_ringbuffer_read( p_sys
->p_jack_ringbuffer
, ( char * ) p_block
->p_buffer
, i_read
);
471 p_block
->i_dts
= p_block
->i_pts
= date_Increment( &p_sys
->pts
,
472 i_read
/(p_sys
->i_channels
* p_sys
->jack_sample_size
) );
474 p_sys
->p_block_audio
= p_block
;
475 p_block
->i_buffer
= i_read
;
476 p_sys
->p_block_audio
= 0;
482 /*****************************************************************************
483 * Port_finder: compare ports with the regexp entered
484 *****************************************************************************/
485 static void Port_finder( demux_t
*p_demux
)
487 demux_sys_t
*p_sys
= p_demux
->p_sys
;
488 char *psz_expr
= p_sys
->psz_ports
;
491 char *psz_uri
= NULL
;
492 const char **pp_jack_port_output
= NULL
;
494 int i_total_out_ports
=0;
495 p_sys
->pp_jack_port_table
= NULL
;
497 /* parse the ports part of the MRL */
498 for( token
= strtok_r( psz_expr
, ",", &state
); token
;
499 token
= strtok_r( NULL
, ",", &state
) )
501 psz_uri
= vlc_uri_decode_duplicate( token
);
502 /* get the ports which match the regexp */
503 pp_jack_port_output
= jack_get_ports( p_sys
->p_jack_client
,
504 psz_uri
, NULL
, JackPortIsOutput
);
505 if( pp_jack_port_output
== NULL
)
506 msg_Err( p_demux
, "port(s) asked not found:%s", psz_uri
);
509 while( pp_jack_port_output
[i_out_ports
] )
511 /* alloc an array to store all the matched ports */
512 p_sys
->pp_jack_port_table
= xrealloc( p_sys
->pp_jack_port_table
,
513 (i_out_ports
* sizeof( char * ) + i_total_out_ports
* sizeof( char * ) ) );
515 for(int i
=0; i
<i_out_ports
;i
++)
516 p_sys
->pp_jack_port_table
[i_total_out_ports
+i
] = ( char * ) pp_jack_port_output
[i
];
518 i_total_out_ports
+= i_out_ports
;
520 free( pp_jack_port_output
);
524 p_sys
->i_match_ports
= i_total_out_ports
;
528 /*****************************************************************************
529 * Parse: Parse the MRL
530 *****************************************************************************/
531 static void Parse( demux_t
*p_demux
)
533 demux_sys_t
*p_sys
= p_demux
->p_sys
;
534 char *psz_dup
= strdup( p_demux
->psz_location
);
535 char *psz_parser
= psz_dup
;
537 if( !strncmp( psz_parser
, "channels=", strlen( "channels=" ) ) )
539 p_sys
->i_channels
= abs( strtol( psz_parser
+ strlen( "channels=" ),
542 else if( !strncmp( psz_parser
, "ports=", strlen( "ports=" ) ) )
545 psz_parser
+= strlen( "ports=" );
546 if( strchr( psz_parser
, ':' ) )
548 i_len
= strchr( psz_parser
, ':' ) - psz_parser
;
552 i_len
= strlen( psz_parser
);
554 p_sys
->psz_ports
= strndup( psz_parser
, i_len
);
559 msg_Warn( p_demux
, "unknown option" );
562 while( *psz_parser
&& *psz_parser
!= ':' )
567 if( *psz_parser
== ':' )
571 *psz_parser
++ = '\0';
572 if( !strncmp( psz_parser
, "channels=", strlen( "channels=" ) ) )
574 p_sys
->i_channels
= abs( strtol(
575 psz_parser
+ strlen( "channels=" ), &psz_parser
, 0 ) );
577 else if( !strncmp( psz_parser
, "ports=", strlen( "ports=" ) ) )
580 psz_parser
+= strlen( "ports=" );
581 if( strchr( psz_parser
, ':' ) )
583 i_len
= strchr( psz_parser
, ':' ) - psz_parser
;
587 i_len
= strlen( psz_parser
);
589 p_sys
->psz_ports
= strndup( psz_parser
, i_len
);
594 msg_Warn( p_demux
, "unknown option" );
596 while( *psz_parser
&& *psz_parser
!= ':' )
601 if( *psz_parser
== '\0' )