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.

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