zhangzengfei
2022-07-20 c90c3e794bdd95127d0c34ff1d9e8759d18a0445
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include <sys/ioctl.h>
#include <pthread.h>
#include <poll.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/epoll.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <sys/mman.h>
#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);    
}