diff options
Diffstat (limited to 'apps/plugins/mpegplayer/stream_mgr.c')
-rw-r--r-- | apps/plugins/mpegplayer/stream_mgr.c | 1163 |
1 files changed, 1163 insertions, 0 deletions
diff --git a/apps/plugins/mpegplayer/stream_mgr.c b/apps/plugins/mpegplayer/stream_mgr.c new file mode 100644 index 0000000000..3cac8c0f57 --- /dev/null +++ b/apps/plugins/mpegplayer/stream_mgr.c | |||
@@ -0,0 +1,1163 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * AV stream manager implementation | ||
11 | * | ||
12 | * Copyright (c) 2007 Michael Sevakis | ||
13 | * | ||
14 | * This program is free software; you can redistribute it and/or | ||
15 | * modify it under the terms of the GNU General Public License | ||
16 | * as published by the Free Software Foundation; either version 2 | ||
17 | * of the License, or (at your option) any later version. | ||
18 | * | ||
19 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
20 | * KIND, either express or implied. | ||
21 | * | ||
22 | ****************************************************************************/ | ||
23 | #include "plugin.h" | ||
24 | #include "mpegplayer.h" | ||
25 | #include "lib/grey.h" | ||
26 | #include "mpeg_settings.h" | ||
27 | |||
28 | #ifndef HAVE_LCD_COLOR | ||
29 | GREY_INFO_STRUCT_IRAM | ||
30 | #endif | ||
31 | |||
32 | static struct event_queue stream_mgr_queue SHAREDBSS_ATTR; | ||
33 | static struct queue_sender_list stream_mgr_queue_send SHAREDBSS_ATTR; | ||
34 | static uint32_t stream_mgr_thread_stack[DEFAULT_STACK_SIZE*2/sizeof(uint32_t)]; | ||
35 | |||
36 | struct stream_mgr stream_mgr SHAREDBSS_ATTR; | ||
37 | |||
38 | /* Forward decs */ | ||
39 | static int stream_on_close(void); | ||
40 | |||
41 | struct str_broadcast_data | ||
42 | { | ||
43 | long cmd; /* Command to send to stream */ | ||
44 | intptr_t data; /* Data to send with command */ | ||
45 | }; | ||
46 | |||
47 | static inline void stream_mgr_lock(void) | ||
48 | { | ||
49 | rb->mutex_lock(&stream_mgr.str_mtx); | ||
50 | } | ||
51 | |||
52 | static inline void stream_mgr_unlock(void) | ||
53 | { | ||
54 | rb->mutex_unlock(&stream_mgr.str_mtx); | ||
55 | } | ||
56 | |||
57 | static inline void actl_lock(void) | ||
58 | { | ||
59 | rb->mutex_lock(&stream_mgr.actl_mtx); | ||
60 | } | ||
61 | |||
62 | static inline void actl_unlock(void) | ||
63 | { | ||
64 | rb->mutex_unlock(&stream_mgr.actl_mtx); | ||
65 | } | ||
66 | |||
67 | static inline void stream_mgr_post_msg(long id, intptr_t data) | ||
68 | { | ||
69 | rb->queue_post(stream_mgr.q, id, data); | ||
70 | } | ||
71 | |||
72 | static inline intptr_t stream_mgr_send_msg(long id, intptr_t data) | ||
73 | { | ||
74 | return rb->queue_send(stream_mgr.q, id, data); | ||
75 | } | ||
76 | |||
77 | static inline void stream_mgr_reply_msg(intptr_t retval) | ||
78 | { | ||
79 | rb->queue_reply(stream_mgr.q, retval); | ||
80 | } | ||
81 | |||
82 | int str_next_data_not_ready(struct stream *str) | ||
83 | { | ||
84 | /* Save the current window since it actually might be ready by the time | ||
85 | * the registration is received by buffering. */ | ||
86 | off_t win_right = str->hdr.win_right; | ||
87 | |||
88 | if (str->hdr.win_right < disk_buf.filesize - MIN_BUFAHEAD && | ||
89 | disk_buf.filesize > MIN_BUFAHEAD) | ||
90 | { | ||
91 | /* Set right edge to where probing left off + the minimum margin */ | ||
92 | str->hdr.win_right += MIN_BUFAHEAD; | ||
93 | } | ||
94 | else | ||
95 | { | ||
96 | /* Request would be passed the end of the file */ | ||
97 | str->hdr.win_right = disk_buf.filesize; | ||
98 | } | ||
99 | |||
100 | switch (disk_buf_send_msg(DISK_BUF_DATA_NOTIFY, (intptr_t)str)) | ||
101 | { | ||
102 | case DISK_BUF_NOTIFY_OK: | ||
103 | /* Was ready - restore window and process */ | ||
104 | str->hdr.win_right = win_right; | ||
105 | return STREAM_OK; | ||
106 | |||
107 | case DISK_BUF_NOTIFY_ERROR: | ||
108 | /* Error - quit parsing */ | ||
109 | str_end_of_stream(str); | ||
110 | return STREAM_DATA_END; | ||
111 | |||
112 | default: | ||
113 | /* Not ready - go wait for notification from buffering. */ | ||
114 | str->pkt_flags = 0; | ||
115 | return STREAM_DATA_NOT_READY; | ||
116 | } | ||
117 | } | ||
118 | |||
119 | void str_data_notify_received(struct stream *str) | ||
120 | { | ||
121 | /* Normalize win_right back to the packet length */ | ||
122 | if (str->state == SSTATE_END) | ||
123 | return; | ||
124 | |||
125 | if (str->curr_packet == NULL) | ||
126 | { | ||
127 | /* Nothing was yet parsed since init */ | ||
128 | str->hdr.win_right = str->hdr.win_left; | ||
129 | } | ||
130 | else | ||
131 | { | ||
132 | /* Restore window based upon current packet */ | ||
133 | str->hdr.win_right = str->hdr.win_left + | ||
134 | (str->curr_packet_end - str->curr_packet); | ||
135 | } | ||
136 | } | ||
137 | |||
138 | /* Set stream manager to a "no-file" state */ | ||
139 | static void stream_mgr_init_state(void) | ||
140 | { | ||
141 | stream_mgr.filename = NULL; | ||
142 | stream_mgr.resume_time = INVALID_TIMESTAMP; | ||
143 | stream_mgr.seeked = false; | ||
144 | } | ||
145 | |||
146 | /* Add a stream to the playback pool */ | ||
147 | void stream_add_stream(struct stream *str) | ||
148 | { | ||
149 | actl_lock(); | ||
150 | |||
151 | list_remove_item(stream_mgr.strl, str); | ||
152 | list_add_item(stream_mgr.strl, str); | ||
153 | |||
154 | actl_unlock(); | ||
155 | } | ||
156 | |||
157 | /* Callback for various list-moving operations */ | ||
158 | static bool strl_enum_callback(struct stream *str, void *data) | ||
159 | { | ||
160 | actl_lock(); | ||
161 | |||
162 | list_remove_item(stream_mgr.strl, str); | ||
163 | |||
164 | if (*(int*)data == 1) | ||
165 | list_add_item(stream_mgr.actl, str); | ||
166 | |||
167 | actl_unlock(); | ||
168 | |||
169 | return true; | ||
170 | } | ||
171 | |||
172 | /* Clear all streams from active and playback pools */ | ||
173 | void stream_remove_streams(void) | ||
174 | { | ||
175 | int add_item = 0; | ||
176 | list_enum_items(stream_mgr.strl, | ||
177 | (list_enum_callback_t)strl_enum_callback, (void *)&add_item); | ||
178 | } | ||
179 | |||
180 | /* Move the playback pool to the active list */ | ||
181 | void move_strl_to_actl(void) | ||
182 | { | ||
183 | int add_item = 1; | ||
184 | list_enum_items(stream_mgr.strl, | ||
185 | (list_enum_callback_t)strl_enum_callback, (void *)&add_item); | ||
186 | } | ||
187 | |||
188 | /* Remove a stream from the active list and return it to the pool */ | ||
189 | static bool actl_stream_remove(struct stream *str) | ||
190 | { | ||
191 | bool retval; | ||
192 | |||
193 | actl_lock(); | ||
194 | |||
195 | retval = list_remove_item(stream_mgr.actl, str); | ||
196 | |||
197 | if (retval) | ||
198 | list_add_item(stream_mgr.strl, str); | ||
199 | |||
200 | actl_unlock(); | ||
201 | |||
202 | return retval; | ||
203 | } | ||
204 | |||
205 | /* Broadcast a message to all active streams */ | ||
206 | static bool actl_stream_broadcast_callback(struct stream *str, | ||
207 | struct str_broadcast_data *sbd) | ||
208 | { | ||
209 | switch (sbd->cmd) | ||
210 | { | ||
211 | case STREAM_PLAY: | ||
212 | case STREAM_PAUSE: | ||
213 | break; | ||
214 | |||
215 | case STREAM_STOP: | ||
216 | if (sbd->data != 0) | ||
217 | { | ||
218 | actl_lock(); | ||
219 | |||
220 | list_remove_item(stream_mgr.actl, str); | ||
221 | list_add_item(stream_mgr.strl, str); | ||
222 | |||
223 | actl_unlock(); | ||
224 | sbd->data = 0; | ||
225 | } | ||
226 | break; | ||
227 | |||
228 | default: | ||
229 | return false; | ||
230 | } | ||
231 | |||
232 | str_send_msg(str, sbd->cmd, sbd->data); | ||
233 | return true; | ||
234 | } | ||
235 | |||
236 | static void actl_stream_broadcast(int cmd, intptr_t data) | ||
237 | { | ||
238 | struct str_broadcast_data sbd; | ||
239 | sbd.cmd = cmd; | ||
240 | sbd.data = data; | ||
241 | list_enum_items(stream_mgr.actl, | ||
242 | (list_enum_callback_t)actl_stream_broadcast_callback, | ||
243 | (void*)&sbd); | ||
244 | } | ||
245 | |||
246 | /* Set the current base clock */ | ||
247 | static void set_stream_clock(uint32_t time) | ||
248 | { | ||
249 | /* Fudge: Start clock 100ms early to allow for some filling time */ | ||
250 | if (time > 100*TS_SECOND/1000) | ||
251 | time -= 100*TS_SECOND/1000; | ||
252 | else | ||
253 | time = 0; | ||
254 | |||
255 | pcm_output_set_clock(TS_TO_TICKS(time)); | ||
256 | } | ||
257 | |||
258 | static void stream_start_playback(uint32_t time, bool fill_buffer) | ||
259 | { | ||
260 | if (stream_mgr.seeked) | ||
261 | { | ||
262 | /* Clear any seeked status */ | ||
263 | stream_mgr.seeked = false; | ||
264 | |||
265 | /* Flush old PCM data */ | ||
266 | pcm_output_flush(); | ||
267 | |||
268 | /* Set the master clock */ | ||
269 | set_stream_clock(time); | ||
270 | |||
271 | /* Make sure streams are back in active pool */ | ||
272 | move_strl_to_actl(); | ||
273 | |||
274 | /* Prepare the parser and associated streams */ | ||
275 | parser_prepare_streaming(); | ||
276 | } | ||
277 | |||
278 | /* Start buffer which optional force fill */ | ||
279 | disk_buf_send_msg(STREAM_PLAY, fill_buffer); | ||
280 | |||
281 | /* Tell each stream to start - may generate end of stream signals | ||
282 | * now - we'll handle this when finished */ | ||
283 | actl_stream_broadcast(STREAM_PLAY, 0); | ||
284 | |||
285 | /* Actually start the clock */ | ||
286 | pcm_output_play_pause(true); | ||
287 | } | ||
288 | |||
289 | /* Return the play time relative to the specified play time */ | ||
290 | static uint32_t time_from_whence(uint32_t time, int whence) | ||
291 | { | ||
292 | int64_t currtime; | ||
293 | uint32_t start; | ||
294 | |||
295 | switch (whence) | ||
296 | { | ||
297 | case SEEK_SET: | ||
298 | /* Set the current time (time = unsigned offset from 0) */ | ||
299 | if (time > str_parser.duration) | ||
300 | time = str_parser.duration; | ||
301 | break; | ||
302 | case SEEK_CUR: | ||
303 | /* Seek forward or backward from the current time | ||
304 | * (time = signed offset from current) */ | ||
305 | currtime = stream_get_seek_time(&start); | ||
306 | currtime -= start; | ||
307 | currtime += (int32_t)time; | ||
308 | |||
309 | if (currtime < 0) | ||
310 | currtime = 0; | ||
311 | else if ((uint64_t)currtime > str_parser.duration) | ||
312 | currtime = str_parser.duration; | ||
313 | |||
314 | time = (uint32_t)currtime; | ||
315 | break; | ||
316 | case SEEK_END: | ||
317 | /* Seek from the end (time = unsigned offset from end) */ | ||
318 | if (time > str_parser.duration) | ||
319 | time = str_parser.duration; | ||
320 | time = str_parser.duration - time; | ||
321 | break; | ||
322 | } | ||
323 | |||
324 | return time; | ||
325 | } | ||
326 | |||
327 | /* Handle seeking details if playing or paused */ | ||
328 | static uint32_t stream_seek_intl(uint32_t time, int whence, | ||
329 | int status, bool *was_buffering) | ||
330 | { | ||
331 | if (status != STREAM_STOPPED) | ||
332 | { | ||
333 | bool wb; | ||
334 | |||
335 | /* Place streams in a non-running state - keep them on actl if | ||
336 | * still there */ | ||
337 | actl_stream_broadcast(STREAM_PAUSE, 0); | ||
338 | |||
339 | /* Stop all buffering or else risk clobbering random-access data */ | ||
340 | wb = disk_buf_send_msg(STREAM_STOP, 0); | ||
341 | |||
342 | if (was_buffering != NULL) | ||
343 | *was_buffering = wb; | ||
344 | } | ||
345 | |||
346 | time = time_from_whence(time, whence); | ||
347 | |||
348 | stream_mgr.seeked = true; | ||
349 | |||
350 | return parser_seek_time(time); | ||
351 | } | ||
352 | |||
353 | /* Store the resume time at the last seek/current clock point */ | ||
354 | static void stream_remember_resume_time(void) | ||
355 | { | ||
356 | /* Assume invalidity */ | ||
357 | stream_mgr.resume_time = 0; | ||
358 | |||
359 | if (stream_can_seek()) | ||
360 | { | ||
361 | /* Read the current stream time or the last seeked position */ | ||
362 | uint32_t start; | ||
363 | uint32_t time = stream_get_seek_time(&start); | ||
364 | |||
365 | if (time >= str_parser.start_pts && time <= str_parser.end_pts) | ||
366 | { | ||
367 | /* Save the current stream time */ | ||
368 | stream_mgr.resume_time = time - start; | ||
369 | } | ||
370 | } | ||
371 | } | ||
372 | |||
373 | /* Handle STREAM_OPEN */ | ||
374 | void stream_on_open(const char *filename) | ||
375 | { | ||
376 | int err = STREAM_ERROR; | ||
377 | |||
378 | stream_mgr_lock(); | ||
379 | |||
380 | trigger_cpu_boost(); | ||
381 | |||
382 | /* Open the video file */ | ||
383 | if (disk_buf_open(filename) >= 0) | ||
384 | { | ||
385 | /* Initialize the parser */ | ||
386 | err = parser_init_stream(); | ||
387 | |||
388 | if (err >= STREAM_OK) | ||
389 | { | ||
390 | /* File ok - save the opened filename */ | ||
391 | stream_mgr.filename = filename; | ||
392 | } | ||
393 | } | ||
394 | |||
395 | /* If error - cleanup */ | ||
396 | if (err < STREAM_OK) | ||
397 | stream_on_close(); | ||
398 | |||
399 | cancel_cpu_boost(); | ||
400 | |||
401 | stream_mgr_unlock(); | ||
402 | |||
403 | stream_mgr_reply_msg(err); | ||
404 | } | ||
405 | |||
406 | /* Handler STREAM_PLAY */ | ||
407 | static void stream_on_play(void) | ||
408 | { | ||
409 | int status = stream_mgr.status; | ||
410 | |||
411 | stream_mgr_lock(); | ||
412 | |||
413 | if (status == STREAM_STOPPED) | ||
414 | { | ||
415 | uint32_t start; | ||
416 | |||
417 | /* We just say we're playing now */ | ||
418 | stream_mgr.status = STREAM_PLAYING; | ||
419 | |||
420 | /* Reply with previous state */ | ||
421 | stream_mgr_reply_msg(status); | ||
422 | |||
423 | trigger_cpu_boost(); | ||
424 | |||
425 | /* Seek to initial position and set clock to that time */ | ||
426 | |||
427 | /* Save the resume time */ | ||
428 | stream_remember_resume_time(); | ||
429 | |||
430 | /* Prepare seek to start point */ | ||
431 | start = stream_seek_intl(stream_mgr.resume_time, SEEK_SET, | ||
432 | STREAM_STOPPED, NULL); | ||
433 | |||
434 | /* Sync and start - force buffer fill */ | ||
435 | stream_start_playback(start, true); | ||
436 | } | ||
437 | else | ||
438 | { | ||
439 | /* Reply with previous state */ | ||
440 | stream_mgr_reply_msg(status); | ||
441 | } | ||
442 | |||
443 | stream_mgr_unlock(); | ||
444 | } | ||
445 | |||
446 | /* Handle STREAM_PAUSE */ | ||
447 | static void stream_on_pause(void) | ||
448 | { | ||
449 | int status = stream_mgr.status; | ||
450 | |||
451 | stream_mgr_lock(); | ||
452 | |||
453 | /* Reply with previous state */ | ||
454 | stream_mgr_reply_msg(status); | ||
455 | |||
456 | if (status == STREAM_PLAYING) | ||
457 | { | ||
458 | /* Pause the clock */ | ||
459 | pcm_output_play_pause(false); | ||
460 | |||
461 | /* Pause each active stream */ | ||
462 | actl_stream_broadcast(STREAM_PAUSE, 0); | ||
463 | |||
464 | /* Pause the disk buffer - buffer may continue filling */ | ||
465 | disk_buf_send_msg(STREAM_PAUSE, false); | ||
466 | |||
467 | /* Unboost the CPU */ | ||
468 | cancel_cpu_boost(); | ||
469 | |||
470 | /* Offically paused */ | ||
471 | stream_mgr.status = STREAM_PAUSED; | ||
472 | } | ||
473 | |||
474 | stream_mgr_unlock(); | ||
475 | } | ||
476 | |||
477 | /* Handle STREAM_RESUME */ | ||
478 | static void stream_on_resume(void) | ||
479 | { | ||
480 | int status = stream_mgr.status; | ||
481 | |||
482 | stream_mgr_lock(); | ||
483 | |||
484 | /* Reply with previous state */ | ||
485 | stream_mgr_reply_msg(status); | ||
486 | |||
487 | if (status == STREAM_PAUSED) | ||
488 | { | ||
489 | /* Boost the CPU */ | ||
490 | trigger_cpu_boost(); | ||
491 | |||
492 | /* Sync and start - no force buffering */ | ||
493 | stream_start_playback(str_parser.last_seek_time, false); | ||
494 | |||
495 | /* Officially playing */ | ||
496 | stream_mgr.status = STREAM_PLAYING; | ||
497 | } | ||
498 | |||
499 | stream_mgr_unlock(); | ||
500 | } | ||
501 | |||
502 | /* Handle STREAM_STOP */ | ||
503 | static void stream_on_stop(bool reply) | ||
504 | { | ||
505 | int status = stream_mgr.status; | ||
506 | |||
507 | stream_mgr_lock(); | ||
508 | |||
509 | if (reply) | ||
510 | stream_mgr_reply_msg(status); | ||
511 | |||
512 | if (status != STREAM_STOPPED) | ||
513 | { | ||
514 | /* Pause the clock */ | ||
515 | pcm_output_play_pause(false); | ||
516 | |||
517 | /* Update the resume time info */ | ||
518 | stream_remember_resume_time(); | ||
519 | |||
520 | /* Not stopped = paused or playing */ | ||
521 | stream_mgr.seeked = false; | ||
522 | |||
523 | /* Stop buffering */ | ||
524 | disk_buf_send_msg(STREAM_STOP, 0); | ||
525 | |||
526 | /* Clear any still-active streams and remove from actl */ | ||
527 | actl_stream_broadcast(STREAM_STOP, 1); | ||
528 | |||
529 | /* Stop PCM output (and clock) */ | ||
530 | pcm_output_stop(); | ||
531 | |||
532 | /* Cancel our processor boost */ | ||
533 | cancel_cpu_boost(); | ||
534 | |||
535 | stream_mgr.status = STREAM_STOPPED; | ||
536 | } | ||
537 | |||
538 | stream_mgr_unlock(); | ||
539 | } | ||
540 | |||
541 | /* Handle STREAM_SEEK */ | ||
542 | static void stream_on_seek(struct stream_seek_data *skd) | ||
543 | { | ||
544 | uint32_t time = skd->time; | ||
545 | int whence = skd->whence; | ||
546 | |||
547 | switch (whence) | ||
548 | { | ||
549 | case SEEK_SET: | ||
550 | case SEEK_CUR: | ||
551 | case SEEK_END: | ||
552 | if (stream_mgr.filename == NULL) | ||
553 | break; | ||
554 | |||
555 | /* Keep things spinning if already doing so */ | ||
556 | stream_keep_disk_active(); | ||
557 | |||
558 | /* Have data - reply in order to acquire lock */ | ||
559 | stream_mgr_reply_msg(STREAM_OK); | ||
560 | |||
561 | stream_mgr_lock(); | ||
562 | |||
563 | /* Either seeking must be possible or a full rewind must be done */ | ||
564 | if (stream_can_seek() || time_from_whence(time, whence) == 0) | ||
565 | { | ||
566 | bool buffer = false; | ||
567 | |||
568 | if (stream_mgr.status == STREAM_PLAYING) | ||
569 | { | ||
570 | /* Keep clock from advancing while seeking */ | ||
571 | pcm_output_play_pause(false); | ||
572 | } | ||
573 | |||
574 | time = stream_seek_intl(time, whence, stream_mgr.status, &buffer); | ||
575 | stream_remember_resume_time(); | ||
576 | |||
577 | if (stream_mgr.status == STREAM_PLAYING) | ||
578 | { | ||
579 | /* Sync and restart - no force buffering */ | ||
580 | stream_start_playback(time, buffer); | ||
581 | } | ||
582 | } | ||
583 | |||
584 | stream_mgr_unlock(); | ||
585 | return; | ||
586 | } | ||
587 | |||
588 | /* Invalid parameter or no file */ | ||
589 | stream_mgr_reply_msg(STREAM_ERROR); | ||
590 | } | ||
591 | |||
592 | /* Handle STREAM_CLOSE */ | ||
593 | static int stream_on_close(void) | ||
594 | { | ||
595 | int status = STREAM_STOPPED; | ||
596 | |||
597 | stream_mgr_lock(); | ||
598 | |||
599 | /* Any open file that was accepted for playback? */ | ||
600 | if (stream_mgr.filename != NULL) | ||
601 | { | ||
602 | /* Yes - hide video */ | ||
603 | stream_show_vo(false); | ||
604 | /* Stop any playback */ | ||
605 | status = stream_mgr.status; | ||
606 | stream_on_stop(false); | ||
607 | /* Tell parser file is finished */ | ||
608 | parser_close_stream(); | ||
609 | /* Reinitialize manager */ | ||
610 | stream_mgr_init_state(); | ||
611 | } | ||
612 | |||
613 | /* Let disk buffer reset itself - file might be open even if no good */ | ||
614 | disk_buf_close(); | ||
615 | |||
616 | stream_mgr_unlock(); | ||
617 | |||
618 | return status; | ||
619 | } | ||
620 | |||
621 | /* Handle STREAM_EV_COMPLETE */ | ||
622 | static void stream_on_ev_complete(struct stream *str) | ||
623 | { | ||
624 | stream_mgr_lock(); | ||
625 | |||
626 | /* Stream is active? */ | ||
627 | if (actl_stream_remove(str)) | ||
628 | { | ||
629 | /* No - remove this stream from the active list */ | ||
630 | DEBUGF(" finished: 0x%02x\n", str->id); | ||
631 | if (list_is_empty(stream_mgr.actl)) | ||
632 | { | ||
633 | /* All streams have acked - stop playback */ | ||
634 | stream_on_stop(false); | ||
635 | stream_mgr.resume_time = 0; /* Played to end - no resume */ | ||
636 | } | ||
637 | else | ||
638 | { | ||
639 | /* Stream is done - stop it and place back in pool */ | ||
640 | str_send_msg(str, STREAM_STOP, 1); | ||
641 | } | ||
642 | } | ||
643 | |||
644 | stream_mgr_unlock(); | ||
645 | } | ||
646 | |||
647 | /* Callback for stream to notify about events internal to them */ | ||
648 | void stream_generate_event(struct stream *str, long id, intptr_t data) | ||
649 | { | ||
650 | if (str == NULL) | ||
651 | return; | ||
652 | |||
653 | switch (id) | ||
654 | { | ||
655 | case STREAM_EV_COMPLETE: | ||
656 | /* The last stream has ended */ | ||
657 | stream_mgr_post_msg(STREAM_EV_COMPLETE, (intptr_t)str); | ||
658 | break; | ||
659 | } | ||
660 | |||
661 | (void)data; | ||
662 | } | ||
663 | |||
664 | /* Clear any particular notification for which a stream registered */ | ||
665 | void stream_clear_notify(struct stream *str, int for_msg) | ||
666 | { | ||
667 | switch (for_msg) | ||
668 | { | ||
669 | case DISK_BUF_DATA_NOTIFY: | ||
670 | disk_buf_send_msg(DISK_BUF_CLEAR_DATA_NOTIFY, (intptr_t)str); | ||
671 | break; | ||
672 | } | ||
673 | } | ||
674 | |||
675 | /* Special handling for certain messages since they involve multiple | ||
676 | * operations behind the scenes */ | ||
677 | static intptr_t send_video_msg(long id, intptr_t data) | ||
678 | { | ||
679 | intptr_t retval = 0; | ||
680 | |||
681 | if (video_str.thread != 0 && disk_buf.in_file >= 0) | ||
682 | { | ||
683 | |||
684 | switch (id) | ||
685 | { | ||
686 | case VIDEO_DISPLAY_SHOW: | ||
687 | if (data != 0 && disk_buf_status() == STREAM_STOPPED) | ||
688 | { /* Only prepare image if showing and not playing */ | ||
689 | parser_prepare_image(str_parser.last_seek_time); | ||
690 | } | ||
691 | break; | ||
692 | |||
693 | case VIDEO_PRINT_FRAME: | ||
694 | if (data) | ||
695 | break; | ||
696 | case VIDEO_PRINT_THUMBNAIL: | ||
697 | if (disk_buf_status() != STREAM_STOPPED) | ||
698 | break; /* Prepare image if not playing */ | ||
699 | |||
700 | /* Ignore return and try video thread anyway */ | ||
701 | parser_prepare_image(str_parser.last_seek_time); | ||
702 | |||
703 | /* Image ready - pass message to video thread */ | ||
704 | break; | ||
705 | } | ||
706 | |||
707 | retval = str_send_msg(&video_str, id, data); | ||
708 | } | ||
709 | |||
710 | return retval; | ||
711 | } | ||
712 | |||
713 | /* Show/hide the video output */ | ||
714 | bool stream_show_vo(bool show) | ||
715 | { | ||
716 | bool vis; | ||
717 | stream_mgr_lock(); | ||
718 | |||
719 | vis = send_video_msg(VIDEO_DISPLAY_SHOW, show); | ||
720 | #ifndef HAVE_LCD_COLOR | ||
721 | grey_show(show); | ||
722 | #endif | ||
723 | stream_mgr_unlock(); | ||
724 | |||
725 | return vis; | ||
726 | } | ||
727 | |||
728 | /* Query the visibility of video output */ | ||
729 | bool stream_vo_is_visible(void) | ||
730 | { | ||
731 | bool vis; | ||
732 | stream_mgr_lock(); | ||
733 | vis = send_video_msg(VIDEO_DISPLAY_IS_VISIBLE, 0); | ||
734 | stream_mgr_unlock(); | ||
735 | return vis; | ||
736 | } | ||
737 | |||
738 | /* Return the video dimensions */ | ||
739 | bool stream_vo_get_size(struct vo_ext *sz) | ||
740 | { | ||
741 | bool retval = false; | ||
742 | |||
743 | stream_mgr_lock(); | ||
744 | |||
745 | if (str_parser.dims.w > 0 && str_parser.dims.h > 0) | ||
746 | { | ||
747 | *sz = str_parser.dims; | ||
748 | retval = true; | ||
749 | } | ||
750 | |||
751 | stream_mgr_unlock(); | ||
752 | |||
753 | return retval; | ||
754 | } | ||
755 | |||
756 | void stream_vo_set_clip(const struct vo_rect *rc) | ||
757 | { | ||
758 | stream_mgr_lock(); | ||
759 | |||
760 | if (rc) | ||
761 | { | ||
762 | stream_mgr.parms.rc = *rc; | ||
763 | rc = &stream_mgr.parms.rc; | ||
764 | } | ||
765 | |||
766 | send_video_msg(VIDEO_SET_CLIP_RECT, (intptr_t)rc); | ||
767 | |||
768 | stream_mgr_unlock(); | ||
769 | } | ||
770 | |||
771 | bool stream_vo_get_clip(struct vo_rect *rc) | ||
772 | { | ||
773 | bool retval; | ||
774 | |||
775 | if (!rc) | ||
776 | return false; | ||
777 | |||
778 | stream_mgr_lock(); | ||
779 | |||
780 | retval = send_video_msg(VIDEO_GET_CLIP_RECT, | ||
781 | (intptr_t)&stream_mgr.parms.rc); | ||
782 | |||
783 | *rc = stream_mgr.parms.rc; | ||
784 | |||
785 | stream_mgr_unlock(); | ||
786 | |||
787 | return retval; | ||
788 | } | ||
789 | |||
790 | #ifndef HAVE_LCD_COLOR | ||
791 | /* Show/hide the gray video overlay (independently of vo visibility). */ | ||
792 | void stream_gray_show(bool show) | ||
793 | { | ||
794 | stream_mgr_lock(); | ||
795 | |||
796 | grey_show(show); | ||
797 | |||
798 | stream_mgr_unlock(); | ||
799 | } | ||
800 | |||
801 | #endif /* !HAVE_LCD_COLOR */ | ||
802 | |||
803 | /* Display a thumbnail at the last seek point */ | ||
804 | bool stream_display_thumb(const struct vo_rect *rc) | ||
805 | { | ||
806 | bool retval; | ||
807 | |||
808 | if (rc == NULL) | ||
809 | return false; | ||
810 | |||
811 | stream_mgr_lock(); | ||
812 | |||
813 | stream_mgr.parms.rc = *rc; | ||
814 | retval = send_video_msg(VIDEO_PRINT_THUMBNAIL, | ||
815 | (intptr_t)&stream_mgr.parms.rc); | ||
816 | |||
817 | stream_mgr_unlock(); | ||
818 | |||
819 | return retval; | ||
820 | } | ||
821 | |||
822 | bool stream_draw_frame(bool no_prepare) | ||
823 | { | ||
824 | bool retval; | ||
825 | stream_mgr_lock(); | ||
826 | |||
827 | retval = send_video_msg(VIDEO_PRINT_FRAME, no_prepare); | ||
828 | |||
829 | stream_mgr_unlock(); | ||
830 | |||
831 | return retval; | ||
832 | } | ||
833 | |||
834 | bool stream_set_callback(long id, void *fn) | ||
835 | { | ||
836 | bool retval = false; | ||
837 | |||
838 | stream_mgr_lock(); | ||
839 | |||
840 | switch (id) | ||
841 | { | ||
842 | case VIDEO_SET_POST_FRAME_CALLBACK: | ||
843 | retval = send_video_msg(id, (intptr_t)fn); | ||
844 | } | ||
845 | |||
846 | stream_mgr_unlock(); | ||
847 | |||
848 | return retval; | ||
849 | } | ||
850 | |||
851 | /* Return the time playback should resume if interrupted */ | ||
852 | uint32_t stream_get_resume_time(void) | ||
853 | { | ||
854 | uint32_t resume_time; | ||
855 | |||
856 | /* A stop request is async and replies before setting this - must lock */ | ||
857 | stream_mgr_lock(); | ||
858 | |||
859 | resume_time = stream_mgr.resume_time; | ||
860 | |||
861 | stream_mgr_unlock(); | ||
862 | |||
863 | return resume_time; | ||
864 | } | ||
865 | |||
866 | uint32_t stream_get_seek_time(uint32_t *start) | ||
867 | { | ||
868 | uint32_t time; | ||
869 | |||
870 | stream_mgr_lock(); | ||
871 | |||
872 | if (stream_mgr.seeked) | ||
873 | { | ||
874 | time = str_parser.last_seek_time; | ||
875 | } | ||
876 | else | ||
877 | { | ||
878 | time = TICKS_TO_TS(pcm_output_get_clock()); | ||
879 | |||
880 | /* Clock can be start early so keep in range */ | ||
881 | if (time < str_parser.start_pts) | ||
882 | time = str_parser.start_pts; | ||
883 | } | ||
884 | |||
885 | if (start != NULL) | ||
886 | *start = str_parser.start_pts; | ||
887 | |||
888 | stream_mgr_unlock(); | ||
889 | |||
890 | return time; | ||
891 | } | ||
892 | |||
893 | /* Wait for a state transistion to complete */ | ||
894 | void stream_wait_status(void) | ||
895 | { | ||
896 | stream_mgr_lock(); | ||
897 | stream_mgr_unlock(); | ||
898 | } | ||
899 | |||
900 | /* Returns the smallest file window that includes all active streams' | ||
901 | * windows */ | ||
902 | static bool stream_get_window_callback(struct stream *str, | ||
903 | struct stream_window *sw) | ||
904 | { | ||
905 | off_t swl = str->hdr.win_left; | ||
906 | off_t swr = str->hdr.win_right; | ||
907 | |||
908 | if (swl < sw->left) | ||
909 | sw->left = swl; | ||
910 | |||
911 | if (swr > sw->right) | ||
912 | sw->right = swr; | ||
913 | |||
914 | return true; | ||
915 | } | ||
916 | |||
917 | bool stream_get_window(struct stream_window *sw) | ||
918 | { | ||
919 | if (sw == NULL) | ||
920 | return false; | ||
921 | |||
922 | sw->left = LONG_MAX; | ||
923 | sw->right = LONG_MIN; | ||
924 | |||
925 | actl_lock(); | ||
926 | list_enum_items(stream_mgr.actl, | ||
927 | (list_enum_callback_t)stream_get_window_callback, | ||
928 | (void*)sw); | ||
929 | actl_unlock(); | ||
930 | |||
931 | return sw->left <= sw->right; | ||
932 | } | ||
933 | |||
934 | /* Playback control thread */ | ||
935 | static void stream_mgr_thread(void) | ||
936 | { | ||
937 | struct queue_event ev; | ||
938 | |||
939 | while (1) | ||
940 | { | ||
941 | rb->queue_wait(stream_mgr.q, &ev); | ||
942 | |||
943 | switch (ev.id) | ||
944 | { | ||
945 | case STREAM_OPEN: | ||
946 | stream_on_open((const char *)ev.data); | ||
947 | break; | ||
948 | |||
949 | case STREAM_CLOSE: | ||
950 | stream_on_close(); | ||
951 | break; | ||
952 | |||
953 | case STREAM_PLAY: | ||
954 | stream_on_play(); | ||
955 | break; | ||
956 | |||
957 | case STREAM_PAUSE: | ||
958 | if (ev.data) | ||
959 | stream_on_resume(); | ||
960 | else | ||
961 | stream_on_pause(); | ||
962 | break; | ||
963 | |||
964 | case STREAM_STOP: | ||
965 | stream_on_stop(true); | ||
966 | break; | ||
967 | |||
968 | case STREAM_SEEK: | ||
969 | stream_on_seek((struct stream_seek_data *)ev.data); | ||
970 | break; | ||
971 | |||
972 | case STREAM_EV_COMPLETE: | ||
973 | stream_on_ev_complete((struct stream *)ev.data); | ||
974 | break; | ||
975 | |||
976 | case STREAM_QUIT: | ||
977 | if (stream_mgr.status != STREAM_STOPPED) | ||
978 | stream_on_stop(false); | ||
979 | return; | ||
980 | } | ||
981 | } | ||
982 | } | ||
983 | |||
984 | /* Stream command interface APIs */ | ||
985 | |||
986 | /* Opens a new file */ | ||
987 | int stream_open(const char *filename) | ||
988 | { | ||
989 | if (stream_mgr.thread != 0) | ||
990 | return stream_mgr_send_msg(STREAM_OPEN, (intptr_t)filename); | ||
991 | return STREAM_ERROR; | ||
992 | } | ||
993 | |||
994 | /* Plays the current file starting at time 'start' */ | ||
995 | int stream_play(void) | ||
996 | { | ||
997 | if (stream_mgr.thread != 0) | ||
998 | return stream_mgr_send_msg(STREAM_PLAY, 0); | ||
999 | return STREAM_ERROR; | ||
1000 | } | ||
1001 | |||
1002 | /* Pauses playback if playing */ | ||
1003 | int stream_pause(void) | ||
1004 | { | ||
1005 | if (stream_mgr.thread != 0) | ||
1006 | return stream_mgr_send_msg(STREAM_PAUSE, false); | ||
1007 | return STREAM_ERROR; | ||
1008 | } | ||
1009 | |||
1010 | /* Resumes playback if paused */ | ||
1011 | int stream_resume(void) | ||
1012 | { | ||
1013 | if (stream_mgr.thread != 0) | ||
1014 | return stream_mgr_send_msg(STREAM_PAUSE, true); | ||
1015 | return STREAM_ERROR; | ||
1016 | } | ||
1017 | |||
1018 | /* Stops playback if not stopped */ | ||
1019 | int stream_stop(void) | ||
1020 | { | ||
1021 | if (stream_mgr.thread != 0) | ||
1022 | return stream_mgr_send_msg(STREAM_STOP, 0); | ||
1023 | return STREAM_ERROR; | ||
1024 | } | ||
1025 | |||
1026 | /* Seeks playback time to/by the specified time */ | ||
1027 | int stream_seek(uint32_t time, int whence) | ||
1028 | { | ||
1029 | int ret; | ||
1030 | |||
1031 | if (stream_mgr.thread == 0) | ||
1032 | return STREAM_ERROR; | ||
1033 | |||
1034 | stream_mgr_lock(); | ||
1035 | |||
1036 | stream_mgr.parms.skd.time = time; | ||
1037 | stream_mgr.parms.skd.whence = whence; | ||
1038 | |||
1039 | ret = stream_mgr_send_msg(STREAM_SEEK, (intptr_t)&stream_mgr.parms.skd); | ||
1040 | |||
1041 | stream_mgr_unlock(); | ||
1042 | |||
1043 | return ret; | ||
1044 | } | ||
1045 | |||
1046 | /* Closes the current file */ | ||
1047 | int stream_close(void) | ||
1048 | { | ||
1049 | if (stream_mgr.thread != 0) | ||
1050 | return stream_mgr_send_msg(STREAM_CLOSE, 0); | ||
1051 | return STREAM_ERROR; | ||
1052 | } | ||
1053 | |||
1054 | /* Initializes the playback engine */ | ||
1055 | int stream_init(void) | ||
1056 | { | ||
1057 | void *mem; | ||
1058 | size_t memsize; | ||
1059 | |||
1060 | stream_mgr.status = STREAM_STOPPED; | ||
1061 | stream_mgr_init_state(); | ||
1062 | |||
1063 | /* Initialize our window to the outside world first */ | ||
1064 | rb->mutex_init(&stream_mgr.str_mtx); | ||
1065 | rb->mutex_init(&stream_mgr.actl_mtx); | ||
1066 | |||
1067 | stream_mgr.q = &stream_mgr_queue; | ||
1068 | rb->queue_init(stream_mgr.q, false); | ||
1069 | |||
1070 | /* sets audiosize and returns buffer pointer */ | ||
1071 | mem = rb->plugin_get_audio_buffer(&memsize); | ||
1072 | |||
1073 | /* Initialize non-allocator blocks first */ | ||
1074 | #ifndef HAVE_LCD_COLOR | ||
1075 | long greysize; | ||
1076 | |||
1077 | /* Greylib init handles all necessary cache alignment */ | ||
1078 | if (!grey_init(mem, memsize, GREY_BUFFERED|GREY_ON_COP, | ||
1079 | LCD_WIDTH, LCD_HEIGHT, &greysize)) | ||
1080 | { | ||
1081 | rb->splash(HZ, "greylib init failed!"); | ||
1082 | return STREAM_ERROR; | ||
1083 | } | ||
1084 | |||
1085 | mem += greysize; | ||
1086 | memsize -= greysize; | ||
1087 | |||
1088 | grey_clear_display(); | ||
1089 | #endif /* !HAVE_LCD_COLOR */ | ||
1090 | |||
1091 | stream_mgr.thread = rb->create_thread(stream_mgr_thread, | ||
1092 | stream_mgr_thread_stack, sizeof(stream_mgr_thread_stack), | ||
1093 | 0, "mpgstream_mgr" IF_PRIO(, PRIORITY_SYSTEM) IF_COP(, CPU)); | ||
1094 | |||
1095 | rb->queue_enable_queue_send(stream_mgr.q, &stream_mgr_queue_send, | ||
1096 | stream_mgr.thread); | ||
1097 | |||
1098 | if (stream_mgr.thread == 0) | ||
1099 | { | ||
1100 | rb->splash(HZ, "Could not create stream manager thread!"); | ||
1101 | return STREAM_ERROR; | ||
1102 | } | ||
1103 | |||
1104 | /* Wait for thread to initialize */ | ||
1105 | stream_mgr_send_msg(STREAM_NULL, 0); | ||
1106 | |||
1107 | /* Initialise our malloc buffer */ | ||
1108 | if (!mpeg_alloc_init(mem, memsize)) | ||
1109 | { | ||
1110 | rb->splash(HZ, "Out of memory in stream_init"); | ||
1111 | } | ||
1112 | /* These inits use the allocator */ | ||
1113 | else if (!pcm_output_init()) | ||
1114 | { | ||
1115 | rb->splash(HZ, "Could not initialize PCM!"); | ||
1116 | } | ||
1117 | else if (!audio_thread_init()) | ||
1118 | { | ||
1119 | rb->splash(HZ, "Cannot create audio thread!"); | ||
1120 | } | ||
1121 | else if (!video_thread_init()) | ||
1122 | { | ||
1123 | rb->splash(HZ, "Cannot create video thread!"); | ||
1124 | } | ||
1125 | /* Disk buffer takes max allotment of what's left so it must be last */ | ||
1126 | else if (!disk_buf_init()) | ||
1127 | { | ||
1128 | rb->splash(HZ, "Cannot create buffering thread!"); | ||
1129 | } | ||
1130 | else if (!parser_init()) | ||
1131 | { | ||
1132 | rb->splash(HZ, "Parser init failed!"); | ||
1133 | } | ||
1134 | else | ||
1135 | { | ||
1136 | return STREAM_OK; | ||
1137 | } | ||
1138 | |||
1139 | return STREAM_ERROR; | ||
1140 | } | ||
1141 | |||
1142 | /* Cleans everything up */ | ||
1143 | void stream_exit(void) | ||
1144 | { | ||
1145 | stream_close(); | ||
1146 | |||
1147 | /* Stop the threads and wait for them to terminate */ | ||
1148 | video_thread_exit(); | ||
1149 | audio_thread_exit(); | ||
1150 | disk_buf_exit(); | ||
1151 | pcm_output_exit(); | ||
1152 | |||
1153 | if (stream_mgr.thread != 0) | ||
1154 | { | ||
1155 | stream_mgr_post_msg(STREAM_QUIT, 0); | ||
1156 | rb->thread_wait(stream_mgr.thread); | ||
1157 | stream_mgr.thread = 0; | ||
1158 | } | ||
1159 | |||
1160 | #ifndef HAVE_LCD_COLOR | ||
1161 | grey_release(); | ||
1162 | #endif | ||
1163 | } | ||