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.

305 lines
9.9KB

  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. len_off = avio_rb24(pb);
  181. if (mflags & 0x20) /**< relative timestamp */
  182. avio_skip(pb, 4);
  183. if (mflags & 0x10) /**< has duration */
  184. avio_skip(pb, 4);
  185. if (mflags & 0x8) /**< has location ID */
  186. avio_skip(pb, 4);
  187. off = avio_tell(pb);
  188. if (!(mflags & 0x40)) {
  189. /**
  190. * If 0x40 is not set, the len_off field specifies an offset
  191. * of this packet's payload data in the complete (reassembled)
  192. * ASF packet. This is used to spread one ASF packet over
  193. * multiple RTP packets.
  194. */
  195. if (asf->pktbuf && len_off != avio_tell(asf->pktbuf)) {
  196. ffio_free_dyn_buf(&asf->pktbuf);
  197. }
  198. if (!len_off && !asf->pktbuf &&
  199. (res = avio_open_dyn_buf(&asf->pktbuf)) < 0)
  200. return res;
  201. if (!asf->pktbuf)
  202. return AVERROR(EIO);
  203. avio_write(asf->pktbuf, buf + off, len - off);
  204. avio_skip(pb, len - off);
  205. if (!(flags & RTP_FLAG_MARKER))
  206. return -1;
  207. out_len = avio_close_dyn_buf(asf->pktbuf, &asf->buf);
  208. asf->pktbuf = NULL;
  209. } else {
  210. /**
  211. * If 0x40 is set, the len_off field specifies the length of
  212. * the next ASF packet that can be read from this payload
  213. * data alone. This is commonly the same as the payload size,
  214. * but could be less in case of packet splitting (i.e.
  215. * multiple ASF packets in one RTP packet).
  216. */
  217. int cur_len = start_off + len_off - off;
  218. int prev_len = out_len;
  219. out_len += cur_len;
  220. if (FFMIN(cur_len, len - off) < 0)
  221. return -1;
  222. if ((res = av_reallocp(&asf->buf, out_len)) < 0)
  223. return res;
  224. memcpy(asf->buf + prev_len, buf + off,
  225. FFMIN(cur_len, len - off));
  226. avio_skip(pb, cur_len);
  227. }
  228. }
  229. init_packetizer(pb, asf->buf, out_len);
  230. pb->pos += rt->asf_pb_pos;
  231. pb->eof_reached = 0;
  232. rt->asf_ctx->pb = pb;
  233. }
  234. for (;;) {
  235. int i;
  236. res = ff_read_packet(rt->asf_ctx, pkt);
  237. rt->asf_pb_pos = avio_tell(pb);
  238. if (res != 0)
  239. break;
  240. for (i = 0; i < s->nb_streams; i++) {
  241. if (s->streams[i]->id == rt->asf_ctx->streams[pkt->stream_index]->id) {
  242. pkt->stream_index = i;
  243. return 1; // FIXME: return 0 if last packet
  244. }
  245. }
  246. av_free_packet(pkt);
  247. }
  248. return res == 1 ? -1 : res;
  249. }
  250. static void asfrtp_close_context(PayloadContext *asf)
  251. {
  252. ffio_free_dyn_buf(&asf->pktbuf);
  253. av_freep(&asf->buf);
  254. }
  255. #define RTP_ASF_HANDLER(n, s, t) \
  256. RTPDynamicProtocolHandler ff_ms_rtp_ ## n ## _handler = { \
  257. .enc_name = s, \
  258. .codec_type = t, \
  259. .codec_id = AV_CODEC_ID_NONE, \
  260. .priv_data_size = sizeof(PayloadContext), \
  261. .parse_sdp_a_line = asfrtp_parse_sdp_line, \
  262. .close = asfrtp_close_context, \
  263. .parse_packet = asfrtp_parse_packet, \
  264. }
  265. RTP_ASF_HANDLER(asf_pfv, "x-asf-pf", AVMEDIA_TYPE_VIDEO);
  266. RTP_ASF_HANDLER(asf_pfa, "x-asf-pf", AVMEDIA_TYPE_AUDIO);