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.

335 lines
9.6KB

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