6349
|
1 /*
|
|
2 * RTP packetization for Xiph audio and video
|
|
3 * Copyright (c) 2010 Josh Allmann
|
|
4 *
|
|
5 * This file is part of FFmpeg.
|
|
6 *
|
|
7 * FFmpeg is free software; you can redistribute it and/or
|
|
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 *
|
|
12 * FFmpeg is distributed in the hope that it will be useful,
|
|
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
|
|
18 * License along with FFmpeg; if not, write to the Free Software
|
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
20 */
|
|
21
|
|
22 #include "avformat.h"
|
|
23 #include "rtpenc.h"
|
|
24
|
|
25 /**
|
|
26 * Packetize Xiph frames into RTP according to
|
|
27 * RFC 5215 (Vorbis) and the Theora RFC draft.
|
|
28 * (http://svn.xiph.org/trunk/theora/doc/draft-ietf-avt-rtp-theora-00.txt)
|
|
29 */
|
|
30 void ff_rtp_send_xiph(AVFormatContext *s1, const uint8_t *buff, int size)
|
|
31 {
|
|
32 RTPMuxContext *s = s1->priv_data;
|
|
33 int max_pkt_size, xdt, frag;
|
|
34 uint8_t *q;
|
|
35
|
|
36 max_pkt_size = s->max_payload_size;
|
|
37
|
|
38 // set xiph data type
|
|
39 switch (*buff) {
|
|
40 case 0x01: // vorbis id
|
|
41 case 0x05: // vorbis setup
|
|
42 case 0x80: // theora header
|
|
43 case 0x82: // theora tables
|
|
44 xdt = 1; // packed config payload
|
|
45 break;
|
|
46 case 0x03: // vorbis comments
|
|
47 case 0x81: // theora comments
|
|
48 xdt = 2; // comment payload
|
|
49 break;
|
|
50 default:
|
|
51 xdt = 0; // raw data payload
|
|
52 break;
|
|
53 }
|
|
54
|
|
55 // Set ident. Must match the one in sdp.c
|
|
56 // Probably need a non-fixed way of generating
|
|
57 // this, but it has to be done in SDP and passed in from there.
|
|
58 q = s->buf;
|
|
59 *q++ = 0xfe;
|
|
60 *q++ = 0xcd;
|
|
61 *q++ = 0xba;
|
|
62
|
|
63 // set fragment
|
|
64 // 0 - whole frame (possibly multiple frames)
|
|
65 // 1 - first fragment
|
|
66 // 2 - fragment continuation
|
|
67 // 3 - last fragmement
|
|
68 frag = size <= max_pkt_size ? 0 : 1;
|
|
69
|
|
70 if (!frag && !xdt) { // do we have a whole frame of raw data?
|
|
71 unsigned end_ptr = (unsigned)s->buf + 6 + max_pkt_size; // what we're allowed to write
|
|
72 unsigned ptr = (unsigned)s->buf_ptr + 2 + size; // what we're going to write
|
|
73 int remaining = end_ptr - ptr;
|
|
74
|
|
75 if ((s->num_frames > 0 && remaining < 0) ||
|
|
76 s->num_frames >= s->max_frames_per_packet) {
|
|
77 // send previous packets now; no room for new data
|
|
78 ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);
|
|
79 s->num_frames = 0;
|
|
80 }
|
|
81
|
|
82 // buffer current frame to send later
|
|
83 if (0 == s->num_frames) s->timestamp = s->cur_timestamp;
|
|
84 s->num_frames++;
|
|
85
|
|
86 // Set packet header. Normally, this is OR'd with frag and xdt,
|
|
87 // but those are zero, so omitted here
|
|
88 *q++ = s->num_frames;
|
|
89
|
|
90 if (s->num_frames > 1) q = s->buf_ptr; // jump ahead if needed
|
|
91 *q++ = (size >> 8) & 0xff;
|
|
92 *q++ = size & 0xff;
|
|
93 memcpy(q, buff, size);
|
|
94 q += size;
|
|
95 s->buf_ptr = q;
|
|
96
|
|
97 return;
|
|
98 } else if (s->num_frames) {
|
|
99 // immediately send buffered frames if buffer is not raw data,
|
|
100 // or if current frame is fragmented.
|
|
101 ff_rtp_send_data(s1, s->buf, s->buf_ptr - s->buf, 0);
|
|
102 }
|
|
103
|
|
104 s->timestamp = s->cur_timestamp;
|
|
105 s->num_frames = 0;
|
|
106 s->buf_ptr = q;
|
|
107 while (size > 0) {
|
|
108 int len = (!frag || frag == 3) ? size : max_pkt_size;
|
|
109 q = s->buf_ptr;
|
|
110
|
|
111 // set packet headers
|
|
112 *q++ = (frag << 6) | (xdt << 4); // num_frames = 0
|
|
113 *q++ = (len >> 8) & 0xff;
|
|
114 *q++ = len & 0xff;
|
|
115 // set packet body
|
|
116 memcpy(q, buff, len);
|
|
117 q += len;
|
|
118 buff += len;
|
|
119 size -= len;
|
|
120
|
|
121 ff_rtp_send_data(s1, s->buf, q - s->buf, 0);
|
|
122
|
|
123 frag = size <= max_pkt_size ? 3 : 2;
|
|
124 }
|
|
125 }
|