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 struct plugin_api
* rb
;
27 #define GUARD_SIZE (32*1024)
29 /* amount of data to read in one read() call */
30 #define AUDIO_DEFAULT_FILECHUNK (1024*32)
32 /* Ring buffer helper macros */
33 /* Buffer pointer (p) plus value (v), wrapped if necessary */
34 #define RINGBUF_ADD(p,v) ((p+v)<buffer_len ? p+v : p+v-buffer_len)
35 /* Buffer pointer (p) minus value (v), wrapped if necessary */
36 #define RINGBUF_SUB(p,v) ((p>=v) ? p-v : p+buffer_len-v)
37 /* How far value (v) plus buffer pointer (p1) will cross buffer pointer (p2) */
38 #define RINGBUF_ADD_CROSS(p1,v,p2) \
39 ((p1<p2) ? (int)(p1+v)-(int)p2 : (int)(p1+v-p2)-(int)buffer_len)
40 /* Bytes available in the buffer */
41 #define BUF_USED RINGBUF_SUB(buf_widx, buf_ridx)
43 #ifdef ROCKBOX_HAS_LOGF
44 #define DEBUGF rb->logf
55 struct memory_handle
{
56 int id
; /* A unique ID for the handle */
57 //enum data_type type;
58 //enum data_state state;
61 size_t data
; /* Start index of the handle's data buffer */
62 size_t data_len
; /* Length of the data buffer for the handle */
63 size_t ridx
; /* Current read pointer, relative to the main buffer */
64 size_t widx
; /* Current write pointer */
65 size_t filesize
; /* File total length */
66 size_t filerem
; /* Remaining bytes of file NOT in buffer */
67 size_t available
; /* Available bytes to read from buffer */
68 size_t offset
; /* Offset at which we started reading the file */
69 struct memory_handle
*next
;
74 static char *guard_buffer
;
76 static size_t buffer_len
;
77 static size_t buf_widx
;
78 static size_t buf_ridx
;
80 static size_t conf_filechunk
;
82 /* current memory handle in the linked list. NULL when the list is empty. */
83 static struct memory_handle
*cur_handle
;
84 /* first memory handle in the linked list. NULL when the list is empty. */
85 static struct memory_handle
*first_handle
;
86 static int num_handles
;
88 static void graph_view(int width
);
91 /* add a new handle to the linked list and return it. It will have become the
92 new current handle. The handle will reserve "data_size" bytes or if that's
93 not possible, decrease "data_size" to allow adding the handle. */
94 static struct memory_handle
*add_handle(size_t *data_size
)
96 /* this will give each handle a unique id */
97 static int cur_handle_id
= 1;
99 /* make sure buf_widx is 32-bit aligned so that the handle struct is,
100 but before that we check we can actually align. */
101 if (RINGBUF_ADD_CROSS(buf_widx
, 3, buf_ridx
) >= 0) {
104 buf_widx
= (RINGBUF_ADD(buf_widx
, 3)) & ~3;
106 size_t len
= (data_size
? *data_size
: 0)
107 + sizeof(struct memory_handle
);
109 /* check that we actually can add the handle and its data */
110 int overlap
= RINGBUF_ADD_CROSS(buf_widx
, len
, buf_ridx
);
112 *data_size
-= overlap
;
115 if (len
< sizeof(struct memory_handle
)) {
119 struct memory_handle
*new_handle
= (struct memory_handle
*)(&buffer
[buf_widx
]);
121 /* only advance the buffer write index of the size of the struct */
122 buf_widx
= RINGBUF_ADD(buf_widx
, sizeof(struct memory_handle
));
125 /* the new handle is the first one */
126 first_handle
= new_handle
;
130 cur_handle
->next
= new_handle
;
133 cur_handle
= new_handle
;
134 cur_handle
->id
= cur_handle_id
++;
135 cur_handle
->next
= NULL
;
140 /* delete a given memory handle from the linked list
141 and return true for success. Nothing is actually erased from memory. */
142 static bool rm_handle(struct memory_handle
*h
)
144 if (h
== first_handle
) {
145 first_handle
= h
->next
;
146 if (h
== cur_handle
) {
147 DEBUGF("removing the first and last handle\n");
148 /* h was the first and last handle */
152 buf_ridx
= (void *)first_handle
- (void *)buffer
;
155 struct memory_handle
*m
= first_handle
;
156 while (m
&& m
->next
!= h
) {
159 if (h
&& m
&& m
->next
== h
) {
161 if (h
== cur_handle
) {
173 /* these are unfortunalty needed to be global
174 so move_handle can invalidate them */
175 static int cached_handle_id
= -1;
176 static struct memory_handle
*cached_handle
= NULL
;
178 /* Return a pointer to the memory handle of given ID.
179 NULL if the handle wasn't found */
180 static struct memory_handle
*find_handle(int handle_id
)
182 /* simple caching because most of the time the requested handle
183 will either be the same as the last, or the one after the last */
186 if (cached_handle_id
== handle_id
&& cached_handle_id
== cached_handle
->id
)
187 return cached_handle
;
188 else if (cached_handle
->next
&& (cached_handle
->next
->id
== handle_id
))
190 /* JD's quick testing showd this block was only entered
191 2/1971 calls to find_handle.
192 8/1971 calls to find_handle resulted in a cache miss */
193 cached_handle
= cached_handle
->next
;
194 cached_handle_id
= handle_id
;
195 return cached_handle
;
199 struct memory_handle
*m
= first_handle
;
200 while (m
&& m
->id
!= handle_id
) {
203 cached_handle_id
= handle_id
;
205 return (m
&& m
->id
== handle_id
) ? m
: NULL
;
208 /* Move a memory handle to newpos.
209 Return a pointer to the new location of the handle */
210 static struct memory_handle
*move_handle(size_t *delta
, struct memory_handle
*h
)
213 /* aligning backwards would yield a negative result,
214 and moving the handle of such a small amount is a waste
218 /* make sure delta is 32-bit aligned so that the handle struct is. */
219 *delta
= (*delta
- 3) & ~3;
221 size_t newpos
= RINGBUF_ADD((void *)h
- (void *)buffer
, *delta
);
223 struct memory_handle
*dest
= (struct memory_handle
*)(&buffer
[newpos
]);
225 /* Invalidate the cache to prevent it from keeping the old location of h */
226 if (h
== cached_handle
)
227 cached_handle
= NULL
;
229 /* the cur_handle pointer might need updating */
230 if (h
== cur_handle
) {
234 if (h
== first_handle
) {
238 struct memory_handle
*m
= first_handle
;
239 while (m
&& m
->next
!= h
) {
242 if (h
&& m
&& m
->next
== h
) {
249 rb
->memmove(dest
, h
, sizeof(struct memory_handle
));
254 /* Buffer data for the given handle. Return the amount of data buffered
255 or -1 if the handle wasn't found */
256 static ssize_t
buffer_handle(int handle_id
)
258 DEBUGF("buffer_handle(%d)\n", handle_id
);
259 struct memory_handle
*h
= find_handle(handle_id
);
263 if (h
->filerem
== 0) {
264 /* nothing left to buffer */
268 if (h
->fd
< 0) /* file closed, reopen */
271 h
->fd
= rb
->open(h
->path
, O_RDONLY
);
279 rb
->lseek(h
->fd
, h
->offset
, SEEK_SET
);
283 while (h
->filerem
> 0)
285 //DEBUGF("h: %d\n", (void *)h - (void *)buffer);
286 //DEBUGF("buf_widx: %ld\n", (long)buf_widx);
287 /* max amount to copy */
288 size_t copy_n
= MIN(conf_filechunk
, buffer_len
- buf_widx
);
290 /* stop copying if it would overwrite the reading position */
291 if (RINGBUF_ADD_CROSS(buf_widx
, copy_n
, buf_ridx
) >= 0)
294 /* rc is the actual amount read */
295 int rc
= rb
->read(h
->fd
, &buffer
[buf_widx
], copy_n
);
299 //DEBUGF("failed on fd %d at buf_widx = %ld for handle %d\n", h->fd, (long)buf_widx, h->id);
300 DEBUGF("File ended %ld bytes early\n", (long)h
->filerem
);
301 h
->filesize
-= h
->filerem
;
307 h
->widx
= RINGBUF_ADD(h
->widx
, rc
);
315 if (h
->filerem
== 0) {
316 /* finished buffering the file */
320 DEBUGF("buffered %ld bytes (%ld of %ld available, remaining: %ld)\n",
321 ret
, h
->available
, h
->filesize
, h
->filerem
);
328 /* Free buffer space by moving the handle struct right before the useful
329 part of its data buffer */
330 static void free_buffer(int handle_id
)
332 DEBUGF("free_buffer(%d)\n", handle_id
);
333 struct memory_handle
*h
= find_handle(handle_id
);
337 size_t delta
= RINGBUF_SUB(h
->ridx
, h
->data
);
338 h
= move_handle(&delta
, h
);
340 /* The value of delta might change for alignment reasons */
341 h
->data
= RINGBUF_ADD(h
->data
, delta
);
343 h
->available
-= delta
;
348 static void fill_buffer(void)
350 DEBUGF("fill buffer()\n");
351 struct memory_handle
*m
= first_handle
;
353 if (m
->filerem
> 0) {
354 buffer_handle(first_handle
->id
);
361 static size_t data_rem(void)
365 struct memory_handle
*m
= first_handle
;
374 static size_t wasted_space(void)
378 struct memory_handle
*m
= first_handle
;
380 ret
+= RINGBUF_SUB(m
->ridx
, m
->data
);
387 /* Request a file be buffered
388 filename: name of the file t open
389 offset: starting offset to buffer from the file
390 RETURNS: <0 if the file cannot be opened, or one file already
391 queued to be opened, otherwise the handle for the file in the buffer
393 int bufopen(char *file
, size_t offset
)
395 DEBUGF("bufopen: %s (offset: %ld)\n", file
, offset
);
397 int fd
= rb
->open(file
, O_RDONLY
);
401 size_t size
= rb
->filesize(fd
) - offset
;
402 struct memory_handle
*h
= add_handle(&size
);
405 DEBUGF("failed to add handle\n");
410 if (offset
) rb
->lseek(fd
, offset
, SEEK_SET
);
411 rb
->strncpy(h
->path
, file
, MAX_PATH
);
413 h
->filesize
= rb
->filesize(fd
);
414 h
->filerem
= h
->filesize
- offset
;
422 DEBUGF("added handle : %d\n", h
->id
);
426 /* Close the handle. Return 0 for success and < 0 for failure */
427 int bufclose(int handle_id
)
429 DEBUGF("bufclose(%d)\n", handle_id
);
430 struct memory_handle
*h
= find_handle(handle_id
);
438 /* Set the reading index in a handle (relatively to the start of the handle data).
439 Return 0 for success and < 0 for failure */
440 int bufseek(int handle_id
, size_t offset
)
442 struct memory_handle
*h
= find_handle(handle_id
);
446 if (offset
> h
->available
)
449 h
->ridx
= RINGBUF_ADD(h
->data
, offset
);
453 /* Advance the reading index in a handle (relatively to its current position).
454 Return 0 for success and < 0 for failure */
455 int bufadvance(int handle_id
, off_t offset
)
457 struct memory_handle
*h
= find_handle(handle_id
);
463 /* check for access beyond what's available */
464 if ((size_t)offset
> (h
->available
- RINGBUF_SUB(h
->ridx
, h
->data
)))
467 h
->ridx
= RINGBUF_ADD(h
->ridx
, offset
);
471 /* check for access before what's available */
472 if ((size_t)(-offset
) > RINGBUF_SUB(h
->ridx
, h
->data
))
475 h
->ridx
= RINGBUF_SUB(h
->ridx
, (size_t)(-offset
));
481 /* Copy data from the given handle to the dest buffer.
482 Return the number of bytes copied or < 0 for failure. */
483 ssize_t
bufread(int handle_id
, size_t size
, char *dest
)
485 struct memory_handle
*h
= find_handle(handle_id
);
486 size_t buffered_data
;
490 if (h
->available
== 0 && h
->filerem
> 0)
493 if (h
->available
== 0 && h
->filerem
== 0)
496 buffered_data
= MIN(size
, h
->available
);
498 if (h
->ridx
+ buffered_data
> buffer_len
)
500 size_t read
= buffer_len
- h
->ridx
;
501 rb
->memcpy(dest
, &buffer
[h
->ridx
], read
);
502 rb
->memcpy(dest
+read
, buffer
, buffered_data
- read
);
504 else rb
->memcpy(dest
, &buffer
[h
->ridx
], buffered_data
);
506 h
->ridx
= RINGBUF_ADD(h
->ridx
, buffered_data
);
507 h
->available
-= buffered_data
;
508 return buffered_data
;
511 /* Update the "data" pointer to make the handle's data available to the caller.
512 Return the length of the available linear data or < 0 for failure. */
513 ssize_t
bufgetdata(int handle_id
, size_t size
, unsigned char **data
)
515 struct memory_handle
*h
= find_handle(handle_id
);
519 if (h
->available
== 0 && h
->filerem
> 0)
522 if (h
->available
== 0 && h
->filerem
== 0)
527 if (h
->ridx
+ size
> buffer_len
&&
528 h
->available
- RINGBUF_SUB(h
->ridx
, h
->data
) >= size
)
530 /* use the guard buffer to provide what was requested. */
531 size_t copy_n
= h
->ridx
+ size
- buffer_len
;
532 rb
->memcpy(guard_buffer
, (unsigned char *)buffer
, copy_n
);
537 ret
= MIN(h
->available
- RINGBUF_SUB(h
->ridx
, h
->data
),
538 buffer_len
- h
->ridx
);
541 *data
= (unsigned char *)&buffer
[h
->ridx
];
543 //DEBUGF("bufgetdata(%d): h->ridx=%ld, ret=%ld\n", handle_id, (long)h->ridx, ret);
549 struct memory_handle
*m1
, *m2
, *m3
, *m4
;
551 if (cur_handle
!= NULL
|| first_handle
!= NULL
)
554 m1
= add_handle(NULL
);
556 if (cur_handle
!= m1
|| first_handle
!= m1
|| m1
->next
!= NULL
)
559 m2
= add_handle(NULL
);
561 if (cur_handle
!= m2
|| first_handle
!= m1
|| m1
->next
!= m2
|| m2
->next
!= NULL
)
564 m3
= add_handle(NULL
);
566 if (cur_handle
!= m3
|| first_handle
!= m1
|| m2
->next
!= m3
|| m3
->next
!= NULL
)
571 if (cur_handle
!= m3
|| first_handle
!= m1
|| m1
->next
!= m3
)
576 if (cur_handle
!= m1
|| first_handle
!= m1
|| m1
->next
!= NULL
)
579 m4
= add_handle(NULL
);
581 if (cur_handle
!= m4
|| first_handle
!= m1
|| m1
->next
!= m4
|| m4
->next
!= NULL
)
586 if (cur_handle
!= m4
|| first_handle
!= m4
)
591 if (cur_handle
!= NULL
|| first_handle
!= NULL
)
594 m1
= add_handle(NULL
);
595 m2
= add_handle(NULL
);
596 m3
= add_handle(NULL
);
597 m4
= add_handle(NULL
);
599 if (cur_handle
!= m4
|| first_handle
!= m1
)
603 m2
= move_handle(1024*100, m2
);
605 if (cur_handle
!= m4
|| first_handle
!= m1
|| m1
->next
!= m2
|| m2
->next
!= m3
)
608 m1
= move_handle(1024*100*3, m1
);
610 if (cur_handle
!= m4
|| first_handle
!= m1
|| m1
->next
!= m2
)
619 if (cur_handle
!= NULL
|| first_handle
!= NULL
)
625 /* display a nice graphical view of the ringbuffer. */
626 static void graph_view(int width
)
628 #ifndef ROCKBOX_HAS_LOGF
630 r_pos
= buf_ridx
* width
/ buffer_len
;
631 w_pos
= buf_widx
* width
/ buffer_len
;
634 for (i
=0; i
<= width
; i
++)
636 if (i
!= r_pos
&& i
!= w_pos
)
638 if (buf_ridx
<= buf_widx
)
640 if (i
> r_pos
&& i
< w_pos
) {
648 if (i
> r_pos
|| i
< w_pos
) {
657 if (i
== r_pos
&& i
== w_pos
)
659 if (buf_ridx
<= buf_widx
) {
664 } else if (i
== r_pos
) {
666 } else if (i
== w_pos
) {
678 void print_progress(size_t amount
, int file
, int numfiles
)
681 rb
->lcd_clear_display();
682 rb
->snprintf(buf
, sizeof(buf
), "file %d of %d", file
, numfiles
);
683 rb
->lcd_puts(0, 0, buf
);
684 rb
->snprintf(buf
, sizeof(buf
), "read: %ld", amount
);
685 rb
->lcd_puts(0, 1, buf
);
689 bool buffer_init(void)
691 buffer
= rb
->plugin_get_audio_buffer(&buffer_len
);
694 DEBUGF("couldn't allocate buffer\n");
697 buffer_len
-= GUARD_SIZE
;
698 guard_buffer
= buffer
+ buffer_len
;
706 conf_filechunk
= AUDIO_DEFAULT_FILECHUNK
;
711 bool disk_is_spinning(void)
718 static long codec_stack
[4*DEFAULT_STACK_SIZE
/sizeof(long)];
719 static struct thread_entry
* codecthread_id
;
721 static long bufopen_stack
[DEFAULT_STACK_SIZE
/sizeof(long)];
722 static struct thread_entry
* bufopenthread_id
;
724 bool done_playing
= false;
726 #define MAX_HANDLES 16
728 int handles
[MAX_HANDLES
];
730 void codec_thread(void)
733 int fd
= -1; /* used to write the files out as they are read */
735 char outfile
[MAX_PATH
];
736 long read
, total
= 0;
742 if (handles
[idx
] > 0) {
745 rb
->snprintf(outfile
, MAX_PATH
, "/file%d.mp3", idx
);
746 fd
= rb
->open(outfile
, O_CREAT
|O_TRUNC
|O_WRONLY
);
748 DEBUGF("couldn't create file\n");
749 rb
->splash(HZ
, "couldn't create file");
754 read
= bufgetdata(handles
[idx
], GUARD_SIZE
, &data
);
756 read
= MIN(read
, GUARD_SIZE
);
757 rb
->write(fd
, data
, read
);
759 bufadvance(handles
[idx
], read
);
760 print_progress(total
, idx
+1, num_files
);
765 if (read
>= 0 && total
>= 0) {
766 DEBUGF("read %ld bytes from handle %d\n", total
, handles
[idx
]);
770 DEBUGF("data for handle %d isn't ready\n", handles
[idx
]);
771 } else if (read
== -1) {
772 DEBUGF("couldn't find handle %d\n", handles
[idx
]);
773 } else if (read
== 0) {
774 DEBUGF("finished reading handle %d\n", handles
[idx
]);
775 bufclose(handles
[idx
]);
781 if (idx
>= num_files
) {
792 DEBUGF("removing the codec thread\n");
793 rb
->remove_thread(NULL
);
796 void bufopen_thread(void)
799 while (idx
< num_files
)
801 ret
= bufopen(files
[idx
], 0);
803 handles
[idx
++] = ret
;
808 DEBUGF("bufopen thread finished\n");
809 rb
->remove_thread(NULL
);
813 /* this is the plugin entry point */
814 enum plugin_status
plugin_start(struct plugin_api
* api
, void* parameter
)
823 DEBUGF("linked list test failed\n");
824 rb
->splash(HZ
, "linked list test failed");
828 codecthread_id
= rb
->create_thread(codec_thread
,
832 IF_PRIO(,PRIORITY_PLAYBACK
)
833 IF_COP(, CPU
, false));
835 bufopenthread_id
= rb
->create_thread(bufopen_thread
,
837 sizeof(bufopen_stack
),
839 IF_PRIO(,PRIORITY_BACKGROUND
)
840 IF_COP(, CPU
, false));
844 rb
->splash(HZ
, "failed to create codec thread");
847 else if (!bufopenthread_id
)
849 rb
->splash(HZ
, "failed to create bufopen thread");
855 while (!done_playing
)
857 if (wasted_space() > buffer_len
/4) {
858 DEBUGF("there is %ld bytes of wasted space\n", wasted_space());
859 struct memory_handle
*m
= first_handle
;
866 if (data_rem() > 0 && BUF_USED
< buffer_len
/4 && disk_is_spinning()) {
867 DEBUGF("%ld bytes left to buffer and the buffer is running low\n", data_rem());
873 DEBUGF("done playing\n");
878 DEBUGF("end of plugin\n");