You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

301 lines
9.2KB

  1. /*
  2. * RTMP input format
  3. * Copyright (c) 2009 Kostya 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. #include "libavcodec/bytestream.h"
  22. #include "libavutil/avstring.h"
  23. #include "avformat.h"
  24. #include "rtmppkt.h"
  25. #include "flv.h"
  26. void ff_amf_write_bool(uint8_t **dst, int val)
  27. {
  28. bytestream_put_byte(dst, AMF_DATA_TYPE_BOOL);
  29. bytestream_put_byte(dst, val);
  30. }
  31. void ff_amf_write_number(uint8_t **dst, double val)
  32. {
  33. bytestream_put_byte(dst, AMF_DATA_TYPE_NUMBER);
  34. bytestream_put_be64(dst, av_dbl2int(val));
  35. }
  36. void ff_amf_write_string(uint8_t **dst, const char *str)
  37. {
  38. bytestream_put_byte(dst, AMF_DATA_TYPE_STRING);
  39. bytestream_put_be16(dst, strlen(str));
  40. bytestream_put_buffer(dst, str, strlen(str));
  41. }
  42. void ff_amf_write_null(uint8_t **dst)
  43. {
  44. bytestream_put_byte(dst, AMF_DATA_TYPE_NULL);
  45. }
  46. void ff_amf_write_object_start(uint8_t **dst)
  47. {
  48. bytestream_put_byte(dst, AMF_DATA_TYPE_OBJECT);
  49. }
  50. void ff_amf_write_field_name(uint8_t **dst, const char *str)
  51. {
  52. bytestream_put_be16(dst, strlen(str));
  53. bytestream_put_buffer(dst, str, strlen(str));
  54. }
  55. void ff_amf_write_object_end(uint8_t **dst)
  56. {
  57. /* first two bytes are field name length = 0,
  58. * AMF object should end with it and end marker
  59. */
  60. bytestream_put_be24(dst, AMF_DATA_TYPE_OBJECT_END);
  61. }
  62. int ff_rtmp_packet_read(URLContext *h, RTMPPacket *p,
  63. int chunk_size, RTMPPacket *prev_pkt)
  64. {
  65. uint8_t hdr, t, buf[16];
  66. int channel_id, timestamp, data_size, offset = 0;
  67. uint32_t extra = 0;
  68. enum RTMPPacketType type;
  69. if (url_read(h, &hdr, 1) != 1)
  70. return AVERROR(EIO);
  71. channel_id = hdr & 0x3F;
  72. if (channel_id < 2) { //special case for channel number >= 64
  73. buf[1] = 0;
  74. if (url_read_complete(h, buf, channel_id + 1) != channel_id + 1)
  75. return AVERROR(EIO);
  76. channel_id = AV_RL16(buf) + 64;
  77. }
  78. data_size = prev_pkt[channel_id].data_size;
  79. type = prev_pkt[channel_id].type;
  80. extra = prev_pkt[channel_id].extra;
  81. hdr >>= 6;
  82. if (hdr == RTMP_PS_ONEBYTE) {
  83. timestamp = prev_pkt[channel_id].ts_delta;
  84. } else {
  85. if (url_read_complete(h, buf, 3) != 3)
  86. return AVERROR(EIO);
  87. timestamp = AV_RB24(buf);
  88. if (hdr != RTMP_PS_FOURBYTES) {
  89. if (url_read_complete(h, buf, 3) != 3)
  90. return AVERROR(EIO);
  91. data_size = AV_RB24(buf);
  92. if (url_read_complete(h, buf, 1) != 1)
  93. return AVERROR(EIO);
  94. type = buf[0];
  95. if (hdr == RTMP_PS_TWELVEBYTES) {
  96. if (url_read_complete(h, buf, 4) != 4)
  97. return AVERROR(EIO);
  98. extra = AV_RL32(buf);
  99. }
  100. }
  101. if (timestamp == 0xFFFFFF) {
  102. if (url_read_complete(h, buf, 4) != 4)
  103. return AVERROR(EIO);
  104. timestamp = AV_RB32(buf);
  105. }
  106. }
  107. if (hdr != RTMP_PS_TWELVEBYTES)
  108. timestamp += prev_pkt[channel_id].timestamp;
  109. if (ff_rtmp_packet_create(p, channel_id, type, timestamp, data_size))
  110. return -1;
  111. p->extra = extra;
  112. // save history
  113. prev_pkt[channel_id].channel_id = channel_id;
  114. prev_pkt[channel_id].type = type;
  115. prev_pkt[channel_id].data_size = data_size;
  116. prev_pkt[channel_id].ts_delta = timestamp - prev_pkt[channel_id].timestamp;
  117. prev_pkt[channel_id].timestamp = timestamp;
  118. prev_pkt[channel_id].extra = extra;
  119. while (data_size > 0) {
  120. int toread = FFMIN(data_size, chunk_size);
  121. if (url_read_complete(h, p->data + offset, toread) != toread) {
  122. ff_rtmp_packet_destroy(p);
  123. return AVERROR(EIO);
  124. }
  125. data_size -= chunk_size;
  126. offset += chunk_size;
  127. if (data_size > 0) {
  128. url_read_complete(h, &t, 1); //marker
  129. if (t != (0xC0 + channel_id))
  130. return -1;
  131. }
  132. }
  133. return 0;
  134. }
  135. int ff_rtmp_packet_write(URLContext *h, RTMPPacket *pkt,
  136. int chunk_size, RTMPPacket *prev_pkt)
  137. {
  138. uint8_t pkt_hdr[16], *p = pkt_hdr;
  139. int mode = RTMP_PS_TWELVEBYTES;
  140. int off = 0;
  141. pkt->ts_delta = pkt->timestamp - prev_pkt[pkt->channel_id].timestamp;
  142. //TODO: header compression
  143. if (pkt->channel_id < 64) {
  144. bytestream_put_byte(&p, pkt->channel_id | (mode << 6));
  145. } else if (pkt->channel_id < 64 + 256) {
  146. bytestream_put_byte(&p, 0 | (mode << 6));
  147. bytestream_put_byte(&p, pkt->channel_id - 64);
  148. } else {
  149. bytestream_put_byte(&p, 1 | (mode << 6));
  150. bytestream_put_le16(&p, pkt->channel_id - 64);
  151. }
  152. if (mode != RTMP_PS_ONEBYTE) {
  153. uint32_t timestamp = pkt->timestamp;
  154. if (mode != RTMP_PS_TWELVEBYTES)
  155. timestamp = pkt->ts_delta;
  156. bytestream_put_be24(&p, timestamp >= 0xFFFFFF ? 0xFFFFFF : timestamp);
  157. if (mode != RTMP_PS_FOURBYTES) {
  158. bytestream_put_be24(&p, pkt->data_size);
  159. bytestream_put_byte(&p, pkt->type);
  160. if (mode == RTMP_PS_TWELVEBYTES)
  161. bytestream_put_le32(&p, pkt->extra);
  162. }
  163. if (timestamp >= 0xFFFFFF)
  164. bytestream_put_be32(&p, timestamp);
  165. }
  166. url_write(h, pkt_hdr, p-pkt_hdr);
  167. while (off < pkt->data_size) {
  168. int towrite = FFMIN(chunk_size, pkt->data_size - off);
  169. url_write(h, pkt->data + off, towrite);
  170. off += towrite;
  171. if (off < pkt->data_size) {
  172. uint8_t marker = 0xC0 | pkt->channel_id;
  173. url_write(h, &marker, 1);
  174. }
  175. }
  176. return 0;
  177. }
  178. int ff_rtmp_packet_create(RTMPPacket *pkt, int channel_id, RTMPPacketType type,
  179. int timestamp, int size)
  180. {
  181. pkt->data = av_malloc(size);
  182. if (!pkt->data)
  183. return AVERROR(ENOMEM);
  184. pkt->data_size = size;
  185. pkt->channel_id = channel_id;
  186. pkt->type = type;
  187. pkt->timestamp = timestamp;
  188. pkt->extra = 0;
  189. pkt->ts_delta = 0;
  190. return 0;
  191. }
  192. void ff_rtmp_packet_destroy(RTMPPacket *pkt)
  193. {
  194. if (!pkt)
  195. return;
  196. av_freep(&pkt->data);
  197. pkt->data_size = 0;
  198. }
  199. int ff_amf_tag_size(const uint8_t *data, const uint8_t *data_end)
  200. {
  201. const uint8_t *base = data;
  202. if (data >= data_end)
  203. return -1;
  204. switch (*data++) {
  205. case AMF_DATA_TYPE_NUMBER: return 9;
  206. case AMF_DATA_TYPE_BOOL: return 2;
  207. case AMF_DATA_TYPE_STRING: return 3 + AV_RB16(data);
  208. case AMF_DATA_TYPE_LONG_STRING: return 5 + AV_RB32(data);
  209. case AMF_DATA_TYPE_NULL: return 1;
  210. case AMF_DATA_TYPE_ARRAY:
  211. data += 4;
  212. case AMF_DATA_TYPE_OBJECT:
  213. for (;;) {
  214. int size = bytestream_get_be16(&data);
  215. int t;
  216. if (!size) {
  217. data++;
  218. break;
  219. }
  220. if (data + size >= data_end || data + size < data)
  221. return -1;
  222. data += size;
  223. t = ff_amf_tag_size(data, data_end);
  224. if (t < 0 || data + t >= data_end)
  225. return -1;
  226. data += t;
  227. }
  228. return data - base;
  229. case AMF_DATA_TYPE_OBJECT_END: return 1;
  230. default: return -1;
  231. }
  232. }
  233. int ff_amf_get_field_value(const uint8_t *data, const uint8_t *data_end,
  234. const uint8_t *name, uint8_t *dst, int dst_size)
  235. {
  236. int namelen = strlen(name);
  237. int len;
  238. while (*data != AMF_DATA_TYPE_OBJECT && data < data_end) {
  239. len = ff_amf_tag_size(data, data_end);
  240. if (len < 0)
  241. len = data_end - data;
  242. data += len;
  243. }
  244. if (data_end - data < 3)
  245. return -1;
  246. data++;
  247. for (;;) {
  248. int size = bytestream_get_be16(&data);
  249. if (!size)
  250. break;
  251. if (data + size >= data_end || data + size < data)
  252. return -1;
  253. data += size;
  254. if (size == namelen && !memcmp(data-size, name, namelen)) {
  255. switch (*data++) {
  256. case AMF_DATA_TYPE_NUMBER:
  257. snprintf(dst, dst_size, "%g", av_int2dbl(AV_RB64(data)));
  258. break;
  259. case AMF_DATA_TYPE_BOOL:
  260. snprintf(dst, dst_size, "%s", *data ? "true" : "false");
  261. break;
  262. case AMF_DATA_TYPE_STRING:
  263. len = bytestream_get_be16(&data);
  264. av_strlcpy(dst, data, FFMIN(len+1, dst_size));
  265. break;
  266. default:
  267. return -1;
  268. }
  269. return 0;
  270. }
  271. len = ff_amf_tag_size(data, data_end);
  272. if (len < 0 || data + len >= data_end || data + len < data)
  273. return -1;
  274. data += len;
  275. }
  276. return -1;
  277. }