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.

301 lines
8.1KB

  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. #include "avformat.h"
  22. #include "avstring.h"
  23. #include <unistd.h>
  24. #include <stdarg.h>
  25. #include "network.h"
  26. #include "os_support.h"
  27. #include <fcntl.h>
  28. #define RTP_TX_BUF_SIZE (64 * 1024)
  29. #define RTP_RX_BUF_SIZE (128 * 1024)
  30. typedef struct RTPContext {
  31. URLContext *rtp_hd, *rtcp_hd;
  32. int rtp_fd, rtcp_fd;
  33. } RTPContext;
  34. /**
  35. * If no filename is given to av_open_input_file because you want to
  36. * get the local port first, then you must call this function to set
  37. * the remote server address.
  38. *
  39. * @param s1 media file context
  40. * @param uri of the remote server
  41. * @return zero if no error.
  42. */
  43. int rtp_set_remote_url(URLContext *h, const char *uri)
  44. {
  45. RTPContext *s = h->priv_data;
  46. char hostname[256];
  47. int port;
  48. char buf[1024];
  49. char path[1024];
  50. url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port,
  51. path, sizeof(path), uri);
  52. snprintf(buf, sizeof(buf), "udp://%s:%d%s", hostname, port, path);
  53. udp_set_remote_url(s->rtp_hd, buf);
  54. snprintf(buf, sizeof(buf), "udp://%s:%d%s", hostname, port + 1, path);
  55. udp_set_remote_url(s->rtcp_hd, buf);
  56. return 0;
  57. }
  58. /* add option to url of the form:
  59. "http://host:port/path?option1=val1&option2=val2... */
  60. static void url_add_option(char *buf, int buf_size, const char *fmt, ...)
  61. {
  62. char buf1[1024];
  63. va_list ap;
  64. va_start(ap, fmt);
  65. if (strchr(buf, '?'))
  66. av_strlcat(buf, "&", buf_size);
  67. else
  68. av_strlcat(buf, "?", buf_size);
  69. vsnprintf(buf1, sizeof(buf1), fmt, ap);
  70. av_strlcat(buf, buf1, buf_size);
  71. va_end(ap);
  72. }
  73. static void build_udp_url(char *buf, int buf_size,
  74. const char *hostname, int port,
  75. int local_port, int multicast, int ttl)
  76. {
  77. snprintf(buf, buf_size, "udp://%s:%d", hostname, port);
  78. if (local_port >= 0)
  79. url_add_option(buf, buf_size, "localport=%d", local_port);
  80. if (multicast)
  81. url_add_option(buf, buf_size, "multicast=1");
  82. if (ttl >= 0)
  83. url_add_option(buf, buf_size, "ttl=%d", ttl);
  84. }
  85. /*
  86. * url syntax: rtp://host:port[?option=val...]
  87. * option: 'multicast=1' : enable multicast
  88. * 'ttl=n' : set the ttl value (for multicast only)
  89. * 'localport=n' : set the local port to n
  90. *
  91. */
  92. static int rtp_open(URLContext *h, const char *uri, int flags)
  93. {
  94. RTPContext *s;
  95. int port, is_output, is_multicast, ttl, local_port;
  96. char hostname[256];
  97. char buf[1024];
  98. char path[1024];
  99. const char *p;
  100. is_output = (flags & URL_WRONLY);
  101. s = av_mallocz(sizeof(RTPContext));
  102. if (!s)
  103. return AVERROR(ENOMEM);
  104. h->priv_data = s;
  105. url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port,
  106. path, sizeof(path), uri);
  107. /* extract parameters */
  108. is_multicast = 0;
  109. ttl = -1;
  110. local_port = -1;
  111. p = strchr(uri, '?');
  112. if (p) {
  113. is_multicast = find_info_tag(buf, sizeof(buf), "multicast", p);
  114. if (find_info_tag(buf, sizeof(buf), "ttl", p)) {
  115. ttl = strtol(buf, NULL, 10);
  116. }
  117. if (find_info_tag(buf, sizeof(buf), "localport", p)) {
  118. local_port = strtol(buf, NULL, 10);
  119. }
  120. }
  121. build_udp_url(buf, sizeof(buf),
  122. hostname, port, local_port, is_multicast, ttl);
  123. if (url_open(&s->rtp_hd, buf, flags) < 0)
  124. goto fail;
  125. local_port = udp_get_local_port(s->rtp_hd);
  126. /* XXX: need to open another connection if the port is not even */
  127. /* well, should suppress localport in path */
  128. build_udp_url(buf, sizeof(buf),
  129. hostname, port + 1, local_port + 1, is_multicast, ttl);
  130. if (url_open(&s->rtcp_hd, buf, flags) < 0)
  131. goto fail;
  132. /* just to ease handle access. XXX: need to suppress direct handle
  133. access */
  134. s->rtp_fd = udp_get_file_handle(s->rtp_hd);
  135. s->rtcp_fd = udp_get_file_handle(s->rtcp_hd);
  136. h->max_packet_size = url_get_max_packet_size(s->rtp_hd);
  137. h->is_streamed = 1;
  138. return 0;
  139. fail:
  140. if (s->rtp_hd)
  141. url_close(s->rtp_hd);
  142. if (s->rtcp_hd)
  143. url_close(s->rtcp_hd);
  144. av_free(s);
  145. return AVERROR(EIO);
  146. }
  147. static int rtp_read(URLContext *h, uint8_t *buf, int size)
  148. {
  149. RTPContext *s = h->priv_data;
  150. struct sockaddr_in from;
  151. socklen_t from_len;
  152. int len, fd_max, n;
  153. fd_set rfds;
  154. #if 0
  155. for(;;) {
  156. from_len = sizeof(from);
  157. len = recvfrom (s->rtp_fd, buf, size, 0,
  158. (struct sockaddr *)&from, &from_len);
  159. if (len < 0) {
  160. if (ff_neterrno() == FF_NETERROR(EAGAIN) ||
  161. ff_neterrno() == FF_NETERROR(EINTR))
  162. continue;
  163. return AVERROR(EIO);
  164. }
  165. break;
  166. }
  167. #else
  168. for(;;) {
  169. /* build fdset to listen to RTP and RTCP packets */
  170. FD_ZERO(&rfds);
  171. fd_max = s->rtp_fd;
  172. FD_SET(s->rtp_fd, &rfds);
  173. if (s->rtcp_fd > fd_max)
  174. fd_max = s->rtcp_fd;
  175. FD_SET(s->rtcp_fd, &rfds);
  176. n = select(fd_max + 1, &rfds, NULL, NULL, NULL);
  177. if (n > 0) {
  178. /* first try RTCP */
  179. if (FD_ISSET(s->rtcp_fd, &rfds)) {
  180. from_len = sizeof(from);
  181. len = recvfrom (s->rtcp_fd, buf, size, 0,
  182. (struct sockaddr *)&from, &from_len);
  183. if (len < 0) {
  184. if (ff_neterrno() == FF_NETERROR(EAGAIN) ||
  185. ff_neterrno() == FF_NETERROR(EINTR))
  186. continue;
  187. return AVERROR(EIO);
  188. }
  189. break;
  190. }
  191. /* then RTP */
  192. if (FD_ISSET(s->rtp_fd, &rfds)) {
  193. from_len = sizeof(from);
  194. len = recvfrom (s->rtp_fd, buf, size, 0,
  195. (struct sockaddr *)&from, &from_len);
  196. if (len < 0) {
  197. if (ff_neterrno() == FF_NETERROR(EAGAIN) ||
  198. ff_neterrno() == FF_NETERROR(EINTR))
  199. continue;
  200. return AVERROR(EIO);
  201. }
  202. break;
  203. }
  204. }
  205. }
  206. #endif
  207. return len;
  208. }
  209. static int rtp_write(URLContext *h, uint8_t *buf, int size)
  210. {
  211. RTPContext *s = h->priv_data;
  212. int ret;
  213. URLContext *hd;
  214. if (buf[1] >= 200 && buf[1] <= 204) {
  215. /* RTCP payload type */
  216. hd = s->rtcp_hd;
  217. } else {
  218. /* RTP payload type */
  219. hd = s->rtp_hd;
  220. }
  221. ret = url_write(hd, buf, size);
  222. #if 0
  223. {
  224. struct timespec ts;
  225. ts.tv_sec = 0;
  226. ts.tv_nsec = 10 * 1000000;
  227. nanosleep(&ts, NULL);
  228. }
  229. #endif
  230. return ret;
  231. }
  232. static int rtp_close(URLContext *h)
  233. {
  234. RTPContext *s = h->priv_data;
  235. url_close(s->rtp_hd);
  236. url_close(s->rtcp_hd);
  237. av_free(s);
  238. return 0;
  239. }
  240. /**
  241. * Return the local port used by the RTP connection
  242. * @param s1 media file context
  243. * @return the local port number
  244. */
  245. int rtp_get_local_port(URLContext *h)
  246. {
  247. RTPContext *s = h->priv_data;
  248. return udp_get_local_port(s->rtp_hd);
  249. }
  250. /**
  251. * Return the rtp and rtcp file handles for select() usage to wait for several RTP
  252. * streams at the same time.
  253. * @param h media file context
  254. */
  255. void rtp_get_file_handles(URLContext *h, int *prtp_fd, int *prtcp_fd)
  256. {
  257. RTPContext *s = h->priv_data;
  258. *prtp_fd = s->rtp_fd;
  259. *prtcp_fd = s->rtcp_fd;
  260. }
  261. URLProtocol rtp_protocol = {
  262. "rtp",
  263. rtp_open,
  264. rtp_read,
  265. rtp_write,
  266. NULL, /* seek */
  267. rtp_close,
  268. };