]> git.sesse.net Git - ffmpeg/blob - libavformat/sapenc.c
In av_close_input_stream(), flush the packet queue before to actually
[ffmpeg] / libavformat / sapenc.c
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/random_seed.h"
24 #include "libavutil/avstring.h"
25 #include "libavutil/intreadwrite.h"
26 #include "internal.h"
27 #include "network.h"
28 #include "os_support.h"
29 #include "rtpenc_chain.h"
30
31 struct SAPState {
32     uint8_t    *ann;
33     int         ann_size;
34     URLContext *ann_fd;
35     int64_t     last_time;
36 };
37
38 static int sap_write_close(AVFormatContext *s)
39 {
40     struct SAPState *sap = s->priv_data;
41     int i;
42
43     for (i = 0; i < s->nb_streams; i++) {
44         AVFormatContext *rtpctx = s->streams[i]->priv_data;
45         if (!rtpctx)
46             continue;
47         av_write_trailer(rtpctx);
48         url_fclose(rtpctx->pb);
49         av_metadata_free(&rtpctx->streams[0]->metadata);
50         av_metadata_free(&rtpctx->metadata);
51         av_free(rtpctx->streams[0]);
52         av_free(rtpctx);
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*/
58         url_write(sap->ann_fd, sap->ann, sap->ann_size);
59     }
60
61     av_freep(&sap->ann);
62     if (sap->ann_fd)
63         url_close(sap->ann_fd);
64     ff_network_close();
65     return 0;
66 }
67
68 static 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;
73     int port = 9875, base_port = 5004, i, pos = 0, same_port = 0, ttl = 255;
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 */
84     av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &base_port,
85                  path, sizeof(path), s->filename);
86     if (base_port < 0)
87         base_port = 5004;
88
89     /* search for options */
90     option_list = strrchr(path, '?');
91     if (option_list) {
92         char buf[50];
93         if (find_info_tag(buf, sizeof(buf), "announce_port", option_list)) {
94             port = strtol(buf, NULL, 10);
95         }
96         if (find_info_tag(buf, sizeof(buf), "same_port", option_list)) {
97             same_port = strtol(buf, NULL, 10);
98         }
99         if (find_info_tag(buf, sizeof(buf), "ttl", option_list)) {
100             ttl = strtol(buf, NULL, 10);
101         }
102         if (find_info_tag(buf, sizeof(buf), "announce_addr", option_list)) {
103             av_strlcpy(announce_addr, buf, sizeof(announce_addr));
104         }
105     }
106
107     if (!announce_addr[0]) {
108         struct addrinfo hints, *ai = NULL;
109         memset(&hints, 0, sizeof(hints));
110         hints.ai_family = AF_UNSPEC;
111         if (getaddrinfo(host, NULL, &hints, &ai)) {
112             av_log(s, AV_LOG_ERROR, "Unable to resolve %s\n", host);
113             ret = AVERROR(EIO);
114             goto fail;
115         }
116         if (ai->ai_family == AF_INET) {
117             /* Also known as sap.mcast.net */
118             av_strlcpy(announce_addr, "224.2.127.254", sizeof(announce_addr));
119 #if HAVE_STRUCT_SOCKADDR_IN6
120         } else if (ai->ai_family == AF_INET6) {
121             /* With IPv6, you can use the same destination in many different
122              * multicast subnets, to choose how far you want it routed.
123              * This one is intended to be routed globally. */
124             av_strlcpy(announce_addr, "ff0e::2:7ffe", sizeof(announce_addr));
125 #endif
126         } else {
127             freeaddrinfo(ai);
128             av_log(s, AV_LOG_ERROR, "Host %s resolved to unsupported "
129                                     "address family\n", host);
130             ret = AVERROR(EIO);
131             goto fail;
132         }
133         freeaddrinfo(ai);
134     }
135
136     contexts = av_mallocz(sizeof(AVFormatContext*) * s->nb_streams);
137     if (!contexts) {
138         ret = AVERROR(ENOMEM);
139         goto fail;
140     }
141
142     s->start_time_realtime = av_gettime();
143     for (i = 0; i < s->nb_streams; i++) {
144         URLContext *fd;
145
146         ff_url_join(url, sizeof(url), "rtp", NULL, host, base_port,
147                     "?ttl=%d", ttl);
148         if (!same_port)
149             base_port += 2;
150         ret = url_open(&fd, url, URL_WRONLY);
151         if (ret) {
152             ret = AVERROR(EIO);
153             goto fail;
154         }
155         s->streams[i]->priv_data = contexts[i] =
156             ff_rtp_chain_mux_open(s, s->streams[i], fd, 0);
157         av_strlcpy(contexts[i]->filename, url, sizeof(contexts[i]->filename));
158     }
159
160     ff_url_join(url, sizeof(url), "udp", NULL, announce_addr, port,
161                 "?ttl=%d&connect=1", ttl);
162     ret = url_open(&sap->ann_fd, url, URL_WRONLY);
163     if (ret) {
164         ret = AVERROR(EIO);
165         goto fail;
166     }
167
168     udp_fd = url_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
174 #if HAVE_STRUCT_SOCKADDR_IN6
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 (avf_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 > url_get_max_packet_size(sap->ann_fd)) {
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
236 static int sap_write_packet(AVFormatContext *s, AVPacket *pkt)
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 = url_write(sap->ann_fd, sap->ann, sap->ann_size);
244         /* Don't abort even if we get "Destination unreachable" */
245         if (ret < 0 && ret != FF_NETERROR(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
253 AVOutputFormat sap_muxer = {
254     "sap",
255     NULL_IF_CONFIG_SMALL("SAP output format"),
256     NULL,
257     NULL,
258     sizeof(struct SAPState),
259     CODEC_ID_AAC,
260     CODEC_ID_MPEG4,
261     sap_write_header,
262     sap_write_packet,
263     sap_write_close,
264     .flags = AVFMT_NOFILE | AVFMT_GLOBALHEADER,
265 };
266