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
|
/*
* Copyright (c) 2012 Yoann Blein
* Licensed under the simplified BSD license.
* See Documentation/Licenses/BSD-simplified.txt for more information.
*/
#include <Swiften/ScreenSharing/RTPSessionImpl.h>
#include <Swiften/ScreenSharing/RTPException.h>
#include <Swiften/Network/BoostUDPSocket.h>
#include <Swiften/Base/boost_bsignals.h>
#include <Swiften/Network/UDPSocket.h>
#include <Swiften/Base/Log.h>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/classification.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/numeric/conversion/cast.hpp>
#include <boost/bind.hpp>
#include <rtppacket.h>
#include <rtpsourcedata.h>
#include <rtpsessionparams.h>
#include <rtpipv4address.h>
#include <rtpipv6address.h>
#include <arpa/inet.h>
namespace Swift {
Sender::Sender(boost::shared_ptr<UDPSocket> udpSocket)
: udpSocket(udpSocket), jRTPLocalAddress(RTPSessionImpl::nativeAddressToJRTPAddress(udpSocket->getLocalAddress())) {
}
Sender::~Sender()
{
delete jRTPLocalAddress;
}
bool Sender::SendRTP(const void *data, size_t len) {
send(data, len);
return true;
}
bool Sender::SendRTCP(const void* data, size_t len) {
send(data, len);
return true;
}
bool Sender::ComesFromThisSender (const jrtplib::RTPAddress* address) {
return jRTPLocalAddress->IsSameAddress(address);
}
void Sender::send(const void* data, size_t len) {
uint8_t* uint8Data = (uint8_t*)data;
udpSocket->send(SafeByteArray(uint8Data, uint8Data + len));
}
RTPSessionImpl::RTPSessionImpl(boost::shared_ptr<UDPSocket> udpSocket, const RTPPayloadType& payloadType)
: udpSocket(udpSocket), payloadType(payloadType), jRTPRemotePeer(nativeAddressToJRTPAddress(udpSocket->getRemoteAddress())), sender(udpSocket)
{
jrtplib::RTPExternalTransmissionParams transparams(&sender, 0);
jrtplib::RTPSessionParams sessparams;
// IMPORTANT: The local timestamp unit MUST be set, otherwise RTCP Sender Report info will be calculated wrong
sessparams.SetOwnTimestampUnit(1.0 / payloadType.getClockrate());
checkError(Create(sessparams, &transparams, jrtplib::RTPTransmitter::ExternalProto));
packetInjecter = static_cast<jrtplib::RTPExternalTransmissionInfo*>(GetTransmissionInfo())->GetPacketInjector();
udpSocket->onDataRead.connect(boost::bind(&RTPSessionImpl::handleDataRead, this, _1));
udpSocket->listen();
}
RTPSessionImpl::~RTPSessionImpl()
{
delete jRTPRemotePeer;
}
void RTPSessionImpl::poll()
{
checkError(Poll()); // Required if threading disabled
}
void RTPSessionImpl::checkIncomingPackets()
{
// session.BeginDataAccess(); // useless without threading
if (GotoFirstSourceWithData() && GetCurrentSourceInfo()->GetRTPDataAddress()->IsSameAddress(jRTPRemotePeer)) {
do {
jrtplib::RTPPacket *pack;
while ((pack = GetNextPacket()) != NULL) {
onIncomingPacket(pack->GetPayloadData(), pack->GetPayloadLength(), pack->HasMarker());
DeletePacket(pack);
}
} while (GotoNextSourceWithData());
}
// session.EndDataAccess(); // useless without threading
}
void RTPSessionImpl::sendPacket(const SafeByteArray& data, int timestampinc, bool marker)
{
checkError(SendPacket((void*)(data.data()), data.size(), payloadType.getID(), marker, timestampinc));
poll();
}
void RTPSessionImpl::injectData(const SafeByteArray& data)
{
packetInjecter->InjectRTPorRTCP((void*)(data.data()), data.size(), *jRTPRemotePeer);
checkIncomingPackets();
poll();
}
void RTPSessionImpl::stop(int maxWaitMs)
{
BYEDestroy(jrtplib::RTPTime(0, maxWaitMs * 1000), "", 0);
udpSocket->close();
}
void RTPSessionImpl::sendSLIFeedback(int pictureID)
{
// Send an SLI as negative feedback. VP8: 0, total number of macroblocks, pictureID (6 bits)
int first = 0; // 13 bits
int number = 0; // 13 bits // TODO : Find the total number of macroblocks per frame
uint32_t data = 0;
data |= first << 19;
data |= (number & 2047) << 6;
data |= (pictureID & 63);
SendUnknownPacket(false, PSFB, PSFB_SLI, (void*)&data, sizeof(uint32_t));
}
void RTPSessionImpl::sendRPSIFeedback(int pictureID)
{
// Send an RPSI as positive feedback. With VP8, it only contains the picture ID (7 bits)
int pb = 9; // trailing padding bits
int pt = payloadType.getID(); // payload type (7 bits)
uint32_t data = 0;
data |= pb << 24;
data |= (pt & 127) << 16;
data |= (pictureID & 127) << 9;
SendUnknownPacket(false, PSFB, PSFB_RPSI, (void*)&data, sizeof(uint32_t));
}
size_t RTPSessionImpl::getMaxRTPPayloadSize() const
{
jrtplib::RTPSessionParams sessparams;
return sessparams.GetMaximumPacketSize();
}
void RTPSessionImpl::OnUnknownPacketType(jrtplib::RTCPPacket* rtcpPack, const jrtplib::RTPTime& /*receivetime*/, const jrtplib::RTPAddress* senderAddress)
{
if (!senderAddress->IsSameAddress(jRTPRemotePeer))
return;
uint8_t* data = rtcpPack->GetPacketData();
size_t len = rtcpPack->GetPacketLength();
jrtplib::RTCPCommonHeader* rtcpHdr = (jrtplib::RTCPCommonHeader*)data;
int type = rtcpHdr->packettype;
if (type != PSFB)
return;
int subtype = rtcpHdr->count;
switch (subtype) {
case PSFB_SLI:
parseSLIFeedBack(data + 8, len - 8);
break;
case PSFB_RPSI:
parseRPSIFeedBack(data + 8, len - 8);
break;
default:
break;
}
}
void RTPSessionImpl::checkError(int rtperr) const
{
if (rtperr < 0)
throw RTPException(jrtplib::RTPGetErrorString(rtperr));
}
void RTPSessionImpl::handleDataRead(boost::shared_ptr<SafeByteArray> data)
{
injectData(*data);
}
void RTPSessionImpl::parseSLIFeedBack(uint8_t* data, size_t len)
{
SWIFT_LOG(debug) << "Got SLI feedback (negative)" << std::endl;
if (len < 4)
return;
int pictureID = data[len - 1] & 63; // pictureID correspond to the 6 last bits
onSLIFeedback(pictureID);
}
void RTPSessionImpl::parseRPSIFeedBack(uint8_t* data, size_t len)
{
SWIFT_LOG(debug) << "Got RPSI feedback (postive)" << std::endl;
if (len < 4)
return;
int pb = data[0]; // First byte : trailing padding bits
uint32_t intData = *((uint32_t*)(data));
intData >>= pb; // remove padding bits
int pictureID = (intData & 127); // pictureID correspond to the 7 last bits
onRPSIFeedback(pictureID);
}
jrtplib::RTPAddress* RTPSessionImpl::nativeAddressToJRTPAddress(const HostAddressPort& hostAddressPort)
{
jrtplib::RTPAddress* jrtpAddress = 0;
std::string ipAddress = hostAddressPort.getAddress().toString();
uint16_t port = boost::numeric_cast<uint16_t>(hostAddressPort.getPort());
if (hostAddressPort.getAddress().getRawAddress().is_v4()) {
// Split address
std::vector<std::string> subStrings;
boost::algorithm::split(subStrings, ipAddress, boost::is_any_of("."));
// Cast sub strings array to array of byte
uint8_t ipNumbers[4];
for (int i = 0; i < std::min(4, (int)subStrings.size()); ++i)
ipNumbers[i] = boost::numeric_cast<uint8_t>(boost::lexical_cast<int>(subStrings[i]));
jrtpAddress = new jrtplib::RTPIPv4Address(ipNumbers, port);
}
else if (hostAddressPort.getAddress().getRawAddress().is_v6()) {
in6_addr addr;
inet_pton(AF_INET6, ipAddress.c_str(), &addr);
jrtpAddress = new jrtplib::RTPIPv6Address(addr, port);
}
return jrtpAddress;
}
}
|