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 #if HAVE_SYS_SELECT_H
00029 #include <sys/select.h>
00030 #endif
00031 #include <sys/time.h>
00032
00033 struct SAPState {
00034 URLContext *ann_fd;
00035 AVFormatContext *sdp_ctx;
00036 ByteIOContext sdp_pb;
00037 uint16_t hash;
00038 char *sdp;
00039 int eof;
00040 };
00041
00042 static int sap_probe(AVProbeData *p)
00043 {
00044 if (av_strstart(p->filename, "sap:", NULL))
00045 return AVPROBE_SCORE_MAX;
00046 return 0;
00047 }
00048
00049 static int sap_read_close(AVFormatContext *s)
00050 {
00051 struct SAPState *sap = s->priv_data;
00052 if (sap->sdp_ctx)
00053 av_close_input_stream(sap->sdp_ctx);
00054 if (sap->ann_fd)
00055 url_close(sap->ann_fd);
00056 av_freep(&sap->sdp);
00057 ff_network_close();
00058 return 0;
00059 }
00060
00061 static int sap_read_header(AVFormatContext *s,
00062 AVFormatParameters *ap)
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 = url_open(&sap->ann_fd, url, URL_RDONLY);
00087 if (ret)
00088 goto fail;
00089
00090 while (1) {
00091 int addr_type, auth_len;
00092 int pos;
00093
00094 ret = url_read(sap->ann_fd, recvbuf, sizeof(recvbuf) - 1);
00095 if (ret == AVERROR(EAGAIN))
00096 continue;
00097 if (ret < 0)
00098 goto fail;
00099 recvbuf[ret] = '\0';
00100 if (ret < 8) {
00101 av_log(s, AV_LOG_WARNING, "Received too short packet\n");
00102 continue;
00103 }
00104
00105 if ((recvbuf[0] & 0xe0) != 0x20) {
00106 av_log(s, AV_LOG_WARNING, "Unsupported SAP version packet "
00107 "received\n");
00108 continue;
00109 }
00110
00111 if (recvbuf[0] & 0x04) {
00112 av_log(s, AV_LOG_WARNING, "Received stream deletion "
00113 "announcement\n");
00114 continue;
00115 }
00116 addr_type = recvbuf[0] & 0x10;
00117 auth_len = recvbuf[1];
00118 sap->hash = AV_RB16(&recvbuf[2]);
00119 pos = 4;
00120 if (addr_type)
00121 pos += 16;
00122 else
00123 pos += 4;
00124 pos += auth_len * 4;
00125 if (pos + 4 >= ret) {
00126 av_log(s, AV_LOG_WARNING, "Received too short packet\n");
00127 continue;
00128 }
00129 #define MIME "application/sdp"
00130 if (strcmp(&recvbuf[pos], MIME) == 0) {
00131 pos += strlen(MIME) + 1;
00132 } else if (strncmp(&recvbuf[pos], "v=0\r\n", 5) == 0) {
00133
00134 } else {
00135 av_log(s, AV_LOG_WARNING, "Unsupported mime type %s\n",
00136 &recvbuf[pos]);
00137 continue;
00138 }
00139
00140 sap->sdp = av_strdup(&recvbuf[pos]);
00141 break;
00142 }
00143
00144 av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", sap->sdp);
00145 init_put_byte(&sap->sdp_pb, sap->sdp, strlen(sap->sdp), 0, NULL, NULL,
00146 NULL, NULL);
00147
00148 infmt = av_find_input_format("sdp");
00149 if (!infmt)
00150 goto fail;
00151 sap->sdp_ctx = avformat_alloc_context();
00152 if (!sap->sdp_ctx) {
00153 ret = AVERROR(ENOMEM);
00154 goto fail;
00155 }
00156 sap->sdp_ctx->max_delay = s->max_delay;
00157 ap->prealloced_context = 1;
00158 ret = av_open_input_stream(&sap->sdp_ctx, &sap->sdp_pb, "temp.sdp",
00159 infmt, ap);
00160 if (ret < 0)
00161 goto fail;
00162 if (sap->sdp_ctx->ctx_flags & AVFMTCTX_NOHEADER)
00163 s->ctx_flags |= AVFMTCTX_NOHEADER;
00164 for (i = 0; i < sap->sdp_ctx->nb_streams; i++) {
00165 AVStream *st = av_new_stream(s, i);
00166 if (!st) {
00167 ret = AVERROR(ENOMEM);
00168 goto fail;
00169 }
00170 avcodec_copy_context(st->codec, sap->sdp_ctx->streams[i]->codec);
00171 st->time_base = sap->sdp_ctx->streams[i]->time_base;
00172 }
00173
00174 return 0;
00175
00176 fail:
00177 sap_read_close(s);
00178 return ret;
00179 }
00180
00181 static int sap_fetch_packet(AVFormatContext *s, AVPacket *pkt)
00182 {
00183 struct SAPState *sap = s->priv_data;
00184 int fd = url_get_file_handle(sap->ann_fd);
00185 int n, ret;
00186 fd_set rfds;
00187 struct timeval tv;
00188 uint8_t recvbuf[1500];
00189
00190 if (sap->eof)
00191 return AVERROR_EOF;
00192
00193 while (1) {
00194 FD_ZERO(&rfds);
00195 FD_SET(fd, &rfds);
00196 tv.tv_sec = tv.tv_usec = 0;
00197 n = select(fd + 1, &rfds, NULL, NULL, &tv);
00198 if (n <= 0 || !FD_ISSET(fd, &rfds))
00199 break;
00200 ret = url_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 = av_new_stream(s, i);
00218 if (!st) {
00219 av_free_packet(pkt);
00220 return AVERROR(ENOMEM);
00221 }
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 sap_demuxer = {
00230 "sap",
00231 NULL_IF_CONFIG_SMALL("SAP input format"),
00232 sizeof(struct SAPState),
00233 sap_probe,
00234 sap_read_header,
00235 sap_fetch_packet,
00236 sap_read_close,
00237 .flags = AVFMT_NOFILE,
00238 };
00239