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.

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