Michael Sevakis | a222f27 | 2007-12-29 19:46:35 +0000 | [diff] [blame] | 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 | * |
Daniel Stenberg | 2acc0ac | 2008-06-28 18:10:04 +0000 | [diff] [blame^] | 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. |
Michael Sevakis | a222f27 | 2007-12-29 19:46:35 +0000 | [diff] [blame] | 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" |
Jens Arnold | feb5b15 | 2008-01-04 23:42:38 +0000 | [diff] [blame] | 25 | #include "grey.h" |
Michael Sevakis | a222f27 | 2007-12-29 19:46:35 +0000 | [diff] [blame] | 26 | #include "mpeg_settings.h" |
| 27 | |
Jens Arnold | a72499a | 2008-01-13 00:11:43 +0000 | [diff] [blame] | 28 | #ifndef HAVE_LCD_COLOR |
| 29 | GREY_INFO_STRUCT_IRAM |
| 30 | #endif |
| 31 | |
Michael Sevakis | 0509914 | 2008-04-06 04:34:57 +0000 | [diff] [blame] | 32 | static struct event_queue stream_mgr_queue SHAREDBSS_ATTR; |
| 33 | static struct queue_sender_list stream_mgr_queue_send SHAREDBSS_ATTR; |
Michael Sevakis | a222f27 | 2007-12-29 19:46:35 +0000 | [diff] [blame] | 34 | static uint32_t stream_mgr_thread_stack[DEFAULT_STACK_SIZE*2/sizeof(uint32_t)]; |
| 35 | |
Michael Sevakis | 0509914 | 2008-04-06 04:34:57 +0000 | [diff] [blame] | 36 | struct stream_mgr stream_mgr SHAREDBSS_ATTR; |
Michael Sevakis | a222f27 | 2007-12-29 19:46:35 +0000 | [diff] [blame] | 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; |
Michael Sevakis | 29775f7 | 2008-01-12 05:45:38 +0000 | [diff] [blame] | 142 | stream_mgr.resume_time = INVALID_TIMESTAMP; |
Michael Sevakis | a222f27 | 2007-12-29 19:46:35 +0000 | [diff] [blame] | 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(&str->l); |
| 152 | list_add_item(&stream_mgr.strl, &str->l); |
| 153 | |
| 154 | actl_unlock(); |
| 155 | } |
| 156 | |
| 157 | /* Callback for various list-moving operations */ |
| 158 | static bool strl_enum_callback(struct list_item *item, intptr_t data) |
| 159 | { |
| 160 | actl_lock(); |
| 161 | |
| 162 | list_remove_item(item); |
| 163 | |
| 164 | if (data == 1) |
| 165 | list_add_item(&stream_mgr.actl, item); |
| 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 | list_enum_items(&stream_mgr.strl, strl_enum_callback, 0); |
| 176 | } |
| 177 | |
| 178 | /* Move the playback pool to the active list */ |
| 179 | void move_strl_to_actl(void) |
| 180 | { |
| 181 | list_enum_items(&stream_mgr.strl, strl_enum_callback, 1); |
| 182 | } |
| 183 | |
| 184 | /* Remove a stream from the active list and return it to the pool */ |
| 185 | static bool actl_stream_remove(struct stream *str) |
| 186 | { |
| 187 | if (list_is_member(&stream_mgr.actl, &str->l)) |
| 188 | { |
| 189 | actl_lock(); |
| 190 | |
| 191 | list_remove_item(&str->l); |
| 192 | list_add_item(&stream_mgr.strl, &str->l); |
| 193 | |
| 194 | actl_unlock(); |
| 195 | return true; |
| 196 | } |
| 197 | |
| 198 | return false; |
| 199 | } |
| 200 | |
| 201 | /* Broadcast a message to all active streams */ |
| 202 | static bool actl_stream_broadcast_callback(struct list_item *item, |
| 203 | struct str_broadcast_data *sbd) |
| 204 | { |
| 205 | struct stream *str = TYPE_FROM_MEMBER(struct stream, item, l); |
| 206 | |
| 207 | switch (sbd->cmd) |
| 208 | { |
| 209 | case STREAM_PLAY: |
| 210 | case STREAM_PAUSE: |
| 211 | break; |
| 212 | |
| 213 | case STREAM_STOP: |
| 214 | if (sbd->data != 0) |
| 215 | { |
| 216 | actl_lock(); |
| 217 | |
| 218 | list_remove_item(item); |
| 219 | list_add_item(&stream_mgr.strl, item); |
| 220 | |
| 221 | actl_unlock(); |
| 222 | sbd->data = 0; |
| 223 | } |
| 224 | break; |
| 225 | |
| 226 | default: |
| 227 | return false; |
| 228 | } |
| 229 | |
| 230 | str_send_msg(str, sbd->cmd, sbd->data); |
| 231 | return true; |
| 232 | } |
| 233 | |
| 234 | static void actl_stream_broadcast(int cmd, intptr_t data) |
| 235 | { |
| 236 | struct str_broadcast_data sbd; |
| 237 | sbd.cmd = cmd; |
| 238 | sbd.data = data; |
| 239 | list_enum_items(&stream_mgr.actl, |
| 240 | (list_enum_callback_t)actl_stream_broadcast_callback, |
| 241 | (intptr_t)&sbd); |
| 242 | } |
| 243 | |
| 244 | /* Set the current base clock */ |
| 245 | static void set_stream_clock(uint32_t time) |
| 246 | { |
| 247 | /* Fudge: Start clock 100ms early to allow for some filling time */ |
| 248 | if (time > 100*TS_SECOND/1000) |
| 249 | time -= 100*TS_SECOND/1000; |
| 250 | else |
| 251 | time = 0; |
| 252 | |
| 253 | pcm_output_set_clock(TS_TO_TICKS(time)); |
| 254 | } |
| 255 | |
Michael Sevakis | 0e98d7e | 2008-01-07 14:58:23 +0000 | [diff] [blame] | 256 | static void stream_start_playback(uint32_t time, bool fill_buffer) |
| 257 | { |
| 258 | if (stream_mgr.seeked) |
| 259 | { |
| 260 | /* Clear any seeked status */ |
| 261 | stream_mgr.seeked = false; |
| 262 | |
| 263 | /* Flush old PCM data */ |
| 264 | pcm_output_flush(); |
| 265 | |
| 266 | /* Set the master clock */ |
| 267 | set_stream_clock(time); |
| 268 | |
| 269 | /* Make sure streams are back in active pool */ |
| 270 | move_strl_to_actl(); |
| 271 | |
| 272 | /* Prepare the parser and associated streams */ |
| 273 | parser_prepare_streaming(); |
| 274 | } |
| 275 | |
| 276 | /* Start buffer which optional force fill */ |
| 277 | disk_buf_send_msg(STREAM_PLAY, fill_buffer); |
| 278 | |
| 279 | /* Tell each stream to start - may generate end of stream signals |
| 280 | * now - we'll handle this when finished */ |
| 281 | actl_stream_broadcast(STREAM_PLAY, 0); |
| 282 | |
| 283 | /* Actually start the clock */ |
| 284 | pcm_output_play_pause(true); |
| 285 | } |
| 286 | |
Michael Sevakis | a222f27 | 2007-12-29 19:46:35 +0000 | [diff] [blame] | 287 | /* Return the play time relative to the specified play time */ |
| 288 | static uint32_t time_from_whence(uint32_t time, int whence) |
| 289 | { |
| 290 | int64_t currtime; |
Michael Sevakis | a5fc3f4 | 2008-01-03 17:14:28 +0000 | [diff] [blame] | 291 | uint32_t start; |
Michael Sevakis | a222f27 | 2007-12-29 19:46:35 +0000 | [diff] [blame] | 292 | |
| 293 | switch (whence) |
| 294 | { |
| 295 | case SEEK_SET: |
| 296 | /* Set the current time (time = unsigned offset from 0) */ |
| 297 | if (time > str_parser.duration) |
| 298 | time = str_parser.duration; |
| 299 | break; |
| 300 | case SEEK_CUR: |
| 301 | /* Seek forward or backward from the current time |
| 302 | * (time = signed offset from current) */ |
Michael Sevakis | a5fc3f4 | 2008-01-03 17:14:28 +0000 | [diff] [blame] | 303 | currtime = stream_get_seek_time(&start); |
| 304 | currtime -= start; |
Michael Sevakis | a222f27 | 2007-12-29 19:46:35 +0000 | [diff] [blame] | 305 | currtime += (int32_t)time; |
| 306 | |
| 307 | if (currtime < 0) |
| 308 | currtime = 0; |
| 309 | else if ((uint64_t)currtime > str_parser.duration) |
| 310 | currtime = str_parser.duration; |
| 311 | |
| 312 | time = (uint32_t)currtime; |
| 313 | break; |
| 314 | case SEEK_END: |
| 315 | /* Seek from the end (time = unsigned offset from end) */ |
| 316 | if (time > str_parser.duration) |
| 317 | time = str_parser.duration; |
| 318 | time = str_parser.duration - time; |
| 319 | break; |
| 320 | } |
| 321 | |
| 322 | return time; |
| 323 | } |
| 324 | |
| 325 | /* Handle seeking details if playing or paused */ |
Michael Sevakis | 0e98d7e | 2008-01-07 14:58:23 +0000 | [diff] [blame] | 326 | static uint32_t stream_seek_intl(uint32_t time, int whence, |
| 327 | int status, bool *was_buffering) |
Michael Sevakis | a222f27 | 2007-12-29 19:46:35 +0000 | [diff] [blame] | 328 | { |
Michael Sevakis | 0e98d7e | 2008-01-07 14:58:23 +0000 | [diff] [blame] | 329 | if (status != STREAM_STOPPED) |
Michael Sevakis | a222f27 | 2007-12-29 19:46:35 +0000 | [diff] [blame] | 330 | { |
Michael Sevakis | 0e98d7e | 2008-01-07 14:58:23 +0000 | [diff] [blame] | 331 | bool wb; |
| 332 | |
| 333 | /* Place streams in a non-running state - keep them on actl */ |
| 334 | actl_stream_broadcast(STREAM_STOP, 0); |
| 335 | |
| 336 | /* Stop all buffering or else risk clobbering random-access data */ |
| 337 | wb = disk_buf_send_msg(STREAM_STOP, 0); |
| 338 | |
| 339 | if (was_buffering != NULL) |
| 340 | *was_buffering = wb; |
Michael Sevakis | a222f27 | 2007-12-29 19:46:35 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Michael Sevakis | a222f27 | 2007-12-29 19:46:35 +0000 | [diff] [blame] | 343 | time = time_from_whence(time, whence); |
Michael Sevakis | a222f27 | 2007-12-29 19:46:35 +0000 | [diff] [blame] | 344 | |
Michael Sevakis | 0e98d7e | 2008-01-07 14:58:23 +0000 | [diff] [blame] | 345 | stream_mgr.seeked = true; |
Michael Sevakis | a222f27 | 2007-12-29 19:46:35 +0000 | [diff] [blame] | 346 | |
Michael Sevakis | 0e98d7e | 2008-01-07 14:58:23 +0000 | [diff] [blame] | 347 | return parser_seek_time(time); |
Michael Sevakis | a222f27 | 2007-12-29 19:46:35 +0000 | [diff] [blame] | 348 | } |
| 349 | |
Michael Sevakis | 29775f7 | 2008-01-12 05:45:38 +0000 | [diff] [blame] | 350 | /* Store the resume time at the last seek/current clock point */ |
| 351 | static void stream_remember_resume_time(void) |
| 352 | { |
| 353 | /* Assume invalidity */ |
| 354 | stream_mgr.resume_time = 0; |
| 355 | |
| 356 | if (stream_can_seek()) |
| 357 | { |
| 358 | /* Read the current stream time or the last seeked position */ |
| 359 | uint32_t start; |
| 360 | uint32_t time = stream_get_seek_time(&start); |
| 361 | |
Michael Sevakis | 93ddef3 | 2008-01-16 02:43:42 +0000 | [diff] [blame] | 362 | if (time >= str_parser.start_pts && time <= str_parser.end_pts) |
Michael Sevakis | 29775f7 | 2008-01-12 05:45:38 +0000 | [diff] [blame] | 363 | { |
| 364 | /* Save the current stream time */ |
| 365 | stream_mgr.resume_time = time - start; |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | |
Michael Sevakis | a222f27 | 2007-12-29 19:46:35 +0000 | [diff] [blame] | 370 | /* Handle STREAM_OPEN */ |
| 371 | void stream_on_open(const char *filename) |
| 372 | { |
| 373 | int err = STREAM_ERROR; |
| 374 | |
| 375 | stream_mgr_lock(); |
| 376 | |
| 377 | trigger_cpu_boost(); |
| 378 | |
| 379 | /* Open the video file */ |
| 380 | if (disk_buf_open(filename) >= 0) |
| 381 | { |
| 382 | /* Initialize the parser */ |
| 383 | err = parser_init_stream(); |
| 384 | |
| 385 | if (err >= STREAM_OK) |
| 386 | { |
| 387 | /* File ok - save the opened filename */ |
| 388 | stream_mgr.filename = filename; |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | /* If error - cleanup */ |
| 393 | if (err < STREAM_OK) |
| 394 | stream_on_close(); |
| 395 | |
| 396 | cancel_cpu_boost(); |
| 397 | |
| 398 | stream_mgr_unlock(); |
| 399 | |
| 400 | stream_mgr_reply_msg(err); |
| 401 | } |
| 402 | |
| 403 | /* Handler STREAM_PLAY */ |
| 404 | static void stream_on_play(void) |
| 405 | { |
| 406 | int status = stream_mgr.status; |
| 407 | |
| 408 | stream_mgr_lock(); |
| 409 | |
| 410 | if (status == STREAM_STOPPED) |
| 411 | { |
| 412 | uint32_t start; |
| 413 | |
| 414 | /* We just say we're playing now */ |
| 415 | stream_mgr.status = STREAM_PLAYING; |
| 416 | |
| 417 | /* Reply with previous state */ |
| 418 | stream_mgr_reply_msg(status); |
| 419 | |
| 420 | trigger_cpu_boost(); |
| 421 | |
| 422 | /* Seek to initial position and set clock to that time */ |
| 423 | |
| 424 | /* Save the resume time */ |
Michael Sevakis | 29775f7 | 2008-01-12 05:45:38 +0000 | [diff] [blame] | 425 | stream_remember_resume_time(); |
Michael Sevakis | a222f27 | 2007-12-29 19:46:35 +0000 | [diff] [blame] | 426 | |
Michael Sevakis | 0e98d7e | 2008-01-07 14:58:23 +0000 | [diff] [blame] | 427 | /* Prepare seek to start point */ |
Michael Sevakis | 29775f7 | 2008-01-12 05:45:38 +0000 | [diff] [blame] | 428 | start = stream_seek_intl(stream_mgr.resume_time, SEEK_SET, |
| 429 | STREAM_STOPPED, NULL); |
Michael Sevakis | a222f27 | 2007-12-29 19:46:35 +0000 | [diff] [blame] | 430 | |
Michael Sevakis | 0e98d7e | 2008-01-07 14:58:23 +0000 | [diff] [blame] | 431 | /* Sync and start - force buffer fill */ |
| 432 | stream_start_playback(start, true); |
Michael Sevakis | a222f27 | 2007-12-29 19:46:35 +0000 | [diff] [blame] | 433 | } |
| 434 | else |
| 435 | { |
| 436 | /* Reply with previous state */ |
| 437 | stream_mgr_reply_msg(status); |
| 438 | } |
| 439 | |
| 440 | stream_mgr_unlock(); |
| 441 | } |
| 442 | |
| 443 | /* Handle STREAM_PAUSE */ |
| 444 | static void stream_on_pause(void) |
| 445 | { |
| 446 | int status = stream_mgr.status; |
| 447 | |
| 448 | stream_mgr_lock(); |
| 449 | |
| 450 | /* Reply with previous state */ |
| 451 | stream_mgr_reply_msg(status); |
| 452 | |
| 453 | if (status == STREAM_PLAYING) |
| 454 | { |
| 455 | /* Pause the clock */ |
| 456 | pcm_output_play_pause(false); |
| 457 | |
| 458 | /* Pause each active stream */ |
| 459 | actl_stream_broadcast(STREAM_PAUSE, 0); |
| 460 | |
| 461 | /* Pause the disk buffer - buffer may continue filling */ |
| 462 | disk_buf_send_msg(STREAM_PAUSE, false); |
| 463 | |
| 464 | /* Unboost the CPU */ |
| 465 | cancel_cpu_boost(); |
| 466 | |
| 467 | /* Offically paused */ |
| 468 | stream_mgr.status = STREAM_PAUSED; |
| 469 | } |
| 470 | |
| 471 | stream_mgr_unlock(); |
| 472 | } |
| 473 | |
| 474 | /* Handle STREAM_RESUME */ |
| 475 | static void stream_on_resume(void) |
| 476 | { |
| 477 | int status = stream_mgr.status; |
| 478 | |
| 479 | stream_mgr_lock(); |
| 480 | |
| 481 | /* Reply with previous state */ |
| 482 | stream_mgr_reply_msg(status); |
| 483 | |
| 484 | if (status == STREAM_PAUSED) |
| 485 | { |
| 486 | /* Boost the CPU */ |
| 487 | trigger_cpu_boost(); |
| 488 | |
Michael Sevakis | 0e98d7e | 2008-01-07 14:58:23 +0000 | [diff] [blame] | 489 | /* Sync and start - no force buffering */ |
| 490 | stream_start_playback(str_parser.last_seek_time, false); |
Michael Sevakis | a222f27 | 2007-12-29 19:46:35 +0000 | [diff] [blame] | 491 | |
| 492 | /* Officially playing */ |
| 493 | stream_mgr.status = STREAM_PLAYING; |
| 494 | } |
| 495 | |
| 496 | stream_mgr_unlock(); |
| 497 | } |
| 498 | |
| 499 | /* Handle STREAM_STOP */ |
| 500 | static void stream_on_stop(bool reply) |
| 501 | { |
| 502 | int status = stream_mgr.status; |
| 503 | |
| 504 | stream_mgr_lock(); |
| 505 | |
| 506 | if (reply) |
| 507 | stream_mgr_reply_msg(status); |
| 508 | |
| 509 | if (status != STREAM_STOPPED) |
| 510 | { |
Michael Sevakis | a222f27 | 2007-12-29 19:46:35 +0000 | [diff] [blame] | 511 | /* Pause the clock */ |
| 512 | pcm_output_play_pause(false); |
| 513 | |
Michael Sevakis | 29775f7 | 2008-01-12 05:45:38 +0000 | [diff] [blame] | 514 | /* Update the resume time info */ |
| 515 | stream_remember_resume_time(); |
Michael Sevakis | a222f27 | 2007-12-29 19:46:35 +0000 | [diff] [blame] | 516 | |
Michael Sevakis | a5fc3f4 | 2008-01-03 17:14:28 +0000 | [diff] [blame] | 517 | /* Not stopped = paused or playing */ |
| 518 | stream_mgr.seeked = false; |
| 519 | |
Michael Sevakis | a222f27 | 2007-12-29 19:46:35 +0000 | [diff] [blame] | 520 | /* Stop buffering */ |
| 521 | disk_buf_send_msg(STREAM_STOP, 0); |
| 522 | |
| 523 | /* Clear any still-active streams and remove from actl */ |
| 524 | actl_stream_broadcast(STREAM_STOP, 1); |
| 525 | |
| 526 | /* Stop PCM output (and clock) */ |
| 527 | pcm_output_stop(); |
| 528 | |
| 529 | /* Cancel our processor boost */ |
| 530 | cancel_cpu_boost(); |
| 531 | |
| 532 | stream_mgr.status = STREAM_STOPPED; |
| 533 | } |
| 534 | |
| 535 | stream_mgr_unlock(); |
| 536 | } |
| 537 | |
| 538 | /* Handle STREAM_SEEK */ |
| 539 | static void stream_on_seek(struct stream_seek_data *skd) |
| 540 | { |
| 541 | uint32_t time = skd->time; |
| 542 | int whence = skd->whence; |
| 543 | |
| 544 | switch (whence) |
| 545 | { |
| 546 | case SEEK_SET: |
| 547 | case SEEK_CUR: |
| 548 | case SEEK_END: |
| 549 | if (stream_mgr.filename == NULL) |
| 550 | break; |
| 551 | |
Michael Sevakis | 0e98d7e | 2008-01-07 14:58:23 +0000 | [diff] [blame] | 552 | /* Keep things spinning if already doing so */ |
Michael Sevakis | a222f27 | 2007-12-29 19:46:35 +0000 | [diff] [blame] | 553 | stream_keep_disk_active(); |
| 554 | |
Michael Sevakis | 0e98d7e | 2008-01-07 14:58:23 +0000 | [diff] [blame] | 555 | /* Have data - reply in order to acquire lock */ |
Michael Sevakis | a5fc3f4 | 2008-01-03 17:14:28 +0000 | [diff] [blame] | 556 | stream_mgr_reply_msg(STREAM_OK); |
| 557 | |
Michael Sevakis | a222f27 | 2007-12-29 19:46:35 +0000 | [diff] [blame] | 558 | stream_mgr_lock(); |
| 559 | |
Michael Sevakis | c898630 | 2008-01-04 20:36:15 +0000 | [diff] [blame] | 560 | if (stream_can_seek()) |
Michael Sevakis | a222f27 | 2007-12-29 19:46:35 +0000 | [diff] [blame] | 561 | { |
Michael Sevakis | 0e98d7e | 2008-01-07 14:58:23 +0000 | [diff] [blame] | 562 | bool buffer; |
| 563 | |
| 564 | if (stream_mgr.status == STREAM_PLAYING) |
Michael Sevakis | a222f27 | 2007-12-29 19:46:35 +0000 | [diff] [blame] | 565 | { |
Michael Sevakis | 0e98d7e | 2008-01-07 14:58:23 +0000 | [diff] [blame] | 566 | /* Keep clock from advancing while seeking */ |
| 567 | pcm_output_play_pause(false); |
Michael Sevakis | a222f27 | 2007-12-29 19:46:35 +0000 | [diff] [blame] | 568 | } |
Michael Sevakis | 0e98d7e | 2008-01-07 14:58:23 +0000 | [diff] [blame] | 569 | |
| 570 | time = stream_seek_intl(time, whence, stream_mgr.status, &buffer); |
Michael Sevakis | 29775f7 | 2008-01-12 05:45:38 +0000 | [diff] [blame] | 571 | stream_remember_resume_time(); |
Michael Sevakis | 0e98d7e | 2008-01-07 14:58:23 +0000 | [diff] [blame] | 572 | |
| 573 | if (stream_mgr.status == STREAM_PLAYING) |
Michael Sevakis | a222f27 | 2007-12-29 19:46:35 +0000 | [diff] [blame] | 574 | { |
Michael Sevakis | 0e98d7e | 2008-01-07 14:58:23 +0000 | [diff] [blame] | 575 | /* Sync and restart - no force buffering */ |
| 576 | stream_start_playback(time, buffer); |
Michael Sevakis | a222f27 | 2007-12-29 19:46:35 +0000 | [diff] [blame] | 577 | } |
| 578 | } |
Michael Sevakis | a222f27 | 2007-12-29 19:46:35 +0000 | [diff] [blame] | 579 | |
| 580 | stream_mgr_unlock(); |
| 581 | return; |
| 582 | } |
| 583 | |
Michael Sevakis | 0e98d7e | 2008-01-07 14:58:23 +0000 | [diff] [blame] | 584 | /* Invalid parameter or no file */ |
Michael Sevakis | a222f27 | 2007-12-29 19:46:35 +0000 | [diff] [blame] | 585 | stream_mgr_reply_msg(STREAM_ERROR); |
| 586 | } |
| 587 | |
| 588 | /* Handle STREAM_CLOSE */ |
| 589 | static int stream_on_close(void) |
| 590 | { |
| 591 | int status = STREAM_STOPPED; |
| 592 | |
| 593 | stream_mgr_lock(); |
| 594 | |
| 595 | /* Any open file? */ |
| 596 | if (stream_mgr.filename != NULL) |
| 597 | { |
| 598 | /* Yes - hide video */ |
| 599 | stream_show_vo(false); |
| 600 | /* Stop any playback */ |
| 601 | status = stream_mgr.status; |
| 602 | stream_on_stop(false); |
| 603 | /* Tell parser file is finished */ |
| 604 | parser_close_stream(); |
| 605 | /* Close file */ |
| 606 | disk_buf_close(); |
| 607 | /* Reinitialize manager */ |
| 608 | stream_mgr_init_state(); |
| 609 | } |
| 610 | |
| 611 | stream_mgr_unlock(); |
| 612 | |
| 613 | return status; |
| 614 | } |
| 615 | |
| 616 | /* Handle STREAM_EV_COMPLETE */ |
| 617 | static void stream_on_ev_complete(struct stream *str) |
| 618 | { |
| 619 | stream_mgr_lock(); |
| 620 | |
| 621 | /* Stream is active? */ |
| 622 | if (actl_stream_remove(str)) |
| 623 | { |
| 624 | /* No - remove this stream from the active list */ |
| 625 | DEBUGF(" finished: 0x%02x\n", str->id); |
| 626 | if (list_is_empty(&stream_mgr.actl)) |
| 627 | { |
| 628 | /* All streams have acked - stop playback */ |
| 629 | stream_on_stop(false); |
| 630 | stream_mgr.resume_time = 0; /* Played to end - no resume */ |
| 631 | } |
| 632 | else |
| 633 | { |
| 634 | /* Stream is done - stop it and place back in pool */ |
| 635 | str_send_msg(str, STREAM_STOP, 1); |
| 636 | } |
| 637 | } |
| 638 | |
| 639 | stream_mgr_unlock(); |
| 640 | } |
| 641 | |
| 642 | /* Callback for stream to notify about events internal to them */ |
| 643 | void stream_generate_event(struct stream *str, long id, intptr_t data) |
| 644 | { |
| 645 | if (str == NULL) |
| 646 | return; |
| 647 | |
| 648 | switch (id) |
| 649 | { |
| 650 | case STREAM_EV_COMPLETE: |
| 651 | /* The last stream has ended */ |
| 652 | stream_mgr_post_msg(STREAM_EV_COMPLETE, (intptr_t)str); |
| 653 | break; |
| 654 | } |
| 655 | |
| 656 | (void)data; |
| 657 | } |
| 658 | |
| 659 | /* Clear any particular notification for which a stream registered */ |
| 660 | void stream_clear_notify(struct stream *str, int for_msg) |
| 661 | { |
| 662 | switch (for_msg) |
| 663 | { |
| 664 | case DISK_BUF_DATA_NOTIFY: |
| 665 | disk_buf_send_msg(DISK_BUF_CLEAR_DATA_NOTIFY, (intptr_t)str); |
| 666 | break; |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | /* Show/hide the video output */ |
| 671 | bool stream_show_vo(bool show) |
| 672 | { |
| 673 | bool vis; |
| 674 | stream_mgr_lock(); |
| 675 | |
| 676 | vis = parser_send_video_msg(VIDEO_DISPLAY_SHOW, show); |
| 677 | #ifndef HAVE_LCD_COLOR |
Jens Arnold | feb5b15 | 2008-01-04 23:42:38 +0000 | [diff] [blame] | 678 | grey_show(show); |
Michael Sevakis | a222f27 | 2007-12-29 19:46:35 +0000 | [diff] [blame] | 679 | #endif |
| 680 | stream_mgr_unlock(); |
| 681 | |
| 682 | return vis; |
| 683 | } |
| 684 | |
| 685 | /* Query the visibility of video output */ |
| 686 | bool stream_vo_is_visible(void) |
| 687 | { |
| 688 | bool vis; |
| 689 | stream_mgr_lock(); |
| 690 | vis = parser_send_video_msg(VIDEO_DISPLAY_IS_VISIBLE, 0); |
| 691 | stream_mgr_unlock(); |
| 692 | return vis; |
| 693 | } |
| 694 | |
| 695 | /* Return the video dimensions */ |
| 696 | bool stream_vo_get_size(struct vo_ext *sz) |
| 697 | { |
| 698 | bool retval = false; |
| 699 | |
| 700 | stream_mgr_lock(); |
| 701 | |
| 702 | if (str_parser.dims.w > 0 && str_parser.dims.h > 0) |
| 703 | { |
| 704 | *sz = str_parser.dims; |
| 705 | retval = true; |
| 706 | } |
| 707 | |
| 708 | stream_mgr_unlock(); |
| 709 | |
| 710 | return retval; |
| 711 | } |
| 712 | |
Michael Sevakis | a5fc3f4 | 2008-01-03 17:14:28 +0000 | [diff] [blame] | 713 | void stream_vo_set_clip(const struct vo_rect *rc) |
| 714 | { |
| 715 | stream_mgr_lock(); |
| 716 | |
| 717 | if (rc) |
| 718 | { |
| 719 | stream_mgr.parms.rc = *rc; |
| 720 | rc = &stream_mgr.parms.rc; |
| 721 | } |
Michael Sevakis | a5fc3f4 | 2008-01-03 17:14:28 +0000 | [diff] [blame] | 722 | |
| 723 | parser_send_video_msg(VIDEO_SET_CLIP_RECT, (intptr_t)rc); |
| 724 | |
Michael Sevakis | a5fc3f4 | 2008-01-03 17:14:28 +0000 | [diff] [blame] | 725 | stream_mgr_unlock(); |
| 726 | } |
| 727 | |
Michael Sevakis | a222f27 | 2007-12-29 19:46:35 +0000 | [diff] [blame] | 728 | #ifndef HAVE_LCD_COLOR |
Michael Sevakis | a222f27 | 2007-12-29 19:46:35 +0000 | [diff] [blame] | 729 | /* Show/hide the gray video overlay (independently of vo visibility). */ |
| 730 | void stream_gray_show(bool show) |
| 731 | { |
| 732 | stream_mgr_lock(); |
| 733 | |
Jens Arnold | feb5b15 | 2008-01-04 23:42:38 +0000 | [diff] [blame] | 734 | grey_show(show); |
Michael Sevakis | a5fc3f4 | 2008-01-03 17:14:28 +0000 | [diff] [blame] | 735 | |
Michael Sevakis | a222f27 | 2007-12-29 19:46:35 +0000 | [diff] [blame] | 736 | stream_mgr_unlock(); |
| 737 | } |
Michael Sevakis | 75380fd | 2008-01-09 22:19:25 +0000 | [diff] [blame] | 738 | |
Michael Sevakis | ea5d196 | 2008-01-10 19:38:38 +0000 | [diff] [blame] | 739 | #endif /* !HAVE_LCD_COLOR */ |
Michael Sevakis | a222f27 | 2007-12-29 19:46:35 +0000 | [diff] [blame] | 740 | |
| 741 | /* Display a thumbnail at the last seek point */ |
| 742 | bool stream_display_thumb(const struct vo_rect *rc) |
| 743 | { |
| 744 | bool retval; |
| 745 | |
| 746 | if (rc == NULL) |
| 747 | return false; |
| 748 | |
| 749 | stream_mgr_lock(); |
| 750 | |
| 751 | stream_mgr.parms.rc = *rc; |
| 752 | retval = parser_send_video_msg(VIDEO_PRINT_THUMBNAIL, |
| 753 | (intptr_t)&stream_mgr.parms.rc); |
| 754 | |
| 755 | stream_mgr_unlock(); |
Michael Sevakis | a5fc3f4 | 2008-01-03 17:14:28 +0000 | [diff] [blame] | 756 | |
| 757 | return retval; |
| 758 | } |
| 759 | |
| 760 | bool stream_draw_frame(bool no_prepare) |
| 761 | { |
| 762 | bool retval; |
| 763 | stream_mgr_lock(); |
| 764 | |
Michael Sevakis | a5fc3f4 | 2008-01-03 17:14:28 +0000 | [diff] [blame] | 765 | retval = parser_send_video_msg(VIDEO_PRINT_FRAME, no_prepare); |
| 766 | |
Michael Sevakis | a5fc3f4 | 2008-01-03 17:14:28 +0000 | [diff] [blame] | 767 | stream_mgr_unlock(); |
| 768 | |
Michael Sevakis | a222f27 | 2007-12-29 19:46:35 +0000 | [diff] [blame] | 769 | return retval; |
| 770 | } |
| 771 | |
| 772 | /* Return the time playback should resume if interrupted */ |
| 773 | uint32_t stream_get_resume_time(void) |
| 774 | { |
| 775 | uint32_t resume_time; |
| 776 | |
| 777 | /* A stop request is async and replies before setting this - must lock */ |
| 778 | stream_mgr_lock(); |
| 779 | |
| 780 | resume_time = stream_mgr.resume_time; |
| 781 | |
| 782 | stream_mgr_unlock(); |
| 783 | |
| 784 | return resume_time; |
| 785 | } |
| 786 | |
Michael Sevakis | a5fc3f4 | 2008-01-03 17:14:28 +0000 | [diff] [blame] | 787 | uint32_t stream_get_seek_time(uint32_t *start) |
| 788 | { |
| 789 | uint32_t time; |
| 790 | |
| 791 | stream_mgr_lock(); |
| 792 | |
| 793 | if (stream_mgr.seeked) |
Michael Sevakis | 0e98d7e | 2008-01-07 14:58:23 +0000 | [diff] [blame] | 794 | { |
Michael Sevakis | a5fc3f4 | 2008-01-03 17:14:28 +0000 | [diff] [blame] | 795 | time = str_parser.last_seek_time; |
Michael Sevakis | 0e98d7e | 2008-01-07 14:58:23 +0000 | [diff] [blame] | 796 | } |
Michael Sevakis | a5fc3f4 | 2008-01-03 17:14:28 +0000 | [diff] [blame] | 797 | else |
Michael Sevakis | 0e98d7e | 2008-01-07 14:58:23 +0000 | [diff] [blame] | 798 | { |
Michael Sevakis | a5fc3f4 | 2008-01-03 17:14:28 +0000 | [diff] [blame] | 799 | time = TICKS_TO_TS(pcm_output_get_clock()); |
| 800 | |
Michael Sevakis | 0e98d7e | 2008-01-07 14:58:23 +0000 | [diff] [blame] | 801 | /* Clock can be start early so keep in range */ |
| 802 | if (time < str_parser.start_pts) |
| 803 | time = str_parser.start_pts; |
| 804 | } |
| 805 | |
Michael Sevakis | a5fc3f4 | 2008-01-03 17:14:28 +0000 | [diff] [blame] | 806 | if (start != NULL) |
| 807 | *start = str_parser.start_pts; |
| 808 | |
| 809 | stream_mgr_unlock(); |
| 810 | |
| 811 | return time; |
| 812 | } |
| 813 | |
Michael Sevakis | 3f4fd4d | 2008-01-27 21:45:15 +0000 | [diff] [blame] | 814 | /* Wait for a state transistion to complete */ |
| 815 | void stream_wait_status(void) |
| 816 | { |
| 817 | stream_mgr_lock(); |
| 818 | stream_mgr_unlock(); |
| 819 | } |
| 820 | |
Michael Sevakis | a222f27 | 2007-12-29 19:46:35 +0000 | [diff] [blame] | 821 | /* Returns the smallest file window that includes all active streams' |
| 822 | * windows */ |
| 823 | static bool stream_get_window_callback(struct list_item *item, |
| 824 | struct stream_window *sw) |
| 825 | { |
| 826 | struct stream *str = TYPE_FROM_MEMBER(struct stream, item, l); |
| 827 | off_t swl = str->hdr.win_left; |
| 828 | off_t swr = str->hdr.win_right; |
| 829 | |
| 830 | if (swl < sw->left) |
| 831 | sw->left = swl; |
| 832 | |
| 833 | if (swr > sw->right) |
| 834 | sw->right = swr; |
| 835 | |
| 836 | return true; |
| 837 | } |
| 838 | |
| 839 | bool stream_get_window(struct stream_window *sw) |
| 840 | { |
| 841 | if (sw == NULL) |
| 842 | return false; |
| 843 | |
| 844 | sw->left = LONG_MAX; |
| 845 | sw->right = LONG_MIN; |
| 846 | |
| 847 | actl_lock(); |
| 848 | list_enum_items(&stream_mgr.actl, |
| 849 | (list_enum_callback_t)stream_get_window_callback, |
| 850 | (intptr_t)sw); |
| 851 | actl_unlock(); |
| 852 | |
| 853 | return sw->left <= sw->right; |
| 854 | } |
| 855 | |
| 856 | /* Playback control thread */ |
| 857 | static void stream_mgr_thread(void) |
| 858 | { |
| 859 | struct queue_event ev; |
| 860 | |
| 861 | while (1) |
| 862 | { |
| 863 | rb->queue_wait(stream_mgr.q, &ev); |
| 864 | |
| 865 | switch (ev.id) |
| 866 | { |
| 867 | case STREAM_OPEN: |
| 868 | stream_on_open((const char *)ev.data); |
| 869 | break; |
| 870 | |
| 871 | case STREAM_CLOSE: |
| 872 | stream_on_close(); |
| 873 | break; |
| 874 | |
| 875 | case STREAM_PLAY: |
| 876 | stream_on_play(); |
| 877 | break; |
| 878 | |
| 879 | case STREAM_PAUSE: |
| 880 | if (ev.data) |
| 881 | stream_on_resume(); |
| 882 | else |
| 883 | stream_on_pause(); |
| 884 | break; |
| 885 | |
| 886 | case STREAM_STOP: |
| 887 | stream_on_stop(true); |
| 888 | break; |
| 889 | |
| 890 | case STREAM_SEEK: |
| 891 | stream_on_seek((struct stream_seek_data *)ev.data); |
| 892 | break; |
| 893 | |
| 894 | case STREAM_EV_COMPLETE: |
| 895 | stream_on_ev_complete((struct stream *)ev.data); |
| 896 | break; |
| 897 | |
| 898 | case STREAM_QUIT: |
| 899 | if (stream_mgr.status != STREAM_STOPPED) |
| 900 | stream_on_stop(false); |
| 901 | return; |
| 902 | } |
| 903 | } |
| 904 | } |
| 905 | |
| 906 | /* Stream command interface APIs */ |
| 907 | |
| 908 | /* Opens a new file */ |
| 909 | int stream_open(const char *filename) |
| 910 | { |
| 911 | if (stream_mgr.thread != NULL) |
| 912 | return stream_mgr_send_msg(STREAM_OPEN, (intptr_t)filename); |
| 913 | return STREAM_ERROR; |
| 914 | } |
| 915 | |
| 916 | /* Plays the current file starting at time 'start' */ |
| 917 | int stream_play(void) |
| 918 | { |
| 919 | if (stream_mgr.thread != NULL) |
| 920 | return stream_mgr_send_msg(STREAM_PLAY, 0); |
| 921 | return STREAM_ERROR; |
| 922 | } |
| 923 | |
| 924 | /* Pauses playback if playing */ |
| 925 | int stream_pause(void) |
| 926 | { |
| 927 | if (stream_mgr.thread != NULL) |
| 928 | return stream_mgr_send_msg(STREAM_PAUSE, false); |
| 929 | return STREAM_ERROR; |
| 930 | } |
| 931 | |
| 932 | /* Resumes playback if paused */ |
| 933 | int stream_resume(void) |
| 934 | { |
| 935 | if (stream_mgr.thread != NULL) |
| 936 | return stream_mgr_send_msg(STREAM_PAUSE, true); |
| 937 | return STREAM_ERROR; |
| 938 | } |
| 939 | |
| 940 | /* Stops playback if not stopped */ |
| 941 | int stream_stop(void) |
| 942 | { |
| 943 | if (stream_mgr.thread != NULL) |
| 944 | return stream_mgr_send_msg(STREAM_STOP, 0); |
| 945 | return STREAM_ERROR; |
| 946 | } |
| 947 | |
| 948 | /* Seeks playback time to/by the specified time */ |
| 949 | int stream_seek(uint32_t time, int whence) |
| 950 | { |
| 951 | int ret; |
| 952 | |
| 953 | if (stream_mgr.thread == NULL) |
| 954 | return STREAM_ERROR; |
| 955 | |
| 956 | stream_mgr_lock(); |
| 957 | |
| 958 | stream_mgr.parms.skd.time = time; |
| 959 | stream_mgr.parms.skd.whence = whence; |
| 960 | |
| 961 | ret = stream_mgr_send_msg(STREAM_SEEK, (intptr_t)&stream_mgr.parms.skd); |
| 962 | |
| 963 | stream_mgr_unlock(); |
| 964 | |
| 965 | return ret; |
| 966 | } |
| 967 | |
| 968 | /* Closes the current file */ |
| 969 | int stream_close(void) |
| 970 | { |
| 971 | if (stream_mgr.thread != NULL) |
| 972 | return stream_mgr_send_msg(STREAM_CLOSE, 0); |
| 973 | return STREAM_ERROR; |
| 974 | } |
| 975 | |
| 976 | /* Initializes the playback engine */ |
| 977 | int stream_init(void) |
| 978 | { |
| 979 | void *mem; |
| 980 | size_t memsize; |
| 981 | |
| 982 | stream_mgr.status = STREAM_STOPPED; |
| 983 | stream_mgr_init_state(); |
| 984 | list_initialize(&stream_mgr.actl); |
| 985 | |
| 986 | /* Initialize our window to the outside world first */ |
| 987 | rb->mutex_init(&stream_mgr.str_mtx); |
| 988 | rb->mutex_init(&stream_mgr.actl_mtx); |
| 989 | |
| 990 | stream_mgr.q = &stream_mgr_queue; |
| 991 | rb->queue_init(stream_mgr.q, false); |
Michael Sevakis | a222f27 | 2007-12-29 19:46:35 +0000 | [diff] [blame] | 992 | |
| 993 | /* sets audiosize and returns buffer pointer */ |
| 994 | mem = rb->plugin_get_audio_buffer(&memsize); |
| 995 | |
| 996 | /* Initialize non-allocator blocks first */ |
| 997 | #ifndef HAVE_LCD_COLOR |
Jens Arnold | c773eed | 2008-04-13 11:57:11 +0000 | [diff] [blame] | 998 | long greysize; |
Michael Sevakis | a222f27 | 2007-12-29 19:46:35 +0000 | [diff] [blame] | 999 | |
Jens Arnold | c773eed | 2008-04-13 11:57:11 +0000 | [diff] [blame] | 1000 | /* Greylib init handles all necessary cache alignment */ |
| 1001 | if (!grey_init(rb, mem, memsize, GREY_BUFFERED|GREY_ON_COP, |
| 1002 | LCD_WIDTH, LCD_HEIGHT, &greysize)) |
Michael Sevakis | a222f27 | 2007-12-29 19:46:35 +0000 | [diff] [blame] | 1003 | { |
Jens Arnold | feb5b15 | 2008-01-04 23:42:38 +0000 | [diff] [blame] | 1004 | rb->splash(HZ, "greylib init failed!"); |
Michael Sevakis | a222f27 | 2007-12-29 19:46:35 +0000 | [diff] [blame] | 1005 | return STREAM_ERROR; |
| 1006 | } |
Michael Sevakis | 75380fd | 2008-01-09 22:19:25 +0000 | [diff] [blame] | 1007 | |
Jens Arnold | c773eed | 2008-04-13 11:57:11 +0000 | [diff] [blame] | 1008 | mem += greysize; |
| 1009 | memsize -= greysize; |
| 1010 | |
Michael Sevakis | 75380fd | 2008-01-09 22:19:25 +0000 | [diff] [blame] | 1011 | grey_clear_display(); |
Michael Sevakis | a222f27 | 2007-12-29 19:46:35 +0000 | [diff] [blame] | 1012 | #endif /* !HAVE_LCD_COLOR */ |
| 1013 | |
| 1014 | stream_mgr.thread = rb->create_thread(stream_mgr_thread, |
| 1015 | stream_mgr_thread_stack, sizeof(stream_mgr_thread_stack), |
| 1016 | 0, "mpgstream_mgr" IF_PRIO(, PRIORITY_SYSTEM) IF_COP(, CPU)); |
| 1017 | |
Michael Sevakis | 27cf677 | 2008-03-25 02:34:12 +0000 | [diff] [blame] | 1018 | rb->queue_enable_queue_send(stream_mgr.q, &stream_mgr_queue_send, |
| 1019 | stream_mgr.thread); |
| 1020 | |
Michael Sevakis | a222f27 | 2007-12-29 19:46:35 +0000 | [diff] [blame] | 1021 | if (stream_mgr.thread == NULL) |
| 1022 | { |
| 1023 | rb->splash(HZ, "Could not create stream manager thread!"); |
| 1024 | return STREAM_ERROR; |
| 1025 | } |
| 1026 | |
| 1027 | /* Wait for thread to initialize */ |
| 1028 | stream_mgr_send_msg(STREAM_NULL, 0); |
| 1029 | |
| 1030 | /* Initialise our malloc buffer */ |
| 1031 | if (!mpeg_alloc_init(mem, memsize)) |
| 1032 | { |
| 1033 | rb->splash(HZ, "Out of memory in stream_init"); |
| 1034 | } |
| 1035 | /* These inits use the allocator */ |
| 1036 | else if (!pcm_output_init()) |
| 1037 | { |
| 1038 | rb->splash(HZ, "Could not initialize PCM!"); |
| 1039 | } |
| 1040 | else if (!audio_thread_init()) |
| 1041 | { |
| 1042 | rb->splash(HZ, "Cannot create audio thread!"); |
| 1043 | } |
| 1044 | else if (!video_thread_init()) |
| 1045 | { |
| 1046 | rb->splash(HZ, "Cannot create video thread!"); |
| 1047 | } |
| 1048 | /* Disk buffer takes max allotment of what's left so it must be last */ |
| 1049 | else if (!disk_buf_init()) |
| 1050 | { |
| 1051 | rb->splash(HZ, "Cannot create buffering thread!"); |
| 1052 | } |
| 1053 | else if (!parser_init()) |
| 1054 | { |
| 1055 | rb->splash(HZ, "Parser init failed!"); |
| 1056 | } |
| 1057 | else |
| 1058 | { |
| 1059 | return STREAM_OK; |
| 1060 | } |
| 1061 | |
| 1062 | return STREAM_ERROR; |
| 1063 | } |
| 1064 | |
| 1065 | /* Cleans everything up */ |
| 1066 | void stream_exit(void) |
| 1067 | { |
| 1068 | stream_close(); |
| 1069 | |
| 1070 | /* Stop the threads and wait for them to terminate */ |
| 1071 | video_thread_exit(); |
| 1072 | audio_thread_exit(); |
| 1073 | disk_buf_exit(); |
| 1074 | pcm_output_exit(); |
| 1075 | |
| 1076 | if (stream_mgr.thread != NULL) |
| 1077 | { |
| 1078 | stream_mgr_post_msg(STREAM_QUIT, 0); |
| 1079 | rb->thread_wait(stream_mgr.thread); |
| 1080 | stream_mgr.thread = NULL; |
| 1081 | } |
Michael Sevakis | 3b1f61a | 2007-12-29 22:12:10 +0000 | [diff] [blame] | 1082 | |
| 1083 | #ifndef HAVE_LCD_COLOR |
Michael Sevakis | 75380fd | 2008-01-09 22:19:25 +0000 | [diff] [blame] | 1084 | grey_release(); |
Michael Sevakis | 3b1f61a | 2007-12-29 22:12:10 +0000 | [diff] [blame] | 1085 | #endif |
Michael Sevakis | a222f27 | 2007-12-29 19:46:35 +0000 | [diff] [blame] | 1086 | } |