rtpproto.c 10.1 KB
Newer Older
Fabrice Bellard's avatar
Fabrice Bellard committed
1 2
/*
 * RTP network protocol
3
 * Copyright (c) 2002 Fabrice Bellard
Fabrice Bellard's avatar
Fabrice Bellard committed
4
 *
5
 * This file is part of Libav.
6
 *
7
 * Libav is free software; you can redistribute it and/or
Fabrice Bellard's avatar
Fabrice Bellard committed
8 9
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
Fabrice Bellard's avatar
Fabrice Bellard committed
11
 *
12
 * Libav is distributed in the hope that it will be useful,
Fabrice Bellard's avatar
Fabrice Bellard committed
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
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Fabrice Bellard's avatar
Fabrice Bellard committed
20
 */
21 22

/**
23
 * @file
24 25 26
 * RTP protocol
 */

27
#include "libavutil/parseutils.h"
28
#include "libavutil/avstring.h"
Fabrice Bellard's avatar
Fabrice Bellard committed
29
#include "avformat.h"
30
#include "avio_internal.h"
31
#include "rtpdec.h"
32
#include "url.h"
Fabrice Bellard's avatar
Fabrice Bellard committed
33 34

#include <unistd.h>
Fabrice Bellard's avatar
Fabrice Bellard committed
35
#include <stdarg.h>
36
#include "internal.h"
37
#include "network.h"
38
#include "os_support.h"
Fabrice Bellard's avatar
Fabrice Bellard committed
39
#include <fcntl.h>
Luca Barbato's avatar
Luca Barbato committed
40 41
#if HAVE_POLL_H
#include <sys/poll.h>
42
#endif
43
#include <sys/time.h>
Fabrice Bellard's avatar
Fabrice Bellard committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57

#define RTP_TX_BUF_SIZE  (64 * 1024)
#define RTP_RX_BUF_SIZE  (128 * 1024)

typedef struct RTPContext {
    URLContext *rtp_hd, *rtcp_hd;
    int rtp_fd, rtcp_fd;
} RTPContext;

/**
 * If no filename is given to av_open_input_file because you want to
 * get the local port first, then you must call this function to set
 * the remote server address.
 *
58
 * @param h media file context
Fabrice Bellard's avatar
Fabrice Bellard committed
59 60 61
 * @param uri of the remote server
 * @return zero if no error.
 */
62

Fabrice Bellard's avatar
Fabrice Bellard committed
63 64 65 66 67
int rtp_set_remote_url(URLContext *h, const char *uri)
{
    RTPContext *s = h->priv_data;
    char hostname[256];
    int port;
Fabrice Bellard's avatar
Fabrice Bellard committed
68

Fabrice Bellard's avatar
Fabrice Bellard committed
69 70
    char buf[1024];
    char path[1024];
71

Måns Rullgård's avatar
Måns Rullgård committed
72
    av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port,
Martin Storsjö's avatar
Martin Storsjö committed
73
                 path, sizeof(path), uri);
Fabrice Bellard's avatar
Fabrice Bellard committed
74

75
    ff_url_join(buf, sizeof(buf), "udp", NULL, hostname, port, "%s", path);
76
    ff_udp_set_remote_url(s->rtp_hd, buf);
Fabrice Bellard's avatar
Fabrice Bellard committed
77

78
    ff_url_join(buf, sizeof(buf), "udp", NULL, hostname, port + 1, "%s", path);
79
    ff_udp_set_remote_url(s->rtcp_hd, buf);
Fabrice Bellard's avatar
Fabrice Bellard committed
80 81 82 83
    return 0;
}


84 85 86 87 88
/**
 * add option to url of the form:
 * "http://host:port/path?option1=val1&option2=val2...
 */

89
static av_printf_format(3, 4) void url_add_option(char *buf, int buf_size, const char *fmt, ...)
Fabrice Bellard's avatar
Fabrice Bellard committed
90 91 92 93 94 95
{
    char buf1[1024];
    va_list ap;

    va_start(ap, fmt);
    if (strchr(buf, '?'))
96
        av_strlcat(buf, "&", buf_size);
Fabrice Bellard's avatar
Fabrice Bellard committed
97
    else
98
        av_strlcat(buf, "?", buf_size);
Fabrice Bellard's avatar
Fabrice Bellard committed
99
    vsnprintf(buf1, sizeof(buf1), fmt, ap);
100
    av_strlcat(buf, buf1, buf_size);
Fabrice Bellard's avatar
Fabrice Bellard committed
101 102 103
    va_end(ap);
}

104
static void build_udp_url(char *buf, int buf_size,
105
                          const char *hostname, int port,
106
                          int local_port, int ttl,
107
                          int max_packet_size, int connect)
