jack.c 19.7 KB
Newer Older
1 2 3
/*****************************************************************************
 * jack.c: JACK audio input module
 *****************************************************************************
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
4
 * Copyright (C) 2007-2008 VLC authors and VideoLAN
5
 * Copyright (C) 2007 Société des arts technologiques
6
 * Copyright (C) 2007 Savoir-faire Linux
7 8 9
 *
 * Authors: Arnaud Sala <arnaud.sala at savoirfairelinux.com>
 *          Julien Plissonneau Duquene <... at savoirfairelinux.com>
10
 *          Pierre-Luc Beaudoin <pierre-luc.beaudoin at savoirfairelinux.com>
11
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
12 13 14
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
15 16 17 18
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
19 20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
21
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
22 23 24
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 26 27 28 29 30 31 32 33 34 35
 *****************************************************************************/

/**
 * \file modules/access/jack.c
 * \brief JACK audio input functions
 */

/*****************************************************************************
 * Preamble
 *****************************************************************************/

36 37 38 39
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

40
#include <vlc_common.h>
41
#include <vlc_plugin.h>
42 43 44 45 46 47 48 49
#include <vlc_input.h>
#include <vlc_demux.h>
#include <vlc_url.h>
#include <vlc_strings.h>

#include <jack/jack.h>
#include <jack/ringbuffer.h>

50 51
#include <sys/types.h>
#include <unistd.h>
52 53 54 55 56 57 58 59 60 61

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
static int  Open ( vlc_object_t * );
static void Close( vlc_object_t * );

#define PACE_TEXT N_( "Pace" )
#define PACE_LONGTEXT N_( \
    "Read the audio stream at VLC pace rather than Jack pace." )
62
#define AUTO_CONNECT_TEXT N_( "Auto connection" )
63 64 65
#define AUTO_CONNECT_LONGTEXT N_( \
    "Automatically connect VLC input ports to available output ports." )

66 67
vlc_module_begin ()
     set_description( N_("JACK audio input") )
68
     set_capability( "access", 0 )
69 70 71
     set_shortname( N_( "JACK Input" ) )
     set_category( CAT_INPUT )
     set_subcategory( SUBCAT_INPUT_ACCESS )
72

73
     add_bool( "jack-input-use-vlc-pace", false,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
74
         PACE_TEXT, PACE_LONGTEXT, true )
75
     add_bool( "jack-input-auto-connect", false,
76
         AUTO_CONNECT_TEXT, AUTO_CONNECT_LONGTEXT, false )
77

78 79 80
     add_shortcut( "jack" )
     set_callbacks( Open, Close )
vlc_module_end ()
81 82 83 84 85

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/

86
typedef struct
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
{
    /* Audio properties */
    vlc_fourcc_t                i_acodec_raw;
    unsigned int                i_channels;
    int                         i_sample_rate;
    int                         i_audio_max_frame_size;
    int                         i_frequency;
    block_t                     *p_block_audio;
    es_out_id_t                 *p_es_audio;
    date_t                      pts;

    /* Jack properties */
    jack_client_t               *p_jack_client;
    jack_port_t                 **pp_jack_port_input;
    jack_default_audio_sample_t **pp_jack_buffer;
    jack_ringbuffer_t           *p_jack_ringbuffer;
    jack_nframes_t              jack_buffer_size;
    jack_nframes_t              jack_sample_rate;
    size_t                      jack_sample_size;
106 107 108
    char                        *psz_ports;
    char                        **pp_jack_port_table;
    char                        i_match_ports;
109
} demux_sys_t;
110 111 112 113 114

static int Demux( demux_t * );
static int Control( demux_t *p_demux, int i_query, va_list args );

static void Parse ( demux_t * );
115
static void Port_finder( demux_t * );
116 117 118 119 120 121 122 123 124
static int Process( jack_nframes_t i_frames, void *p_arg );

