jack.c 19.6 KB
Newer Older
1 2 3
/*****************************************************************************
 * jack.c: JACK audio input module
 *****************************************************************************
Jean-Baptiste Kempf's avatar
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
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
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
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 68 69 70 71
vlc_module_begin ()
     set_description( N_("JACK audio input") )
     set_capability( "access_demux", 0 )
     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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105

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

struct demux_sys_t
{
    /* 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 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 )
{
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 133
    p_demux->pf_demux = Demux;
    p_demux->pf_control = Control;

    /* Allocate structure */
134
    p_demux->p_sys = p_sys = vlc_obj_calloc( p_this, 1, sizeof( demux_sys_t ) );
135
    if( !p_sys )
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
        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() );
151
    p_sys->p_jack_client = jack_client_open( p_vlc_client_name, JackNullOption, NULL );
152 153 154 155 156 157
    if( p_sys->p_jack_client == NULL )
    {
        msg_Err( p_demux, "failed to connect to JACK server" );
        return VLC_EGENERIC;
    }

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

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

    /* allocate ringbuffer */
179 180
    /* 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 */
181
    p_sys->p_jack_ringbuffer = jack_ringbuffer_create( p_sys->i_channels
182 183
         * jack_get_sample_rate( p_sys->p_jack_client )
         * sizeof( jack_default_audio_sample_t ) );
184 185
    if( p_sys->p_jack_ringbuffer == NULL )
    {
186 187
        free( p_sys->pp_jack_port_input );
        jack_client_close( p_sys->p_jack_client );
188 189 190 191
        return VLC_ENOMEM;
    }

    /* register input ports */
192
    for( unsigned i = 0; i <  p_sys->i_channels; i++ )
193 194 195 196 197 198 199 200 201
    {
        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" );
202
            jack_ringbuffer_free( p_sys->p_jack_ringbuffer );
203
            free( p_sys->pp_jack_port_input );
204
            jack_client_close( p_sys->p_jack_client );
205 206 207 208 209 210 211 212 213
            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 )
    {
214
        for( unsigned i = 0; i < p_sys->i_channels; i++ )
215 216 217 218
            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 );
219 220 221 222 223 224 225 226 227 228
        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" );
229
        free( p_sys->pp_jack_buffer );
230
        for( unsigned i = 0; i < p_sys->i_channels; i++ )
231 232 233 234
            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 );
235 236 237
        return VLC_EGENERIC;
    }

238 239 240
    /* 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 )
241
    {
242
        for( int j = 0; j < p_sys->i_match_ports; j++ )
243
        {
244 245 246
            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] ) );
247 248 249 250 251 252
        }
    }

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

        while( pp_jack_port_output && pp_jack_port_output[i_out_ports] )
        {
            i_out_ports++;
        }
260 261

        for( int j = 0; j < i_out_ports; j++ )
262
        {
263 264 265
            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] ) );
266
        }
267

268
        free( pp_jack_port_output );
269 270 271 272 273 274 275 276 277 278 279
    }

    /* 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 */
280
    es_format_Init( &fmt, AUDIO_ES, VLC_CODEC_FL32 );
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
    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 );
308 309 310
    free( p_sys->pp_jack_port_input );
    free( p_sys->pp_jack_buffer );
    free( p_sys->pp_jack_port_table );
311 312 313 314 315 316 317 318
}


/*****************************************************************************
 * Control
 *****************************************************************************/
static int Control( demux_t *p_demux, int i_query, va_list args )
{
319 320
    bool *pb;
    int64_t *pi64;
321 322 323 324 325 326
    demux_sys_t *p_sys = p_demux->p_sys;

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

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

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

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

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

    return VLC_EGENERIC;
}


/*****************************************************************************
 * Demux
 *****************************************************************************/
