2 * RTP input/output format
3 * Copyright (c) 2002 Fabrice Bellard.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <netinet/in.h>
27 # include <arpa/inet.h>
29 # include "barpainet.h"
36 /* TODO: - add RTCP statistics reporting (should be optional).
38 - add support for h263/mpeg4 packetized output : IDEA: send a
39 buffer to 'rtp_write_packet' contains all the packets for ONE
40 frame. Each packet should have a four byte header containing
41 the length in big endian format (same trick as
42 'url_open_dyn_packet_buf')
47 #define RTP_MAX_SDES 256 /* maximum text length for SDES */
49 /* RTCP paquets use 0.5 % of the bandwidth */
50 #define RTCP_TX_RATIO_NUM 5
51 #define RTCP_TX_RATIO_DEN 1000
76 struct RTPDemuxContext {
83 uint32_t base_timestamp;
84 uint32_t cur_timestamp;
86 MpegTSContext *ts; /* only used for RTP_PT_MPEG2TS payloads */
90 /* rtcp sender statistics receive */
91 int64_t last_rtcp_ntp_time;
92 int64_t first_rtcp_ntp_time;
93 uint32_t last_rtcp_timestamp;
94 /* rtcp sender statistics */
95 unsigned int packet_count;
96 unsigned int octet_count;
97 unsigned int last_octet_count;
99 /* buffer for output */
100 uint8_t buf[RTP_MAX_PACKET_LENGTH];
104 int rtp_get_codec_info(AVCodecContext *codec, int payload_type)
106 switch(payload_type) {
108 codec->codec_type = CODEC_TYPE_AUDIO;
109 codec->codec_id = CODEC_ID_PCM_MULAW;
111 codec->sample_rate = 8000;
114 codec->codec_type = CODEC_TYPE_AUDIO;
115 codec->codec_id = CODEC_ID_PCM_ALAW;
117 codec->sample_rate = 8000;
119 case RTP_PT_S16BE_STEREO:
120 codec->codec_type = CODEC_TYPE_AUDIO;
121 codec->codec_id = CODEC_ID_PCM_S16BE;
123 codec->sample_rate = 44100;
125 case RTP_PT_S16BE_MONO:
126 codec->codec_type = CODEC_TYPE_AUDIO;
127 codec->codec_id = CODEC_ID_PCM_S16BE;
129 codec->sample_rate = 44100;
131 case RTP_PT_MPEGAUDIO:
132 codec->codec_type = CODEC_TYPE_AUDIO;
133 codec->codec_id = CODEC_ID_MP2;
136 codec->codec_type = CODEC_TYPE_VIDEO;
137 codec->codec_id = CODEC_ID_MJPEG;
139 case RTP_PT_MPEGVIDEO:
140 codec->codec_type = CODEC_TYPE_VIDEO;
141 codec->codec_id = CODEC_ID_MPEG1VIDEO;
144 codec->codec_type = CODEC_TYPE_DATA;
145 codec->codec_id = CODEC_ID_MPEG2TS;
153 /* return < 0 if unknown payload type */
154 int rtp_get_payload_type(AVCodecContext *codec)
158 /* compute the payload type */
160 switch(codec->codec_id) {
161 case CODEC_ID_PCM_MULAW:
162 payload_type = RTP_PT_ULAW;
164 case CODEC_ID_PCM_ALAW:
165 payload_type = RTP_PT_ALAW;
167 case CODEC_ID_PCM_S16BE:
168 if (codec->channels == 1) {
169 payload_type = RTP_PT_S16BE_MONO;
170 } else if (codec->channels == 2) {
171 payload_type = RTP_PT_S16BE_STEREO;
176 payload_type = RTP_PT_MPEGAUDIO;
179 payload_type = RTP_PT_JPEG;
181 case CODEC_ID_MPEG1VIDEO:
182 payload_type = RTP_PT_MPEGVIDEO;
184 case CODEC_ID_MPEG2TS:
185 payload_type = RTP_PT_MPEG2TS;
193 static inline uint32_t decode_be32(const uint8_t *p)
195 return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
198 static inline uint64_t decode_be64(const uint8_t *p)
200 return ((uint64_t)decode_be32(p) << 32) | decode_be32(p + 4);
203 static int rtcp_parse_packet(RTPDemuxContext *s, const unsigned char *buf, int len)
207 s->last_rtcp_ntp_time = decode_be64(buf + 8);
208 if (s->first_rtcp_ntp_time == AV_NOPTS_VALUE)
209 s->first_rtcp_ntp_time = s->last_rtcp_ntp_time;
210 s->last_rtcp_timestamp = decode_be32(buf + 16);
215 * open a new RTP parse context for stream 'st'. 'st' can be NULL for
216 * MPEG2TS streams to indicate that they should be demuxed inside the
217 * rtp demux (otherwise CODEC_ID_MPEG2TS packets are returned)
219 RTPDemuxContext *rtp_parse_open(AVFormatContext *s1, AVStream *st, int payload_type)
223 s = av_mallocz(sizeof(RTPDemuxContext));
226 s->payload_type = payload_type;
227 s->last_rtcp_ntp_time = AV_NOPTS_VALUE;
228 s->first_rtcp_ntp_time = AV_NOPTS_VALUE;
231 if (payload_type == RTP_PT_MPEG2TS) {
232 s->ts = mpegts_parse_open(s->ic);
238 switch(st->codec.codec_id) {
239 case CODEC_ID_MPEG1VIDEO:
240 case CODEC_ID_MPEG2VIDEO:
244 st->need_parsing = 1;
254 * Parse an RTP or RTCP packet directly sent as a buffer.
255 * @param s RTP parse context.
256 * @param pkt returned packet
257 * @param buf input buffer or NULL to read the next packets
258 * @param len buffer len
259 * @return 0 if a packet is returned, 1 if a packet is returned and more can follow
260 * (use buf as NULL to read the next). -1 if no packet (error or no more packet).
262 int rtp_parse_packet(RTPDemuxContext *s, AVPacket *pkt,
263 const uint8_t *buf, int len)
265 unsigned int ssrc, h;
266 int payload_type, seq, delta_timestamp, ret;
271 /* return the next packets, if any */
272 if (s->read_buf_index >= s->read_buf_size)
274 ret = mpegts_parse_packet(s->ts, pkt, s->buf + s->read_buf_index,
275 s->read_buf_size - s->read_buf_index);
278 s->read_buf_index += ret;
279 if (s->read_buf_index < s->read_buf_size)
288 if ((buf[0] & 0xc0) != (RTP_VERSION << 6))
290 if (buf[1] >= 200 && buf[1] <= 204) {
291 rtcp_parse_packet(s, buf, len);
294 payload_type = buf[1] & 0x7f;
295 seq = (buf[2] << 8) | buf[3];
296 timestamp = decode_be32(buf + 4);
297 ssrc = decode_be32(buf + 8);
299 /* NOTE: we can handle only one payload type */
300 if (s->payload_type != payload_type)
302 #if defined(DEBUG) || 1
303 if (seq != ((s->seq + 1) & 0xffff)) {
304 av_log(&s->st->codec, AV_LOG_ERROR, "RTP: PT=%02x: bad cseq %04x expected=%04x\n",
305 payload_type, seq, ((s->seq + 1) & 0xffff));
314 /* specific MPEG2TS demux support */
315 ret = mpegts_parse_packet(s->ts, pkt, buf, len);
319 s->read_buf_size = len - ret;
320 memcpy(s->buf, buf + ret, s->read_buf_size);
321 s->read_buf_index = 0;
325 switch(st->codec.codec_id) {
327 /* better than nothing: skip mpeg audio RTP header */
330 h = decode_be32(buf);
333 av_new_packet(pkt, len);
334 memcpy(pkt->data, buf, len);
336 case CODEC_ID_MPEG1VIDEO:
337 /* better than nothing: skip mpeg video RTP header */
340 h = decode_be32(buf);
350 av_new_packet(pkt, len);
351 memcpy(pkt->data, buf, len);
354 av_new_packet(pkt, len);
355 memcpy(pkt->data, buf, len);
359 switch(st->codec.codec_id) {
361 case CODEC_ID_MPEG1VIDEO:
362 if (s->last_rtcp_ntp_time != AV_NOPTS_VALUE) {
364 /* XXX: is it really necessary to unify the timestamp base ? */
365 /* compute pts from timestamp with received ntp_time */
366 delta_timestamp = timestamp - s->last_rtcp_timestamp;
367 /* convert to 90 kHz without overflow */
368 addend = (s->last_rtcp_ntp_time - s->first_rtcp_ntp_time) >> 14;
369 addend = (addend * 5625) >> 14;
370 pkt->pts = addend + delta_timestamp;
374 /* no timestamp info yet */
377 pkt->stream_index = s->st->index;
382 void rtp_parse_close(RTPDemuxContext *s)
384 if (s->payload_type == RTP_PT_MPEG2TS) {
385 mpegts_parse_close(s->ts);
392 static int rtp_write_header(AVFormatContext *s1)
394 RTPDemuxContext *s = s1->priv_data;
395 int payload_type, max_packet_size, n;
398 if (s1->nb_streams != 1)
402 payload_type = rtp_get_payload_type(&st->codec);
403 if (payload_type < 0)
404 payload_type = RTP_PT_PRIVATE; /* private payload type */
405 s->payload_type = payload_type;
407 s->base_timestamp = random();
408 s->timestamp = s->base_timestamp;
412 max_packet_size = url_fget_max_packet_size(&s1->pb);
413 if (max_packet_size <= 12)
415 s->max_payload_size = max_packet_size - 12;
417 switch(st->codec.codec_id) {
420 s->buf_ptr = s->buf + 4;
421 s->cur_timestamp = 0;
423 case CODEC_ID_MPEG1VIDEO:
424 s->cur_timestamp = 0;
426 case CODEC_ID_MPEG2TS:
427 n = s->max_payload_size / TS_PACKET_SIZE;
430 s->max_payload_size = n * TS_PACKET_SIZE;
441 /* send an rtcp sender report packet */
442 static void rtcp_send_sr(AVFormatContext *s1, int64_t ntp_time)
444 RTPDemuxContext *s = s1->priv_data;
446 printf("RTCP: %02x %Lx %x\n", s->payload_type, ntp_time, s->timestamp);
448 put_byte(&s1->pb, (RTP_VERSION << 6));
449 put_byte(&s1->pb, 200);
450 put_be16(&s1->pb, 6); /* length in words - 1 */
451 put_be32(&s1->pb, s->ssrc);
452 put_be64(&s1->pb, ntp_time);
453 put_be32(&s1->pb, s->timestamp);
454 put_be32(&s1->pb, s->packet_count);
455 put_be32(&s1->pb, s->octet_count);
456 put_flush_packet(&s1->pb);
459 /* send an rtp packet. sequence number is incremented, but the caller
460 must update the timestamp itself */
461 static void rtp_send_data(AVFormatContext *s1, const uint8_t *buf1, int len)
463 RTPDemuxContext *s = s1->priv_data;
466 printf("rtp_send_data size=%d\n", len);
469 /* build the RTP header */
470 put_byte(&s1->pb, (RTP_VERSION << 6));
471 put_byte(&s1->pb, s->payload_type & 0x7f);
472 put_be16(&s1->pb, s->seq);
473 put_be32(&s1->pb, s->timestamp);
474 put_be32(&s1->pb, s->ssrc);
476 put_buffer(&s1->pb, buf1, len);
477 put_flush_packet(&s1->pb);
480 s->octet_count += len;
484 /* send an integer number of samples and compute time stamp and fill
485 the rtp send buffer before sending. */
486 static void rtp_send_samples(AVFormatContext *s1,
487 const uint8_t *buf1, int size, int sample_size)
489 RTPDemuxContext *s = s1->priv_data;
490 int len, max_packet_size, n;
492 max_packet_size = (s->max_payload_size / sample_size) * sample_size;
493 /* not needed, but who nows */
494 if ((size % sample_size) != 0)
497 len = (max_packet_size - (s->buf_ptr - s->buf));
502 memcpy(s->buf_ptr, buf1, len);
506 n = (s->buf_ptr - s->buf);
507 /* if buffer full, then send it */
508 if (n >= max_packet_size) {
509 rtp_send_data(s1, s->buf, n);
511 /* update timestamp */
512 s->timestamp += n / sample_size;
517 /* NOTE: we suppose that exactly one frame is given as argument here */
519 static void rtp_send_mpegaudio(AVFormatContext *s1,
520 const uint8_t *buf1, int size)
522 RTPDemuxContext *s = s1->priv_data;
523 AVStream *st = s1->streams[0];
524 int len, count, max_packet_size;
526 max_packet_size = s->max_payload_size;
528 /* test if we must flush because not enough space */
529 len = (s->buf_ptr - s->buf);
530 if ((len + size) > max_packet_size) {
532 rtp_send_data(s1, s->buf, s->buf_ptr - s->buf);
533 s->buf_ptr = s->buf + 4;
534 /* 90 KHz time stamp */
535 s->timestamp = s->base_timestamp +
536 (s->cur_timestamp * 90000LL) / st->codec.sample_rate;
541 if (size > max_packet_size) {
542 /* big packet: fragment */
545 len = max_packet_size - 4;
548 /* build fragmented packet */
551 s->buf[2] = count >> 8;
553 memcpy(s->buf + 4, buf1, len);
554 rtp_send_data(s1, s->buf, len + 4);
560 if (s->buf_ptr == s->buf + 4) {
561 /* no fragmentation possible */
567 memcpy(s->buf_ptr, buf1, size);
570 s->cur_timestamp += st->codec.frame_size;
573 /* NOTE: a single frame must be passed with sequence header if
574 needed. XXX: use slices. */
575 static void rtp_send_mpegvideo(AVFormatContext *s1,
576 const uint8_t *buf1, int size)
578 RTPDemuxContext *s = s1->priv_data;
579 AVStream *st = s1->streams[0];
580 int len, h, max_packet_size;
583 max_packet_size = s->max_payload_size;
586 /* XXX: more correct headers */
588 if (st->codec.sub_id == 2)
589 h |= 1 << 26; /* mpeg 2 indicator */
596 if (st->codec.sub_id == 2) {
604 len = max_packet_size - (q - s->buf);
608 memcpy(q, buf1, len);
611 /* 90 KHz time stamp */
612 s->timestamp = s->base_timestamp +
613 av_rescale((int64_t)s->cur_timestamp * st->codec.frame_rate_base, 90000, st->codec.frame_rate);
614 rtp_send_data(s1, s->buf, q - s->buf);
622 static void rtp_send_raw(AVFormatContext *s1,
623 const uint8_t *buf1, int size)
625 RTPDemuxContext *s = s1->priv_data;
626 AVStream *st = s1->streams[0];
627 int len, max_packet_size;
629 max_packet_size = s->max_payload_size;
632 len = max_packet_size;
636 /* 90 KHz time stamp */
637 s->timestamp = s->base_timestamp +
638 av_rescale((int64_t)s->cur_timestamp * st->codec.frame_rate_base, 90000, st->codec.frame_rate);
639 rtp_send_data(s1, buf1, len);
647 /* NOTE: size is assumed to be an integer multiple of TS_PACKET_SIZE */
648 static void rtp_send_mpegts_raw(AVFormatContext *s1,
649 const uint8_t *buf1, int size)
651 RTPDemuxContext *s = s1->priv_data;
654 while (size >= TS_PACKET_SIZE) {
655 len = s->max_payload_size - (s->buf_ptr - s->buf);
658 memcpy(s->buf_ptr, buf1, len);
663 out_len = s->buf_ptr - s->buf;
664 if (out_len >= s->max_payload_size) {
665 rtp_send_data(s1, s->buf, out_len);
671 /* write an RTP packet. 'buf1' must contain a single specific frame. */
672 static int rtp_write_packet(AVFormatContext *s1, int stream_index,
673 const uint8_t *buf1, int size, int64_t pts)
675 RTPDemuxContext *s = s1->priv_data;
676 AVStream *st = s1->streams[0];
681 printf("%d: write len=%d\n", stream_index, size);
684 /* XXX: mpeg pts hardcoded. RTCP send every 0.5 seconds */
685 rtcp_bytes = ((s->octet_count - s->last_octet_count) * RTCP_TX_RATIO_NUM) /
687 if (s->first_packet || rtcp_bytes >= 28) {
688 /* compute NTP time */
689 /* XXX: 90 kHz timestamp hardcoded */
690 ntp_time = (pts << 28) / 5625;
691 rtcp_send_sr(s1, ntp_time);
692 s->last_octet_count = s->octet_count;
696 switch(st->codec.codec_id) {
697 case CODEC_ID_PCM_MULAW:
698 case CODEC_ID_PCM_ALAW:
699 case CODEC_ID_PCM_U8:
700 case CODEC_ID_PCM_S8:
701 rtp_send_samples(s1, buf1, size, 1 * st->codec.channels);
703 case CODEC_ID_PCM_U16BE:
704 case CODEC_ID_PCM_U16LE:
705 case CODEC_ID_PCM_S16BE:
706 case CODEC_ID_PCM_S16LE:
707 rtp_send_samples(s1, buf1, size, 2 * st->codec.channels);
711 rtp_send_mpegaudio(s1, buf1, size);
713 case CODEC_ID_MPEG1VIDEO:
714 rtp_send_mpegvideo(s1, buf1, size);
716 case CODEC_ID_MPEG2TS:
717 rtp_send_mpegts_raw(s1, buf1, size);
720 /* better than nothing : send the codec raw data */
721 rtp_send_raw(s1, buf1, size);
727 static int rtp_write_trailer(AVFormatContext *s1)
729 // RTPDemuxContext *s = s1->priv_data;
733 AVOutputFormat rtp_mux = {
738 sizeof(RTPDemuxContext),
748 av_register_output_format(&rtp_mux);