rtpproto.c 10.0 KB

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