Statistics
| Branch: | Revision:

ffmpeg / libavformat / sapenc.c @ 1f56f5ed

History | View | Annotate | Download (8.15 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]->info);
52
        av_free(rtpctx->streams[0]);
53
        av_free(rtpctx);
54
        s->streams[i]->priv_data = NULL;
55
    }
56

    
57
    if (sap->last_time && sap->ann && sap->ann_fd) {
58
        sap->ann[0] |= 4; /* Session deletion*/
59
        url_write(sap->ann_fd, sap->ann, sap->ann_size);
60
    }
61

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

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

    
81
    if (!ff_network_init())
82
        return AVERROR(EIO);
83

    
84
    /* extract hostname and port */
85
    av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &base_port,
86
                 path, sizeof(path), s->filename);
87
    if (base_port < 0)
88
        base_port = 5004;
89

    
90
    /* search for options */
91
    option_list = strrchr(path, '?');
92
    if (option_list) {
93
        char buf[50];
94
        if (find_info_tag(buf, sizeof(buf), "announce_port", option_list)) {
95
            port = strtol(buf, NULL, 10);
96
        }
97
        if (find_info_tag(buf, sizeof(buf), "same_port", option_list)) {
98
            same_port = strtol(buf, NULL, 10);
99
        }
100
        if (find_info_tag(buf, sizeof(buf), "ttl", option_list)) {
101
            ttl = strtol(buf, NULL, 10);
102
        }
103
        if (find_info_tag(buf, sizeof(buf), "announce_addr", option_list)) {
104
            av_strlcpy(announce_addr, buf, sizeof(announce_addr));
105
        }
106
    }
107

    
108
    if (!announce_addr[0]) {
109
        struct addrinfo hints, *ai = NULL;
110
        memset(&hints, 0, sizeof(hints));
111
        hints.ai_family = AF_UNSPEC;
112
        if (getaddrinfo(host, NULL, &hints, &ai)) {
113
            av_log(s, AV_LOG_ERROR, "Unable to resolve %s\n", host);
114
            ret = AVERROR(EIO);
115
            goto fail;
116
        }
117
        if (ai->ai_family == AF_INET) {
118
            /* Also known as sap.mcast.net */
119
            av_strlcpy(announce_addr, "224.2.127.254", sizeof(announce_addr));
120
#if HAVE_STRUCT_SOCKADDR_IN6
121
        } else if (ai->ai_family == AF_INET6) {
122
            /* With IPv6, you can use the same destination in many different
123
             * multicast subnets, to choose how far you want it routed.
124
             * This one is intended to be routed globally. */
125
            av_strlcpy(announce_addr, "ff0e::2:7ffe", sizeof(announce_addr));
126
#endif
127
        } else {
128
            freeaddrinfo(ai);
129
            av_log(s, AV_LOG_ERROR, "Host %s resolved to unsupported "
130
                                    "address family\n", host);
131
            ret = AVERROR(EIO);
132
            goto fail;
133
        }
134
        freeaddrinfo(ai);
135
    }
136

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

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

    
147
        ff_url_join(url, sizeof(url), "rtp", NULL, host, base_port,
148
                    "?ttl=%d", ttl);
149
        if (!same_port)
150
            base_port += 2;
151
        ret = url_open(&fd, url, URL_WRONLY);
152
        if (ret) {
153
            ret = AVERROR(EIO);
154
            goto fail;
155
        }
156
        s->streams[i]->priv_data = contexts[i] =
157
            ff_rtp_chain_mux_open(s, s->streams[i], fd, 0);
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);
163
    ret = url_open(&sap->ann_fd, url, URL_WRONLY);
164
    if (ret) {
165
        ret = AVERROR(EIO);
166
        goto fail;
167
    }
168

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

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

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

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

    
229
    return 0;
230

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

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

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

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