static block_t *GrabJack( demux_t * );

/*****************************************************************************
 * Open: Connect to the JACK server
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
Filip Roséen's avatar
Filip Roséen committed
125 126 127
    demux_t *p_demux = ( demux_t* )p_this;
    demux_sys_t *p_sys;
    es_format_t fmt;
128 129
    int i_out_ports = 0;

130 131 132
    if (p_demux->out == NULL)
        return VLC_EGENERIC;

133 134 135 136
    p_demux->pf_demux = Demux;
    p_demux->pf_control = Control;

    /* Allocate structure */
137
    p_demux->p_sys = p_sys = vlc_obj_calloc( p_this, 1, sizeof( demux_sys_t ) );
138
    if( !p_sys )
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
        return VLC_ENOMEM;

    /* Parse MRL */
    Parse( p_demux );

    /* Create var */
    var_Create( p_demux, "jack-input-use-vlc-pace",
        VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
    var_Create( p_demux, "jack-input-auto-connect",
        VLC_VAR_BOOL | VLC_VAR_DOINHERIT );

    /* JACK connexions */
    /* define name and connect to jack server */
    char p_vlc_client_name[32];
    sprintf( p_vlc_client_name, "vlc-input-%d", getpid() );
154
    p_sys->p_jack_client = jack_client_open( p_vlc_client_name, JackNullOption, NULL );
155 156 157 158 159 160
    if( p_sys->p_jack_client == NULL )
    {
        msg_Err( p_demux, "failed to connect to JACK server" );
        return VLC_EGENERIC;
    }

161
    /* find some specifics ports if user entered a regexp */
162
    if( p_sys->psz_ports )
163 164 165 166 167 168 169 170
    {
        Port_finder( p_demux );
        if( p_sys->i_channels == 0 )
        {
            p_sys->i_channels = p_sys->i_match_ports;
        }
    }

171
    /* allocate input ports */
172
    if( p_sys->i_channels == 0 ) p_sys->i_channels = 2 ; /* default number of port */
173 174 175 176
    p_sys->pp_jack_port_input = malloc(
        p_sys->i_channels * sizeof( jack_port_t* ) );
    if( p_sys->pp_jack_port_input == NULL )
    {
177
        jack_client_close( p_sys->p_jack_client );
178 179 180 181
        return VLC_ENOMEM;
    }

    /* allocate ringbuffer */
182 183
    /* The length of the ringbuffer is critical, it must be large enought
       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 186
         * jack_get_sample_rate( p_sys->p_jack_client )
         * sizeof( jack_default_audio_sample_t ) );
187 188
    if( p_sys->p_jack_ringbuffer == NULL )
    {
189 190
        free( p_sys->pp_jack_port_input );
        jack_client_close( p_sys->p_jack_client );
191 192 193 194
        return VLC_ENOMEM;
    }

    /* register input ports */
195
    for( unsigned i = 0; i <  p_sys->i_channels; i++ )
196 197 198 199 200 201 202 203 204
    {
        char p_input_name[32];
        snprintf( p_input_name, 32, "vlc_in_%d", i+1 );
        p_sys->pp_jack_port_input[i] = jack_port_register(
            p_sys->p_jack_client, p_input_name, JACK_DEFAULT_AUDIO_TYPE,
            JackPortIsInput, 0 );
        if( p_sys->pp_jack_port_input[i] == NULL )
        {
            msg_Err( p_demux, "failed to register a JACK port" );
205
            jack_ringbuffer_free( p_sys->p_jack_ringbuffer );
206
            free( p_sys->pp_jack_port_input );
207
            jack_client_close( p_sys->p_jack_client );
208 209 210 211 212 213 214 215 216
            return VLC_EGENERIC;
        }
    }

    /* allocate buffer for input ports */
    p_sys->pp_jack_buffer = malloc ( p_sys->i_channels
        * sizeof( jack_default_audio_sample_t * ) );
    if( p_sys->pp_jack_buffer == NULL )
    {
217
        for( unsigned i = 0; i < p_sys->i_channels; i++ )
218 219 220 221
            jack_port_unregister( p_sys->p_jack_client, p_sys->pp_jack_port_input[i] );
        jack_ringbuffer_free( p_sys->p_jack_ringbuffer );
        free( p_sys->pp_jack_port_input );
        jack_client_close( p_sys->p_jack_client );
222 223 224 225 226 227 228 229 230 231
        return VLC_ENOMEM;
    }

    /* set process callback */
    jack_set_process_callback( p_sys->p_jack_client, Process, p_demux );

    /* tell jack server we are ready */
    if ( jack_activate( p_sys->p_jack_client ) )
    {
        msg_Err( p_demux, "failed to activate JACK client" );
232
        free( p_sys->pp_jack_buffer );
233
        for( unsigned i = 0; i < p_sys->i_channels; i++ )
234 235 236 237
            jack_port_unregister( p_sys->p_jack_client, p_sys->pp_jack_port_input[i] );
        jack_ringbuffer_free( p_sys->p_jack_ringbuffer );
        free( p_sys->pp_jack_port_input );
        jack_client_close( p_sys->p_jack_client );
238 239 240
        return VLC_EGENERIC;
    }

241 242 243
    /* connect vlc input to specifics jack output ports if requested */
   /*  if( var_GetBool( p_demux, "jack-input-auto-connect" ) && p_sys->psz_ports ) */
    if( p_sys->psz_ports )
244
    {
245
        for( int j = 0; j < p_sys->i_match_ports; j++ )
246
        {
247 248 249
            int i_input_ports = j % p_sys->i_channels;
            jack_connect( p_sys->p_jack_client, p_sys->pp_jack_port_table[j],
                jack_port_name( p_sys->pp_jack_port_input[i_input_ports] ) );
250 251 252 253 254 255
        }
    }

    /* connect vlc input to all jack output ports if requested */
    if( var_GetBool( p_demux, "jack-input-auto-connect" ) && !p_sys->psz_ports )
    {
256 257
        const char **pp_jack_port_output = jack_get_ports( p_sys->p_jack_client,
                                                           NULL, NULL, JackPortIsOutput );
258 259 260 261 262

        while( pp_jack_port_output && pp_jack_port_output[i_out_ports] )
        {
            i_out_ports++;
        }
263 264

        for( int j = 0; j < i_out_ports; j++ )
265
        {
266 267 268
            int i_input_ports = j % p_sys->i_channels;
            jack_connect( p_sys->p_jack_client, pp_jack_port_output[j],
                jack_port_name( p_sys->pp_jack_port_input[i_input_ports] ) );
269
        }
270

271
        free( pp_jack_port_output );
272 273 274 275 276 277 278 279 280 281 282
    }

    /* info about jack server */
    /* get buffers size */
    p_sys->jack_buffer_size = jack_get_buffer_size( p_sys->p_jack_client );
    /* get sample rate */
    p_sys->jack_sample_rate = jack_get_sample_rate( p_sys->p_jack_client );
    /* get sample size */
    p_sys->jack_sample_size = sizeof( jack_default_audio_sample_t );

    /* Define output format */
283
    es_format_Init( &fmt, AUDIO_ES, VLC_CODEC_FL32 );
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
    fmt.audio.i_channels =  p_sys->i_channels;
    fmt.audio.i_rate =  p_sys->jack_sample_rate;
    fmt.audio.i_bitspersample =  p_sys->jack_sample_size * 8;
    fmt.audio.i_blockalign = fmt.audio.i_bitspersample / 8;
    fmt.i_bitrate = fmt.audio.i_rate * fmt.audio.i_bitspersample
        * fmt.audio.i_channels;

    p_sys->p_es_audio = es_out_Add( p_demux->out, &fmt );
    date_Init( &p_sys->pts, fmt.audio.i_rate, 1 );
    date_Set( &p_sys->pts, 1 );

    return VLC_SUCCESS;
}


