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.

371 lines
10KB

  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
  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 h 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. av_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, int connect)
  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. if (connect)
  98. url_add_option(buf, buf_size, "connect=1");
  99. }
  100. /**
  101. * url syntax: rtp://host:port[?option=val...]
  102. * option: 'ttl=n' : set the ttl value (for multicast only)
  103. * 'rtcpport=n' : set the remote rtcp port to n
  104. * 'localrtpport=n' : set the local rtp port to n
  105. * 'localrtcpport=n' : set the local rtcp port to n
  106. * 'pkt_size=n' : set max packet size
  107. * 'connect=0/1' : do a connect() on the UDP socket
  108. * deprecated option:
  109. * 'localport=n' : set the local port to n
  110. *
  111. * if rtcpport isn't set the rtcp port will be the rtp port + 1
  112. * if local rtp port isn't set any available port will be used for the local
  113. * rtp and rtcp ports
  114. * if the local rtcp port is not set it will be the local rtp port + 1
  115. */
  116. static int rtp_open(URLContext *h, const char *uri, int flags)
  117. {
  118. RTPContext *s;
  119. int rtp_port, rtcp_port,
  120. is_output, ttl, connect,
  121. local_rtp_port, local_rtcp_port, max_packet_size;
  122. char hostname[256];
  123. char buf[1024];
  124. char path[1024];
  125. const char *p;
  126. is_output = (flags & URL_WRONLY);
  127. s = av_mallocz(sizeof(RTPContext));
  128. if (!s)
  129. return AVERROR(ENOMEM);
  130. h->priv_data = s;
  131. av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &rtp_port,
  132. path, sizeof(path), uri);
  133. /* extract parameters */
  134. ttl = -1;
  135. rtcp_port = rtp_port+1;
  136. local_rtp_port = -1;
  137. local_rtcp_port = -1;
  138. max_packet_size = -1;
  139. connect = 0;
  140. p = strchr(uri, '?');
  141. if (p) {
  142. if (find_info_tag(buf, sizeof(buf), "ttl", p)) {
  143. ttl = strtol(buf, NULL, 10);
  144. }
  145. if (find_info_tag(buf, sizeof(buf), "rtcpport", p)) {
  146. rtcp_port = strtol(buf, NULL, 10);
  147. }
  148. if (find_info_tag(buf, sizeof(buf), "localport", p)) {
  149. local_rtp_port = strtol(buf, NULL, 10);
  150. }
  151. if (find_info_tag(buf, sizeof(buf), "localrtpport", p)) {
  152. local_rtp_port = strtol(buf, NULL, 10);
  153. }
  154. if (find_info_tag(buf, sizeof(buf), "localrtcpport", p)) {
  155. local_rtcp_port = strtol(buf, NULL, 10);
  156. }
  157. if (find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
  158. max_packet_size = strtol(buf, NULL, 10);
  159. }
  160. if (find_info_tag(buf, sizeof(buf), "connect", p)) {
  161. connect = strtol(buf, NULL, 10);
  162. }
  163. }
  164. build_udp_url(buf, sizeof(buf),
  165. hostname, rtp_port, local_rtp_port, ttl, max_packet_size,
  166. connect);
  167. if (url_open(&s->rtp_hd, buf, flags) < 0)
  168. goto fail;
  169. if (local_rtp_port>=0 && local_rtcp_port<0)
  170. local_rtcp_port = udp_get_local_port(s->rtp_hd) + 1;
  171. build_udp_url(buf, sizeof(buf),
  172. hostname, rtcp_port, local_rtcp_port, ttl, max_packet_size,
  173. connect);
  174. if (url_open(&s->rtcp_hd, buf, flags) < 0)
  175. goto fail;
  176. /* just to ease handle access. XXX: need to suppress direct handle
  177. access */
  178. s->rtp_fd = url_get_file_handle(s->rtp_hd);
  179. s->rtcp_fd = url_get_file_handle(s->rtcp_hd);
  180. h->max_packet_size = url_get_max_packet_size(s->rtp_hd);
  181. h->is_streamed = 1;
  182. return 0;
  183. fail:
  184. if (s->rtp_hd)
  185. url_close(s->rtp_hd);
  186. if (s->rtcp_hd)
  187. url_close(s->rtcp_hd);
  188. av_free(s);
  189. return AVERROR(EIO);
  190. }
  191. static int rtp_read(URLContext *h, uint8_t *buf, int size)
  192. {
  193. RTPContext *s = h->priv_data;
  194. struct sockaddr_storage from;
  195. socklen_t from_len;
  196. int len, fd_max, n;
  197. fd_set rfds;
  198. struct timeval tv;
  199. #if 0
  200. for(;;) {
  201. from_len = sizeof(from);
  202. len = recvfrom (s->rtp_fd, buf, size, 0,
  203. (struct sockaddr *)&from, &from_len);
  204. if (len < 0) {
  205. if (ff_neterrno() == FF_NETERROR(EAGAIN) ||
  206. ff_neterrno() == FF_NETERROR(EINTR))
  207. continue;
  208. return AVERROR(EIO);
  209. }
  210. break;
  211. }
  212. #else
  213. for(;;) {
  214. if (url_interrupt_cb())
  215. return AVERROR(EINTR);
  216. /* build fdset to listen to RTP and RTCP packets */
  217. FD_ZERO(&rfds);
  218. fd_max = s->rtp_fd;
  219. FD_SET(s->rtp_fd, &rfds);
  220. if (s->rtcp_fd > fd_max)
  221. fd_max = s->rtcp_fd;
  222. FD_SET(s->rtcp_fd, &rfds);
  223. tv.tv_sec = 0;
  224. tv.tv_usec = 100 * 1000;
  225. n = select(fd_max + 1, &rfds, NULL, NULL, &tv);
  226. if (n > 0) {
  227. /* first try RTCP */
  228. if (FD_ISSET(s->rtcp_fd, &rfds)) {
  229. from_len = sizeof(from);
  230. len = recvfrom (s->rtcp_fd, buf, size, 0,
  231. (struct sockaddr *)&from, &from_len);
  232. if (len < 0) {
  233. if (ff_neterrno() == FF_NETERROR(EAGAIN) ||
  234. ff_neterrno() == FF_NETERROR(EINTR))
  235. continue;
  236. return AVERROR(EIO);
  237. }
  238. break;
  239. }
  240. /* then RTP */
  241. if (FD_ISSET(s->rtp_fd, &rfds)) {
  242. from_len = sizeof(from);
  243. len = recvfrom (s->rtp_fd, buf, size, 0,
  244. (struct sockaddr *)&from, &from_len);
  245. if (len < 0) {
  246. if (ff_neterrno() == FF_NETERROR(EAGAIN) ||
  247. ff_neterrno() == FF_NETERROR(EINTR))
  248. continue;
  249. return AVERROR(EIO);
  250. }
  251. break;
  252. }
  253. } else if (n < 0) {
  254. if (ff_neterrno() == FF_NETERROR(EINTR))
  255. continue;
  256. return AVERROR(EIO);
  257. }
  258. }
  259. #endif
  260. return len;
  261. }
  262. static int rtp_write(URLContext *h, const uint8_t *buf, int size)
  263. {
  264. RTPContext *s = h->priv_data;
  265. int ret;
  266. URLContext *hd;
  267. if (buf[1] >= RTCP_SR && buf[1] <= RTCP_APP) {
  268. /* RTCP payload type */
  269. hd = s->rtcp_hd;
  270. } else {
  271. /* RTP payload type */
  272. hd = s->rtp_hd;
  273. }
  274. ret = url_write(hd, buf, size);
  275. #if 0
  276. {
  277. struct timespec ts;
  278. ts.tv_sec = 0;
  279. ts.tv_nsec = 10 * 1000000;
  280. nanosleep(&ts, NULL);
  281. }
  282. #endif
  283. return ret;
  284. }
  285. static int rtp_close(URLContext *h)
  286. {
  287. RTPContext *s = h->priv_data;
  288. url_close(s->rtp_hd);
  289. url_close(s->rtcp_hd);
  290. av_free(s);
  291. return 0;
  292. }
  293. /**
  294. * Return the local rtp port used by the RTP connection
  295. * @param h media file context
  296. * @return the local port number
  297. */
  298. int rtp_get_local_rtp_port(URLContext *h)
  299. {
  300. RTPContext *s = h->priv_data;
  301. return udp_get_local_port(s->rtp_hd);
  302. }
  303. /**
  304. * Return the local rtcp port used by the RTP connection
  305. * @param h media file context
  306. * @return the local port number
  307. */
  308. int rtp_get_local_rtcp_port(URLContext *h)
  309. {
  310. RTPContext *s = h->priv_data;
  311. return udp_get_local_port(s->rtcp_hd);
  312. }
  313. static int rtp_get_file_handle(URLContext *h)
  314. {
  315. RTPContext *s = h->priv_data;
  316. return s->rtp_fd;
  317. }
  318. int rtp_get_rtcp_file_handle(URLContext *h) {
  319. RTPContext *s = h->priv_data;
  320. return s->rtcp_fd;
  321. }
  322. URLProtocol rtp_protocol = {
  323. "rtp",
  324. rtp_open,
  325. rtp_read,
  326. rtp_write,
  327. NULL, /* seek */
  328. rtp_close,
  329. .url_get_file_handle = rtp_get_file_handle,
  330. };