Fabrice Bellard's avatar
Fabrice Bellard committed
108
{
109
    ff_url_join(buf, buf_size, "udp", NULL, hostname, port, NULL);
Fabrice Bellard's avatar
Fabrice Bellard committed
110 111 112 113
    if (local_port >= 0)
        url_add_option(buf, buf_size, "localport=%d", local_port);
    if (ttl >= 0)
        url_add_option(buf, buf_size, "ttl=%d", ttl);
114 115
    if (max_packet_size >=0)
        url_add_option(buf, buf_size, "pkt_size=%d", max_packet_size);
116 117
    if (connect)
        url_add_option(buf, buf_size, "connect=1");
Fabrice Bellard's avatar
Fabrice Bellard committed
118 119
}

120
/**
Fabrice Bellard's avatar
Fabrice Bellard committed
121
 * url syntax: rtp://host:port[?option=val...]
Luca Barbato's avatar
Luca Barbato committed
122 123 124 125 126
 * option: 'ttl=n'            : set the ttl value (for multicast only)
 *         'rtcpport=n'       : set the remote rtcp port to n
 *         'localrtpport=n'   : set the local rtp port to n
 *         'localrtcpport=n'  : set the local rtcp port to n
 *         'pkt_size=n'       : set max packet size
127
 *         'connect=0/1'      : do a connect() on the UDP socket
Luca Barbato's avatar
Luca Barbato committed
128 129
 * deprecated option:
 *         'localport=n'      : set the local port to n
Fabrice Bellard's avatar
Fabrice Bellard committed
130
 *
Luca Barbato's avatar
Luca Barbato committed
131 132 133 134
 * if rtcpport isn't set the rtcp port will be the rtp port + 1
 * if local rtp port isn't set any available port will be used for the local
 * rtp and rtcp ports
 * if the local rtcp port is not set it will be the local rtp port + 1
Fabrice Bellard's avatar
Fabrice Bellard committed
135
 */
136

Fabrice Bellard's avatar
Fabrice Bellard committed
137 138 139
static int rtp_open(URLContext *h, const char *uri, int flags)
{
    RTPContext *s;
Luca Barbato's avatar
Luca Barbato committed
140
    int rtp_port, rtcp_port,
141
        ttl, connect,
Luca Barbato's avatar
Luca Barbato committed
142
        local_rtp_port, local_rtcp_port, max_packet_size;
Fabrice Bellard's avatar
Fabrice Bellard committed
143 144 145
    char hostname[256];
    char buf[1024];
    char path[1024];
Fabrice Bellard's avatar
Fabrice Bellard committed
146
    const char *p;
147

Fabrice Bellard's avatar
Fabrice Bellard committed
148 149
    s = av_mallocz(sizeof(RTPContext));
    if (!s)
150
        return AVERROR(ENOMEM);
Fabrice Bellard's avatar
Fabrice Bellard committed
151
    h->priv_data = s;
152

Måns Rullgård's avatar
Måns Rullgård committed
153
    av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &rtp_port,
Martin Storsjö's avatar
Martin Storsjö committed
154
                 path, sizeof(path), uri);
Fabrice Bellard's avatar
Fabrice Bellard committed
155 156
    /* extract parameters */
    ttl = -1;
Luca Barbato's avatar
Luca Barbato committed
157 158 159
    rtcp_port = rtp_port+1;
    local_rtp_port = -1;
    local_rtcp_port = -1;
160
    max_packet_size = -1;
161
    connect = 0;
162

Fabrice Bellard's avatar
Fabrice Bellard committed
163 164
    p = strchr(uri, '?');
    if (p) {
165
        if (av_find_info_tag(buf, sizeof(buf), "ttl", p)) {
Fabrice Bellard's avatar
Fabrice Bellard committed
166 167
            ttl = strtol(buf, NULL, 10);
        }
168
        if (av_find_info_tag(buf, sizeof(buf), "rtcpport", p)) {
Luca Barbato's avatar
Luca Barbato committed
169 170
            rtcp_port = strtol(buf, NULL, 10);
        }
171
        if (av_find_info_tag(buf, sizeof(buf), "localport", p)) {
Luca Barbato's avatar
Luca Barbato committed
172 173
            local_rtp_port = strtol(buf, NULL, 10);
        }
174
        if (av_find_info_tag(buf, sizeof(buf), "localrtpport", p)) {
Luca Barbato's avatar
Luca Barbato committed
175 176
            local_rtp_port = strtol(buf, NULL, 10);
        }
177
        if (av_find_info_tag(buf, sizeof(buf), "localrtcpport", p)) {
Luca Barbato's avatar
Luca Barbato committed
178
            local_rtcp_port = strtol(buf, NULL, 10);
Fabrice Bellard's avatar
Fabrice Bellard committed
179
        }
180
        if (av_find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
181 182
            max_packet_size = strtol(buf, NULL, 10);
        }
183
        if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
184 185
            connect = strtol(buf, NULL, 10);
        }
