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.

305 lines
8.3KB

  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 "avstring.h"
  23. #include <unistd.h>
  24. #include <stdarg.h>
  25. #include "network.h"
  26. #include "os_support.h"
  27. #include <fcntl.h>
  28. #define RTP_TX_BUF_SIZE (64 * 1024)
  29. #define RTP_RX_BUF_SIZE (128 * 1024)
  30. typedef struct RTPContext {
  31. URLContext *rtp_hd, *rtcp_hd;
  32. int rtp_fd, rtcp_fd;
  33. } RTPContext;
  34. /**
  35. * If no filename is given to av_open_input_file because you want to
  36. * get the local port first, then you must call this function to set
  37. * the remote server address.
  38. *
  39. * @param s1 media file context
  40. * @param uri of the remote server
  41. * @return zero if no error.
  42. */
  43. int rtp_set_remote_url(URLContext *h, const char *uri)
  44. {
  45. RTPContext *s = h->priv_data;
  46. char hostname[256];
  47. int port;
  48. char buf[1024];
  49. char path[1024];
  50. url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port,
  51. path, sizeof(path), uri);
  52. snprintf(buf, sizeof(buf), "udp://%s:%d%s", hostname, port, path);
  53. udp_set_remote_url(s->rtp_hd, buf);
  54. snprintf(buf, sizeof(buf), "udp://%s:%d%s", hostname, port + 1, path);
  55. udp_set_remote_url(s->rtcp_hd, buf);
  56. return 0;
  57. }
  58. /* add option to url of the form:
  59. "http://host:port/path?option1=val1&option2=val2... */
  60. static void url_add_option(char *buf, int buf_size, const char *fmt, ...)
  61. {
  62. char buf1[1024];
  63. va_list ap;
  64. va_start(ap, fmt);
  65. if (strchr(buf, '?'))
  66. av_strlcat(buf, "&", buf_size);
  67. else
  68. av_strlcat(buf, "?", buf_size);
  69. vsnprintf(buf1, sizeof(buf1), fmt, ap);
  70. av_strlcat(buf, buf1, buf_size);
  71. va_end(ap);
  72. }
  73. static void build_udp_url(char *buf, int buf_size,
  74. const char *hostname, int port,
  75. int local_port, int ttl,
  76. int max_packet_size)
  77. {
  78. snprintf(buf, buf_size, "udp://%s:%d", hostname, port);
  79. if (local_port >= 0)
  80. url_add_option(buf, buf_size, "localport=%d", local_port);
  81. if (ttl >= 0)
  82. url_add_option(buf, buf_size, "ttl=%d", ttl);
  83. if (max_packet_size >=0)
  84. url_add_option(buf, buf_size, "pkt_size=%d", max_packet_size);
  85. }
  86. /*
  87. * url syntax: rtp://host:port[?option=val...]
  88. * option: 'ttl=n' : set the ttl value (for multicast only)
  89. * 'localport=n' : set the local port to n
  90. * 'pkt_size=n' : set max packet size
  91. *
  92. */
  93. static int rtp_open(URLContext *h, const char *uri, int flags)
  94. {
  95. RTPContext *s;
  96. int port, is_output, ttl, local_port, max_packet_size;
  97. char hostname[256];
  98. char buf[1024];
  99. char path[1024];
  100. const char *p;
  101. is_output = (flags & URL_WRONLY);
  102. s = av_mallocz(sizeof(RTPContext));
  103. if (!s)
  104. return AVERROR(ENOMEM);
  105. h->priv_data = s;
  106. url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port,
  107. path, sizeof(path), uri);
  108. /* extract parameters */
  109. ttl = -1;
  110. local_port = -1;
  111. max_packet_size = -1;
  112. p = strchr(uri, '?');
  113. if (p) {
  114. if (find_info_tag(buf, sizeof(buf), "ttl", p)) {
  115. ttl = strtol(buf, NULL, 10);
  116. }
  117. if (find_info_tag(buf, sizeof(buf), "localport", p)) {
  118. local_port = strtol(buf, NULL, 10);
  119. }
  120. if (find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
  121. max_packet_size = strtol(buf, NULL, 10);
  122. }
  123. }
  124. build_udp_url(buf, sizeof(buf),
  125. hostname, port, local_port, ttl, max_packet_size);
  126. if (url_open(&s->rtp_hd, buf, flags) < 0)
  127. goto fail;
  128. local_port = udp_get_local_port(s->rtp_hd);
  129. /* XXX: need to open another connection if the port is not even */
  130. /* well, should suppress localport in path */
  131. build_udp_url(buf, sizeof(buf),
  132. hostname, port + 1, local_port + 1, ttl, max_packet_size);
  133. if (url_open(&s->rtcp_hd, buf, flags) < 0)
  134. goto fail;
  135. /* just to ease handle access. XXX: need to suppress direct handle
  136. access */
  137. s->rtp_fd = udp_get_file_handle(s->rtp_hd);
  138. s->rtcp_fd = udp_get_file_handle(s->rtcp_hd);
  139. h->max_packet_size = url_get_max_packet_size(s->rtp_hd);
  140. h->is_streamed = 1;
  141. return 0;
  142. fail:
  143. if (s->rtp_hd)
  144. url_close(s->rtp_hd);
  145. if (s->rtcp_hd)
  146. url_close(s->rtcp_hd);
  147. av_free(s);
  148. return AVERROR(EIO);
  149. }
  150. static int rtp_read(URLContext *h, uint8_t *buf, int size)
  151. {
  152. RTPContext *s = h->priv_data;
  153. struct sockaddr_in from;
  154. socklen_t from_len;
  155. int len, fd_max, n;
  156. fd_set rfds;
  157. #if 0
  158. for(;;) {
  159. from_len = sizeof(from);
  160. len = recvfrom (s->rtp_fd, buf, size, 0,
  161. (struct sockaddr *)&from, &from_len);
  162. if (len < 0) {
  163. if (ff_neterrno() == FF_NETERROR(EAGAIN) ||
  164. ff_neterrno() == FF_NETERROR(EINTR))
  165. continue;
  166. return AVERROR(EIO);
  167. }
  168. break;
  169. }
  170. #else
  171. for(;;) {
  172. /* build fdset to listen to RTP and RTCP packets */
  173. FD_ZERO(&rfds);
  174. fd_max = s->rtp_fd;
  175. FD_SET(s->rtp_fd, &rfds);
  176. if (s->rtcp_fd > fd_max)
  177. fd_max = s->rtcp_fd;
  178. FD_SET(s->rtcp_fd, &rfds);
  179. n = select(fd_max + 1, &rfds, NULL, NULL, NULL);
  180. if (n > 0) {
  181. /* first try RTCP */
  182. if (FD_ISSET(s->rtcp_fd, &rfds)) {
  183. from_len = sizeof(from);
  184. len = recvfrom (s->rtcp_fd, buf, size, 0,
  185. (struct sockaddr *)&from, &from_len);
  186. if (len < 0) {
  187. if (ff_neterrno() == FF_NETERROR(EAGAIN) ||
  188. ff_neterrno() == FF_NETERROR(EINTR))
  189. continue;
  190. return AVERROR(EIO);
  191. }
  192. break;
  193. }
  194. /* then RTP */
  195. if (FD_ISSET(s->rtp_fd, &rfds)) {
  196. from_len = sizeof(from);
  197. len = recvfrom (s->rtp_fd, buf, size, 0,
  198. (struct sockaddr *)&from, &from_len);
  199. if (len < 0) {
  200. if (ff_neterrno() == FF_NETERROR(EAGAIN) ||
  201. ff_neterrno() == FF_NETERROR(EINTR))
  202. continue;
  203. return AVERROR(EIO);
  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 connection
  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. };