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 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
)
126 demux_t
*p_demux
= ( demux_t
* )p_this
;
131 p_demux
->pf_demux
= Demux
;
132 p_demux
->pf_control
= Control
;
134 /* Allocate structure */
135 p_demux
->p_sys
= p_sys
= calloc( 1, sizeof( demux_sys_t
) );
143 var_Create( p_demux
, "jack-input-use-vlc-pace",
144 VLC_VAR_BOOL
| VLC_VAR_DOINHERIT
);
145 var_Create( p_demux
, "jack-input-auto-connect",
146 VLC_VAR_BOOL
| VLC_VAR_DOINHERIT
);
148 /* JACK connexions */
149 /* define name and connect to jack server */
150 char p_vlc_client_name
[32];
151 sprintf( p_vlc_client_name
, "vlc-input-%d", getpid() );
152 p_sys
->p_jack_client
= jack_client_open( p_vlc_client_name
, JackNullOption
, NULL
);
153 if( p_sys
->p_jack_client
== NULL
)
155 msg_Err( p_demux
, "failed to connect to JACK server" );
160 /* find some specifics ports if user entered a regexp */
161 if( p_sys
->psz_ports
)
163 Port_finder( p_demux
);
164 if( p_sys
->i_channels
== 0 )
166 p_sys
->i_channels
= p_sys
->i_match_ports
;
170 /* allocate input ports */
171 if( p_sys
->i_channels
== 0 ) p_sys
->i_channels
= 2 ; /* default number of port */
172 p_sys
->pp_jack_port_input
= malloc(
173 p_sys
->i_channels
* sizeof( jack_port_t
* ) );
174 if( p_sys
->pp_jack_port_input
== NULL
)
176 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
);
195 /* register input ports */
196 for( i
= 0; i
< p_sys
->i_channels
; i
++ )
198 char p_input_name
[32];
199 snprintf( p_input_name
, 32, "vlc_in_%d", i
+1 );
200 p_sys
->pp_jack_port_input
[i
] = jack_port_register(
201 p_sys
->p_jack_client
, p_input_name
, JACK_DEFAULT_AUDIO_TYPE
,
202 JackPortIsInput
, 0 );
203 if( p_sys
->pp_jack_port_input
[i
] == NULL
)
205 msg_Err( p_demux
, "failed to register a JACK port" );
206 jack_ringbuffer_free( p_sys
->p_jack_ringbuffer
);
207 free( p_sys
->pp_jack_port_input
);
208 jack_client_close( p_sys
->p_jack_client
);
214 /* allocate buffer for input ports */
215 p_sys
->pp_jack_buffer
= malloc ( p_sys
->i_channels
216 * sizeof( jack_default_audio_sample_t
* ) );
217 if( p_sys
->pp_jack_buffer
== NULL
)
219 for( i
= 0; i
< p_sys
->i_channels
; i
++ )
220 jack_port_unregister( p_sys
->p_jack_client
, p_sys
->pp_jack_port_input
[i
] );
221 jack_ringbuffer_free( p_sys
->p_jack_ringbuffer
);
222 free( p_sys
->pp_jack_port_input
);
223 jack_client_close( p_sys
->p_jack_client
);
228 /* set process callback */
229 jack_set_process_callback( p_sys
->p_jack_client
, Process
, p_demux
);
231 /* tell jack server we are ready */
232 if ( jack_activate( p_sys
->p_jack_client
) )
234 msg_Err( p_demux
, "failed to activate JACK client" );
235 free( p_sys
->pp_jack_buffer
);
236 for( i
= 0; i
< p_sys
->i_channels
; i
++ )
237 jack_port_unregister( p_sys
->p_jack_client
, p_sys
->pp_jack_port_input
[i
] );
238 jack_ringbuffer_free( p_sys
->p_jack_ringbuffer
);
239 free( p_sys
->pp_jack_port_input
);
240 jack_client_close( p_sys
->p_jack_client
);
245 /* connect vlc input to specifics jack output ports if requested */
246 /* if( var_GetBool( p_demux, "jack-input-auto-connect" ) && p_sys->psz_ports ) */
247 if( p_sys
->psz_ports
)
252 if( p_sys
->i_match_ports
> 0 )
254 for( j
= 0; j
< p_sys
->i_match_ports
; j
++ )
256 i_input_ports
= j
% p_sys
->i_channels
;
257 jack_connect( p_sys
->p_jack_client
, p_sys
->pp_jack_port_table
[j
],
258 jack_port_name( p_sys
->pp_jack_port_input
[i_input_ports
] ) );
263 /* connect vlc input to all jack output ports if requested */
264 if( var_GetBool( p_demux
, "jack-input-auto-connect" ) && !p_sys
->psz_ports
)
268 const char **pp_jack_port_output
;
270 pp_jack_port_output
= jack_get_ports( p_sys
->p_jack_client
, NULL
, NULL
, JackPortIsOutput
);
272 while( pp_jack_port_output
&& pp_jack_port_output
[i_out_ports
] )
276 if( i_out_ports
> 0 )
278 for( j
= 0; j
< i_out_ports
; j
++ )
280 i_input_ports
= j
% p_sys
->i_channels
;
281 jack_connect( p_sys
->p_jack_client
, pp_jack_port_output
[j
],
282 jack_port_name( p_sys
->pp_jack_port_input
[i_input_ports
] ) );
285 free( pp_jack_port_output
);
288 /* info about jack server */
289 /* get buffers size */
290 p_sys
->jack_buffer_size
= jack_get_buffer_size( p_sys
->p_jack_client
);
291 /* get sample rate */
292 p_sys
->jack_sample_rate
= jack_get_sample_rate( p_sys
->p_jack_client
);
293 /* get sample size */
294 p_sys
->jack_sample_size
= sizeof( jack_default_audio_sample_t
);
296 /* Define output format */
297 es_format_Init( &fmt
, AUDIO_ES
, VLC_CODEC_FL32
);
298 fmt
.audio
.i_channels
= p_sys
->i_channels
;
299 fmt
.audio
.i_rate
= p_sys
->jack_sample_rate
;
300 fmt
.audio
.i_bitspersample
= p_sys
->jack_sample_size
* 8;
301 fmt
.audio
.i_blockalign
= fmt
.audio
.i_bitspersample
/ 8;
302 fmt
.i_bitrate
= fmt
.audio
.i_rate
* fmt
.audio
.i_bitspersample
303 * fmt
.audio
.i_channels
;
305 p_sys
->p_es_audio
= es_out_Add( p_demux
->out
, &fmt
);
306 date_Init( &p_sys
->pts
, fmt
.audio
.i_rate
, 1 );
307 date_Set( &p_sys
->pts
, 1 );
313 /*****************************************************************************
314 * Close: Disconnect from jack server and release associated resources
315 *****************************************************************************/
316 static void Close( vlc_object_t
*p_this
)
318 demux_t
*p_demux
= ( demux_t
* )p_this
;
319 demux_sys_t
*p_sys
= p_demux
->p_sys
;
321 msg_Dbg( p_demux
,"Module unloaded" );
322 if( p_sys
->p_block_audio
) block_Release( p_sys
->p_block_audio
);
323 if( p_sys
->p_jack_client
) jack_client_close( p_sys
->p_jack_client
);
324 if( p_sys
->p_jack_ringbuffer
) jack_ringbuffer_free( p_sys
->p_jack_ringbuffer
);
325 free( p_sys
->pp_jack_port_input
);
326 free( p_sys
->pp_jack_buffer
);
327 free( p_sys
->pp_jack_port_table
);
332 /*****************************************************************************
334 *****************************************************************************/
335 static int Control( demux_t
*p_demux
, int i_query
, va_list args
)
339 demux_sys_t
*p_sys
= p_demux
->p_sys
;
343 /* Special for access_demux */
344 case DEMUX_CAN_PAUSE
:
346 pb
= (bool *)va_arg( args
, bool * );
350 case DEMUX_SET_PAUSE_STATE
:
352 case DEMUX_CAN_CONTROL_PACE
:
353 pb
= ( bool* )va_arg( args
, bool * );
354 *pb
= var_GetBool( p_demux
, "jack-input-use-vlc-pace" );
357 case DEMUX_GET_PTS_DELAY
:
358 pi64
= ( int64_t* )va_arg( args
, int64_t * );
359 *pi64
= INT64_C(1000) * var_InheritInteger( p_demux
, "live-caching" );
363 pi64
= ( int64_t* )va_arg( args
, int64_t * );
364 *pi64
= date_Get(&p_sys
->pts
);
367 /* TODO implement others */
376 /*****************************************************************************
378 *****************************************************************************/
379 static int Demux( demux_t
*p_demux
)
386 p_sys
= p_demux
->p_sys
;
387 p_es
= p_sys
->p_es_audio
;
388 p_block
= GrabJack( p_demux
);
392 es_out_Control( p_demux
->out
, ES_OUT_SET_PCR
, p_block
->i_pts
);
393 es_out_Send( p_demux
->out
, p_es
, p_block
);
400 /*****************************************************************************
401 * Process Callback : fill ringbuffer with Jack audio data
402 *****************************************************************************/
403 int Process( jack_nframes_t i_frames
, void *p_arg
)
405 demux_t
*p_demux
= ( demux_t
* )p_arg
;
406 demux_sys_t
*p_sys
= p_demux
->p_sys
;
410 /* Get and interlace buffers */
411 for ( i
= 0; i
< p_sys
->i_channels
; i
++ )
413 p_sys
->pp_jack_buffer
[i
] = jack_port_get_buffer(
414 p_sys
->pp_jack_port_input
[i
], i_frames
);
417 /* fill ring buffer with signal */
418 for( j
= 0; j
< i_frames
; j
++ )
420 for( i
= 0; i
<p_sys
->i_channels
; i
++ )
422 if( jack_ringbuffer_write_space( p_sys
->p_jack_ringbuffer
) <
423 p_sys
->jack_sample_size
) {
424 msg_Err( p_demux
, "buffer overflow");
425 return 0; // buffer overflow
427 i_write
= jack_ringbuffer_write( p_sys
->p_jack_ringbuffer
,
428 ( char * ) (p_sys
->pp_jack_buffer
[i
]+j
),
429 p_sys
->jack_sample_size
);
430 if (i_write
!= p_sys
->jack_sample_size
) {
431 msg_Warn( p_demux
, "error writing on ring buffer");
440 /*****************************************************************************
441 * GrabJack: grab audio data in the Jack buffer
442 *****************************************************************************/
443 static block_t
*GrabJack( demux_t
*p_demux
)
446 demux_sys_t
*p_sys
= p_demux
->p_sys
;
449 /* read signal from ring buffer */
450 i_read
= jack_ringbuffer_read_space( p_sys
->p_jack_ringbuffer
);
452 if( i_read
< 100 ) /* avoid small read */
453 { /* vlc has too much free time on its hands? */
455 #warning Hmm.... looks wrong
460 if( p_sys
->p_block_audio
)
462 p_block
= p_sys
->p_block_audio
;
466 p_block
= block_New( p_demux
, i_read
);
470 msg_Warn( p_demux
, "cannot get block" );
474 //Find the previous power of 2, this algo assumes size_t has the same size on all arch
477 i_read
|= i_read
>> 1;
478 i_read
|= i_read
>> 2;
479 i_read
|= i_read
>> 4;
480 i_read
|= i_read
>> 8;
481 i_read
|= i_read
>> 16;
484 i_read
= jack_ringbuffer_read( p_sys
->p_jack_ringbuffer
, ( char * ) p_block
->p_buffer
, i_read
);
486 p_block
->i_dts
= p_block
->i_pts
= date_Increment( &p_sys
->pts
,
487 i_read
/(p_sys
->i_channels
* p_sys
->jack_sample_size
) );
489 p_sys
->p_block_audio
= p_block
;
490 p_block
->i_buffer
= i_read
;
491 p_sys
->p_block_audio
= 0;
497 /*****************************************************************************
498 * Port_finder: compare ports with the regexp entered
499 *****************************************************************************/
500 static void Port_finder( demux_t
*p_demux
)
503 demux_sys_t
*p_sys
= p_demux
->p_sys
;
504 char *psz_expr
= p_sys
->psz_ports
;
507 char *psz_uri
= NULL
;
508 const char **pp_jack_port_output
= NULL
;
510 int i_total_out_ports
=0;
511 p_sys
->pp_jack_port_table
= NULL
;
513 /* parse the ports part of the MRL */
514 for( token
= strtok_r( psz_expr
, ",", &state
); token
;
515 token
= strtok_r( NULL
, ",", &state
) )
517 psz_uri
= decode_URI_duplicate( token
);
518 /* get the ports which match the regexp */
519 pp_jack_port_output
= jack_get_ports( p_sys
->p_jack_client
,
520 psz_uri
, NULL
, JackPortIsOutput
);
521 if( pp_jack_port_output
== NULL
)
523 msg_Err( p_demux
, "port(s) asked not found:%s", psz_uri
);
524 free( pp_jack_port_output
);
528 while( pp_jack_port_output
&& pp_jack_port_output
[i_out_ports
] )
532 /* alloc an array to store all the matched ports */
533 p_sys
->pp_jack_port_table
= xrealloc( p_sys
->pp_jack_port_table
,
534 (i_out_ports
* sizeof( char * ) + i_total_out_ports
* sizeof( char * ) ) );
536 for(int i
=0; i
<i_out_ports
;i
++)
538 p_sys
->pp_jack_port_table
[i_total_out_ports
+i
] = ( char * ) pp_jack_port_output
[i
];
541 i_total_out_ports
+= i_out_ports
;
545 free( pp_jack_port_output
);
546 p_sys
->i_match_ports
= i_total_out_ports
;
550 /*****************************************************************************
551 * Parse: Parse the MRL
552 *****************************************************************************/
553 static void Parse( demux_t
*p_demux
)
555 demux_sys_t
*p_sys
= p_demux
->p_sys
;
556 char *psz_dup
= strdup( p_demux
->psz_location
);
557 char *psz_parser
= psz_dup
;
559 if( !strncmp( psz_parser
, "channels=", strlen( "channels=" ) ) )
561 p_sys
->i_channels
= abs( strtol( psz_parser
+ strlen( "channels=" ),
564 else if( !strncmp( psz_parser
, "ports=", strlen( "ports=" ) ) )
567 psz_parser
+= strlen( "ports=" );
568 if( strchr( psz_parser
, ':' ) )
570 i_len
= strchr( psz_parser
, ':' ) - psz_parser
;
574 i_len
= strlen( psz_parser
);
576 p_sys
->psz_ports
= strndup( psz_parser
, i_len
);
581 msg_Warn( p_demux
, "unknown option" );
584 while( *psz_parser
&& *psz_parser
!= ':' )
589 if( *psz_parser
== ':' )
593 *psz_parser
++ = '\0';
594 if( !strncmp( psz_parser
, "channels=", strlen( "channels=" ) ) )
596 p_sys
->i_channels
= abs( strtol(
597 psz_parser
+ strlen( "channels=" ), &psz_parser
, 0 ) );
599 else if( !strncmp( psz_parser
, "ports=", strlen( "ports=" ) ) )
602 psz_parser
+= strlen( "ports=" );
603 if( strchr( psz_parser
, ':' ) )
605 i_len
= strchr( psz_parser
, ':' ) - psz_parser
;
609 i_len
= strlen( psz_parser
);
611 p_sys
->psz_ports
= strndup( psz_parser
, i_len
);
616 msg_Warn( p_demux
, "unknown option" );
618 while( *psz_parser
&& *psz_parser
!= ':' )
623 if( *psz_parser
== '\0' )