rtmppkt.c 7.8 KB

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