Statistics
| Branch: | Revision:

ffmpeg / libavformat / rtpproto.c @ 831ec935

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