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", 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 if (p_demux
->out
== NULL
)
133 p_demux
->pf_demux
= Demux
;
134 p_demux
->pf_control
= Control
;
136 /* Allocate structure */
137 p_demux
->p_sys
= p_sys
= vlc_obj_calloc( p_this
, 1, sizeof( demux_sys_t
) );
145 var_Create( p_demux
, "jack-input-use-vlc-pace",
146 VLC_VAR_BOOL
| VLC_VAR_DOINHERIT
);
147 var_Create( p_demux
, "jack-input-auto-connect",
148 VLC_VAR_BOOL
| VLC_VAR_DOINHERIT
);
150 /* JACK connexions */
151 /* define name and connect to jack server */
152 char p_vlc_client_name
[32];
153 sprintf( p_vlc_client_name
, "vlc-input-%d", getpid() );
154 p_sys
->p_jack_client
= jack_client_open( p_vlc_client_name
, JackNullOption
, NULL
);
155 if( p_sys
->p_jack_client
== NULL
)
157 msg_Err( p_demux
, "failed to connect to JACK server" );
161 /* find some specifics ports if user entered a regexp */
162 if( p_sys
->psz_ports
)
164 Port_finder( p_demux
);
165 if( p_sys
->i_channels
== 0 )
167 p_sys
->i_channels
= p_sys
->i_match_ports
;
171 /* allocate input ports */
172 if( p_sys
->i_channels
== 0 ) p_sys
->i_channels
= 2 ; /* default number of port */
173 p_sys
->pp_jack_port_input
= malloc(
174 p_sys
->i_channels
* sizeof( jack_port_t
* ) );
175 if( p_sys
->pp_jack_port_input
== NULL
)
177 jack_client_close( p_sys
->p_jack_client
);
181 /* allocate ringbuffer */
182 /* The length of the ringbuffer is critical, it must be large enought
183 to keep all data between 2 GrabJack() calls. We assume 1 sec is ok */
184 p_sys
->p_jack_ringbuffer
= jack_ringbuffer_create( p_sys
->i_channels
185 * jack_get_sample_rate( p_sys
->p_jack_client
)
186 * sizeof( jack_default_audio_sample_t
) );
187 if( p_sys
->p_jack_ringbuffer
== NULL
)
189 free( p_sys
->pp_jack_port_input
);
190 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
);
212 /* allocate buffer for input ports */
213 p_sys
->pp_jack_buffer
= malloc ( p_sys
->i_channels
214 * sizeof( jack_default_audio_sample_t
* ) );
215 if( p_sys
->pp_jack_buffer
== NULL
)
217 for( unsigned i
= 0; i
< p_sys
->i_channels
; i
++ )
218 jack_port_unregister( p_sys
->p_jack_client
, p_sys
->pp_jack_port_input
[i
] );
219 jack_ringbuffer_free( p_sys
->p_jack_ringbuffer
);
220 free( p_sys
->pp_jack_port_input
);
221 jack_client_close( p_sys
->p_jack_client
);
225 /* set process callback */
226 jack_set_process_callback( p_sys
->p_jack_client
, Process
, p_demux
);
228 /* tell jack server we are ready */
229 if ( jack_activate( p_sys
->p_jack_client
) )
231 msg_Err( p_demux
, "failed to activate JACK client" );
232 free( p_sys
->pp_jack_buffer
);
233 for( unsigned i
= 0; i
< p_sys
->i_channels
; i
++ )
234 jack_port_unregister( p_sys
->p_jack_client
, p_sys
->pp_jack_port_input
[i
] );
235 jack_ringbuffer_free( p_sys
->p_jack_ringbuffer
);
236 free( p_sys
->pp_jack_port_input
);
237 jack_client_close( p_sys
->p_jack_client
);
241 /* connect vlc input to specifics jack output ports if requested */
242 /* if( var_GetBool( p_demux, "jack-input-auto-connect" ) && p_sys->psz_ports ) */
243 if( p_sys
->psz_ports
)
245 for( int j
= 0; j
< p_sys
->i_match_ports
; j
++ )
247 int i_input_ports
= j
% p_sys
->i_channels
;
248 jack_connect( p_sys
->p_jack_client
, p_sys
->pp_jack_port_table
[j
],
249 jack_port_name( p_sys
->pp_jack_port_input
[i_input_ports
] ) );
253 /* connect vlc input to all jack output ports if requested */
254 if( var_GetBool( p_demux
, "jack-input-auto-connect" ) && !p_sys
->psz_ports
)
256 const char **pp_jack_port_output
= jack_get_ports( p_sys
->p_jack_client
,
257 NULL
, NULL
, JackPortIsOutput
);
259 while( pp_jack_port_output
&& pp_jack_port_output
[i_out_ports
] )
264 for( int j
= 0; j
< i_out_ports
; j
++ )
266 int i_input_ports
= j
% p_sys
->i_channels
;
267 jack_connect( p_sys
->p_jack_client
, pp_jack_port_output
[j
],
268 jack_port_name( p_sys
->pp_jack_port_input
[i_input_ports
] ) );
271 free( pp_jack_port_output
);
274 /* info about jack server */
275 /* get buffers size */
276 p_sys
->jack_buffer_size
= jack_get_buffer_size( p_sys
->p_jack_client
);
277 /* get sample rate */
278 p_sys
->jack_sample_rate
= jack_get_sample_rate( p_sys
->p_jack_client
);
279 /* get sample size */
280 p_sys
->jack_sample_size
= sizeof( jack_default_audio_sample_t
);
282 /* Define output format */
283 es_format_Init( &fmt
, AUDIO_ES
, VLC_CODEC_FL32
);
284 fmt
.audio
.i_channels
= p_sys
->i_channels
;
285 fmt
.audio
.i_rate
= p_sys
->jack_sample_rate
;
286 fmt
.audio
.i_bitspersample
= p_sys
->jack_sample_size
* 8;
287 fmt
.audio
.i_blockalign
= fmt
.audio
.i_bitspersample
/ 8;
288 fmt
.i_bitrate
= fmt
.audio
.i_rate
* fmt
.audio
.i_bitspersample
289 * fmt
.audio
.i_channels
;
291 p_sys
->p_es_audio
= es_out_Add( p_demux
->out
, &fmt
);
292 date_Init( &p_sys
->pts
, fmt
.audio
.i_rate
, 1 );
293 date_Set( &p_sys
->pts
, VLC_TICK_0
);
299 /*****************************************************************************
300 * Close: Disconnect from jack server and release associated resources
301 *****************************************************************************/
302 static void Close( vlc_object_t
*p_this
)
304 demux_t
*p_demux
= ( demux_t
* )p_this
;
305 demux_sys_t
*p_sys
= p_demux
->p_sys
;
307 msg_Dbg( p_demux
,"Module unloaded" );
308 if( p_sys
->p_block_audio
) block_Release( p_sys
->p_block_audio
);
309 if( p_sys
->p_jack_client
) jack_client_close( p_sys
->p_jack_client
);
310 if( p_sys
->p_jack_ringbuffer
) jack_ringbuffer_free( p_sys
->p_jack_ringbuffer
);
311 free( p_sys
->pp_jack_port_input
);
312 free( p_sys
->pp_jack_buffer
);
313 free( p_sys
->pp_jack_port_table
);
317 /*****************************************************************************
319 *****************************************************************************/
320 static int Control( demux_t
*p_demux
, int i_query
, va_list args
)
323 demux_sys_t
*p_sys
= p_demux
->p_sys
;
327 /* Special for access_demux */
328 case DEMUX_CAN_PAUSE
:
330 pb
= va_arg( args
, bool * );
334 case DEMUX_SET_PAUSE_STATE
:
336 case DEMUX_CAN_CONTROL_PACE
:
337 pb
= va_arg( args
, bool * );
338 *pb
= var_GetBool( p_demux
, "jack-input-use-vlc-pace" );
341 case DEMUX_GET_PTS_DELAY
:
342 *va_arg( args
, vlc_tick_t
* ) =
343 VLC_TICK_FROM_MS( var_InheritInteger( p_demux
, "live-caching" ) );
347 *va_arg( args
, vlc_tick_t
* ) = date_Get(&p_sys
->pts
);
350 /* TODO implement others */
359 /*****************************************************************************
361 *****************************************************************************/
362 static int Demux( demux_t
*p_demux
)
368 p_sys
= p_demux
->p_sys
;
369 p_es
= p_sys
->p_es_audio
;
370 p_block
= GrabJack( p_demux
);
374 es_out_SetPCR( p_demux
->out
, p_block
->i_pts
);
375 es_out_Send( p_demux
->out
, p_es
, p_block
);
382 /*****************************************************************************
383 * Process Callback : fill ringbuffer with Jack audio data
384 *****************************************************************************/
385 int Process( jack_nframes_t i_frames
, void *p_arg
)
387 demux_t
*p_demux
= ( demux_t
* )p_arg
;
388 demux_sys_t
*p_sys
= p_demux
->p_sys
;
390 /* Get and interlace buffers */
391 for ( unsigned i
= 0; i
< p_sys
->i_channels
; i
++ )
393 p_sys
->pp_jack_buffer
[i
] = jack_port_get_buffer(
394 p_sys
->pp_jack_port_input
[i
], i_frames
);
397 /* fill ring buffer with signal */
398 for( unsigned j
= 0; j
< i_frames
; j
++ )
400 for( unsigned i
= 0; i
<p_sys
->i_channels
; i
++ )
402 if( jack_ringbuffer_write_space( p_sys
->p_jack_ringbuffer
) <
403 p_sys
->jack_sample_size
) {
404 msg_Err( p_demux
, "buffer overflow");
405 return 0; // buffer overflow
407 size_t i_write
= jack_ringbuffer_write( p_sys
->p_jack_ringbuffer
,
408 ( char * ) (p_sys
->pp_jack_buffer
[i
]+j
),
409 p_sys
->jack_sample_size
);
410 if (i_write
!= p_sys
->jack_sample_size
) {
411 msg_Warn( p_demux
, "error writing on ring buffer");
420 /*****************************************************************************
421 * GrabJack: grab audio data in the Jack buffer
422 *****************************************************************************/
423 static block_t
*GrabJack( demux_t
*p_demux
)
425 demux_sys_t
*p_sys
= p_demux
->p_sys
;
428 /* read signal from ring buffer */
429 size_t i_read
= jack_ringbuffer_read_space( p_sys
->p_jack_ringbuffer
);
431 if( i_read
< 100 ) /* avoid small read */
432 { /* vlc has too much free time on its hands? */
433 #undef vlc_tick_sleep
434 #warning Hmm.... looks wrong
435 vlc_tick_sleep(1000);
439 if( p_sys
->p_block_audio
)
441 p_block
= p_sys
->p_block_audio
;
445 p_block
= block_Alloc( i_read
);
449 msg_Warn( p_demux
, "cannot get block" );
453 //Find the previous power of 2, this algo assumes size_t has the same size on all arch
456 i_read
|= i_read
>> 1;
457 i_read
|= i_read
>> 2;
458 i_read
|= i_read
>> 4;
459 i_read
|= i_read
>> 8;
460 i_read
|= i_read
>> 16;
463 i_read
= jack_ringbuffer_read( p_sys
->p_jack_ringbuffer
, ( char * ) p_block
->p_buffer
, i_read
);
465 p_block
->i_dts
= p_block
->i_pts
= date_Increment( &p_sys
->pts
,
466 i_read
/(p_sys
->i_channels
* p_sys
->jack_sample_size
) );
468 p_sys
->p_block_audio
= p_block
;
469 p_block
->i_buffer
= i_read
;
470 p_sys
->p_block_audio
= 0;
476 /*****************************************************************************
477 * Port_finder: compare ports with the regexp entered
478 *****************************************************************************/
479 static void Port_finder( demux_t
*p_demux
)
481 demux_sys_t
*p_sys
= p_demux
->p_sys
;
482 char *psz_expr
= p_sys
->psz_ports
;
485 char *psz_uri
= NULL
;
486 const char **pp_jack_port_output
= NULL
;
488 int i_total_out_ports
=0;
489 p_sys
->pp_jack_port_table
= NULL
;
491 /* parse the ports part of the MRL */
492 for( token
= strtok_r( psz_expr
, ",", &state
); token
;
493 token
= strtok_r( NULL
, ",", &state
) )
495 psz_uri
= vlc_uri_decode_duplicate( token
);
496 /* get the ports which match the regexp */
497 pp_jack_port_output
= jack_get_ports( p_sys
->p_jack_client
,
498 psz_uri
, NULL
, JackPortIsOutput
);
499 if( pp_jack_port_output
== NULL
)
500 msg_Err( p_demux
, "port(s) asked not found:%s", psz_uri
);
503 while( pp_jack_port_output
[i_out_ports
] )
505 /* alloc an array to store all the matched ports */
506 p_sys
->pp_jack_port_table
= xrealloc( p_sys
->pp_jack_port_table
,
507 (i_out_ports
* sizeof( char * ) + i_total_out_ports
* sizeof( char * ) ) );
509 for(int i
=0; i
<i_out_ports
;i
++)
510 p_sys
->pp_jack_port_table
[i_total_out_ports
+i
] = ( char * ) pp_jack_port_output
[i
];
512 i_total_out_ports
+= i_out_ports
;
514 free( pp_jack_port_output
);
518 p_sys
->i_match_ports
= i_total_out_ports
;
522 /*****************************************************************************
523 * Parse: Parse the MRL
524 *****************************************************************************/
525 static void Parse( demux_t
*p_demux
)
527 demux_sys_t
*p_sys
= p_demux
->p_sys
;
528 char *psz_dup
= strdup( p_demux
->psz_location
);
529 char *psz_parser
= psz_dup
;
531 if( !strncmp( psz_parser
, "channels=", strlen( "channels=" ) ) )
533 p_sys
->i_channels
= abs( strtol( psz_parser
+ strlen( "channels=" ),
536 else if( !strncmp( psz_parser
, "ports=", strlen( "ports=" ) ) )
539 psz_parser
+= strlen( "ports=" );
540 if( strchr( psz_parser
, ':' ) )
542 i_len
= strchr( psz_parser
, ':' ) - psz_parser
;
546 i_len
= strlen( psz_parser
);
548 p_sys
->psz_ports
= strndup( psz_parser
, i_len
);
553 msg_Warn( p_demux
, "unknown option" );
556 while( *psz_parser
&& *psz_parser
!= ':' )
561 if( *psz_parser
== ':' )
565 *psz_parser
++ = '\0';
566 if( !strncmp( psz_parser
, "channels=", strlen( "channels=" ) ) )
568 p_sys
->i_channels
= abs( strtol(
569 psz_parser
+ strlen( "channels=" ), &psz_parser
, 0 ) );
571 else if( !strncmp( psz_parser
, "ports=", strlen( "ports=" ) ) )
574 psz_parser
+= strlen( "ports=" );
575 if( strchr( psz_parser
, ':' ) )
577 i_len
= strchr( psz_parser
, ':' ) - psz_parser
;
581 i_len
= strlen( psz_parser
);
583 p_sys
->psz_ports
= strndup( psz_parser
, i_len
);
588 msg_Warn( p_demux
, "unknown option" );
590 while( *psz_parser
&& *psz_parser
!= ':' )
595 if( *psz_parser
== '\0' )