jack.c 12.4 KB
Newer Older
1 2 3 4 5 6
/*****************************************************************************
 * jack : JACK audio output module
 *****************************************************************************
 * Copyright (C) 2006 the VideoLAN team
 * $Id$
 *
7
 * Authors: Cyril Deguet <asmax _at_ videolan.org>
8
 *          Jon Griffiths <jon_p_griffiths _At_ yahoo _DOT_ com>
9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (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
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
dionoea's avatar
dionoea committed
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23
 *****************************************************************************/
24 25 26 27
/**
 * \file modules/audio_output/jack.c
 * \brief JACK audio output functions
 */
28 29 30 31 32
/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <unistd.h>                                      /* write(), close() */

33 34 35 36
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

37
#include <vlc_common.h>
38
#include <vlc_plugin.h>
zorglub's avatar
zorglub committed
39
#include <vlc_aout.h>
40 41 42

#include <jack/jack.h>

43 44
typedef jack_default_audio_sample_t jack_sample_t;

45 46 47 48 49 50 51 52
/*****************************************************************************
 * aout_sys_t: JACK audio output method descriptor
 *****************************************************************************
 * This structure is part of the audio output thread descriptor.
 * It describes some JACK specific variables.
 *****************************************************************************/
struct aout_sys_t
{
53 54 55 56
    jack_client_t  *p_jack_client;
    jack_port_t   **p_jack_ports;
    jack_sample_t **p_jack_buffers;
    unsigned int    i_channels;
jpd's avatar
jpd committed
57
    jack_nframes_t latency;
58 59 60 61 62 63 64
};

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int  Open         ( vlc_object_t * );
static void Close        ( vlc_object_t * );
65
static void Play         ( audio_output_t * );
66
static int  Process      ( jack_nframes_t i_frames, void *p_arg );
jpd's avatar
jpd committed
67
static int  GraphChange  ( void *p_arg );
68

69
#define AUTO_CONNECT_OPTION "jack-auto-connect"
70
#define AUTO_CONNECT_TEXT N_("Automatically connect to writable clients")
71
#define AUTO_CONNECT_LONGTEXT N_( \
72 73
    "If enabled, this option will automatically connect sound output to the " \
    "first writable JACK clients found." )
74

75 76 77 78 79
#define CONNECT_REGEX_OPTION "jack-connect-regex"
#define CONNECT_REGEX_TEXT N_("Connect to clients matching")
#define CONNECT_REGEX_LONGTEXT N_( \
    "If automatic connection is enabled, only JACK clients whose names " \
    "match this regular expression will be considered for connection." )
80

81 82 83
/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
84 85 86 87 88 89
vlc_module_begin ()
    set_shortname( "JACK" )
    set_description( N_("JACK audio output") )
    set_capability( "audio output", 100 )
    set_category( CAT_AUDIO )
    set_subcategory( SUBCAT_AUDIO_AOUT )
90 91 92 93
    add_bool( AUTO_CONNECT_OPTION, true, AUTO_CONNECT_TEXT,
              AUTO_CONNECT_LONGTEXT, false )
    add_string( CONNECT_REGEX_OPTION, "system", CONNECT_REGEX_TEXT,
                CONNECT_REGEX_LONGTEXT, false )
94 95
    set_callbacks( Open, Close )
vlc_module_end ()
96 97 98 99 100 101

/*****************************************************************************
 * Open: create a JACK client
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
102
    char psz_name[32];
103
    audio_output_t *p_aout = (audio_output_t *)p_this;
104 105 106
    struct aout_sys_t *p_sys = NULL;
    int status = VLC_SUCCESS;
    unsigned int i;
107
    int i_error;
108 109

    /* Allocate structure */
110
    p_sys = calloc( 1, sizeof( aout_sys_t ) );
111 112
    if( p_sys == NULL )
    {
113 114
        status = VLC_ENOMEM;
        goto error_out;
115
    }
116
    p_aout->sys = p_sys;
jpd's avatar
jpd committed
117
    p_sys->latency = 0;
118 119

    /* Connect to the JACK server */
120 121
    snprintf( psz_name, sizeof(psz_name), "vlc_%d", getpid());
    psz_name[sizeof(psz_name) - 1] = '\0';
122 123 124
    p_sys->p_jack_client = jack_client_open( psz_name,
                                             JackNullOption | JackNoStartServer,
                                             NULL );
125 126
    if( p_sys->p_jack_client == NULL )
    {
127
        msg_Err( p_aout, "failed to connect to JACK server" );
128 129
        status = VLC_EGENERIC;
        goto error_out;
130 131 132 133
    }

    /* Set the process callback */
    jack_set_process_callback( p_sys->p_jack_client, Process, p_aout );
