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.

271 lines
8.0KB

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