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"
44 #include "mp3_playback.h"
54 #define ata_disk_is_active() 1
58 #define GUARD_BUFSIZE (32*1024)
60 #define GUARD_BUFSIZE (8*1024)
63 /* amount of data to read in one read() call */
64 #define AUDIO_DEFAULT_FILECHUNK (1024*32)
66 /* Ring buffer helper macros */
67 /* Buffer pointer (p) plus value (v), wrapped if necessary */
68 #define RINGBUF_ADD(p,v) ((p+v)<buffer_len ? p+v : p+v-buffer_len)
69 /* Buffer pointer (p) minus value (v), wrapped if necessary */
70 #define RINGBUF_SUB(p,v) ((p>=v) ? p-v : p+buffer_len-v)
71 /* How far value (v) plus buffer pointer (p1) will cross buffer pointer (p2) */
72 #define RINGBUF_ADD_CROSS(p1,v,p2) \
73 ((p1<p2) ? (int)(p1+v)-(int)p2 : (int)(p1+v-p2)-(int)buffer_len)
74 /* Bytes available in the buffer */
75 #define BUF_USED RINGBUF_SUB(buf_widx, buf_ridx)
77 struct memory_handle
{
78 int id
; /* A unique ID for the handle */
82 size_t data
; /* Start index of the handle's data buffer */
83 size_t ridx
; /* Current read pointer, relative to the main buffer */
84 size_t widx
; /* Current write pointer */
85 size_t filesize
; /* File total length */
86 size_t filerem
; /* Remaining bytes of file NOT in buffer */
87 size_t available
; /* Available bytes to read from buffer */
88 size_t offset
; /* Offset at which we started reading the file */
89 struct memory_handle
*next
;
91 /* at all times, we have: filesize == offset + available + filerem */
95 static char *guard_buffer
;
97 static size_t buffer_len
;
99 static size_t buf_widx
; /* current writing position */
100 static size_t buf_ridx
; /* current reading position */
101 /* buf_*idx are values relative to the buffer, not real pointers. */
103 static size_t conf_filechunk
;
105 /* current memory handle in the linked list. NULL when the list is empty. */
106 static struct memory_handle
*cur_handle
;
107 /* first memory handle in the linked list. NULL when the list is empty. */
108 static struct memory_handle
*first_handle
;
110 static int num_handles
; /* number of handles in the list */
112 /* Handle cache (makes find_handle faster).
113 These need to be global so that move_handle can invalidate them. */
114 static int cached_handle_id
= -1;
115 static struct memory_handle
*cached_handle
= NULL
;
119 LINKED LIST MANAGEMENT
120 ======================
122 add_handle : Add a handle to the list
123 rm_handle : Remove a handle from the list
124 find_handle : Get a handle pointer from an ID
125 move_handle : Move a handle in the buffer (with or without its data)
127 These functions only handle the linked list structure. They don't touch the
128 contents of the struct memory_handle headers. They also change the buf_*idx
129 pointers when necessary and manage the handle IDs.
131 The first and current (== last) handle are kept track of.
132 A new handle is added at buf_widx and becomes the current one.
133 buf_widx always points to the current writing position for the current handle
134 buf_ridx always points to the location of the first handle.
135 buf_ridx == buf_widx means the buffer is empty.
139 /* Add a new handle to the linked list and return it. It will have become the
140 new current handle. The handle will reserve "data_size" bytes or if that's
141 not possible, decrease "data_size" to allow adding the handle. */
142 static struct memory_handle
*add_handle(size_t *data_size
)
144 /* this will give each handle a unique id */
145 static int cur_handle_id
= 1;
147 /* make sure buf_widx is 32-bit aligned so that the handle struct is,
148 but before that we check we can actually align. */
149 if (RINGBUF_ADD_CROSS(buf_widx
, 3, buf_ridx
) >= 0) {
152 buf_widx
= (RINGBUF_ADD(buf_widx
, 3)) & ~3;
154 size_t len
= (data_size
? *data_size
: 0)
155 + sizeof(struct memory_handle
);
157 /* check that we actually can add the handle and its data */
158 int overlap
= RINGBUF_ADD_CROSS(buf_widx
, len
, buf_ridx
);
160 *data_size
-= overlap
;
163 if (len
< sizeof(struct memory_handle
)) {
164 /* There isn't even enough space to write the struct */
168 struct memory_handle
*new_handle
=
169 (struct memory_handle
*)(&buffer
[buf_widx
]);
171 /* only advance the buffer write index of the size of the struct */
172 buf_widx
= RINGBUF_ADD(buf_widx
, sizeof(struct memory_handle
));
175 /* the new handle is the first one */
176 first_handle
= new_handle
;
180 cur_handle
->next
= new_handle
;
183 cur_handle
= new_handle
;
184 cur_handle
->id
= cur_handle_id
++;
185 cur_handle
->next
= NULL
;
190 /* Delete a given memory handle from the linked list
191 and return true for success. Nothing is actually erased from memory. */
192 static bool rm_handle(struct memory_handle
*h
)
194 if (h
== first_handle
) {
195 first_handle
= h
->next
;
196 if (h
== cur_handle
) {
197 /* h was the first and last handle: the buffer is now empty */
201 /* update buf_ridx to point to the new first handle */
202 buf_ridx
= (void *)first_handle
- (void *)buffer
;
205 struct memory_handle
*m
= first_handle
;
206 while (m
&& m
->next
!= h
) {
209 if (h
&& m
&& m
->next
== h
) {
211 if (h
== cur_handle
) {
223 /* Return a pointer to the memory handle of given ID.
224 NULL if the handle wasn't found */
225 static struct memory_handle
*find_handle(int handle_id
)
227 /* simple caching because most of the time the requested handle
228 will either be the same as the last, or the one after the last */
231 if (cached_handle_id
== handle_id
&&
232 cached_handle_id
== cached_handle
->id
)
233 return cached_handle
;
234 else if (cached_handle
->next
&& (cached_handle
->next
->id
== handle_id
))
236 /* JD's quick testing showd this block was only entered
237 2/1971 calls to find_handle.
238 8/1971 calls to find_handle resulted in a cache miss */
239 cached_handle
= cached_handle
->next
;
240 cached_handle_id
= handle_id
;
241 return cached_handle
;
245 struct memory_handle
*m
= first_handle
;
246 while (m
&& m
->id
!= handle_id
) {
249 cached_handle_id
= handle_id
;
251 return (m
&& m
->id
== handle_id
) ? m
: NULL
;
254 /* Move a memory handle and data_size of its data of delta.
255 Return a pointer to the new location of the handle.
256 delta is the value of which to move the struct data.
257 data_size is the amount of data to move along with the struct. */
258 static struct memory_handle
*move_handle(struct memory_handle
*h
,
259 size_t *delta
, size_t data_size
)
262 /* aligning backwards would yield a negative result,
263 and moving the handle of such a small amount is a waste
267 /* make sure delta is 32-bit aligned so that the handle struct is. */
268 *delta
= (*delta
- 3) & ~3;
270 size_t newpos
= RINGBUF_ADD((void *)h
- (void *)buffer
, *delta
);
272 struct memory_handle
*dest
= (struct memory_handle
*)(&buffer
[newpos
]);
274 /* Invalidate the cache to prevent it from keeping the old location of h */
275 if (h
== cached_handle
)
276 cached_handle
= NULL
;
278 /* the cur_handle pointer might need updating */
279 if (h
== cur_handle
) {
283 if (h
== first_handle
) {
287 struct memory_handle
*m
= first_handle
;
288 while (m
&& m
->next
!= h
) {
291 if (h
&& m
&& m
->next
== h
) {
298 memmove(dest
, h
, sizeof(struct memory_handle
) + data_size
);
305 BUFFER SPACE MANAGEMENT
306 =======================
308 buffer_handle : Buffer data for a handle
309 free_buffer : Free buffer space by moving a handle
310 fill_buffer : Call buffer_handle for all handles that have data to buffer
311 can_add_handle : Indicate whether it's safe to add a handle.
312 data_rem : Total amount of data needing to be buffered
313 wasted_space : Total amount of space available for freeing
315 These functions are used by the buffering thread to manage buffer space.
318 /* Buffer data for the given handle. Return the amount of data buffered
319 or -1 if the handle wasn't found */
320 static ssize_t
buffer_handle(int handle_id
)
322 DEBUGF("buffer_handle(%d)\n", handle_id
);
323 struct memory_handle
*h
= find_handle(handle_id
);
327 if (h
->filerem
== 0) {
328 /* nothing left to buffer */
332 if (h
->fd
< 0) /* file closed, reopen */
335 h
->fd
= open(h
->path
, O_RDONLY
);
343 lseek(h
->fd
, h
->offset
, SEEK_SET
);
347 while (h
->filerem
> 0)
349 /* max amount to copy */
350 size_t copy_n
= MIN( MIN(h
->filerem
, conf_filechunk
),
351 buffer_len
- h
->widx
);
353 /* stop copying if it would overwrite the reading position
354 or the next handle */
355 if (RINGBUF_ADD_CROSS(h
->widx
, copy_n
, buf_ridx
) >= 0 || (h
->next
&&
356 RINGBUF_ADD_CROSS(h
->widx
, copy_n
, (unsigned)
357 ((void *)h
->next
- (void *)buffer
)) > 0))
360 /* rc is the actual amount read */
361 int rc
= read(h
->fd
, &buffer
[h
->widx
], copy_n
);
365 if (h
->type
== TYPE_CODEC
) {
366 DEBUGF("Partial codec\n");
370 DEBUGF("File ended %ld bytes early\n", (long)h
->filerem
);
371 h
->filesize
-= h
->filerem
;
377 h
->widx
= RINGBUF_ADD(h
->widx
, rc
);
385 if (h
->filerem
== 0) {
386 /* finished buffering the file */
391 DEBUGF("buffered %ld bytes (%ld of %ld available, rem: %ld, off: %ld)\n",
392 ret
, h
->available
, h
->filesize
, h
->filerem
, h
->offset
);
397 /* Free buffer space by moving the handle struct right before the useful
398 part of its data buffer or by moving all the data. */
399 static void free_buffer(int handle_id
)
401 struct memory_handle
*h
= find_handle(handle_id
);
406 /* The value of delta might change for alignment reasons */
408 if (h
->next
&& (h
->type
== TYPE_ID3
|| h
->type
== TYPE_CUESHEET
||
409 h
->type
== TYPE_IMAGE
) && h
->filerem
== 0 )
411 /* metadata handle: we can move all of it */
412 delta
= RINGBUF_SUB( (unsigned)((void *)h
->next
- (void *)buffer
),
413 h
->data
) - h
->available
;
414 h
= move_handle(h
, &delta
, h
->available
);
416 h
->data
= RINGBUF_ADD(h
->data
, delta
);
417 h
->ridx
= RINGBUF_ADD(h
->ridx
, delta
);
418 h
->widx
= RINGBUF_ADD(h
->widx
, delta
);
420 /* when moving a struct mp3entry we need to readjust its pointers. */
421 if (h
->type
== TYPE_ID3
&& h
->filesize
== sizeof(struct mp3entry
)) {
422 adjust_mp3entry((struct mp3entry
*)&buffer
[h
->data
],
423 (void *)&buffer
[h
->data
],
424 (void *)&buffer
[RINGBUF_SUB(h
->data
, delta
)]);
427 DEBUGF("free_buffer(%d): metadata, moved by %ld bytes\n",
432 /* only move the handle struct */
433 delta
= RINGBUF_SUB(h
->ridx
, h
->data
);
434 h
= move_handle(h
, &delta
, 0);
436 h
->data
= RINGBUF_ADD(h
->data
, delta
);
437 h
->available
-= delta
;
439 DEBUGF("free_buffer(%d): audio, %ld bytes freed\n", handle_id
, delta
);
443 /* Fill the buffer by buffering as much data as possible for handles that still
444 have data left to buffer */
445 static void fill_buffer(void)
447 DEBUGF("fill buffer()\n");
448 struct memory_handle
*m
= first_handle
;
450 if (m
->filerem
> 0) {
451 buffer_handle(m
->id
);
457 /* Check whether it's safe to add a new handle and reserve space to let the
458 current one finish buffering its data. Used by bufopen and bufgetdata as
459 a preliminary check before even trying to physically add the handle.
460 Returns true if it's ok to add a new handle, false if not.
462 static bool can_add_handle(void)
464 if (cur_handle
&& cur_handle
->filerem
> 0) {
465 /* the current handle hasn't finished buffering. We can only add
466 a new one if there is already enough free space to finish
468 if (cur_handle
->filerem
< (buffer_len
- BUF_USED
)) {
469 /* Before adding the new handle we reserve some space for the
470 current one to finish buffering its data. */
471 buf_widx
= RINGBUF_ADD(buf_widx
, cur_handle
->filerem
);
480 /* Return the total amount of data left to be buffered for all the handles */
481 static size_t data_rem(void)
485 struct memory_handle
*m
= first_handle
;
494 /* Return the amount of data we have but don't need anymore. This data can be
495 safely erased to reclaim buffer space. */
496 static size_t wasted_space(void)
500 struct memory_handle
*m
= first_handle
;
502 ret
+= RINGBUF_SUB(m
->ridx
, m
->data
);
511 BUFFERING API FUNCTIONS
512 =======================
514 bufopen : Request the opening of a new handle for a file
515 bufalloc : Open a new handle for data other than a file.
516 bufclose : Close an open handle
517 bufseek : Set the read pointer in a handle
518 bufadvance : Move the read pointer in a handle
519 bufread : Copy data from a handle into a given buffer
520 bufgetdata : Give a pointer to the handle's data
521 bufused : Return the amount of buffer space used
523 These functions are exported, to allow interaction with the buffer.
524 They take care of the content of the structs, and rely on the linked list
525 management functions for all the actual handle management work.
529 /* Request a file be buffered
530 filename: name of the file to open
531 offset: offset at which to start buffering the file, useful when the first
532 (offset-1) bytes of the file aren't needed.
533 return value: <0 if the file cannot be opened, or one file already
534 queued to be opened, otherwise the handle for the file in the buffer
536 int bufopen(char *file
, size_t offset
, enum data_type type
)
538 if (!can_add_handle())
541 int fd
= open(file
, O_RDONLY
);
545 size_t size
= filesize(fd
) - offset
;
547 if (type
!= TYPE_AUDIO
&&
548 size
+ sizeof(struct memory_handle
) > buffer_len
- buf_widx
)
550 /* for types other than audio, the data can't wrap */
554 DEBUGF("bufopen: %s (offset: %ld) (%ld bytes needed)...\n",
557 struct memory_handle
*h
= add_handle(&size
);
560 DEBUGF("failed to add handle\n");
565 if (offset
) lseek(fd
, offset
, SEEK_SET
);
566 strncpy(h
->path
, file
, MAX_PATH
);
568 h
->filesize
= filesize(fd
);
569 h
->filerem
= h
->filesize
- offset
;
577 DEBUGF("allocated %ld bytes. ID: %d\n", size
, h
->id
);
579 if (type
== TYPE_CODEC
|| type
== TYPE_CUESHEET
|| type
== TYPE_IMAGE
) {
580 /* Immediately buffer those */
581 ssize_t ret
= buffer_handle(h
->id
);
583 /* Check that we got the complete file */
584 if ((size_t)ret
!= h
->filesize
) {
593 /* Open a new handle from data that isn't in a file.
594 src is the source buffer from which to copy data. It can be NULL to simply
595 reserve buffer space.
596 size is the requested size. The call will only be successful if the
597 requested amount of data can entirely fit in the buffer without wrapping.
598 Return value is the handle id for success or <0 for failure.
600 int bufalloc(void *src
, size_t size
, enum data_type type
)
602 if (!can_add_handle())
605 if (size
+ sizeof(struct memory_handle
) > buffer_len
- buf_widx
)
606 /* The data would need to wrap. */
609 size_t allocsize
= size
;
610 struct memory_handle
*h
= add_handle(&allocsize
);
612 if (!h
|| allocsize
!= size
)
616 if (type
== TYPE_ID3
&& size
== sizeof(struct mp3entry
)) {
617 /* specially take care of struct mp3entry */
618 copy_mp3entry((struct mp3entry
*)&buffer
[buf_widx
],
619 (struct mp3entry
*)src
);
621 memcpy(&buffer
[buf_widx
], src
, size
);
636 buf_widx
= RINGBUF_ADD(buf_widx
, size
);
641 /* Close the handle. Return 0 for success and < 0 for failure */
642 int bufclose(int handle_id
)
644 DEBUGF("bufclose(%d)\n", handle_id
);
645 struct memory_handle
*h
= find_handle(handle_id
);
653 /* Set reading index in handle (relatively to the start of the file).
654 Access before the available data will trigger a rebuffer.
656 TODO: Maybe force an immediate rebuffer by calling buffer_handle() ?
657 Return 0 for success and < 0 for failure:
658 -1 if the handle wasn't found
659 -2 if there is no data available at the new position
660 (the reading index is still moved)
661 -3 if the new requested position was beyond the end of the file
663 int bufseek(int handle_id
, size_t newpos
)
666 struct memory_handle
*h
= find_handle(handle_id
);
670 if (newpos
> h
->filesize
) {
671 /* access beyond the end of the file */
675 else if (newpos
< h
->offset
) {
676 /* access before what we currently have. A rebuffer is needed. */
679 h
->filerem
= h
->filesize
- newpos
;
680 /* having changed filerem should be enough to trigger the rebuffer. */
685 else if (newpos
> h
->offset
+ h
->available
) {
686 /* data isn't available yet. */
690 h
->ridx
= RINGBUF_ADD(h
->data
, newpos
);
694 /* Advance the reading index in a handle (relatively to its current position).
695 Return 0 for success and < 0 for failure
696 TODO: Add some rebuffering like in bufseek */
697 int bufadvance(int handle_id
, off_t offset
)
699 struct memory_handle
*h
= find_handle(handle_id
);
705 /* check for access beyond what's available */
706 if ((size_t)offset
> (h
->available
- RINGBUF_SUB(h
->ridx
, h
->data
)))
709 h
->ridx
= RINGBUF_ADD(h
->ridx
, offset
);
713 /* check for access before what's available */
714 if ((size_t)(-offset
) > RINGBUF_SUB(h
->ridx
, h
->data
))
717 h
->ridx
= RINGBUF_SUB(h
->ridx
, (size_t)(-offset
));
723 /* Copy data from the given handle to the dest buffer.
724 Return the number of bytes copied or < 0 for failure. */
725 ssize_t
bufread(int handle_id
, size_t size
, char *dest
)
727 struct memory_handle
*h
= find_handle(handle_id
);
728 size_t buffered_data
;
732 if (h
->available
< size
&& h
->filerem
> 0) /* Data isn't ready */
735 if (h
->available
== 0 && h
->filerem
== 0) /* File is finished reading */
738 buffered_data
= MIN(size
, h
->available
- RINGBUF_SUB(h
->ridx
, h
->data
));
740 if (h
->ridx
+ buffered_data
> buffer_len
)
742 /* the data wraps around the end of the buffer */
743 size_t read
= buffer_len
- h
->ridx
;
744 memcpy(dest
, &buffer
[h
->ridx
], read
);
745 memcpy(dest
+read
, buffer
, buffered_data
- read
);
747 else memcpy(dest
, &buffer
[h
->ridx
], buffered_data
);
749 return buffered_data
;
752 /* Update the "data" pointer to make the handle's data available to the caller.
753 Return the length of the available linear data or < 0 for failure.
754 size is the amount of linear data requested. it can be 0 to get as
756 The guard buffer may be used to provide the requested size */
757 ssize_t
bufgetdata(int handle_id
, size_t size
, unsigned char **data
)
759 struct memory_handle
*h
= find_handle(handle_id
);
763 if (h
->available
< size
&& h
->filerem
> 0) /* Data isn't ready */
766 if (h
->available
== 0 && h
->filerem
== 0) /* File is finished reading */
771 if (h
->ridx
+ size
> buffer_len
&&
772 h
->available
- RINGBUF_SUB(h
->ridx
, h
->data
) >= size
)
774 /* the data wraps around the end of the buffer :
775 use the guard buffer to provide the requested amount of data. */
776 size_t copy_n
= MIN(h
->ridx
+ size
- buffer_len
, GUARD_BUFSIZE
);
777 memcpy(guard_buffer
, (unsigned char *)buffer
, copy_n
);
778 ret
= buffer_len
- h
->ridx
+ copy_n
;
779 DEBUGF("used the guard buffer to complete\n");
783 ret
= MIN(h
->available
- RINGBUF_SUB(h
->ridx
, h
->data
),
784 buffer_len
- h
->ridx
);
787 *data
= (unsigned char *)&buffer
[h
->ridx
];
789 /* DEBUGF("bufgetdata(%d): h->ridx=%ld, ret=%ld\n", handle_id,
790 (long)h->ridx, ret); */
794 /* Return the amount of buffer space used */
800 bool buffering_init(char *filebuf
, size_t filebuflen
)
802 if (!filebuf
|| !filebuflen
)
806 buffer_len
= filebuflen
;
807 guard_buffer
= buffer
+ buffer_len
;
815 conf_filechunk
= AUDIO_DEFAULT_FILECHUNK
;
820 void buffering_thread(void)
824 if (data_rem() > 0 && wasted_space() > buffer_len
/5) {
825 DEBUGF("there is %ld bytes of wasted space\n", wasted_space());
827 /* free buffer from outdated audio data */
828 struct memory_handle
*m
= first_handle
;
830 if (m
->type
== TYPE_AUDIO
)
835 /* free buffer by moving metadata */
838 if (m
->type
!= TYPE_AUDIO
)
845 if (data_rem() > 0 && BUF_USED
< 3*buffer_len
/4 &&
846 ata_disk_is_active())
848 DEBUGF("%ld bytes left to buffer and the buffer is low\n",