jpd's avatar
jpd committed
134
    jack_set_graph_order_callback ( p_sys->p_jack_client, GraphChange, p_aout );
135

136 137
    p_aout->pf_play = Play;
    p_aout->pf_pause = NULL;
138
    p_aout->pf_flush = NULL;
139 140
    aout_VolumeSoftInit( p_aout );

141
    /* JACK only supports fl32 format */
142
    p_aout->format.i_format = VLC_CODEC_FL32;
143
    // TODO add buffer size callback
144 145
    p_aout->i_nb_samples = jack_get_buffer_size( p_sys->p_jack_client );
    p_aout->format.i_rate = jack_get_sample_rate( p_sys->p_jack_client );
146

147
    p_sys->i_channels = aout_FormatNbChannels( &p_aout->format );
148

149 150
    p_sys->p_jack_ports = malloc( p_sys->i_channels *
                                  sizeof(jack_port_t *) );
151 152 153 154 155 156
    if( p_sys->p_jack_ports == NULL )
    {
        status = VLC_ENOMEM;
        goto error_out;
    }

157 158 159 160 161 162 163 164
    p_sys->p_jack_buffers = malloc( p_sys->i_channels *
                                    sizeof(jack_sample_t *) );
    if( p_sys->p_jack_buffers == NULL )
    {
        status = VLC_ENOMEM;
        goto error_out;
    }

165 166 167
    /* Create the output ports */
    for( i = 0; i < p_sys->i_channels; i++ )
    {
168 169
        snprintf( psz_name, sizeof(psz_name), "out_%d", i + 1);
        psz_name[sizeof(psz_name) - 1] = '\0';
170
        p_sys->p_jack_ports[i] = jack_port_register( p_sys->p_jack_client,
171
                psz_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0 );
172

173
        if( p_sys->p_jack_ports[i] == NULL )
174
        {
175
            msg_Err( p_aout, "failed to register a JACK port" );
176 177
            status = VLC_EGENERIC;
            goto error_out;
178 179 180 181
        }
    }

    /* Tell the JACK server we are ready */
182 183
    i_error = jack_activate( p_sys->p_jack_client );
    if( i_error )
184
    {
185
        msg_Err( p_aout, "failed to activate JACK client (error %d)", i_error );
186 187
        status = VLC_EGENERIC;
        goto error_out;
188 189
    }

190
    /* Auto connect ports if we were asked to */
191
    if( var_InheritBool( p_aout, AUTO_CONNECT_OPTION ) )
192
    {
193
        unsigned int i_in_ports;
194
        char *psz_regex = var_InheritString( p_aout, CONNECT_REGEX_OPTION );
195 196 197
        const char **pp_in_ports = jack_get_ports( p_sys->p_jack_client,
                                                   psz_regex, NULL,
                                                   JackPortIsInput );
198
        free( psz_regex );
199
        /* Count the number of returned ports */
200
        i_in_ports = 0;
201
        while( pp_in_ports && pp_in_ports[i_in_ports] )
202
        {
203
            i_in_ports++;
204
        }
205

206 207
        /* Tie the output ports to JACK input ports */
        for( i = 0; i_in_ports > 0 && i < p_sys->i_channels; i++ )
208
        {
209 210
            const char* psz_in = pp_in_ports[i % i_in_ports];
            const char* psz_out = jack_port_name( p_sys->p_jack_ports[i] );
211

212 213
            i_error = jack_connect( p_sys->p_jack_client, psz_out, psz_in );
            if( i_error )
214
            {
215 216
                msg_Err( p_aout, "failed to connect port %s to port %s (error %d)",
                         psz_out, psz_in, i_error );
217
            }
218
            else
219
            {
220 221
                msg_Dbg( p_aout, "connecting port %s to port %s",
                         psz_out, psz_in );
222 223
            }
        }
ivoire's avatar
ivoire committed
224
        free( pp_in_ports );
225 226 227 228
    }

    msg_Dbg( p_aout, "JACK audio output initialized (%d channels, buffer "
             "size=%d, rate=%d)", p_sys->i_channels,
229
             p_aout->i_nb_samples, p_aout->format.i_rate );
230

231
error_out:
232
    /* Clean up, if an error occurred */
233 234 235
    if( status != VLC_SUCCESS && p_sys != NULL)
    {
        if( p_sys->p_jack_client )
236 237
        {
            jack_deactivate( p_sys->p_jack_client );
238
            jack_client_close( p_sys->p_jack_client );
239
        }
ivoire's avatar
ivoire committed
240 241
        free( p_sys->p_jack_ports );
        free( p_sys->p_jack_buffers );
242 243 244
        free( p_sys );
    }
    return status;
