rtcpdump.cpp
6.38 KB
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
#include "rtpsession.h"
#include "rtpipv4address.h"
#include "rtpsessionparams.h"
#include "rtpudpv4transmitter.h"
#include "rtperrors.h"
#include "rtcpcompoundpacket.h"
#include "rtcpsrpacket.h"
#include "rtcprrpacket.h"
#include "rtcpbyepacket.h"
#include "rtprawpacket.h"
#include <stdio.h>
#include <iostream>
using namespace jrtplib;
void checkError(int status)
{
if (status >= 0)
return;
std::cerr << "An error occured in the RTP component: " << std::endl;
std::cerr << "Error description: " << RTPGetErrorString(status) << std::endl;
exit(-1);
}
class MyRTPSession : public RTPSession
{
private:
FILE *pLogFile;
public:
MyRTPSession()
{
SetChangeIncomingData(true);
pLogFile = fopen("logfile.dat", "wb");
}
~MyRTPSession()
{
if (pLogFile)
fclose(pLogFile);
}
protected:
void OnValidatedRTPPacket(RTPSourceData *srcdat, RTPPacket *rtppack, bool isonprobation, bool *ispackethandled)
{
// Make sure no RTP packets are stored internally, we'd just be wasting memory
DeletePacket(rtppack);
*ispackethandled = true;
}
bool OnChangeIncomingData(RTPRawPacket *pPack)
{
if (pLogFile)
{
double t = pPack->GetReceiveTime().GetDouble();
bool isRTP = pPack->IsRTP();
uint32_t dataLength = (uint32_t)pPack->GetDataLength();
if (isRTP)
fwrite("RTP ", 1, 4, pLogFile);
else
fwrite("RTCP", 1, 4, pLogFile);
fwrite(&t, 1, sizeof(double), pLogFile);
fwrite(&dataLength, 1, sizeof(uint32_t),pLogFile);
fwrite(pPack->GetData(), 1, dataLength, pLogFile);
}
return true;
}
void OnRTCPCompoundPacket(RTCPCompoundPacket *p, const RTPTime &receivetime, const RTPAddress *senderaddress)
{
printf("%u.%06u RECEIVED\n",receivetime.GetSeconds(),receivetime.GetMicroSeconds());
DumpCompoundPacket(stdout,p);
}
void OnSendRTCPCompoundPacket(RTCPCompoundPacket *p)
{
RTPTime t = RTPTime::CurrentTime();
printf("%u.%06u SENDING\n",t.GetSeconds(),t.GetMicroSeconds());
DumpCompoundPacket(stdout,p);
}
void DumpCompoundPacket(FILE *f, RTCPCompoundPacket *p)
{
RTCPPacket *pack;
p->GotoFirstPacket();
while ((pack = p->GetNextPacket()) != 0)
{
if (pack->GetPacketType() == RTCPPacket::SR)
{
RTCPSRPacket *p = (RTCPSRPacket *)pack;
RTPTime t(p->GetNTPTimestamp());
fprintf(f," SR packet\n SSRC %27u\n",p->GetSenderSSRC());
fprintf(f," NTP timestamp: %10u.%06u\n RTP timestamp: %17u\n Packets sent: %18u\n Octets sent: %19u\n",t.GetSeconds(),t.GetMicroSeconds(),p->GetRTPTimestamp(),p->GetSenderPacketCount(),p->GetSenderOctetCount());
for (int i = 0 ; i < p->GetReceptionReportCount() ; i++)
fprintf(f," RR block %d\n SSRC %25u\n Fraction lost: %15d\n Packets lost: %16d\n Ext. high. seq. nr: %10u\n Jitter: %22u\n LSR: %25u\n DLSR: %24u\n",(i+1),
p->GetSSRC(i),(int)p->GetFractionLost(i),p->GetLostPacketCount(i),p->GetExtendedHighestSequenceNumber(i),p->GetJitter(i),p->GetLSR(i),
p->GetDLSR(i));
}
else if (pack->GetPacketType() == RTCPPacket::RR)
{
RTCPRRPacket *p = (RTCPRRPacket *)pack;
fprintf(f," RR packet\n SSRC %27u\n",p->GetSenderSSRC());
for (int i = 0 ; i < p->GetReceptionReportCount() ; i++)
fprintf(f," RR block %d\n SSRC %25u\n Fraction lost: %15d\n Packets lost: %16d\n Ext. high. seq. nr: %10u\n Jitter: %22u\n LSR: %25u\n DLSR: %24u\n",(i+1),
p->GetSSRC(i),(int)p->GetFractionLost(i),p->GetLostPacketCount(i),p->GetExtendedHighestSequenceNumber(i),p->GetJitter(i),p->GetLSR(i),
p->GetDLSR(i));
}
else if (pack->GetPacketType() == RTCPPacket::SDES)
{
RTCPSDESPacket *p = (RTCPSDESPacket *)pack;
char str[1024];
if (!p->GotoFirstChunk())
return;
do
{
fprintf(f," SDES Chunk:\n");
fprintf(f," SSRC: %26u\n",p->GetChunkSSRC());
if (p->GotoFirstItem())
{
do
{
switch (p->GetItemType())
{
case RTCPSDESPacket::None:
strcpy(str,"None ");
break;
case RTCPSDESPacket::CNAME:
strcpy(str,"CNAME: ");
break;
case RTCPSDESPacket::NAME:
strcpy(str,"NAME: ");
break;
case RTCPSDESPacket::EMAIL:
strcpy(str,"EMAIL: ");
break;
case RTCPSDESPacket::PHONE:
strcpy(str,"PHONE: ");
break;
case RTCPSDESPacket::LOC:
strcpy(str,"LOC: ");
break;
case RTCPSDESPacket::TOOL:
strcpy(str,"TOOL: ");
break;
case RTCPSDESPacket::NOTE:
strcpy(str,"NOTE: ");
break;
case RTCPSDESPacket::PRIV:
strcpy(str,"PRIV: ");
break;
case RTCPSDESPacket::Unknown:
default:
strcpy(str,"Unknown ");
}
fprintf(f," %s",str);
if (p->GetItemType() != RTCPSDESPacket::PRIV)
{
char str[1024];
memcpy(str,p->GetItemData(),p->GetItemLength());
str[p->GetItemLength()] = 0;
fprintf(f,"%24s\n",str);
}
} while (p->GotoNextItem());
}
} while (p->GotoNextChunk());
}
else if (pack->GetPacketType() == RTCPPacket::BYE)
{
fprintf(f," BYE packet:\n");
RTCPBYEPacket *p = (RTCPBYEPacket *)pack;
int num = p->GetSSRCCount();
int i;
for (i = 0 ; i < num ; i++)
fprintf(f," SSRC: %26u\n",p->GetSSRC(i));
if (p->HasReasonForLeaving())
{
char str[1024];
memcpy(str,p->GetReasonData(),p->GetReasonLength());
str[p->GetReasonLength()] = 0;
fprintf(f," Reason: %24s\n",str);
}
}
}
fprintf(f,"\n");
}
};
int main(int argc, char *argv[])
{
if (argc != 4)
{
fprintf(stderr, "Usage: rtcpdump portbase destIP destport\n");
return -1;
}
int portBase = atoi(argv[1]);
int destPort = atoi(argv[3]);
std::string destIP(argv[2]);
RTPIPv4Address dest;
RTPUDPv4TransmissionParams transParams;
RTPSessionParams sessParams;
MyRTPSession session;
dest.SetIP(ntohl(inet_addr(destIP.c_str())));
dest.SetPort((uint16_t)destPort);
transParams.SetPortbase((uint16_t)portBase);
transParams.SetRTPReceiveBuffer(1024*1024);
transParams.SetRTCPReceiveBuffer(1024*1024);
sessParams.SetOwnTimestampUnit(1.0/5.0);
sessParams.SetProbationType(RTPSources::NoProbation);
int status;
status = session.Create(sessParams, &transParams);
checkError(status);
status = session.AddDestination(dest);
checkError(status);
int i = 0;
getchar();
return 0;
}