Search for, and load, JPEG album art files.
[kugel-rb.git] / apps / buffering.c
blob66bd22f12dad7a9ea728704773373a42b1864680
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 "storage.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 "screens.h"
48 #include "playlist.h"
49 #include "pcmbuf.h"
50 #include "buffer.h"
51 #include "bmp.h"
52 #include "appevents.h"
53 #include "metadata.h"
54 #ifdef HAVE_ALBUMART
55 #include "albumart.h"
56 #include "jpeg_load.h"
57 #endif
59 #define GUARD_BUFSIZE (32*1024)
61 /* Define LOGF_ENABLE to enable logf output in this file */
62 /*#define LOGF_ENABLE*/
63 #include "logf.h"
65 /* macros to enable logf for queues
66 logging on SYS_TIMEOUT can be disabled */
67 #ifdef SIMULATOR
68 /* Define this for logf output of all queuing except SYS_TIMEOUT */
69 #define BUFFERING_LOGQUEUES
70 /* Define this to logf SYS_TIMEOUT messages */
71 /* #define BUFFERING_LOGQUEUES_SYS_TIMEOUT */
72 #endif
74 #ifdef BUFFERING_LOGQUEUES
75 #define LOGFQUEUE logf
76 #else
77 #define LOGFQUEUE(...)
78 #endif
80 #ifdef BUFFERING_LOGQUEUES_SYS_TIMEOUT
81 #define LOGFQUEUE_SYS_TIMEOUT logf
82 #else
83 #define LOGFQUEUE_SYS_TIMEOUT(...)
84 #endif
86 /* default point to start buffer refill */
87 #define BUFFERING_DEFAULT_WATERMARK (1024*128)
88 /* amount of data to read in one read() call */
89 #define BUFFERING_DEFAULT_FILECHUNK (1024*32)
91 #define BUF_HANDLE_MASK 0x7FFFFFFF
94 /* Ring buffer helper macros */
95 /* Buffer pointer (p) plus value (v), wrapped if necessary */
96 #define RINGBUF_ADD(p,v) (((p)+(v))<buffer_len ? (p)+(v) : (p)+(v)-buffer_len)
97 /* Buffer pointer (p) minus value (v), wrapped if necessary */
98 #define RINGBUF_SUB(p,v) ((p>=v) ? (p)-(v) : (p)+buffer_len-(v))
99 /* How far value (v) plus buffer pointer (p1) will cross buffer pointer (p2) */
100 #define RINGBUF_ADD_CROSS(p1,v,p2) \
101 ((p1<p2) ? (int)((p1)+(v))-(int)(p2) : (int)((p1)+(v)-(p2))-(int)buffer_len)
102 /* Bytes available in the buffer */
103 #define BUF_USED RINGBUF_SUB(buf_widx, buf_ridx)
105 /* assert(sizeof(struct memory_handle)%4==0) */
106 struct memory_handle {
107 int id; /* A unique ID for the handle */
108 enum data_type type; /* Type of data buffered with this handle */
109 char path[MAX_PATH]; /* Path if data originated in a file */
110 int fd; /* File descriptor to path (-1 if closed) */
111 size_t data; /* Start index of the handle's data buffer */
112 volatile size_t ridx; /* Read pointer, relative to the main buffer */
113 size_t widx; /* Write pointer */
114 size_t filesize; /* File total length */
115 size_t filerem; /* Remaining bytes of file NOT in buffer */
116 volatile size_t available; /* Available bytes to read from buffer */
117 size_t offset; /* Offset at which we started reading the file */
118 struct memory_handle *next;
120 /* invariant: filesize == offset + available + filerem */
122 static char *buffer;
123 static char *guard_buffer;
125 static size_t buffer_len;
127 static volatile size_t buf_widx; /* current writing position */
128 static volatile size_t buf_ridx; /* current reading position */
129 /* buf_*idx are values relative to the buffer, not real pointers. */
131 /* Configuration */
132 static size_t conf_watermark = 0; /* Level to trigger filebuf fill */
133 #if MEM > 8
134 static size_t high_watermark = 0; /* High watermark for rebuffer */
135 #endif
137 /* current memory handle in the linked list. NULL when the list is empty. */
138 static struct memory_handle *cur_handle;
139 /* first memory handle in the linked list. NULL when the list is empty. */
140 static struct memory_handle *first_handle;
142 static int num_handles; /* number of handles in the list */
144 static int base_handle_id;
146 static struct mutex llist_mutex;
148 /* Handle cache (makes find_handle faster).
149 This is global so that move_handle and rm_handle can invalidate it. */
150 static struct memory_handle *cached_handle = NULL;
152 static struct {
153 size_t remaining; /* Amount of data needing to be buffered */
154 size_t wasted; /* Amount of space available for freeing */
155 size_t buffered; /* Amount of data currently in the buffer */
156 size_t useful; /* Amount of data still useful to the user */
157 } data_counters;
160 /* Messages available to communicate with the buffering thread */
161 enum {
162 Q_BUFFER_HANDLE = 1, /* Request buffering of a handle, this should not be
163 used in a low buffer situation. */
164 Q_RESET_HANDLE, /* (internal) Request resetting of a handle to its
165 offset (the offset has to be set beforehand) */
166 Q_CLOSE_HANDLE, /* Request closing a handle */
167 Q_BASE_HANDLE, /* Set the reference handle for buf_useful_data */
169 /* Configuration: */
170 Q_START_FILL, /* Request that the buffering thread initiate a buffer
171 fill at its earliest convenience */
172 Q_HANDLE_ADDED, /* Inform the buffering thread that a handle was added,
173 (which means the disk is spinning) */
176 /* Buffering thread */
177 static void buffering_thread(void);
178 static long buffering_stack[(DEFAULT_STACK_SIZE + 0x2000)/sizeof(long)];
179 static const char buffering_thread_name[] = "buffering";
180 static unsigned int buffering_thread_id = 0;
181 static struct event_queue buffering_queue;
182 static struct queue_sender_list buffering_queue_sender_list;
187 LINKED LIST MANAGEMENT
188 ======================
190 add_handle : Add a handle to the list
191 rm_handle : Remove a handle from the list
192 find_handle : Get a handle pointer from an ID
193 move_handle : Move a handle in the buffer (with or without its data)
195 These functions only handle the linked list structure. They don't touch the
196 contents of the struct memory_handle headers. They also change the buf_*idx
197 pointers when necessary and manage the handle IDs.
199 The first and current (== last) handle are kept track of.
200 A new handle is added at buf_widx and becomes the current one.
201 buf_widx always points to the current writing position for the current handle
202 buf_ridx always points to the location of the first handle.
203 buf_ridx == buf_widx means the buffer is empty.
207 /* Add a new handle to the linked list and return it. It will have become the
208 new current handle.
209 data_size must contain the size of what will be in the handle.
210 can_wrap tells us whether this type of data may wrap on buffer
211 alloc_all tells us if we must immediately be able to allocate data_size
212 returns a valid memory handle if all conditions for allocation are met.
213 NULL if there memory_handle itself cannot be allocated or if the
214 data_size cannot be allocated and alloc_all is set. This function's
215 only potential side effect is to allocate space for the cur_handle
216 if it returns NULL.
218 static struct memory_handle *add_handle(size_t data_size, bool can_wrap,
219 bool alloc_all)
221 /* gives each handle a unique id */
222 static int cur_handle_id = 0;
223 size_t shift;
224 size_t new_widx;
225 size_t len;
226 int overlap;
228 if (num_handles >= BUF_MAX_HANDLES)
229 return NULL;
231 mutex_lock(&llist_mutex);
233 if (cur_handle && cur_handle->filerem > 0) {
234 /* the current handle hasn't finished buffering. We can only add
235 a new one if there is already enough free space to finish
236 the buffering. */
237 size_t req = cur_handle->filerem + sizeof(struct memory_handle);
238 if (RINGBUF_ADD_CROSS(cur_handle->widx, req, buf_ridx) >= 0) {
239 /* Not enough space */
240 mutex_unlock(&llist_mutex);
241 return NULL;
242 } else {
243 /* Allocate the remainder of the space for the current handle */
244 buf_widx = RINGBUF_ADD(cur_handle->widx, cur_handle->filerem);
248 /* align to 4 bytes up */
249 new_widx = RINGBUF_ADD(buf_widx, 3) & ~3;
251 len = data_size + sizeof(struct memory_handle);
253 /* First, will the handle wrap? */
254 /* If the handle would wrap, move to the beginning of the buffer,
255 * or if the data must not but would wrap, move it to the beginning */
256 if( (new_widx + sizeof(struct memory_handle) > buffer_len) ||
257 (!can_wrap && (new_widx + len > buffer_len)) ) {
258 new_widx = 0;
261 /* How far we shifted buf_widx to align things, must be < buffer_len */
262 shift = RINGBUF_SUB(new_widx, buf_widx);
264 /* How much space are we short in the actual ring buffer? */
265 overlap = RINGBUF_ADD_CROSS(buf_widx, shift + len, buf_ridx);
266 if (overlap >= 0 && (alloc_all || (unsigned)overlap > data_size)) {
267 /* Not enough space for required allocations */
268 mutex_unlock(&llist_mutex);
269 return NULL;
272 /* There is enough space for the required data, advance the buf_widx and
273 * initialize the struct */
274 buf_widx = new_widx;
276 struct memory_handle *new_handle =
277 (struct memory_handle *)(&buffer[buf_widx]);
279 /* only advance the buffer write index of the size of the struct */
280 buf_widx = RINGBUF_ADD(buf_widx, sizeof(struct memory_handle));
282 new_handle->id = cur_handle_id;
283 /* Wrap signed int is safe and 0 doesn't happen */
284 cur_handle_id = (cur_handle_id + 1) & BUF_HANDLE_MASK;
285 new_handle->next = NULL;
286 num_handles++;
288 if (!first_handle)
289 /* the new handle is the first one */
290 first_handle = new_handle;
292 if (cur_handle)
293 cur_handle->next = new_handle;
295 cur_handle = new_handle;
297 mutex_unlock(&llist_mutex);
298 return new_handle;
301 /* Delete a given memory handle from the linked list
302 and return true for success. Nothing is actually erased from memory. */
303 static bool rm_handle(const struct memory_handle *h)
305 if (h == NULL)
306 return true;
308 mutex_lock(&llist_mutex);
310 if (h == first_handle) {
311 first_handle = h->next;
312 if (h == cur_handle) {
313 /* h was the first and last handle: the buffer is now empty */
314 cur_handle = NULL;
315 buf_ridx = buf_widx = 0;
316 } else {
317 /* update buf_ridx to point to the new first handle */
318 buf_ridx = (void *)first_handle - (void *)buffer;
320 } else {
321 struct memory_handle *m = first_handle;
322 /* Find the previous handle */
323 while (m && m->next != h) {
324 m = m->next;
326 if (m && m->next == h) {
327 m->next = h->next;
328 if (h == cur_handle) {
329 cur_handle = m;
330 buf_widx = cur_handle->widx;
332 } else {
333 mutex_unlock(&llist_mutex);
334 return false;
338 /* Invalidate the cache to prevent it from keeping the old location of h */
339 if (h == cached_handle)
340 cached_handle = NULL;
342 num_handles--;
344 mutex_unlock(&llist_mutex);
345 return true;
348 /* Return a pointer to the memory handle of given ID.
349 NULL if the handle wasn't found */
350 static struct memory_handle *find_handle(int handle_id)
352 if (handle_id < 0)
353 return NULL;
355 mutex_lock(&llist_mutex);
357 /* simple caching because most of the time the requested handle
358 will either be the same as the last, or the one after the last */
359 if (cached_handle)
361 if (cached_handle->id == handle_id) {
362 mutex_unlock(&llist_mutex);
363 return cached_handle;
364 } else if (cached_handle->next &&
365 (cached_handle->next->id == handle_id)) {
366 cached_handle = cached_handle->next;
367 mutex_unlock(&llist_mutex);
368 return cached_handle;
372 struct memory_handle *m = first_handle;
373 while (m && m->id != handle_id) {
374 m = m->next;
376 /* This condition can only be reached with !m or m->id == handle_id */
377 if (m)
378 cached_handle = m;
380 mutex_unlock(&llist_mutex);
381 return m;
384 /* Move a memory handle and data_size of its data delta bytes along the buffer.
385 delta maximum bytes available to move the handle. If the move is performed
386 it is set to the actual distance moved.
387 data_size is the amount of data to move along with the struct.
388 returns a valid memory_handle if the move is successful
389 NULL if the handle is NULL, the move would be less than the size of
390 a memory_handle after correcting for wraps or if the handle is not
391 found in the linked list for adjustment. This function has no side
392 effects if NULL is returned. */
393 static bool move_handle(struct memory_handle **h, size_t *delta,
394 size_t data_size, bool can_wrap)
396 struct memory_handle *dest;
397 const struct memory_handle *src;
398 size_t newpos;
399 size_t size_to_move;
400 size_t final_delta = *delta;
401 int overlap;
403 if (h == NULL || (src = *h) == NULL)
404 return false;
406 size_to_move = sizeof(struct memory_handle) + data_size;
408 /* Align to four bytes, down */
409 final_delta &= ~3;
410 if (final_delta < sizeof(struct memory_handle)) {
411 /* It's not legal to move less than the size of the struct */
412 return false;
415 mutex_lock(&llist_mutex);
417 newpos = RINGBUF_ADD((void *)src - (void *)buffer, final_delta);
418 overlap = RINGBUF_ADD_CROSS(newpos, size_to_move, buffer_len - 1);
420 if (overlap > 0) {
421 /* Some part of the struct + data would wrap, maybe ok */
422 size_t correction = 0;
423 /* If the overlap lands inside the memory_handle */
424 if ((unsigned)overlap > data_size) {
425 /* Correct the position and real delta to prevent the struct from
426 * wrapping, this guarantees an aligned delta, I think */
427 correction = overlap - data_size;
428 } else if (!can_wrap) {
429 /* Otherwise the overlap falls in the data area and must all be
430 * backed out. This may become conditional if ever we move
431 * data that is allowed to wrap (ie audio) */
432 correction = overlap;
433 /* Align correction to four bytes, up */
434 correction = (correction+3) & ~3;
436 if (correction) {
437 if (final_delta < correction + sizeof(struct memory_handle)) {
438 /* Delta cannot end up less than the size of the struct */
439 mutex_unlock(&llist_mutex);
440 return false;
443 newpos -= correction;
444 overlap -= correction;/* Used below to know how to split the data */
445 final_delta -= correction;
449 dest = (struct memory_handle *)(&buffer[newpos]);
451 if (src == first_handle) {
452 first_handle = dest;
453 buf_ridx = newpos;
454 } else {
455 struct memory_handle *m = first_handle;
456 while (m && m->next != src) {
457 m = m->next;
459 if (m && m->next == src) {
460 m->next = dest;
461 } else {
462 mutex_unlock(&llist_mutex);
463 return false;
468 /* Update the cache to prevent it from keeping the old location of h */
469 if (src == cached_handle)
470 cached_handle = dest;
472 /* the cur_handle pointer might need updating */
473 if (src == cur_handle)
474 cur_handle = dest;
476 if (overlap > 0) {
477 size_t first_part = size_to_move - overlap;
478 memmove(dest, src, first_part);
479 memmove(buffer, (const char *)src + first_part, overlap);
480 } else {
481 memmove(dest, src, size_to_move);
484 /* Update the caller with the new location of h and the distance moved */
485 *h = dest;
486 *delta = final_delta;
487 mutex_unlock(&llist_mutex);
488 return dest;
493 BUFFER SPACE MANAGEMENT
494 =======================
496 update_data_counters: Updates the values in data_counters
497 buffer_is_low : Returns true if the amount of useful data in the buffer is low
498 buffer_handle : Buffer data for a handle
499 reset_handle : Reset write position and data buffer of a handle to its offset
500 rebuffer_handle : Seek to a nonbuffered part of a handle by rebuffering the data
501 shrink_handle : Free buffer space by moving a handle
502 fill_buffer : Call buffer_handle for all handles that have data to buffer
504 These functions are used by the buffering thread to manage buffer space.
507 static void update_data_counters(void)
509 struct memory_handle *m = find_handle(base_handle_id);
510 bool is_useful = m==NULL;
512 size_t buffered = 0;
513 size_t wasted = 0;
514 size_t remaining = 0;
515 size_t useful = 0;
517 mutex_lock(&llist_mutex);
519 m = first_handle;
520 while (m) {
521 buffered += m->available;
522 wasted += RINGBUF_SUB(m->ridx, m->data);
523 remaining += m->filerem;
525 if (m->id == base_handle_id)
526 is_useful = true;
528 if (is_useful)
529 useful += RINGBUF_SUB(m->widx, m->ridx);
531 m = m->next;
534 mutex_unlock(&llist_mutex);
536 data_counters.buffered = buffered;
537 data_counters.wasted = wasted;
538 data_counters.remaining = remaining;
539 data_counters.useful = useful;
542 static inline bool buffer_is_low(void)
544 update_data_counters();
545 return data_counters.useful < (conf_watermark / 2);
548 /* Buffer data for the given handle.
549 Return whether or not the buffering should continue explicitly. */
550 static bool buffer_handle(int handle_id)
552 logf("buffer_handle(%d)", handle_id);
553 struct memory_handle *h = find_handle(handle_id);
554 if (!h)
555 return true;
557 if (h->filerem == 0) {
558 /* nothing left to buffer */
559 return true;
562 if (h->fd < 0) /* file closed, reopen */
564 if (*h->path)
565 h->fd = open(h->path, O_RDONLY);
567 if (h->fd < 0)
569 /* could not open the file, truncate it where it is */
570 h->filesize -= h->filerem;
571 h->filerem = 0;
572 return true;
575 if (h->offset)
576 lseek(h->fd, h->offset, SEEK_SET);
579 trigger_cpu_boost();
581 if (h->type == TYPE_ID3)
583 if (!get_metadata((struct mp3entry *)(buffer + h->data), h->fd, h->path))
585 /* metadata parsing failed: clear the buffer. */
586 memset(buffer + h->data, 0, sizeof(struct mp3entry));
588 close(h->fd);
589 h->fd = -1;
590 h->filerem = 0;
591 h->available = sizeof(struct mp3entry);
592 h->widx += sizeof(struct mp3entry);
593 send_event(BUFFER_EVENT_FINISHED, &h->id);
594 return true;
597 while (h->filerem > 0)
599 /* max amount to copy */
600 size_t copy_n = MIN( MIN(h->filerem, BUFFERING_DEFAULT_FILECHUNK),
601 buffer_len - h->widx);
603 /* stop copying if it would overwrite the reading position */
604 if (RINGBUF_ADD_CROSS(h->widx, copy_n, buf_ridx) >= 0)
605 return false;
607 /* This would read into the next handle, this is broken */
608 if (h->next && RINGBUF_ADD_CROSS(h->widx, copy_n,
609 (unsigned)((void *)h->next - (void *)buffer)) > 0) {
610 /* Try to recover by truncating this file */
611 copy_n = RINGBUF_ADD_CROSS(h->widx, copy_n,
612 (unsigned)((void *)h->next - (void *)buffer));
613 h->filerem -= copy_n;
614 h->filesize -= copy_n;
615 logf("buf alloc short %ld", (long)copy_n);
616 if (h->filerem)
617 continue;
618 else
619 break;
622 /* rc is the actual amount read */
623 int rc = read(h->fd, &buffer[h->widx], copy_n);
625 if (rc < 0)
627 /* Some kind of filesystem error, maybe recoverable if not codec */
628 if (h->type == TYPE_CODEC) {
629 logf("Partial codec");
630 break;
633 DEBUGF("File ended %ld bytes early\n", (long)h->filerem);
634 h->filesize -= h->filerem;
635 h->filerem = 0;
636 break;
639 /* Advance buffer */
640 h->widx = RINGBUF_ADD(h->widx, rc);
641 if (h == cur_handle)
642 buf_widx = h->widx;
643 h->available += rc;
644 h->filerem -= rc;
646 /* If this is a large file, see if we need to break or give the codec
647 * more time */
648 if (h->type == TYPE_PACKET_AUDIO &&
649 pcmbuf_is_lowdata() && !buffer_is_low())
651 sleep(1);
653 else
655 yield();
658 if (!queue_empty(&buffering_queue))
659 break;
662 if (h->filerem == 0) {
663 /* finished buffering the file */
664 close(h->fd);
665 h->fd = -1;
666 send_event(BUFFER_EVENT_FINISHED, &h->id);
669 return true;
672 /* Reset writing position and data buffer of a handle to its current offset.
673 Use this after having set the new offset to use. */
674 static void reset_handle(int handle_id)
676 logf("reset_handle(%d)", handle_id);
678 struct memory_handle *h = find_handle(handle_id);
679 if (!h)
680 return;
682 h->ridx = h->widx = h->data;
683 if (h == cur_handle)
684 buf_widx = h->widx;
685 h->available = 0;
686 h->filerem = h->filesize - h->offset;
688 if (h->fd >= 0) {
689 lseek(h->fd, h->offset, SEEK_SET);
693 /* Seek to a nonbuffered part of a handle by rebuffering the data. */
694 static void rebuffer_handle(int handle_id, size_t newpos)
696 struct memory_handle *h = find_handle(handle_id);
697 if (!h)
698 return;
700 /* When seeking foward off of the buffer, if it is a short seek don't
701 rebuffer the whole track, just read enough to satisfy */
702 if (newpos > h->offset && newpos - h->offset < BUFFERING_DEFAULT_FILECHUNK)
704 LOGFQUEUE("buffering >| Q_BUFFER_HANDLE %d", handle_id);
705 queue_send(&buffering_queue, Q_BUFFER_HANDLE, handle_id);
706 h->ridx = h->data + newpos;
707 return;
710 h->offset = newpos;
712 /* Reset the handle to its new offset */
713 LOGFQUEUE("buffering >| Q_RESET_HANDLE %d", handle_id);
714 queue_send(&buffering_queue, Q_RESET_HANDLE, handle_id);
716 size_t next = (unsigned)((void *)h->next - (void *)buffer);
717 if (RINGBUF_SUB(next, h->data) < h->filesize - newpos)
719 /* There isn't enough space to rebuffer all of the track from its new
720 offset, so we ask the user to free some */
721 DEBUGF("rebuffer_handle: space is needed\n");
722 send_event(BUFFER_EVENT_REBUFFER, &handle_id);
725 /* Now we ask for a rebuffer */
726 LOGFQUEUE("buffering >| Q_BUFFER_HANDLE %d", handle_id);
727 queue_send(&buffering_queue, Q_BUFFER_HANDLE, handle_id);
730 static bool close_handle(int handle_id)
732 struct memory_handle *h = find_handle(handle_id);
734 /* If the handle is not found, it is closed */
735 if (!h)
736 return true;
738 if (h->fd >= 0) {
739 close(h->fd);
740 h->fd = -1;
743 /* rm_handle returns true unless the handle somehow persists after exit */
744 return rm_handle(h);
747 /* Free buffer space by moving the handle struct right before the useful
748 part of its data buffer or by moving all the data. */
749 static void shrink_handle(struct memory_handle *h)
751 size_t delta;
753 if (!h)
754 return;
756 if (h->next && h->filerem == 0 &&
757 (h->type == TYPE_ID3 || h->type == TYPE_CUESHEET ||
758 h->type == TYPE_BITMAP || h->type == TYPE_CODEC ||
759 h->type == TYPE_ATOMIC_AUDIO))
761 /* metadata handle: we can move all of it */
762 size_t handle_distance =
763 RINGBUF_SUB((unsigned)((void *)h->next - (void*)buffer), h->data);
764 delta = handle_distance - h->available;
766 /* The value of delta might change for alignment reasons */
767 if (!move_handle(&h, &delta, h->available, h->type==TYPE_CODEC))
768 return;
770 size_t olddata = h->data;
771 h->data = RINGBUF_ADD(h->data, delta);
772 h->ridx = RINGBUF_ADD(h->ridx, delta);
773 h->widx = RINGBUF_ADD(h->widx, delta);
775 if (h->type == TYPE_ID3 && h->filesize == sizeof(struct mp3entry)) {
776 /* when moving an mp3entry we need to readjust its pointers. */
777 adjust_mp3entry((struct mp3entry *)&buffer[h->data],
778 (void *)&buffer[h->data],
779 (const void *)&buffer[olddata]);
780 } else if (h->type == TYPE_BITMAP) {
781 /* adjust the bitmap's pointer */
782 struct bitmap *bmp = (struct bitmap *)&buffer[h->data];
783 bmp->data = &buffer[h->data + sizeof(struct bitmap)];
786 else
788 /* only move the handle struct */
789 delta = RINGBUF_SUB(h->ridx, h->data);
790 if (!move_handle(&h, &delta, 0, true))
791 return;
793 h->data = RINGBUF_ADD(h->data, delta);
794 h->available -= delta;
795 h->offset += delta;
799 /* Fill the buffer by buffering as much data as possible for handles that still
800 have data left to buffer
801 Return whether or not to continue filling after this */
802 static bool fill_buffer(void)
804 logf("fill_buffer()");
805 struct memory_handle *m;
806 shrink_handle(first_handle);
807 m = first_handle;
808 while (queue_empty(&buffering_queue) && m) {
809 if (m->filerem > 0) {
810 if (!buffer_handle(m->id)) {
811 m = NULL;
812 break;
815 m = m->next;
818 if (m) {
819 return true;
821 else
823 /* only spin the disk down if the filling wasn't interrupted by an
824 event arriving in the queue. */
825 storage_sleep();
826 return false;
830 #ifdef HAVE_ALBUMART
831 /* Given a file descriptor to a bitmap file, write the bitmap data to the
832 buffer, with a struct bitmap and the actual data immediately following.
833 Return value is the total size (struct + data). */
834 static int load_image(int fd, const char *path)
836 int rc;
837 int pathlen = strlen(path);
838 struct bitmap *bmp = (struct bitmap *)&buffer[buf_widx];
839 /* FIXME: alignment may be needed for the data buffer. */
840 bmp->data = &buffer[buf_widx + sizeof(struct bitmap)];
842 #if (LCD_DEPTH > 1) || defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1)
843 bmp->maskdata = NULL;
844 #endif
846 int free = (int)MIN(buffer_len - BUF_USED, buffer_len - buf_widx)
847 - sizeof(struct bitmap);
849 get_albumart_size(bmp);
851 if (strcmp(path + pathlen - 4, ".bmp"))
852 rc = read_jpeg_fd(fd, bmp, free, FORMAT_NATIVE|FORMAT_DITHER|
853 FORMAT_RESIZE|FORMAT_KEEP_ASPECT, NULL);
854 else
855 rc = read_bmp_fd(fd, bmp, free, FORMAT_NATIVE|FORMAT_DITHER|
856 FORMAT_RESIZE|FORMAT_KEEP_ASPECT, NULL);
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 */
911 /* Inform the buffering thread that we added a handle */
912 LOGFQUEUE("buffering > Q_HANDLE_ADDED %d", h->id);
913 queue_post(&buffering_queue, Q_HANDLE_ADDED, h->id);
915 return h->id;
918 /* Other cases: there is a little more work. */
920 int fd = open(file, O_RDONLY);
921 if (fd < 0)
922 return ERR_FILE_ERROR;
924 size_t size = filesize(fd);
925 bool can_wrap = type==TYPE_PACKET_AUDIO || type==TYPE_CODEC;
927 size_t adjusted_offset = offset;
928 if (adjusted_offset > size)
929 adjusted_offset = 0;
931 struct memory_handle *h = add_handle(size-adjusted_offset, can_wrap, false);
932 if (!h)
934 DEBUGF("bufopen: failed to add handle\n");
935 close(fd);
936 return ERR_BUFFER_FULL;
939 strncpy(h->path, file, MAX_PATH);
940 h->offset = adjusted_offset;
941 h->ridx = buf_widx;
942 h->data = buf_widx;
943 h->type = type;
945 #ifdef HAVE_ALBUMART
946 if (type == TYPE_BITMAP)
948 /* Bitmap file: we load the data instead of the file */
949 int rc;
950 mutex_lock(&llist_mutex); /* Lock because load_bitmap yields */
951 rc = load_image(fd, file);
952 mutex_unlock(&llist_mutex);
953 if (rc <= 0)
955 rm_handle(h);
956 close(fd);
957 return ERR_FILE_ERROR;
959 h->filerem = 0;
960 h->filesize = rc;
961 h->available = rc;
962 h->widx = buf_widx + rc; /* safe because the data doesn't wrap */
963 buf_widx += rc; /* safe too */
965 else
966 #endif
968 h->filerem = size - adjusted_offset;
969 h->filesize = size;
970 h->available = 0;
971 h->widx = buf_widx;
974 if (type == TYPE_CUESHEET) {
975 h->fd = fd;
976 /* Immediately start buffering those */
977 LOGFQUEUE("buffering >| Q_BUFFER_HANDLE %d", h->id);
978 queue_send(&buffering_queue, Q_BUFFER_HANDLE, h->id);
979 } else {
980 /* Other types will get buffered in the course of normal operations */
981 h->fd = -1;
982 close(fd);
984 /* Inform the buffering thread that we added a handle */
985 LOGFQUEUE("buffering > Q_HANDLE_ADDED %d", h->id);
986 queue_post(&buffering_queue, Q_HANDLE_ADDED, h->id);
989 logf("bufopen: new hdl %d", h->id);
990 return h->id;
993 /* Open a new handle from data that needs to be copied from memory.
994 src is the source buffer from which to copy data. It can be NULL to simply
995 reserve buffer space.
996 size is the requested size. The call will only be successful if the
997 requested amount of data can entirely fit in the buffer without wrapping.
998 Return value is the handle id for success or <0 for failure.
1000 int bufalloc(const void *src, size_t size, enum data_type type)
1002 struct memory_handle *h = add_handle(size, false, true);
1004 if (!h)
1005 return ERR_BUFFER_FULL;
1007 if (src) {
1008 if (type == TYPE_ID3 && size == sizeof(struct mp3entry)) {
1009 /* specially take care of struct mp3entry */
1010 copy_mp3entry((struct mp3entry *)&buffer[buf_widx],
1011 (const struct mp3entry *)src);
1012 } else {
1013 memcpy(&buffer[buf_widx], src, size);
1017 h->fd = -1;
1018 *h->path = 0;
1019 h->filesize = size;
1020 h->filerem = 0;
1021 h->offset = 0;
1022 h->ridx = buf_widx;
1023 h->widx = buf_widx + size; /* this is safe because the data doesn't wrap */
1024 h->data = buf_widx;
1025 h->available = size;
1026 h->type = type;
1028 buf_widx += size; /* safe too */
1030 logf("bufalloc: new hdl %d", h->id);
1031 return h->id;
1034 /* Close the handle. Return true for success and false for failure */
1035 bool bufclose(int handle_id)
1037 logf("bufclose(%d)", handle_id);
1039 LOGFQUEUE("buffering >| Q_CLOSE_HANDLE %d", handle_id);
1040 return queue_send(&buffering_queue, Q_CLOSE_HANDLE, handle_id);
1043 /* Set reading index in handle (relatively to the start of the file).
1044 Access before the available data will trigger a rebuffer.
1045 Return 0 for success and < 0 for failure:
1046 -1 if the handle wasn't found
1047 -2 if the new requested position was beyond the end of the file
1049 int bufseek(int handle_id, size_t newpos)
1051 struct memory_handle *h = find_handle(handle_id);
1052 if (!h)
1053 return ERR_HANDLE_NOT_FOUND;
1055 if (newpos > h->filesize) {
1056 /* access beyond the end of the file */
1057 return ERR_INVALID_VALUE;
1059 else if (newpos < h->offset || h->offset + h->available < newpos) {
1060 /* access before or after buffered data. A rebuffer is needed. */
1061 rebuffer_handle(handle_id, newpos);
1063 else {
1064 h->ridx = RINGBUF_ADD(h->data, newpos - h->offset);
1066 return 0;
1069 /* Advance the reading index in a handle (relatively to its current position).
1070 Return 0 for success and < 0 for failure */
1071 int bufadvance(int handle_id, off_t offset)
1073 const struct memory_handle *h = find_handle(handle_id);
1074 if (!h)
1075 return ERR_HANDLE_NOT_FOUND;
1077 size_t newpos = h->offset + RINGBUF_SUB(h->ridx, h->data) + offset;
1078 return bufseek(handle_id, newpos);
1081 /* Used by bufread and bufgetdata to prepare the buffer and retrieve the
1082 * actual amount of data available for reading. This function explicitly
1083 * does not check the validity of the input handle. It does do range checks
1084 * on size and returns a valid (and explicit) amount of data for reading */
1085 static struct memory_handle *prep_bufdata(int handle_id, size_t *size,
1086 bool guardbuf_limit)
1088 struct memory_handle *h = find_handle(handle_id);
1089 if (!h)
1090 return NULL;
1092 size_t avail = RINGBUF_SUB(h->widx, h->ridx);
1094 if (avail == 0 && h->filerem == 0)
1096 /* File is finished reading */
1097 *size = 0;
1098 return h;
1101 if (*size == 0 || *size > avail + h->filerem)
1102 *size = avail + h->filerem;
1104 if (guardbuf_limit && h->type == TYPE_PACKET_AUDIO && *size > GUARD_BUFSIZE)
1106 logf("data request > guardbuf");
1107 /* If more than the size of the guardbuf is requested and this is a
1108 * bufgetdata, limit to guard_bufsize over the end of the buffer */
1109 *size = MIN(*size, buffer_len - h->ridx + GUARD_BUFSIZE);
1110 /* this ensures *size <= buffer_len - h->ridx + GUARD_BUFSIZE */
1113 if (h->filerem > 0 && avail < *size)
1115 /* Data isn't ready. Request buffering */
1116 buf_request_buffer_handle(handle_id);
1117 /* Wait for the data to be ready */
1120 sleep(1);
1121 /* it is not safe for a non-buffering thread to sleep while
1122 * holding a handle */
1123 h = find_handle(handle_id);
1124 if (!h)
1125 return NULL;
1126 avail = RINGBUF_SUB(h->widx, h->ridx);
1128 while (h->filerem > 0 && avail < *size);
1131 *size = MIN(*size,avail);
1132 return h;
1135 /* Copy data from the given handle to the dest buffer.
1136 Return the number of bytes copied or < 0 for failure (handle not found).
1137 The caller is blocked until the requested amount of data is available.
1139 ssize_t bufread(int handle_id, size_t size, void *dest)
1141 const struct memory_handle *h;
1142 size_t adjusted_size = size;
1144 h = prep_bufdata(handle_id, &adjusted_size, false);
1145 if (!h)
1146 return ERR_HANDLE_NOT_FOUND;
1148 if (h->ridx + adjusted_size > buffer_len)
1150 /* the data wraps around the end of the buffer */
1151 size_t read = buffer_len - h->ridx;
1152 memcpy(dest, &buffer[h->ridx], read);
1153 memcpy(dest+read, buffer, adjusted_size - read);
1155 else
1157 memcpy(dest, &buffer[h->ridx], adjusted_size);
1160 return adjusted_size;
1163 /* Update the "data" pointer to make the handle's data available to the caller.
1164 Return the length of the available linear data or < 0 for failure (handle
1165 not found).
1166 The caller is blocked until the requested amount of data is available.
1167 size is the amount of linear data requested. it can be 0 to get as
1168 much as possible.
1169 The guard buffer may be used to provide the requested size. This means it's
1170 unsafe to request more than the size of the guard buffer.
1172 ssize_t bufgetdata(int handle_id, size_t size, void **data)
1174 const struct memory_handle *h;
1175 size_t adjusted_size = size;
1177 h = prep_bufdata(handle_id, &adjusted_size, true);
1178 if (!h)
1179 return ERR_HANDLE_NOT_FOUND;
1181 if (h->ridx + adjusted_size > buffer_len)
1183 /* the data wraps around the end of the buffer :
1184 use the guard buffer to provide the requested amount of data. */
1185 size_t copy_n = h->ridx + adjusted_size - buffer_len;
1186 /* prep_bufdata ensures adjusted_size <= buffer_len - h->ridx + GUARD_BUFSIZE,
1187 so copy_n <= GUARD_BUFSIZE */
1188 memcpy(guard_buffer, (const unsigned char *)buffer, copy_n);
1191 if (data)
1192 *data = &buffer[h->ridx];
1194 return adjusted_size;
1197 ssize_t bufgettail(int handle_id, size_t size, void **data)
1199 size_t tidx;
1201 const struct memory_handle *h;
1203 h = find_handle(handle_id);
1205 if (!h)
1206 return ERR_HANDLE_NOT_FOUND;
1208 if (h->filerem)
1209 return ERR_HANDLE_NOT_DONE;
1211 /* We don't support tail requests of > guardbuf_size, for simplicity */
1212 if (size > GUARD_BUFSIZE)
1213 return ERR_INVALID_VALUE;
1215 tidx = RINGBUF_SUB(h->widx, size);
1217 if (tidx + size > buffer_len)
1219 size_t copy_n = tidx + size - buffer_len;
1220 memcpy(guard_buffer, (const unsigned char *)buffer, copy_n);
1223 *data = &buffer[tidx];
1224 return size;
1227 ssize_t bufcuttail(int handle_id, size_t size)
1229 struct memory_handle *h;
1230 size_t adjusted_size = size;
1232 h = find_handle(handle_id);
1234 if (!h)
1235 return ERR_HANDLE_NOT_FOUND;
1237 if (h->filerem)
1238 return ERR_HANDLE_NOT_DONE;
1240 if (h->available < adjusted_size)
1241 adjusted_size = h->available;
1243 h->available -= adjusted_size;
1244 h->filesize -= adjusted_size;
1245 h->widx = RINGBUF_SUB(h->widx, adjusted_size);
1246 if (h == cur_handle)
1247 buf_widx = h->widx;
1249 return adjusted_size;
1254 SECONDARY EXPORTED FUNCTIONS
1255 ============================
1257 buf_get_offset
1258 buf_handle_offset
1259 buf_request_buffer_handle
1260 buf_set_base_handle
1261 buf_used
1262 register_buffering_callback
1263 unregister_buffering_callback
1265 These functions are exported, to allow interaction with the buffer.
1266 They take care of the content of the structs, and rely on the linked list
1267 management functions for all the actual handle management work.
1270 /* Get a handle offset from a pointer */
1271 ssize_t buf_get_offset(int handle_id, void *ptr)
1273 const struct memory_handle *h = find_handle(handle_id);
1274 if (!h)
1275 return ERR_HANDLE_NOT_FOUND;
1277 return (size_t)ptr - (size_t)&buffer[h->ridx];
1280 ssize_t buf_handle_offset(int handle_id)
1282 const struct memory_handle *h = find_handle(handle_id);
1283 if (!h)
1284 return ERR_HANDLE_NOT_FOUND;
1285 return h->offset;
1288 void buf_request_buffer_handle(int handle_id)
1290 LOGFQUEUE("buffering >| Q_START_FILL %d",handle_id);
1291 queue_send(&buffering_queue, Q_START_FILL, handle_id);
1294 void buf_set_base_handle(int handle_id)
1296 LOGFQUEUE("buffering > Q_BASE_HANDLE %d", handle_id);
1297 queue_post(&buffering_queue, Q_BASE_HANDLE, handle_id);
1300 /* Return the amount of buffer space used */
1301 size_t buf_used(void)
1303 return BUF_USED;
1306 void buf_set_watermark(size_t bytes)
1308 conf_watermark = bytes;
1311 static void shrink_buffer_inner(struct memory_handle *h)
1313 if (h == NULL)
1314 return;
1316 shrink_buffer_inner(h->next);
1318 shrink_handle(h);
1321 static void shrink_buffer(void)
1323 logf("shrink_buffer()");
1324 shrink_buffer_inner(first_handle);
1327 void buffering_thread(void)
1329 bool filling = false;
1330 struct queue_event ev;
1332 while (true)
1334 if (!filling) {
1335 cancel_cpu_boost();
1338 queue_wait_w_tmo(&buffering_queue, &ev, filling ? 5 : HZ/2);
1340 switch (ev.id)
1342 case Q_START_FILL:
1343 LOGFQUEUE("buffering < Q_START_FILL %d", (int)ev.data);
1344 /* Call buffer callbacks here because this is one of two ways
1345 * to begin a full buffer fill */
1346 send_event(BUFFER_EVENT_BUFFER_LOW, 0);
1347 shrink_buffer();
1348 queue_reply(&buffering_queue, 1);
1349 filling |= buffer_handle((int)ev.data);
1350 break;
1352 case Q_BUFFER_HANDLE:
1353 LOGFQUEUE("buffering < Q_BUFFER_HANDLE %d", (int)ev.data);
1354 queue_reply(&buffering_queue, 1);
1355 buffer_handle((int)ev.data);
1356 break;
1358 case Q_RESET_HANDLE:
1359 LOGFQUEUE("buffering < Q_RESET_HANDLE %d", (int)ev.data);
1360 queue_reply(&buffering_queue, 1);
1361 reset_handle((int)ev.data);
1362 break;
1364 case Q_CLOSE_HANDLE:
1365 LOGFQUEUE("buffering < Q_CLOSE_HANDLE %d", (int)ev.data);
1366 queue_reply(&buffering_queue, close_handle((int)ev.data));
1367 break;
1369 case Q_HANDLE_ADDED:
1370 LOGFQUEUE("buffering < Q_HANDLE_ADDED %d", (int)ev.data);
1371 /* A handle was added: the disk is spinning, so we can fill */
1372 filling = true;
1373 break;
1375 case Q_BASE_HANDLE:
1376 LOGFQUEUE("buffering < Q_BASE_HANDLE %d", (int)ev.data);
1377 base_handle_id = (int)ev.data;
1378 break;
1380 #ifndef SIMULATOR
1381 case SYS_USB_CONNECTED:
1382 LOGFQUEUE("buffering < SYS_USB_CONNECTED");
1383 usb_acknowledge(SYS_USB_CONNECTED_ACK);
1384 usb_wait_for_disconnect(&buffering_queue);
1385 break;
1386 #endif
1388 case SYS_TIMEOUT:
1389 LOGFQUEUE_SYS_TIMEOUT("buffering < SYS_TIMEOUT");
1390 break;
1393 update_data_counters();
1395 /* If the buffer is low, call the callbacks to get new data */
1396 if (num_handles > 0 && data_counters.useful <= conf_watermark)
1397 send_event(BUFFER_EVENT_BUFFER_LOW, 0);
1399 #if 0
1400 /* TODO: This needs to be fixed to use the idle callback, disable it
1401 * for simplicity until its done right */
1402 #if MEM > 8
1403 /* If the disk is spinning, take advantage by filling the buffer */
1404 else if (storage_disk_is_active() && queue_empty(&buffering_queue))
1406 if (num_handles > 0 && data_counters.useful <= high_watermark)
1407 send_event(BUFFER_EVENT_BUFFER_LOW, 0);
1409 if (data_counters.remaining > 0 && BUF_USED <= high_watermark)
1411 /* This is a new fill, shrink the buffer up first */
1412 if (!filling)
1413 shrink_buffer();
1414 filling = fill_buffer();
1415 update_data_counters();
1418 #endif
1419 #endif
1421 if (queue_empty(&buffering_queue)) {
1422 if (filling) {
1423 if (data_counters.remaining > 0 && BUF_USED < buffer_len)
1424 filling = fill_buffer();
1425 else if (data_counters.remaining == 0)
1426 filling = false;
1428 else if (ev.id == SYS_TIMEOUT)
1430 if (data_counters.remaining > 0 &&
1431 data_counters.useful <= conf_watermark) {
1432 shrink_buffer();
1433 filling = fill_buffer();
1440 void buffering_init(void)
1442 mutex_init(&llist_mutex);
1443 #ifdef HAVE_PRIORITY_SCHEDULING
1444 /* This behavior not safe atm */
1445 mutex_set_preempt(&llist_mutex, false);
1446 #endif
1448 conf_watermark = BUFFERING_DEFAULT_WATERMARK;
1450 queue_init(&buffering_queue, true);
1451 buffering_thread_id = create_thread( buffering_thread, buffering_stack,
1452 sizeof(buffering_stack), CREATE_THREAD_FROZEN,
1453 buffering_thread_name IF_PRIO(, PRIORITY_BUFFERING)
1454 IF_COP(, CPU));
1456 queue_enable_queue_send(&buffering_queue, &buffering_queue_sender_list,
1457 buffering_thread_id);
1460 /* Initialise the buffering subsystem */
1461 bool buffering_reset(char *buf, size_t buflen)
1463 if (!buf || !buflen)
1464 return false;
1466 buffer = buf;
1467 buffer_len = buflen;
1468 guard_buffer = buf + buflen;
1470 buf_widx = 0;
1471 buf_ridx = 0;
1473 first_handle = NULL;
1474 cur_handle = NULL;
1475 cached_handle = NULL;
1476 num_handles = 0;
1477 base_handle_id = -1;
1479 /* Set the high watermark as 75% full...or 25% empty :) */
1480 #if MEM > 8
1481 high_watermark = 3*buflen / 4;
1482 #endif
1484 thread_thaw(buffering_thread_id);
1486 return true;
1489 void buffering_get_debugdata(struct buffering_debug *dbgdata)
1491 update_data_counters();
1492 dbgdata->num_handles = num_handles;
1493 dbgdata->data_rem = data_counters.remaining;
1494 dbgdata->wasted_space = data_counters.wasted;
1495 dbgdata->buffered_data = data_counters.buffered;
1496 dbgdata->useful_data = data_counters.useful;
1497 dbgdata->watermark = conf_watermark;