--- src/core/stack.cpp.orig Tue Mar 4 23:12:05 2003 +++ src/core/stack.cpp Tue Mar 4 23:28:48 2003 @@ -177,6 +177,28 @@ //------------------------------------------------------------------------------ +// +//------------------------------------------------------------------------------ +// Take a peek at the data on position given. +//------------------------------------------------------------------------------ +template T* C_Fifo::Peek(unsigned int uiIndex) +{ + T* pData = NULL; + + // Check that there is valid data stored a the given index. + ASSERT(uiIndex < Size()); + + unsigned int iElemPos = m_iWhereToPop + uiIndex; + + if(iElemPos >= (m_iCapacity+1)) + iElemPos = iElemPos - m_iCapacity - 1; + + pData = m_apBuff[iElemPos]; + return pData; +} + + +//------------------------------------------------------------------------------ // //------------------------------------------------------------------------------ // --- src/core/stack.h.orig Tue Mar 4 23:12:12 2003 +++ src/core/stack.h Tue Mar 4 23:27:59 2003 @@ -43,6 +43,7 @@ int Push(T* pData); T* Pop(); + T* Peek(unsigned int uiIndex); inline unsigned int Size() const { return (m_iCapacity + 1 + (m_iWhereToPush - m_iWhereToPop)) --- src/server/tsstreamer.cpp.orig Tue Mar 4 22:21:40 2003 +++ src/server/tsstreamer.cpp Tue Mar 4 23:35:41 2003 @@ -280,6 +280,7 @@ // Update the data for the next PCR m_uiByteRead = 0; m_iLastTime = iPCRTime; + m_iNextPCRTime = 0; } @@ -306,8 +307,8 @@ LogDbg(m_hLog, "Adjusting timer discontinuity for pgrm "+strPgrmName); m_iDeltaClock = GetDate() - iPCRTime; } - else - { + else if (!CalculateSlope(iPCRTime)) + { // (Re)evaluate the slope ASSERT(m_uiByteRead > 0); #ifdef _WIN32 @@ -316,7 +317,6 @@ m_dSlope = ((double)iPCRTime - m_iLastTime) / m_uiByteRead; #endif } - // Update the data for the next PCR m_uiByteRead = 0; m_iLastTime = iPCRTime; @@ -351,6 +351,38 @@ } } +//------------------------------------------------------------------------------ +// Calculate the slope between two PCR's from the stream +//------------------------------------------------------------------------------ +bool C_TsStreamer::CalculateSlope(s64 iPCRTime) +{ + ASSERT( iPCRTime>=m_iNextPCRTime ); + + bool result = false; + unsigned int uiIndex; + unsigned int uiBytesToSend = 0; + + for (uiIndex = 1; uiIndex < m_pBuffer->Size(); uiIndex++) + { + // I do not refcount this packet so in theory a problem + // could result from this when the packet is released. At + // the moment I try to read it. For safety it should be refcounted. + C_TsPacket *pPacket = m_pBuffer->Peek(uiIndex); + ASSERT(pPacket); + uiBytesToSend += TS_PACKET_LEN; + if (pPacket->HasPCR()) + { + // Remember the next PCR time, and use it in calculations for sending. + ASSERT(uiBytesToSend>0); + + m_iNextPCRTime = pPacket->GetPCRTime(); + m_dSlope = ((double) m_iNextPCRTime - iPCRTime) / (s64)uiBytesToSend; + result = true; + break; + } + } + return result; +} //------------------------------------------------------------------------------ // Returns the current date in microseconds --- src/server/tsstreamer.h.orig Tue Mar 4 22:21:47 2003 +++ src/server/tsstreamer.h Tue Mar 4 23:02:17 2003 @@ -57,6 +57,8 @@ inline void InitClock(C_TsPacket* pPacket); inline void WaitSendDate(); inline s64 GetDate(); + + bool CalculateSlope(s64 iPCRTime); bool m_bStop; bool m_bFirstPCR; @@ -74,6 +76,7 @@ u64 m_uiByteRead; s64 m_iLastTime; + s64 m_iNextPCRTime; s64 m_iDeltaClock; double m_dSlope;