public override void ProcessTsPacket(TsPacketMetadata tsPacketMetadata, byte[] packetBuffer, Int64 packetOffsetInBuffer) { //In search mode, we may need to skip several TS packets according to the request. if (tsPacketMetadata.PacketNumber >= searchRequest.CountOfSkipTsPacket) { //Invoke the stream parser to process a TS packet. streamDemux.ProcessTsPacket(tsPacketMetadata, packetBuffer, packetOffsetInBuffer); } }
public Result ProcessTsPacket(TsPacketMetadata tsPacketMetadata, byte[] packetBuffer, Int64 packetOffsetInBuffer) { Result result = new Result(); //Int64 dataLeftInBit = (Int64)tsPacketMetadata.PacketSize; Int64 dataLeftInBit = (Int64)TsPacketSize.SIZE_188 * 8; //Important!!!!No matter 188 or 204 bytes, the valid data will always be 188 bytes.16 bytes are checksum that is not useful for parsing. Int64 bitOffset = packetOffsetInBuffer * 8; //Key point to set the beginning offset!!!!! //Important!!! Pass a copy of the TS packe to the demux module, so that it can assemble ts packet into section or PES packet. streamDemux.ProcessTsPacket(tsPacketMetadata, packetBuffer, packetOffsetInBuffer); if (result.Fine) { //8-bit sync_byte. result = Utility.ByteArraySkipBits(ref dataLeftInBit, ref bitOffset, 8); } if (result.Fine) { //1-bit transport_error_indicator. result = Utility.ByteArraySkipBits(ref dataLeftInBit, ref bitOffset, 1); } Int64 payloadUnitStartIndicator = 0; if (result.Fine) { //1-bit payload_unit_start_indicator. result = Utility.ByteArrayReadBits(packetBuffer, ref dataLeftInBit, ref bitOffset, 1, ref payloadUnitStartIndicator); //GetContext().WriteLog(Utility.GetValueBinaryString(fieldValue, 1) + Environment.NewLine); } if (result.Fine) { //1-bit transport_priority. result = Utility.ByteArraySkipBits(ref dataLeftInBit, ref bitOffset, 1); } Int64 pid = 0; if (result.Fine) { //13-bit PID. result = Utility.ByteArrayReadBits(packetBuffer, ref dataLeftInBit, ref bitOffset, 13, ref pid); //GetContext().WriteLog(Utility.GetValueBinaryString(fieldValue, 13) + Environment.NewLine); } if (result.Fine) { //2-bit transport_scrambling_control. result = Utility.ByteArraySkipBits(ref dataLeftInBit, ref bitOffset, 2); } Int64 adaptationFieldControl = 0; if (result.Fine) { //2-bit adaptation_field_control. result = Utility.ByteArrayReadBits(packetBuffer, ref dataLeftInBit, ref bitOffset, 2, ref adaptationFieldControl); //GetContext().WriteLog(Utility.GetValueBinaryString(fieldValue, 2) + Environment.NewLine); } if (result.Fine) { //4-bit continuity_counter. result = Utility.ByteArraySkipBits(ref dataLeftInBit, ref bitOffset, 4); } //To parse adaption fields. Int64 adaptationFieldLength = 0; if (result.Fine) { //If adaptationFieldControl is 0b10(Adaptation_field only, no payload) or 0b11(Adaptation_field followed by payload),there is a adaption_field. We need to skip the adaption fields. if ((0x2 == adaptationFieldControl) || (0x3 == adaptationFieldControl)) { //8-bit adaptation_field_length result = Utility.ByteArrayReadBits(packetBuffer, ref dataLeftInBit, ref bitOffset, 8, ref adaptationFieldLength); if (result.Fine) { /*Parse adaptation field to get PCR etc.*/ streamBitrateManager.ParseAdaptationField(tsPacketMetadata, (UInt16)pid, packetBuffer, result, dataLeftInBit, bitOffset, adaptationFieldControl); } //Skip adaption fields according to the adaption_field_length. if (result.Fine) { result = Utility.ByteArraySkipBits(ref dataLeftInBit, ref bitOffset, (adaptationFieldLength * 8)); } } } if (result.Fine) { //Notify a new packet to PidManager so that it can do the statistics. pidManager.AddPacket((UInt16)pid); } return(result); }