Add the asap team to the credits.
[kugel-rb.git] / apps / buffering.c
blobe39a4b6ecab8e869e5561917e3ee8f2f7787a022
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2007 Nicolas Pennequin
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include "config.h"
23 #include <stdio.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <ctype.h>
27 #include "buffering.h"
29 #include "ata.h"
30 #include "system.h"
31 #include "thread.h"
32 #include "file.h"
33 #include "panic.h"
34 #include "memory.h"
35 #include "lcd.h"
36 #include "font.h"
37 #include "button.h"
38 #include "kernel.h"
39 #include "tree.h"
40 #include "debug.h"
41 #include "sprintf.h"
42 #include "settings.h"
43 #include "codecs.h"
44 #include "audio.h"
45 #include "mp3_playback.h"
46 #include "usb.h"
47 #include "status.h"
48 #include "screens.h"
49 #include "playlist.h"
50 #include "pcmbuf.h"
51 #include "buffer.h"
52 #include "bmp.h"
53 #include "events.h"
54 #include "metadata.h"
56 #if MEM > 1
57 #define GUARD_BUFSIZE (32*1024)
58 #else
59 #define GUARD_BUFSIZE (8*1024)
60 #endif
62 /* Define LOGF_ENABLE to enable logf output in this file */
63 /*#define LOGF_ENABLE*/
64 #include "logf.h"
66 /* macros to enable logf for queues
67 logging on SYS_TIMEOUT can be disabled */
68 #ifdef SIMULATOR
69 /* Define this for logf output of all queuing except SYS_TIMEOUT */
70 #define BUFFERING_LOGQUEUES
71 /* Define this to logf SYS_TIMEOUT messages */
72 /* #define BUFFERING_LOGQUEUES_SYS_TIMEOUT */
73 #endif
75 #ifdef BUFFERING_LOGQUEUES
76 #define LOGFQUEUE logf
77 #else
78 #define LOGFQUEUE(...)
79 #endif
81 #ifdef BUFFERING_LOGQUEUES_SYS_TIMEOUT
82 #define LOGFQUEUE_SYS_TIMEOUT logf
83 #else
84 #define LOGFQUEUE_SYS_TIMEOUT(...)
85 #endif
87 /* default point to start buffer refill */
88 #define BUFFERING_DEFAULT_WATERMARK (1024*512)
89 /* amount of data to read in one read() call */
90 #define BUFFERING_DEFAULT_FILECHUNK (1024*32)
91 /* point at which the file buffer will fight for CPU time */
92 #define BUFFERING_CRITICAL_LEVEL (1024*128)
94 #define BUF_HANDLE_MASK 0x7FFFFFFF
97 /* Ring buffer helper macros */
98 /* Buffer pointer (p) plus value (v), wrapped if necessary */
99 #define RINGBUF_ADD(p,v) (((p)+(v))<buffer_len ? (p)+(v) : (p)+(v)-buffer_len)
100 /* Buffer pointer (p) minus value (v), wrapped if necessary */
101 #define RINGBUF_SUB(p,v) ((p>=v) ? (p)-(v) : (p)+buffer_len-(v))
102 /* How far value (v) plus buffer pointer (p1) will cross buffer pointer (p2) */
103 #define RINGBUF_ADD_CROSS(p1,v,p2) \
104 ((p1<p2) ? (int)((p1)+(v))-(int)(p2) : (int)((p1)+(v)-(p2))-(int)buffer_len)
105 /* Bytes available in the buffer */
106 #define BUF_USED RINGBUF_SUB(buf_widx, buf_ridx)
108 /* assert(sizeof(struct memory_handle)%4==0) */
109 struct memory_handle {
110 int id; /* A unique ID for the handle */
111 enum data_type type; /* Type of data buffered with this handle */
112 char path[MAX_PATH]; /* Path if data originated in a file */
113 int fd; /* File descriptor to path (-1 if closed) */
114 size_t data; /* Start index of the handle's data buffer */
115 volatile size_t ridx; /* Read pointer, relative to the main buffer */
116 size_t widx; /* Write pointer */
117 size_t filesize; /* File total length */
118 size_t filerem; /* Remaining bytes of file NOT in buffer */
119 volatile size_t available; /* Available bytes to read from buffer */
120 size_t offset; /* Offset at which we started reading the file */
121 struct memory_handle *next;
123 /* invariant: filesize == offset + available + filerem */
125 static char *buffer;
126 static char *guard_buffer;
128 static size_t buffer_len;
130 static volatile size_t buf_widx; /* current writing position */
131 static volatile size_t buf_ridx; /* current reading position */
132 /* buf_*idx are values relative to the buffer, not real pointers. */
134 /* Configuration */
135 static size_t conf_watermark = 0; /* Level to trigger filebuf fill */
136 #if MEM > 8
137 static size_t high_watermark = 0; /* High watermark for rebuffer */
138 #endif
140 /* current memory handle in the linked list. NULL when the list is empty. */
141 static struct memory_handle *cur_handle;
142 /* first memory handle in the linked list. NULL when the list is empty. */
143 static struct memory_handle *first_handle;
145 static int num_handles; /* number of handles in the list */
147 static int base_handle_id;
149 static struct mutex llist_mutex;
151 /* Handle cache (makes find_handle faster).
152 This is global so that move_handle and rm_handle can invalidate it. */
153 static struct memory_handle *cached_handle = NULL;
155 static struct {
156 size_t remaining; /* Amount of data needing to be buffered */
157 size_t wasted; /* Amount of space available for freeing */
158 size_t buffered; /* Amount of data currently in the buffer */
159 size_t useful; /* Amount of data still useful to the user */
160 } data_counters;
163 /* Messages available to communicate with the buffering thread */
164 enum {
165 Q_BUFFER_HANDLE = 1, /* Request buffering of a handle, this should not be
166 used in a low buffer situation. */
167 Q_RESET_HANDLE, /* (internal) Request resetting of a handle to its
168 offset (the offset has to be set beforehand) */
169 Q_CLOSE_HANDLE, /* Request closing a handle */
170 Q_BASE_HANDLE, /* Set the reference handle for buf_useful_data */
172 /* Configuration: */
173 Q_SET_WATERMARK,
174 Q_START_FILL, /* Request that the buffering thread initiate a buffer
175 fill at its earliest convenience */
176 Q_HANDLE_ADDED, /* Inform the buffering thread that a handle was added,
177 (which means the disk is spinning) */
180 /* Buffering thread */
181 static void buffering_thread(void);
182 static long buffering_stack[(DEFAULT_STACK_SIZE + 0x2000)/sizeof(long)];
183 static const char buffering_thread_name[] = "buffering";
184 static struct thread_entry *buffering_thread_p;
185 static struct event_queue buffering_queue;
186 static struct queue_sender_list buffering_queue_sender_list;
191 LINKED LIST MANAGEMENT
192 ======================
194 add_handle : Add a handle to the list
195 rm_handle : Remove a handle from the list
196 find_handle : Get a handle pointer from an ID
197 move_handle : Move a handle in the buffer (with or without its data)
199 These functions only handle the linked list structure. They don't touch the
200 contents of the struct memory_handle headers. They also change the buf_*idx
201 pointers when necessary and manage the handle IDs.
203 The first and current (== last) handle are kept track of.
204 A new handle is added at buf_widx and becomes the current one.
205 buf_widx always points to the current writing position for the current handle
206 buf_ridx always points to the location of the first handle.
207 buf_ridx == buf_widx means the buffer is empty.
211 /* Add a new handle to the linked list and return it. It will have become the
212 new current handle.
213 data_size must contain the size of what will be in the handle.
214 can_wrap tells us whether this type of data may wrap on buffer
215 alloc_all tells us if we must immediately be able to allocate data_size
216 returns a valid memory handle if all conditions for allocation are met.
217 NULL if there memory_handle itself cannot be allocated or if the
218 data_size cannot be allocated and alloc_all is set. This function's
219 only potential side effect is to allocate space for the cur_handle
220 if it returns NULL.
222 static struct memory_handle *add_handle(size_t data_size, bool can_wrap,
223 bool alloc_all)
225 /* gives each handle a unique id */
226 static int cur_handle_id = 0;
227 size_t shift;
228 size_t new_widx;
229 size_t len;
230 int overlap;
232 if (num_handles >= BUF_MAX_HANDLES)
233 return NULL;
235 mutex_lock(&llist_mutex);
237 if (cur_handle && cur_handle->filerem > 0) {
238 /* the current handle hasn't finished buffering. We can only add
239 a new one if there is already enough free space to finish
240 the buffering. */
241 size_t req = cur_handle->filerem + sizeof(struct memory_handle);
242 if (RINGBUF_ADD_CROSS(cur_handle->widx, req, buf_ridx) >= 0) {
243 /* Not enough space */
244 mutex_unlock(&llist_mutex);
245 return NULL;
246 } else {
247 /* Allocate the remainder of the space for the current handle */
248 buf_widx = RINGBUF_ADD(cur_handle->widx, cur_handle->filerem);
252 /* align to 4 bytes up */
253 new_widx = RINGBUF_ADD(buf_widx, 3) & ~3;
255 len = data_size + sizeof(struct memory_handle);
257 /* First, will the handle wrap? */
258 overlap = RINGBUF_ADD_CROSS(new_widx, sizeof(struct memory_handle),
259 buffer_len - 1);
260 /* If the handle would wrap, move to the beginning of the buffer,
261 * otherwise check if the data can/would wrap and move it to the
262 * beginning if needed */
263 if (overlap > 0) {
264 new_widx = 0;
265 } else if (!can_wrap) {
266 overlap = RINGBUF_ADD_CROSS(new_widx, len, buffer_len - 1);
267 if (overlap > 0)
268 new_widx += data_size - overlap;
271 /* How far we shifted buf_widx to align things, must be < buffer_len */
272 shift = RINGBUF_SUB(new_widx, buf_widx);
274 /* How much space are we short in the actual ring buffer? */
275 overlap = RINGBUF_ADD_CROSS(buf_widx, shift + len, buf_ridx);
276 if (overlap >= 0 && (alloc_all || (unsigned)overlap > data_size)) {
277 /* Not enough space for required allocations */
278 mutex_unlock(&llist_mutex);
279 return NULL;
282 /* There is enough space for the required data, advance the buf_widx and
283 * initialize the struct */
284 buf_widx = new_widx;
286 struct memory_handle *new_handle =
287 (struct memory_handle *)(&buffer[buf_widx]);
289 /* only advance the buffer write index of the size of the struct */
290 buf_widx = RINGBUF_ADD(buf_widx, sizeof(struct memory_handle));
292 new_handle->id = cur_handle_id;
293 /* Wrap signed int is safe and 0 doesn't happen */
294 cur_handle_id = (cur_handle_id + 1) & BUF_HANDLE_MASK;
295 new_handle->next = NULL;
296 num_handles++;
298 if (!first_handle)
299 /* the new handle is the first one */
300 first_handle = new_handle;
302 if (cur_handle)
303 cur_handle->next = new_handle;
305 cur_handle = new_handle;
307 mutex_unlock(&llist_mutex);
308 return new_handle;
311 /* Delete a given memory handle from the linked list
312 and return true for success. Nothing is actually erased from memory. */
313 static bool rm_handle(const struct memory_handle *h)
315 if (h == NULL)
316 return true;
318 mutex_lock(&llist_mutex);
320 if (h == first_handle) {
321 first_handle = h->next;
322 if (h == cur_handle) {
323 /* h was the first and last handle: the buffer is now empty */
324 cur_handle = NULL;
325 buf_ridx = buf_widx = 0;
326 } else {
327 /* update buf_ridx to point to the new first handle */
328 buf_ridx = (void *)first_handle - (void *)buffer;
330 } else {
331 struct memory_handle *m = first_handle;
332 /* Find the previous handle */
333 while (m && m->next != h) {
334 m = m->next;
336 if (m && m->next == h) {
337 m->next = h->next;
338 if (h == cur_handle) {
339 cur_handle = m;
340 buf_widx = cur_handle->widx;
342 } else {
343 mutex_unlock(&llist_mutex);
344 return false;
348 /* Invalidate the cache to prevent it from keeping the old location of h */
349 if (h == cached_handle)
350 cached_handle = NULL;
352 num_handles--;
354 mutex_unlock(&llist_mutex);
355 return true;
358 /* Return a pointer to the memory handle of given ID.
359 NULL if the handle wasn't found */
360 static struct memory_handle *find_handle(int handle_id)
362 if (handle_id < 0)
363 return NULL;
365 mutex_lock(&llist_mutex);
367 /* simple caching because most of the time the requested handle
368 will either be the same as the last, or the one after the last */
369 if (cached_handle)
371 if (cached_handle->id == handle_id) {
372 mutex_unlock(&llist_mutex);
373 return cached_handle;
374 } else if (cached_handle->next &&
375 (cached_handle->next->id == handle_id)) {
376 cached_handle = cached_handle->next;
377 mutex_unlock(&llist_mutex);
378 return cached_handle;
382 struct memory_handle *m = first_handle;
383 while (m && m->id != handle_id) {
384 m = m->next;
386 /* This condition can only be reached with !m or m->id == handle_id */
387 if (m)
388 cached_handle = m;
390 mutex_unlock(&llist_mutex);
391 return m;
394 /* Move a memory handle and data_size of its data delta bytes along the buffer.
395 delta maximum bytes available to move the handle. If the move is performed
396 it is set to the actual distance moved.
397 data_size is the amount of data to move along with the struct.
398 returns a valid memory_handle if the move is successful
399 NULL if the handle is NULL, the move would be less than the size of
400 a memory_handle after correcting for wraps or if the handle is not
401 found in the linked list for adjustment. This function has no side
402 effects if NULL is returned. */
403 static bool move_handle(struct memory_handle **h, size_t *delta,
404 size_t data_size, bool can_wrap)
406 struct memory_handle *dest;
407 const struct memory_handle *src;
408 size_t newpos;
409 size_t size_to_move;
410 size_t final_delta = *delta;
411 int overlap;
413 if (h == NULL || (src = *h) == NULL)
414 return false;
416 size_to_move = sizeof(struct memory_handle) + data_size;
418 /* Align to four bytes, down */
419 final_delta &= ~3;
420 if (final_delta < sizeof(struct memory_handle)) {
421 /* It's not legal to move less than the size of the struct */
422 return false;
425 mutex_lock(&llist_mutex);
427 newpos = RINGBUF_ADD((void *)src - (void *)buffer, final_delta);
428 overlap = RINGBUF_ADD_CROSS(newpos, size_to_move, buffer_len - 1);
430 if (overlap > 0) {
431 /* Some part of the struct + data would wrap, maybe ok */
432 size_t correction = 0;
433 /* If the overlap lands inside the memory_handle */
434 if ((unsigned)overlap > data_size) {
435 /* Correct the position and real delta to prevent the struct from
436 * wrapping, this guarantees an aligned delta, I think */
437 correction = overlap - data_size;
438 } else if (!can_wrap) {
439 /* Otherwise the overlap falls in the data area and must all be
440 * backed out. This may become conditional if ever we move
441 * data that is allowed to wrap (ie audio) */
442 correction = overlap;
443 /* Align correction to four bytes, up */
444 correction = (correction+3) & ~3;
446 if (correction) {
447 if (final_delta < correction + sizeof(struct memory_handle)) {
448 /* Delta cannot end up less than the size of the struct */
449 mutex_unlock(&llist_mutex);
450 return false;
453 newpos -= correction;
454 overlap -= correction;/* Used below to know how to split the data */
455 final_delta -= correction;
459 dest = (struct memory_handle *)(&buffer[newpos]);
461 if (src == first_handle) {
462 first_handle = dest;
463 buf_ridx = newpos;
464 } else {
465 struct memory_handle *m = first_handle;
466 while (m && m->next != src) {
467 m = m->next;
469 if (m && m->next == src) {
470 m->next = dest;
471 } else {
472 mutex_unlock(&llist_mutex);
473 return false;
478 /* Update the cache to prevent it from keeping the old location of h */
479 if (src == cached_handle)
480 cached_handle = dest;
482 /* the cur_handle pointer might need updating */
483 if (src == cur_handle)
484 cur_handle = dest;
486 if (overlap > 0) {
487 size_t first_part = size_to_move - overlap;
488 memmove(dest, src, first_part);
489 memmove(buffer, (const char *)src + first_part, overlap);
490 } else {
491 memmove(dest, src, size_to_move);
494 /* Update the caller with the new location of h and the distance moved */
495 *h = dest;
496 *delta = final_delta;
497 mutex_unlock(&llist_mutex);
498 return dest;
503 BUFFER SPACE MANAGEMENT
504 =======================
506 update_data_counters: Updates the values in data_counters
507 buffer_is_low : Returns true if the amount of useful data in the buffer is low
508 buffer_handle : Buffer data for a handle
509 reset_handle : Reset write position and data buffer of a handle to its offset
510 rebuffer_handle : Seek to a nonbuffered part of a handle by rebuffering the data
511 shrink_handle : Free buffer space by moving a handle
512 fill_buffer : Call buffer_handle for all handles that have data to buffer
514 These functions are used by the buffering thread to manage buffer space.
517 static void update_data_counters(void)
519 struct memory_handle *m = find_handle(base_handle_id);
520 bool is_useful = m==NULL;
522 size_t buffered = 0;
523 size_t wasted = 0;
524 size_t remaining = 0;
525 size_t useful = 0;
527 mutex_lock(&llist_mutex);
529 m = first_handle;
530 while (m) {
531 buffered += m->available;
532 wasted += RINGBUF_SUB(m->ridx, m->data);
533 remaining += m->filerem;
535 if (m->id == base_handle_id)
536 is_useful = true;
538 if (is_useful)
539 useful += RINGBUF_SUB(m->widx, m->ridx);
541 m = m->next;
544 mutex_unlock(&llist_mutex);
546 data_counters.buffered = buffered;
547 data_counters.wasted = wasted;
548 data_counters.remaining = remaining;
549 data_counters.useful = useful;
552 static inline bool buffer_is_low(void)
554 update_data_counters();
555 return data_counters.useful < BUFFERING_CRITICAL_LEVEL;
558 /* Buffer data for the given handle.
559 Return whether or not the buffering should continue explicitly. */
560 static bool buffer_handle(int handle_id)
562 logf("buffer_handle(%d)", handle_id);
563 struct memory_handle *h = find_handle(handle_id);
564 if (!h)
565 return true;
567 if (h->filerem == 0) {
568 /* nothing left to buffer */
569 return true;
572 if (h->fd < 0) /* file closed, reopen */
574 if (*h->path)
575 h->fd = open(h->path, O_RDONLY);
577 if (h->fd < 0)
579 /* could not open the file, truncate it where it is */
580 h->filesize -= h->filerem;
581 h->filerem = 0;
582 return true;
585 if (h->offset)
586 lseek(h->fd, h->offset, SEEK_SET);
589 trigger_cpu_boost();
591 if (h->type == TYPE_ID3)
593 if (!get_metadata((struct mp3entry *)(buffer + h->data), h->fd, h->path))
595 /* metadata parsing failed: clear the buffer. */
596 memset(buffer + h->data, 0, sizeof(struct mp3entry));
598 close(h->fd);
599 h->fd = -1;
600 h->filerem = 0;
601 h->available = sizeof(struct mp3entry);
602 h->widx += sizeof(struct mp3entry);
603 send_event(EVENT_HANDLE_FINISHED, &h->id);
604 return true;
607 while (h->filerem > 0)
609 /* max amount to copy */
610 size_t copy_n = MIN( MIN(h->filerem, BUFFERING_DEFAULT_FILECHUNK),
611 buffer_len - h->widx);
613 /* stop copying if it would overwrite the reading position */
614 if (RINGBUF_ADD_CROSS(h->widx, copy_n, buf_ridx) >= 0)
615 return false;
617 /* This would read into the next handle, this is broken */
618 if (h->next && RINGBUF_ADD_CROSS(h->widx, copy_n,
619 (unsigned)((void *)h->next - (void *)buffer)) > 0) {
620 /* Try to recover by truncating this file */
621 copy_n = RINGBUF_ADD_CROSS(h->widx, copy_n,
622 (unsigned)((void *)h->next - (void *)buffer));
623 h->filerem -= copy_n;
624 h->filesize -= copy_n;
625 logf("buf alloc short %ld", (long)copy_n);
626 if (h->filerem)
627 continue;
628 else
629 break;
632 /* rc is the actual amount read */
633 int rc = read(h->fd, &buffer[h->widx], copy_n);
635 if (rc < 0)
637 /* Some kind of filesystem error, maybe recoverable if not codec */
638 if (h->type == TYPE_CODEC) {
639 logf("Partial codec");
640 break;
643 DEBUGF("File ended %ld bytes early\n", (long)h->filerem);
644 h->filesize -= h->filerem;
645 h->filerem = 0;
646 break;
649 /* Advance buffer */
650 h->widx = RINGBUF_ADD(h->widx, rc);
651 if (h == cur_handle)
652 buf_widx = h->widx;
653 h->available += rc;
654 h->filerem -= rc;
656 /* If this is a large file, see if we need to break or give the codec
657 * more time */
658 if (h->type == TYPE_PACKET_AUDIO &&
659 pcmbuf_is_lowdata() && !buffer_is_low())
661 sleep(1);
663 else
665 yield();
668 if (!queue_empty(&buffering_queue))
669 break;
672 if (h->filerem == 0) {
673 /* finished buffering the file */
674 close(h->fd);
675 h->fd = -1;
676 send_event(EVENT_HANDLE_FINISHED, &h->id);
679 return true;
682 /* Reset writing position and data buffer of a handle to its current offset.
683 Use this after having set the new offset to use. */
684 static void reset_handle(int handle_id)
686 logf("reset_handle(%d)", handle_id);
688 struct memory_handle *h = find_handle(handle_id);
689 if (!h)
690 return;
692 h->ridx = h->widx = h->data;
693 if (h == cur_handle)
694 buf_widx = h->widx;
695 h->available = 0;
696 h->filerem = h->filesize - h->offset;
698 if (h->fd >= 0) {
699 lseek(h->fd, h->offset, SEEK_SET);
703 /* Seek to a nonbuffered part of a handle by rebuffering the data. */
704 static void rebuffer_handle(int handle_id, size_t newpos)
706 struct memory_handle *h = find_handle(handle_id);
707 if (!h)
708 return;
710 /* When seeking foward off of the buffer, if it is a short seek don't
711 rebuffer the whole track, just read enough to satisfy */
712 if (newpos > h->offset && newpos - h->offset < BUFFERING_DEFAULT_FILECHUNK)
714 LOGFQUEUE("buffering >| Q_BUFFER_HANDLE %d", handle_id);
715 queue_send(&buffering_queue, Q_BUFFER_HANDLE, handle_id);
716 h->ridx = h->data + newpos;
717 return;
720 h->offset = newpos;
722 /* Reset the handle to its new offset */
723 LOGFQUEUE("buffering >| Q_RESET_HANDLE %d", handle_id);
724 queue_send(&buffering_queue, Q_RESET_HANDLE, handle_id);
726 size_t next = (unsigned)((void *)h->next - (void *)buffer);
727 if (RINGBUF_SUB(next, h->data) < h->filesize - newpos)
729 /* There isn't enough space to rebuffer all of the track from its new
730 offset, so we ask the user to free some */
731 DEBUGF("rebuffer_handle: space is needed\n");
732 send_event(EVENT_HANDLE_REBUFFER, &handle_id);
735 /* Now we ask for a rebuffer */
736 LOGFQUEUE("buffering >| Q_BUFFER_HANDLE %d", handle_id);
737 queue_send(&buffering_queue, Q_BUFFER_HANDLE, handle_id);
740 static bool close_handle(int handle_id)
742 struct memory_handle *h = find_handle(handle_id);
744 /* If the handle is not found, it is closed */
745 if (!h)
746 return true;
748 if (h->fd >= 0) {
749 close(h->fd);
750 h->fd = -1;
753 /* rm_handle returns true unless the handle somehow persists after exit */
754 return rm_handle(h);
757 /* Free buffer space by moving the handle struct right before the useful
758 part of its data buffer or by moving all the data. */
759 static void shrink_handle(struct memory_handle *h)
761 size_t delta;
763 if (!h)
764 return;
766 if (h->next && h->filerem == 0 &&
767 (h->type == TYPE_ID3 || h->type == TYPE_CUESHEET ||
768 h->type == TYPE_BITMAP || h->type == TYPE_CODEC ||
769 h->type == TYPE_ATOMIC_AUDIO))
771 /* metadata handle: we can move all of it */
772 size_t handle_distance =
773 RINGBUF_SUB((unsigned)((void *)h->next - (void*)buffer), h->data);
774 delta = handle_distance - h->available;
776 /* The value of delta might change for alignment reasons */
777 if (!move_handle(&h, &delta, h->available, h->type==TYPE_CODEC))
778 return;
780 size_t olddata = h->data;
781 h->data = RINGBUF_ADD(h->data, delta);
782 h->ridx = RINGBUF_ADD(h->ridx, delta);
783 h->widx = RINGBUF_ADD(h->widx, delta);
785 if (h->type == TYPE_ID3 && h->filesize == sizeof(struct mp3entry)) {
786 /* when moving an mp3entry we need to readjust its pointers. */
787 adjust_mp3entry((struct mp3entry *)&buffer[h->data],
788 (void *)&buffer[h->data],
789 (const void *)&buffer[olddata]);
790 } else if (h->type == TYPE_BITMAP) {
791 /* adjust the bitmap's pointer */
792 struct bitmap *bmp = (struct bitmap *)&buffer[h->data];
793 bmp->data = &buffer[h->data + sizeof(struct bitmap)];
796 else
798 /* only move the handle struct */
799 delta = RINGBUF_SUB(h->ridx, h->data);
800 if (!move_handle(&h, &delta, 0, true))
801 return;
803 h->data = RINGBUF_ADD(h->data, delta);
804 h->available -= delta;
805 h->offset += delta;
809 /* Fill the buffer by buffering as much data as possible for handles that still
810 have data left to buffer
811 Return whether or not to continue filling after this */
812 static bool fill_buffer(void)
814 logf("fill_buffer()");
815 struct memory_handle *m;
816 shrink_handle(first_handle);
817 m = first_handle;
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 %d", h->id);
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 %d", (int)ev.data);
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 %d", (int)ev.data);
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 %d", (int)ev.data);
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 %d", (int)ev.data);
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 %d", (int)ev.data);
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();
1433 else if (data_counters.remaining == 0)
1434 filling = false;
1436 else if (ev.id == SYS_TIMEOUT)
1438 if (data_counters.remaining > 0 &&
1439 data_counters.useful <= conf_watermark) {
1440 shrink_buffer();
1441 filling = fill_buffer();
1448 void buffering_init(void)
1450 mutex_init(&llist_mutex);
1451 #ifdef HAVE_PRIORITY_SCHEDULING
1452 /* This behavior not safe atm */
1453 mutex_set_preempt(&llist_mutex, false);
1454 #endif
1456 conf_watermark = BUFFERING_DEFAULT_WATERMARK;
1458 queue_init(&buffering_queue, true);
1459 buffering_thread_p = create_thread( buffering_thread, buffering_stack,
1460 sizeof(buffering_stack), CREATE_THREAD_FROZEN,
1461 buffering_thread_name IF_PRIO(, PRIORITY_BUFFERING)
1462 IF_COP(, CPU));
1464 queue_enable_queue_send(&buffering_queue, &buffering_queue_sender_list,
1465 buffering_thread_p);
1468 /* Initialise the buffering subsystem */
1469 bool buffering_reset(char *buf, size_t buflen)
1471 if (!buf || !buflen)
1472 return false;
1474 buffer = buf;
1475 buffer_len = buflen;
1476 guard_buffer = buf + buflen;
1478 buf_widx = 0;
1479 buf_ridx = 0;
1481 first_handle = NULL;
1482 cur_handle = NULL;
1483 cached_handle = NULL;
1484 num_handles = 0;
1485 base_handle_id = -1;
1487 /* Set the high watermark as 75% full...or 25% empty :) */
1488 #if MEM > 8
1489 high_watermark = 3*buflen / 4;
1490 #endif
1492 thread_thaw(buffering_thread_p);
1494 return true;
1497 void buffering_get_debugdata(struct buffering_debug *dbgdata)
1499 update_data_counters();
1500 dbgdata->num_handles = num_handles;
1501 dbgdata->data_rem = data_counters.remaining;
1502 dbgdata->wasted_space = data_counters.wasted;
1503 dbgdata->buffered_data = data_counters.buffered;
1504 dbgdata->useful_data = data_counters.useful;