Revision 011071e7

View differences:

configure
1363 1363
psp_muxer_select="mov_muxer"
1364 1364
rtsp_demuxer_select="http_protocol sdp_demuxer"
1365 1365
rtsp_muxer_select="rtp_muxer http_protocol sdp_demuxer"
1366
sap_muxer_select="rtp_muxer"
1366 1367
sdp_demuxer_select="asf_demuxer rm_demuxer rtp_protocol mpegts_demuxer mov_demuxer"
1367 1368
spdif_muxer_select="aac_parser"
1368 1369
tg2_muxer_select="mov_muxer"
doc/general.texi
217 217
    @tab Output is performed by publishing stream to RTMP server
218 218
@item RTP                       @tab X @tab
219 219
@item RTSP                      @tab X @tab X
220
@item SAP                       @tab X @tab
220 221
@item SDP                       @tab   @tab X
221 222
@item Sega FILM/CPK             @tab   @tab X
222 223
    @tab Used in many Sega Saturn console games.
libavformat/Makefile
229 229
                                            avc.o
230 230
OBJS-$(CONFIG_RTSP_DEMUXER)              += rtsp.o httpauth.o
231 231
OBJS-$(CONFIG_RTSP_MUXER)                += rtsp.o rtspenc.o httpauth.o
232
OBJS-$(CONFIG_SAP_MUXER)                 += sapenc.o rtpenc_chain.o
232 233
OBJS-$(CONFIG_SDP_DEMUXER)               += rtsp.o        \
233 234
                                            rdt.o         \
234 235
                                            rtp.o         \
libavformat/allformats.c
181 181
    REGISTER_MUXDEMUX (RSO, rso);
182 182
    REGISTER_MUXER    (RTP, rtp);
183 183
    REGISTER_MUXDEMUX (RTSP, rtsp);
184
    REGISTER_MUXER    (SAP, sap);
184 185
    REGISTER_DEMUXER  (SDP, sdp);
185 186
#if CONFIG_SDP_DEMUXER
186 187
    av_register_rtp_dynamic_payload_handlers();
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 "rtpenc_chain.h"
29

  
30
struct SAPState {
31
    uint8_t    *ann;
32
    int         ann_size;
33
    URLContext *ann_fd;
34
    int64_t     last_time;
35
};
36

  
37
static int sap_write_close(AVFormatContext *s)
38
{
39
    struct SAPState *sap = s->priv_data;
40
    int i;
41

  
42
    for (i = 0; i < s->nb_streams; i++) {
43
        AVFormatContext *rtpctx = s->streams[i]->priv_data;
44
        if (!rtpctx)
45
            continue;
46
        av_write_trailer(rtpctx);
47
        url_fclose(rtpctx->pb);
48
        av_metadata_free(&rtpctx->streams[0]->metadata);
49
        av_metadata_free(&rtpctx->metadata);
50
        av_free(rtpctx->streams[0]);
51
        av_free(rtpctx);
52
        s->streams[i]->priv_data = NULL;
53
    }
54

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

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

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

  
79
    if (!ff_network_init())
80
        return AVERROR(EIO);
81

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

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

  
106
    if (!announce_addr[0]) {
107
        struct addrinfo hints, *ai = NULL;
108
        memset(&hints, 0, sizeof(hints));
109
        hints.ai_family = PF_UNSPEC;
110
        if (getaddrinfo(host, NULL, &hints, &ai)) {
111
            av_log(s, AV_LOG_ERROR, "Unable to resolve %s\n", host);
112
            ret = AVERROR(EIO);
113
            goto fail;
114
        }
115
        if (ai->ai_family == AF_INET) {
116
            /* Also known as sap.mcast.net */
117
            av_strlcpy(announce_addr, "224.2.127.254", sizeof(announce_addr));
118
        } else if (ai->ai_family == AF_INET6) {
119
            /* With IPv6, you can use the same destination in many different
120
             * multicast subnets, to choose how far you want it routed.
121
             * This one is intended to be routed globally. */
122
            av_strlcpy(announce_addr, "ff0e::2:7ffe", sizeof(announce_addr));
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 && localaddr.ss_family != AF_INET6) {
171
        av_log(s, AV_LOG_ERROR, "Unsupported protocol family\n");
172
        ret = AVERROR(EIO);
173
        goto fail;
174
    }
175
    sap->ann_size = 8192;
176
    sap->ann = av_mallocz(sap->ann_size);
177
    if (!sap->ann) {
178
        ret = AVERROR(EIO);
179
        goto fail;
180
    }
181
    sap->ann[pos++] = (1 << 5) | ((localaddr.ss_family == AF_INET6) << 4);
182
    sap->ann[pos++] = 0; /* Authentication length */
183
    AV_WB16(&sap->ann[pos], av_get_random_seed());
184
    pos += 2;
185
    if (localaddr.ss_family == AF_INET) {
186
        memcpy(&sap->ann[pos], &((struct sockaddr_in*)&localaddr)->sin_addr,
187
               sizeof(struct in_addr));
188
        pos += sizeof(struct in_addr);
189
    } else {
190
        memcpy(&sap->ann[pos], &((struct sockaddr_in6*)&localaddr)->sin6_addr,
191
               sizeof(struct in6_addr));
192
        pos += sizeof(struct in6_addr);
193
    }
194

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

  
198
    if (avf_sdp_create(contexts, s->nb_streams, &sap->ann[pos],
199
                       sap->ann_size - pos)) {
200
        ret = AVERROR_INVALIDDATA;
201
        goto fail;
202
    }
203
    av_freep(&contexts);
204
    av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", &sap->ann[pos]);
205
    pos += strlen(&sap->ann[pos]);
206
    sap->ann_size = pos;
207

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

  
214
    return 0;
215

  
216
fail:
217
    av_free(contexts);
218
    sap_write_close(s);
219
    return ret;
220
}
221

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

  
228
    if (!sap->last_time || now - sap->last_time > 5000000) {
229
        int ret = url_write(sap->ann_fd, sap->ann, sap->ann_size);
230
        /* Don't abort even if we get "Destination unreachable" */
231
        if (ret < 0 && ret != FF_NETERROR(ECONNREFUSED))
232
            return ret;
233
        sap->last_time = now;
234
    }
235
    rtpctx = s->streams[pkt->stream_index]->priv_data;
236
    return ff_write_chained(rtpctx, 0, pkt, s);
237
}
238

  
239
AVOutputFormat sap_muxer = {
240
    "sap",
241
    NULL_IF_CONFIG_SMALL("SAP output format"),
242
    NULL,
243
    NULL,
244
    sizeof(struct SAPState),
245
    CODEC_ID_AAC,
246
    CODEC_ID_MPEG4,
247
    sap_write_header,
248
    sap_write_packet,
249
    sap_write_close,
250
    .flags = AVFMT_NOFILE | AVFMT_GLOBALHEADER,
251
};
252

  

Also available in: Unified diff