Statistics
| Branch: | Revision:

ffmpeg / libavformat / rtpproto.c @ d7fb5a18

History | View | Annotate | Download (8.38 KB)

1 3b50d2ad Fabrice Bellard
/*
2
 * RTP network protocol
3
 * Copyright (c) 2002 Fabrice Bellard.
4
 *
5 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8 3b50d2ad Fabrice Bellard
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
11 3b50d2ad Fabrice Bellard
 *
12 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
13 3b50d2ad Fabrice Bellard
 * 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 b78e7197 Diego Biurrun
 * License along with FFmpeg; if not, write to the Free Software
19 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 3b50d2ad Fabrice Bellard
 */
21 b4999290 Luca Barbato
22
/**
23
 * @file rtpproto.c
24
 * RTP protocol
25
 */
26
27 245976da Diego Biurrun
#include "libavutil/avstring.h"
28 3b50d2ad Fabrice Bellard
#include "avformat.h"
29
30
#include <unistd.h>
31 51885599 Fabrice Bellard
#include <stdarg.h>
32 42572ef5 Ramiro Polla
#include "network.h"
33 93115b82 Dave Yeo
#include "os_support.h"
34 3b50d2ad Fabrice Bellard
#include <fcntl.h>
35 7139bfa8 Kurtnoise
#ifdef HAVE_SYS_SELECT_H
36 0e55ba72 Michael Niedermayer
#include <sys/select.h>
37 7139bfa8 Kurtnoise
#endif
38 3b50d2ad Fabrice Bellard
39
#define RTP_TX_BUF_SIZE  (64 * 1024)
40
#define RTP_RX_BUF_SIZE  (128 * 1024)
41
42
typedef struct RTPContext {
43
    URLContext *rtp_hd, *rtcp_hd;
44
    int rtp_fd, rtcp_fd;
45
} RTPContext;
46
47
/**
48
 * If no filename is given to av_open_input_file because you want to
49
 * get the local port first, then you must call this function to set
50
 * the remote server address.
51
 *
52
 * @param s1 media file context
53
 * @param uri of the remote server
54
 * @return zero if no error.
55
 */
56 b4999290 Luca Barbato
57 3b50d2ad Fabrice Bellard
int rtp_set_remote_url(URLContext *h, const char *uri)
58
{
59
    RTPContext *s = h->priv_data;
60
    char hostname[256];
61
    int port;
62 51885599 Fabrice Bellard
63 3b50d2ad Fabrice Bellard
    char buf[1024];
64
    char path[1024];
65 115329f1 Diego Biurrun
66
    url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port,
67 3b50d2ad Fabrice Bellard
              path, sizeof(path), uri);
68
69
    snprintf(buf, sizeof(buf), "udp://%s:%d%s", hostname, port, path);
70
    udp_set_remote_url(s->rtp_hd, buf);
71
72
    snprintf(buf, sizeof(buf), "udp://%s:%d%s", hostname, port + 1, path);
73
    udp_set_remote_url(s->rtcp_hd, buf);
74
    return 0;
75
}
76
77
78 b4999290 Luca Barbato
/**
79
 * add option to url of the form:
80
 * "http://host:port/path?option1=val1&option2=val2...
81
 */
82
83 5c91a675 Zdenek Kabelac
static void url_add_option(char *buf, int buf_size, const char *fmt, ...)
84 51885599 Fabrice Bellard
{
85
    char buf1[1024];
86
    va_list ap;
87
88
    va_start(ap, fmt);
89
    if (strchr(buf, '?'))
90 f7d78f36 Måns Rullgård
        av_strlcat(buf, "&", buf_size);
91 51885599 Fabrice Bellard
    else
92 f7d78f36 Måns Rullgård
        av_strlcat(buf, "?", buf_size);
93 51885599 Fabrice Bellard
    vsnprintf(buf1, sizeof(buf1), fmt, ap);
94 f7d78f36 Måns Rullgård
    av_strlcat(buf, buf1, buf_size);
95 51885599 Fabrice Bellard
    va_end(ap);
96
}
97
98 5c91a675 Zdenek Kabelac
static void build_udp_url(char *buf, int buf_size,
99 bb270c08 Diego Biurrun
                          const char *hostname, int port,
100 fc9b22dd Luca Barbato
                          int local_port, int ttl,
101
                          int max_packet_size)
102 51885599 Fabrice Bellard
{
103
    snprintf(buf, buf_size, "udp://%s:%d", hostname, port);
104
    if (local_port >= 0)
105
        url_add_option(buf, buf_size, "localport=%d", local_port);
106
    if (ttl >= 0)
107
        url_add_option(buf, buf_size, "ttl=%d", ttl);
108 fc9b22dd Luca Barbato
    if (max_packet_size >=0)
109
        url_add_option(buf, buf_size, "pkt_size=%d", max_packet_size);
110 51885599 Fabrice Bellard
}
111
112 b4999290 Luca Barbato
/**
113 51885599 Fabrice Bellard
 * url syntax: rtp://host:port[?option=val...]
114 35b74c3d Luca Abeni
 * option: 'ttl=n'       : set the ttl value (for multicast only)
115 51885599 Fabrice Bellard
 *         'localport=n' : set the local port to n
116 fc9b22dd Luca Barbato
 *         'pkt_size=n'  : set max packet size
117 51885599 Fabrice Bellard
 *
118
 */
