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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
|
/*
* FILE: mix.c
* PROGRAM: RAT
* AUTHOR: Isidor Kouvelas
* MODIFIED BY: Orion Hodson + Colin Perkins
*
* Copyright (c) 1995-2001 University College London
* All rights reserved.
*/
#ifndef HIDE_SOURCE_STRINGS
static const char cvsid[] =
"$Id: mix.c,v 1.119 2002/03/15 17:57:17 ucacoxh Exp $";
#endif /* HIDE_SOURCE_STRINGS */
#include "config_unix.h"
#include "config_win32.h"
#include "memory.h"
#include "util.h"
#include "session.h"
#include "codec_types.h"
#include "codec.h"
#include "audio_util.h"
#include "audio_fmt.h"
#include "channel_types.h"
#include "pdb.h"
#include "mix.h"
#include "playout.h"
#include "debug.h"
#include "parameters.h"
#include "ui_send_audio.h"
#define MIX_MAGIC 0x81654620
struct s_mixer {
int buf_len; /* Length of circular buffer */
int head, tail; /* Index to head and tail samples in buffer */
timestamp_t head_time, tail_time; /* Time rep of head and tail */
int dist; /* Distance between head and tail. (for debug) */
sample *mix_buffer; /* The buffer containing mixed audio data. */
mixer_info_t info;
uint32_t magic; /* Debug check value */
};
typedef void (*mix_f)(sample *buf, sample *incoming, int len);
static mix_f audio_mix_fn;
static void
mix_verify(const mixer_t *ms)
{
#ifdef DEBUG
timestamp_t delta;
int dist;
assert((ms->head + ms->buf_len - ms->tail) % ms->buf_len == ms->dist);
assert(!ts_gt(ms->tail_time, ms->head_time));
assert(ms->dist <= ms->buf_len);
delta = ts_sub(ms->head_time, ms->tail_time);
dist = delta.ticks * ms->info.channels * ms->info.sample_rate / ts_get_freq(delta);
assert(abs((int)ms->dist - (int)dist) <= 1);
if (ts_eq(ms->head_time, ms->tail_time)) {
assert(ms->head == ms->tail);
}
#endif
assert(ms->magic == MIX_MAGIC);
}
/*
* Initialise the circular buffer that is used in mixing.
* The buffer length should be as big as the largest possible
* device cushion used (and maybe some more).
* We allocate space three times the requested one so that we
* dont have to copy everything when we hit the boundaries..
*/
int
mix_create(mixer_t **ppms,
const mixer_info_t *pmi,
timestamp_t now)
{
mixer_t *pms;
pms = (mixer_t *) xmalloc(sizeof(mixer_t));
if (pms) {
memset(pms, 0 , sizeof(mixer_t));
pms->magic = MIX_MAGIC;
memcpy(&pms->info, pmi, sizeof(mixer_info_t));
pms->buf_len = pms->info.buffer_length * pms->info.channels;
pms->mix_buffer = (sample *)xmalloc(3 * pms->buf_len * BYTES_PER_SAMPLE);
audio_zero(pms->mix_buffer, 3 * pms->info.buffer_length , DEV_S16);
pms->mix_buffer += pms->buf_len;
pms->head_time = pms->tail_time = ts_convert(pms->info.sample_rate, now);
*ppms = pms;
audio_mix_fn = audio_mix;
#ifdef WIN32
if (mmx_present()) {
audio_mix_fn = audio_mix_mmx;
}
#endif /* WIN32 */
mix_verify(pms);
debug_msg("Mixer created. Aligned to %d %dkHz\n", now.ticks, ts_get_freq(now));
return TRUE;
}
return FALSE;
}
void
mix_destroy(mixer_t **ppms)
{
mixer_t *pms;
assert(ppms);
pms = *ppms;
assert(pms);
mix_verify(pms);
debug_msg("Mixer destroyed. Head %d %dkHz Tail %d %dkHz\n",
pms->head_time.ticks, ts_get_freq(pms->head_time),
pms->tail_time.ticks, ts_get_freq(pms->tail_time));
xfree(pms->mix_buffer - pms->buf_len); /* yuk! ouch! splat! */
xfree(pms);
*ppms = NULL;
}
static void
mix_zero(mixer_t *ms, int offset, int len)
{
assert(len <= ms->buf_len);
if (offset + len > ms->buf_len) {
audio_zero(ms->mix_buffer + offset, ms->buf_len - offset, DEV_S16);
audio_zero(ms->mix_buffer, offset + len-ms->buf_len, DEV_S16);
} else {
audio_zero(ms->mix_buffer + offset, len, DEV_S16);
}
xmemchk();
}
static void
mix_advance_head(mixer_t *ms, timestamp_t new_head_time)
{
timestamp_t delta;
int zeros;
mix_verify(ms);
assert(ts_gt(new_head_time, ms->head_time));
delta = ts_sub(new_head_time, ms->head_time);
zeros = delta.ticks * ms->info.channels * ms->info.sample_rate / ts_get_freq(delta);
mix_zero(ms, ms->head, zeros);
ms->dist += zeros;
ms->head += zeros;
ms->head %= ms->buf_len;
ms->head_time = new_head_time;
mix_verify(ms);
}
/* mix_put_audio mixes a single audio frame into mix buffer. It returns
* TRUE if incoming audio frame is compatible with mix, FALSE
* otherwise. */
int
mix_put_audio(mixer_t *ms,
pdb_entry_t *pdbe,
coded_unit *frame,
timestamp_t playout)
{
sample *samples;
int32_t pos;
uint32_t nticks, nsamples;
uint16_t channels;
uint32_t rate;
timestamp_t frame_period, playout_end, delta;
mix_verify(ms);
if (!codec_get_native_info(frame->id, &rate, &channels)) {
debug_msg("Cannot mix non-native media\n");
abort();
}
if (rate != ms->info.sample_rate || channels != ms->info.channels) {
/* This should only occur if source changes sample rate
* mid-stream and before buffering runs dry in end host.
* This should be a very rare event.
*/
debug_msg("Unit (%d, %d) not compitible with mix (%d, %d).\n",
rate,
channels,
ms->info.sample_rate,
ms->info.channels);
return FALSE;
}
assert(rate == (uint32_t)ms->info.sample_rate);
assert(channels == (uint32_t)ms->info.channels);
playout = ts_convert(ms->info.sample_rate, playout);
nticks = frame->data_len / (sizeof(sample) * channels);
frame_period = ts_map32(rate, nticks);
/* Map frame period to mixer time base, otherwise we can get
* truncation errors in verification of mixer when sample rate
* conversion is active. */
frame_period = ts_convert(ms->info.sample_rate, frame_period);
if (pdbe->first_mix) {
debug_msg("New mix\n");
pdbe->next_mix = playout;
pdbe->first_mix = 0;
}
mix_verify(ms);
if (ts_gt(ms->tail_time, playout)) {
debug_msg("playout before tail (%d %dkHz < %d %dkHz)\n",
playout.ticks, ts_get_freq(playout),
ms->tail_time.ticks, ts_get_freq(ms->tail_time));
}
samples = (sample*)frame->data;
nsamples = frame->data_len / sizeof(sample);
/* Advance head if necessary */
playout_end = ts_add(playout, ts_map32(ms->info.sample_rate, nsamples / ms->info.channels));
if (ts_gt(playout_end, ms->head_time)) {
uint32_t playout_delta = timestamp_to_ms(ts_sub(playout_end, ms->head_time));
if (playout_delta > 1000) {
debug_msg("WARNING: Large playout buffer advancement (%dms)\n", playout_delta);
}
mix_advance_head(ms, playout_end);
}
/* Check for overlap in decoded frames */
if (!ts_eq(pdbe->next_mix, playout)) {
if (ts_gt(pdbe->next_mix, playout)) {
delta = ts_sub(pdbe->next_mix, playout);
if (ts_gt(frame_period, delta)) {
uint32_t trim;
/* Unit overlaps with earlier data written to buffer.
* Jump past overlapping samples, decrease number of
* samples that need to be written and correct playout
* so they are written to the correct place.
*/
delta = ts_convert(ms->info.sample_rate, delta);
trim = delta.ticks * ms->info.channels;
debug_msg("Mixer trimmed %d samples (Expected playout %d got %d) ssrc (0x%08x)\n",
trim, pdbe->next_mix.ticks, playout.ticks, pdbe->ssrc);
samples += trim;
nsamples -= trim;
playout = ts_add(playout, delta);
} else {
debug_msg("Skipped unit\n");
return TRUE; /* Nothing to do but no fmt change */
}
} else {
debug_msg("Gap between units %d %d ssrc 0x%08x\n",
pdbe->next_mix.ticks,
playout.ticks,
pdbe->ssrc);
}
}
/* Work out where to write the data (head_time > playout) */
delta = ts_sub(ms->head_time, playout);
pos = ms->head - delta.ticks * ms->info.channels;
pos = (pos + ms->buf_len) % ms->buf_len; /* Handle wraps */
if (pos + nsamples > (uint32_t)ms->buf_len) {
audio_mix_fn(ms->mix_buffer + pos,
samples,
ms->buf_len - pos);
audio_mix_fn(ms->mix_buffer,
samples + (ms->buf_len - pos),
pos + nsamples - ms->buf_len);
} else {
audio_mix_fn(ms->mix_buffer + pos,
samples,
nsamples);
}
xmemchk();
pdbe->next_mix = playout_end;
return TRUE;
}
/*
* The mix_get_audio function returns a pointer to "request" samples of mixed
* audio data, suitable for playout (ie: you can do audio_device_write() with
* the returned data).
*
* This function was modified so that it returns the amount of
* silence at the end of the buffer returned so that the cushion
* adjustment functions can use it to decrease the cushion.
*
* Note: "request" is number of samples to get and not sampling intervals!
*/
int
mix_get_audio(mixer_t *ms, int request, sample **bufp)
{
int silence, amount;
timestamp_t delta;
xmemchk();
mix_verify(ms);
amount = request;
assert(amount < ms->buf_len);
if (amount > ms->dist) {
timestamp_t new_head_time;
/*
* If we dont have enough to give one of two things
* must have happened.
* a) There was silence :-)
* b) There wasn't enough time to decode the stuff...
* In either case we will have to return silence for
* now so zero the rest of the buffer and move the head.
*/
#ifdef DEBUG_MIX
if (!ts_eq(ms->head_time, ms->tail_time)) {
/* Only print message if not-silent */
debug_msg("Insufficient audio: %d < %d\n", ms->dist, amount);
}
#endif /* DEBUG_MIX */
silence = amount - ms->dist;
new_head_time = ts_add(ms->head_time,
ts_map32(ms->info.sample_rate, silence/ms->info.channels));
mix_advance_head(ms, new_head_time);
} else {
silence = 0;
}
if (ms->tail + amount > ms->buf_len) {
/*
* We have run into the end of the buffer so we will
* have to copy stuff before we return it.
* The space after the 'end' of the buffer is used
* for this purpose as the space before is used to
* hold silence that is returned in case the cushion
* grows too much.
* Of course we could use both here (depending on which
* direction involves less copying) and copy actual
* voice data in the case a cushion grows into it.
* The problem is that in that case we are probably in
* trouble and want to avoid doing too much...
*
* Also if the device is working in similar boundaries
* to our chunk sizes and we are a bit careful about the
* possible cushion sizes this case can be avoided.
*/
xmemchk();
memcpy(ms->mix_buffer + ms->buf_len, ms->mix_buffer, BYTES_PER_SAMPLE*(ms->tail + amount - ms->buf_len));
xmemchk();
#ifdef DEBUG_MIX
debug_msg("Copying start of mix len: %d\n", ms->tail + amount - ms->buf_len);
#endif /* DEBUG_MIX */
}
mix_verify(ms);
*bufp = ms->mix_buffer + ms->tail;
delta = ts_map32(ms->info.sample_rate, amount/ms->info.channels);
ms->tail_time = ts_add(ms->tail_time, delta);
ms->tail += amount;
ms->tail %= ms->buf_len;
ms->dist -= amount;
mix_verify(ms);
return silence;
}
/*
* We need the amount of time we went dry so that we can make a time
* adjustment to keep in sync with the receive buffer etc...
*/
void
mix_new_cushion(mixer_t *ms,
int last_cushion_size,
int new_cushion_size,
int dry_time,
sample **bufp)
{
int diff, elapsed_time;
debug_msg("Getting new cushion %d old %d\n", new_cushion_size, last_cushion_size);
mix_verify(ms);
elapsed_time = (last_cushion_size + dry_time);
diff = abs(new_cushion_size - elapsed_time) * ms->info.channels;
if (new_cushion_size > elapsed_time) {
/*
* New cushion is larger so move tail back to get
* the right amount and end up at the correct time.
* The effect of moving the tail is that some old
* audio and/or silence will be replayed. We do not
* care to much as we are right after an underflow.
*/
ms->tail -= diff;
if (ms->tail < 0) {
ms->tail += ms->buf_len;
}
ms->dist += diff;
ms->tail_time = ts_sub(ms->tail_time,
ts_map32(ms->info.sample_rate, diff/ms->info.channels));
mix_verify(ms);
} else if (new_cushion_size < elapsed_time) {
/*
* New cushion is smaller so we have to throw away
* some audio.
*/
ms->tail += diff;
ms->tail %= ms->buf_len;
ms->tail_time = ts_add(ms->tail_time,
ts_map32(ms->info.sample_rate, diff/ms->info.channels));
if (diff > ms->dist) {
ms->head = ms->tail;
ms->head_time = ms->tail_time;
ms->dist = 0;
} else {
ms->dist -= diff;
}
mix_verify(ms);
}
mix_verify(ms);
mix_get_audio(ms, new_cushion_size * ms->info.channels, bufp);
mix_verify(ms);
}
uint16_t
mix_get_energy(mixer_t *ms, uint16_t samples)
{
sample *bp;
if (ms->tail < samples) {
bp = ms->mix_buffer + ms->buf_len - samples * ms->info.channels;
} else {
bp = ms->mix_buffer + ms->tail - samples;
}
return audio_avg_energy(bp, samples, 1);
}
int
mix_active(mixer_t *ms)
{
mix_verify(ms);
return !ts_eq(ms->head_time, ms->tail_time);
}
const mixer_info_t *
mix_query(const mixer_t *ms)
{
mix_verify(ms);
if (ms == NULL) {
return FALSE;
}
return &ms->info;
}
|