Add a new thread ("bufopen") that just adds files. Rename the playback thread codec...
[Rockbox-MoB.git] / testplugin.c
blobc954b5ed820804a114d6101c6460f9a04a24bedb
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
21 #include "plugin.h"
23 PLUGIN_HEADER
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 /* Internal ring buffer helper macros */
44 /* Buffer pointer (p) plus value (v), wrapped if necessary */
45 #define INT_RINGBUF_ADD(h,p,v) ((p+v) < h->data_len ? p+v : p+v - h->data_len)
46 /* Buffer pointer (p) minus value (v), wrapped if necessary */
47 #define INT_RINGBUF_SUB(h,p,v) ((p>=v) ? p-v : p + h->data_len - v)
48 /* How far value (v) plus buffer pointer (p1) will cross buffer pointer (p2) */
49 #define INT_RINGBUF_ADD_CROSS(h,p1,v,p2) \
50 ((p1<p2) ? (int)(p1+v)-(int)p2 : (int)(p1+v-p2)-(int)h->data_len)
51 /* Bytes available in the buffer */
52 #define INT_BUF_USED(h) (INT_RINGBUF_SUB(h, h->widx, h->ridx))
54 #ifdef ROCKBOX_HAS_LOGF
55 #define DEBUGF rb->logf
56 #endif
58 int num_files = 5;
59 char *files[] = {
60 "/a.mp3",
61 "/b.mp3",
62 "/c.mp3",
63 "/d.mp3",
64 "/e.mp3" };
66 enum data_type {
67 TYPE_CODEC,
68 TYPE_AUDIO,
69 TYPE_ID3,
70 TYPE_CUESHEET,
71 TYPE_IMAGE,
72 TYPE_BUFFER,
73 TYPE_UNKNOWN,
76 struct memory_handle {
77 int id; /* A unique ID for the handle */
78 enum data_type type;
79 char path[MAX_PATH];
80 int fd;
81 size_t data; /* Start index of the handle's data buffer */
82 size_t data_len; /* Length of the data buffer for the handle */
83 size_t ridx; /* Current read pointer, relative to the data 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;
93 static char *buffer;
94 static char *guard_buffer;
96 static size_t buffer_len;
97 static size_t buf_widx;
98 static size_t buf_ridx;
100 static size_t conf_filechunk;
102 /* current memory handle in the linked list. NULL when the list is empty. */
103 static struct memory_handle *cur_handle;
104 /* first memory handle in the linked list. NULL when the list is empty. */
105 static struct memory_handle *first_handle;
106 static int num_handles;
108 static void graph_view(int width);
111 /* add a new handle to the linked list and return it. It will have become the
112 new current handle. The handle will reserve "data_size" bytes or if that's
113 not possible, decrease "data_size" to allow adding the handle. */
114 static struct memory_handle *add_handle(size_t *data_size)
116 /* this will give each handle a unique id */
117 static int cur_handle_id = 1;
119 /* make sure buf_widx is 32-bit aligned so that the handle struct is,
120 but before that we check we can actually align. */
121 if (RINGBUF_ADD_CROSS(buf_widx, 3, buf_ridx) >= 0) {
122 DEBUGF("no space to even align\n");
123 return NULL;
125 buf_widx = (RINGBUF_ADD(buf_widx, 3)) & ~3;
127 size_t len = (data_size ? *data_size : 0)
128 + sizeof(struct memory_handle);
130 /* check that we actually can add the handle and its data */
131 int overlap = RINGBUF_ADD_CROSS(buf_widx, len, buf_ridx);
132 if (overlap >= 0) {
133 *data_size -= overlap + 1;
134 len -= overlap + 1;
136 if (len < sizeof(struct memory_handle)) {
137 DEBUGF("no space to add the handle\n");
138 return NULL;
141 struct memory_handle *new_handle = (struct memory_handle *)(&buffer[buf_widx]);
143 /* only advance the buffer write index of the size of the struct */
144 buf_widx = RINGBUF_ADD(buf_widx, sizeof(struct memory_handle));
146 if (!first_handle) {
147 /* the new handle is the first one */
148 first_handle = new_handle;
151 if (cur_handle) {
152 cur_handle->next = new_handle;
155 cur_handle = new_handle;
156 cur_handle->id = cur_handle_id++;
157 cur_handle->next = NULL;
158 num_handles++;
159 return cur_handle;
162 /* delete a given memory handle from the linked list
163 and return true for success. Nothing is actually erased from memory. */
164 static bool rm_handle(struct memory_handle *h)
166 if (h == first_handle) {
167 first_handle = h->next;
168 if (h == cur_handle) {
169 DEBUGF("removing the first and last handle\n");
170 /* h was the first and last handle */
171 cur_handle = NULL;
172 buf_ridx = buf_widx;
173 } else {
174 buf_ridx = (void *)first_handle - (void *)buffer;
176 } else {
177 struct memory_handle *m = first_handle;
178 while (m && m->next != h) {
179 m = m->next;
181 if (h && m && m->next == h) {
182 m->next = h->next;
183 if (h == cur_handle) {
184 cur_handle = m;
186 } else {
187 return false;
191 num_handles--;
192 return true;
195 /* these are unfortunalty needed to be global
196 so move_handle can invalidate them */
197 static int cached_handle_id = -1;
198 static struct memory_handle *cached_handle = NULL;
200 /* Return a pointer to the memory handle of given ID.
201 NULL if the handle wasn't found */
202 static struct memory_handle *find_handle(int handle_id)
204 /* simple caching because most of the time the requested handle
205 will either be the same as the last, or the one after the last */
206 if (cached_handle)
208 if (cached_handle_id == handle_id && cached_handle_id == cached_handle->id)
209 return cached_handle;
210 else if (cached_handle->next && (cached_handle->next->id == handle_id))
212 /* JD's quick testing showd this block was only entered
213 2/1971 calls to find_handle.
214 8/1971 calls to find_handle resulted in a cache miss */
215 cached_handle = cached_handle->next;
216 cached_handle_id = handle_id;
217 return cached_handle;
221 struct memory_handle *m = first_handle;
222 while (m && m->id != handle_id) {
223 m = m->next;
225 cached_handle_id = handle_id;
226 cached_handle = m;
227 return (m && m->id == handle_id) ? m : NULL;
230 /* Move a memory handle to newpos.
231 Return a pointer to the new location of the handle */
232 static struct memory_handle *move_handle(size_t *delta, struct memory_handle *h)
234 if (*delta < 4) {
235 /* aligning backwards would yield a negative result,
236 and moving the handle of such a small amount is a waste
237 of time anyway. */
238 return NULL;
240 /* make sure delta is 32-bit aligned so that the handle struct is. */
241 *delta = (*delta - 3) & ~3;
243 size_t newpos = RINGBUF_ADD((void *)h - (void *)buffer, *delta);
245 struct memory_handle *dest = (struct memory_handle *)(&buffer[newpos]);
247 /* Invalidate the cache to prevent it from keeping the old location of h */
248 if (h == cached_handle)
249 cached_handle = NULL;
251 /* the cur_handle pointer might need updating */
252 if (h == cur_handle) {
253 cur_handle = dest;
256 if (h == first_handle) {
257 first_handle = dest;
258 buf_ridx = newpos;
259 } else {
260 struct memory_handle *m = first_handle;
261 while (m && m->next != h) {
262 m = m->next;
264 if (h && m && m->next == h) {
265 m->next = dest;
266 } else {
267 return NULL;
271 rb->memmove(dest, h, sizeof(struct memory_handle));
273 return dest;
275 /* Buffer data for the given handle. Return the amount of data buffered
276 or -1 if the handle wasn't found */
277 static ssize_t buffer_handle(int handle_id)
279 DEBUGF("buffer_handle(%d)\n", handle_id);
280 struct memory_handle *h = find_handle(handle_id);
281 if (!h)
282 return -1;
284 if (h->filerem == 0) {
285 /* nothing left to buffer */
286 return 0;
289 if (h->fd < 0) /* file closed, reopen */
291 if (*h->path)
292 h->fd = rb->open(h->path, O_RDONLY);
293 else
294 return -1;
296 if (h->fd < 0)
297 return -1;
299 if (h->offset)
300 rb->lseek(h->fd, h->offset, SEEK_SET);
303 ssize_t ret = 0;
304 while (h->filerem > 0)
306 /* max amount to copy */
307 size_t copy_n = MIN(MIN(conf_filechunk, h->filerem),
308 MIN(h->data_len - h->widx,
309 buffer_len - RINGBUF_ADD(h->data, h->widx))
312 #if 0
313 DEBUGF("widx: %ld, ridx: %ld copy_n: %ld, writepos: %ld,"
314 "readpos: %ld, add_cross: %d, int_add_cross: %d\n",
315 h->widx, h->ridx, copy_n, RINGBUF_ADD(h->data, h->widx),
316 RINGBUF_ADD(h->data, h->ridx),
317 RINGBUF_ADD_CROSS( RINGBUF_ADD(h->data, h->widx),
318 copy_n,
319 RINGBUF_ADD(h->data, h->ridx)
321 INT_RINGBUF_ADD_CROSS(h, h->widx, copy_n, h->ridx)
323 #endif
325 /* stop copying if it would overwrite the reading position.
326 buf_widx == buf_ridx is defined as buffer empty, not buffer full.
327 same goes with h->widx == h->ridx. */
328 if (RINGBUF_ADD_CROSS( RINGBUF_ADD(h->data, h->widx),
329 copy_n,
330 RINGBUF_ADD(h->data, h->ridx)
331 ) >= 0 ||
332 INT_RINGBUF_ADD_CROSS(h, h->widx, copy_n, h->ridx) >= 0 )
333 break;
335 /* rc is the actual amount read */
336 int rc = rb->read(h->fd, &buffer[RINGBUF_ADD(h->data, h->widx)], copy_n);
338 if (rc < 0)
340 DEBUGF("File ended %ld bytes early\n", (long)h->filerem);
341 h->filesize -= h->filerem;
342 h->filerem = 0;
343 break;
346 /* Advance buffer */
347 h->widx = INT_RINGBUF_ADD(h, h->widx, rc);
348 h->available += rc;
349 ret += rc;
350 h->filerem -= rc;
353 if (h->filerem == 0) {
354 /* finished buffering the file */
355 rb->close(h->fd);
358 if (h == cur_handle)
359 buf_widx = RINGBUF_ADD(h->data, h->data_len);
361 DEBUGF("buffered %ld bytes (%ld of %ld available, remaining: %ld)\n",
362 ret, h->available, h->filesize, h->filerem);
364 graph_view(100);
366 return ret;
369 /* Free buffer space by moving the handle struct right before the useful
370 part of its data buffer */
371 static void free_buffer(int handle_id)
373 DEBUGF("free_buffer(%d)\n", handle_id);
374 struct memory_handle *h = find_handle(handle_id);
375 if (!h)
376 return;
378 DEBUGF("old state: data: %ld, data_len: %ld, avail: %ld, ridx: %ld, widx: %ld\n",
379 h->data, h->data_len, h->available, h->ridx, h->widx);
381 if (h->widx != 0 && h->ridx > h->widx) {
382 /* We don't want to overwrite a useful part of the buffer */
383 return;
386 size_t delta = h->ridx;
387 h = move_handle(&delta, h);
388 if (!h) return;
389 /* The value of delta might change for alignment reasons */
390 h->data = RINGBUF_ADD(h->data, delta);
391 h->data_len -= delta;
392 h->ridx -= delta;
393 if (h->widx >= delta) h->widx -= delta;
394 h->available -= delta;
396 DEBUGF("new state: data: %ld, data_len: %ld, avail: %ld, ridx: %ld, widx: %ld\n",
397 h->data, h->data_len, h->available, h->ridx, h->widx);
399 graph_view(100);
402 static void fill_buffer(void)
404 DEBUGF("fill buffer()\n");
405 struct memory_handle *m = first_handle;
406 while (m) {
407 if (m->filerem > 0) {
408 buffer_handle(m->id);
409 return;
411 m = m->next;
415 static size_t data_rem(void)
417 size_t ret = 0;
419 struct memory_handle *m = first_handle;
420 while (m) {
421 ret += m->filerem;
422 m = m->next;
425 return ret;
428 static size_t wasted_space(void)
430 size_t ret = 0;
432 struct memory_handle *m = first_handle;
433 while (m) {
434 ret += (m->ridx <= m->widx) ? m->ridx : 0;
435 m = m->next;
438 return ret;
441 static size_t used_space(void)
443 size_t ret = 0;
445 struct memory_handle *m = first_handle;
446 while (m) {
447 ret += INT_BUF_USED(m);
448 m = m->next;
451 return ret;
454 /* Request a file be buffered
455 filename: name of the file t open
456 offset: starting offset to buffer from the file
457 RETURNS: <0 if the file cannot be opened, or one file already
458 queued to be opened, otherwise the handle for the file in the buffer
460 int bufopen(char *file, size_t offset)
462 DEBUGF("bufopen: %s (offset: %ld)\n", file, offset);
464 int fd = rb->open(file, O_RDONLY);
465 if (fd < 0)
466 return -1;
468 size_t size = rb->filesize(fd) - offset + 1;
469 DEBUGF("we want to allocate %ld bytes\n", size);
470 struct memory_handle *h = add_handle(&size);
471 if (!h)
473 DEBUGF("failed to add handle\n");
474 rb->close(fd);
475 return -1;
477 DEBUGF("allocated %ld bytes\n", size);
479 if (offset) rb->lseek(fd, offset, SEEK_SET);
480 rb->strncpy(h->path, file, MAX_PATH);
481 h->fd = fd;
482 h->filesize = rb->filesize(fd);
483 h->filerem = h->filesize - offset;
484 h->offset = offset;
485 h->ridx = 0;
486 h->widx = 0;
487 h->data = buf_widx;
488 h->data_len = size;
489 h->available = 0;
491 buf_widx = RINGBUF_ADD(buf_widx, size);
493 DEBUGF("added handle : %d\n", h->id);
494 return h->id;
497 /* Close the handle. Return 0 for success and < 0 for failure */
498 int bufclose(int handle_id)
500 DEBUGF("bufclose(%d)\n", handle_id);
501 struct memory_handle *h = find_handle(handle_id);
502 if (!h)
503 return -1;
505 rm_handle(h);
506 return 0;
509 /* Set the reading index in a handle (relatively to the start of the handle data).
510 Return 0 for success and < 0 for failure */
511 int bufseek(int handle_id, size_t offset)
513 struct memory_handle *h = find_handle(handle_id);
514 if (!h)
515 return -1;
517 if (offset > h->available || offset > h->data_len)
518 return -2;
520 h->ridx = offset;
521 return 0;
524 /* Advance the reading index in a handle (relatively to its current position).
525 Return 0 for success and < 0 for failure */
526 int bufadvance(int handle_id, off_t offset)
528 struct memory_handle *h = find_handle(handle_id);
529 if (!h)
530 return -1;
532 /* DEBUGF("bufadvance(%ld): h->ridx: %ld, h->widx: %ld\n",
533 offset, h->ridx, h->widx); */
535 if (offset >= 0)
537 /* check for access beyond what's available */
538 if ((size_t)offset > (h->available - h->ridx))
539 return -2;
541 h->ridx = INT_RINGBUF_ADD(h, h->ridx, offset);
543 else
545 /* check for access before what's available */
546 if ((size_t)(-offset) > h->ridx)
547 return -2;
549 h->ridx = INT_RINGBUF_SUB(h, h->ridx, (size_t)(-offset));
552 return 0;
555 /* Copy data from the given handle to the dest buffer.
556 Return the number of bytes copied or < 0 for failure. */
557 ssize_t bufread(int handle_id, size_t size, char *dest)
559 struct memory_handle *h = find_handle(handle_id);
560 size_t buffered_data;
561 if (!h)
562 return -1;
564 if (h->available == 0 && h->filerem > 0)
565 return -2;
567 if (h->available == 0 && h->filerem == 0)
568 return 0;
570 buffered_data = MIN(size, h->available);
572 if (h->data + h->ridx + buffered_data > buffer_len)
574 size_t read = buffer_len - (h->data + h->ridx);
575 rb->memcpy(dest, &buffer[h->data + h->ridx], read);
576 rb->memcpy(dest+read, buffer, buffered_data - read);
578 else rb->memcpy(dest, &buffer[h->data + h->ridx], buffered_data);
580 h->ridx += buffered_data;
581 h->available -= buffered_data;
582 return buffered_data;
585 /* Update the "data" pointer to make the handle's data available to the caller.
586 Return the length of the available linear data or < 0 for failure. */
587 ssize_t bufgetdata(int handle_id, size_t size, unsigned char **data)
589 struct memory_handle *h = find_handle(handle_id);
590 if (!h)
591 return -1;
593 if (h->available == 0 && h->filerem > 0)
594 return -2;
596 if (h->available == 0 && h->filerem == 0)
597 return 0;
599 ssize_t ret;
601 *data = (unsigned char *)&buffer[RINGBUF_ADD(h->data, h->ridx)];
603 if (buffer_len - (h->data + h->ridx) > 0 &&
604 buffer_len - (h->data + h->ridx) < size &&
605 h->available - h->ridx >= size)
607 /* use the guard buffer to provide what was requested. */
608 size_t copy_n = h->data + h->ridx + size - buffer_len;
610 if (h->ridx + size > h->data_len) {
611 size_t copy_1 = h->data_len - (h->ridx + size - copy_n);
612 size_t copy_2 = copy_n - copy_1;
613 rb->memcpy(guard_buffer,
614 (unsigned char *)buffer, copy_1);
615 rb->memcpy(guard_buffer + copy_1,
616 (unsigned char *)&buffer[h->data], copy_2);
617 } else {
618 rb->memcpy(guard_buffer, (unsigned char *)buffer, copy_n);
621 ret = size;
622 DEBUGF("used the guard buffer to complete\n");
624 else if (h->ridx + size > h->data_len &&
625 h->available - h->ridx >= size &&
626 size <= GUARD_SIZE)
628 size_t copy_1 = h->data_len - h->ridx;
629 size_t copy_2 = size - copy_1;
630 rb->memcpy(guard_buffer,
631 (unsigned char *)&buffer[RINGBUF_ADD(h->data, h->ridx)],
632 copy_1);
633 rb->memcpy(guard_buffer + copy_1,
634 (unsigned char *)&buffer[h->data], copy_2);
635 *data = guard_buffer;
636 ret = size;
637 DEBUGF("used the guard buffer for a whole piece\n");
639 else
641 ret = MIN(MIN(h->available - h->ridx,
642 INT_BUF_USED(h)),
643 buffer_len - (h->data + h->ridx));
646 //DEBUGF("bufgetdata(%d): h->ridx=%ld, ret=%ld\n", handle_id, (long)h->ridx, ret);
647 return ret;
650 bool test_ll(void)
652 struct memory_handle *m1, *m2, *m3, *m4;
654 if (cur_handle != NULL || first_handle != NULL)
655 return false;
657 m1 = add_handle(NULL);
659 if (cur_handle != m1 || first_handle != m1 || m1->next != NULL)
660 return false;
662 m2 = add_handle(NULL);
664 if (cur_handle != m2 || first_handle != m1 || m1->next != m2 || m2->next != NULL)
665 return false;
667 m3 = add_handle(NULL);
669 if (cur_handle != m3 || first_handle != m1 || m2->next != m3 || m3->next != NULL)
670 return false;
672 rm_handle(m2);
674 if (cur_handle != m3 || first_handle != m1 || m1->next != m3)
675 return false;
677 rm_handle(m3);
679 if (cur_handle != m1 || first_handle != m1 || m1->next != NULL)
680 return false;
682 m4 = add_handle(NULL);
684 if (cur_handle != m4 || first_handle != m1 || m1->next != m4 || m4->next != NULL)
685 return false;
687 rm_handle(m1);
689 if (cur_handle != m4 || first_handle != m4)
690 return false;
692 rm_handle(m4);
694 if (cur_handle != NULL || first_handle != NULL)
695 return false;
697 m1 = add_handle(NULL);
698 m2 = add_handle(NULL);
699 m3 = add_handle(NULL);
700 m4 = add_handle(NULL);
702 if (cur_handle != m4 || first_handle != m1)
703 return false;
705 size_t delta = 1024*100;
706 m2 = move_handle(&delta, m2);
708 if (cur_handle != m4 || first_handle != m1 || m1->next != m2 || m2->next != m3)
709 return false;
711 delta = 1024*100*3;
712 m1 = move_handle(&delta, m1);
714 if (cur_handle != m4 || first_handle != m1 || m1->next != m2)
715 return false;
717 rm_handle(m1);
718 rm_handle(m2);
719 rm_handle(m3);
720 rm_handle(m4);
722 if (cur_handle != NULL || first_handle != NULL)
723 return false;
725 return true;
728 /* display a nice graphical view of the ringbuffer. */
729 static void graph_view(int width)
731 #ifndef ROCKBOX_HAS_LOGF
732 int i, r_pos, w_pos;
733 r_pos = buf_ridx * width / buffer_len;
734 w_pos = buf_widx * width / buffer_len;
736 DEBUGF("|");
737 for (i=0; i <= width; i++)
739 if (i != r_pos && i != w_pos)
741 if (buf_ridx <= buf_widx)
743 if (i > r_pos && i < w_pos) {
744 DEBUGF(">");
745 } else {
746 DEBUGF("-");
749 else
751 if (i > r_pos || i < w_pos) {
752 DEBUGF(">");
753 } else {
754 DEBUGF("-");
758 else
760 if (i == r_pos && i == w_pos)
762 if (buf_ridx <= buf_widx) {
763 DEBUGF("RW");
764 } else {
765 DEBUGF("WR");
767 } else if (i == r_pos) {
768 DEBUGF("R");
769 } else if (i == w_pos) {
770 DEBUGF("W");
774 DEBUGF("|");
775 DEBUGF("\n");
776 #else
777 (void)width;
778 #endif
781 void print_progress(size_t amount, int file, int numfiles)
783 char buf[32];
784 rb->lcd_clear_display();
785 rb->snprintf(buf, sizeof(buf), "file %d of %d", file, numfiles);
786 rb->lcd_puts(0, 0, buf);
787 rb->snprintf(buf, sizeof(buf), "read: %ld", amount);
788 rb->lcd_puts(0, 1, buf);
789 rb->lcd_update();
792 bool buffer_init(void)
794 buffer = rb->plugin_get_audio_buffer(&buffer_len);
795 if (!buffer)
797 DEBUGF("couldn't allocate buffer\n");
798 return false;
800 buffer_len -= GUARD_SIZE;
801 guard_buffer = buffer + buffer_len;
803 buf_widx = 0;
804 buf_ridx = 0;
806 first_handle = NULL;
807 num_handles = 0;
809 conf_filechunk = AUDIO_DEFAULT_FILECHUNK;
811 return true;
814 bool disk_is_spinning(void)
816 return true;
820 static long codec_stack[4*DEFAULT_STACK_SIZE/sizeof(long)];
821 static struct thread_entry* codecthread_id;
823 static long bufopen_stack[DEFAULT_STACK_SIZE/sizeof(long)];
824 static struct thread_entry* bufopenthread_id;
826 bool done_playing = false;
828 #define MAX_HANDLES 16
830 int handles[MAX_HANDLES];
832 void codec_thread(void)
834 int idx = 0;
835 int fd = -1; /* used to write the files out as they are read */
836 unsigned char *data;
837 char outfile[MAX_PATH];
838 long read, total = 0;
840 while (1)
842 if (!done_playing)
844 if (handles[idx] > 0) {
846 if (fd < 0) {
847 rb->snprintf(outfile, MAX_PATH, "/file%d.mp3", idx);
848 fd = rb->open(outfile, O_CREAT|O_TRUNC|O_WRONLY);
849 if (fd < 0) {
850 DEBUGF("couldn't create file\n");
851 rb->splash(HZ, "couldn't create file");
855 do {
856 read = bufgetdata(handles[idx], GUARD_SIZE, &data);
857 if (read >= 0) {
858 read = MIN(read, GUARD_SIZE);
859 rb->write(fd, data, read);
860 total += read;
861 bufadvance(handles[idx], read);
862 print_progress(total, idx+1, num_files);
864 rb->sleep(HZ/20);
865 } while (read > 0);
867 if (read >= 0 && total >= 0) {
868 DEBUGF("read %ld bytes from handle %d\n", total, handles[idx]);
871 if (read == -2) {
872 DEBUGF("data for handle %d isn't ready\n", handles[idx]);
873 } else if (read == -1) {
874 DEBUGF("couldn't find handle %d\n", handles[idx]);
875 } else if (read == 0) {
876 DEBUGF("finished reading handle %d\n", handles[idx]);
877 bufclose(handles[idx]);
878 rb->close(fd);
879 fd = -1;
880 total = 0;
881 idx++;
883 if (idx >= num_files) {
884 done_playing = true;
885 break;
891 rb->sleep(HZ/4);
894 DEBUGF("removing the codec thread\n");
895 rb->remove_thread(NULL);
898 void bufopen_thread(void)
900 int idx = 0, ret;
901 while (idx < num_files)
903 ret = bufopen(files[idx], 0);
904 if (ret > 0) {
905 handles[idx++] = ret;
907 rb->sleep(HZ*8);
910 DEBUGF("bufopen thread finished\n");
911 rb->remove_thread(NULL);
915 /* this is the plugin entry point */
916 enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
918 (void)parameter;
919 rb = api;
921 buffer_init();
923 if (!test_ll())
925 DEBUGF("linked list test failed\n");
926 rb->splash(HZ, "linked list test failed");
927 return PLUGIN_ERROR;
930 codecthread_id = rb->create_thread(codec_thread,
931 codec_stack,
932 sizeof(codec_stack),
933 "codec"
934 IF_PRIO(,PRIORITY_PLAYBACK)
935 IF_COP(, CPU, false));
937 bufopenthread_id = rb->create_thread(bufopen_thread,
938 bufopen_stack,
939 sizeof(bufopen_stack),
940 "bufopen"
941 IF_PRIO(,PRIORITY_BACKGROUND)
942 IF_COP(, CPU, false));
944 if (!codecthread_id)
946 rb->splash(HZ, "failed to create codec thread");
947 return PLUGIN_ERROR;
949 else if (!bufopenthread_id)
951 rb->splash(HZ, "failed to create bufopen thread");
952 return PLUGIN_ERROR;
954 else
957 while (!done_playing)
959 struct memory_handle *m = first_handle;
960 while (m) {
961 if (m->filerem > 0 &&
962 m->data_len - INT_BUF_USED(m) > m->data_len/4)
964 /* there is free space to rebuffer */
965 DEBUGF("handle %d has %ld bytes available to rebuffer\n",
966 m->id, m->data_len - INT_BUF_USED(m));
967 buffer_handle(m->id);
969 m = m->next;
972 if (wasted_space() > buffer_len/4) {
973 DEBUGF("there is %ld bytes of wasted space\n", wasted_space());
974 struct memory_handle *m = first_handle;
975 while (m) {
976 if (m->filerem == 0) {
977 free_buffer(m->id);
979 m = m->next;
983 if (data_rem() > 0 &&
984 used_space() < buffer_len/4 &&
985 disk_is_spinning())
987 DEBUGF("%ld bytes left to buffer and the buffer is running low\n",
988 data_rem());
989 fill_buffer();
990 } else {
991 rb->sleep(HZ/2);
994 DEBUGF("done playing\n");
995 rb->yield();
998 rb->backlight_on();
999 DEBUGF("end of plugin\n");
1000 return PLUGIN_OK;