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.

273 lines
7.2KB

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