rtmp: Return a proper error code in handle_invoke_error
[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
50468f93
JO
74int ff_amf_read_bool(GetByteContext *bc, int *val)
75{
76 if (bytestream2_get_byte(bc) != AMF_DATA_TYPE_BOOL)
77 return AVERROR_INVALIDDATA;
78 *val = bytestream2_get_byte(bc);
79 return 0;
80}
81
82int ff_amf_read_number(GetByteContext *bc, double *val)
83{
84 uint64_t read;
85 if (bytestream2_get_byte(bc) != AMF_DATA_TYPE_NUMBER)
86 return AVERROR_INVALIDDATA;
87 read = bytestream2_get_be64(bc);
88 *val = av_int2double(read);
89 return 0;
90}
91
92int ff_amf_read_string(GetByteContext *bc, uint8_t *str,
93 int strsize, int *length)
94{
95 int stringlen = 0;
96 int readsize;
97 if (bytestream2_get_byte(bc) != AMF_DATA_TYPE_STRING)
98 return AVERROR_INVALIDDATA;
99 stringlen = bytestream2_get_be16(bc);
100 if (stringlen + 1 > strsize)
101 return AVERROR(EINVAL);
102 readsize = bytestream2_get_buffer(bc, str, stringlen);
103 if (readsize != stringlen) {
104 av_log(NULL, AV_LOG_WARNING,
105 "Unable to read as many bytes as AMF string signaled\n");
106 }
107 str[readsize] = '\0';
108 *length = FFMIN(stringlen, readsize);
109 return 0;
110}
111
112int ff_amf_read_null(GetByteContext *bc)
113{
114 if (bytestream2_get_byte(bc) != AMF_DATA_TYPE_NULL)
115 return AVERROR_INVALIDDATA;
116 return 0;
117}
118
9fd6b843
KS
119int ff_rtmp_packet_read(URLContext *h, RTMPPacket *p,
120 int chunk_size, RTMPPacket *prev_pkt)
121{
7dc747f5
SP
122 uint8_t hdr;
123
124 if (ffurl_read(h, &hdr, 1) != 1)
125 return AVERROR(EIO);
126
127 return ff_rtmp_packet_read_internal(h, p, chunk_size, prev_pkt, hdr);
128}
129
130int ff_rtmp_packet_read_internal(URLContext *h, RTMPPacket *p, int chunk_size,
131 RTMPPacket *prev_pkt, uint8_t hdr)
132{
133
134 uint8_t t, buf[16];
9fd6b843
KS
135 int channel_id, timestamp, data_size, offset = 0;
136 uint32_t extra = 0;
0f87b771 137 enum RTMPPacketType type;
0b6b10d9 138 int size = 0;
f645f1d6 139 int ret;
9fd6b843 140
0b6b10d9 141 size++;
9fd6b843
KS
142 channel_id = hdr & 0x3F;
143
5d660598
KS
144 if (channel_id < 2) { //special case for channel number >= 64
145 buf[1] = 0;
dce37564 146 if (ffurl_read_complete(h, buf, channel_id + 1) != channel_id + 1)
5d660598 147 return AVERROR(EIO);
0b6b10d9 148 size += channel_id + 1;
5d660598
KS
149 channel_id = AV_RL16(buf) + 64;
150 }
9fd6b843
KS
151 data_size = prev_pkt[channel_id].data_size;
152 type = prev_pkt[channel_id].type;
153 extra = prev_pkt[channel_id].extra;
154
155 hdr >>= 6;
156 if (hdr == RTMP_PS_ONEBYTE) {
a352b605 157 timestamp = prev_pkt[channel_id].ts_delta;
9fd6b843 158 } else {
dce37564 159 if (ffurl_read_complete(h, buf, 3) != 3)
9fd6b843 160 return AVERROR(EIO);
0b6b10d9 161 size += 3;
9fd6b843
KS
162 timestamp = AV_RB24(buf);
163 if (hdr != RTMP_PS_FOURBYTES) {
dce37564 164 if (ffurl_read_complete(h, buf, 3) != 3)
9fd6b843 165 return AVERROR(EIO);
0b6b10d9 166 size += 3;
9fd6b843 167 data_size = AV_RB24(buf);
dce37564 168 if (ffurl_read_complete(h, buf, 1) != 1)
9fd6b843 169 return AVERROR(EIO);
0b6b10d9 170 size++;
1ab21f19 171 type = buf[0];
9fd6b843 172 if (hdr == RTMP_PS_TWELVEBYTES) {
dce37564 173 if (ffurl_read_complete(h, buf, 4) != 4)
9fd6b843 174 return AVERROR(EIO);
0b6b10d9 175 size += 4;
9fd6b843
KS
176 extra = AV_RL32(buf);
177 }
178 }
59b6482a 179 if (timestamp == 0xFFFFFF) {
dce37564 180 if (ffurl_read_complete(h, buf, 4) != 4)
59b6482a
S
181 return AVERROR(EIO);
182 timestamp = AV_RB32(buf);
183 }
9fd6b843 184 }
a352b605
S
185 if (hdr != RTMP_PS_TWELVEBYTES)
186 timestamp += prev_pkt[channel_id].timestamp;
187
f645f1d6
SP
188 if ((ret = ff_rtmp_packet_create(p, channel_id, type, timestamp,
189 data_size)) < 0)
190 return ret;
9fd6b843
KS
191 p->extra = extra;
192 // save history
193 prev_pkt[channel_id].channel_id = channel_id;
194 prev_pkt[channel_id].type = type;
195 prev_pkt[channel_id].data_size = data_size;
a352b605 196 prev_pkt[channel_id].ts_delta = timestamp - prev_pkt[channel_id].timestamp;
9fd6b843
KS
197 prev_pkt[channel_id].timestamp = timestamp;
198 prev_pkt[channel_id].extra = extra;
199 while (data_size > 0) {
200 int toread = FFMIN(data_size, chunk_size);
dce37564 201 if (ffurl_read_complete(h, p->data + offset, toread) != toread) {
9fd6b843
KS
202 ff_rtmp_packet_destroy(p);
203 return AVERROR(EIO);
204 }
205 data_size -= chunk_size;
206 offset += chunk_size;
0b6b10d9 207 size += chunk_size;
9fd6b843 208 if (data_size > 0) {
bba287fd
SP
209 if ((ret = ffurl_read_complete(h, &t, 1)) < 0) { // marker
210 ff_rtmp_packet_destroy(p);
211 return ret;
212 }
0b6b10d9 213 size++;
9fd6b843
KS
214 if (t != (0xC0 + channel_id))
215 return -1;
216 }
217 }
0b6b10d9 218 return size;
9fd6b843
KS
219}
220
221int ff_rtmp_packet_write(URLContext *h, RTMPPacket *pkt,
222 int chunk_size, RTMPPacket *prev_pkt)
223{
224 uint8_t pkt_hdr[16], *p = pkt_hdr;
225 int mode = RTMP_PS_TWELVEBYTES;
226 int off = 0;
0b6b10d9 227 int size = 0;
bba287fd 228 int ret;
09b67cc4 229
a352b605 230 pkt->ts_delta = pkt->timestamp - prev_pkt[pkt->channel_id].timestamp;
9fd6b843 231
84a29dc1
S
232 //if channel_id = 0, this is first presentation of prev_pkt, send full hdr.
233 if (prev_pkt[pkt->channel_id].channel_id &&
234 pkt->extra == prev_pkt[pkt->channel_id].extra) {
235 if (pkt->type == prev_pkt[pkt->channel_id].type &&
236 pkt->data_size == prev_pkt[pkt->channel_id].data_size) {
237 mode = RTMP_PS_FOURBYTES;
238 if (pkt->ts_delta == prev_pkt[pkt->channel_id].ts_delta)
239 mode = RTMP_PS_ONEBYTE;
240 } else {
241 mode = RTMP_PS_EIGHTBYTES;
242 }
243 }
244
b1eb53ab
KS
245 if (pkt->channel_id < 64) {
246 bytestream_put_byte(&p, pkt->channel_id | (mode << 6));
247 } else if (pkt->channel_id < 64 + 256) {
248 bytestream_put_byte(&p, 0 | (mode << 6));
249 bytestream_put_byte(&p, pkt->channel_id - 64);
250 } else {
251 bytestream_put_byte(&p, 1 | (mode << 6));
252 bytestream_put_le16(&p, pkt->channel_id - 64);
253 }
9fd6b843 254 if (mode != RTMP_PS_ONEBYTE) {
817d967d
KS
255 uint32_t timestamp = pkt->timestamp;
256 if (mode != RTMP_PS_TWELVEBYTES)
a352b605 257 timestamp = pkt->ts_delta;
817d967d 258 bytestream_put_be24(&p, timestamp >= 0xFFFFFF ? 0xFFFFFF : timestamp);
9fd6b843
KS
259 if (mode != RTMP_PS_FOURBYTES) {
260 bytestream_put_be24(&p, pkt->data_size);
261 bytestream_put_byte(&p, pkt->type);
262 if (mode == RTMP_PS_TWELVEBYTES)
263 bytestream_put_le32(&p, pkt->extra);
264 }
817d967d
KS
265 if (timestamp >= 0xFFFFFF)
266 bytestream_put_be32(&p, timestamp);
9fd6b843 267 }
84a29dc1
S
268 // save history
269 prev_pkt[pkt->channel_id].channel_id = pkt->channel_id;
270 prev_pkt[pkt->channel_id].type = pkt->type;
271 prev_pkt[pkt->channel_id].data_size = pkt->data_size;
272 prev_pkt[pkt->channel_id].timestamp = pkt->timestamp;
273 if (mode != RTMP_PS_TWELVEBYTES) {
274 prev_pkt[pkt->channel_id].ts_delta = pkt->ts_delta;
275 } else {
276 prev_pkt[pkt->channel_id].ts_delta = pkt->timestamp;
277 }
278 prev_pkt[pkt->channel_id].extra = pkt->extra;
279
bba287fd
SP
280 if ((ret = ffurl_write(h, pkt_hdr, p - pkt_hdr)) < 0)
281 return ret;
0b6b10d9 282 size = p - pkt_hdr + pkt->data_size;
9fd6b843
KS
283 while (off < pkt->data_size) {
284 int towrite = FFMIN(chunk_size, pkt->data_size - off);
bba287fd
SP
285 if ((ret = ffurl_write(h, pkt->data + off, towrite)) < 0)
286 return ret;
9fd6b843
KS
287 off += towrite;
288 if (off < pkt->data_size) {
289 uint8_t marker = 0xC0 | pkt->channel_id;
bba287fd
SP
290 if ((ret = ffurl_write(h, &marker, 1)) < 0)
291 return ret;
0b6b10d9 292 size++;
9fd6b843
KS
293 }
294 }
0b6b10d9 295 return size;
9fd6b843
KS
296}
297
298int ff_rtmp_packet_create(RTMPPacket *pkt, int channel_id, RTMPPacketType type,
299 int timestamp, int size)
300{
271c869c 301 if (size) {
48b1fb13
MS
302 pkt->data = av_malloc(size);
303 if (!pkt->data)
304 return AVERROR(ENOMEM);
271c869c 305 }
9fd6b843
KS
306 pkt->data_size = size;
307 pkt->channel_id = channel_id;
308 pkt->type = type;
309 pkt->timestamp = timestamp;
310 pkt->extra = 0;
a352b605 311 pkt->ts_delta = 0;
9fd6b843
KS
312
313 return 0;
314}
315
316void ff_rtmp_packet_destroy(RTMPPacket *pkt)
317{
318 if (!pkt)
319 return;
320 av_freep(&pkt->data);
321 pkt->data_size = 0;
322}
323
324int ff_amf_tag_size(const uint8_t *data, const uint8_t *data_end)
325{
326 const uint8_t *base = data;
327
328 if (data >= data_end)
329 return -1;
330 switch (*data++) {
331 case AMF_DATA_TYPE_NUMBER: return 9;
332 case AMF_DATA_TYPE_BOOL: return 2;
333 case AMF_DATA_TYPE_STRING: return 3 + AV_RB16(data);
334 case AMF_DATA_TYPE_LONG_STRING: return 5 + AV_RB32(data);
335 case AMF_DATA_TYPE_NULL: return 1;
336 case AMF_DATA_TYPE_ARRAY:
337 data += 4;
338 case AMF_DATA_TYPE_OBJECT:
339 for (;;) {
340 int size = bytestream_get_be16(&data);
341 int t;
342 if (!size) {
343 data++;
344 break;
345 }
346 if (data + size >= data_end || data + size < data)
347 return -1;
348 data += size;
349 t = ff_amf_tag_size(data, data_end);
350 if (t < 0 || data + t >= data_end)
351 return -1;
352 data += t;
353 }
354 return data - base;
355 case AMF_DATA_TYPE_OBJECT_END: return 1;
356 default: return -1;
357 }
358}
359
360int ff_amf_get_field_value(const uint8_t *data, const uint8_t *data_end,
361 const uint8_t *name, uint8_t *dst, int dst_size)
362{
363 int namelen = strlen(name);
364 int len;
365
87ca1b8f
KS
366 while (*data != AMF_DATA_TYPE_OBJECT && data < data_end) {
367 len = ff_amf_tag_size(data, data_end);
368 if (len < 0)
369 len = data_end - data;
370 data += len;
371 }
9fd6b843
KS
372 if (data_end - data < 3)
373 return -1;
87ca1b8f 374 data++;
9fd6b843
KS
375 for (;;) {
376 int size = bytestream_get_be16(&data);
377 if (!size)
378 break;
379 if (data + size >= data_end || data + size < data)
380 return -1;
381 data += size;
382 if (size == namelen && !memcmp(data-size, name, namelen)) {
383 switch (*data++) {
384 case AMF_DATA_TYPE_NUMBER:
3383a53e 385 snprintf(dst, dst_size, "%g", av_int2double(AV_RB64(data)));
9fd6b843
KS
386 break;
387 case AMF_DATA_TYPE_BOOL:
388 snprintf(dst, dst_size, "%s", *data ? "true" : "false");
389 break;
390 case AMF_DATA_TYPE_STRING:
391 len = bytestream_get_be16(&data);
392 av_strlcpy(dst, data, FFMIN(len+1, dst_size));
393 break;
394 default:
395 return -1;
396 }
397 return 0;
398 }
399 len = ff_amf_tag_size(data, data_end);
400 if (len < 0 || data + len >= data_end || data + len < data)
401 return -1;
402 data += len;
403 }
404 return -1;
405}
cfac91fe
KS
406
407static const char* rtmp_packet_type(int type)
408{
409 switch (type) {
410 case RTMP_PT_CHUNK_SIZE: return "chunk size";
411 case RTMP_PT_BYTES_READ: return "bytes read";
412 case RTMP_PT_PING: return "ping";
413 case RTMP_PT_SERVER_BW: return "server bandwidth";
414 case RTMP_PT_CLIENT_BW: return "client bandwidth";
415 case RTMP_PT_AUDIO: return "audio packet";
416 case RTMP_PT_VIDEO: return "video packet";
417 case RTMP_PT_FLEX_STREAM: return "Flex shared stream";
418 case RTMP_PT_FLEX_OBJECT: return "Flex shared object";
419 case RTMP_PT_FLEX_MESSAGE: return "Flex shared message";
420 case RTMP_PT_NOTIFY: return "notification";
421 case RTMP_PT_SHARED_OBJ: return "shared object";
422 case RTMP_PT_INVOKE: return "invoke";
423 case RTMP_PT_METADATA: return "metadata";
424 default: return "unknown";
425 }
426}
427
428static void ff_amf_tag_contents(void *ctx, const uint8_t *data, const uint8_t *data_end)
429{
25182289 430 int size;
cfac91fe
KS
431 char buf[1024];
432
433 if (data >= data_end)
434 return;
435 switch (*data++) {
436 case AMF_DATA_TYPE_NUMBER:
3383a53e 437 av_log(ctx, AV_LOG_DEBUG, " number %g\n", av_int2double(AV_RB64(data)));
cfac91fe
KS
438 return;
439 case AMF_DATA_TYPE_BOOL:
440 av_log(ctx, AV_LOG_DEBUG, " bool %d\n", *data);
441 return;
442 case AMF_DATA_TYPE_STRING:
443 case AMF_DATA_TYPE_LONG_STRING:
444 if (data[-1] == AMF_DATA_TYPE_STRING) {
445 size = bytestream_get_be16(&data);
446 } else {
f3cb1cd0 447 size = bytestream_get_be32(&data);
cfac91fe
KS
448 }
449 size = FFMIN(size, 1023);
450 memcpy(buf, data, size);
451 buf[size] = 0;
452 av_log(ctx, AV_LOG_DEBUG, " string '%s'\n", buf);
453 return;
454 case AMF_DATA_TYPE_NULL:
455 av_log(ctx, AV_LOG_DEBUG, " NULL\n");
456 return;
457 case AMF_DATA_TYPE_ARRAY:
458 data += 4;
459 case AMF_DATA_TYPE_OBJECT:
460 av_log(ctx, AV_LOG_DEBUG, " {\n");
461 for (;;) {
462 int size = bytestream_get_be16(&data);
463 int t;
464 memcpy(buf, data, size);
465 buf[size] = 0;
466 if (!size) {
467 av_log(ctx, AV_LOG_DEBUG, " }\n");
468 data++;
469 break;
470 }
471 if (data + size >= data_end || data + size < data)
472 return;
473 data += size;
474 av_log(ctx, AV_LOG_DEBUG, " %s: ", buf);
475 ff_amf_tag_contents(ctx, data, data_end);
476 t = ff_amf_tag_size(data, data_end);
477 if (t < 0 || data + t >= data_end)
478 return;
479 data += t;
480 }
481 return;
482 case AMF_DATA_TYPE_OBJECT_END:
483 av_log(ctx, AV_LOG_DEBUG, " }\n");
484 return;
485 default:
486 return;
487 }
488}
489
490void ff_rtmp_packet_dump(void *ctx, RTMPPacket *p)
491{
492 av_log(ctx, AV_LOG_DEBUG, "RTMP packet type '%s'(%d) for channel %d, timestamp %d, extra field %d size %d\n",
493 rtmp_packet_type(p->type), p->type, p->channel_id, p->timestamp, p->extra, p->data_size);
494 if (p->type == RTMP_PT_INVOKE || p->type == RTMP_PT_NOTIFY) {
495 uint8_t *src = p->data, *src_end = p->data + p->data_size;
496 while (src < src_end) {
497 int sz;
498 ff_amf_tag_contents(ctx, src, src_end);
499 sz = ff_amf_tag_size(src, src_end);
500 if (sz < 0)
501 break;
502 src += sz;
503 }
504 } else if (p->type == RTMP_PT_SERVER_BW){
505 av_log(ctx, AV_LOG_DEBUG, "Server BW = %d\n", AV_RB32(p->data));
506 } else if (p->type == RTMP_PT_CLIENT_BW){
507 av_log(ctx, AV_LOG_DEBUG, "Client BW = %d\n", AV_RB32(p->data));
508 } else if (p->type != RTMP_PT_AUDIO && p->type != RTMP_PT_VIDEO && p->type != RTMP_PT_METADATA) {
509 int i;
510 for (i = 0; i < p->data_size; i++)
511 av_log(ctx, AV_LOG_DEBUG, " %02X", p->data[i]);
512 av_log(ctx, AV_LOG_DEBUG, "\n");
513 }
514}