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