00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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 #if HAVE_POLL_H
00030 #include <poll.h>
00031 #endif
00032 #include <sys/time.h>
00033
00034 struct SAPState {
00035 URLContext *ann_fd;
00036 AVFormatContext *sdp_ctx;
00037 AVIOContext sdp_pb;
00038 uint16_t hash;
00039 char *sdp;
00040 int eof;
00041 };
00042
00043 static int sap_probe(AVProbeData *p)
00044 {
00045 if (av_strstart(p->filename, "sap:", NULL))
00046 return AVPROBE_SCORE_MAX;
00047 return 0;
00048 }
00049
00050 static int sap_read_close(AVFormatContext *s)
00051 {
00052 struct SAPState *sap = s->priv_data;
00053 if (sap->sdp_ctx)
00054 av_close_input_stream(sap->sdp_ctx);
00055 if (sap->ann_fd)
00056 url_close(sap->ann_fd);
00057 av_freep(&sap->sdp);
00058 ff_network_close();
00059 return 0;
00060 }
00061
00062 static int sap_read_header(AVFormatContext *s,
00063 AVFormatParameters *ap)
00064 {
00065 struct SAPState *sap = s->priv_data;
00066 char host[1024], path[1024], url[1024];
00067 uint8_t recvbuf[1500];
00068 int port;
00069 int ret, i;
00070 AVInputFormat* infmt;
00071
00072 if (!ff_network_init())
00073 return AVERROR(EIO);
00074
00075 av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &port,
00076 path, sizeof(path), s->filename);
00077 if (port < 0)
00078 port = 9875;
00079
00080 if (!host[0]) {
00081
00082 av_strlcpy(host, "224.2.127.254", sizeof(host));
00083 }
00084
00085 ff_url_join(url, sizeof(url), "udp", NULL, host, port, "?localport=%d",
00086 port);
00087 ret = url_open(&sap->ann_fd, url, URL_RDONLY);
00088 if (ret)
00089 goto fail;
00090
00091 while (1) {
00092 int addr_type, auth_len;
00093 int pos;
00094
00095 ret = url_read(sap->ann_fd, recvbuf, sizeof(recvbuf) - 1);
00096 if (ret == AVERROR(EAGAIN))
00097 continue;
00098 if (ret < 0)
00099 goto fail;
00100 recvbuf[ret] = '\0';
00101 if (ret < 8) {
00102 av_log(s, AV_LOG_WARNING, "Received too short packet\n");
00103 continue;
00104 }
00105
00106 if ((recvbuf[0] & 0xe0) != 0x20) {
00107 av_log(s, AV_LOG_WARNING, "Unsupported SAP version packet "
00108 "received\n");
00109 continue;
00110 }
00111
00112 if (recvbuf[0] & 0x04) {
00113 av_log(s, AV_LOG_WARNING, "Received stream deletion "
00114 "announcement\n");
00115 continue;
00116 }
00117 addr_type = recvbuf[0] & 0x10;
00118 auth_len = recvbuf[1];
00119 sap->hash = AV_RB16(&recvbuf[2]);
00120 pos = 4;
00121 if (addr_type)
00122 pos += 16;
00123 else
00124 pos += 4;
00125 pos += auth_len * 4;
00126 if (pos + 4 >= ret) {
00127 av_log(s, AV_LOG_WARNING, "Received too short packet\n");
00128 continue;
00129 }
00130 #define MIME "application/sdp"
00131 if (strcmp(&recvbuf[pos], MIME) == 0) {
00132 pos += strlen(MIME) + 1;
00133 } else if (strncmp(&recvbuf[pos], "v=0\r\n", 5) == 0) {
00134
00135 } else {
00136 av_log(s, AV_LOG_WARNING, "Unsupported mime type %s\n",
00137 &recvbuf[pos]);
00138 continue;
00139 }
00140
00141 sap->sdp = av_strdup(&recvbuf[pos]);
00142 break;
00143 }
00144
00145 av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", sap->sdp);
00146 ffio_init_context(&sap->sdp_pb, sap->sdp, strlen(sap->sdp), 0, NULL, NULL,
00147 NULL, NULL);
00148
00149 infmt = av_find_input_format("sdp");
00150 if (!infmt)
00151 goto fail;
00152 sap->sdp_ctx = avformat_alloc_context();
00153 if (!sap->sdp_ctx) {
00154 ret = AVERROR(ENOMEM);
00155 goto fail;
00156 }
00157 sap->sdp_ctx->max_delay = s->max_delay;
00158 ap->prealloced_context = 1;
00159 ret = av_open_input_stream(&sap->sdp_ctx, &sap->sdp_pb, "temp.sdp",
00160 infmt, ap);
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 = url_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 = url_read(sap->ann_fd, recvbuf, sizeof(recvbuf));
00198 if (ret >= 8) {
00199 uint16_t hash = AV_RB16(&recvbuf[2]);
00200
00201 if (recvbuf[0] & 0x04 && hash == sap->hash) {
00202
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