2013-01-17 15:34:59 +03:00
|
|
|
/*
|
|
|
|
* RTP MPEG2TS depacketizer
|
|
|
|
* Copyright (c) 2003 Fabrice Bellard
|
|
|
|
*
|
2013-01-21 15:52:06 +03:00
|
|
|
* This file is part of FFmpeg.
|
2013-01-17 15:34:59 +03:00
|
|
|
*
|
2013-01-21 15:52:06 +03:00
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2013-01-17 15:34:59 +03: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.
|
|
|
|
*
|
2013-01-21 15:52:06 +03:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2013-01-17 15:34:59 +03: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
|
2013-01-21 15:52:06 +03:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2013-01-17 15:34:59 +03:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2013-04-18 16:54:26 +03:00
|
|
|
#include "libavutil/attributes.h"
|
2013-01-17 15:34:59 +03:00
|
|
|
#include "mpegts.h"
|
|
|
|
#include "rtpdec_formats.h"
|
|
|
|
|
|
|
|
struct PayloadContext {
|
|
|
|
struct MpegTSContext *ts;
|
|
|
|
int read_buf_index;
|
|
|
|
int read_buf_size;
|
|
|
|
uint8_t buf[RTP_MAX_PACKET_LENGTH];
|
|
|
|
};
|
|
|
|
|
2015-02-24 17:01:48 +02:00
|
|
|
static void mpegts_close_context(PayloadContext *data)
|
2013-01-17 15:34:59 +03:00
|
|
|
{
|
|
|
|
if (!data)
|
|
|
|
return;
|
|
|
|
if (data->ts)
|
2014-08-07 02:59:56 +03:00
|
|
|
avpriv_mpegts_parse_close(data->ts);
|
2013-01-17 15:34:59 +03:00
|
|
|
}
|
|
|
|
|
2013-04-18 16:54:26 +03:00
|
|
|
static av_cold int mpegts_init(AVFormatContext *ctx, int st_index,
|
|
|
|
PayloadContext *data)
|
2013-01-17 15:34:59 +03:00
|
|
|
{
|
2014-08-07 02:59:56 +03:00
|
|
|
data->ts = avpriv_mpegts_parse_open(ctx);
|
2013-01-17 15:34:59 +03:00
|
|
|
if (!data->ts)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mpegts_handle_packet(AVFormatContext *ctx, PayloadContext *data,
|
|
|
|
AVStream *st, AVPacket *pkt, uint32_t *timestamp,
|
|
|
|
const uint8_t *buf, int len, uint16_t seq,
|
|
|
|
int flags)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
// We don't want to use the RTP timestamps at all. If the mpegts demuxer
|
|
|
|
// doesn't set any pts/dts, the generic rtpdec code shouldn't try to
|
|
|
|
// fill it in either, since the mpegts and RTP timestamps are in totally
|
|
|
|
// different ranges.
|
|
|
|
*timestamp = RTP_NOTS_VALUE;
|
|
|
|
|
|
|
|
if (!buf) {
|
|
|
|
if (data->read_buf_index >= data->read_buf_size)
|
|
|
|
return AVERROR(EAGAIN);
|
2014-08-07 02:59:56 +03:00
|
|
|
ret = avpriv_mpegts_parse_packet(data->ts, pkt, data->buf + data->read_buf_index,
|
|
|
|
data->read_buf_size - data->read_buf_index);
|
2013-01-17 15:34:59 +03:00
|
|
|
if (ret < 0)
|
|
|
|
return AVERROR(EAGAIN);
|
|
|
|
data->read_buf_index += ret;
|
|
|
|
if (data->read_buf_index < data->read_buf_size)
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-07 02:59:56 +03:00
|
|
|
ret = avpriv_mpegts_parse_packet(data->ts, pkt, buf, len);
|
|
|
|
/* The only error that can be returned from avpriv_mpegts_parse_packet
|
2013-01-17 15:34:59 +03:00
|
|
|
* is "no more data to return from the provided buffer", so return
|
|
|
|
* AVERROR(EAGAIN) for all errors */
|
|
|
|
if (ret < 0)
|
|
|
|
return AVERROR(EAGAIN);
|
|
|
|
if (ret < len) {
|
|
|
|
data->read_buf_size = FFMIN(len - ret, sizeof(data->buf));
|
|
|
|
memcpy(data->buf, buf + ret, data->read_buf_size);
|
|
|
|
data->read_buf_index = 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-02-12 23:21:06 +02:00
|
|
|
const RTPDynamicProtocolHandler ff_mpegts_dynamic_handler = {
|
2013-01-17 15:34:59 +03:00
|
|
|
.codec_type = AVMEDIA_TYPE_DATA,
|
2015-02-23 22:18:32 +02:00
|
|
|
.priv_data_size = sizeof(PayloadContext),
|
2013-01-17 15:34:59 +03:00
|
|
|
.parse_packet = mpegts_handle_packet,
|
|
|
|
.init = mpegts_init,
|
2015-02-24 17:01:48 +02:00
|
|
|
.close = mpegts_close_context,
|
2013-01-17 15:34:59 +03:00
|
|
|
.static_payload_id = 33,
|
|
|
|
};
|