Permalink
Browse files

Based on 'BUG-3947-TSWriter-Deadlock-v2' branch, with async logging m…

…ods from 'EXP-TsWriter_async_logging', packetSync mods from 'MP1-EXP_TsReader_PacketSync', 'otherMux (0x46)' channel scanning mods from 'MP1-EXP_tvserver_0x46_scan_fixes' and minor Unicode logging fix manually merged into the code. Version resources also added.
  • Loading branch information...
1 parent 2c39871 commit b167e32e76de4e63ae9348c33d49145b625d799f @owlsroost owlsroost committed Sep 29, 2013
@@ -25,7 +25,8 @@
CPacketSync::CPacketSync(void)
{
- m_tempBufferPos=-1;
+ m_tempBufferPos = -1;
+ m_bInSync = false;
}
CPacketSync::~CPacketSync(void)
@@ -34,51 +35,132 @@ CPacketSync::~CPacketSync(void)
void CPacketSync::Reset(void)
{
- m_tempBufferPos=-1;
+ m_tempBufferPos = -1;
+ m_bInSync = false;
}
// Ambass : Now, need to have 2 consecutive TS_PACKET_SYNC to try avoiding bad synchronisation.
// In case of data flow change ( Seek, tv Zap .... ) Reset() should be called first to flush buffer.
void CPacketSync::OnRawData(byte* pData, int nDataLen)
{
int syncOffset=0;
- if (m_tempBufferPos > 0 )
+ int tempBuffOffset=0;
+ bool goodPacket = false;
+
+ if (m_tempBufferPos > 0 ) //We have some residual data from the last call
{
- if (pData[TS_PACKET_LEN - m_tempBufferPos]==TS_PACKET_SYNC)
+ syncOffset = TS_PACKET_LEN - m_tempBufferPos;
+
+ if (nDataLen <= syncOffset)
+ {
+ //not enough total data to scan through a packet length,
+ //so add pData to the tempBuffer and return
+ memcpy(&m_tempBuffer[m_tempBufferPos], pData, nDataLen);
+ m_tempBufferPos += nDataLen;
+ return ;
+ }
+
+ while ((nDataLen > syncOffset) && (m_tempBufferPos > tempBuffOffset))
+ {
+ if ((m_tempBuffer[tempBuffOffset]==TS_PACKET_SYNC) &&
+ (pData[syncOffset]==TS_PACKET_SYNC)) //found a good packet
+ {
+ if (syncOffset)
+ {
+ memcpy(&m_tempBuffer[m_tempBufferPos], pData, syncOffset);
+ }
+ OnTsPacket(&m_tempBuffer[tempBuffOffset]);
+ goodPacket = true;
+ m_bInSync = true;
+ break;
+ }
+ else if (m_bInSync &&
+ ((m_tempBuffer[tempBuffOffset]==TS_PACKET_SYNC) ||
+ (pData[syncOffset]==TS_PACKET_SYNC))) //found a good packet
+ {
+ if (syncOffset)
+ {
+ memcpy(&m_tempBuffer[m_tempBufferPos], pData, syncOffset);
+ }
+ m_tempBuffer[tempBuffOffset] = TS_PACKET_SYNC;
+ pData[syncOffset] = TS_PACKET_SYNC;
+ OnTsPacket(&m_tempBuffer[tempBuffOffset]);
+ goodPacket = true;
+ m_bInSync = false;
+ break;
+ }
+ else
+ {
+ syncOffset++;
+ tempBuffOffset++;
+ m_bInSync = false;
+ }
+ }
+
+ if (!goodPacket)
{
- syncOffset = TS_PACKET_LEN - m_tempBufferPos;
- if (syncOffset) memcpy(&m_tempBuffer[m_tempBufferPos], pData, syncOffset);
- OnTsPacket(m_tempBuffer);
+ if (tempBuffOffset >= m_tempBufferPos)
+ {
+ //We have scanned all of the data in m_tempBuffer,
+ //so continue search from the start of pData buffer.
+ syncOffset = 0;
+ }
+ else
+ {
+ //move data down to discard data we have already scanned
+ m_tempBufferPos -= tempBuffOffset;
+ memmove(m_tempBuffer, &m_tempBuffer[tempBuffOffset], m_tempBufferPos);
+ //add pData to the tempBuffer and return
+ memcpy(&m_tempBuffer[m_tempBufferPos], pData, nDataLen);
+ m_tempBufferPos += nDataLen;
+ return;
+ }
}
- m_tempBufferPos = 0;
}
- while ((syncOffset + TS_PACKET_LEN) < nDataLen)
+ m_tempBufferPos = 0; //We have consumed the residual data
+
+ while (nDataLen > (syncOffset + TS_PACKET_LEN)) //minimum of TS_PACKET_LEN+1 bytes available
{
+ if (!goodPacket && (syncOffset > (TS_PACKET_LEN * 8)) )
+ {
+ //No sync - abandon the buffer
+ Reset();
+ return;
+ }
+
if ((pData[syncOffset] == TS_PACKET_SYNC) &&
(pData[syncOffset + TS_PACKET_LEN]==TS_PACKET_SYNC))
{
OnTsPacket( &pData[syncOffset] );
syncOffset += TS_PACKET_LEN;
+ goodPacket = true;
+ m_bInSync = true;
}
- else
- syncOffset++;
- }
-
- // Here we have less than 188+1 bytes
- while (syncOffset < nDataLen)
- {
- if (pData[syncOffset] == TS_PACKET_SYNC)
+ else if (m_bInSync &&
+ ((pData[syncOffset] == TS_PACKET_SYNC) ||
+ (pData[syncOffset + TS_PACKET_LEN]==TS_PACKET_SYNC)))
{
- m_tempBufferPos= nDataLen - syncOffset;
- memcpy( m_tempBuffer, &pData[syncOffset], m_tempBufferPos );
- return ;
+ pData[syncOffset] = TS_PACKET_SYNC;
+ pData[syncOffset + TS_PACKET_LEN] = TS_PACKET_SYNC;
+ OnTsPacket( &pData[syncOffset] );
+ syncOffset += TS_PACKET_LEN;
+ goodPacket = true;
+ m_bInSync = false;
}
else
+ {
syncOffset++;
+ m_bInSync = false;
+ }
+ }
+
+ // We have less than TS_PACKET_LEN+1 bytes available - store residual data for next time
+ m_tempBufferPos= nDataLen - syncOffset;
+ if (m_tempBufferPos)
+ {
+ memcpy( m_tempBuffer, &pData[syncOffset], m_tempBufferPos );
}
-
- m_tempBufferPos=0 ;
}
// Ambass : Now, need to have 2 consecutive TS_PACKET_SYNC to try avoiding bad synchronisation.
@@ -87,51 +169,123 @@ void CPacketSync::OnRawData(byte* pData, int nDataLen)
void CPacketSync::OnRawData2(byte* pData, int nDataLen)
{
int syncOffset=0;
+ int tempBuffOffset=0;
bool goodPacket = false;
- if (m_tempBufferPos > 0 )
+
+ if (m_tempBufferPos > 0 ) //We have some residual data from the last call
{
- if (pData[TS_PACKET_LEN - m_tempBufferPos]==TS_PACKET_SYNC)
+ syncOffset = TS_PACKET_LEN - m_tempBufferPos;
+
+ if (nDataLen <= syncOffset)
+ {
+ //not enough total data to scan through a packet length,
+ //so add pData to the tempBuffer and return
+ memcpy(&m_tempBuffer[m_tempBufferPos], pData, nDataLen);
+ m_tempBufferPos += nDataLen;
+ return ;
+ }
+
+ while ((nDataLen > syncOffset) && (m_tempBufferPos > tempBuffOffset))
+ {
+ if ((m_tempBuffer[tempBuffOffset]==TS_PACKET_SYNC) &&
+ (pData[syncOffset]==TS_PACKET_SYNC)) //found a good packet
+ {
+ if (syncOffset)
+ {
+ memcpy(&m_tempBuffer[m_tempBufferPos], pData, syncOffset);
+ }
+ OnTsPacket(&m_tempBuffer[tempBuffOffset]);
+ goodPacket = true;
+ m_bInSync = true;
+ break;
+ }
+ else if (m_bInSync &&
+ ((m_tempBuffer[tempBuffOffset]==TS_PACKET_SYNC) ||
+ (pData[syncOffset]==TS_PACKET_SYNC))) //found a good packet
+ {
+ if (syncOffset)
+ {
+ memcpy(&m_tempBuffer[m_tempBufferPos], pData, syncOffset);
+ }
+ m_tempBuffer[tempBuffOffset] = TS_PACKET_SYNC;
+ pData[syncOffset] = TS_PACKET_SYNC;
+ OnTsPacket(&m_tempBuffer[tempBuffOffset]);
+ goodPacket = true;
+ m_bInSync = false;
+ break;
+ }
+ else
+ {
+ syncOffset++;
+ tempBuffOffset++;
+ m_bInSync = false;
+ }
+ }
+
+ if (!goodPacket)
{
- syncOffset = TS_PACKET_LEN - m_tempBufferPos;
- if (syncOffset) memcpy(&m_tempBuffer[m_tempBufferPos], pData, syncOffset);
- OnTsPacket(m_tempBuffer);
+ if (tempBuffOffset >= m_tempBufferPos)
+ {
+ //We have scanned all of the data in m_tempBuffer,
+ //so continue search from the start of pData buffer.
+ syncOffset = 0;
+ }
+ else
+ {
+ //move data down to discard data we have already scanned
+ m_tempBufferPos -= tempBuffOffset;
+ memmove(m_tempBuffer, &m_tempBuffer[tempBuffOffset], m_tempBufferPos);
+ //add pData to the tempBuffer and return
+ memcpy(&m_tempBuffer[m_tempBufferPos], pData, nDataLen);
+ m_tempBufferPos += nDataLen;
+ return;
+ }
}
- m_tempBufferPos = 0;
}
- while ((syncOffset + TS_PACKET_LEN) < nDataLen)
+ m_tempBufferPos = 0; //We have consumed the residual data
+
+ while (nDataLen > (syncOffset + TS_PACKET_LEN)) //minimum of TS_PACKET_LEN+1 bytes available
{
if (!goodPacket && (syncOffset > (TS_PACKET_LEN * 8)) )
{
//No sync - abandon the buffer
- m_tempBufferPos = -1;
+ Reset();
return;
}
+
if ((pData[syncOffset] == TS_PACKET_SYNC) &&
(pData[syncOffset + TS_PACKET_LEN]==TS_PACKET_SYNC))
{
OnTsPacket( &pData[syncOffset] );
syncOffset += TS_PACKET_LEN;
goodPacket = true;
+ m_bInSync = true;
}
- else
- syncOffset++;
- }
-
- // Here we have less than 188+1 bytes
- while (syncOffset < nDataLen)
- {
- if (pData[syncOffset] == TS_PACKET_SYNC)
+ else if (m_bInSync &&
+ ((pData[syncOffset] == TS_PACKET_SYNC) ||
+ (pData[syncOffset + TS_PACKET_LEN]==TS_PACKET_SYNC)))
{
- m_tempBufferPos= nDataLen - syncOffset;
- memcpy( m_tempBuffer, &pData[syncOffset], m_tempBufferPos );
- return ;
+ pData[syncOffset] = TS_PACKET_SYNC;
+ pData[syncOffset + TS_PACKET_LEN] = TS_PACKET_SYNC;
+ OnTsPacket( &pData[syncOffset] );
+ syncOffset += TS_PACKET_LEN;
+ goodPacket = true;
+ m_bInSync = false;
}
else
+ {
syncOffset++;
+ m_bInSync = false;
+ }
+ }
+
+ // We have less than TS_PACKET_LEN+1 bytes available - store residual data for next time
+ m_tempBufferPos= nDataLen - syncOffset;
+ if (m_tempBufferPos)
+ {
+ memcpy( m_tempBuffer, &pData[syncOffset], m_tempBufferPos );
}
-
- m_tempBufferPos=0 ;
}
void CPacketSync::OnTsPacket(byte* tsPacket)
@@ -213,6 +213,7 @@
<ClInclude Include="source\TeletextGrabber.h" />
<ClInclude Include="source\TsChannel.h" />
<ClInclude Include="source\TsWriter.h" />
+ <ClInclude Include="source\version.h" />
<ClInclude Include="source\VideoAnalyzer.h" />
<ClInclude Include="source\videoaudioobserver.h" />
<ClInclude Include="source\PcrDecoder.h" />
@@ -251,6 +252,9 @@
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
</ProjectReference>
</ItemGroup>
+ <ItemGroup>
+ <ResourceCompile Include="source\TsWriter.rc" />
+ </ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
@@ -141,7 +141,7 @@ void CPatParser::OnSdtReceived(const CChannelInfo& sdtInfo)
CChannelInfo* info=it->second;
// check if we already set the sdt for this channel
- if (info->SdtReceived) return;
+ if (info->SdtReceived || (info->TransportId != sdtInfo.TransportId)) return;
info->NetworkId=sdtInfo.NetworkId;
info->TransportId=sdtInfo.TransportId;
@@ -53,7 +53,7 @@ void CSdtParser::OnNewSection(CSection& sections)
byte* section=(&sections.Data)[0];
int sectionLen=sections.section_length;
- //We only want the SDT for this actual mux (not 'others' with table_id 0x46)
+ //We only want the SDT for this mux (table_id 0x42) and the 'others' with table_id 0x46)
if (sections.table_id!=0x42 && sections.table_id!=0x46) return;
long original_network_id = ((section[8])<<8)+section[9];
Oops, something went wrong.

0 comments on commit b167e32

Please sign in to comment.