3 * Copyright (c) 2009 Konstantin Shishkov
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; 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 "libavutil/intfloat.h"
31 void ff_amf_write_bool(uint8_t **dst, int val)
33 bytestream_put_byte(dst, AMF_DATA_TYPE_BOOL);
34 bytestream_put_byte(dst, val);
37 void ff_amf_write_number(uint8_t **dst, double val)
39 bytestream_put_byte(dst, AMF_DATA_TYPE_NUMBER);
40 bytestream_put_be64(dst, av_double2int(val));
43 void ff_amf_write_string(uint8_t **dst, const char *str)
45 bytestream_put_byte(dst, AMF_DATA_TYPE_STRING);
46 bytestream_put_be16(dst, strlen(str));
47 bytestream_put_buffer(dst, str, strlen(str));
50 void ff_amf_write_string2(uint8_t **dst, const char *str1, const char *str2)
52 int len1 = 0, len2 = 0;
57 bytestream_put_byte(dst, AMF_DATA_TYPE_STRING);
58 bytestream_put_be16(dst, len1 + len2);
59 bytestream_put_buffer(dst, str1, len1);
60 bytestream_put_buffer(dst, str2, len2);
63 void ff_amf_write_null(uint8_t **dst)
65 bytestream_put_byte(dst, AMF_DATA_TYPE_NULL);
68 void ff_amf_write_object_start(uint8_t **dst)
70 bytestream_put_byte(dst, AMF_DATA_TYPE_OBJECT);
73 void ff_amf_write_field_name(uint8_t **dst, const char *str)
75 bytestream_put_be16(dst, strlen(str));
76 bytestream_put_buffer(dst, str, strlen(str));
79 void ff_amf_write_object_end(uint8_t **dst)
81 /* first two bytes are field name length = 0,
82 * AMF object should end with it and end marker
84 bytestream_put_be24(dst, AMF_DATA_TYPE_OBJECT_END);
87 int ff_amf_read_bool(GetByteContext *bc, int *val)
89 if (bytestream2_get_byte(bc) != AMF_DATA_TYPE_BOOL)
90 return AVERROR_INVALIDDATA;
91 *val = bytestream2_get_byte(bc);
95 int ff_amf_read_number(GetByteContext *bc, double *val)
98 if (bytestream2_get_byte(bc) != AMF_DATA_TYPE_NUMBER)
99 return AVERROR_INVALIDDATA;
100 read = bytestream2_get_be64(bc);
101 *val = av_int2double(read);
105 int ff_amf_get_string(GetByteContext *bc, uint8_t *str,
106 int strsize, int *length)
110 stringlen = bytestream2_get_be16(bc);
111 if (stringlen + 1 > strsize)
112 return AVERROR(EINVAL);
113 readsize = bytestream2_get_buffer(bc, str, stringlen);
114 if (readsize != stringlen) {
115 av_log(NULL, AV_LOG_WARNING,
116 "Unable to read as many bytes as AMF string signaled\n");
118 str[readsize] = '\0';
119 *length = FFMIN(stringlen, readsize);
123 int ff_amf_read_string(GetByteContext *bc, uint8_t *str,
124 int strsize, int *length)
126 if (bytestream2_get_byte(bc) != AMF_DATA_TYPE_STRING)
127 return AVERROR_INVALIDDATA;
128 return ff_amf_get_string(bc, str, strsize, length);
131 int ff_amf_read_null(GetByteContext *bc)
133 if (bytestream2_get_byte(bc) != AMF_DATA_TYPE_NULL)
134 return AVERROR_INVALIDDATA;
138 int ff_rtmp_check_alloc_array(RTMPPacket **prev_pkt, int *nb_prev_pkt,
143 if (channel < *nb_prev_pkt)
146 nb_alloc = channel + 16;
147 // This can't use the av_reallocp family of functions, since we
148 // would need to free each element in the array before the array
150 ptr = av_realloc_array(*prev_pkt, nb_alloc, sizeof(**prev_pkt));
152 return AVERROR(ENOMEM);
153 memset(ptr + *nb_prev_pkt, 0, (nb_alloc - *nb_prev_pkt) * sizeof(*ptr));
155 *nb_prev_pkt = nb_alloc;
159 int ff_rtmp_packet_read(URLContext *h, RTMPPacket *p,
160 int chunk_size, RTMPPacket **prev_pkt, int *nb_prev_pkt)
164 if (ffurl_read(h, &hdr, 1) != 1)
167 return ff_rtmp_packet_read_internal(h, p, chunk_size, prev_pkt,
171 static int rtmp_packet_read_one_chunk(URLContext *h, RTMPPacket *p,
172 int chunk_size, RTMPPacket **prev_pkt_ptr,
173 int *nb_prev_pkt, uint8_t hdr)
177 int channel_id, timestamp, size;
178 uint32_t ts_field; // non-extended timestamp or delta field
180 enum RTMPPacketType type;
183 RTMPPacket *prev_pkt;
186 channel_id = hdr & 0x3F;
188 if (channel_id < 2) { //special case for channel number >= 64
190 if (ffurl_read_complete(h, buf, channel_id + 1) != channel_id + 1)
192 written += channel_id + 1;
193 channel_id = AV_RL16(buf) + 64;
195 if ((ret = ff_rtmp_check_alloc_array(prev_pkt_ptr, nb_prev_pkt,
198 prev_pkt = *prev_pkt_ptr;
199 size = prev_pkt[channel_id].size;
200 type = prev_pkt[channel_id].type;
201 extra = prev_pkt[channel_id].extra;
203 hdr >>= 6; // header size indicator
204 if (hdr == RTMP_PS_ONEBYTE) {
205 ts_field = prev_pkt[channel_id].ts_field;
207 if (ffurl_read_complete(h, buf, 3) != 3)
210 ts_field = AV_RB24(buf);
211 if (hdr != RTMP_PS_FOURBYTES) {
212 if (ffurl_read_complete(h, buf, 3) != 3)
216 if (ffurl_read_complete(h, buf, 1) != 1)
220 if (hdr == RTMP_PS_TWELVEBYTES) {
221 if (ffurl_read_complete(h, buf, 4) != 4)
224 extra = AV_RL32(buf);
228 if (ts_field == 0xFFFFFF) {
229 if (ffurl_read_complete(h, buf, 4) != 4)
231 timestamp = AV_RB32(buf);
233 timestamp = ts_field;
235 if (hdr != RTMP_PS_TWELVEBYTES)
236 timestamp += prev_pkt[channel_id].timestamp;
238 if (!prev_pkt[channel_id].read) {
239 if ((ret = ff_rtmp_packet_create(p, channel_id, type, timestamp,
244 prev_pkt[channel_id].ts_field = ts_field;
245 prev_pkt[channel_id].timestamp = timestamp;
247 // previous packet in this channel hasn't completed reading
248 RTMPPacket *prev = &prev_pkt[channel_id];
249 p->data = prev->data;
250 p->size = prev->size;
251 p->channel_id = prev->channel_id;
252 p->type = prev->type;
253 p->ts_field = prev->ts_field;
254 p->extra = prev->extra;
255 p->offset = prev->offset;
256 p->read = prev->read + written;
257 p->timestamp = prev->timestamp;
262 prev_pkt[channel_id].channel_id = channel_id;
263 prev_pkt[channel_id].type = type;
264 prev_pkt[channel_id].size = size;
265 prev_pkt[channel_id].extra = extra;
266 size = size - p->offset;
268 toread = FFMIN(size, chunk_size);
269 if (ffurl_read_complete(h, p->data + p->offset, toread) != toread) {
270 ff_rtmp_packet_destroy(p);
278 RTMPPacket *prev = &prev_pkt[channel_id];
279 prev->data = p->data;
280 prev->read = p->read;
281 prev->offset = p->offset;
282 return AVERROR(EAGAIN);
285 prev_pkt[channel_id].read = 0; // read complete; reset if needed
289 int ff_rtmp_packet_read_internal(URLContext *h, RTMPPacket *p, int chunk_size,
290 RTMPPacket **prev_pkt, int *nb_prev_pkt,
294 int ret = rtmp_packet_read_one_chunk(h, p, chunk_size, prev_pkt,
296 if (ret > 0 || ret != AVERROR(EAGAIN))
299 if (ffurl_read(h, &hdr, 1) != 1)
304 int ff_rtmp_packet_write(URLContext *h, RTMPPacket *pkt,
305 int chunk_size, RTMPPacket **prev_pkt_ptr,
308 uint8_t pkt_hdr[16], *p = pkt_hdr;
309 int mode = RTMP_PS_TWELVEBYTES;
313 RTMPPacket *prev_pkt;
314 int use_delta; // flag if using timestamp delta, not RTMP_PS_TWELVEBYTES
315 uint32_t timestamp; // full 32-bit timestamp or delta value
317 if ((ret = ff_rtmp_check_alloc_array(prev_pkt_ptr, nb_prev_pkt,
318 pkt->channel_id)) < 0)
320 prev_pkt = *prev_pkt_ptr;
322 //if channel_id = 0, this is first presentation of prev_pkt, send full hdr.
323 use_delta = prev_pkt[pkt->channel_id].channel_id &&
324 pkt->extra == prev_pkt[pkt->channel_id].extra &&
325 pkt->timestamp >= prev_pkt[pkt->channel_id].timestamp;
327 timestamp = pkt->timestamp;
329 timestamp -= prev_pkt[pkt->channel_id].timestamp;
331 if (timestamp >= 0xFFFFFF) {
332 pkt->ts_field = 0xFFFFFF;
334 pkt->ts_field = timestamp;
338 if (pkt->type == prev_pkt[pkt->channel_id].type &&
339 pkt->size == prev_pkt[pkt->channel_id].size) {
340 mode = RTMP_PS_FOURBYTES;
341 if (pkt->ts_field == prev_pkt[pkt->channel_id].ts_field)
342 mode = RTMP_PS_ONEBYTE;
344 mode = RTMP_PS_EIGHTBYTES;
348 if (pkt->channel_id < 64) {
349 bytestream_put_byte(&p, pkt->channel_id | (mode << 6));
350 } else if (pkt->channel_id < 64 + 256) {
351 bytestream_put_byte(&p, 0 | (mode << 6));
352 bytestream_put_byte(&p, pkt->channel_id - 64);
354 bytestream_put_byte(&p, 1 | (mode << 6));
355 bytestream_put_le16(&p, pkt->channel_id - 64);
357 if (mode != RTMP_PS_ONEBYTE) {
358 bytestream_put_be24(&p, pkt->ts_field);
359 if (mode != RTMP_PS_FOURBYTES) {
360 bytestream_put_be24(&p, pkt->size);
361 bytestream_put_byte(&p, pkt->type);
362 if (mode == RTMP_PS_TWELVEBYTES)
363 bytestream_put_le32(&p, pkt->extra);
366 if (pkt->ts_field == 0xFFFFFF)
367 bytestream_put_be32(&p, timestamp);
369 prev_pkt[pkt->channel_id].channel_id = pkt->channel_id;
370 prev_pkt[pkt->channel_id].type = pkt->type;
371 prev_pkt[pkt->channel_id].size = pkt->size;
372 prev_pkt[pkt->channel_id].timestamp = pkt->timestamp;
373 prev_pkt[pkt->channel_id].ts_field = pkt->ts_field;
374 prev_pkt[pkt->channel_id].extra = pkt->extra;
376 if ((ret = ffurl_write(h, pkt_hdr, p - pkt_hdr)) < 0)
378 written = p - pkt_hdr + pkt->size;
379 while (off < pkt->size) {
380 int towrite = FFMIN(chunk_size, pkt->size - off);
381 if ((ret = ffurl_write(h, pkt->data + off, towrite)) < 0)
384 if (off < pkt->size) {
385 uint8_t marker = 0xC0 | pkt->channel_id;
386 if ((ret = ffurl_write(h, &marker, 1)) < 0)
389 if (pkt->ts_field == 0xFFFFFF) {
390 uint8_t ts_header[4];
391 AV_WB32(ts_header, timestamp);
392 if ((ret = ffurl_write(h, ts_header, 4)) < 0)
401 int ff_rtmp_packet_create(RTMPPacket *pkt, int channel_id, RTMPPacketType type,
402 int timestamp, int size)
405 pkt->data = av_realloc(NULL, size);
407 return AVERROR(ENOMEM);
410 pkt->channel_id = channel_id;
412 pkt->timestamp = timestamp;
419 void ff_rtmp_packet_destroy(RTMPPacket *pkt)
423 av_freep(&pkt->data);
427 int ff_amf_tag_size(const uint8_t *data, const uint8_t *data_end)
429 const uint8_t *base = data;
434 if (data >= data_end)
436 switch ((type = *data++)) {
437 case AMF_DATA_TYPE_NUMBER: return 9;
438 case AMF_DATA_TYPE_BOOL: return 2;
439 case AMF_DATA_TYPE_STRING: return 3 + AV_RB16(data);
440 case AMF_DATA_TYPE_LONG_STRING: return 5 + AV_RB32(data);
441 case AMF_DATA_TYPE_NULL: return 1;
442 case AMF_DATA_TYPE_ARRAY:
444 case AMF_DATA_TYPE_MIXEDARRAY:
445 nb = bytestream_get_be32(&data);
446 case AMF_DATA_TYPE_OBJECT:
447 while (nb-- > 0 || type != AMF_DATA_TYPE_ARRAY) {
450 int size = bytestream_get_be16(&data);
455 if (size < 0 || size >= data_end - data)
459 t = ff_amf_tag_size(data, data_end);
460 if (t < 0 || t >= data_end - data)
465 case AMF_DATA_TYPE_OBJECT_END: return 1;
470 int ff_amf_get_field_value(const uint8_t *data, const uint8_t *data_end,
471 const uint8_t *name, uint8_t *dst, int dst_size)
473 int namelen = strlen(name);
476 while (*data != AMF_DATA_TYPE_OBJECT && data < data_end) {
477 len = ff_amf_tag_size(data, data_end);
479 len = data_end - data;
482 if (data_end - data < 3)
486 int size = bytestream_get_be16(&data);
489 if (size < 0 || size >= data_end - data)
492 if (size == namelen && !memcmp(data-size, name, namelen)) {
494 case AMF_DATA_TYPE_NUMBER:
495 snprintf(dst, dst_size, "%g", av_int2double(AV_RB64(data)));
497 case AMF_DATA_TYPE_BOOL:
498 snprintf(dst, dst_size, "%s", *data ? "true" : "false");
500 case AMF_DATA_TYPE_STRING:
501 len = bytestream_get_be16(&data);
502 av_strlcpy(dst, data, FFMIN(len+1, dst_size));
509 len = ff_amf_tag_size(data, data_end);
510 if (len < 0 || len >= data_end - data)
517 static const char* rtmp_packet_type(int type)
520 case RTMP_PT_CHUNK_SIZE: return "chunk size";
521 case RTMP_PT_BYTES_READ: return "bytes read";
522 case RTMP_PT_PING: return "ping";
523 case RTMP_PT_SERVER_BW: return "server bandwidth";
524 case RTMP_PT_CLIENT_BW: return "client bandwidth";
525 case RTMP_PT_AUDIO: return "audio packet";
526 case RTMP_PT_VIDEO: return "video packet";
527 case RTMP_PT_FLEX_STREAM: return "Flex shared stream";
528 case RTMP_PT_FLEX_OBJECT: return "Flex shared object";
529 case RTMP_PT_FLEX_MESSAGE: return "Flex shared message";
530 case RTMP_PT_NOTIFY: return "notification";
531 case RTMP_PT_SHARED_OBJ: return "shared object";
532 case RTMP_PT_INVOKE: return "invoke";
533 case RTMP_PT_METADATA: return "metadata";
534 default: return "unknown";
538 static void amf_tag_contents(void *ctx, const uint8_t *data,
539 const uint8_t *data_end)
541 unsigned int size, nb = -1;
546 if (data >= data_end)
548 switch ((type = *data++)) {
549 case AMF_DATA_TYPE_NUMBER:
550 av_log(ctx, AV_LOG_DEBUG, " number %g\n", av_int2double(AV_RB64(data)));
552 case AMF_DATA_TYPE_BOOL:
553 av_log(ctx, AV_LOG_DEBUG, " bool %d\n", *data);
555 case AMF_DATA_TYPE_STRING:
556 case AMF_DATA_TYPE_LONG_STRING:
557 if (type == AMF_DATA_TYPE_STRING) {
558 size = bytestream_get_be16(&data);
560 size = bytestream_get_be32(&data);
562 size = FFMIN(size, sizeof(buf) - 1);
563 memcpy(buf, data, size);
565 av_log(ctx, AV_LOG_DEBUG, " string '%s'\n", buf);
567 case AMF_DATA_TYPE_NULL:
568 av_log(ctx, AV_LOG_DEBUG, " NULL\n");
570 case AMF_DATA_TYPE_ARRAY:
572 case AMF_DATA_TYPE_MIXEDARRAY:
573 nb = bytestream_get_be32(&data);
574 case AMF_DATA_TYPE_OBJECT:
575 av_log(ctx, AV_LOG_DEBUG, " {\n");
576 while (nb-- > 0 || type != AMF_DATA_TYPE_ARRAY) {
579 size = bytestream_get_be16(&data);
580 size = FFMIN(size, sizeof(buf) - 1);
582 av_log(ctx, AV_LOG_DEBUG, " }\n");
586 memcpy(buf, data, size);
588 if (size >= data_end - data)
591 av_log(ctx, AV_LOG_DEBUG, " %s: ", buf);
593 amf_tag_contents(ctx, data, data_end);
594 t = ff_amf_tag_size(data, data_end);
595 if (t < 0 || t >= data_end - data)
600 case AMF_DATA_TYPE_OBJECT_END:
601 av_log(ctx, AV_LOG_DEBUG, " }\n");
608 void ff_rtmp_packet_dump(void *ctx, RTMPPacket *p)
610 av_log(ctx, AV_LOG_DEBUG, "RTMP packet type '%s'(%d) for channel %d, timestamp %d, extra field %d size %d\n",
611 rtmp_packet_type(p->type), p->type, p->channel_id, p->timestamp, p->extra, p->size);
612 if (p->type == RTMP_PT_INVOKE || p->type == RTMP_PT_NOTIFY) {
613 uint8_t *src = p->data, *src_end = p->data + p->size;
614 while (src < src_end) {
616 amf_tag_contents(ctx, src, src_end);
617 sz = ff_amf_tag_size(src, src_end);
622 } else if (p->type == RTMP_PT_SERVER_BW){
623 av_log(ctx, AV_LOG_DEBUG, "Server BW = %d\n", AV_RB32(p->data));
624 } else if (p->type == RTMP_PT_CLIENT_BW){
625 av_log(ctx, AV_LOG_DEBUG, "Client BW = %d\n", AV_RB32(p->data));
626 } else if (p->type != RTMP_PT_AUDIO && p->type != RTMP_PT_VIDEO && p->type != RTMP_PT_METADATA) {
628 for (i = 0; i < p->size; i++)
629 av_log(ctx, AV_LOG_DEBUG, " %02X", p->data[i]);
630 av_log(ctx, AV_LOG_DEBUG, "\n");
634 int ff_amf_match_string(const uint8_t *data, int size, const char *str)
636 int len = strlen(str);
644 if (type != AMF_DATA_TYPE_LONG_STRING &&
645 type != AMF_DATA_TYPE_STRING)
648 if (type == AMF_DATA_TYPE_LONG_STRING) {
649 if ((size -= 4 + 1) < 0)
651 amf_len = bytestream_get_be32(&data);
653 if ((size -= 2 + 1) < 0)
655 amf_len = bytestream_get_be16(&data);
664 return !memcmp(data, str, len);