1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
25 #include "buffering.h"
43 #include "mp3_playback.h"
55 #define ata_disk_is_active() 1
59 #define GUARD_BUFSIZE (32*1024)
61 #define GUARD_BUFSIZE (8*1024)
64 /* Define LOGF_ENABLE to enable logf output in this file */
65 /*#define LOGF_ENABLE*/
68 /* macros to enable logf for queues
69 logging on SYS_TIMEOUT can be disabled */
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 */
77 #ifdef BUFFERING_LOGQUEUES
78 #define LOGFQUEUE logf
80 #define LOGFQUEUE(...)
83 #ifdef BUFFERING_LOGQUEUES_SYS_TIMEOUT
84 #define LOGFQUEUE_SYS_TIMEOUT logf
86 #define LOGFQUEUE_SYS_TIMEOUT(...)
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 */
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. */
137 static size_t conf_watermark
= 0; /* Level to trigger filebuf fill */
139 static size_t high_watermark
= 0; /* High watermark for rebuffer */
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
;
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 */
165 /* Messages available to communicate with the buffering thread */
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 */
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
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
224 static struct memory_handle
*add_handle(size_t data_size
, bool can_wrap
,
227 /* gives each handle a unique id */
228 static int cur_handle_id
= 0;
234 if (num_handles
>= BUF_MAX_HANDLES
)
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
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
);
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
),
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 */
267 } else if (!can_wrap
) {
268 overlap
= RINGBUF_ADD_CROSS(new_widx
, len
, buffer_len
- 1);
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
);
284 /* There is enough space for the required data, advance the buf_widx and
285 * initialize the struct */
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
;
301 /* the new handle is the first one */
302 first_handle
= new_handle
;
305 cur_handle
->next
= new_handle
;
307 cur_handle
= new_handle
;
309 mutex_unlock(&llist_mutex
);
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
)
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 */
327 buf_ridx
= buf_widx
= 0;
329 /* update buf_ridx to point to the new first handle */
330 buf_ridx
= (void *)first_handle
- (void *)buffer
;
333 struct memory_handle
*m
= first_handle
;
334 /* Find the previous handle */
335 while (m
&& m
->next
!= h
) {
338 if (m
&& m
->next
== h
) {
340 if (h
== cur_handle
) {
342 buf_widx
= cur_handle
->widx
;
345 mutex_unlock(&llist_mutex
);
350 /* Invalidate the cache to prevent it from keeping the old location of h */
351 if (h
== cached_handle
)
352 cached_handle
= NULL
;
356 mutex_unlock(&llist_mutex
);
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
)
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 */
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
) {
388 /* This condition can only be reached with !m or m->id == handle_id */
392 mutex_unlock(&llist_mutex
);
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
;
412 size_t final_delta
= *delta
;
415 if (h
== NULL
|| (src
= *h
) == NULL
)
418 size_to_move
= sizeof(struct memory_handle
) + data_size
;
420 /* Align to four bytes, down */
422 if (final_delta
< sizeof(struct memory_handle
)) {
423 /* It's not legal to move less than the size of the struct */
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);
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;
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
);
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
) {
467 struct memory_handle
*m
= first_handle
;
468 while (m
&& m
->next
!= src
) {
471 if (m
&& m
->next
== src
) {
474 mutex_unlock(&llist_mutex
);
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
)
489 size_t first_part
= size_to_move
- overlap
;
490 memmove(dest
, src
, first_part
);
491 memmove(buffer
, (const char *)src
+ first_part
, overlap
);
493 memmove(dest
, src
, size_to_move
);
496 /* Update the caller with the new location of h and the distance moved */
498 *delta
= final_delta
;
499 mutex_unlock(&llist_mutex
);
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
;
526 size_t remaining
= 0;
531 buffered
+= m
->available
;
532 wasted
+= RINGBUF_SUB(m
->ridx
, m
->data
);
533 remaining
+= m
->filerem
;
535 if (m
->id
== base_handle_id
)
539 useful
+= RINGBUF_SUB(m
->widx
, m
->ridx
);
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
);
565 if (h
->filerem
== 0) {
566 /* nothing left to buffer */
570 if (h
->fd
< 0) /* file closed, reopen */
573 h
->fd
= open(h
->path
, O_RDONLY
);
577 /* could not open the file, truncate it where it is */
578 h
->filesize
-= h
->filerem
;
584 lseek(h
->fd
, h
->offset
, SEEK_SET
);
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)
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
);
614 /* rc is the actual amount read */
615 int rc
= read(h
->fd
, &buffer
[h
->widx
], copy_n
);
619 /* Some kind of filesystem error, maybe recoverable if not codec */
620 if (h
->type
== TYPE_CODEC
) {
621 logf("Partial codec");
625 DEBUGF("File ended %ld bytes early\n", (long)h
->filerem
);
626 h
->filesize
-= h
->filerem
;
632 h
->widx
= RINGBUF_ADD(h
->widx
, rc
);
638 /* If this is a large file, see if we need to break or give the codec
640 if (h
->type
== TYPE_PACKET_AUDIO
&&
641 pcmbuf_is_lowdata() && !buffer_is_low())
650 if (!queue_empty(&buffering_queue
))
654 if (h
->filerem
== 0) {
655 /* finished buffering the file */
658 send_event(EVENT_HANDLE_FINISHED
, &h
->id
);
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
);
678 h
->filerem
= h
->filesize
- h
->offset
;
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
);
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
;
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
);
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 */
737 /* rm_handle returns true unless the handle somehow persists after exit */
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
)
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
))
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
)];
782 /* only move the handle struct */
783 delta
= RINGBUF_SUB(h
->ridx
, h
->data
);
784 if (!move_handle(&h
, &delta
, 0, true))
787 h
->data
= RINGBUF_ADD(h
->data
, delta
);
788 h
->available
-= 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
;
801 while (queue_empty(&buffering_queue
) && m
) {
802 if (m
->filerem
> 0) {
803 if (!buffer_handle(m
->id
)) {
817 /* only spin the disk down if the filling wasn't interrupted by an
818 event arriving in the queue. */
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
)
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
;
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);
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
);
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
)
886 struct memory_handle
*h
= add_handle(size
-adjusted_offset
, can_wrap
, false);
889 DEBUGF("bufopen: failed to add handle\n");
891 return ERR_BUFFER_FULL
;
894 strncpy(h
->path
, file
, MAX_PATH
);
895 h
->offset
= adjusted_offset
;
901 if (type
== TYPE_BITMAP
)
903 /* Bitmap file: we load the data instead of the file */
905 mutex_lock(&llist_mutex
); /* Lock because load_bitmap yields */
906 rc
= load_bitmap(fd
);
911 mutex_unlock(&llist_mutex
);
912 return ERR_FILE_ERROR
;
917 h
->widx
= buf_widx
+ rc
; /* safe because the data doesn't wrap */
918 buf_widx
+= rc
; /* safe too */
919 mutex_unlock(&llist_mutex
);
924 h
->filerem
= size
- adjusted_offset
;
930 if (type
== TYPE_CUESHEET
) {
932 /* Immediately start buffering those */
933 LOGFQUEUE("buffering >| Q_BUFFER_HANDLE");
934 queue_send(&buffering_queue
, Q_BUFFER_HANDLE
, h
->id
);
936 /* Other types will get buffered in the course of normal operations */
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
);
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);
961 return ERR_BUFFER_FULL
;
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
);
969 memcpy(&buffer
[buf_widx
], src
, size
);
979 h
->widx
= buf_widx
+ size
; /* this is safe because the data doesn't wrap */
984 buf_widx
+= size
; /* safe too */
986 logf("bufalloc: new hdl %d", 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
);
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
);
1020 h
->ridx
= RINGBUF_ADD(h
->data
, newpos
- h
->offset
);
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
);
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
);
1048 size_t avail
= RINGBUF_SUB(h
->widx
, h
->ridx
);
1050 if (avail
== 0 && h
->filerem
== 0)
1052 /* File is finished reading */
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 */
1077 /* it is not safe for a non-buffering thread to sleep while
1078 * holding a handle */
1079 h
= find_handle(handle_id
);
1082 avail
= RINGBUF_SUB(h
->widx
, h
->ridx
);
1084 while (h
->filerem
> 0 && avail
< *size
);
1087 *size
= MIN(*size
,avail
);
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);
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
);
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
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
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);
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
);
1148 *data
= &buffer
[h
->ridx
];
1150 return adjusted_size
;
1153 ssize_t
bufgettail(int handle_id
, size_t size
, void **data
)
1157 const struct memory_handle
*h
;
1159 h
= find_handle(handle_id
);
1162 return ERR_HANDLE_NOT_FOUND
;
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
];
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
);
1191 return ERR_HANDLE_NOT_FOUND
;
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
)
1205 return adjusted_size
;
1210 SECONDARY EXPORTED FUNCTIONS
1211 ============================
1215 buf_request_buffer_handle
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
);
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
);
1240 return ERR_HANDLE_NOT_FOUND
;
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)
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
)
1273 shrink_buffer_inner(h
->next
);
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
;
1295 queue_wait_w_tmo(&buffering_queue
, &ev
, filling
? 5 : HZ
/2);
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);
1305 queue_reply(&buffering_queue
, 1);
1306 filling
|= buffer_handle((int)ev
.data
);
1309 case Q_BUFFER_HANDLE
:
1310 LOGFQUEUE("buffering < Q_BUFFER_HANDLE");
1311 queue_reply(&buffering_queue
, 1);
1312 buffer_handle((int)ev
.data
);
1315 case Q_RESET_HANDLE
:
1316 LOGFQUEUE("buffering < Q_RESET_HANDLE");
1317 queue_reply(&buffering_queue
, 1);
1318 reset_handle((int)ev
.data
);
1321 case Q_CLOSE_HANDLE
:
1322 LOGFQUEUE("buffering < Q_CLOSE_HANDLE");
1323 queue_reply(&buffering_queue
, close_handle((int)ev
.data
));
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 */
1333 LOGFQUEUE("buffering < Q_BASE_HANDLE");
1334 base_handle_id
= (int)ev
.data
;
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
;
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
);
1357 LOGFQUEUE_SYS_TIMEOUT("buffering < SYS_TIMEOUT");
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);
1368 /* TODO: This needs to be fixed to use the idle callback, disable it
1369 * for simplicity until its done right */
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 */
1382 filling
= fill_buffer();
1383 update_data_counters();
1389 if (queue_empty(&buffering_queue
)) {
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
) {
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);
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
)
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
)
1433 buffer_len
= buflen
;
1434 guard_buffer
= buf
+ buflen
;
1439 first_handle
= NULL
;
1441 cached_handle
= NULL
;
1443 base_handle_id
= -1;
1445 /* Set the high watermark as 75% full...or 25% empty :) */
1447 high_watermark
= 3*buflen
/ 4;
1450 thread_thaw(buffering_thread_p
);
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
;