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