| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
#include <unistd.h> |
|---|
| 32 |
|
|---|
| 33 |
#ifdef HAVE_CONFIG_H |
|---|
| 34 |
# include "config.h" |
|---|
| 35 |
#endif |
|---|
| 36 |
|
|---|
| 37 |
#include <vlc_common.h> |
|---|
| 38 |
#include <vlc_plugin.h> |
|---|
| 39 |
#include <vlc_aout.h> |
|---|
| 40 |
|
|---|
| 41 |
#include <jack/jack.h> |
|---|
| 42 |
|
|---|
| 43 |
typedef jack_default_audio_sample_t jack_sample_t; |
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
struct aout_sys_t |
|---|
| 52 |
{ |
|---|
| 53 |
jack_client_t *p_jack_client; |
|---|
| 54 |
jack_port_t **p_jack_ports; |
|---|
| 55 |
jack_sample_t **p_jack_buffers; |
|---|
| 56 |
unsigned int i_channels; |
|---|
| 57 |
}; |
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
static int Open ( vlc_object_t * ); |
|---|
| 63 |
static void Close ( vlc_object_t * ); |
|---|
| 64 |
static void Play ( aout_instance_t * ); |
|---|
| 65 |
static int Process ( jack_nframes_t i_frames, void *p_arg ); |
|---|
| 66 |
|
|---|
| 67 |
#define AUTO_CONNECT_OPTION "jack-auto-connect" |
|---|
| 68 |
#define AUTO_CONNECT_TEXT N_("Automatically connect to writable clients") |
|---|
| 69 |
#define AUTO_CONNECT_LONGTEXT N_( \ |
|---|
| 70 |
"If enabled, this option will automatically connect sound output to the " \ |
|---|
| 71 |
"first writable JACK clients found." ) |
|---|
| 72 |
|
|---|
| 73 |
#define CONNECT_REGEX_OPTION "jack-connect-regex" |
|---|
| 74 |
#define CONNECT_REGEX_TEXT N_("Connect to clients matching") |
|---|
| 75 |
#define CONNECT_REGEX_LONGTEXT N_( \ |
|---|
| 76 |
"If automatic connection is enabled, only JACK clients whose names " \ |
|---|
| 77 |
"match this regular expression will be considered for connection." ) |
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
vlc_module_begin(); |
|---|
| 83 |
set_shortname( "JACK" ); |
|---|
| 84 |
set_description( N_("JACK audio output") ); |
|---|
| 85 |
set_capability( "audio output", 100 ); |
|---|
| 86 |
set_category( CAT_AUDIO ); |
|---|
| 87 |
set_subcategory( SUBCAT_AUDIO_AOUT ); |
|---|
| 88 |
add_bool( AUTO_CONNECT_OPTION, 0, NULL, AUTO_CONNECT_TEXT, |
|---|
| 89 |
AUTO_CONNECT_LONGTEXT, true ); |
|---|
| 90 |
add_string( CONNECT_REGEX_OPTION, NULL, NULL, CONNECT_REGEX_TEXT, |
|---|
| 91 |
CONNECT_REGEX_LONGTEXT, true ); |
|---|
| 92 |
set_callbacks( Open, Close ); |
|---|
| 93 |
vlc_module_end(); |
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 |
static int Open( vlc_object_t *p_this ) |
|---|
| 99 |
{ |
|---|
| 100 |
char psz_name[32]; |
|---|
| 101 |
aout_instance_t *p_aout = (aout_instance_t *)p_this; |
|---|
| 102 |
struct aout_sys_t *p_sys = NULL; |
|---|
| 103 |
int status = VLC_SUCCESS; |
|---|
| 104 |
unsigned int i; |
|---|
| 105 |
int i_error; |
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 |
p_sys = calloc( 1, sizeof( aout_sys_t ) ); |
|---|
| 109 |
if( p_sys == NULL ) |
|---|
| 110 |
{ |
|---|
| 111 |
status = VLC_ENOMEM; |
|---|
| 112 |
goto error_out; |
|---|
| 113 |
} |
|---|
| 114 |
p_aout->output.p_sys = p_sys; |
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 |
snprintf( psz_name, sizeof(psz_name), "vlc_%d", getpid()); |
|---|
| 118 |
psz_name[sizeof(psz_name) - 1] = '\0'; |
|---|
| 119 |
p_sys->p_jack_client = jack_client_new( psz_name ); |
|---|
| 120 |
if( p_sys->p_jack_client == NULL ) |
|---|
| 121 |
{ |
|---|
| 122 |
msg_Err( p_aout, "failed to connect to JACK server" ); |
|---|
| 123 |
status = VLC_EGENERIC; |
|---|
| 124 |
goto error_out; |
|---|
| 125 |
} |
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 |
jack_set_process_callback( p_sys->p_jack_client, Process, p_aout ); |
|---|
| 129 |
|
|---|
| 130 |
p_aout->output.pf_play = Play; |
|---|
| 131 |
aout_VolumeSoftInit( p_aout ); |
|---|
| 132 |
|
|---|
| 133 |
|
|---|
| 134 |
p_aout->output.output.i_format = VLC_FOURCC('f','l','3','2'); |
|---|
| 135 |
|
|---|
| 136 |
p_aout->output.i_nb_samples = jack_get_buffer_size( p_sys->p_jack_client ); |
|---|
| 137 |
p_aout->output.output.i_rate = jack_get_sample_rate( p_sys->p_jack_client ); |
|---|
| 138 |
|
|---|
| 139 |
p_sys->i_channels = aout_FormatNbChannels( &p_aout->output.output ); |
|---|
| 140 |
|
|---|
| 141 |
p_sys->p_jack_ports = malloc( p_sys->i_channels * |
|---|
| 142 |
sizeof(jack_port_t *) ); |
|---|
| 143 |
if( p_sys->p_jack_ports == NULL ) |
|---|
| 144 |
{ |
|---|
| 145 |
status = VLC_ENOMEM; |
|---|
| 146 |
goto error_out; |
|---|
| 147 |
} |
|---|
| 148 |
|
|---|
| 149 |
p_sys->p_jack_buffers = malloc( p_sys->i_channels * |
|---|
| 150 |
sizeof(jack_sample_t *) ); |
|---|
| 151 |
if( p_sys->p_jack_buffers == NULL ) |
|---|
| 152 |
{ |
|---|
| 153 |
status = VLC_ENOMEM; |
|---|
| 154 |
goto error_out; |
|---|
| 155 |
} |
|---|
| 156 |
|
|---|
| 157 |
|
|---|
| 158 |
for( i = 0; i < p_sys->i_channels; i++ ) |
|---|
| 159 |
{ |
|---|
| 160 |
snprintf( psz_name, sizeof(psz_name), "out_%d", i + 1); |
|---|
| 161 |
psz_name[sizeof(psz_name) - 1] = '\0'; |
|---|
| 162 |
p_sys->p_jack_ports[i] = jack_port_register( p_sys->p_jack_client, |
|---|
| 163 |
psz_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0 ); |
|---|
| 164 |
|
|---|
| 165 |
if( p_sys->p_jack_ports[i] == NULL ) |
|---|
| 166 |
{ |
|---|
| 167 |
msg_Err( p_aout, "failed to register a JACK port" ); |
|---|
| 168 |
status = VLC_EGENERIC; |
|---|
| 169 |
goto error_out; |
|---|
| 170 |
} |
|---|
| 171 |
} |
|---|
| 172 |
|
|---|
| 173 |
|
|---|
| 174 |
i_error = jack_activate( p_sys->p_jack_client ); |
|---|
| 175 |
if( i_error ) |
|---|
| 176 |
{ |
|---|
| 177 |
msg_Err( p_aout, "failed to activate JACK client (error %d)", i_error ); |
|---|
| 178 |
status = VLC_EGENERIC; |
|---|
| 179 |
goto error_out; |
|---|
| 180 |
} |
|---|
| 181 |
|
|---|
| 182 |
|
|---|
| 183 |
if( config_GetInt( p_aout, AUTO_CONNECT_OPTION ) ) |
|---|
| 184 |
{ |
|---|
| 185 |
unsigned int i_in_ports; |
|---|
| 186 |
char *psz_regex = config_GetPsz( p_aout, CONNECT_REGEX_OPTION ); |
|---|
| 187 |
const char **pp_in_ports = jack_get_ports( p_sys->p_jack_client, |
|---|
| 188 |
psz_regex, NULL, |
|---|
| 189 |
JackPortIsInput ); |
|---|
| 190 |
free( psz_regex ); |
|---|
| 191 |
|
|---|
| 192 |
i_in_ports = 0; |
|---|
| 193 |
while( pp_in_ports && pp_in_ports[i_in_ports] ) |
|---|
| 194 |
{ |
|---|
| 195 |
i_in_ports++; |
|---|
| 196 |
} |
|---|
| 197 |
|
|---|
| 198 |
|
|---|
| 199 |
for( i = 0; i_in_ports > 0 && i < p_sys->i_channels; i++ ) |
|---|
| 200 |
{ |
|---|
| 201 |
const char* psz_in = pp_in_ports[i % i_in_ports]; |
|---|
| 202 |
const char* psz_out = jack_port_name( p_sys->p_jack_ports[i] ); |
|---|
| 203 |
|
|---|
| 204 |
i_error = jack_connect( p_sys->p_jack_client, psz_out, psz_in ); |
|---|
| 205 |
if( i_error ) |
|---|
| 206 |
{ |
|---|
| 207 |
msg_Err( p_aout, "failed to connect port %s to port %s (error %d)", |
|---|
| 208 |
psz_out, psz_in, i_error ); |
|---|
| 209 |
} |
|---|
| 210 |
else |
|---|
| 211 |
{ |
|---|
| 212 |
msg_Dbg( p_aout, "connecting port %s to port %s", |
|---|
| 213 |
psz_out, psz_in ); |
|---|
| 214 |
} |
|---|
| 215 |
} |
|---|
| 216 |
free( pp_in_ports ); |
|---|
| 217 |
} |
|---|
| 218 |
|
|---|
| 219 |
msg_Dbg( p_aout, "JACK audio output initialized (%d channels, buffer " |
|---|
| 220 |
"size=%d, rate=%d)", p_sys->i_channels, |
|---|
| 221 |
p_aout->output.i_nb_samples, p_aout->output.output.i_rate ); |
|---|
| 222 |
|
|---|
| 223 |
error_out: |
|---|
| 224 |
|
|---|
| 225 |
if( status != VLC_SUCCESS && p_sys != NULL) |
|---|
| 226 |
{ |
|---|
| 227 |
if( p_sys->p_jack_client ) |
|---|
| 228 |
{ |
|---|
| 229 |
jack_deactivate( p_sys->p_jack_client ); |
|---|
| 230 |
jack_client_close( p_sys->p_jack_client ); |
|---|
| 231 |
} |
|---|
| 232 |
free( p_sys->p_jack_ports ); |
|---|
| 233 |
free( p_sys->p_jack_buffers ); |
|---|
| 234 |
free( p_sys ); |
|---|
| 235 |
} |
|---|
| 236 |
return status; |
|---|
| 237 |
} |
|---|
| 238 |
|
|---|
| 239 |
|
|---|
| 240 |
|
|---|
| 241 |
|
|---|
| 242 |
|
|---|
| 243 |
int Process( jack_nframes_t i_frames, void *p_arg ) |
|---|
| 244 |
{ |
|---|
| 245 |
unsigned int i, j, i_nb_samples = 0; |
|---|
| 246 |
aout_instance_t *p_aout = (aout_instance_t*) p_arg; |
|---|
| 247 |
struct aout_sys_t *p_sys = p_aout->output.p_sys; |
|---|
| 248 |
jack_sample_t *p_src = NULL; |
|---|
| 249 |
|
|---|
| 250 |
|
|---|
| 251 |
aout_buffer_t *p_buffer = aout_FifoPop( p_aout, &p_aout->output.fifo ); |
|---|
| 252 |
if( p_buffer != NULL ) |
|---|
| 253 |
{ |
|---|
| 254 |
p_src = (jack_sample_t *)p_buffer->p_buffer; |
|---|
| 255 |
i_nb_samples = p_buffer->i_nb_samples; |
|---|
| 256 |
} |
|---|
| 257 |
|
|---|
| 258 |
|
|---|
| 259 |
for( i = 0; i < p_sys->i_channels; i++ ) |
|---|
| 260 |
{ |
|---|
| 261 |
p_sys->p_jack_buffers[i] = jack_port_get_buffer( p_sys->p_jack_ports[i], |
|---|
| 262 |
i_frames ); |
|---|
| 263 |
} |
|---|
| 264 |
|
|---|
| 265 |
|
|---|
| 266 |
for( j = 0; j < i_nb_samples; j++ ) |
|---|
| 267 |
{ |
|---|
| 268 |
for( i = 0; i < p_sys->i_channels; i++ ) |
|---|
| 269 |
{ |
|---|
| 270 |
jack_sample_t *p_dst = p_sys->p_jack_buffers[i]; |
|---|
| 271 |
p_dst[j] = *p_src; |
|---|
| 272 |
p_src++; |
|---|
| 273 |
} |
|---|
| 274 |
} |
|---|
| 275 |
|
|---|
| 276 |
|
|---|
| 277 |
if( i_nb_samples < i_frames ) |
|---|
| 278 |
{ |
|---|
| 279 |
for( i = 0; i < p_sys->i_channels; i++ ) |
|---|
| 280 |
{ |
|---|
| 281 |
memset( p_sys->p_jack_buffers[i] + i_nb_samples, 0, |
|---|
| 282 |
sizeof( jack_sample_t ) * (i_frames - i_nb_samples) ); |
|---|
| 283 |
} |
|---|
| 284 |
} |
|---|
| 285 |
|
|---|
| 286 |
if( p_buffer ) |
|---|
| 287 |
{ |
|---|
| 288 |
aout_BufferFree( p_buffer ); |
|---|
| 289 |
} |
|---|
| 290 |
return 0; |
|---|
| 291 |
} |
|---|
| 292 |
|
|---|
| 293 |
|
|---|
| 294 |
|
|---|
| 295 |
|
|---|
| 296 |
|
|---|
| 297 |
static void Play( aout_instance_t *p_aout ) |
|---|
| 298 |
{ |
|---|
| 299 |
aout_FifoFirstDate( p_aout, &p_aout->output.fifo ); |
|---|
| 300 |
} |
|---|
| 301 |
|
|---|
| 302 |
|
|---|
| 303 |
|
|---|
| 304 |
|
|---|
| 305 |
static void Close( vlc_object_t *p_this ) |
|---|
| 306 |
{ |
|---|
| 307 |
int i_error; |
|---|
| 308 |
aout_instance_t *p_aout = (aout_instance_t *)p_this; |
|---|
| 309 |
struct aout_sys_t *p_sys = p_aout->output.p_sys; |
|---|
| 310 |
|
|---|
| 311 |
i_error = jack_deactivate( p_sys->p_jack_client ); |
|---|
| 312 |
if( i_error ) |
|---|
| 313 |
{ |
|---|
| 314 |
msg_Err( p_aout, "jack_deactivate failed (error %d)", i_error ); |
|---|
| 315 |
} |
|---|
| 316 |
|
|---|
| 317 |
i_error = jack_client_close( p_sys->p_jack_client ); |
|---|
| 318 |
if( i_error ) |
|---|
| 319 |
{ |
|---|
| 320 |
msg_Err( p_aout, "jack_client_close failed (error %d)", i_error ); |
|---|
| 321 |
} |
|---|
| 322 |
free( p_sys->p_jack_ports ); |
|---|
| 323 |
free( p_sys->p_jack_buffers ); |
|---|
| 324 |
free( p_sys ); |
|---|
| 325 |
} |
|---|