rtpproto.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. /*
  2. * RTP network protocol
  3. * Copyright (c) 2002 Fabrice Bellard
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * RTP protocol
  24. */
  25. #include "libavutil/parseutils.h"
  26. #include "libavutil/avstring.h"
  27. #include "avformat.h"
  28. #include "avio_internal.h"
  29. #include "rtp.h"
  30. #include "rtpproto.h"
  31. #include "url.h"
  32. #include <stdarg.h>
  33. #include "internal.h"
  34. #include "network.h"
  35. #include "os_support.h"
  36. #include <fcntl.h>
  37. #if HAVE_POLL_H
  38. #include <sys/poll.h>
  39. #endif
  40. typedef struct RTPContext {
  41. URLContext *rtp_hd, *rtcp_hd;
  42. int rtp_fd, rtcp_fd, nb_ssm_include_addrs, nb_ssm_exclude_addrs;
  43. struct sockaddr_storage **ssm_include_addrs, **ssm_exclude_addrs;
  44. int write_to_source;
  45. struct sockaddr_storage last_rtp_source, last_rtcp_source;
  46. socklen_t last_rtp_source_len, last_rtcp_source_len;
  47. } RTPContext;
  48. /**
  49. * If no filename is given to av_open_input_file because you want to
  50. * get the local port first, then you must call this function to set
  51. * the remote server address.
  52. *
  53. * @param h media file context
  54. * @param uri of the remote server
  55. * @return zero if no error.
  56. */
  57. int ff_rtp_set_remote_url(URLContext *h, const char *uri)
  58. {
  59. RTPContext *s = h->priv_data;
  60. char hostname[256];
  61. int port, rtcp_port;
  62. const char *p;
  63. char buf[1024];
  64. char path[1024];
  65. av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port,
  66. path, sizeof(path), uri);
  67. rtcp_port = port + 1;
  68. p = strchr(uri, '?');
  69. if (p) {
  70. if (av_find_info_tag(buf, sizeof(buf), "rtcpport", p)) {
  71. rtcp_port = strtol(buf, NULL, 10);
  72. }
  73. }
  74. ff_url_join(buf, sizeof(buf), "udp", NULL, hostname, port, "%s", path);
  75. ff_udp_set_remote_url(s->rtp_hd, buf);
  76. ff_url_join(buf, sizeof(buf), "udp", NULL, hostname, rtcp_port, "%s", path);
  77. ff_udp_set_remote_url(s->rtcp_hd, buf);
  78. return 0;
  79. }
  80. static struct addrinfo* rtp_resolve_host(const char *hostname, int port,
  81. int type, int family, int flags)
  82. {
  83. struct addrinfo hints = { 0 }, *res = 0;
  84. int error;
  85. char service[16];
  86. snprintf(service, sizeof(service), "%d", port);
  87. hints.ai_socktype = type;
  88. hints.ai_family = family;
  89. hints.ai_flags = flags;
  90. if ((error = getaddrinfo(hostname, service, &hints, &res))) {
  91. res = NULL;
  92. av_log(NULL, AV_LOG_ERROR, "rtp_resolve_host: %s\n", gai_strerror(error));
  93. }
  94. return res;
  95. }
  96. static int compare_addr(const struct sockaddr_storage *a,
  97. const struct sockaddr_storage *b)
  98. {
  99. if (a->ss_family != b->ss_family)
  100. return 1;
  101. if (a->ss_family == AF_INET) {
  102. return (((const struct sockaddr_in *)a)->sin_addr.s_addr !=
  103. ((const struct sockaddr_in *)b)->sin_addr.s_addr);
  104. }
  105. #if HAVE_STRUCT_SOCKADDR_IN6
  106. if (a->ss_family == AF_INET6) {
  107. const uint8_t *s6_addr_a = ((const struct sockaddr_in6 *)a)->sin6_addr.s6_addr;
  108. const uint8_t *s6_addr_b = ((const struct sockaddr_in6 *)b)->sin6_addr.s6_addr;
  109. return memcmp(s6_addr_a, s6_addr_b, 16);
  110. }
  111. #endif
  112. return 1;
  113. }
  114. static int get_port(const struct sockaddr_storage *ss)
  115. {
  116. if (ss->ss_family == AF_INET)
  117. return ntohs(((const struct sockaddr_in *)ss)->sin_port);
  118. #if HAVE_STRUCT_SOCKADDR_IN6
  119. if (ss->ss_family == AF_INET6)
  120. return ntohs(((const struct sockaddr_in6 *)ss)->sin6_port);
  121. #endif
  122. return 0;
  123. }
  124. static void set_port(struct sockaddr_storage *ss, int port)
  125. {
  126. if (ss->ss_family == AF_INET)
  127. ((struct sockaddr_in *)ss)->sin_port = htons(port);
  128. #if HAVE_STRUCT_SOCKADDR_IN6
  129. else if (ss->ss_family == AF_INET6)
  130. ((struct sockaddr_in6 *)ss)->sin6_port = htons(port);
  131. #endif
  132. }
  133. static int rtp_check_source_lists(RTPContext *s, struct sockaddr_storage *source_addr_ptr)
  134. {
  135. int i;
  136. if (s->nb_ssm_exclude_addrs) {
  137. for (i = 0; i < s->nb_ssm_exclude_addrs; i++) {
  138. if (!compare_addr(source_addr_ptr, s->ssm_exclude_addrs[i]))
  139. return 1;
  140. }
  141. }
  142. if (s->nb_ssm_include_addrs) {
  143. for (i = 0; i < s->nb_ssm_include_addrs; i++) {
  144. if (!compare_addr(source_addr_ptr, s->ssm_include_addrs[i]))
  145. return 0;
  146. }
  147. return 1;
  148. }
  149. return 0;
  150. }
  151. /**
  152. * add option to url of the form:
  153. * "http://host:port/path?option1=val1&option2=val2...
  154. */
  155. static av_printf_format(3, 4) void url_add_option(char *buf, int buf_size, const char *fmt, ...)
  156. {
  157. char buf1[1024];
  158. va_list ap;
  159. va_start(ap, fmt);
  160. if (strchr(buf, '?'))
  161. av_strlcat(buf, "&", buf_size);
  162. else
  163. av_strlcat(buf, "?", buf_size);
  164. vsnprintf(buf1, sizeof(buf1), fmt, ap);
  165. av_strlcat(buf, buf1, buf_size);
  166. va_end(ap);
  167. }
  168. static void build_udp_url(char *buf, int buf_size,
  169. const char *hostname, int port,
  170. int local_port, int ttl,
  171. int max_packet_size, int connect,
  172. const char *include_sources,
  173. const char *exclude_sources)
  174. {
  175. ff_url_join(buf, buf_size, "udp", NULL, hostname, port, NULL);
  176. if (local_port >= 0)
  177. url_add_option(buf, buf_size, "localport=%d", local_port);
  178. if (ttl >= 0)
  179. url_add_option(buf, buf_size, "ttl=%d", ttl);
  180. if (max_packet_size >=0)
  181. url_add_option(buf, buf_size, "pkt_size=%d", max_packet_size);
  182. if (connect)
  183. url_add_option(buf, buf_size, "connect=1");
  184. if (include_sources && include_sources[0])
  185. url_add_option(buf, buf_size, "sources=%s", include_sources);
  186. if (exclude_sources && exclude_sources[0])
  187. url_add_option(buf, buf_size, "block=%s", exclude_sources);
  188. }
  189. static void rtp_parse_addr_list(URLContext *h, char *buf,
  190. struct sockaddr_storage ***address_list_ptr,
  191. int *address_list_size_ptr)
  192. {
  193. struct addrinfo *ai = NULL;
  194. struct sockaddr_storage *source_addr;
  195. char tmp = '\0', *p = buf, *next;
  196. /* Resolve all of the IPs */
  197. while (p && p[0]) {
  198. next = strchr(p, ',');
  199. if (next) {
  200. tmp = *next;
  201. *next = '\0';
  202. }
  203. ai = rtp_resolve_host(p, 0, SOCK_DGRAM, AF_UNSPEC, 0);
  204. if (ai) {
  205. source_addr = av_mallocz(sizeof(struct sockaddr_storage));
  206. if (!source_addr)
  207. break;
  208. memcpy(source_addr, ai->ai_addr, ai->ai_addrlen);
  209. freeaddrinfo(ai);
  210. dynarray_add(address_list_ptr, address_list_size_ptr, source_addr);
  211. } else {
  212. av_log(h, AV_LOG_WARNING, "Unable to resolve %s\n", p);
  213. }
  214. if (next) {
  215. *next = tmp;
  216. p = next + 1;
  217. } else {
  218. p = NULL;
  219. }
  220. }
  221. }
  222. /**
  223. * url syntax: rtp://host:port[?option=val...]
  224. * option: 'ttl=n' : set the ttl value (for multicast only)
  225. * 'rtcpport=n' : set the remote rtcp port to n
  226. * 'localrtpport=n' : set the local rtp port to n
  227. * 'localrtcpport=n' : set the local rtcp port to n
  228. * 'pkt_size=n' : set max packet size
  229. * 'connect=0/1' : do a connect() on the UDP socket
  230. * 'sources=ip[,ip]' : list allowed source IP addresses
  231. * 'block=ip[,ip]' : list disallowed source IP addresses
  232. * 'write_to_source=0/1' : send packets to the source address of the latest received packet
  233. * deprecated option:
  234. * 'localport=n' : set the local port to n
  235. *
  236. * if rtcpport isn't set the rtcp port will be the rtp port + 1
  237. * if local rtp port isn't set any available port will be used for the local
  238. * rtp and rtcp ports
  239. * if the local rtcp port is not set it will be the local rtp port + 1
  240. */
  241. static int rtp_open(URLContext *h, const char *uri, int flags)
  242. {
  243. RTPContext *s = h->priv_data;
  244. int rtp_port, rtcp_port,
  245. ttl, connect,
  246. local_rtp_port, local_rtcp_port, max_packet_size;
  247. char hostname[256], include_sources[1024] = "", exclude_sources[1024] = "";
  248. char buf[1024];
  249. char path[1024];
  250. const char *p;
  251. av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &rtp_port,
  252. path, sizeof(path), uri);
  253. /* extract parameters */
  254. ttl = -1;
  255. rtcp_port = rtp_port+1;
  256. local_rtp_port = -1;
  257. local_rtcp_port = -1;
  258. max_packet_size = -1;
  259. connect = 0;
  260. p = strchr(uri, '?');
  261. if (p) {
  262. if (av_find_info_tag(buf, sizeof(buf), "ttl", p)) {
  263. ttl = strtol(buf, NULL, 10);
  264. }
  265. if (av_find_info_tag(buf, sizeof(buf), "rtcpport", p)) {
  266. rtcp_port = strtol(buf, NULL, 10);
  267. }
  268. if (av_find_info_tag(buf, sizeof(buf), "localport", p)) {
  269. local_rtp_port = strtol(buf, NULL, 10);
  270. }
  271. if (av_find_info_tag(buf, sizeof(buf), "localrtpport", p)) {
  272. local_rtp_port = strtol(buf, NULL, 10);
  273. }
  274. if (av_find_info_tag(buf, sizeof(buf), "localrtcpport", p)) {
  275. local_rtcp_port = strtol(buf, NULL, 10);
  276. }
  277. if (av_find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
  278. max_packet_size = strtol(buf, NULL, 10);
  279. }
  280. if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
  281. connect = strtol(buf, NULL, 10);
  282. }
  283. if (av_find_info_tag(buf, sizeof(buf), "write_to_source", p)) {
  284. s->write_to_source = strtol(buf, NULL, 10);
  285. }
  286. if (av_find_info_tag(buf, sizeof(buf), "sources", p)) {
  287. av_strlcpy(include_sources, buf, sizeof(include_sources));
  288. rtp_parse_addr_list(h, buf, &s->ssm_include_addrs, &s->nb_ssm_include_addrs);
  289. }
  290. if (av_find_info_tag(buf, sizeof(buf), "block", p)) {
  291. av_strlcpy(exclude_sources, buf, sizeof(exclude_sources));
  292. rtp_parse_addr_list(h, buf, &s->ssm_exclude_addrs, &s->nb_ssm_exclude_addrs);
  293. }
  294. }
  295. build_udp_url(buf, sizeof(buf),
  296. hostname, rtp_port, local_rtp_port, ttl, max_packet_size,
  297. connect, include_sources, exclude_sources);
  298. if (ffurl_open(&s->rtp_hd, buf, flags, &h->interrupt_callback, NULL) < 0)
  299. goto fail;
  300. if (local_rtp_port>=0 && local_rtcp_port<0)
  301. local_rtcp_port = ff_udp_get_local_port(s->rtp_hd) + 1;
  302. build_udp_url(buf, sizeof(buf),
  303. hostname, rtcp_port, local_rtcp_port, ttl, max_packet_size,
  304. connect, include_sources, exclude_sources);
  305. if (ffurl_open(&s->rtcp_hd, buf, flags, &h->interrupt_callback, NULL) < 0)
  306. goto fail;
  307. /* just to ease handle access. XXX: need to suppress direct handle
  308. access */
  309. s->rtp_fd = ffurl_get_file_handle(s->rtp_hd);
  310. s->rtcp_fd = ffurl_get_file_handle(s->rtcp_hd);
  311. h->max_packet_size = s->rtp_hd->max_packet_size;
  312. h->is_streamed = 1;
  313. return 0;
  314. fail:
  315. if (s->rtp_hd)
  316. ffurl_close(s->rtp_hd);
  317. if (s->rtcp_hd)
  318. ffurl_close(s->rtcp_hd);
  319. return AVERROR(EIO);
  320. }
  321. static int rtp_read(URLContext *h, uint8_t *buf, int size)
  322. {
  323. RTPContext *s = h->priv_data;
  324. int len, n, i;
  325. struct pollfd p[2] = {{s->rtp_fd, POLLIN, 0}, {s->rtcp_fd, POLLIN, 0}};
  326. int poll_delay = h->flags & AVIO_FLAG_NONBLOCK ? 0 : 100;
  327. struct sockaddr_storage *addrs[2] = { &s->last_rtp_source, &s->last_rtcp_source };
  328. socklen_t *addr_lens[2] = { &s->last_rtp_source_len, &s->last_rtcp_source_len };
  329. for(;;) {
  330. if (ff_check_interrupt(&h->interrupt_callback))
  331. return AVERROR_EXIT;
  332. n = poll(p, 2, poll_delay);
  333. if (n > 0) {
  334. /* first try RTCP, then RTP */
  335. for (i = 1; i >= 0; i--) {
  336. if (!(p[i].revents & POLLIN))
  337. continue;
  338. *addr_lens[i] = sizeof(*addrs[i]);
  339. len = recvfrom(p[i].fd, buf, size, 0,
  340. (struct sockaddr *)addrs[i], addr_lens[i]);
  341. if (len < 0) {
  342. if (ff_neterrno() == AVERROR(EAGAIN) ||
  343. ff_neterrno() == AVERROR(EINTR))
  344. continue;
  345. return AVERROR(EIO);
  346. }
  347. if (rtp_check_source_lists(s, addrs[i]))
  348. continue;
  349. return len;
  350. }
  351. } else if (n < 0) {
  352. if (ff_neterrno() == AVERROR(EINTR))
  353. continue;
  354. return AVERROR(EIO);
  355. }
  356. if (h->flags & AVIO_FLAG_NONBLOCK)
  357. return AVERROR(EAGAIN);
  358. }
  359. return len;
  360. }
  361. static int rtp_write(URLContext *h, const uint8_t *buf, int size)
  362. {
  363. RTPContext *s = h->priv_data;
  364. int ret;
  365. URLContext *hd;
  366. if (size < 2)
  367. return AVERROR(EINVAL);
  368. if (s->write_to_source) {
  369. int fd;
  370. struct sockaddr_storage *source, temp_source;
  371. socklen_t *source_len, temp_len;
  372. if (!s->last_rtp_source.ss_family && !s->last_rtcp_source.ss_family) {
  373. av_log(h, AV_LOG_ERROR,
  374. "Unable to send packet to source, no packets received yet\n");
  375. // Intentionally not returning an error here
  376. return size;
  377. }
  378. if (RTP_PT_IS_RTCP(buf[1])) {
  379. fd = s->rtcp_fd;
  380. source = &s->last_rtcp_source;
  381. source_len = &s->last_rtcp_source_len;
  382. } else {
  383. fd = s->rtp_fd;
  384. source = &s->last_rtp_source;
  385. source_len = &s->last_rtp_source_len;
  386. }
  387. if (!source->ss_family) {
  388. source = &temp_source;
  389. source_len = &temp_len;
  390. if (RTP_PT_IS_RTCP(buf[1])) {
  391. temp_source = s->last_rtp_source;
  392. temp_len = s->last_rtp_source_len;
  393. set_port(source, get_port(source) + 1);
  394. av_log(h, AV_LOG_INFO,
  395. "Not received any RTCP packets yet, inferring peer port "
  396. "from the RTP port\n");
  397. } else {
  398. temp_source = s->last_rtcp_source;
  399. temp_len = s->last_rtcp_source_len;
  400. set_port(source, get_port(source) - 1);
  401. av_log(h, AV_LOG_INFO,
  402. "Not received any RTP packets yet, inferring peer port "
  403. "from the RTCP port\n");
  404. }
  405. }
  406. if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
  407. ret = ff_network_wait_fd(fd, 1);
  408. if (ret < 0)
  409. return ret;
  410. }
  411. ret = sendto(fd, buf, size, 0, (struct sockaddr *) source,
  412. *source_len);
  413. return ret < 0 ? ff_neterrno() : ret;
  414. }
  415. if (RTP_PT_IS_RTCP(buf[1])) {
  416. /* RTCP payload type */
  417. hd = s->rtcp_hd;
  418. } else {
  419. /* RTP payload type */
  420. hd = s->rtp_hd;
  421. }
  422. ret = ffurl_write(hd, buf, size);
  423. return ret;
  424. }
  425. static int rtp_close(URLContext *h)
  426. {
  427. RTPContext *s = h->priv_data;
  428. int i;
  429. for (i = 0; i < s->nb_ssm_include_addrs; i++)
  430. av_free(s->ssm_include_addrs[i]);
  431. av_freep(&s->ssm_include_addrs);
  432. for (i = 0; i < s->nb_ssm_exclude_addrs; i++)
  433. av_free(s->ssm_exclude_addrs[i]);
  434. av_freep(&s->ssm_exclude_addrs);
  435. ffurl_close(s->rtp_hd);
  436. ffurl_close(s->rtcp_hd);
  437. return 0;
  438. }
  439. /**
  440. * Return the local rtp port used by the RTP connection
  441. * @param h media file context
  442. * @return the local port number
  443. */
  444. int ff_rtp_get_local_rtp_port(URLContext *h)
  445. {
  446. RTPContext *s = h->priv_data;
  447. return ff_udp_get_local_port(s->rtp_hd);
  448. }
  449. /**
  450. * Return the local rtcp port used by the RTP connection
  451. * @param h media file context
  452. * @return the local port number
  453. */
  454. int ff_rtp_get_local_rtcp_port(URLContext *h)
  455. {
  456. RTPContext *s = h->priv_data;
  457. return ff_udp_get_local_port(s->rtcp_hd);
  458. }
  459. static int rtp_get_file_handle(URLContext *h)
  460. {
  461. RTPContext *s = h->priv_data;
  462. return s->rtp_fd;
  463. }
  464. static int rtp_get_multi_file_handle(URLContext *h, int **handles,
  465. int *numhandles)
  466. {
  467. RTPContext *s = h->priv_data;
  468. int *hs = *handles = av_malloc(sizeof(**handles) * 2);
  469. if (!hs)
  470. return AVERROR(ENOMEM);
  471. hs[0] = s->rtp_fd;
  472. hs[1] = s->rtcp_fd;
  473. *numhandles = 2;
  474. return 0;
  475. }
  476. URLProtocol ff_rtp_protocol = {
  477. .name = "rtp",
  478. .url_open = rtp_open,
  479. .url_read = rtp_read,
  480. .url_write = rtp_write,
  481. .url_close = rtp_close,
  482. .url_get_file_handle = rtp_get_file_handle,
  483. .url_get_multi_file_handle = rtp_get_multi_file_handle,
  484. .priv_data_size = sizeof(RTPContext),
  485. .flags = URL_PROTOCOL_FLAG_NETWORK,
  486. };