1 /*
2 * RTMP input format
3 * Copyright (c) 2009 Konstantin Shishkov
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
21
26
30
32 {
34 bytestream_put_byte(dst, val);
35 }
36
38 {
41 }
42
44 {
46 bytestream_put_be16(dst, strlen(str));
48 }
49
51 {
52 int len1 = 0, len2 = 0;
53 if (str1)
54 len1 = strlen(str1);
55 if (str2)
56 len2 = strlen(str2);
58 bytestream_put_be16(dst, len1 + len2);
61 }
62
64 {
66 }
67
69 {
71 }
72
74 {
75 bytestream_put_be16(dst, strlen(str));
77 }
78
80 {
81 /* first two bytes are field name length = 0,
82 * AMF object should end with it and end marker
83 */
85 }
86
88 {
91 *val = bytestream2_get_byte(bc);
92 return 0;
93 }
94
96 {
97 uint64_t read;
100 read = bytestream2_get_be64(bc);
102 return 0;
103 }
104
107 {
108 int stringlen = 0;
109 int readsize;
112 stringlen = bytestream2_get_be16(bc);
113 if (stringlen + 1 > strsize)
116 if (readsize != stringlen) {
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
126 {
129 return 0;
130 }
131
133 int channel)
134 {
135 int nb_alloc;
137 if (channel < *nb_prev_pkt)
138 return 0;
139
140 nb_alloc = channel + 16;
141 // This can't use the av_reallocp family of functions, since we
142 // would need to free each element in the array before the array
143 // itself is freed.
145 if (!ptr)
147 memset(ptr + *nb_prev_pkt, 0, (nb_alloc - *nb_prev_pkt) * sizeof(*ptr));
148 *prev_pkt = ptr;
149 *nb_prev_pkt = nb_alloc;
150 return 0;
151 }
152
154 int chunk_size,
RTMPPacket **prev_pkt,
int *nb_prev_pkt)
155 {
157
160
162 nb_prev_pkt, hdr);
163 }
164
168 {
169
171 int channel_id, timestamp,
size;
172 uint32_t extra = 0;
174 int written = 0;
177
178 written++;
179 channel_id = hdr & 0x3F;
180
181 if (channel_id < 2) { //special case for channel number >= 64
182 buf[1] = 0;
185 written += channel_id + 1;
186 channel_id =
AV_RL16(buf) + 64;
187 }
189 channel_id)) < 0)
191 prev_pkt = *prev_pkt_ptr;
192 size = prev_pkt[channel_id].
size;
193 type = prev_pkt[channel_id].
type;
194 extra = prev_pkt[channel_id].
extra;
195
196 hdr >>= 6;
198 timestamp = prev_pkt[channel_id].
ts_delta;
199 } else {
202 written += 3;
207 written += 3;
211 written++;
212 type = buf[0];
216 written += 4;
218 }
219 }
220 if (timestamp == 0xFFFFFF) {
224 }
225 }
227 timestamp += prev_pkt[channel_id].
timestamp;
228
229 if (!prev_pkt[channel_id].read) {
231 size)) < 0)
235 prev_pkt[channel_id].
ts_delta = timestamp -
237 prev_pkt[channel_id].
timestamp = timestamp;
238 } else {
239 // previous packet in this channel hasn't completed reading
251 }
253 // save history
255 prev_pkt[channel_id].
type = type;
257 prev_pkt[channel_id].
extra = extra;
259
260 toread =
FFMIN(size, chunk_size);
264 }
265 size -= toread;
268
269 if (size > 0) {
275 }
276
277 prev_pkt[channel_id].
read = 0;
// read complete; reset if needed
279 }
280
284 {
285 while (1) {
287 nb_prev_pkt, hdr);
288 if (ret > 0 || ret !=
AVERROR(EAGAIN))
290
293 }
294 }
295
298 int *nb_prev_pkt)
299 {
300 uint8_t pkt_hdr[16], *p = pkt_hdr;
303 int written = 0;
306
310 prev_pkt = *prev_pkt_ptr;
311
313
314 //if channel_id = 0, this is first presentation of prev_pkt, send full hdr.
322 } else {
324 }
325 }
326
328 bytestream_put_byte(&p, pkt->
channel_id | (mode << 6));
330 bytestream_put_byte(&p, 0 | (mode << 6));
331 bytestream_put_byte(&p, pkt->
channel_id - 64);
332 } else {
333 bytestream_put_byte(&p, 1 | (mode << 6));
334 bytestream_put_le16(&p, pkt->
channel_id - 64);
335 }
340 bytestream_put_be24(&p, timestamp >= 0xFFFFFF ? 0xFFFFFF : timestamp);
342 bytestream_put_be24(&p, pkt->
size);
343 bytestream_put_byte(&p, pkt->
type);
345 bytestream_put_le32(&p, pkt->
extra);
346 }
347 if (timestamp >= 0xFFFFFF)
348 bytestream_put_be32(&p, timestamp);
349 }
350 // save history
357 } else {
359 }
361
362 if ((ret =
ffurl_write(h, pkt_hdr, p - pkt_hdr)) < 0)
364 written = p - pkt_hdr + pkt->
size;
365 while (off < pkt->
size) {
366 int towrite =
FFMIN(chunk_size, pkt->
size - off);
369 off += towrite;
370 if (off < pkt->size) {
374 written++;
375 }
376 }
377 return written;
378 }
379
381 int timestamp,
int size)
382 {
383 if (size) {
387 }
394
395 return 0;
396 }
397
399 {
400 if (!pkt)
401 return;
404 }
405
407 {
410 unsigned nb = -1;
411 int parse_key = 1;
412
413 if (data >= data_end)
414 return -1;
415 switch ((type = *data++)) {
422 parse_key = 0;
424 nb = bytestream_get_be32(&data);
428 if (parse_key) {
429 int size = bytestream_get_be16(&data);
430 if (!size) {
431 data++;
432 break;
433 }
434 if (size < 0 || size >= data_end - data)
435 return -1;
437 }
439 if (t < 0 || t >= data_end - data)
440 return -1;
442 }
443 return data - base;
445 default: return -1;
446 }
447 }
448
451 {
452 int namelen = strlen(name);
454
457 if (len < 0)
458 len = data_end -
data;
460 }
461 if (data_end - data < 3)
462 return -1;
463 data++;
464 for (;;) {
465 int size = bytestream_get_be16(&data);
466 if (!size)
467 break;
468 if (size < 0 || size >= data_end - data)
469 return -1;
471 if (size == namelen && !memcmp(data-size, name, namelen)) {
472 switch (*data++) {
475 break;
477 snprintf(dst, dst_size,
"%s", *data ?
"true" :
"false");
478 break;
480 len = bytestream_get_be16(&data);
482 break;
483 default:
484 return -1;
485 }
486 return 0;
487 }
489 if (len < 0 || len >= data_end - data)
490 return -1;
492 }
493 return -1;
494 }
495
497 {
498 switch (type) {
513 default: return "unknown";
514 }
515 }
516
519 {
520 unsigned int size, nb = -1;
523 int parse_key = 1;
524
525 if (data >= data_end)
526 return;
527 switch ((type = *data++)) {
530 return;
533 return;
537 size = bytestream_get_be16(&data);
538 } else {
539 size = bytestream_get_be32(&data);
540 }
541 size =
FFMIN(size,
sizeof(buf) - 1);
542 memcpy(buf, data, size);
545 return;
548 return;
550 parse_key = 0;
552 nb = bytestream_get_be32(&data);
557 if (parse_key) {
558 size = bytestream_get_be16(&data);
559 size =
FFMIN(size,
sizeof(buf) - 1);
560 if (!size) {
562 data++;
563 break;
564 }
565 memcpy(buf, data, size);
567 if (size >= data_end - data)
568 return;
571 }
574 if (t < 0 || t >= data_end - data)
575 return;
577 }
578 return;
581 return;
582 default:
583 return;
584 }
585 }
586
588 {
589 av_log(ctx,
AV_LOG_DEBUG,
"RTMP packet type '%s'(%d) for channel %d, timestamp %d, extra field %d size %d\n",
593 while (src < src_end) {
594 int sz;
597 if (sz < 0)
598 break;
599 src += sz;
600 }
606 int i;
607 for (i = 0; i < p->
size; i++)
610 }
611 }
612
614 {
615 int len = strlen(str);
617
618 if (size < 1)
619 return 0;
620
621 type = *data++;
622
625 return 0;
626
628 if ((size -= 4 + 1) < 0)
629 return 0;
630 amf_len = bytestream_get_be32(&data);
631 } else {
632 if ((size -= 2 + 1) < 0)
633 return 0;
634 amf_len = bytestream_get_be16(&data);
635 }
636
637 if (amf_len > size)
638 return 0;
639
640 if (amf_len != len)
641 return 0;
642
643 return !memcmp(data, str, len);
644 }