FFmpeg
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 "libavutil/avassert.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 {
40  uint16_t hash;
41  char *sdp;
42  int eof;
43 };
44 
45 static int sap_probe(const AVProbeData *p)
46 {
47  if (av_strstart(p->filename, "sap:", NULL))
48  return AVPROBE_SCORE_MAX;
49  return 0;
50 }
51 
53 {
54  struct SAPState *sap = s->priv_data;
55  if (sap->sdp_ctx)
57  ffurl_closep(&sap->ann_fd);
58  av_freep(&sap->sdp);
60  return 0;
61 }
62 
64 {
65  struct SAPState *sap = s->priv_data;
66  char host[1024], path[1024], url[1024];
68  int port;
69  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,
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_context(&sap->sdp_pb, sap->sdp, strlen(sap->sdp), 0, NULL, NULL,
153  NULL, NULL);
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;
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;
173  if (sap->sdp_ctx->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  st->time_base = sap->sdp_ctx->streams[i]->time_base;
184  }
185 
186  return 0;
187 
188 fail:
189  sap_read_close(s);
190  return ret;
191 }
192 
194 {
195  struct SAPState *sap = s->priv_data;
196  int fd = ffurl_get_file_handle(sap->ann_fd);
197  int n, ret;
198  struct pollfd p = {fd, POLLIN, 0};
200 
201  if (sap->eof)
202  return AVERROR_EOF;
203 
204  while (1) {
205  n = poll(&p, 1, 0);
206  if (n <= 0 || !(p.revents & POLLIN))
207  break;
208  ret = ffurl_read(sap->ann_fd, recvbuf, sizeof(recvbuf));
209  if (ret >= 8) {
210  uint16_t hash = AV_RB16(&recvbuf[2]);
211  /* Should ideally check the source IP address, too */
212  if (recvbuf[0] & 0x04 && hash == sap->hash) {
213  /* Stream deletion */
214  sap->eof = 1;
215  return AVERROR_EOF;
216  }
217  }
218  }
219  ret = av_read_frame(sap->sdp_ctx, pkt);
220  if (ret < 0)
221  return ret;
222  if (s->ctx_flags & AVFMTCTX_NOHEADER) {
223  while (sap->sdp_ctx->nb_streams > s->nb_streams) {
224  int i = s->nb_streams;
226  if (!st) {
227  return AVERROR(ENOMEM);
228  }
229  st->id = i;
231  st->time_base = sap->sdp_ctx->streams[i]->time_base;
232  }
233  }
234  return ret;
235 }
236 
238  .name = "sap",
239  .long_name = NULL_IF_CONFIG_SMALL("SAP input"),
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  .flags = AVFMT_NOFILE,
246 };
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:4723
#define NULL
Definition: coverity.c:32
static int sap_read_close(AVFormatContext *s)
Definition: sapdec.c:52
Bytestream IO Context.
Definition: avio.h:161
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:310
#define RTP_MAX_PACKET_LENGTH
Definition: rtpdec.h:36
AVIOInterruptCB interrupt_callback
Custom interrupt callbacks for the I/O layer.
Definition: avformat.h:1633
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
const char * filename
Definition: avformat.h:442
int ff_copy_whiteblacklists(AVFormatContext *dst, const AVFormatContext *src)
Copies the whilelists from one context to the other.
Definition: utils.c:160
AVInputFormat ff_sap_demuxer
Definition: sapdec.c:237
static int sap_probe(const AVProbeData *p)
Definition: sapdec.c:45
#define AVIO_FLAG_READ
read-only
Definition: avio.h:674
void ff_network_close(void)
Definition: network.c:116
AVIOContext sdp_pb
Definition: sapdec.c:39
int avformat_open_input(AVFormatContext **ps, const char *url, ff_const59 AVInputFormat *fmt, AVDictionary **options)
Open an input stream and read the header.
Definition: utils.c:500
static AVPacket pkt
int ctx_flags
Flags signalling stream properties.
Definition: avformat.h:1404
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
Definition: bytestream.h:91
Format I/O context.
Definition: avformat.h:1355
uint8_t
int ff_network_init(void)
Definition: network.c:58
#define AVFMTCTX_NOHEADER
signal that no header is present (streams are added dynamically)
Definition: avformat.h:1299
miscellaneous OS support macros and functions.
int id
Format-specific stream ID.
Definition: avformat.h:887
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4450
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1423
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
Definition: options.c:211
char * protocol_whitelist
&#39;,&#39; separated list of allowed protocols.
Definition: avformat.h:1915
#define AVERROR_EOF
End of file.
Definition: error.h:55
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:210
#define av_log(a,...)
uint16_t hash
Definition: sapdec.c:40
static int sap_fetch_packet(AVFormatContext *s, AVPacket *pkt)
Definition: sapdec.c:193
#define ff_const59
The ff_const59 define is not part of the public API and will be removed without further warning...
Definition: avformat.h:544
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:153
char * url
input or output URL.
Definition: avformat.h:1451
unsigned int pos
Definition: spdifenc.c:410
ff_const59 AVInputFormat * av_find_input_format(const char *short_name)
Find AVInputFormat based on the short name of the input format.
Definition: format.c:118
simple assert() macros that are a bit more flexible than ISO C assert().
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:83
#define fail()
Definition: checkasm.h:123
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1411
URLContext * ann_fd
Definition: sapdec.c:37
AVFormatContext * sdp_ctx
Definition: sapdec.c:38
#define s(width, name)
Definition: cbs_vp9.c:257
int ffurl_get_file_handle(URLContext *h)
Return the file descriptor associated with this URL.
Definition: avio.c:628
char * sdp
Definition: sapdec.c:41
int ffurl_closep(URLContext **hh)
Close the resource accessed by the URLContext h, and free the memory used by it.
Definition: avio.c:446
Stream structure.
Definition: avformat.h:880
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:38
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:253
AVIOContext * pb
I/O context.
Definition: avformat.h:1397
Definition: url.h:38
This structure contains the data a format has to probe a file.
Definition: avformat.h:441
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
Return the next frame of a stream.
Definition: utils.c:1711
static int sap_read_header(AVFormatContext *s)
Definition: sapdec.c:63
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:453
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:34
Main libavformat public API header.
#define MIME
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:458
int ffio_init_context(AVIOContext *s, unsigned char *buffer, int buffer_size, int write_flag, void *opaque, int(*read_packet)(void *opaque, uint8_t *buf, int buf_size), int(*write_packet)(void *opaque, uint8_t *buf, int buf_size), int64_t(*seek)(void *opaque, int64_t offset, int whence))
Definition: aviobuf.c:88
void avformat_close_input(AVFormatContext **s)
Close an opened input AVFormatContext.
Definition: utils.c:4422
void * priv_data
Format private data.
Definition: avformat.h:1383
int eof
Definition: sapdec.c:42
char * protocol_blacklist
&#39;,&#39; separated list of disallowed protocols.
Definition: avformat.h:1950
#define av_freep(p)
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:652
unbuffered private I/O API
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1027
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:909
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Frame references ownership and permissions
int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
Copy the contents of src to dst.
Definition: utils.c:2091
This structure stores compressed data.
Definition: packet.h:340
int ffurl_read(URLContext *h, unsigned char *buf, int size)
Read up to size bytes from the resource accessed by h, and store the read bytes in buf...
Definition: avio.c:409
int i
Definition: input.c:407