Newer
Older
Fabrice Bellard
committed
* RTSP/SDP client
* Copyright (c) 2002 Fabrice Bellard
Diego Biurrun
committed
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
Diego Biurrun
committed
* version 2.1 of the License, or (at your option) any later version.
Diego Biurrun
committed
* FFmpeg is distributed in the hope that it will be useful,
* 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
Diego Biurrun
committed
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#include "libavutil/base64.h"
#include "libavutil/avstring.h"
#include "libavutil/intreadwrite.h"
#include <sys/time.h>
#include <sys/select.h>
#include "internal.h"
#include "os_support.h"
#include "rtsp.h"
Ronald S. Bultje
committed
#include "rdt.h"
#include "rtpdec_asf.h"
#include "rtpdec_vorbis.h"
Ronald S. Bultje
committed
int rtsp_default_protocols = (1 << RTSP_LOWER_TRANSPORT_UDP);
#define SPACE_CHARS " \t\r\n"
/* we use memchr() instead of strchr() here because strchr() will return
* the terminating '\0' of SPACE_CHARS instead of NULL if c is '\0'. */
#define redir_isspace(c) memchr(SPACE_CHARS, c, 4)
static void skip_spaces(const char **pp)
{
const char *p;
p = *pp;
while (redir_isspace(*p))
p++;
*pp = p;
}
static void get_word_until_chars(char *buf, int buf_size,
const char *sep, const char **pp)
{
const char *p;
char *q;
p = *pp;
skip_spaces(&p);
q = buf;
while (!strchr(sep, *p) && *p != '\0') {
if ((q - buf) < buf_size - 1)
*q++ = *p;
p++;
}
if (buf_size > 0)
*q = '\0';
*pp = p;
}
static void get_word_sep(char *buf, int buf_size, const char *sep,
const char **pp)
if (**pp == '/') (*pp)++;
get_word_until_chars(buf, buf_size, sep, pp);
}
static void get_word(char *buf, int buf_size, const char **pp)
{
get_word_until_chars(buf, buf_size, SPACE_CHARS, pp);
/* parse the rtpmap description: <codec_name>/<clock_rate>[/<other params>] */
static int sdp_parse_rtpmap(AVFormatContext *s,
AVCodecContext *codec, RTSPStream *rtsp_st,
int payload_type, const char *p)
Fabrice Bellard
committed
{
char buf[256];
Stefan Huehner
committed
const char *c_name;
Fabrice Bellard
committed
/* Loop into AVRtpDynamicPayloadTypes[] and AVRtpPayloadTypes[] and
* see if we can handle this kind of payload.
* The space should normally not be there but some Real streams or
* particular servers ("RealServer Version 6.1.3.970", see issue 1658)
* have a trailing space. */
get_word_sep(buf, sizeof(buf), "/ ", &p);
RTPDynamicProtocolHandler *handler;
for (handler = RTPFirstDynamicPayloadHandler;
handler; handler = handler->next) {
if (!strcasecmp(buf, handler->enc_name) &&
codec->codec_type == handler->codec_type) {
codec->codec_id = handler->codec_id;
rtsp_st->dynamic_handler = handler;
if (handler->open)
rtsp_st->dynamic_protocol_context = handler->open();
Fabrice Bellard
committed
} else {
/* We are in a standard case
* (from http://www.iana.org/assignments/rtp-parameters). */
codec->codec_id = ff_rtp_codec_id(buf, codec->codec_type);
}
c = avcodec_find_decoder(codec->codec_id);
if (c && c->name)
Stefan Huehner
committed
c_name = c->name;
Ronald S. Bultje
committed
c_name = "(null)";
get_word_sep(buf, sizeof(buf), "/", &p);
i = atoi(buf);
switch (codec->codec_type) {
case CODEC_TYPE_AUDIO:
av_log(s, AV_LOG_DEBUG, "audio codec set to: %s\n", c_name);
codec->sample_rate = RTSP_DEFAULT_AUDIO_SAMPLERATE;
codec->channels = RTSP_DEFAULT_NB_AUDIO_CHANNELS;
if (i > 0) {
codec->sample_rate = i;
get_word_sep(buf, sizeof(buf), "/", &p);
i = atoi(buf);
if (i > 0)
codec->channels = i;
// TODO: there is a bug here; if it is a mono stream, and
// less than 22000Hz, faad upconverts to stereo and twice
// the frequency. No problem, but the sample rate is being
// set here by the sdp line. Patch on its way. (rdm)
av_log(s, AV_LOG_DEBUG, "audio samplerate set to: %i\n",
codec->sample_rate);
av_log(s, AV_LOG_DEBUG, "audio channels set to: %i\n",
codec->channels);
break;
case CODEC_TYPE_VIDEO:
av_log(s, AV_LOG_DEBUG, "video codec set to: %s\n", c_name);
break;
default:
break;
}
return 0;
Fabrice Bellard
committed
}
/* return the length and optionally the data */
Fabrice Bellard
committed
static int hex_to_data(uint8_t *data, const char *p)
{
int c, len, v;
len = 0;
v = 1;
for (;;) {
Fabrice Bellard
committed
skip_spaces(&p);
Ronald S. Bultje
committed
if (*p == '\0')
Fabrice Bellard
committed
break;
c = toupper((unsigned char) *p++);
Fabrice Bellard
committed
if (c >= '0' && c <= '9')
c = c - '0';
else if (c >= 'A' && c <= 'F')
c = c - 'A' + 10;
else
break;
v = (v << 4) | c;
if (v & 0x100) {
if (data)
data[len] = v;
len++;
v = 1;
}
}
return len;
}
static void sdp_parse_fmtp_config(AVCodecContext * codec, void *ctx,
char *attr, char *value)
case CODEC_ID_MPEG4:
case CODEC_ID_AAC:
if (!strcmp(attr, "config")) {
/* decode the hexa encoded parameter */
int len = hex_to_data(NULL, value);
if (codec->extradata)
av_free(codec->extradata);
codec->extradata = av_mallocz(len + FF_INPUT_BUFFER_PADDING_SIZE);
if (!codec->extradata)
return;
codec->extradata_size = len;
hex_to_data(codec->extradata, value);
}
break;
case CODEC_ID_VORBIS:
ff_vorbis_parse_fmtp_config(codec, ctx, attr, value);
break;
default:
break;
Stefan Huehner
committed
const char *str;
uint16_t type;
uint32_t offset;
/* All known fmtp parmeters and the corresping RTPAttrTypeEnum */
#define ATTR_NAME_TYPE_INT 0
#define ATTR_NAME_TYPE_STR 1
{ "SizeLength", ATTR_NAME_TYPE_INT,
offsetof(RTPPayloadData, sizelength) },
{ "IndexLength", ATTR_NAME_TYPE_INT,
offsetof(RTPPayloadData, indexlength) },
{ "IndexDeltaLength", ATTR_NAME_TYPE_INT,
offsetof(RTPPayloadData, indexdeltalength) },
{ "profile-level-id", ATTR_NAME_TYPE_INT,
offsetof(RTPPayloadData, profile_level_id) },
{ "StreamType", ATTR_NAME_TYPE_INT,
offsetof(RTPPayloadData, streamtype) },
{ "mode", ATTR_NAME_TYPE_STR,
offsetof(RTPPayloadData, mode) },
{ NULL, -1, -1 },
/* parse the attribute line from the fmtp a line of an sdp resonse. This
* is broken out as a function because it is used in rtp_h264.c, which is
* forthcoming. */
int ff_rtsp_next_attr_and_value(const char **p, char *attr, int attr_size,
{
skip_spaces(p);
if (**p) {
get_word_sep(attr, attr_size, "=", p);
if (**p == '=')
(*p)++;
get_word_sep(value, value_size, ";", p);
if (**p == ';')
(*p)++;
return 1;
}
return 0;
}
/* parse a SDP line and save stream attributes */
static void sdp_parse_fmtp(AVStream *st, const char *p)
Fabrice Bellard
committed
{
char attr[256];
/* Vorbis setup headers can be up to 12KB and are sent base64
* encoded, giving a 12KB * (4/3) = 16KB FMTP line. */
char value[16384];
int i;
RTSPStream *rtsp_st = st->priv_data;
Michael Niedermayer
committed
AVCodecContext *codec = st->codec;
RTPPayloadData *rtp_payload_data = &rtsp_st->rtp_payload_data;
Fabrice Bellard
committed
/* loop on each attribute */
while (ff_rtsp_next_attr_and_value(&p, attr, sizeof(attr),
/* grab the codec extra_data from the config parameter of the fmtp
* line */
sdp_parse_fmtp_config(codec, rtsp_st->dynamic_protocol_context,
attr, value);
/* Looking for a known attribute */
for (i = 0; attr_names[i].str; ++i) {
if (!strcasecmp(attr, attr_names[i].str)) {
if (attr_names[i].type == ATTR_NAME_TYPE_INT) {
*(int *)((char *)rtp_payload_data +
attr_names[i].offset) = atoi(value);
} else if (attr_names[i].type == ATTR_NAME_TYPE_STR)
*(char **)((char *)rtp_payload_data +
attr_names[i].offset) = av_strdup(value);
Fabrice Bellard
committed
}
}
}
/** Parse a string p in the form of Range:npt=xx-xx, and determine the start
* and end time.
* Used for seeking in the rtp stream.
*/
static void rtsp_parse_range_npt(const char *p, int64_t *start, int64_t *end)
{
char buf[256];
skip_spaces(&p);
if (!av_stristart(p, "npt=", &p))
return;
*start = AV_NOPTS_VALUE;
*end = AV_NOPTS_VALUE;
get_word_sep(buf, sizeof(buf), "-", &p);
*start = parse_date(buf, 1);
if (*p == '-') {
p++;
get_word_sep(buf, sizeof(buf), "-", &p);
*end = parse_date(buf, 1);
}
// av_log(NULL, AV_LOG_DEBUG, "Range Start: %lld\n", *start);
// av_log(NULL, AV_LOG_DEBUG, "Range End: %lld\n", *end);
}
Fabrice Bellard
committed
typedef struct SDPParseState {
/* SDP only */
struct in_addr default_ip;
int default_ttl;
int skip_media; ///< set if an unknown m= line occurs
Fabrice Bellard
committed
} SDPParseState;
static void sdp_parse_line(AVFormatContext *s, SDPParseState *s1,
int letter, const char *buf)
{
Fabrice Bellard
committed
RTSPState *rt = s->priv_data;
char buf1[64], st_type[64];
const char *p;
enum CodecType codec_type;
int payload_type, i;
AVStream *st;
RTSPStream *rtsp_st;
Fabrice Bellard
committed
struct in_addr sdp_ip;
int ttl;
dprintf(s, "sdp: %c='%s'\n", letter, buf);
if (s1->skip_media && letter != 'm')
return;
switch (letter) {
Fabrice Bellard
committed
case 'c':
get_word(buf1, sizeof(buf1), &p);
if (strcmp(buf1, "IN") != 0)
return;
get_word(buf1, sizeof(buf1), &p);
if (strcmp(buf1, "IP4") != 0)
return;
get_word_sep(buf1, sizeof(buf1), "/", &p);
if (ff_inet_aton(buf1, &sdp_ip) == 0)
Fabrice Bellard
committed
return;
ttl = 16;
if (*p == '/') {
p++;
get_word_sep(buf1, sizeof(buf1), "/", &p);
ttl = atoi(buf1);
}
if (s->nb_streams == 0) {
s1->default_ip = sdp_ip;
s1->default_ttl = ttl;
} else {
st = s->streams[s->nb_streams - 1];
rtsp_st = st->priv_data;
rtsp_st->sdp_ip = sdp_ip;
rtsp_st->sdp_ttl = ttl;
}
break;
av_metadata_set(&s->metadata, "title", p);
break;
case 'i':
if (s->nb_streams == 0) {
av_metadata_set(&s->metadata, "comment", p);
break;
}
break;
case 'm':
/* new stream */
s1->skip_media = 0;
get_word(st_type, sizeof(st_type), &p);
if (!strcmp(st_type, "audio")) {
codec_type = CODEC_TYPE_AUDIO;
} else if (!strcmp(st_type, "video")) {
codec_type = CODEC_TYPE_VIDEO;
} else if (!strcmp(st_type, "application")) {
codec_type = CODEC_TYPE_DATA;
s1->skip_media = 1;
return;
}
rtsp_st = av_mallocz(sizeof(RTSPStream));
if (!rtsp_st)
return;
Fabrice Bellard
committed
rtsp_st->stream_index = -1;
dynarray_add(&rt->rtsp_streams, &rt->nb_rtsp_streams, rtsp_st);
Fabrice Bellard
committed
rtsp_st->sdp_ip = s1->default_ip;
rtsp_st->sdp_ttl = s1->default_ttl;
get_word(buf1, sizeof(buf1), &p); /* port */
rtsp_st->sdp_port = atoi(buf1);
get_word(buf1, sizeof(buf1), &p); /* protocol (ignored) */
Fabrice Bellard
committed
/* XXX: handle list of formats */
get_word(buf1, sizeof(buf1), &p); /* format list */
rtsp_st->sdp_payload_type = atoi(buf1);
if (!strcmp(ff_rtp_enc_name(rtsp_st->sdp_payload_type), "MP2T")) {
Fabrice Bellard
committed
/* no corresponding stream */
} else {
st = av_new_stream(s, 0);
if (!st)
return;
st->priv_data = rtsp_st;
rtsp_st->stream_index = st->index;
Michael Niedermayer
committed
st->codec->codec_type = codec_type;
if (rtsp_st->sdp_payload_type < RTP_PT_PRIVATE) {
Fabrice Bellard
committed
/* if standard payload type, we can find the codec right now */
ff_rtp_get_codec_info(st->codec, rtsp_st->sdp_payload_type);
Fabrice Bellard
committed
}
}
/* put a default control url */
Alan Steremberg
committed
av_strlcpy(rtsp_st->control_url, rt->control_uri,
sizeof(rtsp_st->control_url));
Alan Steremberg
committed
if (av_strstart(p, "control:", &p)) {
if (s->nb_streams == 0) {
if (!strncmp(p, "rtsp://", 7))
av_strlcpy(rt->control_uri, p,
sizeof(rt->control_uri));
} else {
char proto[32];
/* get the control url */
st = s->streams[s->nb_streams - 1];
rtsp_st = st->priv_data;
/* XXX: may need to add full url resolution */
ff_url_split(proto, sizeof(proto), NULL, 0, NULL, 0,
if (proto[0] == '\0') {
/* relative control URL */
Alan Steremberg
committed
if (rtsp_st->control_url[strlen(rtsp_st->control_url)-1]!='/')
av_strlcat(rtsp_st->control_url, "/",
sizeof(rtsp_st->control_url));
av_strlcat(rtsp_st->control_url, p,
sizeof(rtsp_st->control_url));
} else
av_strlcpy(rtsp_st->control_url, p,
sizeof(rtsp_st->control_url));
Alan Steremberg
committed
}
Ronald S. Bultje
committed
} else if (av_strstart(p, "rtpmap:", &p) && s->nb_streams > 0) {
Fabrice Bellard
committed
/* NOTE: rtpmap is only supported AFTER the 'm=' tag */
get_word(buf1, sizeof(buf1), &p);
Fabrice Bellard
committed
payload_type = atoi(buf1);
Ronald S. Bultje
committed
st = s->streams[s->nb_streams - 1];
sdp_parse_rtpmap(s, st->codec, rtsp_st, payload_type, p);
} else if (av_strstart(p, "fmtp:", &p)) {
Fabrice Bellard
committed
/* NOTE: fmtp is only supported AFTER the 'a=rtpmap:xxx' tag */
get_word(buf1, sizeof(buf1), &p);
Fabrice Bellard
committed
payload_type = atoi(buf1);
for (i = 0; i < s->nb_streams; i++) {
st = s->streams[i];
Fabrice Bellard
committed
rtsp_st = st->priv_data;
if (rtsp_st->sdp_payload_type == payload_type) {
if (!(rtsp_st->dynamic_handler &&
rtsp_st->dynamic_handler->parse_sdp_a_line &&
rtsp_st->dynamic_handler->parse_sdp_a_line(s,
i, rtsp_st->dynamic_protocol_context, buf)))
sdp_parse_fmtp(st, p);
Fabrice Bellard
committed
}
}
} else if (av_strstart(p, "framesize:", &p)) {
// let dynamic protocol handlers have a stab at the line.
get_word(buf1, sizeof(buf1), &p);
payload_type = atoi(buf1);
for (i = 0; i < s->nb_streams; i++) {
st = s->streams[i];
rtsp_st = st->priv_data;
if (rtsp_st->sdp_payload_type == payload_type &&
rtsp_st->dynamic_handler &&
rtsp_st->dynamic_handler->parse_sdp_a_line)
rtsp_st->dynamic_handler->parse_sdp_a_line(s, i,
rtsp_st->dynamic_protocol_context, buf);
} else if (av_strstart(p, "range:", &p)) {
int64_t start, end;
// this is so that seeking on a streamed file can work.
rtsp_parse_range_npt(p, &start, &end);
s->start_time = start;
/* AV_NOPTS_VALUE means live broadcast (and can't seek) */
s->duration = (end == AV_NOPTS_VALUE) ?
AV_NOPTS_VALUE : end - start;
Ronald S. Bultje
committed
} else if (av_strstart(p, "IsRealDataType:integer;",&p)) {
if (atoi(p) == 1)
rt->transport = RTSP_TRANSPORT_RDT;
Ronald S. Bultje
committed
} else {
if (rt->server_type == RTSP_SERVER_WMS)
ff_wms_parse_sdp_a_line(s, p);
if (s->nb_streams > 0) {
if (rt->server_type == RTSP_SERVER_REAL)
ff_real_parse_sdp_a_line(s, s->nb_streams - 1, p);
rtsp_st = s->streams[s->nb_streams - 1]->priv_data;
if (rtsp_st->dynamic_handler &&
rtsp_st->dynamic_handler->parse_sdp_a_line)
rtsp_st->dynamic_handler->parse_sdp_a_line(s,
s->nb_streams - 1,
Ronald S. Bultje
committed
}
static int sdp_parse(AVFormatContext *s, const char *content)
{
const char *p;
int letter;
/* Some SDP lines, particularly for Realmedia or ASF RTSP streams,
* contain long SDP lines containing complete ASF Headers (several
* kB) or arrays of MDPR (RM stream descriptor) headers plus
* "rulebooks" describing their properties. Therefore, the SDP line
* buffer is large.
*
* The Vorbis FMTP line can be up to 16KB - see sdp_parse_fmtp. */
char buf[16384], *q;
Fabrice Bellard
committed
SDPParseState sdp_parse_state, *s1 = &sdp_parse_state;
Fabrice Bellard
committed
memset(s1, 0, sizeof(SDPParseState));
for (;;) {
skip_spaces(&p);
letter = *p;
if (letter == '\0')
break;
p++;
if (*p != '=')
goto next_line;
p++;
/* get the content */
q = buf;
while (*p != '\n' && *p != '\r' && *p != '\0') {
if ((q - buf) < sizeof(buf) - 1)
*q++ = *p;
p++;
}
*q = '\0';
Fabrice Bellard
committed
sdp_parse_line(s, s1, letter, buf);
next_line:
while (*p != '\n' && *p != '\0')
p++;
if (*p == '\n')
p++;
}
return 0;
}
/* close and free RTSP streams */
void ff_rtsp_close_streams(AVFormatContext *s)
RTSPState *rt = s->priv_data;
int i;
RTSPStream *rtsp_st;
for (i = 0; i < rt->nb_rtsp_streams; i++) {
rtsp_st = rt->rtsp_streams[i];
if (rtsp_st) {
if (rtsp_st->transport_priv) {
Martin Storsjö
committed
if (s->oformat) {
AVFormatContext *rtpctx = rtsp_st->transport_priv;
av_write_trailer(rtpctx);
if (rt->lower_transport == RTSP_LOWER_TRANSPORT_TCP) {
uint8_t *ptr;
url_close_dyn_buf(rtpctx->pb, &ptr);
av_free(ptr);
} else {
av_metadata_free(&rtpctx->streams[0]->metadata);
av_metadata_free(&rtpctx->metadata);
Martin Storsjö
committed
av_free(rtpctx->streams[0]);
av_free(rtpctx);
} else if (rt->transport == RTSP_TRANSPORT_RDT)
ff_rdt_parse_close(rtsp_st->transport_priv);
else
rtp_parse_close(rtsp_st->transport_priv);
}
if (rtsp_st->rtp_handle)
url_close(rtsp_st->rtp_handle);
if (rtsp_st->dynamic_handler && rtsp_st->dynamic_protocol_context)
rtsp_st->dynamic_handler->close(
rtsp_st->dynamic_protocol_context);
}
}
av_free(rt->rtsp_streams);
if (rt->asf_ctx) {
av_close_input_stream (rt->asf_ctx);
rt->asf_ctx = NULL;
}
}
static void *rtsp_rtp_mux_open(AVFormatContext *s, AVStream *st,
URLContext *handle)
{
RTSPState *rt = s->priv_data;
Martin Storsjö
committed
AVFormatContext *rtpctx;
int ret;
AVOutputFormat *rtp_format = av_guess_format("rtp", NULL, NULL);
if (!rtp_format)
return NULL;
/* Allocate an AVFormatContext for each output stream */
rtpctx = avformat_alloc_context();
if (!rtpctx)
return NULL;
rtpctx->oformat = rtp_format;
if (!av_new_stream(rtpctx, 0)) {
av_free(rtpctx);
return NULL;
}
/* Copy the max delay setting; the rtp muxer reads this. */
rtpctx->max_delay = s->max_delay;
/* Copy other stream parameters. */
rtpctx->streams[0]->sample_aspect_ratio = st->sample_aspect_ratio;
/* Set the synchronized start time. */
rtpctx->start_time_realtime = rt->start_time;
Martin Storsjö
committed
/* Remove the local codec, link to the original codec
* context instead, to give the rtp muxer access to
* codec parameters. */
av_free(rtpctx->streams[0]->codec);
rtpctx->streams[0]->codec = st->codec;
} else
url_open_dyn_packet_buf(&rtpctx->pb, RTSP_TCP_MAX_PACKET_SIZE);
Martin Storsjö
committed
ret = av_write_header(rtpctx);
if (ret) {
} else {
uint8_t *ptr;
url_close_dyn_buf(rtpctx->pb, &ptr);
av_free(ptr);
}
Martin Storsjö
committed
av_free(rtpctx->streams[0]);
av_free(rtpctx);
return NULL;
}
/* Copy the RTP AVStream timebase back to the original AVStream */
st->time_base = rtpctx->streams[0]->time_base;
return rtpctx;
}
static int rtsp_open_transport_ctx(AVFormatContext *s, RTSPStream *rtsp_st)
{
RTSPState *rt = s->priv_data;
AVStream *st = NULL;
/* open the RTP context */
if (rtsp_st->stream_index >= 0)
st = s->streams[rtsp_st->stream_index];
if (!st)
s->ctx_flags |= AVFMTCTX_NOHEADER;
Martin Storsjö
committed
if (s->oformat) {
rtsp_st->transport_priv = rtsp_rtp_mux_open(s, st, rtsp_st->rtp_handle);
/* Ownage of rtp_handle is passed to the rtp mux context */
rtsp_st->rtp_handle = NULL;
} else if (rt->transport == RTSP_TRANSPORT_RDT)
rtsp_st->transport_priv = ff_rdt_parse_open(s, st->index,
rtsp_st->dynamic_protocol_context,
rtsp_st->dynamic_handler);
else
rtsp_st->transport_priv = rtp_parse_open(s, st, rtsp_st->rtp_handle,
rtsp_st->sdp_payload_type,
&rtsp_st->rtp_payload_data);
if (!rtsp_st->transport_priv) {
return AVERROR(ENOMEM);
} else if (rt->transport != RTSP_TRANSPORT_RDT) {
if (rtsp_st->dynamic_handler) {
rtp_parse_set_dynamic_protocol(rtsp_st->transport_priv,
rtsp_st->dynamic_protocol_context,
rtsp_st->dynamic_handler);
}
}
return 0;
}
static int rtsp_probe(AVProbeData *p)
{
if (av_strstart(p->filename, "rtsp:", NULL))
return AVPROBE_SCORE_MAX;
return 0;
}
static void rtsp_parse_range(int *min_ptr, int *max_ptr, const char **pp)
{
const char *p;
int v;
p = *pp;
skip_spaces(&p);
v = strtol(p, (char **)&p, 10);
if (*p == '-') {
p++;
*min_ptr = v;
v = strtol(p, (char **)&p, 10);
*max_ptr = v;
} else {
*min_ptr = v;
*max_ptr = v;
}
*pp = p;
}
/* XXX: only one transport specification is parsed */
static void rtsp_parse_transport(RTSPMessageHeader *reply, const char *p)
{
char transport_protocol[16];
char profile[16];
char lower_transport[16];
char parameter[16];
RTSPTransportField *th;
char buf[256];
for (;;) {
skip_spaces(&p);
if (*p == '\0')
break;
th = &reply->transports[reply->nb_transports];
get_word_sep(transport_protocol, sizeof(transport_protocol),
Luca Barbato
committed
if (!strcasecmp (transport_protocol, "rtp")) {
get_word_sep(profile, sizeof(profile), "/;,", &p);
lower_transport[0] = '\0';
/* rtp/avp/<protocol> */
if (*p == '/') {
get_word_sep(lower_transport, sizeof(lower_transport),
";,", &p);
Ronald S. Bultje
committed
}
th->transport = RTSP_TRANSPORT_RTP;
} else if (!strcasecmp (transport_protocol, "x-pn-tng") ||
!strcasecmp (transport_protocol, "x-real-rdt")) {
/* x-pn-tng/<protocol> */
Luca Barbato
committed
get_word_sep(lower_transport, sizeof(lower_transport), "/;,", &p);
profile[0] = '\0';
Ronald S. Bultje
committed
th->transport = RTSP_TRANSPORT_RDT;
Ronald S. Bultje
committed
th->lower_transport = RTSP_LOWER_TRANSPORT_TCP;
Ronald S. Bultje
committed
th->lower_transport = RTSP_LOWER_TRANSPORT_UDP;
if (*p == ';')
p++;
/* get each parameter */
while (*p != '\0' && *p != ',') {
get_word_sep(parameter, sizeof(parameter), "=;,", &p);
if (!strcmp(parameter, "port")) {
if (*p == '=') {
p++;
rtsp_parse_range(&th->port_min, &th->port_max, &p);
}
} else if (!strcmp(parameter, "client_port")) {
if (*p == '=') {
p++;
rtsp_parse_range(&th->client_port_min,
&th->client_port_max, &p);
}
} else if (!strcmp(parameter, "server_port")) {
if (*p == '=') {
p++;
rtsp_parse_range(&th->server_port_min,
&th->server_port_max, &p);
}
} else if (!strcmp(parameter, "interleaved")) {
if (*p == '=') {
p++;
rtsp_parse_range(&th->interleaved_min,
&th->interleaved_max, &p);
}
} else if (!strcmp(parameter, "multicast")) {
Ronald S. Bultje
committed
if (th->lower_transport == RTSP_LOWER_TRANSPORT_UDP)
th->lower_transport = RTSP_LOWER_TRANSPORT_UDP_MULTICAST;
} else if (!strcmp(parameter, "ttl")) {
if (*p == '=') {
p++;
th->ttl = strtol(p, (char **)&p, 10);
}
} else if (!strcmp(parameter, "destination")) {
struct in_addr ipaddr;
if (*p == '=') {
p++;
get_word_sep(buf, sizeof(buf), ";,", &p);
if (ff_inet_aton(buf, &ipaddr))
th->destination = ntohl(ipaddr.s_addr);
}
}
while (*p != ';' && *p != '\0' && *p != ',')
p++;
if (*p == ';')
p++;
}
if (*p == ',')
p++;
reply->nb_transports++;
}
}
void ff_rtsp_parse_line(RTSPMessageHeader *reply, const char *buf,
HTTPAuthState *auth_state)
{
const char *p;
/* NOTE: we do case independent match for broken servers */
p = buf;
if (av_stristart(p, "Session:", &p)) {
int t;
get_word_sep(reply->session_id, sizeof(reply->session_id), ";", &p);
if (av_stristart(p, ";timeout=", &p) &&
(t = strtol(p, NULL, 10)) > 0) {
reply->timeout = t;
}
} else if (av_stristart(p, "Content-Length:", &p)) {
reply->content_length = strtol(p, NULL, 10);
} else if (av_stristart(p, "Transport:", &p)) {
rtsp_parse_transport(reply, p);
} else if (av_stristart(p, "CSeq:", &p)) {
reply->seq = strtol(p, NULL, 10);
} else if (av_stristart(p, "Range:", &p)) {
rtsp_parse_range_npt(p, &reply->range_start, &reply->range_end);
} else if (av_stristart(p, "RealChallenge1:", &p)) {
skip_spaces(&p);
av_strlcpy(reply->real_challenge, p, sizeof(reply->real_challenge));
Ronald S. Bultje
committed
} else if (av_stristart(p, "Server:", &p)) {
skip_spaces(&p);
av_strlcpy(reply->server, p, sizeof(reply->server));
Ronald S. Bultje
committed
} else if (av_stristart(p, "Notice:", &p) ||
av_stristart(p, "X-Notice:", &p)) {
reply->notice = strtol(p, NULL, 10);
} else if (av_stristart(p, "Location:", &p)) {
skip_spaces(&p);
av_strlcpy(reply->location, p , sizeof(reply->location));
} else if (av_stristart(p, "WWW-Authenticate:", &p) && auth_state) {
skip_spaces(&p);
ff_http_auth_handle_header(auth_state, "WWW-Authenticate", p);
} else if (av_stristart(p, "Authentication-Info:", &p) && auth_state) {
skip_spaces(&p);
ff_http_auth_handle_header(auth_state, "Authentication-Info", p);
/* skip a RTP/TCP interleaved packet */
void ff_rtsp_skip_packet(AVFormatContext *s)
{
RTSPState *rt = s->priv_data;
int ret, len, len1;
uint8_t buf[1024];
ret = url_read_complete(rt->rtsp_hd, buf, 3);
if (ret != 3)
return;
dprintf(s, "skipping RTP packet len=%d\n", len);
/* skip payload */
while (len > 0) {
len1 = len;
if (len1 > sizeof(buf))
len1 = sizeof(buf);
ret = url_read_complete(rt->rtsp_hd, buf, len1);
if (ret != len1)
return;
len -= len1;
}
}
int ff_rtsp_read_reply(AVFormatContext *s, RTSPMessageHeader *reply,
unsigned char **content_ptr,
int return_on_interleaved_data)
{
RTSPState *rt = s->priv_data;
char buf[4096], buf1[1024], *q;
unsigned char ch;
const char *p;
Ronald S. Bultje
committed
int ret, content_length, line_count = 0;
unsigned char *content = NULL;
memset(reply, 0, sizeof(*reply));
/* parse reply (XXX: use buffers) */
rt->last_reply[0] = '\0';
for (;;) {
for (;;) {
ret = url_read_complete(rt->rtsp_hd, &ch, 1);
Ronald S. Bultje
committed
#ifdef DEBUG_RTP_TCP
dprintf(s, "ret=%d c=%02x [%c]\n", ret, ch, ch);
Ronald S. Bultje
committed
#endif
if (ret != 1)
return -1;
if (ch == '$') {
/* XXX: only parse it if first char on line ? */
Ronald S. Bultje
committed
if (return_on_interleaved_data) {
return 1;
} else
ff_rtsp_skip_packet(s);
} else if (ch != '\r') {
if ((q - buf) < sizeof(buf) - 1)
*q++ = ch;
}
}
*q = '\0';
dprintf(s, "line='%s'\n", buf);
/* test if last line */
if (buf[0] == '\0')
break;
p = buf;
if (line_count == 0) {
/* get reply code */
get_word(buf1, sizeof(buf1), &p);
get_word(buf1, sizeof(buf1), &p);
reply->status_code = atoi(buf1);
} else {
ff_rtsp_parse_line(reply, p, &rt->auth_state);
av_strlcat(rt->last_reply, p, sizeof(rt->last_reply));
av_strlcat(rt->last_reply, "\n", sizeof(rt->last_reply));
if (rt->session_id[0] == '\0' && reply->session_id[0] != '\0')
av_strlcpy(rt->session_id, reply->session_id, sizeof(rt->session_id));
content_length = reply->content_length;
if (content_length > 0) {
/* leave some room for a trailing '\0' (useful for simple parsing) */
content = av_malloc(content_length + 1);
(void)url_read_complete(rt->rtsp_hd, content, content_length);
content[content_length] = '\0';
}
if (content_ptr)
*content_ptr = content;
else
av_free(content);
Ronald S. Bultje
committed
if (rt->seq != reply->seq) {
av_log(s, AV_LOG_WARNING, "CSeq %d expected, %d received.\n",
rt->seq, reply->seq);
}
Ronald S. Bultje
committed
/* EOS */
if (reply->notice == 2101 /* End-of-Stream Reached */ ||
reply->notice == 2104 /* Start-of-Stream Reached */ ||
reply->notice == 2306 /* Continuous Feed Terminated */) {
Ronald S. Bultje
committed
rt->state = RTSP_STATE_IDLE;
} else if (reply->notice >= 4400 && reply->notice < 5500) {
Ronald S. Bultje
committed
return AVERROR(EIO); /* data or server error */
} else if (reply->notice == 2401 /* Ticket Expired */ ||
Ronald S. Bultje
committed
(reply->notice >= 5500 && reply->notice < 5600) /* end of term */ )
return AVERROR(EPERM);
Ronald S. Bultje
committed
return 0;