You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

402 lines
12KB

  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 "rtpdec.h"
  30. #include "url.h"
  31. #include <stdarg.h>
  32. #include "internal.h"
  33. #include "network.h"
  34. #include "os_support.h"
  35. #include <fcntl.h>
  36. #if HAVE_POLL_H
  37. #include <sys/poll.h>
  38. #endif
  39. typedef struct RTPContext {
  40. URLContext *rtp_hd, *rtcp_hd;
  41. int rtp_fd, rtcp_fd, ssm;
  42. struct sockaddr_storage ssm_addr;
  43. } RTPContext;
  44. /**
  45. * If no filename is given to av_open_input_file because you want to
  46. * get the local port first, then you must call this function to set
  47. * the remote server address.
  48. *
  49. * @param h media file context
  50. * @param uri of the remote server
  51. * @return zero if no error.
  52. */
  53. int ff_rtp_set_remote_url(URLContext *h, const char *uri)
  54. {
  55. RTPContext *s = h->priv_data;
  56. char hostname[256];
  57. int port;
  58. char buf[1024];
  59. char path[1024];
  60. av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port,
  61. path, sizeof(path), uri);
  62. ff_url_join(buf, sizeof(buf), "udp", NULL, hostname, port, "%s", path);
  63. ff_udp_set_remote_url(s->rtp_hd, buf);
  64. ff_url_join(buf, sizeof(buf), "udp", NULL, hostname, port + 1, "%s", path);
  65. ff_udp_set_remote_url(s->rtcp_hd, buf);
  66. return 0;
  67. }
  68. static struct addrinfo* rtp_resolve_host(const char *hostname, int port,
  69. int type, int family, int flags)
  70. {
  71. struct addrinfo hints = { 0 }, *res = 0;
  72. int error;
  73. char service[16];
  74. snprintf(service, sizeof(service), "%d", port);
  75. hints.ai_socktype = type;
  76. hints.ai_family = family;
  77. hints.ai_flags = flags;
  78. if ((error = getaddrinfo(hostname, service, &hints, &res))) {
  79. res = NULL;
  80. av_log(NULL, AV_LOG_ERROR, "rtp_resolve_host: %s\n", gai_strerror(error));
  81. }
  82. return res;
  83. }
  84. static int compare_addr(const struct sockaddr_storage *a,
  85. const struct sockaddr_storage *b)
  86. {
  87. if (a->ss_family != b->ss_family)
  88. return 1;
  89. if (a->ss_family == AF_INET) {
  90. return (((const struct sockaddr_in *)a)->sin_addr.s_addr !=
  91. ((const struct sockaddr_in *)b)->sin_addr.s_addr);
  92. }
  93. #if defined(IPPROTO_IPV6)
  94. if (a->ss_family == AF_INET6) {
  95. const uint8_t *s6_addr_a = ((const struct sockaddr_in6 *)a)->sin6_addr.s6_addr;
  96. const uint8_t *s6_addr_b = ((const struct sockaddr_in6 *)b)->sin6_addr.s6_addr;
  97. return memcmp(s6_addr_a, s6_addr_b, 16);
  98. }
  99. #endif
  100. return 1;
  101. }
  102. /**
  103. * add option to url of the form:
  104. * "http://host:port/path?option1=val1&option2=val2...
  105. */
  106. static av_printf_format(3, 4) void url_add_option(char *buf, int buf_size, const char *fmt, ...)
  107. {
  108. char buf1[1024];
  109. va_list ap;
  110. va_start(ap, fmt);
  111. if (strchr(buf, '?'))
  112. av_strlcat(buf, "&", buf_size);
  113. else
  114. av_strlcat(buf, "?", buf_size);
  115. vsnprintf(buf1, sizeof(buf1), fmt, ap);
  116. av_strlcat(buf, buf1, buf_size);
  117. va_end(ap);
  118. }
  119. static void build_udp_url(char *buf, int buf_size,
  120. const char *hostname, int port,
  121. int local_port, int ttl,
  122. int max_packet_size, int connect,
  123. const char* sources)
  124. {
  125. ff_url_join(buf, buf_size, "udp", NULL, hostname, port, NULL);
  126. if (local_port >= 0)
  127. url_add_option(buf, buf_size, "localport=%d", local_port);
  128. if (ttl >= 0)
  129. url_add_option(buf, buf_size, "ttl=%d", ttl);
  130. if (max_packet_size >=0)
  131. url_add_option(buf, buf_size, "pkt_size=%d", max_packet_size);
  132. if (connect)
  133. url_add_option(buf, buf_size, "connect=1");
  134. if (sources && sources[0])
  135. url_add_option(buf, buf_size, "sources=%s", sources);
  136. }
  137. /**
  138. * url syntax: rtp://host:port[?option=val...]
  139. * option: 'ttl=n' : set the ttl value (for multicast only)
  140. * 'rtcpport=n' : set the remote rtcp port to n
  141. * 'localrtpport=n' : set the local rtp port to n
  142. * 'localrtcpport=n' : set the local rtcp port to n
  143. * 'pkt_size=n' : set max packet size
  144. * 'connect=0/1' : do a connect() on the UDP socket
  145. * deprecated option:
  146. * 'localport=n' : set the local port to n
  147. * 'sources=ip[,ip]' : list allowed source IP addresses
  148. *
  149. * if rtcpport isn't set the rtcp port will be the rtp port + 1
  150. * if local rtp port isn't set any available port will be used for the local
  151. * rtp and rtcp ports
  152. * if the local rtcp port is not set it will be the local rtp port + 1
  153. */
  154. static int rtp_open(URLContext *h, const char *uri, int flags)
  155. {
  156. RTPContext *s = h->priv_data;
  157. int rtp_port, rtcp_port,
  158. ttl, connect,
  159. local_rtp_port, local_rtcp_port, max_packet_size;
  160. char hostname[256], sources[1024] = "";
  161. char buf[1024];
  162. char path[1024];
  163. const char *p;
  164. av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &rtp_port,
  165. path, sizeof(path), uri);
  166. /* extract parameters */
  167. ttl = -1;
  168. rtcp_port = rtp_port+1;
  169. local_rtp_port = -1;
  170. local_rtcp_port = -1;
  171. max_packet_size = -1;
  172. connect = 0;
  173. p = strchr(uri, '?');
  174. if (p) {
  175. if (av_find_info_tag(buf, sizeof(buf), "ttl", p)) {
  176. ttl = strtol(buf, NULL, 10);
  177. }
  178. if (av_find_info_tag(buf, sizeof(buf), "rtcpport", p)) {
  179. rtcp_port = strtol(buf, NULL, 10);
  180. }
  181. if (av_find_info_tag(buf, sizeof(buf), "localport", p)) {
  182. local_rtp_port = strtol(buf, NULL, 10);
  183. }
  184. if (av_find_info_tag(buf, sizeof(buf), "localrtpport", p)) {
  185. local_rtp_port = strtol(buf, NULL, 10);
  186. }
  187. if (av_find_info_tag(buf, sizeof(buf), "localrtcpport", p)) {
  188. local_rtcp_port = strtol(buf, NULL, 10);
  189. }
  190. if (av_find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
  191. max_packet_size = strtol(buf, NULL, 10);
  192. }
  193. if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
  194. connect = strtol(buf, NULL, 10);
  195. }
  196. if (av_find_info_tag(buf, sizeof(buf), "sources", p)) {
  197. struct addrinfo *sourceaddr = NULL;
  198. av_strlcpy(sources, buf, sizeof(sources));
  199. /* Try resolving the IP if only one IP is specified - we don't
  200. * support manually checking more than one IP. */
  201. if (!strchr(sources, ','))
  202. sourceaddr = rtp_resolve_host(sources, 0,
  203. SOCK_DGRAM, AF_UNSPEC,
  204. AI_NUMERICHOST);
  205. if (sourceaddr) {
  206. s->ssm = 1;
  207. memcpy(&s->ssm_addr, sourceaddr->ai_addr, sourceaddr->ai_addrlen);
  208. freeaddrinfo(sourceaddr);
  209. }
  210. }
  211. }
  212. build_udp_url(buf, sizeof(buf),
  213. hostname, rtp_port, local_rtp_port, ttl, max_packet_size,
  214. connect, sources);
  215. if (ffurl_open(&s->rtp_hd, buf, flags, &h->interrupt_callback, NULL) < 0)
  216. goto fail;
  217. if (local_rtp_port>=0 && local_rtcp_port<0)
  218. local_rtcp_port = ff_udp_get_local_port(s->rtp_hd) + 1;
  219. build_udp_url(buf, sizeof(buf),
  220. hostname, rtcp_port, local_rtcp_port, ttl, max_packet_size,
  221. connect, sources);
  222. if (ffurl_open(&s->rtcp_hd, buf, flags, &h->interrupt_callback, NULL) < 0)
  223. goto fail;
  224. /* just to ease handle access. XXX: need to suppress direct handle
  225. access */
  226. s->rtp_fd = ffurl_get_file_handle(s->rtp_hd);
  227. s->rtcp_fd = ffurl_get_file_handle(s->rtcp_hd);
  228. h->max_packet_size = s->rtp_hd->max_packet_size;
  229. h->is_streamed = 1;
  230. return 0;
  231. fail:
  232. if (s->rtp_hd)
  233. ffurl_close(s->rtp_hd);
  234. if (s->rtcp_hd)
  235. ffurl_close(s->rtcp_hd);
  236. return AVERROR(EIO);
  237. }
  238. static int rtp_read(URLContext *h, uint8_t *buf, int size)
  239. {
  240. RTPContext *s = h->priv_data;
  241. struct sockaddr_storage from;
  242. socklen_t from_len;
  243. int len, n;
  244. struct pollfd p[2] = {{s->rtp_fd, POLLIN, 0}, {s->rtcp_fd, POLLIN, 0}};
  245. for(;;) {
  246. if (ff_check_interrupt(&h->interrupt_callback))
  247. return AVERROR_EXIT;
  248. /* build fdset to listen to RTP and RTCP packets */
  249. n = poll(p, 2, 100);
  250. if (n > 0) {
  251. /* first try RTCP */
  252. if (p[1].revents & POLLIN) {
  253. from_len = sizeof(from);
  254. len = recvfrom (s->rtcp_fd, buf, size, 0,
  255. (struct sockaddr *)&from, &from_len);
  256. if (len < 0) {
  257. if (ff_neterrno() == AVERROR(EAGAIN) ||
  258. ff_neterrno() == AVERROR(EINTR))
  259. continue;
  260. return AVERROR(EIO);
  261. }
  262. if (s->ssm && compare_addr(&from, &s->ssm_addr))
  263. continue;
  264. break;
  265. }
  266. /* then RTP */
  267. if (p[0].revents & POLLIN) {
  268. from_len = sizeof(from);
  269. len = recvfrom (s->rtp_fd, buf, size, 0,
  270. (struct sockaddr *)&from, &from_len);
  271. if (len < 0) {
  272. if (ff_neterrno() == AVERROR(EAGAIN) ||
  273. ff_neterrno() == AVERROR(EINTR))
  274. continue;
  275. return AVERROR(EIO);
  276. }
  277. if (s->ssm && compare_addr(&from, &s->ssm_addr))
  278. continue;
  279. break;
  280. }
  281. } else if (n < 0) {
  282. if (ff_neterrno() == AVERROR(EINTR))
  283. continue;
  284. return AVERROR(EIO);
  285. }
  286. }
  287. return len;
  288. }
  289. static int rtp_write(URLContext *h, const uint8_t *buf, int size)
  290. {
  291. RTPContext *s = h->priv_data;
  292. int ret;
  293. URLContext *hd;
  294. if (RTP_PT_IS_RTCP(buf[1])) {
  295. /* RTCP payload type */
  296. hd = s->rtcp_hd;
  297. } else {
  298. /* RTP payload type */
  299. hd = s->rtp_hd;
  300. }
  301. ret = ffurl_write(hd, buf, size);
  302. return ret;
  303. }
  304. static int rtp_close(URLContext *h)
  305. {
  306. RTPContext *s = h->priv_data;
  307. ffurl_close(s->rtp_hd);
  308. ffurl_close(s->rtcp_hd);
  309. return 0;
  310. }
  311. /**
  312. * Return the local rtp port used by the RTP connection
  313. * @param h media file context
  314. * @return the local port number
  315. */
  316. int ff_rtp_get_local_rtp_port(URLContext *h)
  317. {
  318. RTPContext *s = h->priv_data;
  319. return ff_udp_get_local_port(s->rtp_hd);
  320. }
  321. /**
  322. * Return the local rtcp port used by the RTP connection
  323. * @param h media file context
  324. * @return the local port number
  325. */
  326. int ff_rtp_get_local_rtcp_port(URLContext *h)
  327. {
  328. RTPContext *s = h->priv_data;
  329. return ff_udp_get_local_port(s->rtcp_hd);
  330. }
  331. static int rtp_get_file_handle(URLContext *h)
  332. {
  333. RTPContext *s = h->priv_data;
  334. return s->rtp_fd;
  335. }
  336. static int rtp_get_multi_file_handle(URLContext *h, int **handles,
  337. int *numhandles)
  338. {
  339. RTPContext *s = h->priv_data;
  340. int *hs = *handles = av_malloc(sizeof(**handles) * 2);
  341. if (!hs)
  342. return AVERROR(ENOMEM);
  343. hs[0] = s->rtp_fd;
  344. hs[1] = s->rtcp_fd;
  345. *numhandles = 2;
  346. return 0;
  347. }
  348. URLProtocol ff_rtp_protocol = {
  349. .name = "rtp",
  350. .url_open = rtp_open,
  351. .url_read = rtp_read,
  352. .url_write = rtp_write,
  353. .url_close = rtp_close,
  354. .url_get_file_handle = rtp_get_file_handle,
  355. .url_get_multi_file_handle = rtp_get_multi_file_handle,
  356. .priv_data_size = sizeof(RTPContext),
  357. .flags = URL_PROTOCOL_FLAG_NETWORK,
  358. };