1 /* 2 * Copyright (c) 2002, 2003 Marcus Overhagen <Marcus@Overhagen.de> 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining 5 * a copy of this software and associated documentation files or portions 6 * thereof (the "Software"), to deal in the Software without restriction, 7 * including without limitation the rights to use, copy, modify, merge, 8 * publish, distribute, sublicense, and/or sell copies of the Software, 9 * and to permit persons to whom the Software is furnished to do so, subject 10 * to the following conditions: 11 * 12 * * Redistributions of source code must retain the above copyright notice, 13 * this list of conditions and the following disclaimer. 14 * 15 * * Redistributions in binary form must reproduce the above copyright notice 16 * in the binary, as well as this list of conditions and the following 17 * disclaimer in the documentation and/or other materials provided with 18 * the distribution. 19 * 20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 21 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 23 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 26 * THE SOFTWARE. 27 * 28 */ 29 30 #include <MediaEventLooper.h> 31 #include <TimeSource.h> 32 #include <scheduler.h> 33 #include <Buffer.h> 34 #include <ServerInterface.h> 35 #include "debug.h" 36 37 /************************************************************* 38 * protected BMediaEventLooper 39 *************************************************************/ 40 41 /* virtual */ 42 BMediaEventLooper::~BMediaEventLooper() 43 { 44 CALLED(); 45 46 // don't call Quit(); here, except if the user was stupid 47 if (fControlThread != -1) { 48 printf("You MUST call BMediaEventLooper::Quit() in your destructor!\n"); 49 Quit(); 50 } 51 } 52 53 /* explicit */ 54 BMediaEventLooper::BMediaEventLooper(uint32 apiVersion) : 55 BMediaNode("called by BMediaEventLooper"), 56 fControlThread(-1), 57 fCurrentPriority(B_URGENT_PRIORITY), 58 fSetPriority(B_URGENT_PRIORITY), 59 fRunState(B_UNREGISTERED), 60 fEventLatency(0), 61 fSchedulingLatency(0), 62 fBufferDuration(0), 63 fOfflineTime(0), 64 fApiVersion(apiVersion) 65 { 66 CALLED(); 67 fEventQueue.SetCleanupHook(BMediaEventLooper::_CleanUpEntry, this); 68 fRealTimeQueue.SetCleanupHook(BMediaEventLooper::_CleanUpEntry, this); 69 } 70 71 /* virtual */ void 72 BMediaEventLooper::NodeRegistered() 73 { 74 CALLED(); 75 // Calling Run() should be done by the derived class, 76 // at least that's how it is documented by the BeBook. 77 // It appears that BeOS R5 called it here. Calling Run() 78 // twice doesn't hurt, and some nodes need it to be called here. 79 Run(); 80 } 81 82 83 /* virtual */ void 84 BMediaEventLooper::Start(bigtime_t performance_time) 85 { 86 CALLED(); 87 // This hook function is called when a node is started 88 // by a call to the BMediaRoster. The specified 89 // performanceTime, the time at which the node 90 // should start running, may be in the future. 91 fEventQueue.AddEvent(media_timed_event(performance_time, BTimedEventQueue::B_START)); 92 } 93 94 95 /* virtual */ void 96 BMediaEventLooper::Stop(bigtime_t performance_time, 97 bool immediate) 98 { 99 CALLED(); 100 // This hook function is called when a node is stopped 101 // by a call to the BMediaRoster. The specified performanceTime, 102 // the time at which the node should stop, may be in the future. 103 // If immediate is true, your node should ignore the performanceTime 104 // value and synchronously stop performance. When Stop() returns, 105 // you're promising not to write into any BBuffers you may have 106 // received from your downstream consumers, and you promise not 107 // to send any more buffers until Start() is called again. 108 109 if (immediate) { 110 // always be sure to add to the front of the queue so we can make sure it is 111 // handled before any buffers are sent! 112 performance_time = 0; 113 } 114 fEventQueue.AddEvent(media_timed_event(performance_time, BTimedEventQueue::B_STOP)); 115 } 116 117 118 /* virtual */ void 119 BMediaEventLooper::Seek(bigtime_t media_time, 120 bigtime_t performance_time) 121 { 122 CALLED(); 123 // This hook function is called when a node is asked to seek to 124 // the specified mediaTime by a call to the BMediaRoster. 125 // The specified performanceTime, the time at which the node 126 // should begin the seek operation, may be in the future. 127 fEventQueue.AddEvent(media_timed_event(performance_time, BTimedEventQueue::B_SEEK, NULL, 128 BTimedEventQueue::B_NO_CLEANUP, 0, media_time, NULL)); 129 } 130 131 132 /* virtual */ void 133 BMediaEventLooper::TimeWarp(bigtime_t at_real_time, 134 bigtime_t to_performance_time) 135 { 136 CALLED(); 137 // This hook function is called when the time source to which the 138 // node is slaved is repositioned (via a seek operation) such that 139 // there will be a sudden jump in the performance time progression 140 // as seen by the node. The to_performance_time argument indicates 141 // the new performance time; the change should occur at the real 142 // time specified by the at_real_time argument. 143 144 // place in the realtime queue 145 fRealTimeQueue.AddEvent(media_timed_event(at_real_time, BTimedEventQueue::B_WARP, 146 NULL, BTimedEventQueue::B_NO_CLEANUP, 0, to_performance_time, NULL)); 147 148 // BeBook: Your implementation of TimeWarp() should call through to BMediaNode::TimeWarp() 149 // BeBook: as well as all other inherited forms of TimeWarp() 150 // XXX should we do this here? 151 BMediaNode::TimeWarp(at_real_time, to_performance_time); 152 } 153 154 155 /* virtual */ status_t 156 BMediaEventLooper::AddTimer(bigtime_t at_performance_time, 157 int32 cookie) 158 { 159 CALLED(); 160 // XXX what do we need to do here? 161 return BMediaNode::AddTimer(at_performance_time,cookie); 162 } 163 164 165 /* virtual */ void 166 BMediaEventLooper::SetRunMode(run_mode mode) 167 { 168 CALLED(); 169 // The SetRunMode() hook function is called when someone requests that your node's run mode be changed. 170 171 // bump or reduce priority when switching from/to offline run mode 172 int32 priority; 173 priority = (mode == B_OFFLINE) ? min_c(B_NORMAL_PRIORITY, fSetPriority) : fSetPriority; 174 if (priority != fCurrentPriority) { 175 fCurrentPriority = priority; 176 if (fControlThread > 0) { 177 set_thread_priority(fControlThread, fCurrentPriority); 178 fSchedulingLatency = estimate_max_scheduling_latency(fControlThread); 179 printf("BMediaEventLooper: SchedulingLatency is %Ld\n", fSchedulingLatency); 180 } 181 } 182 183 BMediaNode::SetRunMode(mode); 184 } 185 186 187 /* virtual */ void 188 BMediaEventLooper::CleanUpEvent(const media_timed_event *event) 189 { 190 CALLED(); 191 // Implement this function to clean up after custom events you've created 192 // and added to your queue. It's called when a custom event is removed from 193 // the queue, to let you handle any special tidying-up that the event might require. 194 } 195 196 197 /* virtual */ bigtime_t 198 BMediaEventLooper::OfflineTime() 199 { 200 CALLED(); 201 return fOfflineTime; 202 } 203 204 205 /* virtual */ void 206 BMediaEventLooper::ControlLoop() 207 { 208 CALLED(); 209 210 bool is_realtime = false; 211 status_t err; 212 bigtime_t latency; 213 bigtime_t waituntil; 214 bigtime_t lateness; 215 for (;;) { 216 // while there are no events or it is not time for the earliest event, 217 // process messages using WaitForMessages. Whenever this funtion times out, 218 // we need to handle the next event 219 for (;;) { 220 if (RunState() == B_QUITTING) 221 return; 222 // BMediaEventLooper compensates your performance time by adding the event latency 223 // (see SetEventLatency()) and the scheduling latency (or, for real-time events, 224 // only the scheduling latency). 225 226 latency = fEventLatency + fSchedulingLatency; 227 waituntil = B_INFINITE_TIMEOUT; 228 if (fEventQueue.HasEvents()) { 229 const media_timed_event *firstEvent = fEventQueue.FirstEvent(); 230 waituntil = TimeSource()->RealTimeFor(firstEvent->event_time, latency); 231 is_realtime = false; 232 lateness = firstEvent->queued_time - waituntil; 233 if (lateness > 0) { 234 // if (lateness > 1000) 235 // printf("node %02ld handling %12Ld at %12Ld -- %Ld late, queued at %Ld now %12Ld \n", 236 // ID(), fEventQueue.FirstEventTime(), TimeSource()->Now(), lateness, 237 // firstEvent->queued_time, TimeSource()->RealTime()); 238 is_realtime = false; 239 break; 240 } 241 // printf("node %02ld waiting for %12Ld that will happen at %12Ld\n", ID(), fEventQueue.FirstEventTime(), waituntil); 242 } 243 if (fRealTimeQueue.HasEvents()) { 244 const media_timed_event *firstEvent = fRealTimeQueue.FirstEvent(); 245 bigtime_t temp; 246 temp = firstEvent->event_time - fSchedulingLatency; 247 lateness = firstEvent->queued_time - temp; 248 if (lateness > 0) { 249 is_realtime = true; 250 break; 251 } 252 if (temp < waituntil) { 253 waituntil = temp; 254 is_realtime = true; 255 } 256 } 257 lateness = 0; // remove any extraneous value if we get this far 258 err = WaitForMessage(waituntil); 259 if (err == B_TIMED_OUT) 260 break; 261 } 262 /// we have timed out - so handle the next event 263 media_timed_event event; 264 if (is_realtime) 265 err = fRealTimeQueue.RemoveFirstEvent(&event); 266 else 267 err = fEventQueue.RemoveFirstEvent(&event); 268 269 // printf("node %02ld handling %12Ld at %12Ld\n", ID(), event.event_time, TimeSource()->Now()); 270 271 if (err == B_OK) DispatchEvent(&event, lateness, is_realtime); 272 } 273 } 274 275 276 thread_id 277 BMediaEventLooper::ControlThread() 278 { 279 CALLED(); 280 return fControlThread; 281 } 282 283 /************************************************************* 284 * protected BMediaEventLooper 285 *************************************************************/ 286 287 288 BTimedEventQueue * 289 BMediaEventLooper::EventQueue() 290 { 291 CALLED(); 292 return &fEventQueue; 293 } 294 295 296 BTimedEventQueue * 297 BMediaEventLooper::RealTimeQueue() 298 { 299 CALLED(); 300 return &fRealTimeQueue; 301 } 302 303 304 int32 305 BMediaEventLooper::Priority() const 306 { 307 CALLED(); 308 return fCurrentPriority; 309 } 310 311 312 int32 313 BMediaEventLooper::RunState() const 314 { 315 PRINT(6, "CALLED BMediaEventLooper::RunState()\n"); 316 return fRunState; 317 } 318 319 320 bigtime_t 321 BMediaEventLooper::EventLatency() const 322 { 323 CALLED(); 324 return fEventLatency; 325 } 326 327 328 bigtime_t 329 BMediaEventLooper::BufferDuration() const 330 { 331 CALLED(); 332 return fBufferDuration; 333 } 334 335 336 bigtime_t 337 BMediaEventLooper::SchedulingLatency() const 338 { 339 CALLED(); 340 return fSchedulingLatency; 341 } 342 343 344 status_t 345 BMediaEventLooper::SetPriority(int32 priority) 346 { 347 CALLED(); 348 349 // clamp to a valid value 350 if (priority < 5) 351 priority = 5; 352 353 if (priority > 120) 354 priority = 120; 355 356 fSetPriority = priority; 357 fCurrentPriority = (RunMode() == B_OFFLINE) ? min_c(B_NORMAL_PRIORITY, fSetPriority) : fSetPriority; 358 359 if (fControlThread > 0) { 360 set_thread_priority(fControlThread, fCurrentPriority); 361 fSchedulingLatency = estimate_max_scheduling_latency(fControlThread); 362 printf("BMediaEventLooper: SchedulingLatency is %Ld\n", fSchedulingLatency); 363 } 364 365 return B_OK; 366 } 367 368 369 void 370 BMediaEventLooper::SetRunState(run_state state) 371 { 372 CALLED(); 373 374 // don't allow run state changes while quitting, 375 // also needed for correct terminating of the ControlLoop() 376 if (fRunState == B_QUITTING && state != B_TERMINATED) 377 return; 378 379 fRunState = state; 380 } 381 382 383 void 384 BMediaEventLooper::SetEventLatency(bigtime_t latency) 385 { 386 CALLED(); 387 // clamp to a valid value 388 if (latency < 0) 389 latency = 0; 390 391 fEventLatency = latency; 392 write_port_etc(ControlPort(), GENERAL_PURPOSE_WAKEUP, 0, 0, B_TIMEOUT, 0); 393 } 394 395 396 void 397 BMediaEventLooper::SetBufferDuration(bigtime_t duration) 398 { 399 CALLED(); 400 fBufferDuration = duration; 401 } 402 403 404 void 405 BMediaEventLooper::SetOfflineTime(bigtime_t offTime) 406 { 407 CALLED(); 408 fOfflineTime = offTime; 409 } 410 411 412 void 413 BMediaEventLooper::Run() 414 { 415 CALLED(); 416 417 if (fControlThread != -1) 418 return; // thread already running 419 420 // until now, the run state is B_UNREGISTERED, but we need to start in B_STOPPED state. 421 SetRunState(B_STOPPED); 422 423 char threadName[32]; 424 sprintf(threadName, "%.20s control", Name()); 425 fControlThread = spawn_thread(_ControlThreadStart, threadName, fCurrentPriority, this); 426 resume_thread(fControlThread); 427 428 // get latency information 429 fSchedulingLatency = estimate_max_scheduling_latency(fControlThread); 430 printf("BMediaEventLooper: SchedulingLatency is %Ld\n", fSchedulingLatency); 431 } 432 433 434 void 435 BMediaEventLooper::Quit() 436 { 437 CALLED(); 438 439 if (fRunState == B_TERMINATED) 440 return; 441 442 SetRunState(B_QUITTING); 443 close_port(ControlPort()); 444 if (fControlThread != -1) { 445 status_t err; 446 wait_for_thread(fControlThread, &err); 447 fControlThread = -1; 448 } 449 SetRunState(B_TERMINATED); 450 } 451 452 453 void 454 BMediaEventLooper::DispatchEvent(const media_timed_event *event, 455 bigtime_t lateness, 456 bool realTimeEvent) 457 { 458 PRINT(6, "CALLED BMediaEventLooper::DispatchEvent()\n"); 459 460 HandleEvent(event, lateness, realTimeEvent); 461 462 switch (event->type) { 463 case BTimedEventQueue::B_START: 464 SetRunState(B_STARTED); 465 break; 466 467 case BTimedEventQueue::B_STOP: 468 SetRunState(B_STOPPED); 469 break; 470 471 case BTimedEventQueue::B_SEEK: 472 /* nothing */ 473 break; 474 475 case BTimedEventQueue::B_WARP: 476 /* nothing */ 477 break; 478 479 default: 480 break; 481 } 482 483 _DispatchCleanUp(event); 484 } 485 486 /************************************************************* 487 * private BMediaEventLooper 488 *************************************************************/ 489 490 491 /* static */ int32 492 BMediaEventLooper::_ControlThreadStart(void *arg) 493 { 494 CALLED(); 495 ((BMediaEventLooper *)arg)->SetRunState(B_STOPPED); 496 ((BMediaEventLooper *)arg)->ControlLoop(); 497 ((BMediaEventLooper *)arg)->SetRunState(B_QUITTING); 498 return 0; 499 } 500 501 502 /* static */ void 503 BMediaEventLooper::_CleanUpEntry(const media_timed_event *event, 504 void *context) 505 { 506 PRINT(6, "CALLED BMediaEventLooper::_CleanUpEntry()\n"); 507 ((BMediaEventLooper *)context)->_DispatchCleanUp(event); 508 } 509 510 511 void 512 BMediaEventLooper::_DispatchCleanUp(const media_timed_event *event) 513 { 514 PRINT(6, "CALLED BMediaEventLooper::_DispatchCleanUp()\n"); 515 516 // this function to clean up after custom events you've created 517 if (event->cleanup >= BTimedEventQueue::B_USER_CLEANUP) 518 CleanUpEvent(event); 519 } 520 521 /* 522 // unimplemented 523 BMediaEventLooper::BMediaEventLooper(const BMediaEventLooper &) 524 BMediaEventLooper &BMediaEventLooper::operator=(const BMediaEventLooper &) 525 */ 526 527 /************************************************************* 528 * protected BMediaEventLooper 529 *************************************************************/ 530 531 532 status_t 533 BMediaEventLooper::DeleteHook(BMediaNode *node) 534 { 535 CALLED(); 536 // this is the DeleteHook that gets called by the media server 537 // before the media node is deleted 538 Quit(); 539 return BMediaNode::DeleteHook(node); 540 } 541 542 /************************************************************* 543 * private BMediaEventLooper 544 *************************************************************/ 545 546 status_t BMediaEventLooper::_Reserved_BMediaEventLooper_0(int32 arg,...) { return B_ERROR; } 547 status_t BMediaEventLooper::_Reserved_BMediaEventLooper_1(int32 arg,...) { return B_ERROR; } 548 status_t BMediaEventLooper::_Reserved_BMediaEventLooper_2(int32 arg,...) { return B_ERROR; } 549 status_t BMediaEventLooper::_Reserved_BMediaEventLooper_3(int32 arg,...) { return B_ERROR; } 550 status_t BMediaEventLooper::_Reserved_BMediaEventLooper_4(int32 arg,...) { return B_ERROR; } 551 status_t BMediaEventLooper::_Reserved_BMediaEventLooper_5(int32 arg,...) { return B_ERROR; } 552 status_t BMediaEventLooper::_Reserved_BMediaEventLooper_6(int32 arg,...) { return B_ERROR; } 553 status_t BMediaEventLooper::_Reserved_BMediaEventLooper_7(int32 arg,...) { return B_ERROR; } 554 status_t BMediaEventLooper::_Reserved_BMediaEventLooper_8(int32 arg,...) { return B_ERROR; } 555 status_t BMediaEventLooper::_Reserved_BMediaEventLooper_9(int32 arg,...) { return B_ERROR; } 556 status_t BMediaEventLooper::_Reserved_BMediaEventLooper_10(int32 arg,...) { return B_ERROR; } 557 status_t BMediaEventLooper::_Reserved_BMediaEventLooper_11(int32 arg,...) { return B_ERROR; } 558 status_t BMediaEventLooper::_Reserved_BMediaEventLooper_12(int32 arg,...) { return B_ERROR; } 559 status_t BMediaEventLooper::_Reserved_BMediaEventLooper_13(int32 arg,...) { return B_ERROR; } 560 status_t BMediaEventLooper::_Reserved_BMediaEventLooper_14(int32 arg,...) { return B_ERROR; } 561 status_t BMediaEventLooper::_Reserved_BMediaEventLooper_15(int32 arg,...) { return B_ERROR; } 562 status_t BMediaEventLooper::_Reserved_BMediaEventLooper_16(int32 arg,...) { return B_ERROR; } 563 status_t BMediaEventLooper::_Reserved_BMediaEventLooper_17(int32 arg,...) { return B_ERROR; } 564 status_t BMediaEventLooper::_Reserved_BMediaEventLooper_18(int32 arg,...) { return B_ERROR; } 565 status_t BMediaEventLooper::_Reserved_BMediaEventLooper_19(int32 arg,...) { return B_ERROR; } 566 status_t BMediaEventLooper::_Reserved_BMediaEventLooper_20(int32 arg,...) { return B_ERROR; } 567 status_t BMediaEventLooper::_Reserved_BMediaEventLooper_21(int32 arg,...) { return B_ERROR; } 568 status_t BMediaEventLooper::_Reserved_BMediaEventLooper_22(int32 arg,...) { return B_ERROR; } 569 status_t BMediaEventLooper::_Reserved_BMediaEventLooper_23(int32 arg,...) { return B_ERROR; } 570 571