Now that headphone plug pause/resume should be better behaved, support it in mpegplay...
[kugel-rb.git] / apps / plugins / mpegplayer / stream_mgr.c
blobe79c5fd5959488eb59d251cf6d06424cdd05e245
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * AV stream manager implementation
12 * Copyright (c) 2007 Michael Sevakis
14 * All files in this archive are subject to the GNU General Public License.
15 * See the file COPYING in the source tree root for full license agreement.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include "plugin.h"
22 #include "mpegplayer.h"
23 #include "grey.h"
24 #include "mpeg_settings.h"
26 #ifndef HAVE_LCD_COLOR
27 GREY_INFO_STRUCT_IRAM
28 #endif
30 static struct event_queue stream_mgr_queue NOCACHEBSS_ATTR;
31 static struct queue_sender_list stream_mgr_queue_send NOCACHEBSS_ATTR;
32 static uint32_t stream_mgr_thread_stack[DEFAULT_STACK_SIZE*2/sizeof(uint32_t)];
34 struct stream_mgr stream_mgr NOCACHEBSS_ATTR;
36 /* Forward decs */
37 static int stream_on_close(void);
39 struct str_broadcast_data
41 long cmd; /* Command to send to stream */
42 intptr_t data; /* Data to send with command */
45 static inline void stream_mgr_lock(void)
47 rb->mutex_lock(&stream_mgr.str_mtx);
50 static inline void stream_mgr_unlock(void)
52 rb->mutex_unlock(&stream_mgr.str_mtx);
55 static inline void actl_lock(void)
57 rb->mutex_lock(&stream_mgr.actl_mtx);
60 static inline void actl_unlock(void)
62 rb->mutex_unlock(&stream_mgr.actl_mtx);
65 static inline void stream_mgr_post_msg(long id, intptr_t data)
67 rb->queue_post(stream_mgr.q, id, data);
70 static inline intptr_t stream_mgr_send_msg(long id, intptr_t data)
72 return rb->queue_send(stream_mgr.q, id, data);
75 static inline void stream_mgr_reply_msg(intptr_t retval)
77 rb->queue_reply(stream_mgr.q, retval);
80 int str_next_data_not_ready(struct stream *str)
82 /* Save the current window since it actually might be ready by the time
83 * the registration is received by buffering. */
84 off_t win_right = str->hdr.win_right;
86 if (str->hdr.win_right < disk_buf.filesize - MIN_BUFAHEAD &&
87 disk_buf.filesize > MIN_BUFAHEAD)
89 /* Set right edge to where probing left off + the minimum margin */
90 str->hdr.win_right += MIN_BUFAHEAD;
92 else
94 /* Request would be passed the end of the file */
95 str->hdr.win_right = disk_buf.filesize;
98 switch (disk_buf_send_msg(DISK_BUF_DATA_NOTIFY, (intptr_t)str))
100 case DISK_BUF_NOTIFY_OK:
101 /* Was ready - restore window and process */
102 str->hdr.win_right = win_right;
103 return STREAM_OK;
105 case DISK_BUF_NOTIFY_ERROR:
106 /* Error - quit parsing */
107 str_end_of_stream(str);
108 return STREAM_DATA_END;
110 default:
111 /* Not ready - go wait for notification from buffering. */
112 str->pkt_flags = 0;
113 return STREAM_DATA_NOT_READY;
117 void str_data_notify_received(struct stream *str)
119 /* Normalize win_right back to the packet length */
120 if (str->state == SSTATE_END)
121 return;
123 if (str->curr_packet == NULL)
125 /* Nothing was yet parsed since init */
126 str->hdr.win_right = str->hdr.win_left;
128 else
130 /* Restore window based upon current packet */
131 str->hdr.win_right = str->hdr.win_left +
132 (str->curr_packet_end - str->curr_packet);
136 /* Set stream manager to a "no-file" state */
137 static void stream_mgr_init_state(void)
139 stream_mgr.filename = NULL;
140 stream_mgr.resume_time = INVALID_TIMESTAMP;
141 stream_mgr.seeked = false;
144 /* Add a stream to the playback pool */
145 void stream_add_stream(struct stream *str)
147 actl_lock();
149 list_remove_item(&str->l);
150 list_add_item(&stream_mgr.strl, &str->l);
152 actl_unlock();
155 /* Callback for various list-moving operations */
156 static bool strl_enum_callback(struct list_item *item, intptr_t data)
158 actl_lock();
160 list_remove_item(item);
162 if (data == 1)
163 list_add_item(&stream_mgr.actl, item);
165 actl_unlock();
167 return true;
170 /* Clear all streams from active and playback pools */
171 void stream_remove_streams(void)
173 list_enum_items(&stream_mgr.strl, strl_enum_callback, 0);
176 /* Move the playback pool to the active list */
177 void move_strl_to_actl(void)
179 list_enum_items(&stream_mgr.strl, strl_enum_callback, 1);
182 /* Remove a stream from the active list and return it to the pool */
183 static bool actl_stream_remove(struct stream *str)
185 if (list_is_member(&stream_mgr.actl, &str->l))
187 actl_lock();
189 list_remove_item(&str->l);
190 list_add_item(&stream_mgr.strl, &str->l);
192 actl_unlock();
193 return true;
196 return false;
199 /* Broadcast a message to all active streams */
200 static bool actl_stream_broadcast_callback(struct list_item *item,
201 struct str_broadcast_data *sbd)
203 struct stream *str = TYPE_FROM_MEMBER(struct stream, item, l);
205 switch (sbd->cmd)
207 case STREAM_PLAY:
208 case STREAM_PAUSE:
209 break;
211 case STREAM_STOP:
212 if (sbd->data != 0)
214 actl_lock();
216 list_remove_item(item);
217 list_add_item(&stream_mgr.strl, item);
219 actl_unlock();
220 sbd->data = 0;
222 break;
224 default:
225 return false;
228 str_send_msg(str, sbd->cmd, sbd->data);
229 return true;
232 static void actl_stream_broadcast(int cmd, intptr_t data)
234 struct str_broadcast_data sbd;
235 sbd.cmd = cmd;
236 sbd.data = data;
237 list_enum_items(&stream_mgr.actl,
238 (list_enum_callback_t)actl_stream_broadcast_callback,
239 (intptr_t)&sbd);
242 /* Set the current base clock */
243 static void set_stream_clock(uint32_t time)
245 /* Fudge: Start clock 100ms early to allow for some filling time */
246 if (time > 100*TS_SECOND/1000)
247 time -= 100*TS_SECOND/1000;
248 else
249 time = 0;
251 pcm_output_set_clock(TS_TO_TICKS(time));
254 static void stream_start_playback(uint32_t time, bool fill_buffer)
256 if (stream_mgr.seeked)
258 /* Clear any seeked status */
259 stream_mgr.seeked = false;
261 /* Flush old PCM data */
262 pcm_output_flush();
264 /* Set the master clock */
265 set_stream_clock(time);
267 /* Make sure streams are back in active pool */
268 move_strl_to_actl();
270 /* Prepare the parser and associated streams */
271 parser_prepare_streaming();
274 /* Start buffer which optional force fill */
275 disk_buf_send_msg(STREAM_PLAY, fill_buffer);
277 /* Tell each stream to start - may generate end of stream signals
278 * now - we'll handle this when finished */
279 actl_stream_broadcast(STREAM_PLAY, 0);
281 /* Actually start the clock */
282 pcm_output_play_pause(true);
285 /* Return the play time relative to the specified play time */
286 static uint32_t time_from_whence(uint32_t time, int whence)
288 int64_t currtime;
289 uint32_t start;
291 switch (whence)
293 case SEEK_SET:
294 /* Set the current time (time = unsigned offset from 0) */
295 if (time > str_parser.duration)
296 time = str_parser.duration;
297 break;
298 case SEEK_CUR:
299 /* Seek forward or backward from the current time
300 * (time = signed offset from current) */
301 currtime = stream_get_seek_time(&start);
302 currtime -= start;
303 currtime += (int32_t)time;
305 if (currtime < 0)
306 currtime = 0;
307 else if ((uint64_t)currtime > str_parser.duration)
308 currtime = str_parser.duration;
310 time = (uint32_t)currtime;
311 break;
312 case SEEK_END:
313 /* Seek from the end (time = unsigned offset from end) */
314 if (time > str_parser.duration)
315 time = str_parser.duration;
316 time = str_parser.duration - time;
317 break;
320 return time;
323 /* Handle seeking details if playing or paused */
324 static uint32_t stream_seek_intl(uint32_t time, int whence,
325 int status, bool *was_buffering)
327 if (status != STREAM_STOPPED)
329 bool wb;
331 /* Place streams in a non-running state - keep them on actl */
332 actl_stream_broadcast(STREAM_STOP, 0);
334 /* Stop all buffering or else risk clobbering random-access data */
335 wb = disk_buf_send_msg(STREAM_STOP, 0);
337 if (was_buffering != NULL)
338 *was_buffering = wb;
341 time = time_from_whence(time, whence);
343 stream_mgr.seeked = true;
345 return parser_seek_time(time);
348 /* Store the resume time at the last seek/current clock point */
349 static void stream_remember_resume_time(void)
351 /* Assume invalidity */
352 stream_mgr.resume_time = 0;
354 if (stream_can_seek())
356 /* Read the current stream time or the last seeked position */
357 uint32_t start;
358 uint32_t time = stream_get_seek_time(&start);
360 if (time >= str_parser.start_pts && time <= str_parser.end_pts)
362 /* Save the current stream time */
363 stream_mgr.resume_time = time - start;
368 /* Handle STREAM_OPEN */
369 void stream_on_open(const char *filename)
371 int err = STREAM_ERROR;
373 stream_mgr_lock();
375 trigger_cpu_boost();
377 /* Open the video file */
378 if (disk_buf_open(filename) >= 0)
380 /* Initialize the parser */
381 err = parser_init_stream();
383 if (err >= STREAM_OK)
385 /* File ok - save the opened filename */
386 stream_mgr.filename = filename;
390 /* If error - cleanup */
391 if (err < STREAM_OK)
392 stream_on_close();
394 cancel_cpu_boost();
396 stream_mgr_unlock();
398 stream_mgr_reply_msg(err);
401 /* Handler STREAM_PLAY */
402 static void stream_on_play(void)
404 int status = stream_mgr.status;
406 stream_mgr_lock();
408 if (status == STREAM_STOPPED)
410 uint32_t start;
412 /* We just say we're playing now */
413 stream_mgr.status = STREAM_PLAYING;
415 /* Reply with previous state */
416 stream_mgr_reply_msg(status);
418 trigger_cpu_boost();
420 /* Seek to initial position and set clock to that time */
422 /* Save the resume time */
423 stream_remember_resume_time();
425 /* Prepare seek to start point */
426 start = stream_seek_intl(stream_mgr.resume_time, SEEK_SET,
427 STREAM_STOPPED, NULL);
429 /* Sync and start - force buffer fill */
430 stream_start_playback(start, true);
432 else
434 /* Reply with previous state */
435 stream_mgr_reply_msg(status);
438 stream_mgr_unlock();
441 /* Handle STREAM_PAUSE */
442 static void stream_on_pause(void)
444 int status = stream_mgr.status;
446 stream_mgr_lock();
448 /* Reply with previous state */
449 stream_mgr_reply_msg(status);
451 if (status == STREAM_PLAYING)
453 /* Pause the clock */
454 pcm_output_play_pause(false);
456 /* Pause each active stream */
457 actl_stream_broadcast(STREAM_PAUSE, 0);
459 /* Pause the disk buffer - buffer may continue filling */
460 disk_buf_send_msg(STREAM_PAUSE, false);
462 /* Unboost the CPU */
463 cancel_cpu_boost();
465 /* Offically paused */
466 stream_mgr.status = STREAM_PAUSED;
469 stream_mgr_unlock();
472 /* Handle STREAM_RESUME */
473 static void stream_on_resume(void)
475 int status = stream_mgr.status;
477 stream_mgr_lock();
479 /* Reply with previous state */
480 stream_mgr_reply_msg(status);
482 if (status == STREAM_PAUSED)
484 /* Boost the CPU */
485 trigger_cpu_boost();
487 /* Sync and start - no force buffering */
488 stream_start_playback(str_parser.last_seek_time, false);
490 /* Officially playing */
491 stream_mgr.status = STREAM_PLAYING;
494 stream_mgr_unlock();
497 /* Handle STREAM_STOP */
498 static void stream_on_stop(bool reply)
500 int status = stream_mgr.status;
502 stream_mgr_lock();
504 if (reply)
505 stream_mgr_reply_msg(status);
507 if (status != STREAM_STOPPED)
509 /* Pause the clock */
510 pcm_output_play_pause(false);
512 /* Update the resume time info */
513 stream_remember_resume_time();
515 /* Not stopped = paused or playing */
516 stream_mgr.seeked = false;
518 /* Stop buffering */
519 disk_buf_send_msg(STREAM_STOP, 0);
521 /* Clear any still-active streams and remove from actl */
522 actl_stream_broadcast(STREAM_STOP, 1);
524 /* Stop PCM output (and clock) */
525 pcm_output_stop();
527 /* Cancel our processor boost */
528 cancel_cpu_boost();
530 stream_mgr.status = STREAM_STOPPED;
533 stream_mgr_unlock();
536 /* Handle STREAM_SEEK */
537 static void stream_on_seek(struct stream_seek_data *skd)
539 uint32_t time = skd->time;
540 int whence = skd->whence;
542 switch (whence)
544 case SEEK_SET:
545 case SEEK_CUR:
546 case SEEK_END:
547 if (stream_mgr.filename == NULL)
548 break;
550 /* Keep things spinning if already doing so */
551 stream_keep_disk_active();
553 /* Have data - reply in order to acquire lock */
554 stream_mgr_reply_msg(STREAM_OK);
556 stream_mgr_lock();
558 if (stream_can_seek())
560 bool buffer;
562 if (stream_mgr.status == STREAM_PLAYING)
564 /* Keep clock from advancing while seeking */
565 pcm_output_play_pause(false);
568 time = stream_seek_intl(time, whence, stream_mgr.status, &buffer);
569 stream_remember_resume_time();
571 if (stream_mgr.status == STREAM_PLAYING)
573 /* Sync and restart - no force buffering */
574 stream_start_playback(time, buffer);
578 stream_mgr_unlock();
579 return;
582 /* Invalid parameter or no file */
583 stream_mgr_reply_msg(STREAM_ERROR);
586 /* Handle STREAM_CLOSE */
587 static int stream_on_close(void)
589 int status = STREAM_STOPPED;
591 stream_mgr_lock();
593 /* Any open file? */
594 if (stream_mgr.filename != NULL)
596 /* Yes - hide video */
597 stream_show_vo(false);
598 /* Stop any playback */
599 status = stream_mgr.status;
600 stream_on_stop(false);
601 /* Tell parser file is finished */
602 parser_close_stream();
603 /* Close file */
604 disk_buf_close();
605 /* Reinitialize manager */
606 stream_mgr_init_state();
609 stream_mgr_unlock();
611 return status;
614 /* Handle STREAM_EV_COMPLETE */
615 static void stream_on_ev_complete(struct stream *str)
617 stream_mgr_lock();
619 /* Stream is active? */
620 if (actl_stream_remove(str))
622 /* No - remove this stream from the active list */
623 DEBUGF(" finished: 0x%02x\n", str->id);
624 if (list_is_empty(&stream_mgr.actl))
626 /* All streams have acked - stop playback */
627 stream_on_stop(false);
628 stream_mgr.resume_time = 0; /* Played to end - no resume */
630 else
632 /* Stream is done - stop it and place back in pool */
633 str_send_msg(str, STREAM_STOP, 1);
637 stream_mgr_unlock();
640 /* Callback for stream to notify about events internal to them */
641 void stream_generate_event(struct stream *str, long id, intptr_t data)
643 if (str == NULL)
644 return;
646 switch (id)
648 case STREAM_EV_COMPLETE:
649 /* The last stream has ended */
650 stream_mgr_post_msg(STREAM_EV_COMPLETE, (intptr_t)str);
651 break;
654 (void)data;
657 /* Clear any particular notification for which a stream registered */
658 void stream_clear_notify(struct stream *str, int for_msg)
660 switch (for_msg)
662 case DISK_BUF_DATA_NOTIFY:
663 disk_buf_send_msg(DISK_BUF_CLEAR_DATA_NOTIFY, (intptr_t)str);
664 break;
668 /* Show/hide the video output */
669 bool stream_show_vo(bool show)
671 bool vis;
672 stream_mgr_lock();
674 vis = parser_send_video_msg(VIDEO_DISPLAY_SHOW, show);
675 #ifndef HAVE_LCD_COLOR
676 grey_show(show);
677 #endif
678 stream_mgr_unlock();
680 return vis;
683 /* Query the visibility of video output */
684 bool stream_vo_is_visible(void)
686 bool vis;
687 stream_mgr_lock();
688 vis = parser_send_video_msg(VIDEO_DISPLAY_IS_VISIBLE, 0);
689 stream_mgr_unlock();
690 return vis;
693 /* Return the video dimensions */
694 bool stream_vo_get_size(struct vo_ext *sz)
696 bool retval = false;
698 stream_mgr_lock();
700 if (str_parser.dims.w > 0 && str_parser.dims.h > 0)
702 *sz = str_parser.dims;
703 retval = true;
706 stream_mgr_unlock();
708 return retval;
711 void stream_vo_set_clip(const struct vo_rect *rc)
713 stream_mgr_lock();
715 if (rc)
717 stream_mgr.parms.rc = *rc;
718 rc = &stream_mgr.parms.rc;
721 parser_send_video_msg(VIDEO_SET_CLIP_RECT, (intptr_t)rc);
723 stream_mgr_unlock();
726 #ifndef HAVE_LCD_COLOR
727 /* Show/hide the gray video overlay (independently of vo visibility). */
728 void stream_gray_show(bool show)
730 stream_mgr_lock();
732 grey_show(show);
734 stream_mgr_unlock();
737 #endif /* !HAVE_LCD_COLOR */
739 /* Display a thumbnail at the last seek point */
740 bool stream_display_thumb(const struct vo_rect *rc)
742 bool retval;
744 if (rc == NULL)
745 return false;
747 stream_mgr_lock();
749 stream_mgr.parms.rc = *rc;
750 retval = parser_send_video_msg(VIDEO_PRINT_THUMBNAIL,
751 (intptr_t)&stream_mgr.parms.rc);
753 stream_mgr_unlock();
755 return retval;
758 bool stream_draw_frame(bool no_prepare)
760 bool retval;
761 stream_mgr_lock();
763 retval = parser_send_video_msg(VIDEO_PRINT_FRAME, no_prepare);
765 stream_mgr_unlock();
767 return retval;
770 /* Return the time playback should resume if interrupted */
771 uint32_t stream_get_resume_time(void)
773 uint32_t resume_time;
775 /* A stop request is async and replies before setting this - must lock */
776 stream_mgr_lock();
778 resume_time = stream_mgr.resume_time;
780 stream_mgr_unlock();
782 return resume_time;
785 uint32_t stream_get_seek_time(uint32_t *start)
787 uint32_t time;
789 stream_mgr_lock();
791 if (stream_mgr.seeked)
793 time = str_parser.last_seek_time;
795 else
797 time = TICKS_TO_TS(pcm_output_get_clock());
799 /* Clock can be start early so keep in range */
800 if (time < str_parser.start_pts)
801 time = str_parser.start_pts;
804 if (start != NULL)
805 *start = str_parser.start_pts;
807 stream_mgr_unlock();
809 return time;
812 /* Wait for a state transistion to complete */
813 void stream_wait_status(void)
815 stream_mgr_lock();
816 stream_mgr_unlock();
819 /* Returns the smallest file window that includes all active streams'
820 * windows */
821 static bool stream_get_window_callback(struct list_item *item,
822 struct stream_window *sw)
824 struct stream *str = TYPE_FROM_MEMBER(struct stream, item, l);
825 off_t swl = str->hdr.win_left;
826 off_t swr = str->hdr.win_right;
828 if (swl < sw->left)
829 sw->left = swl;
831 if (swr > sw->right)
832 sw->right = swr;
834 return true;
837 bool stream_get_window(struct stream_window *sw)
839 if (sw == NULL)
840 return false;
842 sw->left = LONG_MAX;
843 sw->right = LONG_MIN;
845 actl_lock();
846 list_enum_items(&stream_mgr.actl,
847 (list_enum_callback_t)stream_get_window_callback,
848 (intptr_t)sw);
849 actl_unlock();
851 return sw->left <= sw->right;
854 /* Playback control thread */
855 static void stream_mgr_thread(void)
857 struct queue_event ev;
859 while (1)
861 rb->queue_wait(stream_mgr.q, &ev);
863 switch (ev.id)
865 case STREAM_OPEN:
866 stream_on_open((const char *)ev.data);
867 break;
869 case STREAM_CLOSE:
870 stream_on_close();
871 break;
873 case STREAM_PLAY:
874 stream_on_play();
875 break;
877 case STREAM_PAUSE:
878 if (ev.data)
879 stream_on_resume();
880 else
881 stream_on_pause();
882 break;
884 case STREAM_STOP:
885 stream_on_stop(true);
886 break;
888 case STREAM_SEEK:
889 stream_on_seek((struct stream_seek_data *)ev.data);
890 break;
892 case STREAM_EV_COMPLETE:
893 stream_on_ev_complete((struct stream *)ev.data);
894 break;
896 case STREAM_QUIT:
897 if (stream_mgr.status != STREAM_STOPPED)
898 stream_on_stop(false);
899 return;
904 /* Stream command interface APIs */
906 /* Opens a new file */
907 int stream_open(const char *filename)
909 if (stream_mgr.thread != NULL)
910 return stream_mgr_send_msg(STREAM_OPEN, (intptr_t)filename);
911 return STREAM_ERROR;
914 /* Plays the current file starting at time 'start' */
915 int stream_play(void)
917 if (stream_mgr.thread != NULL)
918 return stream_mgr_send_msg(STREAM_PLAY, 0);
919 return STREAM_ERROR;
922 /* Pauses playback if playing */
923 int stream_pause(void)
925 if (stream_mgr.thread != NULL)
926 return stream_mgr_send_msg(STREAM_PAUSE, false);
927 return STREAM_ERROR;
930 /* Resumes playback if paused */
931 int stream_resume(void)
933 if (stream_mgr.thread != NULL)
934 return stream_mgr_send_msg(STREAM_PAUSE, true);
935 return STREAM_ERROR;
938 /* Stops playback if not stopped */
939 int stream_stop(void)
941 if (stream_mgr.thread != NULL)
942 return stream_mgr_send_msg(STREAM_STOP, 0);
943 return STREAM_ERROR;
946 /* Seeks playback time to/by the specified time */
947 int stream_seek(uint32_t time, int whence)
949 int ret;
951 if (stream_mgr.thread == NULL)
952 return STREAM_ERROR;
954 stream_mgr_lock();
956 stream_mgr.parms.skd.time = time;
957 stream_mgr.parms.skd.whence = whence;
959 ret = stream_mgr_send_msg(STREAM_SEEK, (intptr_t)&stream_mgr.parms.skd);
961 stream_mgr_unlock();
963 return ret;
966 /* Closes the current file */
967 int stream_close(void)
969 if (stream_mgr.thread != NULL)
970 return stream_mgr_send_msg(STREAM_CLOSE, 0);
971 return STREAM_ERROR;
974 /* Initializes the playback engine */
975 int stream_init(void)
977 void *mem;
978 size_t memsize;
980 stream_mgr.status = STREAM_STOPPED;
981 stream_mgr_init_state();
982 list_initialize(&stream_mgr.actl);
984 /* Initialize our window to the outside world first */
985 rb->mutex_init(&stream_mgr.str_mtx);
986 rb->mutex_init(&stream_mgr.actl_mtx);
988 stream_mgr.q = &stream_mgr_queue;
989 rb->queue_init(stream_mgr.q, false);
990 rb->queue_enable_queue_send(stream_mgr.q, &stream_mgr_queue_send);
992 /* sets audiosize and returns buffer pointer */
993 mem = rb->plugin_get_audio_buffer(&memsize);
995 /* Initialize non-allocator blocks first */
996 #ifndef HAVE_LCD_COLOR
997 bool success;
998 long graysize;
999 void *graymem;
1001 #ifdef PROC_NEEDS_CACHEALIGN
1002 /* This can run on another processor - align data */
1003 memsize = CACHEALIGN_BUFFER(&mem, memsize);
1004 graymem = UNCACHED_ADDR(mem);
1005 #else
1006 graymem = mem;
1007 #endif
1009 success = grey_init(rb, graymem, memsize, true, LCD_WIDTH,
1010 LCD_HEIGHT, &graysize);
1012 /* This can run on another processor - align size */
1013 graysize = CACHEALIGN_UP(graysize);
1015 mem += graysize;
1016 memsize -= graysize;
1018 if (!success || (ssize_t)memsize <= 0)
1020 rb->splash(HZ, "greylib init failed!");
1021 return STREAM_ERROR;
1024 grey_clear_display();
1025 #endif /* !HAVE_LCD_COLOR */
1027 stream_mgr.thread = rb->create_thread(stream_mgr_thread,
1028 stream_mgr_thread_stack, sizeof(stream_mgr_thread_stack),
1029 0, "mpgstream_mgr" IF_PRIO(, PRIORITY_SYSTEM) IF_COP(, CPU));
1031 if (stream_mgr.thread == NULL)
1033 rb->splash(HZ, "Could not create stream manager thread!");
1034 return STREAM_ERROR;
1037 /* Wait for thread to initialize */
1038 stream_mgr_send_msg(STREAM_NULL, 0);
1040 /* Initialise our malloc buffer */
1041 if (!mpeg_alloc_init(mem, memsize))
1043 rb->splash(HZ, "Out of memory in stream_init");
1045 /* These inits use the allocator */
1046 else if (!pcm_output_init())
1048 rb->splash(HZ, "Could not initialize PCM!");
1050 else if (!audio_thread_init())
1052 rb->splash(HZ, "Cannot create audio thread!");
1054 else if (!video_thread_init())
1056 rb->splash(HZ, "Cannot create video thread!");
1058 /* Disk buffer takes max allotment of what's left so it must be last */
1059 else if (!disk_buf_init())
1061 rb->splash(HZ, "Cannot create buffering thread!");
1063 else if (!parser_init())
1065 rb->splash(HZ, "Parser init failed!");
1067 else
1069 return STREAM_OK;
1072 return STREAM_ERROR;
1075 /* Cleans everything up */
1076 void stream_exit(void)
1078 stream_close();
1080 /* Stop the threads and wait for them to terminate */
1081 video_thread_exit();
1082 audio_thread_exit();
1083 disk_buf_exit();
1084 pcm_output_exit();
1086 if (stream_mgr.thread != NULL)
1088 stream_mgr_post_msg(STREAM_QUIT, 0);
1089 rb->thread_wait(stream_mgr.thread);
1090 stream_mgr.thread = NULL;
1093 #ifndef HAVE_LCD_COLOR
1094 grey_release();
1095 #endif