119 b4999290 Luca Barbato
120 3b50d2ad Fabrice Bellard
static int rtp_open(URLContext *h, const char *uri, int flags)
121
{
122
    RTPContext *s;
123 fc9b22dd Luca Barbato
    int port, is_output, ttl, local_port, max_packet_size;
124 3b50d2ad Fabrice Bellard
    char hostname[256];
125
    char buf[1024];
126
    char path[1024];
127 51885599 Fabrice Bellard
    const char *p;
128 115329f1 Diego Biurrun
129 3b50d2ad Fabrice Bellard
    is_output = (flags & URL_WRONLY);
130
131
    s = av_mallocz(sizeof(RTPContext));
132
    if (!s)
133 8fa36ae0 François Revol
        return AVERROR(ENOMEM);
134 3b50d2ad Fabrice Bellard
    h->priv_data = s;
135 115329f1 Diego Biurrun
136
    url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port,
137 3b50d2ad Fabrice Bellard
              path, sizeof(path), uri);
138 51885599 Fabrice Bellard
    /* extract parameters */
139
    ttl = -1;
140
    local_port = -1;
141 fc9b22dd Luca Barbato
    max_packet_size = -1;
142
143 51885599 Fabrice Bellard
    p = strchr(uri, '?');
144
    if (p) {
145
        if (find_info_tag(buf, sizeof(buf), "ttl", p)) {
146
            ttl = strtol(buf, NULL, 10);
147
        }
148
        if (find_info_tag(buf, sizeof(buf), "localport", p)) {
149
            local_port = strtol(buf, NULL, 10);
150
        }
151 fc9b22dd Luca Barbato
        if (find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
152
            max_packet_size = strtol(buf, NULL, 10);
153
        }
154 51885599 Fabrice Bellard
    }
155 3b50d2ad Fabrice Bellard
156 51885599 Fabrice Bellard
    build_udp_url(buf, sizeof(buf),
157 fc9b22dd Luca Barbato
                  hostname, port, local_port, ttl, max_packet_size);
158 3b50d2ad Fabrice Bellard
    if (url_open(&s->rtp_hd, buf, flags) < 0)
159
        goto fail;
160
    local_port = udp_get_local_port(s->rtp_hd);
161 b484ec78 Panagiotis Issaris
    /* XXX: need to open another connection if the port is not even */
162 3b50d2ad Fabrice Bellard
163
    /* well, should suppress localport in path */
164 115329f1 Diego Biurrun
165 51885599 Fabrice Bellard
    build_udp_url(buf, sizeof(buf),
166 fc9b22dd Luca Barbato
                  hostname, port + 1, local_port + 1, ttl, max_packet_size);
167 3b50d2ad Fabrice Bellard
    if (url_open(&s->rtcp_hd, buf, flags) < 0)
168
        goto fail;
169 115329f1 Diego Biurrun
170 3b50d2ad Fabrice Bellard
    /* just to ease handle access. XXX: need to suppress direct handle
171
       access */
172
    s->rtp_fd = udp_get_file_handle(s->rtp_hd);
173
    s->rtcp_fd = udp_get_file_handle(s->rtcp_hd);
174
175 115329f1 Diego Biurrun
    h->max_packet_size = url_get_max_packet_size(s->rtp_hd);
176 3b50d2ad Fabrice Bellard
    h->is_streamed = 1;
177
    return 0;
178
179
 fail:
180
    if (s->rtp_hd)
181
        url_close(s->rtp_hd);
182
    if (s->rtcp_hd)
183
        url_close(s->rtcp_hd);
184
    av_free(s);
185 6f3e0b21 Panagiotis Issaris
    return AVERROR(EIO);
