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.

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