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 "rtpdec.h"
00031
00032 #include <unistd.h>
00033 #include <stdarg.h>
00034 #include "internal.h"
00035 #include "network.h"
00036 #include "os_support.h"
00037 #include <fcntl.h>
00038 #if HAVE_POLL_H
00039 #include <sys/poll.h>
00040 #endif
00041 #include <sys/time.h>
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 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 udp_set_remote_url(s->rtp_hd, buf);
00075
00076 ff_url_join(buf, sizeof(buf), "udp", NULL, hostname, port + 1, "%s", path);
00077 udp_set_remote_url(s->rtcp_hd, buf);
00078 return 0;
00079 }
00080
00081
00087 static 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 }
00117
00135 static int rtp_open(URLContext *h, const char *uri, int flags)
00136 {
00137 RTPContext *s;
00138 int rtp_port, rtcp_port,
00139 is_output, ttl, connect,
00140 local_rtp_port, local_rtcp_port, max_packet_size;
00141 char hostname[256];
00142 char buf[1024];
00143 char path[1024];
00144 const char *p;
00145
00146 is_output = (flags & URL_WRONLY);
00147
00148 s = av_mallocz(sizeof(RTPContext));
00149 if (!s)
00150 return AVERROR(ENOMEM);
00151 h->priv_data = s;
00152
00153 av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &rtp_port,
00154 path, sizeof(path), uri);
00155
00156 ttl = -1;
00157 rtcp_port = rtp_port+1;
00158 local_rtp_port = -1;
00159 local_rtcp_port = -1;
00160 max_packet_size = -1;
00161 connect = 0;
00162
00163 p = strchr(uri, '?');
00164 if (p) {
00165 if (av_find_info_tag(buf, sizeof(buf), "ttl", p)) {
00166 ttl = strtol(buf, NULL, 10);
00167 }
00168 if (av_find_info_tag(buf, sizeof(buf), "rtcpport", p)) {
00169 rtcp_port = strtol(buf, NULL, 10);
00170 }
00171 if (av_find_info_tag(buf, sizeof(buf), "localport", p)) {
00172 local_rtp_port = strtol(buf, NULL, 10);
00173 }
00174 if (av_find_info_tag(buf, sizeof(buf), "localrtpport", p)) {
00175 local_rtp_port = strtol(buf, NULL, 10);
00176 }
00177 if (av_find_info_tag(buf, sizeof(buf), "localrtcpport", p)) {
00178 local_rtcp_port = strtol(buf, NULL, 10);
00179 }
00180 if (av_find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
00181 max_packet_size = strtol(buf, NULL, 10);
00182 }
00183 if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
00184 connect = strtol(buf, NULL, 10);
00185 }
00186 }
00187
00188 build_udp_url(buf, sizeof(buf),
00189 hostname, rtp_port, local_rtp_port, ttl, max_packet_size,
00190 connect);
00191 if (url_open(&s->rtp_hd, buf, flags) < 0)
00192 goto fail;
00193 if (local_rtp_port>=0 && local_rtcp_port<0)
00194 local_rtcp_port = udp_get_local_port(s->rtp_hd) + 1;
00195
00196 build_udp_url(buf, sizeof(buf),
00197 hostname, rtcp_port, local_rtcp_port, ttl, max_packet_size,
00198 connect);
00199 if (url_open(&s->rtcp_hd, buf, flags) < 0)
00200 goto fail;
00201
00202
00203
00204 s->rtp_fd = url_get_file_handle(s->rtp_hd);
00205 s->rtcp_fd = url_get_file_handle(s->rtcp_hd);
00206
00207 h->max_packet_size = url_get_max_packet_size(s->rtp_hd);
00208 h->is_streamed = 1;
00209 return 0;
00210
00211 fail:
00212 if (s->rtp_hd)
00213 url_close(s->rtp_hd);
00214 if (s->rtcp_hd)
00215 url_close(s->rtcp_hd);
00216 av_free(s);
00217 return AVERROR(EIO);
00218 }
00219
00220 static int rtp_read(URLContext *h, uint8_t *buf, int size)
00221 {
00222 RTPContext *s = h->priv_data;
00223 struct sockaddr_storage from;
00224 socklen_t from_len;
00225 int len, n;
00226 struct pollfd p[2] = {{s->rtp_fd, POLLIN, 0}, {s->rtcp_fd, POLLIN, 0}};
00227
00228 #if 0
00229 for(;;) {
00230 from_len = sizeof(from);
00231 len = recvfrom (s->rtp_fd, buf, size, 0,
00232 (struct sockaddr *)&from, &from_len);
00233 if (len < 0) {
00234 if (ff_neterrno() == AVERROR(EAGAIN) ||
00235 ff_neterrno() == AVERROR(EINTR))
00236 continue;
00237 return AVERROR(EIO);
00238 }
00239 break;
00240 }
00241 #else
00242 for(;;) {
00243 if (url_interrupt_cb())
00244 return AVERROR(EINTR);
00245
00246 n = poll(p, 2, 100);
00247 if (n > 0) {
00248
00249 if (p[1].revents & POLLIN) {
00250 from_len = sizeof(from);
00251 len = recvfrom (s->rtcp_fd, buf, size, 0,
00252 (struct sockaddr *)&from, &from_len);
00253 if (len < 0) {
00254 if (ff_neterrno() == AVERROR(EAGAIN) ||
00255 ff_neterrno() == AVERROR(EINTR))
00256 continue;
00257 return AVERROR(EIO);
00258 }
00259 break;
00260 }
00261
00262 if (p[0].revents & POLLIN) {
00263 from_len = sizeof(from);
00264 len = recvfrom (s->rtp_fd, buf, size, 0,
00265 (struct sockaddr *)&from, &from_len);
00266 if (len < 0) {
00267 if (ff_neterrno() == AVERROR(EAGAIN) ||
00268 ff_neterrno() == AVERROR(EINTR))
00269 continue;
00270 return AVERROR(EIO);
00271 }
00272 break;
00273 }
00274 } else if (n < 0) {
00275 if (ff_neterrno() == AVERROR(EINTR))
00276 continue;
00277 return AVERROR(EIO);
00278 }
00279 }
00280 #endif
00281 return len;
00282 }
00283
00284 static int rtp_write(URLContext *h, const uint8_t *buf, int size)
00285 {
00286 RTPContext *s = h->priv_data;
00287 int ret;
00288 URLContext *hd;
00289
00290 if (buf[1] >= RTCP_SR && buf[1] <= RTCP_APP) {
00291
00292 hd = s->rtcp_hd;
00293 } else {
00294
00295 hd = s->rtp_hd;
00296 }
00297
00298 ret = url_write(hd, buf, size);
00299 #if 0
00300 {
00301 struct timespec ts;
00302 ts.tv_sec = 0;
00303 ts.tv_nsec = 10 * 1000000;
00304 nanosleep(&ts, NULL);
00305 }
00306 #endif
00307 return ret;
00308 }
00309
00310 static int rtp_close(URLContext *h)
00311 {
00312 RTPContext *s = h->priv_data;
00313
00314 url_close(s->rtp_hd);
00315 url_close(s->rtcp_hd);
00316 av_free(s);
00317 return 0;
00318 }
00319
00326 int rtp_get_local_rtp_port(URLContext *h)
00327 {
00328 RTPContext *s = h->priv_data;
00329 return udp_get_local_port(s->rtp_hd);
00330 }
00331
00338 int rtp_get_local_rtcp_port(URLContext *h)
00339 {
00340 RTPContext *s = h->priv_data;
00341 return udp_get_local_port(s->rtcp_hd);
00342 }
00343
00344 static int rtp_get_file_handle(URLContext *h)
00345 {
00346 RTPContext *s = h->priv_data;
00347 return s->rtp_fd;
00348 }
00349
00350 int rtp_get_rtcp_file_handle(URLContext *h) {
00351 RTPContext *s = h->priv_data;
00352 return s->rtcp_fd;
00353 }
00354
00355 URLProtocol ff_rtp_protocol = {
00356 "rtp",
00357 rtp_open,
00358 rtp_read,
00359 rtp_write,
00360 NULL,
00361 rtp_close,
00362 .url_get_file_handle = rtp_get_file_handle,
00363 };