A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita audio rust zig deno mpris rockbox mpd
at master 1164 lines 27 kB view raw
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 29GREY_INFO_STRUCT_IRAM 30#endif 31 32static struct event_queue stream_mgr_queue SHAREDBSS_ATTR; 33static struct queue_sender_list stream_mgr_queue_send SHAREDBSS_ATTR; 34static uint32_t stream_mgr_thread_stack[DEFAULT_STACK_SIZE*2/sizeof(uint32_t)]; 35 36struct stream_mgr stream_mgr SHAREDBSS_ATTR; 37 38/* Forward decs */ 39static int stream_on_close(void); 40 41struct str_broadcast_data 42{ 43 long cmd; /* Command to send to stream */ 44 intptr_t data; /* Data to send with command */ 45}; 46 47static inline void stream_mgr_lock(void) 48{ 49 rb->mutex_lock(&stream_mgr.str_mtx); 50} 51 52static inline void stream_mgr_unlock(void) 53{ 54 rb->mutex_unlock(&stream_mgr.str_mtx); 55} 56 57static inline void actl_lock(void) 58{ 59 rb->mutex_lock(&stream_mgr.actl_mtx); 60} 61 62static inline void actl_unlock(void) 63{ 64 rb->mutex_unlock(&stream_mgr.actl_mtx); 65} 66 67static inline void stream_mgr_post_msg(long id, intptr_t data) 68{ 69 rb->queue_post(stream_mgr.q, id, data); 70} 71 72static 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 77static inline void stream_mgr_reply_msg(intptr_t retval) 78{ 79 rb->queue_reply(stream_mgr.q, retval); 80} 81 82int 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 119void 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 */ 139static 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 */ 147void 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 */ 158static 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 */ 173void 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 */ 181void 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 */ 189static 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 */ 206static 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 236static 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 */ 247static 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 258static 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 */ 290static 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 */ 328static 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 */ 354static 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 */ 374void 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 */ 407static 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 */ 447static 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 */ 478static 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 */ 503static 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 */ 542static 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 */ 593static 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 */ 622static 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 */ 648void 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 */ 665void 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 */ 677static 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 /* fallthrough */ 697 case VIDEO_PRINT_THUMBNAIL: 698 if (disk_buf_status() != STREAM_STOPPED) 699 break; /* Prepare image if not playing */ 700 701 /* Ignore return and try video thread anyway */ 702 parser_prepare_image(str_parser.last_seek_time); 703 704 /* Image ready - pass message to video thread */ 705 break; 706 } 707 708 retval = str_send_msg(&video_str, id, data); 709 } 710 711 return retval; 712} 713 714/* Show/hide the video output */ 715bool stream_show_vo(bool show) 716{ 717 bool vis; 718 stream_mgr_lock(); 719 720 vis = send_video_msg(VIDEO_DISPLAY_SHOW, show); 721#ifndef HAVE_LCD_COLOR 722 grey_show(show); 723#endif 724 stream_mgr_unlock(); 725 726 return vis; 727} 728 729/* Query the visibility of video output */ 730bool stream_vo_is_visible(void) 731{ 732 bool vis; 733 stream_mgr_lock(); 734 vis = send_video_msg(VIDEO_DISPLAY_IS_VISIBLE, 0); 735 stream_mgr_unlock(); 736 return vis; 737} 738 739/* Return the video dimensions */ 740bool stream_vo_get_size(struct vo_ext *sz) 741{ 742 bool retval = false; 743 744 stream_mgr_lock(); 745 746 if (str_parser.dims.w > 0 && str_parser.dims.h > 0) 747 { 748 *sz = str_parser.dims; 749 retval = true; 750 } 751 752 stream_mgr_unlock(); 753 754 return retval; 755} 756 757void stream_vo_set_clip(const struct vo_rect *rc) 758{ 759 stream_mgr_lock(); 760 761 if (rc) 762 { 763 stream_mgr.parms.rc = *rc; 764 rc = &stream_mgr.parms.rc; 765 } 766 767 send_video_msg(VIDEO_SET_CLIP_RECT, (intptr_t)rc); 768 769 stream_mgr_unlock(); 770} 771 772bool stream_vo_get_clip(struct vo_rect *rc) 773{ 774 bool retval; 775 776 if (!rc) 777 return false; 778 779 stream_mgr_lock(); 780 781 retval = send_video_msg(VIDEO_GET_CLIP_RECT, 782 (intptr_t)&stream_mgr.parms.rc); 783 784 *rc = stream_mgr.parms.rc; 785 786 stream_mgr_unlock(); 787 788 return retval; 789} 790 791#ifndef HAVE_LCD_COLOR 792/* Show/hide the gray video overlay (independently of vo visibility). */ 793void stream_gray_show(bool show) 794{ 795 stream_mgr_lock(); 796 797 grey_show(show); 798 799 stream_mgr_unlock(); 800} 801 802#endif /* !HAVE_LCD_COLOR */ 803 804/* Display a thumbnail at the last seek point */ 805bool stream_display_thumb(const struct vo_rect *rc) 806{ 807 bool retval; 808 809 if (rc == NULL) 810 return false; 811 812 stream_mgr_lock(); 813 814 stream_mgr.parms.rc = *rc; 815 retval = send_video_msg(VIDEO_PRINT_THUMBNAIL, 816 (intptr_t)&stream_mgr.parms.rc); 817 818 stream_mgr_unlock(); 819 820 return retval; 821} 822 823bool stream_draw_frame(bool no_prepare) 824{ 825 bool retval; 826 stream_mgr_lock(); 827 828 retval = send_video_msg(VIDEO_PRINT_FRAME, no_prepare); 829 830 stream_mgr_unlock(); 831 832 return retval; 833} 834 835bool stream_set_callback(long id, void *fn) 836{ 837 bool retval = false; 838 839 stream_mgr_lock(); 840 841 switch (id) 842 { 843 case VIDEO_SET_POST_FRAME_CALLBACK: 844 retval = send_video_msg(id, (intptr_t)fn); 845 } 846 847 stream_mgr_unlock(); 848 849 return retval; 850} 851 852/* Return the time playback should resume if interrupted */ 853uint32_t stream_get_resume_time(void) 854{ 855 uint32_t resume_time; 856 857 /* A stop request is async and replies before setting this - must lock */ 858 stream_mgr_lock(); 859 860 resume_time = stream_mgr.resume_time; 861 862 stream_mgr_unlock(); 863 864 return resume_time; 865} 866 867uint32_t stream_get_seek_time(uint32_t *start) 868{ 869 uint32_t time; 870 871 stream_mgr_lock(); 872 873 if (stream_mgr.seeked) 874 { 875 time = str_parser.last_seek_time; 876 } 877 else 878 { 879 time = TICKS_TO_TS(pcm_output_get_clock()); 880 881 /* Clock can be start early so keep in range */ 882 if (time < str_parser.start_pts) 883 time = str_parser.start_pts; 884 } 885 886 if (start != NULL) 887 *start = str_parser.start_pts; 888 889 stream_mgr_unlock(); 890 891 return time; 892} 893 894/* Wait for a state transistion to complete */ 895void stream_wait_status(void) 896{ 897 stream_mgr_lock(); 898 stream_mgr_unlock(); 899} 900 901/* Returns the smallest file window that includes all active streams' 902 * windows */ 903static bool stream_get_window_callback(struct stream *str, 904 struct stream_window *sw) 905{ 906 off_t swl = str->hdr.win_left; 907 off_t swr = str->hdr.win_right; 908 909 if (swl < sw->left) 910 sw->left = swl; 911 912 if (swr > sw->right) 913 sw->right = swr; 914 915 return true; 916} 917 918bool stream_get_window(struct stream_window *sw) 919{ 920 if (sw == NULL) 921 return false; 922 923 sw->left = LONG_MAX; 924 sw->right = LONG_MIN; 925 926 actl_lock(); 927 list_enum_items(stream_mgr.actl, 928 (list_enum_callback_t)stream_get_window_callback, 929 (void*)sw); 930 actl_unlock(); 931 932 return sw->left <= sw->right; 933} 934 935/* Playback control thread */ 936static void stream_mgr_thread(void) 937{ 938 struct queue_event ev; 939 940 while (1) 941 { 942 rb->queue_wait(stream_mgr.q, &ev); 943 944 switch (ev.id) 945 { 946 case STREAM_OPEN: 947 stream_on_open((const char *)ev.data); 948 break; 949 950 case STREAM_CLOSE: 951 stream_on_close(); 952 break; 953 954 case STREAM_PLAY: 955 stream_on_play(); 956 break; 957 958 case STREAM_PAUSE: 959 if (ev.data) 960 stream_on_resume(); 961 else 962 stream_on_pause(); 963 break; 964 965 case STREAM_STOP: 966 stream_on_stop(true); 967 break; 968 969 case STREAM_SEEK: 970 stream_on_seek((struct stream_seek_data *)ev.data); 971 break; 972 973 case STREAM_EV_COMPLETE: 974 stream_on_ev_complete((struct stream *)ev.data); 975 break; 976 977 case STREAM_QUIT: 978 if (stream_mgr.status != STREAM_STOPPED) 979 stream_on_stop(false); 980 return; 981 } 982 } 983} 984 985/* Stream command interface APIs */ 986 987/* Opens a new file */ 988int stream_open(const char *filename) 989{ 990 if (stream_mgr.thread != 0) 991 return stream_mgr_send_msg(STREAM_OPEN, (intptr_t)filename); 992 return STREAM_ERROR; 993} 994 995/* Plays the current file starting at time 'start' */ 996int stream_play(void) 997{ 998 if (stream_mgr.thread != 0) 999 return stream_mgr_send_msg(STREAM_PLAY, 0); 1000 return STREAM_ERROR; 1001} 1002 1003/* Pauses playback if playing */ 1004int stream_pause(void) 1005{ 1006 if (stream_mgr.thread != 0) 1007 return stream_mgr_send_msg(STREAM_PAUSE, false); 1008 return STREAM_ERROR; 1009} 1010 1011/* Resumes playback if paused */ 1012int stream_resume(void) 1013{ 1014 if (stream_mgr.thread != 0) 1015 return stream_mgr_send_msg(STREAM_PAUSE, true); 1016 return STREAM_ERROR; 1017} 1018 1019/* Stops playback if not stopped */ 1020int stream_stop(void) 1021{ 1022 if (stream_mgr.thread != 0) 1023 return stream_mgr_send_msg(STREAM_STOP, 0); 1024 return STREAM_ERROR; 1025} 1026 1027/* Seeks playback time to/by the specified time */ 1028int stream_seek(uint32_t time, int whence) 1029{ 1030 int ret; 1031 1032 if (stream_mgr.thread == 0) 1033 return STREAM_ERROR; 1034 1035 stream_mgr_lock(); 1036 1037 stream_mgr.parms.skd.time = time; 1038 stream_mgr.parms.skd.whence = whence; 1039 1040 ret = stream_mgr_send_msg(STREAM_SEEK, (intptr_t)&stream_mgr.parms.skd); 1041 1042 stream_mgr_unlock(); 1043 1044 return ret; 1045} 1046 1047/* Closes the current file */ 1048int stream_close(void) 1049{ 1050 if (stream_mgr.thread != 0) 1051 return stream_mgr_send_msg(STREAM_CLOSE, 0); 1052 return STREAM_ERROR; 1053} 1054 1055/* Initializes the playback engine */ 1056int stream_init(void) 1057{ 1058 void *mem; 1059 size_t memsize; 1060 1061 stream_mgr.status = STREAM_STOPPED; 1062 stream_mgr_init_state(); 1063 1064 /* Initialize our window to the outside world first */ 1065 rb->mutex_init(&stream_mgr.str_mtx); 1066 rb->mutex_init(&stream_mgr.actl_mtx); 1067 1068 stream_mgr.q = &stream_mgr_queue; 1069 rb->queue_init(stream_mgr.q, false); 1070 1071 /* sets audiosize and returns buffer pointer */ 1072 mem = rb->plugin_get_audio_buffer(&memsize); 1073 1074 /* Initialize non-allocator blocks first */ 1075#ifndef HAVE_LCD_COLOR 1076 long greysize; 1077 1078 /* Greylib init handles all necessary cache alignment */ 1079 if (!grey_init(mem, memsize, GREY_BUFFERED|GREY_ON_COP, 1080 LCD_WIDTH, LCD_HEIGHT, &greysize)) 1081 { 1082 rb->splash(HZ, "greylib init failed!"); 1083 return STREAM_ERROR; 1084 } 1085 1086 mem += greysize; 1087 memsize -= greysize; 1088 1089 grey_clear_display(); 1090#endif /* !HAVE_LCD_COLOR */ 1091 1092 stream_mgr.thread = rb->create_thread(stream_mgr_thread, 1093 stream_mgr_thread_stack, sizeof(stream_mgr_thread_stack), 1094 0, "mpgstream_mgr" IF_PRIO(, PRIORITY_SYSTEM) IF_COP(, CPU)); 1095 1096 rb->queue_enable_queue_send(stream_mgr.q, &stream_mgr_queue_send, 1097 stream_mgr.thread); 1098 1099 if (stream_mgr.thread == 0) 1100 { 1101 rb->splash(HZ, "Could not create stream manager thread!"); 1102 return STREAM_ERROR; 1103 } 1104 1105 /* Wait for thread to initialize */ 1106 stream_mgr_send_msg(STREAM_NULL, 0); 1107 1108 /* Initialise our malloc buffer */ 1109 if (!mpeg_alloc_init(mem, memsize)) 1110 { 1111 rb->splash(HZ, "Out of memory in stream_init"); 1112 } 1113 /* These inits use the allocator */ 1114 else if (!pcm_output_init()) 1115 { 1116 rb->splash(HZ, "Could not initialize PCM!"); 1117 } 1118 else if (!audio_thread_init()) 1119 { 1120 rb->splash(HZ, "Cannot create audio thread!"); 1121 } 1122 else if (!video_thread_init()) 1123 { 1124 rb->splash(HZ, "Cannot create video thread!"); 1125 } 1126 /* Disk buffer takes max allotment of what's left so it must be last */ 1127 else if (!disk_buf_init()) 1128 { 1129 rb->splash(HZ, "Cannot create buffering thread!"); 1130 } 1131 else if (!parser_init()) 1132 { 1133 rb->splash(HZ, "Parser init failed!"); 1134 } 1135 else 1136 { 1137 return STREAM_OK; 1138 } 1139 1140 return STREAM_ERROR; 1141} 1142 1143/* Cleans everything up */ 1144void stream_exit(void) 1145{ 1146 stream_close(); 1147 1148 /* Stop the threads and wait for them to terminate */ 1149 video_thread_exit(); 1150 audio_thread_exit(); 1151 disk_buf_exit(); 1152 pcm_output_exit(); 1153 1154 if (stream_mgr.thread != 0) 1155 { 1156 stream_mgr_post_msg(STREAM_QUIT, 0); 1157 rb->thread_wait(stream_mgr.thread); 1158 stream_mgr.thread = 0; 1159 } 1160 1161#ifndef HAVE_LCD_COLOR 1162 grey_release(); 1163#endif 1164}