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.

296 lines
7.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. #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", multicast);
  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 connexion 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 (errno == EAGAIN || errno == EINTR)
  159. continue;
  160. return AVERROR_IO;
  161. }
  162. break;
  163. }
  164. #else
  165. for(;;) {
  166. /* build fdset to listen to RTP and RTCP packets */
  167. FD_ZERO(&rfds);
  168. fd_max = s->rtp_fd;
  169. FD_SET(s->rtp_fd, &rfds);
  170. if (s->rtcp_fd > fd_max)
  171. fd_max = s->rtcp_fd;
  172. FD_SET(s->rtcp_fd, &rfds);
  173. n = select(fd_max + 1, &rfds, NULL, NULL, NULL);
  174. if (n > 0) {
  175. /* first try RTCP */
  176. if (FD_ISSET(s->rtcp_fd, &rfds)) {
  177. from_len = sizeof(from);
  178. len = recvfrom (s->rtcp_fd, buf, size, 0,
  179. (struct sockaddr *)&from, &from_len);
  180. if (len < 0) {
  181. if (errno == EAGAIN || errno == EINTR)
  182. continue;
  183. return AVERROR_IO;
  184. }
  185. break;
  186. }
  187. /* then RTP */
  188. if (FD_ISSET(s->rtp_fd, &rfds)) {
  189. from_len = sizeof(from);
  190. len = recvfrom (s->rtp_fd, buf, size, 0,
  191. (struct sockaddr *)&from, &from_len);
  192. if (len < 0) {
  193. if (errno == EAGAIN || errno == EINTR)
  194. continue;
  195. return AVERROR_IO;
  196. }
  197. break;
  198. }
  199. }
  200. }
  201. #endif
  202. return len;
  203. }
  204. static int rtp_write(URLContext *h, uint8_t *buf, int size)
  205. {
  206. RTPContext *s = h->priv_data;
  207. int ret;
  208. URLContext *hd;
  209. if (buf[1] >= 200 && buf[1] <= 204) {
  210. /* RTCP payload type */
  211. hd = s->rtcp_hd;
  212. } else {
  213. /* RTP payload type */
  214. hd = s->rtp_hd;
  215. }
  216. ret = url_write(hd, buf, size);
  217. #if 0
  218. {
  219. struct timespec ts;
  220. ts.tv_sec = 0;
  221. ts.tv_nsec = 10 * 1000000;
  222. nanosleep(&ts, NULL);
  223. }
  224. #endif
  225. return ret;
  226. }
  227. static int rtp_close(URLContext *h)
  228. {
  229. RTPContext *s = h->priv_data;
  230. url_close(s->rtp_hd);
  231. url_close(s->rtcp_hd);
  232. av_free(s);
  233. return 0;
  234. }
  235. /**
  236. * Return the local port used by the RTP connexion
  237. * @param s1 media file context
  238. * @return the local port number
  239. */
  240. int rtp_get_local_port(URLContext *h)
  241. {
  242. RTPContext *s = h->priv_data;
  243. return udp_get_local_port(s->rtp_hd);
  244. }
  245. /**
  246. * Return the rtp and rtcp file handles for select() usage to wait for several RTP
  247. * streams at the same time.
  248. * @param h media file context
  249. */
  250. void rtp_get_file_handles(URLContext *h, int *prtp_fd, int *prtcp_fd)
  251. {
  252. RTPContext *s = h->priv_data;
  253. *prtp_fd = s->rtp_fd;
  254. *prtcp_fd = s->rtcp_fd;
  255. }
  256. URLProtocol rtp_protocol = {
  257. "rtp",
  258. rtp_open,
  259. rtp_read,
  260. rtp_write,
  261. NULL, /* seek */
  262. rtp_close,
  263. };