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
65 struct memory_handle
{
66 int id
; /* A unique ID for the handle */
70 size_t data
; /* Start index of the handle's data buffer */
71 size_t ridx
; /* Current read pointer, relative to the main buffer */
72 size_t widx
; /* Current write pointer */
73 size_t filesize
; /* File total length */
74 size_t filerem
; /* Remaining bytes of file NOT in buffer */
75 size_t available
; /* Available bytes to read from buffer */
76 size_t offset
; /* Offset at which we started reading the file */
77 struct memory_handle
*next
;
82 static char *guard_buffer
;
84 static size_t buffer_len
;
85 static size_t buf_widx
;
86 static size_t buf_ridx
;
88 static size_t conf_filechunk
;
90 /* current memory handle in the linked list. NULL when the list is empty. */
91 static struct memory_handle
*cur_handle
;
92 /* first memory handle in the linked list. NULL when the list is empty. */
93 static struct memory_handle
*first_handle
;
94 static int num_handles
;
96 static void graph_view(int width
);
99 /* add a new handle to the linked list and return it. It will have become the
100 new current handle. The handle will reserve "data_size" bytes or if that's
101 not possible, decrease "data_size" to allow adding the handle. */
102 static struct memory_handle
*add_handle(size_t *data_size
)
104 /* this will give each handle a unique id */
105 static int cur_handle_id
= 1;
107 /* make sure buf_widx is 32-bit aligned so that the handle struct is,
108 but before that we check we can actually align. */
109 if (RINGBUF_ADD_CROSS(buf_widx
, 3, buf_ridx
) >= 0) {
112 buf_widx
= (RINGBUF_ADD(buf_widx
, 3)) & ~3;
114 size_t len
= (data_size
? *data_size
: 0)
115 + sizeof(struct memory_handle
);
117 /* check that we actually can add the handle and its data */
118 int overlap
= RINGBUF_ADD_CROSS(buf_widx
, len
, buf_ridx
);
120 *data_size
-= overlap
;
123 if (len
< sizeof(struct memory_handle
)) {
127 struct memory_handle
*new_handle
=
128 (struct memory_handle
*)(&buffer
[buf_widx
]);
130 /* only advance the buffer write index of the size of the struct */
131 buf_widx
= RINGBUF_ADD(buf_widx
, sizeof(struct memory_handle
));
134 /* the new handle is the first one */
135 first_handle
= new_handle
;
139 cur_handle
->next
= new_handle
;
142 cur_handle
= new_handle
;
143 cur_handle
->id
= cur_handle_id
++;
144 cur_handle
->next
= NULL
;
149 /* delete a given memory handle from the linked list
150 and return true for success. Nothing is actually erased from memory. */
151 static bool rm_handle(struct memory_handle
*h
)
153 if (h
== first_handle
) {
154 first_handle
= h
->next
;
155 if (h
== cur_handle
) {
156 DEBUGF("removing the first and last handle\n");
157 /* h was the first and last handle */
161 buf_ridx
= (void *)first_handle
- (void *)buffer
;
164 struct memory_handle
*m
= first_handle
;
165 while (m
&& m
->next
!= h
) {
168 if (h
&& m
&& m
->next
== h
) {
170 if (h
== cur_handle
) {
182 /* these are unfortunalty needed to be global
183 so move_handle can invalidate them */
184 static int cached_handle_id
= -1;
185 static struct memory_handle
*cached_handle
= NULL
;
187 /* Return a pointer to the memory handle of given ID.
188 NULL if the handle wasn't found */
189 static struct memory_handle
*find_handle(int handle_id
)
191 /* simple caching because most of the time the requested handle
192 will either be the same as the last, or the one after the last */
195 if (cached_handle_id
== handle_id
&&
196 cached_handle_id
== cached_handle
->id
)
197 return cached_handle
;
198 else if (cached_handle
->next
&& (cached_handle
->next
->id
== handle_id
))
200 /* JD's quick testing showd this block was only entered
201 2/1971 calls to find_handle.
202 8/1971 calls to find_handle resulted in a cache miss */
203 cached_handle
= cached_handle
->next
;
204 cached_handle_id
= handle_id
;
205 return cached_handle
;
209 struct memory_handle
*m
= first_handle
;
210 while (m
&& m
->id
!= handle_id
) {
213 cached_handle_id
= handle_id
;
215 return (m
&& m
->id
== handle_id
) ? m
: NULL
;
218 /* Move a memory handle to newpos.
219 Return a pointer to the new location of the handle */
220 static struct memory_handle
*move_handle(size_t *delta
, struct memory_handle
*h
)
223 /* aligning backwards would yield a negative result,
224 and moving the handle of such a small amount is a waste
228 /* make sure delta is 32-bit aligned so that the handle struct is. */
229 *delta
= (*delta
- 3) & ~3;
231 size_t newpos
= RINGBUF_ADD((void *)h
- (void *)buffer
, *delta
);
233 struct memory_handle
*dest
= (struct memory_handle
*)(&buffer
[newpos
]);
235 /* Invalidate the cache to prevent it from keeping the old location of h */
236 if (h
== cached_handle
)
237 cached_handle
= NULL
;
239 /* the cur_handle pointer might need updating */
240 if (h
== cur_handle
) {
244 if (h
== first_handle
) {
248 struct memory_handle
*m
= first_handle
;
249 while (m
&& m
->next
!= h
) {
252 if (h
&& m
&& m
->next
== h
) {
259 rb
->memmove(dest
, h
, sizeof(struct memory_handle
));
264 /* Buffer data for the given handle. Return the amount of data buffered
265 or -1 if the handle wasn't found */
266 static ssize_t
buffer_handle(int handle_id
)
268 DEBUGF("buffer_handle(%d)\n", handle_id
);
269 struct memory_handle
*h
= find_handle(handle_id
);
273 if (h
->filerem
== 0) {
274 /* nothing left to buffer */
278 if (h
->fd
< 0) /* file closed, reopen */
281 h
->fd
= rb
->open(h
->path
, O_RDONLY
);
289 rb
->lseek(h
->fd
, h
->offset
, SEEK_SET
);
293 while (h
->filerem
> 0)
295 /* max amount to copy */
296 size_t copy_n
= MIN(conf_filechunk
, buffer_len
- h
->widx
);
298 /* stop copying if it would overwrite the reading position
299 or the next handle */
300 if (RINGBUF_ADD_CROSS(h
->widx
, copy_n
, buf_ridx
) >= 0 || (h
->next
&&
301 RINGBUF_ADD_CROSS(h
->widx
, copy_n
,
302 ((void *)h
->next
- (void *)buffer
)) >= 0))
305 /* rc is the actual amount read */
306 int rc
= rb
->read(h
->fd
, &buffer
[h
->widx
], copy_n
);
310 DEBUGF("File ended %ld bytes early\n", (long)h
->filerem
);
311 h
->filesize
-= h
->filerem
;
317 h
->widx
= RINGBUF_ADD(h
->widx
, rc
);
325 if (h
->filerem
== 0) {
326 /* finished buffering the file */
330 DEBUGF("buffered %ld bytes (%ld of %ld available, remaining: %ld)\n",
331 ret
, h
->available
, h
->filesize
, h
->filerem
);
338 /* Free buffer space by moving the handle struct right before the useful
339 part of its data buffer */
340 static void free_buffer(int handle_id
)
342 DEBUGF("free_buffer(%d)\n", handle_id
);
343 struct memory_handle
*h
= find_handle(handle_id
);
347 size_t delta
= RINGBUF_SUB(h
->ridx
, h
->data
);
348 h
= move_handle(&delta
, h
);
350 /* The value of delta might change for alignment reasons */
351 h
->data
= RINGBUF_ADD(h
->data
, delta
);
352 h
->available
-= delta
;
357 static void fill_buffer(void)
359 DEBUGF("fill buffer()\n");
360 struct memory_handle
*m
= first_handle
;
362 if (m
->filerem
> 0) {
363 buffer_handle(m
->id
);
370 static size_t data_rem(void)
374 struct memory_handle
*m
= first_handle
;
383 static size_t wasted_space(void)
387 struct memory_handle
*m
= first_handle
;
389 ret
+= RINGBUF_SUB(m
->ridx
, m
->data
);
396 /* Request a file be buffered
397 filename: name of the file to open
398 offset:starting offset to buffer from the file
399 return value: <0 if the file cannot be opened, or one file already
400 queued to be opened, otherwise the handle for the file in the buffer
402 int bufopen(char *file
, size_t offset
)
404 if (cur_handle
&& cur_handle
->filerem
> 0) {
408 int fd
= rb
->open(file
, O_RDONLY
);
412 size_t size
= rb
->filesize(fd
) - offset
;
414 DEBUGF("bufopen: %s (offset: %ld) (%ld bytes needed)...\n",
417 struct memory_handle
*h
= add_handle(&size
);
420 DEBUGF("failed to add handle\n");
425 if (offset
) rb
->lseek(fd
, offset
, SEEK_SET
);
426 rb
->strncpy(h
->path
, file
, MAX_PATH
);
428 h
->filesize
= rb
->filesize(fd
);
429 h
->filerem
= h
->filesize
- offset
;
436 DEBUGF("allocated %ld bytes. ID: %d\n", size
, h
->id
);
440 /* Close the handle. Return 0 for success and < 0 for failure */
441 int bufclose(int handle_id
)
443 DEBUGF("bufclose(%d)\n", handle_id
);
444 struct memory_handle
*h
= find_handle(handle_id
);
452 /* Set reading index in handle (relatively to the start of the handle data).
453 Return 0 for success and < 0 for failure */
454 int bufseek(int handle_id
, size_t offset
)
456 struct memory_handle
*h
= find_handle(handle_id
);
460 if (offset
> h
->available
)
463 h
->ridx
= RINGBUF_ADD(h
->data
, offset
);
467 /* Advance the reading index in a handle (relatively to its current position).
468 Return 0 for success and < 0 for failure */
469 int bufadvance(int handle_id
, off_t offset
)
471 struct memory_handle
*h
= find_handle(handle_id
);
477 /* check for access beyond what's available */
478 if ((size_t)offset
> (h
->available
- RINGBUF_SUB(h
->ridx
, h
->data
)))
481 h
->ridx
= RINGBUF_ADD(h
->ridx
, offset
);
485 /* check for access before what's available */
486 if ((size_t)(-offset
) > RINGBUF_SUB(h
->ridx
, h
->data
))
489 h
->ridx
= RINGBUF_SUB(h
->ridx
, (size_t)(-offset
));
495 /* Copy data from the given handle to the dest buffer.
496 Return the number of bytes copied or < 0 for failure. */
497 ssize_t
bufread(int handle_id
, size_t size
, char *dest
)
499 struct memory_handle
*h
= find_handle(handle_id
);
500 size_t buffered_data
;
504 if (h
->available
== 0 && h
->filerem
> 0)
507 if (h
->available
== 0 && h
->filerem
== 0)
510 buffered_data
= MIN(size
, h
->available
);
512 if (h
->ridx
+ buffered_data
> buffer_len
)
514 size_t read
= buffer_len
- h
->ridx
;
515 rb
->memcpy(dest
, &buffer
[h
->ridx
], read
);
516 rb
->memcpy(dest
+read
, buffer
, buffered_data
- read
);
518 else rb
->memcpy(dest
, &buffer
[h
->ridx
], buffered_data
);
520 h
->ridx
= RINGBUF_ADD(h
->ridx
, buffered_data
);
521 h
->available
-= buffered_data
;
522 return buffered_data
;
525 /* Update the "data" pointer to make the handle's data available to the caller.
526 Return the length of the available linear data or < 0 for failure. */
527 ssize_t
bufgetdata(int handle_id
, size_t size
, unsigned char **data
)
529 struct memory_handle
*h
= find_handle(handle_id
);
533 if (h
->available
== 0 && h
->filerem
> 0)
536 if (h
->available
== 0 && h
->filerem
== 0)
541 if (h
->ridx
+ size
> buffer_len
&&
542 h
->available
- RINGBUF_SUB(h
->ridx
, h
->data
) >= size
)
544 /* use the guard buffer to provide what was requested. */
545 size_t copy_n
= h
->ridx
+ size
- buffer_len
;
546 rb
->memcpy(guard_buffer
, (unsigned char *)buffer
, copy_n
);
548 DEBUGF("used the guard buffer to complete\n");
552 ret
= MIN(h
->available
- RINGBUF_SUB(h
->ridx
, h
->data
),
553 buffer_len
- h
->ridx
);
556 *data
= (unsigned char *)&buffer
[h
->ridx
];
558 /* DEBUGF("bufgetdata(%d): h->ridx=%ld, ret=%ld\n", handle_id,
559 (long)h->ridx, ret); */
565 struct memory_handle
*m1
, *m2
, *m3
, *m4
;
567 if (cur_handle
!= NULL
|| first_handle
!= NULL
)
570 m1
= add_handle(NULL
);
572 if (cur_handle
!= m1
|| first_handle
!= m1
|| m1
->next
!= NULL
)
575 m2
= add_handle(NULL
);
577 if (cur_handle
!= m2
|| first_handle
!= m1
|| m1
->next
!= m2
|| m2
->next
!= NULL
)
580 m3
= add_handle(NULL
);
582 if (cur_handle
!= m3
|| first_handle
!= m1
|| m2
->next
!= m3
|| m3
->next
!= NULL
)
587 if (cur_handle
!= m3
|| first_handle
!= m1
|| m1
->next
!= m3
)
592 if (cur_handle
!= m1
|| first_handle
!= m1
|| m1
->next
!= NULL
)
595 m4
= add_handle(NULL
);
597 if (cur_handle
!= m4
|| first_handle
!= m1
|| m1
->next
!= m4
|| m4
->next
!= NULL
)
602 if (cur_handle
!= m4
|| first_handle
!= m4
)
607 if (cur_handle
!= NULL
|| first_handle
!= NULL
)
610 m1
= add_handle(NULL
);
611 m2
= add_handle(NULL
);
612 m3
= add_handle(NULL
);
613 m4
= add_handle(NULL
);
615 if (cur_handle
!= m4
|| first_handle
!= m1
)
618 size_t delta
= 1024*100;
619 m2
= move_handle(&delta
, m2
);
621 if (cur_handle
!= m4
|| first_handle
!= m1
|| m1
->next
!= m2
|| m2
->next
!= m3
)
625 m1
= move_handle(&delta
, m1
);
627 if (cur_handle
!= m4
|| first_handle
!= m1
|| m1
->next
!= m2
)
635 if (cur_handle
!= NULL
|| first_handle
!= NULL
)
641 /* display a nice graphical view of the ringbuffer. */
642 static void graph_view(int width
)
644 #ifndef ROCKBOX_HAS_LOGF
646 r_pos
= buf_ridx
* width
/ buffer_len
;
647 w_pos
= buf_widx
* width
/ buffer_len
;
650 for (i
=0; i
<= width
; i
++)
652 if (i
!= r_pos
&& i
!= w_pos
)
654 if (buf_ridx
<= buf_widx
)
656 if (i
> r_pos
&& i
< w_pos
) {
664 if (i
> r_pos
|| i
< w_pos
) {
673 if (i
== r_pos
&& i
== w_pos
)
675 if (buf_ridx
<= buf_widx
) {
680 } else if (i
== r_pos
) {
682 } else if (i
== w_pos
) {
694 void print_progress(size_t amount
, int file
, int numfiles
)
697 rb
->lcd_clear_display();
698 rb
->snprintf(buf
, sizeof(buf
), "file %d of %d", file
, numfiles
);
699 rb
->lcd_puts(0, 0, buf
);
700 rb
->snprintf(buf
, sizeof(buf
), "read: %ld", amount
);
701 rb
->lcd_puts(0, 1, buf
);
705 bool buffer_init(void)
707 buffer
= rb
->plugin_get_audio_buffer(&buffer_len
);
710 DEBUGF("couldn't allocate buffer\n");
713 buffer_len
-= GUARD_SIZE
;
714 guard_buffer
= buffer
+ buffer_len
;
722 conf_filechunk
= AUDIO_DEFAULT_FILECHUNK
;
727 bool disk_is_spinning(void)
734 static long codec_stack
[4*DEFAULT_STACK_SIZE
/sizeof(long)];
735 static struct thread_entry
* codecthread_id
;
737 static long bufopen_stack
[DEFAULT_STACK_SIZE
/sizeof(long)];
738 static struct thread_entry
* bufopenthread_id
;
740 bool done_playing
= false;
742 #define MAX_HANDLES 16
744 int handles
[MAX_HANDLES
];
746 void codec_thread(void)
749 int fd
= -1; /* used to write the files out as they are read */
751 char outfile
[MAX_PATH
];
752 long read
, total
= 0;
758 if (handles
[idx
] > 0) {
761 rb
->snprintf(outfile
, MAX_PATH
, "/file%d.mp3", idx
);
762 fd
= rb
->open(outfile
, O_CREAT
|O_TRUNC
|O_WRONLY
);
764 DEBUGF("couldn't create file\n");
765 rb
->splash(HZ
, "couldn't create file");
770 read
= bufgetdata(handles
[idx
], GUARD_SIZE
, &data
);
772 read
= MIN(read
, GUARD_SIZE
);
773 rb
->write(fd
, data
, read
);
775 bufadvance(handles
[idx
], read
);
776 print_progress(total
, idx
+1, num_files
);
781 if (read
>= 0 && total
>= 0) {
782 DEBUGF("read %ld bytes from handle %d\n", total
,
787 DEBUGF("data for handle %d isn't ready\n", handles
[idx
]);
788 } else if (read
== -1) {
789 DEBUGF("couldn't find handle %d\n", handles
[idx
]);
790 } else if (read
== 0) {
791 DEBUGF("finished reading handle %d\n", handles
[idx
]);
792 bufclose(handles
[idx
]);
798 if (idx
>= num_files
) {
809 DEBUGF("removing the codec thread\n");
810 rb
->remove_thread(NULL
);
813 void bufopen_thread(void)
816 while (idx
< num_files
)
818 ret
= bufopen(files
[idx
], 0);
820 handles
[idx
++] = ret
;
825 DEBUGF("bufopen thread finished\n");
826 rb
->remove_thread(NULL
);
830 /* this is the plugin entry point */
831 enum plugin_status
plugin_start(struct plugin_api
* api
, void* parameter
)
840 DEBUGF("linked list test failed\n");
841 rb
->splash(HZ
, "linked list test failed");
845 codecthread_id
= rb
->create_thread(codec_thread
,
849 IF_PRIO(,PRIORITY_PLAYBACK
)
850 IF_COP(, CPU
, false));
852 bufopenthread_id
= rb
->create_thread(bufopen_thread
,
854 sizeof(bufopen_stack
),
856 IF_PRIO(,PRIORITY_BACKGROUND
)
857 IF_COP(, CPU
, false));
861 rb
->splash(HZ
, "failed to create codec thread");
864 else if (!bufopenthread_id
)
866 rb
->splash(HZ
, "failed to create bufopen thread");
872 while (!done_playing
)
874 if (wasted_space() > buffer_len
/10) {
875 DEBUGF("there is %ld bytes of wasted space\n", wasted_space());
876 struct memory_handle
*m
= first_handle
;
883 if (data_rem() > 0 && BUF_USED
< 3*buffer_len
/4 &&
886 DEBUGF("%ld bytes left to buffer and the buffer is low\n",
893 DEBUGF("done playing\n");
898 DEBUGF("end of plugin\n");