Statistics
| Branch: | Revision:

ffmpeg / libavformat / sapenc.c @ 42f97696

History | View | Annotate | Download (7.98 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
        avformat_free_context(rtpctx);
50
        s->streams[i]->priv_data = NULL;
51
    }
52

    
53
    if (sap->last_time && sap->ann && sap->ann_fd) {
54
        sap->ann[0] |= 4; /* Session deletion*/
55
        url_write(sap->ann_fd, sap->ann, sap->ann_size);
56
    }
57

    
58
    av_freep(&sap->ann);
59
    if (sap->ann_fd)
60
        url_close(sap->ann_fd);
61
    ff_network_close();
62
    return 0;
63
}
64

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

    
77
    if (!ff_network_init())
78
        return AVERROR(EIO);
79

    
80
    /* extract hostname and port */
81
    av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &base_port,
82
                 path, sizeof(path), s->filename);
83
    if (base_port < 0)
84
        base_port = 5004;
85

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

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

    
133
    contexts = av_mallocz(sizeof(AVFormatContext*) * s->nb_streams);
134
    if (!contexts) {
135
        ret = AVERROR(ENOMEM);
136
        goto fail;
137
    }
138

    
139
    s->start_time_realtime = av_gettime();
140
    for (i = 0; i < s->nb_streams; i++) {
141
        URLContext *fd;
142

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

    
157
    ff_url_join(url, sizeof(url), "udp", NULL, announce_addr, port,
158
                "?ttl=%d&connect=1", ttl);
159
    ret = url_open(&sap->ann_fd, url, URL_WRONLY);
160
    if (ret) {
161
        ret = AVERROR(EIO);
162
        goto fail;
163
    }
164

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

    
206
    av_strlcpy(&sap->ann[pos], "application/sdp", sap->ann_size - pos);
207
    pos += strlen(&sap->ann[pos]) + 1;
208

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

    
219
    if (sap->ann_size > url_get_max_packet_size(sap->ann_fd)) {
220
        av_log(s, AV_LOG_ERROR, "Announcement too large to send in one "
221
                                "packet\n");
222
        goto fail;
223
    }
224

    
225
    return 0;
226

    
227
fail:
228
    av_free(contexts);
229
    sap_write_close(s);
230
    return ret;
231
}
232

    
233
static int sap_write_packet(AVFormatContext *s, AVPacket *pkt)
234
{
235
    AVFormatContext *rtpctx;
236
    struct SAPState *sap = s->priv_data;
237
    int64_t now = av_gettime();
238

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

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