yading@11: /* yading@11: * RTP network protocol yading@11: * Copyright (c) 2002 Fabrice Bellard yading@11: * yading@11: * This file is part of FFmpeg. yading@11: * yading@11: * FFmpeg is free software; you can redistribute it and/or yading@11: * modify it under the terms of the GNU Lesser General Public yading@11: * License as published by the Free Software Foundation; either yading@11: * version 2.1 of the License, or (at your option) any later version. yading@11: * yading@11: * FFmpeg is distributed in the hope that it will be useful, yading@11: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@11: * Lesser General Public License for more details. yading@11: * yading@11: * You should have received a copy of the GNU Lesser General Public yading@11: * License along with FFmpeg; if not, write to the Free Software yading@11: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@11: */ yading@11: yading@11: /** yading@11: * @file yading@11: * RTP protocol yading@11: */ yading@11: yading@11: #include "libavutil/parseutils.h" yading@11: #include "libavutil/avstring.h" yading@11: #include "avformat.h" yading@11: #include "avio_internal.h" yading@11: #include "rtpdec.h" yading@11: #include "url.h" yading@11: yading@11: #include yading@11: #include "internal.h" yading@11: #include "network.h" yading@11: #include "os_support.h" yading@11: #include yading@11: #if HAVE_POLL_H yading@11: #include yading@11: #endif yading@11: yading@11: typedef struct RTPContext { yading@11: URLContext *rtp_hd, *rtcp_hd; yading@11: int rtp_fd, rtcp_fd; yading@11: } RTPContext; yading@11: yading@11: /** yading@11: * If no filename is given to av_open_input_file because you want to yading@11: * get the local port first, then you must call this function to set yading@11: * the remote server address. yading@11: * yading@11: * @param h media file context yading@11: * @param uri of the remote server yading@11: * @return zero if no error. yading@11: */ yading@11: yading@11: int ff_rtp_set_remote_url(URLContext *h, const char *uri) yading@11: { yading@11: RTPContext *s = h->priv_data; yading@11: char hostname[256]; yading@11: int port; yading@11: yading@11: char buf[1024]; yading@11: char path[1024]; yading@11: yading@11: av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, yading@11: path, sizeof(path), uri); yading@11: yading@11: ff_url_join(buf, sizeof(buf), "udp", NULL, hostname, port, "%s", path); yading@11: ff_udp_set_remote_url(s->rtp_hd, buf); yading@11: yading@11: ff_url_join(buf, sizeof(buf), "udp", NULL, hostname, port + 1, "%s", path); yading@11: ff_udp_set_remote_url(s->rtcp_hd, buf); yading@11: return 0; yading@11: } yading@11: yading@11: yading@11: /** yading@11: * add option to url of the form: yading@11: * "http://host:port/path?option1=val1&option2=val2... yading@11: */ yading@11: yading@11: static av_printf_format(3, 4) void url_add_option(char *buf, int buf_size, const char *fmt, ...) yading@11: { yading@11: char buf1[1024]; yading@11: va_list ap; yading@11: yading@11: va_start(ap, fmt); yading@11: if (strchr(buf, '?')) yading@11: av_strlcat(buf, "&", buf_size); yading@11: else yading@11: av_strlcat(buf, "?", buf_size); yading@11: vsnprintf(buf1, sizeof(buf1), fmt, ap); yading@11: av_strlcat(buf, buf1, buf_size); yading@11: va_end(ap); yading@11: } yading@11: yading@11: static void build_udp_url(char *buf, int buf_size, yading@11: const char *hostname, int port, yading@11: int local_port, int ttl, yading@11: int max_packet_size, int connect) yading@11: { yading@11: ff_url_join(buf, buf_size, "udp", NULL, hostname, port, NULL); yading@11: if (local_port >= 0) yading@11: url_add_option(buf, buf_size, "localport=%d", local_port); yading@11: if (ttl >= 0) yading@11: url_add_option(buf, buf_size, "ttl=%d", ttl); yading@11: if (max_packet_size >=0) yading@11: url_add_option(buf, buf_size, "pkt_size=%d", max_packet_size); yading@11: if (connect) yading@11: url_add_option(buf, buf_size, "connect=1"); yading@11: url_add_option(buf, buf_size, "fifo_size=0"); yading@11: } yading@11: yading@11: /** yading@11: * url syntax: rtp://host:port[?option=val...] yading@11: * option: 'ttl=n' : set the ttl value (for multicast only) yading@11: * 'rtcpport=n' : set the remote rtcp port to n yading@11: * 'localrtpport=n' : set the local rtp port to n yading@11: * 'localrtcpport=n' : set the local rtcp port to n yading@11: * 'pkt_size=n' : set max packet size yading@11: * 'connect=0/1' : do a connect() on the UDP socket yading@11: * deprecated option: yading@11: * 'localport=n' : set the local port to n yading@11: * yading@11: * if rtcpport isn't set the rtcp port will be the rtp port + 1 yading@11: * if local rtp port isn't set any available port will be used for the local yading@11: * rtp and rtcp ports yading@11: * if the local rtcp port is not set it will be the local rtp port + 1 yading@11: */ yading@11: yading@11: static int rtp_open(URLContext *h, const char *uri, int flags) yading@11: { yading@11: RTPContext *s = h->priv_data; yading@11: int rtp_port, rtcp_port, yading@11: ttl, connect, yading@11: local_rtp_port, local_rtcp_port, max_packet_size; yading@11: char hostname[256]; yading@11: char buf[1024]; yading@11: char path[1024]; yading@11: const char *p; yading@11: yading@11: av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &rtp_port, yading@11: path, sizeof(path), uri); yading@11: /* extract parameters */ yading@11: ttl = -1; yading@11: rtcp_port = rtp_port+1; yading@11: local_rtp_port = -1; yading@11: local_rtcp_port = -1; yading@11: max_packet_size = -1; yading@11: connect = 0; yading@11: yading@11: p = strchr(uri, '?'); yading@11: if (p) { yading@11: if (av_find_info_tag(buf, sizeof(buf), "ttl", p)) { yading@11: ttl = strtol(buf, NULL, 10); yading@11: } yading@11: if (av_find_info_tag(buf, sizeof(buf), "rtcpport", p)) { yading@11: rtcp_port = strtol(buf, NULL, 10); yading@11: } yading@11: if (av_find_info_tag(buf, sizeof(buf), "localport", p)) { yading@11: local_rtp_port = strtol(buf, NULL, 10); yading@11: } yading@11: if (av_find_info_tag(buf, sizeof(buf), "localrtpport", p)) { yading@11: local_rtp_port = strtol(buf, NULL, 10); yading@11: } yading@11: if (av_find_info_tag(buf, sizeof(buf), "localrtcpport", p)) { yading@11: local_rtcp_port = strtol(buf, NULL, 10); yading@11: } yading@11: if (av_find_info_tag(buf, sizeof(buf), "pkt_size", p)) { yading@11: max_packet_size = strtol(buf, NULL, 10); yading@11: } yading@11: if (av_find_info_tag(buf, sizeof(buf), "connect", p)) { yading@11: connect = strtol(buf, NULL, 10); yading@11: } yading@11: } yading@11: yading@11: build_udp_url(buf, sizeof(buf), yading@11: hostname, rtp_port, local_rtp_port, ttl, max_packet_size, yading@11: connect); yading@11: if (ffurl_open(&s->rtp_hd, buf, flags, &h->interrupt_callback, NULL) < 0) yading@11: goto fail; yading@11: if (local_rtp_port>=0 && local_rtcp_port<0) yading@11: local_rtcp_port = ff_udp_get_local_port(s->rtp_hd) + 1; yading@11: yading@11: build_udp_url(buf, sizeof(buf), yading@11: hostname, rtcp_port, local_rtcp_port, ttl, max_packet_size, yading@11: connect); yading@11: if (ffurl_open(&s->rtcp_hd, buf, flags, &h->interrupt_callback, NULL) < 0) yading@11: goto fail; yading@11: yading@11: /* just to ease handle access. XXX: need to suppress direct handle yading@11: access */ yading@11: s->rtp_fd = ffurl_get_file_handle(s->rtp_hd); yading@11: s->rtcp_fd = ffurl_get_file_handle(s->rtcp_hd); yading@11: yading@11: h->max_packet_size = s->rtp_hd->max_packet_size; yading@11: h->is_streamed = 1; yading@11: return 0; yading@11: yading@11: fail: yading@11: if (s->rtp_hd) yading@11: ffurl_close(s->rtp_hd); yading@11: if (s->rtcp_hd) yading@11: ffurl_close(s->rtcp_hd); yading@11: return AVERROR(EIO); yading@11: } yading@11: yading@11: static int rtp_read(URLContext *h, uint8_t *buf, int size) yading@11: { yading@11: RTPContext *s = h->priv_data; yading@11: struct sockaddr_storage from; yading@11: socklen_t from_len; yading@11: int len, n; yading@11: struct pollfd p[2] = {{s->rtp_fd, POLLIN, 0}, {s->rtcp_fd, POLLIN, 0}}; yading@11: yading@11: for(;;) { yading@11: if (ff_check_interrupt(&h->interrupt_callback)) yading@11: return AVERROR_EXIT; yading@11: /* build fdset to listen to RTP and RTCP packets */ yading@11: n = poll(p, 2, 100); yading@11: if (n > 0) { yading@11: /* first try RTCP */ yading@11: if (p[1].revents & POLLIN) { yading@11: from_len = sizeof(from); yading@11: len = recvfrom (s->rtcp_fd, buf, size, 0, yading@11: (struct sockaddr *)&from, &from_len); yading@11: if (len < 0) { yading@11: if (ff_neterrno() == AVERROR(EAGAIN) || yading@11: ff_neterrno() == AVERROR(EINTR)) yading@11: continue; yading@11: return AVERROR(EIO); yading@11: } yading@11: break; yading@11: } yading@11: /* then RTP */ yading@11: if (p[0].revents & POLLIN) { yading@11: from_len = sizeof(from); yading@11: len = recvfrom (s->rtp_fd, buf, size, 0, yading@11: (struct sockaddr *)&from, &from_len); yading@11: if (len < 0) { yading@11: if (ff_neterrno() == AVERROR(EAGAIN) || yading@11: ff_neterrno() == AVERROR(EINTR)) yading@11: continue; yading@11: return AVERROR(EIO); yading@11: } yading@11: break; yading@11: } yading@11: } else if (n < 0) { yading@11: if (ff_neterrno() == AVERROR(EINTR)) yading@11: continue; yading@11: return AVERROR(EIO); yading@11: } yading@11: } yading@11: return len; yading@11: } yading@11: yading@11: static int rtp_write(URLContext *h, const uint8_t *buf, int size) yading@11: { yading@11: RTPContext *s = h->priv_data; yading@11: int ret; yading@11: URLContext *hd; yading@11: yading@11: if (RTP_PT_IS_RTCP(buf[1])) { yading@11: /* RTCP payload type */ yading@11: hd = s->rtcp_hd; yading@11: } else { yading@11: /* RTP payload type */ yading@11: hd = s->rtp_hd; yading@11: } yading@11: yading@11: ret = ffurl_write(hd, buf, size); yading@11: return ret; yading@11: } yading@11: yading@11: static int rtp_close(URLContext *h) yading@11: { yading@11: RTPContext *s = h->priv_data; yading@11: yading@11: ffurl_close(s->rtp_hd); yading@11: ffurl_close(s->rtcp_hd); yading@11: return 0; yading@11: } yading@11: yading@11: /** yading@11: * Return the local rtp port used by the RTP connection yading@11: * @param h media file context yading@11: * @return the local port number yading@11: */ yading@11: yading@11: int ff_rtp_get_local_rtp_port(URLContext *h) yading@11: { yading@11: RTPContext *s = h->priv_data; yading@11: return ff_udp_get_local_port(s->rtp_hd); yading@11: } yading@11: yading@11: /** yading@11: * Return the local rtcp port used by the RTP connection yading@11: * @param h media file context yading@11: * @return the local port number yading@11: */ yading@11: yading@11: int ff_rtp_get_local_rtcp_port(URLContext *h) yading@11: { yading@11: RTPContext *s = h->priv_data; yading@11: return ff_udp_get_local_port(s->rtcp_hd); yading@11: } yading@11: yading@11: static int rtp_get_file_handle(URLContext *h) yading@11: { yading@11: RTPContext *s = h->priv_data; yading@11: return s->rtp_fd; yading@11: } yading@11: yading@11: static int rtp_get_multi_file_handle(URLContext *h, int **handles, yading@11: int *numhandles) yading@11: { yading@11: RTPContext *s = h->priv_data; yading@11: int *hs = *handles = av_malloc(sizeof(**handles) * 2); yading@11: if (!hs) yading@11: return AVERROR(ENOMEM); yading@11: hs[0] = s->rtp_fd; yading@11: hs[1] = s->rtcp_fd; yading@11: *numhandles = 2; yading@11: return 0; yading@11: } yading@11: yading@11: URLProtocol ff_rtp_protocol = { yading@11: .name = "rtp", yading@11: .url_open = rtp_open, yading@11: .url_read = rtp_read, yading@11: .url_write = rtp_write, yading@11: .url_close = rtp_close, yading@11: .url_get_file_handle = rtp_get_file_handle, yading@11: .url_get_multi_file_handle = rtp_get_multi_file_handle, yading@11: .priv_data_size = sizeof(RTPContext), yading@11: .flags = URL_PROTOCOL_FLAG_NETWORK, yading@11: };