1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
22 #include "mpegplayer.h"
24 #include "mpeg_settings.h"
26 #ifndef HAVE_LCD_COLOR
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
;
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
;
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
;
105 case DISK_BUF_NOTIFY_ERROR
:
106 /* Error - quit parsing */
107 str_end_of_stream(str
);
108 return STREAM_DATA_END
;
111 /* Not ready - go wait for notification from buffering. */
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
)
123 if (str
->curr_packet
== NULL
)
125 /* Nothing was yet parsed since init */
126 str
->hdr
.win_right
= str
->hdr
.win_left
;
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
)
149 list_remove_item(&str
->l
);
150 list_add_item(&stream_mgr
.strl
, &str
->l
);
155 /* Callback for various list-moving operations */
156 static bool strl_enum_callback(struct list_item
*item
, intptr_t data
)
160 list_remove_item(item
);
163 list_add_item(&stream_mgr
.actl
, item
);
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
))
189 list_remove_item(&str
->l
);
190 list_add_item(&stream_mgr
.strl
, &str
->l
);
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
);
216 list_remove_item(item
);
217 list_add_item(&stream_mgr
.strl
, item
);
228 str_send_msg(str
, sbd
->cmd
, sbd
->data
);
232 static void actl_stream_broadcast(int cmd
, intptr_t data
)
234 struct str_broadcast_data sbd
;
237 list_enum_items(&stream_mgr
.actl
,
238 (list_enum_callback_t
)actl_stream_broadcast_callback
,
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;
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 */
264 /* Set the master clock */
265 set_stream_clock(time
);
267 /* Make sure streams are back in active pool */
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
)
294 /* Set the current time (time = unsigned offset from 0) */
295 if (time
> str_parser
.duration
)
296 time
= str_parser
.duration
;
299 /* Seek forward or backward from the current time
300 * (time = signed offset from current) */
301 currtime
= stream_get_seek_time(&start
);
303 currtime
+= (int32_t)time
;
307 else if ((uint64_t)currtime
> str_parser
.duration
)
308 currtime
= str_parser
.duration
;
310 time
= (uint32_t)currtime
;
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
;
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
)
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
)
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 */
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
;
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 */
398 stream_mgr_reply_msg(err
);
401 /* Handler STREAM_PLAY */
402 static void stream_on_play(void)
404 int status
= stream_mgr
.status
;
408 if (status
== STREAM_STOPPED
)
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
);
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);
434 /* Reply with previous state */
435 stream_mgr_reply_msg(status
);
441 /* Handle STREAM_PAUSE */
442 static void stream_on_pause(void)
444 int status
= stream_mgr
.status
;
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 */
465 /* Offically paused */
466 stream_mgr
.status
= STREAM_PAUSED
;
472 /* Handle STREAM_RESUME */
473 static void stream_on_resume(void)
475 int status
= stream_mgr
.status
;
479 /* Reply with previous state */
480 stream_mgr_reply_msg(status
);
482 if (status
== STREAM_PAUSED
)
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
;
497 /* Handle STREAM_STOP */
498 static void stream_on_stop(bool reply
)
500 int status
= stream_mgr
.status
;
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;
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) */
527 /* Cancel our processor boost */
530 stream_mgr
.status
= STREAM_STOPPED
;
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
;
547 if (stream_mgr
.filename
== NULL
)
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
);
558 if (stream_can_seek())
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
);
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
;
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();
605 /* Reinitialize manager */
606 stream_mgr_init_state();
614 /* Handle STREAM_EV_COMPLETE */
615 static void stream_on_ev_complete(struct stream
*str
)
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 */
632 /* Stream is done - stop it and place back in pool */
633 str_send_msg(str
, STREAM_STOP
, 1);
640 /* Callback for stream to notify about events internal to them */
641 void stream_generate_event(struct stream
*str
, long id
, intptr_t data
)
648 case STREAM_EV_COMPLETE
:
649 /* The last stream has ended */
650 stream_mgr_post_msg(STREAM_EV_COMPLETE
, (intptr_t)str
);
657 /* Clear any particular notification for which a stream registered */
658 void stream_clear_notify(struct stream
*str
, int for_msg
)
662 case DISK_BUF_DATA_NOTIFY
:
663 disk_buf_send_msg(DISK_BUF_CLEAR_DATA_NOTIFY
, (intptr_t)str
);
668 /* Show/hide the video output */
669 bool stream_show_vo(bool show
)
674 vis
= parser_send_video_msg(VIDEO_DISPLAY_SHOW
, show
);
675 #ifndef HAVE_LCD_COLOR
683 /* Query the visibility of video output */
684 bool stream_vo_is_visible(void)
688 vis
= parser_send_video_msg(VIDEO_DISPLAY_IS_VISIBLE
, 0);
693 /* Return the video dimensions */
694 bool stream_vo_get_size(struct vo_ext
*sz
)
700 if (str_parser
.dims
.w
> 0 && str_parser
.dims
.h
> 0)
702 *sz
= str_parser
.dims
;
711 void stream_vo_set_clip(const struct vo_rect
*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
);
726 #ifndef HAVE_LCD_COLOR
727 /* Show/hide the gray video overlay (independently of vo visibility). */
728 void stream_gray_show(bool show
)
737 #endif /* !HAVE_LCD_COLOR */
739 /* Display a thumbnail at the last seek point */
740 bool stream_display_thumb(const struct vo_rect
*rc
)
749 stream_mgr
.parms
.rc
= *rc
;
750 retval
= parser_send_video_msg(VIDEO_PRINT_THUMBNAIL
,
751 (intptr_t)&stream_mgr
.parms
.rc
);
758 bool stream_draw_frame(bool no_prepare
)
763 retval
= parser_send_video_msg(VIDEO_PRINT_FRAME
, no_prepare
);
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 */
778 resume_time
= stream_mgr
.resume_time
;
785 uint32_t stream_get_seek_time(uint32_t *start
)
791 if (stream_mgr
.seeked
)
793 time
= str_parser
.last_seek_time
;
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
;
805 *start
= str_parser
.start_pts
;
812 /* Wait for a state transistion to complete */
813 void stream_wait_status(void)
819 /* Returns the smallest file window that includes all active streams'
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
;
837 bool stream_get_window(struct stream_window
*sw
)
843 sw
->right
= LONG_MIN
;
846 list_enum_items(&stream_mgr
.actl
,
847 (list_enum_callback_t
)stream_get_window_callback
,
851 return sw
->left
<= sw
->right
;
854 /* Playback control thread */
855 static void stream_mgr_thread(void)
857 struct queue_event ev
;
861 rb
->queue_wait(stream_mgr
.q
, &ev
);
866 stream_on_open((const char *)ev
.data
);
885 stream_on_stop(true);
889 stream_on_seek((struct stream_seek_data
*)ev
.data
);
892 case STREAM_EV_COMPLETE
:
893 stream_on_ev_complete((struct stream
*)ev
.data
);
897 if (stream_mgr
.status
!= STREAM_STOPPED
)
898 stream_on_stop(false);
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
);
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);
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);
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);
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);
946 /* Seeks playback time to/by the specified time */
947 int stream_seek(uint32_t time
, int whence
)
951 if (stream_mgr
.thread
== NULL
)
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
);
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);
974 /* Initializes the playback engine */
975 int stream_init(void)
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
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
);
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
);
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!");
1072 return STREAM_ERROR
;
1075 /* Cleans everything up */
1076 void stream_exit(void)
1080 /* Stop the threads and wait for them to terminate */
1081 video_thread_exit();
1082 audio_thread_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