rtpproto.c 10.0 KB
Newer Older
F
Fabrice Bellard 已提交
1 2
/*
 * RTP network protocol
3
 * Copyright (c) 2002 Fabrice Bellard
F
Fabrice Bellard 已提交
4
 *
5
 * This file is part of Libav.
6
 *
7
 * Libav is free software; you can redistribute it and/or
F
Fabrice Bellard 已提交
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.
F
Fabrice Bellard 已提交
11
 *
12
 * Libav is distributed in the hope that it will be useful,
F
Fabrice Bellard 已提交
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
F
Fabrice Bellard 已提交
20
 */
21 22

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

27
#include "libavutil/parseutils.h"
28
#include "libavutil/avstring.h"
F
Fabrice Bellard 已提交
29
#include "avformat.h"
30
#include "avio_internal.h"
31
#include "rtpdec.h"
A
Anton Khirnov 已提交
32
#include "url.h"
F
Fabrice Bellard 已提交
33 34

#include <unistd.h>
F
Fabrice Bellard 已提交
35
#include <stdarg.h>
36
#include "internal.h"
37
#include "network.h"
38
#include "os_support.h"
F
Fabrice Bellard 已提交
39
#include <fcntl.h>
L
Luca Barbato 已提交
40 41
#if HAVE_POLL_H
#include <sys/poll.h>
42
#endif
43
#include <sys/time.h>
F
Fabrice Bellard 已提交
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
F
Fabrice Bellard 已提交
59 60 61
 * @param uri of the remote server
 * @return zero if no error.
 */
62

F
Fabrice Bellard 已提交
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;
F
Fabrice Bellard 已提交
68

F
Fabrice Bellard 已提交
69 70
    char buf[1024];
    char path[1024];
71

M
Måns Rullgård 已提交
72
    av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port,
M
Martin Storsjö 已提交
73
                 path, sizeof(path), uri);
F
Fabrice Bellard 已提交
74

75
    ff_url_join(buf, sizeof(buf), "udp", NULL, hostname, port, "%s", path);
76
    ff_udp_set_remote_url(s->rtp_hd, buf);
F
Fabrice Bellard 已提交
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);
F
Fabrice Bellard 已提交
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 void url_add_option(char *buf, int buf_size, const char *fmt, ...)
F
Fabrice Bellard 已提交
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);
F
Fabrice Bellard 已提交
97
    else
98
        av_strlcat(buf, "?", buf_size);
F
Fabrice Bellard 已提交
99
    vsnprintf(buf1, sizeof(buf1), fmt, ap);
100
    av_strlcat(buf, buf1, buf_size);
F
Fabrice Bellard 已提交
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)
F
Fabrice Bellard 已提交
108
{
109
    ff_url_join(buf, buf_size, "udp", NULL, hostname, port, NULL);
F
Fabrice Bellard 已提交
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");
F
Fabrice Bellard 已提交
118 119
}

120
/**
F
Fabrice Bellard 已提交
121
 * url syntax: rtp://host:port[?option=val...]
L
Luca Barbato 已提交
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
L
Luca Barbato 已提交
128 129
 * deprecated option:
 *         'localport=n'      : set the local port to n
F
Fabrice Bellard 已提交
130
 *
L
Luca Barbato 已提交
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
F
Fabrice Bellard 已提交
135
 */
136

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

F
Fabrice Bellard 已提交
148 149 150 151
    is_output = (flags & URL_WRONLY);

    s = av_mallocz(sizeof(RTPContext));
    if (!s)
152
        return AVERROR(ENOMEM);
F
Fabrice Bellard 已提交
153
    h->priv_data = s;
154

M
Måns Rullgård 已提交
155
    av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &rtp_port,
M
Martin Storsjö 已提交
156
                 path, sizeof(path), uri);
F
Fabrice Bellard 已提交
157 158
    /* extract parameters */
    ttl = -1;
L
Luca Barbato 已提交
159 160 161
    rtcp_port = rtp_port+1;
    local_rtp_port = -1;
    local_rtcp_port = -1;
162
    max_packet_size = -1;
163
    connect = 0;
164

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

F
Fabrice Bellard 已提交
190
    build_udp_url(buf, sizeof(buf),
191 192
                  hostname, rtp_port, local_rtp_port, ttl, max_packet_size,
                  connect);