/*****************************************************************************
 * Close: Disconnect from jack server and release associated resources
 *****************************************************************************/
static void Close( vlc_object_t *p_this )
{
    demux_t    *p_demux = ( demux_t* )p_this;
    demux_sys_t    *p_sys = p_demux->p_sys;

    msg_Dbg( p_demux,"Module unloaded" );
    if( p_sys->p_block_audio ) block_Release( p_sys->p_block_audio );
    if( p_sys->p_jack_client ) jack_client_close( p_sys->p_jack_client );
    if( p_sys->p_jack_ringbuffer ) jack_ringbuffer_free( p_sys->p_jack_ringbuffer );
311 312 313
    free( p_sys->pp_jack_port_input );
    free( p_sys->pp_jack_buffer );
    free( p_sys->pp_jack_port_table );
314 315 316 317 318 319 320 321
}


/*****************************************************************************
 * Control
 *****************************************************************************/
static int Control( demux_t *p_demux, int i_query, va_list args )
{
Filip Roséen's avatar
Filip Roséen committed
322 323
    bool *pb;
    int64_t *pi64;
324 325 326 327 328 329
    demux_sys_t *p_sys = p_demux->p_sys;

    switch( i_query )
    {
    /* Special for access_demux */
    case DEMUX_CAN_PAUSE:
330
    case DEMUX_CAN_SEEK:
331
        pb = va_arg( args, bool * );
332
        *pb = true;
333 334
        return VLC_SUCCESS;

335 336 337
    case DEMUX_SET_PAUSE_STATE:
        return VLC_SUCCESS;
    case DEMUX_CAN_CONTROL_PACE:
338
        pb = va_arg( args, bool * );
339 340 341 342
        *pb = var_GetBool( p_demux, "jack-input-use-vlc-pace" );
        return VLC_SUCCESS;

    case DEMUX_GET_PTS_DELAY:
343
        pi64 = va_arg( args, int64_t * );
344
        *pi64 = INT64_C(1000) * var_InheritInteger( p_demux, "live-caching" );
345 346 347
        return VLC_SUCCESS;

    case DEMUX_GET_TIME:
348 349 350
        pi64 = va_arg( args, int64_t * );
        *pi64 = date_Get(&p_sys->pts);
        return VLC_SUCCESS;
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366

    /* TODO implement others */
    default:
        return VLC_EGENERIC;
    }

    return VLC_EGENERIC;
}


