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.

277 lines
7.3KB

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