3 * Copyright (c) 2002 Fabrice Bellard.
5 * This file is part of FFmpeg.
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.
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.
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
27 #include "libavutil/avstring.h"
33 #include "os_support.h"
35 #include <sys/select.h>
37 #define RTP_TX_BUF_SIZE (64 * 1024)
38 #define RTP_RX_BUF_SIZE (128 * 1024)
40 typedef struct RTPContext {
41 URLContext *rtp_hd, *rtcp_hd;
46 * If no filename is given to av_open_input_file because you want to
47 * get the local port first, then you must call this function to set
48 * the remote server address.
50 * @param s1 media file context
51 * @param uri of the remote server
52 * @return zero if no error.
55 int rtp_set_remote_url(URLContext *h, const char *uri)
57 RTPContext *s = h->priv_data;
64 url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port,
65 path, sizeof(path), uri);
67 snprintf(buf, sizeof(buf), "udp://%s:%d%s", hostname, port, path);
68 udp_set_remote_url(s->rtp_hd, buf);
70 snprintf(buf, sizeof(buf), "udp://%s:%d%s", hostname, port + 1, path);
71 udp_set_remote_url(s->rtcp_hd, buf);
77 * add option to url of the form:
78 * "http://host:port/path?option1=val1&option2=val2...
81 static void url_add_option(char *buf, int buf_size, const char *fmt, ...)
88 av_strlcat(buf, "&", buf_size);
90 av_strlcat(buf, "?", buf_size);
91 vsnprintf(buf1, sizeof(buf1), fmt, ap);
92 av_strlcat(buf, buf1, buf_size);
96 static void build_udp_url(char *buf, int buf_size,
97 const char *hostname, int port,
98 int local_port, int ttl,
101 snprintf(buf, buf_size, "udp://%s:%d", hostname, port);
103 url_add_option(buf, buf_size, "localport=%d", local_port);
105 url_add_option(buf, buf_size, "ttl=%d", ttl);
106 if (max_packet_size >=0)
107 url_add_option(buf, buf_size, "pkt_size=%d", max_packet_size);
111 * url syntax: rtp://host:port[?option=val...]
112 * option: 'ttl=n' : set the ttl value (for multicast only)
113 * 'localport=n' : set the local port to n
114 * 'pkt_size=n' : set max packet size
118 static int rtp_open(URLContext *h, const char *uri, int flags)
121 int port, is_output, ttl, local_port, max_packet_size;
127 is_output = (flags & URL_WRONLY);
129 s = av_mallocz(sizeof(RTPContext));
131 return AVERROR(ENOMEM);
134 url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port,
135 path, sizeof(path), uri);
136 /* extract parameters */
139 max_packet_size = -1;
141 p = strchr(uri, '?');
143 if (find_info_tag(buf, sizeof(buf), "ttl", p)) {
144 ttl = strtol(buf, NULL, 10);
146 if (find_info_tag(buf, sizeof(buf), "localport", p)) {
147 local_port = strtol(buf, NULL, 10);
149 if (find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
150 max_packet_size = strtol(buf, NULL, 10);
154 build_udp_url(buf, sizeof(buf),
155 hostname, port, local_port, ttl, max_packet_size);
156 if (url_open(&s->rtp_hd, buf, flags) < 0)
158 local_port = udp_get_local_port(s->rtp_hd);
159 /* XXX: need to open another connection if the port is not even */
161 /* well, should suppress localport in path */
163 build_udp_url(buf, sizeof(buf),
164 hostname, port + 1, local_port + 1, ttl, max_packet_size);
165 if (url_open(&s->rtcp_hd, buf, flags) < 0)
168 /* just to ease handle access. XXX: need to suppress direct handle
170 s->rtp_fd = udp_get_file_handle(s->rtp_hd);
171 s->rtcp_fd = udp_get_file_handle(s->rtcp_hd);
173 h->max_packet_size = url_get_max_packet_size(s->rtp_hd);
179 url_close(s->rtp_hd);
181 url_close(s->rtcp_hd);
186 static int rtp_read(URLContext *h, uint8_t *buf, int size)
188 RTPContext *s = h->priv_data;
189 struct sockaddr_in from;
195 from_len = sizeof(from);
196 len = recvfrom (s->rtp_fd, buf, size, 0,
197 (struct sockaddr *)&from, &from_len);
199 if (ff_neterrno() == FF_NETERROR(EAGAIN) ||
200 ff_neterrno() == FF_NETERROR(EINTR))
208 /* build fdset to listen to RTP and RTCP packets */
211 FD_SET(s->rtp_fd, &rfds);
212 if (s->rtcp_fd > fd_max)
214 FD_SET(s->rtcp_fd, &rfds);
215 n = select(fd_max + 1, &rfds, NULL, NULL, NULL);
218 if (FD_ISSET(s->rtcp_fd, &rfds)) {
219 from_len = sizeof(from);
220 len = recvfrom (s->rtcp_fd, buf, size, 0,
221 (struct sockaddr *)&from, &from_len);
223 if (ff_neterrno() == FF_NETERROR(EAGAIN) ||
224 ff_neterrno() == FF_NETERROR(EINTR))
231 if (FD_ISSET(s->rtp_fd, &rfds)) {
232 from_len = sizeof(from);
233 len = recvfrom (s->rtp_fd, buf, size, 0,
234 (struct sockaddr *)&from, &from_len);
236 if (ff_neterrno() == FF_NETERROR(EAGAIN) ||
237 ff_neterrno() == FF_NETERROR(EINTR))
249 static int rtp_write(URLContext *h, uint8_t *buf, int size)
251 RTPContext *s = h->priv_data;
255 if (buf[1] >= 200 && buf[1] <= 204) {
256 /* RTCP payload type */
259 /* RTP payload type */
263 ret = url_write(hd, buf, size);
268 ts.tv_nsec = 10 * 1000000;
269 nanosleep(&ts, NULL);
275 static int rtp_close(URLContext *h)
277 RTPContext *s = h->priv_data;
279 url_close(s->rtp_hd);
280 url_close(s->rtcp_hd);
286 * Return the local port used by the RTP connection
287 * @param s1 media file context
288 * @return the local port number
291 int rtp_get_local_port(URLContext *h)
293 RTPContext *s = h->priv_data;
294 return udp_get_local_port(s->rtp_hd);
298 * Return the rtp and rtcp file handles for select() usage to wait for
299 * several RTP streams at the same time.
300 * @param h media file context
303 void rtp_get_file_handles(URLContext *h, int *prtp_fd, int *prtcp_fd)
305 RTPContext *s = h->priv_data;
307 *prtp_fd = s->rtp_fd;
308 *prtcp_fd = s->rtcp_fd;
311 URLProtocol rtp_protocol = {