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.

259 lines
6.9KB

  1. /*
  2. * UDP prototype streaming system
  3. * Copyright (c) 2000, 2001, 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. typedef struct {
  27. int udp_fd;
  28. int ttl;
  29. int is_multicast;
  30. int local_port;
  31. struct ip_mreq mreq;
  32. struct sockaddr_in dest_addr;
  33. } UDPContext;
  34. #define UDP_TX_BUF_SIZE 32768
  35. /**
  36. * If no filename is given to av_open_input_file because you want to
  37. * get the local port first, then you must call this function to set
  38. * the remote server address.
  39. *
  40. * url syntax: udp://host:port[?option=val...]
  41. * option: 'multicast=1' : enable multicast
  42. * 'ttl=n' : set the ttl value (for multicast only)
  43. * 'localport=n' : set the local port
  44. *
  45. * @param s1 media file context
  46. * @param uri of the remote server
  47. * @return zero if no error.
  48. */
  49. int udp_set_remote_url(URLContext *h, const char *uri)
  50. {
  51. UDPContext *s = h->priv_data;
  52. char hostname[256];
  53. int port;
  54. url_split(NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
  55. /* set the destination address */
  56. if (resolve_host(&s->dest_addr.sin_addr, hostname) < 0)
  57. return -EIO;
  58. s->dest_addr.sin_family = AF_INET;
  59. s->dest_addr.sin_port = htons(port);
  60. return 0;
  61. }
  62. /**
  63. * Return the local port used by the UDP connexion
  64. * @param s1 media file context
  65. * @return the local port number
  66. */
  67. int udp_get_local_port(URLContext *h)
  68. {
  69. UDPContext *s = h->priv_data;
  70. return s->local_port;
  71. }
  72. /**
  73. * Return the udp file handle for select() usage to wait for several RTP
  74. * streams at the same time.
  75. * @param h media file context
  76. */
  77. int udp_get_file_handle(URLContext *h)
  78. {
  79. UDPContext *s = h->priv_data;
  80. return s->udp_fd;
  81. }
  82. /* put it in UDP context */
  83. /* return non zero if error */
  84. static int udp_open(URLContext *h, const char *uri, int flags)
  85. {
  86. struct sockaddr_in my_addr, my_addr1;
  87. char hostname[1024];
  88. int port, udp_fd = -1, tmp;
  89. UDPContext *s = NULL;
  90. int is_output, len;
  91. const char *p;
  92. char buf[256];
  93. h->is_streamed = 1;
  94. is_output = (flags & URL_WRONLY);
  95. s = av_malloc(sizeof(UDPContext));
  96. if (!s)
  97. return -ENOMEM;
  98. h->priv_data = s;
  99. s->ttl = 16;
  100. s->is_multicast = 0;
  101. p = strchr(uri, '?');
  102. if (p) {
  103. s->is_multicast = find_info_tag(buf, sizeof(buf), "multicast", p);
  104. if (find_info_tag(buf, sizeof(buf), "ttl", p)) {
  105. s->ttl = strtol(buf, NULL, 10);
  106. }
  107. if (find_info_tag(buf, sizeof(buf), "localport", p)) {
  108. s->local_port = strtol(buf, NULL, 10);
  109. }
  110. }
  111. /* fill the dest addr */
  112. url_split(NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
  113. /* XXX: fix url_split */
  114. if (hostname[0] == '\0' || hostname[0] == '?') {
  115. /* only accepts null hostname if input */
  116. if (s->is_multicast || (flags & URL_WRONLY))
  117. goto fail;
  118. } else {
  119. udp_set_remote_url(h, uri);
  120. }
  121. udp_fd = socket(PF_INET, SOCK_DGRAM, 0);
  122. if (udp_fd < 0)
  123. goto fail;
  124. if (s->is_multicast && !(h->flags & URL_WRONLY)) {
  125. /* special case: the bind must be done on the multicast address */
  126. my_addr = s->dest_addr;
  127. } else {
  128. my_addr.sin_family = AF_INET;
  129. my_addr.sin_port = htons(s->local_port);
  130. my_addr.sin_addr.s_addr = htonl (INADDR_ANY);
  131. }
  132. /* the bind is needed to give a port to the socket now */
  133. if (bind(udp_fd,(struct sockaddr *)&my_addr, sizeof(my_addr)) < 0)
  134. goto fail;
  135. len = sizeof(my_addr1);
  136. getsockname(udp_fd, (struct sockaddr *)&my_addr1, &len);
  137. s->local_port = ntohs(my_addr1.sin_port);
  138. if (s->is_multicast) {
  139. if (h->flags & URL_WRONLY) {
  140. /* output */
  141. if (setsockopt(udp_fd, IPPROTO_IP, IP_MULTICAST_TTL,
  142. &s->ttl, sizeof(s->ttl)) < 0) {
  143. perror("IP_MULTICAST_TTL");
  144. goto fail;
  145. }
  146. } else {
  147. /* input */
  148. memset(&s->mreq, 0, sizeof(s->mreq));
  149. s->mreq.imr_multiaddr = s->dest_addr.sin_addr;
  150. s->mreq.imr_interface.s_addr = htonl (INADDR_ANY);
  151. if (setsockopt(udp_fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
  152. &s->mreq, sizeof(s->mreq)) < 0) {
  153. perror("rtp: IP_ADD_MEMBERSHIP");
  154. goto fail;
  155. }
  156. }
  157. }
  158. if (is_output) {
  159. /* limit the tx buf size to limit latency */
  160. tmp = UDP_TX_BUF_SIZE;
  161. if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) {
  162. perror("setsockopt sndbuf");
  163. goto fail;
  164. }
  165. }
  166. s->udp_fd = udp_fd;
  167. h->max_packet_size = 1472; /* XXX: probe it ? */
  168. return 0;
  169. fail:
  170. if (udp_fd >= 0)
  171. close(udp_fd);
  172. av_free(s);
  173. return -EIO;
  174. }
  175. static int udp_read(URLContext *h, UINT8 *buf, int size)
  176. {
  177. UDPContext *s = h->priv_data;
  178. struct sockaddr_in from;
  179. int from_len, len;
  180. for(;;) {
  181. from_len = sizeof(from);
  182. len = recvfrom (s->udp_fd, buf, size, 0,
  183. (struct sockaddr *)&from, &from_len);
  184. if (len < 0) {
  185. if (errno != EAGAIN && errno != EINTR)
  186. return -EIO;
  187. } else {
  188. break;
  189. }
  190. }
  191. return len;
  192. }
  193. static int udp_write(URLContext *h, UINT8 *buf, int size)
  194. {
  195. UDPContext *s = h->priv_data;
  196. int ret;
  197. for(;;) {
  198. ret = sendto (s->udp_fd, buf, size, 0,
  199. (struct sockaddr *) &s->dest_addr,
  200. sizeof (s->dest_addr));
  201. if (ret < 0) {
  202. if (errno != EINTR && errno != EAGAIN)
  203. return -EIO;
  204. } else {
  205. break;
  206. }
  207. }
  208. return size;
  209. }
  210. static int udp_close(URLContext *h)
  211. {
  212. UDPContext *s = h->priv_data;
  213. if (s->is_multicast && !(h->flags & URL_WRONLY)) {
  214. if (setsockopt(s->udp_fd, IPPROTO_IP, IP_DROP_MEMBERSHIP,
  215. &s->mreq, sizeof(s->mreq)) < 0) {
  216. perror("IP_DROP_MEMBERSHIP");
  217. }
  218. }
  219. close(s->udp_fd);
  220. av_free(s);
  221. return 0;
  222. }
  223. URLProtocol udp_protocol = {
  224. "udp",
  225. udp_open,
  226. udp_read,
  227. udp_write,
  228. NULL, /* seek */
  229. udp_close,
  230. };