jack.c 12.2 KB
Newer Older
1
/*****************************************************************************
Pere Orga's avatar
Pere Orga committed
2
 * jack.c : JACK audio output module
3 4 5 6
 *****************************************************************************
 * 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
Antoine Cellerier's avatar
Antoine Cellerier 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>
Clément Stenac's avatar
Clément Stenac 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
    aout_packet_t   packet;
54 55 56 57
    jack_client_t  *p_jack_client;
    jack_port_t   **p_jack_ports;
    jack_sample_t **p_jack_buffers;
    unsigned int    i_channels;
58
    jack_nframes_t latency;
59 60
    float soft_gain;
    bool soft_mute;
61 62 63 64 65 66 67
};

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int  Open         ( vlc_object_t * );
static void Close        ( vlc_object_t * );
68
static int  Process      ( jack_nframes_t i_frames, void *p_arg );
69
static int  GraphChange  ( void *p_arg );
70

71 72
#include "volume.h"

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

79 80 81 82 83
#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." )
84

85 86 87
/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
88 89 90 91 92 93
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 )
94 95 96 97
    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 )
98
    add_sw_gain( )
99 100
    set_callbacks( Open, Close )
vlc_module_end ()
101 102 103 104 105 106

/*****************************************************************************
 * Open: create a JACK client
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
107
    char psz_name[32];
108
    audio_output_t *p_aout = (audio_output_t *)p_this;
109 110 111
    struct aout_sys_t *p_sys = NULL;
    int status = VLC_SUCCESS;
    unsigned int i;
112
    int i_error;
113 114

    /* Allocate structure */
115
    p_sys = calloc( 1, sizeof( aout_sys_t ) );
116 117
    if( p_sys == NULL )
    {
118 119
        status = VLC_ENOMEM;
        goto error_out;
120
    }
121
    p_aout->sys = p_sys;
122
    p_sys->latency = 0;
123 124

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

    /* Set the process callback */
    jack_set_process_callback( p_sys->p_jack_client, Process, p_aout );
139
    jack_set_graph_order_callback ( p_sys->p_jack_client, GraphChange, p_aout );
140

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

146 147 148
    p_aout->pf_play = aout_PacketPlay;
    p_aout->pf_pause = aout_PacketPause;
    p_aout->pf_flush = aout_PacketFlush;
149 150
    aout_PacketInit( p_aout, &p_sys->packet,
                     jack_get_buffer_size( p_sys->p_jack_client ) );
151
    aout_SoftVolumeInit( p_aout );
152

153
    p_sys->i_channels = aout_FormatNbChannels( &p_aout->format );
154

155 156
    p_sys->p_jack_ports = malloc( p_sys->i_channels *
                                  sizeof(jack_port_t *) );
157 158 159 160 161 162
    if( p_sys->p_jack_ports == NULL )
    {
        status = VLC_ENOMEM;
        goto error_out;
    }

163 164 165 166 167 168 169 170
    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;
    }

171 172 173
    /* Create the output ports */
    for( i = 0; i < p_sys->i_channels; i++ )
    {
174 175
        snprintf( psz_name, sizeof(psz_name), "out_%d", i + 1);
        psz_name[sizeof(psz_name) - 1] = '\0';
176
        p_sys->p_jack_ports[i] = jack_port_register( p_sys->p_jack_client,
177
                psz_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0 );
178

179
        if( p_sys->p_jack_ports[i] == NULL )
180
        {
181
            msg_Err( p_aout, "failed to register a JACK port" );
182 183
            status = VLC_EGENERIC;
            goto error_out;
184 185 186 187
        }
    }

    /* Tell the JACK server we are ready */
188 189
    i_error = jack_activate( p_sys->p_jack_client );
    if( i_error )
190
    {
191
        msg_Err( p_aout, "failed to activate JACK client (error %d)", i_error );
192 193
        status = VLC_EGENERIC;
        goto error_out;
194 195
    }

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

212 213
        /* Tie the output ports to JACK input ports */
        for( i = 0; i_in_ports > 0 && i < p_sys->i_channels; i++ )
214
        {
215 216
            const char* psz_in = pp_in_ports[i % i_in_ports];
            const char* psz_out = jack_port_name( p_sys->p_jack_ports[i] );
217

218 219
            i_error = jack_connect( p_sys->p_jack_client, psz_out, psz_in );
            if( i_error )
220
            {
221 222
                msg_Err( p_aout, "failed to connect port %s to port %s (error %d)",
                         psz_out, psz_in, i_error );
223
            }
224
            else
225
            {
226 227
                msg_Dbg( p_aout, "connecting port %s to port %s",
                         psz_out, psz_in );
228 229
            }
        }
