]>
code.delx.au - pulseaudio/blob - src/pulsecore/memblockq.c
4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
34 #include <pulse/xmalloc.h>
36 #include <pulsecore/log.h>
37 #include <pulsecore/mcalign.h>
38 #include <pulsecore/macro.h>
39 #include <pulsecore/flist.h>
41 #include "memblockq.h"
44 struct list_item
*next
, *prev
;
49 PA_STATIC_FLIST_DECLARE(list_items
, 0, pa_xfree
);
52 struct list_item
*blocks
, *blocks_tail
;
54 size_t maxlength
, tlength
, base
, prebuf
, minreq
;
55 int64_t read_index
, write_index
;
63 pa_memblockq
* pa_memblockq_new(
70 pa_memblock
*silence
) {
75 pa_assert(maxlength
>= base
);
77 bq
= pa_xnew(pa_memblockq
, 1);
78 bq
->blocks
= bq
->blocks_tail
= NULL
;
82 bq
->read_index
= bq
->write_index
= idx
;
84 pa_log_debug("memblockq requested: maxlength=%lu, tlength=%lu, base=%lu, prebuf=%lu, minreq=%lu",
85 (unsigned long) maxlength
, (unsigned long) tlength
, (unsigned long) base
, (unsigned long) prebuf
, (unsigned long) minreq
);
87 bq
->maxlength
= ((maxlength
+base
-1)/base
)*base
;
88 pa_assert(bq
->maxlength
>= base
);
90 bq
->tlength
= ((tlength
+base
-1)/base
)*base
;
91 if (bq
->tlength
<= 0 || bq
->tlength
> bq
->maxlength
)
92 bq
->tlength
= bq
->maxlength
;
94 bq
->prebuf
= (prebuf
== (size_t) -1) ? bq
->tlength
/2 : prebuf
;
95 bq
->prebuf
= ((bq
->prebuf
+base
-1)/base
)*base
;
96 if (bq
->prebuf
> bq
->maxlength
)
97 bq
->prebuf
= bq
->maxlength
;
99 bq
->minreq
= (minreq
/base
)*base
;
101 if (bq
->minreq
> bq
->tlength
- bq
->prebuf
)
102 bq
->minreq
= bq
->tlength
- bq
->prebuf
;
107 pa_log_debug("memblockq sanitized: maxlength=%lu, tlength=%lu, base=%lu, prebuf=%lu, minreq=%lu",
108 (unsigned long)bq
->maxlength
, (unsigned long)bq
->tlength
, (unsigned long)bq
->base
, (unsigned long)bq
->prebuf
, (unsigned long)bq
->minreq
);
110 bq
->in_prebuf
= bq
->prebuf
> 0;
111 bq
->silence
= silence
? pa_memblock_ref(silence
) : NULL
;
114 bq
->missing
= bq
->tlength
;
120 void pa_memblockq_free(pa_memblockq
* bq
) {
123 pa_memblockq_flush(bq
);
126 pa_memblock_unref(bq
->silence
);
129 pa_mcalign_free(bq
->mcalign
);
134 static void drop_block(pa_memblockq
*bq
, struct list_item
*q
) {
138 pa_assert(bq
->n_blocks
>= 1);
141 q
->prev
->next
= q
->next
;
143 bq
->blocks
= q
->next
;
146 q
->next
->prev
= q
->prev
;
148 bq
->blocks_tail
= q
->prev
;
150 pa_memblock_unref(q
->chunk
.memblock
);
152 if (pa_flist_push(PA_STATIC_FLIST_GET(list_items
), q
) < 0)
158 static pa_bool_t
can_push(pa_memblockq
*bq
, size_t l
) {
163 if (bq
->read_index
> bq
->write_index
) {
164 size_t d
= bq
->read_index
- bq
->write_index
;
172 end
= bq
->blocks_tail
? bq
->blocks_tail
->index
+ bq
->blocks_tail
->chunk
.length
: 0;
174 /* Make sure that the list doesn't get too long */
175 if (bq
->write_index
+ (int64_t)l
> end
)
176 if (bq
->write_index
+ l
- bq
->read_index
> bq
->maxlength
)
182 int pa_memblockq_push(pa_memblockq
* bq
, const pa_memchunk
*uchunk
) {
183 struct list_item
*q
, *n
;
189 pa_assert(uchunk
->memblock
);
190 pa_assert(uchunk
->length
> 0);
191 pa_assert(uchunk
->index
+ uchunk
->length
<= pa_memblock_get_length(uchunk
->memblock
));
193 if (uchunk
->length
% bq
->base
)
196 if (!can_push(bq
, uchunk
->length
))
199 old
= bq
->write_index
;
202 if (bq
->read_index
> bq
->write_index
) {
204 /* We currently have a buffer underflow, we need to drop some
207 size_t d
= bq
->read_index
- bq
->write_index
;
209 if (chunk
.length
> d
) {
212 bq
->write_index
+= d
;
214 /* We drop the incoming data completely */
215 bq
->write_index
+= chunk
.length
;
220 /* We go from back to front to look for the right place to add
221 * this new entry. Drop data we will overwrite on the way */
226 if (bq
->write_index
>= q
->index
+ (int64_t) q
->chunk
.length
)
227 /* We found the entry where we need to place the new entry immediately after */
229 else if (bq
->write_index
+ (int64_t) chunk
.length
<= q
->index
) {
230 /* This entry isn't touched at all, let's skip it */
232 } else if (bq
->write_index
<= q
->index
&&
233 bq
->write_index
+ chunk
.length
>= q
->index
+ q
->chunk
.length
) {
235 /* This entry is fully replaced by the new entry, so let's drop it */
241 } else if (bq
->write_index
>= q
->index
) {
242 /* The write index points into this memblock, so let's
243 * truncate or split it */
245 if (bq
->write_index
+ chunk
.length
< q
->index
+ q
->chunk
.length
) {
247 /* We need to save the end of this memchunk */
251 /* Create a new list entry for the end of thie memchunk */
252 if (!(p
= pa_flist_pop(PA_STATIC_FLIST_GET(list_items
))))
253 p
= pa_xnew(struct list_item
, 1);
256 pa_memblock_ref(p
->chunk
.memblock
);
258 /* Calculate offset */
259 d
= bq
->write_index
+ chunk
.length
- q
->index
;
262 /* Drop it from the new entry */
263 p
->index
= q
->index
+ d
;
264 p
->chunk
.length
-= d
;
266 /* Add it to the list */
268 if ((p
->next
= q
->next
))
277 /* Truncate the chunk */
278 if (!(q
->chunk
.length
= bq
->write_index
- q
->index
)) {
285 /* We had to truncate this block, hence we're now at the right position */
290 pa_assert(bq
->write_index
+ (int64_t)chunk
.length
> q
->index
&&
291 bq
->write_index
+ (int64_t)chunk
.length
< q
->index
+ (int64_t)q
->chunk
.length
&&
292 bq
->write_index
< q
->index
);
294 /* The job overwrites the current entry at the end, so let's drop the beginning of this entry */
296 d
= bq
->write_index
+ chunk
.length
- q
->index
;
299 q
->chunk
.length
-= d
;
306 pa_assert(bq
->write_index
>= q
->index
+ (int64_t)q
->chunk
.length
);
307 pa_assert(!q
->next
|| (bq
->write_index
+ (int64_t)chunk
.length
<= q
->next
->index
));
309 /* Try to merge memory blocks */
311 if (q
->chunk
.memblock
== chunk
.memblock
&&
312 q
->chunk
.index
+ (int64_t)q
->chunk
.length
== chunk
.index
&&
313 bq
->write_index
== q
->index
+ (int64_t)q
->chunk
.length
) {
315 q
->chunk
.length
+= chunk
.length
;
316 bq
->write_index
+= chunk
.length
;
320 pa_assert(!bq
->blocks
|| (bq
->write_index
+ (int64_t)chunk
.length
<= bq
->blocks
->index
));
322 if (!(n
= pa_flist_pop(PA_STATIC_FLIST_GET(list_items
))))
323 n
= pa_xnew(struct list_item
, 1);
326 pa_memblock_ref(n
->chunk
.memblock
);
327 n
->index
= bq
->write_index
;
328 bq
->write_index
+= n
->chunk
.length
;
330 n
->next
= q
? q
->next
: bq
->blocks
;
347 delta
= bq
->write_index
- old
;
349 if (delta
>= bq
->requested
) {
350 delta
-= bq
->requested
;
353 bq
->requested
-= delta
;
357 bq
->missing
-= delta
;
362 static pa_bool_t
memblockq_check_prebuf(pa_memblockq
*bq
) {
367 if (pa_memblockq_get_length(bq
) < bq
->prebuf
)
370 bq
->in_prebuf
= FALSE
;
374 if (bq
->prebuf
> 0 && bq
->read_index
>= bq
->write_index
) {
375 bq
->in_prebuf
= TRUE
;
383 int pa_memblockq_peek(pa_memblockq
* bq
, pa_memchunk
*chunk
) {
387 /* We need to pre-buffer */
388 if (memblockq_check_prebuf(bq
))
391 /* Do we need to spit out silence? */
392 if (!bq
->blocks
|| bq
->blocks
->index
> bq
->read_index
) {
396 /* How much silence shall we return? */
397 length
= bq
->blocks
? bq
->blocks
->index
- bq
->read_index
: 0;
399 /* We need to return silence, since no data is yet available */
401 chunk
->memblock
= pa_memblock_ref(bq
->silence
);
403 if (!length
|| length
> pa_memblock_get_length(chunk
->memblock
))
404 length
= pa_memblock_get_length(chunk
->memblock
);
406 chunk
->length
= length
;
409 /* If the memblockq is empty, return -1, otherwise return
410 * the time to sleep */
414 chunk
->memblock
= NULL
;
415 chunk
->length
= length
;
422 /* Ok, let's pass real data to the caller */
423 pa_assert(bq
->blocks
->index
== bq
->read_index
);
425 *chunk
= bq
->blocks
->chunk
;
426 pa_memblock_ref(chunk
->memblock
);
431 void pa_memblockq_drop(pa_memblockq
*bq
, size_t length
) {
434 pa_assert(length
% bq
->base
== 0);
436 old
= bq
->read_index
;
440 /* Do not drop any data when we are in prebuffering mode */
441 if (memblockq_check_prebuf(bq
))
447 pa_assert(bq
->blocks
->index
>= bq
->read_index
);
449 d
= (size_t) (bq
->blocks
->index
- bq
->read_index
);
452 /* The first block is too far in the future */
454 bq
->read_index
+= length
;
462 pa_assert(bq
->blocks
->index
== bq
->read_index
);
464 if (bq
->blocks
->chunk
.length
<= length
) {
465 /* We need to drop the full block */
467 length
-= bq
->blocks
->chunk
.length
;
468 bq
->read_index
+= bq
->blocks
->chunk
.length
;
470 drop_block(bq
, bq
->blocks
);
472 /* Only the start of this block needs to be dropped */
474 bq
->blocks
->chunk
.index
+= length
;
475 bq
->blocks
->chunk
.length
-= length
;
476 bq
->blocks
->index
+= length
;
477 bq
->read_index
+= length
;
483 /* The list is empty, there's nothing we could drop */
484 bq
->read_index
+= length
;
489 delta
= bq
->read_index
- old
;
490 bq
->missing
+= delta
;
493 int pa_memblockq_is_readable(pa_memblockq
*bq
) {
496 if (memblockq_check_prebuf(bq
))
499 if (pa_memblockq_get_length(bq
) <= 0)
505 size_t pa_memblockq_get_length(pa_memblockq
*bq
) {
508 if (bq
->write_index
<= bq
->read_index
)
511 return (size_t) (bq
->write_index
- bq
->read_index
);
514 size_t pa_memblockq_missing(pa_memblockq
*bq
) {
518 if ((l
= pa_memblockq_get_length(bq
)) >= bq
->tlength
)
523 return l
>= bq
->minreq
? l
: 0;
526 size_t pa_memblockq_get_minreq(pa_memblockq
*bq
) {
532 void pa_memblockq_seek(pa_memblockq
*bq
, int64_t offset
, pa_seek_mode_t seek
) {
536 old
= bq
->write_index
;
539 case PA_SEEK_RELATIVE
:
540 bq
->write_index
+= offset
;
542 case PA_SEEK_ABSOLUTE
:
543 bq
->write_index
= offset
;
545 case PA_SEEK_RELATIVE_ON_READ
:
546 bq
->write_index
= bq
->read_index
+ offset
;
548 case PA_SEEK_RELATIVE_END
:
549 bq
->write_index
= (bq
->blocks_tail
? bq
->blocks_tail
->index
+ (int64_t) bq
->blocks_tail
->chunk
.length
: bq
->read_index
) + offset
;
552 pa_assert_not_reached();
555 delta
= bq
->write_index
- old
;
557 if (delta
>= bq
->requested
) {
558 delta
-= bq
->requested
;
560 } else if (delta
>= 0) {
561 bq
->requested
-= delta
;
565 bq
->missing
-= delta
;
568 void pa_memblockq_flush(pa_memblockq
*bq
) {
573 drop_block(bq
, bq
->blocks
);
575 pa_assert(bq
->n_blocks
== 0);
577 old
= bq
->write_index
;
578 bq
->write_index
= bq
->read_index
;
580 pa_memblockq_prebuf_force(bq
);
582 delta
= bq
->write_index
- old
;
584 if (delta
> bq
->requested
) {
585 delta
-= bq
->requested
;
587 } else if (delta
>= 0) {
588 bq
->requested
-= delta
;
592 bq
->missing
-= delta
;
595 size_t pa_memblockq_get_tlength(pa_memblockq
*bq
) {
601 int64_t pa_memblockq_get_read_index(pa_memblockq
*bq
) {
603 return bq
->read_index
;
606 int64_t pa_memblockq_get_write_index(pa_memblockq
*bq
) {
608 return bq
->write_index
;
611 int pa_memblockq_push_align(pa_memblockq
* bq
, const pa_memchunk
*chunk
) {
618 return pa_memblockq_push(bq
, chunk
);
621 bq
->mcalign
= pa_mcalign_new(bq
->base
);
623 if (!can_push(bq
, pa_mcalign_csize(bq
->mcalign
, chunk
->length
)))
626 pa_mcalign_push(bq
->mcalign
, chunk
);
628 while (pa_mcalign_pop(bq
->mcalign
, &rchunk
) >= 0) {
630 r
= pa_memblockq_push(bq
, &rchunk
);
631 pa_memblock_unref(rchunk
.memblock
);
640 void pa_memblockq_shorten(pa_memblockq
*bq
, size_t length
) {
644 l
= pa_memblockq_get_length(bq
);
647 pa_memblockq_drop(bq
, l
- length
);
650 void pa_memblockq_prebuf_disable(pa_memblockq
*bq
) {
653 bq
->in_prebuf
= FALSE
;
656 void pa_memblockq_prebuf_force(pa_memblockq
*bq
) {
659 if (!bq
->in_prebuf
&& bq
->prebuf
> 0)
660 bq
->in_prebuf
= TRUE
;
663 size_t pa_memblockq_get_maxlength(pa_memblockq
*bq
) {
666 return bq
->maxlength
;
669 size_t pa_memblockq_get_prebuf(pa_memblockq
*bq
) {
675 size_t pa_memblockq_pop_missing(pa_memblockq
*bq
) {
680 /* pa_log("pop: %lli", bq->missing); */
682 if (bq
->missing
<= 0)
685 l
= (size_t) bq
->missing
;