sapenc.c 8.0 KB
Newer Older
1 2 3 4
/*
 * Session Announcement Protocol (RFC 2974) muxer
 * Copyright (c) 2010 Martin Storsjo
 *
5
 * This file is part of Libav.
6
 *
7
 * Libav is free software; you can redistribute it and/or
8 9 10 11
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
12
 * Libav is distributed in the hope that it will be useful,
13 14 15 16 17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with Libav; if not, write to the Free Software
19 20 21 22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include "avformat.h"
23
#include "libavutil/parseutils.h"
24 25 26 27 28
#include "libavutil/random_seed.h"
#include "libavutil/avstring.h"
#include "libavutil/intreadwrite.h"
#include "internal.h"
#include "network.h"
29
#include "os_support.h"
30
#include "rtpenc_chain.h"
A
Anton Khirnov 已提交
31
#include "url.h"
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

struct SAPState {
    uint8_t    *ann;
    int         ann_size;
    URLContext *ann_fd;
    int64_t     last_time;
};

static int sap_write_close(AVFormatContext *s)
{
    struct SAPState *sap = s->priv_data;
    int i;

    for (i = 0; i < s->nb_streams; i++) {
        AVFormatContext *rtpctx = s->streams[i]->priv_data;
        if (!rtpctx)
            continue;
        av_write_trailer(rtpctx);
50
        avio_close(rtpctx->pb);
51
        avformat_free_context(rtpctx);
52 53 54 55 56
        s->streams[i]->priv_data = NULL;
    }

    if (sap->last_time && sap->ann && sap->ann_fd) {
        sap->ann[0] |= 4; /* Session deletion*/
A
Anton Khirnov 已提交
57
        ffurl_write(sap->ann_fd, sap->ann, sap->ann_size);
58 59 60 61
    }

    av_freep(&sap->ann);
    if (sap->ann_fd)
A
Anton Khirnov 已提交
62
        ffurl_close(sap->ann_fd);
63 64 65 66 67 68 69 70 71
    ff_network_close();
    return 0;
}

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

    if (!ff_network_init())
        return AVERROR(EIO);

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

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

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

    contexts = av_mallocz(sizeof(AVFormatContext*) * s->nb_streams);
    if (!contexts) {
        ret = AVERROR(ENOMEM);
        goto fail;
    }

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

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

    ff_url_join(url, sizeof(url), "udp", NULL, announce_addr, port,
                "?ttl=%d&connect=1", ttl);
A
Anton Khirnov 已提交
161
    ret = ffurl_open(&sap->ann_fd, url, URL_WRONLY);
162 163 164 165 166 167 168 169 170 171
    if (ret) {
        ret = AVERROR(EIO);
        goto fail;
    }

    udp_fd = url_get_file_handle(sap->ann_fd);
    if (getsockname(udp_fd, (struct sockaddr*) &localaddr, &addrlen)) {
        ret = AVERROR(EIO);
        goto fail;
    }
172 173 174 175 176
    if (localaddr.ss_family != AF_INET
#if HAVE_STRUCT_SOCKADDR_IN6
        && localaddr.ss_family != AF_INET6
#endif
        ) {
177 178 179 180 181 182 183 184 185 186
        av_log(s, AV_LOG_ERROR, "Unsupported protocol family\n");
        ret = AVERROR(EIO);
        goto fail;
    }
    sap->ann_size = 8192;
    sap->ann = av_mallocz(sap->ann_size);
    if (!sap->ann) {
        ret = AVERROR(EIO);
        goto fail;
    }
187 188 189 190 191 192
    sap->ann[pos] = (1 << 5);
#if HAVE_STRUCT_SOCKADDR_IN6
    if (localaddr.ss_family == AF_INET6)
        sap->ann[pos] |= 0x10;
#endif
    pos++;
193 194 195 196 197 198 199
    sap->ann[pos++] = 0; /* Authentication length */
    AV_WB16(&sap->ann[pos], av_get_random_seed());
    pos += 2;
    if (localaddr.ss_family == AF_INET) {
        memcpy(&sap->ann[pos], &((struct sockaddr_in*)&localaddr)->sin_addr,
               sizeof(struct in_addr));
        pos += sizeof(struct in_addr);
200
#if HAVE_STRUCT_SOCKADDR_IN6
201 202 203 204
    } else {
        memcpy(&sap->ann[pos], &((struct sockaddr_in6*)&localaddr)->sin6_addr,
               sizeof(struct in6_addr));
        pos += sizeof(struct in6_addr);
205
#endif
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
    }

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

    if (avf_sdp_create(contexts, s->nb_streams, &sap->ann[pos],
                       sap->ann_size - pos)) {
        ret = AVERROR_INVALIDDATA;
        goto fail;
    }
    av_freep(&contexts);
    av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", &sap->ann[pos]);
    pos += strlen(&sap->ann[pos]);
    sap->ann_size = pos;

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

    return 0;

fail:
    av_free(contexts);
    sap_write_close(s);
    return ret;
}

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

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

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