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.

339 lines
9.8KB

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