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.

342 lines
8.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 libavformat/rtpproto.c
  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. ff_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. * 'localport=n' : set the local port to n
  102. * 'pkt_size=n' : set max packet size
  103. *
  104. */
  105. static int rtp_open(URLContext *h, const char *uri, int flags)
  106. {
  107. RTPContext *s;
  108. int port, is_output, ttl, local_port, max_packet_size;
  109. char hostname[256];
  110. char buf[1024];
  111. char path[1024];
  112. const char *p;
  113. is_output = (flags & URL_WRONLY);
  114. s = av_mallocz(sizeof(RTPContext));
  115. if (!s)
  116. return AVERROR(ENOMEM);
  117. h->priv_data = s;
  118. ff_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port,
  119. path, sizeof(path), uri);
  120. /* extract parameters */
  121. ttl = -1;
  122. local_port = -1;
  123. max_packet_size = -1;
  124. p = strchr(uri, '?');
  125. if (p) {
  126. if (find_info_tag(buf, sizeof(buf), "ttl", p)) {
  127. ttl = strtol(buf, NULL, 10);
  128. }
  129. if (find_info_tag(buf, sizeof(buf), "localport", p)) {
  130. local_port = strtol(buf, NULL, 10);
  131. }
  132. if (find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
  133. max_packet_size = strtol(buf, NULL, 10);
  134. }
  135. }
  136. build_udp_url(buf, sizeof(buf),
  137. hostname, port, local_port, ttl, max_packet_size);
  138. if (url_open(&s->rtp_hd, buf, flags) < 0)
  139. goto fail;
  140. local_port = udp_get_local_port(s->rtp_hd);
  141. /* XXX: need to open another connection if the port is not even */
  142. /* well, should suppress localport in path */
  143. build_udp_url(buf, sizeof(buf),
  144. hostname, port + 1, local_port + 1, ttl, max_packet_size);
  145. if (url_open(&s->rtcp_hd, buf, flags) < 0)
  146. goto fail;
  147. /* just to ease handle access. XXX: need to suppress direct handle
  148. access */
  149. s->rtp_fd = url_get_file_handle(s->rtp_hd);
  150. s->rtcp_fd = url_get_file_handle(s->rtcp_hd);
  151. h->max_packet_size = url_get_max_packet_size(s->rtp_hd);
  152. h->is_streamed = 1;
  153. return 0;
  154. fail:
  155. if (s->rtp_hd)
  156. url_close(s->rtp_hd);
  157. if (s->rtcp_hd)
  158. url_close(s->rtcp_hd);
  159. av_free(s);
  160. return AVERROR(EIO);
  161. }
  162. static int rtp_read(URLContext *h, uint8_t *buf, int size)
  163. {
  164. RTPContext *s = h->priv_data;
  165. struct sockaddr_in from;
  166. socklen_t from_len;
  167. int len, fd_max, n;
  168. fd_set rfds;
  169. struct timeval tv;
  170. #if 0
  171. for(;;) {
  172. from_len = sizeof(from);
  173. len = recvfrom (s->rtp_fd, buf, size, 0,
  174. (struct sockaddr *)&from, &from_len);
  175. if (len < 0) {
  176. if (ff_neterrno() == FF_NETERROR(EAGAIN) ||
  177. ff_neterrno() == FF_NETERROR(EINTR))
  178. continue;
  179. return AVERROR(EIO);
  180. }
  181. break;
  182. }
  183. #else
  184. for(;;) {
  185. if (url_interrupt_cb())
  186. return AVERROR(EINTR);
  187. /* build fdset to listen to RTP and RTCP packets */
  188. FD_ZERO(&rfds);
  189. fd_max = s->rtp_fd;
  190. FD_SET(s->rtp_fd, &rfds);
  191. if (s->rtcp_fd > fd_max)
  192. fd_max = s->rtcp_fd;
  193. FD_SET(s->rtcp_fd, &rfds);
  194. tv.tv_sec = 0;
  195. tv.tv_usec = 100 * 1000;
  196. n = select(fd_max + 1, &rfds, NULL, NULL, &tv);
  197. if (n > 0) {
  198. /* first try RTCP */
  199. if (FD_ISSET(s->rtcp_fd, &rfds)) {
  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() == FF_NETERROR(EAGAIN) ||
  205. ff_neterrno() == FF_NETERROR(EINTR))
  206. continue;
  207. return AVERROR(EIO);
  208. }
  209. break;
  210. }
  211. /* then RTP */
  212. if (FD_ISSET(s->rtp_fd, &rfds)) {
  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() == FF_NETERROR(EAGAIN) ||
  218. ff_neterrno() == FF_NETERROR(EINTR))
  219. continue;
  220. return AVERROR(EIO);
  221. }
  222. break;
  223. }
  224. } else if (n < 0) {
  225. if (ff_neterrno() == FF_NETERROR(EINTR))
  226. continue;
  227. return AVERROR(EIO);
  228. }
  229. }
  230. #endif
  231. return len;
  232. }
  233. static int rtp_write(URLContext *h, uint8_t *buf, int size)
  234. {
  235. RTPContext *s = h->priv_data;
  236. int ret;
  237. URLContext *hd;
  238. if (buf[1] >= 200 && buf[1] <= 204) {
  239. /* RTCP payload type */
  240. hd = s->rtcp_hd;
  241. } else {
  242. /* RTP payload type */
  243. hd = s->rtp_hd;
  244. }
  245. ret = url_write(hd, buf, size);
  246. #if 0
  247. {
  248. struct timespec ts;
  249. ts.tv_sec = 0;
  250. ts.tv_nsec = 10 * 1000000;
  251. nanosleep(&ts, NULL);
  252. }
  253. #endif
  254. return ret;
  255. }
  256. static int rtp_close(URLContext *h)
  257. {
  258. RTPContext *s = h->priv_data;
  259. url_close(s->rtp_hd);
  260. url_close(s->rtcp_hd);
  261. av_free(s);
  262. return 0;
  263. }
  264. /**
  265. * Return the local port used by the RTP connection
  266. * @param s1 media file context
  267. * @return the local port number
  268. */
  269. int rtp_get_local_port(URLContext *h)
  270. {
  271. RTPContext *s = h->priv_data;
  272. return udp_get_local_port(s->rtp_hd);
  273. }
  274. #if (LIBAVFORMAT_VERSION_MAJOR <= 52)
  275. /**
  276. * Return the rtp and rtcp file handles for select() usage to wait for
  277. * several RTP streams at the same time.
  278. * @param h media file context
  279. */
  280. void rtp_get_file_handles(URLContext *h, int *prtp_fd, int *prtcp_fd)
  281. {
  282. RTPContext *s = h->priv_data;
  283. *prtp_fd = s->rtp_fd;
  284. *prtcp_fd = s->rtcp_fd;
  285. }
  286. #endif
  287. static int rtp_get_file_handle(URLContext *h)
  288. {
  289. RTPContext *s = h->priv_data;
  290. return s->rtp_fd;
  291. }
  292. URLProtocol rtp_protocol = {
  293. "rtp",
  294. rtp_open,
  295. rtp_read,
  296. rtp_write,
  297. NULL, /* seek */
  298. rtp_close,
  299. .url_get_file_handle = rtp_get_file_handle,
  300. };