FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 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 
64 {
65  bytestream_put_byte(dst, AMF_DATA_TYPE_NULL);
66 }
67 
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 
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 
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 
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 
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 
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 extra = 0;
173  enum RTMPPacketType type;
174  int written = 0;
175  int ret, toread;
176  RTMPPacket *prev_pkt;
177 
178  written++;
179  channel_id = hdr & 0x3F;
180 
181  if (channel_id < 2) { //special case for channel number >= 64
182  buf[1] = 0;
183  if (ffurl_read_complete(h, buf, channel_id + 1) != channel_id + 1)
184  return AVERROR(EIO);
185  written += channel_id + 1;
186  channel_id = AV_RL16(buf) + 64;
187  }
188  if ((ret = ff_rtmp_check_alloc_array(prev_pkt_ptr, nb_prev_pkt,
189  channel_id)) < 0)
190  return ret;
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;
197  if (hdr == RTMP_PS_ONEBYTE) {
198  timestamp = prev_pkt[channel_id].ts_delta;
199  } else {
200  if (ffurl_read_complete(h, buf, 3) != 3)
201  return AVERROR(EIO);
202  written += 3;
203  timestamp = AV_RB24(buf);
204  if (hdr != RTMP_PS_FOURBYTES) {
205  if (ffurl_read_complete(h, buf, 3) != 3)
206  return AVERROR(EIO);
207  written += 3;
208  size = AV_RB24(buf);
209  if (ffurl_read_complete(h, buf, 1) != 1)
210  return AVERROR(EIO);
211  written++;
212  type = buf[0];
213  if (hdr == RTMP_PS_TWELVEBYTES) {
214  if (ffurl_read_complete(h, buf, 4) != 4)
215  return AVERROR(EIO);
216  written += 4;
217  extra = AV_RL32(buf);
218  }
219  }
220  if (timestamp == 0xFFFFFF) {
221  if (ffurl_read_complete(h, buf, 4) != 4)
222  return AVERROR(EIO);
223  timestamp = AV_RB32(buf);
224  }
225  }
226  if (hdr != RTMP_PS_TWELVEBYTES)
227  timestamp += prev_pkt[channel_id].timestamp;
228 
229  if (!prev_pkt[channel_id].read) {
230  if ((ret = ff_rtmp_packet_create(p, channel_id, type, timestamp,
231  size)) < 0)
232  return ret;
233  p->read = written;
234  p->offset = 0;
235  prev_pkt[channel_id].ts_delta = timestamp -
236  prev_pkt[channel_id].timestamp;
237  prev_pkt[channel_id].timestamp = timestamp;
238  } else {
239  // previous packet in this channel hasn't completed reading
240  RTMPPacket *prev = &prev_pkt[channel_id];
241  p->data = prev->data;
242  p->size = prev->size;
243  p->channel_id = prev->channel_id;
244  p->type = prev->type;
245  p->ts_delta = prev->ts_delta;
246  p->extra = prev->extra;
247  p->offset = prev->offset;
248  p->read = prev->read + written;
249  p->timestamp = prev->timestamp;
250  prev->data = NULL;
251  }
252  p->extra = extra;
253  // save history
254  prev_pkt[channel_id].channel_id = channel_id;
255  prev_pkt[channel_id].type = type;
256  prev_pkt[channel_id].size = size;
257  prev_pkt[channel_id].extra = extra;
258  size = size - p->offset;
259 
260  toread = FFMIN(size, chunk_size);
261  if (ffurl_read_complete(h, p->data + p->offset, toread) != toread) {
263  return AVERROR(EIO);
264  }
265  size -= toread;
266  p->read += toread;
267  p->offset += toread;
268 
269  if (size > 0) {
270  RTMPPacket *prev = &prev_pkt[channel_id];
271  prev->data = p->data;
272  prev->read = p->read;
273  prev->offset = p->offset;
274  return AVERROR(EAGAIN);
275  }
276 
277  prev_pkt[channel_id].read = 0; // read complete; reset if needed
278  return p->read;
279 }
280 
282  RTMPPacket **prev_pkt, int *nb_prev_pkt,
283  uint8_t hdr)
284 {
285  while (1) {
286  int ret = rtmp_packet_read_one_chunk(h, p, chunk_size, prev_pkt,
287  nb_prev_pkt, hdr);
288  if (ret > 0 || ret != AVERROR(EAGAIN))
289  return ret;
290 
291  if (ffurl_read(h, &hdr, 1) != 1)
292  return AVERROR(EIO);
293  }
294 }
295 
297  int chunk_size, RTMPPacket **prev_pkt_ptr,
298  int *nb_prev_pkt)
299 {
300  uint8_t pkt_hdr[16], *p = pkt_hdr;
302  int off = 0;
303  int written = 0;
304  int ret;
305  RTMPPacket *prev_pkt;
306 
307  if ((ret = ff_rtmp_check_alloc_array(prev_pkt_ptr, nb_prev_pkt,
308  pkt->channel_id)) < 0)
309  return ret;
310  prev_pkt = *prev_pkt_ptr;
311 
312  pkt->ts_delta = pkt->timestamp - prev_pkt[pkt->channel_id].timestamp;
313 
314  //if channel_id = 0, this is first presentation of prev_pkt, send full hdr.
315  if (prev_pkt[pkt->channel_id].channel_id &&
316  pkt->extra == prev_pkt[pkt->channel_id].extra &&
317  pkt->timestamp >= prev_pkt[pkt->channel_id].timestamp) {
318  if (pkt->type == prev_pkt[pkt->channel_id].type &&
319  pkt->size == prev_pkt[pkt->channel_id].size) {
320  mode = RTMP_PS_FOURBYTES;
321  if (pkt->ts_delta == prev_pkt[pkt->channel_id].ts_delta)
322  mode = RTMP_PS_ONEBYTE;
323  } else {
324  mode = RTMP_PS_EIGHTBYTES;
325  }
326  }
327 
328  if (pkt->channel_id < 64) {
329  bytestream_put_byte(&p, pkt->channel_id | (mode << 6));
330  } else if (pkt->channel_id < 64 + 256) {
331  bytestream_put_byte(&p, 0 | (mode << 6));
332  bytestream_put_byte(&p, pkt->channel_id - 64);
333  } else {
334  bytestream_put_byte(&p, 1 | (mode << 6));
335  bytestream_put_le16(&p, pkt->channel_id - 64);
336  }
337  if (mode != RTMP_PS_ONEBYTE) {
338  uint32_t timestamp = pkt->timestamp;
339  if (mode != RTMP_PS_TWELVEBYTES)
340  timestamp = pkt->ts_delta;
341  bytestream_put_be24(&p, timestamp >= 0xFFFFFF ? 0xFFFFFF : timestamp);
342  if (mode != RTMP_PS_FOURBYTES) {
343  bytestream_put_be24(&p, pkt->size);
344  bytestream_put_byte(&p, pkt->type);
345  if (mode == RTMP_PS_TWELVEBYTES)
346  bytestream_put_le32(&p, pkt->extra);
347  }
348  if (timestamp >= 0xFFFFFF)
349  bytestream_put_be32(&p, timestamp);
350  }
351  // save history
352  prev_pkt[pkt->channel_id].channel_id = pkt->channel_id;
353  prev_pkt[pkt->channel_id].type = pkt->type;
354  prev_pkt[pkt->channel_id].size = pkt->size;
355  prev_pkt[pkt->channel_id].timestamp = pkt->timestamp;
356  if (mode != RTMP_PS_TWELVEBYTES) {
357  prev_pkt[pkt->channel_id].ts_delta = pkt->ts_delta;
358  } else {
359  prev_pkt[pkt->channel_id].ts_delta = pkt->timestamp;
360  }
361  prev_pkt[pkt->channel_id].extra = pkt->extra;
362 
363  if ((ret = ffurl_write(h, pkt_hdr, p - pkt_hdr)) < 0)
364  return ret;
365  written = p - pkt_hdr + pkt->size;
366  while (off < pkt->size) {
367  int towrite = FFMIN(chunk_size, pkt->size - off);
368  if ((ret = ffurl_write(h, pkt->data + off, towrite)) < 0)
369  return ret;
370  off += towrite;
371  if (off < pkt->size) {
372  uint8_t marker = 0xC0 | pkt->channel_id;
373  if ((ret = ffurl_write(h, &marker, 1)) < 0)
374  return ret;
375  written++;
376  }
377  }
378  return written;
379 }
380 
382  int timestamp, int size)
383 {
384  if (size) {
385  pkt->data = av_malloc(size);
386  if (!pkt->data)
387  return AVERROR(ENOMEM);
388  }
389  pkt->size = size;
390  pkt->channel_id = channel_id;
391  pkt->type = type;
392  pkt->timestamp = timestamp;
393  pkt->extra = 0;
394  pkt->ts_delta = 0;
395 
396  return 0;
397 }
398 
400 {
401  if (!pkt)
402  return;
403  av_freep(&pkt->data);
404  pkt->size = 0;
405 }
406 
407 int ff_amf_tag_size(const uint8_t *data, const uint8_t *data_end)
408 {
409  const uint8_t *base = data;
411  unsigned nb = -1;
412  int parse_key = 1;
413 
414  if (data >= data_end)
415  return -1;
416  switch ((type = *data++)) {
417  case AMF_DATA_TYPE_NUMBER: return 9;
418  case AMF_DATA_TYPE_BOOL: return 2;
419  case AMF_DATA_TYPE_STRING: return 3 + AV_RB16(data);
420  case AMF_DATA_TYPE_LONG_STRING: return 5 + AV_RB32(data);
421  case AMF_DATA_TYPE_NULL: return 1;
422  case AMF_DATA_TYPE_ARRAY:
423  parse_key = 0;
425  nb = bytestream_get_be32(&data);
427  while (nb-- > 0 || type != AMF_DATA_TYPE_ARRAY) {
428  int t;
429  if (parse_key) {
430  int size = bytestream_get_be16(&data);
431  if (!size) {
432  data++;
433  break;
434  }
435  if (size < 0 || size >= data_end - data)
436  return -1;
437  data += size;
438  }
439  t = ff_amf_tag_size(data, data_end);
440  if (t < 0 || t >= data_end - data)
441  return -1;
442  data += t;
443  }
444  return data - base;
445  case AMF_DATA_TYPE_OBJECT_END: return 1;
446  default: return -1;
447  }
448 }
449 
450 int ff_amf_get_field_value(const uint8_t *data, const uint8_t *data_end,
451  const uint8_t *name, uint8_t *dst, int dst_size)
452 {
453  int namelen = strlen(name);
454  int len;
455 
456  while (*data != AMF_DATA_TYPE_OBJECT && data < data_end) {
457  len = ff_amf_tag_size(data, data_end);
458  if (len < 0)
459  len = data_end - data;
460  data += len;
461  }
462  if (data_end - data < 3)
463  return -1;
464  data++;
465  for (;;) {
466  int size = bytestream_get_be16(&data);
467  if (!size)
468  break;
469  if (size < 0 || size >= data_end - data)
470  return -1;
471  data += size;
472  if (size == namelen && !memcmp(data-size, name, namelen)) {
473  switch (*data++) {
475  snprintf(dst, dst_size, "%g", av_int2double(AV_RB64(data)));
476  break;
477  case AMF_DATA_TYPE_BOOL:
478  snprintf(dst, dst_size, "%s", *data ? "true" : "false");
479  break;
481  len = bytestream_get_be16(&data);
482  av_strlcpy(dst, data, FFMIN(len+1, dst_size));
483  break;
484  default:
485  return -1;
486  }
487  return 0;
488  }
489  len = ff_amf_tag_size(data, data_end);
490  if (len < 0 || len >= data_end - data)
491  return -1;
492  data += len;
493  }
494  return -1;
495 }
496 
497 static const char* rtmp_packet_type(int type)
498 {
499  switch (type) {
500  case RTMP_PT_CHUNK_SIZE: return "chunk size";
501  case RTMP_PT_BYTES_READ: return "bytes read";
502  case RTMP_PT_PING: return "ping";
503  case RTMP_PT_SERVER_BW: return "server bandwidth";
504  case RTMP_PT_CLIENT_BW: return "client bandwidth";
505  case RTMP_PT_AUDIO: return "audio packet";
506  case RTMP_PT_VIDEO: return "video packet";
507  case RTMP_PT_FLEX_STREAM: return "Flex shared stream";
508  case RTMP_PT_FLEX_OBJECT: return "Flex shared object";
509  case RTMP_PT_FLEX_MESSAGE: return "Flex shared message";
510  case RTMP_PT_NOTIFY: return "notification";
511  case RTMP_PT_SHARED_OBJ: return "shared object";
512  case RTMP_PT_INVOKE: return "invoke";
513  case RTMP_PT_METADATA: return "metadata";
514  default: return "unknown";
515  }
516 }
517 
518 static void amf_tag_contents(void *ctx, const uint8_t *data,
519  const uint8_t *data_end)
520 {
521  unsigned int size, nb = -1;
522  char buf[1024];
524  int parse_key = 1;
525 
526  if (data >= data_end)
527  return;
528  switch ((type = *data++)) {
530  av_log(ctx, AV_LOG_DEBUG, " number %g\n", av_int2double(AV_RB64(data)));
531  return;
532  case AMF_DATA_TYPE_BOOL:
533  av_log(ctx, AV_LOG_DEBUG, " bool %d\n", *data);
534  return;
537  if (type == AMF_DATA_TYPE_STRING) {
538  size = bytestream_get_be16(&data);
539  } else {
540  size = bytestream_get_be32(&data);
541  }
542  size = FFMIN(size, sizeof(buf) - 1);
543  memcpy(buf, data, size);
544  buf[size] = 0;
545  av_log(ctx, AV_LOG_DEBUG, " string '%s'\n", buf);
546  return;
547  case AMF_DATA_TYPE_NULL:
548  av_log(ctx, AV_LOG_DEBUG, " NULL\n");
549  return;
550  case AMF_DATA_TYPE_ARRAY:
551  parse_key = 0;
553  nb = bytestream_get_be32(&data);
555  av_log(ctx, AV_LOG_DEBUG, " {\n");
556  while (nb-- > 0 || type != AMF_DATA_TYPE_ARRAY) {
557  int t;
558  if (parse_key) {
559  size = bytestream_get_be16(&data);
560  size = FFMIN(size, sizeof(buf) - 1);
561  if (!size) {
562  av_log(ctx, AV_LOG_DEBUG, " }\n");
563  data++;
564  break;
565  }
566  memcpy(buf, data, size);
567  buf[size] = 0;
568  if (size >= data_end - data)
569  return;
570  data += size;
571  av_log(ctx, AV_LOG_DEBUG, " %s: ", buf);
572  }
573  amf_tag_contents(ctx, data, data_end);
574  t = ff_amf_tag_size(data, data_end);
575  if (t < 0 || t >= data_end - data)
576  return;
577  data += t;
578  }
579  return;
581  av_log(ctx, AV_LOG_DEBUG, " }\n");
582  return;
583  default:
584  return;
585  }
586 }
587 
588 void ff_rtmp_packet_dump(void *ctx, RTMPPacket *p)
589 {
590  av_log(ctx, AV_LOG_DEBUG, "RTMP packet type '%s'(%d) for channel %d, timestamp %d, extra field %d size %d\n",
591  rtmp_packet_type(p->type), p->type, p->channel_id, p->timestamp, p->extra, p->size);
592  if (p->type == RTMP_PT_INVOKE || p->type == RTMP_PT_NOTIFY) {
593  uint8_t *src = p->data, *src_end = p->data + p->size;
594  while (src < src_end) {
595  int sz;
596  amf_tag_contents(ctx, src, src_end);
597  sz = ff_amf_tag_size(src, src_end);
598  if (sz < 0)
599  break;
600  src += sz;
601  }
602  } else if (p->type == RTMP_PT_SERVER_BW){
603  av_log(ctx, AV_LOG_DEBUG, "Server BW = %d\n", AV_RB32(p->data));
604  } else if (p->type == RTMP_PT_CLIENT_BW){
605  av_log(ctx, AV_LOG_DEBUG, "Client BW = %d\n", AV_RB32(p->data));
606  } else if (p->type != RTMP_PT_AUDIO && p->type != RTMP_PT_VIDEO && p->type != RTMP_PT_METADATA) {
607  int i;
608  for (i = 0; i < p->size; i++)
609  av_log(ctx, AV_LOG_DEBUG, " %02X", p->data[i]);
610  av_log(ctx, AV_LOG_DEBUG, "\n");
611  }
612 }
613 
614 int ff_amf_match_string(const uint8_t *data, int size, const char *str)
615 {
616  int len = strlen(str);
617  int amf_len, type;
618 
619  if (size < 1)
620  return 0;
621 
622  type = *data++;
623 
624  if (type != AMF_DATA_TYPE_LONG_STRING &&
625  type != AMF_DATA_TYPE_STRING)
626  return 0;
627 
628  if (type == AMF_DATA_TYPE_LONG_STRING) {
629  if ((size -= 4 + 1) < 0)
630  return 0;
631  amf_len = bytestream_get_be32(&data);
632  } else {
633  if ((size -= 2 + 1) < 0)
634  return 0;
635  amf_len = bytestream_get_be16(&data);
636  }
637 
638  if (amf_len > size)
639  return 0;
640 
641  if (amf_len != len)
642  return 0;
643 
644  return !memcmp(data, str, len);
645 }