FFmpeg  4.4.8
rtmppkt.c
Go to the documentation of this file.
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 
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 #define MAX_DEPTH 16 ///< arbitrary limit to prevent unbounded recursion
32 
33 void ff_amf_write_bool(uint8_t **dst, int val)
34 {
35  bytestream_put_byte(dst, AMF_DATA_TYPE_BOOL);
36  bytestream_put_byte(dst, val);
37 }
38 
39 void ff_amf_write_number(uint8_t **dst, double val)
40 {
41  bytestream_put_byte(dst, AMF_DATA_TYPE_NUMBER);
42  bytestream_put_be64(dst, av_double2int(val));
43 }
44 
45 void ff_amf_write_string(uint8_t **dst, const char *str)
46 {
47  bytestream_put_byte(dst, AMF_DATA_TYPE_STRING);
48  bytestream_put_be16(dst, strlen(str));
49  bytestream_put_buffer(dst, str, strlen(str));
50 }
51 
52 void ff_amf_write_string2(uint8_t **dst, const char *str1, const char *str2)
53 {
54  int len1 = 0, len2 = 0;
55  if (str1)
56  len1 = strlen(str1);
57  if (str2)
58  len2 = strlen(str2);
59  bytestream_put_byte(dst, AMF_DATA_TYPE_STRING);
60  bytestream_put_be16(dst, len1 + len2);
61  bytestream_put_buffer(dst, str1, len1);
62  bytestream_put_buffer(dst, str2, len2);
63 }
64 
66 {
67  bytestream_put_byte(dst, AMF_DATA_TYPE_NULL);
68 }
69 
71 {
72  bytestream_put_byte(dst, AMF_DATA_TYPE_OBJECT);
73 }
74 
75 void ff_amf_write_field_name(uint8_t **dst, const char *str)
76 {
77  bytestream_put_be16(dst, strlen(str));
78  bytestream_put_buffer(dst, str, strlen(str));
79 }
80 
82 {
83  /* first two bytes are field name length = 0,
84  * AMF object should end with it and end marker
85  */
86  bytestream_put_be24(dst, AMF_DATA_TYPE_OBJECT_END);
87 }
88 
90 {
91  uint64_t read;
92  if (bytestream2_get_byte(bc) != AMF_DATA_TYPE_NUMBER)
93  return AVERROR_INVALIDDATA;
94  read = bytestream2_get_be64(bc);
95  *val = av_int2double(read);
96  return 0;
97 }
98 
100  int strsize, int *length)
101 {
102  int stringlen = 0;
103  int readsize;
104  stringlen = bytestream2_get_be16(bc);
105  if (stringlen + 1 > strsize)
106  return AVERROR(EINVAL);
107  readsize = bytestream2_get_buffer(bc, str, stringlen);
108  if (readsize != stringlen) {
110  "Unable to read as many bytes as AMF string signaled\n");
111  }
112  str[readsize] = '\0';
113  *length = FFMIN(stringlen, readsize);
114  return 0;
115 }
116 
118  int strsize, int *length)
119 {
120  if (bytestream2_get_byte(bc) != AMF_DATA_TYPE_STRING)
121  return AVERROR_INVALIDDATA;
122  return ff_amf_get_string(bc, str, strsize, length);
123 }
124 
126 {
127  if (bytestream2_get_byte(bc) != AMF_DATA_TYPE_NULL)
128  return AVERROR_INVALIDDATA;
129  return 0;
130 }
131 
132 int ff_rtmp_check_alloc_array(RTMPPacket **prev_pkt, int *nb_prev_pkt,
133  int channel)
134 {
135  int nb_alloc;
136  RTMPPacket *ptr;
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.
144  ptr = av_realloc_array(*prev_pkt, nb_alloc, sizeof(**prev_pkt));
145  if (!ptr)
146  return AVERROR(ENOMEM);
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 {
156  uint8_t hdr;
157 
158  if (ffurl_read(h, &hdr, 1) != 1)
159  return AVERROR(EIO);
160 
161  return ff_rtmp_packet_read_internal(h, p, chunk_size, prev_pkt,
162  nb_prev_pkt, hdr);
163 }
164 
166  int chunk_size, RTMPPacket **prev_pkt_ptr,
167  int *nb_prev_pkt, uint8_t hdr)
168 {
169 
170  uint8_t buf[16];
171  int channel_id, timestamp, size;
172  uint32_t ts_field; // non-extended timestamp or delta field
173  uint32_t extra = 0;
174  enum RTMPPacketType type;
175  int written = 0;
176  int ret, toread;
177  RTMPPacket *prev_pkt;
178 
179  written++;
180  channel_id = hdr & 0x3F;
181 
182  if (channel_id < 2) { //special case for channel number >= 64
183  buf[1] = 0;
184  if (ffurl_read_complete(h, buf, channel_id + 1) != channel_id + 1)
185  return AVERROR(EIO);
186  written += channel_id + 1;
187  channel_id = AV_RL16(buf) + 64;
188  }
189  if ((ret = ff_rtmp_check_alloc_array(prev_pkt_ptr, nb_prev_pkt,
190  channel_id)) < 0)
191  return ret;
192  prev_pkt = *prev_pkt_ptr;
193  size = prev_pkt[channel_id].size;
194  type = prev_pkt[channel_id].type;
195  extra = prev_pkt[channel_id].extra;
196 
197  hdr >>= 6; // header size indicator
198  if (hdr == RTMP_PS_ONEBYTE) {
199  ts_field = prev_pkt[channel_id].ts_field;
200  } else {
201  if (ffurl_read_complete(h, buf, 3) != 3)
202  return AVERROR(EIO);
203  written += 3;
204  ts_field = AV_RB24(buf);
205  if (hdr != RTMP_PS_FOURBYTES) {
206  if (ffurl_read_complete(h, buf, 3) != 3)
207  return AVERROR(EIO);
208  written += 3;
209  size = AV_RB24(buf);
210  if (ffurl_read_complete(h, buf, 1) != 1)
211  return AVERROR(EIO);
212  written++;
213  type = buf[0];
214  if (hdr == RTMP_PS_TWELVEBYTES) {
215  if (ffurl_read_complete(h, buf, 4) != 4)
216  return AVERROR(EIO);
217  written += 4;
218  extra = AV_RL32(buf);
219  }
220  }
221  }
222  if (ts_field == 0xFFFFFF) {
223  if (ffurl_read_complete(h, buf, 4) != 4)
224  return AVERROR(EIO);
225  timestamp = AV_RB32(buf);
226  } else {
227  timestamp = ts_field;
228  }
229  if (hdr != RTMP_PS_TWELVEBYTES)
230  timestamp += prev_pkt[channel_id].timestamp;
231 
232  if (prev_pkt[channel_id].read && size != prev_pkt[channel_id].size) {
233  av_log(h, AV_LOG_ERROR, "RTMP packet size mismatch %d != %d\n",
234  size, prev_pkt[channel_id].size);
235  ff_rtmp_packet_destroy(&prev_pkt[channel_id]);
236  prev_pkt[channel_id].read = 0;
237  return AVERROR_INVALIDDATA;
238  }
239 
240  if (!prev_pkt[channel_id].read) {
241  if ((ret = ff_rtmp_packet_create(p, channel_id, type, timestamp,
242  size)) < 0)
243  return ret;
244  p->read = written;
245  p->offset = 0;
246  prev_pkt[channel_id].ts_field = ts_field;
247  prev_pkt[channel_id].timestamp = timestamp;
248  } else {
249  // previous packet in this channel hasn't completed reading
250  RTMPPacket *prev = &prev_pkt[channel_id];
251  p->data = prev->data;
252  p->size = prev->size;
253  p->channel_id = prev->channel_id;
254  p->type = prev->type;
255  p->ts_field = prev->ts_field;
256  p->extra = prev->extra;
257  p->offset = prev->offset;
258  p->read = prev->read + written;
259  p->timestamp = prev->timestamp;
260  prev->data = NULL;
261  }
262  p->extra = extra;
263  // save history
264  prev_pkt[channel_id].channel_id = channel_id;
265  prev_pkt[channel_id].type = type;
266  prev_pkt[channel_id].size = size;
267  prev_pkt[channel_id].extra = extra;
268  size = size - p->offset;
269 
270  toread = FFMIN(size, chunk_size);
271  if (ffurl_read_complete(h, p->data + p->offset, toread) != toread) {
273  return AVERROR(EIO);
274  }
275  size -= toread;
276  p->read += toread;
277  p->offset += toread;
278 
279  if (size > 0) {
280  RTMPPacket *prev = &prev_pkt[channel_id];
281  prev->data = p->data;
282  prev->read = p->read;
283  prev->offset = p->offset;
284  p->data = NULL;
285  return AVERROR(EAGAIN);
286  }
287 
288  prev_pkt[channel_id].read = 0; // read complete; reset if needed
289  return p->read;
290 }
291 
293  RTMPPacket **prev_pkt, int *nb_prev_pkt,
294  uint8_t hdr)
295 {
296  while (1) {
297  int ret = rtmp_packet_read_one_chunk(h, p, chunk_size, prev_pkt,
298  nb_prev_pkt, hdr);
299  if (ret > 0 || ret != AVERROR(EAGAIN))
300  return ret;
301 
302  if (ffurl_read(h, &hdr, 1) != 1)
303  return AVERROR(EIO);
304  }
305 }
306 
308  int chunk_size, RTMPPacket **prev_pkt_ptr,
309  int *nb_prev_pkt)
310 {
311  uint8_t pkt_hdr[16], *p = pkt_hdr;
313  int off = 0;
314  int written = 0;
315  int ret;
316  RTMPPacket *prev_pkt;
317  int use_delta; // flag if using timestamp delta, not RTMP_PS_TWELVEBYTES
318  uint32_t timestamp; // full 32-bit timestamp or delta value
319 
320  if ((ret = ff_rtmp_check_alloc_array(prev_pkt_ptr, nb_prev_pkt,
321  pkt->channel_id)) < 0)
322  return ret;
323  prev_pkt = *prev_pkt_ptr;
324 
325  //if channel_id = 0, this is first presentation of prev_pkt, send full hdr.
326  use_delta = prev_pkt[pkt->channel_id].channel_id &&
327  pkt->extra == prev_pkt[pkt->channel_id].extra &&
328  pkt->timestamp >= prev_pkt[pkt->channel_id].timestamp;
329 
330  timestamp = pkt->timestamp;
331  if (use_delta) {
332  timestamp -= prev_pkt[pkt->channel_id].timestamp;
333  }
334  if (timestamp >= 0xFFFFFF) {
335  pkt->ts_field = 0xFFFFFF;
336  } else {
337  pkt->ts_field = timestamp;
338  }
339 
340  if (use_delta) {
341  if (pkt->type == prev_pkt[pkt->channel_id].type &&
342  pkt->size == prev_pkt[pkt->channel_id].size) {
344  if (pkt->ts_field == prev_pkt[pkt->channel_id].ts_field)
346  } else {
348  }
349  }
350 
351  if (pkt->channel_id < 64) {
352  bytestream_put_byte(&p, pkt->channel_id | (mode << 6));
353  } else if (pkt->channel_id < 64 + 256) {
354  bytestream_put_byte(&p, 0 | (mode << 6));
355  bytestream_put_byte(&p, pkt->channel_id - 64);
356  } else {
357  bytestream_put_byte(&p, 1 | (mode << 6));
358  bytestream_put_le16(&p, pkt->channel_id - 64);
359  }
360  if (mode != RTMP_PS_ONEBYTE) {
361  bytestream_put_be24(&p, pkt->ts_field);
362  if (mode != RTMP_PS_FOURBYTES) {
363  bytestream_put_be24(&p, pkt->size);
364  bytestream_put_byte(&p, pkt->type);
365  if (mode == RTMP_PS_TWELVEBYTES)
366  bytestream_put_le32(&p, pkt->extra);
367  }
368  }
369  if (pkt->ts_field == 0xFFFFFF)
370  bytestream_put_be32(&p, timestamp);
371  // save history
372  prev_pkt[pkt->channel_id].channel_id = pkt->channel_id;
373  prev_pkt[pkt->channel_id].type = pkt->type;
374  prev_pkt[pkt->channel_id].size = pkt->size;
375  prev_pkt[pkt->channel_id].timestamp = pkt->timestamp;
376  prev_pkt[pkt->channel_id].ts_field = pkt->ts_field;
377  prev_pkt[pkt->channel_id].extra = pkt->extra;
378 
379  if ((ret = ffurl_write(h, pkt_hdr, p - pkt_hdr)) < 0)
380  return ret;
381  written = p - pkt_hdr + pkt->size;
382  while (off < pkt->size) {
383  int towrite = FFMIN(chunk_size, pkt->size - off);
384  if ((ret = ffurl_write(h, pkt->data + off, towrite)) < 0)
385  return ret;
386  off += towrite;
387  if (off < pkt->size) {
388  uint8_t marker = 0xC0 | pkt->channel_id;
389  if ((ret = ffurl_write(h, &marker, 1)) < 0)
390  return ret;
391  written++;
392  if (pkt->ts_field == 0xFFFFFF) {
393  uint8_t ts_header[4];
394  AV_WB32(ts_header, timestamp);
395  if ((ret = ffurl_write(h, ts_header, 4)) < 0)
396  return ret;
397  written += 4;
398  }
399  }
400  }
401  return written;
402 }
403 
405  int timestamp, int size)
406 {
407  if (size) {
408  pkt->data = av_realloc(NULL, size);
409  if (!pkt->data)
410  return AVERROR(ENOMEM);
411  }
412  pkt->size = size;
413  pkt->channel_id = channel_id;
414  pkt->type = type;
415  pkt->timestamp = timestamp;
416  pkt->extra = 0;
417  pkt->ts_field = 0;
418 
419  return 0;
420 }
421 
423 {
424  if (!pkt)
425  return;
426  av_freep(&pkt->data);
427  pkt->size = 0;
428 }
429 
430 static int amf_tag_skip(GetByteContext *gb, int depth)
431 {
433  unsigned nb = -1;
434 
435  if (bytestream2_get_bytes_left(gb) < 1)
436  return -1;
437 
438  if (depth > MAX_DEPTH) {
439  av_log(NULL, AV_LOG_ERROR, "amf_tag_skip: exceeded max depth\n");
440  return AVERROR_PATCHWELCOME;
441  }
442 
443  type = bytestream2_get_byte(gb);
444  switch (type) {
446  bytestream2_get_be64(gb);
447  return 0;
448  case AMF_DATA_TYPE_BOOL:
449  bytestream2_get_byte(gb);
450  return 0;
452  bytestream2_skip(gb, bytestream2_get_be16(gb));
453  return 0;
455  bytestream2_skip(gb, bytestream2_get_be32(gb));
456  return 0;
457  case AMF_DATA_TYPE_NULL:
458  return 0;
459  case AMF_DATA_TYPE_DATE:
460  bytestream2_skip(gb, 10);
461  return 0;
462  case AMF_DATA_TYPE_ARRAY:
464  nb = bytestream2_get_be32(gb);
466  while (type != AMF_DATA_TYPE_ARRAY || nb-- > 0) {
467  int t;
468  if (type != AMF_DATA_TYPE_ARRAY) {
469  int size = bytestream2_get_be16(gb);
470  if (!size) {
471  bytestream2_get_byte(gb);
472  break;
473  }
474  if (size < 0 || size >= bytestream2_get_bytes_left(gb))
475  return -1;
476  bytestream2_skip(gb, size);
477  }
478  t = amf_tag_skip(gb, depth + 1);
479  if (t < 0 || bytestream2_get_bytes_left(gb) <= 0)
480  return -1;
481  }
482  return 0;
483  case AMF_DATA_TYPE_OBJECT_END: return 0;
484  default: return -1;
485  }
486 }
487 
488 int ff_amf_tag_size(const uint8_t *data, const uint8_t *data_end)
489 {
490  GetByteContext gb;
491  int ret;
492 
493  if (data >= data_end)
494  return -1;
495 
496  bytestream2_init(&gb, data, data_end - data);
497 
498  ret = amf_tag_skip(&gb, 0);
499  if (ret < 0 || bytestream2_get_bytes_left(&gb) <= 0)
500  return -1;
501  av_assert0(bytestream2_tell(&gb) >= 0 && bytestream2_tell(&gb) <= data_end - data);
502  return bytestream2_tell(&gb);
503 }
504 
506  const uint8_t *name, uint8_t *dst, int dst_size)
507 {
508  int namelen = strlen(name);
509  int len;
510 
511  while (bytestream2_peek_byte(gb) != AMF_DATA_TYPE_OBJECT && bytestream2_get_bytes_left(gb) > 0) {
512  int ret = amf_tag_skip(gb, 0);
513  if (ret < 0)
514  return -1;
515  }
516  if (bytestream2_get_bytes_left(gb) < 3)
517  return -1;
518  bytestream2_get_byte(gb);
519 
520  for (;;) {
521  int size = bytestream2_get_be16(gb);
522  if (!size)
523  break;
524  if (size < 0 || size >= bytestream2_get_bytes_left(gb))
525  return -1;
526  bytestream2_skip(gb, size);
527  if (size == namelen && !memcmp(gb->buffer-size, name, namelen)) {
528  switch (bytestream2_get_byte(gb)) {
530  snprintf(dst, dst_size, "%g", av_int2double(bytestream2_get_be64(gb)));
531  break;
532  case AMF_DATA_TYPE_BOOL:
533  snprintf(dst, dst_size, "%s", bytestream2_get_byte(gb) ? "true" : "false");
534  break;
536  len = bytestream2_get_be16(gb);
537  if (dst_size < 1)
538  return -1;
539  if (dst_size < len + 1)
540  len = dst_size - 1;
541  bytestream2_get_buffer(gb, dst, len);
542  dst[len] = 0;
543  break;
544  default:
545  return -1;
546  }
547  return 0;
548  }
549  len = amf_tag_skip(gb, 0);
550  if (len < 0 || bytestream2_get_bytes_left(gb) <= 0)
551  return -1;
552  }
553  return -1;
554 }
555 
556 int ff_amf_get_field_value(const uint8_t *data, const uint8_t *data_end,
557  const uint8_t *name, uint8_t *dst, int dst_size)
558 {
559  GetByteContext gb;
560 
561  if (data >= data_end)
562  return -1;
563 
564  bytestream2_init(&gb, data, data_end - data);
565 
566  return amf_get_field_value2(&gb, name, dst, dst_size);
567 }
568 
569 #ifdef DEBUG
570 static const char* rtmp_packet_type(int type)
571 {
572  switch (type) {
573  case RTMP_PT_CHUNK_SIZE: return "chunk size";
574  case RTMP_PT_BYTES_READ: return "bytes read";
575  case RTMP_PT_USER_CONTROL: return "user control";
576  case RTMP_PT_WINDOW_ACK_SIZE: return "window acknowledgement size";
577  case RTMP_PT_SET_PEER_BW: return "set peer bandwidth";
578  case RTMP_PT_AUDIO: return "audio packet";
579  case RTMP_PT_VIDEO: return "video packet";
580  case RTMP_PT_FLEX_STREAM: return "Flex shared stream";
581  case RTMP_PT_FLEX_OBJECT: return "Flex shared object";
582  case RTMP_PT_FLEX_MESSAGE: return "Flex shared message";
583  case RTMP_PT_NOTIFY: return "notification";
584  case RTMP_PT_SHARED_OBJ: return "shared object";
585  case RTMP_PT_INVOKE: return "invoke";
586  case RTMP_PT_METADATA: return "metadata";
587  default: return "unknown";
588  }
589 }
590 
591 static void amf_tag_contents(void *ctx, const uint8_t *data,
592  const uint8_t *data_end, int depth)
593 {
594  unsigned int size, nb = -1;
595  char buf[1024];
597  int parse_key = 1;
598 
599  if (depth > MAX_DEPTH) {
600  av_log(NULL, AV_LOG_ERROR, "amf_tag_contents: exceeded max depth\n");
601  return;
602  }
603 
604  if (data >= data_end)
605  return;
606  switch ((type = *data++)) {
608  av_log(ctx, AV_LOG_DEBUG, " number %g\n", av_int2double(AV_RB64(data)));
609  return;
610  case AMF_DATA_TYPE_BOOL:
611  av_log(ctx, AV_LOG_DEBUG, " bool %d\n", *data);
612  return;
615  if (type == AMF_DATA_TYPE_STRING) {
616  size = bytestream_get_be16(&data);
617  } else {
618  size = bytestream_get_be32(&data);
619  }
620  size = FFMIN(size, sizeof(buf) - 1);
621  memcpy(buf, data, size);
622  buf[size] = 0;
623  av_log(ctx, AV_LOG_DEBUG, " string '%s'\n", buf);
624  return;
625  case AMF_DATA_TYPE_NULL:
626  av_log(ctx, AV_LOG_DEBUG, " NULL\n");
627  return;
628  case AMF_DATA_TYPE_ARRAY:
629  parse_key = 0;
631  nb = bytestream_get_be32(&data);
633  av_log(ctx, AV_LOG_DEBUG, " {\n");
634  while (nb-- > 0 || type != AMF_DATA_TYPE_ARRAY) {
635  int t;
636  if (parse_key) {
637  size = bytestream_get_be16(&data);
638  size = FFMIN(size, sizeof(buf) - 1);
639  if (!size) {
640  av_log(ctx, AV_LOG_DEBUG, " }\n");
641  data++;
642  break;
643  }
644  memcpy(buf, data, size);
645  buf[size] = 0;
646  if (size >= data_end - data)
647  return;
648  data += size;
649  av_log(ctx, AV_LOG_DEBUG, " %s: ", buf);
650  }
651  amf_tag_contents(ctx, data, data_end, depth + 1);
652  t = ff_amf_tag_size(data, data_end);
653  if (t < 0 || t >= data_end - data)
654  return;
655  data += t;
656  }
657  return;
659  av_log(ctx, AV_LOG_DEBUG, " }\n");
660  return;
661  default:
662  return;
663  }
664 }
665 
666 void ff_rtmp_packet_dump(void *ctx, RTMPPacket *p)
667 {
668  av_log(ctx, AV_LOG_DEBUG, "RTMP packet type '%s'(%d) for channel %d, timestamp %d, extra field %d size %d\n",
669  rtmp_packet_type(p->type), p->type, p->channel_id, p->timestamp, p->extra, p->size);
670  if (p->type == RTMP_PT_INVOKE || p->type == RTMP_PT_NOTIFY) {
671  uint8_t *src = p->data, *src_end = p->data + p->size;
672  while (src < src_end) {
673  int sz;
674  amf_tag_contents(ctx, src, src_end, 0);
675  sz = ff_amf_tag_size(src, src_end);
676  if (sz < 0)
677  break;
678  src += sz;
679  }
680  } else if (p->type == RTMP_PT_WINDOW_ACK_SIZE) {
681  av_log(ctx, AV_LOG_DEBUG, "Window acknowledgement size = %d\n", AV_RB32(p->data));
682  } else if (p->type == RTMP_PT_SET_PEER_BW) {
683  av_log(ctx, AV_LOG_DEBUG, "Set Peer BW = %d\n", AV_RB32(p->data));
684  } else if (p->type != RTMP_PT_AUDIO && p->type != RTMP_PT_VIDEO && p->type != RTMP_PT_METADATA) {
685  int i;
686  for (i = 0; i < p->size; i++)
687  av_log(ctx, AV_LOG_DEBUG, " %02X", p->data[i]);
688  av_log(ctx, AV_LOG_DEBUG, "\n");
689  }
690 }
691 #endif
692 
693 int ff_amf_match_string(const uint8_t *data, int size, const char *str)
694 {
695  int len = strlen(str);
696  int amf_len, type;
697 
698  if (size < 1)
699  return 0;
700 
701  type = *data++;
702 
705  return 0;
706 
708  if ((size -= 4 + 1) < 0)
709  return 0;
710  amf_len = bytestream_get_be32(&data);
711  } else {
712  if ((size -= 2 + 1) < 0)
713  return 0;
714  amf_len = bytestream_get_be16(&data);
715  }
716 
717  if (amf_len > size)
718  return 0;
719 
720  if (amf_len != len)
721  return 0;
722 
723  return !memcmp(data, str, len);
724 }
static double val(void *priv, double ch)
Definition: aeval.c:76
uint8_t
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
Main libavformat public API header.
int ffurl_read(URLContext *h, unsigned char *buf, int size)
Read up to size bytes from the resource accessed by h, and store the read bytes in buf.
Definition: avio.c:404
int ffurl_write(URLContext *h, const unsigned char *buf, int size)
Write size bytes from buf to the resource accessed by h.
Definition: avio.c:418
int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
Read as many bytes as possible (up to size), calling the read function multiple times if necessary.
Definition: avio.c:411
#define AV_RB24
Definition: intreadwrite.h:64
#define AV_RL16
Definition: intreadwrite.h:42
#define AV_RB32
Definition: intreadwrite.h:130
#define AV_RL32
Definition: intreadwrite.h:146
#define AV_RB64
Definition: intreadwrite.h:164
static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition: bytestream.h:267
static av_always_inline void bytestream_put_buffer(uint8_t **b, const uint8_t *src, unsigned int size)
Definition: bytestream.h:372
static av_always_inline int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:158
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:137
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:168
static av_always_inline int bytestream2_tell(GetByteContext *g)
Definition: bytestream.h:192
#define FFMIN(a, b)
Definition: common.h:105
#define NULL
Definition: coverity.c:32
static int parse_key(DBEContext *s)
Definition: dolby_e.c:616
mode
Use these values in ebur128_init (or'ed).
Definition: ebur128.h:83
channel
Use these values when setting the channel map with ebur128_set_channel().
Definition: ebur128.h:39
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:215
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
void * av_realloc(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory.
Definition: mem.c:134
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
Allocate, reallocate, or free an array.
Definition: mem.c:198
cl_device_type type
int i
Definition: input.c:407
static av_always_inline uint64_t av_double2int(double f)
Reinterpret a double as a 64-bit integer.
Definition: intfloat.h:70
static av_always_inline double av_int2double(uint64_t i)
Reinterpret a 64-bit integer as a double.
Definition: intfloat.h:60
#define AV_WB32(p, v)
Definition: intreadwrite.h:419
FLV common header.
AMFDataType
Definition: flv.h:123
@ AMF_DATA_TYPE_OBJECT_END
Definition: flv.h:132
@ AMF_DATA_TYPE_LONG_STRING
Definition: flv.h:135
@ AMF_DATA_TYPE_OBJECT
Definition: flv.h:127
@ AMF_DATA_TYPE_BOOL
Definition: flv.h:125
@ AMF_DATA_TYPE_NUMBER
Definition: flv.h:124
@ AMF_DATA_TYPE_MIXEDARRAY
Definition: flv.h:131
@ AMF_DATA_TYPE_NULL
Definition: flv.h:128
@ AMF_DATA_TYPE_STRING
Definition: flv.h:126
@ AMF_DATA_TYPE_ARRAY
Definition: flv.h:133
@ AMF_DATA_TYPE_DATE
Definition: flv.h:134
const char data[16]
Definition: mxf.c:142
const char * name
Definition: qsvenc.c:46
void ff_amf_write_string(uint8_t **dst, const char *str)
Write string in AMF format to buffer.
Definition: rtmppkt.c:45
void ff_amf_write_null(uint8_t **dst)
Write AMF NULL value to buffer.
Definition: rtmppkt.c:65
void ff_amf_write_number(uint8_t **dst, double val)
Write number in AMF format to buffer.
Definition: rtmppkt.c:39
int ff_rtmp_packet_read(URLContext *h, RTMPPacket *p, int chunk_size, RTMPPacket **prev_pkt, int *nb_prev_pkt)
Read RTMP packet sent by the server.
Definition: rtmppkt.c:153
static int rtmp_packet_read_one_chunk(URLContext *h, RTMPPacket *p, int chunk_size, RTMPPacket **prev_pkt_ptr, int *nb_prev_pkt, uint8_t hdr)
Definition: rtmppkt.c:165
int ff_rtmp_packet_write(URLContext *h, RTMPPacket *pkt, int chunk_size, RTMPPacket **prev_pkt_ptr, int *nb_prev_pkt)
Send RTMP packet to the server.
Definition: rtmppkt.c:307
void ff_amf_write_object_start(uint8_t **dst)
Write marker for AMF object to buffer.
Definition: rtmppkt.c:70
void ff_amf_write_string2(uint8_t **dst, const char *str1, const char *str2)
Write a string consisting of two parts in AMF format to a buffer.
Definition: rtmppkt.c:52
#define MAX_DEPTH
arbitrary limit to prevent unbounded recursion
Definition: rtmppkt.c:31
int ff_amf_get_string(GetByteContext *bc, uint8_t *str, int strsize, int *length)
Get AMF string value.
Definition: rtmppkt.c:99
int ff_rtmp_check_alloc_array(RTMPPacket **prev_pkt, int *nb_prev_pkt, int channel)
Enlarge the prev_pkt array to fit the given channel.
Definition: rtmppkt.c:132
int ff_rtmp_packet_read_internal(URLContext *h, RTMPPacket *p, int chunk_size, RTMPPacket **prev_pkt, int *nb_prev_pkt, uint8_t hdr)
Read internal RTMP packet sent by the server.
Definition: rtmppkt.c:292
int ff_amf_read_null(GetByteContext *bc)
Read AMF NULL value.
Definition: rtmppkt.c:125
static int amf_get_field_value2(GetByteContext *gb, const uint8_t *name, uint8_t *dst, int dst_size)
Definition: rtmppkt.c:505
int ff_amf_tag_size(const uint8_t *data, const uint8_t *data_end)
Calculate number of bytes taken by first AMF entry in data.
Definition: rtmppkt.c:488
void ff_amf_write_bool(uint8_t **dst, int val)
Write boolean value in AMF format to buffer.
Definition: rtmppkt.c:33
int ff_amf_get_field_value(const uint8_t *data, const uint8_t *data_end, const uint8_t *name, uint8_t *dst, int dst_size)
Retrieve value of given AMF object field in string form.
Definition: rtmppkt.c:556
int ff_rtmp_packet_create(RTMPPacket *pkt, int channel_id, RTMPPacketType type, int timestamp, int size)
Create new RTMP packet with given attributes.
Definition: rtmppkt.c:404
void ff_rtmp_packet_destroy(RTMPPacket *pkt)
Free RTMP packet.
Definition: rtmppkt.c:422
int ff_amf_match_string(const uint8_t *data, int size, const char *str)
Match AMF string with a NULL-terminated string.
Definition: rtmppkt.c:693
void ff_amf_write_object_end(uint8_t **dst)
Write marker for end of AMF object to buffer.
Definition: rtmppkt.c:81
void ff_amf_write_field_name(uint8_t **dst, const char *str)
Write string used as field name in AMF object to buffer.
Definition: rtmppkt.c:75
int ff_amf_read_string(GetByteContext *bc, uint8_t *str, int strsize, int *length)
Read AMF string value.
Definition: rtmppkt.c:117
static int amf_tag_skip(GetByteContext *gb, int depth)
Definition: rtmppkt.c:430
int ff_amf_read_number(GetByteContext *bc, double *val)
Read AMF number value.
Definition: rtmppkt.c:89
@ RTMP_PS_TWELVEBYTES
packet has 12-byte header
Definition: rtmppkt.h:68
@ RTMP_PS_EIGHTBYTES
packet has 8-byte header
Definition: rtmppkt.h:69
@ RTMP_PS_FOURBYTES
packet has 4-byte header
Definition: rtmppkt.h:70
@ RTMP_PS_ONEBYTE
packet is really a next chunk of a packet
Definition: rtmppkt.h:71
void ff_rtmp_packet_dump(void *ctx, RTMPPacket *p)
Print information and contents of RTMP packet.
RTMPPacketType
known RTMP packet types
Definition: rtmppkt.h:47
@ RTMP_PT_FLEX_OBJECT
Flex shared object.
Definition: rtmppkt.h:56
@ RTMP_PT_USER_CONTROL
user control
Definition: rtmppkt.h:50
@ RTMP_PT_NOTIFY
some notification
Definition: rtmppkt.h:58
@ RTMP_PT_SHARED_OBJ
shared object
Definition: rtmppkt.h:59
@ RTMP_PT_CHUNK_SIZE
chunk size change
Definition: rtmppkt.h:48
@ RTMP_PT_INVOKE
invoke some stream action
Definition: rtmppkt.h:60
@ RTMP_PT_BYTES_READ
number of bytes read
Definition: rtmppkt.h:49
@ RTMP_PT_SET_PEER_BW
peer bandwidth
Definition: rtmppkt.h:52
@ RTMP_PT_WINDOW_ACK_SIZE
window acknowledgement size
Definition: rtmppkt.h:51
@ RTMP_PT_VIDEO
video packet
Definition: rtmppkt.h:54
@ RTMP_PT_FLEX_STREAM
Flex shared stream.
Definition: rtmppkt.h:55
@ RTMP_PT_METADATA
FLV metadata.
Definition: rtmppkt.h:61
@ RTMP_PT_AUDIO
audio packet
Definition: rtmppkt.h:53
@ RTMP_PT_FLEX_MESSAGE
Flex shared message.
Definition: rtmppkt.h:57
#define snprintf
Definition: snprintf.h:34
int size
Definition: packet.h:370
uint8_t * data
Definition: packet.h:369
const uint8_t * buffer
Definition: bytestream.h:34
structure for holding RTMP packets
Definition: rtmppkt.h:77
int size
packet payload size
Definition: rtmppkt.h:84
uint32_t extra
probably an additional channel ID used during streaming data
Definition: rtmppkt.h:82
RTMPPacketType type
packet payload type
Definition: rtmppkt.h:79
uint32_t ts_field
24-bit timestamp or increment to the previous one, in milliseconds (latter only for media packets)....
Definition: rtmppkt.h:81
uint32_t timestamp
packet full timestamp
Definition: rtmppkt.h:80
uint8_t * data
packet payload
Definition: rtmppkt.h:83
int channel_id
RTMP channel ID (nothing to do with audio/video channels though)
Definition: rtmppkt.h:78
int read
amount read, including headers
Definition: rtmppkt.h:86
int offset
amount of data read so far
Definition: rtmppkt.h:85
Definition: url.h:38
#define av_freep(p)
#define av_log(a,...)
#define src
Definition: vp8dsp.c:255
AVPacket * pkt
Definition: movenc.c:59
AVFormatContext * ctx
Definition: movenc.c:48
int size
unbuffered private I/O API
int len