Statistics
| Branch: | Revision:

ffmpeg / libavformat / rtpproto.c @ bb54f6ab

History | View | Annotate | Download (8.03 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
#include "avformat.h"
22
23
#include <unistd.h>
24 51885599 Fabrice Bellard
#include <stdarg.h>
25 3b50d2ad Fabrice Bellard
#include <sys/types.h>
26 b8a78f41 Michael Niedermayer
#include <sys/socket.h>
27 3b50d2ad Fabrice Bellard
#include <netinet/in.h>
28 9ddd71fc François Revol
#ifndef __BEOS__
29
# include <arpa/inet.h>
30
#else
31
# include "barpainet.h"
32
#endif
33 3b50d2ad Fabrice Bellard
#include <netdb.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
int rtp_set_remote_url(URLContext *h, const char *uri)
54
{
55
    RTPContext *s = h->priv_data;
56
    char hostname[256];
57
    int port;
58 51885599 Fabrice Bellard
59 3b50d2ad Fabrice Bellard
    char buf[1024];
60
    char path[1024];
61 115329f1 Diego Biurrun
62
    url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port,
63 3b50d2ad Fabrice Bellard
              path, sizeof(path), uri);
64
65
    snprintf(buf, sizeof(buf), "udp://%s:%d%s", hostname, port, path);
66
    udp_set_remote_url(s->rtp_hd, buf);
67
68
    snprintf(buf, sizeof(buf), "udp://%s:%d%s", hostname, port + 1, path);
69
    udp_set_remote_url(s->rtcp_hd, buf);
70
    return 0;
71
}
72
73
74 51885599 Fabrice Bellard
/* add option to url of the form:
75
   "http://host:port/path?option1=val1&option2=val2... */
76 5c91a675 Zdenek Kabelac
static void url_add_option(char *buf, int buf_size, const char *fmt, ...)
77 51885599 Fabrice Bellard
{
78
    char buf1[1024];
79
    va_list ap;
80
81
    va_start(ap, fmt);
82
    if (strchr(buf, '?'))
83
        pstrcat(buf, buf_size, "&");
84
    else
85
        pstrcat(buf, buf_size, "?");
86
    vsnprintf(buf1, sizeof(buf1), fmt, ap);
87
    pstrcat(buf, buf_size, buf1);
88
    va_end(ap);
89
}
90
91 5c91a675 Zdenek Kabelac
static void build_udp_url(char *buf, int buf_size,
92 bb270c08 Diego Biurrun
                          const char *hostname, int port,
93
                          int local_port, int multicast, int ttl)
94 51885599 Fabrice Bellard
{
95
    snprintf(buf, buf_size, "udp://%s:%d", hostname, port);
96
    if (local_port >= 0)
97
        url_add_option(buf, buf_size, "localport=%d", local_port);
98
    if (multicast)
99
        url_add_option(buf, buf_size, "multicast=1", multicast);
100
    if (ttl >= 0)
101
        url_add_option(buf, buf_size, "ttl=%d", ttl);
102
}
103
104
/*
105
 * url syntax: rtp://host:port[?option=val...]
106 115329f1 Diego Biurrun
 * option: 'multicast=1' : enable multicast
107 51885599 Fabrice Bellard
 *         'ttl=n'       : set the ttl value (for multicast only)
108
 *         'localport=n' : set the local port to n
109
 *
110
 */
111 3b50d2ad Fabrice Bellard
static int rtp_open(URLContext *h, const char *uri, int flags)
112
{
113
    RTPContext *s;
114 51885599 Fabrice Bellard
    int port, is_output, is_multicast, ttl, local_port;
115 3b50d2ad Fabrice Bellard
    char hostname[256];
116
    char buf[1024];
117
    char path[1024];
118 51885599 Fabrice Bellard
    const char *p;
119 115329f1 Diego Biurrun
120 3b50d2ad Fabrice Bellard
    is_output = (flags & URL_WRONLY);
121
122
    s = av_mallocz(sizeof(RTPContext));
123
    if (!s)
124
        return -ENOMEM;
125
    h->priv_data = s;
126 115329f1 Diego Biurrun
127
    url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port,
128 3b50d2ad Fabrice Bellard
              path, sizeof(path), uri);
129 51885599 Fabrice Bellard
    /* extract parameters */
130
    is_multicast = 0;
131
    ttl = -1;
132
    local_port = -1;
133
    p = strchr(uri, '?');
134
    if (p) {
135
        is_multicast = find_info_tag(buf, sizeof(buf), "multicast", p);
136
        if (find_info_tag(buf, sizeof(buf), "ttl", p)) {
137
            ttl = strtol(buf, NULL, 10);
138
        }
139
        if (find_info_tag(buf, sizeof(buf), "localport", p)) {
140
            local_port = strtol(buf, NULL, 10);
141
        }
142
    }
143 3b50d2ad Fabrice Bellard
144 51885599 Fabrice Bellard
    build_udp_url(buf, sizeof(buf),
145
                  hostname, port, local_port, is_multicast, ttl);
146 3b50d2ad Fabrice Bellard
    if (url_open(&s->rtp_hd, buf, flags) < 0)
147
        goto fail;
148
    local_port = udp_get_local_port(s->rtp_hd);
149
    /* XXX: need to open another connexion if the port is not even */
150
151
    /* well, should suppress localport in path */
152 115329f1 Diego Biurrun
153 51885599 Fabrice Bellard
    build_udp_url(buf, sizeof(buf),
154
                  hostname, port + 1, local_port + 1, is_multicast, ttl);
155 3b50d2ad Fabrice Bellard
    if (url_open(&s->rtcp_hd, buf, flags) < 0)
156
        goto fail;
