rtmp: Support reading interleaved chunks.
[libav.git] / libavformat / rtmppkt.c
1 /*
2 * RTMP input format
3 * Copyright (c) 2009 Konstantin Shishkov
4 *
5 * This file is part of Libav.
6 *
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.
11 *
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.
16 *
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
20 */
21
22 #include "libavcodec/bytestream.h"
23 #include "libavutil/avstring.h"
24 #include "libavutil/intfloat.h"
25 #include "avformat.h"
26
27 #include "rtmppkt.h"
28 #include "flv.h"
29 #include "url.h"
30
31 void 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
37 void ff_amf_write_number(uint8_t **dst, double val)
38 {
39 bytestream_put_byte(dst, AMF_DATA_TYPE_NUMBER);
40 bytestream_put_be64(dst, av_double2int(val));
41 }
42
43 void 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
50 void ff_amf_write_string2(uint8_t **dst, const char *str1, const char *str2)
51 {
52 int len1 = 0, len2 = 0;
53 if (str1)
54 len1 = strlen(str1);
55 if (str2)
56 len2 = strlen(str2);
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);
61 }
62
63 void ff_amf_write_null(uint8_t **dst)
64 {
65 bytestream_put_byte(dst, AMF_DATA_TYPE_NULL);
66 }
67
68 void ff_amf_write_object_start(uint8_t **dst)
69 {
70 bytestream_put_byte(dst, AMF_DATA_TYPE_OBJECT);
71 }
72
73 void ff_amf_write_field_name(uint8_t **dst, const char *str)
74 {
75 bytestream_put_be16(dst, strlen(str));
76 bytestream_put_buffer(dst, str, strlen(str));
77 }
78
79 void ff_amf_write_object_end(uint8_t **dst)
80 {
81 /* first two bytes are field name length = 0,
82 * AMF object should end with it and end marker
83 */
84 bytestream_put_be24(dst, AMF_DATA_TYPE_OBJECT_END);
85 }
86
87 int ff_amf_read_bool(GetByteContext *bc, int *val)
88 {
89 if (bytestream2_get_byte(bc) != AMF_DATA_TYPE_BOOL)
90 return AVERROR_INVALIDDATA;
91 *val = bytestream2_get_byte(bc);
92 return 0;
93 }
94
95 int ff_amf_read_number(GetByteContext *bc, double *val)
96 {
97 uint64_t read;
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);
102 return 0;
103 }
104
105 int ff_amf_read_string(GetByteContext *bc, uint8_t *str,
106 int strsize, int *length)
107 {
108 int stringlen = 0;
109 int readsize;
110 if (bytestream2_get_byte(bc) != AMF_DATA_TYPE_STRING)
111 return AVERROR_INVALIDDATA;
112 stringlen = bytestream2_get_be16(bc);
113 if (stringlen + 1 > strsize)
114 return AVERROR(EINVAL);
115 readsize = bytestream2_get_buffer(bc, str, stringlen);
116 if (readsize != stringlen) {
117 av_log(NULL, AV_LOG_WARNING,
118 "Unable to read as many bytes as AMF string signaled\n");
119 }
120 str[readsize] = '\0';
121 *length = FFMIN(stringlen, readsize);
122 return 0;
123 }
124
125 int ff_amf_read_null(GetByteContext *bc)
126 {
127 if (bytestream2_get_byte(bc) != AMF_DATA_TYPE_NULL)
128 return AVERROR_INVALIDDATA;
129 return 0;
130 }
131
132 int ff_rtmp_packet_read(URLContext *h, RTMPPacket *p,
133 int chunk_size, RTMPPacket *prev_pkt)
134 {
135 uint8_t hdr;
136
137 if (ffurl_read(h, &hdr, 1) != 1)
138 return AVERROR(EIO);
139
140 return ff_rtmp_packet_read_internal(h, p, chunk_size, prev_pkt, hdr);
141 }
142
143 static int rtmp_packet_read_one_chunk(URLContext *h, RTMPPacket *p,
144 int chunk_size, RTMPPacket *prev_pkt,
145 uint8_t hdr)
146 {
147
148 uint8_t buf[16];
149 int channel_id, timestamp, size;
150 uint32_t extra = 0;
151 enum RTMPPacketType type;
152 int written = 0;
153 int ret, toread;
154
155 written++;
156 channel_id = hdr & 0x3F;
157
158 if (channel_id < 2) { //special case for channel number >= 64
159 buf[1] = 0;
160 if (ffurl_read_complete(h, buf, channel_id + 1) != channel_id + 1)
161 return AVERROR(EIO);
162 written += channel_id + 1;
163 channel_id = AV_RL16(buf) + 64;
164 }
165 size = prev_pkt[channel_id].size;
166 type = prev_pkt[channel_id].type;
167 extra = prev_pkt[channel_id].extra;
168
169 hdr >>= 6;
170 if (hdr == RTMP_PS_ONEBYTE) {
171 timestamp = prev_pkt[channel_id].ts_delta;
172 } else {
173 if (ffurl_read_complete(h, buf, 3) != 3)
174 return AVERROR(EIO);
175 written += 3;
176 timestamp = AV_RB24(buf);
177 if (hdr != RTMP_PS_FOURBYTES) {
178 if (ffurl_read_complete(h, buf, 3) != 3)
179 return AVERROR(EIO);
180 written += 3;
181 size = AV_RB24(buf);
182 if (ffurl_read_complete(h, buf, 1) != 1)
183 return AVERROR(EIO);
184 written++;
185 type = buf[0];
186 if (hdr == RTMP_PS_TWELVEBYTES) {
187 if (ffurl_read_complete(h, buf, 4) != 4)
188 return AVERROR(EIO);
189 written += 4;
190 extra = AV_RL32(buf);
191 }
192 }
193 if (timestamp == 0xFFFFFF) {
194 if (ffurl_read_complete(h, buf, 4) != 4)
195 return AVERROR(EIO);
196 timestamp = AV_RB32(buf);
197 }
198 }
199 if (hdr != RTMP_PS_TWELVEBYTES)
200 timestamp += prev_pkt[channel_id].timestamp;
201
202 if (!prev_pkt[channel_id].read) {
203 if ((ret = ff_rtmp_packet_create(p, channel_id, type, timestamp,
204 size)) < 0)
205 return ret;
206 p->read = written;
207 p->offset = 0;
208 prev_pkt[channel_id].ts_delta = timestamp -
209 prev_pkt[channel_id].timestamp;
210 prev_pkt[channel_id].timestamp = timestamp;
211 } else {
212 // previous packet in this channel hasn't completed reading
213 RTMPPacket *prev = &prev_pkt[channel_id];
214 p->data = prev->data;
215 p->size = prev->size;
216 p->channel_id = prev->channel_id;
217 p->type = prev->type;
218 p->ts_delta = prev->ts_delta;
219 p->extra = prev->extra;
220 p->offset = prev->offset;
221 p->read = prev->read + written;
222 p->timestamp = prev->timestamp;
223 prev->data = NULL;
224 }
225 p->extra = extra;
226 // save history
227 prev_pkt[channel_id].channel_id = channel_id;
228 prev_pkt[channel_id].type = type;
229 prev_pkt[channel_id].size = size;
230 prev_pkt[channel_id].extra = extra;
231 size = size - p->offset;
232
233 toread = FFMIN(size, chunk_size);
234 if (ffurl_read_complete(h, p->data + p->offset, toread) != toread) {
235 ff_rtmp_packet_destroy(p);
236 return AVERROR(EIO);
237 }
238 size -= toread;
239 p->read += toread;
240 p->offset += toread;
241
242 if (size > 0) {
243 RTMPPacket *prev = &prev_pkt[channel_id];
244 prev->data = p->data;
245 prev->read = p->read;
246 prev->offset = p->offset;
247 return AVERROR(EAGAIN);
248 }
249
250 prev_pkt[channel_id].read = 0; // read complete; reset if needed
251 return p->read;
252 }
253
254 int ff_rtmp_packet_read_internal(URLContext *h, RTMPPacket *p, int chunk_size,
255 RTMPPacket *prev_pkt, uint8_t hdr)
256 {
257 while (1) {
258 int ret = rtmp_packet_read_one_chunk(h, p, chunk_size, prev_pkt, hdr);
259 if (ret > 0 || ret != AVERROR(EAGAIN))
260 return ret;
261
262 if (ffurl_read(h, &hdr, 1) != 1)
263 return AVERROR(EIO);
264 }
265 }
266
267 int ff_rtmp_packet_write(URLContext *h, RTMPPacket *pkt,
268 int chunk_size, RTMPPacket *prev_pkt)
269 {
270 uint8_t pkt_hdr[16], *p = pkt_hdr;
271 int mode = RTMP_PS_TWELVEBYTES;
272 int off = 0;
273 int written = 0;
274 int ret;
275
276 pkt->ts_delta = pkt->timestamp - prev_pkt[pkt->channel_id].timestamp;
277
278 //if channel_id = 0, this is first presentation of prev_pkt, send full hdr.
279 if (prev_pkt[pkt->channel_id].channel_id &&
280 pkt->extra == prev_pkt[pkt->channel_id].extra) {
281 if (pkt->type == prev_pkt[pkt->channel_id].type &&
282 pkt->size == prev_pkt[pkt->channel_id].size) {
283 mode = RTMP_PS_FOURBYTES;
284 if (pkt->ts_delta == prev_pkt[pkt->channel_id].ts_delta)
285 mode = RTMP_PS_ONEBYTE;
286 } else {
287 mode = RTMP_PS_EIGHTBYTES;
288 }
289 }
290
291 if (pkt->channel_id < 64) {
292 bytestream_put_byte(&p, pkt->channel_id | (mode << 6));
293 } else if (pkt->channel_id < 64 + 256) {
294 bytestream_put_byte(&p, 0 | (mode << 6));
295 bytestream_put_byte(&p, pkt->channel_id - 64);
296 } else {
297 bytestream_put_byte(&p, 1 | (mode << 6));
298 bytestream_put_le16(&p, pkt->channel_id - 64);
299 }
300 if (mode != RTMP_PS_ONEBYTE) {
301 uint32_t timestamp = pkt->timestamp;
302 if (mode != RTMP_PS_TWELVEBYTES)
303 timestamp = pkt->ts_delta;
304 bytestream_put_be24(&p, timestamp >= 0xFFFFFF ? 0xFFFFFF : timestamp);
305 if (mode != RTMP_PS_FOURBYTES) {
306 bytestream_put_be24(&p, pkt->size);
307 bytestream_put_byte(&p, pkt->type);
308 if (mode == RTMP_PS_TWELVEBYTES)
309 bytestream_put_le32(&p, pkt->extra);
310 }
311 if (timestamp >= 0xFFFFFF)
312 bytestream_put_be32(&p, timestamp);
313 }
314 // save history
315 prev_pkt[pkt->channel_id].channel_id = pkt->channel_id;
316 prev_pkt[pkt->channel_id].type = pkt->type;
317 prev_pkt[pkt->channel_id].size = pkt->size;
318 prev_pkt[pkt->channel_id].timestamp = pkt->timestamp;
319 if (mode != RTMP_PS_TWELVEBYTES) {
320 prev_pkt[pkt->channel_id].ts_delta = pkt->ts_delta;
321 } else {
322 prev_pkt[pkt->channel_id].ts_delta = pkt->timestamp;
323 }
324 prev_pkt[pkt->channel_id].extra = pkt->extra;
325
326 if ((ret = ffurl_write(h, pkt_hdr, p - pkt_hdr)) < 0)
327 return ret;
328 written = p - pkt_hdr + pkt->size;
329 while (off < pkt->size) {
330 int towrite = FFMIN(chunk_size, pkt->size - off);
331 if ((ret = ffurl_write(h, pkt->data + off, towrite)) < 0)
332 return ret;
333 off += towrite;
334 if (off < pkt->size) {
335 uint8_t marker = 0xC0 | pkt->channel_id;
336 if ((ret = ffurl_write(h, &marker, 1)) < 0)
337 return ret;
338 written++;
339 }
340 }
341 return written;
342 }
343
344 int ff_rtmp_packet_create(RTMPPacket *pkt, int channel_id, RTMPPacketType type,
345 int timestamp, int size)
346 {
347 if (size) {
348 pkt->data = av_malloc(size);
349 if (!pkt->data)
350 return AVERROR(ENOMEM);
351 }
352 pkt->size = size;
353 pkt->channel_id = channel_id;
354 pkt->type = type;
355 pkt->timestamp = timestamp;
356 pkt->extra = 0;
357 pkt->ts_delta = 0;
358
359 return 0;
360 }
361
362 void ff_rtmp_packet_destroy(RTMPPacket *pkt)
363 {
364 if (!pkt)
365 return;
366 av_freep(&pkt->data);
367 pkt->size = 0;
368 }
369
370 int ff_amf_tag_size(const uint8_t *data, const uint8_t *data_end)
371 {
372 const uint8_t *base = data;
373
374 if (data >= data_end)
375 return -1;
376 switch (*data++) {
377 case AMF_DATA_TYPE_NUMBER: return 9;
378 case AMF_DATA_TYPE_BOOL: return 2;
379 case AMF_DATA_TYPE_STRING: return 3 + AV_RB16(data);
380 case AMF_DATA_TYPE_LONG_STRING: return 5 + AV_RB32(data);
381 case AMF_DATA_TYPE_NULL: return 1;
382 case AMF_DATA_TYPE_ARRAY:
383 data += 4;
384 case AMF_DATA_TYPE_OBJECT:
385 for (;;) {
386 int size = bytestream_get_be16(&data);
387 int t;
388 if (!size) {
389 data++;
390 break;
391 }
392 if (size < 0 || size >= data_end - data)
393 return -1;
394 data += size;
395 t = ff_amf_tag_size(data, data_end);
396 if (t < 0 || t >= data_end - data)
397 return -1;
398 data += t;
399 }
400 return data - base;
401 case AMF_DATA_TYPE_OBJECT_END: return 1;
402 default: return -1;
403 }
404 }
405
406 int ff_amf_get_field_value(const uint8_t *data, const uint8_t *data_end,
407 const uint8_t *name, uint8_t *dst, int dst_size)
408 {
409 int namelen = strlen(name);
410 int len;
411
412 while (*data != AMF_DATA_TYPE_OBJECT && data < data_end) {
413 len = ff_amf_tag_size(data, data_end);
414 if (len < 0)
415 len = data_end - data;
416 data += len;
417 }
418 if (data_end - data < 3)
419 return -1;
420 data++;
421 for (;;) {
422 int size = bytestream_get_be16(&data);
423 if (!size)
424 break;
425 if (size < 0 || size >= data_end - data)
426 return -1;
427 data += size;
428 if (size == namelen && !memcmp(data-size, name, namelen)) {
429 switch (*data++) {
430 case AMF_DATA_TYPE_NUMBER:
431 snprintf(dst, dst_size, "%g", av_int2double(AV_RB64(data)));
432 break;
433 case AMF_DATA_TYPE_BOOL:
434 snprintf(dst, dst_size, "%s", *data ? "true" : "false");
435 break;
436 case AMF_DATA_TYPE_STRING:
437 len = bytestream_get_be16(&data);
438 av_strlcpy(dst, data, FFMIN(len+1, dst_size));
439 break;
440 default:
441 return -1;
442 }
443 return 0;
444 }
445 len = ff_amf_tag_size(data, data_end);
446 if (len < 0 || len >= data_end - data)
447 return -1;
448 data += len;
449 }
450 return -1;
451 }
452
453 static const char* rtmp_packet_type(int type)
454 {
455 switch (type) {
456 case RTMP_PT_CHUNK_SIZE: return "chunk size";
457 case RTMP_PT_BYTES_READ: return "bytes read";
458 case RTMP_PT_PING: return "ping";
459 case RTMP_PT_SERVER_BW: return "server bandwidth";
460 case RTMP_PT_CLIENT_BW: return "client bandwidth";
461 case RTMP_PT_AUDIO: return "audio packet";
462 case RTMP_PT_VIDEO: return "video packet";
463 case RTMP_PT_FLEX_STREAM: return "Flex shared stream";
464 case RTMP_PT_FLEX_OBJECT: return "Flex shared object";
465 case RTMP_PT_FLEX_MESSAGE: return "Flex shared message";
466 case RTMP_PT_NOTIFY: return "notification";
467 case RTMP_PT_SHARED_OBJ: return "shared object";
468 case RTMP_PT_INVOKE: return "invoke";
469 case RTMP_PT_METADATA: return "metadata";
470 default: return "unknown";
471 }
472 }
473
474 static void amf_tag_contents(void *ctx, const uint8_t *data,
475 const uint8_t *data_end)
476 {
477 unsigned int size;
478 char buf[1024];
479
480 if (data >= data_end)
481 return;
482 switch (*data++) {
483 case AMF_DATA_TYPE_NUMBER:
484 av_log(ctx, AV_LOG_DEBUG, " number %g\n", av_int2double(AV_RB64(data)));
485 return;
486 case AMF_DATA_TYPE_BOOL:
487 av_log(ctx, AV_LOG_DEBUG, " bool %d\n", *data);
488 return;
489 case AMF_DATA_TYPE_STRING:
490 case AMF_DATA_TYPE_LONG_STRING:
491 if (data[-1] == AMF_DATA_TYPE_STRING) {
492 size = bytestream_get_be16(&data);
493 } else {
494 size = bytestream_get_be32(&data);
495 }
496 size = FFMIN(size, sizeof(buf) - 1);
497 memcpy(buf, data, size);
498 buf[size] = 0;
499 av_log(ctx, AV_LOG_DEBUG, " string '%s'\n", buf);
500 return;
501 case AMF_DATA_TYPE_NULL:
502 av_log(ctx, AV_LOG_DEBUG, " NULL\n");
503 return;
504 case AMF_DATA_TYPE_ARRAY:
505 data += 4;
506 case AMF_DATA_TYPE_OBJECT:
507 av_log(ctx, AV_LOG_DEBUG, " {\n");
508 for (;;) {
509 int t;
510 size = bytestream_get_be16(&data);
511 av_strlcpy(buf, data, FFMIN(sizeof(buf), size + 1));
512 if (!size) {
513 av_log(ctx, AV_LOG_DEBUG, " }\n");
514 data++;
515 break;
516 }
517 if (size >= data_end - data)
518 return;
519 data += size;
520 av_log(ctx, AV_LOG_DEBUG, " %s: ", buf);
521 amf_tag_contents(ctx, data, data_end);
522 t = ff_amf_tag_size(data, data_end);
523 if (t < 0 || t >= data_end - data)
524 return;
525 data += t;
526 }
527 return;
528 case AMF_DATA_TYPE_OBJECT_END:
529 av_log(ctx, AV_LOG_DEBUG, " }\n");
530 return;
531 default:
532 return;
533 }
534 }
535
536 void ff_rtmp_packet_dump(void *ctx, RTMPPacket *p)
537 {
538 av_log(ctx, AV_LOG_DEBUG, "RTMP packet type '%s'(%d) for channel %d, timestamp %d, extra field %d size %d\n",
539 rtmp_packet_type(p->type), p->type, p->channel_id, p->timestamp, p->extra, p->size);
540 if (p->type == RTMP_PT_INVOKE || p->type == RTMP_PT_NOTIFY) {
541 uint8_t *src = p->data, *src_end = p->data + p->size;
542 while (src < src_end) {
543 int sz;
544 amf_tag_contents(ctx, src, src_end);
545 sz = ff_amf_tag_size(src, src_end);
546 if (sz < 0)
547 break;
548 src += sz;
549 }
550 } else if (p->type == RTMP_PT_SERVER_BW){
551 av_log(ctx, AV_LOG_DEBUG, "Server BW = %d\n", AV_RB32(p->data));
552 } else if (p->type == RTMP_PT_CLIENT_BW){
553 av_log(ctx, AV_LOG_DEBUG, "Client BW = %d\n", AV_RB32(p->data));
554 } else if (p->type != RTMP_PT_AUDIO && p->type != RTMP_PT_VIDEO && p->type != RTMP_PT_METADATA) {
555 int i;
556 for (i = 0; i < p->size; i++)
557 av_log(ctx, AV_LOG_DEBUG, " %02X", p->data[i]);
558 av_log(ctx, AV_LOG_DEBUG, "\n");
559 }
560 }
561
562 int ff_amf_match_string(const uint8_t *data, int size, const char *str)
563 {
564 int len = strlen(str);
565 int amf_len, type;
566
567 if (size < 1)
568 return 0;
569
570 type = *data++;
571
572 if (type != AMF_DATA_TYPE_LONG_STRING &&
573 type != AMF_DATA_TYPE_STRING)
574 return 0;
575
576 if (type == AMF_DATA_TYPE_LONG_STRING) {
577 if ((size -= 4 + 1) < 0)
578 return 0;
579 amf_len = bytestream_get_be32(&data);
580 } else {
581 if ((size -= 2 + 1) < 0)
582 return 0;
583 amf_len = bytestream_get_be16(&data);
584 }
585
586 if (amf_len > size)
587 return 0;
588
589 if (amf_len != len)
590 return 0;
591
592 return !memcmp(data, str, len);
593 }