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