245 246 247 248 249 250 251 252
}


/*****************************************************************************
 * Process: callback for JACK
 *****************************************************************************/
int Process( jack_nframes_t i_frames, void *p_arg )
{
253
    unsigned int i, j, i_nb_samples = 0;
254
    audio_output_t *p_aout = (audio_output_t*) p_arg;
255
    struct aout_sys_t *p_sys = p_aout->sys;
256
    jack_sample_t *p_src = NULL;
257

jpd's avatar
jpd committed
258 259 260
    jack_nframes_t dframes = p_sys->latency
                             - jack_frames_since_cycle_start( p_sys->p_jack_client );

jpd's avatar
jpd committed
261 262 263
    jack_time_t dtime = dframes * 1000 * 1000 / jack_get_sample_rate( p_sys->p_jack_client );
    mtime_t play_date = mdate() + (mtime_t) ( dtime );

264
    /* Get the next audio data buffer */
jpd's avatar
jpd committed
265
    aout_buffer_t *p_buffer = aout_OutputNextBuffer( p_aout, play_date, false );
jpd's avatar
jpd committed
266

267
    if( p_buffer != NULL )
268
    {
269
        p_src = (jack_sample_t *)p_buffer->p_buffer;
270 271
        i_nb_samples = p_buffer->i_nb_samples;
    }
272

273 274
    /* Get the JACK buffers to write to */
    for( i = 0; i < p_sys->i_channels; i++ )
275
    {
276 277 278
        p_sys->p_jack_buffers[i] = jack_port_get_buffer( p_sys->p_jack_ports[i],
                                                         i_frames );
    }
279

280 281 282 283
    /* Copy in the audio data */
    for( j = 0; j < i_nb_samples; j++ )
    {
        for( i = 0; i < p_sys->i_channels; i++ )
284
        {
285 286 287
            jack_sample_t *p_dst = p_sys->p_jack_buffers[i];
            p_dst[j] = *p_src;
            p_src++;
288
        }
289 290 291 292 293 294
    }

    /* Fill any remaining buffer with silence */
    if( i_nb_samples < i_frames )
    {
        for( i = 0; i < p_sys->i_channels; i++ )
295
        {
296 297
            memset( p_sys->p_jack_buffers[i] + i_nb_samples, 0,
                    sizeof( jack_sample_t ) * (i_frames - i_nb_samples) );
298
        }
299 300 301 302
    }

    if( p_buffer )
    {
303 304 305 306 307
        aout_BufferFree( p_buffer );
    }
    return 0;
}

jpd's avatar
jpd committed
308 309 310 311 312 313 314
/*****************************************************************************
 * GraphChange: callback when JACK reorders it's process graph.
                We update latency information.
 *****************************************************************************/

static int GraphChange( void *p_arg )
{
315
  audio_output_t *p_aout = (audio_output_t*) p_arg;
316
  struct aout_sys_t *p_sys = p_aout->sys;
jpd's avatar
jpd committed
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
  unsigned int i;
  jack_nframes_t port_latency;

  p_sys->latency = 0;

  for( i = 0; i < p_sys->i_channels; ++i )
  {
    port_latency = jack_port_get_total_latency( p_sys->p_jack_client,
                                                  p_sys->p_jack_ports[i] );
    p_sys->latency = __MAX( p_sys->latency, port_latency );
  }

  msg_Dbg(p_aout, "JACK graph reordered. Our maximum latency=%d.", p_sys->latency);

  return 0;
}
333 334 335 336

/*****************************************************************************
 * Play: nothing to do
 *****************************************************************************/
337
static void Play( audio_output_t *p_aout )
338
{
jpd's avatar
jpd committed
339
    VLC_UNUSED( p_aout );
340 341 342 343 344 345 346
}

/*****************************************************************************
 * Close: close the JACK client
 *****************************************************************************/
static void Close( vlc_object_t *p_this )
{
347
    int i_error;
348
    audio_output_t *p_aout = (audio_output_t *)p_this;
349
    struct aout_sys_t *p_sys = p_aout->sys;
350

351 352 353 354 355 356 357 358 359 360 361
    i_error = jack_deactivate( p_sys->p_jack_client );
    if( i_error )
    {
        msg_Err( p_aout, "jack_deactivate failed (error %d)", i_error );
    }

    i_error = jack_client_close( p_sys->p_jack_client );
    if( i_error )
    {
        msg_Err( p_aout, "jack_client_close failed (error %d)", i_error );
    }
362
    free( p_sys->p_jack_ports );
363
    free( p_sys->p_jack_buffers );
364 365
    free( p_sys );
}