3 * Copyright (c) 2002 Fabrice Bellard.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
29 #define RTP_TX_BUF_SIZE (64 * 1024)
30 #define RTP_RX_BUF_SIZE (128 * 1024)
32 typedef struct RTPContext
{
33 URLContext
*rtp_hd
, *rtcp_hd
;
38 * If no filename is given to av_open_input_file because you want to
39 * get the local port first, then you must call this function to set
40 * the remote server address.
42 * url syntax: rtp://host:port[?option=val...]
43 * option: 'multicast=1' : enable multicast
44 * 'ttl=n' : set the ttl value (for multicast only)
46 * @param s1 media file context
47 * @param uri of the remote server
48 * @return zero if no error.
50 int rtp_set_remote_url(URLContext
*h
, const char *uri
)
52 RTPContext
*s
= h
->priv_data
;
58 url_split(NULL
, 0, hostname
, sizeof(hostname
), &port
,
59 path
, sizeof(path
), uri
);
61 snprintf(buf
, sizeof(buf
), "udp://%s:%d%s", hostname
, port
, path
);
62 udp_set_remote_url(s
->rtp_hd
, buf
);
64 snprintf(buf
, sizeof(buf
), "udp://%s:%d%s", hostname
, port
+ 1, path
);
65 udp_set_remote_url(s
->rtcp_hd
, buf
);
70 static int rtp_open(URLContext
*h
, const char *uri
, int flags
)
73 int port
, is_output
, local_port
;
79 is_output
= (flags
& URL_WRONLY
);
81 s
= av_mallocz(sizeof(RTPContext
));
86 url_split(NULL
, 0, hostname
, sizeof(hostname
), &port
,
87 path
, sizeof(path
), uri
);
89 snprintf(buf
, sizeof(buf
), "udp://%s:%d%s",
90 hostname
, port
, path
);
91 if (url_open(&s
->rtp_hd
, buf
, flags
) < 0)
93 local_port
= udp_get_local_port(s
->rtp_hd
);
94 /* XXX: need to open another connexion if the port is not even */
96 /* well, should suppress localport in path */
98 snprintf(buf
, sizeof(buf
), "udp://%s:%d%s%clocalport=%d",
99 hostname
, port
+ 1, path
,
100 strchr(path
, '?') != NULL ?
'&' : '?',
102 if (url_open(&s
->rtcp_hd
, buf
, flags
) < 0)
105 /* just to ease handle access. XXX: need to suppress direct handle
107 s
->rtp_fd
= udp_get_file_handle(s
->rtp_hd
);
108 s
->rtcp_fd
= udp_get_file_handle(s
->rtcp_hd
);
110 h
->max_packet_size
= url_get_max_packet_size(s
->rtp_hd
);
116 url_close(s
->rtp_hd
);
118 url_close(s
->rtcp_hd
);
123 static int rtp_read(URLContext
*h
, UINT8
*buf
, int size
)
125 RTPContext
*s
= h
->priv_data
;
126 struct sockaddr_in from
;
127 int from_len
, len
, fd_max
, n
;
131 from_len
= sizeof(from
);
132 len
= recvfrom (s
->rtp_fd
, buf
, size
, 0,
133 (struct sockaddr
*)&from
, &from_len
);
135 if (errno
== EAGAIN
|| errno
== EINTR
)
143 /* build fdset to listen to RTP and RTCP packets */
146 FD_SET(s
->rtp_fd
, &rfds
);
147 if (s
->rtcp_fd
> fd_max
)
149 FD_SET(s
->rtcp_fd
, &rfds
);
150 n
= select(fd_max
+ 1, &rfds
, NULL
, NULL
, NULL
);
153 if (FD_ISSET(s
->rtcp_fd
, &rfds
)) {
154 from_len
= sizeof(from
);
155 len
= recvfrom (s
->rtcp_fd
, buf
, size
, 0,
156 (struct sockaddr
*)&from
, &from_len
);
158 if (errno
== EAGAIN
|| errno
== EINTR
)
165 if (FD_ISSET(s
->rtp_fd
, &rfds
)) {
166 from_len
= sizeof(from
);
167 len
= recvfrom (s
->rtp_fd
, buf
, size
, 0,
168 (struct sockaddr
*)&from
, &from_len
);
170 if (errno
== EAGAIN
|| errno
== EINTR
)
182 static int rtp_write(URLContext
*h
, UINT8
*buf
, int size
)
184 RTPContext
*s
= h
->priv_data
;
188 if (buf
[1] >= 200 && buf
[1] <= 204) {
189 /* RTCP payload type */
192 /* RTP payload type */
196 ret
= url_write(hd
, buf
, size
);
201 ts
.tv_nsec
= 10 * 1000000;
202 nanosleep(&ts
, NULL
);
208 static int rtp_close(URLContext
*h
)
210 RTPContext
*s
= h
->priv_data
;
212 url_close(s
->rtp_hd
);
213 url_close(s
->rtcp_hd
);
219 * Return the local port used by the RTP connexion
220 * @param s1 media file context
221 * @return the local port number
223 int rtp_get_local_port(URLContext
*h
)
225 RTPContext
*s
= h
->priv_data
;
226 return udp_get_local_port(s
->rtp_hd
);
230 * Return the rtp and rtcp file handles for select() usage to wait for several RTP
231 * streams at the same time.
232 * @param h media file context
234 void rtp_get_file_handles(URLContext
*h
, int *prtp_fd
, int *prtcp_fd
)
236 RTPContext
*s
= h
->priv_data
;
238 *prtp_fd
= s
->rtp_fd
;
239 *prtcp_fd
= s
->rtcp_fd
;
242 URLProtocol rtp_protocol
= {