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.

284 lines
8.5KB

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