|
|
|
@ -349,11 +349,6 @@ srs_error_t SrsTsContext::encode(ISrsStreamWriter* writer, SrsTsMessage* msg, Sr
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SrsTsContext::set_sync_byte(int8_t sb)
|
|
|
|
|
{
|
|
|
|
|
sync_byte = sb;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
srs_error_t SrsTsContext::encode_pat_pmt(ISrsStreamWriter* writer, int16_t vpid, SrsTsStream vs, int16_t apid, SrsTsStream as)
|
|
|
|
|
{
|
|
|
|
|
srs_error_t err = srs_success;
|
|
|
|
@ -812,24 +807,24 @@ SrsTsPacket* SrsTsPacket::create_pes_first(SrsTsContext* context,
|
|
|
|
|
// LCOV_EXCL_STOP
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pes->packet_start_code_prefix = 0x01;
|
|
|
|
|
pes->stream_id = (uint8_t)sid;
|
|
|
|
|
pes->PES_packet_length = (size > 0xFFFF)? 0:size;
|
|
|
|
|
pes->PES_scrambling_control = 0;
|
|
|
|
|
pes->PES_priority = 0;
|
|
|
|
|
pes->data_alignment_indicator = 0;
|
|
|
|
|
pes->copyright = 0;
|
|
|
|
|
pes->original_or_copy = 0;
|
|
|
|
|
pes->PTS_DTS_flags = (dts == pts)? 0x02:0x03;
|
|
|
|
|
pes->ESCR_flag = 0;
|
|
|
|
|
pes->ES_rate_flag = 0;
|
|
|
|
|
pes->DSM_trick_mode_flag = 0;
|
|
|
|
|
pes->additional_copy_info_flag = 0;
|
|
|
|
|
pes->PES_CRC_flag = 0;
|
|
|
|
|
pes->PES_extension_flag = 0;
|
|
|
|
|
pes->PES_header_data_length = 0; // calc in size.
|
|
|
|
|
pes->pts = pts;
|
|
|
|
|
pes->dts = dts;
|
|
|
|
|
pes->pes.packet_start_code_prefix = 0x01;
|
|
|
|
|
pes->pes.stream_id = (uint8_t)sid;
|
|
|
|
|
pes->pes.PES_packet_length = (size > 0xFFFF)? 0:size;
|
|
|
|
|
pes->pes.PES_scrambling_control = 0;
|
|
|
|
|
pes->pes.PES_priority = 0;
|
|
|
|
|
pes->pes.data_alignment_indicator = 0;
|
|
|
|
|
pes->pes.copyright = 0;
|
|
|
|
|
pes->pes.original_or_copy = 0;
|
|
|
|
|
pes->pes.PTS_DTS_flags = (dts == pts)? 0x02:0x03;
|
|
|
|
|
pes->pes.ESCR_flag = 0;
|
|
|
|
|
pes->pes.ES_rate_flag = 0;
|
|
|
|
|
pes->pes.DSM_trick_mode_flag = 0;
|
|
|
|
|
pes->pes.additional_copy_info_flag = 0;
|
|
|
|
|
pes->pes.PES_CRC_flag = 0;
|
|
|
|
|
pes->pes.PES_extension_flag = 0;
|
|
|
|
|
pes->pes.PES_header_data_length = 0; // calc in size.
|
|
|
|
|
pes->pes.pts = pts;
|
|
|
|
|
pes->pes.dts = dts;
|
|
|
|
|
return pkt;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -1249,7 +1244,7 @@ SrsTsPayload::~SrsTsPayload()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SrsTsPayloadPES::SrsTsPayloadPES(SrsTsPacket* p) : SrsTsPayload(p)
|
|
|
|
|
SrsMpegPES::SrsMpegPES()
|
|
|
|
|
{
|
|
|
|
|
nb_stuffings = 0;
|
|
|
|
|
nb_bytes = 0;
|
|
|
|
@ -1291,409 +1286,308 @@ SrsTsPayloadPES::SrsTsPayloadPES(SrsTsPacket* p) : SrsTsPayload(p)
|
|
|
|
|
original_stuff_length = 0;
|
|
|
|
|
P_STD_buffer_scale = 0;
|
|
|
|
|
P_STD_buffer_size = 0;
|
|
|
|
|
|
|
|
|
|
has_payload_ = false;
|
|
|
|
|
nb_payload_ = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SrsTsPayloadPES::~SrsTsPayloadPES()
|
|
|
|
|
SrsMpegPES::~SrsMpegPES()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
srs_error_t SrsTsPayloadPES::decode(SrsBuffer* stream, SrsTsMessage** ppmsg)
|
|
|
|
|
srs_error_t SrsMpegPES::decode(SrsBuffer* stream)
|
|
|
|
|
{
|
|
|
|
|
srs_error_t err = srs_success;
|
|
|
|
|
|
|
|
|
|
// find the channel from chunk.
|
|
|
|
|
SrsTsChannel* channel = packet->context->get(packet->pid);
|
|
|
|
|
if (!channel) {
|
|
|
|
|
return srs_error_new(ERROR_STREAM_CASTER_TS_PSE, "ts: demux PES no channel for pid=%#x", packet->pid);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// init msg.
|
|
|
|
|
SrsTsMessage* msg = channel->msg;
|
|
|
|
|
if (!msg) {
|
|
|
|
|
msg = new SrsTsMessage(channel, packet);
|
|
|
|
|
channel->msg = msg;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// we must cache the fresh state of msg,
|
|
|
|
|
// for the PES_packet_length is 0, the first payload_unit_start_indicator always 1,
|
|
|
|
|
// so should check for the fresh and not completed it.
|
|
|
|
|
bool is_fresh_msg = msg->fresh();
|
|
|
|
|
|
|
|
|
|
// check when fresh, the payload_unit_start_indicator
|
|
|
|
|
// should be 1 for the fresh msg.
|
|
|
|
|
if (is_fresh_msg && !packet->payload_unit_start_indicator) {
|
|
|
|
|
return srs_error_new(ERROR_STREAM_CASTER_TS_PSE, "ts: PES fresh packet length=%d, us=%d, cc=%d",
|
|
|
|
|
msg->PES_packet_length, packet->payload_unit_start_indicator, packet->continuity_counter);
|
|
|
|
|
// 6B fixed header.
|
|
|
|
|
if (!stream->require(6)) {
|
|
|
|
|
return srs_error_new(ERROR_STREAM_CASTER_TS_PSE, "ts: demux PSE");
|
|
|
|
|
}
|
|
|
|
|
// 3B
|
|
|
|
|
packet_start_code_prefix = stream->read_3bytes();
|
|
|
|
|
// 1B
|
|
|
|
|
stream_id = stream->read_1bytes();
|
|
|
|
|
// 2B
|
|
|
|
|
PES_packet_length = stream->read_2bytes();
|
|
|
|
|
|
|
|
|
|
// check when not fresh and PES_packet_length>0,
|
|
|
|
|
// the payload_unit_start_indicator should never be 1 when not completed.
|
|
|
|
|
if (!is_fresh_msg && msg->PES_packet_length > 0 && !msg->completed(packet->payload_unit_start_indicator) && packet->payload_unit_start_indicator) {
|
|
|
|
|
srs_warn("ts: ignore PES packet length=%d, payload=%d, us=%d, cc=%d",
|
|
|
|
|
msg->PES_packet_length, msg->payload->length(), packet->payload_unit_start_indicator, packet->continuity_counter);
|
|
|
|
|
|
|
|
|
|
// reparse current msg.
|
|
|
|
|
stream->skip(stream->pos() * -1);
|
|
|
|
|
srs_freep(msg);
|
|
|
|
|
channel->msg = NULL;
|
|
|
|
|
return err;
|
|
|
|
|
// check the packet start prefix.
|
|
|
|
|
packet_start_code_prefix &= 0xFFFFFF;
|
|
|
|
|
if (packet_start_code_prefix != 0x01) {
|
|
|
|
|
return srs_error_new(ERROR_STREAM_CASTER_TS_PSE, "ts: demux PES start code failed, expect=0x01, actual=%#x", packet_start_code_prefix);
|
|
|
|
|
}
|
|
|
|
|
int pos_packet = stream->pos();
|
|
|
|
|
|
|
|
|
|
// check the continuity counter
|
|
|
|
|
if (!is_fresh_msg) {
|
|
|
|
|
// late-incoming or duplicated continuity, drop message.
|
|
|
|
|
// @remark check overflow, the counter plus 1 should greater when invalid.
|
|
|
|
|
if (msg->continuity_counter >= packet->continuity_counter && ((msg->continuity_counter + 1) & 0x0f) > packet->continuity_counter) {
|
|
|
|
|
srs_warn("ts: drop PES %dB for duplicated cc=%#x", msg->continuity_counter);
|
|
|
|
|
stream->skip(stream->size() - stream->pos());
|
|
|
|
|
return err;
|
|
|
|
|
// @remark the sid indicates the elementary stream format.
|
|
|
|
|
// the SrsTsPESStreamIdAudio and SrsTsPESStreamIdVideo is start by 0b110 or 0b1110
|
|
|
|
|
SrsTsPESStreamId sid = (SrsTsPESStreamId)stream_id;
|
|
|
|
|
if (sid != SrsTsPESStreamIdProgramStreamMap
|
|
|
|
|
&& sid != SrsTsPESStreamIdPaddingStream
|
|
|
|
|
&& sid != SrsTsPESStreamIdPrivateStream2
|
|
|
|
|
&& sid != SrsTsPESStreamIdEcmStream
|
|
|
|
|
&& sid != SrsTsPESStreamIdEmmStream
|
|
|
|
|
&& sid != SrsTsPESStreamIdProgramStreamDirectory
|
|
|
|
|
&& sid != SrsTsPESStreamIdDsmccStream
|
|
|
|
|
&& sid != SrsTsPESStreamIdH2221TypeE
|
|
|
|
|
) {
|
|
|
|
|
// 3B flags.
|
|
|
|
|
if (!stream->require(3)) {
|
|
|
|
|
return srs_error_new(ERROR_STREAM_CASTER_TS_PSE, "ts: demux PSE flags");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// when got partially message, the continous count must be continuous, or drop it.
|
|
|
|
|
if (((msg->continuity_counter + 1) & 0x0f) != packet->continuity_counter) {
|
|
|
|
|
srs_warn("ts: ignore continuity must be continous, msg=%#x, packet=%#x", msg->continuity_counter, packet->continuity_counter);
|
|
|
|
|
|
|
|
|
|
// reparse current msg.
|
|
|
|
|
stream->skip(stream->pos() * -1);
|
|
|
|
|
srs_freep(msg);
|
|
|
|
|
channel->msg = NULL;
|
|
|
|
|
return err;
|
|
|
|
|
// 1B
|
|
|
|
|
int8_t oocv = stream->read_1bytes();
|
|
|
|
|
// 1B
|
|
|
|
|
int8_t pefv = stream->read_1bytes();
|
|
|
|
|
// 1B
|
|
|
|
|
PES_header_data_length = stream->read_1bytes();
|
|
|
|
|
// position of header start.
|
|
|
|
|
int pos_header = stream->pos();
|
|
|
|
|
|
|
|
|
|
const2bits = (oocv >> 6) & 0x03;
|
|
|
|
|
PES_scrambling_control = (oocv >> 4) & 0x03;
|
|
|
|
|
PES_priority = (oocv >> 3) & 0x01;
|
|
|
|
|
data_alignment_indicator = (oocv >> 2) & 0x01;
|
|
|
|
|
copyright = (oocv >> 1) & 0x01;
|
|
|
|
|
original_or_copy = oocv & 0x01;
|
|
|
|
|
|
|
|
|
|
PTS_DTS_flags = (pefv >> 6) & 0x03;
|
|
|
|
|
ESCR_flag = (pefv >> 5) & 0x01;
|
|
|
|
|
ES_rate_flag = (pefv >> 4) & 0x01;
|
|
|
|
|
DSM_trick_mode_flag = (pefv >> 3) & 0x01;
|
|
|
|
|
additional_copy_info_flag = (pefv >> 2) & 0x01;
|
|
|
|
|
PES_CRC_flag = (pefv >> 1) & 0x01;
|
|
|
|
|
PES_extension_flag = pefv & 0x01;
|
|
|
|
|
|
|
|
|
|
// check required together.
|
|
|
|
|
int nb_required = 0;
|
|
|
|
|
nb_required += (PTS_DTS_flags == 0x2)? 5:0;
|
|
|
|
|
nb_required += (PTS_DTS_flags == 0x3)? 10:0;
|
|
|
|
|
nb_required += ESCR_flag? 6:0;
|
|
|
|
|
nb_required += ES_rate_flag? 3:0;
|
|
|
|
|
nb_required += DSM_trick_mode_flag? 1:0;
|
|
|
|
|
nb_required += additional_copy_info_flag? 1:0;
|
|
|
|
|
nb_required += PES_CRC_flag? 2:0;
|
|
|
|
|
nb_required += PES_extension_flag? 1:0;
|
|
|
|
|
if (!stream->require(nb_required)) {
|
|
|
|
|
return srs_error_new(ERROR_STREAM_CASTER_TS_PSE, "ts: demux PSE payload");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
msg->continuity_counter = packet->continuity_counter;
|
|
|
|
|
|
|
|
|
|
// for the PES_packet_length(0), reap when completed.
|
|
|
|
|
if (!is_fresh_msg && msg->completed(packet->payload_unit_start_indicator)) {
|
|
|
|
|
// reap previous PES packet.
|
|
|
|
|
*ppmsg = msg;
|
|
|
|
|
channel->msg = NULL;
|
|
|
|
|
|
|
|
|
|
// reparse current msg.
|
|
|
|
|
stream->skip(stream->pos() * -1);
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// contious packet, append bytes for unit start is 0
|
|
|
|
|
if (!packet->payload_unit_start_indicator) {
|
|
|
|
|
if ((err = msg->dump(stream, &nb_bytes)) != srs_success) {
|
|
|
|
|
return srs_error_wrap(err, "ts: pes dump");
|
|
|
|
|
// 5B
|
|
|
|
|
if (PTS_DTS_flags == 0x2) {
|
|
|
|
|
if ((err = decode_33bits_dts_pts(stream, &pts)) != srs_success) {
|
|
|
|
|
return srs_error_wrap(err, "dts/pts");
|
|
|
|
|
}
|
|
|
|
|
dts = pts;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// when unit start, parse the fresh msg.
|
|
|
|
|
if (packet->payload_unit_start_indicator) {
|
|
|
|
|
// 6B fixed header.
|
|
|
|
|
if (!stream->require(6)) {
|
|
|
|
|
return srs_error_new(ERROR_STREAM_CASTER_TS_PSE, "ts: demux PSE");
|
|
|
|
|
}
|
|
|
|
|
// 3B
|
|
|
|
|
packet_start_code_prefix = stream->read_3bytes();
|
|
|
|
|
// 1B
|
|
|
|
|
stream_id = stream->read_1bytes();
|
|
|
|
|
// 2B
|
|
|
|
|
PES_packet_length = stream->read_2bytes();
|
|
|
|
|
|
|
|
|
|
// check the packet start prefix.
|
|
|
|
|
packet_start_code_prefix &= 0xFFFFFF;
|
|
|
|
|
if (packet_start_code_prefix != 0x01) {
|
|
|
|
|
return srs_error_new(ERROR_STREAM_CASTER_TS_PSE, "ts: demux PES start code failed, expect=0x01, actual=%#x", packet_start_code_prefix);
|
|
|
|
|
}
|
|
|
|
|
int pos_packet = stream->pos();
|
|
|
|
|
|
|
|
|
|
// @remark the sid indicates the elementary stream format.
|
|
|
|
|
// the SrsTsPESStreamIdAudio and SrsTsPESStreamIdVideo is start by 0b110 or 0b1110
|
|
|
|
|
SrsTsPESStreamId sid = (SrsTsPESStreamId)stream_id;
|
|
|
|
|
msg->sid = sid;
|
|
|
|
|
|
|
|
|
|
if (sid != SrsTsPESStreamIdProgramStreamMap
|
|
|
|
|
&& sid != SrsTsPESStreamIdPaddingStream
|
|
|
|
|
&& sid != SrsTsPESStreamIdPrivateStream2
|
|
|
|
|
&& sid != SrsTsPESStreamIdEcmStream
|
|
|
|
|
&& sid != SrsTsPESStreamIdEmmStream
|
|
|
|
|
&& sid != SrsTsPESStreamIdProgramStreamDirectory
|
|
|
|
|
&& sid != SrsTsPESStreamIdDsmccStream
|
|
|
|
|
&& sid != SrsTsPESStreamIdH2221TypeE
|
|
|
|
|
) {
|
|
|
|
|
// 3B flags.
|
|
|
|
|
if (!stream->require(3)) {
|
|
|
|
|
return srs_error_new(ERROR_STREAM_CASTER_TS_PSE, "ts: demux PSE flags");
|
|
|
|
|
// 10B
|
|
|
|
|
if (PTS_DTS_flags == 0x3) {
|
|
|
|
|
if ((err = decode_33bits_dts_pts(stream, &pts)) != srs_success) {
|
|
|
|
|
return srs_error_wrap(err, "dts/pts");
|
|
|
|
|
}
|
|
|
|
|
// 1B
|
|
|
|
|
int8_t oocv = stream->read_1bytes();
|
|
|
|
|
// 1B
|
|
|
|
|
int8_t pefv = stream->read_1bytes();
|
|
|
|
|
// 1B
|
|
|
|
|
PES_header_data_length = stream->read_1bytes();
|
|
|
|
|
// position of header start.
|
|
|
|
|
int pos_header = stream->pos();
|
|
|
|
|
|
|
|
|
|
const2bits = (oocv >> 6) & 0x03;
|
|
|
|
|
PES_scrambling_control = (oocv >> 4) & 0x03;
|
|
|
|
|
PES_priority = (oocv >> 3) & 0x01;
|
|
|
|
|
data_alignment_indicator = (oocv >> 2) & 0x01;
|
|
|
|
|
copyright = (oocv >> 1) & 0x01;
|
|
|
|
|
original_or_copy = oocv & 0x01;
|
|
|
|
|
|
|
|
|
|
PTS_DTS_flags = (pefv >> 6) & 0x03;
|
|
|
|
|
ESCR_flag = (pefv >> 5) & 0x01;
|
|
|
|
|
ES_rate_flag = (pefv >> 4) & 0x01;
|
|
|
|
|
DSM_trick_mode_flag = (pefv >> 3) & 0x01;
|
|
|
|
|
additional_copy_info_flag = (pefv >> 2) & 0x01;
|
|
|
|
|
PES_CRC_flag = (pefv >> 1) & 0x01;
|
|
|
|
|
PES_extension_flag = pefv & 0x01;
|
|
|
|
|
|
|
|
|
|
// check required together.
|
|
|
|
|
int nb_required = 0;
|
|
|
|
|
nb_required += (PTS_DTS_flags == 0x2)? 5:0;
|
|
|
|
|
nb_required += (PTS_DTS_flags == 0x3)? 10:0;
|
|
|
|
|
nb_required += ESCR_flag? 6:0;
|
|
|
|
|
nb_required += ES_rate_flag? 3:0;
|
|
|
|
|
nb_required += DSM_trick_mode_flag? 1:0;
|
|
|
|
|
nb_required += additional_copy_info_flag? 1:0;
|
|
|
|
|
nb_required += PES_CRC_flag? 2:0;
|
|
|
|
|
nb_required += PES_extension_flag? 1:0;
|
|
|
|
|
if (!stream->require(nb_required)) {
|
|
|
|
|
return srs_error_new(ERROR_STREAM_CASTER_TS_PSE, "ts: demux PSE payload");
|
|
|
|
|
if ((err = decode_33bits_dts_pts(stream, &dts)) != srs_success) {
|
|
|
|
|
return srs_error_wrap(err, "dts/pts");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 5B
|
|
|
|
|
if (PTS_DTS_flags == 0x2) {
|
|
|
|
|
if ((err = decode_33bits_dts_pts(stream, &pts)) != srs_success) {
|
|
|
|
|
return srs_error_wrap(err, "dts/pts");
|
|
|
|
|
}
|
|
|
|
|
dts = pts;
|
|
|
|
|
|
|
|
|
|
// update the dts and pts of message.
|
|
|
|
|
msg->dts = dts;
|
|
|
|
|
msg->pts = pts;
|
|
|
|
|
// check sync, the diff of dts and pts should never greater than 1s.
|
|
|
|
|
if (dts - pts > 90000 || pts - dts > 90000) {
|
|
|
|
|
srs_warn("ts: sync dts=%" PRId64 ", pts=%" PRId64, dts, pts);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 10B
|
|
|
|
|
if (PTS_DTS_flags == 0x3) {
|
|
|
|
|
if ((err = decode_33bits_dts_pts(stream, &pts)) != srs_success) {
|
|
|
|
|
return srs_error_wrap(err, "dts/pts");
|
|
|
|
|
}
|
|
|
|
|
if ((err = decode_33bits_dts_pts(stream, &dts)) != srs_success) {
|
|
|
|
|
return srs_error_wrap(err, "dts/pts");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// check sync, the diff of dts and pts should never greater than 1s.
|
|
|
|
|
if (dts - pts > 90000 || pts - dts > 90000) {
|
|
|
|
|
srs_warn("ts: sync dts=%" PRId64 ", pts=%" PRId64, dts, pts);
|
|
|
|
|
}
|
|
|
|
|
// Ignore coverage bellow, for we don't use them in HLS.
|
|
|
|
|
// LCOV_EXCL_START
|
|
|
|
|
|
|
|
|
|
// update the dts and pts of message.
|
|
|
|
|
msg->dts = dts;
|
|
|
|
|
msg->pts = pts;
|
|
|
|
|
}
|
|
|
|
|
// 6B
|
|
|
|
|
if (ESCR_flag) {
|
|
|
|
|
ESCR_extension = 0;
|
|
|
|
|
ESCR_base = 0;
|
|
|
|
|
|
|
|
|
|
// Ignore coverage bellow, for we don't use them in HLS.
|
|
|
|
|
// LCOV_EXCL_START
|
|
|
|
|
stream->skip(6);
|
|
|
|
|
srs_warn("ts: demux PES, ignore the escr.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 6B
|
|
|
|
|
if (ESCR_flag) {
|
|
|
|
|
ESCR_extension = 0;
|
|
|
|
|
ESCR_base = 0;
|
|
|
|
|
// 3B
|
|
|
|
|
if (ES_rate_flag) {
|
|
|
|
|
ES_rate = stream->read_3bytes();
|
|
|
|
|
|
|
|
|
|
stream->skip(6);
|
|
|
|
|
srs_warn("ts: demux PES, ignore the escr.");
|
|
|
|
|
}
|
|
|
|
|
ES_rate = ES_rate >> 1;
|
|
|
|
|
ES_rate &= 0x3FFFFF;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 3B
|
|
|
|
|
if (ES_rate_flag) {
|
|
|
|
|
ES_rate = stream->read_3bytes();
|
|
|
|
|
// 1B
|
|
|
|
|
if (DSM_trick_mode_flag) {
|
|
|
|
|
trick_mode_control = stream->read_1bytes();
|
|
|
|
|
|
|
|
|
|
ES_rate = ES_rate >> 1;
|
|
|
|
|
ES_rate &= 0x3FFFFF;
|
|
|
|
|
}
|
|
|
|
|
trick_mode_value = trick_mode_control & 0x1f;
|
|
|
|
|
trick_mode_control = (trick_mode_control >> 5) & 0x03;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 1B
|
|
|
|
|
if (DSM_trick_mode_flag) {
|
|
|
|
|
trick_mode_control = stream->read_1bytes();
|
|
|
|
|
// 1B
|
|
|
|
|
if (additional_copy_info_flag) {
|
|
|
|
|
additional_copy_info = stream->read_1bytes();
|
|
|
|
|
|
|
|
|
|
trick_mode_value = trick_mode_control & 0x1f;
|
|
|
|
|
trick_mode_control = (trick_mode_control >> 5) & 0x03;
|
|
|
|
|
}
|
|
|
|
|
additional_copy_info &= 0x7f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 1B
|
|
|
|
|
if (additional_copy_info_flag) {
|
|
|
|
|
additional_copy_info = stream->read_1bytes();
|
|
|
|
|
// 2B
|
|
|
|
|
if (PES_CRC_flag) {
|
|
|
|
|
previous_PES_packet_CRC = stream->read_2bytes();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
additional_copy_info &= 0x7f;
|
|
|
|
|
// 1B
|
|
|
|
|
if (PES_extension_flag) {
|
|
|
|
|
int8_t efv = stream->read_1bytes();
|
|
|
|
|
|
|
|
|
|
PES_private_data_flag = (efv >> 7) & 0x01;
|
|
|
|
|
pack_header_field_flag = (efv >> 6) & 0x01;
|
|
|
|
|
program_packet_sequence_counter_flag = (efv >> 5) & 0x01;
|
|
|
|
|
P_STD_buffer_flag = (efv >> 4) & 0x01;
|
|
|
|
|
const1_value0 = (efv >> 1) & 0x07;
|
|
|
|
|
PES_extension_flag_2 = efv & 0x01;
|
|
|
|
|
|
|
|
|
|
nb_required = 0;
|
|
|
|
|
nb_required += PES_private_data_flag? 16:0;
|
|
|
|
|
nb_required += pack_header_field_flag? 1:0; // 1+x bytes.
|
|
|
|
|
nb_required += program_packet_sequence_counter_flag? 2:0;
|
|
|
|
|
nb_required += P_STD_buffer_flag? 2:0;
|
|
|
|
|
nb_required += PES_extension_flag_2? 1:0; // 1+x bytes.
|
|
|
|
|
if (!stream->require(nb_required)) {
|
|
|
|
|
return srs_error_new(ERROR_STREAM_CASTER_TS_PSE, "ts: demux PSE ext payload");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 2B
|
|
|
|
|
if (PES_CRC_flag) {
|
|
|
|
|
previous_PES_packet_CRC = stream->read_2bytes();
|
|
|
|
|
// 16B
|
|
|
|
|
if (PES_private_data_flag) {
|
|
|
|
|
PES_private_data.resize(16);
|
|
|
|
|
stream->read_bytes(&PES_private_data[0], 16);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 1B
|
|
|
|
|
if (PES_extension_flag) {
|
|
|
|
|
int8_t efv = stream->read_1bytes();
|
|
|
|
|
|
|
|
|
|
PES_private_data_flag = (efv >> 7) & 0x01;
|
|
|
|
|
pack_header_field_flag = (efv >> 6) & 0x01;
|
|
|
|
|
program_packet_sequence_counter_flag = (efv >> 5) & 0x01;
|
|
|
|
|
P_STD_buffer_flag = (efv >> 4) & 0x01;
|
|
|
|
|
const1_value0 = (efv >> 1) & 0x07;
|
|
|
|
|
PES_extension_flag_2 = efv & 0x01;
|
|
|
|
|
|
|
|
|
|
nb_required = 0;
|
|
|
|
|
nb_required += PES_private_data_flag? 16:0;
|
|
|
|
|
nb_required += pack_header_field_flag? 1:0; // 1+x bytes.
|
|
|
|
|
nb_required += program_packet_sequence_counter_flag? 2:0;
|
|
|
|
|
nb_required += P_STD_buffer_flag? 2:0;
|
|
|
|
|
nb_required += PES_extension_flag_2? 1:0; // 1+x bytes.
|
|
|
|
|
if (!stream->require(nb_required)) {
|
|
|
|
|
return srs_error_new(ERROR_STREAM_CASTER_TS_PSE, "ts: demux PSE ext payload");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 16B
|
|
|
|
|
if (PES_private_data_flag) {
|
|
|
|
|
PES_private_data.resize(16);
|
|
|
|
|
stream->read_bytes(&PES_private_data[0], 16);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// (1+x)B
|
|
|
|
|
if (pack_header_field_flag) {
|
|
|
|
|
// This is an 8-bit field which indicates the length, in bytes, of the pack_header_field()
|
|
|
|
|
uint8_t pack_field_length = stream->read_1bytes();
|
|
|
|
|
if (pack_field_length > 0) {
|
|
|
|
|
// the adjust required bytes.
|
|
|
|
|
nb_required = nb_required - 16 - 1 + pack_field_length;
|
|
|
|
|
if (!stream->require(nb_required)) {
|
|
|
|
|
return srs_error_new(ERROR_STREAM_CASTER_TS_PSE, "ts: demux PSE ext pack");
|
|
|
|
|
}
|
|
|
|
|
pack_field.resize(pack_field_length);
|
|
|
|
|
stream->read_bytes(&pack_field[0], pack_field_length);
|
|
|
|
|
// (1+x)B
|
|
|
|
|
if (pack_header_field_flag) {
|
|
|
|
|
// This is an 8-bit field which indicates the length, in bytes, of the pack_header_field()
|
|
|
|
|
uint8_t pack_field_length = stream->read_1bytes();
|
|
|
|
|
if (pack_field_length > 0) {
|
|
|
|
|
// the adjust required bytes.
|
|
|
|
|
nb_required = nb_required - 16 - 1 + pack_field_length;
|
|
|
|
|
if (!stream->require(nb_required)) {
|
|
|
|
|
return srs_error_new(ERROR_STREAM_CASTER_TS_PSE, "ts: demux PSE ext pack");
|
|
|
|
|
}
|
|
|
|
|
pack_field.resize(pack_field_length);
|
|
|
|
|
stream->read_bytes(&pack_field[0], pack_field_length);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 2B
|
|
|
|
|
if (program_packet_sequence_counter_flag) {
|
|
|
|
|
program_packet_sequence_counter = stream->read_1bytes();
|
|
|
|
|
program_packet_sequence_counter &= 0x7f;
|
|
|
|
|
|
|
|
|
|
original_stuff_length = stream->read_1bytes();
|
|
|
|
|
MPEG1_MPEG2_identifier = (original_stuff_length >> 6) & 0x01;
|
|
|
|
|
original_stuff_length &= 0x3f;
|
|
|
|
|
}
|
|
|
|
|
// 2B
|
|
|
|
|
if (program_packet_sequence_counter_flag) {
|
|
|
|
|
program_packet_sequence_counter = stream->read_1bytes();
|
|
|
|
|
program_packet_sequence_counter &= 0x7f;
|
|
|
|
|
|
|
|
|
|
// 2B
|
|
|
|
|
if (P_STD_buffer_flag) {
|
|
|
|
|
P_STD_buffer_size = stream->read_2bytes();
|
|
|
|
|
original_stuff_length = stream->read_1bytes();
|
|
|
|
|
MPEG1_MPEG2_identifier = (original_stuff_length >> 6) & 0x01;
|
|
|
|
|
original_stuff_length &= 0x3f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// '01'
|
|
|
|
|
//int8_t const2bits = (P_STD_buffer_scale >>14) & 0x03;
|
|
|
|
|
// 2B
|
|
|
|
|
if (P_STD_buffer_flag) {
|
|
|
|
|
P_STD_buffer_size = stream->read_2bytes();
|
|
|
|
|
|
|
|
|
|
P_STD_buffer_scale = (P_STD_buffer_scale >>13) & 0x01;
|
|
|
|
|
P_STD_buffer_size &= 0x1FFF;
|
|
|
|
|
}
|
|
|
|
|
// '01'
|
|
|
|
|
//int8_t const2bits = (P_STD_buffer_scale >>14) & 0x03;
|
|
|
|
|
|
|
|
|
|
// (1+x)B
|
|
|
|
|
if (PES_extension_flag_2) {
|
|
|
|
|
/**
|
|
|
|
|
* This is a 7-bit field which specifies the length, in bytes, of the data following this field in
|
|
|
|
|
* the PES extension field up to and including any reserved bytes.
|
|
|
|
|
*/
|
|
|
|
|
uint8_t PES_extension_field_length = stream->read_1bytes();
|
|
|
|
|
PES_extension_field_length &= 0x7F;
|
|
|
|
|
|
|
|
|
|
if (PES_extension_field_length > 0) {
|
|
|
|
|
if (!stream->require(PES_extension_field_length)) {
|
|
|
|
|
return srs_error_new(ERROR_STREAM_CASTER_TS_PSE, "ts: demux PSE ext field");
|
|
|
|
|
}
|
|
|
|
|
PES_extension_field.resize(PES_extension_field_length);
|
|
|
|
|
stream->read_bytes(&PES_extension_field[0], PES_extension_field_length);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
P_STD_buffer_scale = (P_STD_buffer_scale >>13) & 0x01;
|
|
|
|
|
P_STD_buffer_size &= 0x1FFF;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// stuffing_byte
|
|
|
|
|
nb_stuffings = PES_header_data_length - (stream->pos() - pos_header);
|
|
|
|
|
if (nb_stuffings > 0) {
|
|
|
|
|
if (!stream->require(nb_stuffings)) {
|
|
|
|
|
return srs_error_new(ERROR_STREAM_CASTER_TS_PSE, "ts: demux PSE stuffings");
|
|
|
|
|
// (1+x)B
|
|
|
|
|
if (PES_extension_flag_2) {
|
|
|
|
|
/**
|
|
|
|
|
* This is a 7-bit field which specifies the length, in bytes, of the data following this field in
|
|
|
|
|
* the PES extension field up to and including any reserved bytes.
|
|
|
|
|
*/
|
|
|
|
|
uint8_t PES_extension_field_length = stream->read_1bytes();
|
|
|
|
|
PES_extension_field_length &= 0x7F;
|
|
|
|
|
|
|
|
|
|
if (PES_extension_field_length > 0) {
|
|
|
|
|
if (!stream->require(PES_extension_field_length)) {
|
|
|
|
|
return srs_error_new(ERROR_STREAM_CASTER_TS_PSE, "ts: demux PSE ext field");
|
|
|
|
|
}
|
|
|
|
|
PES_extension_field.resize(PES_extension_field_length);
|
|
|
|
|
stream->read_bytes(&PES_extension_field[0], PES_extension_field_length);
|
|
|
|
|
}
|
|
|
|
|
stream->skip(nb_stuffings);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// LCOV_EXCL_STOP
|
|
|
|
|
|
|
|
|
|
// PES_packet_data_byte, page58.
|
|
|
|
|
// the packet size contains the header size.
|
|
|
|
|
// The number of PES_packet_data_bytes, N, is specified by the
|
|
|
|
|
// PES_packet_length field. N shall be equal to the value
|
|
|
|
|
// indicated in the PES_packet_length minus the number of bytes
|
|
|
|
|
// between the last byte of the PES_packet_length field and the
|
|
|
|
|
// first PES_packet_data_byte.
|
|
|
|
|
/**
|
|
|
|
|
* when actual packet length > 0xffff(65535),
|
|
|
|
|
* which exceed the max uint16_t packet length,
|
|
|
|
|
* use 0 packet length, the next unit start indicates the end of packet.
|
|
|
|
|
*/
|
|
|
|
|
if (PES_packet_length > 0) {
|
|
|
|
|
int nb_packet = PES_packet_length - (stream->pos() - pos_packet);
|
|
|
|
|
msg->PES_packet_length = srs_max(0, nb_packet);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// xB
|
|
|
|
|
if ((err = msg->dump(stream, &nb_bytes)) != srs_success) {
|
|
|
|
|
return srs_error_wrap(err, "dump pes");
|
|
|
|
|
// stuffing_byte
|
|
|
|
|
nb_stuffings = PES_header_data_length - (stream->pos() - pos_header);
|
|
|
|
|
if (nb_stuffings > 0) {
|
|
|
|
|
if (!stream->require(nb_stuffings)) {
|
|
|
|
|
return srs_error_new(ERROR_STREAM_CASTER_TS_PSE, "ts: demux PSE stuffings");
|
|
|
|
|
}
|
|
|
|
|
stream->skip(nb_stuffings);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Ignore coverage bellow, for we don't use them in HLS.
|
|
|
|
|
// LCOV_EXCL_START
|
|
|
|
|
} else if (sid == SrsTsPESStreamIdProgramStreamMap
|
|
|
|
|
|| sid == SrsTsPESStreamIdPrivateStream2
|
|
|
|
|
|| sid == SrsTsPESStreamIdEcmStream
|
|
|
|
|
|| sid == SrsTsPESStreamIdEmmStream
|
|
|
|
|
|| sid == SrsTsPESStreamIdProgramStreamDirectory
|
|
|
|
|
|| sid == SrsTsPESStreamIdDsmccStream
|
|
|
|
|
|| sid == SrsTsPESStreamIdH2221TypeE
|
|
|
|
|
) {
|
|
|
|
|
// for (i = 0; i < PES_packet_length; i++) {
|
|
|
|
|
// PES_packet_data_byte
|
|
|
|
|
// }
|
|
|
|
|
// LCOV_EXCL_STOP
|
|
|
|
|
|
|
|
|
|
// xB
|
|
|
|
|
if ((err = msg->dump(stream, &nb_bytes)) != srs_success) {
|
|
|
|
|
return srs_error_wrap(err, "dump packet");
|
|
|
|
|
// PES_packet_data_byte, page58.
|
|
|
|
|
// the packet size contains the header size.
|
|
|
|
|
// The number of PES_packet_data_bytes, N, is specified by the
|
|
|
|
|
// PES_packet_length field. N shall be equal to the value
|
|
|
|
|
// indicated in the PES_packet_length minus the number of bytes
|
|
|
|
|
// between the last byte of the PES_packet_length field and the
|
|
|
|
|
// first PES_packet_data_byte.
|
|
|
|
|
//
|
|
|
|
|
// If the actual size > uin16_t, which exceed the PES_packet_length, then PES_packet_length is 0, and we
|
|
|
|
|
// should dump all left bytes in stream to message util next unit start packet.
|
|
|
|
|
// Otherwise, the PES_packet_length should greater than 0, which is a specified length, then we also dump
|
|
|
|
|
// the left bytes in stream, in such case, the nb_payload_ is the actual size of payload.
|
|
|
|
|
if (PES_packet_length > 0) {
|
|
|
|
|
int nb_packet = PES_packet_length - (stream->pos() - pos_packet);
|
|
|
|
|
if (nb_packet < 0) {
|
|
|
|
|
return srs_error_new(ERROR_STREAM_CASTER_TS_PSE, "ts: Invalid PES_packet_length=%d, pos_packet=%d, pos=%d", PES_packet_length, pos_packet, stream->pos());
|
|
|
|
|
}
|
|
|
|
|
} else if (sid == SrsTsPESStreamIdPaddingStream) {
|
|
|
|
|
// for (i = 0; i < PES_packet_length; i++) {
|
|
|
|
|
// padding_byte
|
|
|
|
|
// }
|
|
|
|
|
nb_paddings = stream->size() - stream->pos();
|
|
|
|
|
stream->skip(nb_paddings);
|
|
|
|
|
srs_info("ts: drop %dB padding bytes", nb_paddings);
|
|
|
|
|
|
|
|
|
|
// LCOV_EXCL_STOP
|
|
|
|
|
} else {
|
|
|
|
|
int nb_drop = stream->size() - stream->pos();
|
|
|
|
|
stream->skip(nb_drop);
|
|
|
|
|
srs_warn("ts: drop the pes packet %dB for stream_id=%#x", nb_drop, stream_id);
|
|
|
|
|
nb_payload_ = nb_packet;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// when fresh and the PES_packet_length is 0,
|
|
|
|
|
// the payload_unit_start_indicator always be 1,
|
|
|
|
|
// the message should never EOF for the first packet.
|
|
|
|
|
if (is_fresh_msg && msg->PES_packet_length == 0) {
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
// Now, it has payload. The size is specified by PES_packet_length, which might be:
|
|
|
|
|
// 0, Dump all bytes in stream util next unit start packet.
|
|
|
|
|
// nb_payload_, Dump specified bytes in stream.
|
|
|
|
|
has_payload_ = true;
|
|
|
|
|
|
|
|
|
|
// check msg, reap when completed.
|
|
|
|
|
if (msg->completed(packet->payload_unit_start_indicator)) {
|
|
|
|
|
*ppmsg = msg;
|
|
|
|
|
channel->msg = NULL;
|
|
|
|
|
srs_info("ts: reap msg for completed.");
|
|
|
|
|
// Ignore coverage bellow, for we don't use them in HLS.
|
|
|
|
|
// LCOV_EXCL_START
|
|
|
|
|
} else if (sid == SrsTsPESStreamIdProgramStreamMap
|
|
|
|
|
|| sid == SrsTsPESStreamIdPrivateStream2
|
|
|
|
|
|| sid == SrsTsPESStreamIdEcmStream
|
|
|
|
|
|| sid == SrsTsPESStreamIdEmmStream
|
|
|
|
|
|| sid == SrsTsPESStreamIdProgramStreamDirectory
|
|
|
|
|
|| sid == SrsTsPESStreamIdDsmccStream
|
|
|
|
|
|| sid == SrsTsPESStreamIdH2221TypeE
|
|
|
|
|
) {
|
|
|
|
|
// for (i = 0; i < PES_packet_length; i++) {
|
|
|
|
|
// PES_packet_data_byte
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// The pos_packet equals to stream pos, so the PES_packet_length is actually the payload length.
|
|
|
|
|
nb_payload_ = PES_packet_length;
|
|
|
|
|
has_payload_ = true;
|
|
|
|
|
} else if (sid == SrsTsPESStreamIdPaddingStream) {
|
|
|
|
|
// for (i = 0; i < PES_packet_length; i++) {
|
|
|
|
|
// padding_byte
|
|
|
|
|
// }
|
|
|
|
|
nb_paddings = stream->size() - stream->pos();
|
|
|
|
|
stream->skip(nb_paddings);
|
|
|
|
|
srs_info("ts: drop %dB padding bytes", nb_paddings);
|
|
|
|
|
|
|
|
|
|
// LCOV_EXCL_STOP
|
|
|
|
|
} else {
|
|
|
|
|
int nb_drop = stream->size() - stream->pos();
|
|
|
|
|
stream->skip(nb_drop);
|
|
|
|
|
srs_warn("ts: drop the pes packet %dB for stream_id=%#x", nb_drop, stream_id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int SrsTsPayloadPES::size()
|
|
|
|
|
int SrsMpegPES::size()
|
|
|
|
|
{
|
|
|
|
|
int sz = 0;
|
|
|
|
|
|
|
|
|
@ -1744,7 +1638,7 @@ int SrsTsPayloadPES::size()
|
|
|
|
|
|| sid == SrsTsPESStreamIdProgramStreamDirectory
|
|
|
|
|
|| sid == SrsTsPESStreamIdDsmccStream
|
|
|
|
|
|| sid == SrsTsPESStreamIdH2221TypeE
|
|
|
|
|
) {
|
|
|
|
|
) {
|
|
|
|
|
// packet bytes
|
|
|
|
|
} else {
|
|
|
|
|
// nb_drop
|
|
|
|
@ -1753,7 +1647,7 @@ int SrsTsPayloadPES::size()
|
|
|
|
|
return sz;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
srs_error_t SrsTsPayloadPES::encode(SrsBuffer* stream)
|
|
|
|
|
srs_error_t SrsMpegPES::encode(SrsBuffer* stream)
|
|
|
|
|
{
|
|
|
|
|
srs_error_t err = srs_success;
|
|
|
|
|
|
|
|
|
@ -1909,7 +1803,7 @@ srs_error_t SrsTsPayloadPES::encode(SrsBuffer* stream)
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
srs_error_t SrsTsPayloadPES::decode_33bits_dts_pts(SrsBuffer* stream, int64_t* pv)
|
|
|
|
|
srs_error_t SrsMpegPES::decode_33bits_dts_pts(SrsBuffer* stream, int64_t* pv)
|
|
|
|
|
{
|
|
|
|
|
srs_error_t err = srs_success;
|
|
|
|
|
|
|
|
|
@ -1960,7 +1854,7 @@ srs_error_t SrsTsPayloadPES::decode_33bits_dts_pts(SrsBuffer* stream, int64_t* p
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
srs_error_t SrsTsPayloadPES::encode_33bits_dts_pts(SrsBuffer* stream, uint8_t fb, int64_t v)
|
|
|
|
|
srs_error_t SrsMpegPES::encode_33bits_dts_pts(SrsBuffer* stream, uint8_t fb, int64_t v)
|
|
|
|
|
{
|
|
|
|
|
srs_error_t err = srs_success;
|
|
|
|
|
|
|
|
|
@ -1987,6 +1881,147 @@ srs_error_t SrsTsPayloadPES::encode_33bits_dts_pts(SrsBuffer* stream, uint8_t fb
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SrsTsPayloadPES::SrsTsPayloadPES(SrsTsPacket* p) : SrsTsPayload(p)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SrsTsPayloadPES::~SrsTsPayloadPES()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
srs_error_t SrsTsPayloadPES::decode(SrsBuffer* stream, SrsTsMessage** ppmsg)
|
|
|
|
|
{
|
|
|
|
|
srs_error_t err = srs_success;
|
|
|
|
|
|
|
|
|
|
// find the channel from chunk.
|
|
|
|
|
SrsTsChannel* channel = packet->context->get(packet->pid);
|
|
|
|
|
if (!channel) {
|
|
|
|
|
return srs_error_new(ERROR_STREAM_CASTER_TS_PSE, "ts: demux PES no channel for pid=%#x", packet->pid);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// init msg.
|
|
|
|
|
SrsTsMessage* msg = channel->msg;
|
|
|
|
|
if (!msg) {
|
|
|
|
|
msg = new SrsTsMessage(channel, packet);
|
|
|
|
|
channel->msg = msg;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// we must cache the fresh state of msg,
|
|
|
|
|
// for the PES_packet_length is 0, the first payload_unit_start_indicator always 1,
|
|
|
|
|
// so should check for the fresh and not completed it.
|
|
|
|
|
bool is_fresh_msg = msg->fresh();
|
|
|
|
|
|
|
|
|
|
// check when fresh, the payload_unit_start_indicator
|
|
|
|
|
// should be 1 for the fresh msg.
|
|
|
|
|
if (is_fresh_msg && !packet->payload_unit_start_indicator) {
|
|
|
|
|
return srs_error_new(ERROR_STREAM_CASTER_TS_PSE, "ts: PES fresh packet length=%d, us=%d, cc=%d",
|
|
|
|
|
msg->PES_packet_length, packet->payload_unit_start_indicator, packet->continuity_counter);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// check when not fresh and PES_packet_length>0,
|
|
|
|
|
// the payload_unit_start_indicator should never be 1 when not completed.
|
|
|
|
|
if (!is_fresh_msg && msg->PES_packet_length > 0 && !msg->completed(packet->payload_unit_start_indicator) && packet->payload_unit_start_indicator) {
|
|
|
|
|
srs_warn("ts: ignore PES packet length=%d, payload=%d, us=%d, cc=%d",
|
|
|
|
|
msg->PES_packet_length, msg->payload->length(), packet->payload_unit_start_indicator, packet->continuity_counter);
|
|
|
|
|
|
|
|
|
|
// reparse current msg.
|
|
|
|
|
stream->skip(stream->pos() * -1);
|
|
|
|
|
srs_freep(msg);
|
|
|
|
|
channel->msg = NULL;
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// check the continuity counter
|
|
|
|
|
if (!is_fresh_msg) {
|
|
|
|
|
// late-incoming or duplicated continuity, drop message.
|
|
|
|
|
// @remark check overflow, the counter plus 1 should greater when invalid.
|
|
|
|
|
if (msg->continuity_counter >= packet->continuity_counter && ((msg->continuity_counter + 1) & 0x0f) > packet->continuity_counter) {
|
|
|
|
|
srs_warn("ts: drop PES %dB for duplicated cc=%#x", msg->continuity_counter);
|
|
|
|
|
stream->skip(stream->size() - stream->pos());
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// when got partially message, the continous count must be continuous, or drop it.
|
|
|
|
|
if (((msg->continuity_counter + 1) & 0x0f) != packet->continuity_counter) {
|
|
|
|
|
srs_warn("ts: ignore continuity must be continous, msg=%#x, packet=%#x", msg->continuity_counter, packet->continuity_counter);
|
|
|
|
|
|
|
|
|
|
// reparse current msg.
|
|
|
|
|
stream->skip(stream->pos() * -1);
|
|
|
|
|
srs_freep(msg);
|
|
|
|
|
channel->msg = NULL;
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
msg->continuity_counter = packet->continuity_counter;
|
|
|
|
|
|
|
|
|
|
// for the PES_packet_length(0), reap when completed.
|
|
|
|
|
if (!is_fresh_msg && msg->completed(packet->payload_unit_start_indicator)) {
|
|
|
|
|
// reap previous PES packet.
|
|
|
|
|
*ppmsg = msg;
|
|
|
|
|
channel->msg = NULL;
|
|
|
|
|
|
|
|
|
|
// reparse current msg.
|
|
|
|
|
stream->skip(stream->pos() * -1);
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// contious packet, append bytes for unit start is 0
|
|
|
|
|
if (!packet->payload_unit_start_indicator) {
|
|
|
|
|
if ((err = msg->dump(stream, &pes.nb_bytes)) != srs_success) {
|
|
|
|
|
return srs_error_wrap(err, "ts: pes dump");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// when unit start, parse the fresh msg.
|
|
|
|
|
if (packet->payload_unit_start_indicator) {
|
|
|
|
|
if ((err = pes.decode(stream)) != srs_success) {
|
|
|
|
|
return srs_error_wrap(err, "header");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update message when decode the first PES packet.
|
|
|
|
|
msg->sid = (SrsTsPESStreamId)pes.stream_id;
|
|
|
|
|
if (pes.PTS_DTS_flags == 0x02 || pes.PTS_DTS_flags == 0x03) {
|
|
|
|
|
msg->dts = pes.dts;
|
|
|
|
|
msg->pts = pes.pts;
|
|
|
|
|
}
|
|
|
|
|
if (pes.has_payload_) {
|
|
|
|
|
// The size of message, might be 0 or a positive value.
|
|
|
|
|
msg->PES_packet_length = pes.nb_payload_;
|
|
|
|
|
|
|
|
|
|
// xB
|
|
|
|
|
if ((err = msg->dump(stream, &pes.nb_bytes)) != srs_success) {
|
|
|
|
|
return srs_error_wrap(err, "dump pes");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// when fresh and the PES_packet_length is 0,
|
|
|
|
|
// the payload_unit_start_indicator always be 1,
|
|
|
|
|
// the message should never EOF for the first packet.
|
|
|
|
|
if (is_fresh_msg && msg->PES_packet_length == 0) {
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// check msg, reap when completed.
|
|
|
|
|
if (msg->completed(packet->payload_unit_start_indicator)) {
|
|
|
|
|
*ppmsg = msg;
|
|
|
|
|
channel->msg = NULL;
|
|
|
|
|
srs_info("ts: reap msg for completed.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int SrsTsPayloadPES::size()
|
|
|
|
|
{
|
|
|
|
|
return pes.size();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
srs_error_t SrsTsPayloadPES::encode(SrsBuffer* stream)
|
|
|
|
|
{
|
|
|
|
|
return pes.encode(stream);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SrsTsPayloadPSI::SrsTsPayloadPSI(SrsTsPacket* p) : SrsTsPayload(p)
|
|
|
|
|
{
|
|
|
|
|
pointer_field = 0;
|
|
|
|
|