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