summaryrefslogtreecommitdiff
path: root/src/pulsecore/memblockq.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/pulsecore/memblockq.c')
-rw-r--r--src/pulsecore/memblockq.c488
1 files changed, 341 insertions, 147 deletions
diff --git a/src/pulsecore/memblockq.c b/src/pulsecore/memblockq.c
index e719151..265da37 100644
--- a/src/pulsecore/memblockq.c
+++ b/src/pulsecore/memblockq.c
@@ -1,5 +1,3 @@
-/* $Id: memblockq.c 2063 2007-11-21 01:19:28Z lennart $ */
-
/***
This file is part of PulseAudio.
@@ -50,11 +48,12 @@ PA_STATIC_FLIST_DECLARE(list_items, 0, pa_xfree);
struct pa_memblockq {
struct list_item *blocks, *blocks_tail;
+ struct list_item *current_read, *current_write;
unsigned n_blocks;
- size_t maxlength, tlength, base, prebuf, minreq;
+ size_t maxlength, tlength, base, prebuf, minreq, maxrewind;
int64_t read_index, write_index;
pa_bool_t in_prebuf;
- pa_memblock *silence;
+ pa_memchunk silence;
pa_mcalign *mcalign;
int64_t missing;
size_t requested;
@@ -67,7 +66,8 @@ pa_memblockq* pa_memblockq_new(
size_t base,
size_t prebuf,
size_t minreq,
- pa_memblock *silence) {
+ size_t maxrewind,
+ pa_memchunk *silence) {
pa_memblockq* bq;
@@ -75,27 +75,35 @@ pa_memblockq* pa_memblockq_new(
bq = pa_xnew(pa_memblockq, 1);
bq->blocks = bq->blocks_tail = NULL;
+ bq->current_read = bq->current_write = NULL;
bq->n_blocks = 0;
bq->base = base;
bq->read_index = bq->write_index = idx;
- pa_log_debug("memblockq requested: maxlength=%lu, tlength=%lu, base=%lu, prebuf=%lu, minreq=%lu",
- (unsigned long) maxlength, (unsigned long) tlength, (unsigned long) base, (unsigned long) prebuf, (unsigned long) minreq);
+ 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);
- bq->missing = bq->requested = bq->maxlength = bq->tlength = bq->prebuf = bq->minreq = 0;
+ bq->missing = 0;
+ bq->requested = bq->maxlength = bq->tlength = bq->prebuf = bq->minreq = bq->maxrewind = 0;
bq->in_prebuf = TRUE;
pa_memblockq_set_maxlength(bq, maxlength);
pa_memblockq_set_tlength(bq, tlength);
pa_memblockq_set_prebuf(bq, prebuf);
pa_memblockq_set_minreq(bq, minreq);
+ pa_memblockq_set_maxrewind(bq, maxrewind);
+
+ 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);
- pa_log_debug("memblockq sanitized: maxlength=%lu, tlength=%lu, base=%lu, prebuf=%lu, minreq=%lu",
- (unsigned long)bq->maxlength, (unsigned long)bq->tlength, (unsigned long)bq->base, (unsigned long)bq->prebuf, (unsigned long)bq->minreq);
+ if (silence) {
+ bq->silence = *silence;
+ pa_memblock_ref(bq->silence.memblock);
+ } else
+ pa_memchunk_reset(&bq->silence);
- bq->silence = silence ? pa_memblock_ref(silence) : NULL;
- bq->mcalign = NULL;
+ bq->mcalign = pa_mcalign_new(bq->base);
return bq;
}
@@ -103,10 +111,10 @@ pa_memblockq* pa_memblockq_new(
void pa_memblockq_free(pa_memblockq* bq) {
pa_assert(bq);
- pa_memblockq_flush(bq);
+ pa_memblockq_silence(bq);
- if (bq->silence)
- pa_memblock_unref(bq->silence);
+ if (bq->silence.memblock)
+ pa_memblock_unref(bq->silence.memblock);
if (bq->mcalign)
pa_mcalign_free(bq->mcalign);
@@ -114,6 +122,62 @@ void pa_memblockq_free(pa_memblockq* bq) {
pa_xfree(bq);
}
+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 */
+}
+
static void drop_block(pa_memblockq *bq, struct list_item *q) {
pa_assert(bq);
pa_assert(q);
@@ -122,13 +186,23 @@ static void drop_block(pa_memblockq *bq, struct list_item *q) {
if (q->prev)
q->prev->next = q->next;
- else
+ else {
+ pa_assert(bq->blocks == q);
bq->blocks = q->next;
+ }
if (q->next)
q->next->prev = q->prev;
- else
+ else {
+ pa_assert(bq->blocks_tail == q);
bq->blocks_tail = q->prev;
+ }
+
+ if (bq->current_write == q)
+ bq->current_write = q->prev;
+
+ if (bq->current_read == q)
+ bq->current_read = q->next;
pa_memblock_unref(q->chunk.memblock);
@@ -138,25 +212,35 @@ static void drop_block(pa_memblockq *bq, struct list_item *q) {
bq->n_blocks--;
}
+static void drop_backlog(pa_memblockq *bq) {
+ int64_t boundary;
+ pa_assert(bq);
+
+ boundary = bq->read_index - (int64_t) bq->maxrewind;
+
+ while (bq->blocks && (bq->blocks->index + (int64_t) bq->blocks->chunk.length <= boundary))
+ drop_block(bq, bq->blocks);
+}
+
static pa_bool_t can_push(pa_memblockq *bq, size_t l) {
int64_t end;
pa_assert(bq);
if (bq->read_index > bq->write_index) {
- size_t d = bq->read_index - bq->write_index;
+ int64_t d = bq->read_index - bq->write_index;
- if (l > d)
- l -= d;
+ if ((int64_t) l > d)
+ l -= (size_t) d;
else
return TRUE;
}
- end = bq->blocks_tail ? bq->blocks_tail->index + bq->blocks_tail->chunk.length : 0;
+ end = bq->blocks_tail ? bq->blocks_tail->index + (int64_t) bq->blocks_tail->chunk.length : bq->write_index;
/* Make sure that the list doesn't get too long */
- if (bq->write_index + (int64_t)l > end)
- if (bq->write_index + l - bq->read_index > bq->maxlength)
+ if (bq->write_index + (int64_t) l > end)
+ if (bq->write_index + (int64_t) l - bq->read_index > (int64_t) bq->maxlength)
return FALSE;
return TRUE;
@@ -182,28 +266,26 @@ int pa_memblockq_push(pa_memblockq* bq, const pa_memchunk *uchunk) {
old = bq->write_index;
chunk = *uchunk;
- if (bq->read_index > bq->write_index) {
-
- /* We currently have a buffer underflow, we need to drop some
- * incoming data */
+ fix_current_write(bq);
+ q = bq->current_write;
- size_t d = bq->read_index - bq->write_index;
+ /* First we advance the q pointer right of where we want to
+ * write to */
- if (chunk.length > d) {
- chunk.index += d;
- chunk.length -= d;
- bq->write_index += d;
- } else {
- /* We drop the incoming data completely */
- bq->write_index += chunk.length;
- goto finish;
- }
+ if (q) {
+ while (bq->write_index + (int64_t) chunk.length > q->index)
+ if (q->next)
+ q = q->next;
+ else
+ break;
}
+ if (!q)
+ q = bq->blocks_tail;
+
/* 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 */
- q = bq->blocks_tail;
while (q) {
if (bq->write_index >= q->index + (int64_t) q->chunk.length)
@@ -213,7 +295,7 @@ int pa_memblockq_push(pa_memblockq* bq, const pa_memchunk *uchunk) {
/* 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) {
+ bq->write_index + (int64_t) chunk.length >= q->index + (int64_t) q->chunk.length) {
/* This entry is fully replaced by the new entry, so let's drop it */
@@ -225,7 +307,7 @@ int pa_memblockq_push(pa_memblockq* bq, const pa_memchunk *uchunk) {
/* 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) {
+ if (bq->write_index + (int64_t) chunk.length < q->index + (int64_t) q->chunk.length) {
/* We need to save the end of this memchunk */
struct list_item *p;
@@ -239,11 +321,11 @@ int pa_memblockq_push(pa_memblockq* bq, const pa_memchunk *uchunk) {
pa_memblock_ref(p->chunk.memblock);
/* Calculate offset */
- d = bq->write_index + chunk.length - q->index;
+ d = (size_t) (bq->write_index + (int64_t) chunk.length - q->index);
pa_assert(d > 0);
/* Drop it from the new entry */
- p->index = q->index + d;
+ p->index = q->index + (int64_t) d;
p->chunk.length -= d;
/* Add it to the list */
@@ -258,7 +340,7 @@ int pa_memblockq_push(pa_memblockq* bq, const pa_memchunk *uchunk) {
}
/* Truncate the chunk */
- if (!(q->chunk.length = bq->write_index - q->index)) {
+ if (!(q->chunk.length = (size_t) (bq->write_index - q->index))) {
struct list_item *p;
p = q;
q = q->prev;
@@ -276,8 +358,8 @@ int pa_memblockq_push(pa_memblockq* bq, const pa_memchunk *uchunk) {
/* 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;
+ d = (size_t) (bq->write_index + (int64_t) chunk.length - q->index);
+ q->index += (int64_t) d;
q->chunk.index += d;
q->chunk.length -= d;
@@ -292,11 +374,11 @@ int pa_memblockq_push(pa_memblockq* bq, const pa_memchunk *uchunk) {
/* Try to merge memory blocks */
if (q->chunk.memblock == chunk.memblock &&
- q->chunk.index + (int64_t)q->chunk.length == chunk.index &&
- bq->write_index == q->index + (int64_t)q->chunk.length) {
+ q->chunk.index + q->chunk.length == chunk.index &&
+ bq->write_index == q->index + (int64_t) q->chunk.length) {
q->chunk.length += chunk.length;
- bq->write_index += chunk.length;
+ bq->write_index += (int64_t) chunk.length;
goto finish;
}
} else
@@ -308,7 +390,7 @@ int pa_memblockq_push(pa_memblockq* bq, const pa_memchunk *uchunk) {
n->chunk = chunk;
pa_memblock_ref(n->chunk.memblock);
n->index = bq->write_index;
- bq->write_index += n->chunk.length;
+ bq->write_index += (int64_t) n->chunk.length;
n->next = q ? q->next : bq->blocks;
n->prev = q;
@@ -329,11 +411,11 @@ finish:
delta = bq->write_index - old;
- if (delta >= bq->requested) {
- delta -= bq->requested;
+ if (delta >= (int64_t) bq->requested) {
+ delta -= (int64_t) bq->requested;
bq->requested = 0;
} else {
- bq->requested -= delta;
+ bq->requested -= (size_t) delta;
delta = 0;
}
@@ -342,7 +424,16 @@ finish:
return 0;
}
-static pa_bool_t memblockq_check_prebuf(pa_memblockq *bq) {
+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) {
pa_assert(bq);
if (bq->in_prebuf) {
@@ -364,34 +455,42 @@ static pa_bool_t memblockq_check_prebuf(pa_memblockq *bq) {
}
int pa_memblockq_peek(pa_memblockq* bq, pa_memchunk *chunk) {
+ int64_t d;
pa_assert(bq);
pa_assert(chunk);
/* We need to pre-buffer */
- if (memblockq_check_prebuf(bq))
+ if (update_prebuf(bq))
return -1;
+ fix_current_read(bq);
+
/* Do we need to spit out silence? */
- if (!bq->blocks || bq->blocks->index > bq->read_index) {
+ if (!bq->current_read || bq->current_read->index > bq->read_index) {
size_t length;
/* How much silence shall we return? */
- length = bq->blocks ? bq->blocks->index - bq->read_index : 0;
+ if (bq->current_read)
+ length = (size_t) (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;
/* We need to return silence, since no data is yet available */
- if (bq->silence) {
- chunk->memblock = pa_memblock_ref(bq->silence);
+ if (bq->silence.memblock) {
+ *chunk = bq->silence;
+ pa_memblock_ref(chunk->memblock);
- if (!length || length > pa_memblock_get_length(chunk->memblock))
- length = pa_memblock_get_length(chunk->memblock);
+ if (length > 0 && length < chunk->length)
+ chunk->length = length;
- chunk->length = length;
} else {
/* If the memblockq is empty, return -1, otherwise return
* the time to sleep */
- if (!bq->blocks)
+ if (length <= 0)
return -1;
chunk->memblock = NULL;
@@ -403,11 +502,14 @@ int pa_memblockq_peek(pa_memblockq* bq, pa_memchunk *chunk) {
}
/* Ok, let's pass real data to the caller */
- pa_assert(bq->blocks->index == bq->read_index);
-
- *chunk = bq->blocks->chunk;
+ *chunk = bq->current_read->chunk;
pa_memblock_ref(chunk->memblock);
+ pa_assert(bq->read_index >= bq->current_read->index);
+ d = bq->read_index - bq->current_read->index;
+ chunk->index += (size_t) d;
+ chunk->length -= (size_t) d;
+
return 0;
}
@@ -421,68 +523,61 @@ void pa_memblockq_drop(pa_memblockq *bq, size_t length) {
while (length > 0) {
/* Do not drop any data when we are in prebuffering mode */
- if (memblockq_check_prebuf(bq))
+ if (update_prebuf(bq))
break;
- if (bq->blocks) {
- size_t d;
-
- pa_assert(bq->blocks->index >= bq->read_index);
+ fix_current_read(bq);
- d = (size_t) (bq->blocks->index - bq->read_index);
+ if (bq->current_read) {
+ int64_t p, d;
- if (d >= length) {
- /* The first block is too far in the future */
+ /* We go through this piece by piece to make sure we don't
+ * drop more than allowed by prebuf */
- bq->read_index += length;
- break;
- } else {
+ p = bq->current_read->index + (int64_t) bq->current_read->chunk.length;
+ pa_assert(p >= bq->read_index);
+ d = p - bq->read_index;
- length -= d;
- bq->read_index += d;
- }
+ if (d > (int64_t) length)
+ d = (int64_t) length;
- pa_assert(bq->blocks->index == bq->read_index);
-
- if (bq->blocks->chunk.length <= length) {
- /* We need to drop the full block */
-
- length -= bq->blocks->chunk.length;
- bq->read_index += bq->blocks->chunk.length;
-
- drop_block(bq, bq->blocks);
- } else {
- /* Only the start of this block needs to be dropped */
-
- bq->blocks->chunk.index += length;
- bq->blocks->chunk.length -= length;
- bq->blocks->index += length;
- bq->read_index += length;
- break;
- }
+ bq->read_index += d;
+ length -= (size_t) d;
} else {
/* The list is empty, there's nothing we could drop */
- bq->read_index += length;
+ bq->read_index += (int64_t) length;
break;
}
}
+ drop_backlog(bq);
+
delta = bq->read_index - old;
bq->missing += delta;
}
-int pa_memblockq_is_readable(pa_memblockq *bq) {
+void pa_memblockq_rewind(pa_memblockq *bq, size_t length) {
pa_assert(bq);
+ pa_assert(length % bq->base == 0);
- if (memblockq_check_prebuf(bq))
- return 0;
+ /* This is kind of the inverse of pa_memblockq_drop() */
+
+ bq->read_index -= (int64_t) length;
+ bq->missing -= (int64_t) length;
+}
+
+pa_bool_t pa_memblockq_is_readable(pa_memblockq *bq) {
+ pa_assert(bq);
+
+ if (pa_memblockq_prebuf_active(bq))
+ return FALSE;
if (pa_memblockq_get_length(bq) <= 0)
- return 0;
+ return FALSE;
- return 1;
+ return TRUE;
}
size_t pa_memblockq_get_length(pa_memblockq *bq) {
@@ -506,12 +601,6 @@ size_t pa_memblockq_missing(pa_memblockq *bq) {
return l >= bq->minreq ? l : 0;
}
-size_t pa_memblockq_get_minreq(pa_memblockq *bq) {
- pa_assert(bq);
-
- return bq->minreq;
-}
-
void pa_memblockq_seek(pa_memblockq *bq, int64_t offset, pa_seek_mode_t seek) {
int64_t old, delta;
pa_assert(bq);
@@ -535,27 +624,26 @@ void pa_memblockq_seek(pa_memblockq *bq, int64_t offset, pa_seek_mode_t seek) {
pa_assert_not_reached();
}
+ drop_backlog(bq);
+
delta = bq->write_index - old;
- if (delta >= bq->requested) {
- delta -= bq->requested;
+ if (delta >= (int64_t) bq->requested) {
+ delta -= (int64_t) bq->requested;
bq->requested = 0;
} else if (delta >= 0) {
- bq->requested -= delta;
+ bq->requested -= (size_t) delta;
delta = 0;
}
bq->missing -= delta;
}
-void pa_memblockq_flush(pa_memblockq *bq) {
+void pa_memblockq_flush_write(pa_memblockq *bq) {
int64_t old, delta;
pa_assert(bq);
- while (bq->blocks)
- drop_block(bq, bq->blocks);
-
- pa_assert(bq->n_blocks == 0);
+ pa_memblockq_silence(bq);
old = bq->write_index;
bq->write_index = bq->read_index;
@@ -564,30 +652,53 @@ void pa_memblockq_flush(pa_memblockq *bq) {
delta = bq->write_index - old;
- if (delta > bq->requested) {
- delta -= bq->requested;
+ if (delta >= (int64_t) bq->requested) {
+ delta -= (int64_t) bq->requested;
bq->requested = 0;
} else if (delta >= 0) {
- bq->requested -= delta;
+ bq->requested -= (size_t) delta;
delta = 0;
}
bq->missing -= delta;
}
+void pa_memblockq_flush_read(pa_memblockq *bq) {
+ int64_t old, delta;
+ pa_assert(bq);
+
+ pa_memblockq_silence(bq);
+
+ old = bq->read_index;
+ bq->read_index = bq->write_index;
+
+ pa_memblockq_prebuf_force(bq);
+
+ delta = bq->read_index - old;
+ bq->missing += delta;
+}
+
size_t pa_memblockq_get_tlength(pa_memblockq *bq) {
pa_assert(bq);
return bq->tlength;
}
+size_t pa_memblockq_get_minreq(pa_memblockq *bq) {
+ pa_assert(bq);
+
+ return bq->minreq;
+}
+
int64_t pa_memblockq_get_read_index(pa_memblockq *bq) {
pa_assert(bq);
+
return bq->read_index;
}
int64_t pa_memblockq_get_write_index(pa_memblockq *bq) {
pa_assert(bq);
+
return bq->write_index;
}
@@ -600,9 +711,6 @@ int pa_memblockq_push_align(pa_memblockq* bq, const pa_memchunk *chunk) {
if (bq->base == 1)
return pa_memblockq_push(bq, chunk);
- if (!bq->mcalign)
- bq->mcalign = pa_mcalign_new(bq->base);
-
if (!can_push(bq, pa_mcalign_csize(bq->mcalign, chunk->length)))
return -1;
@@ -613,23 +721,15 @@ int pa_memblockq_push_align(pa_memblockq* bq, const pa_memchunk *chunk) {
r = pa_memblockq_push(bq, &rchunk);
pa_memblock_unref(rchunk.memblock);
- if (r < 0)
+ if (r < 0) {
+ pa_mcalign_flush(bq->mcalign);
return -1;
+ }
}
return 0;
}
-void pa_memblockq_shorten(pa_memblockq *bq, size_t length) {
- size_t l;
- pa_assert(bq);
-
- l = pa_memblockq_get_length(bq);
-
- if (l > length)
- pa_memblockq_drop(bq, l - length);
-}
-
void pa_memblockq_prebuf_disable(pa_memblockq *bq) {
pa_assert(bq);
@@ -639,7 +739,7 @@ void pa_memblockq_prebuf_disable(pa_memblockq *bq) {
void pa_memblockq_prebuf_force(pa_memblockq *bq) {
pa_assert(bq);
- if (!bq->in_prebuf && bq->prebuf > 0)
+ if (bq->prebuf > 0)
bq->in_prebuf = TRUE;
}
@@ -691,18 +791,20 @@ void pa_memblockq_set_tlength(pa_memblockq *bq, size_t tlength) {
size_t old_tlength;
pa_assert(bq);
- old_tlength = bq->tlength;
-
if (tlength <= 0)
tlength = bq->maxlength;
+ old_tlength = bq->tlength;
bq->tlength = ((tlength+bq->base-1)/bq->base)*bq->base;
if (bq->tlength > bq->maxlength)
bq->tlength = bq->maxlength;
- if (bq->minreq > bq->tlength - bq->prebuf)
- pa_memblockq_set_minreq(bq, bq->tlength - bq->prebuf);
+ if (bq->prebuf > bq->tlength)
+ pa_memblockq_set_prebuf(bq, bq->tlength);
+
+ if (bq->minreq > bq->tlength)
+ pa_memblockq_set_minreq(bq, bq->tlength);
bq->missing += (int64_t) bq->tlength - (int64_t) old_tlength;
}
@@ -710,20 +812,22 @@ void pa_memblockq_set_tlength(pa_memblockq *bq, size_t tlength) {
void pa_memblockq_set_prebuf(pa_memblockq *bq, size_t prebuf) {
pa_assert(bq);
- bq->prebuf = (prebuf == (size_t) -1) ? bq->tlength/2 : prebuf;
- bq->prebuf = ((bq->prebuf+bq->base-1)/bq->base)*bq->base;
+ if (prebuf == (size_t) -1)
+ prebuf = bq->tlength;
+
+ bq->prebuf = ((prebuf+bq->base-1)/bq->base)*bq->base;
if (prebuf > 0 && bq->prebuf < bq->base)
bq->prebuf = bq->base;
- if (bq->prebuf > bq->maxlength)
- bq->prebuf = bq->maxlength;
+ if (bq->prebuf > bq->tlength)
+ bq->prebuf = bq->tlength;
if (bq->prebuf <= 0 || pa_memblockq_get_length(bq) >= bq->prebuf)
bq->in_prebuf = FALSE;
- if (bq->minreq > bq->tlength - bq->prebuf)
- pa_memblockq_set_minreq(bq, bq->tlength - bq->prebuf);
+ if (bq->minreq > bq->prebuf)
+ pa_memblockq_set_minreq(bq, bq->prebuf);
}
void pa_memblockq_set_minreq(pa_memblockq *bq, size_t minreq) {
@@ -731,9 +835,99 @@ void pa_memblockq_set_minreq(pa_memblockq *bq, size_t minreq) {
bq->minreq = (minreq/bq->base)*bq->base;
- if (bq->minreq > bq->tlength - bq->prebuf)
- bq->minreq = bq->tlength - bq->prebuf;
+ if (bq->minreq > bq->tlength)
+ bq->minreq = bq->tlength;
+
+ if (bq->minreq > bq->prebuf)
+ bq->minreq = bq->prebuf;
if (bq->minreq < bq->base)
bq->minreq = bq->base;
}
+
+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, (int64_t) 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;
+}
+
+size_t pa_memblockq_get_base(pa_memblockq *bq) {
+ pa_assert(bq);
+
+ return bq->base;
+}