rtpproto.c
Go to the documentation of this file.
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 
27 #include "libavutil/parseutils.h"
28 #include "libavutil/avstring.h"
29 #include "avformat.h"
30 #include "avio_internal.h"
31 #include "rtpdec.h"
32 #include "url.h"
33 
34 #include <unistd.h>
35 #include <stdarg.h>
36 #include "internal.h"
37 #include "network.h"
38 #include "os_support.h"
39 #include <fcntl.h>
40 #if HAVE_POLL_H
41 #include <sys/poll.h>
42 #endif
43 #include <sys/time.h>
44 
45 #define RTP_TX_BUF_SIZE (64 * 1024)
46 #define RTP_RX_BUF_SIZE (128 * 1024)
47 
48 typedef struct RTPContext {
51 } RTPContext;
52 
63 int ff_rtp_set_remote_url(URLContext *h, const char *uri)
64 {
65  RTPContext *s = h->priv_data;
66  char hostname[256];
67  int port;
68 
69  char buf[1024];
70  char path[1024];
71 
72  av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port,
73  path, sizeof(path), uri);
74 
75  ff_url_join(buf, sizeof(buf), "udp", NULL, hostname, port, "%s", path);
77 
78  ff_url_join(buf, sizeof(buf), "udp", NULL, hostname, port + 1, "%s", path);
80  return 0;
81 }
82 
83 
89 static av_printf_format(3, 4) void url_add_option(char *buf, int buf_size, const char *fmt, ...)
90 {
91  char buf1[1024];
92  va_list ap;
93 
94  va_start(ap, fmt);
95  if (strchr(buf, '?'))
96  av_strlcat(buf, "&", buf_size);
97  else
98  av_strlcat(buf, "?", buf_size);
99  vsnprintf(buf1, sizeof(buf1), fmt, ap);
100  av_strlcat(buf, buf1, buf_size);
101  va_end(ap);
102 }
103 
104 static void build_udp_url(char *buf, int buf_size,
105  const char *hostname, int port,
106  int local_port, int ttl,
107  int max_packet_size, int connect)
108 {
109  ff_url_join(buf, buf_size, "udp", NULL, hostname, port, NULL);
110  if (local_port >= 0)
111  url_add_option(buf, buf_size, "localport=%d", local_port);
112  if (ttl >= 0)
113  url_add_option(buf, buf_size, "ttl=%d", ttl);
114  if (max_packet_size >=0)
115  url_add_option(buf, buf_size, "pkt_size=%d", max_packet_size);
116  if (connect)
117  url_add_option(buf, buf_size, "connect=1");
118 }
119 
137 static int rtp_open(URLContext *h, const char *uri, int flags)
138 {
139  RTPContext *s = h->priv_data;
140  int rtp_port, rtcp_port,
141  ttl, connect,
142  local_rtp_port, local_rtcp_port, max_packet_size;
143  char hostname[256];
144  char buf[1024];
145  char path[1024];
146  const char *p;
147 
148  av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &rtp_port,
149  path, sizeof(path), uri);
150  /* extract parameters */
151  ttl = -1;
152  rtcp_port = rtp_port+1;
153  local_rtp_port = -1;
154  local_rtcp_port = -1;
155  max_packet_size = -1;
156  connect = 0;
157 
158  p = strchr(uri, '?');
159  if (p) {
160  if (av_find_info_tag(buf, sizeof(buf), "ttl", p)) {
161  ttl = strtol(buf, NULL, 10);
162  }
163  if (av_find_info_tag(buf, sizeof(buf), "rtcpport", p)) {
164  rtcp_port = strtol(buf, NULL, 10);
165  }
166  if (av_find_info_tag(buf, sizeof(buf), "localport", p)) {
167  local_rtp_port = strtol(buf, NULL, 10);
168  }
169  if (av_find_info_tag(buf, sizeof(buf), "localrtpport", p)) {
170  local_rtp_port = strtol(buf, NULL, 10);
171  }
172  if (av_find_info_tag(buf, sizeof(buf), "localrtcpport", p)) {
173  local_rtcp_port = strtol(buf, NULL, 10);
174  }
175  if (av_find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
176  max_packet_size = strtol(buf, NULL, 10);
177  }
178  if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
179  connect = strtol(buf, NULL, 10);
180  }
181  }
182 
183  build_udp_url(buf, sizeof(buf),
184  hostname, rtp_port, local_rtp_port, ttl, max_packet_size,
185  connect);
186  if (ffurl_open(&s->rtp_hd, buf, flags, &h->interrupt_callback, NULL) < 0)
187  goto fail;
188  if (local_rtp_port>=0 && local_rtcp_port<0)
189  local_rtcp_port = ff_udp_get_local_port(s->rtp_hd) + 1;
190 
191  build_udp_url(buf, sizeof(buf),
192  hostname, rtcp_port, local_rtcp_port, ttl, max_packet_size,
193  connect);
194  if (ffurl_open(&s->rtcp_hd, buf, flags, &h->interrupt_callback, NULL) < 0)
195  goto fail;
196 
197  /* just to ease handle access. XXX: need to suppress direct handle
198  access */
201 
203  h->is_streamed = 1;
204  return 0;
205 
206  fail:
207  if (s->rtp_hd)
208  ffurl_close(s->rtp_hd);
209  if (s->rtcp_hd)
210  ffurl_close(s->rtcp_hd);
211  return AVERROR(EIO);
212 }
213 
214 static int rtp_read(URLContext *h, uint8_t *buf, int size)
215 {
216  RTPContext *s = h->priv_data;
217  struct sockaddr_storage from;
218  socklen_t from_len;
219  int len, n;
220  struct pollfd p[2] = {{s->rtp_fd, POLLIN, 0}, {s->rtcp_fd, POLLIN, 0}};
221 
222  for(;;) {
224  return AVERROR_EXIT;
225  /* build fdset to listen to RTP and RTCP packets */
226  n = poll(p, 2, 100);
227  if (n > 0) {
228  /* first try RTCP */
229  if (p[1].revents & POLLIN) {
230  from_len = sizeof(from);
231  len = recvfrom (s->rtcp_fd, buf, size, 0,
232  (struct sockaddr *)&from, &from_len);
233  if (len < 0) {
234  if (ff_neterrno() == AVERROR(EAGAIN) ||
235  ff_neterrno() == AVERROR(EINTR))
236  continue;
237  return AVERROR(EIO);
238  }
239  break;
240  }
241  /* then RTP */
242  if (p[0].revents & POLLIN) {
243  from_len = sizeof(from);
244  len = recvfrom (s->rtp_fd, buf, size, 0,
245  (struct sockaddr *)&from, &from_len);
246  if (len < 0) {
247  if (ff_neterrno() == AVERROR(EAGAIN) ||
248  ff_neterrno() == AVERROR(EINTR))
249  continue;
250  return AVERROR(EIO);
251  }
252  break;
253  }
254  } else if (n < 0) {
255  if (ff_neterrno() == AVERROR(EINTR))
256  continue;
257  return AVERROR(EIO);
258  }
259  }
260  return len;
261 }
262 
263 static int rtp_write(URLContext *h, const uint8_t *buf, int size)
264 {
265  RTPContext *s = h->priv_data;
266  int ret;
267  URLContext *hd;
268 
269  if (buf[1] >= RTCP_SR && buf[1] <= RTCP_APP) {
270  /* RTCP payload type */
271  hd = s->rtcp_hd;
272  } else {
273  /* RTP payload type */
274  hd = s->rtp_hd;
275  }
276 
277  ret = ffurl_write(hd, buf, size);
278  return ret;
279 }
280 
281 static int rtp_close(URLContext *h)
282 {
283  RTPContext *s = h->priv_data;
284 
285  ffurl_close(s->rtp_hd);
286  ffurl_close(s->rtcp_hd);
287  return 0;
288 }
289 
297 {
298  RTPContext *s = h->priv_data;
299  return ff_udp_get_local_port(s->rtp_hd);
300 }
301 
309 {
310  RTPContext *s = h->priv_data;
311  return ff_udp_get_local_port(s->rtcp_hd);
312 }
313 
315 {
316  RTPContext *s = h->priv_data;
317  return s->rtp_fd;
318 }
319 
321  RTPContext *s = h->priv_data;
322  return s->rtcp_fd;
323 }
324 
326  .name = "rtp",
327  .url_open = rtp_open,
328  .url_read = rtp_read,
329  .url_write = rtp_write,
330  .url_close = rtp_close,
331  .url_get_file_handle = rtp_get_file_handle,
332  .priv_data_size = sizeof(RTPContext),
334 };