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.

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