configure: Drop unused helper function test_cflags_cpp()
[libav.git] / libavformat / rtpenc_xiph.c
CommitLineData
91af5601
JA
1/*
2 * RTP packetization for Xiph audio and video
3 * Copyright (c) 2010 Josh Allmann
4 *
2912e87a 5 * This file is part of Libav.
91af5601 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
91af5601
JA
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
91af5601
JA
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
2912e87a 18 * License along with Libav; if not, write to the Free Software
91af5601
JA
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
7c1e2e64
MS
22#include "libavutil/intreadwrite.h"
23
91af5601
JA
24#include "avformat.h"
25#include "rtpenc.h"
26
27/**
28 * Packetize Xiph frames into RTP according to
29 * RFC 5215 (Vorbis) and the Theora RFC draft.
30 * (http://svn.xiph.org/trunk/theora/doc/draft-ietf-avt-rtp-theora-00.txt)
31 */
32void ff_rtp_send_xiph(AVFormatContext *s1, const uint8_t *buff, int size)
33{
34 RTPMuxContext *s = s1->priv_data;
4f6cd883 35 AVStream *st = s1->streams[0];
91af5601
JA
36 int max_pkt_size, xdt, frag;
37 uint8_t *q;
38
11edeaea 39 max_pkt_size = s->max_payload_size - 6; // ident+frag+tdt/vdt+pkt_num+pkt_length
91af5601
JA
40
41 // set xiph data type
42 switch (*buff) {
43 case 0x01: // vorbis id
44 case 0x05: // vorbis setup
45 case 0x80: // theora header
46 case 0x82: // theora tables
47 xdt = 1; // packed config payload
48 break;
49 case 0x03: // vorbis comments
50 case 0x81: // theora comments
51 xdt = 2; // comment payload
52 break;
53 default:
54 xdt = 0; // raw data payload
55 break;
56 }
57
b5c4bb98 58 // Set ident.
91af5601
JA
59 // Probably need a non-fixed way of generating
60 // this, but it has to be done in SDP and passed in from there.
61 q = s->buf;
b5c4bb98
MS
62 *q++ = (RTP_XIPH_IDENT >> 16) & 0xff;
63 *q++ = (RTP_XIPH_IDENT >> 8) & 0xff;
64 *q++ = (RTP_XIPH_IDENT ) & 0xff;
91af5601
JA
65
66 // set fragment
67 // 0 - whole frame (possibly multiple frames)
68 // 1 - first fragment
69 // 2 - fragment continuation
41ed7ab4 70 // 3 - last fragment
91af5601
JA
71 frag = size <= max_pkt_size ? 0 : 1;
72
73 if (!frag && !xdt) { // do we have a whole frame of raw data?
62c7e2c3
MS
74 uint8_t *end_ptr = s->buf + 6 + max_pkt_size; // what we're allowed to write
75 uint8_t *ptr = s->buf_ptr + 2 + size; // what we're going to write
91af5601
JA
76 int remaining = end_ptr - ptr;
77
f53b9a29 78 assert(s->num_frames <= s->max_frames_per_packet);
bde2bba4
MS
79 if (s->num_frames > 0 &&
80 (remaining < 0 ||
4f6cd883
MS
81 s->num_frames == s->max_frames_per_packet ||
82 av_compare_ts(s->cur_timestamp - s->timestamp, st->time_base,
83 s1->max_delay, AV_TIME_BASE_Q) >= 0)) {
84 // send previous packets now; no room for new data, or too much delay
91af5601
JA
85 ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);
86 s->num_frames = 0;
87 }
88
89 // buffer current frame to send later
1fc64e2e
MS
90 if (0 == s->num_frames)
91 s->timestamp = s->cur_timestamp;
91af5601
JA
92 s->num_frames++;
93
94 // Set packet header. Normally, this is OR'd with frag and xdt,
95 // but those are zero, so omitted here
96 *q++ = s->num_frames;
97
1fc64e2e
MS
98 if (s->num_frames > 1)
99 q = s->buf_ptr; // jump ahead if needed
7c1e2e64
MS
100 AV_WB16(q, size);
101 q += 2;
91af5601
JA
102 memcpy(q, buff, size);
103 q += size;
104 s->buf_ptr = q;
105
106 return;
107 } else if (s->num_frames) {
108 // immediately send buffered frames if buffer is not raw data,
109 // or if current frame is fragmented.
110 ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);
111 }
112
113 s->timestamp = s->cur_timestamp;
114 s->num_frames = 0;
115 s->buf_ptr = q;
116 while (size > 0) {
117 int len = (!frag || frag == 3) ? size : max_pkt_size;
118 q = s->buf_ptr;
119
120 // set packet headers
121 *q++ = (frag << 6) | (xdt << 4); // num_frames = 0
7c1e2e64
MS
122 AV_WB16(q, len);
123 q += 2;
91af5601
JA
124 // set packet body
125 memcpy(q, buff, len);
126 q += len;
127 buff += len;
128 size -= len;
129
130 ff_rtp_send_data(s1, s->buf, q - s->buf, 0);
131
132 frag = size <= max_pkt_size ? 3 : 2;
133 }
134}