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
/******************************************************************************
* FILE:    RtpApp.cpp
* Description:
*    Implement for CRtpApp class.
*
* Modified Code History
* Mark  Date        By          Modification Reason
*******************************************************************************
* 01    2008-4-16   songxw Initial creation.
******************************************************************************/
 
#include <sys/time.h>
#include <unistd.h>
#include "RtpApp.h"
 
CRtpApp::CRtpApp()
{
    tsunit = 1.0 / 90000;
    
    struct timezone tzone;
    gettimeofday(&RunTime, &tzone);
    gettimeofday(&RunTime2, &tzone);
    SeqNum = 0;
    SendTimeForNAT = 0;
}
CRtpApp::~CRtpApp()
{
    
}
 
/******************************************************************************
* Function:
*    Create rtp session and be ready for receiving data.
******************************************************************************/
int CRtpApp::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 CRtpApp::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 CRtpApp::CBRtpRecv()
{
    
}
 
/******************************************************************************
* Function:
*    Rtp session data poll function.
******************************************************************************/
void CRtpApp::OnPollThreadStep()
{    
    CBRtpRecv(); 
}
 
/******************************************************************************
* Function:
*    Bye operation of rtp session.
******************************************************************************/
void CRtpApp::RtpDestroy()
{
    BYEDestroy(RTPTime(1, 0), 0, 0);
}
 
/******************************************************************************
* Function:
*    Send a packet to traverse NAT and keep mapping status to let rtp data come.
******************************************************************************/
void CRtpApp::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);
}
 
int CRtpApp::GetTimeStamp(void)
{
    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)
    
    /*
    printf("timestamp = %d, CurrTVal(%d %d), RunTime(%d %d), CurrTVal.tv_sec - RunTime.tv_sec = %d, CurrTVal.tv_usec - RunTime.tv_usec = %d\n", timestamp, \
        CurrTVal.tv_sec, CurrTVal.tv_usec, RunTime.tv_sec, RunTime.tv_usec, CurrTVal.tv_sec - RunTime.tv_sec, CurrTVal.tv_usec - RunTime.tv_usec);
    */
    
    if (timestamp != 0)
    {
        RunTime = CurrTVal;
    }
    
    return timestamp;
}
 
int CRtpApp::GetAudioTimeStamp(void)
{
    struct timeval CurrTVal;
    struct timezone tzone;
    gettimeofday(&CurrTVal, &tzone);
    int timestamp = (CurrTVal.tv_sec - RunTime2.tv_sec) * 1000 + (CurrTVal.tv_usec - RunTime2.tv_usec) / 1000;  //(ms)
    
//    printf("----------------CurrTVal(%d %d) and RunTime2(%d %d) from RunTime3(%d %d), timestamp = %d \n", CurrTVal.tv_sec, CurrTVal.tv_usec, RunTime2.tv_sec, RunTime2.tv_usec, RunTime3.tv_sec, RunTime3.tv_usec, timestamp);
    
    if (timestamp != 0)
    {
        RunTime2 = CurrTVal;
    }
    
    return timestamp;
}