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.

339 lines
8.8KB

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