113828475eef5145d1f284a85ef33407088c7957
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
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <netinet/in.h>
26 # include <arpa/inet.h>
28 # include "barpainet.h"
35 /* TODO: - add RTCP statistics reporting (should be optional).
37 - add support for h263/mpeg4 packetized output : IDEA: send a
38 buffer to 'rtp_write_packet' contains all the packets for ONE
39 frame. Each packet should have a four byte header containing
40 the length in big endian format (same trick as
41 'url_open_dyn_packet_buf')
46 #define RTP_MAX_SDES 256 /* maximum text length for SDES */
48 /* RTCP paquets use 0.5 % of the bandwidth */
49 #define RTCP_TX_RATIO_NUM 5
50 #define RTCP_TX_RATIO_DEN 1000
80 RTP_PT_S16BE_STEREO
= 10,
81 RTP_PT_S16BE_MONO
= 11,
82 RTP_PT_MPEGAUDIO
= 14,
85 RTP_PT_MPEGVIDEO
= 32,
87 RTP_PT_H263
= 34, /* old H263 encapsulation */
91 typedef struct RTPContext
{
96 uint32_t base_timestamp
;
97 uint32_t cur_timestamp
;
99 /* rtcp sender statistics receive */
100 int64_t last_rtcp_ntp_time
;
101 int64_t first_rtcp_ntp_time
;
102 uint32_t last_rtcp_timestamp
;
103 /* rtcp sender statistics */
104 unsigned int packet_count
;
105 unsigned int octet_count
;
106 unsigned int last_octet_count
;
108 /* buffer for output */
109 uint8_t buf
[RTP_MAX_PACKET_LENGTH
];
113 int rtp_get_codec_info(AVCodecContext
*codec
, int payload_type
)
115 switch(payload_type
) {
117 codec
->codec_id
= CODEC_ID_PCM_MULAW
;
119 codec
->sample_rate
= 8000;
122 codec
->codec_id
= CODEC_ID_PCM_ALAW
;
124 codec
->sample_rate
= 8000;
126 case RTP_PT_S16BE_STEREO
:
127 codec
->codec_id
= CODEC_ID_PCM_S16BE
;
129 codec
->sample_rate
= 44100;
131 case RTP_PT_S16BE_MONO
:
132 codec
->codec_id
= CODEC_ID_PCM_S16BE
;
134 codec
->sample_rate
= 44100;
136 case RTP_PT_MPEGAUDIO
:
137 codec
->codec_id
= CODEC_ID_MP2
;
140 codec
->codec_id
= CODEC_ID_MJPEG
;
142 case RTP_PT_MPEGVIDEO
:
143 codec
->codec_id
= CODEC_ID_MPEG1VIDEO
;
151 /* return < 0 if unknown payload type */
152 int rtp_get_payload_type(AVCodecContext
*codec
)
156 /* compute the payload type */
158 switch(codec
->codec_id
) {
159 case CODEC_ID_PCM_MULAW
:
160 payload_type
= RTP_PT_ULAW
;
162 case CODEC_ID_PCM_ALAW
:
163 payload_type
= RTP_PT_ALAW
;
165 case CODEC_ID_PCM_S16BE
:
166 if (codec
->channels
== 1) {
167 payload_type
= RTP_PT_S16BE_MONO
;
168 } else if (codec
->channels
== 2) {
169 payload_type
= RTP_PT_S16BE_STEREO
;
174 payload_type
= RTP_PT_MPEGAUDIO
;
177 payload_type
= RTP_PT_JPEG
;
179 case CODEC_ID_MPEG1VIDEO
:
180 payload_type
= RTP_PT_MPEGVIDEO
;
188 static inline uint32_t decode_be32(const uint8_t *p
)
190 return (p
[0] << 24) | (p
[1] << 16) | (p
[2] << 8) | p
[3];
193 static inline uint64_t decode_be64(const uint8_t *p
)
195 return ((uint64_t)decode_be32(p
) << 32) | decode_be32(p
+ 4);
198 static int rtcp_parse_packet(AVFormatContext
*s1
, const unsigned char *buf
, int len
)
200 RTPContext
*s
= s1
->priv_data
;
204 s
->last_rtcp_ntp_time
= decode_be64(buf
+ 8);
205 if (s
->first_rtcp_ntp_time
== AV_NOPTS_VALUE
)
206 s
->first_rtcp_ntp_time
= s
->last_rtcp_ntp_time
;
207 s
->last_rtcp_timestamp
= decode_be32(buf
+ 16);
212 * Parse an RTP packet directly sent as raw data. Can only be used if
213 * 'raw' is given as input file
214 * @param s1 media file context
215 * @param pkt returned packet
216 * @param buf input buffer
217 * @param len buffer len
218 * @return zero if no error.
220 int rtp_parse_packet(AVFormatContext
*s1
, AVPacket
*pkt
,
221 const unsigned char *buf
, int len
)
223 RTPContext
*s
= s1
->priv_data
;
224 unsigned int ssrc
, h
;
225 int payload_type
, seq
, delta_timestamp
;
232 if ((buf
[0] & 0xc0) != (RTP_VERSION
<< 6))
234 if (buf
[1] >= 200 && buf
[1] <= 204) {
235 rtcp_parse_packet(s1
, buf
, len
);
238 payload_type
= buf
[1] & 0x7f;
239 seq
= (buf
[2] << 8) | buf
[3];
240 timestamp
= decode_be32(buf
+ 4);
241 ssrc
= decode_be32(buf
+ 8);
243 if (s
->payload_type
< 0) {
244 s
->payload_type
= payload_type
;
246 if (payload_type
== RTP_PT_MPEG2TS
) {
247 /* XXX: special case : not a single codec but a whole stream */
250 st
= av_new_stream(s1
, 0);
253 rtp_get_codec_info(&st
->codec
, payload_type
);
257 /* NOTE: we can handle only one payload type */
258 if (s
->payload_type
!= payload_type
)
260 #if defined(DEBUG) || 1
261 if (seq
!= ((s
->seq
+ 1) & 0xffff)) {
262 printf("RTP: PT=%02x: bad cseq %04x expected=%04x\n",
263 payload_type
, seq
, ((s
->seq
+ 1) & 0xffff));
270 switch(st
->codec
.codec_id
) {
272 /* better than nothing: skip mpeg audio RTP header */
275 h
= decode_be32(buf
);
278 av_new_packet(pkt
, len
);
279 memcpy(pkt
->data
, buf
, len
);
281 case CODEC_ID_MPEG1VIDEO
:
282 /* better than nothing: skip mpeg audio RTP header */
285 h
= decode_be32(buf
);
295 av_new_packet(pkt
, len
);
296 memcpy(pkt
->data
, buf
, len
);
299 av_new_packet(pkt
, len
);
300 memcpy(pkt
->data
, buf
, len
);
304 switch(st
->codec
.codec_id
) {
306 case CODEC_ID_MPEG1VIDEO
:
307 if (s
->last_rtcp_ntp_time
!= AV_NOPTS_VALUE
) {
309 /* XXX: is it really necessary to unify the timestamp base ? */
310 /* compute pts from timestamp with received ntp_time */
311 delta_timestamp
= timestamp
- s
->last_rtcp_timestamp
;
312 /* convert to 90 kHz without overflow */
313 addend
= (s
->last_rtcp_ntp_time
- s
->first_rtcp_ntp_time
) >> 14;
314 addend
= (addend
* 5625) >> 14;
315 pkt
->pts
= addend
+ delta_timestamp
;
319 /* no timestamp info yet */
325 static int rtp_read_header(AVFormatContext
*s1
,
326 AVFormatParameters
*ap
)
328 RTPContext
*s
= s1
->priv_data
;
329 s
->payload_type
= -1;
330 s
->last_rtcp_ntp_time
= AV_NOPTS_VALUE
;
331 s
->first_rtcp_ntp_time
= AV_NOPTS_VALUE
;
335 static int rtp_read_packet(AVFormatContext
*s1
, AVPacket
*pkt
)
337 char buf
[RTP_MAX_PACKET_LENGTH
];
340 /* XXX: needs a better API for packet handling ? */
342 ret
= url_read(url_fileno(&s1
->pb
), buf
, sizeof(buf
));
345 if (rtp_parse_packet(s1
, pkt
, buf
, ret
) == 0)
351 static int rtp_read_close(AVFormatContext
*s1
)
353 // RTPContext *s = s1->priv_data;
357 static int rtp_probe(AVProbeData
*p
)
359 if (strstart(p
->filename
, "rtp://", NULL
))
360 return AVPROBE_SCORE_MAX
;
366 static int rtp_write_header(AVFormatContext
*s1
)
368 RTPContext
*s
= s1
->priv_data
;
369 int payload_type
, max_packet_size
;
372 if (s1
->nb_streams
!= 1)
376 payload_type
= rtp_get_payload_type(&st
->codec
);
377 if (payload_type
< 0)
378 payload_type
= RTP_PT_PRIVATE
; /* private payload type */
379 s
->payload_type
= payload_type
;
381 s
->base_timestamp
= random();
382 s
->timestamp
= s
->base_timestamp
;
386 max_packet_size
= url_fget_max_packet_size(&s1
->pb
);
387 if (max_packet_size
<= 12)
389 s
->max_payload_size
= max_packet_size
- 12;
391 switch(st
->codec
.codec_id
) {
394 s
->buf_ptr
= s
->buf
+ 4;
395 s
->cur_timestamp
= 0;
397 case CODEC_ID_MPEG1VIDEO
:
398 s
->cur_timestamp
= 0;
408 /* send an rtcp sender report packet */
409 static void rtcp_send_sr(AVFormatContext
*s1
, int64_t ntp_time
)
411 RTPContext
*s
= s1
->priv_data
;
413 printf("RTCP: %02x %Lx %x\n", s
->payload_type
, ntp_time
, s
->timestamp
);
415 put_byte(&s1
->pb
, (RTP_VERSION
<< 6));
416 put_byte(&s1
->pb
, 200);
417 put_be16(&s1
->pb
, 6); /* length in words - 1 */
418 put_be32(&s1
->pb
, s
->ssrc
);
419 put_be64(&s1
->pb
, ntp_time
);
420 put_be32(&s1
->pb
, s
->timestamp
);
421 put_be32(&s1
->pb
, s
->packet_count
);
422 put_be32(&s1
->pb
, s
->octet_count
);
423 put_flush_packet(&s1
->pb
);
426 /* send an rtp packet. sequence number is incremented, but the caller
427 must update the timestamp itself */
428 static void rtp_send_data(AVFormatContext
*s1
, const uint8_t *buf1
, int len
)
430 RTPContext
*s
= s1
->priv_data
;
433 printf("rtp_send_data size=%d\n", len
);
436 /* build the RTP header */
437 put_byte(&s1
->pb
, (RTP_VERSION
<< 6));
438 put_byte(&s1
->pb
, s
->payload_type
& 0x7f);
439 put_be16(&s1
->pb
, s
->seq
);
440 put_be32(&s1
->pb
, s
->timestamp
);
441 put_be32(&s1
->pb
, s
->ssrc
);
443 put_buffer(&s1
->pb
, buf1
, len
);
444 put_flush_packet(&s1
->pb
);
447 s
->octet_count
+= len
;
451 /* send an integer number of samples and compute time stamp and fill
452 the rtp send buffer before sending. */
453 static void rtp_send_samples(AVFormatContext
*s1
,
454 const uint8_t *buf1
, int size
, int sample_size
)
456 RTPContext
*s
= s1
->priv_data
;
457 int len
, max_packet_size
, n
;
459 max_packet_size
= (s
->max_payload_size
/ sample_size
) * sample_size
;
460 /* not needed, but who nows */
461 if ((size
% sample_size
) != 0)
464 len
= (max_packet_size
- (s
->buf_ptr
- s
->buf
));
469 memcpy(s
->buf_ptr
, buf1
, len
);
473 n
= (s
->buf_ptr
- s
->buf
);
474 /* if buffer full, then send it */
475 if (n
>= max_packet_size
) {
476 rtp_send_data(s1
, s
->buf
, n
);
478 /* update timestamp */
479 s
->timestamp
+= n
/ sample_size
;
484 /* NOTE: we suppose that exactly one frame is given as argument here */
486 static void rtp_send_mpegaudio(AVFormatContext
*s1
,
487 const uint8_t *buf1
, int size
)
489 RTPContext
*s
= s1
->priv_data
;
490 AVStream
*st
= s1
->streams
[0];
491 int len
, count
, max_packet_size
;
493 max_packet_size
= s
->max_payload_size
;
495 /* test if we must flush because not enough space */
496 len
= (s
->buf_ptr
- s
->buf
);
497 if ((len
+ size
) > max_packet_size
) {
499 rtp_send_data(s1
, s
->buf
, s
->buf_ptr
- s
->buf
);
500 s
->buf_ptr
= s
->buf
+ 4;
501 /* 90 KHz time stamp */
502 s
->timestamp
= s
->base_timestamp
+
503 (s
->cur_timestamp
* 90000LL) / st
->codec
.sample_rate
;
508 if (size
> max_packet_size
) {
509 /* big packet: fragment */
512 len
= max_packet_size
- 4;
515 /* build fragmented packet */
518 s
->buf
[2] = count
>> 8;
520 memcpy(s
->buf
+ 4, buf1
, len
);
521 rtp_send_data(s1
, s
->buf
, len
+ 4);
527 if (s
->buf_ptr
== s
->buf
+ 4) {
528 /* no fragmentation possible */
534 memcpy(s
->buf_ptr
, buf1
, size
);
537 s
->cur_timestamp
+= st
->codec
.frame_size
;
540 /* NOTE: a single frame must be passed with sequence header if
541 needed. XXX: use slices. */
542 static void rtp_send_mpegvideo(AVFormatContext
*s1
,
543 const uint8_t *buf1
, int size
)
545 RTPContext
*s
= s1
->priv_data
;
546 AVStream
*st
= s1
->streams
[0];
547 int len
, h
, max_packet_size
;
550 max_packet_size
= s
->max_payload_size
;
553 /* XXX: more correct headers */
555 if (st
->codec
.sub_id
== 2)
556 h
|= 1 << 26; /* mpeg 2 indicator */
563 if (st
->codec
.sub_id
== 2) {
571 len
= max_packet_size
- (q
- s
->buf
);
575 memcpy(q
, buf1
, len
);
578 /* 90 KHz time stamp */
579 s
->timestamp
= s
->base_timestamp
+
580 av_rescale((int64_t)s
->cur_timestamp
* st
->codec
.frame_rate_base
, 90000, st
->codec
.frame_rate
);
581 rtp_send_data(s1
, s
->buf
, q
- s
->buf
);
589 static void rtp_send_raw(AVFormatContext
*s1
,
590 const uint8_t *buf1
, int size
)
592 RTPContext
*s
= s1
->priv_data
;
593 AVStream
*st
= s1
->streams
[0];
594 int len
, max_packet_size
;
596 max_packet_size
= s
->max_payload_size
;
599 len
= max_packet_size
;
603 /* 90 KHz time stamp */
604 s
->timestamp
= s
->base_timestamp
+
605 av_rescale((int64_t)s
->cur_timestamp
* st
->codec
.frame_rate_base
, 90000, st
->codec
.frame_rate
);
606 rtp_send_data(s1
, buf1
, len
);
614 /* write an RTP packet. 'buf1' must contain a single specific frame. */
615 static int rtp_write_packet(AVFormatContext
*s1
, int stream_index
,
616 const uint8_t *buf1
, int size
, int64_t pts
)
618 RTPContext
*s
= s1
->priv_data
;
619 AVStream
*st
= s1
->streams
[0];
624 printf("%d: write len=%d\n", stream_index
, size
);
627 /* XXX: mpeg pts hardcoded. RTCP send every 0.5 seconds */
628 rtcp_bytes
= ((s
->octet_count
- s
->last_octet_count
) * RTCP_TX_RATIO_NUM
) /
630 if (s
->first_packet
|| rtcp_bytes
>= 28) {
631 /* compute NTP time */
632 /* XXX: 90 kHz timestamp hardcoded */
633 ntp_time
= (pts
<< 28) / 5625;
634 rtcp_send_sr(s1
, ntp_time
);
635 s
->last_octet_count
= s
->octet_count
;
639 switch(st
->codec
.codec_id
) {
640 case CODEC_ID_PCM_MULAW
:
641 case CODEC_ID_PCM_ALAW
:
642 case CODEC_ID_PCM_U8
:
643 case CODEC_ID_PCM_S8
:
644 rtp_send_samples(s1
, buf1
, size
, 1 * st
->codec
.channels
);
646 case CODEC_ID_PCM_U16BE
:
647 case CODEC_ID_PCM_U16LE
:
648 case CODEC_ID_PCM_S16BE
:
649 case CODEC_ID_PCM_S16LE
:
650 rtp_send_samples(s1
, buf1
, size
, 2 * st
->codec
.channels
);
654 rtp_send_mpegaudio(s1
, buf1
, size
);
656 case CODEC_ID_MPEG1VIDEO
:
657 rtp_send_mpegvideo(s1
, buf1
, size
);
660 /* better than nothing : send the codec raw data */
661 rtp_send_raw(s1
, buf1
, size
);
667 static int rtp_write_trailer(AVFormatContext
*s1
)
669 // RTPContext *s = s1->priv_data;
673 AVInputFormat rtp_demux
= {
681 .flags
= AVFMT_NOHEADER
,
684 AVOutputFormat rtp_mux
= {
699 av_register_output_format(&rtp_mux
);
700 av_register_input_format(&rtp_demux
);