FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/sapdec.c
Date: 2024-03-28 04:31:58
Exec Total Coverage
Lines: 3 123 2.4%
Functions: 1 4 25.0%
Branches: 1 66 1.5%

Line Branch Exec Source
1 /*
2 * Session Announcement Protocol (RFC 2974) demuxer
3 * Copyright (c) 2010 Martin Storsjo
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
21
22 #include "avformat.h"
23 #include "demux.h"
24 #include "libavutil/avstring.h"
25 #include "libavutil/intreadwrite.h"
26 #include "network.h"
27 #include "os_support.h"
28 #include "internal.h"
29 #include "avio_internal.h"
30 #include "url.h"
31 #include "rtpdec.h"
32 #if HAVE_POLL_H
33 #include <poll.h>
34 #endif
35
36 struct SAPState {
37 URLContext *ann_fd;
38 AVFormatContext *sdp_ctx;
39 FFIOContext sdp_pb;
40 uint16_t hash;
41 char *sdp;
42 int eof;
43 };
44
45 3791 static int sap_probe(const AVProbeData *p)
46 {
47
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3791 times.
3791 if (av_strstart(p->filename, "sap:", NULL))
48 return AVPROBE_SCORE_MAX;
49 3791 return 0;
50 }
51
52 static int sap_read_close(AVFormatContext *s)
53 {
54 struct SAPState *sap = s->priv_data;
55 if (sap->sdp_ctx)
56 avformat_close_input(&sap->sdp_ctx);
57 ffurl_closep(&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 {
65 struct SAPState *sap = s->priv_data;
66 char host[1024], path[1024], url[1024];
67 uint8_t recvbuf[RTP_MAX_PACKET_LENGTH];
68 const AVInputFormat *infmt;
69 int port;
70 int ret, i;
71
72 if (!ff_network_init())
73 return AVERROR(EIO);
74
75 av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &port,
76 path, sizeof(path), s->url);
77 if (port < 0)
78 port = 9875;
79
80 if (!host[0]) {
81 /* Listen for announcements on sap.mcast.net if no host was specified */
82 av_strlcpy(host, "224.2.127.254", sizeof(host));
83 }
84
85 ff_url_join(url, sizeof(url), "udp", NULL, host, port, "?localport=%d",
86 port);
87 ret = ffurl_open_whitelist(&sap->ann_fd, url, AVIO_FLAG_READ,
88 &s->interrupt_callback, NULL,
89 s->protocol_whitelist, s->protocol_blacklist, NULL);
90 if (ret)
91 goto fail;
92
93 while (1) {
94 int addr_type, auth_len;
95 int pos;
96
97 ret = ffurl_read(sap->ann_fd, recvbuf, sizeof(recvbuf) - 1);
98 if (ret == AVERROR(EAGAIN))
99 continue;
100 if (ret < 0)
101 goto fail;
102 recvbuf[ret] = '\0'; /* Null terminate for easier parsing */
103 if (ret < 8) {
104 av_log(s, AV_LOG_WARNING, "Received too short packet\n");
105 continue;
106 }
107
108 if ((recvbuf[0] & 0xe0) != 0x20) {
109 av_log(s, AV_LOG_WARNING, "Unsupported SAP version packet "
110 "received\n");
111 continue;
112 }
113
114 if (recvbuf[0] & 0x04) {
115 av_log(s, AV_LOG_WARNING, "Received stream deletion "
116 "announcement\n");
117 continue;
118 }
119 addr_type = recvbuf[0] & 0x10;
120 auth_len = recvbuf[1];
121 sap->hash = AV_RB16(&recvbuf[2]);
122 pos = 4;
123 if (addr_type)
124 pos += 16; /* IPv6 */
125 else
126 pos += 4; /* IPv4 */
127 pos += auth_len * 4;
128 if (pos + 4 >= ret) {
129 av_log(s, AV_LOG_WARNING, "Received too short packet\n");
130 continue;
131 }
132 #define MIME "application/sdp"
133 if (strcmp(&recvbuf[pos], MIME) == 0) {
134 pos += strlen(MIME) + 1;
135 } else if (strncmp(&recvbuf[pos], "v=0\r\n", 5) == 0) {
136 // Direct SDP without a mime type
137 } else {
138 av_log(s, AV_LOG_WARNING, "Unsupported mime type %s\n",
139 &recvbuf[pos]);
140 continue;
141 }
142
143 sap->sdp = av_strdup(&recvbuf[pos]);
144 if (!sap->sdp) {
145 ret = AVERROR(ENOMEM);
146 goto fail;
147 }
148 break;
149 }
150
151 av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", sap->sdp);
152 ffio_init_read_context(&sap->sdp_pb, sap->sdp, strlen(sap->sdp));
153
154 infmt = av_find_input_format("sdp");
155 if (!infmt)
156 goto fail;
157 sap->sdp_ctx = avformat_alloc_context();
158 if (!sap->sdp_ctx) {
159 ret = AVERROR(ENOMEM);
160 goto fail;
161 }
162 sap->sdp_ctx->max_delay = s->max_delay;
163 sap->sdp_ctx->pb = &sap->sdp_pb.pub;
164 sap->sdp_ctx->interrupt_callback = s->interrupt_callback;
165
166 if ((ret = ff_copy_whiteblacklists(sap->sdp_ctx, s)) < 0)
167 goto fail;
168
169 ret = avformat_open_input(&sap->sdp_ctx, "temp.sdp", infmt, NULL);
170 if (ret < 0)
171 goto fail;
172 if (sap->sdp_ctx->ctx_flags & AVFMTCTX_NOHEADER)
173 s->ctx_flags |= AVFMTCTX_NOHEADER;
174 for (i = 0; i < sap->sdp_ctx->nb_streams; i++) {
175 AVStream *st = avformat_new_stream(s, NULL);
176 if (!st) {
177 ret = AVERROR(ENOMEM);
178 goto fail;
179 }
180 st->id = i;
181 avcodec_parameters_copy(st->codecpar, sap->sdp_ctx->streams[i]->codecpar);
182 st->time_base = sap->sdp_ctx->streams[i]->time_base;
183 }
184
185 return 0;
186
187 fail:
188 sap_read_close(s);
189 return ret;
190 }
191
192 static int sap_fetch_packet(AVFormatContext *s, AVPacket *pkt)
193 {
194 struct SAPState *sap = s->priv_data;
195 int fd = ffurl_get_file_handle(sap->ann_fd);
196 int n, ret;
197 struct pollfd p = {fd, POLLIN, 0};
198 uint8_t recvbuf[RTP_MAX_PACKET_LENGTH];
199
200 if (sap->eof)
201 return AVERROR_EOF;
202
203 while (1) {
204 n = poll(&p, 1, 0);
205 if (n <= 0 || !(p.revents & POLLIN))
206 break;
207 ret = ffurl_read(sap->ann_fd, recvbuf, sizeof(recvbuf));
208 if (ret >= 8) {
209 uint16_t hash = AV_RB16(&recvbuf[2]);
210 /* Should ideally check the source IP address, too */
211 if (recvbuf[0] & 0x04 && hash == sap->hash) {
212 /* Stream deletion */
213 sap->eof = 1;
214 return AVERROR_EOF;
215 }
216 }
217 }
218 ret = av_read_frame(sap->sdp_ctx, pkt);
219 if (ret < 0)
220 return ret;
221 if (s->ctx_flags & AVFMTCTX_NOHEADER) {
222 while (sap->sdp_ctx->nb_streams > s->nb_streams) {
223 int i = s->nb_streams;
224 AVStream *st = avformat_new_stream(s, NULL);
225 if (!st) {
226 return AVERROR(ENOMEM);
227 }
228 st->id = i;
229 avcodec_parameters_copy(st->codecpar, sap->sdp_ctx->streams[i]->codecpar);
230 st->time_base = sap->sdp_ctx->streams[i]->time_base;
231 }
232 }
233 return ret;
234 }
235
236 const FFInputFormat ff_sap_demuxer = {
237 .p.name = "sap",
238 .p.long_name = NULL_IF_CONFIG_SMALL("SAP input"),
239 .p.flags = AVFMT_NOFILE,
240 .priv_data_size = sizeof(struct SAPState),
241 .read_probe = sap_probe,
242 .read_header = sap_read_header,
243 .read_packet = sap_fetch_packet,
244 .read_close = sap_read_close,
245 };
246