Statistics
| Branch: | Revision:

ffmpeg / libavformat / sapenc.c @ 1338dc08

History | View | Annotate | Download (8.25 KB)

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]->codec->extradata);
52
        av_free(rtpctx->streams[0]->codec);
53
        av_free(rtpctx->streams[0]->info);
54
        av_free(rtpctx->streams[0]);
55
        av_free(rtpctx);
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
        url_write(sap->ann_fd, sap->ann, sap->ann_size);
62
    }
63

    
64
    av_freep(&sap->ann);
65
    if (sap->ann_fd)
66
        url_close(sap->ann_fd);
67
    ff_network_close();
68
    return 0;
69
}
70

    
71
static int sap_write_header(AVFormatContext *s)
72
{
73
    struct SAPState *sap = s->priv_data;
74
    char host[1024], path[1024], url[1024], announce_addr[50] = "";
75
    char *option_list;
76
    int port = 9875, base_port = 5004, i, pos = 0, same_port = 0, ttl = 255;
77
    AVFormatContext **contexts = NULL;
78
    int ret = 0;
79
    struct sockaddr_storage localaddr;
80
    socklen_t addrlen = sizeof(localaddr);
81
    int udp_fd;
82

    
83
    if (!ff_network_init())
84
        return AVERROR(EIO);
85

    
86
    /* extract hostname and port */
87
    av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &base_port,
88
                 path, sizeof(path), s->filename);
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 (find_info_tag(buf, sizeof(buf), "announce_port", option_list)) {
97
            port = strtol(buf, NULL, 10);
98
        }
99
        if (find_info_tag(buf, sizeof(buf), "same_port", option_list)) {
100
            same_port = strtol(buf, NULL, 10);
101
        }
102
        if (find_info_tag(buf, sizeof(buf), "ttl", option_list)) {
103
            ttl = strtol(buf, NULL, 10);
104
        }
105
        if (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, *ai = NULL;
112
        memset(&hints, 0, sizeof(hints));
113
        hints.ai_family = AF_UNSPEC;
114
        if (getaddrinfo(host, NULL, &hints, &ai)) {
115
            av_log(s, AV_LOG_ERROR, "Unable to resolve %s\n", host);
116
            ret = AVERROR(EIO);
117
            goto fail;
118
        }
119
        if (ai->ai_family == AF_INET) {
120
            /* Also known as sap.mcast.net */
121
            av_strlcpy(announce_addr, "224.2.127.254", sizeof(announce_addr));
122
#if HAVE_STRUCT_SOCKADDR_IN6
123
        } else if (ai->ai_family == AF_INET6) {
124
            /* With IPv6, you can use the same destination in many different
125
             * multicast subnets, to choose how far you want it routed.
126
             * This one is intended to be routed globally. */
127
            av_strlcpy(announce_addr, "ff0e::2:7ffe", sizeof(announce_addr));
128
#endif
129
        } else {
130
            freeaddrinfo(ai);
131
            av_log(s, AV_LOG_ERROR, "Host %s resolved to unsupported "
132
                                    "address family\n", host);
133
            ret = AVERROR(EIO);
134
            goto fail;
135
        }
136
        freeaddrinfo(ai);
137
    }
138

    
139
    contexts = av_mallocz(sizeof(AVFormatContext*) * s->nb_streams);
140
    if (!contexts) {
141
        ret = AVERROR(ENOMEM);
142
        goto fail;
143
    }
144

    
145
    s->start_time_realtime = av_gettime();
146
    for (i = 0; i < s->nb_streams; i++) {
147
        URLContext *fd;
148

    
149
        ff_url_join(url, sizeof(url), "rtp", NULL, host, base_port,
150
                    "?ttl=%d", ttl);
151
        if (!same_port)
152
            base_port += 2;
153
        ret = url_open(&fd, url, URL_WRONLY);
154
        if (ret) {
155
            ret = AVERROR(EIO);
156
            goto fail;
157
        }
158
        s->streams[i]->priv_data = contexts[i] =
159
            ff_rtp_chain_mux_open(s, s->streams[i], fd, 0);
160
        av_strlcpy(contexts[i]->filename, url, sizeof(contexts[i]->filename));
161
    }
162

    
163
    ff_url_join(url, sizeof(url), "udp", NULL, announce_addr, port,
164
                "?ttl=%d&connect=1", ttl);
