3 * Copyright (c) 2009 Kostya 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_null(uint8_t **dst)
52 bytestream_put_byte(dst, AMF_DATA_TYPE_NULL);
55 void ff_amf_write_object_start(uint8_t **dst)
57 bytestream_put_byte(dst, AMF_DATA_TYPE_OBJECT);
60 void ff_amf_write_field_name(uint8_t **dst, const char *str)
62 bytestream_put_be16(dst, strlen(str));
63 bytestream_put_buffer(dst, str, strlen(str));
66 void ff_amf_write_object_end(uint8_t **dst)
68 /* first two bytes are field name length = 0,
69 * AMF object should end with it and end marker
71 bytestream_put_be24(dst, AMF_DATA_TYPE_OBJECT_END);
74 int ff_rtmp_packet_read(URLContext *h, RTMPPacket *p,
75 int chunk_size, RTMPPacket *prev_pkt)
77 uint8_t hdr, t, buf[16];
78 int channel_id, timestamp, data_size, offset = 0;
80 enum RTMPPacketType type;
83 if (ffurl_read(h, &hdr, 1) != 1)
86 channel_id = hdr & 0x3F;
88 if (channel_id < 2) { //special case for channel number >= 64
90 if (ffurl_read_complete(h, buf, channel_id + 1) != channel_id + 1)
92 size += channel_id + 1;
93 channel_id = AV_RL16(buf) + 64;
95 data_size = prev_pkt[channel_id].data_size;
96 type = prev_pkt[channel_id].type;
97 extra = prev_pkt[channel_id].extra;
100 if (hdr == RTMP_PS_ONEBYTE) {
101 timestamp = prev_pkt[channel_id].ts_delta;
103 if (ffurl_read_complete(h, buf, 3) != 3)
106 timestamp = AV_RB24(buf);
107 if (hdr != RTMP_PS_FOURBYTES) {
108 if (ffurl_read_complete(h, buf, 3) != 3)
111 data_size = AV_RB24(buf);
112 if (ffurl_read_complete(h, buf, 1) != 1)
116 if (hdr == RTMP_PS_TWELVEBYTES) {
117 if (ffurl_read_complete(h, buf, 4) != 4)
120 extra = AV_RL32(buf);
123 if (timestamp == 0xFFFFFF) {
124 if (ffurl_read_complete(h, buf, 4) != 4)
126 timestamp = AV_RB32(buf);
129 if (hdr != RTMP_PS_TWELVEBYTES)
130 timestamp += prev_pkt[channel_id].timestamp;
132 if (ff_rtmp_packet_create(p, channel_id, type, timestamp, data_size))
136 prev_pkt[channel_id].channel_id = channel_id;
137 prev_pkt[channel_id].type = type;
138 prev_pkt[channel_id].data_size = data_size;
139 prev_pkt[channel_id].ts_delta = timestamp - prev_pkt[channel_id].timestamp;
140 prev_pkt[channel_id].timestamp = timestamp;
141 prev_pkt[channel_id].extra = extra;
142 while (data_size > 0) {
143 int toread = FFMIN(data_size, chunk_size);
144 if (ffurl_read_complete(h, p->data + offset, toread) != toread) {
145 ff_rtmp_packet_destroy(p);
148 data_size -= chunk_size;
149 offset += chunk_size;
152 ffurl_read_complete(h, &t, 1); //marker
154 if (t != (0xC0 + channel_id))
161 int ff_rtmp_packet_write(URLContext *h, RTMPPacket *pkt,
162 int chunk_size, RTMPPacket *prev_pkt)
164 uint8_t pkt_hdr[16], *p = pkt_hdr;
165 int mode = RTMP_PS_TWELVEBYTES;
169 pkt->ts_delta = pkt->timestamp - prev_pkt[pkt->channel_id].timestamp;
171 //if channel_id = 0, this is first presentation of prev_pkt, send full hdr.
172 if (prev_pkt[pkt->channel_id].channel_id &&
173 pkt->extra == prev_pkt[pkt->channel_id].extra) {
174 if (pkt->type == prev_pkt[pkt->channel_id].type &&
175 pkt->data_size == prev_pkt[pkt->channel_id].data_size) {
176 mode = RTMP_PS_FOURBYTES;
177 if (pkt->ts_delta == prev_pkt[pkt->channel_id].ts_delta)
178 mode = RTMP_PS_ONEBYTE;
180 mode = RTMP_PS_EIGHTBYTES;
184 if (pkt->channel_id < 64) {
185 bytestream_put_byte(&p, pkt->channel_id | (mode << 6));
186 } else if (pkt->channel_id < 64 + 256) {
187 bytestream_put_byte(&p, 0 | (mode << 6));
188 bytestream_put_byte(&p, pkt->channel_id - 64);
190 bytestream_put_byte(&p, 1 | (mode << 6));
191 bytestream_put_le16(&p, pkt->channel_id - 64);
193 if (mode != RTMP_PS_ONEBYTE) {
194 uint32_t timestamp = pkt->timestamp;
195 if (mode != RTMP_PS_TWELVEBYTES)
196 timestamp = pkt->ts_delta;
197 bytestream_put_be24(&p, timestamp >= 0xFFFFFF ? 0xFFFFFF : timestamp);
198 if (mode != RTMP_PS_FOURBYTES) {
199 bytestream_put_be24(&p, pkt->data_size);
200 bytestream_put_byte(&p, pkt->type);
201 if (mode == RTMP_PS_TWELVEBYTES)
202 bytestream_put_le32(&p, pkt->extra);
204 if (timestamp >= 0xFFFFFF)
205 bytestream_put_be32(&p, timestamp);
208 prev_pkt[pkt->channel_id].channel_id = pkt->channel_id;
209 prev_pkt[pkt->channel_id].type = pkt->type;
210 prev_pkt[pkt->channel_id].data_size = pkt->data_size;
211 prev_pkt[pkt->channel_id].timestamp = pkt->timestamp;
212 if (mode != RTMP_PS_TWELVEBYTES) {
213 prev_pkt[pkt->channel_id].ts_delta = pkt->ts_delta;
215 prev_pkt[pkt->channel_id].ts_delta = pkt->timestamp;
217 prev_pkt[pkt->channel_id].extra = pkt->extra;
219 ffurl_write(h, pkt_hdr, p-pkt_hdr);
220 size = p - pkt_hdr + pkt->data_size;
221 while (off < pkt->data_size) {
222 int towrite = FFMIN(chunk_size, pkt->data_size - off);
223 ffurl_write(h, pkt->data + off, towrite);
225 if (off < pkt->data_size) {
226 uint8_t marker = 0xC0 | pkt->channel_id;
227 ffurl_write(h, &marker, 1);
234 int ff_rtmp_packet_create(RTMPPacket *pkt, int channel_id, RTMPPacketType type,
235 int timestamp, int size)
238 pkt->data = av_malloc(size);
240 return AVERROR(ENOMEM);
242 pkt->data_size = size;
243 pkt->channel_id = channel_id;
245 pkt->timestamp = timestamp;
252 void ff_rtmp_packet_destroy(RTMPPacket *pkt)
256 av_freep(&pkt->data);
260 int ff_amf_tag_size(const uint8_t *data, const uint8_t *data_end)
262 const uint8_t *base = data;
264 if (data >= data_end)
267 case AMF_DATA_TYPE_NUMBER: return 9;
268 case AMF_DATA_TYPE_BOOL: return 2;
269 case AMF_DATA_TYPE_STRING: return 3 + AV_RB16(data);
270 case AMF_DATA_TYPE_LONG_STRING: return 5 + AV_RB32(data);
271 case AMF_DATA_TYPE_NULL: return 1;
272 case AMF_DATA_TYPE_ARRAY:
274 case AMF_DATA_TYPE_OBJECT:
276 int size = bytestream_get_be16(&data);
282 if (data + size >= data_end || data + size < data)
285 t = ff_amf_tag_size(data, data_end);
286 if (t < 0 || data + t >= data_end)
291 case AMF_DATA_TYPE_OBJECT_END: return 1;
296 int ff_amf_get_field_value(const uint8_t *data, const uint8_t *data_end,
297 const uint8_t *name, uint8_t *dst, int dst_size)
299 int namelen = strlen(name);
302 while (*data != AMF_DATA_TYPE_OBJECT && data < data_end) {
303 len = ff_amf_tag_size(data, data_end);
305 len = data_end - data;
308 if (data_end - data < 3)
312 int size = bytestream_get_be16(&data);
315 if (data + size >= data_end || data + size < data)
318 if (size == namelen && !memcmp(data-size, name, namelen)) {
320 case AMF_DATA_TYPE_NUMBER:
321 snprintf(dst, dst_size, "%g", av_int2double(AV_RB64(data)));
323 case AMF_DATA_TYPE_BOOL:
324 snprintf(dst, dst_size, "%s", *data ? "true" : "false");
326 case AMF_DATA_TYPE_STRING:
327 len = bytestream_get_be16(&data);
328 av_strlcpy(dst, data, FFMIN(len+1, dst_size));
335 len = ff_amf_tag_size(data, data_end);
336 if (len < 0 || data + len >= data_end || data + len < data)
343 static const char* rtmp_packet_type(int type)
346 case RTMP_PT_CHUNK_SIZE: return "chunk size";
347 case RTMP_PT_BYTES_READ: return "bytes read";
348 case RTMP_PT_PING: return "ping";
349 case RTMP_PT_SERVER_BW: return "server bandwidth";
350 case RTMP_PT_CLIENT_BW: return "client bandwidth";
351 case RTMP_PT_AUDIO: return "audio packet";
352 case RTMP_PT_VIDEO: return "video packet";
353 case RTMP_PT_FLEX_STREAM: return "Flex shared stream";
354 case RTMP_PT_FLEX_OBJECT: return "Flex shared object";
355 case RTMP_PT_FLEX_MESSAGE: return "Flex shared message";
356 case RTMP_PT_NOTIFY: return "notification";
357 case RTMP_PT_SHARED_OBJ: return "shared object";
358 case RTMP_PT_INVOKE: return "invoke";
359 case RTMP_PT_METADATA: return "metadata";
360 default: return "unknown";
364 static void ff_amf_tag_contents(void *ctx, const uint8_t *data, const uint8_t *data_end)
369 if (data >= data_end)
372 case AMF_DATA_TYPE_NUMBER:
373 av_log(ctx, AV_LOG_DEBUG, " number %g\n", av_int2double(AV_RB64(data)));
375 case AMF_DATA_TYPE_BOOL:
376 av_log(ctx, AV_LOG_DEBUG, " bool %d\n", *data);
378 case AMF_DATA_TYPE_STRING:
379 case AMF_DATA_TYPE_LONG_STRING:
380 if (data[-1] == AMF_DATA_TYPE_STRING) {
381 size = bytestream_get_be16(&data);
383 size = bytestream_get_be32(&data);
385 size = FFMIN(size, 1023);
386 memcpy(buf, data, size);
388 av_log(ctx, AV_LOG_DEBUG, " string '%s'\n", buf);
390 case AMF_DATA_TYPE_NULL:
391 av_log(ctx, AV_LOG_DEBUG, " NULL\n");
393 case AMF_DATA_TYPE_ARRAY:
395 case AMF_DATA_TYPE_OBJECT:
396 av_log(ctx, AV_LOG_DEBUG, " {\n");
398 int size = bytestream_get_be16(&data);
400 memcpy(buf, data, size);
403 av_log(ctx, AV_LOG_DEBUG, " }\n");
407 if (data + size >= data_end || data + size < data)
410 av_log(ctx, AV_LOG_DEBUG, " %s: ", buf);
411 ff_amf_tag_contents(ctx, data, data_end);
412 t = ff_amf_tag_size(data, data_end);
413 if (t < 0 || data + t >= data_end)
418 case AMF_DATA_TYPE_OBJECT_END:
419 av_log(ctx, AV_LOG_DEBUG, " }\n");
426 void ff_rtmp_packet_dump(void *ctx, RTMPPacket *p)
428 av_log(ctx, AV_LOG_DEBUG, "RTMP packet type '%s'(%d) for channel %d, timestamp %d, extra field %d size %d\n",
429 rtmp_packet_type(p->type), p->type, p->channel_id, p->timestamp, p->extra, p->data_size);
430 if (p->type == RTMP_PT_INVOKE || p->type == RTMP_PT_NOTIFY) {
431 uint8_t *src = p->data, *src_end = p->data + p->data_size;
432 while (src < src_end) {
434 ff_amf_tag_contents(ctx, src, src_end);
435 sz = ff_amf_tag_size(src, src_end);
440 } else if (p->type == RTMP_PT_SERVER_BW){
441 av_log(ctx, AV_LOG_DEBUG, "Server BW = %d\n", AV_RB32(p->data));
442 } else if (p->type == RTMP_PT_CLIENT_BW){
443 av_log(ctx, AV_LOG_DEBUG, "Client BW = %d\n", AV_RB32(p->data));
444 } else if (p->type != RTMP_PT_AUDIO && p->type != RTMP_PT_VIDEO && p->type != RTMP_PT_METADATA) {
446 for (i = 0; i < p->data_size; i++)
447 av_log(ctx, AV_LOG_DEBUG, " %02X", p->data[i]);
448 av_log(ctx, AV_LOG_DEBUG, "\n");