3 * Copyright (c) 2009 Konstantin 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/intfloat.h"
24 #include "libavutil/mem.h"
30 void ff_amf_write_bool(uint8_t **dst
, int val
)
32 bytestream_put_byte(dst
, AMF_DATA_TYPE_BOOL
);
33 bytestream_put_byte(dst
, val
);
36 void ff_amf_write_number(uint8_t **dst
, double val
)
38 bytestream_put_byte(dst
, AMF_DATA_TYPE_NUMBER
);
39 bytestream_put_be64(dst
, av_double2int(val
));
42 void ff_amf_write_array_start(uint8_t **dst
, uint32_t length
)
44 bytestream_put_byte(dst
, AMF_DATA_TYPE_ARRAY
);
45 bytestream_put_be32(dst
, length
);
48 void ff_amf_write_string(uint8_t **dst
, const char *str
)
50 bytestream_put_byte(dst
, AMF_DATA_TYPE_STRING
);
51 bytestream_put_be16(dst
, strlen(str
));
52 bytestream_put_buffer(dst
, str
, strlen(str
));
55 void ff_amf_write_string2(uint8_t **dst
, const char *str1
, const char *str2
)
57 int len1
= 0, len2
= 0;
62 bytestream_put_byte(dst
, AMF_DATA_TYPE_STRING
);
63 bytestream_put_be16(dst
, len1
+ len2
);
64 bytestream_put_buffer(dst
, str1
, len1
);
65 bytestream_put_buffer(dst
, str2
, len2
);
68 void ff_amf_write_null(uint8_t **dst
)
70 bytestream_put_byte(dst
, AMF_DATA_TYPE_NULL
);
73 void ff_amf_write_object_start(uint8_t **dst
)
75 bytestream_put_byte(dst
, AMF_DATA_TYPE_OBJECT
);
78 void ff_amf_write_field_name(uint8_t **dst
, const char *str
)
80 bytestream_put_be16(dst
, strlen(str
));
81 bytestream_put_buffer(dst
, str
, strlen(str
));
84 void ff_amf_write_object_end(uint8_t **dst
)
86 /* first two bytes are field name length = 0,
87 * AMF object should end with it and end marker
89 bytestream_put_be24(dst
, AMF_DATA_TYPE_OBJECT_END
);
92 int ff_amf_read_number(GetByteContext
*bc
, double *val
)
95 if (bytestream2_get_byte(bc
) != AMF_DATA_TYPE_NUMBER
)
96 return AVERROR_INVALIDDATA
;
97 read
= bytestream2_get_be64(bc
);
98 *val
= av_int2double(read
);
102 int ff_amf_get_string(GetByteContext
*bc
, uint8_t *str
,
103 int strsize
, int *length
)
107 stringlen
= bytestream2_get_be16(bc
);
108 if (stringlen
+ 1 > strsize
)
109 return AVERROR(EINVAL
);
110 readsize
= bytestream2_get_buffer(bc
, str
, stringlen
);
111 if (readsize
!= stringlen
) {
112 av_log(NULL
, AV_LOG_WARNING
,
113 "Unable to read as many bytes as AMF string signaled\n");
115 str
[readsize
] = '\0';
116 *length
= FFMIN(stringlen
, readsize
);
120 int ff_amf_read_string(GetByteContext
*bc
, uint8_t *str
,
121 int strsize
, int *length
)
123 if (bytestream2_get_byte(bc
) != AMF_DATA_TYPE_STRING
)
124 return AVERROR_INVALIDDATA
;
125 return ff_amf_get_string(bc
, str
, strsize
, length
);
128 int ff_amf_read_null(GetByteContext
*bc
)
130 if (bytestream2_get_byte(bc
) != AMF_DATA_TYPE_NULL
)
131 return AVERROR_INVALIDDATA
;
135 int ff_rtmp_check_alloc_array(RTMPPacket
**prev_pkt
, int *nb_prev_pkt
,
140 if (channel
< *nb_prev_pkt
)
143 nb_alloc
= channel
+ 16;
144 // This can't use the av_reallocp family of functions, since we
145 // would need to free each element in the array before the array
147 ptr
= av_realloc_array(*prev_pkt
, nb_alloc
, sizeof(**prev_pkt
));
149 return AVERROR(ENOMEM
);
150 memset(ptr
+ *nb_prev_pkt
, 0, (nb_alloc
- *nb_prev_pkt
) * sizeof(*ptr
));
152 *nb_prev_pkt
= nb_alloc
;
156 int ff_rtmp_packet_read(URLContext
*h
, RTMPPacket
*p
,
157 int chunk_size
, RTMPPacket
**prev_pkt
, int *nb_prev_pkt
)
161 if (ffurl_read(h
, &hdr
, 1) != 1)
164 return ff_rtmp_packet_read_internal(h
, p
, chunk_size
, prev_pkt
,
168 static int rtmp_packet_read_one_chunk(URLContext
*h
, RTMPPacket
*p
,
169 int chunk_size
, RTMPPacket
**prev_pkt_ptr
,
170 int *nb_prev_pkt
, uint8_t hdr
)
174 int channel_id
, timestamp
, size
;
175 uint32_t ts_field
; // non-extended timestamp or delta field
177 enum RTMPPacketType type
;
180 RTMPPacket
*prev_pkt
;
183 channel_id
= hdr
& 0x3F;
185 if (channel_id
< 2) { //special case for channel number >= 64
187 if (ffurl_read_complete(h
, buf
, channel_id
+ 1) != channel_id
+ 1)
189 written
+= channel_id
+ 1;
190 channel_id
= AV_RL16(buf
) + 64;
192 if ((ret
= ff_rtmp_check_alloc_array(prev_pkt_ptr
, nb_prev_pkt
,
195 prev_pkt
= *prev_pkt_ptr
;
196 size
= prev_pkt
[channel_id
].size
;
197 type
= prev_pkt
[channel_id
].type
;
198 extra
= prev_pkt
[channel_id
].extra
;
200 hdr
>>= 6; // header size indicator
201 if (hdr
== RTMP_PS_ONEBYTE
) {
202 ts_field
= prev_pkt
[channel_id
].ts_field
;
204 if (ffurl_read_complete(h
, buf
, 3) != 3)
207 ts_field
= AV_RB24(buf
);
208 if (hdr
!= RTMP_PS_FOURBYTES
) {
209 if (ffurl_read_complete(h
, buf
, 3) != 3)
213 if (ffurl_read_complete(h
, buf
, 1) != 1)
217 if (hdr
== RTMP_PS_TWELVEBYTES
) {
218 if (ffurl_read_complete(h
, buf
, 4) != 4)
221 extra
= AV_RL32(buf
);
225 if (ts_field
== 0xFFFFFF) {
226 if (ffurl_read_complete(h
, buf
, 4) != 4)
228 timestamp
= AV_RB32(buf
);
230 timestamp
= ts_field
;
232 if (hdr
!= RTMP_PS_TWELVEBYTES
)
233 timestamp
+= prev_pkt
[channel_id
].timestamp
;
235 if (prev_pkt
[channel_id
].read
&& size
!= prev_pkt
[channel_id
].size
) {
236 av_log(h
, AV_LOG_ERROR
, "RTMP packet size mismatch %d != %d\n",
237 size
, prev_pkt
[channel_id
].size
);
238 ff_rtmp_packet_destroy(&prev_pkt
[channel_id
]);
239 prev_pkt
[channel_id
].read
= 0;
240 return AVERROR_INVALIDDATA
;
243 if (!prev_pkt
[channel_id
].read
) {
244 if ((ret
= ff_rtmp_packet_create(p
, channel_id
, type
, timestamp
,
249 prev_pkt
[channel_id
].ts_field
= ts_field
;
250 prev_pkt
[channel_id
].timestamp
= timestamp
;
252 // previous packet in this channel hasn't completed reading
253 RTMPPacket
*prev
= &prev_pkt
[channel_id
];
254 p
->data
= prev
->data
;
255 p
->size
= prev
->size
;
256 p
->channel_id
= prev
->channel_id
;
257 p
->type
= prev
->type
;
258 p
->ts_field
= prev
->ts_field
;
259 p
->extra
= prev
->extra
;
260 p
->offset
= prev
->offset
;
261 p
->read
= prev
->read
+ written
;
262 p
->timestamp
= prev
->timestamp
;
267 prev_pkt
[channel_id
].channel_id
= channel_id
;
268 prev_pkt
[channel_id
].type
= type
;
269 prev_pkt
[channel_id
].size
= size
;
270 prev_pkt
[channel_id
].extra
= extra
;
271 size
= size
- p
->offset
;
273 toread
= FFMIN(size
, chunk_size
);
274 if (ffurl_read_complete(h
, p
->data
+ p
->offset
, toread
) != toread
) {
275 ff_rtmp_packet_destroy(p
);
283 RTMPPacket
*prev
= &prev_pkt
[channel_id
];
284 prev
->data
= p
->data
;
285 prev
->read
= p
->read
;
286 prev
->offset
= p
->offset
;
288 return AVERROR(EAGAIN
);
291 prev_pkt
[channel_id
].read
= 0; // read complete; reset if needed
295 int ff_rtmp_packet_read_internal(URLContext
*h
, RTMPPacket
*p
, int chunk_size
,
296 RTMPPacket
**prev_pkt
, int *nb_prev_pkt
,
300 int ret
= rtmp_packet_read_one_chunk(h
, p
, chunk_size
, prev_pkt
,
302 if (ret
> 0 || ret
!= AVERROR(EAGAIN
))
305 if (ffurl_read(h
, &hdr
, 1) != 1)
310 int ff_rtmp_packet_write(URLContext
*h
, RTMPPacket
*pkt
,
311 int chunk_size
, RTMPPacket
**prev_pkt_ptr
,
314 uint8_t pkt_hdr
[16], *p
= pkt_hdr
;
315 int mode
= RTMP_PS_TWELVEBYTES
;
319 RTMPPacket
*prev_pkt
;
320 int use_delta
; // flag if using timestamp delta, not RTMP_PS_TWELVEBYTES
321 uint32_t timestamp
; // full 32-bit timestamp or delta value
323 if ((ret
= ff_rtmp_check_alloc_array(prev_pkt_ptr
, nb_prev_pkt
,
324 pkt
->channel_id
)) < 0)
326 prev_pkt
= *prev_pkt_ptr
;
328 //if channel_id = 0, this is first presentation of prev_pkt, send full hdr.
329 use_delta
= prev_pkt
[pkt
->channel_id
].channel_id
&&
330 pkt
->extra
== prev_pkt
[pkt
->channel_id
].extra
&&
331 pkt
->timestamp
>= prev_pkt
[pkt
->channel_id
].timestamp
;
333 timestamp
= pkt
->timestamp
;
335 timestamp
-= prev_pkt
[pkt
->channel_id
].timestamp
;
337 if (timestamp
>= 0xFFFFFF) {
338 pkt
->ts_field
= 0xFFFFFF;
340 pkt
->ts_field
= timestamp
;
344 if (pkt
->type
== prev_pkt
[pkt
->channel_id
].type
&&
345 pkt
->size
== prev_pkt
[pkt
->channel_id
].size
) {
346 mode
= RTMP_PS_FOURBYTES
;
347 if (pkt
->ts_field
== prev_pkt
[pkt
->channel_id
].ts_field
)
348 mode
= RTMP_PS_ONEBYTE
;
350 mode
= RTMP_PS_EIGHTBYTES
;
354 if (pkt
->channel_id
< 64) {
355 bytestream_put_byte(&p
, pkt
->channel_id
| (mode
<< 6));
356 } else if (pkt
->channel_id
< 64 + 256) {
357 bytestream_put_byte(&p
, 0 | (mode
<< 6));
358 bytestream_put_byte(&p
, pkt
->channel_id
- 64);
360 bytestream_put_byte(&p
, 1 | (mode
<< 6));
361 bytestream_put_le16(&p
, pkt
->channel_id
- 64);
363 if (mode
!= RTMP_PS_ONEBYTE
) {
364 bytestream_put_be24(&p
, pkt
->ts_field
);
365 if (mode
!= RTMP_PS_FOURBYTES
) {
366 bytestream_put_be24(&p
, pkt
->size
);
367 bytestream_put_byte(&p
, pkt
->type
);
368 if (mode
== RTMP_PS_TWELVEBYTES
)
369 bytestream_put_le32(&p
, pkt
->extra
);
372 if (pkt
->ts_field
== 0xFFFFFF)
373 bytestream_put_be32(&p
, timestamp
);
375 prev_pkt
[pkt
->channel_id
].channel_id
= pkt
->channel_id
;
376 prev_pkt
[pkt
->channel_id
].type
= pkt
->type
;
377 prev_pkt
[pkt
->channel_id
].size
= pkt
->size
;
378 prev_pkt
[pkt
->channel_id
].timestamp
= pkt
->timestamp
;
379 prev_pkt
[pkt
->channel_id
].ts_field
= pkt
->ts_field
;
380 prev_pkt
[pkt
->channel_id
].extra
= pkt
->extra
;
383 // Writing packets is currently not optimized to minimize system calls.
384 // Since system calls flush on exit which we cannot change in a system-independant way.
385 // We should fix this behavior and by writing packets in a single or in as few as possible system calls.
386 // Protocols like TCP and RTMP should benefit from this when enabling TCP_NODELAY.
388 if ((ret
= ffurl_write(h
, pkt_hdr
, p
- pkt_hdr
)) < 0)
390 written
= p
- pkt_hdr
+ pkt
->size
;
391 while (off
< pkt
->size
) {
392 int towrite
= FFMIN(chunk_size
, pkt
->size
- off
);
393 if ((ret
= ffurl_write(h
, pkt
->data
+ off
, towrite
)) < 0)
396 if (off
< pkt
->size
) {
397 uint8_t marker
= 0xC0 | pkt
->channel_id
;
398 if ((ret
= ffurl_write(h
, &marker
, 1)) < 0)
401 if (pkt
->ts_field
== 0xFFFFFF) {
402 uint8_t ts_header
[4];
403 AV_WB32(ts_header
, timestamp
);
404 if ((ret
= ffurl_write(h
, ts_header
, 4)) < 0)
413 int ff_rtmp_packet_create(RTMPPacket
*pkt
, int channel_id
, RTMPPacketType type
,
414 int timestamp
, int size
)
417 pkt
->data
= av_realloc(NULL
, size
);
419 return AVERROR(ENOMEM
);
422 pkt
->channel_id
= channel_id
;
424 pkt
->timestamp
= timestamp
;
431 void ff_rtmp_packet_destroy(RTMPPacket
*pkt
)
435 av_freep(&pkt
->data
);
439 static int amf_tag_skip(GetByteContext
*gb
)
444 if (bytestream2_get_bytes_left(gb
) < 1)
447 type
= bytestream2_get_byte(gb
);
449 case AMF_DATA_TYPE_NUMBER
:
450 bytestream2_get_be64(gb
);
452 case AMF_DATA_TYPE_BOOL
:
453 bytestream2_get_byte(gb
);
455 case AMF_DATA_TYPE_STRING
:
456 bytestream2_skip(gb
, bytestream2_get_be16(gb
));
458 case AMF_DATA_TYPE_LONG_STRING
:
459 bytestream2_skip(gb
, bytestream2_get_be32(gb
));
461 case AMF_DATA_TYPE_NULL
:
463 case AMF_DATA_TYPE_DATE
:
464 bytestream2_skip(gb
, 10);
466 case AMF_DATA_TYPE_ARRAY
:
467 case AMF_DATA_TYPE_MIXEDARRAY
:
468 nb
= bytestream2_get_be32(gb
);
469 case AMF_DATA_TYPE_OBJECT
:
470 while (type
!= AMF_DATA_TYPE_ARRAY
|| nb
-- > 0) {
472 if (type
!= AMF_DATA_TYPE_ARRAY
) {
473 int size
= bytestream2_get_be16(gb
);
475 bytestream2_get_byte(gb
);
478 if (size
< 0 || size
>= bytestream2_get_bytes_left(gb
))
480 bytestream2_skip(gb
, size
);
482 t
= amf_tag_skip(gb
);
483 if (t
< 0 || bytestream2_get_bytes_left(gb
) <= 0)
487 case AMF_DATA_TYPE_OBJECT_END
: return 0;
492 int ff_amf_tag_size(const uint8_t *data
, const uint8_t *data_end
)
497 if (data
>= data_end
)
500 bytestream2_init(&gb
, data
, data_end
- data
);
502 ret
= amf_tag_skip(&gb
);
503 if (ret
< 0 || bytestream2_get_bytes_left(&gb
) <= 0)
505 av_assert0(bytestream2_tell(&gb
) >= 0 && bytestream2_tell(&gb
) <= data_end
- data
);
506 return bytestream2_tell(&gb
);
509 static int amf_get_field_value2(GetByteContext
*gb
,
510 const uint8_t *name
, uint8_t *dst
, int dst_size
)
512 int namelen
= strlen(name
);
515 while (bytestream2_peek_byte(gb
) != AMF_DATA_TYPE_OBJECT
&& bytestream2_get_bytes_left(gb
) > 0) {
516 int ret
= amf_tag_skip(gb
);
520 if (bytestream2_get_bytes_left(gb
) < 3)
522 bytestream2_get_byte(gb
);
525 int size
= bytestream2_get_be16(gb
);
528 if (size
< 0 || size
>= bytestream2_get_bytes_left(gb
))
530 bytestream2_skip(gb
, size
);
531 if (size
== namelen
&& !memcmp(gb
->buffer
-size
, name
, namelen
)) {
532 switch (bytestream2_get_byte(gb
)) {
533 case AMF_DATA_TYPE_NUMBER
:
534 snprintf(dst
, dst_size
, "%g", av_int2double(bytestream2_get_be64(gb
)));
536 case AMF_DATA_TYPE_BOOL
:
537 snprintf(dst
, dst_size
, "%s", bytestream2_get_byte(gb
) ? "true" : "false");
539 case AMF_DATA_TYPE_STRING
:
540 len
= bytestream2_get_be16(gb
);
543 if (dst_size
< len
+ 1)
545 bytestream2_get_buffer(gb
, dst
, len
);
553 len
= amf_tag_skip(gb
);
554 if (len
< 0 || bytestream2_get_bytes_left(gb
) <= 0)
560 int ff_amf_get_field_value(const uint8_t *data
, const uint8_t *data_end
,
561 const uint8_t *name
, uint8_t *dst
, int dst_size
)
565 if (data
>= data_end
)
568 bytestream2_init(&gb
, data
, data_end
- data
);
570 return amf_get_field_value2(&gb
, name
, dst
, dst_size
);
574 static const char* rtmp_packet_type(int type
)
577 case RTMP_PT_CHUNK_SIZE
: return "chunk size";
578 case RTMP_PT_BYTES_READ
: return "bytes read";
579 case RTMP_PT_USER_CONTROL
: return "user control";
580 case RTMP_PT_WINDOW_ACK_SIZE
: return "window acknowledgement size";
581 case RTMP_PT_SET_PEER_BW
: return "set peer bandwidth";
582 case RTMP_PT_AUDIO
: return "audio packet";
583 case RTMP_PT_VIDEO
: return "video packet";
584 case RTMP_PT_FLEX_STREAM
: return "Flex shared stream";
585 case RTMP_PT_FLEX_OBJECT
: return "Flex shared object";
586 case RTMP_PT_FLEX_MESSAGE
: return "Flex shared message";
587 case RTMP_PT_NOTIFY
: return "notification";
588 case RTMP_PT_SHARED_OBJ
: return "shared object";
589 case RTMP_PT_INVOKE
: return "invoke";
590 case RTMP_PT_METADATA
: return "metadata";
591 default: return "unknown";
595 static void amf_tag_contents(void *ctx
, const uint8_t *data
,
596 const uint8_t *data_end
)
598 unsigned int size
, nb
= -1;
603 if (data
>= data_end
)
605 switch ((type
= *data
++)) {
606 case AMF_DATA_TYPE_NUMBER
:
607 av_log(ctx
, AV_LOG_DEBUG
, " number %g\n", av_int2double(AV_RB64(data
)));
609 case AMF_DATA_TYPE_BOOL
:
610 av_log(ctx
, AV_LOG_DEBUG
, " bool %d\n", *data
);
612 case AMF_DATA_TYPE_STRING
:
613 case AMF_DATA_TYPE_LONG_STRING
:
614 if (type
== AMF_DATA_TYPE_STRING
) {
615 size
= bytestream_get_be16(&data
);
617 size
= bytestream_get_be32(&data
);
619 size
= FFMIN(size
, sizeof(buf
) - 1);
620 memcpy(buf
, data
, size
);
622 av_log(ctx
, AV_LOG_DEBUG
, " string '%s'\n", buf
);
624 case AMF_DATA_TYPE_NULL
:
625 av_log(ctx
, AV_LOG_DEBUG
, " NULL\n");
627 case AMF_DATA_TYPE_ARRAY
:
629 case AMF_DATA_TYPE_MIXEDARRAY
:
630 nb
= bytestream_get_be32(&data
);
631 case AMF_DATA_TYPE_OBJECT
:
632 av_log(ctx
, AV_LOG_DEBUG
, " {\n");
633 while (nb
-- > 0 || type
!= AMF_DATA_TYPE_ARRAY
) {
636 size
= bytestream_get_be16(&data
);
637 size
= FFMIN(size
, sizeof(buf
) - 1);
639 av_log(ctx
, AV_LOG_DEBUG
, " }\n");
643 memcpy(buf
, data
, size
);
645 if (size
>= data_end
- data
)
648 av_log(ctx
, AV_LOG_DEBUG
, " %s: ", buf
);
650 amf_tag_contents(ctx
, data
, data_end
);
651 t
= ff_amf_tag_size(data
, data_end
);
652 if (t
< 0 || t
>= data_end
- data
)
657 case AMF_DATA_TYPE_OBJECT_END
:
658 av_log(ctx
, AV_LOG_DEBUG
, " }\n");
665 void ff_rtmp_packet_dump(void *ctx
, RTMPPacket
*p
)
667 av_log(ctx
, AV_LOG_DEBUG
, "RTMP packet type '%s'(%d) for channel %d, timestamp %d, extra field %d size %d\n",
668 rtmp_packet_type(p
->type
), p
->type
, p
->channel_id
, p
->timestamp
, p
->extra
, p
->size
);
669 if (p
->type
== RTMP_PT_INVOKE
|| p
->type
== RTMP_PT_NOTIFY
) {
670 uint8_t *src
= p
->data
, *src_end
= p
->data
+ p
->size
;
671 while (src
< src_end
) {
673 amf_tag_contents(ctx
, src
, src_end
);
674 sz
= ff_amf_tag_size(src
, src_end
);
679 } else if (p
->type
== RTMP_PT_WINDOW_ACK_SIZE
) {
680 av_log(ctx
, AV_LOG_DEBUG
, "Window acknowledgement size = %d\n", AV_RB32(p
->data
));
681 } else if (p
->type
== RTMP_PT_SET_PEER_BW
) {
682 av_log(ctx
, AV_LOG_DEBUG
, "Set Peer BW = %d\n", AV_RB32(p
->data
));
683 } else if (p
->type
!= RTMP_PT_AUDIO
&& p
->type
!= RTMP_PT_VIDEO
&& p
->type
!= RTMP_PT_METADATA
) {
685 for (i
= 0; i
< p
->size
; i
++)
686 av_log(ctx
, AV_LOG_DEBUG
, " %02X", p
->data
[i
]);
687 av_log(ctx
, AV_LOG_DEBUG
, "\n");
692 int ff_amf_match_string(const uint8_t *data
, int size
, const char *str
)
694 int len
= strlen(str
);
702 if (type
!= AMF_DATA_TYPE_LONG_STRING
&&
703 type
!= AMF_DATA_TYPE_STRING
)
706 if (type
== AMF_DATA_TYPE_LONG_STRING
) {
707 if ((size
-= 4 + 1) < 0)
709 amf_len
= bytestream_get_be32(&data
);
711 if ((size
-= 2 + 1) < 0)
713 amf_len
= bytestream_get_be16(&data
);
722 return !memcmp(data
, str
, len
);