X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=modules%2Faudio_output%2Fjack.c;h=f2330d6f2676d744ae70ea083928cf71081090dc;hb=d38e44dd960e58e50f79cf1b6793d699789391f6;hp=9e006cbae3ea484d0b5ab16d3601dd8538e7ee97;hpb=d35651d0275f5effd41124469d5521f4c90999ff;p=vlc diff --git a/modules/audio_output/jack.c b/modules/audio_output/jack.c index 9e006cbae3..f2330d6f26 100644 --- a/modules/audio_output/jack.c +++ b/modules/audio_output/jack.c @@ -1,25 +1,25 @@ /***************************************************************************** - * jack : JACK audio output module + * jack.c : JACK audio output module ***************************************************************************** - * Copyright (C) 2006 the VideoLAN team + * Copyright (C) 2006 VLC authors and VideoLAN * $Id$ * - * Authors: Cyril Deguet + * Authors: Cyril Deguet * Jon Griffiths * - * 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 + * 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 * (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. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser 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 - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. + * 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. *****************************************************************************/ /** * \file modules/audio_output/jack.c @@ -28,14 +28,20 @@ /***************************************************************************** * Preamble *****************************************************************************/ -#include /* strerror() */ #include /* write(), close() */ -#include /* calloc(), malloc(), free() */ -#include +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include +#include #include #include +#include + +typedef jack_default_audio_sample_t jack_sample_t; /***************************************************************************** * aout_sys_t: JACK audio output method descriptor @@ -45,9 +51,16 @@ *****************************************************************************/ struct aout_sys_t { - jack_client_t *p_jack_client; - jack_port_t **p_jack_ports; - unsigned int i_channels; + jack_ringbuffer_t *p_jack_ringbuffer; + jack_client_t *p_jack_client; + jack_port_t **p_jack_ports; + jack_sample_t **p_jack_buffers; + unsigned int i_channels; + unsigned int i_rate; + jack_nframes_t latency; + float soft_gain; + bool soft_mute; + mtime_t paused; /**< Time when (last) paused */ }; /***************************************************************************** @@ -55,61 +68,62 @@ struct aout_sys_t *****************************************************************************/ static int Open ( vlc_object_t * ); static void Close ( vlc_object_t * ); -static void Play ( aout_instance_t * ); -static int Process ( jack_nframes_t i_frames, void *p_arg ); +static void Play ( audio_output_t * p_aout, block_t * p_block ); +static void Pause ( audio_output_t *aout, bool paused, mtime_t date ); +static void Flush ( audio_output_t *p_aout, bool wait ); +static int TimeGet ( audio_output_t *, mtime_t * ); +static int Process ( jack_nframes_t i_frames, void *p_arg ); +static int GraphChange ( void *p_arg ); + +#include "audio_output/volume.h" #define AUTO_CONNECT_OPTION "jack-auto-connect" -#define AUTO_CONNECT_TEXT N_("Automatically connect to input devices") +#define AUTO_CONNECT_TEXT N_("Automatically connect to writable clients") #define AUTO_CONNECT_LONGTEXT N_( \ - "If enabled, this option will automatically connect output to the " \ - "first JACK inputs found." ) + "If enabled, this option will automatically connect sound output to the " \ + "first writable JACK clients found." ) -#define CONNECT_MATCH_OPTION "jack-connect-match" -#define CONNECT_MATCH_TEXT N_("Connect to outputs beginning with") -#define CONNECT_MATCH_LONGTEXT N_( \ - "If automatic connection is enabled, only JACK inputs whose names " \ - "begin with this prefix will be considered for connection." ) +#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." ) /***************************************************************************** * Module descriptor *****************************************************************************/ -vlc_module_begin(); - set_shortname( "JACK" ); - set_description( _("JACK audio output") ); - set_capability( "audio output", 100 ); - set_category( CAT_AUDIO ); - set_subcategory( SUBCAT_AUDIO_AOUT ); - add_bool( AUTO_CONNECT_OPTION, 0, NULL, AUTO_CONNECT_TEXT, - AUTO_CONNECT_LONGTEXT, VLC_TRUE ); - add_string( CONNECT_MATCH_OPTION, NULL, NULL, CONNECT_MATCH_TEXT, - CONNECT_MATCH_LONGTEXT, VLC_TRUE ); - set_callbacks( Open, Close ); -vlc_module_end(); - -/***************************************************************************** - * Open: create a JACK client - *****************************************************************************/ -static int Open( vlc_object_t *p_this ) +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 ) + 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 ) + add_sw_gain( ) + set_callbacks( Open, Close ) +vlc_module_end () + + +static int Start( audio_output_t *p_aout, audio_sample_format_t *restrict fmt ) { - aout_instance_t *p_aout = (aout_instance_t *)p_this; - struct aout_sys_t *p_sys = NULL; - char **pp_match_ports = NULL; - char *psz_prefix = NULL; + char psz_name[32]; + struct aout_sys_t *p_sys = p_aout->sys; int status = VLC_SUCCESS; unsigned int i; + int i_error; - /* Allocate structure */ - p_sys = malloc( sizeof( aout_sys_t ) ); - if( p_sys == NULL ) - { - msg_Err( p_aout, "out of memory" ); - status = VLC_ENOMEM; - goto error_out; - } - p_aout->output.p_sys = p_sys; + p_sys->latency = 0; + p_sys->paused = VLC_TS_INVALID; /* Connect to the JACK server */ - p_sys->p_jack_client = jack_client_new( "vlc" ); + snprintf( psz_name, sizeof(psz_name), "vlc_%d", getpid()); + psz_name[sizeof(psz_name) - 1] = '\0'; + p_sys->p_jack_client = jack_client_open( psz_name, + JackNullOption | JackNoStartServer, + NULL ); if( p_sys->p_jack_client == NULL ) { msg_Err( p_aout, "failed to connect to JACK server" ); @@ -119,33 +133,60 @@ static int Open( vlc_object_t *p_this ) /* Set the process callback */ jack_set_process_callback( p_sys->p_jack_client, Process, p_aout ); - - p_aout->output.pf_play = Play; - aout_VolumeSoftInit( p_aout ); + jack_set_graph_order_callback ( p_sys->p_jack_client, GraphChange, p_aout ); /* JACK only supports fl32 format */ - p_aout->output.output.i_format = VLC_FOURCC('f','l','3','2'); + fmt->i_format = VLC_CODEC_FL32; // TODO add buffer size callback - p_aout->output.i_nb_samples = jack_get_buffer_size( p_sys->p_jack_client ); - p_aout->output.output.i_rate = jack_get_sample_rate( p_sys->p_jack_client ); + p_sys->i_rate = fmt->i_rate = jack_get_sample_rate( p_sys->p_jack_client ); - p_sys->i_channels = aout_FormatNbChannels( &p_aout->output.output ); + p_aout->play = Play; + p_aout->pause = Pause; + p_aout->flush = Flush; + p_aout->time_get = TimeGet; + aout_SoftVolumeStart( p_aout ); - p_sys->p_jack_ports = malloc( p_sys->i_channels * sizeof(jack_port_t *) ); + p_sys->i_channels = aout_FormatNbChannels( fmt ); + aout_FormatPrepare(fmt); + + p_sys->p_jack_ports = malloc( p_sys->i_channels * + sizeof(jack_port_t *) ); if( p_sys->p_jack_ports == NULL ) { - msg_Err( p_aout, "out of memory" ); status = VLC_ENOMEM; goto error_out; } + 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; + } + + const size_t buf_sz = AOUT_MAX_ADVANCE_TIME * fmt->i_rate * + fmt->i_bytes_per_frame / CLOCK_FREQ; + p_sys->p_jack_ringbuffer = jack_ringbuffer_create( buf_sz ); + + if( p_sys->p_jack_ringbuffer == NULL ) + { + status = VLC_ENOMEM; + goto error_out; + } + + if( jack_ringbuffer_mlock( p_sys->p_jack_ringbuffer )) + { + msg_Warn( p_aout, "failed to lock JACK ringbuffer in memory" ); + } + /* Create the output ports */ for( i = 0; i < p_sys->i_channels; i++ ) { - char p_name[32]; - snprintf( p_name, 32, "out_%d", i + 1); + snprintf( psz_name, sizeof(psz_name), "out_%d", i + 1); + psz_name[sizeof(psz_name) - 1] = '\0'; p_sys->p_jack_ports[i] = jack_port_register( p_sys->p_jack_client, - p_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0 ); + psz_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0 ); if( p_sys->p_jack_ports[i] == NULL ) { @@ -156,172 +197,264 @@ static int Open( vlc_object_t *p_this ) } /* Tell the JACK server we are ready */ - if( jack_activate( p_sys->p_jack_client ) ) + i_error = jack_activate( p_sys->p_jack_client ); + if( i_error ) { - msg_Err( p_aout, "failed to activate JACK client" ); - jack_client_close( p_sys->p_jack_client ); + msg_Err( p_aout, "failed to activate JACK client (error %d)", i_error ); status = VLC_EGENERIC; goto error_out; } /* Auto connect ports if we were asked to */ - if( config_GetInt( p_aout, AUTO_CONNECT_OPTION ) ) + if( var_InheritBool( p_aout, AUTO_CONNECT_OPTION ) ) { - unsigned int i_in_ports, i_prefix_len; - const char **pp_in_ports; - - pp_in_ports = jack_get_ports( p_sys->p_jack_client, NULL, NULL, - JackPortIsInput ); - psz_prefix = config_GetPsz( p_aout, CONNECT_MATCH_OPTION ); - i_prefix_len = psz_prefix ? strlen(psz_prefix) : 0; - - /* Find JACK input ports to connect to */ - i = 0; + unsigned int i_in_ports; + char *psz_regex = var_InheritString( p_aout, CONNECT_REGEX_OPTION ); + const char **pp_in_ports = jack_get_ports( p_sys->p_jack_client, + psz_regex, NULL, + JackPortIsInput ); + free( psz_regex ); + /* Count the number of returned ports */ i_in_ports = 0; - while( pp_in_ports && pp_in_ports[i] ) + while( pp_in_ports && pp_in_ports[i_in_ports] ) { - if( !psz_prefix || - !strncmp(psz_prefix, pp_in_ports[i], i_prefix_len) ) - { - i_in_ports++; /* Found one */ - } - i++; + i_in_ports++; } - /* Connect the output ports to input ports */ - if( i_in_ports > 0 ) + /* Tie the output ports to JACK input ports */ + for( i = 0; i_in_ports > 0 && i < p_sys->i_channels; i++ ) { - pp_match_ports = malloc( i_in_ports * sizeof(char*) ); - if( pp_match_ports == NULL ) - { - msg_Err( p_aout, "out of memory" ); - status = VLC_ENOMEM; - goto error_out; - } + const char* psz_in = pp_in_ports[i % i_in_ports]; + const char* psz_out = jack_port_name( p_sys->p_jack_ports[i] ); - /* populate list of matching ports */ - i = 0; - i_in_ports = 0; - while( pp_in_ports[i] ) + i_error = jack_connect( p_sys->p_jack_client, psz_out, psz_in ); + if( i_error ) { - if( !psz_prefix || - !strncmp(psz_prefix, pp_in_ports[i], i_prefix_len) ) - { - pp_match_ports[i_in_ports] = pp_in_ports[i]; - i_in_ports++; /* Found one */ - } - i++; + msg_Err( p_aout, "failed to connect port %s to port %s (error %d)", + psz_out, psz_in, i_error ); } - - /* Tie the output ports to JACK input ports */ - for( i = 0; i < p_sys->i_channels; i++ ) + else { - const char* psz_in = pp_match_ports[i % i_in_ports]; - const char* psz_out = jack_port_name( p_sys->p_jack_ports[i] ); - - if( jack_connect( p_sys->p_jack_client, psz_out, psz_in) ) - { - msg_Err( p_aout, "failed to connect port %s to port %s", - psz_out, psz_in ); - } - else - { - msg_Dbg( p_aout, "connecting port %s to port %s", - psz_out, psz_in ); - } + msg_Dbg( p_aout, "connecting port %s to port %s", + psz_out, psz_in ); } } + free( pp_in_ports ); } - msg_Dbg( p_aout, "JACK audio output initialized (%d channels, buffer " - "size=%d, rate=%d)", p_sys->i_channels, - p_aout->output.i_nb_samples, p_aout->output.output.i_rate ); + msg_Dbg( p_aout, "JACK audio output initialized (%d channels, rate=%d)", + p_sys->i_channels, fmt->i_rate ); error_out: - /* Clean up */ - if( psz_prefix ) - free( psz_prefix ); - - if( pp_match_ports ) - free( pp_match_ports ); - - if( status != VLC_SUCCESS && p_sys != NULL) + /* Clean up, if an error occurred */ + if( status != VLC_SUCCESS ) { - if( p_sys->p_jack_ports ) - free( p_sys->p_jack_ports ); if( p_sys->p_jack_client ) + { + jack_deactivate( p_sys->p_jack_client ); jack_client_close( p_sys->p_jack_client ); - free( p_sys ); + } + if( p_sys->p_jack_ringbuffer ) + jack_ringbuffer_free( p_sys->p_jack_ringbuffer ); + + free( p_sys->p_jack_ports ); + free( p_sys->p_jack_buffers ); } return status; } +static void Play (audio_output_t * p_aout, block_t * p_block) +{ + struct aout_sys_t *p_sys = p_aout->sys; + jack_ringbuffer_t *rb = p_sys->p_jack_ringbuffer; + const size_t bytes_per_frame = p_sys->i_channels * sizeof(jack_sample_t); + + while (p_block->i_buffer > 0) { + + /* move data to buffer */ + const size_t write_space = jack_ringbuffer_write_space(rb); + const size_t bytes = p_block->i_buffer < write_space ? + p_block->i_buffer : write_space; + + /* If our audio thread is not reading fast enough */ + if( unlikely( bytes == 0 ) ) { + msg_Warn( p_aout, "%"PRIuPTR " frames of audio dropped", + p_block->i_buffer / bytes_per_frame ); + break; + } + + jack_ringbuffer_write( rb, (const char *) p_block->p_buffer, bytes ); + + p_block->p_buffer += bytes; + p_block->i_buffer -= bytes; + } + + block_Release(p_block); +} + +/** + * Pause or unpause playback + */ +static void Pause(audio_output_t *aout, bool paused, mtime_t date) +{ + aout_sys_t *sys = aout->sys; + + if( paused ) { + sys->paused = date; + } else { + date -= sys->paused; + msg_Dbg(aout, "resuming after %"PRId64" us", date); + sys->paused = VLC_TS_INVALID; + } +} + +static void Flush(audio_output_t *p_aout, bool wait) +{ + struct aout_sys_t * p_sys = p_aout->sys; + jack_ringbuffer_t *rb = p_sys->p_jack_ringbuffer; + + /* Sleep if wait was requested */ + if( wait ) + { + mtime_t delay; + if (!TimeGet(p_aout, &delay)) + msleep(delay); + } + + /* reset ringbuffer read and write pointers */ + jack_ringbuffer_reset(rb); +} + +static int TimeGet(audio_output_t *p_aout, mtime_t *delay) +{ + struct aout_sys_t * p_sys = p_aout->sys; + jack_ringbuffer_t *rb = p_sys->p_jack_ringbuffer; + const size_t bytes_per_frame = p_sys->i_channels * sizeof(jack_sample_t); + + *delay = (p_sys->latency + + (jack_ringbuffer_read_space(rb) / bytes_per_frame)) * + CLOCK_FREQ / p_sys->i_rate; + + return 0; +} /***************************************************************************** * Process: callback for JACK *****************************************************************************/ int Process( jack_nframes_t i_frames, void *p_arg ) { - aout_buffer_t *p_buffer; - jack_default_audio_sample_t *p_jack_buffer; - unsigned int i, j, i_nb_samples = 0; - aout_instance_t *p_aout = (aout_instance_t*) p_arg; - unsigned int i_nb_channels = p_aout->output.p_sys->i_channels; + unsigned int i, j, frames_from_rb = 0; + size_t bytes_read = 0; + size_t frames_read; + audio_output_t *p_aout = (audio_output_t*) p_arg; + struct aout_sys_t *p_sys = p_aout->sys; - /* Get the next audio data buffer */ - p_buffer = aout_FifoPop( p_aout, &p_aout->output.fifo ); + /* Get the next audio data buffer unless paused */ - if( p_buffer ) + if( p_sys->paused == VLC_TS_INVALID ) + frames_from_rb = i_frames; + + /* Get the JACK buffers to write to */ + for( i = 0; i < p_sys->i_channels; i++ ) { - i_nb_samples = p_buffer->i_nb_samples; + p_sys->p_jack_buffers[i] = jack_port_get_buffer( p_sys->p_jack_ports[i], + i_frames ); } - for( i = 0; i < i_nb_channels; i++ ) + /* Copy in the audio data */ + for( j = 0; j < frames_from_rb; j++ ) { - /* Get an output buffer from JACK */ - p_jack_buffer = jack_port_get_buffer( - p_aout->output.p_sys->p_jack_ports[i], i_frames ); - - /* Fill the buffer with audio data */ - for( j = 0; j < i_nb_samples; j++ ) - { - p_jack_buffer[j] = ((float*)p_buffer->p_buffer)[i_nb_channels*j+i]; - } - if( i_nb_samples < i_frames ) + for( i = 0; i < p_sys->i_channels; i++ ) { - memset( p_jack_buffer + i_nb_samples, 0, - sizeof( jack_default_audio_sample_t ) * - (i_frames - i_nb_samples) ); + jack_sample_t *p_dst = p_sys->p_jack_buffers[i] + j; + bytes_read += jack_ringbuffer_read( p_sys->p_jack_ringbuffer, + (char *) p_dst, sizeof(jack_sample_t) ); } } - if( p_buffer ) + /* Fill any remaining buffer with silence */ + frames_read = (bytes_read / sizeof(jack_sample_t)) / p_sys->i_channels; + if( frames_read < i_frames ) { - aout_BufferFree( p_buffer ); + for( i = 0; i < p_sys->i_channels; i++ ) + { + memset( p_sys->p_jack_buffers[i] + frames_read, 0, + sizeof( jack_sample_t ) * (i_frames - frames_read) ); + } } return 0; } - /***************************************************************************** - * Play: nothing to do + * GraphChange: callback when JACK reorders it's process graph. + We update latency information. *****************************************************************************/ -static void Play( aout_instance_t *p_aout ) + +static int GraphChange( void *p_arg ) { - aout_FifoFirstDate( p_aout, &p_aout->output.fifo ); + audio_output_t *p_aout = (audio_output_t*) p_arg; + struct aout_sys_t *p_sys = p_aout->sys; + unsigned int i; + jack_latency_range_t port_latency; + + p_sys->latency = 0; + + for( i = 0; i < p_sys->i_channels; ++i ) + { + jack_port_get_latency_range( p_sys->p_jack_ports[i], JackPlaybackLatency, + &port_latency ); + p_sys->latency = __MAX( p_sys->latency, port_latency.max ); + } + + msg_Dbg(p_aout, "JACK graph reordered. Our maximum latency=%d.", + p_sys->latency); + + return 0; } /***************************************************************************** * Close: close the JACK client *****************************************************************************/ -static void Close( vlc_object_t *p_this ) +static void Stop( audio_output_t *p_aout ) { - aout_instance_t *p_aout = (aout_instance_t *)p_this; - struct aout_sys_t *p_sys = p_aout->output.p_sys; + int i_error; + struct aout_sys_t *p_sys = p_aout->sys; + 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 ); + } free( p_sys->p_jack_ports ); - jack_client_close( p_sys->p_jack_client ); - free( p_sys ); + free( p_sys->p_jack_buffers ); + jack_ringbuffer_free( p_sys->p_jack_ringbuffer ); } +static int Open(vlc_object_t *obj) +{ + audio_output_t *aout = (audio_output_t *)obj; + aout_sys_t *sys = calloc(1, sizeof (*sys)); + + if( unlikely( sys == NULL ) ) + return VLC_ENOMEM; + aout->sys = sys; + aout->start = Start; + aout->stop = Stop; + aout_SoftVolumeInit(aout); + return VLC_SUCCESS; +} + +static void Close(vlc_object_t *obj) +{ + audio_output_t *aout = (audio_output_t *)obj; + aout_sys_t *sys = aout->sys; + + free(sys); +}