A
Anton Khirnov 已提交
193
    if (ffurl_open(&s->rtp_hd, buf, flags) < 0)
F
Fabrice Bellard 已提交
194
        goto fail;
L
Luca Barbato 已提交
195
    if (local_rtp_port>=0 && local_rtcp_port<0)
196
        local_rtcp_port = ff_udp_get_local_port(s->rtp_hd) + 1;
197

F
Fabrice Bellard 已提交
198
    build_udp_url(buf, sizeof(buf),
199 200
                  hostname, rtcp_port, local_rtcp_port, ttl, max_packet_size,
                  connect);
A
Anton Khirnov 已提交
201
    if (ffurl_open(&s->rtcp_hd, buf, flags) < 0)
F
Fabrice Bellard 已提交
202
        goto fail;
203

F
Fabrice Bellard 已提交
204 205
    /* just to ease handle access. XXX: need to suppress direct handle
       access */
206 207
    s->rtp_fd = ffurl_get_file_handle(s->rtp_hd);
    s->rtcp_fd = ffurl_get_file_handle(s->rtcp_hd);
F
Fabrice Bellard 已提交
208

209
    h->max_packet_size = url_get_max_packet_size(s->rtp_hd);
F
Fabrice Bellard 已提交
210 211 212 213 214
    h->is_streamed = 1;
    return 0;

 fail:
    if (s->rtp_hd)
A
Anton Khirnov 已提交
215
        ffurl_close(s->rtp_hd);
F
Fabrice Bellard 已提交
216
    if (s->rtcp_hd)
A
Anton Khirnov 已提交
217
        ffurl_close(s->rtcp_hd);
F
Fabrice Bellard 已提交
218
    av_free(s);
219
    return AVERROR(EIO);
F
Fabrice Bellard 已提交
220 221
}

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

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

286
static int rtp_write(URLContext *h, const uint8_t *buf, int size)
F
Fabrice Bellard 已提交
287 288
{
    RTPContext *s = h->priv_data;
F
Fabrice Bellard 已提交
289
    int ret;
F
Fabrice Bellard 已提交
290
    URLContext *hd;
291

292
    if (buf[1] >= RTCP_SR && buf[1] <= RTCP_APP) {
F
Fabrice Bellard 已提交
293 294 295 296 297 298 299
        /* RTCP payload type */
        hd = s->rtcp_hd;
    } else {
        /* RTP payload type */
        hd = s->rtp_hd;
    }

A
Anton Khirnov 已提交
300
    ret = ffurl_write(hd, buf, size);
F
Fabrice Bellard 已提交
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
#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;

A
Anton Khirnov 已提交
316 317
    ffurl_close(s->rtp_hd);
    ffurl_close(s->rtcp_hd);
F
Fabrice Bellard 已提交
318 319 320 321 322
    av_free(s);
    return 0;
}

/**
L
Luca Barbato 已提交
323
 * Return the local rtp port used by the RTP connection
324
 * @param h media file context
L
Luca Barbato 已提交
325 326 327 328 329 330
 * @return the local port number
 */

int rtp_get_local_rtp_port(URLContext *h)
{
    RTPContext *s = h->priv_data;
331
    return ff_udp_get_local_port(s->rtp_hd);
F
Fabrice Bellard 已提交
332 333
}

L
Luca Barbato 已提交
334 335
/**
 * Return the local rtcp port used by the RTP connection
336
 * @param h media file context
L
Luca Barbato 已提交
337 338 339 340 341 342
 * @return the local port number
 */

int rtp_get_local_rtcp_port(URLContext *h)
{
    RTPContext *s = h->priv_data;
343
    return ff_udp_get_local_port(s->rtcp_hd);
L
Luca Barbato 已提交
344 345
}

346 347 348 349 350
static int rtp_get_file_handle(URLContext *h)
{
    RTPContext *s = h->priv_data;
    return s->rtp_fd;
}
F
Fabrice Bellard 已提交
351

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

357
URLProtocol ff_rtp_protocol = {
F
Fabrice Bellard 已提交
358 359 360 361 362 363
    "rtp",
    rtp_open,
    rtp_read,
    rtp_write,
    NULL, /* seek */
    rtp_close,
364
    .url_get_file_handle = rtp_get_file_handle,
F
Fabrice Bellard 已提交
365
};