| 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 |
#ifdef HAVE_CONFIG_H |
|---|
| 28 |
# include "config.h" |
|---|
| 29 |
#endif |
|---|
| 30 |
|
|---|
| 31 |
#include <stddef.h> |
|---|
| 32 |
#include <vlc_common.h> |
|---|
| 33 |
|
|---|
| 34 |
#ifdef HAVE_ALLOCA_H |
|---|
| 35 |
# include <alloca.h> |
|---|
| 36 |
#endif |
|---|
| 37 |
#include <vlc_aout.h> |
|---|
| 38 |
#include "aout_internal.h" |
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
int aout_MixerNew( aout_instance_t * p_aout ) |
|---|
| 45 |
{ |
|---|
| 46 |
p_aout->mixer.p_module = module_Need( p_aout, "audio mixer", NULL, 0 ); |
|---|
| 47 |
if ( p_aout->mixer.p_module == NULL ) |
|---|
| 48 |
{ |
|---|
| 49 |
msg_Err( p_aout, "no suitable audio mixer" ); |
|---|
| 50 |
return -1; |
|---|
| 51 |
} |
|---|
| 52 |
p_aout->mixer.b_error = 0; |
|---|
| 53 |
return 0; |
|---|
| 54 |
} |
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
void aout_MixerDelete( aout_instance_t * p_aout ) |
|---|
| 62 |
{ |
|---|
| 63 |
if ( p_aout->mixer.b_error ) return; |
|---|
| 64 |
module_Unneed( p_aout, p_aout->mixer.p_module ); |
|---|
| 65 |
p_aout->mixer.b_error = 1; |
|---|
| 66 |
} |
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
static int MixBuffer( aout_instance_t * p_aout ) |
|---|
| 74 |
{ |
|---|
| 75 |
int i, i_first_input = 0; |
|---|
| 76 |
aout_buffer_t * p_output_buffer; |
|---|
| 77 |
mtime_t start_date, end_date; |
|---|
| 78 |
audio_date_t exact_start_date; |
|---|
| 79 |
|
|---|
| 80 |
if ( p_aout->mixer.b_error ) |
|---|
| 81 |
{ |
|---|
| 82 |
|
|---|
| 83 |
aout_lock_input_fifos( p_aout ); |
|---|
| 84 |
for ( i = 0; i < p_aout->i_nb_inputs; i++ ) |
|---|
| 85 |
{ |
|---|
| 86 |
aout_input_t * p_input = p_aout->pp_inputs[i]; |
|---|
| 87 |
aout_buffer_t * p_buffer = p_input->fifo.p_first; |
|---|
| 88 |
if ( p_input->b_error ) continue; |
|---|
| 89 |
while ( p_buffer != NULL ) |
|---|
| 90 |
{ |
|---|
| 91 |
aout_buffer_t * p_next = p_buffer->p_next; |
|---|
| 92 |
aout_BufferFree( p_buffer ); |
|---|
| 93 |
p_buffer = p_next; |
|---|
| 94 |
} |
|---|
| 95 |
} |
|---|
| 96 |
aout_unlock_input_fifos( p_aout ); |
|---|
| 97 |
return -1; |
|---|
| 98 |
} |
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
aout_lock_output_fifo( p_aout ); |
|---|
| 102 |
aout_lock_input_fifos( p_aout ); |
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 |
memcpy( &exact_start_date, &p_aout->output.fifo.end_date, |
|---|
| 106 |
sizeof(audio_date_t) ); |
|---|
| 107 |
start_date = aout_DateGet( &exact_start_date ); |
|---|
| 108 |
|
|---|
| 109 |
if ( start_date != 0 && start_date < mdate() ) |
|---|
| 110 |
{ |
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 |
msg_Warn( p_aout, "output PTS is out of range (%"PRId64"), clearing out", |
|---|
| 115 |
mdate() - start_date ); |
|---|
| 116 |
aout_FifoSet( p_aout, &p_aout->output.fifo, 0 ); |
|---|
| 117 |
aout_DateSet( &exact_start_date, 0 ); |
|---|
| 118 |
start_date = 0; |
|---|
| 119 |
} |
|---|
| 120 |
|
|---|
| 121 |
aout_unlock_output_fifo( p_aout ); |
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 |
|
|---|
| 125 |
if ( !start_date ) |
|---|
| 126 |
{ |
|---|
| 127 |
|
|---|
| 128 |
for ( i = 0; i < p_aout->i_nb_inputs; i++ ) |
|---|
| 129 |
{ |
|---|
| 130 |
aout_input_t * p_input = p_aout->pp_inputs[i]; |
|---|
| 131 |
aout_fifo_t * p_fifo = &p_input->fifo; |
|---|
| 132 |
aout_buffer_t * p_buffer; |
|---|
| 133 |
|
|---|
| 134 |
if ( p_input->b_error ) continue; |
|---|
| 135 |
|
|---|
| 136 |
p_buffer = p_fifo->p_first; |
|---|
| 137 |
while ( p_buffer != NULL && p_buffer->start_date < mdate() ) |
|---|
| 138 |
{ |
|---|
| 139 |
msg_Warn( p_aout, "input PTS is out of range (%"PRId64"), " |
|---|
| 140 |
"trashing", mdate() - p_buffer->start_date ); |
|---|
| 141 |
p_buffer = aout_FifoPop( p_aout, p_fifo ); |
|---|
| 142 |
aout_BufferFree( p_buffer ); |
|---|
| 143 |
if( p_input->p_input_thread ) |
|---|
| 144 |
{ |
|---|
| 145 |
|
|---|
| 146 |
|
|---|
| 147 |
} |
|---|
| 148 |
p_buffer = p_fifo->p_first; |
|---|
| 149 |
p_input->p_first_byte_to_mix = NULL; |
|---|
| 150 |
} |
|---|
| 151 |
|
|---|
| 152 |
if ( p_buffer == NULL ) |
|---|
| 153 |
{ |
|---|
| 154 |
break; |
|---|
| 155 |
} |
|---|
| 156 |
|
|---|
| 157 |
if ( !start_date || start_date < p_buffer->start_date ) |
|---|
| 158 |
{ |
|---|
| 159 |
aout_DateSet( &exact_start_date, p_buffer->start_date ); |
|---|
| 160 |
start_date = p_buffer->start_date; |
|---|
| 161 |
} |
|---|
| 162 |
} |
|---|
| 163 |
|
|---|
| 164 |
if ( i < p_aout->i_nb_inputs ) |
|---|
| 165 |
{ |
|---|
| 166 |
|
|---|
| 167 |
aout_unlock_input_fifos( p_aout ); |
|---|
| 168 |
return -1; |
|---|
| 169 |
} |
|---|
| 170 |
} |
|---|
| 171 |
aout_DateIncrement( &exact_start_date, p_aout->output.i_nb_samples ); |
|---|
| 172 |
end_date = aout_DateGet( &exact_start_date ); |
|---|
| 173 |
|
|---|
| 174 |
|
|---|
| 175 |
|
|---|
| 176 |
for ( i = 0; i < p_aout->i_nb_inputs; i++ ) |
|---|
| 177 |
{ |
|---|
| 178 |
aout_input_t * p_input = p_aout->pp_inputs[i]; |
|---|
| 179 |
aout_fifo_t * p_fifo = &p_input->fifo; |
|---|
| 180 |
aout_buffer_t * p_buffer; |
|---|
| 181 |
mtime_t prev_date; |
|---|
| 182 |
bool b_drop_buffers; |
|---|
| 183 |
|
|---|
| 184 |
if ( p_input->b_error ) |
|---|
| 185 |
{ |
|---|
| 186 |
if ( i_first_input == i ) i_first_input++; |
|---|
| 187 |
continue; |
|---|
| 188 |
} |
|---|
| 189 |
|
|---|
| 190 |
p_buffer = p_fifo->p_first; |
|---|
| 191 |
if ( p_buffer == NULL ) |
|---|
| 192 |
{ |
|---|
| 193 |
break; |
|---|
| 194 |
} |
|---|
| 195 |
|
|---|
| 196 |
|
|---|
| 197 |
while ( p_buffer != NULL && p_buffer->end_date < start_date - 1 ) |
|---|
| 198 |
{ |
|---|
| 199 |
|
|---|
| 200 |
|
|---|
| 201 |
aout_buffer_t * p_next = p_buffer->p_next; |
|---|
| 202 |
msg_Warn( p_aout, "the mixer got a packet in the past (%"PRId64")", |
|---|
| 203 |
start_date - p_buffer->end_date ); |
|---|
| 204 |
aout_BufferFree( p_buffer ); |
|---|
| 205 |
if( p_input->p_input_thread ) |
|---|
| 206 |
{ |
|---|
| 207 |
|
|---|
| 208 |
|
|---|
| 209 |
} |
|---|
| 210 |
p_fifo->p_first = p_buffer = p_next; |
|---|
| 211 |
p_input->p_first_byte_to_mix = NULL; |
|---|
| 212 |
} |
|---|
| 213 |
if ( p_buffer == NULL ) |
|---|
| 214 |
{ |
|---|
| 215 |
p_fifo->pp_last = &p_fifo->p_first; |
|---|
| 216 |
break; |
|---|
| 217 |
} |
|---|
| 218 |
|
|---|
| 219 |
|
|---|
| 220 |
for ( ; ; ) |
|---|
| 221 |
{ |
|---|
| 222 |
p_buffer = p_fifo->p_first; |
|---|
| 223 |
if ( p_buffer == NULL ) break; |
|---|
| 224 |
if ( p_buffer->end_date >= end_date ) break; |
|---|
| 225 |
|
|---|
| 226 |
|
|---|
| 227 |
prev_date = p_fifo->p_first->end_date; |
|---|
| 228 |
p_buffer = p_buffer->p_next; |
|---|
| 229 |
b_drop_buffers = 0; |
|---|
| 230 |
for ( ; p_buffer != NULL; p_buffer = p_buffer->p_next ) |
|---|
| 231 |
{ |
|---|
| 232 |
if ( prev_date != p_buffer->start_date ) |
|---|
| 233 |
{ |
|---|
| 234 |
msg_Warn( p_aout, |
|---|
| 235 |
"buffer hole, dropping packets (%"PRId64")", |
|---|
| 236 |
p_buffer->start_date - prev_date ); |
|---|
| 237 |
b_drop_buffers = 1; |
|---|
| 238 |
break; |
|---|
| 239 |
} |
|---|
| 240 |
if ( p_buffer->end_date >= end_date ) break; |
|---|
| 241 |
prev_date = p_buffer->end_date; |
|---|
| 242 |
} |
|---|
| 243 |
if ( b_drop_buffers ) |
|---|
| 244 |
{ |
|---|
| 245 |
aout_buffer_t * p_deleted = p_fifo->p_first; |
|---|
| 246 |
while ( p_deleted != NULL && p_deleted != p_buffer ) |
|---|
| 247 |
{ |
|---|
| 248 |
aout_buffer_t * p_next = p_deleted->p_next; |
|---|
| 249 |
aout_BufferFree( p_deleted ); |
|---|
| 250 |
p_deleted = p_next; |
|---|
| 251 |
} |
|---|
| 252 |
p_fifo->p_first = p_deleted; |
|---|
| 253 |
} |
|---|
| 254 |
else break; |
|---|
| 255 |
} |
|---|
| 256 |
if ( p_buffer == NULL ) break; |
|---|
| 257 |
|
|---|
| 258 |
p_buffer = p_fifo->p_first; |
|---|
| 259 |
if ( !AOUT_FMT_NON_LINEAR( &p_aout->mixer.mixer ) ) |
|---|
| 260 |
{ |
|---|
| 261 |
|
|---|
| 262 |
|
|---|
| 263 |
mtime_t i_nb_bytes = (start_date - p_buffer->start_date) |
|---|
| 264 |
* p_aout->mixer.mixer.i_bytes_per_frame |
|---|
| 265 |
* p_aout->mixer.mixer.i_rate |
|---|
| 266 |
/ p_aout->mixer.mixer.i_frame_length |
|---|
| 267 |
/ 1000000; |
|---|
| 268 |
ptrdiff_t mixer_nb_bytes; |
|---|
| 269 |
|
|---|
| 270 |
if ( p_input->p_first_byte_to_mix == NULL ) |
|---|
| 271 |
{ |
|---|
| 272 |
p_input->p_first_byte_to_mix = p_buffer->p_buffer; |
|---|
| 273 |
} |
|---|
| 274 |
mixer_nb_bytes = p_input->p_first_byte_to_mix - p_buffer->p_buffer; |
|---|
| 275 |
|
|---|
| 276 |
if ( !((i_nb_bytes + p_aout->mixer.mixer.i_bytes_per_frame |
|---|
| 277 |
> mixer_nb_bytes) && |
|---|
| 278 |
(i_nb_bytes < p_aout->mixer.mixer.i_bytes_per_frame |
|---|
| 279 |
+ mixer_nb_bytes)) ) |
|---|
| 280 |
{ |
|---|
| 281 |
msg_Warn( p_aout, "mixer start isn't output start (%"PRId64")", |
|---|
| 282 |
i_nb_bytes - mixer_nb_bytes ); |
|---|
| 283 |
|
|---|
| 284 |
|
|---|
| 285 |
i_nb_bytes /= p_aout->mixer.mixer.i_bytes_per_frame; |
|---|
| 286 |
i_nb_bytes *= p_aout->mixer.mixer.i_bytes_per_frame; |
|---|
| 287 |
if( i_nb_bytes < 0 ) |
|---|
| 288 |
{ |
|---|
| 289 |
|
|---|
| 290 |
aout_lock_output_fifo( p_aout ); |
|---|
| 291 |
aout_FifoSet( p_aout, &p_aout->output.fifo, 0 ); |
|---|
| 292 |
aout_DateSet( &exact_start_date, 0 ); |
|---|
| 293 |
aout_unlock_output_fifo( p_aout ); |
|---|
| 294 |
break; |
|---|
| 295 |
} |
|---|
| 296 |
|
|---|
| 297 |
p_input->p_first_byte_to_mix = p_buffer->p_buffer + i_nb_bytes; |
|---|
| 298 |
} |
|---|
| 299 |
} |
|---|
| 300 |
} |
|---|
| 301 |
|
|---|
| 302 |
if ( i < p_aout->i_nb_inputs || i_first_input == p_aout->i_nb_inputs ) |
|---|
| 303 |
{ |
|---|
| 304 |
|
|---|
| 305 |
aout_unlock_input_fifos( p_aout ); |
|---|
| 306 |
return -1; |
|---|
| 307 |
} |
|---|
| 308 |
|
|---|
| 309 |
|
|---|
| 310 |
aout_BufferAlloc( &p_aout->mixer.output_alloc, |
|---|
| 311 |
((uint64_t)p_aout->output.i_nb_samples * 1000000) |
|---|
| 312 |
/ p_aout->output.output.i_rate, |
|---|
| 313 |
|
|---|
| 314 |
|
|---|
| 315 |
p_aout->pp_inputs[i_first_input]->fifo.p_first, |
|---|
| 316 |
p_output_buffer ); |
|---|
| 317 |
if ( p_output_buffer == NULL ) |
|---|
| 318 |
{ |
|---|
| 319 |
aout_unlock_input_fifos( p_aout ); |
|---|
| 320 |
return -1; |
|---|
| 321 |
} |
|---|
| 322 |
|
|---|
| 323 |
if ( p_aout->mixer.output_alloc.i_alloc_type != AOUT_ALLOC_NONE ) |
|---|
| 324 |
{ |
|---|
| 325 |
p_output_buffer->i_nb_samples = p_aout->output.i_nb_samples; |
|---|
| 326 |
p_output_buffer->i_nb_bytes = p_aout->output.i_nb_samples |
|---|
| 327 |
* p_aout->mixer.mixer.i_bytes_per_frame |
|---|
| 328 |
/ p_aout->mixer.mixer.i_frame_length; |
|---|
| 329 |
} |
|---|
| 330 |
p_output_buffer->start_date = start_date; |
|---|
| 331 |
p_output_buffer->end_date = end_date; |
|---|
| 332 |
|
|---|
| 333 |
p_aout->mixer.pf_do_work( p_aout, p_output_buffer ); |
|---|
| 334 |
|
|---|
| 335 |
aout_unlock_input_fifos( p_aout ); |
|---|
| 336 |
|
|---|
| 337 |
aout_OutputPlay( p_aout, p_output_buffer ); |
|---|
| 338 |
|
|---|
| 339 |
return 0; |
|---|
| 340 |
} |
|---|
| 341 |
|
|---|
| 342 |
|
|---|
| 343 |
|
|---|
| 344 |
|
|---|
| 345 |
|
|---|
| 346 |
|
|---|
| 347 |
void aout_MixerRun( aout_instance_t * p_aout ) |
|---|
| 348 |
{ |
|---|
| 349 |
while( MixBuffer( p_aout ) != -1 ); |
|---|
| 350 |
} |
|---|
| 351 |
|
|---|
| 352 |
|
|---|
| 353 |
|
|---|
| 354 |
|
|---|
| 355 |
|
|---|
| 356 |
|
|---|
| 357 |
|
|---|
| 358 |
int aout_MixerMultiplierSet( aout_instance_t * p_aout, float f_multiplier ) |
|---|
| 359 |
{ |
|---|
| 360 |
float f_old = p_aout->mixer.f_multiplier; |
|---|
| 361 |
bool b_new_mixer = 0; |
|---|
| 362 |
|
|---|
| 363 |
if ( !p_aout->mixer.b_error ) |
|---|
| 364 |
{ |
|---|
| 365 |
aout_MixerDelete( p_aout ); |
|---|
| 366 |
b_new_mixer = 1; |
|---|
| 367 |
} |
|---|
| 368 |
|
|---|
| 369 |
p_aout->mixer.f_multiplier = f_multiplier; |
|---|
| 370 |
|
|---|
| 371 |
if ( b_new_mixer && aout_MixerNew( p_aout ) ) |
|---|
| 372 |
{ |
|---|
| 373 |
p_aout->mixer.f_multiplier = f_old; |
|---|
| 374 |
aout_MixerNew( p_aout ); |
|---|
| 375 |
return -1; |
|---|
| 376 |
} |
|---|
| 377 |
|
|---|
| 378 |
return 0; |
|---|
| 379 |
} |
|---|
| 380 |
|
|---|
| 381 |
|
|---|
| 382 |
|
|---|
| 383 |
|
|---|
| 384 |
|
|---|
| 385 |
|
|---|
| 386 |
|
|---|
| 387 |
int aout_MixerMultiplierGet( aout_instance_t * p_aout, float * pf_multiplier ) |
|---|
| 388 |
{ |
|---|
| 389 |
*pf_multiplier = p_aout->mixer.f_multiplier; |
|---|
| 390 |
return 0; |
|---|
| 391 |
} |
|---|
| 392 |
|
|---|