#include #include #include #include #include #include #include #include #include #include #include "rtppacket.h" #include "UdpStreamTrans.h" #include "sps_pps.h" CUdpStreamTrans::CUdpStreamTrans() { m_StreamSendIp[0] = '\0'; m_StreamSendPort = 0; m_StreamRecvIp[0] = '\0'; m_StreamRecvPort = 0; UserData = 0; MediaCBFunc = NULL; m_StreamParseHandle1 = 0; m_StreamParseHandle2 = 0; m_StreamParseHandle3 = 0; m_RecvStreamType = 0; m_eVideoStreamType = E_VIDEO_STREAM_NONE; } CUdpStreamTrans::~CUdpStreamTrans() { CRtpApp::ClearDestinations(); CRtpApp::RtpDestroy(); if (m_StreamParseHandle1 != 0) { STREAMPARSE_Close(m_StreamParseHandle1); m_StreamParseHandle1 = 0; } if (m_StreamParseHandle2 != 0) { STREAMPARSE_Close(m_StreamParseHandle2); m_StreamParseHandle2 = 0; } if (m_StreamParseHandle3 != 0) { STREAMPARSE_Close(m_StreamParseHandle3); m_StreamParseHandle3 = 0; } } bool CUdpStreamTrans::SetStreamTransParam(StreamTransType_E streamtype, const char *sendip, int sendport, const char *recvip, int recvport) { strncpy2(m_StreamSendIp, sendip, IPSTR_MAX_LEN); m_StreamSendPort = sendport; strncpy2(m_StreamRecvIp, recvip, IPSTR_MAX_LEN); m_StreamRecvPort= recvport; return true; } int CUdpStreamTrans::InitRtp(void) { int iRet = -1; //´ò¿ª±¾µØÃ½ÌåÍøÂçÁ¬½Ó£¬×¼±¸½ÓÊÕÊý¾Ý¡£ CRtpApp::TransParams.SetRTPReceiveBuffer(3 * 1024 * 1024); CRtpApp::TransParams.SetRTPSendBuffer(0); //Create rtp session iRet = CRtpApp::CreateSession(m_StreamRecvPort); if (-1 == iRet) { printf("%s: Create rtp port<%d> session fail!\n", __FUNCTION__, m_StreamRecvPort); return -1; } #if 0 //Add rtp destination host RTPIPv4Address RtpAddr(ntohl(inet_addr(m_StreamSendIp) ), m_StreamSendPort); int iErrNum = CRtpApp::AddDestination(RtpAddr); if (iErrNum != 0) { printf("%s: Add RTP Destination <%s:%d> fail!\n", __FUNCTION__, m_StreamSendIp, m_StreamSendPort); } else { printf("%s: Add RTP Destination <%s:%d> sucess!\n", __FUNCTION__, m_StreamSendIp, m_StreamSendPort); } #endif return 0; } /******************************************************************************* * Function: * CallBack function of receiving a rtp packet. *******************************************************************************/ void CUdpStreamTrans::CBRtpRecv() { CRtpApp::BeginDataAccess(); //Search data source for rtp if (CRtpApp::GotoFirstSourceWithData()) { do { RTPPacket *pRtpPacket = NULL; while ( (pRtpPacket = CRtpApp::GetNextPacket() ) != NULL) { //printf("1.>>>>>>>>>>>>>>>>>recv jrtplib PayloadType:%d len:%d\n", pRtpPacket->GetPayloadType(), pRtpPacket->GetPayloadLength()); int rtpplaytype = pRtpPacket->GetPayloadType(); if (IsVideoPayloadType(rtpplaytype) ) { HandleRecvStream(pRtpPacket->GetPayloadData(), pRtpPacket->GetPayloadLength(), rtpplaytype, pRtpPacket->HasMarker()); } //Delete rtp packet dynamic memory pointer CRtpApp::DeletePacket(pRtpPacket); } } while (CRtpApp::GotoNextSourceWithData() ); } CRtpApp::EndDataAccess(); //------------------------------ ·¢ËÍýÌåÍøÂ紩͸°ü ------------------------------// int now = GetClockTime(); //time(&now); if (now - (int)CRtpApp::SendTimeForNAT >= 10) { CRtpApp::SendNatMappingPacket(); CRtpApp::SendTimeForNAT = (time_t)now; } } int CUdpStreamTrans::HandleRecvStream(UINT8* pAFrame, UINT32 FrameSize, int PayLoadType, bool bMark) { do { if (0 == m_StreamParseHandle1) { m_StreamParseHandle1 = STREAMPARSE_Open(); if (m_StreamParseHandle1 == 0) { //fail } } StreamParseParamIn In; In.pBufIn = pAFrame; In.nSizeIn = FrameSize; In.bMark = bMark; In.PayLoadType = (StreamParseType_E)PayLoadType; StreamParseParamOut Out; STREAMPARSE_Parse(m_StreamParseHandle1, &In, &Out); if (Out.nSizeOut <= 0) { break; } if (Out.streamtype==0xC0) { // break; } if((m_RecvStreamType == 0) && (Out.streamtype != 0)) { m_RecvStreamType = Out.streamtype; printf("%s:1. >>>>>>>>>Detecing Stream Type... StreamType:%02x!!", __FUNCTION__, Out.streamtype); } if(m_RecvStreamType == 0) { printf("%s:2. Detecing Stream Type... StreamType:%d!!", __FUNCTION__, Out.streamtype); break; } int pts = Out.pts/90; StreamParseParamIn StreamIn; if(m_RecvStreamType == 0xE2)//MPEG4 { StreamIn.PayLoadType = STREAMPARSE_MERGEAMPEG4; } else if(m_RecvStreamType == 0xE3)//MPEG2 { StreamIn.PayLoadType = STREAMPARSE_MERGEAMPEG2; } else//H264 { StreamIn.PayLoadType = STREAMPARSE_ESTOES; } if (0 == m_StreamParseHandle2) { m_StreamParseHandle2 = STREAMPARSE_Open(); if (m_StreamParseHandle2 == 0) { //fail } } StreamIn.pBufIn = Out.pBufOut; StreamIn.nSizeIn = Out.nSizeOut; StreamIn.bMark = false; StreamParseParamOut StreamOut; STREAMPARSE_Parse(m_StreamParseHandle2, &StreamIn, &StreamOut); if (StreamOut.nSizeOut > 0) { int FrameType = GB_VIDEO_FRAME_P; VideoStreamType_E eVideoStreamType = E_VIDEO_STREAM_H264; if(m_RecvStreamType == 0xE2) { eVideoStreamType = E_VIDEO_STREAM_MPEG4; mpeg_header_t* VOL = (mpeg_header_t*)StreamOut.pBufOut; if(is_VOL_header(VOL)) { FrameType = GB_VIDEO_FRAME_I; } } else if(m_RecvStreamType == 0xE3) { eVideoStreamType = E_VIDEO_STREAM_MPEG2; mpeg_header_t* SH = (mpeg_header_t*)StreamOut.pBufOut; if(is_sequence_header(SH)) { FrameType = GB_VIDEO_FRAME_I; } } else if(m_RecvStreamType == 0xE1) { eVideoStreamType = E_VIDEO_STREAM_H265; if (is_h265_IFrame_Data(StreamOut.pBufOut, StreamOut.nSizeOut)) { FrameType = GB_VIDEO_FRAME_I; } } else { eVideoStreamType = E_VIDEO_STREAM_H264; if (is_H264_IFrame_Data(StreamOut.pBufOut, StreamOut.nSizeOut)) { FrameType = GB_VIDEO_FRAME_I; } } if (m_eVideoStreamType == E_VIDEO_STREAM_NONE) { m_eVideoStreamType = eVideoStreamType; printf("%s:>>>>>>>>>Detecing Stream Type:%d!!!!!!!!", __FUNCTION__, (int)m_eVideoStreamType); } if (MediaCBFunc != NULL) { MediaCBFunc((int)m_eVideoStreamType, FrameType, StreamOut.pBufOut, StreamOut.nSizeOut, UserData); } } break; }while(0); }