2011-05-09 13:44:54 +00:00
|
|
|
/*
|
|
|
|
* RTP/mpegts muxer
|
|
|
|
* Copyright (c) 2011 Martin Storsjo
|
|
|
|
*
|
2014-12-19 01:49:36 +00:00
|
|
|
* This file is part of FFmpeg.
|
2011-05-09 13:44:54 +00:00
|
|
|
*
|
2014-12-19 01:49:36 +00:00
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2011-05-09 13:44:54 +00:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
2014-12-19 01:49:36 +00:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2011-05-09 13:44:54 +00:00
|
|
|
* 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
|
2014-12-19 01:49:36 +00:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2011-05-09 13:44:54 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libavutil/mathematics.h"
|
|
|
|
#include "avformat.h"
|
2015-02-24 12:07:54 +00:00
|
|
|
#include "avio_internal.h"
|
2011-05-09 13:44:54 +00:00
|
|
|
|
2021-03-18 12:33:28 +00:00
|
|
|
typedef struct MuxChain {
|
2011-05-09 13:44:54 +00:00
|
|
|
AVFormatContext *mpegts_ctx;
|
|
|
|
AVFormatContext *rtp_ctx;
|
2021-01-29 15:07:57 +00:00
|
|
|
AVPacket *pkt;
|
2021-03-18 12:33:28 +00:00
|
|
|
} MuxChain;
|
2011-05-09 13:44:54 +00:00
|
|
|
|
|
|
|
static int rtp_mpegts_write_close(AVFormatContext *s)
|
|
|
|
{
|
2021-03-18 12:33:28 +00:00
|
|
|
MuxChain *chain = s->priv_data;
|
2011-05-09 13:44:54 +00:00
|
|
|
|
|
|
|
if (chain->mpegts_ctx) {
|
2014-12-19 13:26:23 +00:00
|
|
|
av_write_trailer(chain->mpegts_ctx);
|
2015-02-24 12:07:54 +00:00
|
|
|
ffio_free_dyn_buf(&chain->mpegts_ctx->pb);
|
2011-05-09 13:44:54 +00:00
|
|
|
avformat_free_context(chain->mpegts_ctx);
|
|
|
|
}
|
|
|
|
if (chain->rtp_ctx) {
|
|
|
|
av_write_trailer(chain->rtp_ctx);
|
|
|
|
avformat_free_context(chain->rtp_ctx);
|
|
|
|
}
|
2021-01-29 15:07:57 +00:00
|
|
|
|
|
|
|
av_packet_free(&chain->pkt);
|
|
|
|
|
2011-05-09 13:44:54 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rtp_mpegts_write_header(AVFormatContext *s)
|
|
|
|
{
|
2021-03-18 12:33:28 +00:00
|
|
|
MuxChain *chain = s->priv_data;
|
2011-05-09 13:44:54 +00:00
|
|
|
AVFormatContext *mpegts_ctx = NULL, *rtp_ctx = NULL;
|
2019-03-20 17:38:48 +00:00
|
|
|
ff_const59 AVOutputFormat *mpegts_format = av_guess_format("mpegts", NULL, NULL);
|
|
|
|
ff_const59 AVOutputFormat *rtp_format = av_guess_format("rtp", NULL, NULL);
|
2011-05-09 13:44:54 +00:00
|
|
|
int i, ret = AVERROR(ENOMEM);
|
|
|
|
AVStream *st;
|
|
|
|
|
|
|
|
if (!mpegts_format || !rtp_format)
|
|
|
|
return AVERROR(ENOSYS);
|
|
|
|
mpegts_ctx = avformat_alloc_context();
|
|
|
|
if (!mpegts_ctx)
|
|
|
|
return AVERROR(ENOMEM);
|
2021-01-29 15:07:57 +00:00
|
|
|
chain->pkt = av_packet_alloc();
|
|
|
|
if (!chain->pkt)
|
|
|
|
goto fail;
|
2011-05-09 13:44:54 +00:00
|
|
|
mpegts_ctx->oformat = mpegts_format;
|
|
|
|
mpegts_ctx->max_delay = s->max_delay;
|
2018-07-02 11:38:04 +00:00
|
|
|
av_dict_copy(&mpegts_ctx->metadata, s->metadata, 0);
|
2011-05-09 13:44:54 +00:00
|
|
|
for (i = 0; i < s->nb_streams; i++) {
|
|
|
|
AVStream* st = avformat_new_stream(mpegts_ctx, NULL);
|
|
|
|
if (!st)
|
|
|
|
goto fail;
|
|
|
|
st->time_base = s->streams[i]->time_base;
|
|
|
|
st->sample_aspect_ratio = s->streams[i]->sample_aspect_ratio;
|
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.
2014-06-18 18:42:52 +00:00
|
|
|
avcodec_parameters_copy(st->codecpar, s->streams[i]->codecpar);
|
2011-05-09 13:44:54 +00:00
|
|
|
}
|
|
|
|
if ((ret = avio_open_dyn_buf(&mpegts_ctx->pb)) < 0)
|
|
|
|
goto fail;
|
|
|
|
if ((ret = avformat_write_header(mpegts_ctx, NULL)) < 0)
|
|
|
|
goto fail;
|
|
|
|
for (i = 0; i < s->nb_streams; i++)
|
|
|
|
s->streams[i]->time_base = mpegts_ctx->streams[i]->time_base;
|
|
|
|
|
|
|
|
chain->mpegts_ctx = mpegts_ctx;
|
|
|
|
mpegts_ctx = NULL;
|
|
|
|
|
|
|
|
rtp_ctx = avformat_alloc_context();
|
|
|
|
if (!rtp_ctx) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
rtp_ctx->oformat = rtp_format;
|
|
|
|
st = avformat_new_stream(rtp_ctx, NULL);
|
2017-11-27 01:30:53 +00:00
|
|
|
if (!st) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
2011-05-09 13:44:54 +00:00
|
|
|
st->time_base.num = 1;
|
|
|
|
st->time_base.den = 90000;
|
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.
2014-06-18 18:42:52 +00:00
|
|
|
st->codecpar->codec_id = AV_CODEC_ID_MPEG2TS;
|
2011-05-09 13:44:54 +00:00
|
|
|
rtp_ctx->pb = s->pb;
|
|
|
|
if ((ret = avformat_write_header(rtp_ctx, NULL)) < 0)
|
|
|
|
goto fail;
|
2015-03-09 21:14:19 +00:00
|
|
|
chain->rtp_ctx = rtp_ctx;
|
2011-05-09 13:44:54 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (mpegts_ctx) {
|
2015-03-09 21:09:10 +00:00
|
|
|
ffio_free_dyn_buf(&mpegts_ctx->pb);
|
2018-07-02 11:38:04 +00:00
|
|
|
av_dict_free(&mpegts_ctx->metadata);
|
2011-05-09 13:44:54 +00:00
|
|
|
avformat_free_context(mpegts_ctx);
|
|
|
|
}
|
2019-11-29 05:15:59 +00:00
|
|
|
avformat_free_context(rtp_ctx);
|
2011-05-09 13:44:54 +00:00
|
|
|
rtp_mpegts_write_close(s);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rtp_mpegts_write_packet(AVFormatContext *s, AVPacket *pkt)
|
|
|
|
{
|
2021-03-18 12:33:28 +00:00
|
|
|
MuxChain *chain = s->priv_data;
|
2011-05-09 13:44:54 +00:00
|
|
|
int ret = 0, size;
|
|
|
|
uint8_t *buf;
|
2021-01-29 15:07:57 +00:00
|
|
|
AVPacket *local_pkt = chain->pkt;
|
2011-05-09 13:44:54 +00:00
|
|
|
|
|
|
|
if (!chain->mpegts_ctx->pb) {
|
|
|
|
if ((ret = avio_open_dyn_buf(&chain->mpegts_ctx->pb)) < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
if ((ret = av_write_frame(chain->mpegts_ctx, pkt)) < 0)
|
|
|
|
return ret;
|
|
|
|
size = avio_close_dyn_buf(chain->mpegts_ctx->pb, &buf);
|
|
|
|
chain->mpegts_ctx->pb = NULL;
|
|
|
|
if (size == 0) {
|
|
|
|
av_free(buf);
|
|
|
|
return 0;
|
|
|
|
}
|
2021-01-29 15:07:57 +00:00
|
|
|
av_packet_unref(local_pkt);
|
|
|
|
local_pkt->data = buf;
|
|
|
|
local_pkt->size = size;
|
|
|
|
local_pkt->stream_index = 0;
|
2011-05-09 13:44:54 +00:00
|
|
|
if (pkt->pts != AV_NOPTS_VALUE)
|
2021-01-29 15:07:57 +00:00
|
|
|
local_pkt->pts = av_rescale_q(pkt->pts,
|
2011-05-09 13:44:54 +00:00
|
|
|
s->streams[pkt->stream_index]->time_base,
|
|
|
|
chain->rtp_ctx->streams[0]->time_base);
|
|
|
|
if (pkt->dts != AV_NOPTS_VALUE)
|
2021-01-29 15:07:57 +00:00
|
|
|
local_pkt->dts = av_rescale_q(pkt->dts,
|
2011-05-09 13:44:54 +00:00
|
|
|
s->streams[pkt->stream_index]->time_base,
|
|
|
|
chain->rtp_ctx->streams[0]->time_base);
|
2021-01-29 15:07:57 +00:00
|
|
|
ret = av_write_frame(chain->rtp_ctx, local_pkt);
|
2011-05-09 13:44:54 +00:00
|
|
|
av_free(buf);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
AVOutputFormat ff_rtp_mpegts_muxer = {
|
|
|
|
.name = "rtp_mpegts",
|
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("RTP/mpegts output format"),
|
2021-03-18 12:33:28 +00:00
|
|
|
.priv_data_size = sizeof(MuxChain),
|
2011-05-09 13:44:54 +00:00
|
|
|
.audio_codec = AV_CODEC_ID_AAC,
|
|
|
|
.video_codec = AV_CODEC_ID_MPEG4,
|
|
|
|
.write_header = rtp_mpegts_write_header,
|
|
|
|
.write_packet = rtp_mpegts_write_packet,
|
|
|
|
.write_trailer = rtp_mpegts_write_close,
|
|
|
|
};
|