Libav 0.7.1
|
00001 /* 00002 * Session Announcement Protocol (RFC 2974) demuxer 00003 * Copyright (c) 2010 Martin Storsjo 00004 * 00005 * This file is part of Libav. 00006 * 00007 * Libav 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 * Libav 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 Libav; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 */ 00021 00022 #include "avformat.h" 00023 #include "libavutil/avstring.h" 00024 #include "libavutil/intreadwrite.h" 00025 #include "network.h" 00026 #include "os_support.h" 00027 #include "internal.h" 00028 #include "avio_internal.h" 00029 #include "url.h" 00030 #if HAVE_POLL_H 00031 #include <poll.h> 00032 #endif 00033 #include <sys/time.h> 00034 00035 struct SAPState { 00036 URLContext *ann_fd; 00037 AVFormatContext *sdp_ctx; 00038 AVIOContext sdp_pb; 00039 uint16_t hash; 00040 char *sdp; 00041 int eof; 00042 }; 00043 00044 static int sap_probe(AVProbeData *p) 00045 { 00046 if (av_strstart(p->filename, "sap:", NULL)) 00047 return AVPROBE_SCORE_MAX; 00048 return 0; 00049 } 00050 00051 static int sap_read_close(AVFormatContext *s) 00052 { 00053 struct SAPState *sap = s->priv_data; 00054 if (sap->sdp_ctx) 00055 av_close_input_file(sap->sdp_ctx); 00056 if (sap->ann_fd) 00057 ffurl_close(sap->ann_fd); 00058 av_freep(&sap->sdp); 00059 ff_network_close(); 00060 return 0; 00061 } 00062 00063 static int sap_read_header(AVFormatContext *s, 00064 AVFormatParameters *ap) 00065 { 00066 struct SAPState *sap = s->priv_data; 00067 char host[1024], path[1024], url[1024]; 00068 uint8_t recvbuf[1500]; 00069 int port; 00070 int ret, i; 00071 AVInputFormat* infmt; 00072 00073 if (!ff_network_init()) 00074 return AVERROR(EIO); 00075 00076 av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &port, 00077 path, sizeof(path), s->filename); 00078 if (port < 0) 00079 port = 9875; 00080 00081 if (!host[0]) { 00082 /* Listen for announcements on sap.mcast.net if no host was specified */ 00083 av_strlcpy(host, "224.2.127.254", sizeof(host)); 00084 } 00085 00086 ff_url_join(url, sizeof(url), "udp", NULL, host, port, "?localport=%d", 00087 port); 00088 ret = ffurl_open(&sap->ann_fd, url, AVIO_FLAG_READ); 00089 if (ret) 00090 goto fail; 00091 00092 while (1) { 00093 int addr_type, auth_len; 00094 int pos; 00095 00096 ret = ffurl_read(sap->ann_fd, recvbuf, sizeof(recvbuf) - 1); 00097 if (ret == AVERROR(EAGAIN)) 00098 continue; 00099 if (ret < 0) 00100 goto fail; 00101 recvbuf[ret] = '\0'; /* Null terminate for easier parsing */ 00102 if (ret < 8) { 00103 av_log(s, AV_LOG_WARNING, "Received too short packet\n"); 00104 continue; 00105 } 00106 00107 if ((recvbuf[0] & 0xe0) != 0x20) { 00108 av_log(s, AV_LOG_WARNING, "Unsupported SAP version packet " 00109 "received\n"); 00110 continue; 00111 } 00112 00113 if (recvbuf[0] & 0x04) { 00114 av_log(s, AV_LOG_WARNING, "Received stream deletion " 00115 "announcement\n"); 00116 continue; 00117 } 00118 addr_type = recvbuf[0] & 0x10; 00119 auth_len = recvbuf[1]; 00120 sap->hash = AV_RB16(&recvbuf[2]); 00121 pos = 4; 00122 if (addr_type) 00123 pos += 16; /* IPv6 */ 00124 else 00125 pos += 4; /* IPv4 */ 00126 pos += auth_len * 4; 00127 if (pos + 4 >= ret) { 00128 av_log(s, AV_LOG_WARNING, "Received too short packet\n"); 00129 continue; 00130 } 00131 #define MIME "application/sdp" 00132 if (strcmp(&recvbuf[pos], MIME) == 0) { 00133 pos += strlen(MIME) + 1; 00134 } else if (strncmp(&recvbuf[pos], "v=0\r\n", 5) == 0) { 00135 // Direct SDP without a mime type 00136 } else { 00137 av_log(s, AV_LOG_WARNING, "Unsupported mime type %s\n", 00138 &recvbuf[pos]); 00139 continue; 00140 } 00141 00142 sap->sdp = av_strdup(&recvbuf[pos]); 00143 break; 00144 } 00145 00146 av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", sap->sdp); 00147 ffio_init_context(&sap->sdp_pb, sap->sdp, strlen(sap->sdp), 0, NULL, NULL, 00148 NULL, NULL); 00149 00150 infmt = av_find_input_format("sdp"); 00151 if (!infmt) 00152 goto fail; 00153 sap->sdp_ctx = avformat_alloc_context(); 00154 if (!sap->sdp_ctx) { 00155 ret = AVERROR(ENOMEM); 00156 goto fail; 00157 } 00158 sap->sdp_ctx->max_delay = s->max_delay; 00159 sap->sdp_ctx->pb = &sap->sdp_pb; 00160 ret = avformat_open_input(&sap->sdp_ctx, "temp.sdp", infmt, NULL); 00161 if (ret < 0) 00162 goto fail; 00163 if (sap->sdp_ctx->ctx_flags & AVFMTCTX_NOHEADER) 00164 s->ctx_flags |= AVFMTCTX_NOHEADER; 00165 for (i = 0; i < sap->sdp_ctx->nb_streams; i++) { 00166 AVStream *st = av_new_stream(s, i); 00167 if (!st) { 00168 ret = AVERROR(ENOMEM); 00169 goto fail; 00170 } 00171 avcodec_copy_context(st->codec, sap->sdp_ctx->streams[i]->codec); 00172 st->time_base = sap->sdp_ctx->streams[i]->time_base; 00173 } 00174 00175 return 0; 00176 00177 fail: 00178 sap_read_close(s); 00179 return ret; 00180 } 00181 00182 static int sap_fetch_packet(AVFormatContext *s, AVPacket *pkt) 00183 { 00184 struct SAPState *sap = s->priv_data; 00185 int fd = ffurl_get_file_handle(sap->ann_fd); 00186 int n, ret; 00187 struct pollfd p = {fd, POLLIN, 0}; 00188 uint8_t recvbuf[1500]; 00189 00190 if (sap->eof) 00191 return AVERROR_EOF; 00192 00193 while (1) { 00194 n = poll(&p, 1, 0); 00195 if (n <= 0 || !(p.revents & POLLIN)) 00196 break; 00197 ret = ffurl_read(sap->ann_fd, recvbuf, sizeof(recvbuf)); 00198 if (ret >= 8) { 00199 uint16_t hash = AV_RB16(&recvbuf[2]); 00200 /* Should ideally check the source IP address, too */ 00201 if (recvbuf[0] & 0x04 && hash == sap->hash) { 00202 /* Stream deletion */ 00203 sap->eof = 1; 00204 return AVERROR_EOF; 00205 } 00206 } 00207 } 00208 ret = av_read_frame(sap->sdp_ctx, pkt); 00209 if (ret < 0) 00210 return ret; 00211 if (s->ctx_flags & AVFMTCTX_NOHEADER) { 00212 while (sap->sdp_ctx->nb_streams > s->nb_streams) { 00213 int i = s->nb_streams; 00214 AVStream *st = av_new_stream(s, i); 00215 if (!st) { 00216 av_free_packet(pkt); 00217 return AVERROR(ENOMEM); 00218 } 00219 avcodec_copy_context(st->codec, sap->sdp_ctx->streams[i]->codec); 00220 st->time_base = sap->sdp_ctx->streams[i]->time_base; 00221 } 00222 } 00223 return ret; 00224 } 00225 00226 AVInputFormat ff_sap_demuxer = { 00227 "sap", 00228 NULL_IF_CONFIG_SMALL("SAP input format"), 00229 sizeof(struct SAPState), 00230 sap_probe, 00231 sap_read_header, 00232 sap_fetch_packet, 00233 sap_read_close, 00234 .flags = AVFMT_NOFILE, 00235 }; 00236