Libav
|
00001 /* 00002 * Microsoft RTP/ASF support. 00003 * Copyright (c) 2008 Ronald S. Bultje 00004 * 00005 * This file is part of FFmpeg. 00006 * 00007 * FFmpeg is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * FFmpeg is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with FFmpeg; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 */ 00021 00028 #include <libavutil/base64.h> 00029 #include <libavutil/avstring.h> 00030 #include <libavutil/intreadwrite.h> 00031 #include "rtp.h" 00032 #include "rtpdec_asf.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 /* skip most of the file header, to min_pktsize */ 00062 p += 6 * 8 + 3 * 4 + sizeof(ff_asf_guid) * 2; 00063 if (p + 8 <= end && AV_RL32(p) == AV_RL32(p + 4)) { 00064 /* and set that to zero */ 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 /* this "fills" the buffer with its current content */ 00090 pb->pos = len; 00091 pb->buf_end = buf + len; 00092 } 00093 00094 void ff_wms_parse_sdp_a_line(AVFormatContext *s, const char *p) 00095 { 00096 if (av_strstart(p, "pgmpu:data:application/vnd.ms.wms-hdr.asfv1;base64,", &p)) { 00097 ByteIOContext pb; 00098 RTSPState *rt = s->priv_data; 00099 int len = strlen(p) * 6 / 8; 00100 char *buf = av_mallocz(len); 00101 av_base64_decode(buf, p, len); 00102 00103 if (rtp_asf_fix_header(buf, len) < 0) 00104 av_log(s, AV_LOG_ERROR, 00105 "Failed to fix invalid RTSP-MS/ASF min_pktsize\n"); 00106 init_packetizer(&pb, buf, len); 00107 if (rt->asf_ctx) { 00108 av_close_input_stream(rt->asf_ctx); 00109 rt->asf_ctx = NULL; 00110 } 00111 av_open_input_stream(&rt->asf_ctx, &pb, "", &asf_demuxer, NULL); 00112 rt->asf_pb_pos = url_ftell(&pb); 00113 av_free(buf); 00114 rt->asf_ctx->pb = NULL; 00115 } 00116 } 00117 00118 static int asfrtp_parse_sdp_line(AVFormatContext *s, int stream_index, 00119 PayloadContext *asf, const char *line) 00120 { 00121 if (av_strstart(line, "stream:", &line)) { 00122 RTSPState *rt = s->priv_data; 00123 00124 s->streams[stream_index]->id = strtol(line, NULL, 10); 00125 00126 if (rt->asf_ctx) { 00127 int i; 00128 00129 for (i = 0; i < rt->asf_ctx->nb_streams; i++) { 00130 if (s->streams[stream_index]->id == rt->asf_ctx->streams[i]->id) { 00131 *s->streams[stream_index]->codec = 00132 *rt->asf_ctx->streams[i]->codec; 00133 rt->asf_ctx->streams[i]->codec->extradata_size = 0; 00134 rt->asf_ctx->streams[i]->codec->extradata = NULL; 00135 av_set_pts_info(s->streams[stream_index], 32, 1, 1000); 00136 } 00137 } 00138 } 00139 } 00140 00141 return 0; 00142 } 00143 00144 struct PayloadContext { 00145 ByteIOContext *pktbuf, pb; 00146 char *buf; 00147 }; 00148 00154 static int asfrtp_parse_packet(AVFormatContext *s, PayloadContext *asf, 00155 AVStream *st, AVPacket *pkt, 00156 uint32_t *timestamp, 00157 const uint8_t *buf, int len, int flags) 00158 { 00159 ByteIOContext *pb = &asf->pb; 00160 int res, mflags, len_off; 00161 RTSPState *rt = s->priv_data; 00162 00163 if (!rt->asf_ctx) 00164 return -1; 00165 00166 if (len > 0) { 00167 int off, out_len; 00168 00169 if (len < 4) 00170 return -1; 00171 00172 init_put_byte(pb, buf, len, 0, NULL, NULL, NULL, NULL); 00173 mflags = get_byte(pb); 00174 if (mflags & 0x80) 00175 flags |= RTP_FLAG_KEY; 00176 len_off = get_be24(pb); 00177 if (mflags & 0x20) 00178 url_fskip(pb, 4); 00179 if (mflags & 0x10) 00180 url_fskip(pb, 4); 00181 if (mflags & 0x8) 00182 url_fskip(pb, 4); 00183 off = url_ftell(pb); 00184 00185 av_freep(&asf->buf); 00186 if (!(mflags & 0x40)) { 00192 if (asf->pktbuf && len_off != url_ftell(asf->pktbuf)) { 00193 uint8_t *p; 00194 url_close_dyn_buf(asf->pktbuf, &p); 00195 asf->pktbuf = NULL; 00196 av_free(p); 00197 } 00198 if (!len_off && !asf->pktbuf && 00199 (res = url_open_dyn_buf(&asf->pktbuf)) < 0) 00200 return res; 00201 if (!asf->pktbuf) 00202 return AVERROR(EIO); 00203 00204 put_buffer(asf->pktbuf, buf + off, len - off); 00205 if (!(flags & RTP_FLAG_MARKER)) 00206 return -1; 00207 out_len = url_close_dyn_buf(asf->pktbuf, &asf->buf); 00208 asf->pktbuf = NULL; 00209 } else { 00217 if (len_off != len) { 00218 av_log_missing_feature(s, 00219 "RTSP-MS packet splitting", 1); 00220 return -1; 00221 } 00222 asf->buf = av_malloc(len - off); 00223 out_len = len - off; 00224 memcpy(asf->buf, buf + off, len - off); 00225 } 00226 00227 init_packetizer(pb, asf->buf, out_len); 00228 pb->pos += rt->asf_pb_pos; 00229 pb->eof_reached = 0; 00230 rt->asf_ctx->pb = pb; 00231 } 00232 00233 for (;;) { 00234 int i; 00235 00236 res = av_read_packet(rt->asf_ctx, pkt); 00237 rt->asf_pb_pos = url_ftell(pb); 00238 if (res != 0) 00239 break; 00240 for (i = 0; i < s->nb_streams; i++) { 00241 if (s->streams[i]->id == rt->asf_ctx->streams[pkt->stream_index]->id) { 00242 pkt->stream_index = i; 00243 return 1; // FIXME: return 0 if last packet 00244 } 00245 } 00246 av_free_packet(pkt); 00247 } 00248 00249 return res == 1 ? -1 : res; 00250 } 00251 00252 static PayloadContext *asfrtp_new_context(void) 00253 { 00254 return av_mallocz(sizeof(PayloadContext)); 00255 } 00256 00257 static void asfrtp_free_context(PayloadContext *asf) 00258 { 00259 if (asf->pktbuf) { 00260 uint8_t *p = NULL; 00261 url_close_dyn_buf(asf->pktbuf, &p); 00262 asf->pktbuf = NULL; 00263 av_free(p); 00264 } 00265 av_freep(&asf->buf); 00266 av_free(asf); 00267 } 00268 00269 #define RTP_ASF_HANDLER(n, s, t) \ 00270 RTPDynamicProtocolHandler ff_ms_rtp_ ## n ## _handler = { \ 00271 .enc_name = s, \ 00272 .codec_type = t, \ 00273 .codec_id = CODEC_ID_NONE, \ 00274 .parse_sdp_a_line = asfrtp_parse_sdp_line, \ 00275 .open = asfrtp_new_context, \ 00276 .close = asfrtp_free_context, \ 00277 .parse_packet = asfrtp_parse_packet, \ 00278 }; 00279 00280 RTP_ASF_HANDLER(asf_pfv, "x-asf-pf", AVMEDIA_TYPE_VIDEO); 00281 RTP_ASF_HANDLER(asf_pfa, "x-asf-pf", AVMEDIA_TYPE_AUDIO);