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
23 * @file libavformat/rtpproto.c
27 #include "libavutil/avstring.h"
33 #include "os_support.h"
36 #include <sys/select.h>
39 #define RTP_TX_BUF_SIZE (64 * 1024)
40 #define RTP_RX_BUF_SIZE (128 * 1024)
43 #include "libavformat_config.h"
46 #ifdef CONFIG_FFMPEG_WITH_FRSH
47 #define URL_PROTO "frsh"
49 #define URL_PROTO "udp"
52 typedef struct RTPContext {
53 URLContext *rtp_hd, *rtcp_hd;
58 * If no filename is given to av_open_input_file because you want to
59 * get the local port first, then you must call this function to set
60 * the remote server address.
62 * @param s1 media file context
63 * @param uri of the remote server
64 * @return zero if no error.
67 int rtp_set_remote_url(URLContext *h, const char *uri)
69 RTPContext *s = h->priv_data;
76 url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port,
77 path, sizeof(path), uri);
79 snprintf(buf, sizeof(buf), URL_PROTO"://%s:%d%s", hostname, port, path);
80 udp_set_remote_url(s->rtp_hd, buf);
82 snprintf(buf, sizeof(buf), URL_PROTO"://%s:%d%s", hostname, port + 1, path);
83 udp_set_remote_url(s->rtcp_hd, buf);
89 * add option to url of the form:
90 * "http://host:port/path?option1=val1&option2=val2...
93 static void url_add_option(char *buf, int buf_size, const char *fmt, ...)
100 av_strlcat(buf, "&", buf_size);
102 av_strlcat(buf, "?", buf_size);
103 vsnprintf(buf1, sizeof(buf1), fmt, ap);
104 av_strlcat(buf, buf1, buf_size);
108 static void build_udp_url(char *buf, int buf_size,
109 const char *hostname, int port,
110 int local_port, int ttl,
114 snprintf(buf, buf_size, URL_PROTO"://%s:%d", hostname, port);
116 url_add_option(buf, buf_size, "localport=%d", local_port);
118 url_add_option(buf, buf_size, "ttl=%d", ttl);
119 if (max_packet_size >=0)
120 url_add_option(buf, buf_size, "pkt_size=%d", max_packet_size);
122 url_add_option(buf, buf_size, "contract_label=%s", label);
126 * url syntax: rtp://host:port[?option=val...]
127 * option: 'ttl=n' : set the ttl value (for multicast only)
128 * 'localport=n' : set the local port to n
129 * 'pkt_size=n' : set max packet size
133 static int rtp_open(URLContext *h, const char *uri, int flags)
136 int port, is_output, ttl, local_port, max_packet_size;
142 is_output = (flags & URL_WRONLY);
144 s = av_mallocz(sizeof(RTPContext));
146 return AVERROR(ENOMEM);
149 url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port,
150 path, sizeof(path), uri);
151 /* extract parameters */
154 max_packet_size = -1;
156 p = strchr(uri, '?');
158 if (find_info_tag(buf, sizeof(buf), "ttl", p)) {
159 ttl = strtol(buf, NULL, 10);
161 if (find_info_tag(buf, sizeof(buf), "localport", p)) {
162 local_port = strtol(buf, NULL, 10);
164 if (find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
165 max_packet_size = strtol(buf, NULL, 10);
169 build_udp_url(buf, sizeof(buf),
170 hostname, port, local_port, ttl, max_packet_size, "RTP");
171 if (url_open(&s->rtp_hd, buf, flags) < 0)
173 local_port = udp_get_local_port(s->rtp_hd);
174 /* XXX: need to open another connection if the port is not even */
176 /* well, should suppress localport in path */
178 build_udp_url(buf, sizeof(buf),
179 hostname, port + 1, local_port + 1, ttl, max_packet_size, "RTCP");
180 if (url_open(&s->rtcp_hd, buf, flags) < 0)
183 /* just to ease handle access. XXX: need to suppress direct handle
185 s->rtp_fd = url_get_file_handle(s->rtp_hd);
186 s->rtcp_fd = url_get_file_handle(s->rtcp_hd);
188 h->max_packet_size = url_get_max_packet_size(s->rtp_hd);
194 url_close(s->rtp_hd);
196 url_close(s->rtcp_hd);
201 static int rtp_read(URLContext *h, uint8_t *buf, int size)
203 RTPContext *s = h->priv_data;
204 struct sockaddr_in from;
210 from_len = sizeof(from);
211 len = recvfrom (s->rtp_fd, buf, size, 0,
212 (struct sockaddr *)&from, &from_len);
214 if (ff_neterrno() == FF_NETERROR(EAGAIN) ||
215 ff_neterrno() == FF_NETERROR(EINTR))
223 /* build fdset to listen to RTP and RTCP packets */
226 FD_SET(s->rtp_fd, &rfds);
227 if (s->rtcp_fd > fd_max)
229 FD_SET(s->rtcp_fd, &rfds);
230 n = select(fd_max + 1, &rfds, NULL, NULL, NULL);
233 if (FD_ISSET(s->rtcp_fd, &rfds)) {
234 from_len = sizeof(from);
235 len = recvfrom (s->rtcp_fd, buf, size, 0,
236 (struct sockaddr *)&from, &from_len);
238 if (ff_neterrno() == FF_NETERROR(EAGAIN) ||
239 ff_neterrno() == FF_NETERROR(EINTR))
246 if (FD_ISSET(s->rtp_fd, &rfds)) {
247 from_len = sizeof(from);
248 len = recvfrom (s->rtp_fd, buf, size, 0,
249 (struct sockaddr *)&from, &from_len);
251 if (ff_neterrno() == FF_NETERROR(EAGAIN) ||
252 ff_neterrno() == FF_NETERROR(EINTR))
264 static int rtp_write(URLContext *h, uint8_t *buf, int size)
266 RTPContext *s = h->priv_data;
270 if (buf[1] >= 200 && buf[1] <= 204) {
271 /* RTCP payload type */
274 /* RTP payload type */
278 ret = url_write(hd, buf, size);
283 ts.tv_nsec = 10 * 1000000;
284 nanosleep(&ts, NULL);
290 static int rtp_close(URLContext *h)
292 RTPContext *s = h->priv_data;
294 url_close(s->rtp_hd);
295 url_close(s->rtcp_hd);
301 * Return the local port used by the RTP connection
302 * @param s1 media file context
303 * @return the local port number
306 int rtp_get_local_port(URLContext *h)
308 RTPContext *s = h->priv_data;
309 return udp_get_local_port(s->rtp_hd);
312 #if (LIBAVFORMAT_VERSION_MAJOR <= 52)
314 * Return the rtp and rtcp file handles for select() usage to wait for
315 * several RTP streams at the same time.
316 * @param h media file context
319 void rtp_get_file_handles(URLContext *h, int *prtp_fd, int *prtcp_fd)
321 RTPContext *s = h->priv_data;
323 *prtp_fd = s->rtp_fd;
324 *prtcp_fd = s->rtcp_fd;
328 static int rtp_get_file_handle(URLContext *h)
330 RTPContext *s = h->priv_data;
334 URLProtocol rtp_protocol = {
341 .url_get_file_handle = rtp_get_file_handle,