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.

321 lines
10KB

  1. /*
  2. * Microsoft RTP/ASF support.
  3. * Copyright (c) 2008 Ronald S. Bultje
  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. /**
  22. * @file
  23. * @brief Microsoft RTP/ASF support
  24. * @author Ronald S. Bultje <rbultje@ronald.bitfreak.net>
  25. */
  26. #include "libavutil/avassert.h"
  27. #include "libavutil/base64.h"
  28. #include "libavutil/avstring.h"
  29. #include "libavutil/intreadwrite.h"
  30. #include "rtp.h"
  31. #include "rtpdec_formats.h"
  32. #include "rtsp.h"
  33. #include "asf.h"
  34. #include "avio_internal.h"
  35. #include "internal.h"
  36. /**
  37. * From MSDN 2.2.1.4, we learn that ASF data packets over RTP should not
  38. * contain any padding. Unfortunately, the header min/max_pktsize are not
  39. * updated (thus making min_pktsize invalid). Here, we "fix" these faulty
  40. * min_pktsize values in the ASF file header.
  41. * @return 0 on success, <0 on failure (currently -1).
  42. */
  43. static int rtp_asf_fix_header(uint8_t *buf, int len)
  44. {
  45. uint8_t *p = buf, *end = buf + len;
  46. if (len < sizeof(ff_asf_guid) * 2 + 22 ||
  47. memcmp(p, ff_asf_header, sizeof(ff_asf_guid))) {
  48. return -1;
  49. }
  50. p += sizeof(ff_asf_guid) + 14;
  51. do {
  52. uint64_t chunksize = AV_RL64(p + sizeof(ff_asf_guid));
  53. if (memcmp(p, ff_asf_file_header, sizeof(ff_asf_guid))) {
  54. if (chunksize > end - p)
  55. return -1;
  56. p += chunksize;
  57. continue;
  58. }
  59. /* skip most of the file header, to min_pktsize */
  60. p += 6 * 8 + 3 * 4 + sizeof(ff_asf_guid) * 2;
  61. if (p + 8 <= end && AV_RL32(p) == AV_RL32(p + 4)) {
  62. /* and set that to zero */
  63. AV_WL32(p, 0);
  64. return 0;
  65. }
  66. break;
  67. } while (end - p >= sizeof(ff_asf_guid) + 8);
  68. return -1;
  69. }
  70. /**
  71. * The following code is basically a buffered AVIOContext,
  72. * with the added benefit of returning -EAGAIN (instead of 0)
  73. * on packet boundaries, such that the ASF demuxer can return
  74. * safely and resume business at the next packet.
  75. */
  76. static int packetizer_read(void *opaque, uint8_t *buf, int buf_size)
  77. {
  78. return AVERROR(EAGAIN);
  79. }
  80. static void init_packetizer(AVIOContext *pb, uint8_t *buf, int len)
  81. {
  82. ffio_init_context(pb, buf, len, 0, NULL, packetizer_read, NULL, NULL);
  83. /* this "fills" the buffer with its current content */
  84. pb->pos = len;
  85. pb->buf_end = buf + len;
  86. }
  87. int ff_wms_parse_sdp_a_line(AVFormatContext *s, const char *p)
  88. {
  89. int ret = 0;
  90. if (av_strstart(p, "pgmpu:data:application/vnd.ms.wms-hdr.asfv1;base64,", &p)) {
  91. AVIOContext pb;
  92. RTSPState *rt = s->priv_data;
  93. AVDictionary *opts = NULL;
  94. int len = strlen(p) * 6 / 8;
  95. char *buf = av_mallocz(len);
  96. AVInputFormat *iformat;
  97. av_base64_decode(buf, p, len);
  98. if (rtp_asf_fix_header(buf, len) < 0)
  99. av_log(s, AV_LOG_ERROR,
  100. "Failed to fix invalid RTSP-MS/ASF min_pktsize\n");
  101. init_packetizer(&pb, buf, len);
  102. if (rt->asf_ctx) {
  103. avformat_close_input(&rt->asf_ctx);
  104. }
  105. if (!(iformat = av_find_input_format("asf")))
  106. return AVERROR_DEMUXER_NOT_FOUND;
  107. if (!(rt->asf_ctx = avformat_alloc_context()))
  108. return AVERROR(ENOMEM);
  109. rt->asf_ctx->pb = &pb;
  110. av_dict_set(&opts, "no_resync_search", "1", 0);
  111. if ((ret = ff_copy_whitelists(rt->asf_ctx, s)) < 0) {
  112. av_dict_free(&opts);
  113. return ret;
  114. }
  115. ret = avformat_open_input(&rt->asf_ctx, "", iformat, &opts);
  116. av_dict_free(&opts);
  117. if (ret < 0)
  118. return ret;
  119. av_dict_copy(&s->metadata, rt->asf_ctx->metadata, 0);
  120. rt->asf_pb_pos = avio_tell(&pb);
  121. av_free(buf);
  122. rt->asf_ctx->pb = NULL;
  123. }
  124. return ret;
  125. }
  126. static int asfrtp_parse_sdp_line(AVFormatContext *s, int stream_index,
  127. PayloadContext *asf, const char *line)
  128. {
  129. if (stream_index < 0)
  130. return 0;
  131. if (av_strstart(line, "stream:", &line)) {
  132. RTSPState *rt = s->priv_data;
  133. s->streams[stream_index]->id = strtol(line, NULL, 10);
  134. if (rt->asf_ctx) {
  135. int i;
  136. for (i = 0; i < rt->asf_ctx->nb_streams; i++) {
  137. if (s->streams[stream_index]->id == rt->asf_ctx->streams[i]->id) {
  138. *s->streams[stream_index]->codec =
  139. *rt->asf_ctx->streams[i]->codec;
  140. s->streams[stream_index]->need_parsing =
  141. rt->asf_ctx->streams[i]->need_parsing;
  142. rt->asf_ctx->streams[i]->codec->extradata_size = 0;
  143. rt->asf_ctx->streams[i]->codec->extradata = NULL;
  144. avpriv_set_pts_info(s->streams[stream_index], 32, 1, 1000);
  145. }
  146. }
  147. }
  148. }
  149. return 0;
  150. }
  151. struct PayloadContext {
  152. AVIOContext *pktbuf, pb;
  153. uint8_t *buf;
  154. };
  155. /**
  156. * @return 0 when a packet was written into /p pkt, and no more data is left;
  157. * 1 when a packet was written into /p pkt, and more packets might be left;
  158. * <0 when not enough data was provided to return a full packet, or on error.
  159. */
  160. static int asfrtp_parse_packet(AVFormatContext *s, PayloadContext *asf,
  161. AVStream *st, AVPacket *pkt,
  162. uint32_t *timestamp,
  163. const uint8_t *buf, int len, uint16_t seq,
  164. int flags)
  165. {
  166. AVIOContext *pb = &asf->pb;
  167. int res, mflags, len_off;
  168. RTSPState *rt = s->priv_data;
  169. if (!rt->asf_ctx)
  170. return -1;
  171. if (len > 0) {
  172. int off, out_len = 0;
  173. if (len < 4)
  174. return -1;
  175. av_freep(&asf->buf);
  176. ffio_init_context(pb, (uint8_t *)buf, len, 0, NULL, NULL, NULL, NULL);
  177. while (avio_tell(pb) + 4 < len) {
  178. int start_off = avio_tell(pb);
  179. mflags = avio_r8(pb);
  180. if (mflags & 0x80)
  181. flags |= RTP_FLAG_KEY;
  182. len_off = avio_rb24(pb);
  183. if (mflags & 0x20) /**< relative timestamp */
  184. avio_skip(pb, 4);
  185. if (mflags & 0x10) /**< has duration */
  186. avio_skip(pb, 4);
  187. if (mflags & 0x8) /**< has location ID */
  188. avio_skip(pb, 4);
  189. off = avio_tell(pb);
  190. if (!(mflags & 0x40)) {
  191. /**
  192. * If 0x40 is not set, the len_off field specifies an offset
  193. * of this packet's payload data in the complete (reassembled)
  194. * ASF packet. This is used to spread one ASF packet over
  195. * multiple RTP packets.
  196. */
  197. if (asf->pktbuf && len_off != avio_tell(asf->pktbuf)) {
  198. uint8_t *p;
  199. avio_close_dyn_buf(asf->pktbuf, &p);
  200. asf->pktbuf = NULL;
  201. av_free(p);
  202. }
  203. if (!len_off && !asf->pktbuf &&
  204. (res = avio_open_dyn_buf(&asf->pktbuf)) < 0)
  205. return res;
  206. if (!asf->pktbuf)
  207. return AVERROR(EIO);
  208. avio_write(asf->pktbuf, buf + off, len - off);
  209. avio_skip(pb, len - off);
  210. if (!(flags & RTP_FLAG_MARKER))
  211. return -1;
  212. out_len = avio_close_dyn_buf(asf->pktbuf, &asf->buf);
  213. asf->pktbuf = NULL;
  214. } else {
  215. /**
  216. * If 0x40 is set, the len_off field specifies the length of
  217. * the next ASF packet that can be read from this payload
  218. * data alone. This is commonly the same as the payload size,
  219. * but could be less in case of packet splitting (i.e.
  220. * multiple ASF packets in one RTP packet).
  221. */
  222. int cur_len = start_off + len_off - off;
  223. int prev_len = out_len;
  224. out_len += cur_len;
  225. if (FFMIN(cur_len, len - off) < 0)
  226. return -1;
  227. if ((res = av_reallocp(&asf->buf, out_len)) < 0)
  228. return res;
  229. memcpy(asf->buf + prev_len, buf + off,
  230. FFMIN(cur_len, len - off));
  231. avio_skip(pb, cur_len);
  232. }
  233. }
  234. init_packetizer(pb, asf->buf, out_len);
  235. pb->pos += rt->asf_pb_pos;
  236. pb->eof_reached = 0;
  237. rt->asf_ctx->pb = pb;
  238. }
  239. for (;;) {
  240. int i;
  241. res = ff_read_packet(rt->asf_ctx, pkt);
  242. rt->asf_pb_pos = avio_tell(pb);
  243. if (res != 0)
  244. break;
  245. for (i = 0; i < s->nb_streams; i++) {
  246. if (s->streams[i]->id == rt->asf_ctx->streams[pkt->stream_index]->id) {
  247. pkt->stream_index = i;
  248. return 1; // FIXME: return 0 if last packet
  249. }
  250. }
  251. av_free_packet(pkt);
  252. }
  253. return res == 1 ? -1 : res;
  254. }
  255. static PayloadContext *asfrtp_new_context(void)
  256. {
  257. return av_mallocz(sizeof(PayloadContext));
  258. }
  259. static void asfrtp_free_context(PayloadContext *asf)
  260. {
  261. if (asf->pktbuf) {
  262. uint8_t *p = NULL;
  263. avio_close_dyn_buf(asf->pktbuf, &p);
  264. asf->pktbuf = NULL;
  265. av_free(p);
  266. }
  267. av_freep(&asf->buf);
  268. av_free(asf);
  269. }
  270. #define RTP_ASF_HANDLER(n, s, t) \
  271. RTPDynamicProtocolHandler ff_ms_rtp_ ## n ## _handler = { \
  272. .enc_name = s, \
  273. .codec_type = t, \
  274. .codec_id = AV_CODEC_ID_NONE, \
  275. .parse_sdp_a_line = asfrtp_parse_sdp_line, \
  276. .alloc = asfrtp_new_context, \
  277. .free = asfrtp_free_context, \
  278. .parse_packet = asfrtp_parse_packet, \
  279. }
  280. RTP_ASF_HANDLER(asf_pfv, "x-asf-pf", AVMEDIA_TYPE_VIDEO);
  281. RTP_ASF_HANDLER(asf_pfa, "x-asf-pf", AVMEDIA_TYPE_AUDIO);