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.

385 lines
10KB

  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/avstring.h"
  26. #include "avformat.h"
  27. #include "rtpdec.h"
  28. #include <unistd.h>
  29. #include <stdarg.h>
  30. #include "internal.h"
  31. #include "network.h"
  32. #include "os_support.h"
  33. #include <fcntl.h>
  34. #if HAVE_SYS_SELECT_H
  35. #include <sys/select.h>
  36. #endif
  37. #include <sys/time.h>
  38. #define RTP_TX_BUF_SIZE (64 * 1024)
  39. #define RTP_RX_BUF_SIZE (128 * 1024)
  40. typedef struct RTPContext {
  41. URLContext *rtp_hd, *rtcp_hd;
  42. int rtp_fd, rtcp_fd;
  43. } RTPContext;
  44. /**
  45. * If no filename is given to av_open_input_file because you want to
  46. * get the local port first, then you must call this function to set
  47. * the remote server address.
  48. *
  49. * @param s1 media file context
  50. * @param uri of the remote server
  51. * @return zero if no error.
  52. */
  53. int rtp_set_remote_url(URLContext *h, const char *uri)
  54. {
  55. RTPContext *s = h->priv_data;
  56. char hostname[256];
  57. int port;
  58. char buf[1024];
  59. char path[1024];
  60. av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port,
  61. path, sizeof(path), uri);
  62. ff_url_join(buf, sizeof(buf), "udp", NULL, hostname, port, "%s", path);
  63. udp_set_remote_url(s->rtp_hd, buf);
  64. ff_url_join(buf, sizeof(buf), "udp", NULL, hostname, port + 1, "%s", path);
  65. udp_set_remote_url(s->rtcp_hd, buf);
  66. return 0;
  67. }
  68. /**
  69. * add option to url of the form:
  70. * "http://host:port/path?option1=val1&option2=val2...
  71. */
  72. static void url_add_option(char *buf, int buf_size, const char *fmt, ...)
  73. {
  74. char buf1[1024];
  75. va_list ap;
  76. va_start(ap, fmt);
  77. if (strchr(buf, '?'))
  78. av_strlcat(buf, "&", buf_size);
  79. else
  80. av_strlcat(buf, "?", buf_size);
  81. vsnprintf(buf1, sizeof(buf1), fmt, ap);
  82. av_strlcat(buf, buf1, buf_size);
  83. va_end(ap);
  84. }
  85. static void build_udp_url(char *buf, int buf_size,
  86. const char *hostname, int port,
  87. int local_port, int ttl,
  88. int max_packet_size)
  89. {
  90. ff_url_join(buf, buf_size, "udp", NULL, hostname, port, NULL);
  91. if (local_port >= 0)
  92. url_add_option(buf, buf_size, "localport=%d", local_port);
  93. if (ttl >= 0)
  94. url_add_option(buf, buf_size, "ttl=%d", ttl);
  95. if (max_packet_size >=0)
  96. url_add_option(buf, buf_size, "pkt_size=%d", max_packet_size);
  97. }
  98. /**
  99. * url syntax: rtp://host:port[?option=val...]
  100. * option: 'ttl=n' : set the ttl value (for multicast only)
  101. * 'rtcpport=n' : set the remote rtcp port to n
  102. * 'localrtpport=n' : set the local rtp port to n
  103. * 'localrtcpport=n' : set the local rtcp port to n
  104. * 'pkt_size=n' : set max packet size
  105. * deprecated option:
  106. * 'localport=n' : set the local port to n
  107. *
  108. * if rtcpport isn't set the rtcp port will be the rtp port + 1
  109. * if local rtp port isn't set any available port will be used for the local
  110. * rtp and rtcp ports
  111. * if the local rtcp port is not set it will be the local rtp port + 1
  112. */
  113. static int rtp_open(URLContext *h, const char *uri, int flags)
  114. {
  115. RTPContext *s;
  116. int rtp_port, rtcp_port,
  117. is_output, ttl,
  118. local_rtp_port, local_rtcp_port, max_packet_size;
  119. char hostname[256];
  120. char buf[1024];
  121. char path[1024];
  122. const char *p;
  123. is_output = (flags & URL_WRONLY);
  124. s = av_mallocz(sizeof(RTPContext));
  125. if (!s)
  126. return AVERROR(ENOMEM);
  127. h->priv_data = s;
  128. av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &rtp_port,
  129. path, sizeof(path), uri);
  130. /* extract parameters */
  131. ttl = -1;
  132. rtcp_port = rtp_port+1;
  133. local_rtp_port = -1;
  134. local_rtcp_port = -1;
  135. max_packet_size = -1;
  136. p = strchr(uri, '?');
  137. if (p) {
  138. if (find_info_tag(buf, sizeof(buf), "ttl", p)) {
  139. ttl = strtol(buf, NULL, 10);
  140. }
  141. if (find_info_tag(buf, sizeof(buf), "rtcpport", p)) {
  142. rtcp_port = strtol(buf, NULL, 10);
  143. }
  144. if (find_info_tag(buf, sizeof(buf), "localport", p)) {
  145. local_rtp_port = strtol(buf, NULL, 10);
  146. }
  147. if (find_info_tag(buf, sizeof(buf), "localrtpport", p)) {
  148. local_rtp_port = strtol(buf, NULL, 10);
  149. }
  150. if (find_info_tag(buf, sizeof(buf), "localrtcpport", p)) {
  151. local_rtcp_port = strtol(buf, NULL, 10);
  152. }
  153. if (find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
  154. max_packet_size = strtol(buf, NULL, 10);
  155. }
  156. }
  157. build_udp_url(buf, sizeof(buf),
  158. hostname, rtp_port, local_rtp_port, ttl, max_packet_size);
  159. if (url_open(&s->rtp_hd, buf, flags) < 0)
  160. goto fail;
  161. if (local_rtp_port>=0 && local_rtcp_port<0)
  162. local_rtcp_port = udp_get_local_port(s->rtp_hd) + 1;
  163. build_udp_url(buf, sizeof(buf),
  164. hostname, rtcp_port, local_rtcp_port, ttl, max_packet_size);
  165. if (url_open(&s->rtcp_hd, buf, flags) < 0)
  166. goto fail;
  167. /* just to ease handle access. XXX: need to suppress direct handle
  168. access */
  169. s->rtp_fd = url_get_file_handle(s->rtp_hd);
  170. s->rtcp_fd = url_get_file_handle(s->rtcp_hd);
  171. h->max_packet_size = url_get_max_packet_size(s->rtp_hd);
  172. h->is_streamed = 1;
  173. return 0;
  174. fail:
  175. if (s->rtp_hd)
  176. url_close(s->rtp_hd);
  177. if (s->rtcp_hd)
  178. url_close(s->rtcp_hd);
  179. av_free(s);
  180. return AVERROR(EIO);
  181. }
  182. static int rtp_read(URLContext *h, uint8_t *buf, int size)
  183. {
  184. RTPContext *s = h->priv_data;
  185. struct sockaddr_in from;
  186. socklen_t from_len;
  187. int len, fd_max, n;
  188. fd_set rfds;
  189. struct timeval tv;
  190. #if 0
  191. for(;;) {
  192. from_len = sizeof(from);
  193. len = recvfrom (s->rtp_fd, buf, size, 0,
  194. (struct sockaddr *)&from, &from_len);
  195. if (len < 0) {
  196. if (ff_neterrno() == FF_NETERROR(EAGAIN) ||
  197. ff_neterrno() == FF_NETERROR(EINTR))
  198. continue;
  199. return AVERROR(EIO);
  200. }
  201. break;
  202. }
  203. #else
  204. for(;;) {
  205. if (url_interrupt_cb())
  206. return AVERROR(EINTR);
  207. /* build fdset to listen to RTP and RTCP packets */
  208. FD_ZERO(&rfds);
  209. fd_max = s->rtp_fd;
  210. FD_SET(s->rtp_fd, &rfds);
  211. if (s->rtcp_fd > fd_max)
  212. fd_max = s->rtcp_fd;
  213. FD_SET(s->rtcp_fd, &rfds);
  214. tv.tv_sec = 0;
  215. tv.tv_usec = 100 * 1000;
  216. n = select(fd_max + 1, &rfds, NULL, NULL, &tv);
  217. if (n > 0) {
  218. /* first try RTCP */
  219. if (FD_ISSET(s->rtcp_fd, &rfds)) {
  220. from_len = sizeof(from);
  221. len = recvfrom (s->rtcp_fd, buf, size, 0,
  222. (struct sockaddr *)&from, &from_len);
  223. if (len < 0) {
  224. if (ff_neterrno() == FF_NETERROR(EAGAIN) ||
  225. ff_neterrno() == FF_NETERROR(EINTR))
  226. continue;
  227. return AVERROR(EIO);
  228. }
  229. break;
  230. }
  231. /* then RTP */
  232. if (FD_ISSET(s->rtp_fd, &rfds)) {
  233. from_len = sizeof(from);
  234. len = recvfrom (s->rtp_fd, buf, size, 0,
  235. (struct sockaddr *)&from, &from_len);
  236. if (len < 0) {
  237. if (ff_neterrno() == FF_NETERROR(EAGAIN) ||
  238. ff_neterrno() == FF_NETERROR(EINTR))
  239. continue;
  240. return AVERROR(EIO);
  241. }
  242. break;
  243. }
  244. } else if (n < 0) {
  245. if (ff_neterrno() == FF_NETERROR(EINTR))
  246. continue;
  247. return AVERROR(EIO);
  248. }
  249. }
  250. #endif
  251. return len;
  252. }
  253. static int rtp_write(URLContext *h, const uint8_t *buf, int size)
  254. {
  255. RTPContext *s = h->priv_data;
  256. int ret;
  257. URLContext *hd;
  258. if (buf[1] >= 200 && buf[1] <= 204) {
  259. /* RTCP payload type */
  260. hd = s->rtcp_hd;
  261. } else {
  262. /* RTP payload type */
  263. hd = s->rtp_hd;
  264. }
  265. ret = url_write(hd, buf, size);
  266. #if 0
  267. {
  268. struct timespec ts;
  269. ts.tv_sec = 0;
  270. ts.tv_nsec = 10 * 1000000;
  271. nanosleep(&ts, NULL);
  272. }
  273. #endif
  274. return ret;
  275. }
  276. static int rtp_close(URLContext *h)
  277. {
  278. RTPContext *s = h->priv_data;
  279. url_close(s->rtp_hd);
  280. url_close(s->rtcp_hd);
  281. av_free(s);
  282. return 0;
  283. }
  284. /**
  285. * Return the local rtp port used by the RTP connection
  286. * @param s1 media file context
  287. * @return the local port number
  288. */
  289. int rtp_get_local_rtp_port(URLContext *h)
  290. {
  291. RTPContext *s = h->priv_data;
  292. return udp_get_local_port(s->rtp_hd);
  293. }
  294. /**
  295. * Return the local rtp port used by the RTP connection
  296. * @param s1 media file context
  297. * @return the local port number
  298. */
  299. int rtp_get_local_port(URLContext *h)
  300. {
  301. RTPContext *s = h->priv_data;
  302. return udp_get_local_port(s->rtp_hd);
  303. }
  304. /**
  305. * Return the local rtcp port used by the RTP connection
  306. * @param s1 media file context
  307. * @return the local port number
  308. */
  309. int rtp_get_local_rtcp_port(URLContext *h)
  310. {
  311. RTPContext *s = h->priv_data;
  312. return udp_get_local_port(s->rtcp_hd);
  313. }
  314. #if (LIBAVFORMAT_VERSION_MAJOR <= 52)
  315. /**
  316. * Return the rtp and rtcp file handles for select() usage to wait for
  317. * several RTP streams at the same time.
  318. * @param h media file context
  319. */
  320. void rtp_get_file_handles(URLContext *h, int *prtp_fd, int *prtcp_fd)
  321. {
  322. RTPContext *s = h->priv_data;
  323. *prtp_fd = s->rtp_fd;
  324. *prtcp_fd = s->rtcp_fd;
  325. }
  326. #endif
  327. static int rtp_get_file_handle(URLContext *h)
  328. {
  329. RTPContext *s = h->priv_data;
  330. return s->rtp_fd;
  331. }
  332. URLProtocol rtp_protocol = {
  333. "rtp",
  334. rtp_open,
  335. rtp_read,
  336. rtp_write,
  337. NULL, /* seek */
  338. rtp_close,
  339. .url_get_file_handle = rtp_get_file_handle,
  340. };