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