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.

302 lines
8.0KB

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