1 #include <fcntl.h> 2 #include <malloc.h> 3 #include <math.h> 4 #include <stdio.h> 5 #include <string.h> 6 #include <sys/uio.h> 7 #include <unistd.h> 8 9 #include <media/Buffer.h> 10 #include <media/BufferGroup.h> 11 #include <media/ParameterWeb.h> 12 #include <media/TimeSource.h> 13 14 #include <support/Autolock.h> 15 #include <support/Debug.h> 16 17 //XXX: change interface 18 #include <interface/Bitmap.h> 19 20 #include "CamDevice.h" 21 22 #define TOUCH(x) ((void)(x)) 23 24 #define PRINTF(a,b) \ 25 do { \ 26 if (a < 2) { \ 27 printf("VideoProducer::"); \ 28 printf b; \ 29 } \ 30 } while (0) 31 32 #include "Producer.h" 33 34 #define FIELD_RATE 30.f 35 36 int32 VideoProducer::fInstances = 0; 37 38 VideoProducer::VideoProducer( 39 BMediaAddOn *addon, CamDevice *dev, const char *name, int32 internal_id) 40 : BMediaNode(name), 41 BMediaEventLooper(), 42 BBufferProducer(B_MEDIA_RAW_VIDEO), 43 BControllable() 44 { 45 // status_t err; 46 47 fInitStatus = B_NO_INIT; 48 49 /* Only allow one instance of the node to exist at any time */ 50 if (atomic_add(&fInstances, 1) != 0) 51 return; 52 53 fInternalID = internal_id; 54 fAddOn = addon; 55 fCamDevice = dev; 56 57 fBufferGroup = NULL; 58 59 fThread = -1; 60 fFrameSync = -1; 61 fProcessingLatency = 0LL; 62 63 fRunning = false; 64 fConnected = false; 65 fEnabled = false; 66 67 fOutput.destination = media_destination::null; 68 69 AddNodeKind(B_PHYSICAL_INPUT); 70 71 fInitStatus = B_OK; 72 return; 73 } 74 75 VideoProducer::~VideoProducer() 76 { 77 if (fInitStatus == B_OK) { 78 /* Clean up after ourselves, in case the application didn't make us 79 * do so. */ 80 if (fConnected) 81 Disconnect(fOutput.source, fOutput.destination); 82 if (fRunning) 83 HandleStop(); 84 } 85 86 atomic_add(&fInstances, -1); 87 } 88 89 /* BMediaNode */ 90 91 port_id 92 VideoProducer::ControlPort() const 93 { 94 return BMediaNode::ControlPort(); 95 } 96 97 BMediaAddOn * 98 VideoProducer::AddOn(int32 *internal_id) const 99 { 100 if (internal_id) 101 *internal_id = fInternalID; 102 return fAddOn; 103 } 104 105 status_t 106 VideoProducer::HandleMessage(int32 /*message*/, const void* /*data*/, size_t /*size*/) 107 { 108 return B_ERROR; 109 } 110 111 void 112 VideoProducer::Preroll() 113 { 114 /* This hook may be called before the node is started to give the hardware 115 * a chance to start. */ 116 } 117 118 void 119 VideoProducer::SetTimeSource(BTimeSource* /*time_source*/) 120 { 121 /* Tell frame generation thread to recalculate delay value */ 122 release_sem(fFrameSync); 123 } 124 125 status_t 126 VideoProducer::RequestCompleted(const media_request_info &info) 127 { 128 return BMediaNode::RequestCompleted(info); 129 } 130 131 /* BMediaEventLooper */ 132 133 void 134 VideoProducer::NodeRegistered() 135 { 136 if (fInitStatus != B_OK) { 137 ReportError(B_NODE_IN_DISTRESS); 138 return; 139 } 140 141 /* Set up the parameter web */ 142 BParameterWeb *web = new BParameterWeb(); 143 BParameterGroup *main = web->MakeGroup(Name()); 144 BDiscreteParameter *state = main->MakeDiscreteParameter( 145 P_COLOR, B_MEDIA_RAW_VIDEO, "Color", "Color"); 146 state->AddItem(B_HOST_TO_LENDIAN_INT32(0x00ff0000), "Red"); 147 state->AddItem(B_HOST_TO_LENDIAN_INT32(0x0000ff00), "Green"); 148 state->AddItem(B_HOST_TO_LENDIAN_INT32(0x000000ff), "Blue"); 149 150 fColor = B_HOST_TO_LENDIAN_INT32(0x00ff0000); 151 fLastColorChange = system_time(); 152 153 /* After this call, the BControllable owns the BParameterWeb object and 154 * will delete it for you */ 155 SetParameterWeb(web); 156 157 fOutput.node = Node(); 158 fOutput.source.port = ControlPort(); 159 fOutput.source.id = 0; 160 fOutput.destination = media_destination::null; 161 strcpy(fOutput.name, Name()); 162 163 /* Tailor these for the output of your device */ 164 fOutput.format.type = B_MEDIA_RAW_VIDEO; 165 fOutput.format.u.raw_video = media_raw_video_format::wildcard; 166 fOutput.format.u.raw_video.interlace = 1; 167 fOutput.format.u.raw_video.display.format = B_RGB32; 168 fOutput.format.u.raw_video.field_rate = 29.97f; // XXX: mmu 169 170 /* Start the BMediaEventLooper control loop running */ 171 Run(); 172 } 173 174 void 175 VideoProducer::Start(bigtime_t performance_time) 176 { 177 BMediaEventLooper::Start(performance_time); 178 } 179 180 void 181 VideoProducer::Stop(bigtime_t performance_time, bool immediate) 182 { 183 BMediaEventLooper::Stop(performance_time, immediate); 184 } 185 186 void 187 VideoProducer::Seek(bigtime_t media_time, bigtime_t performance_time) 188 { 189 BMediaEventLooper::Seek(media_time, performance_time); 190 } 191 192 void 193 VideoProducer::TimeWarp(bigtime_t at_real_time, bigtime_t to_performance_time) 194 { 195 BMediaEventLooper::TimeWarp(at_real_time, to_performance_time); 196 } 197 198 status_t 199 VideoProducer::AddTimer(bigtime_t at_performance_time, int32 cookie) 200 { 201 return BMediaEventLooper::AddTimer(at_performance_time, cookie); 202 } 203 204 void 205 VideoProducer::SetRunMode(run_mode mode) 206 { 207 BMediaEventLooper::SetRunMode(mode); 208 } 209 210 void 211 VideoProducer::HandleEvent(const media_timed_event *event, 212 bigtime_t lateness, bool realTimeEvent) 213 { 214 TOUCH(lateness); TOUCH(realTimeEvent); 215 216 switch(event->type) 217 { 218 case BTimedEventQueue::B_START: 219 HandleStart(event->event_time); 220 break; 221 case BTimedEventQueue::B_STOP: 222 HandleStop(); 223 break; 224 case BTimedEventQueue::B_WARP: 225 HandleTimeWarp(event->bigdata); 226 break; 227 case BTimedEventQueue::B_SEEK: 228 HandleSeek(event->bigdata); 229 break; 230 case BTimedEventQueue::B_HANDLE_BUFFER: 231 case BTimedEventQueue::B_DATA_STATUS: 232 case BTimedEventQueue::B_PARAMETER: 233 default: 234 PRINTF(-1, ("HandleEvent: Unhandled event -- %lx\n", event->type)); 235 break; 236 } 237 } 238 239 void 240 VideoProducer::CleanUpEvent(const media_timed_event *event) 241 { 242 BMediaEventLooper::CleanUpEvent(event); 243 } 244 245 bigtime_t 246 VideoProducer::OfflineTime() 247 { 248 return BMediaEventLooper::OfflineTime(); 249 } 250 251 void 252 VideoProducer::ControlLoop() 253 { 254 BMediaEventLooper::ControlLoop(); 255 } 256 257 status_t 258 VideoProducer::DeleteHook(BMediaNode * node) 259 { 260 return BMediaEventLooper::DeleteHook(node); 261 } 262 263 /* BBufferProducer */ 264 265 status_t 266 VideoProducer::FormatSuggestionRequested( 267 media_type type, int32 quality, media_format *format) 268 { 269 if (type != B_MEDIA_ENCODED_VIDEO) 270 return B_MEDIA_BAD_FORMAT; 271 272 TOUCH(quality); 273 274 *format = fOutput.format; 275 format->u.raw_video.field_rate = 29.97f; 276 return B_OK; 277 } 278 279 status_t 280 VideoProducer::FormatProposal(const media_source &output, media_format *format) 281 { 282 status_t err; 283 284 if (!format) 285 return B_BAD_VALUE; 286 287 if (output != fOutput.source) 288 return B_MEDIA_BAD_SOURCE; 289 290 err = format_is_compatible(*format, fOutput.format) ? 291 B_OK : B_MEDIA_BAD_FORMAT; 292 *format = fOutput.format; 293 return err; 294 295 } 296 297 status_t 298 VideoProducer::FormatChangeRequested(const media_source &source, 299 const media_destination &destination, media_format *io_format, 300 int32 *_deprecated_) 301 { 302 TOUCH(destination); TOUCH(io_format); TOUCH(_deprecated_); 303 if (source != fOutput.source) 304 return B_MEDIA_BAD_SOURCE; 305 306 return B_ERROR; 307 } 308 309 status_t 310 VideoProducer::GetNextOutput(int32 *cookie, media_output *out_output) 311 { 312 if (!out_output) 313 return B_BAD_VALUE; 314 315 if ((*cookie) != 0) 316 return B_BAD_INDEX; 317 318 *out_output = fOutput; 319 (*cookie)++; 320 return B_OK; 321 } 322 323 status_t 324 VideoProducer::DisposeOutputCookie(int32 cookie) 325 { 326 TOUCH(cookie); 327 328 return B_OK; 329 } 330 331 status_t 332 VideoProducer::SetBufferGroup(const media_source &for_source, 333 BBufferGroup *group) 334 { 335 TOUCH(for_source); TOUCH(group); 336 337 return B_ERROR; 338 } 339 340 status_t 341 VideoProducer::VideoClippingChanged(const media_source &for_source, 342 int16 num_shorts, int16 *clip_data, 343 const media_video_display_info &display, int32 *_deprecated_) 344 { 345 TOUCH(for_source); TOUCH(num_shorts); TOUCH(clip_data); 346 TOUCH(display); TOUCH(_deprecated_); 347 348 return B_ERROR; 349 } 350 351 status_t 352 VideoProducer::GetLatency(bigtime_t *out_latency) 353 { 354 *out_latency = EventLatency() + SchedulingLatency(); 355 return B_OK; 356 } 357 358 status_t 359 VideoProducer::PrepareToConnect(const media_source &source, 360 const media_destination &destination, media_format *format, 361 media_source *out_source, char *out_name) 362 { 363 // status_t err; 364 365 PRINTF(1, ("PrepareToConnect() %ldx%ld\n", \ 366 format->u.raw_video.display.line_width, \ 367 format->u.raw_video.display.line_count)); 368 369 if (fConnected) { 370 PRINTF(0, ("PrepareToConnect: Already connected\n")); 371 return EALREADY; 372 } 373 374 if (source != fOutput.source) 375 return B_MEDIA_BAD_SOURCE; 376 377 if (fOutput.destination != media_destination::null) 378 return B_MEDIA_ALREADY_CONNECTED; 379 380 /* The format parameter comes in with the suggested format, and may be 381 * specialized as desired by the node */ 382 if (!format_is_compatible(*format, fOutput.format)) { 383 *format = fOutput.format; 384 return B_MEDIA_BAD_FORMAT; 385 } 386 387 //XXX:FIXME 388 // if (format->u.raw_video.display.line_width == 0) 389 format->u.raw_video.display.line_width = 352;//320; 390 format->u.raw_video.display.line_width = 320; 391 // if (format->u.raw_video.display.line_count == 0) 392 format->u.raw_video.display.line_count = 288;//240; 393 format->u.raw_video.display.line_count = 240; 394 if (format->u.raw_video.field_rate == 0) 395 format->u.raw_video.field_rate = 29.97f; 396 397 *out_source = fOutput.source; 398 strcpy(out_name, fOutput.name); 399 400 fOutput.destination = destination; 401 402 return B_OK; 403 } 404 405 void 406 VideoProducer::Connect(status_t error, const media_source &source, 407 const media_destination &destination, const media_format &format, 408 char *io_name) 409 { 410 PRINTF(1, ("Connect() %ldx%ld\n", \ 411 format.u.raw_video.display.line_width, \ 412 format.u.raw_video.display.line_count)); 413 414 if (fConnected) { 415 PRINTF(0, ("Connect: Already connected\n")); 416 return; 417 } 418 419 BAutolock lock(fCamDevice->Locker()); 420 if (!fCamDevice->IsPlugged()) { 421 PRINTF(0, ("Connect: Device unplugged\n")); 422 return; 423 } 424 425 if ( (source != fOutput.source) || (error < B_OK) || 426 !const_cast<media_format *>(&format)->Matches(&fOutput.format)) { 427 PRINTF(1, ("Connect: Connect error\n")); 428 return; 429 } 430 431 fOutput.destination = destination; 432 strcpy(io_name, fOutput.name); 433 434 if (fOutput.format.u.raw_video.field_rate != 0.0f) { 435 fPerformanceTimeBase = fPerformanceTimeBase + 436 (bigtime_t) 437 ((fFrame - fFrameBase) * 438 (1000000 / fOutput.format.u.raw_video.field_rate)); 439 fFrameBase = fFrame; 440 } 441 442 fConnectedFormat = format.u.raw_video; 443 444 /* get the latency */ 445 bigtime_t latency = 0; 446 media_node_id tsID = 0; 447 FindLatencyFor(fOutput.destination, &latency, &tsID); 448 #define NODE_LATENCY 1000 449 SetEventLatency(latency + NODE_LATENCY); 450 451 uint32 *buffer, *p, f = 3; 452 p = buffer = (uint32 *)malloc(4 * fConnectedFormat.display.line_count * 453 fConnectedFormat.display.line_width); 454 if (!buffer) { 455 PRINTF(0, ("Connect: Out of memory\n")); 456 return; 457 } 458 bigtime_t now = system_time(); 459 for (uint32 y=0;y<fConnectedFormat.display.line_count;y++) 460 for (uint32 x=0;x<fConnectedFormat.display.line_width;x++) 461 *(p++) = ((((x+y)^0^x)+f) & 0xff) * (0x01010101 & fColor); 462 fProcessingLatency = system_time() - now; 463 free(buffer); 464 465 /* Create the buffer group */ 466 fBufferGroup = new BBufferGroup(4 * fConnectedFormat.display.line_width * 467 fConnectedFormat.display.line_count, 8); 468 if (fBufferGroup->InitCheck() < B_OK) { 469 delete fBufferGroup; 470 fBufferGroup = NULL; 471 return; 472 } 473 474 fConnected = true; 475 fEnabled = true; 476 477 /* Tell frame generation thread to recalculate delay value */ 478 release_sem(fFrameSync); 479 } 480 481 void 482 VideoProducer::Disconnect(const media_source &source, 483 const media_destination &destination) 484 { 485 PRINTF(1, ("Disconnect()\n")); 486 487 if (!fConnected) { 488 PRINTF(0, ("Disconnect: Not connected\n")); 489 return; 490 } 491 492 if ((source != fOutput.source) || (destination != fOutput.destination)) { 493 PRINTF(0, ("Disconnect: Bad source and/or destination\n")); 494 return; 495 } 496 497 fEnabled = false; 498 fOutput.destination = media_destination::null; 499 500 fLock.Lock(); 501 delete fBufferGroup; 502 fBufferGroup = NULL; 503 fLock.Unlock(); 504 505 fConnected = false; 506 } 507 508 void 509 VideoProducer::LateNoticeReceived(const media_source &source, 510 bigtime_t how_much, bigtime_t performance_time) 511 { 512 TOUCH(source); TOUCH(how_much); TOUCH(performance_time); 513 } 514 515 void 516 VideoProducer::EnableOutput(const media_source &source, bool enabled, 517 int32 *_deprecated_) 518 { 519 TOUCH(_deprecated_); 520 521 if (source != fOutput.source) 522 return; 523 524 fEnabled = enabled; 525 } 526 527 status_t 528 VideoProducer::SetPlayRate(int32 numer, int32 denom) 529 { 530 TOUCH(numer); TOUCH(denom); 531 532 return B_ERROR; 533 } 534 535 void 536 VideoProducer::AdditionalBufferRequested(const media_source &source, 537 media_buffer_id prev_buffer, bigtime_t prev_time, 538 const media_seek_tag *prev_tag) 539 { 540 TOUCH(source); TOUCH(prev_buffer); TOUCH(prev_time); TOUCH(prev_tag); 541 } 542 543 void 544 VideoProducer::LatencyChanged(const media_source &source, 545 const media_destination &destination, bigtime_t new_latency, 546 uint32 flags) 547 { 548 TOUCH(source); TOUCH(destination); TOUCH(new_latency); TOUCH(flags); 549 } 550 551 /* BControllable */ 552 553 status_t 554 VideoProducer::GetParameterValue( 555 int32 id, bigtime_t *last_change, void *value, size_t *size) 556 { 557 if (id != P_COLOR) 558 return B_BAD_VALUE; 559 560 *last_change = fLastColorChange; 561 *size = sizeof(uint32); 562 *((uint32 *)value) = fColor; 563 564 return B_OK; 565 } 566 567 void 568 VideoProducer::SetParameterValue( 569 int32 id, bigtime_t when, const void *value, size_t size) 570 { 571 if ((id != P_COLOR) || !value || (size != sizeof(uint32))) 572 return; 573 574 if (*(uint32 *)value == fColor) 575 return; 576 577 fColor = *(uint32 *)value; 578 fLastColorChange = when; 579 580 BroadcastNewParameterValue( 581 fLastColorChange, P_COLOR, &fColor, sizeof(fColor)); 582 } 583 584 status_t 585 VideoProducer::StartControlPanel(BMessenger *out_messenger) 586 { 587 return BControllable::StartControlPanel(out_messenger); 588 } 589 590 /* VideoProducer */ 591 592 void 593 VideoProducer::HandleStart(bigtime_t performance_time) 594 { 595 /* Start producing frames, even if the output hasn't been connected yet. */ 596 597 PRINTF(1, ("HandleStart(%Ld)\n", performance_time)); 598 599 if (fRunning) { 600 PRINTF(-1, ("HandleStart: Node already started\n")); 601 return; 602 } 603 604 fFrame = 0; 605 fFrameBase = 0; 606 fPerformanceTimeBase = performance_time; 607 608 fFrameSync = create_sem(0, "frame synchronization"); 609 if (fFrameSync < B_OK) 610 goto err1; 611 612 fThread = spawn_thread(_frame_generator_, "frame generator", 613 B_NORMAL_PRIORITY, this); 614 if (fThread < B_OK) 615 goto err2; 616 617 resume_thread(fThread); 618 619 fCamDevice->StartTransfer(); 620 621 fRunning = true; 622 return; 623 624 err2: 625 delete_sem(fFrameSync); 626 err1: 627 return; 628 } 629 630 void 631 VideoProducer::HandleStop(void) 632 { 633 PRINTF(1, ("HandleStop()\n")); 634 635 if (!fRunning) { 636 PRINTF(-1, ("HandleStop: Node isn't running\n")); 637 return; 638 } 639 640 delete_sem(fFrameSync); 641 wait_for_thread(fThread, &fThread); 642 643 fCamDevice->StopTransfer(); 644 645 fRunning = false; 646 } 647 648 void 649 VideoProducer::HandleTimeWarp(bigtime_t performance_time) 650 { 651 fPerformanceTimeBase = performance_time; 652 fFrameBase = fFrame; 653 654 /* Tell frame generation thread to recalculate delay value */ 655 release_sem(fFrameSync); 656 } 657 658 void 659 VideoProducer::HandleSeek(bigtime_t performance_time) 660 { 661 fPerformanceTimeBase = performance_time; 662 fFrameBase = fFrame; 663 664 /* Tell frame generation thread to recalculate delay value */ 665 release_sem(fFrameSync); 666 } 667 668 /* The following functions form the thread that generates frames. You should 669 * replace this with the code that interfaces to your hardware. */ 670 int32 671 VideoProducer::FrameGenerator() 672 { 673 bigtime_t wait_until = system_time(); 674 675 while (1) { 676 status_t err = acquire_sem_etc(fFrameSync, 1, B_ABSOLUTE_TIMEOUT, 677 wait_until); 678 679 /* The only acceptable responses are B_OK and B_TIMED_OUT. Everything 680 * else means the thread should quit. Deleting the semaphore, as in 681 * VideoProducer::HandleStop(), will trigger this behavior. */ 682 if ((err != B_OK) && (err != B_TIMED_OUT)) 683 break; 684 685 fFrame++; 686 687 /* Recalculate the time until the thread should wake up to begin 688 * processing the next frame. Subtract fProcessingLatency so that 689 * the frame is sent in time. */ 690 wait_until = TimeSource()->RealTimeFor(fPerformanceTimeBase, 0) + 691 (bigtime_t) 692 ((fFrame - fFrameBase) * 693 (1000000 / fConnectedFormat.field_rate)) - 694 fProcessingLatency; 695 696 /* Drop frame if it's at least a frame late */ 697 if (wait_until < system_time()) 698 continue; 699 700 /* If the semaphore was acquired successfully, it means something 701 * changed the timing information (see VideoProducer::Connect()) and 702 * so the thread should go back to sleep until the newly-calculated 703 * wait_until time. */ 704 if (err == B_OK) 705 continue; 706 707 /* Send buffers only if the node is running and the output has been 708 * enabled */ 709 if (!fRunning || !fEnabled) 710 continue; 711 712 BAutolock _(fLock); 713 714 /* Fetch a buffer from the buffer group */ 715 BBuffer *buffer = fBufferGroup->RequestBuffer( 716 4 * fConnectedFormat.display.line_width * 717 fConnectedFormat.display.line_count, 0LL); 718 if (!buffer) 719 continue; 720 721 /* Fill out the details about this buffer. */ 722 media_header *h = buffer->Header(); 723 h->type = B_MEDIA_RAW_VIDEO; 724 h->time_source = TimeSource()->ID(); 725 h->size_used = 4 * fConnectedFormat.display.line_width * 726 fConnectedFormat.display.line_count; 727 /* For a buffer originating from a device, you might want to calculate 728 * this based on the PerformanceTimeFor the time your buffer arrived at 729 * the hardware (plus any applicable adjustments). */ 730 h->start_time = fPerformanceTimeBase + 731 (bigtime_t) 732 ((fFrame - fFrameBase) * 733 (1000000 / fConnectedFormat.field_rate)); 734 h->file_pos = 0; 735 h->orig_size = 0; 736 h->data_offset = 0; 737 h->u.raw_video.field_gamma = 1.0; 738 h->u.raw_video.field_sequence = fFrame; 739 h->u.raw_video.field_number = 0; 740 h->u.raw_video.pulldown_number = 0; 741 h->u.raw_video.first_active_line = 1; 742 h->u.raw_video.line_count = fConnectedFormat.display.line_count; 743 744 // This is where we fill the video buffer. 745 746 uint32 *p = (uint32 *)buffer->Data(); 747 #if 0 748 /* Fill in a pattern */ 749 for (uint32 y=0;y<fConnectedFormat.display.line_count;y++) 750 for (uint32 x=0;x<fConnectedFormat.display.line_width;x++) 751 *(p++) = ((((x+y)^0^x)+fFrame) & 0xff) * (0x01010101 & fColor); 752 #endif 753 754 //#ifdef UseFillFrameBuffer 755 err = fCamDevice->FillFrameBuffer(buffer); 756 if (err < B_OK) { 757 ;//XXX handle error 758 } 759 //#endif 760 #ifdef UseGetFrameBitmap 761 BBitmap *bm; 762 err = fCamDevice->GetFrameBitmap(&bm); 763 if (err >= B_OK) { 764 ;//XXX handle error 765 } 766 #endif 767 768 /* Send the buffer on down to the consumer */ 769 if (SendBuffer(buffer, fOutput.destination) < B_OK) { 770 PRINTF(-1, ("FrameGenerator: Error sending buffer\n")); 771 /* If there is a problem sending the buffer, return it to its 772 * buffer group. */ 773 buffer->Recycle(); 774 } 775 } 776 777 return B_OK; 778 } 779 780 int32 781 VideoProducer::_frame_generator_(void *data) 782 { 783 return ((VideoProducer *)data)->FrameGenerator(); 784 } 785