Commit 1760c10b authored by Jean-Baptiste Kempf's avatar Jean-Baptiste Kempf

Jack Input bugfixes and some more options:

   - bug: cracking in the captured sound
   - autoconnect to inputs
   - use VLC pace
Patch by Pierre-Luc Beaudoin from Savoir-faire Linux
parent 923f5384
...@@ -3,9 +3,11 @@ ...@@ -3,9 +3,11 @@
***************************************************************************** *****************************************************************************
* Copyright (C) 2007 the VideoLAN team * Copyright (C) 2007 the VideoLAN team
* Copyright (C) 2007 Société des arts technologiques * Copyright (C) 2007 Société des arts technologiques
* Copyright (C) 2007 Savoir-faire Linux
* *
* Authors: Arnaud Sala <arnaud.sala at savoirfairelinux.com> * Authors: Arnaud Sala <arnaud.sala at savoirfairelinux.com>
* Julien Plissonneau Duquene <... at savoirfairelinux.com> * Julien Plissonneau Duquene <... at savoirfairelinux.com>
* Pierre-Luc Beaudoin <pierre-luc.beaudoin at savoirfairelinux.com>
* *
* This program is free software; you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
...@@ -42,8 +44,9 @@ ...@@ -42,8 +44,9 @@
#include <jack/jack.h> #include <jack/jack.h>
#include <jack/ringbuffer.h> #include <jack/ringbuffer.h>
#include <sys/mman.h> /* mlock() */
#include <errno.h> #include <errno.h>
#include <sys/types.h>
#include <unistd.h>
/***************************************************************************** /*****************************************************************************
* Module descriptor * Module descriptor
...@@ -104,12 +107,16 @@ struct demux_sys_t ...@@ -104,12 +107,16 @@ struct demux_sys_t
jack_nframes_t jack_buffer_size; jack_nframes_t jack_buffer_size;
jack_nframes_t jack_sample_rate; jack_nframes_t jack_sample_rate;
size_t jack_sample_size; size_t jack_sample_size;
char *psz_ports;
char **pp_jack_port_table;
char i_match_ports;
}; };
static int Demux( demux_t * ); static int Demux( demux_t * );
static int Control( demux_t *p_demux, int i_query, va_list args ); static int Control( demux_t *p_demux, int i_query, va_list args );
static void Parse ( demux_t * ); static void Parse ( demux_t * );
static void Port_finder( demux_t * );
static int Process( jack_nframes_t i_frames, void *p_arg ); static int Process( jack_nframes_t i_frames, void *p_arg );
static block_t *GrabJack( demux_t * ); static block_t *GrabJack( demux_t * );
...@@ -123,6 +130,8 @@ static int Open( vlc_object_t *p_this ) ...@@ -123,6 +130,8 @@ static int Open( vlc_object_t *p_this )
demux_t *p_demux = ( demux_t* )p_this; demux_t *p_demux = ( demux_t* )p_this;
demux_sys_t *p_sys; demux_sys_t *p_sys;
es_format_t fmt; es_format_t fmt;
int i_out_ports = 0;
p_demux->pf_demux = Demux; p_demux->pf_demux = Demux;
p_demux->pf_control = Control; p_demux->pf_control = Control;
...@@ -158,9 +167,18 @@ static int Open( vlc_object_t *p_this ) ...@@ -158,9 +167,18 @@ static int Open( vlc_object_t *p_this )
return VLC_EGENERIC; return VLC_EGENERIC;
} }
/* find some specifics ports if user entered a regexp */
if( p_sys->psz_ports)
{
Port_finder( p_demux );
if( p_sys->i_channels == 0 )
{
p_sys->i_channels = p_sys->i_match_ports;
}
}
/* allocate input ports */ /* allocate input ports */
if( p_sys->i_channels == 0 || p_sys->i_channels > 8 ) if( p_sys->i_channels == 0 ) p_sys->i_channels = 2 ; /* default number of port */
p_sys->i_channels = 2 ; /* default number of port */
p_sys->pp_jack_port_input = malloc( p_sys->pp_jack_port_input = malloc(
p_sys->i_channels * sizeof( jack_port_t* ) ); p_sys->i_channels * sizeof( jack_port_t* ) );
if( p_sys->pp_jack_port_input == NULL ) if( p_sys->pp_jack_port_input == NULL )
...@@ -170,8 +188,10 @@ static int Open( vlc_object_t *p_this ) ...@@ -170,8 +188,10 @@ static int Open( vlc_object_t *p_this )
} }
/* allocate ringbuffer */ /* allocate ringbuffer */
/* 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 */
p_sys->p_jack_ringbuffer = jack_ringbuffer_create( p_sys->i_channels p_sys->p_jack_ringbuffer = jack_ringbuffer_create( p_sys->i_channels
* jack_get_buffer_size( p_sys->p_jack_client ) * jack_get_sample_rate( p_sys->p_jack_client )
* sizeof( jack_default_audio_sample_t ) ); * sizeof( jack_default_audio_sample_t ) );
if( p_sys->p_jack_ringbuffer == NULL ) if( p_sys->p_jack_ringbuffer == NULL )
{ {
...@@ -223,15 +243,32 @@ static int Open( vlc_object_t *p_this ) ...@@ -223,15 +243,32 @@ static int Open( vlc_object_t *p_this )
return VLC_EGENERIC; return VLC_EGENERIC;
} }
/* connect vlc input to jack output ports if requested */ /* connect vlc input to specifics jack output ports if requested */
if( var_GetBool( p_demux, "jack-input-auto-connect" ) ) /* if( var_GetBool( p_demux, "jack-input-auto-connect" ) && p_sys->psz_ports ) */
if( p_sys->psz_ports )
{ {
const char **pp_jack_port_output; int i_input_ports;
int i_out_ports = 0;
int i_in;
int j; int j;
pp_jack_port_output = jack_get_ports( p_sys->p_jack_client, NULL, NULL,
JackPortIsOutput ); if( p_sys->i_match_ports > 0 )
{
for( j = 0; j < p_sys->i_match_ports; j++ )
{
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] ) );
}
}
}
/* connect vlc input to all jack output ports if requested */
if( var_GetBool( p_demux, "jack-input-auto-connect" ) && !p_sys->psz_ports )
{
int i_input_ports;
int j;
const char **pp_jack_port_output;
pp_jack_port_output = jack_get_ports( p_sys->p_jack_client, NULL, NULL, JackPortIsOutput );
while( pp_jack_port_output && pp_jack_port_output[i_out_ports] ) while( pp_jack_port_output && pp_jack_port_output[i_out_ports] )
{ {
...@@ -241,9 +278,9 @@ static int Open( vlc_object_t *p_this ) ...@@ -241,9 +278,9 @@ static int Open( vlc_object_t *p_this )
{ {
for( j = 0; j < i_out_ports; j++ ) for( j = 0; j < i_out_ports; j++ )
{ {
i_in = j % p_sys->i_channels; i_input_ports = j % p_sys->i_channels;
jack_connect( p_sys->p_jack_client, pp_jack_port_output[j], jack_connect( p_sys->p_jack_client, pp_jack_port_output[j],
jack_port_name( p_sys->pp_jack_port_input[i_in] ) ); jack_port_name( p_sys->pp_jack_port_input[i_input_ports] ) );
} }
} }
if( pp_jack_port_output ) free( pp_jack_port_output ); if( pp_jack_port_output ) free( pp_jack_port_output );
...@@ -288,6 +325,7 @@ static void Close( vlc_object_t *p_this ) ...@@ -288,6 +325,7 @@ static void Close( vlc_object_t *p_this )
if( p_sys->p_jack_ringbuffer ) jack_ringbuffer_free( p_sys->p_jack_ringbuffer ); if( p_sys->p_jack_ringbuffer ) jack_ringbuffer_free( p_sys->p_jack_ringbuffer );
if( p_sys->pp_jack_port_input ) free( p_sys->pp_jack_port_input ); if( p_sys->pp_jack_port_input ) free( p_sys->pp_jack_port_input );
if( p_sys->pp_jack_buffer ) free( p_sys->pp_jack_buffer ); if( p_sys->pp_jack_buffer ) free( p_sys->pp_jack_buffer );
if( p_sys->pp_jack_port_table ) free( p_sys->pp_jack_port_table );
free( p_sys ); free( p_sys );
} }
...@@ -374,14 +412,21 @@ int Process( jack_nframes_t i_frames, void *p_arg ) ...@@ -374,14 +412,21 @@ int Process( jack_nframes_t i_frames, void *p_arg )
} }
/* fill ring buffer with signal */ /* fill ring buffer with signal */
for( j = 0; j < p_sys->jack_buffer_size; j++ ) for( j = 0; j < i_frames; j++ )
{ {
for( i = 0; i <p_sys->i_channels; i++ ) for( i = 0; i <p_sys->i_channels; i++ )
{ {
if( jack_ringbuffer_write_space( p_sys->p_jack_ringbuffer ) if( jack_ringbuffer_write_space( p_sys->p_jack_ringbuffer ) <
< p_sys->jack_sample_size ) return 1; // buffer overflow p_sys->jack_sample_size ) {
msg_Err( p_demux, "buffer overflow");
return 0; // buffer overflow
}
i_write = jack_ringbuffer_write( p_sys->p_jack_ringbuffer, i_write = jack_ringbuffer_write( p_sys->p_jack_ringbuffer,
p_sys->pp_jack_buffer[i]+j, p_sys->jack_sample_size ); ( char * ) (p_sys->pp_jack_buffer[i]+j),
p_sys->jack_sample_size );
if (i_write != p_sys->jack_sample_size ) {
msg_Warn( p_demux, "error writing on ring buffer");
}
} }
} }
...@@ -421,16 +466,79 @@ static block_t *GrabJack( demux_t *p_demux ) ...@@ -421,16 +466,79 @@ static block_t *GrabJack( demux_t *p_demux )
return 0; return 0;
} }
//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++;
i_read = jack_ringbuffer_read( p_sys->p_jack_ringbuffer, ( char * ) p_block->p_buffer, i_read );
p_block->i_dts = p_block->i_pts = date_Increment( &p_sys->pts,
i_read/(p_sys->i_channels * p_sys->jack_sample_size) );
p_sys->p_block_audio = p_block; p_sys->p_block_audio = p_block;
p_block->i_buffer = i_read; p_block->i_buffer = i_read;
p_sys->p_block_audio = 0; p_sys->p_block_audio = 0;
jack_ringbuffer_read( p_sys->p_jack_ringbuffer, p_block->p_buffer, i_read ); return p_block;
}
p_block->i_dts = p_block->i_pts = date_Increment(
&p_sys->pts, i_read/(p_sys->i_channels * p_sys->jack_sample_size) );
return p_block; /*****************************************************************************
* Port_finder: compare ports with the regexp entered
*****************************************************************************/
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 ) )
{
psz_uri = decode_URI_duplicate( token );
/* 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 );
free( pp_jack_port_output );
}
else
{
while( pp_jack_port_output && pp_jack_port_output[i_out_ports] )
{
i_out_ports++;
}
/* alloc an array to store all the matched ports */
p_sys->pp_jack_port_table = realloc( p_sys->pp_jack_port_table,
(i_out_ports * sizeof( char * ) + i_total_out_ports * sizeof( char * ) ) );
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];
}
i_total_out_ports += i_out_ports;
}
}
if( pp_jack_port_output ) free( pp_jack_port_output );
p_sys->i_match_ports = i_total_out_ports;
} }
...@@ -448,6 +556,21 @@ static void Parse( demux_t *p_demux ) ...@@ -448,6 +556,21 @@ static void Parse( demux_t *p_demux )
p_sys->i_channels = abs( strtol( psz_parser + strlen( "channels=" ), p_sys->i_channels = abs( strtol( psz_parser + strlen( "channels=" ),
&psz_parser, 0 ) ); &psz_parser, 0 ) );
} }
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;
}
else else
{ {
msg_Warn( p_demux, "unknown option" ); msg_Warn( p_demux, "unknown option" );
...@@ -468,6 +591,21 @@ static void Parse( demux_t *p_demux ) ...@@ -468,6 +591,21 @@ static void Parse( demux_t *p_demux )
p_sys->i_channels = abs( strtol( p_sys->i_channels = abs( strtol(
psz_parser + strlen( "channels=" ), &psz_parser, 0 ) ); psz_parser + strlen( "channels=" ), &psz_parser, 0 ) );
} }
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;
}
else else
{ {
msg_Warn( p_demux, "unknown option" ); msg_Warn( p_demux, "unknown option" );
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment