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.

250 lines
6.6KB

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