接收ts

以前以为是丢包啦,看了代码才知道是代码自身的问题,修改了,再也不花屏啦!

struct sockaddr_in stPeerAddr;

socklen_t tPeerSize = sizeof( stPeerAddr );


int iRet = recvfrom( GetFd(),
        ( char* )(m_stRecvPacket.m_pchBuf+m_stRecvPacket.m_uiOffset),
m_stRecvPacket.m_uiSize - m_stRecvPacket.m_uiOffset,
0,
        (struct sockaddr*)&stPeerAddr,&tPeerSize );
if ( iRet < 0 )
{
if ( CDTS_Auxiliary::GetSockErr() != EAGAIN && CDTS_Auxiliary::GetSockErr() != EINTR )
{
GAL_ERROR_PRINT << "recvfrom error = %m";
}
return -3;
}


//GAL_DEBUG_PRINT << "recv len = " << iRet << " from " << inet_ntoa( stPeerAddr.sin_addr ) << " : " << DTS_NET_TO_HOST_16( stPeerAddr.sin_port );
//fprintf( stderr,"------------- 0x%x\n",*m_stRecvPacket.m_pchBuf );


m_stRecvPacket.m_uiOffset += iRet;


if ( m_stRecvPacket.m_uiOffset < TRANSPORT_PACKET_SIZE )
{
GAL_DEBUG_PRINT << "We didn't read a complete TS packet";
return -4;
}


//GAL_DEBUG_PRINT << "buffer size = " << m_stRecvPacket.m_uiOffset << " Ts size = " << fFrameSize;


// Make sure the data begins with a sync byte:
dts_uint32 syncBytePosition;
for ( syncBytePosition = 0; syncBytePosition < m_stRecvPacket.m_uiOffset; ++syncBytePosition ) {
if ( m_stRecvPacket.m_pchBuf[syncBytePosition] == TRANSPORT_SYNC_BYTE ) 
{
if ( syncBytePosition + TRANSPORT_PACKET_SIZE >= m_stRecvPacket.m_uiOffset )
{
dts_uint32 uiLen = m_stRecvPacket.m_uiOffset - syncBytePosition;
memcpy( m_stRecvPacket.m_pchBuf,m_stRecvPacket.m_pchBuf+syncBytePosition,uiLen );
m_stRecvPacket.m_uiOffset = uiLen;


return -5;
}
if ( m_stRecvPacket.m_pchBuf[ syncBytePosition + TRANSPORT_PACKET_SIZE ] == TRANSPORT_SYNC_BYTE )
{
break;
}
}
}


if (syncBytePosition == m_stRecvPacket.m_uiOffset) {
GAL_ERROR <<  "No Transport Stream sync byte in data.";
GAL_ERROR_PRINT <<  "No Transport Stream sync byte in data.";


::bzero( m_stRecvPacket.m_pchBuf,m_stRecvPacket.m_uiOffset );
m_stRecvPacket.m_uiOffset = 0;

return -6;
} else if (syncBytePosition > 0) {
// There's a sync byte, but not at the start of the data.  Move the good data
// to the start of the buffer, then read more to fill it up again:


GAL_DEBUG_PRINT<< "There's a sync byte, but not at the start of the data. = " << syncBytePosition;


memmove(m_stRecvPacket.m_pchBuf, &m_stRecvPacket.m_pchBuf[syncBytePosition], m_stRecvPacket.m_uiOffset - syncBytePosition);
//fFrameSize -= syncBytePosition;
m_stRecvPacket.m_uiOffset -= syncBytePosition;
//numTSPackets = m_stRecvPacket.m_uiOffset/TRANSPORT_PACKET_SIZE;
} // else normal case: the data begins with a sync byte

CTSParse* pstTSParse = NULL;
// unsigned char* pszBuf;
// dts_uint8 cc;
for ( unsigned i = 0; i < numTSPackets; ++i ) 
{
// pszBuf = &m_stRecvPacket.m_pchBuf[ i*TRANSPORT_PACKET_SIZE ];
// cc = pszBuf[ 3 ] & 0x0F;
pstTSParse = new CTSParse( &m_stRecvPacket.m_pchBuf[ i*TRANSPORT_PACKET_SIZE ] );
if ( pstTSParse->GetCreateError() == -1 )
{
//fprintf( stderr,"!!!!!!!!!!!!!!!!! 0x%x\n",m_stRecvPacket.m_pchBuf[ i*TRANSPORT_PACKET_SIZE ] );
//GAL_ERROR_PRINT << i << " ts parse fail";


//m_stTsFile.WriteData( (char*)(m_stRecvPacket.m_pchBuf),numTSPackets*TRANSPORT_PACKET_SIZE );


delete pstTSParse;
break;
}

m_stTSProgramMgr.AddMultiTSPacket( pstTSParse );
}


dts_uint32 fFrameSize = numTSPackets * TRANSPORT_PACKET_SIZE;


if ( m_stRecvPacket.m_uiOffset > fFrameSize )
{
dts_uint32 uiLen = m_stRecvPacket.m_uiOffset - fFrameSize;
memcpy( m_stRecvPacket.m_pchBuf,m_stRecvPacket.m_pchBuf+fFrameSize,uiLen );
m_stRecvPacket.m_uiOffset = uiLen;
}
else
{
::bzero( m_stRecvPacket.m_pchBuf,m_stRecvPacket.m_uiOffset );
m_stRecvPacket.m_uiOffset = 0;
}

你可能感兴趣的:(接收ts)