A couple more tweaks
[Rockbox.git] / apps / buffering.c
blob08c7357429641af9940f6f01973b57271f32ebcf
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2007 Nicolas Pennequin
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #include "config.h"
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <ctype.h>
25 #include "buffering.h"
27 #include "ata.h"
28 #include "system.h"
29 #include "thread.h"
30 #include "file.h"
31 #include "panic.h"
32 #include "memory.h"
33 #include "lcd.h"
34 #include "font.h"
35 #include "button.h"
36 #include "kernel.h"
37 #include "tree.h"
38 #include "debug.h"
39 #include "sprintf.h"
40 #include "settings.h"
41 #include "codecs.h"
42 #include "audio.h"
43 #include "logf.h"
44 #include "mp3_playback.h"
45 #include "usb.h"
46 #include "status.h"
47 #include "screens.h"
48 #include "playlist.h"
49 #include "playback.h"
50 #include "pcmbuf.h"
51 #include "buffer.h"
53 #ifdef SIMULATOR
54 #define ata_disk_is_active() 1
55 #endif
57 #if MEM > 1
58 #define GUARD_BUFSIZE (32*1024)
59 #else
60 #define GUARD_BUFSIZE (8*1024)
61 #endif
63 /* amount of data to read in one read() call */
64 #define AUDIO_DEFAULT_FILECHUNK (1024*32)
66 /* Ring buffer helper macros */
67 /* Buffer pointer (p) plus value (v), wrapped if necessary */
68 #define RINGBUF_ADD(p,v) ((p+v)<buffer_len ? p+v : p+v-buffer_len)
69 /* Buffer pointer (p) minus value (v), wrapped if necessary */
70 #define RINGBUF_SUB(p,v) ((p>=v) ? p-v : p+buffer_len-v)
71 /* How far value (v) plus buffer pointer (p1) will cross buffer pointer (p2) */
72 #define RINGBUF_ADD_CROSS(p1,v,p2) \
73 ((p1<p2) ? (int)(p1+v)-(int)p2 : (int)(p1+v-p2)-(int)buffer_len)
74 /* Bytes available in the buffer */
75 #define BUF_USED RINGBUF_SUB(buf_widx, buf_ridx)
77 struct memory_handle {
78 int id; /* A unique ID for the handle */
79 enum data_type type;
80 char path[MAX_PATH];
81 int fd;
82 size_t data; /* Start index of the handle's data buffer */
83 size_t ridx; /* Current read pointer, relative to the main buffer */
84 size_t widx; /* Current write pointer */
85 size_t filesize; /* File total length */
86 size_t filerem; /* Remaining bytes of file NOT in buffer */
87 size_t available; /* Available bytes to read from buffer */
88 size_t offset; /* Offset at which we started reading the file */
89 struct memory_handle *next;
91 /* at all times, we have: filesize == offset + available + filerem */
94 static char *buffer;
95 static char *guard_buffer;
97 static size_t buffer_len;
99 static size_t buf_widx; /* current writing position */
100 static size_t buf_ridx; /* current reading position */
101 /* buf_*idx are values relative to the buffer, not real pointers. */
103 static size_t conf_filechunk;
105 /* current memory handle in the linked list. NULL when the list is empty. */
106 static struct memory_handle *cur_handle;
107 /* first memory handle in the linked list. NULL when the list is empty. */
108 static struct memory_handle *first_handle;
110 static int num_handles; /* number of handles in the list */
112 /* Handle cache (makes find_handle faster).
113 These need to be global so that move_handle can invalidate them. */
114 static int cached_handle_id = -1;
115 static struct memory_handle *cached_handle = NULL;
119 LINKED LIST MANAGEMENT
120 ======================
122 add_handle : Add a handle to the list
123 rm_handle : Remove a handle from the list
124 find_handle : Get a handle pointer from an ID
125 move_handle : Move a handle in the buffer (with or without its data)
127 These functions only handle the linked list structure. They don't touch the
128 contents of the struct memory_handle headers. They also change the buf_*idx
129 pointers when necessary and manage the handle IDs.
131 The first and current (== last) handle are kept track of.
132 A new handle is added at buf_widx and becomes the current one.
133 buf_widx always points to the current writing position for the current handle
134 buf_ridx always points to the location of the first handle.
135 buf_ridx == buf_widx means the buffer is empty.
139 /* Add a new handle to the linked list and return it. It will have become the
140 new current handle. The handle will reserve "data_size" bytes or if that's
141 not possible, decrease "data_size" to allow adding the handle. */
142 static struct memory_handle *add_handle(size_t *data_size)
144 /* this will give each handle a unique id */
145 static int cur_handle_id = 1;
147 /* make sure buf_widx is 32-bit aligned so that the handle struct is,
148 but before that we check we can actually align. */
149 if (RINGBUF_ADD_CROSS(buf_widx, 3, buf_ridx) >= 0) {
150 return NULL;
152 buf_widx = (RINGBUF_ADD(buf_widx, 3)) & ~3;
154 size_t len = (data_size ? *data_size : 0)
155 + sizeof(struct memory_handle);
157 /* check that we actually can add the handle and its data */
158 int overlap = RINGBUF_ADD_CROSS(buf_widx, len, buf_ridx);
159 if (overlap >= 0) {
160 *data_size -= overlap;
161 len -= overlap;
163 if (len < sizeof(struct memory_handle)) {
164 /* There isn't even enough space to write the struct */
165 return NULL;
168 struct memory_handle *new_handle =
169 (struct memory_handle *)(&buffer[buf_widx]);
171 /* only advance the buffer write index of the size of the struct */
172 buf_widx = RINGBUF_ADD(buf_widx, sizeof(struct memory_handle));
174 if (!first_handle) {
175 /* the new handle is the first one */
176 first_handle = new_handle;
179 if (cur_handle) {
180 cur_handle->next = new_handle;
183 cur_handle = new_handle;
184 cur_handle->id = cur_handle_id++;
185 cur_handle->next = NULL;
186 num_handles++;
187 return cur_handle;
190 /* Delete a given memory handle from the linked list
191 and return true for success. Nothing is actually erased from memory. */
192 static bool rm_handle(struct memory_handle *h)
194 if (h == first_handle) {
195 first_handle = h->next;
196 if (h == cur_handle) {
197 /* h was the first and last handle: the buffer is now empty */
198 cur_handle = NULL;
199 buf_ridx = buf_widx;
200 } else {
201 /* update buf_ridx to point to the new first handle */
202 buf_ridx = (void *)first_handle - (void *)buffer;
204 } else {
205 struct memory_handle *m = first_handle;
206 while (m && m->next != h) {
207 m = m->next;
209 if (h && m && m->next == h) {
210 m->next = h->next;
211 if (h == cur_handle) {
212 cur_handle = m;
214 } else {
215 return false;
219 num_handles--;
220 return true;
223 /* Return a pointer to the memory handle of given ID.
224 NULL if the handle wasn't found */
225 static struct memory_handle *find_handle(int handle_id)
227 /* simple caching because most of the time the requested handle
228 will either be the same as the last, or the one after the last */
229 if (cached_handle)
231 if (cached_handle_id == handle_id &&
232 cached_handle_id == cached_handle->id)
233 return cached_handle;
234 else if (cached_handle->next && (cached_handle->next->id == handle_id))
236 /* JD's quick testing showd this block was only entered
237 2/1971 calls to find_handle.
238 8/1971 calls to find_handle resulted in a cache miss */
239 cached_handle = cached_handle->next;
240 cached_handle_id = handle_id;
241 return cached_handle;
245 struct memory_handle *m = first_handle;
246 while (m && m->id != handle_id) {
247 m = m->next;
249 cached_handle_id = handle_id;
250 cached_handle = m;
251 return (m && m->id == handle_id) ? m : NULL;
254 /* Move a memory handle and data_size of its data of delta.
255 Return a pointer to the new location of the handle.
256 delta is the value of which to move the struct data.
257 data_size is the amount of data to move along with the struct. */
258 static struct memory_handle *move_handle(struct memory_handle *h,
259 size_t *delta, size_t data_size)
261 if (*delta < 4) {
262 /* aligning backwards would yield a negative result,
263 and moving the handle of such a small amount is a waste
264 of time anyway. */
265 return NULL;
267 /* make sure delta is 32-bit aligned so that the handle struct is. */
268 *delta = (*delta - 3) & ~3;
270 size_t newpos = RINGBUF_ADD((void *)h - (void *)buffer, *delta);
272 struct memory_handle *dest = (struct memory_handle *)(&buffer[newpos]);
274 /* Invalidate the cache to prevent it from keeping the old location of h */
275 if (h == cached_handle)
276 cached_handle = NULL;
278 /* the cur_handle pointer might need updating */
279 if (h == cur_handle) {
280 cur_handle = dest;
283 if (h == first_handle) {
284 first_handle = dest;
285 buf_ridx = newpos;
286 } else {
287 struct memory_handle *m = first_handle;
288 while (m && m->next != h) {
289 m = m->next;
291 if (h && m && m->next == h) {
292 m->next = dest;
293 } else {
294 return NULL;
298 memmove(dest, h, sizeof(struct memory_handle) + data_size);
300 return dest;
305 BUFFER SPACE MANAGEMENT
306 =======================
308 buffer_handle : Buffer data for a handle
309 free_buffer : Free buffer space by moving a handle
310 fill_buffer : Call buffer_handle for all handles that have data to buffer
311 can_add_handle : Indicate whether it's safe to add a handle.
312 data_rem : Total amount of data needing to be buffered
313 wasted_space : Total amount of space available for freeing
315 These functions are used by the buffering thread to manage buffer space.
318 /* Buffer data for the given handle. Return the amount of data buffered
319 or -1 if the handle wasn't found */
320 static ssize_t buffer_handle(int handle_id)
322 DEBUGF("buffer_handle(%d)\n", handle_id);
323 struct memory_handle *h = find_handle(handle_id);
324 if (!h)
325 return -1;
327 if (h->filerem == 0) {
328 /* nothing left to buffer */
329 return 0;
332 if (h->fd < 0) /* file closed, reopen */
334 if (*h->path)
335 h->fd = open(h->path, O_RDONLY);
336 else
337 return -1;
339 if (h->fd < 0)
340 return -1;
342 if (h->offset)
343 lseek(h->fd, h->offset, SEEK_SET);
346 ssize_t ret = 0;
347 while (h->filerem > 0)
349 /* max amount to copy */
350 size_t copy_n = MIN( MIN(h->filerem, conf_filechunk),
351 buffer_len - h->widx);
353 /* stop copying if it would overwrite the reading position
354 or the next handle */
355 if (RINGBUF_ADD_CROSS(h->widx, copy_n, buf_ridx) >= 0 || (h->next &&
356 RINGBUF_ADD_CROSS(h->widx, copy_n, (unsigned)
357 ((void *)h->next - (void *)buffer)) > 0))
358 break;
360 /* rc is the actual amount read */
361 int rc = read(h->fd, &buffer[h->widx], copy_n);
363 if (rc < 0)
365 if (h->type == TYPE_CODEC) {
366 DEBUGF("Partial codec\n");
367 break;
370 DEBUGF("File ended %ld bytes early\n", (long)h->filerem);
371 h->filesize -= h->filerem;
372 h->filerem = 0;
373 break;
376 /* Advance buffer */
377 h->widx = RINGBUF_ADD(h->widx, rc);
378 if (h == cur_handle)
379 buf_widx = h->widx;
380 h->available += rc;
381 ret += rc;
382 h->filerem -= rc;
385 if (h->filerem == 0) {
386 /* finished buffering the file */
387 close(h->fd);
388 h->fd = -1;
391 DEBUGF("buffered %ld bytes (%ld of %ld available, rem: %ld, off: %ld)\n",
392 ret, h->available, h->filesize, h->filerem, h->offset);
394 return ret;
397 /* Free buffer space by moving the handle struct right before the useful
398 part of its data buffer or by moving all the data. */
399 static void free_buffer(int handle_id)
401 struct memory_handle *h = find_handle(handle_id);
402 if (!h)
403 return;
405 size_t delta;
406 /* The value of delta might change for alignment reasons */
408 if (h->next && (h->type == TYPE_ID3 || h->type == TYPE_CUESHEET ||
409 h->type == TYPE_IMAGE) && h->filerem == 0 )
411 /* metadata handle: we can move all of it */
412 delta = RINGBUF_SUB( (unsigned)((void *)h->next - (void *)buffer),
413 h->data) - h->available;
414 h = move_handle(h, &delta, h->available);
415 if (!h) return;
416 h->data = RINGBUF_ADD(h->data, delta);
417 h->ridx = RINGBUF_ADD(h->ridx, delta);
418 h->widx = RINGBUF_ADD(h->widx, delta);
420 /* when moving a struct mp3entry we need to readjust its pointers. */
421 if (h->type == TYPE_ID3 && h->filesize == sizeof(struct mp3entry)) {
422 adjust_mp3entry((struct mp3entry *)&buffer[h->data],
423 (void *)&buffer[h->data],
424 (void *)&buffer[RINGBUF_SUB(h->data, delta)]);
427 DEBUGF("free_buffer(%d): metadata, moved by %ld bytes\n",
428 handle_id, delta);
430 else
432 /* only move the handle struct */
433 delta = RINGBUF_SUB(h->ridx, h->data);
434 h = move_handle(h, &delta, 0);
435 if (!h) return;
436 h->data = RINGBUF_ADD(h->data, delta);
437 h->available -= delta;
438 h->offset += delta;
439 DEBUGF("free_buffer(%d): audio, %ld bytes freed\n", handle_id, delta);
443 /* Fill the buffer by buffering as much data as possible for handles that still
444 have data left to buffer */
445 static void fill_buffer(void)
447 DEBUGF("fill buffer()\n");
448 struct memory_handle *m = first_handle;
449 while (m) {
450 if (m->filerem > 0) {
451 buffer_handle(m->id);
453 m = m->next;
457 /* Check whether it's safe to add a new handle and reserve space to let the
458 current one finish buffering its data. Used by bufopen and bufgetdata as
459 a preliminary check before even trying to physically add the handle.
460 Returns true if it's ok to add a new handle, false if not.
462 static bool can_add_handle(void)
464 if (cur_handle && cur_handle->filerem > 0) {
465 /* the current handle hasn't finished buffering. We can only add
466 a new one if there is already enough free space to finish
467 the buffering. */
468 if (cur_handle->filerem < (buffer_len - BUF_USED)) {
469 /* Before adding the new handle we reserve some space for the
470 current one to finish buffering its data. */
471 buf_widx = RINGBUF_ADD(buf_widx, cur_handle->filerem);
472 } else {
473 return false;
477 return true;
480 /* Return the total amount of data left to be buffered for all the handles */
481 static size_t data_rem(void)
483 size_t ret = 0;
485 struct memory_handle *m = first_handle;
486 while (m) {
487 ret += m->filerem;
488 m = m->next;
491 return ret;
494 /* Return the amount of data we have but don't need anymore. This data can be
495 safely erased to reclaim buffer space. */
496 static size_t wasted_space(void)
498 size_t ret = 0;
500 struct memory_handle *m = first_handle;
501 while (m) {
502 ret += RINGBUF_SUB(m->ridx, m->data);
503 m = m->next;
506 return ret;
511 BUFFERING API FUNCTIONS
512 =======================
514 bufopen : Request the opening of a new handle for a file
515 bufalloc : Open a new handle for data other than a file.
516 bufclose : Close an open handle
517 bufseek : Set the read pointer in a handle
518 bufadvance : Move the read pointer in a handle
519 bufread : Copy data from a handle into a given buffer
520 bufgetdata : Give a pointer to the handle's data
521 bufused : Return the amount of buffer space used
523 These functions are exported, to allow interaction with the buffer.
524 They take care of the content of the structs, and rely on the linked list
525 management functions for all the actual handle management work.
529 /* Request a file be buffered
530 filename: name of the file to open
531 offset: offset at which to start buffering the file, useful when the first
532 (offset-1) bytes of the file aren't needed.
533 return value: <0 if the file cannot be opened, or one file already
534 queued to be opened, otherwise the handle for the file in the buffer
536 int bufopen(char *file, size_t offset, enum data_type type)
538 if (!can_add_handle())
539 return -2;
541 int fd = open(file, O_RDONLY);
542 if (fd < 0)
543 return -1;
545 size_t size = filesize(fd) - offset;
547 if (type != TYPE_AUDIO &&
548 size + sizeof(struct memory_handle) > buffer_len - buf_widx)
550 /* for types other than audio, the data can't wrap */
551 return -2;
554 DEBUGF("bufopen: %s (offset: %ld) (%ld bytes needed)...\n",
555 file, offset, size);
557 struct memory_handle *h = add_handle(&size);
558 if (!h)
560 DEBUGF("failed to add handle\n");
561 close(fd);
562 return -2;
565 if (offset) lseek(fd, offset, SEEK_SET);
566 strncpy(h->path, file, MAX_PATH);
567 h->fd = fd;
568 h->filesize = filesize(fd);
569 h->filerem = h->filesize - offset;
570 h->offset = offset;
571 h->ridx = buf_widx;
572 h->widx = buf_widx;
573 h->data = buf_widx;
574 h->available = 0;
575 h->type = type;
577 DEBUGF("allocated %ld bytes. ID: %d\n", size, h->id);
579 if (type == TYPE_CODEC || type == TYPE_CUESHEET || type == TYPE_IMAGE) {
580 /* Immediately buffer those */
581 ssize_t ret = buffer_handle(h->id);
583 /* Check that we got the complete file */
584 if ((size_t)ret != h->filesize) {
585 bufclose(h->id);
586 return -3;
590 return h->id;
593 /* Open a new handle from data that isn't in a file.
594 src is the source buffer from which to copy data. It can be NULL to simply
595 reserve buffer space.
596 size is the requested size. The call will only be successful if the
597 requested amount of data can entirely fit in the buffer without wrapping.
598 Return value is the handle id for success or <0 for failure.
600 int bufalloc(void *src, size_t size, enum data_type type)
602 if (!can_add_handle())
603 return -2;
605 if (size + sizeof(struct memory_handle) > buffer_len - buf_widx)
606 /* The data would need to wrap. */
607 return -2;
609 size_t allocsize = size;
610 struct memory_handle *h = add_handle(&allocsize);
612 if (!h || allocsize != size)
613 return -2;
615 if (src) {
616 if (type == TYPE_ID3 && size == sizeof(struct mp3entry)) {
617 /* specially take care of struct mp3entry */
618 copy_mp3entry((struct mp3entry *)&buffer[buf_widx],
619 (struct mp3entry *)src);
620 } else {
621 memcpy(&buffer[buf_widx], src, size);
625 h->fd = -1;
626 *h->path = 0;
627 h->filesize = size;
628 h->filerem = 0;
629 h->offset = 0;
630 h->ridx = buf_widx;
631 h->widx = buf_widx;
632 h->data = buf_widx;
633 h->available = size;
634 h->type = type;
636 buf_widx = RINGBUF_ADD(buf_widx, size);
638 return h->id;
641 /* Close the handle. Return 0 for success and < 0 for failure */
642 int bufclose(int handle_id)
644 DEBUGF("bufclose(%d)\n", handle_id);
645 struct memory_handle *h = find_handle(handle_id);
646 if (!h)
647 return -1;
649 rm_handle(h);
650 return 0;
653 /* Set reading index in handle (relatively to the start of the file).
654 Access before the available data will trigger a rebuffer.
655 TODO: Test this
656 TODO: Maybe force an immediate rebuffer by calling buffer_handle() ?
657 Return 0 for success and < 0 for failure:
658 -1 if the handle wasn't found
659 -2 if there is no data available at the new position
660 (the reading index is still moved)
661 -3 if the new requested position was beyond the end of the file
663 int bufseek(int handle_id, size_t newpos)
665 int ret = 0;
666 struct memory_handle *h = find_handle(handle_id);
667 if (!h)
668 return -1;
670 if (newpos > h->filesize) {
671 /* access beyond the end of the file */
672 return -3;
675 else if (newpos < h->offset) {
676 /* access before what we currently have. A rebuffer is needed. */
677 h->offset = newpos;
678 h->available = 0;
679 h->filerem = h->filesize - newpos;
680 /* having changed filerem should be enough to trigger the rebuffer. */
681 h->widx = h->data;
682 ret = -2;
685 else if (newpos > h->offset + h->available) {
686 /* data isn't available yet. */
687 ret = -2;
690 h->ridx = RINGBUF_ADD(h->data, newpos);
691 return ret;
694 /* Advance the reading index in a handle (relatively to its current position).
695 Return 0 for success and < 0 for failure
696 TODO: Add some rebuffering like in bufseek */
697 int bufadvance(int handle_id, off_t offset)
699 struct memory_handle *h = find_handle(handle_id);
700 if (!h)
701 return -1;
703 if (offset >= 0)
705 /* check for access beyond what's available */
706 if ((size_t)offset > (h->available - RINGBUF_SUB(h->ridx, h->data)))
707 return -2;
709 h->ridx = RINGBUF_ADD(h->ridx, offset);
711 else
713 /* check for access before what's available */
714 if ((size_t)(-offset) > RINGBUF_SUB(h->ridx, h->data))
715 return -2;
717 h->ridx = RINGBUF_SUB(h->ridx, (size_t)(-offset));
720 return 0;
723 /* Copy data from the given handle to the dest buffer.
724 Return the number of bytes copied or < 0 for failure. */
725 ssize_t bufread(int handle_id, size_t size, char *dest)
727 struct memory_handle *h = find_handle(handle_id);
728 size_t buffered_data;
729 if (!h)
730 return -1;
732 if (h->available < size && h->filerem > 0) /* Data isn't ready */
733 return -2;
735 if (h->available == 0 && h->filerem == 0) /* File is finished reading */
736 return 0;
738 buffered_data = MIN(size, h->available - RINGBUF_SUB(h->ridx, h->data));
740 if (h->ridx + buffered_data > buffer_len)
742 /* the data wraps around the end of the buffer */
743 size_t read = buffer_len - h->ridx;
744 memcpy(dest, &buffer[h->ridx], read);
745 memcpy(dest+read, buffer, buffered_data - read);
747 else memcpy(dest, &buffer[h->ridx], buffered_data);
749 return buffered_data;
752 /* Update the "data" pointer to make the handle's data available to the caller.
753 Return the length of the available linear data or < 0 for failure.
754 size is the amount of linear data requested. it can be 0 to get as
755 much as possible.
756 The guard buffer may be used to provide the requested size */
757 ssize_t bufgetdata(int handle_id, size_t size, unsigned char **data)
759 struct memory_handle *h = find_handle(handle_id);
760 if (!h)
761 return -1;
763 if (h->available < size && h->filerem > 0) /* Data isn't ready */
764 return -2;
766 if (h->available == 0 && h->filerem == 0) /* File is finished reading */
767 return 0;
769 ssize_t ret;
771 if (h->ridx + size > buffer_len &&
772 h->available - RINGBUF_SUB(h->ridx, h->data) >= size)
774 /* the data wraps around the end of the buffer :
775 use the guard buffer to provide the requested amount of data. */
776 size_t copy_n = MIN(h->ridx + size - buffer_len, GUARD_BUFSIZE);
777 memcpy(guard_buffer, (unsigned char *)buffer, copy_n);
778 ret = buffer_len - h->ridx + copy_n;
779 DEBUGF("used the guard buffer to complete\n");
781 else
783 ret = MIN(h->available - RINGBUF_SUB(h->ridx, h->data),
784 buffer_len - h->ridx);
787 *data = (unsigned char *)&buffer[h->ridx];
789 /* DEBUGF("bufgetdata(%d): h->ridx=%ld, ret=%ld\n", handle_id,
790 (long)h->ridx, ret); */
791 return ret;
794 /* Return the amount of buffer space used */
795 size_t bufused(void)
797 return BUF_USED;
800 bool buffering_init(char *filebuf, size_t filebuflen)
802 if (!filebuf || !filebuflen)
803 return false;
805 buffer = filebuf;
806 buffer_len = filebuflen;
807 guard_buffer = buffer + buffer_len;
809 buf_widx = 0;
810 buf_ridx = 0;
812 first_handle = NULL;
813 num_handles = 0;
815 conf_filechunk = AUDIO_DEFAULT_FILECHUNK;
817 return true;
820 void buffering_thread(void)
822 while (true)
824 if (data_rem() > 0 && wasted_space() > buffer_len/5) {
825 DEBUGF("there is %ld bytes of wasted space\n", wasted_space());
827 /* free buffer from outdated audio data */
828 struct memory_handle *m = first_handle;
829 while (m) {
830 if (m->type == TYPE_AUDIO)
831 free_buffer(m->id);
832 m = m->next;
835 /* free buffer by moving metadata */
836 m = first_handle;
837 while (m) {
838 if (m->type != TYPE_AUDIO)
839 free_buffer(m->id);
840 m = m->next;
845 if (data_rem() > 0 && BUF_USED < 3*buffer_len/4 &&
846 ata_disk_is_active())
848 DEBUGF("%ld bytes left to buffer and the buffer is low\n",
849 data_rem());
850 fill_buffer();
851 } else {
852 sleep(HZ/2);