static int Demux( demux_t *p_demux )
{
    demux_sys_t *p_sys;
364
    es_out_id_t *p_es;
365 366 367 368 369 370 371 372
    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 )
    {
373
        es_out_SetPCR( p_demux->out, p_block->i_pts );
374 375 376 377 378 379 380 381 382 383 384 385
        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 )
{
386 387
    demux_t *p_demux = ( demux_t* )p_arg;
    demux_sys_t *p_sys = p_demux->p_sys;
388

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

    /* fill ring buffer with signal */
397
    for( unsigned j = 0; j < i_frames; j++ )
398
    {
399
        for( unsigned i = 0; i <p_sys->i_channels; i++ )
400
        {
401
            if( jack_ringbuffer_write_space( p_sys->p_jack_ringbuffer ) <
402 403 404 405
                p_sys->jack_sample_size ) {
                msg_Err( p_demux, "buffer overflow");
                return 0; // buffer overflow
            }
406 407 408
            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 );
409 410 411
            if (i_write != p_sys->jack_sample_size ) {
                msg_Warn( p_demux, "error writing on ring buffer");
            }
412 413 414
        }
    }

415
    return 0;
416 417 418 419 420 421 422 423 424
}


/*****************************************************************************
 * 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;
425
    block_t *p_block;
426 427

    /* read signal from ring buffer */
428
    size_t i_read = jack_ringbuffer_read_space( p_sys->p_jack_ringbuffer );
429 430 431

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

    if( p_sys->p_block_audio )
    {
        p_block = p_sys->p_block_audio;
    }
    else
    {
444
        p_block = block_Alloc( i_read );
445 446 447 448 449 450
    }
    if( !p_block )
    {
        msg_Warn( p_demux, "cannot get block" );
        return 0;
    }
451

452 453 454 455 456 457 458 459 460
    //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++;
461

462
    i_read = jack_ringbuffer_read( p_sys->p_jack_ringbuffer, ( char * ) p_block->p_buffer, i_read );
463

464 465
    p_block->i_dts = p_block->i_pts =    date_Increment( &p_sys->pts,
         i_read/(p_sys->i_channels * p_sys->jack_sample_size) );
466 467 468 469

    p_sys->p_block_audio = p_block;
    p_block->i_buffer = i_read;
    p_sys->p_block_audio = 0;
470

471 472 473 474 475
    return p_block;
}


/*****************************************************************************
476
 * Port_finder: compare ports with the regexp entered
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
 *****************************************************************************/
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 ) )
    {
494
        psz_uri = vlc_uri_decode_duplicate( token );
495 496 497 498 499 500 501
        /* 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
502
            while( pp_jack_port_output[i_out_ports] )
503 504
                i_out_ports++;
            /* alloc an array to store all the matched ports */
505
            p_sys->pp_jack_port_table = xrealloc( p_sys->pp_jack_port_table,
506
                (i_out_ports * sizeof( char * ) + i_total_out_ports * sizeof( char * ) ) );
507

508 509
            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];
510

511
            i_total_out_ports += i_out_ports;
Rémi Duraffort's avatar
Rémi Duraffort committed
512 513

            free( pp_jack_port_output );
514 515
        }
    }
516

517
    p_sys->i_match_ports = i_total_out_ports;
518 519 520 521 522 523 524 525 526
}


/*****************************************************************************
 * Parse: Parse the MRL
 *****************************************************************************/
static void Parse( demux_t *p_demux )
{
    demux_sys_t *p_sys = p_demux->p_sys;
527
    char *psz_dup = strdup( p_demux->psz_location );
528 529 530 531 532 533 534
    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 ) );
    }
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
    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;
    }
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
    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 ) );
            }
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
            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;
            }
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
            else
            {
                msg_Warn( p_demux, "unknown option" );
            }
            while( *psz_parser && *psz_parser != ':' )
            {
                psz_parser++;
            }

            if( *psz_parser == '\0' )
            {
                break;
            }
        }
    }
600 601

    free( psz_dup );
602 603
}