Oops. Put back some changes to go only with others.
[kugel-rb.git] / apps / buffering.c
blob48ef1bc6c0491fde6bc52e4bb28b64e108d2a7b7
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 <inttypes.h>
28 #include "buffering.h"
30 #include "storage.h"
31 #include "system.h"
32 #include "thread.h"
33 #include "file.h"
34 #include "panic.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 "settings.h"
42 #include "codecs.h"
43 #include "audio.h"
44 #include "mp3_playback.h"
45 #include "usb.h"
46 #include "screens.h"
47 #include "playlist.h"
48 #include "pcmbuf.h"
49 #include "appevents.h"
50 #include "metadata.h"
51 #ifdef HAVE_ALBUMART
52 #include "albumart.h"
53 #include "jpeg_load.h"
54 #include "bmp.h"
55 #include "playback.h"
56 #endif
58 #define GUARD_BUFSIZE (32*1024)
60 /* Define LOGF_ENABLE to enable logf output in this file */
61 /*#define LOGF_ENABLE*/
62 #include "logf.h"
64 /* macros to enable logf for queues
65 logging on SYS_TIMEOUT can be disabled */
66 #ifdef SIMULATOR
67 /* Define this for logf output of all queuing except SYS_TIMEOUT */
68 #define BUFFERING_LOGQUEUES
69 /* Define this to logf SYS_TIMEOUT messages */
70 /* #define BUFFERING_LOGQUEUES_SYS_TIMEOUT */
71 #endif
73 #ifdef BUFFERING_LOGQUEUES
74 #define LOGFQUEUE logf
75 #else
76 #define LOGFQUEUE(...)
77 #endif
79 #ifdef BUFFERING_LOGQUEUES_SYS_TIMEOUT
80 #define LOGFQUEUE_SYS_TIMEOUT logf
81 #else
82 #define LOGFQUEUE_SYS_TIMEOUT(...)
83 #endif
85 /* default point to start buffer refill */
86 #define BUFFERING_DEFAULT_WATERMARK (1024*128)
87 /* amount of data to read in one read() call */
88 #define BUFFERING_DEFAULT_FILECHUNK (1024*32)
90 #define BUF_HANDLE_MASK 0x7FFFFFFF
93 /* assert(sizeof(struct memory_handle)%4==0) */
94 struct memory_handle {
95 int id; /* A unique ID for the handle */
96 enum data_type type; /* Type of data buffered with this handle */
97 char path[MAX_PATH]; /* Path if data originated in a file */
98 int fd; /* File descriptor to path (-1 if closed) */
99 size_t start; /* Start index of the handle's data buffer,
100 for use by reset_handle. */
101 size_t data; /* Start index of the handle's data */
102 volatile size_t ridx; /* Read pointer, relative to the main buffer */
103 size_t widx; /* Write pointer */
104 size_t filesize; /* File total length */
105 size_t filerem; /* Remaining bytes of file NOT in buffer */
106 volatile size_t available; /* Available bytes to read from buffer */
107 size_t offset; /* Offset at which we started reading the file */
108 struct memory_handle *next;
110 /* invariant: filesize == offset + available + filerem */
112 static char *buffer;
113 static char *guard_buffer;
115 static size_t buffer_len;
117 static volatile size_t buf_widx; /* current writing position */
118 static volatile size_t buf_ridx; /* current reading position */
119 /* buf_*idx are values relative to the buffer, not real pointers. */
121 /* Configuration */
122 static size_t conf_watermark = 0; /* Level to trigger filebuf fill */
123 #if MEMORYSIZE > 8
124 static size_t high_watermark = 0; /* High watermark for rebuffer */
125 #endif
127 /* current memory handle in the linked list. NULL when the list is empty. */
128 static struct memory_handle *cur_handle;
129 /* first memory handle in the linked list. NULL when the list is empty. */
130 static struct memory_handle *first_handle;
132 static int num_handles; /* number of handles in the list */
134 static int base_handle_id;
136 static struct mutex llist_mutex;
137 static struct mutex llist_mod_mutex;
139 /* Handle cache (makes find_handle faster).
140 This is global so that move_handle and rm_handle can invalidate it. */
141 static struct memory_handle *cached_handle = NULL;
143 static struct {
144 size_t remaining; /* Amount of data needing to be buffered */
145 size_t wasted; /* Amount of space available for freeing */
146 size_t buffered; /* Amount of data currently in the buffer */
147 size_t useful; /* Amount of data still useful to the user */
148 } data_counters;
151 /* Messages available to communicate with the buffering thread */
152 enum {
153 Q_BUFFER_HANDLE = 1, /* Request buffering of a handle, this should not be
154 used in a low buffer situation. */
155 Q_RESET_HANDLE, /* (internal) Request resetting of a handle to its
156 offset (the offset has to be set beforehand) */
157 Q_CLOSE_HANDLE, /* Request closing a handle */
158 Q_BASE_HANDLE, /* Set the reference handle for buf_useful_data */
160 /* Configuration: */
161 Q_START_FILL, /* Request that the buffering thread initiate a buffer
162 fill at its earliest convenience */
163 Q_HANDLE_ADDED, /* Inform the buffering thread that a handle was added,
164 (which means the disk is spinning) */
167 /* Buffering thread */
168 static void buffering_thread(void);
169 static long buffering_stack[(DEFAULT_STACK_SIZE + 0x2000)/sizeof(long)];
170 static const char buffering_thread_name[] = "buffering";
171 static unsigned int buffering_thread_id = 0;
172 static struct event_queue buffering_queue;
173 static struct queue_sender_list buffering_queue_sender_list;
177 /* Ring buffer helper functions */
179 static inline uintptr_t ringbuf_offset(const void *ptr)
181 return (uintptr_t)(ptr - (void*)buffer);
184 /* Buffer pointer (p) plus value (v), wrapped if necessary */
185 static inline uintptr_t ringbuf_add(uintptr_t p, size_t v)
187 uintptr_t res = p + v;
188 if (res >= buffer_len)
189 res -= buffer_len; /* wrap if necssary */
190 return res;
194 /* Buffer pointer (p) minus value (v), wrapped if necessary */
195 static inline uintptr_t ringbuf_sub(uintptr_t p, size_t v)
197 uintptr_t res = p;
198 if (p < v)
199 res += buffer_len; /* wrap */
201 return res - v;
205 /* How far value (v) plus buffer pointer (p1) will cross buffer pointer (p2) */
206 static inline ssize_t ringbuf_add_cross(uintptr_t p1, size_t v, uintptr_t p2)
208 ssize_t res = p1 + v - p2;
209 if (p1 >= p2) /* wrap if necessary */
210 res -= buffer_len;
212 return res;
215 /* Bytes available in the buffer */
216 #define BUF_USED ringbuf_sub(buf_widx, buf_ridx)
219 LINKED LIST MANAGEMENT
220 ======================
222 add_handle : Add a handle to the list
223 rm_handle : Remove a handle from the list
224 find_handle : Get a handle pointer from an ID
225 move_handle : Move a handle in the buffer (with or without its data)
227 These functions only handle the linked list structure. They don't touch the
228 contents of the struct memory_handle headers. They also change the buf_*idx
229 pointers when necessary and manage the handle IDs.
231 The first and current (== last) handle are kept track of.
232 A new handle is added at buf_widx and becomes the current one.
233 buf_widx always points to the current writing position for the current handle
234 buf_ridx always points to the location of the first handle.
235 buf_ridx == buf_widx means the buffer is empty.
239 /* Add a new handle to the linked list and return it. It will have become the
240 new current handle.
241 data_size must contain the size of what will be in the handle.
242 can_wrap tells us whether this type of data may wrap on buffer
243 alloc_all tells us if we must immediately be able to allocate data_size
244 returns a valid memory handle if all conditions for allocation are met.
245 NULL if there memory_handle itself cannot be allocated or if the
246 data_size cannot be allocated and alloc_all is set. This function's
247 only potential side effect is to allocate space for the cur_handle
248 if it returns NULL.
250 static struct memory_handle *add_handle(size_t data_size, bool can_wrap,
251 bool alloc_all)
253 /* gives each handle a unique id */
254 static int cur_handle_id = 0;
255 size_t shift;
256 size_t new_widx;
257 size_t len;
258 ssize_t overlap;
260 if (num_handles >= BUF_MAX_HANDLES)
261 return NULL;
263 mutex_lock(&llist_mutex);
264 mutex_lock(&llist_mod_mutex);
266 if (cur_handle && cur_handle->filerem > 0) {
267 /* the current handle hasn't finished buffering. We can only add
268 a new one if there is already enough free space to finish
269 the buffering. */
270 size_t req = cur_handle->filerem + sizeof(struct memory_handle);
271 if (ringbuf_add_cross(cur_handle->widx, req, buf_ridx) >= 0) {
272 /* Not enough space */
273 mutex_unlock(&llist_mod_mutex);
274 mutex_unlock(&llist_mutex);
275 return NULL;
276 } else {
277 /* Allocate the remainder of the space for the current handle */
278 buf_widx = ringbuf_add(cur_handle->widx, cur_handle->filerem);
282 /* align to 4 bytes up */
283 new_widx = ringbuf_add(buf_widx, 3) & ~3;
285 len = data_size + sizeof(struct memory_handle);
287 /* First, will the handle wrap? */
288 /* If the handle would wrap, move to the beginning of the buffer,
289 * or if the data must not but would wrap, move it to the beginning */
290 if( (new_widx + sizeof(struct memory_handle) > buffer_len) ||
291 (!can_wrap && (new_widx + len > buffer_len)) ) {
292 new_widx = 0;
295 /* How far we shifted buf_widx to align things, must be < buffer_len */
296 shift = ringbuf_sub(new_widx, buf_widx);
298 /* How much space are we short in the actual ring buffer? */
299 overlap = ringbuf_add_cross(buf_widx, shift + len, buf_ridx);
300 if (overlap >= 0 && (alloc_all || (size_t)overlap >= data_size)) {
301 /* Not enough space for required allocations */
302 mutex_unlock(&llist_mod_mutex);
303 mutex_unlock(&llist_mutex);
304 return NULL;
307 /* There is enough space for the required data, advance the buf_widx and
308 * initialize the struct */
309 buf_widx = new_widx;
311 struct memory_handle *new_handle =
312 (struct memory_handle *)(&buffer[buf_widx]);
314 /* only advance the buffer write index of the size of the struct */
315 buf_widx = ringbuf_add(buf_widx, sizeof(struct memory_handle));
317 new_handle->id = cur_handle_id;
318 /* Wrap signed int is safe and 0 doesn't happen */
319 cur_handle_id = (cur_handle_id + 1) & BUF_HANDLE_MASK;
320 new_handle->next = NULL;
321 num_handles++;
323 if (!first_handle)
324 /* the new handle is the first one */
325 first_handle = new_handle;
327 if (cur_handle)
328 cur_handle->next = new_handle;
330 cur_handle = new_handle;
332 mutex_unlock(&llist_mod_mutex);
333 mutex_unlock(&llist_mutex);
334 return new_handle;
337 /* Delete a given memory handle from the linked list
338 and return true for success. Nothing is actually erased from memory. */
339 static bool rm_handle(const struct memory_handle *h)
341 if (h == NULL)
342 return true;
344 mutex_lock(&llist_mutex);
345 mutex_lock(&llist_mod_mutex);
347 if (h == first_handle) {
348 first_handle = h->next;
349 if (h == cur_handle) {
350 /* h was the first and last handle: the buffer is now empty */
351 cur_handle = NULL;
352 buf_ridx = buf_widx = 0;
353 } else {
354 /* update buf_ridx to point to the new first handle */
355 buf_ridx = (size_t)ringbuf_offset(first_handle);
357 } else {
358 struct memory_handle *m = first_handle;
359 /* Find the previous handle */
360 while (m && m->next != h) {
361 m = m->next;
363 if (m && m->next == h) {
364 m->next = h->next;
365 if (h == cur_handle) {
366 cur_handle = m;
367 buf_widx = cur_handle->widx;
369 } else {
370 mutex_unlock(&llist_mod_mutex);
371 mutex_unlock(&llist_mutex);
372 return false;
376 /* Invalidate the cache to prevent it from keeping the old location of h */
377 if (h == cached_handle)
378 cached_handle = NULL;
380 num_handles--;
382 mutex_unlock(&llist_mod_mutex);
383 mutex_unlock(&llist_mutex);
384 return true;
387 /* Return a pointer to the memory handle of given ID.
388 NULL if the handle wasn't found */
389 static struct memory_handle *find_handle(int handle_id)
391 if (handle_id < 0)
392 return NULL;
394 mutex_lock(&llist_mutex);
396 /* simple caching because most of the time the requested handle
397 will either be the same as the last, or the one after the last */
398 if (cached_handle)
400 if (cached_handle->id == handle_id) {
401 mutex_unlock(&llist_mutex);
402 return cached_handle;
403 } else if (cached_handle->next &&
404 (cached_handle->next->id == handle_id)) {
405 cached_handle = cached_handle->next;
406 mutex_unlock(&llist_mutex);
407 return cached_handle;
411 struct memory_handle *m = first_handle;
412 while (m && m->id != handle_id) {
413 m = m->next;
415 /* This condition can only be reached with !m or m->id == handle_id */
416 if (m)
417 cached_handle = m;
419 mutex_unlock(&llist_mutex);
420 return m;
423 /* Move a memory handle and data_size of its data delta bytes along the buffer.
424 delta maximum bytes available to move the handle. If the move is performed
425 it is set to the actual distance moved.
426 data_size is the amount of data to move along with the struct.
427 returns true if the move is successful and false if the handle is NULL,
428 the move would be less than the size of a memory_handle after
429 correcting for wraps or if the handle is not found in the linked
430 list for adjustment. This function has no side effects if false
431 is returned. */
432 static bool move_handle(struct memory_handle **h, size_t *delta,
433 size_t data_size, bool can_wrap)
435 struct memory_handle *dest;
436 const struct memory_handle *src;
437 size_t final_delta = *delta, size_to_move;
438 uintptr_t oldpos, newpos;
439 intptr_t overlap, overlap_old;
441 if (h == NULL || (src = *h) == NULL)
442 return false;
444 size_to_move = sizeof(struct memory_handle) + data_size;
446 /* Align to four bytes, down */
447 final_delta &= ~3;
448 if (final_delta < sizeof(struct memory_handle)) {
449 /* It's not legal to move less than the size of the struct */
450 return false;
453 mutex_lock(&llist_mutex);
454 mutex_lock(&llist_mod_mutex);
456 oldpos = ringbuf_offset(src);
457 newpos = ringbuf_add(oldpos, final_delta);
458 overlap = ringbuf_add_cross(newpos, size_to_move, buffer_len);
459 overlap_old = ringbuf_add_cross(oldpos, size_to_move, buffer_len);
461 if (overlap > 0) {
462 /* Some part of the struct + data would wrap, maybe ok */
463 ssize_t correction = 0;
464 /* If the overlap lands inside the memory_handle */
465 if (!can_wrap) {
466 /* Otherwise the overlap falls in the data area and must all be
467 * backed out. This may become conditional if ever we move
468 * data that is allowed to wrap (ie audio) */
469 correction = overlap;
470 } else if ((uintptr_t)overlap > data_size) {
471 /* Correct the position and real delta to prevent the struct from
472 * wrapping, this guarantees an aligned delta if the struct size is
473 * aligned and the buffer is aligned */
474 correction = overlap - data_size;
476 if (correction) {
477 /* Align correction to four bytes up */
478 correction = (correction + 3) & ~3;
479 if (final_delta < correction + sizeof(struct memory_handle)) {
480 /* Delta cannot end up less than the size of the struct */
481 mutex_unlock(&llist_mod_mutex);
482 mutex_unlock(&llist_mutex);
483 return false;
485 newpos -= correction;
486 overlap -= correction;/* Used below to know how to split the data */
487 final_delta -= correction;
491 dest = (struct memory_handle *)(&buffer[newpos]);
493 if (src == first_handle) {
494 first_handle = dest;
495 buf_ridx = newpos;
496 } else {
497 struct memory_handle *m = first_handle;
498 while (m && m->next != src) {
499 m = m->next;
501 if (m && m->next == src) {
502 m->next = dest;
503 } else {
504 mutex_unlock(&llist_mod_mutex);
505 mutex_unlock(&llist_mutex);
506 return false;
510 /* Update the cache to prevent it from keeping the old location of h */
511 if (src == cached_handle)
512 cached_handle = dest;
514 /* the cur_handle pointer might need updating */
515 if (src == cur_handle)
516 cur_handle = dest;
518 /* x = handle(s) following this one...
519 * ...if last handle, unmoveable if metadata, only shrinkable if audio.
520 * In other words, no legal move can be made that would have the src head
521 * and dest tail of the data overlap itself. These facts reduce the
522 * problem to four essential permutations.
524 * movement: always "clockwise" >>>>
526 * (src nowrap, dest nowrap)
527 * |0123 x |
528 * | 0123x | etc...
529 * move: "0123"
531 * (src nowrap, dest wrap)
532 * | x0123 |
533 * |23x 01|
534 * move: "23", "01"
536 * (src wrap, dest nowrap)
537 * |23 x01|
538 * | 0123x |
539 * move: "23", "01"
541 * (src wrap, dest wrap)
542 * |23 x 01|
543 * |123x 0|
544 * move: "23", "1", "0"
546 if (overlap_old > 0) {
547 /* Move over already wrapped data by the final delta */
548 memmove(&buffer[final_delta], buffer, overlap_old);
549 if (overlap <= 0)
550 size_to_move -= overlap_old;
553 if (overlap > 0) {
554 /* Move data that now wraps to the beginning */
555 size_to_move -= overlap;
556 memmove(buffer, SKIPBYTES(src, size_to_move),
557 overlap_old > 0 ? final_delta : (size_t)overlap);
560 /* Move leading fragment containing handle struct */
561 memmove(dest, src, size_to_move);
563 /* Update the caller with the new location of h and the distance moved */
564 *h = dest;
565 *delta = final_delta;
566 mutex_unlock(&llist_mod_mutex);
567 mutex_unlock(&llist_mutex);
568 return true;
573 BUFFER SPACE MANAGEMENT
574 =======================
576 update_data_counters: Updates the values in data_counters
577 buffer_is_low : Returns true if the amount of useful data in the buffer is low
578 buffer_handle : Buffer data for a handle
579 reset_handle : Reset write position and data buffer of a handle to its offset
580 rebuffer_handle : Seek to a nonbuffered part of a handle by rebuffering the data
581 shrink_handle : Free buffer space by moving a handle
582 fill_buffer : Call buffer_handle for all handles that have data to buffer
584 These functions are used by the buffering thread to manage buffer space.
587 static void update_data_counters(void)
589 struct memory_handle *m = find_handle(base_handle_id);
590 bool is_useful = m==NULL;
592 size_t buffered = 0;
593 size_t wasted = 0;
594 size_t remaining = 0;
595 size_t useful = 0;
597 mutex_lock(&llist_mutex);
599 m = first_handle;
600 while (m) {
601 buffered += m->available;
602 wasted += ringbuf_sub(m->ridx, m->data);
603 remaining += m->filerem;
605 if (m->id == base_handle_id)
606 is_useful = true;
608 if (is_useful)
609 useful += ringbuf_sub(m->widx, m->ridx);
611 m = m->next;
614 mutex_unlock(&llist_mutex);
616 data_counters.buffered = buffered;
617 data_counters.wasted = wasted;
618 data_counters.remaining = remaining;
619 data_counters.useful = useful;
622 static inline bool buffer_is_low(void)
624 update_data_counters();
625 return data_counters.useful < (conf_watermark / 2);
628 /* Buffer data for the given handle.
629 Return whether or not the buffering should continue explicitly. */
630 static bool buffer_handle(int handle_id)
632 logf("buffer_handle(%d)", handle_id);
633 struct memory_handle *h = find_handle(handle_id);
634 bool stop = false;
636 if (!h)
637 return true;
639 if (h->filerem == 0) {
640 /* nothing left to buffer */
641 return true;
644 if (h->fd < 0) /* file closed, reopen */
646 if (*h->path)
647 h->fd = open(h->path, O_RDONLY);
649 if (h->fd < 0)
651 /* could not open the file, truncate it where it is */
652 h->filesize -= h->filerem;
653 h->filerem = 0;
654 return true;
657 if (h->offset)
658 lseek(h->fd, h->offset, SEEK_SET);
661 trigger_cpu_boost();
663 if (h->type == TYPE_ID3)
665 if (!get_metadata((struct mp3entry *)(buffer + h->data), h->fd, h->path))
667 /* metadata parsing failed: clear the buffer. */
668 memset(buffer + h->data, 0, sizeof(struct mp3entry));
670 close(h->fd);
671 h->fd = -1;
672 h->filerem = 0;
673 h->available = sizeof(struct mp3entry);
674 h->widx += sizeof(struct mp3entry);
675 send_event(BUFFER_EVENT_FINISHED, &h->id);
676 return true;
679 while (h->filerem > 0 && !stop)
681 /* max amount to copy */
682 ssize_t copy_n = MIN( MIN(h->filerem, BUFFERING_DEFAULT_FILECHUNK),
683 buffer_len - h->widx);
684 uintptr_t offset = h->next ? ringbuf_offset(h->next) : buf_ridx;
685 ssize_t overlap = ringbuf_add_cross(h->widx, copy_n, offset);
687 if (!h->next)
688 overlap++; /* sub one more below to avoid buffer overflow */
690 if (overlap > 0)
692 /* read only up to available space and stop if it would overwrite
693 the reading position or the next handle */
694 stop = true;
695 copy_n -= overlap;
698 if (copy_n <= 0)
699 return false; /* no space for read */
701 /* rc is the actual amount read */
702 int rc = read(h->fd, &buffer[h->widx], copy_n);
704 if (rc < 0)
706 /* Some kind of filesystem error, maybe recoverable if not codec */
707 if (h->type == TYPE_CODEC) {
708 logf("Partial codec");
709 break;
712 DEBUGF("File ended %ld bytes early\n", (long)h->filerem);
713 h->filesize -= h->filerem;
714 h->filerem = 0;
715 break;
718 /* Advance buffer */
719 h->widx = ringbuf_add(h->widx, rc);
720 if (h == cur_handle)
721 buf_widx = h->widx;
722 h->available += rc;
723 h->filerem -= rc;
725 /* If this is a large file, see if we need to break or give the codec
726 * more time */
727 if (h->type == TYPE_PACKET_AUDIO &&
728 pcmbuf_is_lowdata() && !buffer_is_low())
730 sleep(1);
732 else
734 yield();
737 if (!queue_empty(&buffering_queue))
738 break;
741 if (h->filerem == 0) {
742 /* finished buffering the file */
743 close(h->fd);
744 h->fd = -1;
745 send_event(BUFFER_EVENT_FINISHED, &h->id);
748 return !stop;
751 /* Reset writing position and data buffer of a handle to its current offset.
752 Use this after having set the new offset to use. */
753 static void reset_handle(int handle_id)
755 size_t alignment_pad;
757 logf("reset_handle(%d)", handle_id);
759 struct memory_handle *h = find_handle(handle_id);
760 if (!h)
761 return;
763 /* Align to desired storage alignment */
764 alignment_pad = STORAGE_OVERLAP(h->offset - (size_t)(&buffer[h->start]));
765 h->ridx = h->widx = h->data = ringbuf_add(h->start, alignment_pad);
767 if (h == cur_handle)
768 buf_widx = h->widx;
769 h->available = 0;
770 h->filerem = h->filesize - h->offset;
772 if (h->fd >= 0) {
773 lseek(h->fd, h->offset, SEEK_SET);
777 /* Seek to a nonbuffered part of a handle by rebuffering the data. */
778 static void rebuffer_handle(int handle_id, size_t newpos)
780 struct memory_handle *h = find_handle(handle_id);
781 if (!h)
782 return;
784 /* When seeking foward off of the buffer, if it is a short seek don't
785 rebuffer the whole track, just read enough to satisfy */
786 if (newpos > h->offset && newpos - h->offset < BUFFERING_DEFAULT_FILECHUNK)
788 LOGFQUEUE("buffering >| Q_BUFFER_HANDLE %d", handle_id);
789 queue_send(&buffering_queue, Q_BUFFER_HANDLE, handle_id);
790 h->ridx = ringbuf_add(h->data, newpos - h->offset);
791 return;
794 h->offset = newpos;
796 /* Reset the handle to its new offset */
797 LOGFQUEUE("buffering >| Q_RESET_HANDLE %d", handle_id);
798 queue_send(&buffering_queue, Q_RESET_HANDLE, handle_id);
800 uintptr_t next = ringbuf_offset(h->next);
801 if (ringbuf_sub(next, h->data) < h->filesize - newpos)
803 /* There isn't enough space to rebuffer all of the track from its new
804 offset, so we ask the user to free some */
805 DEBUGF("%s(): space is needed\n", __func__);
806 send_event(BUFFER_EVENT_REBUFFER, &handle_id);
809 /* Now we ask for a rebuffer */
810 LOGFQUEUE("buffering >| Q_BUFFER_HANDLE %d", handle_id);
811 queue_send(&buffering_queue, Q_BUFFER_HANDLE, handle_id);
814 static bool close_handle(int handle_id)
816 struct memory_handle *h = find_handle(handle_id);
818 /* If the handle is not found, it is closed */
819 if (!h)
820 return true;
822 if (h->fd >= 0) {
823 close(h->fd);
824 h->fd = -1;
827 /* rm_handle returns true unless the handle somehow persists after exit */
828 return rm_handle(h);
831 /* Free buffer space by moving the handle struct right before the useful
832 part of its data buffer or by moving all the data. */
833 static void shrink_handle(struct memory_handle *h)
835 size_t delta;
837 if (!h)
838 return;
840 if (h->type == TYPE_ID3 || h->type == TYPE_CUESHEET ||
841 h->type == TYPE_BITMAP || h->type == TYPE_CODEC ||
842 h->type == TYPE_ATOMIC_AUDIO)
844 /* metadata handle: we can move all of it */
845 if (!h->next || h->filerem != 0)
846 return; /* Last handle or not finished loading */
848 uintptr_t handle_distance =
849 ringbuf_sub(ringbuf_offset(h->next), h->data);
850 delta = handle_distance - h->available;
852 /* The value of delta might change for alignment reasons */
853 if (!move_handle(&h, &delta, h->available, h->type==TYPE_CODEC))
854 return;
856 size_t olddata = h->data;
857 h->data = ringbuf_add(h->data, delta);
858 h->ridx = ringbuf_add(h->ridx, delta);
859 h->widx = ringbuf_add(h->widx, delta);
861 if (h->type == TYPE_ID3 && h->filesize == sizeof(struct mp3entry)) {
862 /* when moving an mp3entry we need to readjust its pointers. */
863 adjust_mp3entry((struct mp3entry *)&buffer[h->data],
864 (void *)&buffer[h->data],
865 (const void *)&buffer[olddata]);
866 } else if (h->type == TYPE_BITMAP) {
867 /* adjust the bitmap's pointer */
868 struct bitmap *bmp = (struct bitmap *)&buffer[h->data];
869 bmp->data = &buffer[h->data + sizeof(struct bitmap)];
872 else
874 /* only move the handle struct */
875 delta = ringbuf_sub(h->ridx, h->data);
876 if (!move_handle(&h, &delta, 0, true))
877 return;
879 h->data = ringbuf_add(h->data, delta);
880 h->start = ringbuf_add(h->start, delta);
881 h->available -= delta;
882 h->offset += delta;
886 /* Fill the buffer by buffering as much data as possible for handles that still
887 have data left to buffer
888 Return whether or not to continue filling after this */
889 static bool fill_buffer(void)
891 logf("fill_buffer()");
892 struct memory_handle *m;
893 shrink_handle(first_handle);
894 m = first_handle;
895 while (queue_empty(&buffering_queue) && m) {
896 if (m->filerem > 0) {
897 if (!buffer_handle(m->id)) {
898 m = NULL;
899 break;
902 m = m->next;
905 if (m) {
906 return true;
908 else
910 /* only spin the disk down if the filling wasn't interrupted by an
911 event arriving in the queue. */
912 storage_sleep();
913 return false;
917 #ifdef HAVE_ALBUMART
918 /* Given a file descriptor to a bitmap file, write the bitmap data to the
919 buffer, with a struct bitmap and the actual data immediately following.
920 Return value is the total size (struct + data). */
921 static int load_image(int fd, const char *path, struct bufopen_bitmap_data *data)
923 int rc;
924 struct bitmap *bmp = (struct bitmap *)&buffer[buf_widx];
925 struct dim *dim = data->dim;
926 struct mp3_albumart *aa = data->embedded_albumart;
928 /* get the desired image size */
929 bmp->width = dim->width, bmp->height = dim->height;
930 /* FIXME: alignment may be needed for the data buffer. */
931 bmp->data = &buffer[buf_widx + sizeof(struct bitmap)];
932 #ifndef HAVE_JPEG
933 (void) path;
934 #endif
935 #if (LCD_DEPTH > 1) || defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1)
936 bmp->maskdata = NULL;
937 #endif
939 int free = (int)MIN(buffer_len - BUF_USED, buffer_len - buf_widx)
940 - sizeof(struct bitmap);
942 #ifdef HAVE_JPEG
943 if (aa != NULL)
945 lseek(fd, aa->pos, SEEK_SET);
946 rc = clip_jpeg_fd(fd, aa->size, bmp, free, FORMAT_NATIVE|FORMAT_DITHER|
947 FORMAT_RESIZE|FORMAT_KEEP_ASPECT, NULL);
949 else if (strcmp(path + strlen(path) - 4, ".bmp"))
950 rc = read_jpeg_fd(fd, bmp, free, FORMAT_NATIVE|FORMAT_DITHER|
951 FORMAT_RESIZE|FORMAT_KEEP_ASPECT, NULL);
952 else
953 #endif
954 rc = read_bmp_fd(fd, bmp, free, FORMAT_NATIVE|FORMAT_DITHER|
955 FORMAT_RESIZE|FORMAT_KEEP_ASPECT, NULL);
956 return rc + (rc > 0 ? sizeof(struct bitmap) : 0);
958 #endif
962 MAIN BUFFERING API CALLS
963 ========================
965 bufopen : Request the opening of a new handle for a file
966 bufalloc : Open a new handle for data other than a file.
967 bufclose : Close an open handle
968 bufseek : Set the read pointer in a handle
969 bufadvance : Move the read pointer in a handle
970 bufread : Copy data from a handle into a given buffer
971 bufgetdata : Give a pointer to the handle's data
973 These functions are exported, to allow interaction with the buffer.
974 They take care of the content of the structs, and rely on the linked list
975 management functions for all the actual handle management work.
979 /* Reserve space in the buffer for a file.
980 filename: name of the file to open
981 offset: offset at which to start buffering the file, useful when the first
982 (offset-1) bytes of the file aren't needed.
983 type: one of the data types supported (audio, image, cuesheet, others
984 user_data: user data passed possibly passed in subcalls specific to a
985 data_type (only used for image (albumart) buffering so far )
986 return value: <0 if the file cannot be opened, or one file already
987 queued to be opened, otherwise the handle for the file in the buffer
989 int bufopen(const char *file, size_t offset, enum data_type type,
990 void *user_data)
992 #ifndef HAVE_ALBUMART
993 /* currently only used for aa loading */
994 (void)user_data;
995 #endif
996 if (type == TYPE_ID3)
998 /* ID3 case: allocate space, init the handle and return. */
1000 struct memory_handle *h = add_handle(sizeof(struct mp3entry), false, true);
1001 if (!h)
1002 return ERR_BUFFER_FULL;
1004 h->fd = -1;
1005 h->filesize = sizeof(struct mp3entry);
1006 h->filerem = sizeof(struct mp3entry);
1007 h->offset = 0;
1008 h->data = buf_widx;
1009 h->ridx = buf_widx;
1010 h->widx = buf_widx;
1011 h->available = 0;
1012 h->type = type;
1013 strlcpy(h->path, file, MAX_PATH);
1015 buf_widx += sizeof(struct mp3entry); /* safe because the handle
1016 can't wrap */
1018 /* Inform the buffering thread that we added a handle */
1019 LOGFQUEUE("buffering > Q_HANDLE_ADDED %d", h->id);
1020 queue_post(&buffering_queue, Q_HANDLE_ADDED, h->id);
1022 return h->id;
1024 #ifdef APPLICATION
1025 /* loading code from memory is not supported in application builds */
1026 else if (type == TYPE_CODEC)
1027 return ERR_UNSUPPORTED_TYPE;
1028 #endif
1029 /* Other cases: there is a little more work. */
1030 int fd = open(file, O_RDONLY);
1031 if (fd < 0)
1032 return ERR_FILE_ERROR;
1034 size_t size = 0;
1035 #ifdef HAVE_ALBUMART
1036 if (type == TYPE_BITMAP)
1037 { /* if albumart is embedded, the complete file is not buffered,
1038 * but only the jpeg part; filesize() would be wrong */
1039 struct bufopen_bitmap_data *aa = (struct bufopen_bitmap_data*)user_data;
1040 if (aa->embedded_albumart)
1041 size = aa->embedded_albumart->size;
1043 #endif
1044 if (size == 0)
1045 size = filesize(fd);
1046 bool can_wrap = type==TYPE_PACKET_AUDIO || type==TYPE_CODEC;
1048 size_t adjusted_offset = offset;
1049 if (adjusted_offset > size)
1050 adjusted_offset = 0;
1052 /* Reserve extra space because alignment can move data forward */
1053 size_t padded_size = STORAGE_PAD(size-adjusted_offset);
1054 struct memory_handle *h = add_handle(padded_size, can_wrap, false);
1055 if (!h)
1057 DEBUGF("%s(): failed to add handle\n", __func__);
1058 close(fd);
1059 return ERR_BUFFER_FULL;
1062 strlcpy(h->path, file, MAX_PATH);
1063 h->offset = adjusted_offset;
1065 /* Don't bother to storage align bitmaps because they are not
1066 * loaded directly into the buffer.
1068 if (type != TYPE_BITMAP)
1070 size_t alignment_pad;
1072 /* Remember where data area starts, for use by reset_handle */
1073 h->start = buf_widx;
1075 /* Align to desired storage alignment */
1076 alignment_pad = STORAGE_OVERLAP(adjusted_offset - (size_t)(&buffer[buf_widx]));
1077 buf_widx = ringbuf_add(buf_widx, alignment_pad);
1080 h->ridx = buf_widx;
1081 h->widx = buf_widx;
1082 h->data = buf_widx;
1083 h->available = 0;
1084 h->filerem = 0;
1085 h->type = type;
1087 #ifdef HAVE_ALBUMART
1088 if (type == TYPE_BITMAP)
1090 /* Bitmap file: we load the data instead of the file */
1091 int rc;
1092 mutex_lock(&llist_mod_mutex); /* Lock because load_bitmap yields */
1093 rc = load_image(fd, file, (struct bufopen_bitmap_data*)user_data);
1094 mutex_unlock(&llist_mod_mutex);
1095 if (rc <= 0)
1097 rm_handle(h);
1098 close(fd);
1099 return ERR_FILE_ERROR;
1101 h->filerem = 0;
1102 h->filesize = rc;
1103 h->available = rc;
1104 h->widx = buf_widx + rc; /* safe because the data doesn't wrap */
1105 buf_widx += rc; /* safe too */
1107 else
1108 #endif
1110 h->filerem = size - adjusted_offset;
1111 h->filesize = size;
1112 h->available = 0;
1113 h->widx = buf_widx;
1116 if (type == TYPE_CUESHEET) {
1117 h->fd = fd;
1118 /* Immediately start buffering those */
1119 LOGFQUEUE("buffering >| Q_BUFFER_HANDLE %d", h->id);
1120 queue_send(&buffering_queue, Q_BUFFER_HANDLE, h->id);
1121 } else {
1122 /* Other types will get buffered in the course of normal operations */
1123 h->fd = -1;
1124 close(fd);
1126 /* Inform the buffering thread that we added a handle */
1127 LOGFQUEUE("buffering > Q_HANDLE_ADDED %d", h->id);
1128 queue_post(&buffering_queue, Q_HANDLE_ADDED, h->id);
1131 logf("bufopen: new hdl %d", h->id);
1132 return h->id;
1135 /* Open a new handle from data that needs to be copied from memory.
1136 src is the source buffer from which to copy data. It can be NULL to simply
1137 reserve buffer space.
1138 size is the requested size. The call will only be successful if the
1139 requested amount of data can entirely fit in the buffer without wrapping.
1140 Return value is the handle id for success or <0 for failure.
1142 int bufalloc(const void *src, size_t size, enum data_type type)
1144 struct memory_handle *h = add_handle(size, false, true);
1146 if (!h)
1147 return ERR_BUFFER_FULL;
1149 if (src) {
1150 if (type == TYPE_ID3 && size == sizeof(struct mp3entry)) {
1151 /* specially take care of struct mp3entry */
1152 copy_mp3entry((struct mp3entry *)&buffer[buf_widx],
1153 (const struct mp3entry *)src);
1154 } else {
1155 memcpy(&buffer[buf_widx], src, size);
1159 h->fd = -1;
1160 *h->path = 0;
1161 h->filesize = size;
1162 h->filerem = 0;
1163 h->offset = 0;
1164 h->ridx = buf_widx;
1165 h->widx = buf_widx + size; /* this is safe because the data doesn't wrap */
1166 h->data = buf_widx;
1167 h->available = size;
1168 h->type = type;
1170 buf_widx += size; /* safe too */
1172 logf("bufalloc: new hdl %d", h->id);
1173 return h->id;
1176 /* Close the handle. Return true for success and false for failure */
1177 bool bufclose(int handle_id)
1179 logf("bufclose(%d)", handle_id);
1181 LOGFQUEUE("buffering >| Q_CLOSE_HANDLE %d", handle_id);
1182 return queue_send(&buffering_queue, Q_CLOSE_HANDLE, handle_id);
1185 /* Set reading index in handle (relatively to the start of the file).
1186 Access before the available data will trigger a rebuffer.
1187 Return 0 for success and < 0 for failure:
1188 -1 if the handle wasn't found
1189 -2 if the new requested position was beyond the end of the file
1191 int bufseek(int handle_id, size_t newpos)
1193 struct memory_handle *h = find_handle(handle_id);
1194 if (!h)
1195 return ERR_HANDLE_NOT_FOUND;
1197 if (newpos > h->filesize) {
1198 /* access beyond the end of the file */
1199 return ERR_INVALID_VALUE;
1201 else if (newpos < h->offset || h->offset + h->available < newpos) {
1202 /* access before or after buffered data. A rebuffer is needed. */
1203 rebuffer_handle(handle_id, newpos);
1205 else {
1206 h->ridx = ringbuf_add(h->data, newpos - h->offset);
1208 return 0;
1211 /* Advance the reading index in a handle (relatively to its current position).
1212 Return 0 for success and < 0 for failure */
1213 int bufadvance(int handle_id, off_t offset)
1215 const struct memory_handle *h = find_handle(handle_id);
1216 if (!h)
1217 return ERR_HANDLE_NOT_FOUND;
1219 size_t newpos = h->offset + ringbuf_sub(h->ridx, h->data) + offset;
1220 return bufseek(handle_id, newpos);
1223 /* Used by bufread and bufgetdata to prepare the buffer and retrieve the
1224 * actual amount of data available for reading. This function explicitly
1225 * does not check the validity of the input handle. It does do range checks
1226 * on size and returns a valid (and explicit) amount of data for reading */
1227 static struct memory_handle *prep_bufdata(int handle_id, size_t *size,
1228 bool guardbuf_limit)
1230 struct memory_handle *h = find_handle(handle_id);
1231 if (!h)
1232 return NULL;
1234 size_t avail = ringbuf_sub(h->widx, h->ridx);
1236 if (avail == 0 && h->filerem == 0)
1238 /* File is finished reading */
1239 *size = 0;
1240 return h;
1243 if (*size == 0 || *size > avail + h->filerem)
1244 *size = avail + h->filerem;
1246 if (guardbuf_limit && h->type == TYPE_PACKET_AUDIO && *size > GUARD_BUFSIZE)
1248 logf("data request > guardbuf");
1249 /* If more than the size of the guardbuf is requested and this is a
1250 * bufgetdata, limit to guard_bufsize over the end of the buffer */
1251 *size = MIN(*size, buffer_len - h->ridx + GUARD_BUFSIZE);
1252 /* this ensures *size <= buffer_len - h->ridx + GUARD_BUFSIZE */
1255 if (h->filerem > 0 && avail < *size)
1257 /* Data isn't ready. Request buffering */
1258 buf_request_buffer_handle(handle_id);
1259 /* Wait for the data to be ready */
1262 sleep(1);
1263 /* it is not safe for a non-buffering thread to sleep while
1264 * holding a handle */
1265 h = find_handle(handle_id);
1266 if (!h)
1267 return NULL;
1268 avail = ringbuf_sub(h->widx, h->ridx);
1270 while (h->filerem > 0 && avail < *size);
1273 *size = MIN(*size,avail);
1274 return h;
1277 /* Copy data from the given handle to the dest buffer.
1278 Return the number of bytes copied or < 0 for failure (handle not found).
1279 The caller is blocked until the requested amount of data is available.
1281 ssize_t bufread(int handle_id, size_t size, void *dest)
1283 const struct memory_handle *h;
1284 size_t adjusted_size = size;
1286 h = prep_bufdata(handle_id, &adjusted_size, false);
1287 if (!h)
1288 return ERR_HANDLE_NOT_FOUND;
1290 if (h->ridx + adjusted_size > buffer_len)
1292 /* the data wraps around the end of the buffer */
1293 size_t read = buffer_len - h->ridx;
1294 memcpy(dest, &buffer[h->ridx], read);
1295 memcpy(dest+read, buffer, adjusted_size - read);
1297 else
1299 memcpy(dest, &buffer[h->ridx], adjusted_size);
1302 return adjusted_size;
1305 /* Update the "data" pointer to make the handle's data available to the caller.
1306 Return the length of the available linear data or < 0 for failure (handle
1307 not found).
1308 The caller is blocked until the requested amount of data is available.
1309 size is the amount of linear data requested. it can be 0 to get as
1310 much as possible.
1311 The guard buffer may be used to provide the requested size. This means it's
1312 unsafe to request more than the size of the guard buffer.
1314 ssize_t bufgetdata(int handle_id, size_t size, void **data)
1316 const struct memory_handle *h;
1317 size_t adjusted_size = size;
1319 h = prep_bufdata(handle_id, &adjusted_size, true);
1320 if (!h)
1321 return ERR_HANDLE_NOT_FOUND;
1323 if (h->ridx + adjusted_size > buffer_len)
1325 /* the data wraps around the end of the buffer :
1326 use the guard buffer to provide the requested amount of data. */
1327 size_t copy_n = h->ridx + adjusted_size - buffer_len;
1328 /* prep_bufdata ensures adjusted_size <= buffer_len - h->ridx + GUARD_BUFSIZE,
1329 so copy_n <= GUARD_BUFSIZE */
1330 memcpy(guard_buffer, (const unsigned char *)buffer, copy_n);
1333 if (data)
1334 *data = &buffer[h->ridx];
1336 return adjusted_size;
1339 ssize_t bufgettail(int handle_id, size_t size, void **data)
1341 size_t tidx;
1343 const struct memory_handle *h;
1345 h = find_handle(handle_id);
1347 if (!h)
1348 return ERR_HANDLE_NOT_FOUND;
1350 if (h->filerem)
1351 return ERR_HANDLE_NOT_DONE;
1353 /* We don't support tail requests of > guardbuf_size, for simplicity */
1354 if (size > GUARD_BUFSIZE)
1355 return ERR_INVALID_VALUE;
1357 tidx = ringbuf_sub(h->widx, size);
1359 if (tidx + size > buffer_len)
1361 size_t copy_n = tidx + size - buffer_len;
1362 memcpy(guard_buffer, (const unsigned char *)buffer, copy_n);
1365 *data = &buffer[tidx];
1366 return size;
1369 ssize_t bufcuttail(int handle_id, size_t size)
1371 struct memory_handle *h;
1372 size_t adjusted_size = size;
1374 h = find_handle(handle_id);
1376 if (!h)
1377 return ERR_HANDLE_NOT_FOUND;
1379 if (h->filerem)
1380 return ERR_HANDLE_NOT_DONE;
1382 if (h->available < adjusted_size)
1383 adjusted_size = h->available;
1385 h->available -= adjusted_size;
1386 h->filesize -= adjusted_size;
1387 h->widx = ringbuf_sub(h->widx, adjusted_size);
1388 if (h == cur_handle)
1389 buf_widx = h->widx;
1391 return adjusted_size;
1396 SECONDARY EXPORTED FUNCTIONS
1397 ============================
1399 buf_get_offset
1400 buf_handle_offset
1401 buf_request_buffer_handle
1402 buf_set_base_handle
1403 buf_used
1404 register_buffering_callback
1405 unregister_buffering_callback
1407 These functions are exported, to allow interaction with the buffer.
1408 They take care of the content of the structs, and rely on the linked list
1409 management functions for all the actual handle management work.
1412 /* Get a handle offset from a pointer */
1413 ssize_t buf_get_offset(int handle_id, void *ptr)
1415 const struct memory_handle *h = find_handle(handle_id);
1416 if (!h)
1417 return ERR_HANDLE_NOT_FOUND;
1419 return (size_t)ptr - (size_t)&buffer[h->ridx];
1422 ssize_t buf_handle_offset(int handle_id)
1424 const struct memory_handle *h = find_handle(handle_id);
1425 if (!h)
1426 return ERR_HANDLE_NOT_FOUND;
1427 return h->offset;
1430 void buf_request_buffer_handle(int handle_id)
1432 LOGFQUEUE("buffering >| Q_START_FILL %d",handle_id);
1433 queue_send(&buffering_queue, Q_START_FILL, handle_id);
1436 void buf_set_base_handle(int handle_id)
1438 LOGFQUEUE("buffering > Q_BASE_HANDLE %d", handle_id);
1439 queue_post(&buffering_queue, Q_BASE_HANDLE, handle_id);
1442 /* Return the amount of buffer space used */
1443 size_t buf_used(void)
1445 return BUF_USED;
1448 void buf_set_watermark(size_t bytes)
1450 conf_watermark = bytes;
1453 static void shrink_buffer_inner(struct memory_handle *h)
1455 if (h == NULL)
1456 return;
1458 shrink_buffer_inner(h->next);
1460 shrink_handle(h);
1463 static void shrink_buffer(void)
1465 logf("shrink_buffer()");
1466 shrink_buffer_inner(first_handle);
1469 void buffering_thread(void)
1471 bool filling = false;
1472 struct queue_event ev;
1474 while (true)
1476 if (!filling) {
1477 cancel_cpu_boost();
1480 queue_wait_w_tmo(&buffering_queue, &ev, filling ? 5 : HZ/2);
1482 switch (ev.id)
1484 case Q_START_FILL:
1485 LOGFQUEUE("buffering < Q_START_FILL %d", (int)ev.data);
1486 /* Call buffer callbacks here because this is one of two ways
1487 * to begin a full buffer fill */
1488 send_event(BUFFER_EVENT_BUFFER_LOW, 0);
1489 shrink_buffer();
1490 queue_reply(&buffering_queue, 1);
1491 filling |= buffer_handle((int)ev.data);
1492 break;
1494 case Q_BUFFER_HANDLE:
1495 LOGFQUEUE("buffering < Q_BUFFER_HANDLE %d", (int)ev.data);
1496 queue_reply(&buffering_queue, 1);
1497 buffer_handle((int)ev.data);
1498 break;
1500 case Q_RESET_HANDLE:
1501 LOGFQUEUE("buffering < Q_RESET_HANDLE %d", (int)ev.data);
1502 queue_reply(&buffering_queue, 1);
1503 reset_handle((int)ev.data);
1504 break;
1506 case Q_CLOSE_HANDLE:
1507 LOGFQUEUE("buffering < Q_CLOSE_HANDLE %d", (int)ev.data);
1508 queue_reply(&buffering_queue, close_handle((int)ev.data));
1509 break;
1511 case Q_HANDLE_ADDED:
1512 LOGFQUEUE("buffering < Q_HANDLE_ADDED %d", (int)ev.data);
1513 /* A handle was added: the disk is spinning, so we can fill */
1514 filling = true;
1515 break;
1517 case Q_BASE_HANDLE:
1518 LOGFQUEUE("buffering < Q_BASE_HANDLE %d", (int)ev.data);
1519 base_handle_id = (int)ev.data;
1520 break;
1522 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
1523 case SYS_USB_CONNECTED:
1524 LOGFQUEUE("buffering < SYS_USB_CONNECTED");
1525 usb_acknowledge(SYS_USB_CONNECTED_ACK);
1526 usb_wait_for_disconnect(&buffering_queue);
1527 break;
1528 #endif
1530 case SYS_TIMEOUT:
1531 LOGFQUEUE_SYS_TIMEOUT("buffering < SYS_TIMEOUT");
1532 break;
1535 update_data_counters();
1537 /* If the buffer is low, call the callbacks to get new data */
1538 if (num_handles > 0 && data_counters.useful <= conf_watermark)
1539 send_event(BUFFER_EVENT_BUFFER_LOW, 0);
1541 #if 0
1542 /* TODO: This needs to be fixed to use the idle callback, disable it
1543 * for simplicity until its done right */
1544 #if MEMORYSIZE > 8
1545 /* If the disk is spinning, take advantage by filling the buffer */
1546 else if (storage_disk_is_active() && queue_empty(&buffering_queue))
1548 if (num_handles > 0 && data_counters.useful <= high_watermark)
1549 send_event(BUFFER_EVENT_BUFFER_LOW, 0);
1551 if (data_counters.remaining > 0 && BUF_USED <= high_watermark)
1553 /* This is a new fill, shrink the buffer up first */
1554 if (!filling)
1555 shrink_buffer();
1556 filling = fill_buffer();
1557 update_data_counters();
1560 #endif
1561 #endif
1563 if (queue_empty(&buffering_queue)) {
1564 if (filling) {
1565 if (data_counters.remaining > 0 && BUF_USED < buffer_len)
1566 filling = fill_buffer();
1567 else if (data_counters.remaining == 0)
1568 filling = false;
1570 else if (ev.id == SYS_TIMEOUT)
1572 if (data_counters.remaining > 0 &&
1573 data_counters.useful <= conf_watermark) {
1574 shrink_buffer();
1575 filling = fill_buffer();
1582 void buffering_init(void)
1584 mutex_init(&llist_mutex);
1585 mutex_init(&llist_mod_mutex);
1586 #ifdef HAVE_PRIORITY_SCHEDULING
1587 /* This behavior not safe atm */
1588 mutex_set_preempt(&llist_mutex, false);
1589 mutex_set_preempt(&llist_mod_mutex, false);
1590 #endif
1592 conf_watermark = BUFFERING_DEFAULT_WATERMARK;
1594 queue_init(&buffering_queue, true);
1595 buffering_thread_id = create_thread( buffering_thread, buffering_stack,
1596 sizeof(buffering_stack), CREATE_THREAD_FROZEN,
1597 buffering_thread_name IF_PRIO(, PRIORITY_BUFFERING)
1598 IF_COP(, CPU));
1600 queue_enable_queue_send(&buffering_queue, &buffering_queue_sender_list,
1601 buffering_thread_id);
1604 /* Initialise the buffering subsystem */
1605 bool buffering_reset(char *buf, size_t buflen)
1607 /* Wraps of storage-aligned data must also be storage aligned,
1608 thus buf and buflen must be a aligned to an integer multiple of
1609 the storage alignment */
1610 STORAGE_ALIGN_BUFFER(buf, buflen);
1612 if (!buf || !buflen)
1613 return false;
1615 buffer = buf;
1616 buffer_len = buflen;
1617 guard_buffer = buf + buflen;
1619 buf_widx = 0;
1620 buf_ridx = 0;
1622 first_handle = NULL;
1623 cur_handle = NULL;
1624 cached_handle = NULL;
1625 num_handles = 0;
1626 base_handle_id = -1;
1628 /* Set the high watermark as 75% full...or 25% empty :) */
1629 #if MEMORYSIZE > 8
1630 high_watermark = 3*buflen / 4;
1631 #endif
1633 thread_thaw(buffering_thread_id);
1635 return true;
1638 void buffering_get_debugdata(struct buffering_debug *dbgdata)
1640 update_data_counters();
1641 dbgdata->num_handles = num_handles;
1642 dbgdata->data_rem = data_counters.remaining;
1643 dbgdata->wasted_space = data_counters.wasted;
1644 dbgdata->buffered_data = data_counters.buffered;
1645 dbgdata->useful_data = data_counters.useful;
1646 dbgdata->watermark = conf_watermark;