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.

346 lines
10KB

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