FS#7980: Select default option in an option list
[kugel-rb.git] / apps / buffering.c
blob9dafa673b01726e3052aee4f1c02e9a0147d3257
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 "mp3_playback.h"
44 #include "usb.h"
45 #include "status.h"
46 #include "screens.h"
47 #include "playlist.h"
48 #include "playback.h"
49 #include "pcmbuf.h"
50 #include "buffer.h"
51 #include "bmp.h"
52 #include "events.h"
53 #include "metadata.h"
55 #ifdef SIMULATOR
56 #define ata_disk_is_active() 1
57 #endif
59 #if MEM > 1
60 #define GUARD_BUFSIZE (32*1024)
61 #else
62 #define GUARD_BUFSIZE (8*1024)
63 #endif
65 /* Define LOGF_ENABLE to enable logf output in this file */
66 /*#define LOGF_ENABLE*/
67 #include "logf.h"
69 /* macros to enable logf for queues
70 logging on SYS_TIMEOUT can be disabled */
71 #ifdef SIMULATOR
72 /* Define this for logf output of all queuing except SYS_TIMEOUT */
73 #define BUFFERING_LOGQUEUES
74 /* Define this to logf SYS_TIMEOUT messages */
75 /* #define BUFFERING_LOGQUEUES_SYS_TIMEOUT */
76 #endif
78 #ifdef BUFFERING_LOGQUEUES
79 #define LOGFQUEUE logf
80 #else
81 #define LOGFQUEUE(...)
82 #endif
84 #ifdef BUFFERING_LOGQUEUES_SYS_TIMEOUT
85 #define LOGFQUEUE_SYS_TIMEOUT logf
86 #else
87 #define LOGFQUEUE_SYS_TIMEOUT(...)
88 #endif
90 /* default point to start buffer refill */
91 #define BUFFERING_DEFAULT_WATERMARK (1024*512)
92 /* amount of data to read in one read() call */
93 #define BUFFERING_DEFAULT_FILECHUNK (1024*32)
94 /* point at which the file buffer will fight for CPU time */
95 #define BUFFERING_CRITICAL_LEVEL (1024*128)
97 #define BUF_HANDLE_MASK 0x7FFFFFFF
100 /* Ring buffer helper macros */
101 /* Buffer pointer (p) plus value (v), wrapped if necessary */
102 #define RINGBUF_ADD(p,v) (((p)+(v))<buffer_len ? (p)+(v) : (p)+(v)-buffer_len)
103 /* Buffer pointer (p) minus value (v), wrapped if necessary */
104 #define RINGBUF_SUB(p,v) ((p>=v) ? (p)-(v) : (p)+buffer_len-(v))
105 /* How far value (v) plus buffer pointer (p1) will cross buffer pointer (p2) */
106 #define RINGBUF_ADD_CROSS(p1,v,p2) \
107 ((p1<p2) ? (int)((p1)+(v))-(int)(p2) : (int)((p1)+(v)-(p2))-(int)buffer_len)
108 /* Bytes available in the buffer */
109 #define BUF_USED RINGBUF_SUB(buf_widx, buf_ridx)
111 /* assert(sizeof(struct memory_handle)%4==0) */
112 struct memory_handle {
113 int id; /* A unique ID for the handle */
114 enum data_type type; /* Type of data buffered with this handle */
115 char path[MAX_PATH]; /* Path if data originated in a file */
116 int fd; /* File descriptor to path (-1 if closed) */
117 size_t data; /* Start index of the handle's data buffer */
118 volatile size_t ridx; /* Read pointer, relative to the main buffer */
119 size_t widx; /* Write pointer */
120 size_t filesize; /* File total length */
121 size_t filerem; /* Remaining bytes of file NOT in buffer */
122 volatile size_t available; /* Available bytes to read from buffer */
123 size_t offset; /* Offset at which we started reading the file */
124 struct memory_handle *next;
126 /* invariant: filesize == offset + available + filerem */
128 static char *buffer;
129 static char *guard_buffer;
131 static size_t buffer_len;
133 static volatile size_t buf_widx; /* current writing position */
134 static volatile size_t buf_ridx; /* current reading position */
135 /* buf_*idx are values relative to the buffer, not real pointers. */
137 /* Configuration */
138 static size_t conf_watermark = 0; /* Level to trigger filebuf fill */
139 #if MEM > 8
140 static size_t high_watermark = 0; /* High watermark for rebuffer */
141 #endif
143 /* current memory handle in the linked list. NULL when the list is empty. */
144 static struct memory_handle *cur_handle;
145 /* first memory handle in the linked list. NULL when the list is empty. */
146 static struct memory_handle *first_handle;
148 static int num_handles; /* number of handles in the list */
150 static int base_handle_id;
152 static struct mutex llist_mutex;
154 /* Handle cache (makes find_handle faster).
155 This is global so that move_handle and rm_handle can invalidate it. */
156 static struct memory_handle *cached_handle = NULL;
158 static struct {
159 size_t remaining; /* Amount of data needing to be buffered */
160 size_t wasted; /* Amount of space available for freeing */
161 size_t buffered; /* Amount of data currently in the buffer */
162 size_t useful; /* Amount of data still useful to the user */
163 } data_counters;
166 /* Messages available to communicate with the buffering thread */
167 enum {
168 Q_BUFFER_HANDLE = 1, /* Request buffering of a handle, this should not be
169 used in a low buffer situation. */
170 Q_RESET_HANDLE, /* (internal) Request resetting of a handle to its
171 offset (the offset has to be set beforehand) */
172 Q_CLOSE_HANDLE, /* Request closing a handle */
173 Q_BASE_HANDLE, /* Set the reference handle for buf_useful_data */
175 /* Configuration: */
176 Q_SET_WATERMARK,
177 Q_START_FILL, /* Request that the buffering thread initiate a buffer
178 fill at its earliest convenience */
179 Q_HANDLE_ADDED, /* Inform the buffering thread that a handle was added,
180 (which means the disk is spinning) */
183 /* Buffering thread */
184 static void buffering_thread(void);
185 static long buffering_stack[(DEFAULT_STACK_SIZE + 0x2000)/sizeof(long)];
186 static const char buffering_thread_name[] = "buffering";
187 static struct thread_entry *buffering_thread_p;
188 static struct event_queue buffering_queue;
189 static struct queue_sender_list buffering_queue_sender_list;
194 LINKED LIST MANAGEMENT
195 ======================
197 add_handle : Add a handle to the list
198 rm_handle : Remove a handle from the list
199 find_handle : Get a handle pointer from an ID
200 move_handle : Move a handle in the buffer (with or without its data)
202 These functions only handle the linked list structure. They don't touch the
203 contents of the struct memory_handle headers. They also change the buf_*idx
204 pointers when necessary and manage the handle IDs.
206 The first and current (== last) handle are kept track of.
207 A new handle is added at buf_widx and becomes the current one.
208 buf_widx always points to the current writing position for the current handle
209 buf_ridx always points to the location of the first handle.
210 buf_ridx == buf_widx means the buffer is empty.
214 /* Add a new handle to the linked list and return it. It will have become the
215 new current handle.
216 data_size must contain the size of what will be in the handle.
217 can_wrap tells us whether this type of data may wrap on buffer
218 alloc_all tells us if we must immediately be able to allocate data_size
219 returns a valid memory handle if all conditions for allocation are met.
220 NULL if there memory_handle itself cannot be allocated or if the
221 data_size cannot be allocated and alloc_all is set. This function's
222 only potential side effect is to allocate space for the cur_handle
223 if it returns NULL.
225 static struct memory_handle *add_handle(size_t data_size, bool can_wrap,
226 bool alloc_all)
228 /* gives each handle a unique id */
229 static int cur_handle_id = 0;
230 size_t shift;
231 size_t new_widx;
232 size_t len;
233 int overlap;
235 if (num_handles >= BUF_MAX_HANDLES)
236 return NULL;
238 mutex_lock(&llist_mutex);
240 if (cur_handle && cur_handle->filerem > 0) {
241 /* the current handle hasn't finished buffering. We can only add
242 a new one if there is already enough free space to finish
243 the buffering. */
244 size_t req = cur_handle->filerem + sizeof(struct memory_handle);
245 if (RINGBUF_ADD_CROSS(cur_handle->widx, req, buf_ridx) >= 0) {
246 /* Not enough space */
247 mutex_unlock(&llist_mutex);
248 return NULL;
249 } else {
250 /* Allocate the remainder of the space for the current handle */
251 buf_widx = RINGBUF_ADD(cur_handle->widx, cur_handle->filerem);
255 /* align to 4 bytes up */
256 new_widx = RINGBUF_ADD(buf_widx, 3) & ~3;
258 len = data_size + sizeof(struct memory_handle);
260 /* First, will the handle wrap? */
261 overlap = RINGBUF_ADD_CROSS(new_widx, sizeof(struct memory_handle),
262 buffer_len - 1);
263 /* If the handle would wrap, move to the beginning of the buffer,
264 * otherwise check if the data can/would wrap and move it to the
265 * beginning if needed */
266 if (overlap > 0) {
267 new_widx = 0;
268 } else if (!can_wrap) {
269 overlap = RINGBUF_ADD_CROSS(new_widx, len, buffer_len - 1);
270 if (overlap > 0)
271 new_widx += data_size - overlap;
274 /* How far we shifted buf_widx to align things, must be < buffer_len */
275 shift = RINGBUF_SUB(new_widx, buf_widx);
277 /* How much space are we short in the actual ring buffer? */
278 overlap = RINGBUF_ADD_CROSS(buf_widx, shift + len, buf_ridx);
279 if (overlap >= 0 && (alloc_all || (unsigned)overlap > data_size)) {
280 /* Not enough space for required allocations */
281 mutex_unlock(&llist_mutex);
282 return NULL;
285 /* There is enough space for the required data, advance the buf_widx and
286 * initialize the struct */
287 buf_widx = new_widx;
289 struct memory_handle *new_handle =
290 (struct memory_handle *)(&buffer[buf_widx]);
292 /* only advance the buffer write index of the size of the struct */
293 buf_widx = RINGBUF_ADD(buf_widx, sizeof(struct memory_handle));
295 new_handle->id = cur_handle_id;
296 /* Wrap signed int is safe and 0 doesn't happen */
297 cur_handle_id = (cur_handle_id + 1) & BUF_HANDLE_MASK;
298 new_handle->next = NULL;
299 num_handles++;
301 if (!first_handle)
302 /* the new handle is the first one */
303 first_handle = new_handle;
305 if (cur_handle)
306 cur_handle->next = new_handle;
308 cur_handle = new_handle;
310 mutex_unlock(&llist_mutex);
311 return new_handle;
314 /* Delete a given memory handle from the linked list
315 and return true for success. Nothing is actually erased from memory. */
316 static bool rm_handle(const struct memory_handle *h)
318 if (h == NULL)
319 return true;
321 mutex_lock(&llist_mutex);
323 if (h == first_handle) {
324 first_handle = h->next;
325 if (h == cur_handle) {
326 /* h was the first and last handle: the buffer is now empty */
327 cur_handle = NULL;
328 buf_ridx = buf_widx = 0;
329 } else {
330 /* update buf_ridx to point to the new first handle */
331 buf_ridx = (void *)first_handle - (void *)buffer;
333 } else {
334 struct memory_handle *m = first_handle;
335 /* Find the previous handle */
336 while (m && m->next != h) {
337 m = m->next;
339 if (m && m->next == h) {
340 m->next = h->next;
341 if (h == cur_handle) {
342 cur_handle = m;
343 buf_widx = cur_handle->widx;
345 } else {
346 mutex_unlock(&llist_mutex);
347 return false;
351 /* Invalidate the cache to prevent it from keeping the old location of h */
352 if (h == cached_handle)
353 cached_handle = NULL;
355 num_handles--;
357 mutex_unlock(&llist_mutex);
358 return true;
361 /* Return a pointer to the memory handle of given ID.
362 NULL if the handle wasn't found */
363 static struct memory_handle *find_handle(int handle_id)
365 if (handle_id < 0)
366 return NULL;
368 mutex_lock(&llist_mutex);
370 /* simple caching because most of the time the requested handle
371 will either be the same as the last, or the one after the last */
372 if (cached_handle)
374 if (cached_handle->id == handle_id) {
375 mutex_unlock(&llist_mutex);
376 return cached_handle;
377 } else if (cached_handle->next &&
378 (cached_handle->next->id == handle_id)) {
379 cached_handle = cached_handle->next;
380 mutex_unlock(&llist_mutex);
381 return cached_handle;
385 struct memory_handle *m = first_handle;
386 while (m && m->id != handle_id) {
387 m = m->next;
389 /* This condition can only be reached with !m or m->id == handle_id */
390 if (m)
391 cached_handle = m;
393 mutex_unlock(&llist_mutex);
394 return m;
397 /* Move a memory handle and data_size of its data delta bytes along the buffer.
398 delta maximum bytes available to move the handle. If the move is performed
399 it is set to the actual distance moved.
400 data_size is the amount of data to move along with the struct.
401 returns a valid memory_handle if the move is successful
402 NULL if the handle is NULL, the move would be less than the size of
403 a memory_handle after correcting for wraps or if the handle is not
404 found in the linked list for adjustment. This function has no side
405 effects if NULL is returned. */
406 static bool move_handle(struct memory_handle **h, size_t *delta,
407 size_t data_size, bool can_wrap)
409 struct memory_handle *dest;
410 const struct memory_handle *src;
411 size_t newpos;
412 size_t size_to_move;
413 size_t final_delta = *delta;
414 int overlap;
416 if (h == NULL || (src = *h) == NULL)
417 return false;
419 size_to_move = sizeof(struct memory_handle) + data_size;
421 /* Align to four bytes, down */
422 final_delta &= ~3;
423 if (final_delta < sizeof(struct memory_handle)) {
424 /* It's not legal to move less than the size of the struct */
425 return false;
428 mutex_lock(&llist_mutex);
430 newpos = RINGBUF_ADD((void *)src - (void *)buffer, final_delta);
431 overlap = RINGBUF_ADD_CROSS(newpos, size_to_move, buffer_len - 1);
433 if (overlap > 0) {
434 /* Some part of the struct + data would wrap, maybe ok */
435 size_t correction = 0;
436 /* If the overlap lands inside the memory_handle */
437 if ((unsigned)overlap > data_size) {
438 /* Correct the position and real delta to prevent the struct from
439 * wrapping, this guarantees an aligned delta, I think */
440 correction = overlap - data_size;
441 } else if (!can_wrap) {
442 /* Otherwise the overlap falls in the data area and must all be
443 * backed out. This may become conditional if ever we move
444 * data that is allowed to wrap (ie audio) */
445 correction = overlap;
446 /* Align correction to four bytes, up */
447 correction = (correction+3) & ~3;
449 if (correction) {
450 if (final_delta < correction + sizeof(struct memory_handle)) {
451 /* Delta cannot end up less than the size of the struct */
452 mutex_unlock(&llist_mutex);
453 return false;
456 newpos -= correction;
457 overlap -= correction;/* Used below to know how to split the data */
458 final_delta -= correction;
462 dest = (struct memory_handle *)(&buffer[newpos]);
464 if (src == first_handle) {
465 first_handle = dest;
466 buf_ridx = newpos;
467 } else {
468 struct memory_handle *m = first_handle;
469 while (m && m->next != src) {
470 m = m->next;
472 if (m && m->next == src) {
473 m->next = dest;
474 } else {
475 mutex_unlock(&llist_mutex);
476 return false;
481 /* Update the cache to prevent it from keeping the old location of h */
482 if (src == cached_handle)
483 cached_handle = dest;
485 /* the cur_handle pointer might need updating */
486 if (src == cur_handle)
487 cur_handle = dest;
489 if (overlap > 0) {
490 size_t first_part = size_to_move - overlap;
491 memmove(dest, src, first_part);
492 memmove(buffer, (const char *)src + first_part, overlap);
493 } else {
494 memmove(dest, src, size_to_move);
497 /* Update the caller with the new location of h and the distance moved */
498 *h = dest;
499 *delta = final_delta;
500 mutex_unlock(&llist_mutex);
501 return dest;
506 BUFFER SPACE MANAGEMENT
507 =======================
509 update_data_counters: Updates the values in data_counters
510 buffer_is_low : Returns true if the amount of useful data in the buffer is low
511 buffer_handle : Buffer data for a handle
512 reset_handle : Reset write position and data buffer of a handle to its offset
513 rebuffer_handle : Seek to a nonbuffered part of a handle by rebuffering the data
514 shrink_handle : Free buffer space by moving a handle
515 fill_buffer : Call buffer_handle for all handles that have data to buffer
517 These functions are used by the buffering thread to manage buffer space.
520 static void update_data_counters(void)
522 struct memory_handle *m = find_handle(base_handle_id);
523 bool is_useful = m==NULL;
525 size_t buffered = 0;
526 size_t wasted = 0;
527 size_t remaining = 0;
528 size_t useful = 0;
530 m = first_handle;
531 while (m) {
532 buffered += m->available;
533 wasted += RINGBUF_SUB(m->ridx, m->data);
534 remaining += m->filerem;
536 if (m->id == base_handle_id)
537 is_useful = true;
539 if (is_useful)
540 useful += RINGBUF_SUB(m->widx, m->ridx);
542 m = m->next;
545 data_counters.buffered = buffered;
546 data_counters.wasted = wasted;
547 data_counters.remaining = remaining;
548 data_counters.useful = useful;
551 static inline bool buffer_is_low(void)
553 update_data_counters();
554 return data_counters.useful < BUFFERING_CRITICAL_LEVEL;
557 /* Buffer data for the given handle.
558 Return whether or not the buffering should continue explicitly. */
559 static bool buffer_handle(int handle_id)
561 logf("buffer_handle(%d)", handle_id);
562 struct memory_handle *h = find_handle(handle_id);
563 if (!h)
564 return true;
566 if (h->filerem == 0) {
567 /* nothing left to buffer */
568 return true;
571 if (h->fd < 0) /* file closed, reopen */
573 if (*h->path)
574 h->fd = open(h->path, O_RDONLY);
576 if (h->fd < 0)
578 /* could not open the file, truncate it where it is */
579 h->filesize -= h->filerem;
580 h->filerem = 0;
581 return true;
584 if (h->offset)
585 lseek(h->fd, h->offset, SEEK_SET);
588 trigger_cpu_boost();
590 if (h->type == TYPE_ID3)
592 if (!get_metadata((struct mp3entry *)(buffer + h->data), h->fd, h->path))
594 /* metadata parsing failed: clear the buffer. */
595 memset(buffer + h->data, 0, sizeof(struct mp3entry));
597 close(h->fd);
598 h->fd = -1;
599 h->filerem = 0;
600 h->available = sizeof(struct mp3entry);
601 h->widx += sizeof(struct mp3entry);
602 send_event(EVENT_HANDLE_FINISHED, &h->id);
603 return true;
606 while (h->filerem > 0)
608 /* max amount to copy */
609 size_t copy_n = MIN( MIN(h->filerem, BUFFERING_DEFAULT_FILECHUNK),
610 buffer_len - h->widx);
612 /* stop copying if it would overwrite the reading position */
613 if (RINGBUF_ADD_CROSS(h->widx, copy_n, buf_ridx) >= 0)
614 return false;
616 /* This would read into the next handle, this is broken */
617 if (h->next && RINGBUF_ADD_CROSS(h->widx, copy_n,
618 (unsigned)((void *)h->next - (void *)buffer)) > 0) {
619 /* Try to recover by truncating this file */
620 copy_n = RINGBUF_ADD_CROSS(h->widx, copy_n,
621 (unsigned)((void *)h->next - (void *)buffer));
622 h->filerem -= copy_n;
623 h->filesize -= copy_n;
624 logf("buf alloc short %ld", (long)copy_n);
625 if (h->filerem)
626 continue;
627 else
628 break;
631 /* rc is the actual amount read */
632 int rc = read(h->fd, &buffer[h->widx], copy_n);
634 if (rc < 0)
636 /* Some kind of filesystem error, maybe recoverable if not codec */
637 if (h->type == TYPE_CODEC) {
638 logf("Partial codec");
639 break;
642 DEBUGF("File ended %ld bytes early\n", (long)h->filerem);
643 h->filesize -= h->filerem;
644 h->filerem = 0;
645 break;
648 /* Advance buffer */
649 h->widx = RINGBUF_ADD(h->widx, rc);
650 if (h == cur_handle)
651 buf_widx = h->widx;
652 h->available += rc;
653 h->filerem -= rc;
655 /* If this is a large file, see if we need to break or give the codec
656 * more time */
657 if (h->type == TYPE_PACKET_AUDIO &&
658 pcmbuf_is_lowdata() && !buffer_is_low())
660 sleep(1);
662 else
664 yield();
667 if (!queue_empty(&buffering_queue))
668 break;
671 if (h->filerem == 0) {
672 /* finished buffering the file */
673 close(h->fd);
674 h->fd = -1;
675 send_event(EVENT_HANDLE_FINISHED, &h->id);
678 return true;
681 /* Reset writing position and data buffer of a handle to its current offset.
682 Use this after having set the new offset to use. */
683 static void reset_handle(int handle_id)
685 logf("reset_handle(%d)", handle_id);
687 struct memory_handle *h = find_handle(handle_id);
688 if (!h)
689 return;
691 h->widx = h->data;
692 if (h == cur_handle)
693 buf_widx = h->widx;
694 h->available = 0;
695 h->filerem = h->filesize - h->offset;
697 if (h->fd >= 0) {
698 lseek(h->fd, h->offset, SEEK_SET);
702 /* Seek to a nonbuffered part of a handle by rebuffering the data. */
703 static void rebuffer_handle(int handle_id, size_t newpos)
705 struct memory_handle *h = find_handle(handle_id);
706 if (!h)
707 return;
709 /* When seeking foward off of the buffer, if it is a short seek don't
710 rebuffer the whole track, just read enough to satisfy */
711 if (newpos > h->offset && newpos - h->offset < BUFFERING_DEFAULT_FILECHUNK)
713 LOGFQUEUE("buffering >| Q_BUFFER_HANDLE");
714 queue_send(&buffering_queue, Q_BUFFER_HANDLE, handle_id);
715 h->ridx = h->data + newpos;
716 return;
719 h->offset = newpos;
721 /* Reset the handle to its new offset */
722 LOGFQUEUE("buffering >| Q_RESET_HANDLE");
723 queue_send(&buffering_queue, Q_RESET_HANDLE, handle_id);
725 size_t next = (unsigned)((void *)h->next - (void *)buffer);
726 if (next - h->data < h->filesize - newpos)
728 /* There isn't enough space to rebuffer all of the track from its new
729 offset, so we ask the user to free some */
730 DEBUGF("rebuffer_handle: space is needed\n");
731 send_event(EVENT_HANDLE_REBUFFER, &handle_id);
734 /* Now we ask for a rebuffer */
735 LOGFQUEUE("buffering >| Q_BUFFER_HANDLE");
736 queue_send(&buffering_queue, Q_BUFFER_HANDLE, handle_id);
738 h->ridx = h->data;
741 static bool close_handle(int handle_id)
743 struct memory_handle *h = find_handle(handle_id);
745 /* If the handle is not found, it is closed */
746 if (!h)
747 return true;
749 if (h->fd >= 0) {
750 close(h->fd);
751 h->fd = -1;
754 /* rm_handle returns true unless the handle somehow persists after exit */
755 return rm_handle(h);
758 /* Free buffer space by moving the handle struct right before the useful
759 part of its data buffer or by moving all the data. */
760 static void shrink_handle(struct memory_handle *h)
762 size_t delta;
764 if (!h)
765 return;
767 if (h->next && h->filerem == 0 &&
768 (h->type == TYPE_ID3 || h->type == TYPE_CUESHEET ||
769 h->type == TYPE_BITMAP || h->type == TYPE_CODEC ||
770 h->type == TYPE_ATOMIC_AUDIO))
772 /* metadata handle: we can move all of it */
773 size_t handle_distance =
774 RINGBUF_SUB((unsigned)((void *)h->next - (void*)buffer), h->data);
775 delta = handle_distance - h->available;
777 /* The value of delta might change for alignment reasons */
778 if (!move_handle(&h, &delta, h->available, h->type==TYPE_CODEC))
779 return;
781 size_t olddata = h->data;
782 h->data = RINGBUF_ADD(h->data, delta);
783 h->ridx = RINGBUF_ADD(h->ridx, delta);
784 h->widx = RINGBUF_ADD(h->widx, delta);
786 if (h->type == TYPE_ID3 && h->filesize == sizeof(struct mp3entry)) {
787 /* when moving an mp3entry we need to readjust its pointers. */
788 adjust_mp3entry((struct mp3entry *)&buffer[h->data],
789 (void *)&buffer[h->data],
790 (const void *)&buffer[olddata]);
791 } else if (h->type == TYPE_BITMAP) {
792 /* adjust the bitmap's pointer */
793 struct bitmap *bmp = (struct bitmap *)&buffer[h->data];
794 bmp->data = &buffer[h->data + sizeof(struct bitmap)];
797 else
799 /* only move the handle struct */
800 delta = RINGBUF_SUB(h->ridx, h->data);
801 if (!move_handle(&h, &delta, 0, true))
802 return;
804 h->data = RINGBUF_ADD(h->data, delta);
805 h->available -= delta;
806 h->offset += delta;
810 /* Fill the buffer by buffering as much data as possible for handles that still
811 have data left to buffer
812 Return whether or not to continue filling after this */
813 static bool fill_buffer(void)
815 logf("fill_buffer()");
816 struct memory_handle *m = first_handle;
817 shrink_handle(m);
818 while (queue_empty(&buffering_queue) && m) {
819 if (m->filerem > 0) {
820 if (!buffer_handle(m->id)) {
821 m = NULL;
822 break;
825 m = m->next;
828 if (m) {
829 return true;
831 else
833 /* only spin the disk down if the filling wasn't interrupted by an
834 event arriving in the queue. */
835 ata_sleep();
836 return false;
840 #ifdef HAVE_ALBUMART
841 /* Given a file descriptor to a bitmap file, write the bitmap data to the
842 buffer, with a struct bitmap and the actual data immediately following.
843 Return value is the total size (struct + data). */
844 static int load_bitmap(int fd)
846 int rc;
847 struct bitmap *bmp = (struct bitmap *)&buffer[buf_widx];
848 /* FIXME: alignment may be needed for the data buffer. */
849 bmp->data = &buffer[buf_widx + sizeof(struct bitmap)];
851 #if (LCD_DEPTH > 1) || defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1)
852 bmp->maskdata = NULL;
853 #endif
855 int free = (int)MIN(buffer_len - BUF_USED, buffer_len - buf_widx);
856 rc = read_bmp_fd(fd, bmp, free, FORMAT_ANY|FORMAT_DITHER);
857 return rc + (rc > 0 ? sizeof(struct bitmap) : 0);
859 #endif
863 MAIN BUFFERING API CALLS
864 ========================
866 bufopen : Request the opening of a new handle for a file
867 bufalloc : Open a new handle for data other than a file.
868 bufclose : Close an open handle
869 bufseek : Set the read pointer in a handle
870 bufadvance : Move the read pointer in a handle
871 bufread : Copy data from a handle into a given buffer
872 bufgetdata : Give a pointer to the handle's data
874 These functions are exported, to allow interaction with the buffer.
875 They take care of the content of the structs, and rely on the linked list
876 management functions for all the actual handle management work.
880 /* Reserve space in the buffer for a file.
881 filename: name of the file to open
882 offset: offset at which to start buffering the file, useful when the first
883 (offset-1) bytes of the file aren't needed.
884 return value: <0 if the file cannot be opened, or one file already
885 queued to be opened, otherwise the handle for the file in the buffer
887 int bufopen(const char *file, size_t offset, enum data_type type)
889 if (type == TYPE_ID3)
891 /* ID3 case: allocate space, init the handle and return. */
893 struct memory_handle *h = add_handle(sizeof(struct mp3entry), false, true);
894 if (!h)
895 return ERR_BUFFER_FULL;
897 h->fd = -1;
898 h->filesize = sizeof(struct mp3entry);
899 h->filerem = sizeof(struct mp3entry);
900 h->offset = 0;
901 h->data = buf_widx;
902 h->ridx = buf_widx;
903 h->widx = buf_widx;
904 h->available = 0;
905 h->type = type;
906 strncpy(h->path, file, MAX_PATH);
908 buf_widx += sizeof(struct mp3entry); /* safe because the handle
909 can't wrap */
910 return h->id;
913 /* Other cases: there is a little more work. */
915 int fd = open(file, O_RDONLY);
916 if (fd < 0)
917 return ERR_FILE_ERROR;
919 size_t size = filesize(fd);
920 bool can_wrap = type==TYPE_PACKET_AUDIO || type==TYPE_CODEC;
922 size_t adjusted_offset = offset;
923 if (adjusted_offset > size)
924 adjusted_offset = 0;
926 struct memory_handle *h = add_handle(size-adjusted_offset, can_wrap, false);
927 if (!h)
929 DEBUGF("bufopen: failed to add handle\n");
930 close(fd);
931 return ERR_BUFFER_FULL;
934 strncpy(h->path, file, MAX_PATH);
935 h->offset = adjusted_offset;
936 h->ridx = buf_widx;
937 h->data = buf_widx;
938 h->type = type;
940 #ifdef HAVE_ALBUMART
941 if (type == TYPE_BITMAP)
943 /* Bitmap file: we load the data instead of the file */
944 int rc;
945 mutex_lock(&llist_mutex); /* Lock because load_bitmap yields */
946 rc = load_bitmap(fd);
947 if (rc <= 0)
949 rm_handle(h);
950 close(fd);
951 mutex_unlock(&llist_mutex);
952 return ERR_FILE_ERROR;
954 h->filerem = 0;
955 h->filesize = rc;
956 h->available = rc;
957 h->widx = buf_widx + rc; /* safe because the data doesn't wrap */
958 buf_widx += rc; /* safe too */
959 mutex_unlock(&llist_mutex);
961 else
962 #endif
964 h->filerem = size - adjusted_offset;
965 h->filesize = size;
966 h->available = 0;
967 h->widx = buf_widx;
970 if (type == TYPE_CUESHEET) {
971 h->fd = fd;
972 /* Immediately start buffering those */
973 LOGFQUEUE("buffering >| Q_BUFFER_HANDLE");
974 queue_send(&buffering_queue, Q_BUFFER_HANDLE, h->id);
975 } else {
976 /* Other types will get buffered in the course of normal operations */
977 h->fd = -1;
978 close(fd);
980 /* Inform the buffering thread that we added a handle */
981 LOGFQUEUE("buffering > Q_HANDLE_ADDED %d", h->id);
982 queue_post(&buffering_queue, Q_HANDLE_ADDED, h->id);
985 logf("bufopen: new hdl %d", h->id);
986 return h->id;
989 /* Open a new handle from data that needs to be copied from memory.
990 src is the source buffer from which to copy data. It can be NULL to simply
991 reserve buffer space.
992 size is the requested size. The call will only be successful if the
993 requested amount of data can entirely fit in the buffer without wrapping.
994 Return value is the handle id for success or <0 for failure.
996 int bufalloc(const void *src, size_t size, enum data_type type)
998 struct memory_handle *h = add_handle(size, false, true);
1000 if (!h)
1001 return ERR_BUFFER_FULL;
1003 if (src) {
1004 if (type == TYPE_ID3 && size == sizeof(struct mp3entry)) {
1005 /* specially take care of struct mp3entry */
1006 copy_mp3entry((struct mp3entry *)&buffer[buf_widx],
1007 (const struct mp3entry *)src);
1008 } else {
1009 memcpy(&buffer[buf_widx], src, size);
1013 h->fd = -1;
1014 *h->path = 0;
1015 h->filesize = size;
1016 h->filerem = 0;
1017 h->offset = 0;
1018 h->ridx = buf_widx;
1019 h->widx = buf_widx + size; /* this is safe because the data doesn't wrap */
1020 h->data = buf_widx;
1021 h->available = size;
1022 h->type = type;
1024 buf_widx += size; /* safe too */
1026 logf("bufalloc: new hdl %d", h->id);
1027 return h->id;
1030 /* Close the handle. Return true for success and false for failure */
1031 bool bufclose(int handle_id)
1033 logf("bufclose(%d)", handle_id);
1035 LOGFQUEUE("buffering >| Q_CLOSE_HANDLE %d", handle_id);
1036 return queue_send(&buffering_queue, Q_CLOSE_HANDLE, handle_id);
1039 /* Set reading index in handle (relatively to the start of the file).
1040 Access before the available data will trigger a rebuffer.
1041 Return 0 for success and < 0 for failure:
1042 -1 if the handle wasn't found
1043 -2 if the new requested position was beyond the end of the file
1045 int bufseek(int handle_id, size_t newpos)
1047 struct memory_handle *h = find_handle(handle_id);
1048 if (!h)
1049 return ERR_HANDLE_NOT_FOUND;
1051 if (newpos > h->filesize) {
1052 /* access beyond the end of the file */
1053 return ERR_INVALID_VALUE;
1055 else if (newpos < h->offset || h->offset + h->available < newpos) {
1056 /* access before or after buffered data. A rebuffer is needed. */
1057 rebuffer_handle(handle_id, newpos);
1059 else {
1060 h->ridx = RINGBUF_ADD(h->data, newpos - h->offset);
1062 return 0;
1065 /* Advance the reading index in a handle (relatively to its current position).
1066 Return 0 for success and < 0 for failure */
1067 int bufadvance(int handle_id, off_t offset)
1069 const struct memory_handle *h = find_handle(handle_id);
1070 if (!h)
1071 return ERR_HANDLE_NOT_FOUND;
1073 size_t newpos = h->offset + RINGBUF_SUB(h->ridx, h->data) + offset;
1074 return bufseek(handle_id, newpos);
1077 /* Used by bufread and bufgetdata to prepare the buffer and retrieve the
1078 * actual amount of data available for reading. This function explicitly
1079 * does not check the validity of the input handle. It does do range checks
1080 * on size and returns a valid (and explicit) amount of data for reading */
1081 static struct memory_handle *prep_bufdata(int handle_id, size_t *size,
1082 bool guardbuf_limit)
1084 struct memory_handle *h = find_handle(handle_id);
1085 if (!h)
1086 return NULL;
1088 size_t avail = RINGBUF_SUB(h->widx, h->ridx);
1090 if (avail == 0 && h->filerem == 0)
1092 /* File is finished reading */
1093 *size = 0;
1094 return h;
1097 if (*size == 0 || *size > avail + h->filerem)
1098 *size = avail + h->filerem;
1100 if (guardbuf_limit && h->type == TYPE_PACKET_AUDIO && *size > GUARD_BUFSIZE)
1102 logf("data request > guardbuf");
1103 /* If more than the size of the guardbuf is requested and this is a
1104 * bufgetdata, limit to guard_bufsize over the end of the buffer */
1105 *size = MIN(*size, buffer_len - h->ridx + GUARD_BUFSIZE);
1106 /* this ensures *size <= buffer_len - h->ridx + GUARD_BUFSIZE */
1109 if (h->filerem > 0 && avail < *size)
1111 /* Data isn't ready. Request buffering */
1112 buf_request_buffer_handle(handle_id);
1113 /* Wait for the data to be ready */
1116 sleep(1);
1117 /* it is not safe for a non-buffering thread to sleep while
1118 * holding a handle */
1119 h = find_handle(handle_id);
1120 if (!h)
1121 return NULL;
1122 avail = RINGBUF_SUB(h->widx, h->ridx);
1124 while (h->filerem > 0 && avail < *size);
1127 *size = MIN(*size,avail);
1128 return h;
1131 /* Copy data from the given handle to the dest buffer.
1132 Return the number of bytes copied or < 0 for failure (handle not found).
1133 The caller is blocked until the requested amount of data is available.
1135 ssize_t bufread(int handle_id, size_t size, void *dest)
1137 const struct memory_handle *h;
1138 size_t adjusted_size = size;
1140 h = prep_bufdata(handle_id, &adjusted_size, false);
1141 if (!h)
1142 return ERR_HANDLE_NOT_FOUND;
1144 if (h->ridx + adjusted_size > buffer_len)
1146 /* the data wraps around the end of the buffer */
1147 size_t read = buffer_len - h->ridx;
1148 memcpy(dest, &buffer[h->ridx], read);
1149 memcpy(dest+read, buffer, adjusted_size - read);
1151 else
1153 memcpy(dest, &buffer[h->ridx], adjusted_size);
1156 return adjusted_size;
1159 /* Update the "data" pointer to make the handle's data available to the caller.
1160 Return the length of the available linear data or < 0 for failure (handle
1161 not found).
1162 The caller is blocked until the requested amount of data is available.
1163 size is the amount of linear data requested. it can be 0 to get as
1164 much as possible.
1165 The guard buffer may be used to provide the requested size. This means it's
1166 unsafe to request more than the size of the guard buffer.
1168 ssize_t bufgetdata(int handle_id, size_t size, void **data)
1170 const struct memory_handle *h;
1171 size_t adjusted_size = size;
1173 h = prep_bufdata(handle_id, &adjusted_size, true);
1174 if (!h)
1175 return ERR_HANDLE_NOT_FOUND;
1177 if (h->ridx + adjusted_size > buffer_len)
1179 /* the data wraps around the end of the buffer :
1180 use the guard buffer to provide the requested amount of data. */
1181 size_t copy_n = h->ridx + adjusted_size - buffer_len;
1182 /* prep_bufdata ensures adjusted_size <= buffer_len - h->ridx + GUARD_BUFSIZE,
1183 so copy_n <= GUARD_BUFSIZE */
1184 memcpy(guard_buffer, (const unsigned char *)buffer, copy_n);
1187 if (data)
1188 *data = &buffer[h->ridx];
1190 return adjusted_size;
1193 ssize_t bufgettail(int handle_id, size_t size, void **data)
1195 size_t tidx;
1197 const struct memory_handle *h;
1199 h = find_handle(handle_id);
1201 if (!h)
1202 return ERR_HANDLE_NOT_FOUND;
1204 if (h->filerem)
1205 return ERR_HANDLE_NOT_DONE;
1207 /* We don't support tail requests of > guardbuf_size, for simplicity */
1208 if (size > GUARD_BUFSIZE)
1209 return ERR_INVALID_VALUE;
1211 tidx = RINGBUF_SUB(h->widx, size);
1213 if (tidx + size > buffer_len)
1215 size_t copy_n = tidx + size - buffer_len;
1216 memcpy(guard_buffer, (const unsigned char *)buffer, copy_n);
1219 *data = &buffer[tidx];
1220 return size;
1223 ssize_t bufcuttail(int handle_id, size_t size)
1225 struct memory_handle *h;
1226 size_t adjusted_size = size;
1228 h = find_handle(handle_id);
1230 if (!h)
1231 return ERR_HANDLE_NOT_FOUND;
1233 if (h->filerem)
1234 return ERR_HANDLE_NOT_DONE;
1236 if (h->available < adjusted_size)
1237 adjusted_size = h->available;
1239 h->available -= adjusted_size;
1240 h->filesize -= adjusted_size;
1241 h->widx = RINGBUF_SUB(h->widx, adjusted_size);
1242 if (h == cur_handle)
1243 buf_widx = h->widx;
1245 return adjusted_size;
1250 SECONDARY EXPORTED FUNCTIONS
1251 ============================
1253 buf_get_offset
1254 buf_handle_offset
1255 buf_request_buffer_handle
1256 buf_set_base_handle
1257 buf_used
1258 register_buffering_callback
1259 unregister_buffering_callback
1261 These functions are exported, to allow interaction with the buffer.
1262 They take care of the content of the structs, and rely on the linked list
1263 management functions for all the actual handle management work.
1266 /* Get a handle offset from a pointer */
1267 ssize_t buf_get_offset(int handle_id, void *ptr)
1269 const struct memory_handle *h = find_handle(handle_id);
1270 if (!h)
1271 return ERR_HANDLE_NOT_FOUND;
1273 return (size_t)ptr - (size_t)&buffer[h->ridx];
1276 ssize_t buf_handle_offset(int handle_id)
1278 const struct memory_handle *h = find_handle(handle_id);
1279 if (!h)
1280 return ERR_HANDLE_NOT_FOUND;
1281 return h->offset;
1284 void buf_request_buffer_handle(int handle_id)
1286 LOGFQUEUE("buffering >| Q_START_FILL %d",handle_id);
1287 queue_send(&buffering_queue, Q_START_FILL, handle_id);
1290 void buf_set_base_handle(int handle_id)
1292 LOGFQUEUE("buffering > Q_BASE_HANDLE %d", handle_id);
1293 queue_post(&buffering_queue, Q_BASE_HANDLE, handle_id);
1296 /* Return the amount of buffer space used */
1297 size_t buf_used(void)
1299 return BUF_USED;
1302 void buf_set_watermark(size_t bytes)
1304 LOGFQUEUE("buffering > Q_SET_WATERMARK %ld", (long)bytes);
1305 queue_post(&buffering_queue, Q_SET_WATERMARK, bytes);
1308 static void shrink_buffer_inner(struct memory_handle *h)
1310 if (h == NULL)
1311 return;
1313 shrink_buffer_inner(h->next);
1315 shrink_handle(h);
1318 static void shrink_buffer(void)
1320 logf("shrink_buffer()");
1321 shrink_buffer_inner(first_handle);
1324 void buffering_thread(void)
1326 bool filling = false;
1327 struct queue_event ev;
1329 while (true)
1331 if (!filling) {
1332 cancel_cpu_boost();
1335 queue_wait_w_tmo(&buffering_queue, &ev, filling ? 5 : HZ/2);
1337 switch (ev.id)
1339 case Q_START_FILL:
1340 LOGFQUEUE("buffering < Q_START_FILL");
1341 /* Call buffer callbacks here because this is one of two ways
1342 * to begin a full buffer fill */
1343 send_event(EVENT_BUFFER_LOW, 0);
1344 shrink_buffer();
1345 queue_reply(&buffering_queue, 1);
1346 filling |= buffer_handle((int)ev.data);
1347 break;
1349 case Q_BUFFER_HANDLE:
1350 LOGFQUEUE("buffering < Q_BUFFER_HANDLE");
1351 queue_reply(&buffering_queue, 1);
1352 buffer_handle((int)ev.data);
1353 break;
1355 case Q_RESET_HANDLE:
1356 LOGFQUEUE("buffering < Q_RESET_HANDLE");
1357 queue_reply(&buffering_queue, 1);
1358 reset_handle((int)ev.data);
1359 break;
1361 case Q_CLOSE_HANDLE:
1362 LOGFQUEUE("buffering < Q_CLOSE_HANDLE");
1363 queue_reply(&buffering_queue, close_handle((int)ev.data));
1364 break;
1366 case Q_HANDLE_ADDED:
1367 LOGFQUEUE("buffering < Q_HANDLE_ADDED %d", (int)ev.data);
1368 /* A handle was added: the disk is spinning, so we can fill */
1369 filling = true;
1370 break;
1372 case Q_BASE_HANDLE:
1373 LOGFQUEUE("buffering < Q_BASE_HANDLE");
1374 base_handle_id = (int)ev.data;
1375 break;
1377 case Q_SET_WATERMARK:
1378 LOGFQUEUE("buffering < Q_SET_WATERMARK");
1379 conf_watermark = (size_t)ev.data;
1380 if (conf_watermark < BUFFERING_DEFAULT_FILECHUNK)
1382 logf("wmark<chunk %ld<%d",
1383 (long)conf_watermark, BUFFERING_DEFAULT_FILECHUNK);
1384 conf_watermark = BUFFERING_DEFAULT_FILECHUNK;
1386 break;
1388 #ifndef SIMULATOR
1389 case SYS_USB_CONNECTED:
1390 LOGFQUEUE("buffering < SYS_USB_CONNECTED");
1391 usb_acknowledge(SYS_USB_CONNECTED_ACK);
1392 usb_wait_for_disconnect(&buffering_queue);
1393 break;
1394 #endif
1396 case SYS_TIMEOUT:
1397 LOGFQUEUE_SYS_TIMEOUT("buffering < SYS_TIMEOUT");
1398 break;
1401 update_data_counters();
1403 /* If the buffer is low, call the callbacks to get new data */
1404 if (num_handles > 0 && data_counters.useful <= conf_watermark)
1405 send_event(EVENT_BUFFER_LOW, 0);
1407 #if 0
1408 /* TODO: This needs to be fixed to use the idle callback, disable it
1409 * for simplicity until its done right */
1410 #if MEM > 8
1411 /* If the disk is spinning, take advantage by filling the buffer */
1412 else if (ata_disk_is_active() && queue_empty(&buffering_queue))
1414 if (num_handles > 0 && data_counters.useful <= high_watermark)
1415 send_event(EVENT_BUFFER_LOW, 0);
1417 if (data_counters.remaining > 0 && BUF_USED <= high_watermark)
1419 /* This is a new fill, shrink the buffer up first */
1420 if (!filling)
1421 shrink_buffer();
1422 filling = fill_buffer();
1423 update_data_counters();
1426 #endif
1427 #endif
1429 if (queue_empty(&buffering_queue)) {
1430 if (filling) {
1431 if (data_counters.remaining > 0 && BUF_USED < buffer_len)
1432 filling = fill_buffer();
1434 else if (ev.id == SYS_TIMEOUT)
1436 if (data_counters.remaining > 0 &&
1437 data_counters.useful <= conf_watermark) {
1438 shrink_buffer();
1439 filling = fill_buffer();
1446 void buffering_init(void)
1448 mutex_init(&llist_mutex);
1449 #ifdef HAVE_PRIORITY_SCHEDULING
1450 /* This behavior not safe atm */
1451 mutex_set_preempt(&llist_mutex, false);
1452 #endif
1454 conf_watermark = BUFFERING_DEFAULT_WATERMARK;
1456 queue_init(&buffering_queue, true);
1457 buffering_thread_p = create_thread( buffering_thread, buffering_stack,
1458 sizeof(buffering_stack), CREATE_THREAD_FROZEN,
1459 buffering_thread_name IF_PRIO(, PRIORITY_BUFFERING)
1460 IF_COP(, CPU));
1462 queue_enable_queue_send(&buffering_queue, &buffering_queue_sender_list,
1463 buffering_thread_p);
1466 /* Initialise the buffering subsystem */
1467 bool buffering_reset(char *buf, size_t buflen)
1469 if (!buf || !buflen)
1470 return false;
1472 buffer = buf;
1473 buffer_len = buflen;
1474 guard_buffer = buf + buflen;
1476 buf_widx = 0;
1477 buf_ridx = 0;
1479 first_handle = NULL;
1480 cur_handle = NULL;
1481 cached_handle = NULL;
1482 num_handles = 0;
1483 base_handle_id = -1;
1485 /* Set the high watermark as 75% full...or 25% empty :) */
1486 #if MEM > 8
1487 high_watermark = 3*buflen / 4;
1488 #endif
1490 thread_thaw(buffering_thread_p);
1492 return true;
1495 void buffering_get_debugdata(struct buffering_debug *dbgdata)
1497 update_data_counters();
1498 dbgdata->num_handles = num_handles;
1499 dbgdata->data_rem = data_counters.remaining;
1500 dbgdata->wasted_space = data_counters.wasted;
1501 dbgdata->buffered_data = data_counters.buffered;
1502 dbgdata->useful_data = data_counters.useful;