Fabrice Bellard's avatar
Fabrice Bellard committed
186
    }
Fabrice Bellard's avatar
Fabrice Bellard committed
187

Fabrice Bellard's avatar
Fabrice Bellard committed
188
    build_udp_url(buf, sizeof(buf),
189 190
                  hostname, rtp_port, local_rtp_port, ttl, max_packet_size,
                  connect);
191
    if (ffurl_open(&s->rtp_hd, buf, flags) < 0)
Fabrice Bellard's avatar
Fabrice Bellard committed
192
        goto fail;
Luca Barbato's avatar
Luca Barbato committed
193
    if (local_rtp_port>=0 && local_rtcp_port<0)
194
        local_rtcp_port = ff_udp_get_local_port(s->rtp_hd) + 1;
195

Fabrice Bellard's avatar
Fabrice Bellard committed
196
    build_udp_url(buf, sizeof(buf),
197 198
                  hostname, rtcp_port, local_rtcp_port, ttl, max_packet_size,
                  connect);
199
    if (ffurl_open(&s->rtcp_hd, buf, flags) < 0)
Fabrice Bellard's avatar
Fabrice Bellard committed
200
        goto fail;
201

Fabrice Bellard's avatar
Fabrice Bellard committed
202 203
    /* just to ease handle access. XXX: need to suppress direct handle
       access */
204 205
    s->rtp_fd = ffurl_get_file_handle(s->rtp_hd);
    s->rtcp_fd = ffurl_get_file_handle(s->rtcp_hd);
Fabrice Bellard's avatar
Fabrice Bellard committed
206

207
    h->max_packet_size = s->rtp_hd->max_packet_size;
Fabrice Bellard's avatar
Fabrice Bellard committed
208 209 210 211 212
    h->is_streamed = 1;
    return 0;

 fail:
    if (s->rtp_hd)
213
        ffurl_close(s->rtp_hd);
Fabrice Bellard's avatar
Fabrice Bellard committed
214
    if (s->rtcp_hd)
215
        ffurl_close(s->rtcp_hd);
Fabrice Bellard's avatar
Fabrice Bellard committed
216
    av_free(s);
217
    return AVERROR(EIO);
Fabrice Bellard's avatar
Fabrice Bellard committed
218 219
}

220
static int rtp_read(URLContext *h, uint8_t *buf, int size)
Fabrice Bellard's avatar
Fabrice Bellard committed
221 222
{
    RTPContext *s = h->priv_data;
223
    struct sockaddr_storage from;
Måns Rullgård's avatar
Måns Rullgård committed
224
    socklen_t from_len;
Luca Barbato's avatar
Luca Barbato committed
225 226 227
    int len, n;
    struct pollfd p[2] = {{s->rtp_fd, POLLIN, 0}, {s->rtcp_fd, POLLIN, 0}};

Fabrice Bellard's avatar
Fabrice Bellard committed
228 229 230 231 232 233
#if 0
    for(;;) {
        from_len = sizeof(from);
        len = recvfrom (s->rtp_fd, buf, size, 0,
                        (struct sockaddr *)&from, &from_len);
        if (len < 0) {
234 235
            if (ff_neterrno() == AVERROR(EAGAIN) ||
                ff_neterrno() == AVERROR(EINTR))
Fabrice Bellard's avatar
Fabrice Bellard committed
236
                continue;
237
            return AVERROR(EIO);
Fabrice Bellard's avatar
Fabrice Bellard committed
238 239 240 241 242
        }
        break;
    }
#else
    for(;;) {
243
        if (url_interrupt_cb())
244
            return AVERROR_EXIT;
Fabrice Bellard's avatar
Fabrice Bellard committed
245
        /* build fdset to listen to RTP and RTCP packets */
Luca Barbato's avatar
Luca Barbato committed
246
        n = poll(p, 2, 100);
Fabrice Bellard's avatar
Fabrice Bellard committed
247 248
        if (n > 0) {
            /* first try RTCP */
Luca Barbato's avatar
Luca Barbato committed
249
            if (p[1].revents & POLLIN) {
Fabrice Bellard's avatar
Fabrice Bellard committed
250 251 252 253
                from_len = sizeof(from);
                len = recvfrom (s->rtcp_fd, buf, size, 0,
                                (struct sockaddr *)&from, &from_len);
                if (len < 0) {
254 255
                    if (ff_neterrno() == AVERROR(EAGAIN) ||
                        ff_neterrno() == AVERROR(EINTR))
Fabrice Bellard's avatar
Fabrice Bellard committed
256
                        continue;
257
                    return AVERROR(EIO);
Fabrice Bellard's avatar
Fabrice Bellard committed
258 259 260 261
                }
                break;
            }
            /* then RTP */
Luca Barbato's avatar
Luca Barbato committed
262
            if (p[0].revents & POLLIN) {
Fabrice Bellard's avatar
Fabrice Bellard committed
263 264 265 266
                from_len = sizeof(from);
                len = recvfrom (s->rtp_fd, buf, size, 0,
                                (struct sockaddr *)&from, &from_len);
                if (len < 0) {
267 268
                    if (ff_neterrno() == AVERROR(EAGAIN) ||
                        ff_neterrno() == AVERROR(EINTR))
Fabrice Bellard's avatar
Fabrice Bellard committed
269
                        continue;
270
                    return AVERROR(EIO);
Fabrice Bellard's avatar
Fabrice Bellard committed
271 272 273
                }
                break;
            }
274
        } else if (n < 0) {
275
            if (ff_neterrno() == AVERROR(EINTR))
276
                continue;
277
            return AVERROR(EIO);
Fabrice Bellard's avatar
Fabrice Bellard committed
278 279 280 281 282 283
        }
    }
#endif
    return len;
}

