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.

403 lines
12KB

  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. * @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. url_add_option(buf, buf_size, "fifo_size=0");
  135. if (sources && sources[0])
  136. url_add_option(buf, buf_size, "sources=%s", sources);
  137. }
  138. /**
  139. * url syntax: rtp://host:port[?option=val...]
  140. * option: 'ttl=n' : set the ttl value (for multicast only)
  141. * 'rtcpport=n' : set the remote rtcp port to n
  142. * 'localrtpport=n' : set the local rtp port to n
  143. * 'localrtcpport=n' : set the local rtcp port to n
  144. * 'pkt_size=n' : set max packet size
  145. * 'connect=0/1' : do a connect() on the UDP socket
  146. * deprecated option:
  147. * 'localport=n' : set the local port to n
  148. * 'sources=ip[,ip]' : list allowed source IP addresses
  149. *
  150. * if rtcpport isn't set the rtcp port will be the rtp port + 1
  151. * if local rtp port isn't set any available port will be used for the local
  152. * rtp and rtcp ports
  153. * if the local rtcp port is not set it will be the local rtp port + 1
  154. */
  155. static int rtp_open(URLContext *h, const char *uri, int flags)
  156. {
  157. RTPContext *s = h->priv_data;
  158. int rtp_port, rtcp_port,
  159. ttl, connect,
  160. local_rtp_port, local_rtcp_port, max_packet_size;
  161. char hostname[256], sources[1024] = "";
  162. char buf[1024];
  163. char path[1024];
  164. const char *p;
  165. av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &rtp_port,
  166. path, sizeof(path), uri);
  167. /* extract parameters */
  168. ttl = -1;
  169. rtcp_port = rtp_port+1;
  170. local_rtp_port = -1;
  171. local_rtcp_port = -1;
  172. max_packet_size = -1;
  173. connect = 0;
  174. p = strchr(uri, '?');
  175. if (p) {
  176. if (av_find_info_tag(buf, sizeof(buf), "ttl", p)) {
  177. ttl = strtol(buf, NULL, 10);
  178. }
  179. if (av_find_info_tag(buf, sizeof(buf), "rtcpport", p)) {
  180. rtcp_port = strtol(buf, NULL, 10);
  181. }
  182. if (av_find_info_tag(buf, sizeof(buf), "localport", p)) {
  183. local_rtp_port = strtol(buf, NULL, 10);
  184. }
  185. if (av_find_info_tag(buf, sizeof(buf), "localrtpport", p)) {
  186. local_rtp_port = strtol(buf, NULL, 10);
  187. }
  188. if (av_find_info_tag(buf, sizeof(buf), "localrtcpport", p)) {
  189. local_rtcp_port = strtol(buf, NULL, 10);
  190. }
  191. if (av_find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
  192. max_packet_size = strtol(buf, NULL, 10);
  193. }
  194. if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
  195. connect = strtol(buf, NULL, 10);
  196. }
  197. if (av_find_info_tag(buf, sizeof(buf), "sources", p)) {
  198. struct addrinfo *sourceaddr = NULL;
  199. av_strlcpy(sources, buf, sizeof(sources));
  200. /* Try resolving the IP if only one IP is specified - we don't
  201. * support manually checking more than one IP. */
  202. if (!strchr(sources, ','))
  203. sourceaddr = rtp_resolve_host(sources, 0,
  204. SOCK_DGRAM, AF_UNSPEC,
  205. AI_NUMERICHOST);
  206. if (sourceaddr) {
  207. s->ssm = 1;
  208. memcpy(&s->ssm_addr, sourceaddr->ai_addr, sourceaddr->ai_addrlen);
  209. freeaddrinfo(sourceaddr);
  210. }
  211. }
  212. }
  213. build_udp_url(buf, sizeof(buf),
  214. hostname, rtp_port, local_rtp_port, ttl, max_packet_size,
  215. connect, sources);
  216. if (ffurl_open(&s->rtp_hd, buf, flags, &h->interrupt_callback, NULL) < 0)
  217. goto fail;
  218. if (local_rtp_port>=0 && local_rtcp_port<0)
  219. local_rtcp_port = ff_udp_get_local_port(s->rtp_hd) + 1;
  220. build_udp_url(buf, sizeof(buf),
  221. hostname, rtcp_port, local_rtcp_port, ttl, max_packet_size,
  222. connect, sources);
  223. if (ffurl_open(&s->rtcp_hd, buf, flags, &h->interrupt_callback, NULL) < 0)
  224. goto fail;
  225. /* just to ease handle access. XXX: need to suppress direct handle
  226. access */
  227. s->rtp_fd = ffurl_get_file_handle(s->rtp_hd);
  228. s->rtcp_fd = ffurl_get_file_handle(s->rtcp_hd);
  229. h->max_packet_size = s->rtp_hd->max_packet_size;
  230. h->is_streamed = 1;
  231. return 0;
  232. fail:
  233. if (s->rtp_hd)
  234. ffurl_close(s->rtp_hd);
  235. if (s->rtcp_hd)
  236. ffurl_close(s->rtcp_hd);
  237. return AVERROR(EIO);
  238. }
  239. static int rtp_read(URLContext *h, uint8_t *buf, int size)
  240. {
  241. RTPContext *s = h->priv_data;
  242. struct sockaddr_storage from;
  243. socklen_t from_len;
  244. int len, n;
  245. struct pollfd p[2] = {{s->rtp_fd, POLLIN, 0}, {s->rtcp_fd, POLLIN, 0}};
  246. for(;;) {
  247. if (ff_check_interrupt(&h->interrupt_callback))
  248. return AVERROR_EXIT;
  249. /* build fdset to listen to RTP and RTCP packets */
  250. n = poll(p, 2, 100);
  251. if (n > 0) {
  252. /* first try RTCP */
  253. if (p[1].revents & POLLIN) {
  254. from_len = sizeof(from);
  255. len = recvfrom (s->rtcp_fd, buf, size, 0,
  256. (struct sockaddr *)&from, &from_len);
  257. if (len < 0) {
  258. if (ff_neterrno() == AVERROR(EAGAIN) ||
  259. ff_neterrno() == AVERROR(EINTR))
  260. continue;
  261. return AVERROR(EIO);
  262. }
  263. if (s->ssm && compare_addr(&from, &s->ssm_addr))
  264. continue;
  265. break;
  266. }
  267. /* then RTP */
  268. if (p[0].revents & POLLIN) {
  269. from_len = sizeof(from);
  270. len = recvfrom (s->rtp_fd, buf, size, 0,
  271. (struct sockaddr *)&from, &from_len);
  272. if (len < 0) {
  273. if (ff_neterrno() == AVERROR(EAGAIN) ||
  274. ff_neterrno() == AVERROR(EINTR))
  275. continue;
  276. return AVERROR(EIO);
  277. }
  278. if (s->ssm && compare_addr(&from, &s->ssm_addr))
  279. continue;
  280. break;
  281. }
  282. } else if (n < 0) {
  283. if (ff_neterrno() == AVERROR(EINTR))
  284. continue;
  285. return AVERROR(EIO);
  286. }
  287. }
  288. return len;
  289. }
  290. static int rtp_write(URLContext *h, const uint8_t *buf, int size)
  291. {
  292. RTPContext *s = h->priv_data;
  293. int ret;
  294. URLContext *hd;
  295. if (RTP_PT_IS_RTCP(buf[1])) {
  296. /* RTCP payload type */
  297. hd = s->rtcp_hd;
  298. } else {
  299. /* RTP payload type */
  300. hd = s->rtp_hd;
  301. }
  302. ret = ffurl_write(hd, buf, size);
  303. return ret;
  304. }
  305. static int rtp_close(URLContext *h)
  306. {
  307. RTPContext *s = h->priv_data;
  308. ffurl_close(s->rtp_hd);
  309. ffurl_close(s->rtcp_hd);
  310. return 0;
  311. }
  312. /**
  313. * Return the local rtp port used by the RTP connection
  314. * @param h media file context
  315. * @return the local port number
  316. */
  317. int ff_rtp_get_local_rtp_port(URLContext *h)
  318. {
  319. RTPContext *s = h->priv_data;
  320. return ff_udp_get_local_port(s->rtp_hd);
  321. }
  322. /**
  323. * Return the local rtcp port used by the RTP connection
  324. * @param h media file context
  325. * @return the local port number
  326. */
  327. int ff_rtp_get_local_rtcp_port(URLContext *h)
  328. {
  329. RTPContext *s = h->priv_data;
  330. return ff_udp_get_local_port(s->rtcp_hd);
  331. }
  332. static int rtp_get_file_handle(URLContext *h)
  333. {
  334. RTPContext *s = h->priv_data;
  335. return s->rtp_fd;
  336. }
  337. static int rtp_get_multi_file_handle(URLContext *h, int **handles,
  338. int *numhandles)
  339. {
  340. RTPContext *s = h->priv_data;
  341. int *hs = *handles = av_malloc(sizeof(**handles) * 2);
  342. if (!hs)
  343. return AVERROR(ENOMEM);
  344. hs[0] = s->rtp_fd;
  345. hs[1] = s->rtcp_fd;
  346. *numhandles = 2;
  347. return 0;
  348. }
  349. URLProtocol ff_rtp_protocol = {
  350. .name = "rtp",
  351. .url_open = rtp_open,
  352. .url_read = rtp_read,
  353. .url_write = rtp_write,
  354. .url_close = rtp_close,
  355. .url_get_file_handle = rtp_get_file_handle,
  356. .url_get_multi_file_handle = rtp_get_multi_file_handle,
  357. .priv_data_size = sizeof(RTPContext),
  358. .flags = URL_PROTOCOL_FLAG_NETWORK,
  359. };