| 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 |
#include <errno.h> |
|---|
| 28 |
#include <unistd.h> |
|---|
| 29 |
|
|---|
| 30 |
#ifdef HAVE_CONFIG_H |
|---|
| 31 |
# include "config.h" |
|---|
| 32 |
#endif |
|---|
| 33 |
|
|---|
| 34 |
#include <vlc_common.h> |
|---|
| 35 |
#include <vlc_plugin.h> |
|---|
| 36 |
#include <vlc_aout.h> |
|---|
| 37 |
|
|---|
| 38 |
#include <sys/socket.h> |
|---|
| 39 |
|
|---|
| 40 |
#include <sys/time.h> |
|---|
| 41 |
#include <time.h> |
|---|
| 42 |
|
|---|
| 43 |
#include <esd.h> |
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
struct aout_sys_t |
|---|
| 52 |
{ |
|---|
| 53 |
esd_format_t esd_format; |
|---|
| 54 |
int i_fd; |
|---|
| 55 |
|
|---|
| 56 |
mtime_t latency; |
|---|
| 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 |
|
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
vlc_module_begin(); |
|---|
| 70 |
set_description( N_("EsounD audio output") ); |
|---|
| 71 |
set_shortname( "EsounD" ); |
|---|
| 72 |
set_capability( "audio output", 50 ); |
|---|
| 73 |
add_string( "esdserver", "", NULL, N_("Esound server"), NULL, false ); |
|---|
| 74 |
set_category( CAT_AUDIO ); |
|---|
| 75 |
set_subcategory( SUBCAT_AUDIO_AOUT ); |
|---|
| 76 |
set_callbacks( Open, Close ); |
|---|
| 77 |
add_shortcut( "esound" ); |
|---|
| 78 |
vlc_module_end(); |
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
static int Open( vlc_object_t *p_this ) |
|---|
| 84 |
{ |
|---|
| 85 |
aout_instance_t *p_aout = (aout_instance_t *)p_this; |
|---|
| 86 |
struct aout_sys_t * p_sys; |
|---|
| 87 |
char * psz_server; |
|---|
| 88 |
int i_nb_channels; |
|---|
| 89 |
int i_newfd; |
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 |
p_sys = malloc( sizeof( aout_sys_t ) ); |
|---|
| 93 |
if( p_sys == NULL ) |
|---|
| 94 |
return VLC_ENOMEM; |
|---|
| 95 |
|
|---|
| 96 |
p_aout->output.p_sys = p_sys; |
|---|
| 97 |
|
|---|
| 98 |
p_aout->output.pf_play = Play; |
|---|
| 99 |
aout_VolumeSoftInit( p_aout ); |
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 |
p_sys->esd_format = ESD_BITS16 | ESD_STREAM | ESD_PLAY; |
|---|
| 103 |
p_sys->esd_format &= ~ESD_MASK_CHAN; |
|---|
| 104 |
|
|---|
| 105 |
p_aout->output.output.i_format = AOUT_FMT_S16_NE; |
|---|
| 106 |
i_nb_channels = aout_FormatNbChannels( &p_aout->output.output ); |
|---|
| 107 |
if ( i_nb_channels > 2 ) |
|---|
| 108 |
{ |
|---|
| 109 |
|
|---|
| 110 |
i_nb_channels = 2; |
|---|
| 111 |
p_aout->output.output.i_physical_channels = |
|---|
| 112 |
AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT; |
|---|
| 113 |
} |
|---|
| 114 |
|
|---|
| 115 |
switch( i_nb_channels ) |
|---|
| 116 |
{ |
|---|
| 117 |
case 1: |
|---|
| 118 |
p_sys->esd_format |= ESD_MONO; |
|---|
| 119 |
break; |
|---|
| 120 |
case 2: |
|---|
| 121 |
p_sys->esd_format |= ESD_STEREO; |
|---|
| 122 |
break; |
|---|
| 123 |
} |
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 |
p_aout->output.output.i_rate = ESD_DEFAULT_RATE; |
|---|
| 127 |
p_aout->output.i_nb_samples = ESD_BUF_SIZE * 2; |
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 |
psz_server = config_GetPsz( p_aout, "esdserver" ); |
|---|
| 132 |
if( psz_server && *psz_server ) |
|---|
| 133 |
{ |
|---|
| 134 |
p_sys->i_fd = esd_play_stream_fallback( p_sys->esd_format, |
|---|
| 135 |
p_aout->output.output.i_rate, |
|---|
| 136 |
psz_server, "vlc" ); |
|---|
| 137 |
} |
|---|
| 138 |
else |
|---|
| 139 |
{ |
|---|
| 140 |
p_sys->i_fd = esd_play_stream_fallback( p_sys->esd_format, |
|---|
| 141 |
p_aout->output.output.i_rate, |
|---|
| 142 |
NULL, "vlc" ); |
|---|
| 143 |
} |
|---|
| 144 |
|
|---|
| 145 |
if( p_sys->i_fd < 0 ) |
|---|
| 146 |
{ |
|---|
| 147 |
msg_Err( p_aout, "cannot open esound socket (format 0x%08x at %d Hz)", |
|---|
| 148 |
p_sys->esd_format, p_aout->output.output.i_rate ); |
|---|
| 149 |
free( psz_server ); |
|---|
| 150 |
free( p_sys ); |
|---|
| 151 |
return VLC_EGENERIC; |
|---|
| 152 |
} |
|---|
| 153 |
|
|---|
| 154 |
if( psz_server && *psz_server ) |
|---|
| 155 |
{ |
|---|
| 156 |
struct timeval start, stop; |
|---|
| 157 |
esd_server_info_t * p_info; |
|---|
| 158 |
|
|---|
| 159 |
gettimeofday( &start, NULL ); |
|---|
| 160 |
p_info = esd_get_server_info( p_sys->i_fd ); |
|---|
| 161 |
gettimeofday( &stop, NULL ); |
|---|
| 162 |
|
|---|
| 163 |
p_sys->latency = (mtime_t)( stop.tv_sec - start.tv_sec ) |
|---|
| 164 |
* (mtime_t)1000000; |
|---|
| 165 |
p_sys->latency += stop.tv_usec - start.tv_usec; |
|---|
| 166 |
} |
|---|
| 167 |
else |
|---|
| 168 |
{ |
|---|
| 169 |
p_sys->latency = 0; |
|---|
| 170 |
} |
|---|
| 171 |
|
|---|
| 172 |
|
|---|
| 173 |
|
|---|
| 174 |
p_sys->latency += |
|---|
| 175 |
(mtime_t)( esd_get_latency( i_newfd = esd_open_sound(NULL) ) |
|---|
| 176 |
+ ESD_BUF_SIZE / 2 |
|---|
| 177 |
* p_aout->output.output.i_bytes_per_frame |
|---|
| 178 |
* p_aout->output.output.i_rate |
|---|
| 179 |
/ ESD_DEFAULT_RATE ) |
|---|
| 180 |
* (mtime_t)1000000 |
|---|
| 181 |
/ p_aout->output.output.i_bytes_per_frame |
|---|
| 182 |
/ p_aout->output.output.i_rate; |
|---|
| 183 |
|
|---|
| 184 |
free( psz_server ); |
|---|
| 185 |
close( i_newfd ); |
|---|
| 186 |
return VLC_SUCCESS; |
|---|
| 187 |
} |
|---|
| 188 |
|
|---|
| 189 |
|
|---|
| 190 |
|
|---|
| 191 |
|
|---|
| 192 |
static void Play( aout_instance_t *p_aout ) |
|---|
| 193 |
{ |
|---|
| 194 |
struct aout_sys_t * p_sys = p_aout->output.p_sys; |
|---|
| 195 |
aout_buffer_t * p_buffer; |
|---|
| 196 |
int i_tmp; |
|---|
| 197 |
|
|---|
| 198 |
p_buffer = aout_FifoPop( p_aout, &p_aout->output.fifo ); |
|---|
| 199 |
|
|---|
| 200 |
if ( p_buffer != NULL ) |
|---|
| 201 |
{ |
|---|
| 202 |
unsigned int pos; |
|---|
| 203 |
unsigned char *data = p_buffer->p_buffer; |
|---|
| 204 |
|
|---|
| 205 |
for( pos = 0; pos + ESD_BUF_SIZE <= p_buffer->i_nb_bytes; |
|---|
| 206 |
pos += ESD_BUF_SIZE ) |
|---|
| 207 |
{ |
|---|
| 208 |
i_tmp = write( p_sys->i_fd, data + pos, ESD_BUF_SIZE ); |
|---|
| 209 |
if( i_tmp < 0 ) |
|---|
| 210 |
{ |
|---|
| 211 |
msg_Err( p_aout, "write failed (%m)" ); |
|---|
| 212 |
} |
|---|
| 213 |
} |
|---|
| 214 |
aout_BufferFree( p_buffer ); |
|---|
| 215 |
} |
|---|
| 216 |
} |
|---|
| 217 |
|
|---|
| 218 |
|
|---|
| 219 |
|
|---|
| 220 |
|
|---|
| 221 |
static void Close( vlc_object_t *p_this ) |
|---|
| 222 |
{ |
|---|
| 223 |
aout_instance_t *p_aout = (aout_instance_t *)p_this; |
|---|
| 224 |
struct aout_sys_t * p_sys = p_aout->output.p_sys; |
|---|
| 225 |
|
|---|
| 226 |
close( p_sys->i_fd ); |
|---|
| 227 |
free( p_sys ); |
|---|
| 228 |
} |
|---|
| 229 |
|
|---|
| 230 |
#if 0 |
|---|
| 231 |
|
|---|
| 232 |
|
|---|
| 233 |
|
|---|
| 234 |
|
|---|
| 235 |
|
|---|
| 236 |
static void Play( aout_thread_t *p_aout, uint8_t *buffer, int i_size ) |
|---|
| 237 |
{ |
|---|
| 238 |
int i_amount; |
|---|
| 239 |
|
|---|
| 240 |
int m1 = p_aout->output.p_sys->esd_format & ESD_STEREO ? 1 : 2; |
|---|
| 241 |
int m2 = p_aout->output.p_sys->esd_format & ESD_BITS16 ? 64 : 128; |
|---|
| 242 |
|
|---|
| 243 |
i_amount = (m1 * 44100 * (ESD_BUF_SIZE + m1 * m2)) / p_aout->i_rate; |
|---|
| 244 |
|
|---|
| 245 |
write( p_aout->output.p_sys->i_fd, buffer, i_size ); |
|---|
| 246 |
} |
|---|
| 247 |
#endif |
|---|
| 248 |
|
|---|