284
static int rtp_write(URLContext *h, const uint8_t *buf, int size)
Fabrice Bellard's avatar
Fabrice Bellard committed
285 286
{
    RTPContext *s = h->priv_data;
Fabrice Bellard's avatar
Fabrice Bellard committed
287
    int ret;
Fabrice Bellard's avatar
Fabrice Bellard committed
288
    URLContext *hd;
289

290
    if (buf[1] >= RTCP_SR && buf[1] <= RTCP_APP) {
Fabrice Bellard's avatar
Fabrice Bellard committed
291 292 293 294 295 296 297
        /* RTCP payload type */
        hd = s->rtcp_hd;
    } else {
        /* RTP payload type */
        hd = s->rtp_hd;
    }

298
    ret = ffurl_write(hd, buf, size);
Fabrice Bellard's avatar
Fabrice Bellard committed
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
#if 0
    {
        struct timespec ts;
        ts.tv_sec = 0;
        ts.tv_nsec = 10 * 1000000;
        nanosleep(&ts, NULL);
    }
#endif
    return ret;
}

static int rtp_close(URLContext *h)
{
    RTPContext *s = h->priv_data;

314 315
    ffurl_close(s->rtp_hd);
    ffurl_close(s->rtcp_hd);
Fabrice Bellard's avatar
Fabrice Bellard committed
316 317 318 319 320
    av_free(s);
    return 0;
}

/**
Luca Barbato's avatar
Luca Barbato committed
321
 * Return the local rtp port used by the RTP connection
322
 * @param h media file context
Luca Barbato's avatar
Luca Barbato committed
323 324 325 326 327 328
 * @return the local port number
 */

int rtp_get_local_rtp_port(URLContext *h)
{
    RTPContext *s = h->priv_data;
329
    return ff_udp_get_local_port(s->rtp_hd);
Fabrice Bellard's avatar
Fabrice Bellard committed
330 331
}

Luca Barbato's avatar
Luca Barbato committed
332 333
/**
 * Return the local rtcp port used by the RTP connection
334
 * @param h media file context
Luca Barbato's avatar
Luca Barbato committed
335 336 337 338 339 340
 * @return the local port number
 */

int rtp_get_local_rtcp_port(URLContext *h)
{
    RTPContext *s = h->priv_data;
341
    return ff_udp_get_local_port(s->rtcp_hd);
Luca Barbato's avatar
Luca Barbato committed
342 343
}

344 345 346 347 348
static int rtp_get_file_handle(URLContext *h)
{
    RTPContext *s = h->priv_data;
    return s->rtp_fd;
}
Fabrice Bellard's avatar
Fabrice Bellard committed
349

350 351 352 353 354
int rtp_get_rtcp_file_handle(URLContext *h) {
    RTPContext *s = h->priv_data;
    return s->rtcp_fd;
}

355
URLProtocol ff_rtp_protocol = {
356 357 358 359 360
    .name                = "rtp",
    .url_open            = rtp_open,
    .url_read            = rtp_read,
    .url_write           = rtp_write,
    .url_close           = rtp_close,
361
    .url_get_file_handle = rtp_get_file_handle,
Fabrice Bellard's avatar
Fabrice Bellard committed
362
};