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.

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