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/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 #include "rtpdec.h"
31 #if HAVE_POLL_H
32 #include <poll.h>
33 #endif
34 
35 struct SAPState {
39  uint16_t hash;
40  char *sdp;
41  int eof;
42 };
43 
44 static int sap_probe(const AVProbeData *p)
45 {
46  if (av_strstart(p->filename, "sap:", NULL))
47  return AVPROBE_SCORE_MAX;
48  return 0;
49 }
50 
52 {
53  struct SAPState *sap = s->priv_data;
54  if (sap->sdp_ctx)
56  ffurl_closep(&sap->ann_fd);
57  av_freep(&sap->sdp);
59  return 0;
60 }
61 
63 {
64  struct SAPState *sap = s->priv_data;
65  char host[1024], path[1024], url[1024];
66  uint8_t recvbuf[RTP_MAX_PACKET_LENGTH];
67  const AVInputFormat *infmt;
68  int port;
69  int ret, i;
70 
71  if (!ff_network_init())
72  return AVERROR(EIO);
73 
74  av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &port,
75  path, sizeof(path), s->url);
76  if (port < 0)
77  port = 9875;
78 
79  if (!host[0]) {
80  /* Listen for announcements on sap.mcast.net if no host was specified */
81  av_strlcpy(host, "224.2.127.254", sizeof(host));
82  }
83 
84  ff_url_join(url, sizeof(url), "udp", NULL, host, port, "?localport=%d",
85  port);
87  &s->interrupt_callback, NULL,
88  s->protocol_whitelist, s->protocol_blacklist, NULL);
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  if (!sap->sdp) {
144  ret = AVERROR(ENOMEM);
145  goto fail;
146  }
147  break;
148  }
149 
150  av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", sap->sdp);
151  ffio_init_read_context(&sap->sdp_pb, sap->sdp, strlen(sap->sdp));
152 
153  infmt = av_find_input_format("sdp");
154  if (!infmt)
155  goto fail;
157  if (!sap->sdp_ctx) {
158  ret = AVERROR(ENOMEM);
159  goto fail;
160  }
161  sap->sdp_ctx->max_delay = s->max_delay;
162  sap->sdp_ctx->pb = &sap->sdp_pb.pub;
163  sap->sdp_ctx->interrupt_callback = s->interrupt_callback;
164 
165  if ((ret = ff_copy_whiteblacklists(sap->sdp_ctx, s)) < 0)
166  goto fail;
167 
168  ret = avformat_open_input(&sap->sdp_ctx, "temp.sdp", infmt, NULL);
169  if (ret < 0)
170  goto fail;
171  if (sap->sdp_ctx->ctx_flags & AVFMTCTX_NOHEADER)
172  s->ctx_flags |= AVFMTCTX_NOHEADER;
173  for (i = 0; i < sap->sdp_ctx->nb_streams; i++) {
175  if (!st) {
176  ret = AVERROR(ENOMEM);
177  goto fail;
178  }
179  st->id = i;
181  st->time_base = sap->sdp_ctx->streams[i]->time_base;
182  }
183 
184  return 0;
185 
186 fail:
187  sap_read_close(s);
188  return ret;
189 }
190 
192 {
193  struct SAPState *sap = s->priv_data;
194  int fd = ffurl_get_file_handle(sap->ann_fd);
195  int n, ret;
196  struct pollfd p = {fd, POLLIN, 0};
197  uint8_t recvbuf[RTP_MAX_PACKET_LENGTH];
198 
199  if (sap->eof)
200  return AVERROR_EOF;
201 
202  while (1) {
203  n = poll(&p, 1, 0);
204  if (n <= 0 || !(p.revents & POLLIN))
205  break;
206  ret = ffurl_read(sap->ann_fd, recvbuf, sizeof(recvbuf));
207  if (ret >= 8) {
208  uint16_t hash = AV_RB16(&recvbuf[2]);
209  /* Should ideally check the source IP address, too */
210  if (recvbuf[0] & 0x04 && hash == sap->hash) {
211  /* Stream deletion */
212  sap->eof = 1;
213  return AVERROR_EOF;
214  }
215  }
216  }
217  ret = av_read_frame(sap->sdp_ctx, pkt);
218  if (ret < 0)
219  return ret;
220  if (s->ctx_flags & AVFMTCTX_NOHEADER) {
221  while (sap->sdp_ctx->nb_streams > s->nb_streams) {
222  int i = s->nb_streams;
224  if (!st) {
225  return AVERROR(ENOMEM);
226  }
227  st->id = i;
229  st->time_base = sap->sdp_ctx->streams[i]->time_base;
230  }
231  }
232  return ret;
233 }
234 
236  .name = "sap",
237  .long_name = NULL_IF_CONFIG_SMALL("SAP input"),
238  .priv_data_size = sizeof(struct SAPState),
240  .read_header = sap_read_header,
241  .read_packet = sap_fetch_packet,
242  .read_close = sap_read_close,
243  .flags = AVFMT_NOFILE,
244 };
SAPState::hash
uint16_t hash
Definition: sapdec.c:39
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
sap_read_header
static int sap_read_header(AVFormatContext *s)
Definition: sapdec.c:62
AVERROR
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
SAPState::ann_fd
URLContext * ann_fd
Definition: sapdec.c:36
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
AVFormatContext::streams
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1431
SAPState
Definition: sapdec.c:35
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:196
MIME
#define MIME
ffio_init_read_context
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:134
ff_network_close
void ff_network_close(void)
Definition: network.c:116
av_read_frame
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
Return the next frame of a stream.
Definition: demux.c:1558
hash
uint8_t hash[HASH_SIZE]
Definition: movenc.c:57
sap_read_close
static int sap_read_close(AVFormatContext *s)
Definition: sapdec.c:51
os_support.h
FFIOContext
Definition: avio_internal.h:28
ff_network_init
int ff_network_init(void)
Definition: network.c:58
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:464
avformat_close_input
void avformat_close_input(AVFormatContext **s)
Close an opened input AVFormatContext.
Definition: demux.c:374
AVFormatContext::interrupt_callback
AVIOInterruptCB interrupt_callback
Custom interrupt callbacks for the I/O layer.
Definition: avformat.h:1629
fail
#define fail()
Definition: checkasm.h:179
ff_sap_demuxer
const AVInputFormat ff_sap_demuxer
Definition: sapdec.c:235
pkt
AVPacket * pkt
Definition: movenc.c:59
AVInputFormat
Definition: avformat.h:549
AVFormatContext::ctx_flags
int ctx_flags
Flags signalling stream properties.
Definition: avformat.h:1412
avformat_open_input
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:226
ffurl_open_whitelist
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:300
SAPState::sdp
char * sdp
Definition: sapdec.c:40
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
SAPState::eof
int eof
Definition: sapdec.c:41
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:554
AVProbeData::filename
const char * filename
Definition: avformat.h:453
ff_url_join
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
SAPState::sdp_pb
FFIOContext sdp_pb
Definition: sapdec.c:38
AVFormatContext
Format I/O context.
Definition: avformat.h:1363
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:864
AVStream::time_base
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avformat.h:880
NULL
#define NULL
Definition: coverity.c:32
AVFMTCTX_NOHEADER
#define AVFMTCTX_NOHEADER
signal that no header is present (streams are added dynamically)
Definition: avformat.h:1314
ff_copy_whiteblacklists
int ff_copy_whiteblacklists(AVFormatContext *dst, const AVFormatContext *src)
Copies the whilelists from one context to the other.
Definition: avformat.c:888
AVFormatContext::pb
AVIOContext * pb
I/O context.
Definition: avformat.h:1405
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:452
rtpdec.h
AVFormatContext::nb_streams
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1419
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:106
avformat_alloc_context
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
Definition: options.c:167
FFIOContext::pub
AVIOContext pub
Definition: avio_internal.h:29
AVFMT_NOFILE
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:469
SAPState::sdp_ctx
AVFormatContext * sdp_ctx
Definition: sapdec.c:37
av_strstart
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
URLContext
Definition: url.h:37
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
avio_internal.h
av_url_split
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:358
url.h
AVFormatContext::max_delay
int max_delay
Definition: avformat.h:1475
ffurl_closep
int ffurl_closep(URLContext **hh)
Close the resource accessed by the URLContext h, and free the memory used by it.
Definition: avio.c:439
sap_probe
static int sap_probe(const AVProbeData *p)
Definition: sapdec.c:44
AVStream::id
int id
Format-specific stream ID.
Definition: avformat.h:853
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:841
pos
unsigned int pos
Definition: spdifenc.c:413
avformat.h
network.h
RTP_MAX_PACKET_LENGTH
#define RTP_MAX_PACKET_LENGTH
Definition: rtpdec.h:37
sap_fetch_packet
static int sap_fetch_packet(AVFormatContext *s, AVPacket *pkt)
Definition: sapdec.c:191
av_find_input_format
const AVInputFormat * av_find_input_format(const char *short_name)
Find AVInputFormat based on the short name of the input format.
Definition: format.c:144
AVIO_FLAG_READ
#define AVIO_FLAG_READ
read-only
Definition: avio.h:636
av_strdup
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:270
read_probe
static int read_probe(const AVProbeData *p)
Definition: cdg.c:29
AVPacket
This structure stores compressed data.
Definition: packet.h:499
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
av_strlcpy
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
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
avstring.h
ffurl_get_file_handle
int ffurl_get_file_handle(URLContext *h)
Return the file descriptor associated with this URL.
Definition: avio.c:627
avcodec_parameters_copy
int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
Copy the contents of src to dst.
Definition: codec_par.c:106
AV_RB16
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:98
ffurl_read
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