00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00028 #include "libavutil/base64.h"
00029 #include "libavutil/avstring.h"
00030 #include "libavutil/intreadwrite.h"
00031 #include "rtp.h"
00032 #include "rtpdec_formats.h"
00033 #include "rtsp.h"
00034 #include "asf.h"
00035 #include "avio_internal.h"
00036
00044 static int rtp_asf_fix_header(uint8_t *buf, int len)
00045 {
00046 uint8_t *p = buf, *end = buf + len;
00047
00048 if (len < sizeof(ff_asf_guid) * 2 + 22 ||
00049 memcmp(p, ff_asf_header, sizeof(ff_asf_guid))) {
00050 return -1;
00051 }
00052 p += sizeof(ff_asf_guid) + 14;
00053 do {
00054 uint64_t chunksize = AV_RL64(p + sizeof(ff_asf_guid));
00055 if (memcmp(p, ff_asf_file_header, sizeof(ff_asf_guid))) {
00056 if (chunksize > end - p)
00057 return -1;
00058 p += chunksize;
00059 continue;
00060 }
00061
00062
00063 p += 6 * 8 + 3 * 4 + sizeof(ff_asf_guid) * 2;
00064 if (p + 8 <= end && AV_RL32(p) == AV_RL32(p + 4)) {
00065
00066 AV_WL32(p, 0);
00067 return 0;
00068 }
00069 break;
00070 } while (end - p >= sizeof(ff_asf_guid) + 8);
00071
00072 return -1;
00073 }
00074
00081 static int packetizer_read(void *opaque, uint8_t *buf, int buf_size)
00082 {
00083 return AVERROR(EAGAIN);
00084 }
00085
00086 static void init_packetizer(AVIOContext *pb, uint8_t *buf, int len)
00087 {
00088 ffio_init_context(pb, buf, len, 0, NULL, packetizer_read, NULL, NULL);
00089
00090
00091 pb->pos = len;
00092 pb->buf_end = buf + len;
00093 }
00094
00095 int ff_wms_parse_sdp_a_line(AVFormatContext *s, const char *p)
00096 {
00097 int ret = 0;
00098 if (av_strstart(p, "pgmpu:data:application/vnd.ms.wms-hdr.asfv1;base64,", &p)) {
00099 AVIOContext pb;
00100 RTSPState *rt = s->priv_data;
00101 int len = strlen(p) * 6 / 8;
00102 char *buf = av_mallocz(len);
00103 av_base64_decode(buf, p, len);
00104
00105 if (rtp_asf_fix_header(buf, len) < 0)
00106 av_log(s, AV_LOG_ERROR,
00107 "Failed to fix invalid RTSP-MS/ASF min_pktsize\n");
00108 init_packetizer(&pb, buf, len);
00109 if (rt->asf_ctx) {
00110 av_close_input_stream(rt->asf_ctx);
00111 rt->asf_ctx = NULL;
00112 }
00113 ret = av_open_input_stream(&rt->asf_ctx, &pb, "", &ff_asf_demuxer, NULL);
00114 if (ret < 0)
00115 return ret;
00116 av_metadata_copy(&s->metadata, rt->asf_ctx->metadata, 0);
00117 rt->asf_pb_pos = avio_tell(&pb);
00118 av_free(buf);
00119 rt->asf_ctx->pb = NULL;
00120 }
00121 return ret;
00122 }
00123
00124 static int asfrtp_parse_sdp_line(AVFormatContext *s, int stream_index,
00125 PayloadContext *asf, const char *line)
00126 {
00127 if (av_strstart(line, "stream:", &line)) {
00128 RTSPState *rt = s->priv_data;
00129
00130 s->streams[stream_index]->id = strtol(line, NULL, 10);
00131
00132 if (rt->asf_ctx) {
00133 int i;
00134
00135 for (i = 0; i < rt->asf_ctx->nb_streams; i++) {
00136 if (s->streams[stream_index]->id == rt->asf_ctx->streams[i]->id) {
00137 *s->streams[stream_index]->codec =
00138 *rt->asf_ctx->streams[i]->codec;
00139 rt->asf_ctx->streams[i]->codec->extradata_size = 0;
00140 rt->asf_ctx->streams[i]->codec->extradata = NULL;
00141 av_set_pts_info(s->streams[stream_index], 32, 1, 1000);
00142 }
00143 }
00144 }
00145 }
00146
00147 return 0;
00148 }
00149
00150 struct PayloadContext {
00151 AVIOContext *pktbuf, pb;
00152 uint8_t *buf;
00153 };
00154
00160 static int asfrtp_parse_packet(AVFormatContext *s, PayloadContext *asf,
00161 AVStream *st, AVPacket *pkt,
00162 uint32_t *timestamp,
00163 const uint8_t *buf, int len, int flags)
00164 {
00165 AVIOContext *pb = &asf->pb;
00166 int res, mflags, len_off;
00167 RTSPState *rt = s->priv_data;
00168
00169 if (!rt->asf_ctx)
00170 return -1;
00171
00172 if (len > 0) {
00173 int off, out_len = 0;
00174
00175 if (len < 4)
00176 return -1;
00177
00178 av_freep(&asf->buf);
00179
00180 ffio_init_context(pb, buf, len, 0, NULL, NULL, NULL, NULL);
00181
00182 while (avio_tell(pb) + 4 < len) {
00183 int start_off = avio_tell(pb);
00184
00185 mflags = avio_r8(pb);
00186 if (mflags & 0x80)
00187 flags |= RTP_FLAG_KEY;
00188 len_off = avio_rb24(pb);
00189 if (mflags & 0x20)
00190 avio_seek(pb, 4, SEEK_CUR);
00191 if (mflags & 0x10)
00192 avio_seek(pb, 4, SEEK_CUR);
00193 if (mflags & 0x8)
00194 avio_seek(pb, 4, SEEK_CUR);
00195 off = avio_tell(pb);
00196
00197 if (!(mflags & 0x40)) {
00204 if (asf->pktbuf && len_off != avio_tell(asf->pktbuf)) {
00205 uint8_t *p;
00206 url_close_dyn_buf(asf->pktbuf, &p);
00207 asf->pktbuf = NULL;
00208 av_free(p);
00209 }
00210 if (!len_off && !asf->pktbuf &&
00211 (res = url_open_dyn_buf(&asf->pktbuf)) < 0)
00212 return res;
00213 if (!asf->pktbuf)
00214 return AVERROR(EIO);
00215
00216 avio_write(asf->pktbuf, buf + off, len - off);
00217 avio_seek(pb, len - off, SEEK_CUR);
00218 if (!(flags & RTP_FLAG_MARKER))
00219 return -1;
00220 out_len = url_close_dyn_buf(asf->pktbuf, &asf->buf);
00221 asf->pktbuf = NULL;
00222 } else {
00231 int cur_len = start_off + len_off - off;
00232 int prev_len = out_len;
00233 out_len += cur_len;
00234 asf->buf = av_realloc(asf->buf, out_len);
00235 memcpy(asf->buf + prev_len, buf + off,
00236 FFMIN(cur_len, len - off));
00237 avio_seek(pb, cur_len, SEEK_CUR);
00238 }
00239 }
00240
00241 init_packetizer(pb, asf->buf, out_len);
00242 pb->pos += rt->asf_pb_pos;
00243 pb->eof_reached = 0;
00244 rt->asf_ctx->pb = pb;
00245 }
00246
00247 for (;;) {
00248 int i;
00249
00250 res = av_read_packet(rt->asf_ctx, pkt);
00251 rt->asf_pb_pos = avio_tell(pb);
00252 if (res != 0)
00253 break;
00254 for (i = 0; i < s->nb_streams; i++) {
00255 if (s->streams[i]->id == rt->asf_ctx->streams[pkt->stream_index]->id) {
00256 pkt->stream_index = i;
00257 return 1;
00258 }
00259 }
00260 av_free_packet(pkt);
00261 }
00262
00263 return res == 1 ? -1 : res;
00264 }
00265
00266 static PayloadContext *asfrtp_new_context(void)
00267 {
00268 return av_mallocz(sizeof(PayloadContext));
00269 }
00270
00271 static void asfrtp_free_context(PayloadContext *asf)
00272 {
00273 if (asf->pktbuf) {
00274 uint8_t *p = NULL;
00275 url_close_dyn_buf(asf->pktbuf, &p);
00276 asf->pktbuf = NULL;
00277 av_free(p);
00278 }
00279 av_freep(&asf->buf);
00280 av_free(asf);
00281 }
00282
00283 #define RTP_ASF_HANDLER(n, s, t) \
00284 RTPDynamicProtocolHandler ff_ms_rtp_ ## n ## _handler = { \
00285 .enc_name = s, \
00286 .codec_type = t, \
00287 .codec_id = CODEC_ID_NONE, \
00288 .parse_sdp_a_line = asfrtp_parse_sdp_line, \
00289 .open = asfrtp_new_context, \
00290 .close = asfrtp_free_context, \
00291 .parse_packet = asfrtp_parse_packet, \
00292 }
00293
00294 RTP_ASF_HANDLER(asf_pfv, "x-asf-pf", AVMEDIA_TYPE_VIDEO);
00295 RTP_ASF_HANDLER(asf_pfa, "x-asf-pf", AVMEDIA_TYPE_AUDIO);