Hopefully fix FS#8506 (OF cant be loaded on some PP targets). also hopefully fixes...
[Rockbox.git] / apps / buffering.c
blob99a4a3b058684955568aea7f0ea9f06d1d63de49
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"
54 #ifdef SIMULATOR
55 #define ata_disk_is_active() 1
56 #endif
58 #if MEM > 1
59 #define GUARD_BUFSIZE (32*1024)
60 #else
61 #define GUARD_BUFSIZE (8*1024)
62 #endif
64 /* Define LOGF_ENABLE to enable logf output in this file */
65 /*#define LOGF_ENABLE*/
66 #include "logf.h"
68 /* macros to enable logf for queues
69 logging on SYS_TIMEOUT can be disabled */
70 #ifdef SIMULATOR
71 /* Define this for logf output of all queuing except SYS_TIMEOUT */
72 #define BUFFERING_LOGQUEUES
73 /* Define this to logf SYS_TIMEOUT messages */
74 /* #define BUFFERING_LOGQUEUES_SYS_TIMEOUT */
75 #endif
77 #ifdef BUFFERING_LOGQUEUES
78 #define LOGFQUEUE logf
79 #else
80 #define LOGFQUEUE(...)
81 #endif
83 #ifdef BUFFERING_LOGQUEUES_SYS_TIMEOUT
84 #define LOGFQUEUE_SYS_TIMEOUT logf
85 #else
86 #define LOGFQUEUE_SYS_TIMEOUT(...)
87 #endif
89 /* default point to start buffer refill */
90 #define BUFFERING_DEFAULT_WATERMARK (1024*512)
91 /* amount of data to read in one read() call */
92 #define BUFFERING_DEFAULT_FILECHUNK (1024*32)
93 /* point at which the file buffer will fight for CPU time */
94 #define BUFFERING_CRITICAL_LEVEL (1024*128)
96 #define BUF_HANDLE_MASK 0x7FFFFFFF
99 /* Ring buffer helper macros */
100 /* Buffer pointer (p) plus value (v), wrapped if necessary */
101 #define RINGBUF_ADD(p,v) (((p)+(v))<buffer_len ? (p)+(v) : (p)+(v)-buffer_len)
102 /* Buffer pointer (p) minus value (v), wrapped if necessary */
103 #define RINGBUF_SUB(p,v) ((p>=v) ? (p)-(v) : (p)+buffer_len-(v))
104 /* How far value (v) plus buffer pointer (p1) will cross buffer pointer (p2) */
105 #define RINGBUF_ADD_CROSS(p1,v,p2) \
106 ((p1<p2) ? (int)((p1)+(v))-(int)(p2) : (int)((p1)+(v)-(p2))-(int)buffer_len)
107 /* Bytes available in the buffer */
108 #define BUF_USED RINGBUF_SUB(buf_widx, buf_ridx)
110 /* assert(sizeof(struct memory_handle)%4==0) */
111 struct memory_handle {
112 int id; /* A unique ID for the handle */
113 enum data_type type; /* Type of data buffered with this handle */
114 char path[MAX_PATH]; /* Path if data originated in a file */
115 int fd; /* File descriptor to path (-1 if closed) */
116 size_t data; /* Start index of the handle's data buffer */
117 volatile size_t ridx; /* Read pointer, relative to the main buffer */
118 size_t widx; /* Write pointer */
119 size_t filesize; /* File total length */
120 size_t filerem; /* Remaining bytes of file NOT in buffer */
121 volatile size_t available; /* Available bytes to read from buffer */
122 size_t offset; /* Offset at which we started reading the file */
123 struct memory_handle *next;
125 /* invariant: filesize == offset + available + filerem */
127 static char *buffer;
128 static char *guard_buffer;
130 static size_t buffer_len;
132 static volatile size_t buf_widx; /* current writing position */
133 static volatile size_t buf_ridx; /* current reading position */
134 /* buf_*idx are values relative to the buffer, not real pointers. */
136 /* Configuration */
137 static size_t conf_watermark = 0; /* Level to trigger filebuf fill */
138 #if MEM > 8
139 static size_t high_watermark = 0; /* High watermark for rebuffer */
140 #endif
142 /* current memory handle in the linked list. NULL when the list is empty. */
143 static struct memory_handle *cur_handle;
144 /* first memory handle in the linked list. NULL when the list is empty. */
145 static struct memory_handle *first_handle;
147 static int num_handles; /* number of handles in the list */
149 static int base_handle_id;
151 static struct mutex llist_mutex;
153 /* Handle cache (makes find_handle faster).
154 This is global so that move_handle and rm_handle can invalidate it. */
155 static struct memory_handle *cached_handle = NULL;
157 static struct {
158 size_t remaining; /* Amount of data needing to be buffered */
159 size_t wasted; /* Amount of space available for freeing */
160 size_t buffered; /* Amount of data currently in the buffer */
161 size_t useful; /* Amount of data still useful to the user */
162 } data_counters;
165 /* Messages available to communicate with the buffering thread */
166 enum {
167 Q_BUFFER_HANDLE = 1, /* Request buffering of a handle, this should not be
168 used in a low buffer situation. */
169 Q_RESET_HANDLE, /* (internal) Request resetting of a handle to its
170 offset (the offset has to be set beforehand) */
171 Q_CLOSE_HANDLE, /* Request closing a handle */
172 Q_BASE_HANDLE, /* Set the reference handle for buf_useful_data */
174 /* Configuration: */
175 Q_SET_WATERMARK,
176 Q_START_FILL, /* Request that the buffering thread initiate a buffer
177 fill at its earliest convenience */
178 Q_HANDLE_ADDED, /* Inform the buffering thread that a handle was added,
179 (which means the disk is spinning) */
182 /* Buffering thread */
183 static void buffering_thread(void);
184 static long buffering_stack[(DEFAULT_STACK_SIZE + 0x2000)/sizeof(long)];
185 static const char buffering_thread_name[] = "buffering";
186 static struct thread_entry *buffering_thread_p;
187 static struct event_queue buffering_queue;
188 static struct queue_sender_list buffering_queue_sender_list;
193 LINKED LIST MANAGEMENT
194 ======================
196 add_handle : Add a handle to the list
197 rm_handle : Remove a handle from the list
198 find_handle : Get a handle pointer from an ID
199 move_handle : Move a handle in the buffer (with or without its data)
201 These functions only handle the linked list structure. They don't touch the
202 contents of the struct memory_handle headers. They also change the buf_*idx
203 pointers when necessary and manage the handle IDs.
205 The first and current (== last) handle are kept track of.
206 A new handle is added at buf_widx and becomes the current one.
207 buf_widx always points to the current writing position for the current handle
208 buf_ridx always points to the location of the first handle.
209 buf_ridx == buf_widx means the buffer is empty.
213 /* Add a new handle to the linked list and return it. It will have become the
214 new current handle.
215 data_size must contain the size of what will be in the handle.
216 can_wrap tells us whether this type of data may wrap on buffer
217 alloc_all tells us if we must immediately be able to allocate data_size
218 returns a valid memory handle if all conditions for allocation are met.
219 NULL if there memory_handle itself cannot be allocated or if the
220 data_size cannot be allocated and alloc_all is set. This function's
221 only potential side effect is to allocate space for the cur_handle
222 if it returns NULL.
224 static struct memory_handle *add_handle(size_t data_size, bool can_wrap,
225 bool alloc_all)
227 /* gives each handle a unique id */
228 static int cur_handle_id = 0;
229 size_t shift;
230 size_t new_widx;
231 size_t len;
232 int overlap;
234 if (num_handles >= BUF_MAX_HANDLES)
235 return NULL;
237 mutex_lock(&llist_mutex);
239 if (cur_handle && cur_handle->filerem > 0) {
240 /* the current handle hasn't finished buffering. We can only add
241 a new one if there is already enough free space to finish
242 the buffering. */
243 size_t req = cur_handle->filerem + sizeof(struct memory_handle);
244 if (RINGBUF_ADD_CROSS(cur_handle->widx, req, buf_ridx) >= 0) {
245 /* Not enough space */
246 mutex_unlock(&llist_mutex);
247 return NULL;
248 } else {
249 /* Allocate the remainder of the space for the current handle */
250 buf_widx = RINGBUF_ADD(cur_handle->widx, cur_handle->filerem);
254 /* align to 4 bytes up */
255 new_widx = RINGBUF_ADD(buf_widx, 3) & ~3;
257 len = data_size + sizeof(struct memory_handle);
259 /* First, will the handle wrap? */
260 overlap = RINGBUF_ADD_CROSS(new_widx, sizeof(struct memory_handle),
261 buffer_len - 1);
262 /* If the handle would wrap, move to the beginning of the buffer,
263 * otherwise check if the data can/would wrap and move it to the
264 * beginning if needed */
265 if (overlap > 0) {
266 new_widx = 0;
267 } else if (!can_wrap) {
268 overlap = RINGBUF_ADD_CROSS(new_widx, len, buffer_len - 1);
269 if (overlap > 0)
270 new_widx += data_size - overlap;
273 /* How far we shifted buf_widx to align things, must be < buffer_len */
274 shift = RINGBUF_SUB(new_widx, buf_widx);
276 /* How much space are we short in the actual ring buffer? */
277 overlap = RINGBUF_ADD_CROSS(buf_widx, shift + len, buf_ridx);
278 if (overlap >= 0 && (alloc_all || (unsigned)overlap > data_size)) {
279 /* Not enough space for required allocations */
280 mutex_unlock(&llist_mutex);
281 return NULL;
284 /* There is enough space for the required data, advance the buf_widx and
285 * initialize the struct */
286 buf_widx = new_widx;
288 struct memory_handle *new_handle =
289 (struct memory_handle *)(&buffer[buf_widx]);
291 /* only advance the buffer write index of the size of the struct */
292 buf_widx = RINGBUF_ADD(buf_widx, sizeof(struct memory_handle));
294 new_handle->id = cur_handle_id;
295 /* Wrap signed int is safe and 0 doesn't happen */
296 cur_handle_id = (cur_handle_id + 1) & BUF_HANDLE_MASK;
297 new_handle->next = NULL;
298 num_handles++;
300 if (!first_handle)
301 /* the new handle is the first one */
302 first_handle = new_handle;
304 if (cur_handle)
305 cur_handle->next = new_handle;
307 cur_handle = new_handle;
309 mutex_unlock(&llist_mutex);
310 return new_handle;
313 /* Delete a given memory handle from the linked list
314 and return true for success. Nothing is actually erased from memory. */
315 static bool rm_handle(const struct memory_handle *h)
317 if (h == NULL)
318 return true;
320 mutex_lock(&llist_mutex);
322 if (h == first_handle) {
323 first_handle = h->next;
324 if (h == cur_handle) {
325 /* h was the first and last handle: the buffer is now empty */
326 cur_handle = NULL;
327 buf_ridx = buf_widx = 0;
328 } else {
329 /* update buf_ridx to point to the new first handle */
330 buf_ridx = (void *)first_handle - (void *)buffer;
332 } else {
333 struct memory_handle *m = first_handle;
334 /* Find the previous handle */
335 while (m && m->next != h) {
336 m = m->next;
338 if (m && m->next == h) {
339 m->next = h->next;
340 if (h == cur_handle) {
341 cur_handle = m;
342 buf_widx = cur_handle->widx;
344 } else {
345 mutex_unlock(&llist_mutex);
346 return false;
350 /* Invalidate the cache to prevent it from keeping the old location of h */
351 if (h == cached_handle)
352 cached_handle = NULL;
354 num_handles--;
356 mutex_unlock(&llist_mutex);
357 return true;
360 /* Return a pointer to the memory handle of given ID.
361 NULL if the handle wasn't found */
362 static struct memory_handle *find_handle(int handle_id)
364 if (handle_id < 0)
365 return NULL;
367 mutex_lock(&llist_mutex);
369 /* simple caching because most of the time the requested handle
370 will either be the same as the last, or the one after the last */
371 if (cached_handle)
373 if (cached_handle->id == handle_id) {
374 mutex_unlock(&llist_mutex);
375 return cached_handle;
376 } else if (cached_handle->next &&
377 (cached_handle->next->id == handle_id)) {
378 cached_handle = cached_handle->next;
379 mutex_unlock(&llist_mutex);
380 return cached_handle;
384 struct memory_handle *m = first_handle;
385 while (m && m->id != handle_id) {
386 m = m->next;
388 /* This condition can only be reached with !m or m->id == handle_id */
389 if (m)
390 cached_handle = m;
392 mutex_unlock(&llist_mutex);
393 return m;
396 /* Move a memory handle and data_size of its data delta bytes along the buffer.
397 delta maximum bytes available to move the handle. If the move is performed
398 it is set to the actual distance moved.
399 data_size is the amount of data to move along with the struct.
400 returns a valid memory_handle if the move is successful
401 NULL if the handle is NULL, the move would be less than the size of
402 a memory_handle after correcting for wraps or if the handle is not
403 found in the linked list for adjustment. This function has no side
404 effects if NULL is returned. */
405 static bool move_handle(struct memory_handle **h, size_t *delta,
406 size_t data_size, bool can_wrap)
408 struct memory_handle *dest;
409 const struct memory_handle *src;
410 size_t newpos;
411 size_t size_to_move;
412 size_t final_delta = *delta;
413 int overlap;
415 if (h == NULL || (src = *h) == NULL)
416 return false;
418 size_to_move = sizeof(struct memory_handle) + data_size;
420 /* Align to four bytes, down */
421 final_delta &= ~3;
422 if (final_delta < sizeof(struct memory_handle)) {
423 /* It's not legal to move less than the size of the struct */
424 return false;
427 mutex_lock(&llist_mutex);
429 newpos = RINGBUF_ADD((void *)src - (void *)buffer, final_delta);
430 overlap = RINGBUF_ADD_CROSS(newpos, size_to_move, buffer_len - 1);
432 if (overlap > 0) {
433 /* Some part of the struct + data would wrap, maybe ok */
434 size_t correction = 0;
435 /* If the overlap lands inside the memory_handle */
436 if ((unsigned)overlap > data_size) {
437 /* Correct the position and real delta to prevent the struct from
438 * wrapping, this guarantees an aligned delta, I think */
439 correction = overlap - data_size;
440 } else if (!can_wrap) {
441 /* Otherwise the overlap falls in the data area and must all be
442 * backed out. This may become conditional if ever we move
443 * data that is allowed to wrap (ie audio) */
444 correction = overlap;
445 /* Align correction to four bytes, up */
446 correction = (correction+3) & ~3;
448 if (correction) {
449 if (final_delta < correction + sizeof(struct memory_handle)) {
450 /* Delta cannot end up less than the size of the struct */
451 mutex_unlock(&llist_mutex);
452 return false;
455 newpos -= correction;
456 overlap -= correction;/* Used below to know how to split the data */
457 final_delta -= correction;
461 dest = (struct memory_handle *)(&buffer[newpos]);
463 if (src == first_handle) {
464 first_handle = dest;
465 buf_ridx = newpos;
466 } else {
467 struct memory_handle *m = first_handle;
468 while (m && m->next != src) {
469 m = m->next;
471 if (m && m->next == src) {
472 m->next = dest;
473 } else {
474 mutex_unlock(&llist_mutex);
475 return false;
480 /* Update the cache to prevent it from keeping the old location of h */
481 if (src == cached_handle)
482 cached_handle = dest;
484 /* the cur_handle pointer might need updating */
485 if (src == cur_handle)
486 cur_handle = dest;
488 if (overlap > 0) {
489 size_t first_part = size_to_move - overlap;
490 memmove(dest, src, first_part);
491 memmove(buffer, (const char *)src + first_part, overlap);
492 } else {
493 memmove(dest, src, size_to_move);
496 /* Update the caller with the new location of h and the distance moved */
497 *h = dest;
498 *delta = final_delta;
499 mutex_unlock(&llist_mutex);
500 return dest;
505 BUFFER SPACE MANAGEMENT
506 =======================
508 update_data_counters: Updates the values in data_counters
509 buffer_is_low : Returns true if the amount of useful data in the buffer is low
510 buffer_handle : Buffer data for a handle
511 reset_handle : Reset write position and data buffer of a handle to its offset
512 rebuffer_handle : Seek to a nonbuffered part of a handle by rebuffering the data
513 shrink_handle : Free buffer space by moving a handle
514 fill_buffer : Call buffer_handle for all handles that have data to buffer
516 These functions are used by the buffering thread to manage buffer space.
519 static void update_data_counters(void)
521 struct memory_handle *m = find_handle(base_handle_id);
522 bool is_useful = m==NULL;
524 size_t buffered = 0;
525 size_t wasted = 0;
526 size_t remaining = 0;
527 size_t useful = 0;
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 data_counters.buffered = buffered;
545 data_counters.wasted = wasted;
546 data_counters.remaining = remaining;
547 data_counters.useful = useful;
550 static inline bool buffer_is_low(void)
552 update_data_counters();
553 return data_counters.useful < BUFFERING_CRITICAL_LEVEL;
556 /* Buffer data for the given handle.
557 Return whether or not the buffering should continue explicitly. */
558 static bool buffer_handle(int handle_id)
560 logf("buffer_handle(%d)", handle_id);
561 struct memory_handle *h = find_handle(handle_id);
562 if (!h)
563 return true;
565 if (h->filerem == 0) {
566 /* nothing left to buffer */
567 return true;
570 if (h->fd < 0) /* file closed, reopen */
572 if (*h->path)
573 h->fd = open(h->path, O_RDONLY);
575 if (h->fd < 0)
577 /* could not open the file, truncate it where it is */
578 h->filesize -= h->filerem;
579 h->filerem = 0;
580 return true;
583 if (h->offset)
584 lseek(h->fd, h->offset, SEEK_SET);
587 trigger_cpu_boost();
589 while (h->filerem > 0)
591 /* max amount to copy */
592 size_t copy_n = MIN( MIN(h->filerem, BUFFERING_DEFAULT_FILECHUNK),
593 buffer_len - h->widx);
595 /* stop copying if it would overwrite the reading position */
596 if (RINGBUF_ADD_CROSS(h->widx, copy_n, buf_ridx) >= 0)
597 return false;
599 /* This would read into the next handle, this is broken */
600 if (h->next && RINGBUF_ADD_CROSS(h->widx, copy_n,
601 (unsigned)((void *)h->next - (void *)buffer)) > 0) {
602 /* Try to recover by truncating this file */
603 copy_n = RINGBUF_ADD_CROSS(h->widx, copy_n,
604 (unsigned)((void *)h->next - (void *)buffer));
605 h->filerem -= copy_n;
606 h->filesize -= copy_n;
607 logf("buf alloc short %ld", (long)copy_n);
608 if (h->filerem)
609 continue;
610 else
611 break;
614 /* rc is the actual amount read */
615 int rc = read(h->fd, &buffer[h->widx], copy_n);
617 if (rc < 0)
619 /* Some kind of filesystem error, maybe recoverable if not codec */
620 if (h->type == TYPE_CODEC) {
621 logf("Partial codec");
622 break;
625 DEBUGF("File ended %ld bytes early\n", (long)h->filerem);
626 h->filesize -= h->filerem;
627 h->filerem = 0;
628 break;
631 /* Advance buffer */
632 h->widx = RINGBUF_ADD(h->widx, rc);
633 if (h == cur_handle)
634 buf_widx = h->widx;
635 h->available += rc;
636 h->filerem -= rc;
638 /* If this is a large file, see if we need to break or give the codec
639 * more time */
640 if (h->type == TYPE_PACKET_AUDIO &&
641 pcmbuf_is_lowdata() && !buffer_is_low())
643 sleep(1);
645 else
647 yield();
650 if (!queue_empty(&buffering_queue))
651 break;
654 if (h->filerem == 0) {
655 /* finished buffering the file */
656 close(h->fd);
657 h->fd = -1;
658 send_event(EVENT_HANDLE_FINISHED, &h->id);
661 return true;
664 /* Reset writing position and data buffer of a handle to its current offset.
665 Use this after having set the new offset to use. */
666 static void reset_handle(int handle_id)
668 logf("reset_handle(%d)", handle_id);
670 struct memory_handle *h = find_handle(handle_id);
671 if (!h)
672 return;
674 h->widx = h->data;
675 if (h == cur_handle)
676 buf_widx = h->widx;
677 h->available = 0;
678 h->filerem = h->filesize - h->offset;
680 if (h->fd >= 0) {
681 lseek(h->fd, h->offset, SEEK_SET);
685 /* Seek to a nonbuffered part of a handle by rebuffering the data. */
686 static void rebuffer_handle(int handle_id, size_t newpos)
688 struct memory_handle *h = find_handle(handle_id);
689 if (!h)
690 return;
692 /* When seeking foward off of the buffer, if it is a short seek don't
693 rebuffer the whole track, just read enough to satisfy */
694 if (newpos > h->offset && newpos - h->offset < BUFFERING_DEFAULT_FILECHUNK)
696 LOGFQUEUE("buffering >| Q_BUFFER_HANDLE");
697 queue_send(&buffering_queue, Q_BUFFER_HANDLE, handle_id);
698 h->ridx = h->data + newpos;
699 return;
702 h->offset = newpos;
704 /* Reset the handle to its new offset */
705 LOGFQUEUE("buffering >| Q_RESET_HANDLE");
706 queue_send(&buffering_queue, Q_RESET_HANDLE, handle_id);
708 size_t next = (unsigned)((void *)h->next - (void *)buffer);
709 if (next - h->data < h->filesize - newpos)
711 /* There isn't enough space to rebuffer all of the track from its new
712 offset, so we ask the user to free some */
713 DEBUGF("rebuffer_handle: space is needed\n");
714 send_event(EVENT_HANDLE_REBUFFER, &handle_id);
717 /* Now we ask for a rebuffer */
718 LOGFQUEUE("buffering >| Q_BUFFER_HANDLE");
719 queue_send(&buffering_queue, Q_BUFFER_HANDLE, handle_id);
721 h->ridx = h->data;
724 static bool close_handle(int handle_id)
726 struct memory_handle *h = find_handle(handle_id);
728 /* If the handle is not found, it is closed */
729 if (!h)
730 return true;
732 if (h->fd >= 0) {
733 close(h->fd);
734 h->fd = -1;
737 /* rm_handle returns true unless the handle somehow persists after exit */
738 return rm_handle(h);
741 /* Free buffer space by moving the handle struct right before the useful
742 part of its data buffer or by moving all the data. */
743 static void shrink_handle(struct memory_handle *h)
745 size_t delta;
747 if (!h)
748 return;
750 if (h->next && h->filerem == 0 &&
751 (h->type == TYPE_ID3 || h->type == TYPE_CUESHEET ||
752 h->type == TYPE_BITMAP || h->type == TYPE_CODEC ||
753 h->type == TYPE_ATOMIC_AUDIO))
755 /* metadata handle: we can move all of it */
756 size_t handle_distance =
757 RINGBUF_SUB((unsigned)((void *)h->next - (void*)buffer), h->data);
758 delta = handle_distance - h->available;
760 /* The value of delta might change for alignment reasons */
761 if (!move_handle(&h, &delta, h->available, h->type==TYPE_CODEC))
762 return;
764 size_t olddata = h->data;
765 h->data = RINGBUF_ADD(h->data, delta);
766 h->ridx = RINGBUF_ADD(h->ridx, delta);
767 h->widx = RINGBUF_ADD(h->widx, delta);
769 if (h->type == TYPE_ID3 && h->filesize == sizeof(struct mp3entry)) {
770 /* when moving an mp3entry we need to readjust its pointers. */
771 adjust_mp3entry((struct mp3entry *)&buffer[h->data],
772 (void *)&buffer[h->data],
773 (const void *)&buffer[olddata]);
774 } else if (h->type == TYPE_BITMAP) {
775 /* adjust the bitmap's pointer */
776 struct bitmap *bmp = (struct bitmap *)&buffer[h->data];
777 bmp->data = &buffer[h->data + sizeof(struct bitmap)];
780 else
782 /* only move the handle struct */
783 delta = RINGBUF_SUB(h->ridx, h->data);
784 if (!move_handle(&h, &delta, 0, true))
785 return;
787 h->data = RINGBUF_ADD(h->data, delta);
788 h->available -= delta;
789 h->offset += delta;
793 /* Fill the buffer by buffering as much data as possible for handles that still
794 have data left to buffer
795 Return whether or not to continue filling after this */
796 static bool fill_buffer(void)
798 logf("fill_buffer()");
799 struct memory_handle *m = first_handle;
800 shrink_handle(m);
801 while (queue_empty(&buffering_queue) && m) {
802 if (m->filerem > 0) {
803 if (!buffer_handle(m->id)) {
804 m = NULL;
805 break;
808 m = m->next;
811 if (m) {
812 return true;
814 else
816 #ifndef SIMULATOR
817 /* only spin the disk down if the filling wasn't interrupted by an
818 event arriving in the queue. */
819 ata_sleep();
820 #endif
821 return false;
825 #ifdef HAVE_ALBUMART
826 /* Given a file descriptor to a bitmap file, write the bitmap data to the
827 buffer, with a struct bitmap and the actual data immediately following.
828 Return value is the total size (struct + data). */
829 static int load_bitmap(int fd)
831 int rc;
832 struct bitmap *bmp = (struct bitmap *)&buffer[buf_widx];
833 /* FIXME: alignment may be needed for the data buffer. */
834 bmp->data = &buffer[buf_widx + sizeof(struct bitmap)];
836 #if (LCD_DEPTH > 1) || defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1)
837 bmp->maskdata = NULL;
838 #endif
840 int free = (int)MIN(buffer_len - BUF_USED, buffer_len - buf_widx);
841 rc = read_bmp_fd(fd, bmp, free, FORMAT_ANY|FORMAT_DITHER);
842 return rc + (rc > 0 ? sizeof(struct bitmap) : 0);
844 #endif
848 MAIN BUFFERING API CALLS
849 ========================
851 bufopen : Request the opening of a new handle for a file
852 bufalloc : Open a new handle for data other than a file.
853 bufclose : Close an open handle
854 bufseek : Set the read pointer in a handle
855 bufadvance : Move the read pointer in a handle
856 bufread : Copy data from a handle into a given buffer
857 bufgetdata : Give a pointer to the handle's data
859 These functions are exported, to allow interaction with the buffer.
860 They take care of the content of the structs, and rely on the linked list
861 management functions for all the actual handle management work.
865 /* Reserve space in the buffer for a file.
866 filename: name of the file to open
867 offset: offset at which to start buffering the file, useful when the first
868 (offset-1) bytes of the file aren't needed.
869 return value: <0 if the file cannot be opened, or one file already
870 queued to be opened, otherwise the handle for the file in the buffer
872 int bufopen(const char *file, size_t offset, enum data_type type)
874 size_t adjusted_offset = offset;
876 int fd = open(file, O_RDONLY);
877 if (fd < 0)
878 return ERR_FILE_ERROR;
880 size_t size = filesize(fd);
881 bool can_wrap = type==TYPE_PACKET_AUDIO || type==TYPE_CODEC;
883 if (adjusted_offset > size)
884 adjusted_offset = 0;
886 struct memory_handle *h = add_handle(size-adjusted_offset, can_wrap, false);
887 if (!h)
889 DEBUGF("bufopen: failed to add handle\n");
890 close(fd);
891 return ERR_BUFFER_FULL;
894 strncpy(h->path, file, MAX_PATH);
895 h->offset = adjusted_offset;
896 h->ridx = buf_widx;
897 h->data = buf_widx;
898 h->type = type;
900 #ifdef HAVE_ALBUMART
901 if (type == TYPE_BITMAP)
903 /* Bitmap file: we load the data instead of the file */
904 int rc;
905 mutex_lock(&llist_mutex); /* Lock because load_bitmap yields */
906 rc = load_bitmap(fd);
907 if (rc <= 0)
909 rm_handle(h);
910 close(fd);
911 mutex_unlock(&llist_mutex);
912 return ERR_FILE_ERROR;
914 h->filerem = 0;
915 h->filesize = rc;
916 h->available = rc;
917 h->widx = buf_widx + rc; /* safe because the data doesn't wrap */
918 buf_widx += rc; /* safe too */
919 mutex_unlock(&llist_mutex);
921 else
922 #endif
924 h->filerem = size - adjusted_offset;
925 h->filesize = size;
926 h->available = 0;
927 h->widx = buf_widx;
930 if (type == TYPE_CUESHEET) {
931 h->fd = fd;
932 /* Immediately start buffering those */
933 LOGFQUEUE("buffering >| Q_BUFFER_HANDLE");
934 queue_send(&buffering_queue, Q_BUFFER_HANDLE, h->id);
935 } else {
936 /* Other types will get buffered in the course of normal operations */
937 h->fd = -1;
938 close(fd);
940 /* Inform the buffering thread that we added a handle */
941 LOGFQUEUE("buffering > Q_HANDLE_ADDED %d", h->id);
942 queue_post(&buffering_queue, Q_HANDLE_ADDED, h->id);
945 logf("bufopen: new hdl %d", h->id);
946 return h->id;
949 /* Open a new handle from data that needs to be copied from memory.
950 src is the source buffer from which to copy data. It can be NULL to simply
951 reserve buffer space.
952 size is the requested size. The call will only be successful if the
953 requested amount of data can entirely fit in the buffer without wrapping.
954 Return value is the handle id for success or <0 for failure.
956 int bufalloc(const void *src, size_t size, enum data_type type)
958 struct memory_handle *h = add_handle(size, false, true);
960 if (!h)
961 return ERR_BUFFER_FULL;
963 if (src) {
964 if (type == TYPE_ID3 && size == sizeof(struct mp3entry)) {
965 /* specially take care of struct mp3entry */
966 copy_mp3entry((struct mp3entry *)&buffer[buf_widx],
967 (const struct mp3entry *)src);
968 } else {
969 memcpy(&buffer[buf_widx], src, size);
973 h->fd = -1;
974 *h->path = 0;
975 h->filesize = size;
976 h->filerem = 0;
977 h->offset = 0;
978 h->ridx = buf_widx;
979 h->widx = buf_widx + size; /* this is safe because the data doesn't wrap */
980 h->data = buf_widx;
981 h->available = size;
982 h->type = type;
984 buf_widx += size; /* safe too */
986 logf("bufalloc: new hdl %d", h->id);
987 return h->id;
990 /* Close the handle. Return true for success and false for failure */
991 bool bufclose(int handle_id)
993 logf("bufclose(%d)", handle_id);
995 LOGFQUEUE("buffering >| Q_CLOSE_HANDLE %d", handle_id);
996 return queue_send(&buffering_queue, Q_CLOSE_HANDLE, handle_id);
999 /* Set reading index in handle (relatively to the start of the file).
1000 Access before the available data will trigger a rebuffer.
1001 Return 0 for success and < 0 for failure:
1002 -1 if the handle wasn't found
1003 -2 if the new requested position was beyond the end of the file
1005 int bufseek(int handle_id, size_t newpos)
1007 struct memory_handle *h = find_handle(handle_id);
1008 if (!h)
1009 return ERR_HANDLE_NOT_FOUND;
1011 if (newpos > h->filesize) {
1012 /* access beyond the end of the file */
1013 return ERR_INVALID_VALUE;
1015 else if (newpos < h->offset || h->offset + h->available < newpos) {
1016 /* access before or after buffered data. A rebuffer is needed. */
1017 rebuffer_handle(handle_id, newpos);
1019 else {
1020 h->ridx = RINGBUF_ADD(h->data, newpos - h->offset);
1022 return 0;
1025 /* Advance the reading index in a handle (relatively to its current position).
1026 Return 0 for success and < 0 for failure */
1027 int bufadvance(int handle_id, off_t offset)
1029 const struct memory_handle *h = find_handle(handle_id);
1030 if (!h)
1031 return ERR_HANDLE_NOT_FOUND;
1033 size_t newpos = h->offset + RINGBUF_SUB(h->ridx, h->data) + offset;
1034 return bufseek(handle_id, newpos);
1037 /* Used by bufread and bufgetdata to prepare the buffer and retrieve the
1038 * actual amount of data available for reading. This function explicitly
1039 * does not check the validity of the input handle. It does do range checks
1040 * on size and returns a valid (and explicit) amount of data for reading */
1041 static struct memory_handle *prep_bufdata(int handle_id, size_t *size,
1042 bool guardbuf_limit)
1044 struct memory_handle *h = find_handle(handle_id);
1045 if (!h)
1046 return NULL;
1048 size_t avail = RINGBUF_SUB(h->widx, h->ridx);
1050 if (avail == 0 && h->filerem == 0)
1052 /* File is finished reading */
1053 *size = 0;
1054 return h;
1057 if (*size == 0 || *size > avail + h->filerem)
1058 *size = avail + h->filerem;
1060 if (guardbuf_limit && h->type == TYPE_PACKET_AUDIO && *size > GUARD_BUFSIZE)
1062 logf("data request > guardbuf");
1063 /* If more than the size of the guardbuf is requested and this is a
1064 * bufgetdata, limit to guard_bufsize over the end of the buffer */
1065 *size = MIN(*size, buffer_len - h->ridx + GUARD_BUFSIZE);
1066 /* this ensures *size <= buffer_len - h->ridx + GUARD_BUFSIZE */
1069 if (h->filerem > 0 && avail < *size)
1071 /* Data isn't ready. Request buffering */
1072 buf_request_buffer_handle(handle_id);
1073 /* Wait for the data to be ready */
1076 sleep(1);
1077 /* it is not safe for a non-buffering thread to sleep while
1078 * holding a handle */
1079 h = find_handle(handle_id);
1080 if (!h)
1081 return NULL;
1082 avail = RINGBUF_SUB(h->widx, h->ridx);
1084 while (h->filerem > 0 && avail < *size);
1087 *size = MIN(*size,avail);
1088 return h;
1091 /* Copy data from the given handle to the dest buffer.
1092 Return the number of bytes copied or < 0 for failure (handle not found).
1093 The caller is blocked until the requested amount of data is available.
1095 ssize_t bufread(int handle_id, size_t size, void *dest)
1097 const struct memory_handle *h;
1098 size_t adjusted_size = size;
1100 h = prep_bufdata(handle_id, &adjusted_size, false);
1101 if (!h)
1102 return ERR_HANDLE_NOT_FOUND;
1104 if (h->ridx + adjusted_size > buffer_len)
1106 /* the data wraps around the end of the buffer */
1107 size_t read = buffer_len - h->ridx;
1108 memcpy(dest, &buffer[h->ridx], read);
1109 memcpy(dest+read, buffer, adjusted_size - read);
1111 else
1113 memcpy(dest, &buffer[h->ridx], adjusted_size);
1116 return adjusted_size;
1119 /* Update the "data" pointer to make the handle's data available to the caller.
1120 Return the length of the available linear data or < 0 for failure (handle
1121 not found).
1122 The caller is blocked until the requested amount of data is available.
1123 size is the amount of linear data requested. it can be 0 to get as
1124 much as possible.
1125 The guard buffer may be used to provide the requested size. This means it's
1126 unsafe to request more than the size of the guard buffer.
1128 ssize_t bufgetdata(int handle_id, size_t size, void **data)
1130 const struct memory_handle *h;
1131 size_t adjusted_size = size;
1133 h = prep_bufdata(handle_id, &adjusted_size, true);
1134 if (!h)
1135 return ERR_HANDLE_NOT_FOUND;
1137 if (h->ridx + adjusted_size > buffer_len)
1139 /* the data wraps around the end of the buffer :
1140 use the guard buffer to provide the requested amount of data. */
1141 size_t copy_n = h->ridx + adjusted_size - buffer_len;
1142 /* prep_bufdata ensures adjusted_size <= buffer_len - h->ridx + GUARD_BUFSIZE,
1143 so copy_n <= GUARD_BUFSIZE */
1144 memcpy(guard_buffer, (const unsigned char *)buffer, copy_n);
1147 if (data)
1148 *data = &buffer[h->ridx];
1150 return adjusted_size;
1153 ssize_t bufgettail(int handle_id, size_t size, void **data)
1155 size_t tidx;
1157 const struct memory_handle *h;
1159 h = find_handle(handle_id);
1161 if (!h)
1162 return ERR_HANDLE_NOT_FOUND;
1164 if (h->filerem)
1165 return ERR_HANDLE_NOT_DONE;
1167 /* We don't support tail requests of > guardbuf_size, for simplicity */
1168 if (size > GUARD_BUFSIZE)
1169 return ERR_INVALID_VALUE;
1171 tidx = RINGBUF_SUB(h->widx, size);
1173 if (tidx + size > buffer_len)
1175 size_t copy_n = tidx + size - buffer_len;
1176 memcpy(guard_buffer, (const unsigned char *)buffer, copy_n);
1179 *data = &buffer[tidx];
1180 return size;
1183 ssize_t bufcuttail(int handle_id, size_t size)
1185 struct memory_handle *h;
1186 size_t adjusted_size = size;
1188 h = find_handle(handle_id);
1190 if (!h)
1191 return ERR_HANDLE_NOT_FOUND;
1193 if (h->filerem)
1194 return ERR_HANDLE_NOT_DONE;
1196 if (h->available < adjusted_size)
1197 adjusted_size = h->available;
1199 h->available -= adjusted_size;
1200 h->filesize -= adjusted_size;
1201 h->widx = RINGBUF_SUB(h->widx, adjusted_size);
1202 if (h == cur_handle)
1203 buf_widx = h->widx;
1205 return adjusted_size;
1210 SECONDARY EXPORTED FUNCTIONS
1211 ============================
1213 buf_get_offset
1214 buf_handle_offset
1215 buf_request_buffer_handle
1216 buf_set_base_handle
1217 buf_used
1218 register_buffering_callback
1219 unregister_buffering_callback
1221 These functions are exported, to allow interaction with the buffer.
1222 They take care of the content of the structs, and rely on the linked list
1223 management functions for all the actual handle management work.
1226 /* Get a handle offset from a pointer */
1227 ssize_t buf_get_offset(int handle_id, void *ptr)
1229 const struct memory_handle *h = find_handle(handle_id);
1230 if (!h)
1231 return ERR_HANDLE_NOT_FOUND;
1233 return (size_t)ptr - (size_t)&buffer[h->ridx];
1236 ssize_t buf_handle_offset(int handle_id)
1238 const struct memory_handle *h = find_handle(handle_id);
1239 if (!h)
1240 return ERR_HANDLE_NOT_FOUND;
1241 return h->offset;
1244 void buf_request_buffer_handle(int handle_id)
1246 LOGFQUEUE("buffering >| Q_START_FILL %d",handle_id);
1247 queue_send(&buffering_queue, Q_START_FILL, handle_id);
1250 void buf_set_base_handle(int handle_id)
1252 LOGFQUEUE("buffering > Q_BASE_HANDLE %d", handle_id);
1253 queue_post(&buffering_queue, Q_BASE_HANDLE, handle_id);
1256 /* Return the amount of buffer space used */
1257 size_t buf_used(void)
1259 return BUF_USED;
1262 void buf_set_watermark(size_t bytes)
1264 LOGFQUEUE("buffering > Q_SET_WATERMARK %ld", (long)bytes);
1265 queue_post(&buffering_queue, Q_SET_WATERMARK, bytes);
1268 static void shrink_buffer_inner(struct memory_handle *h)
1270 if (h == NULL)
1271 return;
1273 shrink_buffer_inner(h->next);
1275 shrink_handle(h);
1278 static void shrink_buffer(void)
1280 logf("shrink_buffer()");
1281 shrink_buffer_inner(first_handle);
1284 void buffering_thread(void)
1286 bool filling = false;
1287 struct queue_event ev;
1289 while (true)
1291 if (!filling) {
1292 cancel_cpu_boost();
1295 queue_wait_w_tmo(&buffering_queue, &ev, filling ? 5 : HZ/2);
1297 switch (ev.id)
1299 case Q_START_FILL:
1300 LOGFQUEUE("buffering < Q_START_FILL");
1301 /* Call buffer callbacks here because this is one of two ways
1302 * to begin a full buffer fill */
1303 send_event(EVENT_BUFFER_LOW, 0);
1304 shrink_buffer();
1305 queue_reply(&buffering_queue, 1);
1306 filling |= buffer_handle((int)ev.data);
1307 break;
1309 case Q_BUFFER_HANDLE:
1310 LOGFQUEUE("buffering < Q_BUFFER_HANDLE");
1311 queue_reply(&buffering_queue, 1);
1312 buffer_handle((int)ev.data);
1313 break;
1315 case Q_RESET_HANDLE:
1316 LOGFQUEUE("buffering < Q_RESET_HANDLE");
1317 queue_reply(&buffering_queue, 1);
1318 reset_handle((int)ev.data);
1319 break;
1321 case Q_CLOSE_HANDLE:
1322 LOGFQUEUE("buffering < Q_CLOSE_HANDLE");
1323 queue_reply(&buffering_queue, close_handle((int)ev.data));
1324 break;
1326 case Q_HANDLE_ADDED:
1327 LOGFQUEUE("buffering < Q_HANDLE_ADDED %d", (int)ev.data);
1328 /* A handle was added: the disk is spinning, so we can fill */
1329 filling = true;
1330 break;
1332 case Q_BASE_HANDLE:
1333 LOGFQUEUE("buffering < Q_BASE_HANDLE");
1334 base_handle_id = (int)ev.data;
1335 break;
1337 case Q_SET_WATERMARK:
1338 LOGFQUEUE("buffering < Q_SET_WATERMARK");
1339 conf_watermark = (size_t)ev.data;
1340 if (conf_watermark < BUFFERING_DEFAULT_FILECHUNK)
1342 logf("wmark<chunk %ld<%d",
1343 (long)conf_watermark, BUFFERING_DEFAULT_FILECHUNK);
1344 conf_watermark = BUFFERING_DEFAULT_FILECHUNK;
1346 break;
1348 #ifndef SIMULATOR
1349 case SYS_USB_CONNECTED:
1350 LOGFQUEUE("buffering < SYS_USB_CONNECTED");
1351 usb_acknowledge(SYS_USB_CONNECTED_ACK);
1352 usb_wait_for_disconnect(&buffering_queue);
1353 break;
1354 #endif
1356 case SYS_TIMEOUT:
1357 LOGFQUEUE_SYS_TIMEOUT("buffering < SYS_TIMEOUT");
1358 break;
1361 update_data_counters();
1363 /* If the buffer is low, call the callbacks to get new data */
1364 if (num_handles > 0 && data_counters.useful <= conf_watermark)
1365 send_event(EVENT_BUFFER_LOW, 0);
1367 #if 0
1368 /* TODO: This needs to be fixed to use the idle callback, disable it
1369 * for simplicity until its done right */
1370 #if MEM > 8
1371 /* If the disk is spinning, take advantage by filling the buffer */
1372 else if (ata_disk_is_active() && queue_empty(&buffering_queue))
1374 if (num_handles > 0 && data_counters.useful <= high_watermark)
1375 send_event(EVENT_BUFFER_LOW, 0);
1377 if (data_counters.remaining > 0 && BUF_USED <= high_watermark)
1379 /* This is a new fill, shrink the buffer up first */
1380 if (!filling)
1381 shrink_buffer();
1382 filling = fill_buffer();
1383 update_data_counters();
1386 #endif
1387 #endif
1389 if (queue_empty(&buffering_queue)) {
1390 if (filling) {
1391 if (data_counters.remaining > 0 && BUF_USED < buffer_len)
1392 filling = fill_buffer();
1394 else if (ev.id == SYS_TIMEOUT)
1396 if (data_counters.remaining > 0 &&
1397 data_counters.useful <= conf_watermark) {
1398 shrink_buffer();
1399 filling = fill_buffer();
1406 void buffering_init(void)
1408 mutex_init(&llist_mutex);
1409 #ifdef HAVE_PRIORITY_SCHEDULING
1410 /* This behavior not safe atm */
1411 mutex_set_preempt(&llist_mutex, false);
1412 #endif
1414 conf_watermark = BUFFERING_DEFAULT_WATERMARK;
1416 queue_init(&buffering_queue, true);
1417 buffering_thread_p = create_thread( buffering_thread, buffering_stack,
1418 sizeof(buffering_stack), CREATE_THREAD_FROZEN,
1419 buffering_thread_name IF_PRIO(, PRIORITY_BUFFERING)
1420 IF_COP(, CPU));
1422 queue_enable_queue_send(&buffering_queue, &buffering_queue_sender_list,
1423 buffering_thread_p);
1426 /* Initialise the buffering subsystem */
1427 bool buffering_reset(char *buf, size_t buflen)
1429 if (!buf || !buflen)
1430 return false;
1432 buffer = buf;
1433 buffer_len = buflen;
1434 guard_buffer = buf + buflen;
1436 buf_widx = 0;
1437 buf_ridx = 0;
1439 first_handle = NULL;
1440 cur_handle = NULL;
1441 cached_handle = NULL;
1442 num_handles = 0;
1443 base_handle_id = -1;
1445 /* Set the high watermark as 75% full...or 25% empty :) */
1446 #if MEM > 8
1447 high_watermark = 3*buflen / 4;
1448 #endif
1450 thread_thaw(buffering_thread_p);
1452 return true;
1455 void buffering_get_debugdata(struct buffering_debug *dbgdata)
1457 update_data_counters();
1458 dbgdata->num_handles = num_handles;
1459 dbgdata->data_rem = data_counters.remaining;
1460 dbgdata->wasted_space = data_counters.wasted;
1461 dbgdata->buffered_data = data_counters.buffered;
1462 dbgdata->useful_data = data_counters.useful;