rtpproto.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 "avio_internal.h"
  29. #include "rtpdec.h"
  30. #include "url.h"
  31. #include <stdarg.h>
  32. #include "internal.h"
  33. #include "network.h"
  34. #include "os_support.h"
  35. #include <fcntl.h>
  36. #if HAVE_POLL_H
  37. #include <sys/poll.h>
  38. #endif
  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 ff_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. ff_udp_set_remote_url(s->rtp_hd, buf);
  65. ff_url_join(buf, sizeof(buf), "udp", NULL, hostname, port + 1, "%s", path);
  66. ff_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 av_printf_format(3, 4) 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. url_add_option(buf, buf_size, "fifo_size=0");
  101. }
  102. /**
  103. * url syntax: rtp://host:port[?option=val...]
  104. * option: 'ttl=n' : set the ttl value (for multicast only)
  105. * 'rtcpport=n' : set the remote rtcp port to n
  106. * 'localrtpport=n' : set the local rtp port to n
  107. * 'localrtcpport=n' : set the local rtcp port to n
  108. * 'pkt_size=n' : set max packet size
  109. * 'connect=0/1' : do a connect() on the UDP socket
  110. * deprecated option:
  111. * 'localport=n' : set the local port to n
  112. *
  113. * if rtcpport isn't set the rtcp port will be the rtp port + 1
  114. * if local rtp port isn't set any available port will be used for the local
  115. * rtp and rtcp ports
  116. * if the local rtcp port is not set it will be the local rtp port + 1
  117. */
  118. static int rtp_open(URLContext *h, const char *uri, int flags)
  119. {
  120. RTPContext *s = h->priv_data;
  121. int rtp_port, rtcp_port,
  122. ttl, connect,
  123. local_rtp_port, local_rtcp_port, max_packet_size;
  124. char hostname[256];
  125. char buf[1024];
  126. char path[1024];
  127. const char *p;
  128. av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &rtp_port,
  129. path, sizeof(path), uri);
  130. /* extract parameters */
  131. ttl = -1;
  132. rtcp_port = rtp_port+1;
  133. local_rtp_port = -1;
  134. local_rtcp_port = -1;
  135. max_packet_size = -1;
  136. connect = 0;
  137. p = strchr(uri, '?');
  138. if (p) {
  139. if (av_find_info_tag(buf, sizeof(buf), "ttl", p)) {
  140. ttl = strtol(buf, NULL, 10);
  141. }
  142. if (av_find_info_tag(buf, sizeof(buf), "rtcpport", p)) {
  143. rtcp_port = strtol(buf, NULL, 10);
  144. }
  145. if (av_find_info_tag(buf, sizeof(buf), "localport", p)) {
  146. local_rtp_port = strtol(buf, NULL, 10);
  147. }
  148. if (av_find_info_tag(buf, sizeof(buf), "localrtpport", p)) {
  149. local_rtp_port = strtol(buf, NULL, 10);
  150. }
  151. if (av_find_info_tag(buf, sizeof(buf), "localrtcpport", p)) {
  152. local_rtcp_port = strtol(buf, NULL, 10);
  153. }
  154. if (av_find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
  155. max_packet_size = strtol(buf, NULL, 10);
  156. }
  157. if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
  158. connect = strtol(buf, NULL, 10);
  159. }
  160. }
  161. build_udp_url(buf, sizeof(buf),
  162. hostname, rtp_port, local_rtp_port, ttl, max_packet_size,
  163. connect);
  164. if (ffurl_open(&s->rtp_hd, buf, flags, &h->interrupt_callback, NULL) < 0)
  165. goto fail;
  166. if (local_rtp_port>=0 && local_rtcp_port<0)
  167. local_rtcp_port = ff_udp_get_local_port(s->rtp_hd) + 1;
  168. build_udp_url(buf, sizeof(buf),
  169. hostname, rtcp_port, local_rtcp_port, ttl, max_packet_size,
  170. connect);
  171. if (ffurl_open(&s->rtcp_hd, buf, flags, &h->interrupt_callback, NULL) < 0)
  172. goto fail;
  173. /* just to ease handle access. XXX: need to suppress direct handle
  174. access */
  175. s->rtp_fd = ffurl_get_file_handle(s->rtp_hd);
  176. s->rtcp_fd = ffurl_get_file_handle(s->rtcp_hd);
  177. h->max_packet_size = s->rtp_hd->max_packet_size;
  178. h->is_streamed = 1;
  179. return 0;
  180. fail:
  181. if (s->rtp_hd)
  182. ffurl_close(s->rtp_hd);
  183. if (s->rtcp_hd)
  184. ffurl_close(s->rtcp_hd);
  185. return AVERROR(EIO);
  186. }
  187. static int rtp_read(URLContext *h, uint8_t *buf, int size)
  188. {
  189. RTPContext *s = h->priv_data;
  190. struct sockaddr_storage from;
  191. socklen_t from_len;
  192. int len, n;
  193. struct pollfd p[2] = {{s->rtp_fd, POLLIN, 0}, {s->rtcp_fd, POLLIN, 0}};
  194. for(;;) {
  195. if (ff_check_interrupt(&h->interrupt_callback))
  196. return AVERROR_EXIT;
  197. /* build fdset to listen to RTP and RTCP packets */
  198. n = poll(p, 2, 100);
  199. if (n > 0) {
  200. /* first try RTCP */
  201. if (p[1].revents & POLLIN) {
  202. from_len = sizeof(from);
  203. len = recvfrom (s->rtcp_fd, buf, size, 0,
  204. (struct sockaddr *)&from, &from_len);
  205. if (len < 0) {
  206. if (ff_neterrno() == AVERROR(EAGAIN) ||
  207. ff_neterrno() == AVERROR(EINTR))
  208. continue;
  209. return AVERROR(EIO);
  210. }
  211. break;
  212. }
  213. /* then RTP */
  214. if (p[0].revents & POLLIN) {
  215. from_len = sizeof(from);
  216. len = recvfrom (s->rtp_fd, buf, size, 0,
  217. (struct sockaddr *)&from, &from_len);
  218. if (len < 0) {
  219. if (ff_neterrno() == AVERROR(EAGAIN) ||
  220. ff_neterrno() == AVERROR(EINTR))
  221. continue;
  222. return AVERROR(EIO);
  223. }
  224. break;
  225. }
  226. } else if (n < 0) {
  227. if (ff_neterrno() == AVERROR(EINTR))
  228. continue;
  229. return AVERROR(EIO);
  230. }
  231. }
  232. return len;
  233. }
  234. static int rtp_write(URLContext *h, const uint8_t *buf, int size)
  235. {
  236. RTPContext *s = h->priv_data;
  237. int ret;
  238. URLContext *hd;
  239. if (RTP_PT_IS_RTCP(buf[1])) {
  240. /* RTCP payload type */
  241. hd = s->rtcp_hd;
  242. } else {
  243. /* RTP payload type */
  244. hd = s->rtp_hd;
  245. }
  246. ret = ffurl_write(hd, buf, size);
  247. return ret;
  248. }
  249. static int rtp_close(URLContext *h)
  250. {
  251. RTPContext *s = h->priv_data;
  252. ffurl_close(s->rtp_hd);
  253. ffurl_close(s->rtcp_hd);
  254. return 0;
  255. }
  256. /**
  257. * Return the local rtp port used by the RTP connection
  258. * @param h media file context
  259. * @return the local port number
  260. */
  261. int ff_rtp_get_local_rtp_port(URLContext *h)
  262. {
  263. RTPContext *s = h->priv_data;
  264. return ff_udp_get_local_port(s->rtp_hd);
  265. }
  266. /**
  267. * Return the local rtcp port used by the RTP connection
  268. * @param h media file context
  269. * @return the local port number
  270. */
  271. int ff_rtp_get_local_rtcp_port(URLContext *h)
  272. {
  273. RTPContext *s = h->priv_data;
  274. return ff_udp_get_local_port(s->rtcp_hd);
  275. }
  276. static int rtp_get_file_handle(URLContext *h)
  277. {
  278. RTPContext *s = h->priv_data;
  279. return s->rtp_fd;
  280. }
  281. static int rtp_get_multi_file_handle(URLContext *h, int **handles,
  282. int *numhandles)
  283. {
  284. RTPContext *s = h->priv_data;
  285. int *hs = *handles = av_malloc(sizeof(**handles) * 2);
  286. if (!hs)
  287. return AVERROR(ENOMEM);
  288. hs[0] = s->rtp_fd;
  289. hs[1] = s->rtcp_fd;
  290. *numhandles = 2;
  291. return 0;
  292. }
  293. URLProtocol ff_rtp_protocol = {
  294. .name = "rtp",
  295. .url_open = rtp_open,
  296. .url_read = rtp_read,
  297. .url_write = rtp_write,
  298. .url_close = rtp_close,
  299. .url_get_file_handle = rtp_get_file_handle,
  300. .url_get_multi_file_handle = rtp_get_multi_file_handle,
  301. .priv_data_size = sizeof(RTPContext),
  302. .flags = URL_PROTOCOL_FLAG_NETWORK,
  303. };