| OLD | NEW |
| 1 /* | 1 /* |
| 2 * RTP input format | 2 * RTP input format |
| 3 * Copyright (c) 2002 Fabrice Bellard | 3 * Copyright (c) 2002 Fabrice Bellard |
| 4 * | 4 * |
| 5 * This file is part of FFmpeg. | 5 * This file is part of FFmpeg. |
| 6 * | 6 * |
| 7 * FFmpeg is free software; you can redistribute it and/or | 7 * FFmpeg is free software; you can redistribute it and/or |
| 8 * modify it under the terms of the GNU Lesser General Public | 8 * modify it under the terms of the GNU Lesser General Public |
| 9 * License as published by the Free Software Foundation; either | 9 * License as published by the Free Software Foundation; either |
| 10 * version 2.1 of the License, or (at your option) any later version. | 10 * version 2.1 of the License, or (at your option) any later version. |
| (...skipping 453 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 464 if (len < ext) | 464 if (len < ext) |
| 465 return -1; | 465 return -1; |
| 466 // skip past RTP header extension | 466 // skip past RTP header extension |
| 467 len -= ext; | 467 len -= ext; |
| 468 buf += ext; | 468 buf += ext; |
| 469 } | 469 } |
| 470 | 470 |
| 471 if (!st) { | 471 if (!st) { |
| 472 /* specific MPEG2TS demux support */ | 472 /* specific MPEG2TS demux support */ |
| 473 ret = ff_mpegts_parse_packet(s->ts, pkt, buf, len); | 473 ret = ff_mpegts_parse_packet(s->ts, pkt, buf, len); |
| 474 if (ret < 0) { | 474 /* The only error that can be returned from ff_mpegts_parse_packet |
| 475 s->prev_ret = -1; | 475 * is "no more data to return from the provided buffer", so return |
| 476 return -1; | 476 * AVERROR(EAGAIN) for all errors */ |
| 477 } | 477 if (ret < 0) |
| 478 return AVERROR(EAGAIN); |
| 478 if (ret < len) { | 479 if (ret < len) { |
| 479 s->read_buf_size = len - ret; | 480 s->read_buf_size = len - ret; |
| 480 memcpy(s->buf, buf + ret, s->read_buf_size); | 481 memcpy(s->buf, buf + ret, s->read_buf_size); |
| 481 s->read_buf_index = 0; | 482 s->read_buf_index = 0; |
| 482 s->prev_ret = 1; | |
| 483 return 1; | 483 return 1; |
| 484 } | 484 } |
| 485 s->prev_ret = 0; | |
| 486 return 0; | 485 return 0; |
| 487 } else if (s->parse_packet) { | 486 } else if (s->parse_packet) { |
| 488 rv = s->parse_packet(s->ic, s->dynamic_protocol_context, | 487 rv = s->parse_packet(s->ic, s->dynamic_protocol_context, |
| 489 s->st, pkt, ×tamp, buf, len, flags); | 488 s->st, pkt, ×tamp, buf, len, flags); |
| 490 } else { | 489 } else { |
| 491 // at this point, the RTP header has been stripped; This is ASSUMING th
at there is only 1 CSRC, which in't wise. | 490 // at this point, the RTP header has been stripped; This is ASSUMING th
at there is only 1 CSRC, which in't wise. |
| 492 switch(st->codec->codec_id) { | 491 switch(st->codec->codec_id) { |
| 493 case CODEC_ID_MP2: | 492 case CODEC_ID_MP2: |
| 494 case CODEC_ID_MP3: | 493 case CODEC_ID_MP3: |
| 495 /* better than nothing: skip mpeg audio RTP header */ | 494 /* better than nothing: skip mpeg audio RTP header */ |
| (...skipping 28 matching lines...) Expand all Loading... |
| 524 memcpy(pkt->data, buf, len); | 523 memcpy(pkt->data, buf, len); |
| 525 break; | 524 break; |
| 526 } | 525 } |
| 527 | 526 |
| 528 pkt->stream_index = st->index; | 527 pkt->stream_index = st->index; |
| 529 } | 528 } |
| 530 | 529 |
| 531 // now perform timestamp things.... | 530 // now perform timestamp things.... |
| 532 finalize_packet(s, pkt, timestamp); | 531 finalize_packet(s, pkt, timestamp); |
| 533 | 532 |
| 534 s->prev_ret = rv; | |
| 535 return rv; | 533 return rv; |
| 536 } | 534 } |
| 537 | 535 |
| 538 void ff_rtp_reset_packet_queue(RTPDemuxContext *s) | 536 void ff_rtp_reset_packet_queue(RTPDemuxContext *s) |
| 539 { | 537 { |
| 540 while (s->queue) { | 538 while (s->queue) { |
| 541 RTPPacket *next = s->queue->next; | 539 RTPPacket *next = s->queue->next; |
| 542 av_free(s->queue->buf); | 540 av_free(s->queue->buf); |
| 543 av_free(s->queue); | 541 av_free(s->queue); |
| 544 s->queue = next; | 542 s->queue = next; |
| (...skipping 27 matching lines...) Expand all Loading... |
| 572 packet->next = cur; | 570 packet->next = cur; |
| 573 if (prev) | 571 if (prev) |
| 574 prev->next = packet; | 572 prev->next = packet; |
| 575 else | 573 else |
| 576 s->queue = packet; | 574 s->queue = packet; |
| 577 s->queue_len++; | 575 s->queue_len++; |
| 578 } | 576 } |
| 579 | 577 |
| 580 static int has_next_packet(RTPDemuxContext *s) | 578 static int has_next_packet(RTPDemuxContext *s) |
| 581 { | 579 { |
| 582 return s->queue && s->queue->seq == s->seq + 1; | 580 return s->queue && s->queue->seq == (uint16_t) (s->seq + 1); |
| 583 } | 581 } |
| 584 | 582 |
| 585 int64_t ff_rtp_queued_packet_time(RTPDemuxContext *s) | 583 int64_t ff_rtp_queued_packet_time(RTPDemuxContext *s) |
| 586 { | 584 { |
| 587 return s->queue ? s->queue->recvtime : 0; | 585 return s->queue ? s->queue->recvtime : 0; |
| 588 } | 586 } |
| 589 | 587 |
| 590 static int rtp_parse_queued_packet(RTPDemuxContext *s, AVPacket *pkt) | 588 static int rtp_parse_queued_packet(RTPDemuxContext *s, AVPacket *pkt) |
| 591 { | 589 { |
| 592 int rv; | 590 int rv; |
| 593 RTPPacket *next; | 591 RTPPacket *next; |
| 594 | 592 |
| 595 if (s->queue_len <= 0) | 593 if (s->queue_len <= 0) |
| 596 return -1; | 594 return -1; |
| 597 | 595 |
| 598 if (!has_next_packet(s)) | 596 if (!has_next_packet(s)) |
| 599 av_log(s->st ? s->st->codec : NULL, AV_LOG_WARNING, | 597 av_log(s->st ? s->st->codec : NULL, AV_LOG_WARNING, |
| 600 "RTP: missed %d packets\n", s->queue->seq - s->seq - 1); | 598 "RTP: missed %d packets\n", s->queue->seq - s->seq - 1); |
| 601 | 599 |
| 602 /* Parse the first packet in the queue, and dequeue it */ | 600 /* Parse the first packet in the queue, and dequeue it */ |
| 603 rv = rtp_parse_packet_internal(s, pkt, s->queue->buf, s->queue->len); | 601 rv = rtp_parse_packet_internal(s, pkt, s->queue->buf, s->queue->len); |
| 604 next = s->queue->next; | 602 next = s->queue->next; |
| 605 av_free(s->queue->buf); | 603 av_free(s->queue->buf); |
| 606 av_free(s->queue); | 604 av_free(s->queue); |
| 607 s->queue = next; | 605 s->queue = next; |
| 608 s->queue_len--; | 606 s->queue_len--; |
| 609 return rv ? rv : has_next_packet(s); | 607 return rv; |
| 610 } | 608 } |
| 611 | 609 |
| 612 /** | 610 static int rtp_parse_one_packet(RTPDemuxContext *s, AVPacket *pkt, |
| 613 * Parse an RTP or RTCP packet directly sent as a buffer. | |
| 614 * @param s RTP parse context. | |
| 615 * @param pkt returned packet | |
| 616 * @param bufptr pointer to the input buffer or NULL to read the next packets | |
| 617 * @param len buffer len | |
| 618 * @return 0 if a packet is returned, 1 if a packet is returned and more can fol
low | |
| 619 * (use buf as NULL to read the next). -1 if no packet (error or no more packet)
. | |
| 620 */ | |
| 621 int rtp_parse_packet(RTPDemuxContext *s, AVPacket *pkt, | |
| 622 uint8_t **bufptr, int len) | 611 uint8_t **bufptr, int len) |
| 623 { | 612 { |
| 624 uint8_t* buf = bufptr ? *bufptr : NULL; | 613 uint8_t* buf = bufptr ? *bufptr : NULL; |
| 625 int ret, flags = 0; | 614 int ret, flags = 0; |
| 626 uint32_t timestamp; | 615 uint32_t timestamp; |
| 627 int rv= 0; | 616 int rv= 0; |
| 628 | 617 |
| 629 if (!buf) { | 618 if (!buf) { |
| 630 /* If parsing of the previous packet actually returned 0, there's | 619 /* If parsing of the previous packet actually returned 0 or an error, |
| 631 * nothing more to be parsed from that packet, but we may have | 620 * there's nothing more to be parsed from that packet, but we may have |
| 632 * indicated that we can return the next enqueued packet. */ | 621 * indicated that we can return the next enqueued packet. */ |
| 633 if (!s->prev_ret) | 622 if (s->prev_ret <= 0) |
| 634 return rtp_parse_queued_packet(s, pkt); | 623 return rtp_parse_queued_packet(s, pkt); |
| 635 /* return the next packets, if any */ | 624 /* return the next packets, if any */ |
| 636 if(s->st && s->parse_packet) { | 625 if(s->st && s->parse_packet) { |
| 637 /* timestamp should be overwritten by parse_packet, if not, | 626 /* timestamp should be overwritten by parse_packet, if not, |
| 638 * the packet is left with pts == AV_NOPTS_VALUE */ | 627 * the packet is left with pts == AV_NOPTS_VALUE */ |
| 639 timestamp = RTP_NOTS_VALUE; | 628 timestamp = RTP_NOTS_VALUE; |
| 640 rv= s->parse_packet(s->ic, s->dynamic_protocol_context, | 629 rv= s->parse_packet(s->ic, s->dynamic_protocol_context, |
| 641 s->st, pkt, ×tamp, NULL, 0, flags); | 630 s->st, pkt, ×tamp, NULL, 0, flags); |
| 642 finalize_packet(s, pkt, timestamp); | 631 finalize_packet(s, pkt, timestamp); |
| 643 s->prev_ret = rv; | 632 return rv; |
| 644 return rv ? rv : has_next_packet(s); | |
| 645 } else { | 633 } else { |
| 646 // TODO: Move to a dynamic packet handler (like above) | 634 // TODO: Move to a dynamic packet handler (like above) |
| 647 if (s->read_buf_index >= s->read_buf_size) { | 635 if (s->read_buf_index >= s->read_buf_size) |
| 648 s->prev_ret = -1; | 636 return AVERROR(EAGAIN); |
| 649 return -1; | |
| 650 } | |
| 651 ret = ff_mpegts_parse_packet(s->ts, pkt, s->buf + s->read_buf_index, | 637 ret = ff_mpegts_parse_packet(s->ts, pkt, s->buf + s->read_buf_index, |
| 652 s->read_buf_size - s->read_buf_index); | 638 s->read_buf_size - s->read_buf_index); |
| 653 if (ret < 0) { | 639 if (ret < 0) |
| 654 s->prev_ret = -1; | 640 return AVERROR(EAGAIN); |
| 655 return -1; | |
| 656 } | |
| 657 s->read_buf_index += ret; | 641 s->read_buf_index += ret; |
| 658 if (s->read_buf_index < s->read_buf_size) | 642 if (s->read_buf_index < s->read_buf_size) |
| 659 return 1; | 643 return 1; |
| 660 else { | 644 else |
| 661 s->prev_ret = 0; | 645 return 0; |
| 662 return has_next_packet(s); | |
| 663 } | |
| 664 } | 646 } |
| 665 } | 647 } |
| 666 | 648 |
| 667 if (len < 12) | 649 if (len < 12) |
| 668 return -1; | 650 return -1; |
| 669 | 651 |
| 670 if ((buf[0] & 0xc0) != (RTP_VERSION << 6)) | 652 if ((buf[0] & 0xc0) != (RTP_VERSION << 6)) |
| 671 return -1; | 653 return -1; |
| 672 if (buf[1] >= RTCP_SR && buf[1] <= RTCP_APP) { | 654 if (buf[1] >= RTCP_SR && buf[1] <= RTCP_APP) { |
| 673 return rtcp_parse_packet(s, buf, len); | 655 return rtcp_parse_packet(s, buf, len); |
| 674 } | 656 } |
| 675 | 657 |
| 676 if (s->seq == 0 || s->queue_size <= 1) { | 658 if ((s->seq == 0 && !s->queue) || s->queue_size <= 1) { |
| 677 /* First packet, or no reordering */ | 659 /* First packet, or no reordering */ |
| 678 return rtp_parse_packet_internal(s, pkt, buf, len); | 660 return rtp_parse_packet_internal(s, pkt, buf, len); |
| 679 } else { | 661 } else { |
| 680 uint16_t seq = AV_RB16(buf + 2); | 662 uint16_t seq = AV_RB16(buf + 2); |
| 681 int16_t diff = seq - s->seq; | 663 int16_t diff = seq - s->seq; |
| 682 if (diff < 0) { | 664 if (diff < 0) { |
| 683 /* Packet older than the previously emitted one, drop */ | 665 /* Packet older than the previously emitted one, drop */ |
| 684 av_log(s->st ? s->st->codec : NULL, AV_LOG_WARNING, | 666 av_log(s->st ? s->st->codec : NULL, AV_LOG_WARNING, |
| 685 "RTP: dropping old packet received too late\n"); | 667 "RTP: dropping old packet received too late\n"); |
| 686 return -1; | 668 return -1; |
| 687 } else if (diff <= 1) { | 669 } else if (diff <= 1) { |
| 688 /* Correct packet */ | 670 /* Correct packet */ |
| 689 rv = rtp_parse_packet_internal(s, pkt, buf, len); | 671 rv = rtp_parse_packet_internal(s, pkt, buf, len); |
| 690 return rv ? rv : has_next_packet(s); | 672 return rv; |
| 691 } else { | 673 } else { |
| 692 /* Still missing some packet, enqueue this one. */ | 674 /* Still missing some packet, enqueue this one. */ |
| 693 enqueue_packet(s, buf, len); | 675 enqueue_packet(s, buf, len); |
| 694 *bufptr = NULL; | 676 *bufptr = NULL; |
| 695 /* Return the first enqueued packet if the queue is full, | 677 /* Return the first enqueued packet if the queue is full, |
| 696 * even if we're missing something */ | 678 * even if we're missing something */ |
| 697 if (s->queue_len >= s->queue_size) | 679 if (s->queue_len >= s->queue_size) |
| 698 return rtp_parse_queued_packet(s, pkt); | 680 return rtp_parse_queued_packet(s, pkt); |
| 699 return -1; | 681 return -1; |
| 700 } | 682 } |
| 701 } | 683 } |
| 702 } | 684 } |
| 703 | 685 |
| 686 /** |
| 687 * Parse an RTP or RTCP packet directly sent as a buffer. |
| 688 * @param s RTP parse context. |
| 689 * @param pkt returned packet |
| 690 * @param bufptr pointer to the input buffer or NULL to read the next packets |
| 691 * @param len buffer len |
| 692 * @return 0 if a packet is returned, 1 if a packet is returned and more can fol
low |
| 693 * (use buf as NULL to read the next). -1 if no packet (error or no more packet)
. |
| 694 */ |
| 695 int rtp_parse_packet(RTPDemuxContext *s, AVPacket *pkt, |
| 696 uint8_t **bufptr, int len) |
| 697 { |
| 698 int rv = rtp_parse_one_packet(s, pkt, bufptr, len); |
| 699 s->prev_ret = rv; |
| 700 while (rv == AVERROR(EAGAIN) && has_next_packet(s)) |
| 701 rv = rtp_parse_queued_packet(s, pkt); |
| 702 return rv ? rv : has_next_packet(s); |
| 703 } |
| 704 |
| 704 void rtp_parse_close(RTPDemuxContext *s) | 705 void rtp_parse_close(RTPDemuxContext *s) |
| 705 { | 706 { |
| 706 ff_rtp_reset_packet_queue(s); | 707 ff_rtp_reset_packet_queue(s); |
| 707 if (!strcmp(ff_rtp_enc_name(s->payload_type), "MP2T")) { | 708 if (!strcmp(ff_rtp_enc_name(s->payload_type), "MP2T")) { |
| 708 ff_mpegts_parse_close(s->ts); | 709 ff_mpegts_parse_close(s->ts); |
| 709 } | 710 } |
| 710 av_free(s); | 711 av_free(s); |
| 711 } | 712 } |
| 712 | 713 |
| 713 int ff_parse_fmtp(AVStream *stream, PayloadContext *data, const char *p, | 714 int ff_parse_fmtp(AVStream *stream, PayloadContext *data, const char *p, |
| (...skipping 22 matching lines...) Expand all Loading... |
| 736 | 737 |
| 737 res = parse_fmtp(stream, data, attr, value); | 738 res = parse_fmtp(stream, data, attr, value); |
| 738 if (res < 0 && res != AVERROR_PATCHWELCOME) { | 739 if (res < 0 && res != AVERROR_PATCHWELCOME) { |
| 739 av_free(value); | 740 av_free(value); |
| 740 return res; | 741 return res; |
| 741 } | 742 } |
| 742 } | 743 } |
| 743 av_free(value); | 744 av_free(value); |
| 744 return 0; | 745 return 0; |
| 745 } | 746 } |
| OLD | NEW |