/*****************************************************************************
 * Demux
 *****************************************************************************/
static int Demux( demux_t *p_demux )
{
    demux_sys_t *p_sys;
Filip Roséen's avatar
Filip Roséen committed
367
    es_out_id_t *p_es;
368 369 370 371 372 373 374 375
    block_t *p_block;

    p_sys = p_demux->p_sys;
    p_es = p_sys->p_es_audio;
    p_block = GrabJack( p_demux );

    if( p_block )
    {
376
        es_out_SetPCR( p_demux->out, p_block->i_pts );
377 378 379 380 381 382 383 384 385 386 387 388
        es_out_Send( p_demux->out, p_es, p_block );
    }

    return 1;
}


/*****************************************************************************
 * Process Callback : fill ringbuffer with Jack audio data
 *****************************************************************************/
int Process( jack_nframes_t i_frames, void *p_arg )
{
Filip Roséen's avatar
Filip Roséen committed
389 390
    demux_t *p_demux = ( demux_t* )p_arg;
    demux_sys_t *p_sys = p_demux->p_sys;
391

392
    /* Get and interlace buffers */
393
    for ( unsigned i = 0; i < p_sys->i_channels ; i++ )
394 395 396 397 398 399
    {
        p_sys->pp_jack_buffer[i] = jack_port_get_buffer(
            p_sys->pp_jack_port_input[i], i_frames );
    }

    /* fill ring buffer with signal */
400
    for( unsigned j = 0; j < i_frames; j++ )
401
    {
402
        for( unsigned i = 0; i <p_sys->i_channels; i++ )
403
        {
404
            if( jack_ringbuffer_write_space( p_sys->p_jack_ringbuffer ) <
405 406 407 408
                p_sys->jack_sample_size ) {
                msg_Err( p_demux, "buffer overflow");
                return 0; // buffer overflow
            }
409 410 411
            size_t i_write = jack_ringbuffer_write( p_sys->p_jack_ringbuffer,
                                                    ( char * ) (p_sys->pp_jack_buffer[i]+j),
                                                    p_sys->jack_sample_size );
412 413 414
            if (i_write != p_sys->jack_sample_size ) {
                msg_Warn( p_demux, "error writing on ring buffer");
            }
415 416 417
        }
    }

Adrian Knoth's avatar
Adrian Knoth committed
418
    return 0;
419 420 421 422 423 424 425 426 427
}


