FFmpeg
Loading...
Searching...
No Matches
sapdec.c
Go to the documentation of this file.
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"
26#include "libavutil/mem.h"
27#include "network.h"
28#include "os_support.h"
29#include "internal.h"
30#include "avio_internal.h"
31#include "url.h"
32#include "rtpdec.h"
33#if HAVE_POLL_H
34#include <poll.h>
35#endif
36
45
46static int sap_probe(const AVProbeData *p)
47{
48 if (av_strstart(p->filename, "sap:", NULL))
49 return AVPROBE_SCORE_MAX;
50 return 0;
51}
52
54{
55 struct SAPState *sap = s->priv_data;
56 if (sap->sdp_ctx)
58 ffurl_closep(&sap->ann_fd);
59 av_freep(&sap->sdp);
61 return 0;
62}
63
65{
66 struct SAPState *sap = s->priv_data;
67 char host[1024], path[1024], url[1024];
68 uint8_t recvbuf[RTP_MAX_PACKET_LENGTH];
69 const AVInputFormat *infmt;
70 int port;
71 int ret, i;
72
73 if ((ret = ff_network_init()) < 0)
74 return ret;
75
76 av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &port,
77 path, sizeof(path), s->url);
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);
89 &s->interrupt_callback, NULL,
90 s->protocol_whitelist, s->protocol_blacklist, NULL);
91 if (ret)
92 goto fail;
93
94 while (1) {
95 int addr_type, auth_len;
96 int pos;
97
98 ret = ffurl_read(sap->ann_fd, recvbuf, sizeof(recvbuf) - 1);
99 if (ret == AVERROR(EAGAIN))
100 continue;
101 if (ret < 0)
102 goto fail;
103 recvbuf[ret] = '\0'; /* Null terminate for easier parsing */
104 if (ret < 8) {
105 av_log(s, AV_LOG_WARNING, "Received too short packet\n");
106 continue;
107 }
108
109 if ((recvbuf[0] & 0xe0) != 0x20) {
110 av_log(s, AV_LOG_WARNING, "Unsupported SAP version packet "
111 "received\n");
112 continue;
113 }
114
115 if (recvbuf[0] & 0x04) {
116 av_log(s, AV_LOG_WARNING, "Received stream deletion "
117 "announcement\n");
118 continue;
119 }
120 addr_type = recvbuf[0] & 0x10;
121 auth_len = recvbuf[1];
122 sap->hash = AV_RB16(&recvbuf[2]);
123 pos = 4;
124 if (addr_type)
125 pos += 16; /* IPv6 */
126 else
127 pos += 4; /* IPv4 */
128 pos += auth_len * 4;
129 if (pos + 4 >= ret) {
130 av_log(s, AV_LOG_WARNING, "Received too short packet\n");
131 continue;
132 }
133#define MIME "application/sdp"
134 if (strcmp(&recvbuf[pos], MIME) == 0) {
135 pos += strlen(MIME) + 1;
136 } else if (strncmp(&recvbuf[pos], "v=0\r\n", 5) == 0) {
137 // Direct SDP without a mime type
138 } else {
139 av_log(s, AV_LOG_WARNING, "Unsupported mime type %s\n",
140 &recvbuf[pos]);
141 continue;
142 }
143
144 sap->sdp = av_strdup(&recvbuf[pos]);
145 if (!sap->sdp) {
146 ret = AVERROR(ENOMEM);
147 goto fail;
148 }
149 break;
150 }
151
152 av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", sap->sdp);
153 ffio_init_read_context(&sap->sdp_pb, sap->sdp, strlen(sap->sdp));
154
155 infmt = av_find_input_format("sdp");
156 if (!infmt)
157 goto fail;
159 if (!sap->sdp_ctx) {
160 ret = AVERROR(ENOMEM);
161 goto fail;
162 }
163 sap->sdp_ctx->max_delay = s->max_delay;
164 sap->sdp_ctx->pb = &sap->sdp_pb.pub;
165 sap->sdp_ctx->interrupt_callback = s->interrupt_callback;
166
167 if ((ret = ff_copy_whiteblacklists(sap->sdp_ctx, s)) < 0)
168 goto fail;
169
170 ret = avformat_open_input(&sap->sdp_ctx, "temp.sdp", infmt, NULL);
171 if (ret < 0)
172 goto fail;
174 s->ctx_flags |= AVFMTCTX_NOHEADER;
175 for (i = 0; i < sap->sdp_ctx->nb_streams; i++) {
177 if (!st) {
178 ret = AVERROR(ENOMEM);
179 goto fail;
180 }
181 st->id = i;
183 if (ret < 0)
184 goto fail;
185 st->time_base = sap->sdp_ctx->streams[i]->time_base;
186 }
187
188 return 0;
189
190fail:
192 return ret;
193}
194
196{
197 struct SAPState *sap = s->priv_data;
198 int fd = ffurl_get_file_handle(sap->ann_fd);
199 int n, ret;
200 struct pollfd p = {fd, POLLIN, 0};
201 uint8_t recvbuf[RTP_MAX_PACKET_LENGTH];
202
203 if (fd < 0)
204 return fd;
205
206 if (sap->eof)
207 return AVERROR_EOF;
208
209 while (1) {
210 n = poll(&p, 1, 0);
211 if (n <= 0 || !(p.revents & POLLIN))
212 break;
213 ret = ffurl_read(sap->ann_fd, recvbuf, sizeof(recvbuf));
214 if (ret >= 8) {
215 uint16_t hash = AV_RB16(&recvbuf[2]);
216 /* Should ideally check the source IP address, too */
217 if (recvbuf[0] & 0x04 && hash == sap->hash) {
218 /* Stream deletion */
219 sap->eof = 1;
220 return AVERROR_EOF;
221 }
222 }
223 }
224 ret = av_read_frame(sap->sdp_ctx, pkt);
225 if (ret < 0)
226 return ret;
227 if (s->ctx_flags & AVFMTCTX_NOHEADER) {
228 while (sap->sdp_ctx->nb_streams > s->nb_streams) {
229 int i = s->nb_streams;
231 if (!st) {
232 return AVERROR(ENOMEM);
233 }
234 st->id = i;
236 st->time_base = sap->sdp_ctx->streams[i]->time_base;
237 }
238 }
239 return ret;
240}
241
243 .p.name = "sap",
244 .p.long_name = NULL_IF_CONFIG_SMALL("SAP input"),
245 .p.flags = AVFMT_NOFILE,
246 .priv_data_size = sizeof(struct SAPState),
248 .read_header = sap_read_header,
249 .read_packet = sap_fetch_packet,
250 .read_close = sap_read_close,
251};
const FFInputFormat ff_sap_demuxer
Definition sapdec.c:242
static uint8_t hash[HASH_SIZE]
int ff_copy_whiteblacklists(AVFormatContext *dst, const AVFormatContext *src)
Copies the whilelists from one context to the other.
Definition avformat.c:879
Main libavformat public API header.
#define AVPROBE_SCORE_MAX
maximum score
Definition avformat.h:483
#define AVFMTCTX_NOHEADER
signal that no header is present (streams are added dynamically)
Definition avformat.h:1284
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition avformat.h:488
int ffurl_open_whitelist(URLContext **puc, const char *filename, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options, const char *whitelist, const char *blacklist, URLContext *parent)
Create an URLContext for accessing to the resource indicated by url, and open it.
Definition avio.c:461
int ffurl_closep(URLContext **hh)
Close the resource accessed by the URLContext h, and free the memory used by it.
Definition avio.c:656
int ffurl_get_file_handle(URLContext *h)
Return the file descriptor associated with this URL.
Definition avio.c:882
#define AVIO_FLAG_READ
read-only
Definition avio.h:617
void ffio_init_read_context(FFIOContext *s, const uint8_t *buffer, int buffer_size)
Wrap a buffer in an AVIOContext for reading.
Definition aviobuf.c:99
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
static int read_probe(const AVProbeData *p)
Definition cdg.c:30
int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
Definition codec_par.c:107
#define NULL
Definition coverity.c:32
static AVPacket * pkt
#define fail
Definition test.h:479
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
Definition options.c:165
const AVInputFormat * av_find_input_format(const char *short_name)
Find AVInputFormat based on the short name of the input format.
Definition format.c:146
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
Return the next frame of a stream.
Definition demux.c:1588
int avformat_open_input(AVFormatContext **ps, const char *url, const AVInputFormat *fmt, AVDictionary **options)
Open an input stream and read the header.
Definition demux.c:231
void avformat_close_input(AVFormatContext **s)
Close an opened input AVFormatContext.
Definition demux.c:377
void av_url_split(char *proto, int proto_size, char *authorization, int authorization_size, char *hostname, int hostname_size, int *port_ptr, char *path, int path_size, const char *url)
Split a URL string into components.
Definition utils.c:361
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_VERBOSE
Detailed information.
Definition log.h:226
int av_strstart(const char *str, const char *pfx, const char **ptr)
Return non-zero if pfx is a prefix of str.
Definition avstring.c:36
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
Definition avstring.c:85
#define AV_RB16(p)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
Memory handling functions.
int ff_network_init(void)
Initialize the network subsystem.
Definition network.c:63
void ff_network_close(void)
Definition network.c:121
#define av_strdup(s)
Definition ops_static.c:55
miscellaneous OS support macros and functions.
#define RTP_MAX_PACKET_LENGTH
Definition rtpdec.h:37
static int sap_probe(const AVProbeData *p)
Definition sapdec.c:46
static int sap_read_header(AVFormatContext *s)
Definition sapdec.c:64
#define MIME
static int sap_read_close(AVFormatContext *s)
Definition sapdec.c:53
static int sap_fetch_packet(AVFormatContext *s, AVPacket *pkt)
Definition sapdec.c:195
unsigned int pos
Definition spdifenc.c:431
Format I/O context.
Definition avformat.h:1333
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition avformat.h:1389
AVIOContext * pb
I/O context.
Definition avformat.h:1375
int ctx_flags
Flags signalling stream properties.
Definition avformat.h:1382
AVIOInterruptCB interrupt_callback
Custom interrupt callbacks for the I/O layer.
Definition avformat.h:1618
AVStream ** streams
A list of all streams in the file.
Definition avformat.h:1401
This structure stores compressed data.
Definition packet.h:580
This structure contains the data a format has to probe a file.
Definition avformat.h:471
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
int id
Format-specific stream ID.
Definition avformat.h:778
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition avformat.h:805
AVIOContext pub
URLContext * ann_fd
Definition sapdec.c:38
int eof
Definition sapdec.c:43
uint16_t hash
Definition sapdec.c:41
AVFormatContext * sdp_ctx
Definition sapdec.c:39
FFIOContext sdp_pb
Definition sapdec.c:40
char * sdp
Definition sapdec.c:42
#define av_freep(p)
#define av_log(a,...)
int ff_url_join(char *str, int size, const char *proto, const char *authorization, const char *hostname, int port, const char *fmt,...)
Definition url.c:40
unbuffered private I/O API
static int ffurl_read(URLContext *h, uint8_t *buf, int size)
Read up to size bytes from the resource accessed by h, and store the read bytes in buf.
Definition url.h:183