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.0KB

  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 <sys/types.h>
  25. #include <sys/socket.h>
  26. #include <netinet/in.h>
  27. #include <arpa/inet.h>
  28. #include <netdb.h>
  29. #include <fcntl.h>
  30. #define RTP_TX_BUF_SIZE (64 * 1024)
  31. #define RTP_RX_BUF_SIZE (128 * 1024)
  32. typedef struct RTPContext {
  33. URLContext *rtp_hd, *rtcp_hd;
  34. int rtp_fd, rtcp_fd;
  35. } RTPContext;
  36. /**
  37. * If no filename is given to av_open_input_file because you want to
  38. * get the local port first, then you must call this function to set
  39. * the remote server address.
  40. *
  41. * @param s1 media file context
  42. * @param uri of the remote server
  43. * @return zero if no error.
  44. */
  45. int rtp_set_remote_url(URLContext *h, const char *uri)
  46. {
  47. RTPContext *s = h->priv_data;
  48. char hostname[256];
  49. int port;
  50. char buf[1024];
  51. char path[1024];
  52. url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port,
  53. path, sizeof(path), uri);
  54. snprintf(buf, sizeof(buf), "udp://%s:%d%s", hostname, port, path);
  55. udp_set_remote_url(s->rtp_hd, buf);
  56. snprintf(buf, sizeof(buf), "udp://%s:%d%s", hostname, port + 1, path);
  57. udp_set_remote_url(s->rtcp_hd, buf);
  58. return 0;
  59. }
  60. /* add option to url of the form:
  61. "http://host:port/path?option1=val1&option2=val2... */
  62. static void url_add_option(char *buf, int buf_size, const char *fmt, ...)
  63. {
  64. char buf1[1024];
  65. va_list ap;
  66. va_start(ap, fmt);
  67. if (strchr(buf, '?'))
  68. pstrcat(buf, buf_size, "&");
  69. else
  70. pstrcat(buf, buf_size, "?");
  71. vsnprintf(buf1, sizeof(buf1), fmt, ap);
  72. pstrcat(buf, buf_size, buf1);
  73. va_end(ap);
  74. }
  75. static void build_udp_url(char *buf, int buf_size,
  76. const char *hostname, int port,
  77. int local_port, int multicast, int ttl)
  78. {
  79. snprintf(buf, buf_size, "udp://%s:%d", hostname, port);
  80. if (local_port >= 0)
  81. url_add_option(buf, buf_size, "localport=%d", local_port);
  82. if (multicast)
  83. url_add_option(buf, buf_size, "multicast=1", multicast);
  84. if (ttl >= 0)
  85. url_add_option(buf, buf_size, "ttl=%d", ttl);
  86. }
  87. /*
  88. * url syntax: rtp://host:port[?option=val...]
  89. * option: 'multicast=1' : enable multicast
  90. * 'ttl=n' : set the ttl value (for multicast only)
  91. * 'localport=n' : set the local port to n
  92. *
  93. */
  94. static int rtp_open(URLContext *h, const char *uri, int flags)
  95. {
  96. RTPContext *s;
  97. int port, is_output, is_multicast, ttl, local_port;
  98. char hostname[256];
  99. char buf[1024];
  100. char path[1024];
  101. const char *p;
  102. is_output = (flags & URL_WRONLY);
  103. s = av_mallocz(sizeof(RTPContext));
  104. if (!s)
  105. return -ENOMEM;
  106. h->priv_data = s;
  107. url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port,
  108. path, sizeof(path), uri);
  109. /* extract parameters */
  110. is_multicast = 0;
  111. ttl = -1;
  112. local_port = -1;
  113. p = strchr(uri, '?');
  114. if (p) {
  115. is_multicast = find_info_tag(buf, sizeof(buf), "multicast", p);
  116. if (find_info_tag(buf, sizeof(buf), "ttl", p)) {
  117. ttl = strtol(buf, NULL, 10);
  118. }
  119. if (find_info_tag(buf, sizeof(buf), "localport", p)) {
  120. local_port = strtol(buf, NULL, 10);
  121. }
  122. }
  123. build_udp_url(buf, sizeof(buf),
  124. hostname, port, local_port, is_multicast, ttl);
  125. if (url_open(&s->rtp_hd, buf, flags) < 0)
  126. goto fail;
  127. local_port = udp_get_local_port(s->rtp_hd);
  128. /* XXX: need to open another connexion if the port is not even */
  129. /* well, should suppress localport in path */
  130. build_udp_url(buf, sizeof(buf),
  131. hostname, port + 1, local_port + 1, is_multicast, ttl);
  132. if (url_open(&s->rtcp_hd, buf, flags) < 0)
  133. goto fail;
  134. /* just to ease handle access. XXX: need to suppress direct handle
  135. access */
  136. s->rtp_fd = udp_get_file_handle(s->rtp_hd);
  137. s->rtcp_fd = udp_get_file_handle(s->rtcp_hd);
  138. h->max_packet_size = url_get_max_packet_size(s->rtp_hd);
  139. h->is_streamed = 1;
  140. return 0;
  141. fail:
  142. if (s->rtp_hd)
  143. url_close(s->rtp_hd);
  144. if (s->rtcp_hd)
  145. url_close(s->rtcp_hd);
  146. av_free(s);
  147. return AVERROR_IO;
  148. }
  149. static int rtp_read(URLContext *h, uint8_t *buf, int size)
  150. {
  151. RTPContext *s = h->priv_data;
  152. struct sockaddr_in from;
  153. socklen_t from_len;
  154. int len, fd_max, n;
  155. fd_set rfds;
  156. #if 0
  157. for(;;) {
  158. from_len = sizeof(from);
  159. len = recvfrom (s->rtp_fd, buf, size, 0,
  160. (struct sockaddr *)&from, &from_len);
  161. if (len < 0) {
  162. if (errno == EAGAIN || errno == EINTR)
  163. continue;
  164. return AVERROR_IO;
  165. }
  166. break;
  167. }
  168. #else
  169. for(;;) {
  170. /* build fdset to listen to RTP and RTCP packets */
  171. FD_ZERO(&rfds);
  172. fd_max = s->rtp_fd;
  173. FD_SET(s->rtp_fd, &rfds);
  174. if (s->rtcp_fd > fd_max)
  175. fd_max = s->rtcp_fd;
  176. FD_SET(s->rtcp_fd, &rfds);
  177. n = select(fd_max + 1, &rfds, NULL, NULL, NULL);
  178. if (n > 0) {
  179. /* first try RTCP */
  180. if (FD_ISSET(s->rtcp_fd, &rfds)) {
  181. from_len = sizeof(from);
  182. len = recvfrom (s->rtcp_fd, buf, size, 0,
  183. (struct sockaddr *)&from, &from_len);
  184. if (len < 0) {
  185. if (errno == EAGAIN || errno == EINTR)
  186. continue;
  187. return AVERROR_IO;
  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 (errno == EAGAIN || errno == EINTR)
  198. continue;
  199. return AVERROR_IO;
  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 connexion
  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. };