157 115329f1 Diego Biurrun
158 3b50d2ad Fabrice Bellard
    /* just to ease handle access. XXX: need to suppress direct handle
159
       access */
160
    s->rtp_fd = udp_get_file_handle(s->rtp_hd);
161
    s->rtcp_fd = udp_get_file_handle(s->rtcp_hd);
162
163 115329f1 Diego Biurrun
    h->max_packet_size = url_get_max_packet_size(s->rtp_hd);
164 3b50d2ad Fabrice Bellard
    h->is_streamed = 1;
165
    return 0;
166
167
 fail:
168
    if (s->rtp_hd)
169
        url_close(s->rtp_hd);
170
    if (s->rtcp_hd)
171
        url_close(s->rtcp_hd);
172
    av_free(s);
173 0bd586c5 Mike Melanson
    return AVERROR_IO;
174 3b50d2ad Fabrice Bellard
}
175
176 0c1a9eda Zdenek Kabelac
static int rtp_read(URLContext *h, uint8_t *buf, int size)
177 3b50d2ad Fabrice Bellard
{
178
    RTPContext *s = h->priv_data;
179
    struct sockaddr_in from;
180 191e8ca7 Måns Rullgård
    socklen_t from_len;
181
    int len, fd_max, n;
182 3b50d2ad Fabrice Bellard
    fd_set rfds;
183
#if 0
184
    for(;;) {
185
        from_len = sizeof(from);
186
        len = recvfrom (s->rtp_fd, buf, size, 0,
187
                        (struct sockaddr *)&from, &from_len);
188
        if (len < 0) {
189
            if (errno == EAGAIN || errno == EINTR)
190
                continue;
191 0bd586c5 Mike Melanson
            return AVERROR_IO;
192 3b50d2ad Fabrice Bellard
        }
193
        break;
194
    }
195
#else
196
    for(;;) {
197
        /* build fdset to listen to RTP and RTCP packets */
198
        FD_ZERO(&rfds);
199
        fd_max = s->rtp_fd;
200
        FD_SET(s->rtp_fd, &rfds);
201
        if (s->rtcp_fd > fd_max)
202
            fd_max = s->rtcp_fd;
203
        FD_SET(s->rtcp_fd, &rfds);
204
        n = select(fd_max + 1, &rfds, NULL, NULL, NULL);
205
        if (n > 0) {
206
            /* first try RTCP */
207
            if (FD_ISSET(s->rtcp_fd, &rfds)) {
208
                from_len = sizeof(from);
209
                len = recvfrom (s->rtcp_fd, buf, size, 0,
210
                                (struct sockaddr *)&from, &from_len);
211
                if (len < 0) {
212
                    if (errno == EAGAIN || errno == EINTR)
213
                        continue;
214 0bd586c5 Mike Melanson
                    return AVERROR_IO;
215 3b50d2ad Fabrice Bellard
                }
216
                break;
217
            }
218
            /* then RTP */
219
            if (FD_ISSET(s->rtp_fd, &rfds)) {
220
                from_len = sizeof(from);
221
                len = recvfrom (s->rtp_fd, buf, size, 0,
222
                                (struct sockaddr *)&from, &from_len);
223
                if (len < 0) {
224
                    if (errno == EAGAIN || errno == EINTR)
225
                        continue;
226 0bd586c5 Mike Melanson
                    return AVERROR_IO;
227 3b50d2ad Fabrice Bellard
                }
228
                break;
229
            }
230
        }
231
    }
232
#endif
233
    return len;
234
}
235
236 0c1a9eda Zdenek Kabelac
static int rtp_write(URLContext *h, uint8_t *buf, int size)
237 3b50d2ad Fabrice Bellard
{
238
    RTPContext *s = h->priv_data;
239 51885599 Fabrice Bellard
    int ret;
240 3b50d2ad Fabrice Bellard
    URLContext *hd;
241 115329f1 Diego Biurrun
242 3b50d2ad Fabrice Bellard
    if (buf[1] >= 200 && buf[1] <= 204) {
243
        /* RTCP payload type */
244
        hd = s->rtcp_hd;
245
    } else {
246
        /* RTP payload type */
247
        hd = s->rtp_hd;
248
    }
249
250
    ret = url_write(hd, buf, size);
251
#if 0
252
    {
253
        struct timespec ts;
254
        ts.tv_sec = 0;
255
        ts.tv_nsec = 10 * 1000000;
256
        nanosleep(&ts, NULL);
257
    }
258
#endif
259
    return ret;
260
}
261
262
static int rtp_close(URLContext *h)
263
{
264
    RTPContext *s = h->priv_data;
265
266
    url_close(s->rtp_hd);
267
    url_close(s->rtcp_hd);
268
    av_free(s);
269
    return 0;
270
}
271
272
/**
273
 * Return the local port used by the RTP connexion
274
 * @param s1 media file context
275
 * @return the local port number
276
 */
277
int rtp_get_local_port(URLContext *h)
278
{
279
    RTPContext *s = h->priv_data;
280
    return udp_get_local_port(s->rtp_hd);
281
}
282
283
/**
284
 * Return the rtp and rtcp file handles for select() usage to wait for several RTP
285
 * streams at the same time.
286
 * @param h media file context
287
 */
288
void rtp_get_file_handles(URLContext *h, int *prtp_fd, int *prtcp_fd)
289
{
290
    RTPContext *s = h->priv_data;
291
292
    *prtp_fd = s->rtp_fd;
293
    *prtcp_fd = s->rtcp_fd;
294
}
295
296
URLProtocol rtp_protocol = {
297
    "rtp",
298
    rtp_open,
299
    rtp_read,
300
    rtp_write,
301
    NULL, /* seek */
302
    rtp_close,
303
};