-
Notifications
You must be signed in to change notification settings - Fork 0
/
jrtplib_receive.cpp
212 lines (178 loc) · 4.65 KB
/
jrtplib_receive.cpp
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
/*
This IPv4 example uses the background thread itself to process all packets.
You can use example one to send data to the session that's created in this
example.
*/
#include "jrtplib3/rtpsession.h"
#include "jrtplib3/rtpsessionparams.h"
#include "jrtplib3/rtpudpv4transmitter.h"
#include "jrtplib3/rtpipv4address.h"
#include "jrtplib3/rtptimeutilities.h"
#include "jrtplib3/rtppacket.h"
#include <jrtplib3/rtpsourcedata.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string>
using namespace jrtplib;
#ifdef RTP_SUPPORT_THREAD
//
// This function checks if there was a RTP error. If so, it displays an error
// message and exists.
//
size_t len;
uint8_t *loaddata;
uint8_t buff[1024*100] = {0};
uint8_t start_bits[4] = {0,0,0,1};
int pos = 0;
FILE *fd = fopen("./recv.264","wb+");
void checkerror(int rtperr)
{
if (rtperr < 0)
{
std::cout << "ERROR: " << RTPGetErrorString(rtperr) << std::endl;
exit(-1);
}
}
//
// The new class routine
//
class MyRTPSession : public RTPSession
{
protected:
void OnPollThreadStep();
void ProcessRTPPacket(const RTPSourceData &srcdat,const RTPPacket &rtppack);
};
void MyRTPSession::OnPollThreadStep()
{
BeginDataAccess();
// check incoming packets
if (GotoFirstSourceWithData())
{
do
{
RTPPacket *pack;
RTPSourceData *srcdat;
srcdat = GetCurrentSourceInfo();
while ((pack = GetNextPacket()) != NULL)
{
ProcessRTPPacket(*srcdat,*pack);
DeletePacket(pack);
}
} while (GotoNextSourceWithData());
}
EndDataAccess();
}
bool isFUid(uint8_t* loaddata){
if((*loaddata & 31 )==28){
return true;
}
return false;
}
void MyRTPSession::ProcessRTPPacket(const RTPSourceData &srcdat,const RTPPacket &rtppack)
{
// You can inspect the packet and the source's info here
std::cout << "Got packet " << rtppack.GetExtendedSequenceNumber() << " from SSRC " << srcdat.GetSSRC() << std::endl;
loaddata = rtppack.GetPayloadData();
//std::cout<<loaddata<<std::endl;
len = rtppack.GetPayloadLength();
//std::cout<<len<<std::endl;
if(rtppack.GetPayloadType()==96){
//std::cout<<rtppack.HasMarker()<<std::endl;
if(rtppack.HasMarker()) // the last packet
{
if(!isFUid(loaddata)){ //not FU
memcpy(&buff[pos],start_bits,4);
pos=pos+4;
}else{
if(((*(loaddata+1))>>7)& 1){ //start FU
memcpy(&buff[pos],start_bits,4);
pos=pos+4;
loaddata=loaddata+1;
uint8_t temp = *loaddata & 31;
*loaddata = 0;
*loaddata = temp +96; // replace nalu header
len=len-1;
}else{
loaddata = loaddata+2;
len = len-2;
}
}
memcpy(&buff[pos],loaddata,len);
//fwrite(start_bit,3,pos+3,fd);
fwrite(buff, 1, pos+len, fd);
pos = 0;
}
else
{
if(!isFUid(loaddata)){ //not FU
memcpy(&buff[pos],start_bits,4);
pos=pos+4;
}else{
//std::cout<<(*(loaddata++)>>8 & 1)<<std::endl;
if(((*(loaddata+1))>>7 )& 1){ //start FU
memcpy(&buff[pos],start_bits,4);
pos=pos+4;
loaddata=loaddata+1;
uint8_t temp = *loaddata & 31;
*loaddata = 0;
*loaddata = temp +96; // replace nalu header
len=len-1;
}else{
loaddata = loaddata+2; //not a start FU, just ignore first 2 bytes
len = len-2;
}
}
memcpy(&buff[pos],loaddata,len);
pos = pos + len;
}
}
}
//
// The main routine
//
int main(void)
{
#ifdef RTP_SOCKETTYPE_WINSOCK
WSADATA dat;
WSAStartup(MAKEWORD(2,2),&dat);
#endif // RTP_SOCKETTYPE_WINSOCK
MyRTPSession sess;
uint16_t portbase;
std::string ipstr;
int status,num;
// First, we'll ask for the necessary information
// std::cout << "Enter local portbase:" << std::endl;
// std::cin >> portbase;
// std::cout << std::endl;
portbase = 9000;
// std::cout << std::endl;
// std::cout << "Number of seconds you wish to wait:" << std::endl;
// std::cin >> num;
num = 30;
// Now, we'll create a RTP session, set the destination
// and poll for incoming data.
RTPUDPv4TransmissionParams transparams;
RTPSessionParams sessparams;
// IMPORTANT: The local timestamp unit MUST be set, otherwise
// RTCP Sender Report info will be calculated wrong
// In this case, we'll be just use 8000 samples per second.
sessparams.SetOwnTimestampUnit(1.0/90000.0);
transparams.SetPortbase(portbase);
status = sess.Create(sessparams,&transparams);
checkerror(status);
// Wait a number of seconds
RTPTime::Wait(RTPTime(num,0));
sess.BYEDestroy(RTPTime(10,0),0,0);
#ifdef RTP_SOCKETTYPE_WINSOCK
WSACleanup();
#endif // RTP_SOCKETTYPE_WINSOCK
return 0;
}
#else
int main(void)
{
std::cerr << "Thread support is required for this example" << std::endl;
return 0;
}
#endif // RTP_SUPPORT_THREAD