-
Notifications
You must be signed in to change notification settings - Fork 6
/
receive.cpp
112 lines (94 loc) · 2.28 KB
/
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
/*=============================================================================
* FileName: receive.cpp
* Desc: reveive h.264 from RTP server
* Author: licaibiao
* LastChange: 2017-04-22
* =============================================================================*/
#include <rtpsession.h>
#include <rtpudpv4transmitter.h>
#include <rtpipv4address.h>
#include <rtpsessionparams.h>
#include <rtperrors.h>
#include <rtplibraryversion.h>
#include <rtppacket.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string>
using namespace jrtplib;
void checkerror(int rtperr)
{
if (rtperr < 0)
{
std::cout << "ERROR: " << RTPGetErrorString(rtperr) << std::endl;
exit(-1);
}
}
int main(void)
{
RTPSession sess;
uint16_t portbase = 6664;
int status;
bool done = false;
RTPUDPv4TransmissionParams transparams;
RTPSessionParams sessparams;
sessparams.SetOwnTimestampUnit(1.0/10.0);
sessparams.SetAcceptOwnPackets(true);
transparams.SetPortbase(portbase);
status = sess.Create(sessparams,&transparams);
checkerror(status);
sess.BeginDataAccess();
RTPTime delay(0.001);
RTPTime starttime = RTPTime::CurrentTime();
FILE *fd;
size_t len;
uint8_t *loaddata;
RTPPacket *pack;
uint8_t buff[1024*100] = {0};
int pos = 0;
fd = fopen("./test_recv.h264","wb+");
while (!done)
{
status = sess.Poll();
checkerror(status);
if (sess.GotoFirstSourceWithData())
{
do
{
while ((pack = sess.GetNextPacket()) != NULL)
{
loaddata = pack->GetPayloadData();
len = pack->GetPayloadLength();
if(pack->GetPayloadType() == 96) //H264
{
if(pack->HasMarker()) // the last packet
{
memcpy(&buff[pos],loaddata,len);
fwrite(buff, 1, pos+len, fd);
pos = 0;
}
else
{
memcpy(&buff[pos],loaddata,len);
pos = pos + len;
}
}else
{
printf("!!! GetPayloadType = %d !!!! \n ",pack->GetPayloadType());
}
sess.DeletePacket(pack);
}
} while (sess.GotoNextSourceWithData());
}
RTPTime::Wait(delay);
RTPTime t = RTPTime::CurrentTime();
t -= starttime;
if (t > RTPTime(40.0))
done = true;
}
fclose(fd);
sess.EndDataAccess();
delay = RTPTime(10.0);
sess.BYEDestroy(delay,0,0);
return 0;
}