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.

363 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/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_POLL_H
  35. #include <sys/poll.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, n;
  197. struct pollfd p[2] = {{s->rtp_fd, POLLIN, 0}, {s->rtcp_fd, POLLIN, 0}};
  198. #if 0
  199. for(;;) {
  200. from_len = sizeof(from);
  201. len = recvfrom (s->rtp_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. #else
  212. for(;;) {
  213. if (url_interrupt_cb())
  214. return AVERROR(EINTR);
  215. /* build fdset to listen to RTP and RTCP packets */
  216. n = poll(p, 2, 100);
  217. if (n > 0) {
  218. /* first try RTCP */
  219. if (p[1].revents & POLLIN) {
  220. from_len = sizeof(from);
  221. len = recvfrom (s->rtcp_fd, buf, size, 0,
  222. (struct sockaddr *)&from, &from_len);
  223. if (len < 0) {
  224. if (ff_neterrno() == FF_NETERROR(EAGAIN) ||
  225. ff_neterrno() == FF_NETERROR(EINTR))
  226. continue;
  227. return AVERROR(EIO);
  228. }
  229. break;
  230. }
  231. /* then RTP */
  232. if (p[0].revents & POLLIN) {
  233. from_len = sizeof(from);
  234. len = recvfrom (s->rtp_fd, buf, size, 0,
  235. (struct sockaddr *)&from, &from_len);
  236. if (len < 0) {
  237. if (ff_neterrno() == FF_NETERROR(EAGAIN) ||
  238. ff_neterrno() == FF_NETERROR(EINTR))
  239. continue;
  240. return AVERROR(EIO);
  241. }
  242. break;
  243. }
  244. } else if (n < 0) {
  245. if (ff_neterrno() == FF_NETERROR(EINTR))
  246. continue;
  247. return AVERROR(EIO);
  248. }
  249. }
  250. #endif
  251. return len;
  252. }
  253. static int rtp_write(URLContext *h, const uint8_t *buf, int size)
  254. {
  255. RTPContext *s = h->priv_data;
  256. int ret;
  257. URLContext *hd;
  258. if (buf[1] >= RTCP_SR && buf[1] <= RTCP_APP) {
  259. /* RTCP payload type */
  260. hd = s->rtcp_hd;
  261. } else {
  262. /* RTP payload type */
  263. hd = s->rtp_hd;
  264. }
  265. ret = url_write(hd, buf, size);
  266. #if 0
  267. {
  268. struct timespec ts;
  269. ts.tv_sec = 0;
  270. ts.tv_nsec = 10 * 1000000;
  271. nanosleep(&ts, NULL);
  272. }
  273. #endif
  274. return ret;
  275. }
  276. static int rtp_close(URLContext *h)
  277. {
  278. RTPContext *s = h->priv_data;
  279. url_close(s->rtp_hd);
  280. url_close(s->rtcp_hd);
  281. av_free(s);
  282. return 0;
  283. }
  284. /**
  285. * Return the local rtp port used by the RTP connection
  286. * @param h media file context
  287. * @return the local port number
  288. */
  289. int rtp_get_local_rtp_port(URLContext *h)
  290. {
  291. RTPContext *s = h->priv_data;
  292. return udp_get_local_port(s->rtp_hd);
  293. }
  294. /**
  295. * Return the local rtcp port used by the RTP connection
  296. * @param h media file context
  297. * @return the local port number
  298. */
  299. int rtp_get_local_rtcp_port(URLContext *h)
  300. {
  301. RTPContext *s = h->priv_data;
  302. return udp_get_local_port(s->rtcp_hd);
  303. }
  304. static int rtp_get_file_handle(URLContext *h)
  305. {
  306. RTPContext *s = h->priv_data;
  307. return s->rtp_fd;
  308. }
  309. int rtp_get_rtcp_file_handle(URLContext *h) {
  310. RTPContext *s = h->priv_data;
  311. return s->rtcp_fd;
  312. }
  313. URLProtocol ff_rtp_protocol = {
  314. "rtp",
  315. rtp_open,
  316. rtp_read,
  317. rtp_write,
  318. NULL, /* seek */
  319. rtp_close,
  320. .url_get_file_handle = rtp_get_file_handle,
  321. };