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 "debug.h" 35 36 /************************************************************* 37 * protected BMediaEventLooper 38 *************************************************************/ 39 40 /* virtual */ 41 BMediaEventLooper::~BMediaEventLooper() 42 { 43 CALLED(); 44 45 // don't call Quit(); here, except if the user was stupid 46 if (fControlThread != -1) { 47 printf("You MUST call BMediaEventLooper::Quit() in your destructor!\n"); 48 Quit(); 49 } 50 } 51 52 /* explicit */ 53 BMediaEventLooper::BMediaEventLooper(uint32 apiVersion) : 54 BMediaNode("called by BMediaEventLooper"), 55 fControlThread(-1), 56 fCurrentPriority(B_URGENT_PRIORITY), 57 fSetPriority(B_URGENT_PRIORITY), 58 fRunState(B_UNREGISTERED), 59 fEventLatency(0), 60 fSchedulingLatency(0), 61 fBufferDuration(0), 62 fOfflineTime(0), 63 fApiVersion(apiVersion) 64 { 65 CALLED(); 66 fEventQueue.SetCleanupHook(BMediaEventLooper::_CleanUpEntry, this); 67 fRealTimeQueue.SetCleanupHook(BMediaEventLooper::_CleanUpEntry, this); 68 } 69 70 /* virtual */ void 71 BMediaEventLooper::NodeRegistered() 72 { 73 CALLED(); 74 // Calling Run() should be done by the derived class, 75 // at least that's how it is documented by the BeBook. 76 // It appears that BeOS R5 called it here. Calling Run() 77 // twice doesn't hurt, and some nodes need it to be called here. 78 Run(); 79 } 80 81 82 /* virtual */ void 83 BMediaEventLooper::Start(bigtime_t performance_time) 84 { 85 CALLED(); 86 // This hook function is called when a node is started 87 // by a call to the BMediaRoster. The specified 88 // performanceTime, the time at which the node 89 // should start running, may be in the future. 90 fEventQueue.AddEvent(media_timed_event(performance_time, BTimedEventQueue::B_START)); 91 } 92 93 94 /* virtual */ void 95 BMediaEventLooper::Stop(bigtime_t performance_time, 96 bool immediate) 97 { 98 CALLED(); 99 // This hook function is called when a node is stopped 100 // by a call to the BMediaRoster. The specified performanceTime, 101 // the time at which the node should stop, may be in the future. 102 // If immediate is true, your node should ignore the performanceTime 103 // value and synchronously stop performance. When Stop() returns, 104 // you're promising not to write into any BBuffers you may have 105 // received from your downstream consumers, and you promise not 106 // to send any more buffers until Start() is called again. 107 108 if (immediate) { 109 // always be sure to add to the front of the queue so we can make sure it is 110 // handled before any buffers are sent! 111 performance_time = 0; 112 } 113 fEventQueue.AddEvent(media_timed_event(performance_time, BTimedEventQueue::B_STOP)); 114 } 115 116 117 /* virtual */ void 118 BMediaEventLooper::Seek(bigtime_t media_time, 119 bigtime_t performance_time) 120 { 121 CALLED(); 122 // This hook function is called when a node is asked to seek to 123 // the specified mediaTime by a call to the BMediaRoster. 124 // The specified performanceTime, the time at which the node 125 // should begin the seek operation, may be in the future. 126 fEventQueue.AddEvent(media_timed_event(performance_time, BTimedEventQueue::B_SEEK, NULL, 127 BTimedEventQueue::B_NO_CLEANUP, 0, media_time, NULL)); 128 } 129 130 131 /* virtual */ void 132 BMediaEventLooper::TimeWarp(bigtime_t at_real_time, 133 bigtime_t to_performance_time) 134 { 135 CALLED(); 136 // This hook function is called when the time source to which the 137 // node is slaved is repositioned (via a seek operation) such that 138 // there will be a sudden jump in the performance time progression 139 // as seen by the node. The to_performance_time argument indicates 140 // the new performance time; the change should occur at the real 141 // time specified by the at_real_time argument. 142 143 // place in the realtime queue 144 fRealTimeQueue.AddEvent(media_timed_event(at_real_time, BTimedEventQueue::B_WARP, 145 NULL, BTimedEventQueue::B_NO_CLEANUP, 0, to_performance_time, NULL)); 146 147 // BeBook: Your implementation of TimeWarp() should call through to BMediaNode::TimeWarp() 148 // BeBook: as well as all other inherited forms of TimeWarp() 149 // XXX should we do this here? 150 BMediaNode::TimeWarp(at_real_time, to_performance_time); 151 } 152 153 154 /* virtual */ status_t 155 BMediaEventLooper::AddTimer(bigtime_t at_performance_time, 156 int32 cookie) 157 { 158 CALLED(); 159 // XXX what do we need to do here? 160 return BMediaNode::AddTimer(at_performance_time,cookie); 161 } 162 163 164 /* virtual */ void 165 BMediaEventLooper::SetRunMode(run_mode mode) 166 { 167 CALLED(); 168 // The SetRunMode() hook function is called when someone requests that your node's run mode be changed. 169 170 // bump or reduce priority when switching from/to offline run mode 171 int32 priority; 172 priority = (mode == B_OFFLINE) ? min_c(B_NORMAL_PRIORITY, fSetPriority) : fSetPriority; 173 if (priority != fCurrentPriority) { 174 fCurrentPriority = priority; 175 if(fControlThread > 0) { 176 set_thread_priority(fControlThread, fCurrentPriority); 177 fSchedulingLatency = estimate_max_scheduling_latency(fControlThread); 178 printf("BMediaEventLooper: SchedulingLatency is %Ld\n", fSchedulingLatency); 179 } 180 } 181 182 BMediaNode::SetRunMode(mode); 183 } 184 185 186 /* virtual */ void 187 BMediaEventLooper::CleanUpEvent(const media_timed_event *event) 188 { 189 CALLED(); 190 // Implement this function to clean up after custom events you've created 191 // and added to your queue. It's called when a custom event is removed from 192 // the queue, to let you handle any special tidying-up that the event might require. 193 } 194 195 196 /* virtual */ bigtime_t 197 BMediaEventLooper::OfflineTime() 198 { 199 CALLED(); 200 return fOfflineTime; 201 } 202 203 204 /* virtual */ void 205 BMediaEventLooper::ControlLoop() 206 { 207 CALLED(); 208 209 bool is_realtime = false; 210 status_t err; 211 bigtime_t latency; 212 bigtime_t waituntil; 213 for (;;) { 214 // while there are no events or it is not time for the earliest event, 215 // process messages using WaitForMessages. Whenever this funtion times out, 216 // we need to handle the next event 217 for (;;) { 218 if (RunState() == B_QUITTING) 219 return; 220 // BMediaEventLooper compensates your performance time by adding the event latency 221 // (see SetEventLatency()) and the scheduling latency (or, for real-time events, 222 // only the scheduling latency). 223 224 latency = fEventLatency + fSchedulingLatency; 225 // printf("node %02d, latency %Ld\n", ID(), latency); 226 227 if (fEventQueue.HasEvents() && (TimeSource()->Now() - latency) >= fEventQueue.FirstEventTime()) { 228 // printf("node %02d waiting for %12Ld that has already happened, now %12Ld\n", ID(), fEventQueue.FirstEventTime(), system_time()); 229 is_realtime = false; 230 break; 231 } 232 if (fRealTimeQueue.HasEvents() && (TimeSource()->RealTimeFor(TimeSource()->Now(),fSchedulingLatency)) >= fRealTimeQueue.FirstEventTime()) { 233 latency = fSchedulingLatency; 234 is_realtime = true; 235 break; 236 } 237 waituntil = B_INFINITE_TIMEOUT; 238 if (fEventQueue.HasEvents()) { 239 waituntil = TimeSource()->RealTimeFor(fEventQueue.FirstEventTime(), latency); 240 // printf("node %02d waiting for %12Ld that will happen at %12Ld\n", ID(), fEventQueue.FirstEventTime(), waituntil); 241 is_realtime = false; 242 } 243 if (fRealTimeQueue.HasEvents()) { 244 bigtime_t temp; 245 temp = fRealTimeQueue.FirstEventTime() - fSchedulingLatency; 246 if (temp < waituntil) { 247 waituntil = temp; 248 is_realtime = true; 249 latency = fSchedulingLatency; 250 } 251 } 252 err = WaitForMessage(waituntil); 253 if (err == B_TIMED_OUT) 254 break; 255 } 256 /// we have timed out - so handle the next event 257 media_timed_event event; 258 if (is_realtime) 259 err = fRealTimeQueue.RemoveFirstEvent(&event); 260 else 261 err = fEventQueue.RemoveFirstEvent(&event); 262 263 // printf("node %02d handling %12Ld at %12Ld\n", ID(), event.event_time, system_time()); 264 265 if (err == B_OK) { 266 bigtime_t lateness; 267 if (is_realtime) 268 lateness = TimeSource()->RealTime() - event.event_time; 269 else 270 lateness = TimeSource()->RealTime() - TimeSource()->RealTimeFor(event.event_time, 0) + fEventLatency; 271 DispatchEvent(&event, lateness, is_realtime); 272 } 273 } 274 } 275 276 277 thread_id 278 BMediaEventLooper::ControlThread() 279 { 280 CALLED(); 281 return fControlThread; 282 } 283 284 /************************************************************* 285 * protected BMediaEventLooper 286 *************************************************************/ 287 288 289 BTimedEventQueue * 290 BMediaEventLooper::EventQueue() 291 { 292 CALLED(); 293 return &fEventQueue; 294 } 295 296 297 BTimedEventQueue * 298 BMediaEventLooper::RealTimeQueue() 299 { 300 CALLED(); 301 return &fRealTimeQueue; 302 } 303 304 305 int32 306 BMediaEventLooper::Priority() const 307 { 308 CALLED(); 309 return fCurrentPriority; 310 } 311 312 313 int32 314 BMediaEventLooper::RunState() const 315 { 316 PRINT(6, "CALLED BMediaEventLooper::RunState()\n"); 317 return fRunState; 318 } 319 320 321 bigtime_t 322 BMediaEventLooper::EventLatency() const 323 { 324 CALLED(); 325 return fEventLatency; 326 } 327 328 329 bigtime_t 330 BMediaEventLooper::BufferDuration() const 331 { 332 CALLED(); 333 return fBufferDuration; 334 } 335 336 337 bigtime_t 338 BMediaEventLooper::SchedulingLatency() const 339 { 340 CALLED(); 341 return fSchedulingLatency; 342 } 343 344 345 status_t 346 BMediaEventLooper::SetPriority(int32 priority) 347 { 348 CALLED(); 349 350 // clamp to a valid value 351 if (priority < 5) 352 priority = 5; 353 354 if (priority > 120) 355 priority = 120; 356 357 fSetPriority = priority; 358 fCurrentPriority = (RunMode() == B_OFFLINE) ? min_c(B_NORMAL_PRIORITY, fSetPriority) : fSetPriority; 359 360 if(fControlThread > 0) { 361 set_thread_priority(fControlThread, fCurrentPriority); 362 fSchedulingLatency = estimate_max_scheduling_latency(fControlThread); 363 printf("BMediaEventLooper: SchedulingLatency is %Ld\n", fSchedulingLatency); 364 } 365 366 return B_OK; 367 } 368 369 370 void 371 BMediaEventLooper::SetRunState(run_state state) 372 { 373 CALLED(); 374 375 // don't allow run state changes while quitting, 376 // also needed for correct terminating of the ControlLoop() 377 if (fRunState == B_QUITTING && state != B_TERMINATED) 378 return; 379 380 fRunState = state; 381 } 382 383 384 void 385 BMediaEventLooper::SetEventLatency(bigtime_t latency) 386 { 387 CALLED(); 388 // clamp to a valid value 389 if (latency < 0) 390 latency = 0; 391 392 fEventLatency = latency; 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 status_t err; 439 440 if (fRunState == B_TERMINATED) 441 return; 442 443 SetRunState(B_QUITTING); 444 445 close_port(ControlPort()); 446 if (fControlThread != -1) 447 wait_for_thread(fControlThread, &err); 448 fControlThread = -1; 449 450 SetRunState(B_TERMINATED); 451 } 452 453 454 void 455 BMediaEventLooper::DispatchEvent(const media_timed_event *event, 456 bigtime_t lateness, 457 bool realTimeEvent) 458 { 459 PRINT(6, "CALLED BMediaEventLooper::DispatchEvent()\n"); 460 461 HandleEvent(event, lateness, realTimeEvent); 462 463 switch (event->type) { 464 case BTimedEventQueue::B_START: 465 SetRunState(B_STARTED); 466 break; 467 468 case BTimedEventQueue::B_STOP: 469 SetRunState(B_STOPPED); 470 break; 471 472 case BTimedEventQueue::B_SEEK: 473 /* nothing */ 474 break; 475 476 case BTimedEventQueue::B_WARP: 477 /* nothing */ 478 break; 479 480 default: 481 break; 482 } 483 484 _DispatchCleanUp(event); 485 } 486 487 /************************************************************* 488 * private BMediaEventLooper 489 *************************************************************/ 490 491 492 /* static */ int32 493 BMediaEventLooper::_ControlThreadStart(void *arg) 494 { 495 CALLED(); 496 ((BMediaEventLooper *)arg)->SetRunState(B_STOPPED); 497 ((BMediaEventLooper *)arg)->ControlLoop(); 498 ((BMediaEventLooper *)arg)->SetRunState(B_QUITTING); 499 return 0; 500 } 501 502 503 /* static */ void 504 BMediaEventLooper::_CleanUpEntry(const media_timed_event *event, 505 void *context) 506 { 507 PRINT(6, "CALLED BMediaEventLooper::_CleanUpEntry()\n"); 508 ((BMediaEventLooper *)context)->_DispatchCleanUp(event); 509 } 510 511 512 void 513 BMediaEventLooper::_DispatchCleanUp(const media_timed_event *event) 514 { 515 PRINT(6, "CALLED BMediaEventLooper::_DispatchCleanUp()\n"); 516 517 // this function to clean up after custom events you've created 518 if (event->cleanup >= BTimedEventQueue::B_USER_CLEANUP) 519 CleanUpEvent(event); 520 } 521 522 /* 523 // unimplemented 524 BMediaEventLooper::BMediaEventLooper(const BMediaEventLooper &) 525 BMediaEventLooper &BMediaEventLooper::operator=(const BMediaEventLooper &) 526 */ 527 528 /************************************************************* 529 * protected BMediaEventLooper 530 *************************************************************/ 531 532 533 status_t 534 BMediaEventLooper::DeleteHook(BMediaNode *node) 535 { 536 CALLED(); 537 // this is the DeleteHook that gets called by the media server 538 // before the media node is deleted 539 Quit(); 540 return BMediaNode::DeleteHook(node); 541 } 542 543 /************************************************************* 544 * private BMediaEventLooper 545 *************************************************************/ 546 547 status_t BMediaEventLooper::_Reserved_BMediaEventLooper_0(int32 arg,...) { return B_ERROR; } 548 status_t BMediaEventLooper::_Reserved_BMediaEventLooper_1(int32 arg,...) { return B_ERROR; } 549 status_t BMediaEventLooper::_Reserved_BMediaEventLooper_2(int32 arg,...) { return B_ERROR; } 550 status_t BMediaEventLooper::_Reserved_BMediaEventLooper_3(int32 arg,...) { return B_ERROR; } 551 status_t BMediaEventLooper::_Reserved_BMediaEventLooper_4(int32 arg,...) { return B_ERROR; } 552 status_t BMediaEventLooper::_Reserved_BMediaEventLooper_5(int32 arg,...) { return B_ERROR; } 553 status_t BMediaEventLooper::_Reserved_BMediaEventLooper_6(int32 arg,...) { return B_ERROR; } 554 status_t BMediaEventLooper::_Reserved_BMediaEventLooper_7(int32 arg,...) { return B_ERROR; } 555 status_t BMediaEventLooper::_Reserved_BMediaEventLooper_8(int32 arg,...) { return B_ERROR; } 556 status_t BMediaEventLooper::_Reserved_BMediaEventLooper_9(int32 arg,...) { return B_ERROR; } 557 status_t BMediaEventLooper::_Reserved_BMediaEventLooper_10(int32 arg,...) { return B_ERROR; } 558 status_t BMediaEventLooper::_Reserved_BMediaEventLooper_11(int32 arg,...) { return B_ERROR; } 559 status_t BMediaEventLooper::_Reserved_BMediaEventLooper_12(int32 arg,...) { return B_ERROR; } 560 status_t BMediaEventLooper::_Reserved_BMediaEventLooper_13(int32 arg,...) { return B_ERROR; } 561 status_t BMediaEventLooper::_Reserved_BMediaEventLooper_14(int32 arg,...) { return B_ERROR; } 562 status_t BMediaEventLooper::_Reserved_BMediaEventLooper_15(int32 arg,...) { return B_ERROR; } 563 status_t BMediaEventLooper::_Reserved_BMediaEventLooper_16(int32 arg,...) { return B_ERROR; } 564 status_t BMediaEventLooper::_Reserved_BMediaEventLooper_17(int32 arg,...) { return B_ERROR; } 565 status_t BMediaEventLooper::_Reserved_BMediaEventLooper_18(int32 arg,...) { return B_ERROR; } 566 status_t BMediaEventLooper::_Reserved_BMediaEventLooper_19(int32 arg,...) { return B_ERROR; } 567 status_t BMediaEventLooper::_Reserved_BMediaEventLooper_20(int32 arg,...) { return B_ERROR; } 568 status_t BMediaEventLooper::_Reserved_BMediaEventLooper_21(int32 arg,...) { return B_ERROR; } 569 status_t BMediaEventLooper::_Reserved_BMediaEventLooper_22(int32 arg,...) { return B_ERROR; } 570 status_t BMediaEventLooper::_Reserved_BMediaEventLooper_23(int32 arg,...) { return B_ERROR; } 571 572