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
|
2009-03-03 20:23:02 +00:00
|
|
|
by the Free Software Foundation; either version 2.1 of the License,
|
2004-07-16 19:56:36 +00:00
|
|
|
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-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"
|
|
|
|
|
|
2011-11-10 12:43:09 +05:30
|
|
|
/* #define MEMBLOCKQ_DEBUG */
|
|
|
|
|
|
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;
|
2010-01-08 20:07:34 +01:00
|
|
|
int64_t missing, requested;
|
2011-09-29 18:54:03 +03:00
|
|
|
char *name;
|
|
|
|
|
pa_sample_spec sample_spec;
|
2004-06-08 23:54:24 +00:00
|
|
|
};
|
|
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
pa_memblockq* pa_memblockq_new(
|
2011-09-29 18:54:03 +03:00
|
|
|
const char *name,
|
2006-02-20 04:05:16 +00:00
|
|
|
int64_t idx,
|
|
|
|
|
size_t maxlength,
|
|
|
|
|
size_t tlength,
|
2011-09-29 18:54:03 +03:00
|
|
|
const pa_sample_spec *sample_spec,
|
2006-02-20 04:05:16 +00:00
|
|
|
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
|
|
|
|
2011-09-29 18:54:03 +03:00
|
|
|
pa_assert(sample_spec);
|
|
|
|
|
pa_assert(name);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2012-04-28 18:54:10 +03:00
|
|
|
bq = pa_xnew0(pa_memblockq, 1);
|
2011-09-29 18:54:03 +03:00
|
|
|
bq->name = pa_xstrdup(name);
|
2004-07-07 00:22:46 +00:00
|
|
|
|
2011-09-29 18:54:03 +03:00
|
|
|
bq->sample_spec = *sample_spec;
|
|
|
|
|
bq->base = pa_frame_size(sample_spec);
|
2006-02-20 04:05:16 +00:00
|
|
|
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",
|
2011-09-29 18:54:03 +03:00
|
|
|
(unsigned long) maxlength, (unsigned long) tlength, (unsigned long) bq->base, (unsigned long) prebuf, (unsigned long) minreq, (unsigned long) maxrewind);
|
2004-07-07 00:22:46 +00:00
|
|
|
|
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_minreq(bq, minreq);
|
2009-04-06 22:09:38 +02:00
|
|
|
pa_memblockq_set_prebuf(bq, prebuf);
|
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);
|
2012-04-28 18:54:10 +03:00
|
|
|
}
|
2008-05-15 23:34:41 +00:00
|
|
|
|
|
|
|
|
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
|
|
|
|
2011-09-29 18:54:03 +03:00
|
|
|
pa_xfree(bq->name);
|
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);
|
|
|
|
|
|
2008-08-19 22:39:54 +02:00
|
|
|
boundary = bq->read_index - (int64_t) bq->maxrewind;
|
2008-05-15 23:34:41 +00:00
|
|
|
|
|
|
|
|
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) {
|
2008-08-19 22:39:54 +02:00
|
|
|
int64_t d = bq->read_index - bq->write_index;
|
2004-06-15 00:29:01 +00:00
|
|
|
|
2008-08-19 22:39:54 +02:00
|
|
|
if ((int64_t) l > d)
|
|
|
|
|
l -= (size_t) d;
|
2006-02-20 04:05:16 +00:00
|
|
|
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)
|
2008-08-19 22:39:54 +02:00
|
|
|
if (bq->write_index + (int64_t) l - bq->read_index > (int64_t) 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
|
|
|
}
|
|
|
|
|
|
2010-01-08 20:07:34 +01:00
|
|
|
static void write_index_changed(pa_memblockq *bq, int64_t old_write_index, pa_bool_t account) {
|
|
|
|
|
int64_t delta;
|
|
|
|
|
|
|
|
|
|
pa_assert(bq);
|
|
|
|
|
|
|
|
|
|
delta = bq->write_index - old_write_index;
|
|
|
|
|
|
|
|
|
|
if (account)
|
|
|
|
|
bq->requested -= delta;
|
2010-01-18 01:33:04 +01:00
|
|
|
else
|
|
|
|
|
bq->missing -= delta;
|
2010-01-08 20:07:34 +01:00
|
|
|
|
2011-11-10 12:43:09 +05:30
|
|
|
#ifdef MEMBLOCKQ_DEBUG
|
|
|
|
|
pa_log("[%s] pushed/seeked %lli: requested counter at %lli, account=%i", bq->name, (long long) delta, (long long) bq->requested, account);
|
|
|
|
|
#endif
|
2010-01-08 20:07:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void read_index_changed(pa_memblockq *bq, int64_t old_read_index) {
|
|
|
|
|
int64_t delta;
|
|
|
|
|
|
|
|
|
|
pa_assert(bq);
|
|
|
|
|
|
|
|
|
|
delta = bq->read_index - old_read_index;
|
|
|
|
|
bq->missing += delta;
|
|
|
|
|
|
2011-11-10 12:43:09 +05:30
|
|
|
#ifdef MEMBLOCKQ_DEBUG
|
|
|
|
|
pa_log("[%s] popped %lli: missing counter at %lli", bq->name, (long long) delta, (long long) bq->missing);
|
|
|
|
|
#endif
|
2010-01-08 20:07:34 +01: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;
|
2010-01-08 20:07:34 +01:00
|
|
|
int64_t old;
|
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 &&
|
2008-08-19 22:39:54 +02:00
|
|
|
bq->write_index + (int64_t) chunk.length >= q->index + (int64_t) q->chunk.length) {
|
2006-02-20 04:05:16 +00:00
|
|
|
|
|
|
|
|
/* 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 */
|
|
|
|
|
|
2008-08-19 22:39:54 +02:00
|
|
|
if (bq->write_index + (int64_t) chunk.length < q->index + (int64_t) q->chunk.length) {
|
2006-02-20 04:05:16 +00:00
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
|
|
2011-08-24 18:24:46 +02:00
|
|
|
/* Create a new list entry for the end of the 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 */
|
2008-08-19 22:39:54 +02:00
|
|
|
d = (size_t) (bq->write_index + (int64_t) 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 */
|
2008-08-19 22:39:54 +02:00
|
|
|
p->index = q->index + (int64_t) d;
|
2006-02-20 04:05:16 +00:00
|
|
|
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 */
|
2008-08-19 22:39:54 +02:00
|
|
|
if (!(q->chunk.length = (size_t) (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 &&
|
2011-03-02 12:41:26 +01:00
|
|
|
bq->write_index + (int64_t)chunk.length < q->index + (int64_t)q->chunk.length &&
|
|
|
|
|
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 */
|
|
|
|
|
|
2008-08-19 22:39:54 +02:00
|
|
|
d = (size_t) (bq->write_index + (int64_t) chunk.length - q->index);
|
|
|
|
|
q->index += (int64_t) d;
|
2006-02-20 04:05:16 +00:00
|
|
|
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 &&
|
2008-08-19 22:39:54 +02:00
|
|
|
q->chunk.index + 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;
|
2008-08-19 22:39:54 +02:00
|
|
|
bq->write_index += (int64_t) 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;
|
2008-08-19 22:39:54 +02:00
|
|
|
bq->write_index += (int64_t) n->chunk.length;
|
2006-02-20 04:05:16 +00:00
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
|
2010-01-08 20:07:34 +01:00
|
|
|
write_index_changed(bq, old, TRUE);
|
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) {
|
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)
|
2008-08-19 22:39:54 +02:00
|
|
|
length = (size_t) (bq->current_read->index - bq->read_index);
|
2008-05-15 23:34:41 +00:00
|
|
|
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;
|
2008-08-19 22:39:54 +02:00
|
|
|
chunk->index += (size_t) d;
|
|
|
|
|
chunk->length -= (size_t) d;
|
2008-05-15 23:34:41 +00:00
|
|
|
|
2006-02-20 04:05:16 +00:00
|
|
|
return 0;
|
2004-06-08 23:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
2010-02-25 02:10:45 +01:00
|
|
|
int pa_memblockq_peek_fixed_size(pa_memblockq *bq, size_t block_size, pa_memchunk *chunk) {
|
|
|
|
|
pa_memchunk tchunk, rchunk;
|
|
|
|
|
int64_t ri;
|
|
|
|
|
struct list_item *item;
|
|
|
|
|
|
|
|
|
|
pa_assert(bq);
|
|
|
|
|
pa_assert(block_size > 0);
|
|
|
|
|
pa_assert(chunk);
|
|
|
|
|
pa_assert(bq->silence.memblock);
|
|
|
|
|
|
|
|
|
|
if (pa_memblockq_peek(bq, &tchunk) < 0)
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
if (tchunk.length >= block_size) {
|
|
|
|
|
*chunk = tchunk;
|
|
|
|
|
chunk->length = block_size;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rchunk.memblock = pa_memblock_new(pa_memblock_get_pool(tchunk.memblock), block_size);
|
|
|
|
|
rchunk.index = 0;
|
|
|
|
|
rchunk.length = tchunk.length;
|
|
|
|
|
|
|
|
|
|
pa_memchunk_memcpy(&rchunk, &tchunk);
|
|
|
|
|
pa_memblock_unref(tchunk.memblock);
|
|
|
|
|
|
|
|
|
|
rchunk.index += tchunk.length;
|
|
|
|
|
|
|
|
|
|
/* We don't need to call fix_current_read() here, since
|
|
|
|
|
* pa_memblock_peek() already did that */
|
|
|
|
|
item = bq->current_read;
|
|
|
|
|
ri = bq->read_index + tchunk.length;
|
|
|
|
|
|
|
|
|
|
while (rchunk.index < block_size) {
|
|
|
|
|
|
|
|
|
|
if (!item || item->index > ri) {
|
|
|
|
|
/* Do we need to append silence? */
|
|
|
|
|
tchunk = bq->silence;
|
|
|
|
|
|
|
|
|
|
if (item)
|
|
|
|
|
tchunk.length = PA_MIN(tchunk.length, (size_t) (item->index - ri));
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
int64_t d;
|
|
|
|
|
|
|
|
|
|
/* We can append real data! */
|
|
|
|
|
tchunk = item->chunk;
|
|
|
|
|
|
|
|
|
|
d = ri - item->index;
|
|
|
|
|
tchunk.index += (size_t) d;
|
|
|
|
|
tchunk.length -= (size_t) d;
|
|
|
|
|
|
|
|
|
|
/* Go to next item for the next iteration */
|
|
|
|
|
item = item->next;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rchunk.length = tchunk.length = PA_MIN(tchunk.length, block_size - rchunk.index);
|
|
|
|
|
pa_memchunk_memcpy(&rchunk, &tchunk);
|
|
|
|
|
|
|
|
|
|
rchunk.index += rchunk.length;
|
|
|
|
|
ri += rchunk.length;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rchunk.index = 0;
|
|
|
|
|
rchunk.length = block_size;
|
|
|
|
|
|
|
|
|
|
*chunk = rchunk;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
void pa_memblockq_drop(pa_memblockq *bq, size_t length) {
|
2010-01-08 20:07:34 +01:00
|
|
|
int64_t old;
|
2007-10-28 19:13:50 +00:00
|
|
|
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-08-19 22:39:54 +02:00
|
|
|
p = bq->current_read->index + (int64_t) bq->current_read->chunk.length;
|
2008-05-15 23:34:41 +00:00
|
|
|
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)
|
2008-08-19 22:39:54 +02:00
|
|
|
d = (int64_t) length;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
bq->read_index += d;
|
2008-08-19 22:39:54 +02:00
|
|
|
length -= (size_t) 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 */
|
2008-08-19 22:39:54 +02:00
|
|
|
bq->read_index += (int64_t) length;
|
2006-02-20 04:05:16 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-10-28 19:13:50 +00:00
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
drop_backlog(bq);
|
2010-01-08 20:07:34 +01:00
|
|
|
read_index_changed(bq, old);
|
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) {
|
2010-01-08 20:07:34 +01:00
|
|
|
int64_t old;
|
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
|
|
|
|
2010-01-08 20:07:34 +01:00
|
|
|
old = bq->read_index;
|
|
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
/* This is kind of the inverse of pa_memblockq_drop() */
|
|
|
|
|
|
2008-08-19 22:39:54 +02:00
|
|
|
bq->read_index -= (int64_t) length;
|
2010-01-08 20:07:34 +01:00
|
|
|
|
|
|
|
|
read_index_changed(bq, old);
|
2008-05-15 23:34:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2009-04-01 23:05:09 +02:00
|
|
|
void pa_memblockq_seek(pa_memblockq *bq, int64_t offset, pa_seek_mode_t seek, pa_bool_t account) {
|
2010-01-08 20:07:34 +01:00
|
|
|
int64_t old;
|
2007-10-28 19:13:50 +00:00
|
|
|
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);
|
2010-01-08 20:07:34 +01:00
|
|
|
write_index_changed(bq, old, account);
|
2006-02-20 04:05:16 +00:00
|
|
|
}
|
|
|
|
|
|
2010-01-18 01:33:04 +01:00
|
|
|
void pa_memblockq_flush_write(pa_memblockq *bq, pa_bool_t account) {
|
2010-01-08 20:07:34 +01:00
|
|
|
int64_t old;
|
2007-10-28 19:13:50 +00:00
|
|
|
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);
|
2010-01-18 01:33:04 +01:00
|
|
|
write_index_changed(bq, old, account);
|
2004-07-03 00:19:17 +00:00
|
|
|
}
|
2004-07-07 00:22:46 +00:00
|
|
|
|
2008-06-26 00:39:31 +02:00
|
|
|
void pa_memblockq_flush_read(pa_memblockq *bq) {
|
2010-01-08 20:07:34 +01:00
|
|
|
int64_t old;
|
2008-06-26 00:39:31 +02:00
|
|
|
pa_assert(bq);
|
|
|
|
|
|
|
|
|
|
pa_memblockq_silence(bq);
|
|
|
|
|
|
|
|
|
|
old = bq->read_index;
|
|
|
|
|
bq->read_index = bq->write_index;
|
|
|
|
|
|
|
|
|
|
pa_memblockq_prebuf_force(bq);
|
2010-01-08 20:07:34 +01:00
|
|
|
read_index_changed(bq, old);
|
2008-06-26 00:39:31 +02: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;
|
|
|
|
|
}
|
|
|
|
|
|
2009-08-14 04:16:28 +02:00
|
|
|
size_t pa_memblockq_get_maxrewind(pa_memblockq *bq) {
|
|
|
|
|
pa_assert(bq);
|
|
|
|
|
|
|
|
|
|
return bq->maxrewind;
|
|
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
|
2011-11-10 12:43:09 +05:30
|
|
|
#ifdef MEMBLOCKQ_DEBUG
|
|
|
|
|
pa_log("[%s] pop: %lli", bq->name, (long long) bq->missing);
|
|
|
|
|
#endif
|
2007-10-28 19:13:50 +00:00
|
|
|
|
|
|
|
|
if (bq->missing <= 0)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
l = (size_t) bq->missing;
|
2010-01-08 20:07:34 +01:00
|
|
|
|
|
|
|
|
bq->requested += bq->missing;
|
2007-10-28 19:13:50 +00:00
|
|
|
bq->missing = 0;
|
2010-01-08 20:07:34 +01:00
|
|
|
|
2011-11-10 12:43:09 +05:30
|
|
|
#ifdef MEMBLOCKQ_DEBUG
|
|
|
|
|
pa_log("[%s] sent %lli: request counter is at %lli", bq->name, (long long) l, (long long) bq->requested);
|
|
|
|
|
#endif
|
2007-10-28 19:13:50 +00:00
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void pa_memblockq_set_tlength(pa_memblockq *bq, size_t tlength) {
|
|
|
|
|
size_t old_tlength;
|
|
|
|
|
pa_assert(bq);
|
|
|
|
|
|
2009-04-06 22:09:38 +02:00
|
|
|
if (tlength <= 0 || tlength == (size_t) -1)
|
2007-11-21 01:19:28 +00:00
|
|
|
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-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
|
|
|
|
2009-04-06 22:09:38 +02:00
|
|
|
if (bq->prebuf > bq->tlength+bq->base-bq->minreq)
|
|
|
|
|
pa_memblockq_set_prebuf(bq, bq->tlength+bq->base-bq->minreq);
|
|
|
|
|
|
2007-11-21 01:19:28 +00:00
|
|
|
bq->missing += (int64_t) bq->tlength - (int64_t) old_tlength;
|
|
|
|
|
}
|
|
|
|
|
|
2009-04-06 22:09:38 +02:00
|
|
|
void pa_memblockq_set_minreq(pa_memblockq *bq, size_t minreq) {
|
|
|
|
|
pa_assert(bq);
|
|
|
|
|
|
|
|
|
|
bq->minreq = (minreq/bq->base)*bq->base;
|
|
|
|
|
|
|
|
|
|
if (bq->minreq > bq->tlength)
|
|
|
|
|
bq->minreq = bq->tlength;
|
|
|
|
|
|
|
|
|
|
if (bq->minreq < bq->base)
|
|
|
|
|
bq->minreq = bq->base;
|
|
|
|
|
|
|
|
|
|
if (bq->prebuf > bq->tlength+bq->base-bq->minreq)
|
|
|
|
|
pa_memblockq_set_prebuf(bq, bq->tlength+bq->base-bq->minreq);
|
|
|
|
|
}
|
|
|
|
|
|
2007-11-21 01:19:28 +00:00
|
|
|
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)
|
2009-04-06 22:09:38 +02:00
|
|
|
prebuf = bq->tlength+bq->base-bq->minreq;
|
2008-05-15 23:34:41 +00:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
2009-04-06 22:09:38 +02:00
|
|
|
if (bq->prebuf > bq->tlength+bq->base-bq->minreq)
|
|
|
|
|
bq->prebuf = bq->tlength+bq->base-bq->minreq;
|
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
|
|
|
|
|
|
|
|
void pa_memblockq_set_maxrewind(pa_memblockq *bq, size_t maxrewind) {
|
|
|
|
|
pa_assert(bq);
|
|
|
|
|
|
|
|
|
|
bq->maxrewind = (maxrewind/bq->base)*bq->base;
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-30 18:27:07 +02:00
|
|
|
void pa_memblockq_apply_attr(pa_memblockq *bq, const pa_buffer_attr *a) {
|
|
|
|
|
pa_assert(bq);
|
|
|
|
|
pa_assert(a);
|
|
|
|
|
|
|
|
|
|
pa_memblockq_set_maxlength(bq, a->maxlength);
|
|
|
|
|
pa_memblockq_set_tlength(bq, a->tlength);
|
|
|
|
|
pa_memblockq_set_minreq(bq, a->minreq);
|
2012-04-28 18:54:11 +03:00
|
|
|
pa_memblockq_set_prebuf(bq, a->prebuf);
|
2009-03-30 18:27:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void pa_memblockq_get_attr(pa_memblockq *bq, pa_buffer_attr *a) {
|
|
|
|
|
pa_assert(bq);
|
|
|
|
|
pa_assert(a);
|
|
|
|
|
|
|
|
|
|
a->maxlength = (uint32_t) pa_memblockq_get_maxlength(bq);
|
|
|
|
|
a->tlength = (uint32_t) pa_memblockq_get_tlength(bq);
|
|
|
|
|
a->prebuf = (uint32_t) pa_memblockq_get_prebuf(bq);
|
|
|
|
|
a->minreq = (uint32_t) pa_memblockq_get_minreq(bq);
|
|
|
|
|
}
|
|
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
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
|
2009-04-01 23:05:09 +02:00
|
|
|
pa_memblockq_seek(bq, (int64_t) chunk.length, PA_SEEK_RELATIVE, TRUE);
|
2008-05-15 23:34:41 +00:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|