FFmpeg
Loading...
Searching...
No Matches
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 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/mem.h"
26#include "libavutil/avstring.h"
27#include "libavutil/dict.h"
29#include "libavutil/time.h"
30#include "internal.h"
31#include "mux.h"
32#include "network.h"
33#include "os_support.h"
34#include "rtpenc_chain.h"
35#include "url.h"
36
37struct SAPState {
38 uint8_t *ann;
42};
43
45{
46 struct SAPState *sap = s->priv_data;
47 int i;
48
49 for (i = 0; i < s->nb_streams; i++) {
50 AVFormatContext *rtpctx = s->streams[i]->priv_data;
51 if (!rtpctx)
52 continue;
53 av_write_trailer(rtpctx);
54 avio_closep(&rtpctx->pb);
56 s->streams[i]->priv_data = NULL;
57 }
58
59 if (sap->last_time && sap->ann && sap->ann_fd) {
60 sap->ann[0] |= 4; /* Session deletion*/
61 ffurl_write(sap->ann_fd, sap->ann, sap->ann_size);
62 }
63
64 av_freep(&sap->ann);
65 ffurl_closep(&sap->ann_fd);
67 return 0;
68}
69
71{
72 struct SAPState *sap = s->priv_data;
73 char host[1024], path[1024], url[1024], announce_addr[50] = "";
74 char *option_list;
75 int port = 9875, base_port = 5004, i, pos = 0, same_port = 0, ttl = 255;
76 AVFormatContext **contexts = NULL;
77 int ret = 0;
78 struct sockaddr_storage localaddr;
79 socklen_t addrlen = sizeof(localaddr);
80 int udp_fd;
81 AVDictionaryEntry* title = av_dict_get(s->metadata, "title", NULL, 0);
82
83 if ((ret = ff_network_init()) < 0)
84 return ret;
85
86 /* extract hostname and port */
87 av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &base_port,
88 path, sizeof(path), s->url);
89 if (base_port < 0)
90 base_port = 5004;
91
92 /* search for options */
93 option_list = strrchr(path, '?');
94 if (option_list) {
95 char buf[50];
96 if (av_find_info_tag(buf, sizeof(buf), "announce_port", option_list)) {
97 port = strtol(buf, NULL, 10);
98 }
99 if (av_find_info_tag(buf, sizeof(buf), "same_port", option_list)) {
100 same_port = strtol(buf, NULL, 10);
101 }
102 if (av_find_info_tag(buf, sizeof(buf), "ttl", option_list)) {
103 ttl = strtol(buf, NULL, 10);
104 }
105 if (av_find_info_tag(buf, sizeof(buf), "announce_addr", option_list)) {
106 av_strlcpy(announce_addr, buf, sizeof(announce_addr));
107 }
108 }
109
110 if (!announce_addr[0]) {
111 struct addrinfo hints = { 0 }, *ai = NULL;
112 hints.ai_family = AF_UNSPEC;
113 if (getaddrinfo(host, NULL, &hints, &ai)) {
114 av_log(s, AV_LOG_ERROR, "Unable to resolve %s\n", host);
115 ret = AVERROR(EIO);
116 goto fail;
117 }
118 if (ai->ai_family == AF_INET) {
119 /* Also known as sap.mcast.net */
120 av_strlcpy(announce_addr, "224.2.127.254", sizeof(announce_addr));
121#if HAVE_STRUCT_SOCKADDR_IN6
122 } else if (ai->ai_family == AF_INET6) {
123 /* With IPv6, you can use the same destination in many different
124 * multicast subnets, to choose how far you want it routed.
125 * This one is intended to be routed globally. */
126 av_strlcpy(announce_addr, "ff0e::2:7ffe", sizeof(announce_addr));
127#endif
128 } else {
129 freeaddrinfo(ai);
130 av_log(s, AV_LOG_ERROR, "Host %s resolved to unsupported "
131 "address family\n", host);
132 ret = AVERROR(EIO);
133 goto fail;
134 }
135 freeaddrinfo(ai);
136 }
137
138 contexts = av_calloc(s->nb_streams, sizeof(*contexts));
139 if (!contexts) {
140 ret = AVERROR(ENOMEM);
141 goto fail;
142 }
143
144 if (s->start_time_realtime == 0 || s->start_time_realtime == AV_NOPTS_VALUE)
145 s->start_time_realtime = av_gettime();
146 for (i = 0; i < s->nb_streams; i++) {
147 URLContext *fd;
148 char *new_url;
149
150 ff_url_join(url, sizeof(url), "rtp", NULL, host, base_port,
151 "?ttl=%d", ttl);
152 if (!same_port)
153 base_port += 2;
155 &s->interrupt_callback, NULL,
156 s->protocol_whitelist, s->protocol_blacklist, NULL);
157 if (ret) {
158 ret = AVERROR(EIO);
159 goto fail;
160 }
161 ret = ff_rtp_chain_mux_open(&contexts[i], s, s->streams[i], fd, 0, i);
162 if (ret < 0)
163 goto fail;
164 s->streams[i]->priv_data = contexts[i];
165 s->streams[i]->time_base = contexts[i]->streams[0]->time_base;
166 new_url = av_strdup(url);
167 if (!new_url) {
168 ret = AVERROR(ENOMEM);
169 goto fail;
170 }
171 ff_format_set_url(contexts[i], new_url);
172 }
173
174 if (s->nb_streams > 0 && title)
175 av_dict_set(&contexts[0]->metadata, "title", title->value, 0);
176
177 ff_url_join(url, sizeof(url), "udp", NULL, announce_addr, port,
178 "?ttl=%d&connect=1", ttl);
180 &s->interrupt_callback, NULL,
181 s->protocol_whitelist, s->protocol_blacklist, NULL);
182 if (ret) {
183 ret = AVERROR(EIO);
184 goto fail;
185 }
186
187 udp_fd = ffurl_get_file_handle(sap->ann_fd);
188 if (getsockname(udp_fd, (struct sockaddr*) &localaddr, &addrlen)) {
189 ret = AVERROR(EIO);
190 goto fail;
191 }
192 if (localaddr.ss_family != AF_INET
193#if HAVE_STRUCT_SOCKADDR_IN6
194 && localaddr.ss_family != AF_INET6
195#endif
196 ) {
197 av_log(s, AV_LOG_ERROR, "Unsupported protocol family\n");
198 ret = AVERROR(EIO);
199 goto fail;
200 }
201 sap->ann_size = 8192;
202 sap->ann = av_mallocz(sap->ann_size);
203 if (!sap->ann) {
204 ret = AVERROR(EIO);
205 goto fail;
206 }
207 sap->ann[pos] = (1 << 5);
208#if HAVE_STRUCT_SOCKADDR_IN6
209 if (localaddr.ss_family == AF_INET6)
210 sap->ann[pos] |= 0x10;
211#endif
212 pos++;
213 sap->ann[pos++] = 0; /* Authentication length */
215 pos += 2;
216 if (localaddr.ss_family == AF_INET) {
217 memcpy(&sap->ann[pos], &((struct sockaddr_in*)&localaddr)->sin_addr,
218 sizeof(struct in_addr));
219 pos += sizeof(struct in_addr);
220#if HAVE_STRUCT_SOCKADDR_IN6
221 } else {
222 memcpy(&sap->ann[pos], &((struct sockaddr_in6*)&localaddr)->sin6_addr,
223 sizeof(struct in6_addr));
224 pos += sizeof(struct in6_addr);
225#endif
226 }
227
228 av_strlcpy(&sap->ann[pos], "application/sdp", sap->ann_size - pos);
229 pos += strlen(&sap->ann[pos]) + 1;
230
231 if (av_sdp_create(contexts, s->nb_streams, &sap->ann[pos],
232 sap->ann_size - pos)) {
234 goto fail;
235 }
236 av_freep(&contexts);
237 av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", &sap->ann[pos]);
238 pos += strlen(&sap->ann[pos]);
239 sap->ann_size = pos;
240
241 if (sap->ann_size > sap->ann_fd->max_packet_size) {
242 av_log(s, AV_LOG_ERROR, "Announcement too large to send in one "
243 "packet\n");
244 goto fail;
245 }
246
247 return 0;
248
249fail:
250 av_free(contexts);
252 return ret;
253}
254
256{
257 AVFormatContext *rtpctx;
258 struct SAPState *sap = s->priv_data;
260
261 if (!sap->last_time || now - sap->last_time > 5000000) {
262 int ret = ffurl_write(sap->ann_fd, sap->ann, sap->ann_size);
263 /* Don't abort even if we get "Destination unreachable" */
264 if (ret < 0 && ret != AVERROR(ECONNREFUSED))
265 return ret;
266 sap->last_time = now;
267 }
268 rtpctx = s->streams[pkt->stream_index]->priv_data;
269 return ff_write_chained(rtpctx, 0, pkt, s, 0);
270}
271
273 .p.name = "sap",
274 .p.long_name = NULL_IF_CONFIG_SMALL("SAP output"),
275 .priv_data_size = sizeof(struct SAPState),
276 .p.audio_codec = AV_CODEC_ID_AAC,
277 .p.video_codec = AV_CODEC_ID_MPEG4,
278 .write_header = sap_write_header,
279 .write_packet = sap_write_packet,
280 .write_trailer = sap_write_close,
281 .p.flags = AVFMT_NOFILE | AVFMT_GLOBALHEADER,
282};
const FFOutputFormat ff_sap_muxer
Definition sapenc.c:272
void ff_format_set_url(AVFormatContext *s, char *url)
Set AVFormatContext url field to the provided pointer.
Definition avformat.c:923
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:488
#define AVFMT_GLOBALHEADER
Format wants global header.
Definition avformat.h:497
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
int avio_closep(AVIOContext **s)
Close the resource accessed by the AVIOContext *s, free it and set the pointer pointing to it to NULL...
Definition avio.c:717
#define AVIO_FLAG_WRITE
write-only
Definition avio.h:618
static int FUNC metadata(CodedBitstreamContext *ctx, RWContext *rw, APVRawMetadata *current)
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static AVPacket * pkt
Public dictionary API.
#define fail
Definition test.h:479
@ AV_CODEC_ID_AAC
Definition codec_id.h:455
@ AV_CODEC_ID_MPEG4
Definition codec_id.h:62
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition avformat.c:150
int av_write_trailer(AVFormatContext *s)
Write the stream trailer to an output media file and free the file private data.
Definition mux.c:1238
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
int av_sdp_create(AVFormatContext *ac[], int n_files, char *buf, int size)
Generate an SDP for an RTP session.
Definition sdp.c:950
uint32_t av_get_random_seed(void)
Get a seed to use in conjunction with random functions.
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition dict.c:60
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition dict.c:86
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_VERBOSE
Detailed information.
Definition log.h:226
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
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_NOPTS_VALUE
Undefined timestamp value.
Definition avutil.h:247
#define AV_WB16(p, v)
int ff_write_chained(AVFormatContext *dst, int dst_stream, AVPacket *pkt, AVFormatContext *src, int interleave)
Write a packet to another muxer than the one the user originally intended.
Definition mux.c:1337
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:264
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 getaddrinfo
Definition network.h:217
#define freeaddrinfo
Definition network.h:218
#define av_strdup(s)
Definition ops_static.c:55
miscellaneous OS support macros and functions.
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:756
misc parsing utilities
int ff_rtp_chain_mux_open(AVFormatContext **out, AVFormatContext *s, AVStream *st, URLContext *handle, int packet_size, int idx)
static int sap_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition sapenc.c:255
static int sap_write_header(AVFormatContext *s)
Definition sapenc.c:70
static int sap_write_close(AVFormatContext *s)
Definition sapenc.c:44
unsigned int pos
Definition spdifenc.c:431
char * value
Definition dict.h:92
Format I/O context.
Definition avformat.h:1333
AVIOContext * pb
I/O context.
Definition avformat.h:1375
AVStream ** streams
A list of all streams in the file.
Definition avformat.h:1401
This structure stores compressed data.
Definition packet.h:580
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition avformat.h:805
int64_t last_time
Definition sapenc.c:41
URLContext * ann_fd
Definition sapdec.c:38
int ann_size
Definition sapenc.c:39
uint8_t * ann
Definition sapenc.c:38
int max_packet_size
if non zero, the stream is packetized with this max packet size
Definition url.h:41
int ai_family
Definition network.h:139
uint16_t ss_family
Definition network.h:116
#define av_free(p)
#define av_mallocz(s)
#define av_freep(p)
#define av_log(a,...)
int64_t av_gettime_relative(void)
Get the current time in microseconds since some unspecified starting point.
Definition time.c:57
int64_t av_gettime(void)
Get the current time in microseconds.
Definition time.c:40
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_write(URLContext *h, const uint8_t *buf, int size)
Write size bytes from buf to the resource accessed by h.
Definition url.h:204