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.

297 lines
7.9KB

  1. /*
  2. * RTP network protocol
  3. * Copyright (c) 2002 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include "avformat.h"
  20. #include <unistd.h>
  21. #include <stdarg.h>
  22. #include <sys/types.h>
  23. #include <sys/socket.h>
  24. #include <netinet/in.h>
  25. #include <arpa/inet.h>
  26. #include <netdb.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, 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. 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. pstrcat(buf, buf_size, "&");
  67. else
  68. pstrcat(buf, buf_size, "?");
  69. vsnprintf(buf1, sizeof(buf1), fmt, ap);
  70. pstrcat(buf, buf_size, buf1);
  71. va_end(ap);
  72. }
  73. 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", multicast);
  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 -ENOMEM;
  104. h->priv_data = s;
  105. url_split(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 connexion 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 -EIO;
  146. }
  147. static int rtp_read(URLContext *h, UINT8 *buf, int size)
  148. {
  149. RTPContext *s = h->priv_data;
  150. struct sockaddr_in from;
  151. int from_len, 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 (errno == EAGAIN || errno == EINTR)
  160. continue;
  161. return -EIO;
  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 (errno == EAGAIN || errno == EINTR)
  183. continue;
  184. return -EIO;
  185. }
  186. break;
  187. }
  188. /* then RTP */
  189. if (FD_ISSET(s->rtp_fd, &rfds)) {
  190. from_len = sizeof(from);
  191. len = recvfrom (s->rtp_fd, buf, size, 0,
  192. (struct sockaddr *)&from, &from_len);
  193. if (len < 0) {
  194. if (errno == EAGAIN || errno == EINTR)
  195. continue;
  196. return -EIO;
  197. }
  198. break;
  199. }
  200. }
  201. }
  202. #endif
  203. return len;
  204. }
  205. static int rtp_write(URLContext *h, UINT8 *buf, int size)
  206. {
  207. RTPContext *s = h->priv_data;
  208. int ret;
  209. URLContext *hd;
  210. if (buf[1] >= 200 && buf[1] <= 204) {
  211. /* RTCP payload type */
  212. hd = s->rtcp_hd;
  213. } else {
  214. /* RTP payload type */
  215. hd = s->rtp_hd;
  216. }
  217. ret = url_write(hd, buf, size);
  218. #if 0
  219. {
  220. struct timespec ts;
  221. ts.tv_sec = 0;
  222. ts.tv_nsec = 10 * 1000000;
  223. nanosleep(&ts, NULL);
  224. }
  225. #endif
  226. return ret;
  227. }
  228. static int rtp_close(URLContext *h)
  229. {
  230. RTPContext *s = h->priv_data;
  231. url_close(s->rtp_hd);
  232. url_close(s->rtcp_hd);
  233. av_free(s);
  234. return 0;
  235. }
  236. /**
  237. * Return the local port used by the RTP connexion
  238. * @param s1 media file context
  239. * @return the local port number
  240. */
  241. int rtp_get_local_port(URLContext *h)
  242. {
  243. RTPContext *s = h->priv_data;
  244. return udp_get_local_port(s->rtp_hd);
  245. }
  246. /**
  247. * Return the rtp and rtcp file handles for select() usage to wait for several RTP
  248. * streams at the same time.
  249. * @param h media file context
  250. */
  251. void rtp_get_file_handles(URLContext *h, int *prtp_fd, int *prtcp_fd)
  252. {
  253. RTPContext *s = h->priv_data;
  254. *prtp_fd = s->rtp_fd;
  255. *prtcp_fd = s->rtcp_fd;
  256. }
  257. URLProtocol rtp_protocol = {
  258. "rtp",
  259. rtp_open,
  260. rtp_read,
  261. rtp_write,
  262. NULL, /* seek */
  263. rtp_close,
  264. };