rtpproto.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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 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 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;
  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. s = av_mallocz(sizeof(RTPContext));
  131. if (!s)
  132. return AVERROR(ENOMEM);
  133. h->priv_data = s;
  134. av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &rtp_port,
  135. path, sizeof(path), uri);
  136. /* extract parameters */
  137. ttl = -1;
  138. rtcp_port = rtp_port+1;
  139. local_rtp_port = -1;
  140. local_rtcp_port = -1;
  141. max_packet_size = -1;
  142. connect = 0;
  143. p = strchr(uri, '?');
  144. if (p) {
  145. if (av_find_info_tag(buf, sizeof(buf), "ttl", p)) {
  146. ttl = strtol(buf, NULL, 10);
  147. }
  148. if (av_find_info_tag(buf, sizeof(buf), "rtcpport", p)) {
  149. rtcp_port = strtol(buf, NULL, 10);
  150. }
  151. if (av_find_info_tag(buf, sizeof(buf), "localport", p)) {
  152. local_rtp_port = strtol(buf, NULL, 10);
  153. }
  154. if (av_find_info_tag(buf, sizeof(buf), "localrtpport", p)) {
  155. local_rtp_port = strtol(buf, NULL, 10);
  156. }
  157. if (av_find_info_tag(buf, sizeof(buf), "localrtcpport", p)) {
  158. local_rtcp_port = strtol(buf, NULL, 10);
  159. }
  160. if (av_find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
  161. max_packet_size = strtol(buf, NULL, 10);
  162. }
  163. if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
  164. connect = strtol(buf, NULL, 10);
  165. }
  166. }
  167. build_udp_url(buf, sizeof(buf),
  168. hostname, rtp_port, local_rtp_port, ttl, max_packet_size,
  169. connect);
  170. if (ffurl_open(&s->rtp_hd, buf, flags) < 0)
  171. goto fail;
  172. if (local_rtp_port>=0 && local_rtcp_port<0)
  173. local_rtcp_port = ff_udp_get_local_port(s->rtp_hd) + 1;
  174. build_udp_url(buf, sizeof(buf),
  175. hostname, rtcp_port, local_rtcp_port, ttl, max_packet_size,
  176. connect);
  177. if (ffurl_open(&s->rtcp_hd, buf, flags) < 0)
  178. goto fail;
  179. /* just to ease handle access. XXX: need to suppress direct handle
  180. access */
  181. s->rtp_fd = ffurl_get_file_handle(s->rtp_hd);
  182. s->rtcp_fd = ffurl_get_file_handle(s->rtcp_hd);
  183. h->max_packet_size = s->rtp_hd->max_packet_size;
  184. h->is_streamed = 1;
  185. return 0;
  186. fail:
  187. if (s->rtp_hd)
  188. ffurl_close(s->rtp_hd);
  189. if (s->rtcp_hd)
  190. ffurl_close(s->rtcp_hd);
  191. av_free(s);
  192. return AVERROR(EIO);
  193. }
  194. static int rtp_read(URLContext *h, uint8_t *buf, int size)
  195. {
  196. RTPContext *s = h->priv_data;
  197. struct sockaddr_storage from;
  198. socklen_t from_len;
  199. int len, n;
  200. struct pollfd p[2] = {{s->rtp_fd, POLLIN, 0}, {s->rtcp_fd, POLLIN, 0}};
  201. #if 0
  202. for(;;) {
  203. from_len = sizeof(from);
  204. len = recvfrom (s->rtp_fd, buf, size, 0,
  205. (struct sockaddr *)&from, &from_len);
  206. if (len < 0) {
  207. if (ff_neterrno() == AVERROR(EAGAIN) ||
  208. ff_neterrno() == AVERROR(EINTR))
  209. continue;
  210. return AVERROR(EIO);
  211. }
  212. break;
  213. }
  214. #else
  215. for(;;) {
  216. if (url_interrupt_cb())
  217. return AVERROR_EXIT;
  218. /* build fdset to listen to RTP and RTCP packets */
  219. n = poll(p, 2, 100);
  220. if (n > 0) {
  221. /* first try RTCP */
  222. if (p[1].revents & POLLIN) {
  223. from_len = sizeof(from);
  224. len = recvfrom (s->rtcp_fd, buf, size, 0,
  225. (struct sockaddr *)&from, &from_len);
  226. if (len < 0) {
  227. if (ff_neterrno() == AVERROR(EAGAIN) ||
  228. ff_neterrno() == AVERROR(EINTR))
  229. continue;
  230. return AVERROR(EIO);
  231. }
  232. break;
  233. }
  234. /* then RTP */
  235. if (p[0].revents & POLLIN) {
  236. from_len = sizeof(from);
  237. len = recvfrom (s->rtp_fd, buf, size, 0,
  238. (struct sockaddr *)&from, &from_len);
  239. if (len < 0) {
  240. if (ff_neterrno() == AVERROR(EAGAIN) ||
  241. ff_neterrno() == AVERROR(EINTR))
  242. continue;
  243. return AVERROR(EIO);
  244. }
  245. break;
  246. }
  247. } else if (n < 0) {
  248. if (ff_neterrno() == AVERROR(EINTR))
  249. continue;
  250. return AVERROR(EIO);
  251. }
  252. }
  253. #endif
  254. return len;
  255. }
  256. static int rtp_write(URLContext *h, const uint8_t *buf, int size)
  257. {
  258. RTPContext *s = h->priv_data;
  259. int ret;
  260. URLContext *hd;
  261. if (buf[1] >= RTCP_SR && buf[1] <= RTCP_APP) {
  262. /* RTCP payload type */
  263. hd = s->rtcp_hd;
  264. } else {
  265. /* RTP payload type */
  266. hd = s->rtp_hd;
  267. }
  268. ret = ffurl_write(hd, buf, size);
  269. #if 0
  270. {
  271. struct timespec ts;
  272. ts.tv_sec = 0;
  273. ts.tv_nsec = 10 * 1000000;
  274. nanosleep(&ts, NULL);
  275. }
  276. #endif
  277. return ret;
  278. }
  279. static int rtp_close(URLContext *h)
  280. {
  281. RTPContext *s = h->priv_data;
  282. ffurl_close(s->rtp_hd);
  283. ffurl_close(s->rtcp_hd);
  284. av_free(s);
  285. return 0;
  286. }
  287. /**
  288. * Return the local rtp port used by the RTP connection
  289. * @param h media file context
  290. * @return the local port number
  291. */
  292. int rtp_get_local_rtp_port(URLContext *h)
  293. {
  294. RTPContext *s = h->priv_data;
  295. return ff_udp_get_local_port(s->rtp_hd);
  296. }
  297. /**
  298. * Return the local rtcp port used by the RTP connection
  299. * @param h media file context
  300. * @return the local port number
  301. */
  302. int rtp_get_local_rtcp_port(URLContext *h)
  303. {
  304. RTPContext *s = h->priv_data;
  305. return ff_udp_get_local_port(s->rtcp_hd);
  306. }
  307. static int rtp_get_file_handle(URLContext *h)
  308. {
  309. RTPContext *s = h->priv_data;
  310. return s->rtp_fd;
  311. }
  312. int rtp_get_rtcp_file_handle(URLContext *h) {
  313. RTPContext *s = h->priv_data;
  314. return s->rtcp_fd;
  315. }
  316. URLProtocol ff_rtp_protocol = {
  317. .name = "rtp",
  318. .url_open = rtp_open,
  319. .url_read = rtp_read,
  320. .url_write = rtp_write,
  321. .url_close = rtp_close,
  322. .url_get_file_handle = rtp_get_file_handle,
  323. };