2 * Session Announcement Protocol (RFC 2974) demuxer
3 * Copyright (c) 2010 Martin Storsjo
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "libavutil/avstring.h"
24 #include "libavutil/intreadwrite.h"
26 #include "os_support.h"
29 #include <sys/select.h>
35 AVFormatContext *sdp_ctx;
42 static int sap_probe(AVProbeData *p)
44 if (av_strstart(p->filename, "sap:", NULL))
45 return AVPROBE_SCORE_MAX;
49 static int sap_read_close(AVFormatContext *s)
51 struct SAPState *sap = s->priv_data;
53 av_close_input_stream(sap->sdp_ctx);
55 url_close(sap->ann_fd);
61 static int sap_read_header(AVFormatContext *s,
62 AVFormatParameters *ap)
64 struct SAPState *sap = s->priv_data;
65 char host[1024], path[1024], url[1024];
66 uint8_t recvbuf[1500];
71 if (!ff_network_init())
74 av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &port,
75 path, sizeof(path), s->filename);
80 /* Listen for announcements on sap.mcast.net if no host was specified */
81 av_strlcpy(host, "224.2.127.254", sizeof(host));
84 ff_url_join(url, sizeof(url), "udp", NULL, host, port, "?localport=%d",
86 ret = url_open(&sap->ann_fd, url, URL_RDONLY);
91 int addr_type, auth_len;
94 ret = url_read(sap->ann_fd, recvbuf, sizeof(recvbuf) - 1);
95 if (ret == AVERROR(EAGAIN))
99 recvbuf[ret] = '\0'; /* Null terminate for easier parsing */
101 av_log(s, AV_LOG_WARNING, "Received too short packet\n");
105 if ((recvbuf[0] & 0xe0) != 0x20) {
106 av_log(s, AV_LOG_WARNING, "Unsupported SAP version packet "
111 if (recvbuf[0] & 0x04) {
112 av_log(s, AV_LOG_WARNING, "Received stream deletion "
116 addr_type = recvbuf[0] & 0x10;
117 auth_len = recvbuf[1];
118 sap->hash = AV_RB16(&recvbuf[2]);
121 pos += 16; /* IPv6 */
125 if (pos + 4 >= ret) {
126 av_log(s, AV_LOG_WARNING, "Received too short packet\n");
129 #define MIME "application/sdp"
130 if (strcmp(&recvbuf[pos], MIME) == 0) {
131 pos += strlen(MIME) + 1;
132 } else if (strncmp(&recvbuf[pos], "v=0\r\n", 5) == 0) {
133 // Direct SDP without a mime type
135 av_log(s, AV_LOG_WARNING, "Unsupported mime type %s\n",
140 sap->sdp = av_strdup(&recvbuf[pos]);
144 av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", sap->sdp);
145 init_put_byte(&sap->sdp_pb, sap->sdp, strlen(sap->sdp), 0, NULL, NULL,
148 infmt = av_find_input_format("sdp");
151 sap->sdp_ctx = avformat_alloc_context();
153 ret = AVERROR(ENOMEM);
156 sap->sdp_ctx->max_delay = s->max_delay;
157 ap->prealloced_context = 1;
158 ret = av_open_input_stream(&sap->sdp_ctx, &sap->sdp_pb, "temp.sdp",
162 if (sap->sdp_ctx->ctx_flags & AVFMTCTX_NOHEADER)
163 s->ctx_flags |= AVFMTCTX_NOHEADER;
164 for (i = 0; i < sap->sdp_ctx->nb_streams; i++) {
165 AVStream *st = av_new_stream(s, i);
167 ret = AVERROR(ENOMEM);
170 avcodec_copy_context(st->codec, sap->sdp_ctx->streams[i]->codec);
171 st->time_base = sap->sdp_ctx->streams[i]->time_base;
181 static int sap_fetch_packet(AVFormatContext *s, AVPacket *pkt)
183 struct SAPState *sap = s->priv_data;
184 int fd = url_get_file_handle(sap->ann_fd);
188 uint8_t recvbuf[1500];
196 tv.tv_sec = tv.tv_usec = 0;
197 n = select(fd + 1, &rfds, NULL, NULL, &tv);
198 if (n <= 0 || !FD_ISSET(fd, &rfds))
200 ret = url_read(sap->ann_fd, recvbuf, sizeof(recvbuf));
202 uint16_t hash = AV_RB16(&recvbuf[2]);
203 /* Should ideally check the source IP address, too */
204 if (recvbuf[0] & 0x04 && hash == sap->hash) {
205 /* Stream deletion */
211 ret = av_read_frame(sap->sdp_ctx, pkt);
214 if (s->ctx_flags & AVFMTCTX_NOHEADER) {
215 while (sap->sdp_ctx->nb_streams > s->nb_streams) {
216 int i = s->nb_streams;
217 AVStream *st = av_new_stream(s, i);
220 return AVERROR(ENOMEM);
222 avcodec_copy_context(st->codec, sap->sdp_ctx->streams[i]->codec);
223 st->time_base = sap->sdp_ctx->streams[i]->time_base;
229 AVInputFormat ff_sap_demuxer = {
231 NULL_IF_CONFIG_SMALL("SAP input format"),
232 sizeof(struct SAPState),
237 .flags = AVFMT_NOFILE,