e31fb6dfb4d27a47c2afe630f0a77a20da2b38ab
[profile/ivi/pulseaudio-panda.git] / src / pulsecore / memblockq.c
1 /* $Id$ */
2
3 /***
4   This file is part of PulseAudio.
5
6   Copyright 2004-2006 Lennart Poettering
7
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.
12
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.
17
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
21   USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <sys/time.h>
29 #include <time.h>
30 #include <stdio.h>
31 #include <assert.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 #include <pulse/xmalloc.h>
36
37 #include <pulsecore/log.h>
38 #include <pulsecore/mcalign.h>
39
40 #include "memblockq.h"
41
42 struct memblock_list {
43     struct memblock_list *next, *prev;
44     int64_t index;
45     pa_memchunk chunk;
46 };
47
48 struct pa_memblockq {
49     struct memblock_list *blocks, *blocks_tail;
50     unsigned n_blocks;
51     size_t maxlength, tlength, base, prebuf, minreq;
52     int64_t read_index, write_index;
53     enum { PREBUF, RUNNING } state;
54     pa_memblock *silence;
55     pa_mcalign *mcalign;
56 };
57
58 pa_memblockq* pa_memblockq_new(
59         int64_t idx,
60         size_t maxlength,
61         size_t tlength,
62         size_t base,
63         size_t prebuf,
64         size_t minreq,
65         pa_memblock *silence) {
66
67     pa_memblockq* bq;
68
69     assert(base > 0);
70     assert(maxlength >= base);
71
72     bq = pa_xnew(pa_memblockq, 1);
73     bq->blocks = bq->blocks_tail = NULL;
74     bq->n_blocks = 0;
75
76     bq->base = base;
77     bq->read_index = bq->write_index = idx;
78
79     pa_log_debug("memblockq requested: maxlength=%lu, tlength=%lu, base=%lu, prebuf=%lu, minreq=%lu",
80         (unsigned long)maxlength, (unsigned long)tlength, (unsigned long)base, (unsigned long)prebuf, (unsigned long)minreq);
81
82     bq->maxlength = ((maxlength+base-1)/base)*base;
83     assert(bq->maxlength >= base);
84
85     bq->tlength = ((tlength+base-1)/base)*base;
86     if (!bq->tlength || bq->tlength >= bq->maxlength)
87         bq->tlength = bq->maxlength;
88
89     bq->prebuf = (prebuf == (size_t) -1) ? bq->tlength/2 : prebuf;
90     bq->prebuf = ((bq->prebuf+base-1)/base)*base;
91     if (bq->prebuf > bq->maxlength)
92         bq->prebuf = bq->maxlength;
93
94     bq->minreq = (minreq/base)*base;
95
96     if (bq->minreq > bq->tlength - bq->prebuf)
97         bq->minreq = bq->tlength - bq->prebuf;
98
99     if (!bq->minreq)
100         bq->minreq = 1;
101
102     pa_log_debug("memblockq sanitized: maxlength=%lu, tlength=%lu, base=%lu, prebuf=%lu, minreq=%lu",
103         (unsigned long)bq->maxlength, (unsigned long)bq->tlength, (unsigned long)bq->base, (unsigned long)bq->prebuf, (unsigned long)bq->minreq);
104
105     bq->state = bq->prebuf ? PREBUF : RUNNING;
106     bq->silence = silence ? pa_memblock_ref(silence) : NULL;
107     bq->mcalign = NULL;
108
109     return bq;
110 }
111
112 void pa_memblockq_free(pa_memblockq* bq) {
113     assert(bq);
114
115     pa_memblockq_flush(bq);
116
117     if (bq->silence)
118         pa_memblock_unref(bq->silence);
119
120     if (bq->mcalign)
121         pa_mcalign_free(bq->mcalign);
122
123     pa_xfree(bq);
124 }
125
126 static void drop_block(pa_memblockq *bq, struct memblock_list *q) {
127     assert(bq);
128     assert(q);
129
130     assert(bq->n_blocks >= 1);
131
132     if (q->prev)
133         q->prev->next = q->next;
134     else
135         bq->blocks = q->next;
136
137     if (q->next)
138         q->next->prev = q->prev;
139     else
140         bq->blocks_tail = q->prev;
141
142     pa_memblock_unref(q->chunk.memblock);
143     pa_xfree(q);
144
145     bq->n_blocks--;
146 }
147
148 static int can_push(pa_memblockq *bq, size_t l) {
149     int64_t end;
150
151     assert(bq);
152
153     if (bq->read_index > bq->write_index) {
154         size_t d =  bq->read_index - bq->write_index;
155
156         if (l > d)
157             l -= d;
158         else
159             return 1;
160     }
161
162     end = bq->blocks_tail ? bq->blocks_tail->index + bq->blocks_tail->chunk.length : 0;
163
164     /* Make sure that the list doesn't get too long */
165     if (bq->write_index + (int64_t)l > end)
166         if (bq->write_index + l - bq->read_index > bq->maxlength)
167             return 0;
168
169     return 1;
170 }
171
172 int pa_memblockq_push(pa_memblockq* bq, const pa_memchunk *uchunk) {
173
174     struct memblock_list *q, *n;
175     pa_memchunk chunk;
176
177     assert(bq);
178     assert(uchunk);
179     assert(uchunk->memblock);
180     assert(uchunk->length > 0);
181     assert(uchunk->index + uchunk->length <= uchunk->memblock->length);
182
183     if (uchunk->length % bq->base)
184         return -1;
185
186     if (!can_push(bq, uchunk->length))
187         return -1;
188
189     chunk = *uchunk;
190
191     if (bq->read_index > bq->write_index) {
192
193         /* We currently have a buffer underflow, we need to drop some
194          * incoming data */
195
196         size_t d = bq->read_index - bq->write_index;
197
198         if (chunk.length > d) {
199             chunk.index += d;
200             chunk.length -= d;
201             bq->write_index = bq->read_index;
202         } else {
203             /* We drop the incoming data completely */
204             bq->write_index += chunk.length;
205             return 0;
206         }
207     }
208
209     /* We go from back to front to look for the right place to add
210      * this new entry. Drop data we will overwrite on the way */
211
212     q = bq->blocks_tail;
213     while (q) {
214
215         if (bq->write_index >= q->index + (int64_t)q->chunk.length)
216             /* We found the entry where we need to place the new entry immediately after */
217             break;
218         else if (bq->write_index + (int64_t)chunk.length <= q->index) {
219             /* This entry isn't touched at all, let's skip it */
220             q = q->prev;
221         } else if (bq->write_index <= q->index &&
222             bq->write_index + chunk.length >= q->index + q->chunk.length) {
223
224             /* This entry is fully replaced by the new entry, so let's drop it */
225
226             struct memblock_list *p;
227             p = q;
228             q = q->prev;
229             drop_block(bq, p);
230         } else if (bq->write_index >= q->index) {
231             /* The write index points into this memblock, so let's
232              * truncate or split it */
233
234             if (bq->write_index + chunk.length < q->index + q->chunk.length) {
235
236                 /* We need to save the end of this memchunk */
237                 struct memblock_list *p;
238                 size_t d;
239
240                 /* Create a new list entry for the end of thie memchunk */
241                 p = pa_xnew(struct memblock_list, 1);
242                 p->chunk = q->chunk;
243                 pa_memblock_ref(p->chunk.memblock);
244
245                 /* Calculate offset */
246                 d = bq->write_index + chunk.length - q->index;
247                 assert(d > 0);
248
249                 /* Drop it from the new entry */
250                 p->index = q->index + d;
251                 p->chunk.length -= d;
252
253                 /* Add it to the list */
254                 p->prev = q;
255                 if ((p->next = q->next))
256                     q->next->prev = p;
257                 else
258                     bq->blocks_tail = p;
259                 q->next = p;
260
261                 bq->n_blocks++;
262             }
263
264             /* Truncate the chunk */
265             if (!(q->chunk.length = bq->write_index - q->index)) {
266                 struct memblock_list *p;
267                 p = q;
268                 q = q->prev;
269                 drop_block(bq, p);
270             }
271
272             /* We had to truncate this block, hence we're now at the right position */
273             break;
274         } else {
275             size_t d;
276
277             assert(bq->write_index + (int64_t)chunk.length > q->index &&
278                    bq->write_index + (int64_t)chunk.length < q->index + (int64_t)q->chunk.length &&
279                    bq->write_index < q->index);
280
281             /* The job overwrites the current entry at the end, so let's drop the beginning of this entry */
282
283             d = bq->write_index + chunk.length - q->index;
284             q->index += d;
285             q->chunk.index += d;
286             q->chunk.length -= d;
287
288             q = q->prev;
289         }
290
291     }
292
293     if (q) {
294         assert(bq->write_index >=  q->index + (int64_t)q->chunk.length);
295         assert(!q->next || (bq->write_index + (int64_t)chunk.length <= q->next->index));
296
297         /* Try to merge memory blocks */
298
299         if (q->chunk.memblock == chunk.memblock &&
300             q->chunk.index + (int64_t)q->chunk.length == chunk.index &&
301             bq->write_index == q->index + (int64_t)q->chunk.length) {
302
303             q->chunk.length += chunk.length;
304             bq->write_index += chunk.length;
305             return 0;
306         }
307     } else
308         assert(!bq->blocks || (bq->write_index + (int64_t)chunk.length <= bq->blocks->index));
309
310
311     n = pa_xnew(struct memblock_list, 1);
312     n->chunk = chunk;
313     pa_memblock_ref(n->chunk.memblock);
314     n->index = bq->write_index;
315     bq->write_index += n->chunk.length;
316
317     n->next = q ? q->next : bq->blocks;
318     n->prev = q;
319
320     if (n->next)
321         n->next->prev = n;
322     else
323         bq->blocks_tail = n;
324
325     if (n->prev)
326         n->prev->next = n;
327     else
328         bq->blocks = n;
329
330     bq->n_blocks++;
331     return 0;
332 }
333
334 int pa_memblockq_peek(pa_memblockq* bq, pa_memchunk *chunk) {
335     assert(bq);
336     assert(chunk);
337
338     if (bq->state == PREBUF) {
339
340         /* We need to pre-buffer */
341         if (pa_memblockq_get_length(bq) < bq->prebuf)
342             return -1;
343
344         bq->state = RUNNING;
345
346     } else if (bq->prebuf > 0 && bq->read_index >= bq->write_index) {
347
348         /* Buffer underflow protection */
349         bq->state = PREBUF;
350         return -1;
351     }
352
353     /* Do we need to spit out silence? */
354     if (!bq->blocks || bq->blocks->index > bq->read_index) {
355
356         size_t length;
357
358         /* How much silence shall we return? */
359         length = bq->blocks ? bq->blocks->index - bq->read_index : 0;
360
361         /* We need to return silence, since no data is yet available */
362         if (bq->silence) {
363             chunk->memblock = pa_memblock_ref(bq->silence);
364
365             if (!length || length > chunk->memblock->length)
366                 length = chunk->memblock->length;
367
368             chunk->length = length;
369         } else {
370
371             /* If the memblockq is empty, return -1, otherwise return
372              * the time to sleep */
373             if (!bq->blocks)
374                 return -1;
375
376             chunk->memblock = NULL;
377             chunk->length = length;
378         }
379
380         chunk->index = 0;
381         return 0;
382     }
383
384     /* Ok, let's pass real data to the caller */
385     assert(bq->blocks->index == bq->read_index);
386
387     *chunk = bq->blocks->chunk;
388     pa_memblock_ref(chunk->memblock);
389
390     return 0;
391 }
392
393 void pa_memblockq_drop(pa_memblockq *bq, const pa_memchunk *chunk, size_t length) {
394     assert(bq);
395     assert(length % bq->base == 0);
396
397     assert(!chunk || length <= chunk->length);
398
399     if (chunk) {
400
401         if (bq->blocks && bq->blocks->index == bq->read_index) {
402             /* The first item in queue is valid */
403
404             /* Does the chunk match with what the user supplied us? */
405             if (memcmp(chunk, &bq->blocks->chunk, sizeof(pa_memchunk)) != 0)
406                 return;
407
408         } else {
409             size_t l;
410
411             /* The first item in the queue is not yet relevant */
412
413             assert(!bq->blocks || bq->blocks->index > bq->read_index);
414             l = bq->blocks ? bq->blocks->index - bq->read_index : 0;
415
416             if (bq->silence) {
417
418                 if (!l || l > bq->silence->length)
419                     l = bq->silence->length;
420
421             }
422
423             /* Do the entries still match? */
424             if (chunk->index != 0 || chunk->length != l || chunk->memblock != bq->silence)
425                 return;
426         }
427     }
428
429     while (length > 0) {
430
431         if (bq->blocks) {
432             size_t d;
433
434             assert(bq->blocks->index >= bq->read_index);
435
436             d = (size_t) (bq->blocks->index - bq->read_index);
437
438             if (d >= length) {
439                 /* The first block is too far in the future */
440
441                 bq->read_index += length;
442                 break;
443             } else {
444
445                 length -= d;
446                 bq->read_index += d;
447             }
448
449             assert(bq->blocks->index == bq->read_index);
450
451             if (bq->blocks->chunk.length <= length) {
452                 /* We need to drop the full block */
453
454                 length -= bq->blocks->chunk.length;
455                 bq->read_index += bq->blocks->chunk.length;
456
457                 drop_block(bq, bq->blocks);
458             } else {
459                 /* Only the start of this block needs to be dropped */
460
461                 bq->blocks->chunk.index += length;
462                 bq->blocks->chunk.length -= length;
463                 bq->blocks->index += length;
464                 bq->read_index += length;
465                 break;
466             }
467
468         } else {
469
470             /* The list is empty, there's nothing we could drop */
471             bq->read_index += length;
472             break;
473         }
474     }
475 }
476
477 int pa_memblockq_is_readable(pa_memblockq *bq) {
478     assert(bq);
479
480     if (bq->prebuf > 0) {
481         size_t l = pa_memblockq_get_length(bq);
482
483         if (bq->state == PREBUF && l < bq->prebuf)
484             return 0;
485
486         if (l <= 0)
487             return 0;
488     }
489
490     return 1;
491 }
492
493 int pa_memblockq_is_writable(pa_memblockq *bq, size_t length) {
494     assert(bq);
495
496     if (length % bq->base)
497         return 0;
498
499     return pa_memblockq_get_length(bq) + length <= bq->tlength;
500 }
501
502 size_t pa_memblockq_get_length(pa_memblockq *bq) {
503     assert(bq);
504
505     if (bq->write_index <= bq->read_index)
506         return 0;
507
508     return (size_t) (bq->write_index - bq->read_index);
509 }
510
511 size_t pa_memblockq_missing(pa_memblockq *bq) {
512     size_t l;
513     assert(bq);
514
515     if ((l = pa_memblockq_get_length(bq)) >= bq->tlength)
516         return 0;
517
518     l = bq->tlength - l;
519     return (l >= bq->minreq) ? l : 0;
520 }
521
522 size_t pa_memblockq_get_minreq(pa_memblockq *bq) {
523     assert(bq);
524
525     return bq->minreq;
526 }
527
528 void pa_memblockq_seek(pa_memblockq *bq, int64_t offset, pa_seek_mode_t seek) {
529     assert(bq);
530
531     switch (seek) {
532         case PA_SEEK_RELATIVE:
533             bq->write_index += offset;
534             return;
535         case PA_SEEK_ABSOLUTE:
536             bq->write_index = offset;
537             return;
538         case PA_SEEK_RELATIVE_ON_READ:
539             bq->write_index = bq->read_index + offset;
540             return;
541         case PA_SEEK_RELATIVE_END:
542             bq->write_index = (bq->blocks_tail ? bq->blocks_tail->index + (int64_t)bq->blocks_tail->chunk.length : bq->read_index) + offset;
543             return;
544     }
545
546     assert(0);
547 }
548
549 void pa_memblockq_flush(pa_memblockq *bq) {
550     assert(bq);
551
552     while (bq->blocks)
553         drop_block(bq, bq->blocks);
554
555     assert(bq->n_blocks == 0);
556
557     bq->write_index = bq->read_index;
558
559     pa_memblockq_prebuf_force(bq);
560 }
561
562 size_t pa_memblockq_get_tlength(pa_memblockq *bq) {
563     assert(bq);
564
565     return bq->tlength;
566 }
567
568 int64_t pa_memblockq_get_read_index(pa_memblockq *bq) {
569     assert(bq);
570     return bq->read_index;
571 }
572
573 int64_t pa_memblockq_get_write_index(pa_memblockq *bq) {
574     assert(bq);
575     return bq->write_index;
576 }
577
578 int pa_memblockq_push_align(pa_memblockq* bq, const pa_memchunk *chunk) {
579     pa_memchunk rchunk;
580
581     assert(bq);
582     assert(chunk && bq->base);
583
584     if (bq->base == 1)
585         return pa_memblockq_push(bq, chunk);
586
587     if (!bq->mcalign)
588         bq->mcalign = pa_mcalign_new(bq->base);
589
590     if (!can_push(bq, pa_mcalign_csize(bq->mcalign, chunk->length)))
591         return -1;
592
593     pa_mcalign_push(bq->mcalign, chunk);
594
595     while (pa_mcalign_pop(bq->mcalign, &rchunk) >= 0) {
596         int r;
597         r = pa_memblockq_push(bq, &rchunk);
598         pa_memblock_unref(rchunk.memblock);
599
600         if (r < 0)
601             return -1;
602     }
603
604     return 0;
605 }
606
607 void pa_memblockq_shorten(pa_memblockq *bq, size_t length) {
608     size_t l;
609     assert(bq);
610
611     l = pa_memblockq_get_length(bq);
612
613     if (l > length)
614         pa_memblockq_drop(bq, NULL, l - length);
615 }
616
617 void pa_memblockq_prebuf_disable(pa_memblockq *bq) {
618     assert(bq);
619
620     if (bq->state == PREBUF)
621         bq->state = RUNNING;
622 }
623
624 void pa_memblockq_prebuf_force(pa_memblockq *bq) {
625     assert(bq);
626
627     if (bq->state == RUNNING && bq->prebuf > 0)
628         bq->state = PREBUF;
629 }
630
631 size_t pa_memblockq_get_maxlength(pa_memblockq *bq) {
632     assert(bq);
633
634     return bq->maxlength;
635 }
636
637 size_t pa_memblockq_get_prebuf(pa_memblockq *bq) {
638     assert(bq);
639
640     return bq->prebuf;
641 }