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 #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 avformat_close_input(&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
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 &s->interrupt_callback, NULL);
00090 if (ret)
00091 goto fail;
00092
00093 while (1) {
00094 int addr_type, auth_len;
00095 int pos;
00096
00097 ret = ffurl_read(sap->ann_fd, recvbuf, sizeof(recvbuf) - 1);
00098 if (ret == AVERROR(EAGAIN))
00099 continue;
00100 if (ret < 0)
00101 goto fail;
00102 recvbuf[ret] = '\0';
00103 if (ret < 8) {
00104 av_log(s, AV_LOG_WARNING, "Received too short packet\n");
00105 continue;
00106 }
00107
00108 if ((recvbuf[0] & 0xe0) != 0x20) {
00109 av_log(s, AV_LOG_WARNING, "Unsupported SAP version packet "
00110 "received\n");
00111 continue;
00112 }
00113
00114 if (recvbuf[0] & 0x04) {
00115 av_log(s, AV_LOG_WARNING, "Received stream deletion "
00116 "announcement\n");
00117 continue;
00118 }
00119 addr_type = recvbuf[0] & 0x10;
00120 auth_len = recvbuf[1];
00121 sap->hash = AV_RB16(&recvbuf[2]);
00122 pos = 4;
00123 if (addr_type)
00124 pos += 16;
00125 else
00126 pos += 4;
00127 pos += auth_len * 4;
00128 if (pos + 4 >= ret) {
00129 av_log(s, AV_LOG_WARNING, "Received too short packet\n");
00130 continue;
00131 }
00132 #define MIME "application/sdp"
00133 if (strcmp(&recvbuf[pos], MIME) == 0) {
00134 pos += strlen(MIME) + 1;
00135 } else if (strncmp(&recvbuf[pos], "v=0\r\n", 5) == 0) {
00136
00137 } else {
00138 av_log(s, AV_LOG_WARNING, "Unsupported mime type %s\n",
00139 &recvbuf[pos]);
00140 continue;
00141 }
00142
00143 sap->sdp = av_strdup(&recvbuf[pos]);
00144 break;
00145 }
00146
00147 av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", sap->sdp);
00148 ffio_init_context(&sap->sdp_pb, sap->sdp, strlen(sap->sdp), 0, NULL, NULL,
00149 NULL, NULL);
00150
00151 infmt = av_find_input_format("sdp");
00152 if (!infmt)
00153 goto fail;
00154 sap->sdp_ctx = avformat_alloc_context();
00155 if (!sap->sdp_ctx) {
00156 ret = AVERROR(ENOMEM);
00157 goto fail;
00158 }
00159 sap->sdp_ctx->max_delay = s->max_delay;
00160 sap->sdp_ctx->pb = &sap->sdp_pb;
00161 sap->sdp_ctx->interrupt_callback = s->interrupt_callback;
00162 ret = avformat_open_input(&sap->sdp_ctx, "temp.sdp", infmt, NULL);
00163 if (ret < 0)
00164 goto fail;
00165 if (sap->sdp_ctx->ctx_flags & AVFMTCTX_NOHEADER)
00166 s->ctx_flags |= AVFMTCTX_NOHEADER;
00167 for (i = 0; i < sap->sdp_ctx->nb_streams; i++) {
00168 AVStream *st = avformat_new_stream(s, NULL);
00169 if (!st) {
00170 ret = AVERROR(ENOMEM);
00171 goto fail;
00172 }
00173 st->id = i;
00174 avcodec_copy_context(st->codec, sap->sdp_ctx->streams[i]->codec);
00175 st->time_base = sap->sdp_ctx->streams[i]->time_base;
00176 }
00177
00178 return 0;
00179
00180 fail:
00181 sap_read_close(s);
00182 return ret;
00183 }
00184
00185 static int sap_fetch_packet(AVFormatContext *s, AVPacket *pkt)
00186 {
00187 struct SAPState *sap = s->priv_data;
00188 int fd = ffurl_get_file_handle(sap->ann_fd);
00189 int n, ret;
00190 struct pollfd p = {fd, POLLIN, 0};
00191 uint8_t recvbuf[1500];
00192
00193 if (sap->eof)
00194 return AVERROR_EOF;
00195
00196 while (1) {
00197 n = poll(&p, 1, 0);
00198 if (n <= 0 || !(p.revents & POLLIN))
00199 break;
00200 ret = ffurl_read(sap->ann_fd, recvbuf, sizeof(recvbuf));
00201 if (ret >= 8) {
00202 uint16_t hash = AV_RB16(&recvbuf[2]);
00203
00204 if (recvbuf[0] & 0x04 && hash == sap->hash) {
00205
00206 sap->eof = 1;
00207 return AVERROR_EOF;
00208 }
00209 }
00210 }
00211 ret = av_read_frame(sap->sdp_ctx, pkt);
00212 if (ret < 0)
00213 return ret;
00214 if (s->ctx_flags & AVFMTCTX_NOHEADER) {
00215 while (sap->sdp_ctx->nb_streams > s->nb_streams) {
00216 int i = s->nb_streams;
00217 AVStream *st = avformat_new_stream(s, NULL);
00218 if (!st) {
00219 av_free_packet(pkt);
00220 return AVERROR(ENOMEM);
00221 }
00222 st->id = i;
00223 avcodec_copy_context(st->codec, sap->sdp_ctx->streams[i]->codec);
00224 st->time_base = sap->sdp_ctx->streams[i]->time_base;
00225 }
00226 }
00227 return ret;
00228 }
00229
00230 AVInputFormat ff_sap_demuxer = {
00231 .name = "sap",
00232 .long_name = NULL_IF_CONFIG_SMALL("SAP input format"),
00233 .priv_data_size = sizeof(struct SAPState),
00234 .read_probe = sap_probe,
00235 .read_header = sap_read_header,
00236 .read_packet = sap_fetch_packet,
00237 .read_close = sap_read_close,
00238 .flags = AVFMT_NOFILE,
00239 };
00240