vc1dec: remove useless #include simple_idct.h
[libav.git] / libavformat / rtmppkt.c
CommitLineData
9fd6b843
KS
1/*
2 * RTMP input format
3 * Copyright (c) 2009 Kostya Shishkov
4 *
2912e87a 5 * This file is part of Libav.
9fd6b843 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
9fd6b843
KS
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,
9fd6b843
KS
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
9fd6b843
KS
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include "libavcodec/bytestream.h"
23#include "libavutil/avstring.h"
3383a53e 24#include "libavutil/intfloat.h"
9fd6b843
KS
25#include "avformat.h"
26
27#include "rtmppkt.h"
28#include "flv.h"
bc371aca 29#include "url.h"
9fd6b843
KS
30
31void ff_amf_write_bool(uint8_t **dst, int val)
32{
33 bytestream_put_byte(dst, AMF_DATA_TYPE_BOOL);
34 bytestream_put_byte(dst, val);
35}
36
37void ff_amf_write_number(uint8_t **dst, double val)
38{
39 bytestream_put_byte(dst, AMF_DATA_TYPE_NUMBER);
3383a53e 40 bytestream_put_be64(dst, av_double2int(val));
9fd6b843
KS
41}
42
43void ff_amf_write_string(uint8_t **dst, const char *str)
44{
45 bytestream_put_byte(dst, AMF_DATA_TYPE_STRING);
46 bytestream_put_be16(dst, strlen(str));
47 bytestream_put_buffer(dst, str, strlen(str));
48}
49
50void ff_amf_write_null(uint8_t **dst)
51{
52 bytestream_put_byte(dst, AMF_DATA_TYPE_NULL);
53}
54
55void ff_amf_write_object_start(uint8_t **dst)
56{
57 bytestream_put_byte(dst, AMF_DATA_TYPE_OBJECT);
58}
59
60void ff_amf_write_field_name(uint8_t **dst, const char *str)
61{
62 bytestream_put_be16(dst, strlen(str));
63 bytestream_put_buffer(dst, str, strlen(str));
64}
65
66void ff_amf_write_object_end(uint8_t **dst)
67{
68 /* first two bytes are field name length = 0,
69 * AMF object should end with it and end marker
70 */
71 bytestream_put_be24(dst, AMF_DATA_TYPE_OBJECT_END);
72}
73
74int ff_rtmp_packet_read(URLContext *h, RTMPPacket *p,
75 int chunk_size, RTMPPacket *prev_pkt)
76{
7dc747f5
SP
77 uint8_t hdr;
78
79 if (ffurl_read(h, &hdr, 1) != 1)
80 return AVERROR(EIO);
81
82 return ff_rtmp_packet_read_internal(h, p, chunk_size, prev_pkt, hdr);
83}
84
85int ff_rtmp_packet_read_internal(URLContext *h, RTMPPacket *p, int chunk_size,
86 RTMPPacket *prev_pkt, uint8_t hdr)
87{
88
89 uint8_t t, buf[16];
9fd6b843
KS
90 int channel_id, timestamp, data_size, offset = 0;
91 uint32_t extra = 0;
0f87b771 92 enum RTMPPacketType type;
0b6b10d9 93 int size = 0;
f645f1d6 94 int ret;
9fd6b843 95
0b6b10d9 96 size++;
9fd6b843
KS
97 channel_id = hdr & 0x3F;
98
5d660598
KS
99 if (channel_id < 2) { //special case for channel number >= 64
100 buf[1] = 0;
dce37564 101 if (ffurl_read_complete(h, buf, channel_id + 1) != channel_id + 1)
5d660598 102 return AVERROR(EIO);
0b6b10d9 103 size += channel_id + 1;
5d660598
KS
104 channel_id = AV_RL16(buf) + 64;
105 }
9fd6b843
KS
106 data_size = prev_pkt[channel_id].data_size;
107 type = prev_pkt[channel_id].type;
108 extra = prev_pkt[channel_id].extra;
109
110 hdr >>= 6;
111 if (hdr == RTMP_PS_ONEBYTE) {
a352b605 112 timestamp = prev_pkt[channel_id].ts_delta;
9fd6b843 113 } else {
dce37564 114 if (ffurl_read_complete(h, buf, 3) != 3)
9fd6b843 115 return AVERROR(EIO);
0b6b10d9 116 size += 3;
9fd6b843
KS
117 timestamp = AV_RB24(buf);
118 if (hdr != RTMP_PS_FOURBYTES) {
dce37564 119 if (ffurl_read_complete(h, buf, 3) != 3)
9fd6b843 120 return AVERROR(EIO);
0b6b10d9 121 size += 3;
9fd6b843 122 data_size = AV_RB24(buf);
dce37564 123 if (ffurl_read_complete(h, buf, 1) != 1)
9fd6b843 124 return AVERROR(EIO);
0b6b10d9 125 size++;
1ab21f19 126 type = buf[0];
9fd6b843 127 if (hdr == RTMP_PS_TWELVEBYTES) {
dce37564 128 if (ffurl_read_complete(h, buf, 4) != 4)
9fd6b843 129 return AVERROR(EIO);
0b6b10d9 130 size += 4;
9fd6b843
KS
131 extra = AV_RL32(buf);
132 }
133 }
59b6482a 134 if (timestamp == 0xFFFFFF) {
dce37564 135 if (ffurl_read_complete(h, buf, 4) != 4)
59b6482a
S
136 return AVERROR(EIO);
137 timestamp = AV_RB32(buf);
138 }
9fd6b843 139 }
a352b605
S
140 if (hdr != RTMP_PS_TWELVEBYTES)
141 timestamp += prev_pkt[channel_id].timestamp;
142
f645f1d6
SP
143 if ((ret = ff_rtmp_packet_create(p, channel_id, type, timestamp,
144 data_size)) < 0)
145 return ret;
9fd6b843
KS
146 p->extra = extra;
147 // save history
148 prev_pkt[channel_id].channel_id = channel_id;
149 prev_pkt[channel_id].type = type;
150 prev_pkt[channel_id].data_size = data_size;
a352b605 151 prev_pkt[channel_id].ts_delta = timestamp - prev_pkt[channel_id].timestamp;
9fd6b843
KS
152 prev_pkt[channel_id].timestamp = timestamp;
153 prev_pkt[channel_id].extra = extra;
154 while (data_size > 0) {
155 int toread = FFMIN(data_size, chunk_size);
dce37564 156 if (ffurl_read_complete(h, p->data + offset, toread) != toread) {
9fd6b843
KS
157 ff_rtmp_packet_destroy(p);
158 return AVERROR(EIO);
159 }
160 data_size -= chunk_size;
161 offset += chunk_size;
0b6b10d9 162 size += chunk_size;
9fd6b843 163 if (data_size > 0) {
bba287fd
SP
164 if ((ret = ffurl_read_complete(h, &t, 1)) < 0) { // marker
165 ff_rtmp_packet_destroy(p);
166 return ret;
167 }
0b6b10d9 168 size++;
9fd6b843
KS
169 if (t != (0xC0 + channel_id))
170 return -1;
171 }
172 }
0b6b10d9 173 return size;
9fd6b843
KS
174}
175
176int ff_rtmp_packet_write(URLContext *h, RTMPPacket *pkt,
177 int chunk_size, RTMPPacket *prev_pkt)
178{
179 uint8_t pkt_hdr[16], *p = pkt_hdr;
180 int mode = RTMP_PS_TWELVEBYTES;
181 int off = 0;
0b6b10d9 182 int size = 0;
bba287fd 183 int ret;
09b67cc4 184
a352b605 185 pkt->ts_delta = pkt->timestamp - prev_pkt[pkt->channel_id].timestamp;
9fd6b843 186
84a29dc1
S
187 //if channel_id = 0, this is first presentation of prev_pkt, send full hdr.
188 if (prev_pkt[pkt->channel_id].channel_id &&
189 pkt->extra == prev_pkt[pkt->channel_id].extra) {
190 if (pkt->type == prev_pkt[pkt->channel_id].type &&
191 pkt->data_size == prev_pkt[pkt->channel_id].data_size) {
192 mode = RTMP_PS_FOURBYTES;
193 if (pkt->ts_delta == prev_pkt[pkt->channel_id].ts_delta)
194 mode = RTMP_PS_ONEBYTE;
195 } else {
196 mode = RTMP_PS_EIGHTBYTES;
197 }
198 }
199
b1eb53ab
KS
200 if (pkt->channel_id < 64) {
201 bytestream_put_byte(&p, pkt->channel_id | (mode << 6));
202 } else if (pkt->channel_id < 64 + 256) {
203 bytestream_put_byte(&p, 0 | (mode << 6));
204 bytestream_put_byte(&p, pkt->channel_id - 64);
205 } else {
206 bytestream_put_byte(&p, 1 | (mode << 6));
207 bytestream_put_le16(&p, pkt->channel_id - 64);
208 }
9fd6b843 209 if (mode != RTMP_PS_ONEBYTE) {
817d967d
KS
210 uint32_t timestamp = pkt->timestamp;
211 if (mode != RTMP_PS_TWELVEBYTES)
a352b605 212 timestamp = pkt->ts_delta;
817d967d 213 bytestream_put_be24(&p, timestamp >= 0xFFFFFF ? 0xFFFFFF : timestamp);
9fd6b843
KS
214 if (mode != RTMP_PS_FOURBYTES) {
215 bytestream_put_be24(&p, pkt->data_size);
216 bytestream_put_byte(&p, pkt->type);
217 if (mode == RTMP_PS_TWELVEBYTES)
218 bytestream_put_le32(&p, pkt->extra);
219 }
817d967d
KS
220 if (timestamp >= 0xFFFFFF)
221 bytestream_put_be32(&p, timestamp);
9fd6b843 222 }
84a29dc1
S
223 // save history
224 prev_pkt[pkt->channel_id].channel_id = pkt->channel_id;
225 prev_pkt[pkt->channel_id].type = pkt->type;
226 prev_pkt[pkt->channel_id].data_size = pkt->data_size;
227 prev_pkt[pkt->channel_id].timestamp = pkt->timestamp;
228 if (mode != RTMP_PS_TWELVEBYTES) {
229 prev_pkt[pkt->channel_id].ts_delta = pkt->ts_delta;
230 } else {
231 prev_pkt[pkt->channel_id].ts_delta = pkt->timestamp;
232 }
233 prev_pkt[pkt->channel_id].extra = pkt->extra;
234
bba287fd
SP
235 if ((ret = ffurl_write(h, pkt_hdr, p - pkt_hdr)) < 0)
236 return ret;
0b6b10d9 237 size = p - pkt_hdr + pkt->data_size;
9fd6b843
KS
238 while (off < pkt->data_size) {
239 int towrite = FFMIN(chunk_size, pkt->data_size - off);
bba287fd
SP
240 if ((ret = ffurl_write(h, pkt->data + off, towrite)) < 0)
241 return ret;
9fd6b843
KS
242 off += towrite;
243 if (off < pkt->data_size) {
244 uint8_t marker = 0xC0 | pkt->channel_id;
bba287fd
SP
245 if ((ret = ffurl_write(h, &marker, 1)) < 0)
246 return ret;
0b6b10d9 247 size++;
9fd6b843
KS
248 }
249 }
0b6b10d9 250 return size;
9fd6b843
KS
251}
252
253int ff_rtmp_packet_create(RTMPPacket *pkt, int channel_id, RTMPPacketType type,
254 int timestamp, int size)
255{
271c869c 256 if (size) {
48b1fb13
MS
257 pkt->data = av_malloc(size);
258 if (!pkt->data)
259 return AVERROR(ENOMEM);
271c869c 260 }
9fd6b843
KS
261 pkt->data_size = size;
262 pkt->channel_id = channel_id;
263 pkt->type = type;
264 pkt->timestamp = timestamp;
265 pkt->extra = 0;
a352b605 266 pkt->ts_delta = 0;
9fd6b843
KS
267
268 return 0;
269}
270
271void ff_rtmp_packet_destroy(RTMPPacket *pkt)
272{
273 if (!pkt)
274 return;
275 av_freep(&pkt->data);
276 pkt->data_size = 0;
277}
278
279int ff_amf_tag_size(const uint8_t *data, const uint8_t *data_end)
280{
281 const uint8_t *base = data;
282
283 if (data >= data_end)
284 return -1;
285 switch (*data++) {
286 case AMF_DATA_TYPE_NUMBER: return 9;
287 case AMF_DATA_TYPE_BOOL: return 2;
288 case AMF_DATA_TYPE_STRING: return 3 + AV_RB16(data);
289 case AMF_DATA_TYPE_LONG_STRING: return 5 + AV_RB32(data);
290 case AMF_DATA_TYPE_NULL: return 1;
291 case AMF_DATA_TYPE_ARRAY:
292 data += 4;
293 case AMF_DATA_TYPE_OBJECT:
294 for (;;) {
295 int size = bytestream_get_be16(&data);
296 int t;
297 if (!size) {
298 data++;
299 break;
300 }
301 if (data + size >= data_end || data + size < data)
302 return -1;
303 data += size;
304 t = ff_amf_tag_size(data, data_end);
305 if (t < 0 || data + t >= data_end)
306 return -1;
307 data += t;
308 }
309 return data - base;
310 case AMF_DATA_TYPE_OBJECT_END: return 1;
311 default: return -1;
312 }
313}
314
315int ff_amf_get_field_value(const uint8_t *data, const uint8_t *data_end,
316 const uint8_t *name, uint8_t *dst, int dst_size)
317{
318 int namelen = strlen(name);
319 int len;
320
87ca1b8f
KS
321 while (*data != AMF_DATA_TYPE_OBJECT && data < data_end) {
322 len = ff_amf_tag_size(data, data_end);
323 if (len < 0)
324 len = data_end - data;
325 data += len;
326 }
9fd6b843
KS
327 if (data_end - data < 3)
328 return -1;
87ca1b8f 329 data++;
9fd6b843
KS
330 for (;;) {
331 int size = bytestream_get_be16(&data);
332 if (!size)
333 break;
334 if (data + size >= data_end || data + size < data)
335 return -1;
336 data += size;
337 if (size == namelen && !memcmp(data-size, name, namelen)) {
338 switch (*data++) {
339 case AMF_DATA_TYPE_NUMBER:
3383a53e 340 snprintf(dst, dst_size, "%g", av_int2double(AV_RB64(data)));
9fd6b843
KS
341 break;
342 case AMF_DATA_TYPE_BOOL:
343 snprintf(dst, dst_size, "%s", *data ? "true" : "false");
344 break;
345 case AMF_DATA_TYPE_STRING:
346 len = bytestream_get_be16(&data);
347 av_strlcpy(dst, data, FFMIN(len+1, dst_size));
348 break;
349 default:
350 return -1;
351 }
352 return 0;
353 }
354 len = ff_amf_tag_size(data, data_end);
355 if (len < 0 || data + len >= data_end || data + len < data)
356 return -1;
357 data += len;
358 }
359 return -1;
360}
cfac91fe
KS
361
362static const char* rtmp_packet_type(int type)
363{
364 switch (type) {
365 case RTMP_PT_CHUNK_SIZE: return "chunk size";
366 case RTMP_PT_BYTES_READ: return "bytes read";
367 case RTMP_PT_PING: return "ping";
368 case RTMP_PT_SERVER_BW: return "server bandwidth";
369 case RTMP_PT_CLIENT_BW: return "client bandwidth";
370 case RTMP_PT_AUDIO: return "audio packet";
371 case RTMP_PT_VIDEO: return "video packet";
372 case RTMP_PT_FLEX_STREAM: return "Flex shared stream";
373 case RTMP_PT_FLEX_OBJECT: return "Flex shared object";
374 case RTMP_PT_FLEX_MESSAGE: return "Flex shared message";
375 case RTMP_PT_NOTIFY: return "notification";
376 case RTMP_PT_SHARED_OBJ: return "shared object";
377 case RTMP_PT_INVOKE: return "invoke";
378 case RTMP_PT_METADATA: return "metadata";
379 default: return "unknown";
380 }
381}
382
383static void ff_amf_tag_contents(void *ctx, const uint8_t *data, const uint8_t *data_end)
384{
25182289 385 int size;
cfac91fe
KS
386 char buf[1024];
387
388 if (data >= data_end)
389 return;
390 switch (*data++) {
391 case AMF_DATA_TYPE_NUMBER:
3383a53e 392 av_log(ctx, AV_LOG_DEBUG, " number %g\n", av_int2double(AV_RB64(data)));
cfac91fe
KS
393 return;
394 case AMF_DATA_TYPE_BOOL:
395 av_log(ctx, AV_LOG_DEBUG, " bool %d\n", *data);
396 return;
397 case AMF_DATA_TYPE_STRING:
398 case AMF_DATA_TYPE_LONG_STRING:
399 if (data[-1] == AMF_DATA_TYPE_STRING) {
400 size = bytestream_get_be16(&data);
401 } else {
f3cb1cd0 402 size = bytestream_get_be32(&data);
cfac91fe
KS
403 }
404 size = FFMIN(size, 1023);
405 memcpy(buf, data, size);
406 buf[size] = 0;
407 av_log(ctx, AV_LOG_DEBUG, " string '%s'\n", buf);
408 return;
409 case AMF_DATA_TYPE_NULL:
410 av_log(ctx, AV_LOG_DEBUG, " NULL\n");
411 return;
412 case AMF_DATA_TYPE_ARRAY:
413 data += 4;
414 case AMF_DATA_TYPE_OBJECT:
415 av_log(ctx, AV_LOG_DEBUG, " {\n");
416 for (;;) {
417 int size = bytestream_get_be16(&data);
418 int t;
419 memcpy(buf, data, size);
420 buf[size] = 0;
421 if (!size) {
422 av_log(ctx, AV_LOG_DEBUG, " }\n");
423 data++;
424 break;
425 }
426 if (data + size >= data_end || data + size < data)
427 return;
428 data += size;
429 av_log(ctx, AV_LOG_DEBUG, " %s: ", buf);
430 ff_amf_tag_contents(ctx, data, data_end);
431 t = ff_amf_tag_size(data, data_end);
432 if (t < 0 || data + t >= data_end)
433 return;
434 data += t;
435 }
436 return;
437 case AMF_DATA_TYPE_OBJECT_END:
438 av_log(ctx, AV_LOG_DEBUG, " }\n");
439 return;
440 default:
441 return;
442 }
443}
444
445void ff_rtmp_packet_dump(void *ctx, RTMPPacket *p)
446{
447 av_log(ctx, AV_LOG_DEBUG, "RTMP packet type '%s'(%d) for channel %d, timestamp %d, extra field %d size %d\n",
448 rtmp_packet_type(p->type), p->type, p->channel_id, p->timestamp, p->extra, p->data_size);
449 if (p->type == RTMP_PT_INVOKE || p->type == RTMP_PT_NOTIFY) {
450 uint8_t *src = p->data, *src_end = p->data + p->data_size;
451 while (src < src_end) {
452 int sz;
453 ff_amf_tag_contents(ctx, src, src_end);
454 sz = ff_amf_tag_size(src, src_end);
455 if (sz < 0)
456 break;
457 src += sz;
458 }
459 } else if (p->type == RTMP_PT_SERVER_BW){
460 av_log(ctx, AV_LOG_DEBUG, "Server BW = %d\n", AV_RB32(p->data));
461 } else if (p->type == RTMP_PT_CLIENT_BW){
462 av_log(ctx, AV_LOG_DEBUG, "Client BW = %d\n", AV_RB32(p->data));
463 } else if (p->type != RTMP_PT_AUDIO && p->type != RTMP_PT_VIDEO && p->type != RTMP_PT_METADATA) {
464 int i;
465 for (i = 0; i < p->data_size; i++)
466 av_log(ctx, AV_LOG_DEBUG, " %02X", p->data[i]);
467 av_log(ctx, AV_LOG_DEBUG, "\n");
468 }
469}