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
/******************************************************************************
* FILE:    RtpApp2.cpp
* Description:
*    Implement for CRtpApp2 class.
*
* Modified Code History
* Mark  Date        By          Modification Reason
*******************************************************************************
* 01    2010-10-3   songxw Initial creation.
******************************************************************************/
 
#include <sys/time.h>
#include <unistd.h>
#include "RtpApp2.h"
 
CRtpApp2::CRtpApp2()
{
    tsunit = 1.0 / 8000.0;
    
    bSendTo = false;
    struct timezone tzone;
    gettimeofday(&RunTime, &tzone);
    SeqNum = 0;
    SendTimeForNAT = 0;
}
CRtpApp2::~CRtpApp2()
{
    
}
 
/******************************************************************************
* Function:
*    Create rtp session and be ready for receiving data.
******************************************************************************/
int CRtpApp2::CreateSession(UINT16 ListenPort, UINT32 nSsrc, bool usethread)
{    
    SessParams.SetUsePollThread(usethread);
    SessParams.SetOwnTimestampUnit(tsunit);
    SessParams.SetAcceptOwnPackets(true);
    TransParams.SetPortbase(ListenPort);
 
    SessParams.SetMaximumPacketSize(60*1024);
    if(nSsrc > 0)
    {
        SessParams.SetUsePredefinedSSRC(true);
        SessParams.SetPredefinedSSRC(nSsrc);
    }
    
    int iErrNum = Create(SessParams, &TransParams);
    if (iErrNum < 0)
    {
        string RtpError = RTPGetErrorString(iErrNum);
        DBGPrint(M_RtpSessMgr, ERROR_LEVEL, "Create RTP Session error! Reason: %s!", RtpError.c_str() );
        return -1;
    }
    
    SetDefaultPayloadType(0);
    SetDefaultMark(false);
    SetDefaultTimestampIncrement(0);
//    IncrementTimestampDefault();
//    IncrementTimestamp(1);
            
    return 0;
}
 
/******************************************************************************
* Function:
*    Set source ip address, port and session ID. Send several packets to traverse
* NAT.
******************************************************************************/
int CRtpApp2::NatMappingControl(char IpStr[], UINT16 port, int SSId)
{
    SessionID = SSId;
    
    RTPIPv4Address RtpAddr( ntohl(inet_addr(IpStr) ), port);
    int iErrNum = AddDestination(RtpAddr);
    if (iErrNum != 0)
    {
        string RtpError = RTPGetErrorString(iErrNum);
        DBGPrint(M_RtpSessMgr, ERROR_LEVEL, "AddDestination <%s: %d> fail! Reason: %s!", IpStr, port, RtpError.c_str() );
        return -1;
    }
    
    SendNatMappingPacket();
    usleep(1000);
    SendNatMappingPacket();
    usleep(1000);
    SendNatMappingPacket();
    
    return 0;    
}
 
/******************************************************************************
* Function:
*    Hook function of receiving a rtp packet.
******************************************************************************/
void CRtpApp2::CBRtpRecv2()
{
    
}
 
/******************************************************************************
* Function:
*    Rtp session data poll function.
******************************************************************************/
void CRtpApp2::OnPollThreadStep()
{    
    CBRtpRecv2();
}
 
/******************************************************************************
* Function:
*    Bye operation of rtp session.
******************************************************************************/
void CRtpApp2::RtpDestroy()
{
    BYEDestroy(RTPTime(1, 0), 0, 0);
}
 
/******************************************************************************
* Function:
*    Send a packet to traverse NAT and keep mapping status to let rtp data come.
******************************************************************************/
void CRtpApp2::SendNatMappingPacket(void)
{
    UINT8 data[48+1] = {0};
    
    //Set version and payload type
    data[0] = 0x80;
    data[1] = 0x30;    //Mark is always true. 0xb0 = 0x80 + 0x30
    
    //Set sequence number
    *( (UINT16 *)&data[2] ) = htons(SeqNum++);
    
    struct timeval CurrTVal;
    struct timezone tzone;
    gettimeofday(&CurrTVal, &tzone);
    int timestamp = (CurrTVal.tv_sec - RunTime.tv_sec) * 1000 + (CurrTVal.tv_usec - RunTime.tv_usec) / 1000;  //(ms)
    
    //Set timestamp
    *( (UINT32 *)&data[4] ) = htonl(timestamp);
    
    //Set SSRC
    *( (UINT32 *)&data[8] ) = htonl(SessionID);
    
    //Set payload data
    snprintf( (char *)&data[12], 36, "%s", "A Packet to traverse NAT.");
    
    SendPacketIntegratedData(data, 48);
}