summaryrefslogtreecommitdiff
path: root/libavformat/rtsp.c
diff options
context:
space:
mode:
authorAnton Khirnov <anton@khirnov.net>2014-06-18 20:42:52 +0200
committerAnton Khirnov <anton@khirnov.net>2016-02-23 17:01:58 +0100
commit9200514ad8717c63f82101dc394f4378854325bf (patch)
tree566b8d48565a88303363198acc81de06363daa7a /libavformat/rtsp.c
parenta8068346e48e123f8d3bdf4d64464d81e53e5fc7 (diff)
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which is used by demuxers to export stream parameters to the caller and by muxers to receive stream parameters from the caller. It is also used internally as the codec context that is passed to parsers. In addition, it is also widely used by the callers as the decoding (when demuxer) or encoding (when muxing) context, though this has been officially discouraged since Libav 11. There are multiple important problems with this approach: - the fields in AVCodecContext are in general one of * stream parameters * codec options * codec state However, it's not clear which ones are which. It is consequently unclear which fields are a demuxer allowed to set or a muxer allowed to read. This leads to erratic behaviour depending on whether decoding or encoding is being performed or not (and whether it uses the AVStream embedded codec context). - various synchronization issues arising from the fact that the same context is used by several different APIs (muxers/demuxers, parsers, bitstream filters and encoders/decoders) simultaneously, with there being no clear rules for who can modify what and the different processes being typically delayed with respect to each other. - avformat_find_stream_info() making it necessary to support opening and closing a single codec context multiple times, thus complicating the semantics of freeing various allocated objects in the codec context. Those problems are resolved by replacing the AVStream embedded codec context with a newly added AVCodecParameters instance, which stores only the stream parameters exported by the demuxers or read by the muxers.
Diffstat (limited to 'libavformat/rtsp.c')
-rw-r--r--libavformat/rtsp.c68
1 files changed, 38 insertions, 30 deletions
diff --git a/libavformat/rtsp.c b/libavformat/rtsp.c
index f1e0780b47..dc07422349 100644
--- a/libavformat/rtsp.c
+++ b/libavformat/rtsp.c
@@ -200,11 +200,11 @@ static int get_sockaddr(AVFormatContext *s,
static void init_rtp_handler(RTPDynamicProtocolHandler *handler,
RTSPStream *rtsp_st, AVStream *st)
{
- AVCodecContext *codec = st ? st->codec : NULL;
+ AVCodecParameters *par = st ? st->codecpar : NULL;
if (!handler)
return;
- if (codec)
- codec->codec_id = handler->codec_id;
+ if (par)
+ par->codec_id = handler->codec_id;
rtsp_st->dynamic_handler = handler;
if (st)
st->need_parsing = handler->need_parsing;
@@ -239,7 +239,7 @@ static int sdp_parse_rtpmap(AVFormatContext *s,
AVStream *st, RTSPStream *rtsp_st,
int payload_type, const char *p)
{
- AVCodecContext *codec = st->codec;
+ AVCodecParameters *par = st->codecpar;
char buf[256];
int i;
AVCodec *c;
@@ -253,22 +253,22 @@ static int sdp_parse_rtpmap(AVFormatContext *s,
if (payload_type < RTP_PT_PRIVATE) {
/* 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);
+ par->codec_id = ff_rtp_codec_id(buf, par->codec_type);
}
- if (codec->codec_id == AV_CODEC_ID_NONE) {
+ if (par->codec_id == AV_CODEC_ID_NONE) {
RTPDynamicProtocolHandler *handler =
- ff_rtp_handler_find_by_name(buf, codec->codec_type);
+ ff_rtp_handler_find_by_name(buf, par->codec_type);
init_rtp_handler(handler, rtsp_st, st);
/* If no dynamic handler was found, check with the list of standard
* allocated types, if such a stream for some reason happens to
* use a private payload type. This isn't handled in rtpdec.c, since
* the format name from the rtpmap line never is passed into rtpdec. */
if (!rtsp_st->dynamic_handler)
- codec->codec_id = ff_rtp_codec_id(buf, codec->codec_type);
+ par->codec_id = ff_rtp_codec_id(buf, par->codec_type);
}
- c = avcodec_find_decoder(codec->codec_id);
+ c = avcodec_find_decoder(par->codec_id);
if (c && c->name)
c_name = c->name;
else
@@ -276,23 +276,23 @@ static int sdp_parse_rtpmap(AVFormatContext *s,
get_word_sep(buf, sizeof(buf), "/", &p);
i = atoi(buf);
- switch (codec->codec_type) {
+ switch (par->codec_type) {
case AVMEDIA_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;
+ par->sample_rate = RTSP_DEFAULT_AUDIO_SAMPLERATE;
+ par->channels = RTSP_DEFAULT_NB_AUDIO_CHANNELS;
if (i > 0) {
- codec->sample_rate = i;
- avpriv_set_pts_info(st, 32, 1, codec->sample_rate);
+ par->sample_rate = i;
+ avpriv_set_pts_info(st, 32, 1, par->sample_rate);
get_word_sep(buf, sizeof(buf), "/", &p);
i = atoi(buf);
if (i > 0)
- codec->channels = i;
+ par->channels = i;
}
av_log(s, AV_LOG_DEBUG, "audio samplerate set to: %i\n",
- codec->sample_rate);
+ par->sample_rate);
av_log(s, AV_LOG_DEBUG, "audio channels set to: %i\n",
- codec->channels);
+ par->channels);
break;
case AVMEDIA_TYPE_VIDEO:
av_log(s, AV_LOG_DEBUG, "video codec set to: %s\n", c_name);
@@ -495,17 +495,17 @@ static void sdp_parse_line(AVFormatContext *s, SDPParseState *s1,
return;
st->id = rt->nb_rtsp_streams - 1;
rtsp_st->stream_index = st->index;
- st->codec->codec_type = codec_type;
+ st->codecpar->codec_type = codec_type;
if (rtsp_st->sdp_payload_type < RTP_PT_PRIVATE) {
RTPDynamicProtocolHandler *handler;
/* if standard payload type, we can find the codec right now */
- ff_rtp_get_codec_info(st->codec, rtsp_st->sdp_payload_type);
- if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
- st->codec->sample_rate > 0)
- avpriv_set_pts_info(st, 32, 1, st->codec->sample_rate);
+ ff_rtp_get_codec_info(st->codecpar, rtsp_st->sdp_payload_type);
+ if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
+ st->codecpar->sample_rate > 0)
+ avpriv_set_pts_info(st, 32, 1, st->codecpar->sample_rate);
/* Even static payload types may need a custom depacketizer */
handler = ff_rtp_handler_find_by_id(
- rtsp_st->sdp_payload_type, st->codec->codec_type);
+ rtsp_st->sdp_payload_type, st->codecpar->codec_type);
init_rtp_handler(handler, rtsp_st, st);
finalize_rtp_handler_init(s, rtsp_st, st);
}
@@ -590,7 +590,7 @@ static void sdp_parse_line(AVFormatContext *s, SDPParseState *s1,
} else if (av_strstart(p, "SampleRate:integer;", &p) &&
s->nb_streams > 0) {
st = s->streams[s->nb_streams - 1];
- st->codec->sample_rate = atoi(p);
+ st->codecpar->sample_rate = atoi(p);
} else if (av_strstart(p, "crypto:", &p) && s->nb_streams > 0) {
// RFC 4568
rtsp_st = rt->rtsp_streams[rt->nb_rtsp_streams - 1];
@@ -1499,7 +1499,7 @@ int ff_rtsp_make_setup_request(AVFormatContext *s, const char *host, int port,
* will return an error. Therefore, we skip those streams. */
if (rt->server_type == RTSP_SERVER_WMS &&
(rtsp_st->stream_index < 0 ||
- s->streams[rtsp_st->stream_index]->codec->codec_type ==
+ s->streams[rtsp_st->stream_index]->codecpar->codec_type ==
AVMEDIA_TYPE_DATA))
continue;
snprintf(transport, sizeof(transport) - 1,
@@ -2371,7 +2371,7 @@ static int rtp_read_header(AVFormatContext *s)
int ret, port;
URLContext* in = NULL;
int payload_type;
- AVCodecContext codec = { 0 };
+ AVCodecParameters *par = NULL;
struct sockaddr_storage addr;
AVIOContext pb;
socklen_t addrlen = sizeof(addr);
@@ -2419,13 +2419,19 @@ static int rtp_read_header(AVFormatContext *s)
ffurl_close(in);
in = NULL;
- if (ff_rtp_get_codec_info(&codec, payload_type)) {
+ par = avcodec_parameters_alloc();
+ if (!par) {
+ ret = AVERROR(ENOMEM);
+ goto fail;
+ }
+
+ if (ff_rtp_get_codec_info(par, payload_type)) {
av_log(s, AV_LOG_ERROR, "Unable to receive RTP payload type %d "
"without an SDP file describing it\n",
payload_type);
goto fail;
}
- if (codec.codec_type != AVMEDIA_TYPE_DATA) {
+ if (par->codec_type != AVMEDIA_TYPE_DATA) {
av_log(s, AV_LOG_WARNING, "Guessing on RTP content - if not received "
"properly you need an SDP file "
"describing it\n");
@@ -2437,10 +2443,11 @@ static int rtp_read_header(AVFormatContext *s)
snprintf(sdp, sizeof(sdp),
"v=0\r\nc=IN IP%d %s\r\nm=%s %d RTP/AVP %d\r\n",
addr.ss_family == AF_INET ? 4 : 6, host,
- codec.codec_type == AVMEDIA_TYPE_DATA ? "application" :
- codec.codec_type == AVMEDIA_TYPE_VIDEO ? "video" : "audio",
+ par->codec_type == AVMEDIA_TYPE_DATA ? "application" :
+ par->codec_type == AVMEDIA_TYPE_VIDEO ? "video" : "audio",
port, payload_type);
av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", sdp);
+ avcodec_parameters_free(&par);
ffio_init_context(&pb, sdp, strlen(sdp), 0, NULL, NULL, NULL, NULL);
s->pb = &pb;
@@ -2455,6 +2462,7 @@ static int rtp_read_header(AVFormatContext *s)
return ret;
fail:
+ avcodec_parameters_free(&par);
if (in)
ffurl_close(in);
ff_network_close();