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_codecs.h>
47 #include <vlc_strings.h>
49 #include <jack/jack.h>
50 #include <jack/ringbuffer.h>
53 #include <sys/types.h>
56 /*****************************************************************************
58 *****************************************************************************/
59 static int Open ( vlc_object_t
* );
60 static void Close( vlc_object_t
* );
62 #define CACHING_TEXT N_("Caching value in ms")
63 #define CACHING_LONGTEXT N_( \
64 "Make VLC buffer audio data captured from jack for the specified " \
65 "length in milliseconds." )
66 #define PACE_TEXT N_( "Pace" )
67 #define PACE_LONGTEXT N_( \
68 "Read the audio stream at VLC pace rather than Jack pace." )
69 #define AUTO_CONNECT_TEXT N_( "Auto Connection" )
70 #define AUTO_CONNECT_LONGTEXT N_( \
71 "Automatically connect VLC input ports to available output ports." )
74 set_description( N_("JACK audio input") );
75 set_capability( "access_demux", 0 );
76 set_shortname( N_( "JACK Input" ) );
77 set_category( CAT_INPUT
);
78 set_subcategory( SUBCAT_INPUT_ACCESS
);
80 add_integer( "jack-input-caching", DEFAULT_PTS_DELAY
/ 1000, NULL
,
81 CACHING_TEXT
, CACHING_LONGTEXT
, true );
82 add_bool( "jack-input-use-vlc-pace", false, NULL
,
83 PACE_TEXT
, PACE_LONGTEXT
, true );
84 add_bool( "jack-input-auto-connect", false, NULL
,
85 AUTO_CONNECT_TEXT
, AUTO_CONNECT_LONGTEXT
, true );
87 add_shortcut( "jack" );
88 set_callbacks( Open
, Close
);
91 /*****************************************************************************
93 *****************************************************************************/
97 /* Audio properties */
98 vlc_fourcc_t i_acodec_raw
;
99 unsigned int i_channels
;
101 int i_audio_max_frame_size
;
103 block_t
*p_block_audio
;
104 es_out_id_t
*p_es_audio
;
107 /* Jack properties */
108 jack_client_t
*p_jack_client
;
109 jack_port_t
**pp_jack_port_input
;
110 jack_default_audio_sample_t
**pp_jack_buffer
;
111 jack_ringbuffer_t
*p_jack_ringbuffer
;
112 jack_nframes_t jack_buffer_size
;
113 jack_nframes_t jack_sample_rate
;
114 size_t jack_sample_size
;
116 char **pp_jack_port_table
;
120 static int Demux( demux_t
* );
121 static int Control( demux_t
*p_demux
, int i_query
, va_list args
);
123 static void Parse ( demux_t
* );
124 static void Port_finder( demux_t
* );
125 static int Process( jack_nframes_t i_frames
, void *p_arg
);
127 static block_t
*GrabJack( demux_t
* );
129 /*****************************************************************************
130 * Open: Connect to the JACK server
131 *****************************************************************************/
132 static int Open( vlc_object_t
*p_this
)
135 demux_t
*p_demux
= ( demux_t
* )p_this
;
140 p_demux
->pf_demux
= Demux
;
141 p_demux
->pf_control
= Control
;
143 /* Allocate structure */
144 p_demux
->p_sys
= p_sys
= calloc( 1, sizeof( demux_sys_t
) );
147 memset( p_sys
, 0, sizeof( demux_sys_t
) );
153 var_Create( p_demux
, "jack-input-caching",
154 VLC_VAR_INTEGER
| VLC_VAR_DOINHERIT
);
155 var_Create( p_demux
, "jack-input-use-vlc-pace",
156 VLC_VAR_BOOL
| VLC_VAR_DOINHERIT
);
157 var_Create( p_demux
, "jack-input-auto-connect",
158 VLC_VAR_BOOL
| VLC_VAR_DOINHERIT
);
160 /* JACK connexions */
161 /* define name and connect to jack server */
162 char p_vlc_client_name
[32];
163 sprintf( p_vlc_client_name
, "vlc-input-%d", getpid() );
164 p_sys
->p_jack_client
= jack_client_new( p_vlc_client_name
);
165 if( p_sys
->p_jack_client
== NULL
)
167 msg_Err( p_demux
, "failed to connect to JACK server" );
172 /* find some specifics ports if user entered a regexp */
173 if( p_sys
->psz_ports
)
175 Port_finder( p_demux
);
176 if( p_sys
->i_channels
== 0 )
178 p_sys
->i_channels
= p_sys
->i_match_ports
;
182 /* allocate input ports */
183 if( p_sys
->i_channels
== 0 ) p_sys
->i_channels
= 2 ; /* default number of port */
184 p_sys
->pp_jack_port_input
= malloc(
185 p_sys
->i_channels
* sizeof( jack_port_t
* ) );
186 if( p_sys
->pp_jack_port_input
== NULL
)
188 jack_client_close( p_sys
->p_jack_client
);
193 /* allocate ringbuffer */
194 /* The length of the ringbuffer is critical, it must be large enought
195 to keep all data between 2 GrabJack() calls. We assume 1 sec is ok */
196 p_sys
->p_jack_ringbuffer
= jack_ringbuffer_create( p_sys
->i_channels
197 * jack_get_sample_rate( p_sys
->p_jack_client
)
198 * sizeof( jack_default_audio_sample_t
) );
199 if( p_sys
->p_jack_ringbuffer
== NULL
)
201 free( p_sys
->pp_jack_port_input
);
202 jack_client_close( p_sys
->p_jack_client
);
207 /* register input ports */
208 for( i
= 0; i
< p_sys
->i_channels
; i
++ )
210 char p_input_name
[32];
211 snprintf( p_input_name
, 32, "vlc_in_%d", i
+1 );
212 p_sys
->pp_jack_port_input
[i
] = jack_port_register(
213 p_sys
->p_jack_client
, p_input_name
, JACK_DEFAULT_AUDIO_TYPE
,
214 JackPortIsInput
, 0 );
215 if( p_sys
->pp_jack_port_input
[i
] == NULL
)
217 msg_Err( p_demux
, "failed to register a JACK port" );
218 jack_ringbuffer_free( p_sys
->p_jack_ringbuffer
);
219 free( p_sys
->pp_jack_port_input
);
220 jack_client_close( p_sys
->p_jack_client
);
226 /* allocate buffer for input ports */
227 p_sys
->pp_jack_buffer
= malloc ( p_sys
->i_channels
228 * sizeof( jack_default_audio_sample_t
* ) );
229 if( p_sys
->pp_jack_buffer
== NULL
)
231 for( i
= 0; i
< p_sys
->i_channels
; i
++ )
232 jack_port_unregister( p_sys
->p_jack_client
, p_sys
->pp_jack_port_input
[i
] );
233 jack_ringbuffer_free( p_sys
->p_jack_ringbuffer
);
234 free( p_sys
->pp_jack_port_input
);
235 jack_client_close( p_sys
->p_jack_client
);
240 /* set process callback */
241 jack_set_process_callback( p_sys
->p_jack_client
, Process
, p_demux
);
243 /* tell jack server we are ready */
244 if ( jack_activate( p_sys
->p_jack_client
) )
246 msg_Err( p_demux
, "failed to activate JACK client" );
247 free( p_sys
->pp_jack_buffer
);
248 for( i
= 0; i
< p_sys
->i_channels
; i
++ )
249 jack_port_unregister( p_sys
->p_jack_client
, p_sys
->pp_jack_port_input
[i
] );
250 jack_ringbuffer_free( p_sys
->p_jack_ringbuffer
);
251 free( p_sys
->pp_jack_port_input
);
252 jack_client_close( p_sys
->p_jack_client
);
257 /* connect vlc input to specifics jack output ports if requested */
258 /* if( var_GetBool( p_demux, "jack-input-auto-connect" ) && p_sys->psz_ports ) */
259 if( p_sys
->psz_ports
)
264 if( p_sys
->i_match_ports
> 0 )
266 for( j
= 0; j
< p_sys
->i_match_ports
; j
++ )
268 i_input_ports
= j
% p_sys
->i_channels
;
269 jack_connect( p_sys
->p_jack_client
, p_sys
->pp_jack_port_table
[j
],
270 jack_port_name( p_sys
->pp_jack_port_input
[i_input_ports
] ) );
275 /* connect vlc input to all jack output ports if requested */
276 if( var_GetBool( p_demux
, "jack-input-auto-connect" ) && !p_sys
->psz_ports
)
280 const char **pp_jack_port_output
;
282 pp_jack_port_output
= jack_get_ports( p_sys
->p_jack_client
, NULL
, NULL
, JackPortIsOutput
);
284 while( pp_jack_port_output
&& pp_jack_port_output
[i_out_ports
] )
288 if( i_out_ports
> 0 )
290 for( j
= 0; j
< i_out_ports
; j
++ )
292 i_input_ports
= j
% p_sys
->i_channels
;
293 jack_connect( p_sys
->p_jack_client
, pp_jack_port_output
[j
],
294 jack_port_name( p_sys
->pp_jack_port_input
[i_input_ports
] ) );
297 free( pp_jack_port_output
);
300 /* info about jack server */
301 /* get buffers size */
302 p_sys
->jack_buffer_size
= jack_get_buffer_size( p_sys
->p_jack_client
);
303 /* get sample rate */
304 p_sys
->jack_sample_rate
= jack_get_sample_rate( p_sys
->p_jack_client
);
305 /* get sample size */
306 p_sys
->jack_sample_size
= sizeof( jack_default_audio_sample_t
);
308 /* Define output format */
309 es_format_Init( &fmt
, AUDIO_ES
, VLC_FOURCC( 'f','l','3','2' ) );
310 fmt
.audio
.i_channels
= p_sys
->i_channels
;
311 fmt
.audio
.i_rate
= p_sys
->jack_sample_rate
;
312 fmt
.audio
.i_bitspersample
= p_sys
->jack_sample_size
* 8;
313 fmt
.audio
.i_blockalign
= fmt
.audio
.i_bitspersample
/ 8;
314 fmt
.i_bitrate
= fmt
.audio
.i_rate
* fmt
.audio
.i_bitspersample
315 * fmt
.audio
.i_channels
;
317 p_sys
->p_es_audio
= es_out_Add( p_demux
->out
, &fmt
);
318 date_Init( &p_sys
->pts
, fmt
.audio
.i_rate
, 1 );
319 date_Set( &p_sys
->pts
, 1 );
325 /*****************************************************************************
326 * Close: Disconnect from jack server and release associated resources
327 *****************************************************************************/
328 static void Close( vlc_object_t
*p_this
)
330 demux_t
*p_demux
= ( demux_t
* )p_this
;
331 demux_sys_t
*p_sys
= p_demux
->p_sys
;
333 msg_Dbg( p_demux
,"Module unloaded" );
334 if( p_sys
->p_block_audio
) block_Release( p_sys
->p_block_audio
);
335 if( p_sys
->p_jack_client
) jack_client_close( p_sys
->p_jack_client
);
336 if( p_sys
->p_jack_ringbuffer
) jack_ringbuffer_free( p_sys
->p_jack_ringbuffer
);
337 free( p_sys
->pp_jack_port_input
);
338 free( p_sys
->pp_jack_buffer
);
339 free( p_sys
->pp_jack_port_table
);
344 /*****************************************************************************
346 *****************************************************************************/
347 static int Control( demux_t
*p_demux
, int i_query
, va_list args
)
351 demux_sys_t
*p_sys
= p_demux
->p_sys
;
355 /* Special for access_demux */
356 case DEMUX_CAN_PAUSE
:
358 pb
= (bool *)va_arg( args
, bool * );
362 case DEMUX_SET_PAUSE_STATE
:
364 case DEMUX_CAN_CONTROL_PACE
:
365 pb
= ( bool* )va_arg( args
, bool * );
366 *pb
= var_GetBool( p_demux
, "jack-input-use-vlc-pace" );
369 case DEMUX_GET_PTS_DELAY
:
370 pi64
= ( int64_t* )va_arg( args
, int64_t * );
371 *pi64
= ( int64_t )var_GetInteger( p_demux
, "jack-input-caching" )
376 pi64
= ( int64_t* )va_arg( args
, int64_t * );
377 *pi64
= date_Get(&p_sys
->pts
);
380 /* TODO implement others */
389 /*****************************************************************************
391 *****************************************************************************/
392 static int Demux( demux_t
*p_demux
)
399 p_sys
= p_demux
->p_sys
;
400 p_es
= p_sys
->p_es_audio
;
401 p_block
= GrabJack( p_demux
);
405 es_out_Control( p_demux
->out
, ES_OUT_SET_PCR
, p_block
->i_pts
);
406 es_out_Send( p_demux
->out
, p_es
, p_block
);
413 /*****************************************************************************
414 * Process Callback : fill ringbuffer with Jack audio data
415 *****************************************************************************/
416 int Process( jack_nframes_t i_frames
, void *p_arg
)
418 demux_t
*p_demux
= ( demux_t
* )p_arg
;
419 demux_sys_t
*p_sys
= p_demux
->p_sys
;
423 /* Get and interlace buffers */
424 for ( i
= 0; i
< p_sys
->i_channels
; i
++ )
426 p_sys
->pp_jack_buffer
[i
] = jack_port_get_buffer(
427 p_sys
->pp_jack_port_input
[i
], i_frames
);
430 /* fill ring buffer with signal */
431 for( j
= 0; j
< i_frames
; j
++ )
433 for( i
= 0; i
<p_sys
->i_channels
; i
++ )
435 if( jack_ringbuffer_write_space( p_sys
->p_jack_ringbuffer
) <
436 p_sys
->jack_sample_size
) {
437 msg_Err( p_demux
, "buffer overflow");
438 return 0; // buffer overflow
440 i_write
= jack_ringbuffer_write( p_sys
->p_jack_ringbuffer
,
441 ( char * ) (p_sys
->pp_jack_buffer
[i
]+j
),
442 p_sys
->jack_sample_size
);
443 if (i_write
!= p_sys
->jack_sample_size
) {
444 msg_Warn( p_demux
, "error writing on ring buffer");
453 /*****************************************************************************
454 * GrabJack: grab audio data in the Jack buffer
455 *****************************************************************************/
456 static block_t
*GrabJack( demux_t
*p_demux
)
459 demux_sys_t
*p_sys
= p_demux
->p_sys
;
462 /* read signal from ring buffer */
463 i_read
= jack_ringbuffer_read_space( p_sys
->p_jack_ringbuffer
);
465 if( i_read
< 100 ) /* avoid small read */
466 { /* vlc has too much free time on its hands? */
471 if( p_sys
->p_block_audio
)
473 p_block
= p_sys
->p_block_audio
;
477 p_block
= block_New( p_demux
, i_read
);
481 msg_Warn( p_demux
, "cannot get block" );
485 //Find the previous power of 2, this algo assumes size_t has the same size on all arch
488 i_read
|= i_read
>> 1;
489 i_read
|= i_read
>> 2;
490 i_read
|= i_read
>> 4;
491 i_read
|= i_read
>> 8;
492 i_read
|= i_read
>> 16;
495 i_read
= jack_ringbuffer_read( p_sys
->p_jack_ringbuffer
, ( char * ) p_block
->p_buffer
, i_read
);
497 p_block
->i_dts
= p_block
->i_pts
= date_Increment( &p_sys
->pts
,
498 i_read
/(p_sys
->i_channels
* p_sys
->jack_sample_size
) );
500 p_sys
->p_block_audio
= p_block
;
501 p_block
->i_buffer
= i_read
;
502 p_sys
->p_block_audio
= 0;
508 /*****************************************************************************
509 * Port_finder: compare ports with the regexp entered
510 *****************************************************************************/
511 static void Port_finder( demux_t
*p_demux
)
514 demux_sys_t
*p_sys
= p_demux
->p_sys
;
515 char *psz_expr
= p_sys
->psz_ports
;
518 char *psz_uri
= NULL
;
519 const char **pp_jack_port_output
= NULL
;
521 int i_total_out_ports
=0;
522 p_sys
->pp_jack_port_table
= NULL
;
524 /* parse the ports part of the MRL */
525 for( token
= strtok_r( psz_expr
, ",", &state
); token
;
526 token
= strtok_r( NULL
, ",", &state
) )
528 psz_uri
= decode_URI_duplicate( token
);
529 /* get the ports which match the regexp */
530 pp_jack_port_output
= jack_get_ports( p_sys
->p_jack_client
,
531 psz_uri
, NULL
, JackPortIsOutput
);
532 if( pp_jack_port_output
== NULL
)
534 msg_Err( p_demux
, "port(s) asked not found:%s", psz_uri
);
535 free( pp_jack_port_output
);
539 while( pp_jack_port_output
&& pp_jack_port_output
[i_out_ports
] )
543 /* alloc an array to store all the matched ports */
544 p_sys
->pp_jack_port_table
= realloc( p_sys
->pp_jack_port_table
,
545 (i_out_ports
* sizeof( char * ) + i_total_out_ports
* sizeof( char * ) ) );
547 for(int i
=0; i
<i_out_ports
;i
++)
549 p_sys
->pp_jack_port_table
[i_total_out_ports
+i
] = ( char * ) pp_jack_port_output
[i
];
552 i_total_out_ports
+= i_out_ports
;
556 free( pp_jack_port_output
);
557 p_sys
->i_match_ports
= i_total_out_ports
;
561 /*****************************************************************************
562 * Parse: Parse the MRL
563 *****************************************************************************/
564 static void Parse( demux_t
*p_demux
)
566 demux_sys_t
*p_sys
= p_demux
->p_sys
;
567 char *psz_dup
= strdup( p_demux
->psz_path
);
568 char *psz_parser
= psz_dup
;
570 if( !strncmp( psz_parser
, "channels=", strlen( "channels=" ) ) )
572 p_sys
->i_channels
= abs( strtol( psz_parser
+ strlen( "channels=" ),
575 else if( !strncmp( psz_parser
, "ports=", strlen( "ports=" ) ) )
578 psz_parser
+= strlen( "ports=" );
579 if( strchr( psz_parser
, ':' ) )
581 i_len
= strchr( psz_parser
, ':' ) - psz_parser
;
585 i_len
= strlen( psz_parser
);
587 p_sys
->psz_ports
= strndup( psz_parser
, i_len
);
592 msg_Warn( p_demux
, "unknown option" );
595 while( *psz_parser
&& *psz_parser
!= ':' )
600 if( *psz_parser
== ':' )
604 *psz_parser
++ = '\0';
605 if( !strncmp( psz_parser
, "channels=", strlen( "channels=" ) ) )
607 p_sys
->i_channels
= abs( strtol(
608 psz_parser
+ strlen( "channels=" ), &psz_parser
, 0 ) );
610 else if( !strncmp( psz_parser
, "ports=", strlen( "ports=" ) ) )
613 psz_parser
+= strlen( "ports=" );
614 if( strchr( psz_parser
, ':' ) )
616 i_len
= strchr( psz_parser
, ':' ) - psz_parser
;
620 i_len
= strlen( psz_parser
);
622 p_sys
->psz_ports
= strndup( psz_parser
, i_len
);
627 msg_Warn( p_demux
, "unknown option" );
629 while( *psz_parser
&& *psz_parser
!= ':' )
634 if( *psz_parser
== '\0' )