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