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.

278 lines
7.4KB

  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. s->local_port = 0;
  108. p = strchr(uri, '?');
  109. if (p) {
  110. s->is_multicast = find_info_tag(buf, sizeof(buf), "multicast", p);
  111. if (find_info_tag(buf, sizeof(buf), "ttl", p)) {
  112. s->ttl = strtol(buf, NULL, 10);
  113. }
  114. if (find_info_tag(buf, sizeof(buf), "localport", p)) {
  115. s->local_port = strtol(buf, NULL, 10);
  116. }
  117. if (find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
  118. h->max_packet_size = strtol(buf, NULL, 10);
  119. }
  120. }
  121. /* fill the dest addr */
  122. url_split(NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
  123. /* XXX: fix url_split */
  124. if (hostname[0] == '\0' || hostname[0] == '?') {
  125. /* only accepts null hostname if input */
  126. if (s->is_multicast || (flags & URL_WRONLY))
  127. goto fail;
  128. } else {
  129. udp_set_remote_url(h, uri);
  130. }
  131. udp_fd = socket(PF_INET, SOCK_DGRAM, 0);
  132. if (udp_fd < 0)
  133. goto fail;
  134. my_addr.sin_family = AF_INET;
  135. my_addr.sin_addr.s_addr = htonl (INADDR_ANY);
  136. if (s->is_multicast && !(h->flags & URL_WRONLY)) {
  137. /* special case: the bind must be done on the multicast address port */
  138. my_addr.sin_port = s->dest_addr.sin_port;
  139. } else {
  140. my_addr.sin_port = htons(s->local_port);
  141. }
  142. /* the bind is needed to give a port to the socket now */
  143. if (bind(udp_fd,(struct sockaddr *)&my_addr, sizeof(my_addr)) < 0)
  144. goto fail;
  145. len = sizeof(my_addr1);
  146. getsockname(udp_fd, (struct sockaddr *)&my_addr1, &len);
  147. s->local_port = ntohs(my_addr1.sin_port);
  148. #ifndef CONFIG_BEOS_NETSERVER
  149. if (s->is_multicast) {
  150. if (h->flags & URL_WRONLY) {
  151. /* output */
  152. if (setsockopt(udp_fd, IPPROTO_IP, IP_MULTICAST_TTL,
  153. &s->ttl, sizeof(s->ttl)) < 0) {
  154. perror("IP_MULTICAST_TTL");
  155. goto fail;
  156. }
  157. } else {
  158. /* input */
  159. memset(&s->mreq, 0, sizeof(s->mreq));
  160. s->mreq.imr_multiaddr = s->dest_addr.sin_addr;
  161. s->mreq.imr_interface.s_addr = htonl (INADDR_ANY);
  162. if (setsockopt(udp_fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
  163. &s->mreq, sizeof(s->mreq)) < 0) {
  164. perror("rtp: IP_ADD_MEMBERSHIP");
  165. goto fail;
  166. }
  167. }
  168. }
  169. #endif
  170. if (is_output) {
  171. /* limit the tx buf size to limit latency */
  172. tmp = UDP_TX_BUF_SIZE;
  173. if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) {
  174. perror("setsockopt sndbuf");
  175. goto fail;
  176. }
  177. }
  178. s->udp_fd = udp_fd;
  179. return 0;
  180. fail:
  181. if (udp_fd >= 0)
  182. #ifdef CONFIG_BEOS_NETSERVER
  183. closesocket(udp_fd);
  184. #else
  185. close(udp_fd);
  186. #endif
  187. av_free(s);
  188. return -EIO;
  189. }
  190. static int udp_read(URLContext *h, uint8_t *buf, int size)
  191. {
  192. UDPContext *s = h->priv_data;
  193. struct sockaddr_in from;
  194. int from_len, len;
  195. for(;;) {
  196. from_len = sizeof(from);
  197. len = recvfrom (s->udp_fd, buf, size, 0,
  198. (struct sockaddr *)&from, &from_len);
  199. if (len < 0) {
  200. if (errno != EAGAIN && errno != EINTR)
  201. return -EIO;
  202. } else {
  203. break;
  204. }
  205. }
  206. return len;
  207. }
  208. static int udp_write(URLContext *h, uint8_t *buf, int size)
  209. {
  210. UDPContext *s = h->priv_data;
  211. int ret;
  212. for(;;) {
  213. ret = sendto (s->udp_fd, buf, size, 0,
  214. (struct sockaddr *) &s->dest_addr,
  215. sizeof (s->dest_addr));
  216. if (ret < 0) {
  217. if (errno != EINTR && errno != EAGAIN)
  218. return -EIO;
  219. } else {
  220. break;
  221. }
  222. }
  223. return size;
  224. }
  225. static int udp_close(URLContext *h)
  226. {
  227. UDPContext *s = h->priv_data;
  228. #ifndef CONFIG_BEOS_NETSERVER
  229. if (s->is_multicast && !(h->flags & URL_WRONLY)) {
  230. if (setsockopt(s->udp_fd, IPPROTO_IP, IP_DROP_MEMBERSHIP,
  231. &s->mreq, sizeof(s->mreq)) < 0) {
  232. perror("IP_DROP_MEMBERSHIP");
  233. }
  234. }
  235. close(s->udp_fd);
  236. #else
  237. closesocket(s->udp_fd);
  238. #endif
  239. av_free(s);
  240. return 0;
  241. }
  242. URLProtocol udp_protocol = {
  243. "udp",
  244. udp_open,
  245. udp_read,
  246. udp_write,
  247. NULL, /* seek */
  248. udp_close,
  249. };