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 {
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 = ffurl_open(&sap->ann_fd, url, AVIO_FLAG_READ,
00088 &s->interrupt_callback, NULL);
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';
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;
00124 else
00125 pos += 4;
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
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 sap->sdp_ctx->interrupt_callback = s->interrupt_callback;
00161 ret = avformat_open_input(&sap->sdp_ctx, "temp.sdp", infmt, NULL);
00162 if (ret < 0)
00163 goto fail;
00164 if (sap->sdp_ctx->ctx_flags & AVFMTCTX_NOHEADER)
00165 s->ctx_flags |= AVFMTCTX_NOHEADER;
00166 for (i = 0; i < sap->sdp_ctx->nb_streams; i++) {
00167 AVStream *st = avformat_new_stream(s, NULL);
00168 if (!st) {
00169 ret = AVERROR(ENOMEM);
00170 goto fail;
00171 }
00172 st->id = i;
00173 avcodec_copy_context(st->codec, sap->sdp_ctx->streams[i]->codec);
00174 st->time_base = sap->sdp_ctx->streams[i]->time_base;
00175 }
00176
00177 return 0;
00178
00179 fail:
00180 sap_read_close(s);
00181 return ret;
00182 }
00183
00184 static int sap_fetch_packet(AVFormatContext *s, AVPacket *pkt)
00185 {
00186 struct SAPState *sap = s->priv_data;
00187 int fd = ffurl_get_file_handle(sap->ann_fd);
00188 int n, ret;
00189 struct pollfd p = {fd, POLLIN, 0};
00190 uint8_t recvbuf[1500];
00191
00192 if (sap->eof)
00193 return AVERROR_EOF;
00194
00195 while (1) {
00196 n = poll(&p, 1, 0);
00197 if (n <= 0 || !(p.revents & POLLIN))
00198 break;
00199 ret = ffurl_read(sap->ann_fd, recvbuf, sizeof(recvbuf));
00200 if (ret >= 8) {
00201 uint16_t hash = AV_RB16(&recvbuf[2]);
00202
00203 if (recvbuf[0] & 0x04 && hash == sap->hash) {
00204
00205 sap->eof = 1;
00206 return AVERROR_EOF;
00207 }
00208 }
00209 }
00210 ret = av_read_frame(sap->sdp_ctx, pkt);
00211 if (ret < 0)
00212 return ret;
00213 if (s->ctx_flags & AVFMTCTX_NOHEADER) {
00214 while (sap->sdp_ctx->nb_streams > s->nb_streams) {
00215 int i = s->nb_streams;
00216 AVStream *st = avformat_new_stream(s, NULL);
00217 if (!st) {
00218 av_free_packet(pkt);
00219 return AVERROR(ENOMEM);
00220 }
00221 st->id = i;
00222 avcodec_copy_context(st->codec, sap->sdp_ctx->streams[i]->codec);
00223 st->time_base = sap->sdp_ctx->streams[i]->time_base;
00224 }
00225 }
00226 return ret;
00227 }
00228
00229 AVInputFormat ff_sap_demuxer = {
00230 .name = "sap",
00231 .long_name = NULL_IF_CONFIG_SMALL("SAP input format"),
00232 .priv_data_size = sizeof(struct SAPState),
00233 .read_probe = sap_probe,
00234 .read_header = sap_read_header,
00235 .read_packet = sap_fetch_packet,
00236 .read_close = sap_read_close,
00237 .flags = AVFMT_NOFILE,
00238 };