Statistics
| Branch: | Revision:

ffmpeg / libavformat / rtpproto.c @ 831ec935

History | View | Annotate | Download (8.32 KB)

1
/*
2
 * RTP network protocol
3
 * Copyright (c) 2002 Fabrice Bellard.
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
/**
23
 * @file rtpproto.c
24
 * RTP protocol
25
 */
26

    
27
#include "libavutil/avstring.h"
28
#include "avformat.h"
29

    
30
#include <unistd.h>
31
#include <stdarg.h>
32
#include "network.h"
33
#include "os_support.h"
34
#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

    
54
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

    
60
    char buf[1024];
61
    char path[1024];
62

    
63
    url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port,
64
              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
/**
76
 * add option to url of the form:
77
 * "http://host:port/path?option1=val1&option2=val2...
78
 */
79

    
80
static void url_add_option(char *buf, int buf_size, const char *fmt, ...)
81
{
82
    char buf1[1024];
83
    va_list ap;
84

    
85
    va_start(ap, fmt);
86
    if (strchr(buf, '?'))
87
        av_strlcat(buf, "&", buf_size);
88
    else
89
        av_strlcat(buf, "?", buf_size);
90
    vsnprintf(buf1, sizeof(buf1), fmt, ap);
91
    av_strlcat(buf, buf1, buf_size);
92
    va_end(ap);
93
}
94

    
95
static void build_udp_url(char *buf, int buf_size,
96
                          const char *hostname, int port,
97
                          int local_port, int ttl,
98
                          int max_packet_size)
99
{
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
    if (max_packet_size >=0)
106
        url_add_option(buf, buf_size, "pkt_size=%d", max_packet_size);
107
}
108

    
109
/**
110
 * url syntax: rtp://host:port[?option=val...]
111
 * option: 'ttl=n'       : set the ttl value (for multicast only)
112
 *         'localport=n' : set the local port to n
113
 *         'pkt_size=n'  : set max packet size
114
 *
115
 */
116

    
117
static int rtp_open(URLContext *h, const char *uri, int flags)
118
{
119
    RTPContext *s;
120
    int port, is_output, ttl, local_port, max_packet_size;
121
    char hostname[256];
122
    char buf[1024];
123
    char path[1024];
124
    const char *p;
125

    
126
    is_output = (flags & URL_WRONLY);
127

    
128
    s = av_mallocz(sizeof(RTPContext));
129
    if (!s)
130
        return AVERROR(ENOMEM);
131
    h->priv_data = s;
132

    
133
    url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port,
134
              path, sizeof(path), uri);
135
    /* extract parameters */
136
    ttl = -1;
137
    local_port = -1;
138
    max_packet_size = -1;
139

    
140
    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
        if (find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
149
            max_packet_size = strtol(buf, NULL, 10);
150
        }
151
    }
152

    
153
    build_udp_url(buf, sizeof(buf),
154
                  hostname, port, local_port, ttl, max_packet_size);
155
    if (url_open(&s->rtp_hd, buf, flags) < 0)
156
        goto fail;
157
    local_port = udp_get_local_port(s->rtp_hd);
158
    /* XXX: need to open another connection if the port is not even */
159

    
160
    /* well, should suppress localport in path */
161

    
162
    build_udp_url(buf, sizeof(buf),
163
                  hostname, port + 1, local_port + 1, ttl, max_packet_size);
164
    if (url_open(&s->rtcp_hd, buf, flags) < 0)
165
        goto fail;
166

    
167
    /* 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
    h->max_packet_size = url_get_max_packet_size(s->rtp_hd);
173
    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
    return AVERROR(EIO);
183
}
184

    
185
static int rtp_read(URLContext *h, uint8_t *buf, int size)
186
{
187
    RTPContext *s = h->priv_data;
188
    struct sockaddr_in from;
189
    socklen_t from_len;
190
    int len, fd_max, n;
191
    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
            if (ff_neterrno() == FF_NETERROR(EAGAIN) ||
199
                ff_neterrno() == FF_NETERROR(EINTR))
200
                continue;
201
            return AVERROR(EIO);
202
        }
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
                    if (ff_neterrno() == FF_NETERROR(EAGAIN) ||
223
                        ff_neterrno() == FF_NETERROR(EINTR))
224
                        continue;
225
                    return AVERROR(EIO);
226
                }
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
                    if (ff_neterrno() == FF_NETERROR(EAGAIN) ||
236
                        ff_neterrno() == FF_NETERROR(EINTR))
237
                        continue;
238
                    return AVERROR(EIO);
239
                }
240
                break;
241
            }
242
        }
243
    }
244
#endif
245
    return len;
246
}
247

    
248
static int rtp_write(URLContext *h, uint8_t *buf, int size)
249
{
250
    RTPContext *s = h->priv_data;
251
    int ret;
252
    URLContext *hd;
253

    
254
    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
 * Return the local port used by the RTP connection
286
 * @param s1 media file context
287
 * @return the local port number
288
 */
289

    
290
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
 * Return the rtp and rtcp file handles for select() usage to wait for
298
 * several RTP streams at the same time.
299
 * @param h media file context
300
 */
301

    
302
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
};