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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
|
/*
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "media/base/rtpdataengine.h"
#include <map>
#include "media/base/codec.h"
#include "media/base/mediaconstants.h"
#include "media/base/rtputils.h"
#include "media/base/streamparams.h"
#include "rtc_base/copyonwritebuffer.h"
#include "rtc_base/helpers.h"
#include "rtc_base/logging.h"
#include "rtc_base/ratelimiter.h"
#include "rtc_base/sanitizer.h"
#include "rtc_base/stringutils.h"
namespace cricket {
// We want to avoid IP fragmentation.
static const size_t kDataMaxRtpPacketLen = 1200U;
// We reserve space after the RTP header for future wiggle room.
static const unsigned char kReservedSpace[] = {
0x00, 0x00, 0x00, 0x00
};
// Amount of overhead SRTP may take. We need to leave room in the
// buffer for it, otherwise SRTP will fail later. If SRTP ever uses
// more than this, we need to increase this number.
static const size_t kMaxSrtpHmacOverhead = 16;
RtpDataEngine::RtpDataEngine() {
data_codecs_.push_back(
DataCodec(kGoogleRtpDataCodecPlType, kGoogleRtpDataCodecName));
}
DataMediaChannel* RtpDataEngine::CreateChannel(
const MediaConfig& config) {
return new RtpDataMediaChannel(config);
}
static const DataCodec* FindCodecByName(const std::vector<DataCodec>& codecs,
const std::string& name) {
for (const DataCodec& codec : codecs) {
if (_stricmp(name.c_str(), codec.name.c_str()) == 0)
return &codec;
}
return nullptr;
}
RtpDataMediaChannel::RtpDataMediaChannel(const MediaConfig& config)
: DataMediaChannel(config) {
Construct();
}
void RtpDataMediaChannel::Construct() {
sending_ = false;
receiving_ = false;
send_limiter_.reset(new rtc::RateLimiter(kDataMaxBandwidth / 8, 1.0));
}
RtpDataMediaChannel::~RtpDataMediaChannel() {
std::map<uint32_t, RtpClock*>::const_iterator iter;
for (iter = rtp_clock_by_send_ssrc_.begin();
iter != rtp_clock_by_send_ssrc_.end();
++iter) {
delete iter->second;
}
}
void RTC_NO_SANITIZE("float-cast-overflow") // bugs.webrtc.org/8204
RtpClock::Tick(double now, int* seq_num, uint32_t* timestamp) {
*seq_num = ++last_seq_num_;
*timestamp = timestamp_offset_ + static_cast<uint32_t>(now * clockrate_);
// UBSan: 5.92374e+10 is outside the range of representable values of type
// 'unsigned int'
}
const DataCodec* FindUnknownCodec(const std::vector<DataCodec>& codecs) {
DataCodec data_codec(kGoogleRtpDataCodecPlType, kGoogleRtpDataCodecName);
std::vector<DataCodec>::const_iterator iter;
for (iter = codecs.begin(); iter != codecs.end(); ++iter) {
if (!iter->Matches(data_codec)) {
return &(*iter);
}
}
return NULL;
}
const DataCodec* FindKnownCodec(const std::vector<DataCodec>& codecs) {
DataCodec data_codec(kGoogleRtpDataCodecPlType, kGoogleRtpDataCodecName);
std::vector<DataCodec>::const_iterator iter;
for (iter = codecs.begin(); iter != codecs.end(); ++iter) {
if (iter->Matches(data_codec)) {
return &(*iter);
}
}
return NULL;
}
bool RtpDataMediaChannel::SetRecvCodecs(const std::vector<DataCodec>& codecs) {
const DataCodec* unknown_codec = FindUnknownCodec(codecs);
if (unknown_codec) {
RTC_LOG(LS_WARNING) << "Failed to SetRecvCodecs because of unknown codec: "
<< unknown_codec->ToString();
return false;
}
recv_codecs_ = codecs;
return true;
}
bool RtpDataMediaChannel::SetSendCodecs(const std::vector<DataCodec>& codecs) {
const DataCodec* known_codec = FindKnownCodec(codecs);
if (!known_codec) {
RTC_LOG(LS_WARNING)
<< "Failed to SetSendCodecs because there is no known codec.";
return false;
}
send_codecs_ = codecs;
return true;
}
bool RtpDataMediaChannel::SetSendParameters(const DataSendParameters& params) {
return (SetSendCodecs(params.codecs) &&
SetMaxSendBandwidth(params.max_bandwidth_bps));
}
bool RtpDataMediaChannel::SetRecvParameters(const DataRecvParameters& params) {
return SetRecvCodecs(params.codecs);
}
bool RtpDataMediaChannel::AddSendStream(const StreamParams& stream) {
if (!stream.has_ssrcs()) {
return false;
}
if (GetStreamBySsrc(send_streams_, stream.first_ssrc())) {
RTC_LOG(LS_WARNING) << "Not adding data send stream '" << stream.id
<< "' with ssrc=" << stream.first_ssrc()
<< " because stream already exists.";
return false;
}
send_streams_.push_back(stream);
// TODO(pthatcher): This should be per-stream, not per-ssrc.
// And we should probably allow more than one per stream.
rtp_clock_by_send_ssrc_[stream.first_ssrc()] = new RtpClock(
kDataCodecClockrate,
rtc::CreateRandomNonZeroId(), rtc::CreateRandomNonZeroId());
RTC_LOG(LS_INFO) << "Added data send stream '" << stream.id
<< "' with ssrc=" << stream.first_ssrc();
return true;
}
bool RtpDataMediaChannel::RemoveSendStream(uint32_t ssrc) {
if (!GetStreamBySsrc(send_streams_, ssrc)) {
return false;
}
RemoveStreamBySsrc(&send_streams_, ssrc);
delete rtp_clock_by_send_ssrc_[ssrc];
rtp_clock_by_send_ssrc_.erase(ssrc);
return true;
}
bool RtpDataMediaChannel::AddRecvStream(const StreamParams& stream) {
if (!stream.has_ssrcs()) {
return false;
}
if (GetStreamBySsrc(recv_streams_, stream.first_ssrc())) {
RTC_LOG(LS_WARNING) << "Not adding data recv stream '" << stream.id
<< "' with ssrc=" << stream.first_ssrc()
<< " because stream already exists.";
return false;
}
recv_streams_.push_back(stream);
RTC_LOG(LS_INFO) << "Added data recv stream '" << stream.id
<< "' with ssrc=" << stream.first_ssrc();
return true;
}
bool RtpDataMediaChannel::RemoveRecvStream(uint32_t ssrc) {
RemoveStreamBySsrc(&recv_streams_, ssrc);
return true;
}
void RtpDataMediaChannel::OnPacketReceived(
rtc::CopyOnWriteBuffer* packet, const rtc::PacketTime& packet_time) {
RtpHeader header;
if (!GetRtpHeader(packet->cdata(), packet->size(), &header)) {
// Don't want to log for every corrupt packet.
// RTC_LOG(LS_WARNING) << "Could not read rtp header from packet of length "
// << packet->length() << ".";
return;
}
size_t header_length;
if (!GetRtpHeaderLen(packet->cdata(), packet->size(), &header_length)) {
// Don't want to log for every corrupt packet.
// RTC_LOG(LS_WARNING) << "Could not read rtp header"
// << length from packet of length "
// << packet->length() << ".";
return;
}
const char* data =
packet->cdata<char>() + header_length + sizeof(kReservedSpace);
size_t data_len = packet->size() - header_length - sizeof(kReservedSpace);
if (!receiving_) {
RTC_LOG(LS_WARNING) << "Not receiving packet " << header.ssrc << ":"
<< header.seq_num << " before SetReceive(true) called.";
return;
}
if (!FindCodecById(recv_codecs_, header.payload_type)) {
// For bundling, this will be logged for every message.
// So disable this logging.
// RTC_LOG(LS_WARNING) << "Not receiving packet "
// << header.ssrc << ":" << header.seq_num
// << " (" << data_len << ")"
// << " because unknown payload id: " << header.payload_type;
return;
}
if (!GetStreamBySsrc(recv_streams_, header.ssrc)) {
RTC_LOG(LS_WARNING) << "Received packet for unknown ssrc: " << header.ssrc;
return;
}
// Uncomment this for easy debugging.
// const auto* found_stream = GetStreamBySsrc(recv_streams_, header.ssrc);
// RTC_LOG(LS_INFO) << "Received packet"
// << " groupid=" << found_stream.groupid
// << ", ssrc=" << header.ssrc
// << ", seqnum=" << header.seq_num
// << ", timestamp=" << header.timestamp
// << ", len=" << data_len;
ReceiveDataParams params;
params.ssrc = header.ssrc;
params.seq_num = header.seq_num;
params.timestamp = header.timestamp;
SignalDataReceived(params, data, data_len);
}
bool RtpDataMediaChannel::SetMaxSendBandwidth(int bps) {
if (bps <= 0) {
bps = kDataMaxBandwidth;
}
send_limiter_.reset(new rtc::RateLimiter(bps / 8, 1.0));
RTC_LOG(LS_INFO) << "RtpDataMediaChannel::SetSendBandwidth to " << bps
<< "bps.";
return true;
}
bool RtpDataMediaChannel::SendData(
const SendDataParams& params,
const rtc::CopyOnWriteBuffer& payload,
SendDataResult* result) {
if (result) {
// If we return true, we'll set this to SDR_SUCCESS.
*result = SDR_ERROR;
}
if (!sending_) {
RTC_LOG(LS_WARNING) << "Not sending packet with ssrc=" << params.ssrc
<< " len=" << payload.size()
<< " before SetSend(true).";
return false;
}
if (params.type != cricket::DMT_TEXT) {
RTC_LOG(LS_WARNING)
<< "Not sending data because binary type is unsupported.";
return false;
}
const StreamParams* found_stream =
GetStreamBySsrc(send_streams_, params.ssrc);
if (!found_stream) {
RTC_LOG(LS_WARNING) << "Not sending data because ssrc is unknown: "
<< params.ssrc;
return false;
}
const DataCodec* found_codec =
FindCodecByName(send_codecs_, kGoogleRtpDataCodecName);
if (!found_codec) {
RTC_LOG(LS_WARNING) << "Not sending data because codec is unknown: "
<< kGoogleRtpDataCodecName;
return false;
}
size_t packet_len = (kMinRtpPacketLen + sizeof(kReservedSpace) +
payload.size() + kMaxSrtpHmacOverhead);
if (packet_len > kDataMaxRtpPacketLen) {
return false;
}
double now =
rtc::TimeMicros() / static_cast<double>(rtc::kNumMicrosecsPerSec);
if (!send_limiter_->CanUse(packet_len, now)) {
RTC_LOG(LS_VERBOSE) << "Dropped data packet of len=" << packet_len
<< "; already sent " << send_limiter_->used_in_period()
<< "/" << send_limiter_->max_per_period();
return false;
}
RtpHeader header;
header.payload_type = found_codec->id;
header.ssrc = params.ssrc;
rtp_clock_by_send_ssrc_[header.ssrc]->Tick(
now, &header.seq_num, &header.timestamp);
rtc::CopyOnWriteBuffer packet(kMinRtpPacketLen, packet_len);
if (!SetRtpHeader(packet.data(), packet.size(), header)) {
return false;
}
packet.AppendData(kReservedSpace);
packet.AppendData(payload);
RTC_LOG(LS_VERBOSE) << "Sent RTP data packet: "
<< " stream=" << found_stream->id
<< " ssrc=" << header.ssrc
<< ", seqnum=" << header.seq_num
<< ", timestamp=" << header.timestamp
<< ", len=" << payload.size();
MediaChannel::SendPacket(&packet, rtc::PacketOptions());
send_limiter_->Use(packet_len, now);
if (result) {
*result = SDR_SUCCESS;
}
return true;
}
rtc::DiffServCodePoint RtpDataMediaChannel::PreferredDscp() const {
return rtc::DSCP_AF41;
}
} // namespace cricket
|