00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include "libavutil/avstring.h"
00028 #include "avformat.h"
00029 #include "rtpdec.h"
00030
00031 #include <unistd.h>
00032 #include <stdarg.h>
00033 #include "internal.h"
00034 #include "network.h"
00035 #include "os_support.h"
00036 #include <fcntl.h>
00037 #if HAVE_SYS_SELECT_H
00038 #include <sys/select.h>
00039 #endif
00040 #include <sys/time.h>
00041
00042 #define RTP_TX_BUF_SIZE (64 * 1024)
00043 #define RTP_RX_BUF_SIZE (128 * 1024)
00044
00045 typedef struct RTPContext {
00046 URLContext *rtp_hd, *rtcp_hd;
00047 int rtp_fd, rtcp_fd;
00048 } RTPContext;
00049
00060 int rtp_set_remote_url(URLContext *h, const char *uri)
00061 {
00062 RTPContext *s = h->priv_data;
00063 char hostname[256];
00064 int port;
00065
00066 char buf[1024];
00067 char path[1024];
00068
00069 av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port,
00070 path, sizeof(path), uri);
00071
00072 ff_url_join(buf, sizeof(buf), "udp", NULL, hostname, port, "%s", path);
00073 udp_set_remote_url(s->rtp_hd, buf);
00074
00075 ff_url_join(buf, sizeof(buf), "udp", NULL, hostname, port + 1, "%s", path);
00076 udp_set_remote_url(s->rtcp_hd, buf);
00077 return 0;
00078 }
00079
00080
00086 static void url_add_option(char *buf, int buf_size, const char *fmt, ...)
00087 {
00088 char buf1[1024];
00089 va_list ap;
00090
00091 va_start(ap, fmt);
00092 if (strchr(buf, '?'))
00093 av_strlcat(buf, "&", buf_size);
00094 else
00095 av_strlcat(buf, "?", buf_size);
00096 vsnprintf(buf1, sizeof(buf1), fmt, ap);
00097 av_strlcat(buf, buf1, buf_size);
00098 va_end(ap);
00099 }
00100
00101 static void build_udp_url(char *buf, int buf_size,
00102 const char *hostname, int port,
00103 int local_port, int ttl,
00104 int max_packet_size, int connect)
00105 {
00106 ff_url_join(buf, buf_size, "udp", NULL, hostname, port, NULL);
00107 if (local_port >= 0)
00108 url_add_option(buf, buf_size, "localport=%d", local_port);
00109 if (ttl >= 0)
00110 url_add_option(buf, buf_size, "ttl=%d", ttl);
00111 if (max_packet_size >=0)
00112 url_add_option(buf, buf_size, "pkt_size=%d", max_packet_size);
00113 if (connect)
00114 url_add_option(buf, buf_size, "connect=1");
00115 }
00116
00134 static int rtp_open(URLContext *h, const char *uri, int flags)
00135 {
00136 RTPContext *s;
00137 int rtp_port, rtcp_port,
00138 is_output, ttl, connect,
00139 local_rtp_port, local_rtcp_port, max_packet_size;
00140 char hostname[256];
00141 char buf[1024];
00142 char path[1024];
00143 const char *p;
00144
00145 is_output = (flags & URL_WRONLY);
00146
00147 s = av_mallocz(sizeof(RTPContext));
00148 if (!s)
00149 return AVERROR(ENOMEM);
00150 h->priv_data = s;
00151
00152 av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &rtp_port,
00153 path, sizeof(path), uri);
00154
00155 ttl = -1;
00156 rtcp_port = rtp_port+1;
00157 local_rtp_port = -1;
00158 local_rtcp_port = -1;
00159 max_packet_size = -1;
00160 connect = 0;
00161
00162 p = strchr(uri, '?');
00163 if (p) {
00164 if (find_info_tag(buf, sizeof(buf), "ttl", p)) {
00165 ttl = strtol(buf, NULL, 10);
00166 }
00167 if (find_info_tag(buf, sizeof(buf), "rtcpport", p)) {
00168 rtcp_port = strtol(buf, NULL, 10);
00169 }
00170 if (find_info_tag(buf, sizeof(buf), "localport", p)) {
00171 local_rtp_port = strtol(buf, NULL, 10);
00172 }
00173 if (find_info_tag(buf, sizeof(buf), "localrtpport", p)) {
00174 local_rtp_port = strtol(buf, NULL, 10);
00175 }
00176 if (find_info_tag(buf, sizeof(buf), "localrtcpport", p)) {
00177 local_rtcp_port = strtol(buf, NULL, 10);
00178 }
00179 if (find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
00180 max_packet_size = strtol(buf, NULL, 10);
00181 }
00182 if (find_info_tag(buf, sizeof(buf), "connect", p)) {
00183 connect = strtol(buf, NULL, 10);
00184 }
00185 }
00186
00187 build_udp_url(buf, sizeof(buf),
00188 hostname, rtp_port, local_rtp_port, ttl, max_packet_size,
00189 connect);
00190 if (url_open(&s->rtp_hd, buf, flags) < 0)
00191 goto fail;
00192 if (local_rtp_port>=0 && local_rtcp_port<0)
00193 local_rtcp_port = udp_get_local_port(s->rtp_hd) + 1;
00194
00195 build_udp_url(buf, sizeof(buf),
00196 hostname, rtcp_port, local_rtcp_port, ttl, max_packet_size,
00197 connect);
00198 if (url_open(&s->rtcp_hd, buf, flags) < 0)
00199 goto fail;
00200
00201
00202
00203 s->rtp_fd = url_get_file_handle(s->rtp_hd);
00204 s->rtcp_fd = url_get_file_handle(s->rtcp_hd);
00205
00206 h->max_packet_size = url_get_max_packet_size(s->rtp_hd);
00207 h->is_streamed = 1;
00208 return 0;
00209
00210 fail:
00211 if (s->rtp_hd)
00212 url_close(s->rtp_hd);
00213 if (s->rtcp_hd)
00214 url_close(s->rtcp_hd);
00215 av_free(s);
00216 return AVERROR(EIO);
00217 }
00218
00219 static int rtp_read(URLContext *h, uint8_t *buf, int size)
00220 {
00221 RTPContext *s = h->priv_data;
00222 struct sockaddr_storage from;
00223 socklen_t from_len;
00224 int len, fd_max, n;
00225 fd_set rfds;
00226 struct timeval tv;
00227 #if 0
00228 for(;;) {
00229 from_len = sizeof(from);
00230 len = recvfrom (s->rtp_fd, buf, size, 0,
00231 (struct sockaddr *)&from, &from_len);
00232 if (len < 0) {
00233 if (ff_neterrno() == FF_NETERROR(EAGAIN) ||
00234 ff_neterrno() == FF_NETERROR(EINTR))
00235 continue;
00236 return AVERROR(EIO);
00237 }
00238 break;
00239 }
00240 #else
00241 for(;;) {
00242 if (url_interrupt_cb())
00243 return AVERROR(EINTR);
00244
00245 FD_ZERO(&rfds);
00246 fd_max = s->rtp_fd;
00247 FD_SET(s->rtp_fd, &rfds);
00248 if (s->rtcp_fd > fd_max)
00249 fd_max = s->rtcp_fd;
00250 FD_SET(s->rtcp_fd, &rfds);
00251 tv.tv_sec = 0;
00252 tv.tv_usec = 100 * 1000;
00253 n = select(fd_max + 1, &rfds, NULL, NULL, &tv);
00254 if (n > 0) {
00255
00256 if (FD_ISSET(s->rtcp_fd, &rfds)) {
00257 from_len = sizeof(from);
00258 len = recvfrom (s->rtcp_fd, buf, size, 0,
00259 (struct sockaddr *)&from, &from_len);
00260 if (len < 0) {
00261 if (ff_neterrno() == FF_NETERROR(EAGAIN) ||
00262 ff_neterrno() == FF_NETERROR(EINTR))
00263 continue;
00264 return AVERROR(EIO);
00265 }
00266 break;
00267 }
00268
00269 if (FD_ISSET(s->rtp_fd, &rfds)) {
00270 from_len = sizeof(from);
00271 len = recvfrom (s->rtp_fd, buf, size, 0,
00272 (struct sockaddr *)&from, &from_len);
00273 if (len < 0) {
00274 if (ff_neterrno() == FF_NETERROR(EAGAIN) ||
00275 ff_neterrno() == FF_NETERROR(EINTR))
00276 continue;
00277 return AVERROR(EIO);
00278 }
00279 break;
00280 }
00281 } else if (n < 0) {
00282 if (ff_neterrno() == FF_NETERROR(EINTR))
00283 continue;
00284 return AVERROR(EIO);
00285 }
00286 }
00287 #endif
00288 return len;
00289 }
00290
00291 static int rtp_write(URLContext *h, const uint8_t *buf, int size)
00292 {
00293 RTPContext *s = h->priv_data;
00294 int ret;
00295 URLContext *hd;
00296
00297 if (buf[1] >= RTCP_SR && buf[1] <= RTCP_APP) {
00298
00299 hd = s->rtcp_hd;
00300 } else {
00301
00302 hd = s->rtp_hd;
00303 }
00304
00305 ret = url_write(hd, buf, size);
00306 #if 0
00307 {
00308 struct timespec ts;
00309 ts.tv_sec = 0;
00310 ts.tv_nsec = 10 * 1000000;
00311 nanosleep(&ts, NULL);
00312 }
00313 #endif
00314 return ret;
00315 }
00316
00317 static int rtp_close(URLContext *h)
00318 {
00319 RTPContext *s = h->priv_data;
00320
00321 url_close(s->rtp_hd);
00322 url_close(s->rtcp_hd);
00323 av_free(s);
00324 return 0;
00325 }
00326
00333 int rtp_get_local_rtp_port(URLContext *h)
00334 {
00335 RTPContext *s = h->priv_data;
00336 return udp_get_local_port(s->rtp_hd);
00337 }
00338
00345 int rtp_get_local_rtcp_port(URLContext *h)
00346 {
00347 RTPContext *s = h->priv_data;
00348 return udp_get_local_port(s->rtcp_hd);
00349 }
00350
00351 static int rtp_get_file_handle(URLContext *h)
00352 {
00353 RTPContext *s = h->priv_data;
00354 return s->rtp_fd;
00355 }
00356
00357 int rtp_get_rtcp_file_handle(URLContext *h) {
00358 RTPContext *s = h->priv_data;
00359 return s->rtcp_fd;
00360 }
00361
00362 URLProtocol rtp_protocol = {
00363 "rtp",
00364 rtp_open,
00365 rtp_read,
00366 rtp_write,
00367 NULL,
00368 rtp_close,
00369 .url_get_file_handle = rtp_get_file_handle,
00370 };