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
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 avformat_close_input(&sap->sdp_ctx);
00055 if (sap->ann_fd)
00056 ffurl_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 {
00064 struct SAPState *sap = s->priv_data;
00065 char host[1024], path[1024], url[1024];
00066 uint8_t recvbuf[1500];
00067 int port;
00068 int ret, i;
00069 AVInputFormat* infmt;
00070
00071 if (!ff_network_init())
00072 return AVERROR(EIO);
00073
00074 av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &port,
00075 path, sizeof(path), s->filename);
00076 if (port < 0)
00077 port = 9875;
00078
00079 if (!host[0]) {
00080
00081 av_strlcpy(host, "224.2.127.254", sizeof(host));
00082 }
00083
00084 ff_url_join(url, sizeof(url), "udp", NULL, host, port, "?localport=%d",
00085 port);
00086 ret = ffurl_open(&sap->ann_fd, url, AVIO_FLAG_READ,
00087 &s->interrupt_callback, NULL);
00088 if (ret)
00089 goto fail;
00090
00091 while (1) {
00092 int addr_type, auth_len;
00093 int pos;
00094
00095 ret = ffurl_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 sap->sdp_ctx->pb = &sap->sdp_pb;
00159 sap->sdp_ctx->interrupt_callback = s->interrupt_callback;
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 = avformat_new_stream(s, NULL);
00167 if (!st) {
00168 ret = AVERROR(ENOMEM);
00169 goto fail;
00170 }
00171 st->id = i;
00172 avcodec_copy_context(st->codec, sap->sdp_ctx->streams[i]->codec);
00173 st->time_base = sap->sdp_ctx->streams[i]->time_base;
00174 }
00175
00176 return 0;
00177
00178 fail:
00179 sap_read_close(s);
00180 return ret;
00181 }
00182
00183 static int sap_fetch_packet(AVFormatContext *s, AVPacket *pkt)
00184 {
00185 struct SAPState *sap = s->priv_data;
00186 int fd = ffurl_get_file_handle(sap->ann_fd);
00187 int n, ret;
00188 struct pollfd p = {fd, POLLIN, 0};
00189 uint8_t recvbuf[1500];
00190
00191 if (sap->eof)
00192 return AVERROR_EOF;
00193
00194 while (1) {
00195 n = poll(&p, 1, 0);
00196 if (n <= 0 || !(p.revents & POLLIN))
00197 break;
00198 ret = ffurl_read(sap->ann_fd, recvbuf, sizeof(recvbuf));
00199 if (ret >= 8) {
00200 uint16_t hash = AV_RB16(&recvbuf[2]);
00201
00202 if (recvbuf[0] & 0x04 && hash == sap->hash) {
00203
00204 sap->eof = 1;
00205 return AVERROR_EOF;
00206 }
00207 }
00208 }
00209 ret = av_read_frame(sap->sdp_ctx, pkt);
00210 if (ret < 0)
00211 return ret;
00212 if (s->ctx_flags & AVFMTCTX_NOHEADER) {
00213 while (sap->sdp_ctx->nb_streams > s->nb_streams) {
00214 int i = s->nb_streams;
00215 AVStream *st = avformat_new_stream(s, NULL);
00216 if (!st) {
00217 av_free_packet(pkt);
00218 return AVERROR(ENOMEM);
00219 }
00220 st->id = i;
00221 avcodec_copy_context(st->codec, sap->sdp_ctx->streams[i]->codec);
00222 st->time_base = sap->sdp_ctx->streams[i]->time_base;
00223 }
00224 }
00225 return ret;
00226 }
00227
00228 AVInputFormat ff_sap_demuxer = {
00229 .name = "sap",
00230 .long_name = NULL_IF_CONFIG_SMALL("SAP input"),
00231 .priv_data_size = sizeof(struct SAPState),
00232 .read_probe = sap_probe,
00233 .read_header = sap_read_header,
00234 .read_packet = sap_fetch_packet,
00235 .read_close = sap_read_close,
00236 .flags = AVFMT_NOFILE,
00237 };