diff -X ignore-files -urN vls-org/Makefile vls-syncfifo-tsanalysis/Makefile --- vls-org/Makefile Thu Mar 21 19:56:38 2002 +++ vls-syncfifo-tsanalysis/Makefile Thu Mar 28 15:36:56 2002 @@ -64,6 +64,7 @@ ifeq ($(NEEDSRC_mpegbase),1) SRC+= src/mpeg/ts.cpp \ + src/mpeg/tsanalysis.cpp \ src/mpeg/streamdescr.cpp \ src/mpeg/reader.cpp \ src/mpeg/converter.cpp \ diff -X ignore-files -urN vls-org/src/core/stack.cpp vls-syncfifo-tsanalysis/src/core/stack.cpp --- vls-org/src/core/stack.cpp Sat Oct 6 23:23:36 2001 +++ vls-syncfifo-tsanalysis/src/core/stack.cpp Thu Mar 28 15:35:22 2002 @@ -5,6 +5,7 @@ * $Id: stack.cpp,v 1.1 2001/10/06 21:23:36 bozo Exp $ * * Authors: Benoit Steiner +* Jean-Paul Saman * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -24,15 +25,12 @@ * *******************************************************************************/ - //------------------------------------------------------------------------------ // Preamble //------------------------------------------------------------------------------ // There is no preamble since this file is to be included in the files which // use the template: look at list.h for further explanations - - //****************************************************************************** // C_Fifo class //****************************************************************************** @@ -89,12 +87,24 @@ ASSERT(pData); int iRc = NO_ERR; + if ( m_iSize != ( (m_iCapacity + (m_iWhereToPush - m_iWhereToPop)) % m_iCapacity ) ) + { + printf( "[ PUSH ] Size != (m_iWhereToPush - m_iWhereToPop) mod m_iCapacity) = %d\n", ((m_iCapacity + (m_iWhereToPush - m_iWhereToPop))%m_iCapacity) ); + printf( "m_iSize (%d)==m_iCapacity(%d) [m_iWhereToPush(%d),m_iWhereToPop (%d)]\n ",m_iSize, m_iCapacity, m_iWhereToPush, m_iWhereToPop); + } + if ( m_iSize >= (m_iCapacity-1) ) + { + printf( "[ PUSH ] writing in full FIFO\n" ); + printf( "m_iSize (%d)==m_iCapacity(%d) [m_iWhereToPush(%d),m_iWhereToPop (%d)]\n ",m_iSize, m_iCapacity, m_iWhereToPush, m_iWhereToPop); + } + if (m_iSize < m_iCapacity) { // Add the element m_apBuff[m_iWhereToPush] = pData; + // Update the WhereToPush index - if (++m_iWhereToPush >= m_iCapacity) + if ( (++m_iWhereToPush) >= m_iCapacity) { // We must roll to the 1st element of the array m_iWhereToPush = 0; @@ -107,7 +117,6 @@ { // There is no space to store the data: decide what to do //according to the settings - if(m_bOverride) { // Override the older element @@ -157,10 +166,21 @@ template T* C_Fifo::Pop() { T* pData = NULL; - + + if ( m_iSize != ((m_iCapacity + (m_iWhereToPush - m_iWhereToPop) )%m_iCapacity) ) + { + printf( "[ POP ] Size != (m_iWhereToPush - m_iWhereToPop) mod m_iCapacity)\n" ); + printf( "m_iSize (%d)==m_iCapacity(%d) [m_iWhereToPush(%d),m_iWhereToPop (%d)]\n ",m_iSize, m_iCapacity, m_iWhereToPush, m_iWhereToPop); + } + if ( m_iSize == 0 ) + { + printf( "[ POP ] fifo is empty\n" ); + printf( "m_iSize (%d)==m_iCapacity(%d) [m_iWhereToPush(%d),m_iWhereToPop (%d)]\n ",m_iSize, m_iCapacity, m_iWhereToPush, m_iWhereToPop); + } + if (m_iSize > 0) { - // Read the element + // Read the element pData = m_apBuff[m_iWhereToPop]; // Update the WhereToPop pointer @@ -193,7 +213,7 @@ if(iElemPos >= m_iCapacity) iElemPos = iElemPos - m_iCapacity; - + return *m_apBuff[iElemPos]; } @@ -235,3 +255,10 @@ } } +//------------------------------------------------------------------------------ +// isFull +//------------------------------------------------------------------------------ +template bool C_Fifo::isFull() +{ + return (m_iSize == (m_iCapacity-1) ); +} diff -X ignore-files -urN vls-org/src/core/stack.h vls-syncfifo-tsanalysis/src/core/stack.h --- vls-org/src/core/stack.h Sat Oct 6 23:23:36 2001 +++ vls-syncfifo-tsanalysis/src/core/stack.h Thu Mar 28 15:35:22 2002 @@ -37,7 +37,7 @@ template class C_Fifo { public: - C_Fifo(unsigned int iCapacity, byte bAutoClean = NO, bool bOverride = false); + C_Fifo(unsigned int iCapacity, byte bAutoClean = NO, bool bOverride = false); ~C_Fifo(); C_Fifo(const C_Fifo& cSrc); @@ -52,8 +52,9 @@ T& operator [] (unsigned int iIndex) const; void Empty(); - - protected: + bool isFull(); + + protected: // Capacity of the buffer unsigned int m_iCapacity; // Number of TS_Packet stored in the Fifo diff -X ignore-files -urN vls-org/src/core/thread.cpp vls-syncfifo-tsanalysis/src/core/thread.cpp --- vls-org/src/core/thread.cpp Sun Oct 7 14:26:45 2001 +++ vls-syncfifo-tsanalysis/src/core/thread.cpp Thu Mar 28 15:35:22 2002 @@ -5,6 +5,7 @@ * $Id: thread.cpp,v 1.2 2001/10/07 12:26:45 bozo Exp $ * * Authors: Benoit Steiner +* Jean-Paul Saman * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -528,7 +529,6 @@ #endif } - //------------------------------------------------------------------------------ // //------------------------------------------------------------------------------ @@ -671,6 +671,33 @@ #endif } +//------------------------------------------------------------------------------ +// +//------------------------------------------------------------------------------ +int C_Semaphore::TryWait() +{ +#ifdef PTHREAD_COND_T_IN_PTHREAD_H + return sem_trywait(&sSemaphore); + +#elif defined WIN32 + ASSERT(false); + return GEN_ERR; +#endif +} + +//------------------------------------------------------------------------------ +// +//------------------------------------------------------------------------------ +int C_Semaphore::myValue(int *value) +{ +#ifdef PTHREAD_COND_T_IN_PTHREAD_H + return sem_getvalue(&sSemaphore, value); + +#elif defined WIN32 + ASSERT(false); + return GEN_ERR; +#endif +} //****************************************************************************** // Class C_Condition diff -X ignore-files -urN vls-org/src/core/thread.h vls-syncfifo-tsanalysis/src/core/thread.h --- vls-org/src/core/thread.h Sat Oct 6 23:23:36 2001 +++ vls-syncfifo-tsanalysis/src/core/thread.h Thu Mar 28 15:35:22 2002 @@ -28,7 +28,6 @@ #ifndef _THREAD_H_ #define _THREAD_H_ - //------------------------------------------------------------------------------ // Forward declaration //------------------------------------------------------------------------------ @@ -129,7 +128,6 @@ }; - //------------------------------------------------------------------------------ // //------------------------------------------------------------------------------ @@ -174,6 +172,7 @@ int TryWait(); // Return the count of the semaphore unsigned int GetValue(); + int myValue(int *value); private: #ifdef PTHREAD_COND_T_IN_PTHREAD_H diff -X ignore-files -urN vls-org/src/modules/dvdreader/dvdreader.cpp vls-syncfifo-tsanalysis/src/modules/dvdreader/dvdreader.cpp --- vls-org/src/modules/dvdreader/dvdreader.cpp Thu Nov 29 17:11:42 2001 +++ vls-syncfifo-tsanalysis/src/modules/dvdreader/dvdreader.cpp Thu Mar 28 15:35:22 2002 @@ -38,8 +38,10 @@ #include #include +#include #include "../../mpeg/mpeg.h" #include "../../mpeg/ts.h" +#include "../../mpeg/tsanalysis.h" #include "../../mpeg/streamdescr.h" #include "../../mpeg/ps2ts.h" diff -X ignore-files -urN vls-org/src/modules/localinput/localinput.cpp vls-syncfifo-tsanalysis/src/modules/localinput/localinput.cpp --- vls-org/src/modules/localinput/localinput.cpp Thu Mar 21 15:09:19 2002 +++ vls-syncfifo-tsanalysis/src/modules/localinput/localinput.cpp Thu Mar 28 15:35:22 2002 @@ -35,8 +35,10 @@ #include "../../core/core.h" +#include #include "../../mpeg/mpeg.h" #include "../../mpeg/ts.h" +#include "../../mpeg/tsanalysis.h" #include "../../server/program.h" #include "../../server/buffer.h" diff -X ignore-files -urN vls-org/src/modules/ps2ts/ps2ts.cpp vls-syncfifo-tsanalysis/src/modules/ps2ts/ps2ts.cpp --- vls-org/src/modules/ps2ts/ps2ts.cpp Mon Mar 18 00:20:22 2002 +++ vls-syncfifo-tsanalysis/src/modules/ps2ts/ps2ts.cpp Thu Mar 28 15:35:22 2002 @@ -5,6 +5,7 @@ * $Id: ps2ts.cpp,v 1.3 2002/03/17 23:20:22 bozo Exp $ * * Authors: Arnaud de Bossoreille de Ribou +* Jean-Paul Saman * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -32,8 +33,10 @@ #include "../../core/core.h" +#include #include "../../mpeg/mpeg.h" #include "../../mpeg/ts.h" +#include "../../mpeg/tsanalysis.h" #include "../../mpeg/streamdescr.h" #include "../../mpeg/ps2ts.h" @@ -134,10 +137,17 @@ if(m_iInitFill) { - for(unsigned int ui = 0; ui < m_iInitFill; ui++) + for(unsigned int ui = 0; ui < m_iInitFill-1; ui++) { C_TsPacket* pPacket = m_cConverter.GetPacket(); ASSERT(pPacket); + + // Check continuity of TS stream + if ( !pPacket->HasSyncByte() ) + printf("[ Ps2Ts ] Packet is no valied TS packet, mising sync byte 0x47"); + if ( !m_cTsAnalysis.ContinuityCounterCheck(pPacket) ) + printf( "[ Ps2Ts ] Continuity Counter inconsistency found\n" ); + m_pHandler->HandlePacket(pPacket); if(m_cConverter.GetStatus()) { @@ -183,53 +193,63 @@ bDiscontinuity = true; } - // Get a packet to fill - C_TsPacket* pPacket = m_cConverter.GetPacket(); - - switch(m_cConverter.GetStatus()) + // Do not read a packet when the fifo is full + if ( !m_pHandler->CanHandlePacket() ) { - case 0: - if(!pPacket) - iRc = MPEG_STREAMERROR; - break; - case -99: - iRc = MPEG_ENDOFSTREAM; - break; - default: - iRc = MPEG_STREAMERROR; - break; - } + // Get a packet to fill + C_TsPacket* pPacket = m_cConverter.GetPacket(); - // Check stream discontinuity - if( !iRc - && (m_pReader->HasDiscontinuity() || bDiscontinuity) - && pPacket->HasPCR()) - { - ASSERT(pPacket); - ASSERT(pPacket->SetDiscontinuityFlag()); - pPacket->SetDiscontinuityFlag(); - m_pReader->ResetDiscontinuity(); - bDiscontinuity = false; - } - - if(!iRc) - { - // Stores the packet in the buffer - m_pHandler->HandlePacket(pPacket); - } - else if(iRc == MPEG_ENDOFSTREAM) - { - C_String strPgrmName = m_pBroadcast->GetProgram()->GetName(); - LogDbg(m_hLog, "End of program \"" + strPgrmName + "\" reached"); - if(pPacket) - m_pTsProvider->ReleasePacket(pPacket); - } - else - { - C_String strPgrmName = m_pBroadcast->GetProgram()->GetName(); - Log(m_hLog, LOG_ERROR, "Read error for program \"" + strPgrmName + "\""); - if(pPacket) - m_pTsProvider->ReleasePacket(pPacket); + switch(m_cConverter.GetStatus()) + { + case 0: + if(!pPacket) + iRc = MPEG_STREAMERROR; + break; + case -99: + iRc = MPEG_ENDOFSTREAM; + break; + default: + iRc = MPEG_STREAMERROR; + break; + } + + // Check stream discontinuity + if( !iRc + && (m_pReader->HasDiscontinuity() || bDiscontinuity) + && pPacket->HasPCR()) + { + ASSERT(pPacket); + ASSERT(pPacket->SetDiscontinuityFlag()); + pPacket->SetDiscontinuityFlag(); + m_pReader->ResetDiscontinuity(); + bDiscontinuity = false; + } + + if(!iRc) + { + // Check continuity of TS stream + if ( !pPacket->HasSyncByte() ) + printf("[ Ps2Ts ] Packet is no valied TS packet, mising sync byte 0x47"); + if ( !m_cTsAnalysis.ContinuityCounterCheck(pPacket) ) + printf( "[ Ps2Ts ] Continuity Counter inconsistency found\n" ); + + // Stores the packet in the buffer + m_pHandler->HandlePacket(pPacket); + } + else if(iRc == MPEG_ENDOFSTREAM) + { + C_String strPgrmName = m_pBroadcast->GetProgram()->GetName(); + LogDbg(m_hLog, "End of program \"" + strPgrmName + "\" reached"); + if(pPacket) + m_pTsProvider->ReleasePacket(pPacket); + } + else + { + C_String strPgrmName = m_pBroadcast->GetProgram()->GetName(); + Log(m_hLog, LOG_ERROR, "Read error for program \"" + strPgrmName + "\""); + if(pPacket) + m_pTsProvider->ReleasePacket(pPacket); + } } } diff -X ignore-files -urN vls-org/src/modules/ps2ts/ps2ts.h vls-syncfifo-tsanalysis/src/modules/ps2ts/ps2ts.h --- vls-org/src/modules/ps2ts/ps2ts.h Wed Nov 28 20:32:23 2001 +++ vls-syncfifo-tsanalysis/src/modules/ps2ts/ps2ts.h Thu Mar 28 15:35:22 2002 @@ -65,6 +65,7 @@ C_Ps2Ts m_cConverter; bool m_bPreParse; + C_TsAnalysis m_cTsAnalysis; }; diff -X ignore-files -urN vls-org/src/modules/ts2ts/ts2ts.cpp vls-syncfifo-tsanalysis/src/modules/ts2ts/ts2ts.cpp --- vls-org/src/modules/ts2ts/ts2ts.cpp Mon Mar 18 00:20:22 2002 +++ vls-syncfifo-tsanalysis/src/modules/ts2ts/ts2ts.cpp Thu Mar 28 15:35:22 2002 @@ -5,6 +5,7 @@ * $Id: ts2ts.cpp,v 1.3 2002/03/17 23:20:22 bozo Exp $ * * Authors: Arnaud de Bossoreille de Ribou +* Jean-Paul Saman * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -32,8 +33,11 @@ #include "../../core/core.h" +#include #include "../../mpeg/mpeg.h" #include "../../mpeg/ts.h" +#include "../../mpeg/tsanalysis.h" + #include "../../server/buffer.h" #include "../../server/program.h" #include "../../server/broadcast.h" @@ -89,7 +93,6 @@ { // Inherited method C_MpegConverter::InitWork(); - int iRc = 0; if(m_iInitFill) @@ -98,15 +101,27 @@ C_TsPacket* pPacket = m_pTsProvider->GetPacket(); ASSERT(pPacket); iRc = SyncFillPacket(pPacket); + if ( !pPacket->HasSyncByte() ) + printf("[ Ts2Ts ] Packet is no valied TS packet, mising sync byte 0x47"); + if ( !m_cTsAnalysis.ContinuityCounterCheck(pPacket) ) + printf( "[ Ts2Ts ] Continuity Counter inconsistency found\n" ); + m_pHandler->HandlePacket(pPacket); if(!iRc) { - for(unsigned int ui = 1; ui < m_iInitFill; ui++) + for(unsigned int ui = 1; ui < (m_iInitFill-1); ui++) { C_TsPacket* pPacket = m_pTsProvider->GetPacket(); ASSERT(pPacket); iRc = SyncFillPacket(pPacket); + + // Check continuity of TS stream + if ( !pPacket->HasSyncByte() ) + printf("[ Ts2Ts ] Packet is no valied TS packet, mising sync byte 0x47"); + if ( !m_cTsAnalysis.ContinuityCounterCheck(pPacket) ) + printf( "[ Ts2Ts ] Continuity Counter inconsistency found\n" ); + m_pHandler->HandlePacket(pPacket); if(iRc) break; @@ -150,47 +165,56 @@ bDiscontinuity = true; } - // Get a packet to fill - C_TsPacket* pPacket = m_pTsProvider->GetPacket(); - ASSERT(pPacket); - // Fill it with the data from the file - iRc = FillPacket(pPacket); - if ((*pPacket)[0] != 0x47) - bDiscontinuity = true; - - // Check stream discontinuity - if(!iRc && (m_pReader->HasDiscontinuity() || bDiscontinuity)) + // Do not read a packet when the fifo is full + if ( !m_pHandler->CanHandlePacket() ) { - while((((*pPacket)[0] != 0x47) || !pPacket->HasPCR()) && !iRc) - iRc = SyncFillPacket(pPacket); - if(!iRc) + // Get a packet to fill + C_TsPacket* pPacket = m_pTsProvider->GetPacket(); + ASSERT(pPacket); + // Fill it with the data from the file + iRc = FillPacket(pPacket); + if ((*pPacket)[0] != 0x47) + bDiscontinuity = true; + + // Check stream discontinuity + if(!iRc && (m_pReader->HasDiscontinuity() || bDiscontinuity)) { - ASSERT(pPacket->SetDiscontinuityFlag()); - pPacket->SetDiscontinuityFlag(); - m_pReader->ResetDiscontinuity(); - bDiscontinuity = false; + while((((*pPacket)[0] != 0x47) || !pPacket->HasPCR()) && !iRc) + iRc = SyncFillPacket(pPacket); + if(!iRc) + { + ASSERT(pPacket->SetDiscontinuityFlag()); + pPacket->SetDiscontinuityFlag(); + m_pReader->ResetDiscontinuity(); + bDiscontinuity = false; + } } - } - if(!iRc) - { - // Stores the packet in the buffer - m_pHandler->HandlePacket(pPacket); - } - else if(iRc == MPEG_ENDOFSTREAM) - { - C_String strPgrmName = m_pBroadcast->GetProgram()->GetName(); - LogDbg(m_hLog, "End of program \"" + strPgrmName + "\" reached"); - m_pTsProvider->ReleasePacket(pPacket); - } - else - { - C_String strPgrmName = m_pBroadcast->GetProgram()->GetName(); - Log(m_hLog, LOG_ERROR, "Read error for program \"" + strPgrmName + "\""); - m_pTsProvider->ReleasePacket(pPacket); + if(!iRc) + { + // Check continuity of TS stream + if ( !pPacket->HasSyncByte() ) + printf("[ Ts2Ts ] Packet is no valied TS packet, mising sync byte 0x47"); + if ( !m_cTsAnalysis.ContinuityCounterCheck(pPacket) ) + printf( "[ Ts2Ts ] Continuity Counter inconsistency found\n" ); + + // Stores the packet in the buffer + m_pHandler->HandlePacket(pPacket); + } + else if(iRc == MPEG_ENDOFSTREAM) + { + C_String strPgrmName = m_pBroadcast->GetProgram()->GetName(); + LogDbg(m_hLog, "End of program \"" + strPgrmName + "\" reached"); + m_pTsProvider->ReleasePacket(pPacket); + } + else + { + C_String strPgrmName = m_pBroadcast->GetProgram()->GetName(); + Log(m_hLog, LOG_ERROR, "Read error for program \"" + strPgrmName + "\""); + m_pTsProvider->ReleasePacket(pPacket); + } } } - if(!m_bStop) { LogDbg(m_hLog, "Stopping converter by callback for program " + strPgrmName); diff -X ignore-files -urN vls-org/src/modules/ts2ts/ts2ts.h vls-syncfifo-tsanalysis/src/modules/ts2ts/ts2ts.h --- vls-org/src/modules/ts2ts/ts2ts.h Wed Nov 28 20:32:23 2001 +++ vls-syncfifo-tsanalysis/src/modules/ts2ts/ts2ts.h Thu Mar 28 15:35:22 2002 @@ -45,6 +45,8 @@ int FillPacket(C_TsPacket* pPacket); int SyncFillPacket(C_TsPacket* pPacket); +private: + C_TsAnalysis m_cTsAnalysis; }; diff -X ignore-files -urN vls-org/src/modules/videoinput/videoinput.cpp vls-syncfifo-tsanalysis/src/modules/videoinput/videoinput.cpp --- vls-org/src/modules/videoinput/videoinput.cpp Thu Mar 21 15:09:19 2002 +++ vls-syncfifo-tsanalysis/src/modules/videoinput/videoinput.cpp Thu Mar 28 15:35:22 2002 @@ -36,8 +36,11 @@ #include "../../core/core.h" +#include #include "../../mpeg/mpeg.h" #include "../../mpeg/ts.h" +#include "../../mpeg/tsanalysis.h" + #include "../../mpeg/streamdescr.h" #include "../../mpeg/ps2ts.h" diff -X ignore-files -urN vls-org/src/mpeg/dvbpsi.cpp vls-syncfifo-tsanalysis/src/mpeg/dvbpsi.cpp --- vls-org/src/mpeg/dvbpsi.cpp Thu Mar 21 20:03:54 2002 +++ vls-syncfifo-tsanalysis/src/mpeg/dvbpsi.cpp Thu Mar 28 15:35:22 2002 @@ -393,6 +393,11 @@ m_pTsProvider->ReleasePacket(pPacket); } +bool C_DvbPsiPatDecoder::CanHandlePacket(void) +{ + return true; +} + //****************************************************************************** // C_DvbPsiPmt class @@ -706,3 +711,7 @@ m_pTsProvider->ReleasePacket(pPacket); } +bool C_DvbPsiPmtDecoder::CanHandlePacket(void) +{ + return true; +} diff -X ignore-files -urN vls-org/src/mpeg/dvbpsi.h vls-syncfifo-tsanalysis/src/mpeg/dvbpsi.h --- vls-org/src/mpeg/dvbpsi.h Thu Mar 21 15:09:19 2002 +++ vls-syncfifo-tsanalysis/src/mpeg/dvbpsi.h Thu Mar 28 15:35:22 2002 @@ -121,7 +121,7 @@ void Detach(); virtual void HandlePacket(C_TsPacket* pPacket); - + virtual bool CanHandlePacket( void ); private: static void Callback(C_DvbPsiPatDecoder *pThis, dvbpsi_pat_t* p_new_pat); @@ -207,7 +207,7 @@ void Detach(); virtual void HandlePacket(C_TsPacket* pPacket); - + virtual bool CanHandlePacket( void ); private: static void Callback(C_DvbPsiPmtDecoder *pThis, dvbpsi_pmt_t* p_new_pmt); diff -X ignore-files -urN vls-org/src/mpeg/ts.cpp vls-syncfifo-tsanalysis/src/mpeg/ts.cpp --- vls-org/src/mpeg/ts.cpp Mon Mar 18 00:20:22 2002 +++ vls-syncfifo-tsanalysis/src/mpeg/ts.cpp Thu Mar 28 15:35:22 2002 @@ -5,6 +5,7 @@ * $Id: ts.cpp,v 1.2 2002/03/17 23:20:22 bozo Exp $ * * Authors: Benoit Steiner +* Jean-Paul Saman * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -149,6 +150,27 @@ return ((bData[3] & 0x20) && bData[4] && (bData[5] & 0x80)); } +//------------------------------------------------------------------------------ +// +//------------------------------------------------------------------------------ +// Checks if the first byte of the packet is a syncbyte +//------------------------------------------------------------------------------ +bool C_TsPacket::HasSyncByte() const +{ + // Return true only if the first byte of the packet is the sync byte + return (bData[0] == (byte)0x47); +} + +//------------------------------------------------------------------------------ +// +//------------------------------------------------------------------------------ +// Get the ContinuityCounter +//------------------------------------------------------------------------------ +unsigned char C_TsPacket::GetContinuityCounter() const +{ + u8 iByte = bData[3]; + return(iByte & 0x0F) ; +} //------------------------------------------------------------------------------ @@ -273,8 +295,6 @@ return TS_PACKET_LEN - iPayloadLen; } - - //****************************************************************************** // C_PsiSection class //****************************************************************************** diff -X ignore-files -urN vls-org/src/mpeg/ts.h vls-syncfifo-tsanalysis/src/mpeg/ts.h --- vls-org/src/mpeg/ts.h Thu Mar 21 15:09:19 2002 +++ vls-syncfifo-tsanalysis/src/mpeg/ts.h Thu Mar 28 15:35:22 2002 @@ -5,6 +5,7 @@ * $Id: ts.h,v 1.3 2002/03/21 14:09:19 bozo Exp $ * * Authors: Benoit Steiner +* Jean-Paul Saman * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -28,7 +29,6 @@ #ifndef _TS_H_ #define _TS_H_ - //------------------------------------------------------------------------------ // //------------------------------------------------------------------------------ @@ -54,6 +54,10 @@ bool IsDiscontinuity() const; s64 GetPCRTime() const; + // Packet consistency checks + bool HasSyncByte() const; + unsigned char GetContinuityCounter() const; + // Reference counter unsigned int Ref() { @@ -84,6 +88,7 @@ { public: virtual void HandlePacket(C_TsPacket* pPacket) = 0; + virtual bool CanHandlePacket( void ) = 0; bool operator == (const I_TsPacketHandler& cHandler) const { @@ -91,7 +96,6 @@ }; }; - //------------------------------------------------------------------------------ // //------------------------------------------------------------------------------ diff -X ignore-files -urN vls-org/src/mpeg/tsanalysis.cpp vls-syncfifo-tsanalysis/src/mpeg/tsanalysis.cpp --- vls-org/src/mpeg/tsanalysis.cpp Thu Jan 1 01:00:00 1970 +++ vls-syncfifo-tsanalysis/src/mpeg/tsanalysis.cpp Thu Mar 28 15:35:22 2002 @@ -0,0 +1,80 @@ +/******************************************************************************* +* tsanalysis.cpp: TsAnalysis packet administration +*------------------------------------------------------------------------------- +* (c)1999-2001 VideoLAN +* $Id: ts.cpp,v 1.2 2002/03/22 23:20:22 jpsaman Exp $ +* +* Authors: Jean-Paul Saman +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +* +*------------------------------------------------------------------------------- +* +*******************************************************************************/ +#include "../core/defs.h" + +//#include "config.h" +#include "../core/core.h" +#include "mpeg.h" +#include +#include "ts.h" +#include "tsanalysis.h" + +//****************************************************************************** +// C_TsAnalysis class +//****************************************************************************** +// Class primarily used for debugging and analysis of a Ts stream. It currently checks +// the ContinuityCounter for all Pid found in the stream. +//****************************************************************************** +C_TsAnalysis::C_TsAnalysis() +{ +} + +C_TsAnalysis::~C_TsAnalysis() +{ +} + +//****************************************************************************** +// ContinuityCounterCheck - Analyze continuity counter for every Pid in the streamer +//****************************************************************************** +bool C_TsAnalysis::ContinuityCounterCheck(C_TsPacket* pPacket) +{ + int iContinuityCounter = -1; + + // Does pPacket->GetPid() already exists as key? + if ( m_aContinuity.find(pPacket->GetPid())!=m_aContinuity.end() ) + { + iContinuityCounter = m_aContinuity[pPacket->GetPid()]; + } + // Check continuity of TS stream + if (iContinuityCounter!=-1) + { + iContinuityCounter = (iContinuityCounter+1)%16; + if ( iContinuityCounter != pPacket->GetContinuityCounter() ) + { + printf(" [ TsAnalysis ] Continuity Counter mismatch for Pid(%d): found %d, expected %d\n", + pPacket->GetPid(),iContinuityCounter, pPacket->GetContinuityCounter() ); + iContinuityCounter = pPacket->GetContinuityCounter(); + return false; + } + } + else + { + iContinuityCounter = pPacket->GetContinuityCounter(); + } + m_aContinuity[pPacket->GetPid()] = iContinuityCounter; + return true; +} + diff -X ignore-files -urN vls-org/src/mpeg/tsanalysis.h vls-syncfifo-tsanalysis/src/mpeg/tsanalysis.h --- vls-org/src/mpeg/tsanalysis.h Thu Jan 1 01:00:00 1970 +++ vls-syncfifo-tsanalysis/src/mpeg/tsanalysis.h Thu Mar 28 15:35:22 2002 @@ -0,0 +1,50 @@ +/******************************************************************************* +* tsanalysis.h: TsAnalisys class definition +*------------------------------------------------------------------------------- +* (c)1999-2001 VideoLAN +* $Id: ts.h,v 1.3 2002/03/22 14:09:19 jpsaman Exp $ +* +* Authors: Jean-Paul Saman +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +* +*------------------------------------------------------------------------------- +* +*******************************************************************************/ + +#ifndef _TSANALYSIS_H_ +#define _TSANALYSIS_H_ + +//------------------------------------------------------------------------------ +// Analysis of TS stream +//------------------------------------------------------------------------------ +class C_TsAnalysis +{ + public: + C_TsAnalysis(); + ~C_TsAnalysis(); + + bool ContinuityCounterCheck( C_TsPacket* pPacket); + + private: + // The map contains (Pid, ContinuityCounter) pair of a TsPacket + map m_aContinuity; +}; + +#else +#error "Multiple inclusions of tsanalysis.h" +#endif + + diff -X ignore-files -urN vls-org/src/mpeg/tsdemux.cpp vls-syncfifo-tsanalysis/src/mpeg/tsdemux.cpp --- vls-org/src/mpeg/tsdemux.cpp Thu Mar 21 15:09:19 2002 +++ vls-syncfifo-tsanalysis/src/mpeg/tsdemux.cpp Thu Mar 28 15:35:22 2002 @@ -187,3 +187,7 @@ } +bool C_TsDemux::CanHandlePacket(void) +{ + return true; +} diff -X ignore-files -urN vls-org/src/mpeg/tsdemux.h vls-syncfifo-tsanalysis/src/mpeg/tsdemux.h --- vls-org/src/mpeg/tsdemux.h Thu Mar 21 15:09:19 2002 +++ vls-syncfifo-tsanalysis/src/mpeg/tsdemux.h Thu Mar 28 15:35:22 2002 @@ -65,6 +65,7 @@ void UnLock(); virtual void HandlePacket(C_TsPacket* pPacket); + virtual bool CanHandlePacket(void); protected: // Selection hooks diff -X ignore-files -urN vls-org/src/mpeg/tsmux.cpp vls-syncfifo-tsanalysis/src/mpeg/tsmux.cpp --- vls-org/src/mpeg/tsmux.cpp Thu Mar 21 15:09:19 2002 +++ vls-syncfifo-tsanalysis/src/mpeg/tsmux.cpp Thu Mar 28 15:35:22 2002 @@ -353,4 +353,7 @@ } } - +bool C_TsMux::CanHandlePacket(void) +{ + return m_pHandler->CanHandlePacket(); +} diff -X ignore-files -urN vls-org/src/mpeg/tsmux.h vls-syncfifo-tsanalysis/src/mpeg/tsmux.h --- vls-org/src/mpeg/tsmux.h Thu Mar 21 15:09:19 2002 +++ vls-syncfifo-tsanalysis/src/mpeg/tsmux.h Thu Mar 28 15:35:22 2002 @@ -85,6 +85,7 @@ void DetachProgram(u16 iPmtPid); virtual void HandlePacket(C_TsPacket* pPacket); + virtual bool CanHandlePacket(void); protected: // PMT PID -> PMT decoder diff -X ignore-files -urN vls-org/src/server/buffer.cpp vls-syncfifo-tsanalysis/src/server/buffer.cpp --- vls-org/src/server/buffer.cpp Thu Mar 21 15:09:19 2002 +++ vls-syncfifo-tsanalysis/src/server/buffer.cpp Thu Mar 28 15:35:22 2002 @@ -5,6 +5,7 @@ * $Id: buffer.cpp,v 1.3 2002/03/21 14:09:19 bozo Exp $ * * Authors: Benoit Steiner +* Jean-Paul Saman * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -24,7 +25,6 @@ * *******************************************************************************/ - //------------------------------------------------------------------------------ // Preamble //------------------------------------------------------------------------------ @@ -36,11 +36,6 @@ #include "../mpeg/ts.h" #include "buffer.h" - - - - - //****************************************************************************** // C_SyncFifo class //****************************************************************************** @@ -76,8 +71,11 @@ // Push the data (wait for a pop if the fifo is full) int iRc = m_cNotFullSignal.Wait(); ASSERT(!iRc); - iRc = m_cFifo.Push(pPacket); - ASSERT(!iRc); + + m_cAtomic.Lock(); + iRc = m_cFifo.Push(pPacket); + m_cAtomic.UnLock(); + ASSERT(!iRc); // Warn the waiting threads if any that they have data to pop iRc = m_cNotEmptySignal.Post(); @@ -113,13 +111,15 @@ int iRc = m_cNotEmptySignal.Wait(); ASSERT(!iRc); + m_cAtomic.Lock(); C_TsPacket* pPacket = m_cFifo.Pop(); + m_cAtomic.UnLock(); ASSERT(pPacket); // Warn the waiting threads if any that they have data to push iRc = m_cNotFullSignal.Post(); ASSERT(!iRc); - + return pPacket; /* diff -X ignore-files -urN vls-org/src/server/buffer.h vls-syncfifo-tsanalysis/src/server/buffer.h --- vls-org/src/server/buffer.h Thu Mar 21 15:09:19 2002 +++ vls-syncfifo-tsanalysis/src/server/buffer.h Thu Mar 28 15:35:22 2002 @@ -5,6 +5,7 @@ * $Id: buffer.h,v 1.3 2002/03/21 14:09:19 bozo Exp $ * * Authors: Benoit Steiner +* Jean-Paul Saman * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -44,17 +45,23 @@ { return m_cFifo.Capacity(); } unsigned int Size() const { return m_cFifo.Size(); } - - void HandlePacket(C_TsPacket* pPacket); + + virtual bool CanHandlePacket( void ) + { return m_cFifo.isFull(); } + virtual void HandlePacket(C_TsPacket* pPacket); + C_TsPacket* Pop(); private: // Thread synchro objects C_Semaphore m_cNotEmptySignal; C_Semaphore m_cNotFullSignal; - + // Fifo C_Fifo m_cFifo; + + // Protect some atomic operations + C_Mutex m_cAtomic; }; diff -X ignore-files -urN vls-org/src/server/manager.cpp vls-syncfifo-tsanalysis/src/server/manager.cpp --- vls-org/src/server/manager.cpp Wed Mar 13 13:32:48 2002 +++ vls-syncfifo-tsanalysis/src/server/manager.cpp Thu Mar 28 15:35:22 2002 @@ -35,8 +35,10 @@ #include "../core/core.h" +#include #include "../mpeg/mpeg.h" #include "../mpeg/ts.h" +#include "../mpeg/tsanalysis.h" #include "program.h" #include "request.h" diff -X ignore-files -urN vls-org/src/server/output.cpp vls-syncfifo-tsanalysis/src/server/output.cpp --- vls-org/src/server/output.cpp Thu Nov 22 02:51:12 2001 +++ vls-syncfifo-tsanalysis/src/server/output.cpp Thu Mar 28 15:35:22 2002 @@ -5,7 +5,8 @@ * $Id: output.cpp,v 1.3 2001/11/22 01:51:12 bozo Exp $ * * Authors: Benoit Steiner -* Arnaud de Bossoreille de Ribou +* Arnaud de Bossoreille de Ribou +* Jean-Paul Saman * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -117,7 +118,7 @@ int iRc = m_cTsBuff.Push(pPacket); ASSERT(!iRc); - if(m_cTsBuff.Size() == m_cTsBuff.Capacity()) + if(m_cTsBuff.Size() == (m_cTsBuff.Capacity()-1)) { // Send the buffered packets to the device and give them back to the // netlist diff -X ignore-files -urN vls-org/src/server/output.h vls-syncfifo-tsanalysis/src/server/output.h --- vls-org/src/server/output.h Thu Nov 22 02:51:12 2001 +++ vls-syncfifo-tsanalysis/src/server/output.h Thu Mar 28 15:35:22 2002 @@ -67,7 +67,10 @@ // Return the capacity of the output buffer unsigned int GetBuffCapacity() { return m_cTsBuff.Capacity(); } - + // Is the output buffer full ? + bool isFull() + { return m_cTsBuff.isFull(); } + // Return the number of byte lost unsigned int GetByteLoss() { return m_iByteLost; } diff -X ignore-files -urN vls-org/src/server/tsstreamer.cpp vls-syncfifo-tsanalysis/src/server/tsstreamer.cpp --- vls-org/src/server/tsstreamer.cpp Thu Mar 21 15:09:19 2002 +++ vls-syncfifo-tsanalysis/src/server/tsstreamer.cpp Thu Mar 28 15:35:22 2002 @@ -6,6 +6,7 @@ * * Authors: Benoit Steiner * Arnaud de Bossoreille de Ribou +* Jean-Paul Saman * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -35,8 +36,11 @@ #include #include "../core/core.h" +#include #include "../mpeg/mpeg.h" #include "../mpeg/ts.h" +#include "../mpeg/tsanalysis.h" + #include "program.h" #include "buffer.h" #include "output.h" @@ -142,55 +146,65 @@ //------------------------------------------------------------------------------ void C_TsStreamer::DoWork() { - int iRc = NO_ERR; + int iRc = NO_ERR; // Get the packet from the buffer and send them to the output while(!m_bStop && iRc >= 0) { - C_TsPacket* pPacket = m_pBuffer->Pop(); - ASSERT(pPacket); - - if(m_bUsePcr) + // Wait till there is room in the output buffer + if ( !m_pOutput->isFull() ) { - // Increases the counters - m_uiByteRead += TS_PACKET_LEN; - m_iHowMany ++; - - // Read the PCR info if any - if(pPacket->HasPCR()) + C_TsPacket* pPacket = m_pBuffer->Pop(); + ASSERT(pPacket); + + if(m_bUsePcr) { - if(m_bFirstPCR) + // Increases the counters + m_uiByteRead += TS_PACKET_LEN; + m_iHowMany ++; + + // Check continuity of TS stream + if ( !pPacket->HasSyncByte() ) + printf("[ TsStreamer ] Packet is no valied TS packet, mising sync byte 0x47"); + if ( !m_cTsAnalysis.ContinuityCounterCheck(pPacket) ) + printf( "[ TsStreamer ] Continuity Counter inconsistency found\n" ); + + // Read the PCR info if any + if(pPacket->HasPCR()) + { + if(m_bFirstPCR) + { + InitClock(pPacket); + m_bFirstPCR = 0; + } + else + AdjustClock(pPacket); + } + + // Wait until the date at which the packet must be sent is reached + // For better perf, doesn't sleep for each TS but only when the output + // buffer is ready to be send + if(m_iHowMany == m_pOutput->GetBuffCapacity()) { - InitClock(pPacket); - m_bFirstPCR = 0; + WaitSendDate(); + m_iHowMany = 0; } else - AdjustClock(pPacket); + m_iHowMany++; } - // Wait until the date at which the packet must be sent is reached - // For better perf, doesn't sleep for each TS but only when the output - // buffer is ready to be send - if(m_iHowMany == m_pOutput->GetBuffCapacity()) + // TS packet is ok, send it + try { - WaitSendDate(); - m_iHowMany = 0; + m_pOutput->Send(pPacket); + } + catch(E_Exception e) + { + iRc = GEN_ERR; + C_String strPgrmName = m_pBroadcast->GetProgram()->GetName(); + Log(m_hLog, LOG_NOTE, C_String("Unable to send stream for pgrm ")+ + strPgrmName + ": "/* + e.Dump()*/); } - else - m_iHowMany++; - } - - // TS packet is ok, send it - try - { - m_pOutput->Send(pPacket); - } - catch(E_Exception e) - { - iRc = GEN_ERR; - C_String strPgrmName = m_pBroadcast->GetProgram()->GetName(); - Log(m_hLog, LOG_NOTE, C_String("Unable to send stream for pgrm ")+ - strPgrmName + ": "/* + e.Dump()*/); } } diff -X ignore-files -urN vls-org/src/server/tsstreamer.h vls-syncfifo-tsanalysis/src/server/tsstreamer.h --- vls-org/src/server/tsstreamer.h Thu Mar 21 15:09:19 2002 +++ vls-syncfifo-tsanalysis/src/server/tsstreamer.h Thu Mar 28 15:35:22 2002 @@ -78,6 +78,8 @@ double m_dSlope; unsigned int m_iHowMany; + // TsAnalysis + C_TsAnalysis m_cTsAnalysis; }; #else