rtp: set the payload type as stream id
[libav.git] / libavformat / sapenc.c
CommitLineData
011071e7
MS
1/*
2 * Session Announcement Protocol (RFC 2974) muxer
3 * Copyright (c) 2010 Martin Storsjo
4 *
2912e87a 5 * This file is part of Libav.
011071e7 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
011071e7
MS
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 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
011071e7
MS
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
2912e87a 18 * License along with Libav; if not, write to the Free Software
011071e7
MS
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include "avformat.h"
ab0287fc 23#include "libavutil/parseutils.h"
011071e7
MS
24#include "libavutil/random_seed.h"
25#include "libavutil/avstring.h"
26#include "libavutil/intreadwrite.h"
c4ef6a3e 27#include "libavutil/time.h"
011071e7
MS
28#include "internal.h"
29#include "network.h"
a1fc29f5 30#include "os_support.h"
011071e7 31#include "rtpenc_chain.h"
0589da0a 32#include "url.h"
011071e7
MS
33
34struct SAPState {
35 uint8_t *ann;
36 int ann_size;
37 URLContext *ann_fd;
38 int64_t last_time;
39};
40
41static int sap_write_close(AVFormatContext *s)
42{
43 struct SAPState *sap = s->priv_data;
44 int i;
45
46 for (i = 0; i < s->nb_streams; i++) {
47 AVFormatContext *rtpctx = s->streams[i]->priv_data;
48 if (!rtpctx)
49 continue;
50 av_write_trailer(rtpctx);
22a3212e 51 avio_close(rtpctx->pb);
b22dbb29 52 avformat_free_context(rtpctx);
011071e7
MS
53 s->streams[i]->priv_data = NULL;
54 }
55
56 if (sap->last_time && sap->ann && sap->ann_fd) {
57 sap->ann[0] |= 4; /* Session deletion*/
925e908b 58 ffurl_write(sap->ann_fd, sap->ann, sap->ann_size);
011071e7
MS
59 }
60
61 av_freep(&sap->ann);
62 if (sap->ann_fd)
e52a9145 63 ffurl_close(sap->ann_fd);
011071e7
MS
64 ff_network_close();
65 return 0;
66}
67
68static int sap_write_header(AVFormatContext *s)
69{
70 struct SAPState *sap = s->priv_data;
71 char host[1024], path[1024], url[1024], announce_addr[50] = "";
72 char *option_list;
1b40d496 73 int port = 9875, base_port = 5004, i, pos = 0, same_port = 0, ttl = 255;
011071e7
MS
74 AVFormatContext **contexts = NULL;
75 int ret = 0;
76 struct sockaddr_storage localaddr;
77 socklen_t addrlen = sizeof(localaddr);
78 int udp_fd;
79
80 if (!ff_network_init())
81 return AVERROR(EIO);
82
83 /* extract hostname and port */
1b40d496 84 av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &base_port,
011071e7 85 path, sizeof(path), s->filename);
1b40d496
MS
86 if (base_port < 0)
87 base_port = 5004;
011071e7
MS
88
89 /* search for options */
90 option_list = strrchr(path, '?');
91 if (option_list) {
92 char buf[50];
ab0287fc 93 if (av_find_info_tag(buf, sizeof(buf), "announce_port", option_list)) {
1b40d496 94 port = strtol(buf, NULL, 10);
011071e7 95 }
ab0287fc 96 if (av_find_info_tag(buf, sizeof(buf), "same_port", option_list)) {
011071e7
MS
97 same_port = strtol(buf, NULL, 10);
98 }
ab0287fc 99 if (av_find_info_tag(buf, sizeof(buf), "ttl", option_list)) {
011071e7
MS
100 ttl = strtol(buf, NULL, 10);
101 }
ab0287fc 102 if (av_find_info_tag(buf, sizeof(buf), "announce_addr", option_list)) {
011071e7
MS
103 av_strlcpy(announce_addr, buf, sizeof(announce_addr));
104 }
105 }
106
107 if (!announce_addr[0]) {
a92be9b8 108 struct addrinfo hints = { 0 }, *ai = NULL;
d1f676b8 109 hints.ai_family = AF_UNSPEC;
011071e7
MS
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));
fcc7f404 118#if HAVE_STRUCT_SOCKADDR_IN6
011071e7
MS
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));
fcc7f404 124#endif
011071e7
MS
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
141 s->start_time_realtime = av_gettime();
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;
ddffc2fd 149 ret = ffurl_open(&fd, url, AVIO_FLAG_WRITE, &s->interrupt_callback, NULL);
011071e7
MS
150 if (ret) {
151 ret = AVERROR(EIO);
152 goto fail;
153 }
8034130e 154 ret = ff_rtp_chain_mux_open(&contexts[i], s, s->streams[i], fd, 0, i);
68c81308
MS
155 if (ret < 0)
156 goto fail;
157 s->streams[i]->priv_data = contexts[i];
011071e7
MS
158 av_strlcpy(contexts[i]->filename, url, sizeof(contexts[i]->filename));
159 }
160
161 ff_url_join(url, sizeof(url), "udp", NULL, announce_addr, port,
162 "?ttl=%d&connect=1", ttl);
ddffc2fd
AK
163 ret = ffurl_open(&sap->ann_fd, url, AVIO_FLAG_WRITE,
164 &s->interrupt_callback, NULL);
011071e7
MS
165 if (ret) {
166 ret = AVERROR(EIO);
167 goto fail;
168 }
169
1869ea03 170 udp_fd = ffurl_get_file_handle(sap->ann_fd);
011071e7
MS
171 if (getsockname(udp_fd, (struct sockaddr*) &localaddr, &addrlen)) {
172 ret = AVERROR(EIO);
173 goto fail;
174 }
fcc7f404
MS
175 if (localaddr.ss_family != AF_INET
176#if HAVE_STRUCT_SOCKADDR_IN6
177 && localaddr.ss_family != AF_INET6
178#endif
179 ) {
011071e7
MS
180 av_log(s, AV_LOG_ERROR, "Unsupported protocol family\n");
181 ret = AVERROR(EIO);
182 goto fail;
183 }
184 sap->ann_size = 8192;
185 sap->ann = av_mallocz(sap->ann_size);
186 if (!sap->ann) {
187 ret = AVERROR(EIO);
188 goto fail;
189 }
fcc7f404
MS
190 sap->ann[pos] = (1 << 5);
191#if HAVE_STRUCT_SOCKADDR_IN6
192 if (localaddr.ss_family == AF_INET6)
193 sap->ann[pos] |= 0x10;
194#endif
195 pos++;
011071e7
MS
196 sap->ann[pos++] = 0; /* Authentication length */
197 AV_WB16(&sap->ann[pos], av_get_random_seed());
198 pos += 2;
199 if (localaddr.ss_family == AF_INET) {
200 memcpy(&sap->ann[pos], &((struct sockaddr_in*)&localaddr)->sin_addr,
201 sizeof(struct in_addr));
202 pos += sizeof(struct in_addr);
fcc7f404 203#if HAVE_STRUCT_SOCKADDR_IN6
011071e7
MS
204 } else {
205 memcpy(&sap->ann[pos], &((struct sockaddr_in6*)&localaddr)->sin6_addr,
206 sizeof(struct in6_addr));
207 pos += sizeof(struct in6_addr);
fcc7f404 208#endif
011071e7
MS
209 }
210
211 av_strlcpy(&sap->ann[pos], "application/sdp", sap->ann_size - pos);
212 pos += strlen(&sap->ann[pos]) + 1;
213
c3675dfe 214 if (av_sdp_create(contexts, s->nb_streams, &sap->ann[pos],
39e29aa0 215 sap->ann_size - pos)) {
011071e7
MS
216 ret = AVERROR_INVALIDDATA;
217 goto fail;
218 }
219 av_freep(&contexts);
220 av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", &sap->ann[pos]);
221 pos += strlen(&sap->ann[pos]);
222 sap->ann_size = pos;
223
5958df34 224 if (sap->ann_size > sap->ann_fd->max_packet_size) {
011071e7
MS
225 av_log(s, AV_LOG_ERROR, "Announcement too large to send in one "
226 "packet\n");
227 goto fail;
228 }
229
230 return 0;
231
232fail:
233 av_free(contexts);
234 sap_write_close(s);
235 return ret;
236}
237
238static int sap_write_packet(AVFormatContext *s, AVPacket *pkt)
239{
240 AVFormatContext *rtpctx;
241 struct SAPState *sap = s->priv_data;
242 int64_t now = av_gettime();
243
244 if (!sap->last_time || now - sap->last_time > 5000000) {
925e908b 245 int ret = ffurl_write(sap->ann_fd, sap->ann, sap->ann_size);
011071e7 246 /* Don't abort even if we get "Destination unreachable" */
28c4741a 247 if (ret < 0 && ret != AVERROR(ECONNREFUSED))
011071e7
MS
248 return ret;
249 sap->last_time = now;
250 }
251 rtpctx = s->streams[pkt->stream_index]->priv_data;
252 return ff_write_chained(rtpctx, 0, pkt, s);
253}
254
c6610a21 255AVOutputFormat ff_sap_muxer = {
dfc2c4d9 256 .name = "sap",
6774247a 257 .long_name = NULL_IF_CONFIG_SMALL("SAP output"),
dfc2c4d9 258 .priv_data_size = sizeof(struct SAPState),
36ef5369
AK
259 .audio_codec = AV_CODEC_ID_AAC,
260 .video_codec = AV_CODEC_ID_MPEG4,
dfc2c4d9
AK
261 .write_header = sap_write_header,
262 .write_packet = sap_write_packet,
263 .write_trailer = sap_write_close,
20234a4b 264 .flags = AVFMT_NOFILE | AVFMT_GLOBALHEADER,
011071e7 265};