| 319 | | |
|---|
| 320 | | /***************************************************************************** |
|---|
| 321 | | * BlockTCP: Framed RTP/AVP packet reception for COMEDIA (see RFC4571) |
|---|
| 322 | | *****************************************************************************/ |
|---|
| 323 | | static block_t *BlockTCP( access_t *p_access ) |
|---|
| 324 | | { |
|---|
| 325 | | access_sys_t *p_sys = p_access->p_sys; |
|---|
| 326 | | block_t *p_block = p_sys->p_partial_frame; |
|---|
| 327 | | |
|---|
| 328 | | if( p_access->info.b_eof ) |
|---|
| 329 | | return NULL; |
|---|
| 330 | | |
|---|
| 331 | | if( p_block == NULL ) |
|---|
| 332 | | { |
|---|
| 333 | | /* MTU should always be 65535 in this case */ |
|---|
| 334 | | p_sys->p_partial_frame = p_block = block_New( p_access, 2 + MTU ); |
|---|
| 335 | | if (p_block == NULL) |
|---|
| 336 | | return NULL; |
|---|
| 337 | | } |
|---|
| 338 | | |
|---|
| 339 | | /* Read RTP framing */ |
|---|
| 340 | | if (p_block->i_buffer < 2) |
|---|
| 341 | | { |
|---|
| 342 | | int i_read = net_Read( p_access, p_sys->fd, NULL, |
|---|
| 343 | | p_block->p_buffer + p_block->i_buffer, |
|---|
| 344 | | 2 - p_block->i_buffer, false ); |
|---|
| 345 | | if( i_read <= 0 ) |
|---|
| 346 | | goto error; |
|---|
| 347 | | |
|---|
| 348 | | p_block->i_buffer += i_read; |
|---|
| 349 | | if (p_block->i_buffer < 2) |
|---|
| 350 | | return NULL; |
|---|
| 351 | | } |
|---|
| 352 | | |
|---|
| 353 | | uint16_t framelen = GetWLE( p_block->p_buffer ); |
|---|
| 354 | | /* Read RTP frame */ |
|---|
| 355 | | if( framelen > 0 ) |
|---|
| 356 | | { |
|---|
| 357 | | int i_read = net_Read( p_access, p_sys->fd, NULL, |
|---|
| 358 | | p_block->p_buffer + p_block->i_buffer, |
|---|
| 359 | | 2 + framelen - p_block->i_buffer, false ); |
|---|
| 360 | | if( i_read <= 0 ) |
|---|
| 361 | | goto error; |
|---|
| 362 | | |
|---|
| 363 | | p_block->i_buffer += i_read; |
|---|
| 364 | | } |
|---|
| 365 | | |
|---|
| 366 | | if( p_block->i_buffer < (2u + framelen) ) |
|---|
| 367 | | return NULL; // incomplete frame |
|---|
| 368 | | |
|---|
| 369 | | /* Hide framing from RTP layer */ |
|---|
| 370 | | p_block->p_buffer += 2; |
|---|
| 371 | | p_block->i_buffer -= 2; |
|---|
| 372 | | p_sys->p_partial_frame = NULL; |
|---|
| 373 | | return p_block; |
|---|
| 374 | | |
|---|
| 375 | | error: |
|---|
| 376 | | p_access->info.b_eof = true; |
|---|
| 377 | | block_Release( p_block ); |
|---|
| 378 | | p_sys->p_partial_frame = NULL; |
|---|
| 379 | | return NULL; |
|---|
| 380 | | } |
|---|
| 381 | | |
|---|
| 382 | | |
|---|
| 383 | | /* |
|---|
| 384 | | * rtp_ChainInsert - insert a p_block in the chain and |
|---|
| 385 | | * look at the sequence numbers. |
|---|
| 386 | | */ |
|---|
| 387 | | static inline bool rtp_ChainInsert( access_t *p_access, block_t *p_block ) |
|---|
| 388 | | { |
|---|
| 389 | | access_sys_t *p_sys = (access_sys_t *) p_access->p_sys; |
|---|
| 390 | | block_t *p_prev = NULL; |
|---|
| 391 | | block_t *p = p_sys->p_end; |
|---|
| 392 | | uint16_t i_new = (uint16_t) p_block->i_dts; |
|---|
| 393 | | uint16_t i_tmp = 0; |
|---|
| 394 | | |
|---|
| 395 | | if( !p_sys->p_list ) |
|---|
| 396 | | { |
|---|
| 397 | | p_sys->p_list = p_block; |
|---|
| 398 | | p_sys->p_end = p_block; |
|---|
| 399 | | return true; |
|---|
| 400 | | } |
|---|
| 401 | | /* walk through the queue from top down since the new packet is in |
|---|
| 402 | | most cases just appended to the end */ |
|---|
| 403 | | |
|---|
| 404 | | for( ;; ) |
|---|
| 405 | | { |
|---|
| 406 | | i_tmp = i_new - (uint16_t) p->i_dts; |
|---|
| 407 | | |
|---|
| 408 | | if( !i_tmp ) /* trash duplicate */ |
|---|
| 409 | | break; |
|---|
| 410 | | |
|---|
| 411 | | if ( i_tmp < 32768 ) |
|---|
| 412 | | { /* insert after this block ( i_new > p->i_dts ) */ |
|---|
| 413 | | p_block->p_next = p->p_next; |
|---|
| 414 | | p->p_next = p_block; |
|---|
| 415 | | p_block->p_prev = p; |
|---|
| 416 | | if (p_prev) |
|---|
| 417 | | { |
|---|
| 418 | | p_prev->p_prev = p_block; |
|---|
| 419 | | msg_Dbg(p_access, "RTP reordering: insert after %d, new %d", |
|---|
| 420 | | (uint16_t) p->i_dts, i_new ); |
|---|
| 421 | | } |
|---|
| 422 | | else |
|---|
| 423 | | { |
|---|
| 424 | | p_sys->p_end = p_block; |
|---|
| 425 | | } |
|---|
| 426 | | return true; |
|---|
| 427 | | } |
|---|
| 428 | | if( p == p_sys->p_list ) |
|---|
| 429 | | { /* we've reached bottom of chain */ |
|---|
| 430 | | i_tmp = p_sys->i_last_seqno - i_new; |
|---|
| 431 | | if( !p_access->info.b_prebuffered || (i_tmp > 32767) ) |
|---|
| 432 | | { |
|---|
| 433 | | msg_Dbg(p_access, "RTP reordering: prepend %d before %d", |
|---|
| 434 | | i_new, (uint16_t) p->i_dts ); |
|---|
| 435 | | p_block->p_next = p; |
|---|
| 436 | | p->p_prev = p_block; |
|---|
| 437 | | p_sys->p_list = p_block; |
|---|
| 438 | | return true; |
|---|
| 439 | | } |
|---|
| 440 | | |
|---|
| 441 | | if( !i_tmp ) /* trash duplicate */ |
|---|
| 442 | | break; |
|---|
| 443 | | |
|---|
| 444 | | /* reordering failed - append the packet to the end of queue */ |
|---|
| 445 | | msg_Dbg(p_access, "RTP: sequence changed (or buffer too small) " |
|---|
| 446 | | "new: %d, buffer %d...%d", i_new, (uint16_t) p->i_dts, |
|---|
| 447 | | (uint16_t) p_sys->p_end->i_dts); |
|---|
| 448 | | p_sys->p_end->p_next = p_block; |
|---|
| 449 | | p_block->p_prev = p_sys->p_end; |
|---|
| 450 | | p_sys->p_end = p_block; |
|---|
| 451 | | return true; |
|---|
| 452 | | } |
|---|
| 453 | | p_prev = p; |
|---|
| 454 | | p = p->p_prev; |
|---|
| 455 | | } |
|---|
| 456 | | block_Release( p_block ); |
|---|
| 457 | | return false; |
|---|
| 458 | | } |
|---|
| 459 | | |
|---|
| 460 | | /***************************************************************************** |
|---|
| 461 | | * BlockParseRTP: decapsulate the RTP packet and return it |
|---|
| 462 | | *****************************************************************************/ |
|---|
| 463 | | static block_t *BlockParseRTP( access_t *p_access, block_t *p_block ) |
|---|
| 464 | | { |
|---|
| 465 | | int i_payload_type; |
|---|
| 466 | | size_t i_skip = RTP_HEADER_LEN; |
|---|
| 467 | | |
|---|
| 468 | | if( p_block == NULL ) |
|---|
| 469 | | return NULL; |
|---|
| 470 | | |
|---|
| 471 | | if( p_block->i_buffer < RTP_HEADER_LEN ) |
|---|
| 472 | | { |
|---|
| 473 | | msg_Dbg( p_access, "short RTP packet received" ); |
|---|
| 474 | | goto trash; |
|---|
| 475 | | } |
|---|
| 476 | | |
|---|
| 477 | | /* Parse the header and make some verifications. |
|---|
| 478 | | * See RFC 3550. */ |
|---|
| 479 | | // Version number: |
|---|
| 480 | | if( ( p_block->p_buffer[0] >> 6 ) != 2) |
|---|
| 481 | | { |
|---|
| 482 | | msg_Dbg( p_access, "RTP version is %u instead of 2", |
|---|
| 483 | | p_block->p_buffer[0] >> 6 ); |
|---|
| 484 | | goto trash; |
|---|
| 485 | | } |
|---|
| 486 | | // Padding bit: |
|---|
| 487 | | uint8_t pad = (p_block->p_buffer[0] & 0x20) |
|---|
| 488 | | ? p_block->p_buffer[p_block->i_buffer - 1] : 0; |
|---|
| 489 | | // CSRC count: |
|---|
| 490 | | i_skip += (p_block->p_buffer[0] & 0x0F) * 4; |
|---|
| 491 | | // Extension header: |
|---|
| 492 | | if (p_block->p_buffer[0] & 0x10) /* Extension header */ |
|---|
| 493 | | { |
|---|
| 494 | | i_skip += 4; |
|---|
| 495 | | if ((size_t)p_block->i_buffer < i_skip) |
|---|
| 496 | | goto trash; |
|---|
| 497 | | |
|---|
| 498 | | i_skip += 4 * GetWBE( p_block->p_buffer + i_skip - 2 ); |
|---|
| 499 | | } |
|---|
| 500 | | |
|---|
| 501 | | i_payload_type = p_block->p_buffer[1] & 0x7F; |
|---|
| 502 | | |
|---|
| 503 | | /* Remember sequence number in i_dts */ |
|---|
| 504 | | p_block->i_pts = mdate(); |
|---|
| 505 | | p_block->i_dts = (mtime_t) GetWBE( p_block->p_buffer + 2 ); |
|---|
| 506 | | |
|---|
| 507 | | /* FIXME: use rtpmap */ |
|---|
| 508 | | const char *psz_demux = NULL; |
|---|
| 509 | | |
|---|
| 510 | | switch( i_payload_type ) |
|---|
| 511 | | { |
|---|
| 512 | | case 14: // MPA: MPEG Audio (RFC2250, §3.4) |
|---|
| 513 | | i_skip += 4; // 32 bits RTP/MPA header |
|---|
| 514 | | psz_demux = "mpga"; |
|---|
| 515 | | break; |
|---|
| 516 | | |
|---|
| 517 | | case 32: // MPV: MPEG Video (RFC2250, §3.5) |
|---|
| 518 | | i_skip += 4; // 32 bits RTP/MPV header |
|---|
| 519 | | if( (size_t)p_block->i_buffer < i_skip ) |
|---|
| 520 | | goto trash; |
|---|
| 521 | | if( p_block->p_buffer[i_skip - 3] & 0x4 ) |
|---|
| 522 | | { |
|---|
| 523 | | /* MPEG2 Video extension header */ |
|---|
| 524 | | /* TODO: shouldn't we skip this too ? */ |
|---|
| 525 | | } |
|---|
| 526 | | psz_demux = "mpgv"; |
|---|
| 527 | | break; |
|---|
| 528 | | |
|---|
| 529 | | case 33: // MP2: MPEG TS (RFC2250, §2) |
|---|
| 530 | | /* plain TS over RTP */ |
|---|
| 531 | | psz_demux = "ts"; |
|---|
| 532 | | break; |
|---|
| 533 | | |
|---|
| 534 | | case 72: /* muxed SR */ |
|---|
| 535 | | case 73: /* muxed RR */ |
|---|
| 536 | | case 74: /* muxed SDES */ |
|---|
| 537 | | case 75: /* muxed BYE */ |
|---|
| 538 | | case 76: /* muxed APP */ |
|---|
| 539 | | goto trash; /* ooh! ignoring RTCP is evil! */ |
|---|
| 540 | | |
|---|
| 541 | | default: |
|---|
| 542 | | msg_Dbg( p_access, "unsupported RTP payload type: %u", i_payload_type ); |
|---|
| 543 | | goto trash; |
|---|
| 544 | | } |
|---|
| 545 | | |
|---|
| 546 | | if( (size_t)p_block->i_buffer < (i_skip + pad) ) |
|---|
| 547 | | goto trash; |
|---|
| 548 | | |
|---|
| 549 | | /* Remove the RTP header */ |
|---|
| 550 | | p_block->i_buffer -= i_skip; |
|---|
| 551 | | p_block->p_buffer += i_skip; |
|---|
| 552 | | |
|---|
| 553 | | /* This is the place for deciphering and authentication */ |
|---|
| 554 | | |
|---|
| 555 | | /* Remove padding (at the end) */ |
|---|
| 556 | | p_block->i_buffer -= pad; |
|---|
| 557 | | |
|---|
| 558 | | #if 0 |
|---|
| 559 | | /* Emulate packet loss */ |
|---|
| 560 | | if ( (i_sequence_number % 4000) == 0) |
|---|
| 561 | | { |
|---|
| 562 | | msg_Warn( p_access, "Emulating packet drop" ); |
|---|
| 563 | | block_Release( p_block ); |
|---|
| 564 | | return NULL; |
|---|
| 565 | | } |
|---|
| 566 | | #endif |
|---|
| 567 | | |
|---|
| 568 | | if( !p_access->psz_demux || !*p_access->psz_demux ) |
|---|
| 569 | | { |
|---|
| 570 | | free( p_access->psz_demux ); |
|---|
| 571 | | p_access->psz_demux = strdup( psz_demux ); |
|---|
| 572 | | } |
|---|
| 573 | | |
|---|
| 574 | | return p_block; |
|---|
| 575 | | |
|---|
| 576 | | trash: |
|---|
| 577 | | block_Release( p_block ); |
|---|
| 578 | | return NULL; |
|---|
| 579 | | } |
|---|
| 580 | | |
|---|
| 581 | | /***************************************************************************** |
|---|
| 582 | | * BlockRTP: receives an RTP packet, parses it, queues it queue, |
|---|
| 583 | | * then dequeues the oldest packet and returns it to input/demux. |
|---|
| 584 | | ****************************************************************************/ |
|---|
| 585 | | static block_t *BlockRTP( access_t *p_access ) |
|---|
| 586 | | { |
|---|
| 587 | | access_sys_t *p_sys = p_access->p_sys; |
|---|
| 588 | | block_t *p; |
|---|
| 589 | | |
|---|
| 590 | | while ( !p_sys->p_list || |
|---|
| 591 | | ( mdate() - p_sys->p_list->i_pts ) < p_sys->i_rtp_late ) |
|---|
| 592 | | { |
|---|
| 593 | | p = BlockParseRTP( p_access, |
|---|
| 594 | | p_sys->b_framed_rtp ? BlockTCP( p_access ) |
|---|
| 595 | | : BlockUDP( p_access ) ); |
|---|
| 596 | | if ( !p ) |
|---|
| 597 | | return NULL; |
|---|
| 598 | | |
|---|
| 599 | | rtp_ChainInsert( p_access, p ); |
|---|
| 600 | | } |
|---|
| 601 | | |
|---|
| 602 | | p = p_sys->p_list; |
|---|
| 603 | | p_sys->p_list = p_sys->p_list->p_next; |
|---|
| 604 | | p_sys->i_last_seqno++; |
|---|
| 605 | | if( p_sys->i_last_seqno != (uint16_t) p->i_dts ) |
|---|
| 606 | | { |
|---|
| 607 | | msg_Dbg( p_access, "RTP: packet(s) lost, expected %d, got %d", |
|---|
| 608 | | p_sys->i_last_seqno, (uint16_t) p->i_dts ); |
|---|
| 609 | | p_sys->i_last_seqno = (uint16_t) p->i_dts; |
|---|
| 610 | | } |
|---|
| 611 | | p->p_next = NULL; |
|---|
| 612 | | return p; |
|---|
| 613 | | } |
|---|
| 614 | | |
|---|
| 615 | | /***************************************************************************** |
|---|
| 616 | | * BlockPrebufferRTP: waits until we have at least two RTP datagrams, |
|---|
| 617 | | * so that we can synchronize the RTP sequence number. |
|---|
| 618 | | * This is only useful for non-reliable transport protocols. |
|---|
| 619 | | ****************************************************************************/ |
|---|
| 620 | | static block_t *BlockPrebufferRTP( access_t *p_access, block_t *p_block ) |
|---|
| 621 | | { |
|---|
| 622 | | access_sys_t *p_sys = p_access->p_sys; |
|---|
| 623 | | mtime_t i_first = mdate(); |
|---|
| 624 | | int i_count = 0; |
|---|
| 625 | | block_t *p = p_block; |
|---|
| 626 | | |
|---|
| 627 | | if( BlockParseRTP( p_access, p_block ) == NULL ) |
|---|
| 628 | | return NULL; |
|---|
| 629 | | |
|---|
| 630 | | for( ;; ) |
|---|
| 631 | | { |
|---|
| 632 | | mtime_t i_date = mdate(); |
|---|
| 633 | | |
|---|
| 634 | | if( p && rtp_ChainInsert( p_access, p )) |
|---|
| 635 | | i_count++; |
|---|
| 636 | | |
|---|
| 637 | | /* Require at least 2 packets in the buffer */ |
|---|
| 638 | | if( i_count > 2 && (i_date - i_first) > p_sys->i_rtp_late ) |
|---|
| 639 | | break; |
|---|
| 640 | | |
|---|
| 641 | | p = BlockParseRTP( p_access, BlockUDP( p_access ) ); |
|---|
| 642 | | if( !p && (i_date - i_first) > p_sys->i_rtp_late ) |
|---|
| 643 | | { |
|---|
| 644 | | msg_Err( p_access, "error in RTP prebuffering!" ); |
|---|
| 645 | | return NULL; |
|---|
| 646 | | } |
|---|
| 647 | | } |
|---|
| 648 | | |
|---|
| 649 | | msg_Dbg( p_access, "RTP: prebuffered %d packets", i_count - 1 ); |
|---|
| 650 | | p_access->info.b_prebuffered = true; |
|---|
| 651 | | p = p_sys->p_list; |
|---|
| 652 | | p_sys->p_list = p_sys->p_list->p_next; |
|---|
| 653 | | p_sys->i_last_seqno = (uint16_t) p->i_dts; |
|---|
| 654 | | p->p_next = NULL; |
|---|
| 655 | | return p; |
|---|
| 656 | | } |
|---|
| 657 | | |
|---|
| 658 | | static block_t *BlockStartRTP( access_t *p_access ) |
|---|
| 659 | | { |
|---|
| 660 | | p_access->pf_block = BlockRTP; |
|---|
| 661 | | return BlockPrebufferRTP( p_access, BlockUDP( p_access ) ); |
|---|
| 662 | | } |
|---|