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.

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