Libav
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 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 #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 
44 };
45 
46 static int sap_probe(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  if (sap->ann_fd)
59  ffurl_close(sap->ann_fd);
60  av_freep(&sap->protocols);
61  av_freep(&sap->sdp);
63  return 0;
64 }
65 
67 {
68  struct SAPState *sap = s->priv_data;
69  char host[1024], path[1024], url[1024];
71  int port;
72  int ret, i;
73  AVInputFormat* infmt;
74 
75  if (!ff_network_init())
76  return AVERROR(EIO);
77 
78  av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &port,
79  path, sizeof(path), s->filename);
80  if (port < 0)
81  port = 9875;
82 
83  if (!host[0]) {
84  /* Listen for announcements on sap.mcast.net if no host was specified */
85  av_strlcpy(host, "224.2.127.254", sizeof(host));
86  }
87 
90  if (!sap->protocols) {
91  ret = AVERROR(ENOMEM);
92  goto fail;
93  }
94 
95  ff_url_join(url, sizeof(url), "udp", NULL, host, port, "?localport=%d",
96  port);
97  ret = ffurl_open(&sap->ann_fd, url, AVIO_FLAG_READ,
99  if (ret)
100  goto fail;
101 
102  while (1) {
103  int addr_type, auth_len;
104  int pos;
105 
106  ret = ffurl_read(sap->ann_fd, recvbuf, sizeof(recvbuf) - 1);
107  if (ret == AVERROR(EAGAIN))
108  continue;
109  if (ret < 0)
110  goto fail;
111  recvbuf[ret] = '\0'; /* Null terminate for easier parsing */
112  if (ret < 8) {
113  av_log(s, AV_LOG_WARNING, "Received too short packet\n");
114  continue;
115  }
116 
117  if ((recvbuf[0] & 0xe0) != 0x20) {
118  av_log(s, AV_LOG_WARNING, "Unsupported SAP version packet "
119  "received\n");
120  continue;
121  }
122 
123  if (recvbuf[0] & 0x04) {
124  av_log(s, AV_LOG_WARNING, "Received stream deletion "
125  "announcement\n");
126  continue;
127  }
128  addr_type = recvbuf[0] & 0x10;
129  auth_len = recvbuf[1];
130  sap->hash = AV_RB16(&recvbuf[2]);
131  pos = 4;
132  if (addr_type)
133  pos += 16; /* IPv6 */
134  else
135  pos += 4; /* IPv4 */
136  pos += auth_len * 4;
137  if (pos + 4 >= ret) {
138  av_log(s, AV_LOG_WARNING, "Received too short packet\n");
139  continue;
140  }
141 #define MIME "application/sdp"
142  if (strcmp(&recvbuf[pos], MIME) == 0) {
143  pos += strlen(MIME) + 1;
144  } else if (strncmp(&recvbuf[pos], "v=0\r\n", 5) == 0) {
145  // Direct SDP without a mime type
146  } else {
147  av_log(s, AV_LOG_WARNING, "Unsupported mime type %s\n",
148  &recvbuf[pos]);
149  continue;
150  }
151 
152  sap->sdp = av_strdup(&recvbuf[pos]);
153  break;
154  }
155 
156  av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", sap->sdp);
157  ffio_init_context(&sap->sdp_pb, sap->sdp, strlen(sap->sdp), 0, NULL, NULL,
158  NULL, NULL);
159 
160  infmt = av_find_input_format("sdp");
161  if (!infmt)
162  goto fail;
164  if (!sap->sdp_ctx) {
165  ret = AVERROR(ENOMEM);
166  goto fail;
167  }
168  sap->sdp_ctx->max_delay = s->max_delay;
169  sap->sdp_ctx->pb = &sap->sdp_pb;
171  ret = avformat_open_input(&sap->sdp_ctx, "temp.sdp", infmt, NULL);
172  if (ret < 0)
173  goto fail;
174  if (sap->sdp_ctx->ctx_flags & AVFMTCTX_NOHEADER)
176  for (i = 0; i < sap->sdp_ctx->nb_streams; i++) {
178  if (!st) {
179  ret = AVERROR(ENOMEM);
180  goto fail;
181  }
182  st->id = i;
184  st->time_base = sap->sdp_ctx->streams[i]->time_base;
185  }
186 
187  return 0;
188 
189 fail:
190  sap_read_close(s);
191  return ret;
192 }
193 
195 {
196  struct SAPState *sap = s->priv_data;
197  int fd = ffurl_get_file_handle(sap->ann_fd);
198  int n, ret;
199  struct pollfd p = {fd, POLLIN, 0};
201 
202  if (sap->eof)
203  return AVERROR_EOF;
204 
205  while (1) {
206  n = poll(&p, 1, 0);
207  if (n <= 0 || !(p.revents & POLLIN))
208  break;
209  ret = ffurl_read(sap->ann_fd, recvbuf, sizeof(recvbuf));
210  if (ret >= 8) {
211  uint16_t hash = AV_RB16(&recvbuf[2]);
212  /* Should ideally check the source IP address, too */
213  if (recvbuf[0] & 0x04 && hash == sap->hash) {
214  /* Stream deletion */
215  sap->eof = 1;
216  return AVERROR_EOF;
217  }
218  }
219  }
220  ret = av_read_frame(sap->sdp_ctx, pkt);
221  if (ret < 0)
222  return ret;
223  if (s->ctx_flags & AVFMTCTX_NOHEADER) {
224  while (sap->sdp_ctx->nb_streams > s->nb_streams) {
225  int i = s->nb_streams;
227  if (!st) {
228  av_packet_unref(pkt);
229  return AVERROR(ENOMEM);
230  }
231  st->id = i;
233  st->time_base = sap->sdp_ctx->streams[i]->time_base;
234  }
235  }
236  return ret;
237 }
238 
240  .name = "sap",
241  .long_name = NULL_IF_CONFIG_SMALL("SAP input"),
242  .priv_data_size = sizeof(struct SAPState),
243  .read_probe = sap_probe,
244  .read_header = sap_read_header,
245  .read_packet = sap_fetch_packet,
246  .read_close = sap_read_close,
247  .flags = AVFMT_NOFILE,
248 };
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:2870
static int sap_read_close(AVFormatContext *s)
Definition: sapdec.c:53
Bytestream IO Context.
Definition: avio.h:104
#define RTP_MAX_PACKET_LENGTH
Definition: rtpdec.h:36
AVIOInterruptCB interrupt_callback
Custom interrupt callbacks for the I/O layer.
Definition: avformat.h:1181
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:130
const char * filename
Definition: avformat.h:399
int avformat_open_input(AVFormatContext **ps, const char *filename, AVInputFormat *fmt, AVDictionary **options)
Open an input stream and read the header.
Definition: utils.c:284
AVInputFormat ff_sap_demuxer
Definition: sapdec.c:239
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
#define AVIO_FLAG_READ
read-only
Definition: avio.h:368
void ff_network_close(void)
Definition: network.c:77
AVIOContext sdp_pb
Definition: sapdec.c:38
int ctx_flags
Flags signalling stream properties.
Definition: avformat.h:989
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:202
Format I/O context.
Definition: avformat.h:940
uint8_t
int ff_network_init(void)
Definition: network.c:50
#define AVFMTCTX_NOHEADER
signal that no header is present (streams are added dynamically)
Definition: avformat.h:919
miscellaneous OS support macros and functions.
int id
Format-specific stream ID.
Definition: avformat.h:712
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2648
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1008
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
Definition: options.c:136
char * protocol_whitelist
A comma-separated list of protocol names that can be used internally by libavformat.
Definition: avformat.h:1294
#define AVERROR_EOF
End of file.
Definition: error.h:51
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:140
int ffurl_open(URLContext **puc, const char *filename, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options, const URLProtocol **protocols, URLContext *parent)
Create an URLContext for accessing to the resource indicated by url, and open it. ...
Definition: avio.c:175
uint16_t hash
Definition: sapdec.c:39
static int sap_fetch_packet(AVFormatContext *s, AVPacket *pkt)
Definition: sapdec.c:194
int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
Copy the contents of src to dst.
Definition: utils.c:2819
#define AV_RB16
Definition: intreadwrite.h:53
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:148
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:81
#define fail()
Definition: checkasm.h:80
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:996
AVInputFormat * av_find_input_format(const char *short_name)
Find AVInputFormat based on the short name of the input format.
Definition: format.c:162
char filename[1024]
input or output filename
Definition: avformat.h:1016
URLContext * ann_fd
Definition: sapdec.c:36
AVFormatContext * sdp_ctx
Definition: sapdec.c:37
int ffurl_get_file_handle(URLContext *h)
Return the file descriptor associated with this URL.
Definition: avio.c:345
char * sdp
Definition: sapdec.c:40
Stream structure.
Definition: avformat.h:705
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:36
NULL
Definition: eval.c:55
const URLProtocol ** protocols
Definition: sapdec.c:43
char * av_strdup(const char *s)
Duplicate the string s.
Definition: mem.c:219
AVIOContext * pb
I/O context.
Definition: avformat.h:982
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:347
Definition: url.h:38
This structure contains the data a format has to probe a file.
Definition: avformat.h:398
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
Return the next frame of a stream.
Definition: utils.c:1021
static int sap_read_header(AVFormatContext *s)
Definition: sapdec.c:66
static int sap_probe(AVProbeData *p)
Definition: sapdec.c:46
int ffurl_close(URLContext *h)
Close the resource accessed by the URLContext h, and free the memory used by it.
Definition: avio.c:280
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:407
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:32
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:412
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:109
void avformat_close_input(AVFormatContext **s)
Close an opened input AVFormatContext.
Definition: utils.c:2626
void * priv_data
Format private data.
Definition: avformat.h:968
int eof
Definition: sapdec.c:41
char * protocol_blacklist
A comma-separated list of protocol names that will not be used internally by libavformat.
Definition: avformat.h:1285
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:529
unbuffered private I/O API
AVCodecParameters * codecpar
Definition: avformat.h:831
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:742
const URLProtocol ** ffurl_get_protocols(const char *whitelist, const char *blacklist)
Construct a list of protocols matching a given whitelist and/or blacklist.
Definition: protocols.c:98
This structure stores compressed data.
Definition: avcodec.h:1323
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:243