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.

340 lines
9.5KB

  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 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;
  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. s = av_mallocz(sizeof(RTPContext));
  130. if (!s)
  131. return AVERROR(ENOMEM);
  132. h->priv_data = s;
  133. av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &rtp_port,
  134. path, sizeof(path), uri);
  135. /* extract parameters */
  136. ttl = -1;
  137. rtcp_port = rtp_port+1;
  138. local_rtp_port = -1;
  139. local_rtcp_port = -1;
  140. max_packet_size = -1;
  141. connect = 0;
  142. p = strchr(uri, '?');
  143. if (p) {
  144. if (av_find_info_tag(buf, sizeof(buf), "ttl", p)) {
  145. ttl = strtol(buf, NULL, 10);
  146. }
  147. if (av_find_info_tag(buf, sizeof(buf), "rtcpport", p)) {
  148. rtcp_port = strtol(buf, NULL, 10);
  149. }
  150. if (av_find_info_tag(buf, sizeof(buf), "localport", p)) {
  151. local_rtp_port = strtol(buf, NULL, 10);
  152. }
  153. if (av_find_info_tag(buf, sizeof(buf), "localrtpport", p)) {
  154. local_rtp_port = strtol(buf, NULL, 10);
  155. }
  156. if (av_find_info_tag(buf, sizeof(buf), "localrtcpport", p)) {
  157. local_rtcp_port = strtol(buf, NULL, 10);
  158. }
  159. if (av_find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
  160. max_packet_size = strtol(buf, NULL, 10);
  161. }
  162. if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
  163. connect = strtol(buf, NULL, 10);
  164. }
  165. }
  166. build_udp_url(buf, sizeof(buf),
  167. hostname, rtp_port, local_rtp_port, ttl, max_packet_size,
  168. connect);
  169. if (ffurl_open(&s->rtp_hd, buf, flags) < 0)
  170. goto fail;
  171. if (local_rtp_port>=0 && local_rtcp_port<0)
  172. local_rtcp_port = ff_udp_get_local_port(s->rtp_hd) + 1;
  173. build_udp_url(buf, sizeof(buf),
  174. hostname, rtcp_port, local_rtcp_port, ttl, max_packet_size,
  175. connect);
  176. if (ffurl_open(&s->rtcp_hd, buf, flags) < 0)
  177. goto fail;
  178. /* just to ease handle access. XXX: need to suppress direct handle
  179. access */
  180. s->rtp_fd = ffurl_get_file_handle(s->rtp_hd);
  181. s->rtcp_fd = ffurl_get_file_handle(s->rtcp_hd);
  182. h->max_packet_size = s->rtp_hd->max_packet_size;
  183. h->is_streamed = 1;
  184. return 0;
  185. fail:
  186. if (s->rtp_hd)
  187. ffurl_close(s->rtp_hd);
  188. if (s->rtcp_hd)
  189. ffurl_close(s->rtcp_hd);
  190. av_free(s);
  191. return AVERROR(EIO);
  192. }
  193. static int rtp_read(URLContext *h, uint8_t *buf, int size)
  194. {
  195. RTPContext *s = h->priv_data;
  196. struct sockaddr_storage from;
  197. socklen_t from_len;
  198. int len, n;
  199. struct pollfd p[2] = {{s->rtp_fd, POLLIN, 0}, {s->rtcp_fd, POLLIN, 0}};
  200. for(;;) {
  201. if (url_interrupt_cb())
  202. return AVERROR_EXIT;
  203. /* build fdset to listen to RTP and RTCP packets */
  204. n = poll(p, 2, 100);
  205. if (n > 0) {
  206. /* first try RTCP */
  207. if (p[1].revents & POLLIN) {
  208. from_len = sizeof(from);
  209. len = recvfrom (s->rtcp_fd, buf, size, 0,
  210. (struct sockaddr *)&from, &from_len);
  211. if (len < 0) {
  212. if (ff_neterrno() == AVERROR(EAGAIN) ||
  213. ff_neterrno() == AVERROR(EINTR))
  214. continue;
  215. return AVERROR(EIO);
  216. }
  217. break;
  218. }
  219. /* then RTP */
  220. if (p[0].revents & POLLIN) {
  221. from_len = sizeof(from);
  222. len = recvfrom (s->rtp_fd, buf, size, 0,
  223. (struct sockaddr *)&from, &from_len);
  224. if (len < 0) {
  225. if (ff_neterrno() == AVERROR(EAGAIN) ||
  226. ff_neterrno() == AVERROR(EINTR))
  227. continue;
  228. return AVERROR(EIO);
  229. }
  230. break;
  231. }
  232. } else if (n < 0) {
  233. if (ff_neterrno() == AVERROR(EINTR))
  234. continue;
  235. return AVERROR(EIO);
  236. }
  237. }
  238. return len;
  239. }
  240. static int rtp_write(URLContext *h, const uint8_t *buf, int size)
  241. {
  242. RTPContext *s = h->priv_data;
  243. int ret;
  244. URLContext *hd;
  245. if (buf[1] >= RTCP_SR && buf[1] <= RTCP_APP) {
  246. /* RTCP payload type */
  247. hd = s->rtcp_hd;
  248. } else {
  249. /* RTP payload type */
  250. hd = s->rtp_hd;
  251. }
  252. ret = ffurl_write(hd, buf, size);
  253. return ret;
  254. }
  255. static int rtp_close(URLContext *h)
  256. {
  257. RTPContext *s = h->priv_data;
  258. ffurl_close(s->rtp_hd);
  259. ffurl_close(s->rtcp_hd);
  260. av_free(s);
  261. return 0;
  262. }
  263. /**
  264. * Return the local rtp port used by the RTP connection
  265. * @param h media file context
  266. * @return the local port number
  267. */
  268. int rtp_get_local_rtp_port(URLContext *h)
  269. {
  270. RTPContext *s = h->priv_data;
  271. return ff_udp_get_local_port(s->rtp_hd);
  272. }
  273. /**
  274. * Return the local rtcp port used by the RTP connection
  275. * @param h media file context
  276. * @return the local port number
  277. */
  278. int rtp_get_local_rtcp_port(URLContext *h)
  279. {
  280. RTPContext *s = h->priv_data;
  281. return ff_udp_get_local_port(s->rtcp_hd);
  282. }
  283. static int rtp_get_file_handle(URLContext *h)
  284. {
  285. RTPContext *s = h->priv_data;
  286. return s->rtp_fd;
  287. }
  288. int rtp_get_rtcp_file_handle(URLContext *h) {
  289. RTPContext *s = h->priv_data;
  290. return s->rtcp_fd;
  291. }
  292. URLProtocol ff_rtp_protocol = {
  293. .name = "rtp",
  294. .url_open = rtp_open,
  295. .url_read = rtp_read,
  296. .url_write = rtp_write,
  297. .url_close = rtp_close,
  298. .url_get_file_handle = rtp_get_file_handle,
  299. };