rtpproto.c 9.7 KB

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