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