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.

340 lines
8.9KB

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