added rtp support (not activated yet)
[libav.git] / libav / rtp.c
CommitLineData
e309128f
FB
1/*
2 * RTP input/output format
3 * Copyright (c) 2002 Fabrice Bellard.
4 *
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.
9 *
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.
14 *
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
18 */
19#include "avformat.h"
20
21#include <unistd.h>
22#include <sys/types.h>
23#include <sys/socket.h>
24#include <netinet/in.h>
25#include <arpa/inet.h>
26#include <netdb.h>
27
28//#define DEBUG
29
30
31/* TODO: - add RTCP statistics reporting (should be optional).
32
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')
38*/
39
40#define RTP_VERSION 2
41
42#define RTP_MAX_SDES 256 /* maximum text length for SDES */
43
44/* RTCP paquets use 0.5 % of the bandwidth */
45#define RTCP_TX_RATIO_NUM 5
46#define RTCP_TX_RATIO_DEN 1000
47
48typedef enum {
49 RTCP_SR = 200,
50 RTCP_RR = 201,
51 RTCP_SDES = 202,
52 RTCP_BYE = 203,
53 RTCP_APP = 204
54} rtcp_type_t;
55
56typedef enum {
57 RTCP_SDES_END = 0,
58 RTCP_SDES_CNAME = 1,
59 RTCP_SDES_NAME = 2,
60 RTCP_SDES_EMAIL = 3,
61 RTCP_SDES_PHONE = 4,
62 RTCP_SDES_LOC = 5,
63 RTCP_SDES_TOOL = 6,
64 RTCP_SDES_NOTE = 7,
65 RTCP_SDES_PRIV = 8,
66 RTCP_SDES_IMG = 9,
67 RTCP_SDES_DOOR = 10,
68 RTCP_SDES_SOURCE = 11
69} rtcp_sdes_type_t;
70
71enum RTPPayloadType {
72 RTP_PT_ULAW = 0,
73 RTP_PT_GSM = 3,
74 RTP_PT_G723 = 4,
75 RTP_PT_ALAW = 8,
76 RTP_PT_S16BE_STEREO = 10,
77 RTP_PT_S16BE_MONO = 11,
78 RTP_PT_MPEGAUDIO = 14,
79 RTP_PT_JPEG = 26,
80 RTP_PT_H261 = 31,
81 RTP_PT_MPEGVIDEO = 32,
82 RTP_PT_MPEG2TS = 33,
83 RTP_PT_H263 = 34, /* old H263 encapsulation */
84};
85
86typedef struct RTPContext {
87 int payload_type;
88 UINT32 ssrc;
89 UINT16 seq;
90 UINT32 timestamp;
91 UINT32 base_timestamp;
92 UINT32 cur_timestamp;
93 int max_payload_size;
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;
101 int first_packet;
102 /* buffer for output */
103 UINT8 buf[RTP_MAX_PACKET_LENGTH];
104 UINT8 *buf_ptr;
105} RTPContext;
106
107int rtp_get_codec_info(AVCodecContext *codec, int payload_type)
108{
109 switch(payload_type) {
110 case RTP_PT_ULAW:
111 codec->codec_id = CODEC_ID_PCM_MULAW;
112 codec->channels = 1;
113 codec->sample_rate = 8000;
114 break;
115 case RTP_PT_ALAW:
116 codec->codec_id = CODEC_ID_PCM_ALAW;
117 codec->channels = 1;
118 codec->sample_rate = 8000;
119 break;
120 case RTP_PT_S16BE_STEREO:
121 codec->codec_id = CODEC_ID_PCM_S16BE;
122 codec->channels = 2;
123 codec->sample_rate = 44100;
124 break;
125 case RTP_PT_S16BE_MONO:
126 codec->codec_id = CODEC_ID_PCM_S16BE;
127 codec->channels = 1;
128 codec->sample_rate = 44100;
129 break;
130 case RTP_PT_MPEGAUDIO:
131 codec->codec_id = CODEC_ID_MP2;
132 break;
133 case RTP_PT_JPEG:
134 codec->codec_id = CODEC_ID_MJPEG;
135 break;
136 case RTP_PT_MPEGVIDEO:
137 codec->codec_id = CODEC_ID_MPEG1VIDEO;
138 break;
139 default:
140 return -1;
141 }
142 return 0;
143}
144
145/* return < 0 if unknown payload type */
146int rtp_get_payload_type(AVCodecContext *codec)
147{
148 int payload_type;
149
150 /* compute the payload type */
151 payload_type = -1;
152 switch(codec->codec_id) {
153 case CODEC_ID_PCM_MULAW:
154 payload_type = RTP_PT_ULAW;
155 break;
156 case CODEC_ID_PCM_ALAW:
157 payload_type = RTP_PT_ALAW;
158 break;
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;
164 }
165 break;
166 case CODEC_ID_MP2:
167 case CODEC_ID_MP3LAME:
168 payload_type = RTP_PT_MPEGAUDIO;
169 break;
170 case CODEC_ID_MJPEG:
171 payload_type = RTP_PT_JPEG;
172 break;
173 case CODEC_ID_MPEG1VIDEO:
174 payload_type = RTP_PT_MPEGVIDEO;
175 break;
176 default:
177 break;
178 }
179 return payload_type;
180}
181
182static inline UINT32 decode_be32(const UINT8 *p)
183{
184 return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
185}
186
187static inline UINT32 decode_be64(const UINT8 *p)
188{
189 return ((UINT64)decode_be32(p) << 32) | decode_be32(p + 4);
190}
191
192static int rtcp_parse_packet(AVFormatContext *s1, const unsigned char *buf, int len)
193{
194 RTPContext *s = s1->priv_data;
195
196 if (buf[1] != 200)
197 return -1;
198 s->last_rtcp_ntp_time = decode_be64(buf + 8);
199 s->last_rtcp_timestamp = decode_be32(buf + 16);
200 return 0;
201}
202
203/**
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.
211 */
212int rtp_parse_packet(AVFormatContext *s1, AVPacket *pkt,
213 const unsigned char *buf, int len)
214{
215 RTPContext *s = s1->priv_data;
216 unsigned int ssrc, h;
217 int payload_type, seq, delta_timestamp;
218 AVStream *st;
219 UINT32 timestamp;
220
221 if (len < 12)
222 return -1;
223
224 if ((buf[0] & 0xc0) != (RTP_VERSION << 6))
225 return -1;
226 if (buf[1] >= 200 && buf[1] <= 204) {
227 rtcp_parse_packet(s1, buf, len);
228 return -1;
229 }
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);
234
235 if (s->payload_type < 0) {
236 s->payload_type = payload_type;
237
238 if (payload_type == RTP_PT_MPEG2TS) {
239 /* XXX: special case : not a single codec but a whole stream */
240 return -1;
241 } else {
242 st = av_new_stream(s1, 0);
243 if (!st)
244 return -1;
245 rtp_get_codec_info(&st->codec, payload_type);
246 }
247 }
248
249 /* NOTE: we can handle only one payload type */
250 if (s->payload_type != payload_type)
251 return -1;
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));
256 }
257 s->seq = seq;
258#endif
259 len -= 12;
260 buf += 12;
261 st = s1->streams[0];
262 switch(st->codec.codec_id) {
263 case CODEC_ID_MP2:
264 /* better than nothing: skip mpeg audio RTP header */
265 if (len <= 4)
266 return -1;
267 h = decode_be32(buf);
268 len -= 4;
269 buf += 4;
270 av_new_packet(pkt, len);
271 memcpy(pkt->data, buf, len);
272 break;
273 case CODEC_ID_MPEG1VIDEO:
274 /* better than nothing: skip mpeg audio RTP header */
275 if (len <= 4)
276 return -1;
277 h = decode_be32(buf);
278 buf += 4;
279 len -= 4;
280 if (h & (1 << 26)) {
281 /* mpeg2 */
282 if (len <= 4)
283 return -1;
284 buf += 4;
285 len -= 4;
286 }
287 av_new_packet(pkt, len);
288 memcpy(pkt->data, buf, len);
289 break;
290 default:
291 av_new_packet(pkt, len);
292 memcpy(pkt->data, buf, len);
293 break;
294 }
295
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;
301 }
302 return 0;
303}
304
305static int rtp_read_header(AVFormatContext *s1,
306 AVFormatParameters *ap)
307{
308 RTPContext *s = s1->priv_data;
309 s->payload_type = -1;
310 s->last_rtcp_ntp_time = AV_NOPTS_VALUE;
311 return 0;
312}
313
314static int rtp_read_packet(AVFormatContext *s1, AVPacket *pkt)
315{
316 char buf[RTP_MAX_PACKET_LENGTH];
317 int ret;
318
319 /* XXX: needs a better API for packet handling ? */
320 for(;;) {
321 ret = url_read(url_fileno(&s1->pb), buf, sizeof(buf));
322 if (ret < 0)
323 return AVERROR_IO;
324 if (rtp_parse_packet(s1, pkt, buf, ret) == 0)
325 break;
326 }
327 return 0;
328}
329
330static int rtp_read_close(AVFormatContext *s1)
331{
332 // RTPContext *s = s1->priv_data;
333 return 0;
334}
335
336static int rtp_probe(AVProbeData *p)
337{
338 if (strstart(p->filename, "rtp://", NULL))
339 return AVPROBE_SCORE_MAX;
340 return 0;
341}
342
343/* rtp output */
344
345static int rtp_write_header(AVFormatContext *s1)
346{
347 RTPContext *s = s1->priv_data;
348 int payload_type, max_packet_size;
349 AVStream *st;
350
351 if (s1->nb_streams != 1)
352 return -1;
353 st = s1->streams[0];
354
355 payload_type = rtp_get_payload_type(&st->codec);
356 if (payload_type < 0)
357 return -1;
358 s->payload_type = payload_type;
359
360 s->base_timestamp = random();
361 s->timestamp = s->base_timestamp;
362 s->ssrc = random();
363 s->first_packet = 1;
364
365 max_packet_size = url_fget_max_packet_size(&s1->pb);
366 if (max_packet_size <= 12)
367 return AVERROR_IO;
368 s->max_payload_size = max_packet_size - 12;
369
370 switch(st->codec.codec_id) {
371 case CODEC_ID_MP2:
372 case CODEC_ID_MP3LAME:
373 s->buf_ptr = s->buf + 4;
374 s->cur_timestamp = 0;
375 break;
376 case CODEC_ID_MPEG1VIDEO:
377 s->cur_timestamp = 0;
378 break;
379 default:
380 s->buf_ptr = s->buf;
381 break;
382 }
383
384 return 0;
385}
386
387/* send an rtcp sender report packet */
388static void rtcp_send_sr(AVFormatContext *s1, INT64 ntp_time)
389{
390 RTPContext *s = s1->priv_data;
391#if defined(DEBUG)
392 printf("RTCP: %02x %Lx %x\n", s->payload_type, ntp_time, s->timestamp);
393#endif
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);
403}
404
405/* send an rtp packet. sequence number is incremented, but the caller
406 must update the timestamp itself */
407static void rtp_send_data(AVFormatContext *s1, UINT8 *buf1, int len)
408{
409 RTPContext *s = s1->priv_data;
410
411#ifdef DEBUG
412 printf("rtp_send_data size=%d\n", len);
413#endif
414
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);
421
422 put_buffer(&s1->pb, buf1, len);
423 put_flush_packet(&s1->pb);
424
425 s->seq++;
426 s->octet_count += len;
427 s->packet_count++;
428}
429
430/* send an integer number of samples and compute time stamp and fill
431 the rtp send buffer before sending. */
432static void rtp_send_samples(AVFormatContext *s1,
433 UINT8 *buf1, int size, int sample_size)
434{
435 RTPContext *s = s1->priv_data;
436 int len, max_packet_size, n;
437
438 max_packet_size = (s->max_payload_size / sample_size) * sample_size;
439 /* not needed, but who nows */
440 if ((size % sample_size) != 0)
441 av_abort();
442 while (size > 0) {
443 len = (max_packet_size - (s->buf_ptr - s->buf));
444 if (len > size)
445 len = size;
446
447 /* copy data */
448 memcpy(s->buf_ptr, buf1, len);
449 s->buf_ptr += len;
450 buf1 += len;
451 size -= 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);
456 s->buf_ptr = s->buf;
457 /* update timestamp */
458 s->timestamp += n / sample_size;
459 }
460 }
461}
462
463/* NOTE: we suppose that exactly one frame is given as argument here */
464/* XXX: test it */
465static void rtp_send_mpegaudio(AVFormatContext *s1,
466 UINT8 *buf1, int size)
467{
468 RTPContext *s = s1->priv_data;
469 AVStream *st = s1->streams[0];
470 int len, count, max_packet_size;
471
472 max_packet_size = s->max_payload_size;
473
474 /* test if we must flush because not enough space */
475 len = (s->buf_ptr - s->buf);
476 if ((len + size) > max_packet_size) {
477 if (len > 4) {
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;
483 }
484 }
485
486 /* add the packet */
487 if (size > max_packet_size) {
488 /* big packet: fragment */
489 count = 0;
490 while (size > 0) {
491 len = max_packet_size - 4;
492 if (len > size)
493 len = size;
494 /* build fragmented packet */
495 s->buf[0] = 0;
496 s->buf[1] = 0;
497 s->buf[2] = count >> 8;
498 s->buf[3] = count;
499 memcpy(s->buf + 4, buf1, len);
500 rtp_send_data(s1, s->buf, len + 4);
501 size -= len;
502 buf1 += len;
503 count += len;
504 }
505 } else {
506 if (s->buf_ptr == s->buf + 4) {
507 /* no fragmentation possible */
508 s->buf[0] = 0;
509 s->buf[1] = 0;
510 s->buf[2] = 0;
511 s->buf[3] = 0;
512 }
513 memcpy(s->buf_ptr, buf1, size);
514 s->buf_ptr += size;
515 }
516 s->cur_timestamp += st->codec.frame_size;
517}
518
519/* NOTE: a single frame must be passed with sequence header if
520 needed. XXX: use slices. */
521static void rtp_send_mpegvideo(AVFormatContext *s1,
522 UINT8 *buf1, int size)
523{
524 RTPContext *s = s1->priv_data;
525 AVStream *st = s1->streams[0];
526 int len, h, max_packet_size;
527 UINT8 *q;
528
529 max_packet_size = s->max_payload_size;
530
531 while (size > 0) {
532 /* XXX: more correct headers */
533 h = 0;
534 if (st->codec.sub_id == 2)
535 h |= 1 << 26; /* mpeg 2 indicator */
536 q = s->buf;
537 *q++ = h >> 24;
538 *q++ = h >> 16;
539 *q++ = h >> 8;
540 *q++ = h;
541
542 if (st->codec.sub_id == 2) {
543 h = 0;
544 *q++ = h >> 24;
545 *q++ = h >> 16;
546 *q++ = h >> 8;
547 *q++ = h;
548 }
549
550 len = max_packet_size - (q - s->buf);
551 if (len > size)
552 len = size;
553
554 memcpy(q, buf1, len);
555 q += len;
556
557 /* 90 KHz time stamp */
558 /* XXX: overflow */
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);
562
563 buf1 += len;
564 size -= len;
565 }
566 s->cur_timestamp++;
567}
568
569/* write an RTP packet. 'buf1' must contain a single specific frame. */
570static int rtp_write_packet(AVFormatContext *s1, int stream_index,
571 UINT8 *buf1, int size, int force_pts)
572{
573 RTPContext *s = s1->priv_data;
574 AVStream *st = s1->streams[0];
575 int rtcp_bytes;
576 INT64 ntp_time;
577
578#ifdef DEBUG
579 printf("%d: write len=%d\n", stream_index, size);
580#endif
581
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) /
584 RTCP_TX_RATIO_DEN;
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;
590 s->first_packet = 0;
591 }
592
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);
599 break;
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);
605 break;
606 case CODEC_ID_MP2:
607 case CODEC_ID_MP3LAME:
608 rtp_send_mpegaudio(s1, buf1, size);
609 break;
610 case CODEC_ID_MPEG1VIDEO:
611 rtp_send_mpegvideo(s1, buf1, size);
612 break;
613 default:
614 return AVERROR_IO;
615 }
616 return 0;
617}
618
619static int rtp_write_trailer(AVFormatContext *s1)
620{
621 // RTPContext *s = s1->priv_data;
622 return 0;
623}
624
625AVInputFormat rtp_demux = {
626 "rtp",
627 "RTP input format",
628 sizeof(RTPContext),
629 rtp_probe,
630 rtp_read_header,
631 rtp_read_packet,
632 rtp_read_close,
633 flags: AVFMT_NOHEADER,
634};
635
636AVOutputFormat rtp_mux = {
637 "rtp",
638 "RTP output format",
639 NULL,
640 NULL,
641 sizeof(RTPContext),
642 CODEC_ID_PCM_MULAW,
643 CODEC_ID_NONE,
644 rtp_write_header,
645 rtp_write_packet,
646 rtp_write_trailer,
647};
648
649int rtp_init(void)
650{
651 av_register_output_format(&rtp_mux);
652 av_register_input_format(&rtp_demux);
653 return 0;
654}