sapenc.c
Go to the documentation of this file.
1 /*
2  * Session Announcement Protocol (RFC 2974) muxer
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/parseutils.h"
24 #include "libavutil/random_seed.h"
25 #include "libavutil/avstring.h"
26 #include "libavutil/intreadwrite.h"
27 #include "internal.h"
28 #include "network.h"
29 #include "os_support.h"
30 #include "rtpenc_chain.h"
31 #include "url.h"
32 
33 struct SAPState {
34  uint8_t *ann;
35  int ann_size;
37  int64_t last_time;
38 };
39 
41 {
42  struct SAPState *sap = s->priv_data;
43  int i;
44 
45  for (i = 0; i < s->nb_streams; i++) {
46  AVFormatContext *rtpctx = s->streams[i]->priv_data;
47  if (!rtpctx)
48  continue;
49  av_write_trailer(rtpctx);
50  avio_close(rtpctx->pb);
51  avformat_free_context(rtpctx);
52  s->streams[i]->priv_data = NULL;
53  }
54 
55  if (sap->last_time && sap->ann && sap->ann_fd) {
56  sap->ann[0] |= 4; /* Session deletion*/
57  ffurl_write(sap->ann_fd, sap->ann, sap->ann_size);
58  }
59 
60  av_freep(&sap->ann);
61  if (sap->ann_fd)
62  ffurl_close(sap->ann_fd);
64  return 0;
65 }
66 
68 {
69  struct SAPState *sap = s->priv_data;
70  char host[1024], path[1024], url[1024], announce_addr[50] = "";
71  char *option_list;
72  int port = 9875, base_port = 5004, i, pos = 0, same_port = 0, ttl = 255;
73  AVFormatContext **contexts = NULL;
74  int ret = 0;
75  struct sockaddr_storage localaddr;
76  socklen_t addrlen = sizeof(localaddr);
77  int udp_fd;
78 
79  if (!ff_network_init())
80  return AVERROR(EIO);
81 
82  /* extract hostname and port */
83  av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &base_port,
84  path, sizeof(path), s->filename);
85  if (base_port < 0)
86  base_port = 5004;
87 
88  /* search for options */
89  option_list = strrchr(path, '?');
90  if (option_list) {
91  char buf[50];
92  if (av_find_info_tag(buf, sizeof(buf), "announce_port", option_list)) {
93  port = strtol(buf, NULL, 10);
94  }
95  if (av_find_info_tag(buf, sizeof(buf), "same_port", option_list)) {
96  same_port = strtol(buf, NULL, 10);
97  }
98  if (av_find_info_tag(buf, sizeof(buf), "ttl", option_list)) {
99  ttl = strtol(buf, NULL, 10);
100  }
101  if (av_find_info_tag(buf, sizeof(buf), "announce_addr", option_list)) {
102  av_strlcpy(announce_addr, buf, sizeof(announce_addr));
103  }
104  }
105 
106  if (!announce_addr[0]) {
107  struct addrinfo hints, *ai = NULL;
108  memset(&hints, 0, sizeof(hints));
109  hints.ai_family = AF_UNSPEC;
110  if (getaddrinfo(host, NULL, &hints, &ai)) {
111  av_log(s, AV_LOG_ERROR, "Unable to resolve %s\n", host);
112  ret = AVERROR(EIO);
113  goto fail;
114  }
115  if (ai->ai_family == AF_INET) {
116  /* Also known as sap.mcast.net */
117  av_strlcpy(announce_addr, "224.2.127.254", sizeof(announce_addr));
118 #if HAVE_STRUCT_SOCKADDR_IN6
119  } else if (ai->ai_family == AF_INET6) {
120  /* With IPv6, you can use the same destination in many different
121  * multicast subnets, to choose how far you want it routed.
122  * This one is intended to be routed globally. */
123  av_strlcpy(announce_addr, "ff0e::2:7ffe", sizeof(announce_addr));
124 #endif
125  } else {
126  freeaddrinfo(ai);
127  av_log(s, AV_LOG_ERROR, "Host %s resolved to unsupported "
128  "address family\n", host);
129  ret = AVERROR(EIO);
130  goto fail;
131  }
132  freeaddrinfo(ai);
133  }
134 
135  contexts = av_mallocz(sizeof(AVFormatContext*) * s->nb_streams);
136  if (!contexts) {
137  ret = AVERROR(ENOMEM);
138  goto fail;
139  }
140 
142  for (i = 0; i < s->nb_streams; i++) {
143  URLContext *fd;
144 
145  ff_url_join(url, sizeof(url), "rtp", NULL, host, base_port,
146  "?ttl=%d", ttl);
147  if (!same_port)
148  base_port += 2;
149  ret = ffurl_open(&fd, url, AVIO_FLAG_WRITE, &s->interrupt_callback, NULL);
150  if (ret) {
151  ret = AVERROR(EIO);
152  goto fail;
153  }
154  s->streams[i]->priv_data = contexts[i] =
155  ff_rtp_chain_mux_open(s, s->streams[i], fd, 0);
156  av_strlcpy(contexts[i]->filename, url, sizeof(contexts[i]->filename));
157  }
158 
159  ff_url_join(url, sizeof(url), "udp", NULL, announce_addr, port,
160  "?ttl=%d&connect=1", ttl);
161  ret = ffurl_open(&sap->ann_fd, url, AVIO_FLAG_WRITE,
162  &s->interrupt_callback, NULL);
163  if (ret) {
164  ret = AVERROR(EIO);
165  goto fail;
166  }
167 
168  udp_fd = ffurl_get_file_handle(sap->ann_fd);
169  if (getsockname(udp_fd, (struct sockaddr*) &localaddr, &addrlen)) {
170  ret = AVERROR(EIO);
171  goto fail;
172  }
173  if (localaddr.ss_family != AF_INET
175  && localaddr.ss_family != AF_INET6
176 #endif
177  ) {
178  av_log(s, AV_LOG_ERROR, "Unsupported protocol family\n");
179  ret = AVERROR(EIO);
180  goto fail;
181  }
182  sap->ann_size = 8192;
183  sap->ann = av_mallocz(sap->ann_size);
184  if (!sap->ann) {
185  ret = AVERROR(EIO);
186  goto fail;
187  }
188  sap->ann[pos] = (1 << 5);
189 #if HAVE_STRUCT_SOCKADDR_IN6
190  if (localaddr.ss_family == AF_INET6)
191  sap->ann[pos] |= 0x10;
192 #endif
193  pos++;
194  sap->ann[pos++] = 0; /* Authentication length */
195  AV_WB16(&sap->ann[pos], av_get_random_seed());
196  pos += 2;
197  if (localaddr.ss_family == AF_INET) {
198  memcpy(&sap->ann[pos], &((struct sockaddr_in*)&localaddr)->sin_addr,
199  sizeof(struct in_addr));
200  pos += sizeof(struct in_addr);
201 #if HAVE_STRUCT_SOCKADDR_IN6
202  } else {
203  memcpy(&sap->ann[pos], &((struct sockaddr_in6*)&localaddr)->sin6_addr,
204  sizeof(struct in6_addr));
205  pos += sizeof(struct in6_addr);
206 #endif
207  }
208 
209  av_strlcpy(&sap->ann[pos], "application/sdp", sap->ann_size - pos);
210  pos += strlen(&sap->ann[pos]) + 1;
211 
212  if (av_sdp_create(contexts, s->nb_streams, &sap->ann[pos],
213  sap->ann_size - pos)) {
214  ret = AVERROR_INVALIDDATA;
215  goto fail;
216  }
217  av_freep(&contexts);
218  av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", &sap->ann[pos]);
219  pos += strlen(&sap->ann[pos]);
220  sap->ann_size = pos;
221 
222  if (sap->ann_size > sap->ann_fd->max_packet_size) {
223  av_log(s, AV_LOG_ERROR, "Announcement too large to send in one "
224  "packet\n");
225  goto fail;
226  }
227 
228  return 0;
229 
230 fail:
231  av_free(contexts);
232  sap_write_close(s);
233  return ret;
234 }
235 
237 {
238  AVFormatContext *rtpctx;
239  struct SAPState *sap = s->priv_data;
240  int64_t now = av_gettime();
241 
242  if (!sap->last_time || now - sap->last_time > 5000000) {
243  int ret = ffurl_write(sap->ann_fd, sap->ann, sap->ann_size);
244  /* Don't abort even if we get "Destination unreachable" */
245  if (ret < 0 && ret != AVERROR(ECONNREFUSED))
246  return ret;
247  sap->last_time = now;
248  }
249  rtpctx = s->streams[pkt->stream_index]->priv_data;
250  return ff_write_chained(rtpctx, 0, pkt, s);
251 }
252 
254  .name = "sap",
255  .long_name = NULL_IF_CONFIG_SMALL("SAP output format"),
256  .priv_data_size = sizeof(struct SAPState),
257  .audio_codec = CODEC_ID_AAC,
258  .video_codec = CODEC_ID_MPEG4,
259  .write_header = sap_write_header,
260  .write_packet = sap_write_packet,
261  .write_trailer = sap_write_close,
262  .flags = AVFMT_NOFILE | AVFMT_GLOBALHEADER,
263 };
264 
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:3710
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:154
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
int64_t start_time_realtime
Start time of the stream in real world time, in microseconds since the unix epoch (00:00 1st January ...
Definition: avformat.h:1103
AVIOInterruptCB interrupt_callback
Custom interrupt callbacks for the I/O layer.
Definition: avformat.h:1127
int ffurl_write(URLContext *h, const unsigned char *buf, int size)
Write size bytes from buf to the resource accessed by h.
Definition: avio.c:355
int64_t last_time
Definition: sapenc.c:37
#define AVIO_FLAG_WRITE
write-only
Definition: avio.h:566
void ff_network_close(void)
Definition: network.c:151
void * priv_data
Definition: avformat.h:633
#define freeaddrinfo
Definition: network.h:151
AVOutputFormat ff_sap_muxer
Definition: sapenc.c:253
int ff_url_join(char *str, int size, const char *proto, const char *authorization, const char *hostname, int port, const char *fmt,...) av_printf_format(7
Assemble a URL string from components.
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:147
Format I/O context.
Definition: avformat.h:863
static int sap_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: sapenc.c:236
int ff_network_init(void)
Definition: network.c:124
miscellaneous OS support macros and functions.
uint16_t ss_family
Definition: network.h:76
AVStream ** streams
Definition: avformat.h:908
int av_sdp_create(AVFormatContext *ac[], int n_files, char *buf, int size)
Generate an SDP for an RTP session.
Definition: sdp.c:636
int av_find_info_tag(char *arg, int arg_size, const char *tag1, const char *info)
Attempt to find a specific tag in a URL.
Definition: parseutils.c:608
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:137
#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:191
int avio_close(AVIOContext *s)
Close the resource accessed by the AVIOContext s and free it.
Definition: aviobuf.c:987
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:140
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:64
#define HAVE_STRUCT_SOCKADDR_IN6
Definition: config.h:134
unsigned int nb_streams
A list of all streams in the file.
Definition: avformat.h:907
#define AV_LOG_VERBOSE
Definition: log.h:120
char filename[1024]
input or output filename
Definition: avformat.h:910
URLContext * ann_fd
Definition: sapdec.c:36
uint8_t * ann
Definition: sapenc.c:34
#define AVFMT_GLOBALHEADER
Format wants global header.
Definition: avformat.h:374
const char * name
Definition: avformat.h:390
static int sap_write_close(AVFormatContext *s)
Definition: sapenc.c:40
int ffurl_get_file_handle(URLContext *h)
Return the file descriptor associated with this URL.
Definition: avio.c:441
NULL
Definition: eval.c:50
static int sap_write_header(AVFormatContext *s)
Definition: sapenc.c:67
int ann_size
Definition: sapenc.c:35
AVIOContext * pb
Definition: avformat.h:896
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:111
Definition: url.h:42
#define AV_WB16(p, d)
Definition: intreadwrite.h:213
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition: utils.c:2702
misc parsing utilities
int ffurl_close(URLContext *h)
Close the resource accessed by the URLContext h, and free the memory used by it.
Definition: avio.c:376
#define getaddrinfo
Definition: network.h:150
Main libavformat public API header.
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:368
int ffurl_open(URLContext **puc, const char *filename, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options)
Create an URLContext for accessing to the resource indicated by url, and open it. ...
Definition: avio.c:293
int ff_write_chained(AVFormatContext *dst, int dst_stream, AVPacket *pkt, AVFormatContext *src)
Write a packet to another muxer than the one the user originally intended.
Definition: utils.c:3891
AVFormatContext * ff_rtp_chain_mux_open(AVFormatContext *s, AVStream *st, URLContext *handle, int packet_size)
Definition: rtpenc_chain.c:28
void * priv_data
Format private data.
Definition: avformat.h:883
int av_write_trailer(AVFormatContext *s)
Write the stream trailer to an output media file and free the file private data.
Definition: utils.c:3296
int max_packet_size
if non zero, the stream is packetized with this max packet size
Definition: url.h:48
unbuffered private I/O API
uint32_t av_get_random_seed(void)
Get random data.
Definition: random_seed.c:73
int stream_index
Definition: avcodec.h:910
int64_t av_gettime(void)
Get the current time in microseconds.
Definition: utils.c:3531
int ai_family
Definition: network.h:87