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>
25 #include <arpa/inet.h>
31 /* TODO: - add RTCP statistics reporting (should be optional).
33 - add support for h263/mpeg4 packetized output : IDEA: send a
34 buffer to 'rtp_write_packet' contains all the packets for ONE
35 frame. Each packet should have a four byte header containing
36 the length in big endian format (same trick as
37 'url_open_dyn_packet_buf')
42 #define RTP_MAX_SDES 256 /* maximum text length for SDES */
44 /* RTCP paquets use 0.5 % of the bandwidth */
45 #define RTCP_TX_RATIO_NUM 5
46 #define RTCP_TX_RATIO_DEN 1000
76 RTP_PT_S16BE_STEREO
= 10,
77 RTP_PT_S16BE_MONO
= 11,
78 RTP_PT_MPEGAUDIO
= 14,
81 RTP_PT_MPEGVIDEO
= 32,
83 RTP_PT_H263
= 34, /* old H263 encapsulation */
86 typedef struct RTPContext
{
91 UINT32 base_timestamp
;
94 /* rtcp sender statistics receive */
95 INT64 last_rtcp_ntp_time
;
96 UINT32 last_rtcp_timestamp
;
97 /* rtcp sender statistics */
98 unsigned int packet_count
;
99 unsigned int octet_count
;
100 unsigned int last_octet_count
;
102 /* buffer for output */
103 UINT8 buf
[RTP_MAX_PACKET_LENGTH
];
107 int rtp_get_codec_info(AVCodecContext
*codec
, int payload_type
)
109 switch(payload_type
) {
111 codec
->codec_id
= CODEC_ID_PCM_MULAW
;
113 codec
->sample_rate
= 8000;
116 codec
->codec_id
= CODEC_ID_PCM_ALAW
;
118 codec
->sample_rate
= 8000;
120 case RTP_PT_S16BE_STEREO
:
121 codec
->codec_id
= CODEC_ID_PCM_S16BE
;
123 codec
->sample_rate
= 44100;
125 case RTP_PT_S16BE_MONO
:
126 codec
->codec_id
= CODEC_ID_PCM_S16BE
;
128 codec
->sample_rate
= 44100;
130 case RTP_PT_MPEGAUDIO
:
131 codec
->codec_id
= CODEC_ID_MP2
;
134 codec
->codec_id
= CODEC_ID_MJPEG
;
136 case RTP_PT_MPEGVIDEO
:
137 codec
->codec_id
= CODEC_ID_MPEG1VIDEO
;
145 /* return < 0 if unknown payload type */
146 int rtp_get_payload_type(AVCodecContext
*codec
)
150 /* compute the payload type */
152 switch(codec
->codec_id
) {
153 case CODEC_ID_PCM_MULAW
:
154 payload_type
= RTP_PT_ULAW
;
156 case CODEC_ID_PCM_ALAW
:
157 payload_type
= RTP_PT_ALAW
;
159 case CODEC_ID_PCM_S16BE
:
160 if (codec
->channels
== 1) {
161 payload_type
= RTP_PT_S16BE_MONO
;
162 } else if (codec
->channels
== 2) {
163 payload_type
= RTP_PT_S16BE_STEREO
;
167 case CODEC_ID_MP3LAME
:
168 payload_type
= RTP_PT_MPEGAUDIO
;
171 payload_type
= RTP_PT_JPEG
;
173 case CODEC_ID_MPEG1VIDEO
:
174 payload_type
= RTP_PT_MPEGVIDEO
;
182 static inline UINT32
decode_be32(const UINT8
*p
)
184 return (p
[0] << 24) | (p
[1] << 16) | (p
[2] << 8) | p
[3];
187 static inline UINT32
decode_be64(const UINT8
*p
)
189 return ((UINT64
)decode_be32(p
) << 32) | decode_be32(p
+ 4);
192 static int rtcp_parse_packet(AVFormatContext
*s1
, const unsigned char *buf
, int len
)
194 RTPContext
*s
= s1
->priv_data
;
198 s
->last_rtcp_ntp_time
= decode_be64(buf
+ 8);
199 s
->last_rtcp_timestamp
= decode_be32(buf
+ 16);
204 * Parse an RTP packet directly sent as raw data. Can only be used if
205 * 'raw' is given as input file
206 * @param s1 media file context
207 * @param pkt returned packet
208 * @param buf input buffer
209 * @param len buffer len
210 * @return zero if no error.
212 int rtp_parse_packet(AVFormatContext
*s1
, AVPacket
*pkt
,
213 const unsigned char *buf
, int len
)
215 RTPContext
*s
= s1
->priv_data
;
216 unsigned int ssrc
, h
;
217 int payload_type
, seq
, delta_timestamp
;
224 if ((buf
[0] & 0xc0) != (RTP_VERSION
<< 6))
226 if (buf
[1] >= 200 && buf
[1] <= 204) {
227 rtcp_parse_packet(s1
, buf
, len
);
230 payload_type
= buf
[1] & 0x7f;
231 seq
= (buf
[2] << 8) | buf
[3];
232 timestamp
= decode_be32(buf
+ 4);
233 ssrc
= decode_be32(buf
+ 8);
235 if (s
->payload_type
< 0) {
236 s
->payload_type
= payload_type
;
238 if (payload_type
== RTP_PT_MPEG2TS
) {
239 /* XXX: special case : not a single codec but a whole stream */
242 st
= av_new_stream(s1
, 0);
245 rtp_get_codec_info(&st
->codec
, payload_type
);
249 /* NOTE: we can handle only one payload type */
250 if (s
->payload_type
!= payload_type
)
252 #if defined(DEBUG) || 1
253 if (seq
!= ((s
->seq
+ 1) & 0xffff)) {
254 printf("RTP: PT=%02x: bad cseq %04x expected=%04x\n",
255 payload_type
, seq
, ((s
->seq
+ 1) & 0xffff));
262 switch(st
->codec
.codec_id
) {
264 /* better than nothing: skip mpeg audio RTP header */
267 h
= decode_be32(buf
);
270 av_new_packet(pkt
, len
);
271 memcpy(pkt
->data
, buf
, len
);
273 case CODEC_ID_MPEG1VIDEO
:
274 /* better than nothing: skip mpeg audio RTP header */
277 h
= decode_be32(buf
);
287 av_new_packet(pkt
, len
);
288 memcpy(pkt
->data
, buf
, len
);
291 av_new_packet(pkt
, len
);
292 memcpy(pkt
->data
, buf
, len
);
296 if (s
->last_rtcp_ntp_time
!= AV_NOPTS_VALUE
) {
297 /* compute pts from timestamp with received ntp_time */
298 delta_timestamp
= timestamp
- s
->last_rtcp_timestamp
;
299 /* XXX: do conversion, but not needed for mpeg at 90 KhZ */
300 pkt
->pts
= s
->last_rtcp_ntp_time
+ delta_timestamp
;
305 static int rtp_read_header(AVFormatContext
*s1
,
306 AVFormatParameters
*ap
)
308 RTPContext
*s
= s1
->priv_data
;
309 s
->payload_type
= -1;
310 s
->last_rtcp_ntp_time
= AV_NOPTS_VALUE
;
314 static int rtp_read_packet(AVFormatContext
*s1
, AVPacket
*pkt
)
316 char buf
[RTP_MAX_PACKET_LENGTH
];
319 /* XXX: needs a better API for packet handling ? */
321 ret
= url_read(url_fileno(&s1
->pb
), buf
, sizeof(buf
));
324 if (rtp_parse_packet(s1
, pkt
, buf
, ret
) == 0)
330 static int rtp_read_close(AVFormatContext
*s1
)
332 // RTPContext *s = s1->priv_data;
336 static int rtp_probe(AVProbeData
*p
)
338 if (strstart(p
->filename
, "rtp://", NULL
))
339 return AVPROBE_SCORE_MAX
;
345 static int rtp_write_header(AVFormatContext
*s1
)
347 RTPContext
*s
= s1
->priv_data
;
348 int payload_type
, max_packet_size
;
351 if (s1
->nb_streams
!= 1)
355 payload_type
= rtp_get_payload_type(&st
->codec
);
356 if (payload_type
< 0)
358 s
->payload_type
= payload_type
;
360 s
->base_timestamp
= random();
361 s
->timestamp
= s
->base_timestamp
;
365 max_packet_size
= url_fget_max_packet_size(&s1
->pb
);
366 if (max_packet_size
<= 12)
368 s
->max_payload_size
= max_packet_size
- 12;
370 switch(st
->codec
.codec_id
) {
372 case CODEC_ID_MP3LAME
:
373 s
->buf_ptr
= s
->buf
+ 4;
374 s
->cur_timestamp
= 0;
376 case CODEC_ID_MPEG1VIDEO
:
377 s
->cur_timestamp
= 0;
387 /* send an rtcp sender report packet */
388 static void rtcp_send_sr(AVFormatContext
*s1
, INT64 ntp_time
)
390 RTPContext
*s
= s1
->priv_data
;
392 printf("RTCP: %02x %Lx %x\n", s
->payload_type
, ntp_time
, s
->timestamp
);
394 put_byte(&s1
->pb
, (RTP_VERSION
<< 6));
395 put_byte(&s1
->pb
, 200);
396 put_be16(&s1
->pb
, 6); /* length in words - 1 */
397 put_be32(&s1
->pb
, s
->ssrc
);
398 put_be64(&s1
->pb
, ntp_time
);
399 put_be32(&s1
->pb
, s
->timestamp
);
400 put_be32(&s1
->pb
, s
->packet_count
);
401 put_be32(&s1
->pb
, s
->octet_count
);
402 put_flush_packet(&s1
->pb
);
405 /* send an rtp packet. sequence number is incremented, but the caller
406 must update the timestamp itself */
407 static void rtp_send_data(AVFormatContext
*s1
, UINT8
*buf1
, int len
)
409 RTPContext
*s
= s1
->priv_data
;
412 printf("rtp_send_data size=%d\n", len
);
415 /* build the RTP header */
416 put_byte(&s1
->pb
, (RTP_VERSION
<< 6));
417 put_byte(&s1
->pb
, s
->payload_type
& 0x7f);
418 put_be16(&s1
->pb
, s
->seq
);
419 put_be32(&s1
->pb
, s
->timestamp
);
420 put_be32(&s1
->pb
, s
->ssrc
);
422 put_buffer(&s1
->pb
, buf1
, len
);
423 put_flush_packet(&s1
->pb
);
426 s
->octet_count
+= len
;
430 /* send an integer number of samples and compute time stamp and fill
431 the rtp send buffer before sending. */
432 static void rtp_send_samples(AVFormatContext
*s1
,
433 UINT8
*buf1
, int size
, int sample_size
)
435 RTPContext
*s
= s1
->priv_data
;
436 int len
, max_packet_size
, n
;
438 max_packet_size
= (s
->max_payload_size
/ sample_size
) * sample_size
;
439 /* not needed, but who nows */
440 if ((size
% sample_size
) != 0)
443 len
= (max_packet_size
- (s
->buf_ptr
- s
->buf
));
448 memcpy(s
->buf_ptr
, buf1
, len
);
452 n
= (s
->buf_ptr
- s
->buf
);
453 /* if buffer full, then send it */
454 if (n
>= max_packet_size
) {
455 rtp_send_data(s1
, s
->buf
, n
);
457 /* update timestamp */
458 s
->timestamp
+= n
/ sample_size
;
463 /* NOTE: we suppose that exactly one frame is given as argument here */
465 static void rtp_send_mpegaudio(AVFormatContext
*s1
,
466 UINT8
*buf1
, int size
)
468 RTPContext
*s
= s1
->priv_data
;
469 AVStream
*st
= s1
->streams
[0];
470 int len
, count
, max_packet_size
;
472 max_packet_size
= s
->max_payload_size
;
474 /* test if we must flush because not enough space */
475 len
= (s
->buf_ptr
- s
->buf
);
476 if ((len
+ size
) > max_packet_size
) {
478 rtp_send_data(s1
, s
->buf
, s
->buf_ptr
- s
->buf
);
479 s
->buf_ptr
= s
->buf
+ 4;
480 /* 90 KHz time stamp */
481 s
->timestamp
= s
->base_timestamp
+
482 (s
->cur_timestamp
* 90000LL) / st
->codec
.sample_rate
;
487 if (size
> max_packet_size
) {
488 /* big packet: fragment */
491 len
= max_packet_size
- 4;
494 /* build fragmented packet */
497 s
->buf
[2] = count
>> 8;
499 memcpy(s
->buf
+ 4, buf1
, len
);
500 rtp_send_data(s1
, s
->buf
, len
+ 4);
506 if (s
->buf_ptr
== s
->buf
+ 4) {
507 /* no fragmentation possible */
513 memcpy(s
->buf_ptr
, buf1
, size
);
516 s
->cur_timestamp
+= st
->codec
.frame_size
;
519 /* NOTE: a single frame must be passed with sequence header if
520 needed. XXX: use slices. */
521 static void rtp_send_mpegvideo(AVFormatContext
*s1
,
522 UINT8
*buf1
, int size
)
524 RTPContext
*s
= s1
->priv_data
;
525 AVStream
*st
= s1
->streams
[0];
526 int len
, h
, max_packet_size
;
529 max_packet_size
= s
->max_payload_size
;
532 /* XXX: more correct headers */
534 if (st
->codec
.sub_id
== 2)
535 h
|= 1 << 26; /* mpeg 2 indicator */
542 if (st
->codec
.sub_id
== 2) {
550 len
= max_packet_size
- (q
- s
->buf
);
554 memcpy(q
, buf1
, len
);
557 /* 90 KHz time stamp */
559 s
->timestamp
= s
->base_timestamp
+
560 (s
->cur_timestamp
* 90000LL * FRAME_RATE_BASE
) / st
->codec
.frame_rate
;
561 rtp_send_data(s1
, s
->buf
, q
- s
->buf
);
569 /* write an RTP packet. 'buf1' must contain a single specific frame. */
570 static int rtp_write_packet(AVFormatContext
*s1
, int stream_index
,
571 UINT8
*buf1
, int size
, int force_pts
)
573 RTPContext
*s
= s1
->priv_data
;
574 AVStream
*st
= s1
->streams
[0];
579 printf("%d: write len=%d\n", stream_index
, size
);
582 /* XXX: mpeg pts hardcoded. RTCP send every 0.5 seconds */
583 rtcp_bytes
= ((s
->octet_count
- s
->last_octet_count
) * RTCP_TX_RATIO_NUM
) /
585 if (s
->first_packet
|| rtcp_bytes
>= 28) {
586 /* compute NTP time */
587 ntp_time
= force_pts
; // ((INT64)force_pts << 28) / 5625
588 rtcp_send_sr(s1
, ntp_time
);
589 s
->last_octet_count
= s
->octet_count
;
593 switch(st
->codec
.codec_id
) {
594 case CODEC_ID_PCM_MULAW
:
595 case CODEC_ID_PCM_ALAW
:
596 case CODEC_ID_PCM_U8
:
597 case CODEC_ID_PCM_S8
:
598 rtp_send_samples(s1
, buf1
, size
, 1 * st
->codec
.channels
);
600 case CODEC_ID_PCM_U16BE
:
601 case CODEC_ID_PCM_U16LE
:
602 case CODEC_ID_PCM_S16BE
:
603 case CODEC_ID_PCM_S16LE
:
604 rtp_send_samples(s1
, buf1
, size
, 2 * st
->codec
.channels
);
607 case CODEC_ID_MP3LAME
:
608 rtp_send_mpegaudio(s1
, buf1
, size
);
610 case CODEC_ID_MPEG1VIDEO
:
611 rtp_send_mpegvideo(s1
, buf1
, size
);
619 static int rtp_write_trailer(AVFormatContext
*s1
)
621 // RTPContext *s = s1->priv_data;
625 AVInputFormat rtp_demux
= {
633 flags
: AVFMT_NOHEADER
,
636 AVOutputFormat rtp_mux
= {
651 av_register_output_format(&rtp_mux
);
652 av_register_input_format(&rtp_demux
);