/*****************************************************************************
 * GrabJack: grab audio data in the Jack buffer
 *****************************************************************************/
static block_t *GrabJack( demux_t *p_demux )
{
    demux_sys_t *p_sys = p_demux->p_sys;
Filip Roséen's avatar
Filip Roséen committed
428
    block_t *p_block;
429 430

    /* read signal from ring buffer */
431
    size_t i_read = jack_ringbuffer_read_space( p_sys->p_jack_ringbuffer );
432 433 434

    if( i_read < 100 ) /* avoid small read */
    {   /* vlc has too much free time on its hands? */
435 436
#undef msleep
#warning Hmm.... looks wrong
437 438 439 440 441 442 443 444 445 446
        msleep(1000);
        return NULL;
    }

    if( p_sys->p_block_audio )
    {
        p_block = p_sys->p_block_audio;
    }
    else
    {
447
        p_block = block_Alloc( i_read );
448 449 450 451 452 453
    }
    if( !p_block )
    {
        msg_Warn( p_demux, "cannot get block" );
        return 0;
    }
Filip Roséen's avatar
Filip Roséen committed
454

455 456 457 458 459 460 461 462 463
    //Find the previous power of 2, this algo assumes size_t has the same size on all arch
    i_read >>= 1;
    i_read--;
    i_read |= i_read >> 1;
    i_read |= i_read >> 2;
    i_read |= i_read >> 4;
    i_read |= i_read >> 8;
    i_read |= i_read >> 16;
    i_read++;
Filip Roséen's avatar
Filip Roséen committed
464

465
    i_read = jack_ringbuffer_read( p_sys->p_jack_ringbuffer, ( char * ) p_block->p_buffer, i_read );
Filip Roséen's avatar
Filip Roséen committed
466

467 468
    p_block->i_dts = p_block->i_pts =    date_Increment( &p_sys->pts,
         i_read/(p_sys->i_channels * p_sys->jack_sample_size) );
469 470 471 472

    p_sys->p_block_audio = p_block;
    p_block->i_buffer = i_read;
    p_sys->p_block_audio = 0;
Filip Roséen's avatar
Filip Roséen committed
473

474 475 476 477 478
    return p_block;
}


/*****************************************************************************
479
 * Port_finder: compare ports with the regexp entered
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
 *****************************************************************************/
