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.

304 lines
8.0KB

  1. /*
  2. * RTP network protocol
  3. * Copyright (c) 2002 Fabrice Bellard.
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "avformat.h"
  22. #include <unistd.h>
  23. #include <stdarg.h>
  24. #include <sys/types.h>
  25. #include <sys/socket.h>
  26. #include <netinet/in.h>
  27. #ifndef __BEOS__
  28. # include <arpa/inet.h>
  29. #else
  30. # include "barpainet.h"
  31. #endif
  32. #include <netdb.h>
  33. #include <fcntl.h>
  34. #define RTP_TX_BUF_SIZE (64 * 1024)
  35. #define RTP_RX_BUF_SIZE (128 * 1024)
  36. typedef struct RTPContext {
  37. URLContext *rtp_hd, *rtcp_hd;
  38. int rtp_fd, rtcp_fd;
  39. } RTPContext;
  40. /**
  41. * If no filename is given to av_open_input_file because you want to
  42. * get the local port first, then you must call this function to set
  43. * the remote server address.
  44. *
  45. * @param s1 media file context
  46. * @param uri of the remote server
  47. * @return zero if no error.
  48. */
  49. int rtp_set_remote_url(URLContext *h, const char *uri)
  50. {
  51. RTPContext *s = h->priv_data;
  52. char hostname[256];
  53. int port;
  54. char buf[1024];
  55. char path[1024];
  56. url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port,
  57. path, sizeof(path), uri);
  58. snprintf(buf, sizeof(buf), "udp://%s:%d%s", hostname, port, path);
  59. udp_set_remote_url(s->rtp_hd, buf);
  60. snprintf(buf, sizeof(buf), "udp://%s:%d%s", hostname, port + 1, path);
  61. udp_set_remote_url(s->rtcp_hd, buf);
  62. return 0;
  63. }
  64. /* add option to url of the form:
  65. "http://host:port/path?option1=val1&option2=val2... */
  66. static void url_add_option(char *buf, int buf_size, const char *fmt, ...)
  67. {
  68. char buf1[1024];
  69. va_list ap;
  70. va_start(ap, fmt);
  71. if (strchr(buf, '?'))
  72. pstrcat(buf, buf_size, "&");
  73. else
  74. pstrcat(buf, buf_size, "?");
  75. vsnprintf(buf1, sizeof(buf1), fmt, ap);
  76. pstrcat(buf, buf_size, buf1);
  77. va_end(ap);
  78. }
  79. static void build_udp_url(char *buf, int buf_size,
  80. const char *hostname, int port,
  81. int local_port, int multicast, int ttl)
  82. {
  83. snprintf(buf, buf_size, "udp://%s:%d", hostname, port);
  84. if (local_port >= 0)
  85. url_add_option(buf, buf_size, "localport=%d", local_port);
  86. if (multicast)
  87. url_add_option(buf, buf_size, "multicast=1", multicast);
  88. if (ttl >= 0)
  89. url_add_option(buf, buf_size, "ttl=%d", ttl);
  90. }
  91. /*
  92. * url syntax: rtp://host:port[?option=val...]
  93. * option: 'multicast=1' : enable multicast
  94. * 'ttl=n' : set the ttl value (for multicast only)
  95. * 'localport=n' : set the local port to n
  96. *
  97. */
  98. static int rtp_open(URLContext *h, const char *uri, int flags)
  99. {
  100. RTPContext *s;
  101. int port, is_output, is_multicast, ttl, local_port;
  102. char hostname[256];
  103. char buf[1024];
  104. char path[1024];
  105. const char *p;
  106. is_output = (flags & URL_WRONLY);
  107. s = av_mallocz(sizeof(RTPContext));
  108. if (!s)
  109. return -ENOMEM;
  110. h->priv_data = s;
  111. url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port,
  112. path, sizeof(path), uri);
  113. /* extract parameters */
  114. is_multicast = 0;
  115. ttl = -1;
  116. local_port = -1;
  117. p = strchr(uri, '?');
  118. if (p) {
  119. is_multicast = find_info_tag(buf, sizeof(buf), "multicast", p);
  120. if (find_info_tag(buf, sizeof(buf), "ttl", p)) {
  121. ttl = strtol(buf, NULL, 10);
  122. }
  123. if (find_info_tag(buf, sizeof(buf), "localport", p)) {
  124. local_port = strtol(buf, NULL, 10);
  125. }
  126. }
  127. build_udp_url(buf, sizeof(buf),
  128. hostname, port, local_port, is_multicast, ttl);
  129. if (url_open(&s->rtp_hd, buf, flags) < 0)
  130. goto fail;
  131. local_port = udp_get_local_port(s->rtp_hd);
  132. /* XXX: need to open another connexion if the port is not even */
  133. /* well, should suppress localport in path */
  134. build_udp_url(buf, sizeof(buf),
  135. hostname, port + 1, local_port + 1, is_multicast, ttl);
  136. if (url_open(&s->rtcp_hd, buf, flags) < 0)
  137. goto fail;
  138. /* just to ease handle access. XXX: need to suppress direct handle
  139. access */
  140. s->rtp_fd = udp_get_file_handle(s->rtp_hd);
  141. s->rtcp_fd = udp_get_file_handle(s->rtcp_hd);
  142. h->max_packet_size = url_get_max_packet_size(s->rtp_hd);
  143. h->is_streamed = 1;
  144. return 0;
  145. fail:
  146. if (s->rtp_hd)
  147. url_close(s->rtp_hd);
  148. if (s->rtcp_hd)
  149. url_close(s->rtcp_hd);
  150. av_free(s);
  151. return AVERROR_IO;
  152. }
  153. static int rtp_read(URLContext *h, uint8_t *buf, int size)
  154. {
  155. RTPContext *s = h->priv_data;
  156. struct sockaddr_in from;
  157. socklen_t from_len;
  158. int len, fd_max, n;
  159. fd_set rfds;
  160. #if 0
  161. for(;;) {
  162. from_len = sizeof(from);
  163. len = recvfrom (s->rtp_fd, buf, size, 0,
  164. (struct sockaddr *)&from, &from_len);
  165. if (len < 0) {
  166. if (errno == EAGAIN || errno == EINTR)
  167. continue;
  168. return AVERROR_IO;
  169. }
  170. break;
  171. }
  172. #else
  173. for(;;) {
  174. /* build fdset to listen to RTP and RTCP packets */
  175. FD_ZERO(&rfds);
  176. fd_max = s->rtp_fd;
  177. FD_SET(s->rtp_fd, &rfds);
  178. if (s->rtcp_fd > fd_max)
  179. fd_max = s->rtcp_fd;
  180. FD_SET(s->rtcp_fd, &rfds);
  181. n = select(fd_max + 1, &rfds, NULL, NULL, NULL);
  182. if (n > 0) {
  183. /* first try RTCP */
  184. if (FD_ISSET(s->rtcp_fd, &rfds)) {
  185. from_len = sizeof(from);
  186. len = recvfrom (s->rtcp_fd, buf, size, 0,
  187. (struct sockaddr *)&from, &from_len);
  188. if (len < 0) {
  189. if (errno == EAGAIN || errno == EINTR)
  190. continue;
  191. return AVERROR_IO;
  192. }
  193. break;
  194. }
  195. /* then RTP */
  196. if (FD_ISSET(s->rtp_fd, &rfds)) {
  197. from_len = sizeof(from);
  198. len = recvfrom (s->rtp_fd, buf, size, 0,
  199. (struct sockaddr *)&from, &from_len);
  200. if (len < 0) {
  201. if (errno == EAGAIN || errno == EINTR)
  202. continue;
  203. return AVERROR_IO;
  204. }
  205. break;
  206. }
  207. }
  208. }
  209. #endif
  210. return len;
  211. }
  212. static int rtp_write(URLContext *h, uint8_t *buf, int size)
  213. {
  214. RTPContext *s = h->priv_data;
  215. int ret;
  216. URLContext *hd;
  217. if (buf[1] >= 200 && buf[1] <= 204) {
  218. /* RTCP payload type */
  219. hd = s->rtcp_hd;
  220. } else {
  221. /* RTP payload type */
  222. hd = s->rtp_hd;
  223. }
  224. ret = url_write(hd, buf, size);
  225. #if 0
  226. {
  227. struct timespec ts;
  228. ts.tv_sec = 0;
  229. ts.tv_nsec = 10 * 1000000;
  230. nanosleep(&ts, NULL);
  231. }
  232. #endif
  233. return ret;
  234. }
  235. static int rtp_close(URLContext *h)
  236. {
  237. RTPContext *s = h->priv_data;
  238. url_close(s->rtp_hd);
  239. url_close(s->rtcp_hd);
  240. av_free(s);
  241. return 0;
  242. }
  243. /**
  244. * Return the local port used by the RTP connexion
  245. * @param s1 media file context
  246. * @return the local port number
  247. */
  248. int rtp_get_local_port(URLContext *h)
  249. {
  250. RTPContext *s = h->priv_data;
  251. return udp_get_local_port(s->rtp_hd);
  252. }
  253. /**
  254. * Return the rtp and rtcp file handles for select() usage to wait for several RTP
  255. * streams at the same time.
  256. * @param h media file context
  257. */
  258. void rtp_get_file_handles(URLContext *h, int *prtp_fd, int *prtcp_fd)
  259. {
  260. RTPContext *s = h->priv_data;
  261. *prtp_fd = s->rtp_fd;
  262. *prtcp_fd = s->rtcp_fd;
  263. }
  264. URLProtocol rtp_protocol = {
  265. "rtp",
  266. rtp_open,
  267. rtp_read,
  268. rtp_write,
  269. NULL, /* seek */
  270. rtp_close,
  271. };