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.

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