static void Port_finder( demux_t *p_demux )
{
    demux_sys_t *p_sys = p_demux->p_sys;
    char *psz_expr = p_sys->psz_ports;
    char *token = NULL;
    char *state = NULL;
    char *psz_uri = NULL;
    const char **pp_jack_port_output = NULL;
    int i_out_ports = 0;
    int i_total_out_ports =0;
    p_sys->pp_jack_port_table = NULL;

    /* parse the ports part of the MRL */
    for( token = strtok_r( psz_expr, ",", &state ); token;
            token = strtok_r( NULL, ",", &state ) )
    {
497
        psz_uri = vlc_uri_decode_duplicate( token );
498 499 500 501 502 503 504
        /* get the ports which match the regexp */
        pp_jack_port_output = jack_get_ports( p_sys->p_jack_client,
           psz_uri, NULL, JackPortIsOutput );
        if( pp_jack_port_output == NULL )
            msg_Err( p_demux, "port(s) asked not found:%s", psz_uri );
        else
        {
Rémi Duraffort's avatar
Rémi Duraffort committed
505
            while( pp_jack_port_output[i_out_ports] )
506 507
                i_out_ports++;
            /* alloc an array to store all the matched ports */
508
            p_sys->pp_jack_port_table = xrealloc( p_sys->pp_jack_port_table,
509
                (i_out_ports * sizeof( char * ) + i_total_out_ports * sizeof( char * ) ) );
510

511 512
            for(int i=0; i<i_out_ports;i++)
                p_sys->pp_jack_port_table[i_total_out_ports+i] = ( char * ) pp_jack_port_output[i];
513

514
            i_total_out_ports += i_out_ports;
Rémi Duraffort's avatar
Rémi Duraffort committed
515 516

            free( pp_jack_port_output );
517 518
        }
    }
519

520
    p_sys->i_match_ports = i_total_out_ports;
521 522 523 524 525 526 527 528 529
}


/*****************************************************************************
 * Parse: Parse the MRL
 *****************************************************************************/
static void Parse( demux_t *p_demux )
{
    demux_sys_t *p_sys = p_demux->p_sys;
530
    char *psz_dup = strdup( p_demux->psz_location );
531 532 533 534 535 536 537
    char *psz_parser = psz_dup;

    if( !strncmp( psz_parser, "channels=", strlen( "channels=" ) ) )
    {
        p_sys->i_channels = abs( strtol( psz_parser + strlen( "channels=" ),
            &psz_parser, 0 ) );
    }
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
    else if( !strncmp( psz_parser, "ports=", strlen( "ports=" ) ) )
    {
        int i_len;
        psz_parser += strlen( "ports=" );
        if( strchr( psz_parser, ':' ) )
        {
            i_len = strchr( psz_parser, ':' ) - psz_parser;
        }
        else
        {
            i_len = strlen( psz_parser );
        }
        p_sys->psz_ports = strndup( psz_parser, i_len );
        psz_parser += i_len;
    }
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
    else
    {
        msg_Warn( p_demux, "unknown option" );
    }

    while( *psz_parser && *psz_parser != ':' )
    {
        psz_parser++;
    }

    if( *psz_parser == ':' )
    {
        for( ;; )
        {
            *psz_parser++ = '\0';
            if( !strncmp( psz_parser, "channels=", strlen( "channels=" ) ) )
            {
                p_sys->i_channels = abs( strtol(
                    psz_parser + strlen( "channels=" ), &psz_parser, 0 ) );
            }
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
            else if( !strncmp( psz_parser, "ports=", strlen( "ports=" ) ) )
            {
                int i_len;
                psz_parser += strlen( "ports=" );
                if( strchr( psz_parser, ':' ) )
                {
                    i_len = strchr( psz_parser, ':' ) - psz_parser;
                }
                else
                {
                    i_len = strlen( psz_parser );
                }
                p_sys->psz_ports = strndup( psz_parser, i_len );
                psz_parser += i_len;
            }
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
            else
            {
                msg_Warn( p_demux, "unknown option" );
            }
            while( *psz_parser && *psz_parser != ':' )
            {
                psz_parser++;
            }

            if( *psz_parser == '\0' )
            {
                break;
            }
        }
    }
Rémi Duraffort's avatar
Rémi Duraffort committed
603 604

    free( psz_dup );
605 606
}