Skip to content

Commit 4862770

Browse files
committed
for ossrs#250, parse the PES ts packet payload.
1 parent 755e61e commit 4862770

File tree

2 files changed

+458
-28
lines changed

2 files changed

+458
-28
lines changed

trunk/src/kernel/srs_kernel_ts.cpp

+83-8
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,12 @@ SrsTsContext::SrsTsContext()
408408

409409
SrsTsContext::~SrsTsContext()
410410
{
411+
std::map<int, SrsTsChannel*>::iterator it;
412+
for (it = pids.begin(); it != pids.end(); ++it) {
413+
SrsTsChannel* channel = it->second;
414+
srs_freep(channel);
415+
}
416+
pids.clear();
411417
}
412418

413419
int SrsTsContext::decode(SrsStream* stream)
@@ -429,17 +435,28 @@ int SrsTsContext::decode(SrsStream* stream)
429435
return ret;
430436
}
431437

432-
SrsTsPidApply SrsTsContext::get(int pid)
438+
SrsTsChannel* SrsTsContext::get(int pid)
433439
{
434440
if (pids.find(pid) == pids.end()) {
435-
return SrsTsPidApplyReserved;
441+
return NULL;
436442
}
437443
return pids[pid];
438444
}
439445

440-
void SrsTsContext::set(int pid, SrsTsPidApply apply_pid)
446+
void SrsTsContext::set(int pid, SrsTsPidApply apply_pid, SrsTsStream stream)
441447
{
442-
pids[pid] = apply_pid;
448+
SrsTsChannel* channel = NULL;
449+
450+
if (pids.find(pid) == pids.end()) {
451+
channel = new SrsTsChannel();
452+
pids[pid] = channel;
453+
} else {
454+
channel = pids[pid];
455+
}
456+
457+
channel->pid = pid;
458+
channel->apply = apply_pid;
459+
channel->stream = stream;
443460
}
444461

445462
SrsTsPacket::SrsTsPacket(SrsTsContext* c)
@@ -523,11 +540,15 @@ int SrsTsPacket::decode(SrsStream* stream)
523540
srs_freep(payload);
524541
payload = new SrsTsPayloadPAT(this);
525542
} else {
526-
SrsTsPidApply apply_pid = context->get(pid);
527-
if (apply_pid == SrsTsPidApplyPMT) {
543+
SrsTsChannel* channel = context->get(pid);
544+
if (channel && channel->apply == SrsTsPidApplyPMT) {
528545
// 2.4.4.8 Program Map Table
529546
srs_freep(payload);
530547
payload = new SrsTsPayloadPMT(this);
548+
} else if (channel && (channel->apply == SrsTsPidApplyVideo || channel->apply == SrsTsPidApplyAudio)) {
549+
// 2.4.3.6 PES packet
550+
srs_freep(payload);
551+
payload = new SrsTsPayloadPES(this);
531552
} else {
532553
// left bytes as reserved.
533554
stream->skip(nb_payload);
@@ -793,6 +814,31 @@ SrsTsPayload::~SrsTsPayload()
793814
{
794815
}
795816

817+
SrsTsPayloadPES::SrsTsPayloadPES(SrsTsPacket* p) : SrsTsPayload(p)
818+
{
819+
PES_private_data = NULL;
820+
pack_field = NULL;
821+
PES_extension_field = NULL;
822+
nb_stuffings = 0;
823+
nb_bytes = 0;
824+
bytes = NULL;
825+
nb_paddings = 0;
826+
}
827+
828+
SrsTsPayloadPES::~SrsTsPayloadPES()
829+
{
830+
srs_freep(PES_private_data);
831+
srs_freep(pack_field);
832+
srs_freep(PES_extension_field);
833+
srs_freep(bytes);
834+
}
835+
836+
int SrsTsPayloadPES::decode(SrsStream* stream)
837+
{
838+
int ret = ERROR_SUCCESS;
839+
return ret;
840+
}
841+
796842
SrsTsPayloadPSI::SrsTsPayloadPSI(SrsTsPacket* p) : SrsTsPayload(p)
797843
{
798844
pointer_field = 0;
@@ -879,7 +925,19 @@ int SrsTsPayloadPSI::decode(SrsStream* stream)
879925

880926
// consume left stuffings
881927
if (!stream->empty()) {
882-
stream->skip(stream->size() - stream->pos());
928+
int nb_stuffings = stream->size() - stream->pos();
929+
char* stuffing = stream->data() + stream->pos();
930+
931+
// all stuffing must be 0xff.
932+
// TODO: FIXME: maybe need to remove the following.
933+
for (int i = 0; i < nb_stuffings; i++) {
934+
if ((u_int8_t)stuffing[i] != 0xff) {
935+
srs_warn("ts: stuff is not 0xff, actual=%#x", stuffing[i]);
936+
break;
937+
}
938+
}
939+
940+
stream->skip(nb_stuffings);
883941
}
884942

885943
return ret;
@@ -1049,7 +1107,7 @@ int SrsTsPayloadPMT::psi_decode(SrsStream* stream)
10491107
return ret;
10501108
}
10511109

1052-
info->stream_type = stream->read_1bytes();
1110+
info->stream_type = (SrsTsStream)stream->read_1bytes();
10531111
info->elementary_PID = stream->read_2bytes();
10541112
info->ES_info_length = stream->read_2bytes();
10551113

@@ -1066,6 +1124,23 @@ int SrsTsPayloadPMT::psi_decode(SrsStream* stream)
10661124
info->ES_info = new char[info->ES_info_length];
10671125
stream->read_bytes(info->ES_info, info->ES_info_length);
10681126
}
1127+
1128+
// update the apply pid table
1129+
switch (info->stream_type) {
1130+
case SrsTsStreamVideoH264:
1131+
case SrsTsStreamVideoMpeg4:
1132+
packet->context->set(info->elementary_PID, SrsTsPidApplyVideo, info->stream_type);
1133+
break;
1134+
case SrsTsStreamAudioAAC:
1135+
case SrsTsStreamAudioAC3:
1136+
case SrsTsStreamAudioDTS:
1137+
case SrsTsStreamAudioMp3:
1138+
packet->context->set(info->elementary_PID, SrsTsPidApplyAudio, info->stream_type);
1139+
break;
1140+
default:
1141+
srs_warn("ts: drop pid=%#x, stream=%#x", info->elementary_PID, info->stream_type);
1142+
break;
1143+
}
10691144
}
10701145

10711146
// update the apply pid table.

0 commit comments

Comments
 (0)