2004-07-16 19:56:36 +00:00
|
|
|
/***
|
2006-06-19 21:53:48 +00:00
|
|
|
This file is part of PulseAudio.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-02-13 15:35:19 +00:00
|
|
|
Copyright 2004-2006 Lennart Poettering
|
|
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
PulseAudio is free software; you can redistribute it and/or modify
|
2004-11-14 14:58:54 +00:00
|
|
|
it under the terms of the GNU Lesser General Public License as published
|
2004-07-16 19:56:36 +00:00
|
|
|
by the Free Software Foundation; either version 2 of the License,
|
|
|
|
|
or (at your option) any later version.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
PulseAudio is distributed in the hope that it will be useful, but
|
2004-07-16 19:56:36 +00:00
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
General Public License for more details.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-11-14 14:58:54 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
2006-06-19 21:53:48 +00:00
|
|
|
along with PulseAudio; if not, write to the Free Software
|
2004-07-16 19:56:36 +00:00
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
|
USA.
|
|
|
|
|
***/
|
|
|
|
|
|
2004-07-16 19:16:42 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2004-06-16 00:05:30 +00:00
|
|
|
#include <sys/time.h>
|
|
|
|
|
#include <time.h>
|
2004-06-15 00:29:01 +00:00
|
|
|
#include <stdio.h>
|
2004-06-08 23:54:24 +00:00
|
|
|
#include <stdlib.h>
|
2004-08-22 21:13:58 +00:00
|
|
|
#include <string.h>
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
#include <pulse/xmalloc.h>
|
2006-05-17 16:34:18 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
#include <pulsecore/log.h>
|
|
|
|
|
#include <pulsecore/mcalign.h>
|
2007-10-28 19:13:50 +00:00
|
|
|
#include <pulsecore/macro.h>
|
|
|
|
|
#include <pulsecore/flist.h>
|
2006-02-17 12:10:58 +00:00
|
|
|
|
2004-06-08 23:54:24 +00:00
|
|
|
#include "memblockq.h"
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
struct list_item {
|
|
|
|
|
struct list_item *next, *prev;
|
2006-02-20 04:05:16 +00:00
|
|
|
int64_t index;
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_memchunk chunk;
|
2004-06-08 23:54:24 +00:00
|
|
|
};
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
PA_STATIC_FLIST_DECLARE(list_items, 0, pa_xfree);
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
struct pa_memblockq {
|
2007-10-28 19:13:50 +00:00
|
|
|
struct list_item *blocks, *blocks_tail;
|
2008-05-15 23:34:41 +00:00
|
|
|
struct list_item *current_read, *current_write;
|
2004-06-08 23:54:24 +00:00
|
|
|
unsigned n_blocks;
|
2008-05-15 23:34:41 +00:00
|
|
|
size_t maxlength, tlength, base, prebuf, minreq, maxrewind;
|
2006-02-20 04:05:16 +00:00
|
|
|
int64_t read_index, write_index;
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_bool_t in_prebuf;
|
2008-05-15 23:34:41 +00:00
|
|
|
pa_memchunk silence;
|
2006-02-20 04:05:16 +00:00
|
|
|
pa_mcalign *mcalign;
|
2007-10-28 19:13:50 +00:00
|
|
|
int64_t missing;
|
|
|
|
|
size_t requested;
|
2004-06-08 23:54:24 +00:00
|
|
|
};
|
|
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
pa_memblockq* pa_memblockq_new(
|
|
|
|
|
int64_t idx,
|
|
|
|
|
size_t maxlength,
|
|
|
|
|
size_t tlength,
|
|
|
|
|
size_t base,
|
|
|
|
|
size_t prebuf,
|
|
|
|
|
size_t minreq,
|
2008-05-15 23:34:41 +00:00
|
|
|
size_t maxrewind,
|
|
|
|
|
pa_memchunk *silence) {
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_memblockq* bq;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(base > 0);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
bq = pa_xnew(pa_memblockq, 1);
|
|
|
|
|
bq->blocks = bq->blocks_tail = NULL;
|
2008-05-15 23:34:41 +00:00
|
|
|
bq->current_read = bq->current_write = NULL;
|
2004-06-08 23:54:24 +00:00
|
|
|
bq->n_blocks = 0;
|
2004-07-07 00:22:46 +00:00
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
bq->base = base;
|
|
|
|
|
bq->read_index = bq->write_index = idx;
|
2004-07-07 00:22:46 +00:00
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
pa_log_debug("memblockq requested: maxlength=%lu, tlength=%lu, base=%lu, prebuf=%lu, minreq=%lu maxrewind=%lu",
|
|
|
|
|
(unsigned long) maxlength, (unsigned long) tlength, (unsigned long) base, (unsigned long) prebuf, (unsigned long) minreq, (unsigned long) maxrewind);
|
2004-07-07 00:22:46 +00:00
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
bq->missing = bq->requested = bq->maxlength = bq->tlength = bq->prebuf = bq->minreq = bq->maxrewind = 0;
|
2007-11-21 01:19:28 +00:00
|
|
|
bq->in_prebuf = TRUE;
|
2004-07-07 00:22:46 +00:00
|
|
|
|
2007-11-21 01:19:28 +00:00
|
|
|
pa_memblockq_set_maxlength(bq, maxlength);
|
|
|
|
|
pa_memblockq_set_tlength(bq, tlength);
|
|
|
|
|
pa_memblockq_set_prebuf(bq, prebuf);
|
|
|
|
|
pa_memblockq_set_minreq(bq, minreq);
|
2008-05-15 23:34:41 +00:00
|
|
|
pa_memblockq_set_maxrewind(bq, maxrewind);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
pa_log_debug("memblockq sanitized: maxlength=%lu, tlength=%lu, base=%lu, prebuf=%lu, minreq=%lu maxrewind=%lu",
|
|
|
|
|
(unsigned long) bq->maxlength, (unsigned long) bq->tlength, (unsigned long) bq->base, (unsigned long) bq->prebuf, (unsigned long) bq->minreq, (unsigned long) bq->maxrewind);
|
2004-08-22 21:13:58 +00:00
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
if (silence) {
|
|
|
|
|
bq->silence = *silence;
|
|
|
|
|
pa_memblock_ref(bq->silence.memblock);
|
|
|
|
|
} else
|
|
|
|
|
pa_memchunk_reset(&bq->silence);
|
|
|
|
|
|
|
|
|
|
bq->mcalign = pa_mcalign_new(bq->base);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-06-08 23:54:24 +00:00
|
|
|
return bq;
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
void pa_memblockq_free(pa_memblockq* bq) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(bq);
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2008-06-26 00:33:07 +02:00
|
|
|
pa_memblockq_silence(bq);
|
2006-02-20 04:05:16 +00:00
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
if (bq->silence.memblock)
|
|
|
|
|
pa_memblock_unref(bq->silence.memblock);
|
2006-02-20 04:05:16 +00:00
|
|
|
|
2004-07-03 00:19:17 +00:00
|
|
|
if (bq->mcalign)
|
2004-07-03 23:35:12 +00:00
|
|
|
pa_mcalign_free(bq->mcalign);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-08-04 16:39:30 +00:00
|
|
|
pa_xfree(bq);
|
2004-06-08 23:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
static void fix_current_read(pa_memblockq *bq) {
|
|
|
|
|
pa_assert(bq);
|
|
|
|
|
|
|
|
|
|
if (PA_UNLIKELY(!bq->blocks)) {
|
|
|
|
|
bq->current_read = NULL;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (PA_UNLIKELY(!bq->current_read))
|
|
|
|
|
bq->current_read = bq->blocks;
|
|
|
|
|
|
|
|
|
|
/* Scan left */
|
|
|
|
|
while (PA_UNLIKELY(bq->current_read->index > bq->read_index))
|
|
|
|
|
|
|
|
|
|
if (bq->current_read->prev)
|
|
|
|
|
bq->current_read = bq->current_read->prev;
|
|
|
|
|
else
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
/* Scan right */
|
|
|
|
|
while (PA_LIKELY(bq->current_read != NULL) && PA_UNLIKELY(bq->current_read->index + (int64_t) bq->current_read->chunk.length <= bq->read_index))
|
|
|
|
|
bq->current_read = bq->current_read->next;
|
|
|
|
|
|
|
|
|
|
/* At this point current_read will either point at or left of the
|
|
|
|
|
next block to play. It may be NULL in case everything in
|
|
|
|
|
the queue was already played */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void fix_current_write(pa_memblockq *bq) {
|
|
|
|
|
pa_assert(bq);
|
|
|
|
|
|
|
|
|
|
if (PA_UNLIKELY(!bq->blocks)) {
|
|
|
|
|
bq->current_write = NULL;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (PA_UNLIKELY(!bq->current_write))
|
|
|
|
|
bq->current_write = bq->blocks_tail;
|
|
|
|
|
|
|
|
|
|
/* Scan right */
|
|
|
|
|
while (PA_UNLIKELY(bq->current_write->index + (int64_t) bq->current_write->chunk.length <= bq->write_index))
|
|
|
|
|
|
|
|
|
|
if (bq->current_write->next)
|
|
|
|
|
bq->current_write = bq->current_write->next;
|
|
|
|
|
else
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
/* Scan left */
|
|
|
|
|
while (PA_LIKELY(bq->current_write != NULL) && PA_UNLIKELY(bq->current_write->index > bq->write_index))
|
|
|
|
|
bq->current_write = bq->current_write->prev;
|
|
|
|
|
|
|
|
|
|
/* At this point current_write will either point at or right of
|
|
|
|
|
the next block to write data to. It may be NULL in case
|
|
|
|
|
everything in the queue is still to be played */
|
|
|
|
|
}
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
static void drop_block(pa_memblockq *bq, struct list_item *q) {
|
|
|
|
|
pa_assert(bq);
|
|
|
|
|
pa_assert(q);
|
2004-07-10 19:23:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(bq->n_blocks >= 1);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
if (q->prev)
|
|
|
|
|
q->prev->next = q->next;
|
2008-05-15 23:34:41 +00:00
|
|
|
else {
|
|
|
|
|
pa_assert(bq->blocks == q);
|
2006-02-20 04:05:16 +00:00
|
|
|
bq->blocks = q->next;
|
2008-05-15 23:34:41 +00:00
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
if (q->next)
|
|
|
|
|
q->next->prev = q->prev;
|
2008-05-15 23:34:41 +00:00
|
|
|
else {
|
|
|
|
|
pa_assert(bq->blocks_tail == q);
|
2006-02-20 04:05:16 +00:00
|
|
|
bq->blocks_tail = q->prev;
|
2008-05-15 23:34:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (bq->current_write == q)
|
|
|
|
|
bq->current_write = q->prev;
|
|
|
|
|
|
|
|
|
|
if (bq->current_read == q)
|
|
|
|
|
bq->current_read = q->next;
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
pa_memblock_unref(q->chunk.memblock);
|
2007-10-28 19:13:50 +00:00
|
|
|
|
|
|
|
|
if (pa_flist_push(PA_STATIC_FLIST_GET(list_items), q) < 0)
|
|
|
|
|
pa_xfree(q);
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
bq->n_blocks--;
|
2004-06-08 23:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
static void drop_backlog(pa_memblockq *bq) {
|
|
|
|
|
int64_t boundary;
|
|
|
|
|
pa_assert(bq);
|
|
|
|
|
|
|
|
|
|
boundary = bq->read_index - bq->maxrewind;
|
|
|
|
|
|
|
|
|
|
while (bq->blocks && (bq->blocks->index + (int64_t) bq->blocks->chunk.length <= boundary))
|
|
|
|
|
drop_block(bq, bq->blocks);
|
|
|
|
|
}
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
static pa_bool_t can_push(pa_memblockq *bq, size_t l) {
|
2006-02-20 04:05:16 +00:00
|
|
|
int64_t end;
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(bq);
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
if (bq->read_index > bq->write_index) {
|
2006-02-27 08:58:29 +00:00
|
|
|
size_t d = bq->read_index - bq->write_index;
|
2004-06-15 00:29:01 +00:00
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
if (l > d)
|
|
|
|
|
l -= d;
|
|
|
|
|
else
|
2007-10-28 19:13:50 +00:00
|
|
|
return TRUE;
|
2006-02-20 04:05:16 +00:00
|
|
|
}
|
2004-07-03 00:19:17 +00:00
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
end = bq->blocks_tail ? bq->blocks_tail->index + (int64_t) bq->blocks_tail->chunk.length : bq->write_index;
|
2006-02-20 04:05:16 +00:00
|
|
|
|
|
|
|
|
/* Make sure that the list doesn't get too long */
|
2008-05-15 23:34:41 +00:00
|
|
|
if (bq->write_index + (int64_t) l > end)
|
2006-02-20 04:05:16 +00:00
|
|
|
if (bq->write_index + l - bq->read_index > bq->maxlength)
|
2007-10-28 19:13:50 +00:00
|
|
|
return FALSE;
|
2006-02-20 04:05:16 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
return TRUE;
|
2004-06-08 23:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
int pa_memblockq_push(pa_memblockq* bq, const pa_memchunk *uchunk) {
|
2007-10-28 19:13:50 +00:00
|
|
|
struct list_item *q, *n;
|
2006-02-20 04:05:16 +00:00
|
|
|
pa_memchunk chunk;
|
2007-10-28 19:13:50 +00:00
|
|
|
int64_t old, delta;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(bq);
|
|
|
|
|
pa_assert(uchunk);
|
|
|
|
|
pa_assert(uchunk->memblock);
|
|
|
|
|
pa_assert(uchunk->length > 0);
|
|
|
|
|
pa_assert(uchunk->index + uchunk->length <= pa_memblock_get_length(uchunk->memblock));
|
2006-02-20 04:05:16 +00:00
|
|
|
|
|
|
|
|
if (uchunk->length % bq->base)
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
if (!can_push(bq, uchunk->length))
|
|
|
|
|
return -1;
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
old = bq->write_index;
|
2006-02-20 04:05:16 +00:00
|
|
|
chunk = *uchunk;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
fix_current_write(bq);
|
|
|
|
|
q = bq->current_write;
|
2004-06-16 00:05:30 +00:00
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
/* First we advance the q pointer right of where we want to
|
|
|
|
|
* write to */
|
2006-02-20 04:05:16 +00:00
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
if (q) {
|
|
|
|
|
while (bq->write_index + (int64_t) chunk.length > q->index)
|
|
|
|
|
if (q->next)
|
|
|
|
|
q = q->next;
|
|
|
|
|
else
|
|
|
|
|
break;
|
2004-08-22 21:13:58 +00:00
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
if (!q)
|
|
|
|
|
q = bq->blocks_tail;
|
|
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
/* We go from back to front to look for the right place to add
|
|
|
|
|
* this new entry. Drop data we will overwrite on the way */
|
|
|
|
|
|
|
|
|
|
while (q) {
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
if (bq->write_index >= q->index + (int64_t) q->chunk.length)
|
2006-02-20 04:05:16 +00:00
|
|
|
/* We found the entry where we need to place the new entry immediately after */
|
|
|
|
|
break;
|
2007-10-28 19:13:50 +00:00
|
|
|
else if (bq->write_index + (int64_t) chunk.length <= q->index) {
|
2006-02-20 04:05:16 +00:00
|
|
|
/* This entry isn't touched at all, let's skip it */
|
|
|
|
|
q = q->prev;
|
|
|
|
|
} else if (bq->write_index <= q->index &&
|
|
|
|
|
bq->write_index + chunk.length >= q->index + q->chunk.length) {
|
|
|
|
|
|
|
|
|
|
/* This entry is fully replaced by the new entry, so let's drop it */
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
struct list_item *p;
|
2006-02-20 04:05:16 +00:00
|
|
|
p = q;
|
|
|
|
|
q = q->prev;
|
|
|
|
|
drop_block(bq, p);
|
|
|
|
|
} else if (bq->write_index >= q->index) {
|
|
|
|
|
/* The write index points into this memblock, so let's
|
|
|
|
|
* truncate or split it */
|
|
|
|
|
|
|
|
|
|
if (bq->write_index + chunk.length < q->index + q->chunk.length) {
|
|
|
|
|
|
|
|
|
|
/* We need to save the end of this memchunk */
|
2007-10-28 19:13:50 +00:00
|
|
|
struct list_item *p;
|
2006-02-20 04:05:16 +00:00
|
|
|
size_t d;
|
|
|
|
|
|
|
|
|
|
/* Create a new list entry for the end of thie memchunk */
|
2007-10-28 19:13:50 +00:00
|
|
|
if (!(p = pa_flist_pop(PA_STATIC_FLIST_GET(list_items))))
|
|
|
|
|
p = pa_xnew(struct list_item, 1);
|
|
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
p->chunk = q->chunk;
|
|
|
|
|
pa_memblock_ref(p->chunk.memblock);
|
|
|
|
|
|
|
|
|
|
/* Calculate offset */
|
|
|
|
|
d = bq->write_index + chunk.length - q->index;
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(d > 0);
|
2006-02-20 04:05:16 +00:00
|
|
|
|
|
|
|
|
/* Drop it from the new entry */
|
|
|
|
|
p->index = q->index + d;
|
|
|
|
|
p->chunk.length -= d;
|
|
|
|
|
|
|
|
|
|
/* Add it to the list */
|
|
|
|
|
p->prev = q;
|
|
|
|
|
if ((p->next = q->next))
|
|
|
|
|
q->next->prev = p;
|
|
|
|
|
else
|
|
|
|
|
bq->blocks_tail = p;
|
|
|
|
|
q->next = p;
|
|
|
|
|
|
|
|
|
|
bq->n_blocks++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Truncate the chunk */
|
|
|
|
|
if (!(q->chunk.length = bq->write_index - q->index)) {
|
2007-10-28 19:13:50 +00:00
|
|
|
struct list_item *p;
|
2006-02-20 04:05:16 +00:00
|
|
|
p = q;
|
|
|
|
|
q = q->prev;
|
|
|
|
|
drop_block(bq, p);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* We had to truncate this block, hence we're now at the right position */
|
|
|
|
|
break;
|
|
|
|
|
} else {
|
|
|
|
|
size_t d;
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(bq->write_index + (int64_t)chunk.length > q->index &&
|
2006-02-27 08:58:29 +00:00
|
|
|
bq->write_index + (int64_t)chunk.length < q->index + (int64_t)q->chunk.length &&
|
2006-02-20 04:05:16 +00:00
|
|
|
bq->write_index < q->index);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
/* The job overwrites the current entry at the end, so let's drop the beginning of this entry */
|
|
|
|
|
|
|
|
|
|
d = bq->write_index + chunk.length - q->index;
|
|
|
|
|
q->index += d;
|
|
|
|
|
q->chunk.index += d;
|
|
|
|
|
q->chunk.length -= d;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
q = q->prev;
|
|
|
|
|
}
|
2004-08-22 21:13:58 +00:00
|
|
|
}
|
2006-02-20 04:05:16 +00:00
|
|
|
|
|
|
|
|
if (q) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(bq->write_index >= q->index + (int64_t)q->chunk.length);
|
|
|
|
|
pa_assert(!q->next || (bq->write_index + (int64_t)chunk.length <= q->next->index));
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
/* Try to merge memory blocks */
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
if (q->chunk.memblock == chunk.memblock &&
|
2006-02-27 08:58:29 +00:00
|
|
|
q->chunk.index + (int64_t)q->chunk.length == chunk.index &&
|
|
|
|
|
bq->write_index == q->index + (int64_t)q->chunk.length) {
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
q->chunk.length += chunk.length;
|
|
|
|
|
bq->write_index += chunk.length;
|
2007-10-28 19:13:50 +00:00
|
|
|
goto finish;
|
2006-02-20 04:05:16 +00:00
|
|
|
}
|
|
|
|
|
} else
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(!bq->blocks || (bq->write_index + (int64_t)chunk.length <= bq->blocks->index));
|
2006-02-20 04:05:16 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
if (!(n = pa_flist_pop(PA_STATIC_FLIST_GET(list_items))))
|
|
|
|
|
n = pa_xnew(struct list_item, 1);
|
2006-02-20 04:05:16 +00:00
|
|
|
|
|
|
|
|
n->chunk = chunk;
|
|
|
|
|
pa_memblock_ref(n->chunk.memblock);
|
|
|
|
|
n->index = bq->write_index;
|
|
|
|
|
bq->write_index += n->chunk.length;
|
|
|
|
|
|
|
|
|
|
n->next = q ? q->next : bq->blocks;
|
|
|
|
|
n->prev = q;
|
|
|
|
|
|
|
|
|
|
if (n->next)
|
|
|
|
|
n->next->prev = n;
|
|
|
|
|
else
|
|
|
|
|
bq->blocks_tail = n;
|
|
|
|
|
|
|
|
|
|
if (n->prev)
|
|
|
|
|
n->prev->next = n;
|
|
|
|
|
else
|
|
|
|
|
bq->blocks = n;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
bq->n_blocks++;
|
2007-10-28 19:13:50 +00:00
|
|
|
|
|
|
|
|
finish:
|
|
|
|
|
|
|
|
|
|
delta = bq->write_index - old;
|
|
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
if (delta >= (int64_t) bq->requested) {
|
2007-10-28 19:13:50 +00:00
|
|
|
delta -= bq->requested;
|
|
|
|
|
bq->requested = 0;
|
|
|
|
|
} else {
|
|
|
|
|
bq->requested -= delta;
|
|
|
|
|
delta = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bq->missing -= delta;
|
|
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
return 0;
|
2004-06-16 00:05:30 +00:00
|
|
|
}
|
|
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
pa_bool_t pa_memblockq_prebuf_active(pa_memblockq *bq) {
|
|
|
|
|
pa_assert(bq);
|
|
|
|
|
|
|
|
|
|
if (bq->in_prebuf)
|
|
|
|
|
return pa_memblockq_get_length(bq) < bq->prebuf;
|
|
|
|
|
else
|
|
|
|
|
return bq->prebuf > 0 && bq->read_index >= bq->write_index;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static pa_bool_t update_prebuf(pa_memblockq *bq) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(bq);
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
if (bq->in_prebuf) {
|
2004-06-16 00:05:30 +00:00
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
if (pa_memblockq_get_length(bq) < bq->prebuf)
|
2007-10-28 19:13:50 +00:00
|
|
|
return TRUE;
|
2006-02-20 04:05:16 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
bq->in_prebuf = FALSE;
|
|
|
|
|
return FALSE;
|
|
|
|
|
} else {
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
if (bq->prebuf > 0 && bq->read_index >= bq->write_index) {
|
|
|
|
|
bq->in_prebuf = TRUE;
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
2006-02-20 04:05:16 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
return FALSE;
|
2004-06-08 23:54:24 +00:00
|
|
|
}
|
2007-10-28 19:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int pa_memblockq_peek(pa_memblockq* bq, pa_memchunk *chunk) {
|
2008-05-15 23:34:41 +00:00
|
|
|
int64_t d;
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(bq);
|
|
|
|
|
pa_assert(chunk);
|
|
|
|
|
|
|
|
|
|
/* We need to pre-buffer */
|
2008-05-15 23:34:41 +00:00
|
|
|
if (update_prebuf(bq))
|
2007-10-28 19:13:50 +00:00
|
|
|
return -1;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
fix_current_read(bq);
|
|
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
/* Do we need to spit out silence? */
|
2008-05-15 23:34:41 +00:00
|
|
|
if (!bq->current_read || bq->current_read->index > bq->read_index) {
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
size_t length;
|
|
|
|
|
|
|
|
|
|
/* How much silence shall we return? */
|
2008-05-15 23:34:41 +00:00
|
|
|
if (bq->current_read)
|
|
|
|
|
length = bq->current_read->index - bq->read_index;
|
|
|
|
|
else if (bq->write_index > bq->read_index)
|
|
|
|
|
length = (size_t) (bq->write_index - bq->read_index);
|
|
|
|
|
else
|
|
|
|
|
length = 0;
|
2006-02-20 04:05:16 +00:00
|
|
|
|
|
|
|
|
/* We need to return silence, since no data is yet available */
|
2008-05-15 23:34:41 +00:00
|
|
|
if (bq->silence.memblock) {
|
|
|
|
|
*chunk = bq->silence;
|
|
|
|
|
pa_memblock_ref(chunk->memblock);
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
if (length > 0 && length < chunk->length)
|
|
|
|
|
chunk->length = length;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
} else {
|
2006-07-29 15:03:26 +00:00
|
|
|
|
|
|
|
|
/* If the memblockq is empty, return -1, otherwise return
|
|
|
|
|
* the time to sleep */
|
2008-05-15 23:34:41 +00:00
|
|
|
if (length <= 0)
|
2006-07-29 15:03:26 +00:00
|
|
|
return -1;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
chunk->memblock = NULL;
|
|
|
|
|
chunk->length = length;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
chunk->index = 0;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
/* Ok, let's pass real data to the caller */
|
2008-05-15 23:34:41 +00:00
|
|
|
*chunk = bq->current_read->chunk;
|
2006-02-20 04:05:16 +00:00
|
|
|
pa_memblock_ref(chunk->memblock);
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
pa_assert(bq->read_index >= bq->current_read->index);
|
|
|
|
|
d = bq->read_index - bq->current_read->index;
|
|
|
|
|
chunk->index += d;
|
|
|
|
|
chunk->length -= d;
|
|
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
return 0;
|
2004-06-08 23:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
void pa_memblockq_drop(pa_memblockq *bq, size_t length) {
|
|
|
|
|
int64_t old, delta;
|
|
|
|
|
pa_assert(bq);
|
|
|
|
|
pa_assert(length % bq->base == 0);
|
2006-02-20 04:05:16 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
old = bq->read_index;
|
2006-02-20 04:05:16 +00:00
|
|
|
|
|
|
|
|
while (length > 0) {
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
/* Do not drop any data when we are in prebuffering mode */
|
2008-05-15 23:34:41 +00:00
|
|
|
if (update_prebuf(bq))
|
2007-10-28 19:13:50 +00:00
|
|
|
break;
|
|
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
fix_current_read(bq);
|
2006-02-20 04:05:16 +00:00
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
if (bq->current_read) {
|
|
|
|
|
int64_t p, d;
|
2006-02-20 04:05:16 +00:00
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
/* We go through this piece by piece to make sure we don't
|
|
|
|
|
* drop more than allowed by prebuf */
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
p = bq->current_read->index + bq->current_read->chunk.length;
|
|
|
|
|
pa_assert(p >= bq->read_index);
|
|
|
|
|
d = p - bq->read_index;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
if (d > (int64_t) length)
|
|
|
|
|
d = length;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
bq->read_index += d;
|
|
|
|
|
length -= d;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
/* The list is empty, there's nothing we could drop */
|
|
|
|
|
bq->read_index += length;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-10-28 19:13:50 +00:00
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
drop_backlog(bq);
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
delta = bq->read_index - old;
|
|
|
|
|
bq->missing += delta;
|
2004-06-08 23:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
void pa_memblockq_rewind(pa_memblockq *bq, size_t length) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(bq);
|
2008-05-15 23:34:41 +00:00
|
|
|
pa_assert(length % bq->base == 0);
|
2004-06-15 00:29:01 +00:00
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
/* This is kind of the inverse of pa_memblockq_drop() */
|
|
|
|
|
|
|
|
|
|
bq->read_index -= length;
|
|
|
|
|
bq->missing -= length;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pa_bool_t pa_memblockq_is_readable(pa_memblockq *bq) {
|
|
|
|
|
pa_assert(bq);
|
|
|
|
|
|
|
|
|
|
if (pa_memblockq_prebuf_active(bq))
|
|
|
|
|
return FALSE;
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
if (pa_memblockq_get_length(bq) <= 0)
|
2008-05-15 23:34:41 +00:00
|
|
|
return FALSE;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
return TRUE;
|
2004-06-08 23:54:24 +00:00
|
|
|
}
|
2004-06-16 00:05:30 +00:00
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
size_t pa_memblockq_get_length(pa_memblockq *bq) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(bq);
|
2006-02-20 04:05:16 +00:00
|
|
|
|
|
|
|
|
if (bq->write_index <= bq->read_index)
|
|
|
|
|
return 0;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
return (size_t) (bq->write_index - bq->read_index);
|
2004-06-16 00:05:30 +00:00
|
|
|
}
|
2004-06-23 23:17:30 +00:00
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
size_t pa_memblockq_missing(pa_memblockq *bq) {
|
2004-07-07 00:22:46 +00:00
|
|
|
size_t l;
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(bq);
|
2004-06-23 23:17:30 +00:00
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
if ((l = pa_memblockq_get_length(bq)) >= bq->tlength)
|
2004-06-23 23:17:30 +00:00
|
|
|
return 0;
|
|
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
l = bq->tlength - l;
|
2007-10-28 19:13:50 +00:00
|
|
|
|
|
|
|
|
return l >= bq->minreq ? l : 0;
|
2004-06-23 23:17:30 +00:00
|
|
|
}
|
2004-07-03 00:19:17 +00:00
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
void pa_memblockq_seek(pa_memblockq *bq, int64_t offset, pa_seek_mode_t seek) {
|
2007-10-28 19:13:50 +00:00
|
|
|
int64_t old, delta;
|
|
|
|
|
pa_assert(bq);
|
|
|
|
|
|
|
|
|
|
old = bq->write_index;
|
2006-02-20 04:05:16 +00:00
|
|
|
|
|
|
|
|
switch (seek) {
|
|
|
|
|
case PA_SEEK_RELATIVE:
|
|
|
|
|
bq->write_index += offset;
|
2007-10-28 19:13:50 +00:00
|
|
|
break;
|
2006-02-20 04:05:16 +00:00
|
|
|
case PA_SEEK_ABSOLUTE:
|
|
|
|
|
bq->write_index = offset;
|
2007-10-28 19:13:50 +00:00
|
|
|
break;
|
2006-02-20 04:05:16 +00:00
|
|
|
case PA_SEEK_RELATIVE_ON_READ:
|
|
|
|
|
bq->write_index = bq->read_index + offset;
|
2007-10-28 19:13:50 +00:00
|
|
|
break;
|
2006-02-20 04:05:16 +00:00
|
|
|
case PA_SEEK_RELATIVE_END:
|
2007-10-28 19:13:50 +00:00
|
|
|
bq->write_index = (bq->blocks_tail ? bq->blocks_tail->index + (int64_t) bq->blocks_tail->chunk.length : bq->read_index) + offset;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
pa_assert_not_reached();
|
2004-07-03 00:19:17 +00:00
|
|
|
}
|
2006-02-20 04:05:16 +00:00
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
drop_backlog(bq);
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
delta = bq->write_index - old;
|
|
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
if (delta >= (int64_t) bq->requested) {
|
2007-10-28 19:13:50 +00:00
|
|
|
delta -= bq->requested;
|
|
|
|
|
bq->requested = 0;
|
|
|
|
|
} else if (delta >= 0) {
|
|
|
|
|
bq->requested -= delta;
|
|
|
|
|
delta = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bq->missing -= delta;
|
2006-02-20 04:05:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void pa_memblockq_flush(pa_memblockq *bq) {
|
2007-10-28 19:13:50 +00:00
|
|
|
int64_t old, delta;
|
|
|
|
|
pa_assert(bq);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
pa_memblockq_silence(bq);
|
2006-02-20 04:05:16 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
old = bq->write_index;
|
2006-04-12 17:09:51 +00:00
|
|
|
bq->write_index = bq->read_index;
|
|
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
pa_memblockq_prebuf_force(bq);
|
2007-10-28 19:13:50 +00:00
|
|
|
|
|
|
|
|
delta = bq->write_index - old;
|
|
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
if (delta >= (int64_t) bq->requested) {
|
2007-10-28 19:13:50 +00:00
|
|
|
delta -= bq->requested;
|
|
|
|
|
bq->requested = 0;
|
|
|
|
|
} else if (delta >= 0) {
|
|
|
|
|
bq->requested -= delta;
|
|
|
|
|
delta = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bq->missing -= delta;
|
2004-07-03 00:19:17 +00:00
|
|
|
}
|
2004-07-07 00:22:46 +00:00
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
size_t pa_memblockq_get_tlength(pa_memblockq *bq) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(bq);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
return bq->tlength;
|
2004-07-07 00:22:46 +00:00
|
|
|
}
|
2004-08-17 18:53:42 +00:00
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
size_t pa_memblockq_get_minreq(pa_memblockq *bq) {
|
|
|
|
|
pa_assert(bq);
|
|
|
|
|
|
|
|
|
|
return bq->minreq;
|
|
|
|
|
}
|
|
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
int64_t pa_memblockq_get_read_index(pa_memblockq *bq) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(bq);
|
2008-05-15 23:34:41 +00:00
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
return bq->read_index;
|
2004-08-17 18:53:42 +00:00
|
|
|
}
|
2004-08-22 21:13:58 +00:00
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
int64_t pa_memblockq_get_write_index(pa_memblockq *bq) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(bq);
|
2008-05-15 23:34:41 +00:00
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
return bq->write_index;
|
2004-09-26 17:02:26 +00:00
|
|
|
}
|
|
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
int pa_memblockq_push_align(pa_memblockq* bq, const pa_memchunk *chunk) {
|
|
|
|
|
pa_memchunk rchunk;
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(bq);
|
|
|
|
|
pa_assert(chunk);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
if (bq->base == 1)
|
|
|
|
|
return pa_memblockq_push(bq, chunk);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
if (!can_push(bq, pa_mcalign_csize(bq->mcalign, chunk->length)))
|
|
|
|
|
return -1;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
pa_mcalign_push(bq->mcalign, chunk);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
while (pa_mcalign_pop(bq->mcalign, &rchunk) >= 0) {
|
|
|
|
|
int r;
|
|
|
|
|
r = pa_memblockq_push(bq, &rchunk);
|
|
|
|
|
pa_memblock_unref(rchunk.memblock);
|
2004-08-22 21:13:58 +00:00
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
if (r < 0) {
|
|
|
|
|
pa_mcalign_flush(bq->mcalign);
|
2006-02-20 04:05:16 +00:00
|
|
|
return -1;
|
2008-05-15 23:34:41 +00:00
|
|
|
}
|
2006-02-20 04:05:16 +00:00
|
|
|
}
|
2004-08-22 21:13:58 +00:00
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
2004-08-22 21:13:58 +00:00
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
void pa_memblockq_prebuf_disable(pa_memblockq *bq) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(bq);
|
2004-08-22 21:13:58 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
bq->in_prebuf = FALSE;
|
2004-08-22 21:13:58 +00:00
|
|
|
}
|
2004-09-01 00:23:51 +00:00
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
void pa_memblockq_prebuf_force(pa_memblockq *bq) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(bq);
|
2006-02-20 04:05:16 +00:00
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
if (bq->prebuf > 0)
|
2007-10-28 19:13:50 +00:00
|
|
|
bq->in_prebuf = TRUE;
|
2004-09-01 00:23:51 +00:00
|
|
|
}
|
2006-05-25 23:20:28 +00:00
|
|
|
|
|
|
|
|
size_t pa_memblockq_get_maxlength(pa_memblockq *bq) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(bq);
|
2006-05-25 23:20:28 +00:00
|
|
|
|
|
|
|
|
return bq->maxlength;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t pa_memblockq_get_prebuf(pa_memblockq *bq) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(bq);
|
2006-05-25 23:20:28 +00:00
|
|
|
|
|
|
|
|
return bq->prebuf;
|
|
|
|
|
}
|
2007-10-28 19:13:50 +00:00
|
|
|
|
|
|
|
|
size_t pa_memblockq_pop_missing(pa_memblockq *bq) {
|
|
|
|
|
size_t l;
|
|
|
|
|
|
|
|
|
|
pa_assert(bq);
|
|
|
|
|
|
|
|
|
|
/* pa_log("pop: %lli", bq->missing); */
|
|
|
|
|
|
|
|
|
|
if (bq->missing <= 0)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
l = (size_t) bq->missing;
|
|
|
|
|
bq->missing = 0;
|
|
|
|
|
bq->requested += l;
|
|
|
|
|
|
|
|
|
|
return l;
|
|
|
|
|
}
|
2007-11-21 01:19:28 +00:00
|
|
|
|
|
|
|
|
void pa_memblockq_set_maxlength(pa_memblockq *bq, size_t maxlength) {
|
|
|
|
|
pa_assert(bq);
|
|
|
|
|
|
|
|
|
|
bq->maxlength = ((maxlength+bq->base-1)/bq->base)*bq->base;
|
|
|
|
|
|
|
|
|
|
if (bq->maxlength < bq->base)
|
|
|
|
|
bq->maxlength = bq->base;
|
|
|
|
|
|
|
|
|
|
if (bq->tlength > bq->maxlength)
|
|
|
|
|
pa_memblockq_set_tlength(bq, bq->maxlength);
|
|
|
|
|
|
|
|
|
|
if (bq->prebuf > bq->maxlength)
|
|
|
|
|
pa_memblockq_set_prebuf(bq, bq->maxlength);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void pa_memblockq_set_tlength(pa_memblockq *bq, size_t tlength) {
|
|
|
|
|
size_t old_tlength;
|
|
|
|
|
pa_assert(bq);
|
|
|
|
|
|
|
|
|
|
if (tlength <= 0)
|
|
|
|
|
tlength = bq->maxlength;
|
|
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
old_tlength = bq->tlength;
|
2007-11-21 01:19:28 +00:00
|
|
|
bq->tlength = ((tlength+bq->base-1)/bq->base)*bq->base;
|
|
|
|
|
|
|
|
|
|
if (bq->tlength > bq->maxlength)
|
|
|
|
|
bq->tlength = bq->maxlength;
|
|
|
|
|
|
2008-06-17 20:07:51 +00:00
|
|
|
if (bq->prebuf > bq->tlength)
|
|
|
|
|
pa_memblockq_set_prebuf(bq, bq->tlength);
|
|
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
if (bq->minreq > bq->tlength)
|
|
|
|
|
pa_memblockq_set_minreq(bq, bq->tlength);
|
2007-11-21 01:19:28 +00:00
|
|
|
|
|
|
|
|
bq->missing += (int64_t) bq->tlength - (int64_t) old_tlength;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void pa_memblockq_set_prebuf(pa_memblockq *bq, size_t prebuf) {
|
|
|
|
|
pa_assert(bq);
|
|
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
if (prebuf == (size_t) -1)
|
|
|
|
|
prebuf = bq->tlength;
|
|
|
|
|
|
|
|
|
|
bq->prebuf = ((prebuf+bq->base-1)/bq->base)*bq->base;
|
2007-11-21 01:19:28 +00:00
|
|
|
|
|
|
|
|
if (prebuf > 0 && bq->prebuf < bq->base)
|
|
|
|
|
bq->prebuf = bq->base;
|
|
|
|
|
|
2008-06-17 20:07:51 +00:00
|
|
|
if (bq->prebuf > bq->tlength)
|
|
|
|
|
bq->prebuf = bq->tlength;
|
2007-11-21 01:19:28 +00:00
|
|
|
|
|
|
|
|
if (bq->prebuf <= 0 || pa_memblockq_get_length(bq) >= bq->prebuf)
|
|
|
|
|
bq->in_prebuf = FALSE;
|
|
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
if (bq->minreq > bq->prebuf)
|
|
|
|
|
pa_memblockq_set_minreq(bq, bq->prebuf);
|
2007-11-21 01:19:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void pa_memblockq_set_minreq(pa_memblockq *bq, size_t minreq) {
|
|
|
|
|
pa_assert(bq);
|
|
|
|
|
|
|
|
|
|
bq->minreq = (minreq/bq->base)*bq->base;
|
|
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
if (bq->minreq > bq->tlength)
|
|
|
|
|
bq->minreq = bq->tlength;
|
|
|
|
|
|
|
|
|
|
if (bq->minreq > bq->prebuf)
|
|
|
|
|
bq->minreq = bq->prebuf;
|
2007-11-21 01:19:28 +00:00
|
|
|
|
|
|
|
|
if (bq->minreq < bq->base)
|
|
|
|
|
bq->minreq = bq->base;
|
|
|
|
|
}
|
2008-05-15 23:34:41 +00:00
|
|
|
|
|
|
|
|
void pa_memblockq_set_maxrewind(pa_memblockq *bq, size_t maxrewind) {
|
|
|
|
|
pa_assert(bq);
|
|
|
|
|
|
|
|
|
|
bq->maxrewind = (maxrewind/bq->base)*bq->base;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int pa_memblockq_splice(pa_memblockq *bq, pa_memblockq *source) {
|
|
|
|
|
|
|
|
|
|
pa_assert(bq);
|
|
|
|
|
pa_assert(source);
|
|
|
|
|
|
|
|
|
|
pa_memblockq_prebuf_disable(bq);
|
|
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
|
pa_memchunk chunk;
|
|
|
|
|
|
|
|
|
|
if (pa_memblockq_peek(source, &chunk) < 0)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
pa_assert(chunk.length > 0);
|
|
|
|
|
|
|
|
|
|
if (chunk.memblock) {
|
|
|
|
|
|
|
|
|
|
if (pa_memblockq_push_align(bq, &chunk) < 0) {
|
|
|
|
|
pa_memblock_unref(chunk.memblock);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pa_memblock_unref(chunk.memblock);
|
|
|
|
|
} else
|
|
|
|
|
pa_memblockq_seek(bq, chunk.length, PA_SEEK_RELATIVE);
|
|
|
|
|
|
|
|
|
|
pa_memblockq_drop(bq, chunk.length);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void pa_memblockq_willneed(pa_memblockq *bq) {
|
|
|
|
|
struct list_item *q;
|
|
|
|
|
|
|
|
|
|
pa_assert(bq);
|
|
|
|
|
|
|
|
|
|
fix_current_read(bq);
|
|
|
|
|
|
|
|
|
|
for (q = bq->current_read; q; q = q->next)
|
|
|
|
|
pa_memchunk_will_need(&q->chunk);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void pa_memblockq_set_silence(pa_memblockq *bq, pa_memchunk *silence) {
|
|
|
|
|
pa_assert(bq);
|
|
|
|
|
|
|
|
|
|
if (bq->silence.memblock)
|
|
|
|
|
pa_memblock_unref(bq->silence.memblock);
|
|
|
|
|
|
|
|
|
|
if (silence) {
|
|
|
|
|
bq->silence = *silence;
|
|
|
|
|
pa_memblock_ref(bq->silence.memblock);
|
|
|
|
|
} else
|
|
|
|
|
pa_memchunk_reset(&bq->silence);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pa_bool_t pa_memblockq_is_empty(pa_memblockq *bq) {
|
|
|
|
|
pa_assert(bq);
|
|
|
|
|
|
|
|
|
|
return !bq->blocks;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void pa_memblockq_silence(pa_memblockq *bq) {
|
|
|
|
|
pa_assert(bq);
|
|
|
|
|
|
|
|
|
|
while (bq->blocks)
|
|
|
|
|
drop_block(bq, bq->blocks);
|
|
|
|
|
|
|
|
|
|
pa_assert(bq->n_blocks == 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsigned pa_memblockq_get_nblocks(pa_memblockq *bq) {
|
|
|
|
|
pa_assert(bq);
|
|
|
|
|
|
|
|
|
|
return bq->n_blocks;
|
|
|
|
|
}
|
2008-06-16 19:01:09 +00:00
|
|
|
|
|
|
|
|
size_t pa_memblockq_get_base(pa_memblockq *bq) {
|
|
|
|
|
pa_assert(bq);
|
|
|
|
|
|
|
|
|
|
return bq->base;
|
|
|
|
|
}
|