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
24 * RTP protocol
25 */
26
34
35 #include <stdarg.h>
39 #include <fcntl.h>
40 #if HAVE_POLL_H
41 #include <sys/poll.h>
42 #endif
43
52
53 /**
54 * If no filename is given to av_open_input_file because you want to
55 * get the local port first, then you must call this function to set
56 * the remote server address.
57 *
58 * @param h media file context
59 * @param uri of the remote server
60 * @return zero if no error.
61 */
62
64 {
66 char hostname[256];
67 int port, rtcp_port;
68 const char *p;
69
71 char path[1024];
72
74 path, sizeof(path), uri);
75 rtcp_port = port + 1;
76
77 p = strchr(uri, '?');
78 if (p) {
80 rtcp_port = strtol(buf,
NULL, 10);
81 }
82 }
83
86
87 ff_url_join(buf,
sizeof(buf),
"udp",
NULL, hostname, rtcp_port,
"%s", path);
89 return 0;
90 }
91
94 {
95 struct addrinfo hints = { 0 }, *res = 0;
96 int error;
97 char service[16];
98
99 snprintf(service,
sizeof(service),
"%d", port);
103 if ((error =
getaddrinfo(hostname, service, &hints, &res))) {
106 }
107
108 return res;
109 }
110
113 {
115 return 1;
117 return (((const struct sockaddr_in *)a)->sin_addr.s_addr !=
118 ((const struct sockaddr_in *)b)->sin_addr.s_addr);
119 }
120
121 #if HAVE_STRUCT_SOCKADDR_IN6
123 const uint8_t *s6_addr_a = ((
const struct sockaddr_in6 *)a)->sin6_addr.s6_addr;
124 const uint8_t *s6_addr_b = ((
const struct sockaddr_in6 *)b)->sin6_addr.s6_addr;
125 return memcmp(s6_addr_a, s6_addr_b, 16);
126 }
127 #endif
128 return 1;
129 }
130
132 {
134 return ntohs(((const struct sockaddr_in *)ss)->sin_port);
135 #if HAVE_STRUCT_SOCKADDR_IN6
137 return ntohs(((const struct sockaddr_in6 *)ss)->sin6_port);
138 #endif
139 return 0;
140 }
141
143 {
145 ((struct sockaddr_in *)ss)->sin_port = htons(port);
146 #if HAVE_STRUCT_SOCKADDR_IN6
147 else if (ss->ss_family == AF_INET6)
148 ((struct sockaddr_in6 *)ss)->sin6_port = htons(port);
149 #endif
150 }
151
153 {
154 int i;
158 return 1;
159 }
160 }
164 return 0;
165 }
166 return 1;
167 }
168 return 0;
169 }
170
171 /**
172 * add option to url of the form:
173 * "http://host:port/path?option1=val1&option2=val2...
174 */
175
177 {
178 char buf1[1024];
179 va_list ap;
180
181 va_start(ap, fmt);
182 if (strchr(buf, '?'))
184 else
188 va_end(ap);
189 }
190
192 const char *hostname, int port,
193 int local_port, int ttl,
194 int max_packet_size, int connect,
195 int dscp,
196 const char *include_sources,
197 const char *exclude_sources)
198 {
200 if (local_port >= 0)
201 url_add_option(buf, buf_size, "localport=%d", local_port);
202 if (ttl >= 0)
203 url_add_option(buf, buf_size, "ttl=%d", ttl);
204 if (max_packet_size >=0)
205 url_add_option(buf, buf_size, "pkt_size=%d", max_packet_size);
206 if (connect)
207 url_add_option(buf, buf_size, "connect=1");
208 if (dscp >= 0)
209 url_add_option(buf, buf_size, "dscp=%d", dscp);
210 url_add_option(buf, buf_size, "fifo_size=0");
211 if (include_sources && include_sources[0])
212 url_add_option(buf, buf_size, "sources=%s", include_sources);
213 if (exclude_sources && exclude_sources[0])
214 url_add_option(buf, buf_size, "block=%s", exclude_sources);
215 }
216
219 int *address_list_size_ptr)
220 {
223 char tmp =
'0円', *p =
buf, *next;
224
225 /* Resolve all of the IPs */
226
227 while (p && p[0]) {
228 next = strchr(p, ',');
229
230 if (next) {
231 tmp = *next;
232 *next = '0円';
233 }
234
236 if (ai) {
238 if (!source_addr) {
240 break;
241 }
242
245 dynarray_add(address_list_ptr, address_list_size_ptr, source_addr);
246 } else {
248 }
249
250 if (next) {
251 *next = tmp;
252 p = next + 1;
253 } else {
255 }
256 }
257 }
258
259 /**
260 * url syntax: rtp://host:port[?option=val...]
261 * option: 'ttl=n' : set the ttl value (for multicast only)
262 * 'rtcpport=n' : set the remote rtcp port to n
263 * 'localrtpport=n' : set the local rtp port to n
264 * 'localrtcpport=n' : set the local rtcp port to n
265 * 'pkt_size=n' : set max packet size
266 * 'connect=0/1' : do a connect() on the UDP socket
267 * 'sources=ip[,ip]' : list allowed source IP addresses
268 * 'block=ip[,ip]' : list disallowed source IP addresses
269 * 'write_to_source=0/1' : send packets to the source address of the latest received packet
270 * 'dscp=n' : set DSCP value to n (QoS)
271 * deprecated option:
272 * 'localport=n' : set the local port to n
273 *
274 * if rtcpport isn't set the rtcp port will be the rtp port + 1
275 * if local rtp port isn't set any available port will be used for the local
276 * rtp and rtcp ports
277 * if the local rtcp port is not set it will be the local rtp port + 1
278 */
279
281 {
283 int rtp_port, rtcp_port,
284 ttl, connect,
285 local_rtp_port, local_rtcp_port, max_packet_size, dscp;
286 char hostname[256], include_sources[1024] = "", exclude_sources[1024] = "";
288 char path[1024];
289 const char *p;
290 int i, max_retry_count = 3;
291
293 path, sizeof(path), uri);
294 /* extract parameters */
295 ttl = -1;
296 rtcp_port = rtp_port+1;
297 local_rtp_port = -1;
298 local_rtcp_port = -1;
299 max_packet_size = -1;
300 connect = 0;
301 dscp = -1;
302
303 p = strchr(uri, '?');
304 if (p) {
306 ttl = strtol(buf,
NULL, 10);
307 }
309 rtcp_port = strtol(buf,
NULL, 10);
310 }
312 local_rtp_port = strtol(buf,
NULL, 10);
313 }
315 local_rtp_port = strtol(buf,
NULL, 10);
316 }
318 local_rtcp_port = strtol(buf,
NULL, 10);
319 }
321 max_packet_size = strtol(buf,
NULL, 10);
322 }
324 connect = strtol(buf,
NULL, 10);
325 }
328 }
330 dscp = strtol(buf,
NULL, 10);
331 }
333 av_strlcpy(include_sources, buf,
sizeof(include_sources));
335 }
337 av_strlcpy(exclude_sources, buf,
sizeof(exclude_sources));
339 }
340 }
341
342 for (i = 0;i < max_retry_count;i++) {
344 hostname, rtp_port, local_rtp_port, ttl, max_packet_size,
345 connect, dscp, include_sources, exclude_sources);
347 goto fail;
349 if(local_rtp_port == 65535) {
350 local_rtp_port = -1;
351 continue;
352 }
353 if (local_rtcp_port<0) {
354 local_rtcp_port = local_rtp_port + 1;
356 hostname, rtcp_port, local_rtcp_port, ttl, max_packet_size,
357 connect, dscp, include_sources, exclude_sources);
359 local_rtp_port = local_rtcp_port = -1;
360 continue;
361 }
362 break;
363 }
365 hostname, rtcp_port, local_rtcp_port, ttl, max_packet_size,
366 connect, dscp, include_sources, exclude_sources);
368 goto fail;
369 break;
370 }
371
372 /* just to ease handle access. XXX: need to suppress direct handle
373 access */
376
379 return 0;
380
381 fail:
387 }
388
390 {
393 struct pollfd p[2] = {{s->
rtp_fd, POLLIN, 0}, {s->
rtcp_fd, POLLIN, 0}};
397
398 for(;;) {
401 n = poll(p, 2, poll_delay);
402 if (n > 0) {
403 /* first try RTCP, then RTP */
404 for (i = 1; i >= 0; i--) {
405 if (!(p[i].revents & POLLIN))
406 continue;
407 *addr_lens[i] = sizeof(*addrs[i]);
408 len = recvfrom(p[i].fd, buf, size, 0,
409 (struct sockaddr *)addrs[i], addr_lens[i]);
410 if (len < 0) {
413 continue;
415 }
417 continue;
419 }
420 } else if (n < 0) {
422 continue;
424 }
427 }
429 }
430
432 {
436
437 if (size < 2)
439
442 "make sure the RTP muxer is used\n");
443
445 int fd;
447 socklen_t *source_len, temp_len;
450 "Unable to send packet to source, no packets received yet\n");
451 // Intentionally not returning an error here
453 }
454
459 } else {
461 source = &s->last_rtp_source;
463 }
465 source = &temp_source;
466 source_len = &temp_len;
468 temp_source = s->last_rtp_source;
472 "Not received any RTCP packets yet, inferring peer port "
473 "from the RTP port\n");
474 } else {
479 "Not received any RTP packets yet, inferring peer port "
480 "from the RTCP port\n");
481 }
482 }
483
486 if (ret < 0)
488 }
489 ret = sendto(fd, buf, size, 0, (struct sockaddr *) source,
490 *source_len);
491
493 }
494
496 /* RTCP payload type */
498 } else {
499 /* RTP payload type */
501 }
502
505 }
506
508 {
510 int i;
511
518
521 return 0;
522 }
523
524 /**
525 * Return the local rtp port used by the RTP connection
526 * @param h media file context
527 * @return the local port number
528 */
529
531 {
534 }
535
536 /**
537 * Return the local rtcp port used by the RTP connection
538 * @param h media file context
539 * @return the local port number
540 */
541
543 {
546 }
547
549 {
552 }
553
555 int *numhandles)
556 {
558 int *hs = *handles =
av_malloc(
sizeof(**handles) * 2);
559 if (!hs)
563 *numhandles = 2;
564 return 0;
565 }
566
577 };