-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtransfer.cpp
More file actions
148 lines (136 loc) · 4.52 KB
/
transfer.cpp
File metadata and controls
148 lines (136 loc) · 4.52 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
#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 "stdlib.h"
#include "transfer.h"
#include "debug.h"
#include "config.h"
using namespace jrtplib;
NALU_HEADER *nalu_hdr;
FU_INDICATOR *fu_ind;
FU_HEADER *fu_hdr;
char sendbuf[1500];
char *nalu_payload;
NALU_t *n;
RTPSession session;
RTPSessionParams sessionparams;
RTPUDPv4TransmissionParams transparams;
/**
* @brief Get the startcodeprefix len object for 4. such as 00 00 00 01.
*
* @param nalu
* @return int
*/
static int get_startcodeprefix_len(uint8_t *nalu)
{
if(nalu[2]==1)return 3;
return 4;
}
void close_transfer()
{
session.BYEDestroy(RTPTime(10.0), "Time's up", 9);
}
int init_param_for_jrtplib()
{
int ret = 0;
uint8_t dest_ip[] = DEST_IP;
sessionparams.SetOwnTimestampUnit(1.0 / BYTES_PER_SECOND);
transparams.SetPortbase(LOCAL_PORT);
ret = session.Create(sessionparams, &transparams);
RTPIPv4Address addr(dest_ip, REMOTE_PORT);
ret = session.AddDestination(addr);
session.SetDefaultPayloadType(96);
session.SetDefaultMark(false);
session.SetDefaultTimestampIncrement(BYTES_PER_SECOND/FPS);
n = (NALU_t *)calloc(1, sizeof(NALU_t));
return ret;
}
int transfer_one_nalu_by_rtp(uint8_t *nalu, int len, uint32_t inc)
{
int ret;
n->startcodeprefix_len = get_startcodeprefix_len(nalu);
n->buf = nalu + n->startcodeprefix_len;
n->forbidden_bit = n->buf[0] & 0x80;
n->nal_reference_idc = n->buf[0] & 0x60;
n->nal_unit_type = (n->buf[0]) & 0x1f;
n->len = len - n->startcodeprefix_len;
debug_print("nalu type = %u.",n->nal_unit_type);
if (n->len <= MAX_RTP_PKT_LENGTH)
{
nalu_hdr = (NALU_HEADER *)&sendbuf[0];
nalu_hdr->F = n->forbidden_bit;
nalu_hdr->NRI = n->nal_reference_idc >> 5;
nalu_hdr->TYPE = n->nal_unit_type;
nalu_payload = &sendbuf[1];
memcpy(nalu_payload, n->buf + 1, n->len - 1);
if (n->nal_unit_type == 1 || n->nal_unit_type == 5)
{
ret = session.SendPacket((void *)sendbuf, n->len, 96, true, inc);
}
else
{
ret = session.SendPacket((void *)sendbuf, n->len, 96, true, 0);
}
}
else if (n->len > MAX_RTP_PKT_LENGTH)
{
int k = 0, l = 0, t = 0;
k = n->len / MAX_RTP_PKT_LENGTH;
l = n->len % MAX_RTP_PKT_LENGTH;
while (t <= k)
{
if (!t)
{
memset(sendbuf, 0, 1500);
fu_ind = (FU_INDICATOR *)&sendbuf[0];
fu_ind->F = n->forbidden_bit;
fu_ind->NRI = n->nal_reference_idc >> 5;
fu_ind->TYPE = 28;
fu_hdr = (FU_HEADER *)&sendbuf[1];
fu_hdr->E = 0;
fu_hdr->R = 0;
fu_hdr->S = 1;
fu_hdr->TYPE = n->nal_unit_type;
nalu_payload = &sendbuf[2];
memcpy(nalu_payload, n->buf + 1, MAX_RTP_PKT_LENGTH);
ret = session.SendPacket((void *)sendbuf, MAX_RTP_PKT_LENGTH + 2, 96, false, 0);
t++;
}
else if (k == t)
{
fu_ind->F = n->forbidden_bit;
fu_ind->NRI = n->nal_reference_idc >> 5;
fu_ind->TYPE = 28;
fu_hdr = (FU_HEADER *)&sendbuf[1];
fu_hdr->R = 0;
fu_hdr->S = 0;
fu_hdr->TYPE = n->nal_unit_type;
fu_hdr->E = 1;
nalu_payload = &sendbuf[2];
memcpy(nalu_payload, n->buf + t * MAX_RTP_PKT_LENGTH + 1, l - 1);
ret = session.SendPacket((void *)sendbuf, l + 1, 96, true, inc);
t++;
}
else if (t < k && 0 != t)
{
fu_ind = (FU_INDICATOR *)&sendbuf[0];
fu_ind->F = n->forbidden_bit;
fu_ind->NRI = n->nal_reference_idc >> 5;
fu_ind->TYPE = 28;
fu_hdr = (FU_HEADER *)&sendbuf[1];
fu_hdr->R = 0;
fu_hdr->S = 0;
fu_hdr->E = 0;
fu_hdr->TYPE = n->nal_unit_type;
nalu_payload = &sendbuf[2];
memcpy(nalu_payload, n->buf + t * MAX_RTP_PKT_LENGTH + 1, MAX_RTP_PKT_LENGTH);
ret = session.SendPacket((void *)sendbuf, MAX_RTP_PKT_LENGTH + 2, 96, false, 0);
t++;
}
}
}
return ret;
}