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.

319 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. /**
  22. * @file rtpproto.c
  23. * RTP protocol
  24. */
  25. #include "libavutil/avstring.h"
  26. #include "avformat.h"
  27. #include <unistd.h>
  28. #include <stdarg.h>
  29. #include "network.h"
  30. #include "os_support.h"
  31. #include <fcntl.h>
  32. #include <sys/select.h>
  33. #define RTP_TX_BUF_SIZE (64 * 1024)
  34. #define RTP_RX_BUF_SIZE (128 * 1024)
  35. typedef struct RTPContext {
  36. URLContext *rtp_hd, *rtcp_hd;
  37. int rtp_fd, rtcp_fd;
  38. } RTPContext;
  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. * @param s1 media file context
  45. * @param uri of the remote server
  46. * @return zero if no error.
  47. */
  48. int rtp_set_remote_url(URLContext *h, const char *uri)
  49. {
  50. RTPContext *s = h->priv_data;
  51. char hostname[256];
  52. int port;
  53. char buf[1024];
  54. char path[1024];
  55. url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port,
  56. path, sizeof(path), uri);
  57. snprintf(buf, sizeof(buf), "udp://%s:%d%s", hostname, port, path);
  58. udp_set_remote_url(s->rtp_hd, buf);
  59. snprintf(buf, sizeof(buf), "udp://%s:%d%s", hostname, port + 1, path);
  60. udp_set_remote_url(s->rtcp_hd, buf);
  61. return 0;
  62. }
  63. /**
  64. * add option to url of the form:
  65. * "http://host:port/path?option1=val1&option2=val2...
  66. */
  67. static void url_add_option(char *buf, int buf_size, const char *fmt, ...)
  68. {
  69. char buf1[1024];
  70. va_list ap;
  71. va_start(ap, fmt);
  72. if (strchr(buf, '?'))
  73. av_strlcat(buf, "&", buf_size);
  74. else
  75. av_strlcat(buf, "?", buf_size);
  76. vsnprintf(buf1, sizeof(buf1), fmt, ap);
  77. av_strlcat(buf, buf1, buf_size);
  78. va_end(ap);
  79. }
  80. static void build_udp_url(char *buf, int buf_size,
  81. const char *hostname, int port,
  82. int local_port, int ttl,
  83. int max_packet_size)
  84. {
  85. snprintf(buf, buf_size, "udp://%s:%d", hostname, port);
  86. if (local_port >= 0)
  87. url_add_option(buf, buf_size, "localport=%d", local_port);
  88. if (ttl >= 0)
  89. url_add_option(buf, buf_size, "ttl=%d", ttl);
  90. if (max_packet_size >=0)
  91. url_add_option(buf, buf_size, "pkt_size=%d", max_packet_size);
  92. }
  93. /**
  94. * url syntax: rtp://host:port[?option=val...]
  95. * option: 'ttl=n' : set the ttl value (for multicast only)
  96. * 'localport=n' : set the local port to n
  97. * 'pkt_size=n' : set max packet size
  98. *
  99. */
  100. static int rtp_open(URLContext *h, const char *uri, int flags)
  101. {
  102. RTPContext *s;
  103. int port, is_output, ttl, local_port, max_packet_size;
  104. char hostname[256];
  105. char buf[1024];
  106. char path[1024];
  107. const char *p;
  108. is_output = (flags & URL_WRONLY);
  109. s = av_mallocz(sizeof(RTPContext));
  110. if (!s)
  111. return AVERROR(ENOMEM);
  112. h->priv_data = s;
  113. url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port,
  114. path, sizeof(path), uri);
  115. /* extract parameters */
  116. ttl = -1;
  117. local_port = -1;
  118. max_packet_size = -1;
  119. p = strchr(uri, '?');
  120. if (p) {
  121. if (find_info_tag(buf, sizeof(buf), "ttl", p)) {
  122. ttl = strtol(buf, NULL, 10);
  123. }
  124. if (find_info_tag(buf, sizeof(buf), "localport", p)) {
  125. local_port = strtol(buf, NULL, 10);
  126. }
  127. if (find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
  128. max_packet_size = strtol(buf, NULL, 10);
  129. }
  130. }
  131. build_udp_url(buf, sizeof(buf),
  132. hostname, port, local_port, ttl, max_packet_size);
  133. if (url_open(&s->rtp_hd, buf, flags) < 0)
  134. goto fail;
  135. local_port = udp_get_local_port(s->rtp_hd);
  136. /* XXX: need to open another connection if the port is not even */
  137. /* well, should suppress localport in path */
  138. build_udp_url(buf, sizeof(buf),
  139. hostname, port + 1, local_port + 1, ttl, max_packet_size);
  140. if (url_open(&s->rtcp_hd, buf, flags) < 0)
  141. goto fail;
  142. /* just to ease handle access. XXX: need to suppress direct handle
  143. access */
  144. s->rtp_fd = udp_get_file_handle(s->rtp_hd);
  145. s->rtcp_fd = udp_get_file_handle(s->rtcp_hd);
  146. h->max_packet_size = url_get_max_packet_size(s->rtp_hd);
  147. h->is_streamed = 1;
  148. return 0;
  149. fail:
  150. if (s->rtp_hd)
  151. url_close(s->rtp_hd);
  152. if (s->rtcp_hd)
  153. url_close(s->rtcp_hd);
  154. av_free(s);
  155. return AVERROR(EIO);
  156. }
  157. static int rtp_read(URLContext *h, uint8_t *buf, int size)
  158. {
  159. RTPContext *s = h->priv_data;
  160. struct sockaddr_in from;
  161. socklen_t from_len;
  162. int len, fd_max, n;
  163. fd_set rfds;
  164. #if 0
  165. for(;;) {
  166. from_len = sizeof(from);
  167. len = recvfrom (s->rtp_fd, buf, size, 0,
  168. (struct sockaddr *)&from, &from_len);
  169. if (len < 0) {
  170. if (ff_neterrno() == FF_NETERROR(EAGAIN) ||
  171. ff_neterrno() == FF_NETERROR(EINTR))
  172. continue;
  173. return AVERROR(EIO);
  174. }
  175. break;
  176. }
  177. #else
  178. for(;;) {
  179. /* build fdset to listen to RTP and RTCP packets */
  180. FD_ZERO(&rfds);
  181. fd_max = s->rtp_fd;
  182. FD_SET(s->rtp_fd, &rfds);
  183. if (s->rtcp_fd > fd_max)
  184. fd_max = s->rtcp_fd;
  185. FD_SET(s->rtcp_fd, &rfds);
  186. n = select(fd_max + 1, &rfds, NULL, NULL, NULL);
  187. if (n > 0) {
  188. /* first try RTCP */
  189. if (FD_ISSET(s->rtcp_fd, &rfds)) {
  190. from_len = sizeof(from);
  191. len = recvfrom (s->rtcp_fd, buf, size, 0,
  192. (struct sockaddr *)&from, &from_len);
  193. if (len < 0) {
  194. if (ff_neterrno() == FF_NETERROR(EAGAIN) ||
  195. ff_neterrno() == FF_NETERROR(EINTR))
  196. continue;
  197. return AVERROR(EIO);
  198. }
  199. break;
  200. }
  201. /* then RTP */
  202. if (FD_ISSET(s->rtp_fd, &rfds)) {
  203. from_len = sizeof(from);
  204. len = recvfrom (s->rtp_fd, buf, size, 0,
  205. (struct sockaddr *)&from, &from_len);
  206. if (len < 0) {
  207. if (ff_neterrno() == FF_NETERROR(EAGAIN) ||
  208. ff_neterrno() == FF_NETERROR(EINTR))
  209. continue;
  210. return AVERROR(EIO);
  211. }
  212. break;
  213. }
  214. }
  215. }
  216. #endif
  217. return len;
  218. }
  219. static int rtp_write(URLContext *h, uint8_t *buf, int size)
  220. {
  221. RTPContext *s = h->priv_data;
  222. int ret;
  223. URLContext *hd;
  224. if (buf[1] >= 200 && buf[1] <= 204) {
  225. /* RTCP payload type */
  226. hd = s->rtcp_hd;
  227. } else {
  228. /* RTP payload type */
  229. hd = s->rtp_hd;
  230. }
  231. ret = url_write(hd, buf, size);
  232. #if 0
  233. {
  234. struct timespec ts;
  235. ts.tv_sec = 0;
  236. ts.tv_nsec = 10 * 1000000;
  237. nanosleep(&ts, NULL);
  238. }
  239. #endif
  240. return ret;
  241. }
  242. static int rtp_close(URLContext *h)
  243. {
  244. RTPContext *s = h->priv_data;
  245. url_close(s->rtp_hd);
  246. url_close(s->rtcp_hd);
  247. av_free(s);
  248. return 0;
  249. }
  250. /**
  251. * Return the local port used by the RTP connection
  252. * @param s1 media file context
  253. * @return the local port number
  254. */
  255. int rtp_get_local_port(URLContext *h)
  256. {
  257. RTPContext *s = h->priv_data;
  258. return udp_get_local_port(s->rtp_hd);
  259. }
  260. /**
  261. * Return the rtp and rtcp file handles for select() usage to wait for
  262. * several RTP streams at the same time.
  263. * @param h media file context
  264. */
  265. void rtp_get_file_handles(URLContext *h, int *prtp_fd, int *prtcp_fd)
  266. {
  267. RTPContext *s = h->priv_data;
  268. *prtp_fd = s->rtp_fd;
  269. *prtcp_fd = s->rtcp_fd;
  270. }
  271. URLProtocol rtp_protocol = {
  272. "rtp",
  273. rtp_open,
  274. rtp_read,
  275. rtp_write,
  276. NULL, /* seek */
  277. rtp_close,
  278. };