ffmpeg / libavformat / sapdec.c @ bc371aca
History | View | Annotate | Download (6.68 KB)
1 |
/*
|
---|---|
2 |
* Session Announcement Protocol (RFC 2974) demuxer
|
3 |
* Copyright (c) 2010 Martin Storsjo
|
4 |
*
|
5 |
* This file is part of Libav.
|
6 |
*
|
7 |
* Libav 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.
|
11 |
*
|
12 |
* Libav 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.
|
16 |
*
|
17 |
* You should have received a copy of the GNU Lesser General Public
|
18 |
* License along with Libav; if not, write to the Free Software
|
19 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
20 |
*/
|
21 |
|
22 |
#include "avformat.h" |
23 |
#include "libavutil/avstring.h" |
24 |
#include "libavutil/intreadwrite.h" |
25 |
#include "network.h" |
26 |
#include "os_support.h" |
27 |
#include "internal.h" |
28 |
#include "avio_internal.h" |
29 |
#include "url.h" |
30 |
#if HAVE_POLL_H
|
31 |
#include <poll.h> |
32 |
#endif
|
33 |
#include <sys/time.h> |
34 |
|
35 |
struct SAPState {
|
36 |
URLContext *ann_fd; |
37 |
AVFormatContext *sdp_ctx; |
38 |
AVIOContext sdp_pb; |
39 |
uint16_t hash; |
40 |
char *sdp;
|
41 |
int eof;
|
42 |
}; |
43 |
|
44 |
static int sap_probe(AVProbeData *p) |
45 |
{ |
46 |
if (av_strstart(p->filename, "sap:", NULL)) |
47 |
return AVPROBE_SCORE_MAX;
|
48 |
return 0; |
49 |
} |
50 |
|
51 |
static int sap_read_close(AVFormatContext *s) |
52 |
{ |
53 |
struct SAPState *sap = s->priv_data;
|
54 |
if (sap->sdp_ctx)
|
55 |
av_close_input_stream(sap->sdp_ctx); |
56 |
if (sap->ann_fd)
|
57 |
url_close(sap->ann_fd); |
58 |
av_freep(&sap->sdp); |
59 |
ff_network_close(); |
60 |
return 0; |
61 |
} |
62 |
|
63 |
static int sap_read_header(AVFormatContext *s, |
64 |
AVFormatParameters *ap) |
65 |
{ |
66 |
struct SAPState *sap = s->priv_data;
|
67 |
char host[1024], path[1024], url[1024]; |
68 |
uint8_t recvbuf[1500];
|
69 |
int port;
|
70 |
int ret, i;
|
71 |
AVInputFormat* infmt; |
72 |
|
73 |
if (!ff_network_init())
|
74 |
return AVERROR(EIO);
|
75 |
|
76 |
av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &port, |
77 |
path, sizeof(path), s->filename);
|
78 |
if (port < 0) |
79 |
port = 9875;
|
80 |
|
81 |
if (!host[0]) { |
82 |
/* Listen for announcements on sap.mcast.net if no host was specified */
|
83 |
av_strlcpy(host, "224.2.127.254", sizeof(host)); |
84 |
} |
85 |
|
86 |
ff_url_join(url, sizeof(url), "udp", NULL, host, port, "?localport=%d", |
87 |
port); |
88 |
ret = ffurl_open(&sap->ann_fd, url, URL_RDONLY); |
89 |
if (ret)
|
90 |
goto fail;
|
91 |
|
92 |
while (1) { |
93 |
int addr_type, auth_len;
|
94 |
int pos;
|
95 |
|
96 |
ret = ffurl_read(sap->ann_fd, recvbuf, sizeof(recvbuf) - 1); |
97 |
if (ret == AVERROR(EAGAIN))
|
98 |
continue;
|
99 |
if (ret < 0) |
100 |
goto fail;
|
101 |
recvbuf[ret] = '\0'; /* Null terminate for easier parsing */ |
102 |
if (ret < 8) { |
103 |
av_log(s, AV_LOG_WARNING, "Received too short packet\n");
|
104 |
continue;
|
105 |
} |
106 |
|
107 |
if ((recvbuf[0] & 0xe0) != 0x20) { |
108 |
av_log(s, AV_LOG_WARNING, "Unsupported SAP version packet "
|
109 |
"received\n");
|
110 |
continue;
|
111 |
} |
112 |
|
113 |
if (recvbuf[0] & 0x04) { |
114 |
av_log(s, AV_LOG_WARNING, "Received stream deletion "
|
115 |
"announcement\n");
|
116 |
continue;
|
117 |
} |
118 |
addr_type = recvbuf[0] & 0x10; |
119 |
auth_len = recvbuf[1];
|
120 |
sap->hash = AV_RB16(&recvbuf[2]);
|
121 |
pos = 4;
|
122 |
if (addr_type)
|
123 |
pos += 16; /* IPv6 */ |
124 |
else
|
125 |
pos += 4; /* IPv4 */ |
126 |
pos += auth_len * 4;
|
127 |
if (pos + 4 >= ret) { |
128 |
av_log(s, AV_LOG_WARNING, "Received too short packet\n");
|
129 |
continue;
|
130 |
} |
131 |
#define MIME "application/sdp" |
132 |
if (strcmp(&recvbuf[pos], MIME) == 0) { |
133 |
pos += strlen(MIME) + 1;
|
134 |
} else if (strncmp(&recvbuf[pos], "v=0\r\n", 5) == 0) { |
135 |
// Direct SDP without a mime type
|
136 |
} else {
|
137 |
av_log(s, AV_LOG_WARNING, "Unsupported mime type %s\n",
|
138 |
&recvbuf[pos]); |
139 |
continue;
|
140 |
} |
141 |
|
142 |
sap->sdp = av_strdup(&recvbuf[pos]); |
143 |
break;
|
144 |
} |
145 |
|
146 |
av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", sap->sdp);
|
147 |
ffio_init_context(&sap->sdp_pb, sap->sdp, strlen(sap->sdp), 0, NULL, NULL, |
148 |
NULL, NULL); |
149 |
|
150 |
infmt = av_find_input_format("sdp");
|
151 |
if (!infmt)
|
152 |
goto fail;
|
153 |
sap->sdp_ctx = avformat_alloc_context(); |
154 |
if (!sap->sdp_ctx) {
|
155 |
ret = AVERROR(ENOMEM); |
156 |
goto fail;
|
157 |
} |
158 |
sap->sdp_ctx->max_delay = s->max_delay; |
159 |
ap->prealloced_context = 1;
|
160 |
ret = av_open_input_stream(&sap->sdp_ctx, &sap->sdp_pb, "temp.sdp",
|
161 |
infmt, ap); |
162 |
if (ret < 0) |
163 |
goto fail;
|
164 |
if (sap->sdp_ctx->ctx_flags & AVFMTCTX_NOHEADER)
|
165 |
s->ctx_flags |= AVFMTCTX_NOHEADER; |
166 |
for (i = 0; i < sap->sdp_ctx->nb_streams; i++) { |
167 |
AVStream *st = av_new_stream(s, i); |
168 |
if (!st) {
|
169 |
ret = AVERROR(ENOMEM); |
170 |
goto fail;
|
171 |
} |
172 |
avcodec_copy_context(st->codec, sap->sdp_ctx->streams[i]->codec); |
173 |
st->time_base = sap->sdp_ctx->streams[i]->time_base; |
174 |
} |
175 |
|
176 |
return 0; |
177 |
|
178 |
fail:
|
179 |
sap_read_close(s); |
180 |
return ret;
|
181 |
} |
182 |
|
183 |
static int sap_fetch_packet(AVFormatContext *s, AVPacket *pkt) |
184 |
{ |
185 |
struct SAPState *sap = s->priv_data;
|
186 |
int fd = url_get_file_handle(sap->ann_fd);
|
187 |
int n, ret;
|
188 |
struct pollfd p = {fd, POLLIN, 0}; |
189 |
uint8_t recvbuf[1500];
|
190 |
|
191 |
if (sap->eof)
|
192 |
return AVERROR_EOF;
|
193 |
|
194 |
while (1) { |
195 |
n = poll(&p, 1, 0); |
196 |
if (n <= 0 || !(p.revents & POLLIN)) |
197 |
break;
|
198 |
ret = ffurl_read(sap->ann_fd, recvbuf, sizeof(recvbuf));
|
199 |
if (ret >= 8) { |
200 |
uint16_t hash = AV_RB16(&recvbuf[2]);
|
201 |
/* Should ideally check the source IP address, too */
|
202 |
if (recvbuf[0] & 0x04 && hash == sap->hash) { |
203 |
/* Stream deletion */
|
204 |
sap->eof = 1;
|
205 |
return AVERROR_EOF;
|
206 |
} |
207 |
} |
208 |
} |
209 |
ret = av_read_frame(sap->sdp_ctx, pkt); |
210 |
if (ret < 0) |
211 |
return ret;
|
212 |
if (s->ctx_flags & AVFMTCTX_NOHEADER) {
|
213 |
while (sap->sdp_ctx->nb_streams > s->nb_streams) {
|
214 |
int i = s->nb_streams;
|
215 |
AVStream *st = av_new_stream(s, i); |
216 |
if (!st) {
|
217 |
av_free_packet(pkt); |
218 |
return AVERROR(ENOMEM);
|
219 |
} |
220 |
avcodec_copy_context(st->codec, sap->sdp_ctx->streams[i]->codec); |
221 |
st->time_base = sap->sdp_ctx->streams[i]->time_base; |
222 |
} |
223 |
} |
224 |
return ret;
|
225 |
} |
226 |
|
227 |
AVInputFormat ff_sap_demuxer = { |
228 |
"sap",
|
229 |
NULL_IF_CONFIG_SMALL("SAP input format"),
|
230 |
sizeof(struct SAPState), |
231 |
sap_probe, |
232 |
sap_read_header, |
233 |
sap_fetch_packet, |
234 |
sap_read_close, |
235 |
.flags = AVFMT_NOFILE, |
236 |
}; |
237 |
|