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.9KB

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