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.

297 lines
9.1KB

  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].timestamp;
  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. if (hdr != RTMP_PS_TWELVEBYTES)
  107. timestamp += prev_pkt[channel_id].timestamp;
  108. }
  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].timestamp = timestamp;
  117. prev_pkt[channel_id].extra = extra;
  118. while (data_size > 0) {
  119. int toread = FFMIN(data_size, chunk_size);
  120. if (url_read_complete(h, p->data + offset, toread) != toread) {
  121. ff_rtmp_packet_destroy(p);
  122. return AVERROR(EIO);
  123. }
  124. data_size -= chunk_size;
  125. offset += chunk_size;
  126. if (data_size > 0) {
  127. url_read_complete(h, &t, 1); //marker
  128. if (t != (0xC0 + channel_id))
  129. return -1;
  130. }
  131. }
  132. return 0;
  133. }
  134. int ff_rtmp_packet_write(URLContext *h, RTMPPacket *pkt,
  135. int chunk_size, RTMPPacket *prev_pkt)
  136. {
  137. uint8_t pkt_hdr[16], *p = pkt_hdr;
  138. int mode = RTMP_PS_TWELVEBYTES;
  139. int off = 0;
  140. //TODO: header compression
  141. if (pkt->channel_id < 64) {
  142. bytestream_put_byte(&p, pkt->channel_id | (mode << 6));
  143. } else if (pkt->channel_id < 64 + 256) {
  144. bytestream_put_byte(&p, 0 | (mode << 6));
  145. bytestream_put_byte(&p, pkt->channel_id - 64);
  146. } else {
  147. bytestream_put_byte(&p, 1 | (mode << 6));
  148. bytestream_put_le16(&p, pkt->channel_id - 64);
  149. }
  150. if (mode != RTMP_PS_ONEBYTE) {
  151. uint32_t timestamp = pkt->timestamp;
  152. if (mode != RTMP_PS_TWELVEBYTES)
  153. timestamp -= prev_pkt[pkt->channel_id].timestamp;
  154. bytestream_put_be24(&p, timestamp >= 0xFFFFFF ? 0xFFFFFF : timestamp);
  155. if (mode != RTMP_PS_FOURBYTES) {
  156. bytestream_put_be24(&p, pkt->data_size);
  157. bytestream_put_byte(&p, pkt->type);
  158. if (mode == RTMP_PS_TWELVEBYTES)
  159. bytestream_put_le32(&p, pkt->extra);
  160. }
  161. if (timestamp >= 0xFFFFFF)
  162. bytestream_put_be32(&p, timestamp);
  163. }
  164. url_write(h, pkt_hdr, p-pkt_hdr);
  165. while (off < pkt->data_size) {
  166. int towrite = FFMIN(chunk_size, pkt->data_size - off);
  167. url_write(h, pkt->data + off, towrite);
  168. off += towrite;
  169. if (off < pkt->data_size) {
  170. uint8_t marker = 0xC0 | pkt->channel_id;
  171. url_write(h, &marker, 1);
  172. }
  173. }
  174. return 0;
  175. }
  176. int ff_rtmp_packet_create(RTMPPacket *pkt, int channel_id, RTMPPacketType type,
  177. int timestamp, int size)
  178. {
  179. pkt->data = av_malloc(size);
  180. if (!pkt->data)
  181. return AVERROR(ENOMEM);
  182. pkt->data_size = size;
  183. pkt->channel_id = channel_id;
  184. pkt->type = type;
  185. pkt->timestamp = timestamp;
  186. pkt->extra = 0;
  187. return 0;
  188. }
  189. void ff_rtmp_packet_destroy(RTMPPacket *pkt)
  190. {
  191. if (!pkt)
  192. return;
  193. av_freep(&pkt->data);
  194. pkt->data_size = 0;
  195. }
  196. int ff_amf_tag_size(const uint8_t *data, const uint8_t *data_end)
  197. {
  198. const uint8_t *base = data;
  199. if (data >= data_end)
  200. return -1;
  201. switch (*data++) {
  202. case AMF_DATA_TYPE_NUMBER: return 9;
  203. case AMF_DATA_TYPE_BOOL: return 2;
  204. case AMF_DATA_TYPE_STRING: return 3 + AV_RB16(data);
  205. case AMF_DATA_TYPE_LONG_STRING: return 5 + AV_RB32(data);
  206. case AMF_DATA_TYPE_NULL: return 1;
  207. case AMF_DATA_TYPE_ARRAY:
  208. data += 4;
  209. case AMF_DATA_TYPE_OBJECT:
  210. for (;;) {
  211. int size = bytestream_get_be16(&data);
  212. int t;
  213. if (!size) {
  214. data++;
  215. break;
  216. }
  217. if (data + size >= data_end || data + size < data)
  218. return -1;
  219. data += size;
  220. t = ff_amf_tag_size(data, data_end);
  221. if (t < 0 || data + t >= data_end)
  222. return -1;
  223. data += t;
  224. }
  225. return data - base;
  226. case AMF_DATA_TYPE_OBJECT_END: return 1;
  227. default: return -1;
  228. }
  229. }
  230. int ff_amf_get_field_value(const uint8_t *data, const uint8_t *data_end,
  231. const uint8_t *name, uint8_t *dst, int dst_size)
  232. {
  233. int namelen = strlen(name);
  234. int len;
  235. while (*data != AMF_DATA_TYPE_OBJECT && data < data_end) {
  236. len = ff_amf_tag_size(data, data_end);
  237. if (len < 0)
  238. len = data_end - data;
  239. data += len;
  240. }
  241. if (data_end - data < 3)
  242. return -1;
  243. data++;
  244. for (;;) {
  245. int size = bytestream_get_be16(&data);
  246. if (!size)
  247. break;
  248. if (data + size >= data_end || data + size < data)
  249. return -1;
  250. data += size;
  251. if (size == namelen && !memcmp(data-size, name, namelen)) {
  252. switch (*data++) {
  253. case AMF_DATA_TYPE_NUMBER:
  254. snprintf(dst, dst_size, "%g", av_int2dbl(AV_RB64(data)));
  255. break;
  256. case AMF_DATA_TYPE_BOOL:
  257. snprintf(dst, dst_size, "%s", *data ? "true" : "false");
  258. break;
  259. case AMF_DATA_TYPE_STRING:
  260. len = bytestream_get_be16(&data);
  261. av_strlcpy(dst, data, FFMIN(len+1, dst_size));
  262. break;
  263. default:
  264. return -1;
  265. }
  266. return 0;
  267. }
  268. len = ff_amf_tag_size(data, data_end);
  269. if (len < 0 || data + len >= data_end || data + len < data)
  270. return -1;
  271. data += len;
  272. }
  273. return -1;
  274. }