230
        free( pp_in_ports );
231 232
    }

233 234
    msg_Dbg( p_aout, "JACK audio output initialized (%d channels, rate=%d)",
             p_sys->i_channels, p_aout->format.i_rate );
235

236
error_out:
237
    /* Clean up, if an error occurred */
238 239 240
    if( status != VLC_SUCCESS && p_sys != NULL)
    {
        if( p_sys->p_jack_client )
241 242
        {
            jack_deactivate( p_sys->p_jack_client );
243
            jack_client_close( p_sys->p_jack_client );
244
            aout_PacketDestroy( p_aout );
245
        }
246 247
        free( p_sys->p_jack_ports );
        free( p_sys->p_jack_buffers );
248 249 250
        free( p_sys );
    }
    return status;
251 252 253 254 255 256 257 258
}


/*****************************************************************************
 * Process: callback for JACK
 *****************************************************************************/
int Process( jack_nframes_t i_frames, void *p_arg )
{
259
    unsigned int i, j, i_nb_samples = 0;
260
    audio_output_t *p_aout = (audio_output_t*) p_arg;
261
    struct aout_sys_t *p_sys = p_aout->sys;
262
    jack_sample_t *p_src = NULL;
263

264 265 266
    jack_nframes_t dframes = p_sys->latency
                             - jack_frames_since_cycle_start( p_sys->p_jack_client );

267 268 269
    jack_time_t dtime = dframes * 1000 * 1000 / jack_get_sample_rate( p_sys->p_jack_client );
    mtime_t play_date = mdate() + (mtime_t) ( dtime );

270
    /* Get the next audio data buffer */
271
    block_t *p_buffer = aout_PacketNext( p_aout, play_date );
Jakob Leben's avatar
Jakob Leben committed
272

273
    if( p_buffer != NULL )
274
    {
275
        p_src = (jack_sample_t *)p_buffer->p_buffer;
276 277
        i_nb_samples = p_buffer->i_nb_samples;
    }
278

279 280
    /* Get the JACK buffers to write to */
    for( i = 0; i < p_sys->i_channels; i++ )
281
    {
282 283 284
        p_sys->p_jack_buffers[i] = jack_port_get_buffer( p_sys->p_jack_ports[i],
                                                         i_frames );
    }
285

286 287 288 289
    /* Copy in the audio data */
    for( j = 0; j < i_nb_samples; j++ )
    {
        for( i = 0; i < p_sys->i_channels; i++ )
290
        {
291 292 293
            jack_sample_t *p_dst = p_sys->p_jack_buffers[i];
            p_dst[j] = *p_src;
            p_src++;
294
        }
295 296 297 298 299 300
    }

    /* Fill any remaining buffer with silence */
    if( i_nb_samples < i_frames )
    {
        for( i = 0; i < p_sys->i_channels; i++ )
301
        {
302 303
            memset( p_sys->p_jack_buffers[i] + i_nb_samples, 0,
                    sizeof( jack_sample_t ) * (i_frames - i_nb_samples) );
304
        }
305 306 307
    }

    if( p_buffer )
308
        block_Release( p_buffer );
309 310 311
    return 0;
}

312 313 314 315 316 317 318
/*****************************************************************************
 * GraphChange: callback when JACK reorders it's process graph.
                We update latency information.
 *****************************************************************************/

static int GraphChange( void *p_arg )
{
319
  audio_output_t *p_aout = (audio_output_t*) p_arg;
320
  struct aout_sys_t *p_sys = p_aout->sys;
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
  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;
}
337 338 339 340 341 342

/*****************************************************************************
 * Close: close the JACK client
 *****************************************************************************/
static void Close( vlc_object_t *p_this )
{
343
    int i_error;
344
    audio_output_t *p_aout = (audio_output_t *)p_this;
345
    struct aout_sys_t *p_sys = p_aout->sys;
346

347 348 349 350 351 352 353 354 355 356 357
    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 );
    }
358
    free( p_sys->p_jack_ports );
359
    free( p_sys->p_jack_buffers );
360
    aout_PacketDestroy( p_aout );
361 362
    free( p_sys );
}