]> git.sesse.net Git - vlc/blob - modules/access/jack.c
Initial JACK audio access-demux from Julien Plissonneau Duquène
[vlc] / modules / access / jack.c
1 /*****************************************************************************
2  * jack.c: JACK audio input module
3  *****************************************************************************
4  * Copyright (C) 2007 the VideoLAN team
5  * Copyright (C) 2007 Société des arts technologiques
6  *
7  * Authors: Arnaud Sala <arnaud.sala at savoirfairelinux.com>
8  *          Julien Plissonneau Duquene <... at savoirfairelinux.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /**
26  * \file modules/access/jack.c
27  * \brief JACK audio input functions
28  */
29
30 /*****************************************************************************
31  * Preamble
32  *****************************************************************************/
33 #define _GNU_SOURCE
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <string.h>
37
38 #include <vlc/vlc.h>
39 #include <vlc_input.h>
40 #include <vlc_demux.h>
41 #include <vlc_vout.h>
42 #include <vlc_codecs.h>
43 #include <vlc_url.h>
44 #include <vlc_strings.h>
45
46 #include <jack/jack.h>
47 #include <jack/ringbuffer.h>
48
49 #include <sys/mman.h> /* mlock() */
50 #include <errno.h>
51
52 /*****************************************************************************
53  * Module descriptor
54  *****************************************************************************/
55 static int  Open ( vlc_object_t * );
56 static void Close( vlc_object_t * );
57
58 #define CACHING_TEXT N_("Caching value in ms")
59 #define CACHING_LONGTEXT N_( \
60     "Make VLC buffer audio data capturer from jack for the specified " \
61     "length in milliseconds." )
62 #define PACE_TEXT N_( "Pace" )
63 #define PACE_LONGTEXT N_( \
64     "Read the audio stream at VLC pace rather than Jack pace." )
65 #define AUTO_CONNECT_TEXT N_( "Auto Connection" )
66 #define AUTO_CONNECT_LONGTEXT N_( \
67     "Automatically connect VLC input ports to available output ports." )
68
69 vlc_module_begin();
70      set_description( _("JACK audio input") );
71      set_capability( "access_demux", 0 );
72      set_shortname( _( "JACK Input" ) );
73      set_category( CAT_INPUT );
74      set_subcategory( SUBCAT_INPUT_ACCESS );
75
76      add_integer( "jack-input-caching", DEFAULT_PTS_DELAY / 1000, NULL,
77          CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
78      add_bool( "jack-input-use-vlc-pace", VLC_FALSE, NULL,
79          PACE_TEXT, PACE_LONGTEXT, VLC_TRUE );
80      add_bool( "jack-input-auto-connect", VLC_FALSE, NULL,
81          PACE_TEXT, PACE_LONGTEXT, VLC_TRUE );
82
83      add_shortcut( "jack" );
84      set_callbacks( Open, Close );
85 vlc_module_end();
86
87 /*****************************************************************************
88  * Local prototypes
89  *****************************************************************************/
90
91 struct demux_sys_t
92 {
93     /* Audio properties */
94     vlc_fourcc_t                i_acodec_raw;
95     unsigned int                i_channels;
96     int                         i_sample_rate;
97     int                         i_audio_max_frame_size;
98     int                         i_frequency;
99     block_t                     *p_block_audio;
100     es_out_id_t                 *p_es_audio;
101     date_t                      pts;
102
103     /* Jack properties */
104     jack_client_t               *p_jack_client;
105     jack_port_t                 **pp_jack_port_input;
106     jack_default_audio_sample_t **pp_jack_buffer;
107     jack_ringbuffer_t           *p_jack_ringbuffer;
108     jack_nframes_t              jack_buffer_size;
109     jack_nframes_t              jack_sample_rate;
110     size_t                      jack_sample_size;
111 };
112
113 static int Demux( demux_t * );
114 static int Control( demux_t *p_demux, int i_query, va_list args );
115
116 static void Parse ( demux_t * );
117 static int Process( jack_nframes_t i_frames, void *p_arg );
118
119 static block_t *GrabJack( demux_t * );
120
121 /*****************************************************************************
122  * Open: Connect to the JACK server
123  *****************************************************************************/
124 static int Open( vlc_object_t *p_this )
125 {
126     unsigned int i;
127     demux_t             *p_demux = ( demux_t* )p_this;
128     demux_sys_t         *p_sys;
129     es_format_t         fmt;
130     p_demux->pf_demux = Demux;
131     p_demux->pf_control = Control;
132
133     /* Allocate structure */
134     p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
135     if( p_sys == NULL )
136     {
137         msg_Err( p_demux, "out of memory, cannot allocate structure" );
138         return VLC_ENOMEM;
139     }
140     memset( p_sys, 0, sizeof( demux_sys_t ) );
141
142     /* Parse MRL */
143     Parse( p_demux );
144
145     /* Create var */
146     var_Create( p_demux, "jack-input-caching",
147         VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
148     var_Create( p_demux, "jack-input-use-vlc-pace",
149         VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
150     var_Create( p_demux, "jack-input-auto-connect",
151         VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
152
153     /* JACK connexions */
154     /* define name and connect to jack server */
155     char p_vlc_client_name[32];
156     sprintf( p_vlc_client_name, "vlc-input-%d", getpid() );
157     p_sys->p_jack_client = jack_client_new( p_vlc_client_name );
158     if( p_sys->p_jack_client == NULL )
159     {
160         msg_Err( p_demux, "failed to connect to JACK server" );
161         free( p_sys );
162         return VLC_EGENERIC;
163     }
164
165     /* allocate input ports */
166     if( p_sys->i_channels == 0 || p_sys->i_channels > 8 )
167         p_sys->i_channels = 2 ; /* default number of port */
168     p_sys->pp_jack_port_input = malloc(
169         p_sys->i_channels * sizeof( jack_port_t* ) );
170     if( p_sys->pp_jack_port_input == NULL )
171     {
172         msg_Err( p_demux, "out of memory, cannot allocate input ports" );
173         return VLC_ENOMEM;
174     }
175
176     /* allocate ringbuffer */
177     p_sys->p_jack_ringbuffer = jack_ringbuffer_create( p_sys->i_channels
178         * jack_get_buffer_size( p_sys->p_jack_client )
179         * sizeof( jack_default_audio_sample_t ) );
180     if( p_sys->p_jack_ringbuffer == NULL )
181     {
182         msg_Err( p_demux, "out of memory, cannot allocate ringbuffer" );
183         return VLC_ENOMEM;
184     }
185
186     /* register input ports */
187     for( i = 0; i <  p_sys->i_channels; i++ )
188     {
189         char p_input_name[32];
190         snprintf( p_input_name, 32, "vlc_in_%d", i+1 );
191         p_sys->pp_jack_port_input[i] = jack_port_register(
192             p_sys->p_jack_client, p_input_name, JACK_DEFAULT_AUDIO_TYPE,
193             JackPortIsInput, 0 );
194         if( p_sys->pp_jack_port_input[i] == NULL )
195         {
196             msg_Err( p_demux, "failed to register a JACK port" );
197             if( p_sys->p_jack_client) jack_client_close( p_sys->p_jack_client );
198             if( p_sys->pp_jack_port_input ) free( p_sys->pp_jack_port_input );
199             if( p_sys->p_jack_ringbuffer ) jack_ringbuffer_free( p_sys->p_jack_ringbuffer );
200             if( p_sys->pp_jack_buffer ) free( p_sys->pp_jack_buffer );
201             free( p_sys );
202             return VLC_EGENERIC;
203         }
204     }
205
206     /* allocate buffer for input ports */
207     p_sys->pp_jack_buffer = malloc ( p_sys->i_channels
208         * sizeof( jack_default_audio_sample_t * ) );
209     if( p_sys->pp_jack_buffer == NULL )
210     {
211         msg_Err( p_demux, "out of memory, cannot allocate input buffer" );
212         return VLC_ENOMEM;
213     }
214
215     /* set process callback */
216     jack_set_process_callback( p_sys->p_jack_client, Process, p_demux );
217
218     /* tell jack server we are ready */
219     if ( jack_activate( p_sys->p_jack_client ) )
220     {
221         msg_Err( p_demux, "failed to activate JACK client" );
222         if( p_sys->p_jack_client) jack_client_close( p_sys->p_jack_client );
223         if( p_sys->pp_jack_port_input ) free( p_sys->pp_jack_port_input );
224         if( p_sys->p_jack_ringbuffer ) jack_ringbuffer_free( p_sys->p_jack_ringbuffer );
225         if( p_sys->pp_jack_buffer ) free( p_sys->pp_jack_buffer );
226         free( p_sys );
227         return VLC_EGENERIC;
228     }
229
230     /* connect vlc input to jack output ports if requested */
231     if( var_GetBool( p_demux, "jack-input-auto-connect" ) )
232     {
233         const char **pp_jack_port_output;
234         int        i_out_ports = 0;
235         int        i_in;
236         int        j;
237         pp_jack_port_output = jack_get_ports( p_sys->p_jack_client, NULL, NULL,
238             JackPortIsOutput );
239
240         while( pp_jack_port_output && pp_jack_port_output[i_out_ports] )
241         {
242             i_out_ports++;
243         }
244         if( i_out_ports > 0 )
245         {
246             for( j = 0; j < i_out_ports; j++ )
247             {
248                 i_in = j % p_sys->i_channels;
249                 jack_connect( p_sys->p_jack_client, pp_jack_port_output[j],
250                     jack_port_name( p_sys->pp_jack_port_input[i_in] ) );
251             }
252         }
253         if( pp_jack_port_output ) free( pp_jack_port_output );
254     }
255
256     /* info about jack server */
257     /* get buffers size */
258     p_sys->jack_buffer_size = jack_get_buffer_size( p_sys->p_jack_client );
259     /* get sample rate */
260     p_sys->jack_sample_rate = jack_get_sample_rate( p_sys->p_jack_client );
261     /* get sample size */
262     p_sys->jack_sample_size = sizeof( jack_default_audio_sample_t );
263
264     /* Define output format */
265     es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 'f','l','3','2' ) );
266     fmt.audio.i_channels =  p_sys->i_channels;
267     fmt.audio.i_rate =  p_sys->jack_sample_rate;
268     fmt.audio.i_bitspersample =  p_sys->jack_sample_size * 8;
269     fmt.audio.i_blockalign = fmt.audio.i_bitspersample / 8;
270     fmt.i_bitrate = fmt.audio.i_rate * fmt.audio.i_bitspersample
271         * fmt.audio.i_channels;
272
273     p_sys->p_es_audio = es_out_Add( p_demux->out, &fmt );
274     date_Init( &p_sys->pts, fmt.audio.i_rate, 1 );
275     date_Set( &p_sys->pts, 1 );
276
277     return VLC_SUCCESS;
278 }
279
280
281 /*****************************************************************************
282  * Close: Disconnect from jack server and release associated resources
283  *****************************************************************************/
284 static void Close( vlc_object_t *p_this )
285 {
286     demux_t    *p_demux = ( demux_t* )p_this;
287     demux_sys_t    *p_sys = p_demux->p_sys;
288
289     msg_Dbg( p_demux,"Module unloaded" );
290     if( p_sys->p_block_audio ) block_Release( p_sys->p_block_audio );
291     if( p_sys->p_jack_client ) jack_client_close( p_sys->p_jack_client );
292     if( p_sys->p_jack_ringbuffer ) jack_ringbuffer_free( p_sys->p_jack_ringbuffer );
293     if( p_sys->pp_jack_port_input ) free( p_sys->pp_jack_port_input );
294     if( p_sys->pp_jack_buffer ) free( p_sys->pp_jack_buffer );
295     free( p_sys );
296 }
297
298
299 /*****************************************************************************
300  * Control
301  *****************************************************************************/
302 static int Control( demux_t *p_demux, int i_query, va_list args )
303 {
304     vlc_bool_t  *pb;
305     int64_t     *pi64;
306     demux_sys_t *p_sys = p_demux->p_sys;
307
308     switch( i_query )
309     {
310     /* Special for access_demux */
311     case DEMUX_CAN_PAUSE:
312     case DEMUX_SET_PAUSE_STATE:
313         return VLC_SUCCESS;
314     case DEMUX_CAN_CONTROL_PACE:
315         pb = ( vlc_bool_t* )va_arg( args, vlc_bool_t * );
316         *pb = var_GetBool( p_demux, "jack-input-use-vlc-pace" );
317         return VLC_SUCCESS;
318
319     case DEMUX_GET_PTS_DELAY:
320         pi64 = ( int64_t* )va_arg( args, int64_t * );
321         *pi64 = ( int64_t )var_GetInteger( p_demux, "jack-input-caching" )
322             * 1000;
323         return VLC_SUCCESS;
324
325     case DEMUX_GET_TIME:
326         pi64 = ( int64_t* )va_arg( args, int64_t * );
327         *pi64 =  date_Get(&p_sys->pts);
328             return VLC_SUCCESS;
329
330     /* TODO implement others */
331     default:
332         return VLC_EGENERIC;
333     }
334
335     return VLC_EGENERIC;
336 }
337
338
339 /*****************************************************************************
340  * Demux
341  *****************************************************************************/
342 static int Demux( demux_t *p_demux )
343 {
344
345     demux_sys_t *p_sys;
346     es_out_id_t  *p_es;
347     block_t *p_block;
348
349     p_sys = p_demux->p_sys;
350     p_es = p_sys->p_es_audio;
351     p_block = GrabJack( p_demux );
352
353     if( p_block )
354     {
355         es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
356         es_out_Send( p_demux->out, p_es, p_block );
357     }
358
359     return 1;
360 }
361
362
363 /*****************************************************************************
364  * Process Callback : fill ringbuffer with Jack audio data
365  *****************************************************************************/
366 int Process( jack_nframes_t i_frames, void *p_arg )
367 {
368     demux_t            *p_demux = ( demux_t* )p_arg;
369     demux_sys_t        *p_sys = p_demux->p_sys;
370     unsigned int        i, j;
371     size_t            i_write;
372
373     /* Get and interlace buffers */
374     for ( i = 0; i < p_sys->i_channels ; i++ )
375     {
376         p_sys->pp_jack_buffer[i] = jack_port_get_buffer(
377             p_sys->pp_jack_port_input[i], i_frames );
378     }
379
380     /* fill ring buffer with signal */
381     for( j = 0; j < p_sys->jack_buffer_size; j++ )
382     {
383         for( i = 0; i <p_sys->i_channels; i++ )
384         {
385             if( jack_ringbuffer_write_space( p_sys->p_jack_ringbuffer )
386                 < p_sys->jack_sample_size ) return 1; // buffer overflow
387             i_write = jack_ringbuffer_write( p_sys->p_jack_ringbuffer,
388                 p_sys->pp_jack_buffer[i]+j, p_sys->jack_sample_size );
389         }
390     }
391
392     return 1;
393 }
394
395
396 /*****************************************************************************
397  * GrabJack: grab audio data in the Jack buffer
398  *****************************************************************************/
399 static block_t *GrabJack( demux_t *p_demux )
400 {
401     size_t      i_read;
402     demux_sys_t *p_sys = p_demux->p_sys;
403     block_t     *p_block;
404
405     /* read signal from ring buffer */
406     i_read = jack_ringbuffer_read_space( p_sys->p_jack_ringbuffer );
407
408     if( i_read < 100 ) /* avoid small read */
409     {   /* vlc has too much free time on its hands? */
410         msleep(1000);
411         return NULL;
412     }
413
414     if( p_sys->p_block_audio )
415     {
416         p_block = p_sys->p_block_audio;
417     }
418     else
419     {
420         p_block = block_New( p_demux, i_read );
421     }
422     if( !p_block )
423     {
424         msg_Warn( p_demux, "cannot get block" );
425         return 0;
426     }
427
428     p_sys->p_block_audio = p_block;
429     p_block->i_buffer = i_read;
430     p_sys->p_block_audio = 0;
431
432     jack_ringbuffer_read( p_sys->p_jack_ringbuffer, p_block->p_buffer, i_read );
433
434     p_block->i_dts = p_block->i_pts = date_Increment(
435         &p_sys->pts, i_read/(p_sys->i_channels * p_sys->jack_sample_size) );
436
437     return p_block;
438 }
439
440
441 /*****************************************************************************
442  * Parse: Parse the MRL
443  *****************************************************************************/
444 static void Parse( demux_t *p_demux )
445 {
446     demux_sys_t *p_sys = p_demux->p_sys;
447     char *psz_dup = strdup( p_demux->psz_path );
448     char *psz_parser = psz_dup;
449
450     if( !strncmp( psz_parser, "channels=", strlen( "channels=" ) ) )
451     {
452         p_sys->i_channels = abs( strtol( psz_parser + strlen( "channels=" ),
453             &psz_parser, 0 ) );
454     }
455     else
456     {
457         msg_Warn( p_demux, "unknown option" );
458     }
459
460     while( *psz_parser && *psz_parser != ':' )
461     {
462         psz_parser++;
463     }
464
465     if( *psz_parser == ':' )
466     {
467         for( ;; )
468         {
469             *psz_parser++ = '\0';
470             if( !strncmp( psz_parser, "channels=", strlen( "channels=" ) ) )
471             {
472                 p_sys->i_channels = abs( strtol(
473                     psz_parser + strlen( "channels=" ), &psz_parser, 0 ) );
474             }
475             else
476             {
477                 msg_Warn( p_demux, "unknown option" );
478             }
479             while( *psz_parser && *psz_parser != ':' )
480             {
481                 psz_parser++;
482             }
483
484             if( *psz_parser == '\0' )
485             {
486                 break;
487             }
488         }
489     }
490 }
491