comparison rtp.c @ 885:da1d5db0ce5c libavformat

COSMETICS: Remove all trailing whitespace.
author diego
date Sat, 17 Dec 2005 18:14:38 +0000
parents d064b7d0899d
children edbe5c3717f9
comparison
equal deleted inserted replaced
884:2ece9c9dd94c 885:da1d5db0ce5c
38 38
39 - add support for h263/mpeg4 packetized output : IDEA: send a 39 - add support for h263/mpeg4 packetized output : IDEA: send a
40 buffer to 'rtp_write_packet' contains all the packets for ONE 40 buffer to 'rtp_write_packet' contains all the packets for ONE
41 frame. Each packet should have a four byte header containing 41 frame. Each packet should have a four byte header containing
42 the length in big endian format (same trick as 42 the length in big endian format (same trick as
43 'url_open_dyn_packet_buf') 43 'url_open_dyn_packet_buf')
44 */ 44 */
45 45
46 /* from http://www.iana.org/assignments/rtp-parameters last updated 05 January 2005 */ 46 /* from http://www.iana.org/assignments/rtp-parameters last updated 05 January 2005 */
47 AVRtpPayloadType_t AVRtpPayloadTypes[]= 47 AVRtpPayloadType_t AVRtpPayloadTypes[]=
48 { 48 {
195 uint32_t cur_timestamp; 195 uint32_t cur_timestamp;
196 int max_payload_size; 196 int max_payload_size;
197 MpegTSContext *ts; /* only used for MP2T payloads */ 197 MpegTSContext *ts; /* only used for MP2T payloads */
198 int read_buf_index; 198 int read_buf_index;
199 int read_buf_size; 199 int read_buf_size;
200 200
201 /* rtcp sender statistics receive */ 201 /* rtcp sender statistics receive */
202 int64_t last_rtcp_ntp_time; 202 int64_t last_rtcp_ntp_time;
203 int64_t first_rtcp_ntp_time; 203 int64_t first_rtcp_ntp_time;
204 uint32_t last_rtcp_timestamp; 204 uint32_t last_rtcp_timestamp;
205 /* rtcp sender statistics */ 205 /* rtcp sender statistics */
266 } 266 }
267 267
268 /** 268 /**
269 * open a new RTP parse context for stream 'st'. 'st' can be NULL for 269 * open a new RTP parse context for stream 'st'. 'st' can be NULL for
270 * MPEG2TS streams to indicate that they should be demuxed inside the 270 * MPEG2TS streams to indicate that they should be demuxed inside the
271 * rtp demux (otherwise CODEC_ID_MPEG2TS packets are returned) 271 * rtp demux (otherwise CODEC_ID_MPEG2TS packets are returned)
272 */ 272 */
273 RTPDemuxContext *rtp_parse_open(AVFormatContext *s1, AVStream *st, int payload_type, rtp_payload_data_t *rtp_payload_data) 273 RTPDemuxContext *rtp_parse_open(AVFormatContext *s1, AVStream *st, int payload_type, rtp_payload_data_t *rtp_payload_data)
274 { 274 {
275 RTPDemuxContext *s; 275 RTPDemuxContext *s;
276 276
352 352
353 return 0; 353 return 0;
354 } 354 }
355 355
356 /** 356 /**
357 * Parse an RTP or RTCP packet directly sent as a buffer. 357 * Parse an RTP or RTCP packet directly sent as a buffer.
358 * @param s RTP parse context. 358 * @param s RTP parse context.
359 * @param pkt returned packet 359 * @param pkt returned packet
360 * @param buf input buffer or NULL to read the next packets 360 * @param buf input buffer or NULL to read the next packets
361 * @param len buffer len 361 * @param len buffer len
362 * @return 0 if a packet is returned, 1 if a packet is returned and more can follow 362 * @return 0 if a packet is returned, 1 if a packet is returned and more can follow
363 * (use buf as NULL to read the next). -1 if no packet (error or no more packet). 363 * (use buf as NULL to read the next). -1 if no packet (error or no more packet).
364 */ 364 */
365 int rtp_parse_packet(RTPDemuxContext *s, AVPacket *pkt, 365 int rtp_parse_packet(RTPDemuxContext *s, AVPacket *pkt,
366 const uint8_t *buf, int len) 366 const uint8_t *buf, int len)
367 { 367 {
368 unsigned int ssrc, h; 368 unsigned int ssrc, h;
369 int payload_type, seq, delta_timestamp, ret; 369 int payload_type, seq, delta_timestamp, ret;
370 AVStream *st; 370 AVStream *st;
371 uint32_t timestamp; 371 uint32_t timestamp;
372 372
373 if (!buf) { 373 if (!buf) {
374 /* return the next packets, if any */ 374 /* return the next packets, if any */
375 if (s->read_buf_index >= s->read_buf_size) 375 if (s->read_buf_index >= s->read_buf_size)
376 return -1; 376 return -1;
377 ret = mpegts_parse_packet(s->ts, pkt, s->buf + s->read_buf_index, 377 ret = mpegts_parse_packet(s->ts, pkt, s->buf + s->read_buf_index,
378 s->read_buf_size - s->read_buf_index); 378 s->read_buf_size - s->read_buf_index);
379 if (ret < 0) 379 if (ret < 0)
380 return -1; 380 return -1;
381 s->read_buf_index += ret; 381 s->read_buf_index += ret;
382 if (s->read_buf_index < s->read_buf_size) 382 if (s->read_buf_index < s->read_buf_size)
396 } 396 }
397 payload_type = buf[1] & 0x7f; 397 payload_type = buf[1] & 0x7f;
398 seq = (buf[2] << 8) | buf[3]; 398 seq = (buf[2] << 8) | buf[3];
399 timestamp = decode_be32(buf + 4); 399 timestamp = decode_be32(buf + 4);
400 ssrc = decode_be32(buf + 8); 400 ssrc = decode_be32(buf + 8);
401 401
402 /* NOTE: we can handle only one payload type */ 402 /* NOTE: we can handle only one payload type */
403 if (s->payload_type != payload_type) 403 if (s->payload_type != payload_type)
404 return -1; 404 return -1;
405 #if defined(DEBUG) || 1 405 #if defined(DEBUG) || 1
406 if (seq != ((s->seq + 1) & 0xffff)) { 406 if (seq != ((s->seq + 1) & 0xffff)) {
407 av_log(s->st->codec, AV_LOG_ERROR, "RTP: PT=%02x: bad cseq %04x expected=%04x\n", 407 av_log(s->st->codec, AV_LOG_ERROR, "RTP: PT=%02x: bad cseq %04x expected=%04x\n",
408 payload_type, seq, ((s->seq + 1) & 0xffff)); 408 payload_type, seq, ((s->seq + 1) & 0xffff));
409 } 409 }
410 #endif 410 #endif
411 s->seq = seq; 411 s->seq = seq;
412 len -= 12; 412 len -= 12;
456 default: 456 default:
457 av_new_packet(pkt, len); 457 av_new_packet(pkt, len);
458 memcpy(pkt->data, buf, len); 458 memcpy(pkt->data, buf, len);
459 break; 459 break;
460 } 460 }
461 461
462 switch(st->codec->codec_id) { 462 switch(st->codec->codec_id) {
463 case CODEC_ID_MP2: 463 case CODEC_ID_MP2:
464 case CODEC_ID_MPEG1VIDEO: 464 case CODEC_ID_MPEG1VIDEO:
465 if (s->last_rtcp_ntp_time != AV_NOPTS_VALUE) { 465 if (s->last_rtcp_ntp_time != AV_NOPTS_VALUE) {
466 int64_t addend; 466 int64_t addend;
597 put_byte(&s1->pb, (RTP_VERSION << 6)); 597 put_byte(&s1->pb, (RTP_VERSION << 6));
598 put_byte(&s1->pb, (s->payload_type & 0x7f) | ((m & 0x01) << 7)); 598 put_byte(&s1->pb, (s->payload_type & 0x7f) | ((m & 0x01) << 7));
599 put_be16(&s1->pb, s->seq); 599 put_be16(&s1->pb, s->seq);
600 put_be32(&s1->pb, s->timestamp); 600 put_be32(&s1->pb, s->timestamp);
601 put_be32(&s1->pb, s->ssrc); 601 put_be32(&s1->pb, s->ssrc);
602 602
603 put_buffer(&s1->pb, buf1, len); 603 put_buffer(&s1->pb, buf1, len);
604 put_flush_packet(&s1->pb); 604 put_flush_packet(&s1->pb);
605 605
606 s->seq++; 606 s->seq++;
607 s->octet_count += len; 607 s->octet_count += len;
608 s->packet_count++; 608 s->packet_count++;
609 } 609 }
610 610
637 s->buf_ptr = s->buf; 637 s->buf_ptr = s->buf;
638 /* update timestamp */ 638 /* update timestamp */
639 s->timestamp += n / sample_size; 639 s->timestamp += n / sample_size;
640 } 640 }
641 } 641 }
642 } 642 }
643 643
644 /* NOTE: we suppose that exactly one frame is given as argument here */ 644 /* NOTE: we suppose that exactly one frame is given as argument here */
645 /* XXX: test it */ 645 /* XXX: test it */
646 static void rtp_send_mpegaudio(AVFormatContext *s1, 646 static void rtp_send_mpegaudio(AVFormatContext *s1,
647 const uint8_t *buf1, int size) 647 const uint8_t *buf1, int size)
657 if ((len + size) > max_packet_size) { 657 if ((len + size) > max_packet_size) {
658 if (len > 4) { 658 if (len > 4) {
659 rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0); 659 rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);
660 s->buf_ptr = s->buf + 4; 660 s->buf_ptr = s->buf + 4;
661 /* 90 KHz time stamp */ 661 /* 90 KHz time stamp */
662 s->timestamp = s->base_timestamp + 662 s->timestamp = s->base_timestamp +
663 (s->cur_timestamp * 90000LL) / st->codec->sample_rate; 663 (s->cur_timestamp * 90000LL) / st->codec->sample_rate;
664 } 664 }
665 } 665 }
666 666
667 /* add the packet */ 667 /* add the packet */
725 *q++ = h >> 24; 725 *q++ = h >> 24;
726 *q++ = h >> 16; 726 *q++ = h >> 16;
727 *q++ = h >> 8; 727 *q++ = h >> 8;
728 *q++ = h; 728 *q++ = h;
729 } 729 }
730 730
731 len = max_packet_size - (q - s->buf); 731 len = max_packet_size - (q - s->buf);
732 if (len > size) 732 if (len > size)
733 len = size; 733 len = size;
734 734
735 memcpy(q, buf1, len); 735 memcpy(q, buf1, len);
736 q += len; 736 q += len;
737 737
738 /* 90 KHz time stamp */ 738 /* 90 KHz time stamp */
739 s->timestamp = s->base_timestamp + 739 s->timestamp = s->base_timestamp +
740 av_rescale((int64_t)s->cur_timestamp * st->codec->time_base.num, 90000, st->codec->time_base.den); //FIXME pass timestamps 740 av_rescale((int64_t)s->cur_timestamp * st->codec->time_base.num, 90000, st->codec->time_base.den); //FIXME pass timestamps
741 rtp_send_data(s1, s->buf, q - s->buf, (len == size)); 741 rtp_send_data(s1, s->buf, q - s->buf, (len == size));
742 742
743 buf1 += len; 743 buf1 += len;
744 size -= len; 744 size -= len;
759 len = max_packet_size; 759 len = max_packet_size;
760 if (len > size) 760 if (len > size)
761 len = size; 761 len = size;
762 762
763 /* 90 KHz time stamp */ 763 /* 90 KHz time stamp */
764 s->timestamp = s->base_timestamp + 764 s->timestamp = s->base_timestamp +
765 av_rescale((int64_t)s->cur_timestamp * st->codec->time_base.num, 90000, st->codec->time_base.den); //FIXME pass timestamps 765 av_rescale((int64_t)s->cur_timestamp * st->codec->time_base.num, 90000, st->codec->time_base.den); //FIXME pass timestamps
766 rtp_send_data(s1, buf1, len, (len == size)); 766 rtp_send_data(s1, buf1, len, (len == size));
767 767
768 buf1 += len; 768 buf1 += len;
769 size -= len; 769 size -= len;
784 len = size; 784 len = size;
785 memcpy(s->buf_ptr, buf1, len); 785 memcpy(s->buf_ptr, buf1, len);
786 buf1 += len; 786 buf1 += len;
787 size -= len; 787 size -= len;
788 s->buf_ptr += len; 788 s->buf_ptr += len;
789 789
790 out_len = s->buf_ptr - s->buf; 790 out_len = s->buf_ptr - s->buf;
791 if (out_len >= s->max_payload_size) { 791 if (out_len >= s->max_payload_size) {
792 rtp_send_data(s1, s->buf, out_len, 0); 792 rtp_send_data(s1, s->buf, out_len, 0);
793 s->buf_ptr = s->buf; 793 s->buf_ptr = s->buf;
794 } 794 }
802 AVStream *st = s1->streams[0]; 802 AVStream *st = s1->streams[0];
803 int rtcp_bytes; 803 int rtcp_bytes;
804 int64_t ntp_time; 804 int64_t ntp_time;
805 int size= pkt->size; 805 int size= pkt->size;
806 uint8_t *buf1= pkt->data; 806 uint8_t *buf1= pkt->data;
807 807
808 #ifdef DEBUG 808 #ifdef DEBUG
809 printf("%d: write len=%d\n", pkt->stream_index, size); 809 printf("%d: write len=%d\n", pkt->stream_index, size);
810 #endif 810 #endif
811 811
812 /* XXX: mpeg pts hardcoded. RTCP send every 0.5 seconds */ 812 /* XXX: mpeg pts hardcoded. RTCP send every 0.5 seconds */
813 rtcp_bytes = ((s->octet_count - s->last_octet_count) * RTCP_TX_RATIO_NUM) / 813 rtcp_bytes = ((s->octet_count - s->last_octet_count) * RTCP_TX_RATIO_NUM) /
814 RTCP_TX_RATIO_DEN; 814 RTCP_TX_RATIO_DEN;
815 if (s->first_packet || rtcp_bytes >= 28) { 815 if (s->first_packet || rtcp_bytes >= 28) {
816 /* compute NTP time */ 816 /* compute NTP time */
817 /* XXX: 90 kHz timestamp hardcoded */ 817 /* XXX: 90 kHz timestamp hardcoded */
818 ntp_time = (pkt->pts << 28) / 5625; 818 ntp_time = (pkt->pts << 28) / 5625;
819 rtcp_send_sr(s1, ntp_time); 819 rtcp_send_sr(s1, ntp_time);
820 s->last_octet_count = s->octet_count; 820 s->last_octet_count = s->octet_count;
821 s->first_packet = 0; 821 s->first_packet = 0;
822 } 822 }
823 823
824 switch(st->codec->codec_id) { 824 switch(st->codec->codec_id) {