00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include "libavutil/parseutils.h"
00028 #include "libavutil/avstring.h"
00029 #include "avformat.h"
00030 #include "avio_internal.h"
00031 #include "rtpdec.h"
00032 #include "url.h"
00033
00034 #include <stdarg.h>
00035 #include "internal.h"
00036 #include "network.h"
00037 #include "os_support.h"
00038 #include <fcntl.h>
00039 #if HAVE_POLL_H
00040 #include <sys/poll.h>
00041 #endif
00042
00043 #define RTP_TX_BUF_SIZE (64 * 1024)
00044 #define RTP_RX_BUF_SIZE (128 * 1024)
00045
00046 typedef struct RTPContext {
00047 URLContext *rtp_hd, *rtcp_hd;
00048 int rtp_fd, rtcp_fd;
00049 } RTPContext;
00050
00061 int ff_rtp_set_remote_url(URLContext *h, const char *uri)
00062 {
00063 RTPContext *s = h->priv_data;
00064 char hostname[256];
00065 int port;
00066
00067 char buf[1024];
00068 char path[1024];
00069
00070 av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port,
00071 path, sizeof(path), uri);
00072
00073 ff_url_join(buf, sizeof(buf), "udp", NULL, hostname, port, "%s", path);
00074 ff_udp_set_remote_url(s->rtp_hd, buf);
00075
00076 ff_url_join(buf, sizeof(buf), "udp", NULL, hostname, port + 1, "%s", path);
00077 ff_udp_set_remote_url(s->rtcp_hd, buf);
00078 return 0;
00079 }
00080
00081
00087 static av_printf_format(3, 4) void url_add_option(char *buf, int buf_size, const char *fmt, ...)
00088 {
00089 char buf1[1024];
00090 va_list ap;
00091
00092 va_start(ap, fmt);
00093 if (strchr(buf, '?'))
00094 av_strlcat(buf, "&", buf_size);
00095 else
00096 av_strlcat(buf, "?", buf_size);
00097 vsnprintf(buf1, sizeof(buf1), fmt, ap);
00098 av_strlcat(buf, buf1, buf_size);
00099 va_end(ap);
00100 }
00101
00102 static void build_udp_url(char *buf, int buf_size,
00103 const char *hostname, int port,
00104 int local_port, int ttl,
00105 int max_packet_size, int connect)
00106 {
00107 ff_url_join(buf, buf_size, "udp", NULL, hostname, port, NULL);
00108 if (local_port >= 0)
00109 url_add_option(buf, buf_size, "localport=%d", local_port);
00110 if (ttl >= 0)
00111 url_add_option(buf, buf_size, "ttl=%d", ttl);
00112 if (max_packet_size >=0)
00113 url_add_option(buf, buf_size, "pkt_size=%d", max_packet_size);
00114 if (connect)
00115 url_add_option(buf, buf_size, "connect=1");
00116 url_add_option(buf, buf_size, "fifo_size=0");
00117 }
00118
00136 static int rtp_open(URLContext *h, const char *uri, int flags)
00137 {
00138 RTPContext *s = h->priv_data;
00139 int rtp_port, rtcp_port,
00140 ttl, connect,
00141 local_rtp_port, local_rtcp_port, max_packet_size;
00142 char hostname[256];
00143 char buf[1024];
00144 char path[1024];
00145 const char *p;
00146
00147 av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &rtp_port,
00148 path, sizeof(path), uri);
00149
00150 ttl = -1;
00151 rtcp_port = rtp_port+1;
00152 local_rtp_port = -1;
00153 local_rtcp_port = -1;
00154 max_packet_size = -1;
00155 connect = 0;
00156
00157 p = strchr(uri, '?');
00158 if (p) {
00159 if (av_find_info_tag(buf, sizeof(buf), "ttl", p)) {
00160 ttl = strtol(buf, NULL, 10);
00161 }
00162 if (av_find_info_tag(buf, sizeof(buf), "rtcpport", p)) {
00163 rtcp_port = strtol(buf, NULL, 10);
00164 }
00165 if (av_find_info_tag(buf, sizeof(buf), "localport", p)) {
00166 local_rtp_port = strtol(buf, NULL, 10);
00167 }
00168 if (av_find_info_tag(buf, sizeof(buf), "localrtpport", p)) {
00169 local_rtp_port = strtol(buf, NULL, 10);
00170 }
00171 if (av_find_info_tag(buf, sizeof(buf), "localrtcpport", p)) {
00172 local_rtcp_port = strtol(buf, NULL, 10);
00173 }
00174 if (av_find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
00175 max_packet_size = strtol(buf, NULL, 10);
00176 }
00177 if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
00178 connect = strtol(buf, NULL, 10);
00179 }
00180 }
00181
00182 build_udp_url(buf, sizeof(buf),
00183 hostname, rtp_port, local_rtp_port, ttl, max_packet_size,
00184 connect);
00185 if (ffurl_open(&s->rtp_hd, buf, flags, &h->interrupt_callback, NULL) < 0)
00186 goto fail;
00187 if (local_rtp_port>=0 && local_rtcp_port<0)
00188 local_rtcp_port = ff_udp_get_local_port(s->rtp_hd) + 1;
00189
00190 build_udp_url(buf, sizeof(buf),
00191 hostname, rtcp_port, local_rtcp_port, ttl, max_packet_size,
00192 connect);
00193 if (ffurl_open(&s->rtcp_hd, buf, flags, &h->interrupt_callback, NULL) < 0)
00194 goto fail;
00195
00196
00197
00198 s->rtp_fd = ffurl_get_file_handle(s->rtp_hd);
00199 s->rtcp_fd = ffurl_get_file_handle(s->rtcp_hd);
00200
00201 h->max_packet_size = s->rtp_hd->max_packet_size;
00202 h->is_streamed = 1;
00203 return 0;
00204
00205 fail:
00206 if (s->rtp_hd)
00207 ffurl_close(s->rtp_hd);
00208 if (s->rtcp_hd)
00209 ffurl_close(s->rtcp_hd);
00210 return AVERROR(EIO);
00211 }
00212
00213 static int rtp_read(URLContext *h, uint8_t *buf, int size)
00214 {
00215 RTPContext *s = h->priv_data;
00216 struct sockaddr_storage from;
00217 socklen_t from_len;
00218 int len, n;
00219 struct pollfd p[2] = {{s->rtp_fd, POLLIN, 0}, {s->rtcp_fd, POLLIN, 0}};
00220
00221 for(;;) {
00222 if (ff_check_interrupt(&h->interrupt_callback))
00223 return AVERROR_EXIT;
00224
00225 n = poll(p, 2, 100);
00226 if (n > 0) {
00227
00228 if (p[1].revents & POLLIN) {
00229 from_len = sizeof(from);
00230 len = recvfrom (s->rtcp_fd, buf, size, 0,
00231 (struct sockaddr *)&from, &from_len);
00232 if (len < 0) {
00233 if (ff_neterrno() == AVERROR(EAGAIN) ||
00234 ff_neterrno() == AVERROR(EINTR))
00235 continue;
00236 return AVERROR(EIO);
00237 }
00238 break;
00239 }
00240
00241 if (p[0].revents & POLLIN) {
00242 from_len = sizeof(from);
00243 len = recvfrom (s->rtp_fd, buf, size, 0,
00244 (struct sockaddr *)&from, &from_len);
00245 if (len < 0) {
00246 if (ff_neterrno() == AVERROR(EAGAIN) ||
00247 ff_neterrno() == AVERROR(EINTR))
00248 continue;
00249 return AVERROR(EIO);
00250 }
00251 break;
00252 }
00253 } else if (n < 0) {
00254 if (ff_neterrno() == AVERROR(EINTR))
00255 continue;
00256 return AVERROR(EIO);
00257 }
00258 }
00259 return len;
00260 }
00261
00262 static int rtp_write(URLContext *h, const uint8_t *buf, int size)
00263 {
00264 RTPContext *s = h->priv_data;
00265 int ret;
00266 URLContext *hd;
00267
00268 if (RTP_PT_IS_RTCP(buf[1])) {
00269
00270 hd = s->rtcp_hd;
00271 } else {
00272
00273 hd = s->rtp_hd;
00274 }
00275
00276 ret = ffurl_write(hd, buf, size);
00277 return ret;
00278 }
00279
00280 static int rtp_close(URLContext *h)
00281 {
00282 RTPContext *s = h->priv_data;
00283
00284 ffurl_close(s->rtp_hd);
00285 ffurl_close(s->rtcp_hd);
00286 return 0;
00287 }
00288
00295 int ff_rtp_get_local_rtp_port(URLContext *h)
00296 {
00297 RTPContext *s = h->priv_data;
00298 return ff_udp_get_local_port(s->rtp_hd);
00299 }
00300
00307 int ff_rtp_get_local_rtcp_port(URLContext *h)
00308 {
00309 RTPContext *s = h->priv_data;
00310 return ff_udp_get_local_port(s->rtcp_hd);
00311 }
00312
00313 static int rtp_get_file_handle(URLContext *h)
00314 {
00315 RTPContext *s = h->priv_data;
00316 return s->rtp_fd;
00317 }
00318
00319 static int rtp_get_multi_file_handle(URLContext *h, int **handles,
00320 int *numhandles)
00321 {
00322 RTPContext *s = h->priv_data;
00323 int *hs = *handles = av_malloc(sizeof(**handles) * 2);
00324 if (!hs)
00325 return AVERROR(ENOMEM);
00326 hs[0] = s->rtp_fd;
00327 hs[1] = s->rtcp_fd;
00328 *numhandles = 2;
00329 return 0;
00330 }
00331
00332 URLProtocol ff_rtp_protocol = {
00333 .name = "rtp",
00334 .url_open = rtp_open,
00335 .url_read = rtp_read,
00336 .url_write = rtp_write,
00337 .url_close = rtp_close,
00338 .url_get_file_handle = rtp_get_file_handle,
00339 .url_get_multi_file_handle = rtp_get_multi_file_handle,
00340 .priv_data_size = sizeof(RTPContext),
00341 .flags = URL_PROTOCOL_FLAG_NETWORK,
00342 };