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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "signaling/src/jsep/JsepTrack.h"
#include "signaling/src/jsep/JsepCodecDescription.h"
#include "signaling/src/jsep/JsepTrackEncoding.h"
#include <algorithm>
namespace mozilla
{
void
JsepTrack::GetNegotiatedPayloadTypes(std::vector<uint16_t>* payloadTypes)
{
if (!mNegotiatedDetails) {
return;
}
for (const auto* encoding : mNegotiatedDetails->mEncodings.values) {
GetPayloadTypes(encoding->GetCodecs(), payloadTypes);
}
// Prune out dupes
std::sort(payloadTypes->begin(), payloadTypes->end());
auto newEnd = std::unique(payloadTypes->begin(), payloadTypes->end());
payloadTypes->erase(newEnd, payloadTypes->end());
}
/* static */
void
JsepTrack::GetPayloadTypes(
const std::vector<JsepCodecDescription*>& codecs,
std::vector<uint16_t>* payloadTypes)
{
for (JsepCodecDescription* codec : codecs) {
uint16_t pt;
if (!codec->GetPtAsInt(&pt)) {
MOZ_ASSERT(false);
continue;
}
payloadTypes->push_back(pt);
}
}
void
JsepTrack::EnsureNoDuplicatePayloadTypes(
std::vector<JsepCodecDescription*>* codecs)
{
std::set<uint16_t> uniquePayloadTypes;
for (JsepCodecDescription* codec : *codecs) {
// We assume there are no dupes in negotiated codecs; unnegotiated codecs
// need to change if there is a clash.
if (!codec->mEnabled) {
continue;
}
// Disable, and only re-enable if we can ensure it has a unique pt.
codec->mEnabled = false;
uint16_t currentPt;
if (!codec->GetPtAsInt(¤tPt)) {
MOZ_ASSERT(false);
continue;
}
if (!uniquePayloadTypes.count(currentPt)) {
codec->mEnabled = true;
uniquePayloadTypes.insert(currentPt);
continue;
}
// |codec| cannot use its current payload type. Try to find another.
for (uint16_t freePt = 96; freePt <= 127; ++freePt) {
// Not super efficient, but readability is probably more important.
if (!uniquePayloadTypes.count(freePt)) {
uniquePayloadTypes.insert(freePt);
codec->mEnabled = true;
std::ostringstream os;
os << freePt;
codec->mDefaultPt = os.str();
break;
}
}
}
}
void
JsepTrack::PopulateCodecs(const std::vector<JsepCodecDescription*>& prototype)
{
for (const JsepCodecDescription* prototypeCodec : prototype) {
if (prototypeCodec->mType == mType) {
mPrototypeCodecs.values.push_back(prototypeCodec->Clone());
mPrototypeCodecs.values.back()->mDirection = mDirection;
}
}
EnsureNoDuplicatePayloadTypes(&mPrototypeCodecs.values);
}
void
JsepTrack::AddToOffer(SdpMediaSection* offer) const
{
AddToMsection(mPrototypeCodecs.values, offer);
if (mDirection == sdp::kSend) {
AddToMsection(mJsEncodeConstraints, sdp::kSend, offer);
}
}
void
JsepTrack::AddToAnswer(const SdpMediaSection& offer,
SdpMediaSection* answer) const
{
// We do not modify mPrototypeCodecs here, since we're only creating an
// answer. Once offer/answer concludes, we will update mPrototypeCodecs.
PtrVector<JsepCodecDescription> codecs;
codecs.values = GetCodecClones();
NegotiateCodecs(offer, &codecs.values);
if (codecs.values.empty()) {
return;
}
AddToMsection(codecs.values, answer);
if (mDirection == sdp::kSend) {
std::vector<JsConstraints> constraints;
std::vector<SdpRidAttributeList::Rid> rids;
GetRids(offer, sdp::kRecv, &rids);
NegotiateRids(rids, &constraints);
AddToMsection(constraints, sdp::kSend, answer);
}
}
void
JsepTrack::AddToMsection(const std::vector<JsepCodecDescription*>& codecs,
SdpMediaSection* msection) const
{
MOZ_ASSERT(msection->GetMediaType() == mType);
MOZ_ASSERT(!codecs.empty());
for (const JsepCodecDescription* codec : codecs) {
codec->AddToMediaSection(*msection);
}
if (mDirection == sdp::kSend) {
if (msection->GetMediaType() != SdpMediaSection::kApplication) {
msection->SetSsrcs(mSsrcs, mCNAME);
msection->AddMsid(mStreamId, mTrackId);
}
msection->SetSending(true);
} else {
msection->SetReceiving(true);
}
}
// Updates the |id| values in |constraintsList| with the rid values in |rids|,
// where necessary.
void
JsepTrack::NegotiateRids(const std::vector<SdpRidAttributeList::Rid>& rids,
std::vector<JsConstraints>* constraintsList) const
{
for (const SdpRidAttributeList::Rid& rid : rids) {
if (!FindConstraints(rid.id, *constraintsList)) {
// Pair up the first JsConstraints with an empty id, if it exists.
JsConstraints* constraints = FindConstraints("", *constraintsList);
if (constraints) {
constraints->rid = rid.id;
}
}
}
}
/* static */
void
JsepTrack::AddToMsection(const std::vector<JsConstraints>& constraintsList,
sdp::Direction direction,
SdpMediaSection* msection)
{
UniquePtr<SdpSimulcastAttribute> simulcast(new SdpSimulcastAttribute);
UniquePtr<SdpRidAttributeList> rids(new SdpRidAttributeList);
for (const JsConstraints& constraints : constraintsList) {
if (!constraints.rid.empty()) {
SdpRidAttributeList::Rid rid;
rid.id = constraints.rid;
rid.direction = direction;
rids->mRids.push_back(rid);
SdpSimulcastAttribute::Version version;
version.choices.push_back(constraints.rid);
if (direction == sdp::kSend) {
simulcast->sendVersions.push_back(version);
} else {
simulcast->recvVersions.push_back(version);
}
}
}
if (!rids->mRids.empty()) {
msection->GetAttributeList().SetAttribute(simulcast.release());
msection->GetAttributeList().SetAttribute(rids.release());
}
}
void
JsepTrack::GetRids(const SdpMediaSection& msection,
sdp::Direction direction,
std::vector<SdpRidAttributeList::Rid>* rids) const
{
rids->clear();
if (!msection.GetAttributeList().HasAttribute(
SdpAttribute::kSimulcastAttribute)) {
return;
}
const SdpSimulcastAttribute& simulcast(
msection.GetAttributeList().GetSimulcast());
const SdpSimulcastAttribute::Versions* versions = nullptr;
switch (direction) {
case sdp::kSend:
versions = &simulcast.sendVersions;
break;
case sdp::kRecv:
versions = &simulcast.recvVersions;
break;
}
if (!versions->IsSet()) {
return;
}
if (versions->type != SdpSimulcastAttribute::Versions::kRid) {
// No support for PT-based simulcast, yet.
return;
}
for (const SdpSimulcastAttribute::Version& version : *versions) {
if (!version.choices.empty()) {
// We validate that rids are present (and sane) elsewhere.
rids->push_back(*msection.FindRid(version.choices[0]));
}
}
}
JsepTrack::JsConstraints*
JsepTrack::FindConstraints(const std::string& id,
std::vector<JsConstraints>& constraintsList) const
{
for (JsConstraints& constraints : constraintsList) {
if (constraints.rid == id) {
return &constraints;
}
}
return nullptr;
}
void
JsepTrack::CreateEncodings(
const SdpMediaSection& remote,
const std::vector<JsepCodecDescription*>& negotiatedCodecs,
JsepTrackNegotiatedDetails* negotiatedDetails)
{
std::vector<SdpRidAttributeList::Rid> rids;
GetRids(remote, sdp::kRecv, &rids); // Get rids we will send
NegotiateRids(rids, &mJsEncodeConstraints);
if (rids.empty()) {
// Add dummy value with an empty id to make sure we get a single unicast
// stream.
rids.push_back(SdpRidAttributeList::Rid());
}
// For each rid in the remote, make sure we have an encoding, and configure
// that encoding appropriately.
for (size_t i = 0; i < rids.size(); ++i) {
if (i == negotiatedDetails->mEncodings.values.size()) {
negotiatedDetails->mEncodings.values.push_back(new JsepTrackEncoding);
}
JsepTrackEncoding* encoding = negotiatedDetails->mEncodings.values[i];
for (const JsepCodecDescription* codec : negotiatedCodecs) {
if (rids[i].HasFormat(codec->mDefaultPt)) {
encoding->AddCodec(*codec);
}
}
encoding->mRid = rids[i].id;
// If we end up supporting params for rid, we would handle that here.
// Incorporate the corresponding JS encoding constraints, if they exist
for (const JsConstraints& jsConstraints : mJsEncodeConstraints) {
if (jsConstraints.rid == rids[i].id) {
encoding->mConstraints = jsConstraints.constraints;
}
}
encoding->UpdateMaxBitrate(remote);
}
}
std::vector<JsepCodecDescription*>
JsepTrack::GetCodecClones() const
{
std::vector<JsepCodecDescription*> clones;
for (const JsepCodecDescription* codec : mPrototypeCodecs.values) {
clones.push_back(codec->Clone());
}
return clones;
}
static bool
CompareCodec(const JsepCodecDescription* lhs, const JsepCodecDescription* rhs)
{
return lhs->mStronglyPreferred && !rhs->mStronglyPreferred;
}
void
JsepTrack::NegotiateCodecs(
const SdpMediaSection& remote,
std::vector<JsepCodecDescription*>* codecs,
std::map<std::string, std::string>* formatChanges) const
{
PtrVector<JsepCodecDescription> unnegotiatedCodecs;
std::swap(unnegotiatedCodecs.values, *codecs);
// Outer loop establishes the remote side's preference
for (const std::string& fmt : remote.GetFormats()) {
for (size_t i = 0; i < unnegotiatedCodecs.values.size(); ++i) {
JsepCodecDescription* codec = unnegotiatedCodecs.values[i];
if (!codec || !codec->mEnabled || !codec->Matches(fmt, remote)) {
continue;
}
std::string originalFormat = codec->mDefaultPt;
if(codec->Negotiate(fmt, remote)) {
codecs->push_back(codec);
unnegotiatedCodecs.values[i] = nullptr;
if (formatChanges) {
(*formatChanges)[originalFormat] = codec->mDefaultPt;
}
break;
}
}
}
// Find the (potential) red codec and ulpfec codec or telephone-event
JsepVideoCodecDescription* red = nullptr;
JsepVideoCodecDescription* ulpfec = nullptr;
JsepAudioCodecDescription* dtmf = nullptr;
// We can safely cast here since JsepTrack has a MediaType and only codecs
// that match that MediaType (kAudio or kVideo) are added.
for (auto codec : *codecs) {
if (codec->mName == "red") {
red = static_cast<JsepVideoCodecDescription*>(codec);
}
else if (codec->mName == "ulpfec") {
ulpfec = static_cast<JsepVideoCodecDescription*>(codec);
}
else if (codec->mName == "telephone-event") {
dtmf = static_cast<JsepAudioCodecDescription*>(codec);
}
}
// if we have a red codec remove redundant encodings that don't exist
if (red) {
// Since we could have an externally specified redundant endcodings
// list, we shouldn't simply rebuild the redundant encodings list
// based on the current list of codecs.
std::vector<uint8_t> unnegotiatedEncodings;
std::swap(unnegotiatedEncodings, red->mRedundantEncodings);
for (auto redundantPt : unnegotiatedEncodings) {
std::string pt = std::to_string(redundantPt);
for (auto codec : *codecs) {
if (pt == codec->mDefaultPt) {
red->mRedundantEncodings.push_back(redundantPt);
break;
}
}
}
}
// Video FEC is indicated by the existence of the red and ulpfec
// codecs and not an attribute on the particular video codec (like in
// a rtcpfb attr). If we see both red and ulpfec codecs, we enable FEC
// on all the other codecs.
if (red && ulpfec) {
for (auto codec : *codecs) {
if (codec->mName != "red" && codec->mName != "ulpfec") {
JsepVideoCodecDescription* videoCodec =
static_cast<JsepVideoCodecDescription*>(codec);
videoCodec->EnableFec();
}
}
}
// Dtmf support is indicated by the existence of the telephone-event
// codec, and not an attribute on the particular audio codec (like in a
// rtcpfb attr). If we see the telephone-event codec, we enabled dtmf
// support on all the other audio codecs.
if (dtmf) {
for (auto codec : *codecs) {
JsepAudioCodecDescription* audioCodec =
static_cast<JsepAudioCodecDescription*>(codec);
audioCodec->mDtmfEnabled = true;
}
}
// Make sure strongly preferred codecs are up front, overriding the remote
// side's preference.
std::stable_sort(codecs->begin(), codecs->end(), CompareCodec);
// TODO(bug 814227): Remove this once we're ready to put multiple codecs in an
// answer. For now, remove all but the first codec unless the red codec
// exists, and then we include the others per RFC 5109, section 14.2.
// Note: now allows keeping the telephone-event codec, if it appears, as the
// last codec in the list.
if (!codecs->empty() && !red) {
int newSize = dtmf ? 2 : 1;
for (size_t i = 1; i < codecs->size(); ++i) {
if (!dtmf || dtmf != (*codecs)[i]) {
delete (*codecs)[i];
(*codecs)[i] = nullptr;
}
}
if (dtmf) {
(*codecs)[newSize-1] = dtmf;
}
codecs->resize(newSize);
}
}
void
JsepTrack::Negotiate(const SdpMediaSection& answer,
const SdpMediaSection& remote)
{
PtrVector<JsepCodecDescription> negotiatedCodecs;
negotiatedCodecs.values = GetCodecClones();
std::map<std::string, std::string> formatChanges;
NegotiateCodecs(remote,
&negotiatedCodecs.values,
&formatChanges);
// Use |formatChanges| to update mPrototypeCodecs
size_t insertPos = 0;
for (size_t i = 0; i < mPrototypeCodecs.values.size(); ++i) {
if (formatChanges.count(mPrototypeCodecs.values[i]->mDefaultPt)) {
// Update the payload type to what was negotiated
mPrototypeCodecs.values[i]->mDefaultPt =
formatChanges[mPrototypeCodecs.values[i]->mDefaultPt];
// Move this negotiated codec up front
std::swap(mPrototypeCodecs.values[insertPos],
mPrototypeCodecs.values[i]);
++insertPos;
}
}
EnsureNoDuplicatePayloadTypes(&mPrototypeCodecs.values);
UniquePtr<JsepTrackNegotiatedDetails> negotiatedDetails =
MakeUnique<JsepTrackNegotiatedDetails>();
CreateEncodings(remote, negotiatedCodecs.values, negotiatedDetails.get());
if (answer.GetAttributeList().HasAttribute(SdpAttribute::kExtmapAttribute)) {
for (auto& extmapAttr : answer.GetAttributeList().GetExtmap().mExtmaps) {
negotiatedDetails->mExtmap[extmapAttr.extensionname] = extmapAttr;
}
}
if (mDirection == sdp::kRecv) {
mSsrcs.clear();
if (remote.GetAttributeList().HasAttribute(SdpAttribute::kSsrcAttribute)) {
for (auto& ssrcAttr : remote.GetAttributeList().GetSsrc().mSsrcs) {
AddSsrc(ssrcAttr.ssrc);
}
}
}
mNegotiatedDetails = Move(negotiatedDetails);
}
// When doing bundle, if all else fails we can try to figure out which m-line a
// given RTP packet belongs to by looking at the payload type field. This only
// works, however, if that payload type appeared in only one m-section.
// We figure that out here.
/* static */
void
JsepTrack::SetUniquePayloadTypes(const std::vector<RefPtr<JsepTrack>>& tracks)
{
// Maps to track details if no other track contains the payload type,
// otherwise maps to nullptr.
std::map<uint16_t, JsepTrackNegotiatedDetails*> payloadTypeToDetailsMap;
for (const RefPtr<JsepTrack>& track : tracks) {
if (track->GetMediaType() == SdpMediaSection::kApplication) {
continue;
}
auto* details = track->GetNegotiatedDetails();
if (!details) {
// Can happen if negotiation fails on a track
continue;
}
std::vector<uint16_t> payloadTypesForTrack;
track->GetNegotiatedPayloadTypes(&payloadTypesForTrack);
for (uint16_t pt : payloadTypesForTrack) {
if (payloadTypeToDetailsMap.count(pt)) {
// Found in more than one track, not unique
payloadTypeToDetailsMap[pt] = nullptr;
} else {
payloadTypeToDetailsMap[pt] = details;
}
}
}
for (auto ptAndDetails : payloadTypeToDetailsMap) {
uint16_t uniquePt = ptAndDetails.first;
MOZ_ASSERT(uniquePt <= UINT8_MAX);
auto trackDetails = ptAndDetails.second;
if (trackDetails) {
trackDetails->mUniquePayloadTypes.push_back(
static_cast<uint8_t>(uniquePt));
}
}
}
} // namespace mozilla
|