165
    ret = url_open(&sap->ann_fd, url, URL_WRONLY);
166
    if (ret) {
167
        ret = AVERROR(EIO);
168
        goto fail;
169
    }
170

    
171
    udp_fd = url_get_file_handle(sap->ann_fd);
172
    if (getsockname(udp_fd, (struct sockaddr*) &localaddr, &addrlen)) {
173
        ret = AVERROR(EIO);
174
        goto fail;
175
    }
176
    if (localaddr.ss_family != AF_INET
177
#if HAVE_STRUCT_SOCKADDR_IN6
178
        && localaddr.ss_family != AF_INET6
179
#endif
180
        ) {
181
        av_log(s, AV_LOG_ERROR, "Unsupported protocol family\n");
182
        ret = AVERROR(EIO);
183
        goto fail;
184
    }
185
    sap->ann_size = 8192;
186
    sap->ann = av_mallocz(sap->ann_size);
187
    if (!sap->ann) {
188
        ret = AVERROR(EIO);
189
        goto fail;
190
    }
191
    sap->ann[pos] = (1 << 5);
192
#if HAVE_STRUCT_SOCKADDR_IN6
193
    if (localaddr.ss_family == AF_INET6)
194
        sap->ann[pos] |= 0x10;
195
#endif
196
    pos++;
197
    sap->ann[pos++] = 0; /* Authentication length */
198
    AV_WB16(&sap->ann[pos], av_get_random_seed());
199
    pos += 2;
200
    if (localaddr.ss_family == AF_INET) {
201
        memcpy(&sap->ann[pos], &((struct sockaddr_in*)&localaddr)->sin_addr,
202
               sizeof(struct in_addr));
203
        pos += sizeof(struct in_addr);
204
#if HAVE_STRUCT_SOCKADDR_IN6
205
    } else {
206
        memcpy(&sap->ann[pos], &((struct sockaddr_in6*)&localaddr)->sin6_addr,
207
               sizeof(struct in6_addr));
208
        pos += sizeof(struct in6_addr);
209
#endif
210
    }
211

    
212
    av_strlcpy(&sap->ann[pos], "application/sdp", sap->ann_size - pos);
213
    pos += strlen(&sap->ann[pos]) + 1;
214

    
215
    if (avf_sdp_create(contexts, s->nb_streams, &sap->ann[pos],
216
                       sap->ann_size - pos)) {
217
        ret = AVERROR_INVALIDDATA;
218
        goto fail;
219
    }
220
    av_freep(&contexts);
221
    av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", &sap->ann[pos]);
222
    pos += strlen(&sap->ann[pos]);
223
    sap->ann_size = pos;
224

    
225
    if (sap->ann_size > url_get_max_packet_size(sap->ann_fd)) {
226
        av_log(s, AV_LOG_ERROR, "Announcement too large to send in one "
227
                                "packet\n");
228
        goto fail;
229
    }
230

    
231
    return 0;
232

    
233
fail:
234
    av_free(contexts);
235
    sap_write_close(s);
236
    return ret;
237
}
238

    
239
static int sap_write_packet(AVFormatContext *s, AVPacket *pkt)
240
{
241
    AVFormatContext *rtpctx;
242
    struct SAPState *sap = s->priv_data;
243
    int64_t now = av_gettime();
244

    
245
    if (!sap->last_time || now - sap->last_time > 5000000) {
246
        int ret = url_write(sap->ann_fd, sap->ann, sap->ann_size);
247
        /* Don't abort even if we get "Destination unreachable" */
248
        if (ret < 0 && ret != FF_NETERROR(ECONNREFUSED))
249
            return ret;
250
        sap->last_time = now;
251
    }
252
    rtpctx = s->streams[pkt->stream_index]->priv_data;
253
    return ff_write_chained(rtpctx, 0, pkt, s);
254
}
255

    
256
AVOutputFormat ff_sap_muxer = {
257
    "sap",
258
    NULL_IF_CONFIG_SMALL("SAP output format"),
259
    NULL,
260
    NULL,
261
    sizeof(struct SAPState),
262
    CODEC_ID_AAC,
263
    CODEC_ID_MPEG4,
264
    sap_write_header,
265
    sap_write_packet,
266
    sap_write_close,
267
    .flags = AVFMT_NOFILE | AVFMT_GLOBALHEADER,
268
};
269