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 ff_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)
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 }
00114
00123 static int rtp_open(URLContext *h, const char *uri, int flags)
00124 {
00125 RTPContext *s;
00126 int port, is_output, ttl, local_port, max_packet_size;
00127 char hostname[256];
00128 char buf[1024];
00129 char path[1024];
00130 const char *p;
00131
00132 is_output = (flags & URL_WRONLY);
00133
00134 s = av_mallocz(sizeof(RTPContext));
00135 if (!s)
00136 return AVERROR(ENOMEM);
00137 h->priv_data = s;
00138
00139 ff_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port,
00140 path, sizeof(path), uri);
00141
00142 ttl = -1;
00143 local_port = -1;
00144 max_packet_size = -1;
00145
00146 p = strchr(uri, '?');
00147 if (p) {
00148 if (find_info_tag(buf, sizeof(buf), "ttl", p)) {
00149 ttl = strtol(buf, NULL, 10);
00150 }
00151 if (find_info_tag(buf, sizeof(buf), "localport", p)) {
00152 local_port = strtol(buf, NULL, 10);
00153 }
00154 if (find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
00155 max_packet_size = strtol(buf, NULL, 10);
00156 }
00157 }
00158
00159 build_udp_url(buf, sizeof(buf),
00160 hostname, port, local_port, ttl, max_packet_size);
00161 if (url_open(&s->rtp_hd, buf, flags) < 0)
00162 goto fail;
00163 local_port = udp_get_local_port(s->rtp_hd);
00164
00165
00166
00167
00168 build_udp_url(buf, sizeof(buf),
00169 hostname, port + 1, local_port + 1, ttl, max_packet_size);
00170 if (url_open(&s->rtcp_hd, buf, flags) < 0)
00171 goto fail;
00172
00173
00174
00175 s->rtp_fd = url_get_file_handle(s->rtp_hd);
00176 s->rtcp_fd = url_get_file_handle(s->rtcp_hd);
00177
00178 h->max_packet_size = url_get_max_packet_size(s->rtp_hd);
00179 h->is_streamed = 1;
00180 return 0;
00181
00182 fail:
00183 if (s->rtp_hd)
00184 url_close(s->rtp_hd);
00185 if (s->rtcp_hd)
00186 url_close(s->rtcp_hd);
00187 av_free(s);
00188 return AVERROR(EIO);
00189 }
00190
00191 static int rtp_read(URLContext *h, uint8_t *buf, int size)
00192 {
00193 RTPContext *s = h->priv_data;
00194 struct sockaddr_in from;
00195 socklen_t from_len;
00196 int len, fd_max, n;
00197 fd_set rfds;
00198 struct timeval tv;
00199 #if 0
00200 for(;;) {
00201 from_len = sizeof(from);
00202 len = recvfrom (s->rtp_fd, buf, size, 0,
00203 (struct sockaddr *)&from, &from_len);
00204 if (len < 0) {
00205 if (ff_neterrno() == FF_NETERROR(EAGAIN) ||
00206 ff_neterrno() == FF_NETERROR(EINTR))
00207 continue;
00208 return AVERROR(EIO);
00209 }
00210 break;
00211 }
00212 #else
00213 for(;;) {
00214 if (url_interrupt_cb())
00215 return AVERROR(EINTR);
00216
00217 FD_ZERO(&rfds);
00218 fd_max = s->rtp_fd;
00219 FD_SET(s->rtp_fd, &rfds);
00220 if (s->rtcp_fd > fd_max)
00221 fd_max = s->rtcp_fd;
00222 FD_SET(s->rtcp_fd, &rfds);
00223 tv.tv_sec = 0;
00224 tv.tv_usec = 100 * 1000;
00225 n = select(fd_max + 1, &rfds, NULL, NULL, &tv);
00226 if (n > 0) {
00227
00228 if (FD_ISSET(s->rtcp_fd, &rfds)) {
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() == FF_NETERROR(EAGAIN) ||
00234 ff_neterrno() == FF_NETERROR(EINTR))
00235 continue;
00236 return AVERROR(EIO);
00237 }
00238 break;
00239 }
00240
00241 if (FD_ISSET(s->rtp_fd, &rfds)) {
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() == FF_NETERROR(EAGAIN) ||
00247 ff_neterrno() == FF_NETERROR(EINTR))
00248 continue;
00249 return AVERROR(EIO);
00250 }
00251 break;
00252 }
00253 } else if (n < 0) {
00254 if (ff_neterrno() == FF_NETERROR(EINTR))
00255 continue;
00256 return AVERROR(EIO);
00257 }
00258 }
00259 #endif
00260 return len;
00261 }
00262
00263 static int rtp_write(URLContext *h, uint8_t *buf, int size)
00264 {
00265 RTPContext *s = h->priv_data;
00266 int ret;
00267 URLContext *hd;
00268
00269 if (buf[1] >= 200 && buf[1] <= 204) {
00270
00271 hd = s->rtcp_hd;
00272 } else {
00273
00274 hd = s->rtp_hd;
00275 }
00276
00277 ret = url_write(hd, buf, size);
00278 #if 0
00279 {
00280 struct timespec ts;
00281 ts.tv_sec = 0;
00282 ts.tv_nsec = 10 * 1000000;
00283 nanosleep(&ts, NULL);
00284 }
00285 #endif
00286 return ret;
00287 }
00288
00289 static int rtp_close(URLContext *h)
00290 {
00291 RTPContext *s = h->priv_data;
00292
00293 url_close(s->rtp_hd);
00294 url_close(s->rtcp_hd);
00295 av_free(s);
00296 return 0;
00297 }
00298
00305 int rtp_get_local_port(URLContext *h)
00306 {
00307 RTPContext *s = h->priv_data;
00308 return udp_get_local_port(s->rtp_hd);
00309 }
00310
00311 #if (LIBAVFORMAT_VERSION_MAJOR <= 52)
00312
00318 void rtp_get_file_handles(URLContext *h, int *prtp_fd, int *prtcp_fd)
00319 {
00320 RTPContext *s = h->priv_data;
00321
00322 *prtp_fd = s->rtp_fd;
00323 *prtcp_fd = s->rtcp_fd;
00324 }
00325 #endif
00326
00327 static int rtp_get_file_handle(URLContext *h)
00328 {
00329 RTPContext *s = h->priv_data;
00330 return s->rtp_fd;
00331 }
00332
00333 URLProtocol rtp_protocol = {
00334 "rtp",
00335 rtp_open,
00336 rtp_read,
00337 rtp_write,
00338 NULL,
00339 rtp_close,
00340 .url_get_file_handle = rtp_get_file_handle,
00341 };