Add missing #includes for avutil.h, required for the AV_VERSION* macros.
[ffmpeg-lucabe.git] / libavformat / rtmppkt.c
blobe1f0647fbbde2a71533d3d85c40a275fda5901e6
1 /*
2 * RTMP input format
3 * Copyright (c) 2009 Kostya Shishkov
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "libavcodec/bytestream.h"
23 #include "libavutil/avstring.h"
24 #include "avformat.h"
26 #include "rtmppkt.h"
27 #include "flv.h"
29 void ff_amf_write_bool(uint8_t **dst, int val)
31 bytestream_put_byte(dst, AMF_DATA_TYPE_BOOL);
32 bytestream_put_byte(dst, val);
35 void ff_amf_write_number(uint8_t **dst, double val)
37 bytestream_put_byte(dst, AMF_DATA_TYPE_NUMBER);
38 bytestream_put_be64(dst, av_dbl2int(val));
41 void ff_amf_write_string(uint8_t **dst, const char *str)
43 bytestream_put_byte(dst, AMF_DATA_TYPE_STRING);
44 bytestream_put_be16(dst, strlen(str));
45 bytestream_put_buffer(dst, str, strlen(str));
48 void ff_amf_write_null(uint8_t **dst)
50 bytestream_put_byte(dst, AMF_DATA_TYPE_NULL);
53 void ff_amf_write_object_start(uint8_t **dst)
55 bytestream_put_byte(dst, AMF_DATA_TYPE_OBJECT);
58 void ff_amf_write_field_name(uint8_t **dst, const char *str)
60 bytestream_put_be16(dst, strlen(str));
61 bytestream_put_buffer(dst, str, strlen(str));
64 void ff_amf_write_object_end(uint8_t **dst)
66 /* first two bytes are field name length = 0,
67 * AMF object should end with it and end marker
69 bytestream_put_be24(dst, AMF_DATA_TYPE_OBJECT_END);
72 int ff_rtmp_packet_read(URLContext *h, RTMPPacket *p,
73 int chunk_size, RTMPPacket *prev_pkt)
75 uint8_t hdr, t, buf[16];
76 int channel_id, timestamp, data_size, offset = 0;
77 uint32_t extra = 0;
78 enum RTMPPacketType type;
80 if (url_read(h, &hdr, 1) != 1)
81 return AVERROR(EIO);
82 channel_id = hdr & 0x3F;
84 if (channel_id < 2) { //special case for channel number >= 64
85 buf[1] = 0;
86 if (url_read_complete(h, buf, channel_id + 1) != channel_id + 1)
87 return AVERROR(EIO);
88 channel_id = AV_RL16(buf) + 64;
90 data_size = prev_pkt[channel_id].data_size;
91 type = prev_pkt[channel_id].type;
92 extra = prev_pkt[channel_id].extra;
94 hdr >>= 6;
95 if (hdr == RTMP_PS_ONEBYTE) {
96 timestamp = prev_pkt[channel_id].timestamp;
97 } else {
98 if (url_read_complete(h, buf, 3) != 3)
99 return AVERROR(EIO);
100 timestamp = AV_RB24(buf);
101 if (hdr != RTMP_PS_FOURBYTES) {
102 if (url_read_complete(h, buf, 3) != 3)
103 return AVERROR(EIO);
104 data_size = AV_RB24(buf);
105 if (url_read_complete(h, &type, 1) != 1)
106 return AVERROR(EIO);
107 if (hdr == RTMP_PS_TWELVEBYTES) {
108 if (url_read_complete(h, buf, 4) != 4)
109 return AVERROR(EIO);
110 extra = AV_RL32(buf);
114 if (ff_rtmp_packet_create(p, channel_id, type, timestamp, data_size))
115 return -1;
116 p->extra = extra;
117 // save history
118 prev_pkt[channel_id].channel_id = channel_id;
119 prev_pkt[channel_id].type = type;
120 prev_pkt[channel_id].data_size = data_size;
121 prev_pkt[channel_id].timestamp = timestamp;
122 prev_pkt[channel_id].extra = extra;
123 while (data_size > 0) {
124 int toread = FFMIN(data_size, chunk_size);
125 if (url_read_complete(h, p->data + offset, toread) != toread) {
126 ff_rtmp_packet_destroy(p);
127 return AVERROR(EIO);
129 data_size -= chunk_size;
130 offset += chunk_size;
131 if (data_size > 0) {
132 url_read_complete(h, &t, 1); //marker
133 if (t != (0xC0 + channel_id))
134 return -1;
137 return 0;
140 int ff_rtmp_packet_write(URLContext *h, RTMPPacket *pkt,
141 int chunk_size, RTMPPacket *prev_pkt)
143 uint8_t pkt_hdr[16], *p = pkt_hdr;
144 int mode = RTMP_PS_TWELVEBYTES;
145 int off = 0;
147 //TODO: header compression
148 bytestream_put_byte(&p, pkt->channel_id | (mode << 6));
149 if (mode != RTMP_PS_ONEBYTE) {
150 bytestream_put_be24(&p, pkt->timestamp);
151 if (mode != RTMP_PS_FOURBYTES) {
152 bytestream_put_be24(&p, pkt->data_size);
153 bytestream_put_byte(&p, pkt->type);
154 if (mode == RTMP_PS_TWELVEBYTES)
155 bytestream_put_le32(&p, pkt->extra);
158 url_write(h, pkt_hdr, p-pkt_hdr);
159 while (off < pkt->data_size) {
160 int towrite = FFMIN(chunk_size, pkt->data_size - off);
161 url_write(h, pkt->data + off, towrite);
162 off += towrite;
163 if (off < pkt->data_size) {
164 uint8_t marker = 0xC0 | pkt->channel_id;
165 url_write(h, &marker, 1);
168 return 0;
171 int ff_rtmp_packet_create(RTMPPacket *pkt, int channel_id, RTMPPacketType type,
172 int timestamp, int size)
174 pkt->data = av_malloc(size);
175 if (!pkt->data)
176 return AVERROR(ENOMEM);
177 pkt->data_size = size;
178 pkt->channel_id = channel_id;
179 pkt->type = type;
180 pkt->timestamp = timestamp;
181 pkt->extra = 0;
183 return 0;
186 void ff_rtmp_packet_destroy(RTMPPacket *pkt)
188 if (!pkt)
189 return;
190 av_freep(&pkt->data);
191 pkt->data_size = 0;
194 int ff_amf_tag_size(const uint8_t *data, const uint8_t *data_end)
196 const uint8_t *base = data;
198 if (data >= data_end)
199 return -1;
200 switch (*data++) {
201 case AMF_DATA_TYPE_NUMBER: return 9;
202 case AMF_DATA_TYPE_BOOL: return 2;
203 case AMF_DATA_TYPE_STRING: return 3 + AV_RB16(data);
204 case AMF_DATA_TYPE_LONG_STRING: return 5 + AV_RB32(data);
205 case AMF_DATA_TYPE_NULL: return 1;
206 case AMF_DATA_TYPE_ARRAY:
207 data += 4;
208 case AMF_DATA_TYPE_OBJECT:
209 for (;;) {
210 int size = bytestream_get_be16(&data);
211 int t;
212 if (!size) {
213 data++;
214 break;
216 if (data + size >= data_end || data + size < data)
217 return -1;
218 data += size;
219 t = ff_amf_tag_size(data, data_end);
220 if (t < 0 || data + t >= data_end)
221 return -1;
222 data += t;
224 return data - base;
225 case AMF_DATA_TYPE_OBJECT_END: return 1;
226 default: return -1;
230 int ff_amf_get_field_value(const uint8_t *data, const uint8_t *data_end,
231 const uint8_t *name, uint8_t *dst, int dst_size)
233 int namelen = strlen(name);
234 int len;
236 if (data_end - data < 3)
237 return -1;
238 if (*data++ != AMF_DATA_TYPE_OBJECT)
239 return -1;
240 for (;;) {
241 int size = bytestream_get_be16(&data);
242 if (!size)
243 break;
244 if (data + size >= data_end || data + size < data)
245 return -1;
246 data += size;
247 if (size == namelen && !memcmp(data-size, name, namelen)) {
248 switch (*data++) {
249 case AMF_DATA_TYPE_NUMBER:
250 snprintf(dst, dst_size, "%g", av_int2dbl(AV_RB64(data)));
251 break;
252 case AMF_DATA_TYPE_BOOL:
253 snprintf(dst, dst_size, "%s", *data ? "true" : "false");
254 break;
255 case AMF_DATA_TYPE_STRING:
256 len = bytestream_get_be16(&data);
257 av_strlcpy(dst, data, FFMIN(len+1, dst_size));
258 break;
259 default:
260 return -1;
262 return 0;
264 len = ff_amf_tag_size(data, data_end);
265 if (len < 0 || data + len >= data_end || data + len < data)
266 return -1;
267 data += len;
269 return -1;