186 3b50d2ad Fabrice Bellard
}
187
188 0c1a9eda Zdenek Kabelac
static int rtp_read(URLContext *h, uint8_t *buf, int size)
189 3b50d2ad Fabrice Bellard
{
190
    RTPContext *s = h->priv_data;
191
    struct sockaddr_in from;
192 191e8ca7 Måns Rullgård
    socklen_t from_len;
193
    int len, fd_max, n;
194 3b50d2ad Fabrice Bellard
    fd_set rfds;
195
#if 0
196
    for(;;) {
197
        from_len = sizeof(from);
198
        len = recvfrom (s->rtp_fd, buf, size, 0,
199
                        (struct sockaddr *)&from, &from_len);
200
        if (len < 0) {
201 8da4034f Alex Beregszaszi
            if (ff_neterrno() == FF_NETERROR(EAGAIN) ||
202
                ff_neterrno() == FF_NETERROR(EINTR))
203 3b50d2ad Fabrice Bellard
                continue;
204 6f3e0b21 Panagiotis Issaris
            return AVERROR(EIO);
205 3b50d2ad Fabrice Bellard
        }
206
        break;
207
    }
208
#else
209
    for(;;) {
210
        /* build fdset to listen to RTP and RTCP packets */
211
        FD_ZERO(&rfds);
212
        fd_max = s->rtp_fd;
213
        FD_SET(s->rtp_fd, &rfds);
214
        if (s->rtcp_fd > fd_max)
215
            fd_max = s->rtcp_fd;
216
        FD_SET(s->rtcp_fd, &rfds);
217
        n = select(fd_max + 1, &rfds, NULL, NULL, NULL);
218
        if (n > 0) {
219
            /* first try RTCP */
220
            if (FD_ISSET(s->rtcp_fd, &rfds)) {
221
                from_len = sizeof(from);
222
                len = recvfrom (s->rtcp_fd, buf, size, 0,
223
                                (struct sockaddr *)&from, &from_len);
224
                if (len < 0) {
225 8da4034f Alex Beregszaszi
                    if (ff_neterrno() == FF_NETERROR(EAGAIN) ||
226
                        ff_neterrno() == FF_NETERROR(EINTR))
227 3b50d2ad Fabrice Bellard
                        continue;
228 6f3e0b21 Panagiotis Issaris
                    return AVERROR(EIO);
229 3b50d2ad Fabrice Bellard
                }
230
                break;
231
            }
232
            /* then RTP */
233
            if (FD_ISSET(s->rtp_fd, &rfds)) {
234
                from_len = sizeof(from);
235
                len = recvfrom (s->rtp_fd, buf, size, 0,
236
                                (struct sockaddr *)&from, &from_len);
237
                if (len < 0) {
238 8da4034f Alex Beregszaszi
                    if (ff_neterrno() == FF_NETERROR(EAGAIN) ||
239
                        ff_neterrno() == FF_NETERROR(EINTR))
240 3b50d2ad Fabrice Bellard
                        continue;
241 6f3e0b21 Panagiotis Issaris
                    return AVERROR(EIO);
242 3b50d2ad Fabrice Bellard
                }
243
                break;
244
            }
245
        }
246
    }
247
#endif
248
    return len;
249
}
250
251 0c1a9eda Zdenek Kabelac
static int rtp_write(URLContext *h, uint8_t *buf, int size)
252 3b50d2ad Fabrice Bellard
{
253
    RTPContext *s = h->priv_data;
254 51885599 Fabrice Bellard
    int ret;
255 3b50d2ad Fabrice Bellard
    URLContext *hd;
256 115329f1 Diego Biurrun
257 3b50d2ad Fabrice Bellard
    if (buf[1] >= 200 && buf[1] <= 204) {
258
        /* RTCP payload type */
259
        hd = s->rtcp_hd;
260
    } else {
261
        /* RTP payload type */
262
        hd = s->rtp_hd;
263
    }
264
265
    ret = url_write(hd, buf, size);
266
#if 0
267
    {
268
        struct timespec ts;
269
        ts.tv_sec = 0;
270
        ts.tv_nsec = 10 * 1000000;
271
        nanosleep(&ts, NULL);
272
    }
273
#endif
274
    return ret;
275
}
276
277
static int rtp_close(URLContext *h)
278
{
279
    RTPContext *s = h->priv_data;
280
281
    url_close(s->rtp_hd);
282
    url_close(s->rtcp_hd);
283
    av_free(s);
284
    return 0;
285
}
286
287
/**
288 b484ec78 Panagiotis Issaris
 * Return the local port used by the RTP connection
289 3b50d2ad Fabrice Bellard
 * @param s1 media file context
290
 * @return the local port number
291
 */
292 b4999290 Luca Barbato
293 3b50d2ad Fabrice Bellard
int rtp_get_local_port(URLContext *h)
294
{
295
    RTPContext *s = h->priv_data;
296
    return udp_get_local_port(s->rtp_hd);
297
}
298
299
/**
300 b4999290 Luca Barbato
 * Return the rtp and rtcp file handles for select() usage to wait for
301
 * several RTP streams at the same time.
302 3b50d2ad Fabrice Bellard
 * @param h media file context
303
 */
304 b4999290 Luca Barbato
305 3b50d2ad Fabrice Bellard
void rtp_get_file_handles(URLContext *h, int *prtp_fd, int *prtcp_fd)
306
{
307
    RTPContext *s = h->priv_data;
308
309
    *prtp_fd = s->rtp_fd;
310
    *prtcp_fd = s->rtcp_fd;
311
}
312
313
URLProtocol rtp_protocol = {
314
    "rtp",
315
    rtp_open,
316
    rtp_read,
317
    rtp_write,
318
    NULL, /* seek */
319
    rtp_close,
320
};