|
@@ -0,0 +1,317 @@
|
|
1
|
+import MediaDirection from '../../service/RTC/MediaDirection';
|
|
2
|
+import * as MediaType from '../../service/RTC/MediaType';
|
|
3
|
+
|
|
4
|
+import * as transform from 'sdp-transform';
|
|
5
|
+
|
|
6
|
+const DEFAULT_NUM_OF_LAYERS = 3;
|
|
7
|
+
|
|
8
|
+interface Description {
|
|
9
|
+ type: RTCSdpType;
|
|
10
|
+ sdp: string;
|
|
11
|
+}
|
|
12
|
+
|
|
13
|
+interface Options {
|
|
14
|
+ numOfLayers?: number
|
|
15
|
+}
|
|
16
|
+
|
|
17
|
+/**
|
|
18
|
+ * This class handles SDP munging for enabling simulcast for local video streams in Unified plan. A set of random SSRCs
|
|
19
|
+ * are generated for the higher layer streams and they are cached for a given mid. The cached SSRCs are then reused on
|
|
20
|
+ * the subsequent iterations while munging the local description. This class also handles imploding of the simulcast
|
|
21
|
+ * SSRCs for remote endpoints into the primary FID group in remote description since Jicofo signals all SSRCs relevant
|
|
22
|
+ * to a given endpoint.
|
|
23
|
+ */
|
|
24
|
+export default class SdpSimulcast {
|
|
25
|
+ private _options: Options;
|
|
26
|
+ private _ssrcCache: Map<string, Array<number>>;
|
|
27
|
+
|
|
28
|
+ /**
|
|
29
|
+ * Creates a new instance.
|
|
30
|
+ *
|
|
31
|
+ * @param options
|
|
32
|
+ */
|
|
33
|
+ constructor(options: Options) {
|
|
34
|
+ this._options = options;
|
|
35
|
+ this._ssrcCache = new Map();
|
|
36
|
+
|
|
37
|
+ if (!this._options.numOfLayers) {
|
|
38
|
+ this._options.numOfLayers = DEFAULT_NUM_OF_LAYERS;
|
|
39
|
+ }
|
|
40
|
+ }
|
|
41
|
+
|
|
42
|
+ /**
|
|
43
|
+ * Updates the given media description using the SSRCs that were cached for the mid associated
|
|
44
|
+ * with the media description and returns the modified media description.
|
|
45
|
+ *
|
|
46
|
+ * @param mLine
|
|
47
|
+ * @returns
|
|
48
|
+ */
|
|
49
|
+ _fillSsrcsFromCache(mLine: transform.MediaDescription) : any {
|
|
50
|
+ const mid = mLine.mid;
|
|
51
|
+ const cachedSsrcs = this._ssrcCache.get(mid);
|
|
52
|
+ const newSsrcs = this._parseSimLayers(mLine);
|
|
53
|
+ const newMsid = this._getSsrcAttribute(mLine, newSsrcs[0], 'msid');
|
|
54
|
+ const newCname = this._getSsrcAttribute(mLine, newSsrcs[0], 'cname');
|
|
55
|
+
|
|
56
|
+ mLine.ssrcs = [];
|
|
57
|
+ mLine.ssrcGroups = [];
|
|
58
|
+
|
|
59
|
+ for (const ssrc of cachedSsrcs) {
|
|
60
|
+ mLine.ssrcs.push({
|
|
61
|
+ id: ssrc,
|
|
62
|
+ attribute: 'msid',
|
|
63
|
+ value: newMsid
|
|
64
|
+ });
|
|
65
|
+ mLine.ssrcs.push({
|
|
66
|
+ id: ssrc,
|
|
67
|
+ attribute: 'cname',
|
|
68
|
+ value: newCname
|
|
69
|
+ });
|
|
70
|
+ }
|
|
71
|
+
|
|
72
|
+ mLine.ssrcGroups.push({
|
|
73
|
+ semantics: 'SIM',
|
|
74
|
+ ssrcs: cachedSsrcs.join(' ')
|
|
75
|
+ });
|
|
76
|
+
|
|
77
|
+ return mLine;
|
|
78
|
+ }
|
|
79
|
+
|
|
80
|
+ /**
|
|
81
|
+ * Generates a new set of SSRCs for the higher simulcast layers/streams and adds the attributes and SIM group to
|
|
82
|
+ * the given media description and returns the modified media description.
|
|
83
|
+ *
|
|
84
|
+ * @param mLine
|
|
85
|
+ * @param primarySsrc
|
|
86
|
+ * @returns
|
|
87
|
+ */
|
|
88
|
+ _generateNewSsrcsForSimulcast(mLine: transform.MediaDescription, primarySsrc: number) : any {
|
|
89
|
+ const cname = this._getSsrcAttribute(mLine, primarySsrc, 'cname');
|
|
90
|
+ let msid = this._getSsrcAttribute(mLine, primarySsrc, 'msid');
|
|
91
|
+ const addAssociatedAttributes = (mLine: transform.MediaDescription, ssrc: number) => {
|
|
92
|
+ mLine.ssrcs.push({
|
|
93
|
+ id: ssrc,
|
|
94
|
+ attribute: 'cname',
|
|
95
|
+ value: cname
|
|
96
|
+ });
|
|
97
|
+ mLine.ssrcs.push({
|
|
98
|
+ id: ssrc,
|
|
99
|
+ attribute: 'msid',
|
|
100
|
+ value: msid
|
|
101
|
+ });
|
|
102
|
+ }
|
|
103
|
+
|
|
104
|
+ // In Unified-plan mode, the a=ssrc lines with the msid attribute are not present (only cname attributes are
|
|
105
|
+ // present) in the answers that Chrome and Safari generate for an offer received from Jicofo. Generate these
|
|
106
|
+ // a=ssrc lines using the msid values from the a=msid line.
|
|
107
|
+ if (!msid) {
|
|
108
|
+ msid = mLine.msid;
|
|
109
|
+ const primarySsrcs = mLine.ssrcs;
|
|
110
|
+
|
|
111
|
+ primarySsrcs.forEach(ssrc => {
|
|
112
|
+ mLine.ssrcs.push({
|
|
113
|
+ id: ssrc.id,
|
|
114
|
+ attribute: 'msid',
|
|
115
|
+ value: msid
|
|
116
|
+ });
|
|
117
|
+ })
|
|
118
|
+ }
|
|
119
|
+
|
|
120
|
+ // Generate SIM layers.
|
|
121
|
+ const simSsrcs = [];
|
|
122
|
+
|
|
123
|
+ for (let i = 0; i < this._options.numOfLayers - 1; ++i) {
|
|
124
|
+ const simSsrc = this._generateSsrc();
|
|
125
|
+
|
|
126
|
+ addAssociatedAttributes(mLine, simSsrc);
|
|
127
|
+ simSsrcs.push(simSsrc);
|
|
128
|
+ }
|
|
129
|
+
|
|
130
|
+ mLine.ssrcGroups = mLine.ssrcGroups || [];
|
|
131
|
+ mLine.ssrcGroups.push({
|
|
132
|
+ semantics: 'SIM',
|
|
133
|
+ ssrcs: primarySsrc + ' ' + simSsrcs.join(' ')
|
|
134
|
+ });
|
|
135
|
+
|
|
136
|
+ return mLine;
|
|
137
|
+ }
|
|
138
|
+
|
|
139
|
+ /**
|
|
140
|
+ * Returns a random number to be used for the SSRC.
|
|
141
|
+ *
|
|
142
|
+ * @returns
|
|
143
|
+ */
|
|
144
|
+ _generateSsrc() : number {
|
|
145
|
+ const max = 0xffffffff;
|
|
146
|
+
|
|
147
|
+ return Math.floor(Math.random() * max);
|
|
148
|
+ }
|
|
149
|
+
|
|
150
|
+ /**
|
|
151
|
+ * Returns the requested attribute value for a SSRC from a given media description.
|
|
152
|
+ *
|
|
153
|
+ * @param mLine
|
|
154
|
+ * @param ssrc
|
|
155
|
+ * @param attributeName
|
|
156
|
+ * @returns
|
|
157
|
+ */
|
|
158
|
+ _getSsrcAttribute(mLine: transform.MediaDescription, ssrc: number, attributeName: string) : string | undefined {
|
|
159
|
+ return mLine.ssrcs?.find(
|
|
160
|
+ ssrcInfo => Number(ssrcInfo.id) === ssrc
|
|
161
|
+ && ssrcInfo.attribute === attributeName)?.value;
|
|
162
|
+ }
|
|
163
|
+
|
|
164
|
+ /**
|
|
165
|
+ * Returns an array of all the primary SSRCs in the SIM group for a given media description.
|
|
166
|
+ *
|
|
167
|
+ * @param mLine
|
|
168
|
+ * @returns
|
|
169
|
+ */
|
|
170
|
+ _parseSimLayers(mLine: transform.MediaDescription) : Array<number> | null {
|
|
171
|
+ const simGroup = mLine.ssrcGroups?.find(group => group.semantics === 'SIM');
|
|
172
|
+
|
|
173
|
+ if (simGroup) {
|
|
174
|
+ return simGroup.ssrcs.split(' ').map(ssrc => Number(ssrc));
|
|
175
|
+ }
|
|
176
|
+
|
|
177
|
+ if (mLine.ssrcs?.length) {
|
|
178
|
+ return [ Number(mLine.ssrcs[0].id) ];
|
|
179
|
+ }
|
|
180
|
+
|
|
181
|
+ return null;
|
|
182
|
+ }
|
|
183
|
+
|
|
184
|
+ /**
|
|
185
|
+ * Munges the given media description to enable simulcast for the video media sections that are in either have
|
|
186
|
+ * SENDRECV or SENDONLY as the media direction thereby ignoring all the RECVONLY transceivers created for remote
|
|
187
|
+ * endpoints.
|
|
188
|
+ * NOTE: This needs to be called only when simulcast is enabled.
|
|
189
|
+ *
|
|
190
|
+ * @param description
|
|
191
|
+ * @returns
|
|
192
|
+ */
|
|
193
|
+ mungeLocalDescription(description: Description) : Description {
|
|
194
|
+ if (!description || !description.sdp) {
|
|
195
|
+ return description;
|
|
196
|
+ }
|
|
197
|
+ const session = transform.parse(description.sdp);
|
|
198
|
+
|
|
199
|
+ for (let media of session.media) {
|
|
200
|
+ // Ignore recvonly and inactive transceivers created for remote sources.
|
|
201
|
+ if (media.direction === MediaDirection.RECVONLY || media.direction === MediaDirection.INACTIVE) {
|
|
202
|
+ continue;
|
|
203
|
+ }
|
|
204
|
+
|
|
205
|
+ // Ignore audio m-lines.
|
|
206
|
+ if (media.type !== MediaType.VIDEO) {
|
|
207
|
+ continue;
|
|
208
|
+ }
|
|
209
|
+ const mid = media.mid;
|
|
210
|
+ const numSsrcs = new Set(media.ssrcs?.map(ssrcInfo => ssrcInfo.id));
|
|
211
|
+ const numGroups = media.ssrcGroups?.length ?? 0;
|
|
212
|
+ let primarySsrc: number;
|
|
213
|
+
|
|
214
|
+ // Do not munge if the description has no ssrcs or if simulcast is already enabled.
|
|
215
|
+ if (numSsrcs.size === 0 || numSsrcs.size > 2 || (numSsrcs.size === 2 && numGroups === 0)) {
|
|
216
|
+ continue;
|
|
217
|
+ }
|
|
218
|
+ if (numSsrcs.size === 1) {
|
|
219
|
+ primarySsrc = Number(media.ssrcs[0]?.id);
|
|
220
|
+ } else {
|
|
221
|
+ const fidGroup = media.ssrcGroups.find(group => group.semantics === 'FID');
|
|
222
|
+
|
|
223
|
+ if (fidGroup) {
|
|
224
|
+ primarySsrc = Number(fidGroup.ssrcs.split(' ')[0]);
|
|
225
|
+ }
|
|
226
|
+ }
|
|
227
|
+
|
|
228
|
+ if (this._ssrcCache.has(mid)) {
|
|
229
|
+ media = this._fillSsrcsFromCache(media);
|
|
230
|
+ } else {
|
|
231
|
+ media = this._generateNewSsrcsForSimulcast(media, primarySsrc);
|
|
232
|
+ const simulcastSsrcs = this._parseSimLayers(media);
|
|
233
|
+
|
|
234
|
+ // Update the SSRCs in the cache so that they can re-used for the same mid again.
|
|
235
|
+ this._ssrcCache.set(mid, simulcastSsrcs);
|
|
236
|
+ }
|
|
237
|
+ }
|
|
238
|
+
|
|
239
|
+ return new RTCSessionDescription({
|
|
240
|
+ type: description.type,
|
|
241
|
+ sdp: transform.write(session)
|
|
242
|
+ });
|
|
243
|
+ }
|
|
244
|
+
|
|
245
|
+ /**
|
|
246
|
+ * Munges the given media description by removing the SSRCs and related FID groups for the higher layer streams.
|
|
247
|
+ *
|
|
248
|
+ * @param description
|
|
249
|
+ * @returns
|
|
250
|
+ */
|
|
251
|
+ mungeRemoteDescription(description: Description) : Description {
|
|
252
|
+ if (!description || !description.sdp) {
|
|
253
|
+ return description;
|
|
254
|
+ }
|
|
255
|
+
|
|
256
|
+ const session = transform.parse(description.sdp);
|
|
257
|
+
|
|
258
|
+ for (const media of session.media) {
|
|
259
|
+ if (media.type !== MediaType.VIDEO) {
|
|
260
|
+ continue;
|
|
261
|
+ }
|
|
262
|
+
|
|
263
|
+ if (media.direction !== MediaDirection.SENDONLY) {
|
|
264
|
+ continue;
|
|
265
|
+ }
|
|
266
|
+
|
|
267
|
+ // Ignore m-lines that do not have any SSRCs or SSRC groups. These are the ones associated with remote
|
|
268
|
+ // sources that have left the call. These will be recycled when a new remote source joins the call.
|
|
269
|
+ if (!media.ssrcGroups?.length || !media?.ssrcs.length) {
|
|
270
|
+ continue;
|
|
271
|
+ }
|
|
272
|
+
|
|
273
|
+ // Cache the SSRCs and the source groups.
|
|
274
|
+ const mungedSsrcs = new Set(media.ssrcs.slice());
|
|
275
|
+ const mungedSsrcGroups = new Set(media.ssrcGroups.slice());
|
|
276
|
+ const fidGroups = media.ssrcGroups.filter(group => group.semantics === 'FID');
|
|
277
|
+ const simGroup = media.ssrcGroups.find(group => group.semantics === 'SIM');
|
|
278
|
+ const primarySsrc = simGroup?.ssrcs.split(' ')[0];;
|
|
279
|
+
|
|
280
|
+ // When simulcast and RTX are both enabled.
|
|
281
|
+ if (fidGroups.length && simGroup) {
|
|
282
|
+ const fidGroup = fidGroups.find(group => group.ssrcs.includes(primarySsrc));
|
|
283
|
+ const secondarySsrc = fidGroup.ssrcs.split(' ')[1];
|
|
284
|
+
|
|
285
|
+ for (const ssrcGroup of media.ssrcGroups) {
|
|
286
|
+ if (ssrcGroup !== fidGroup) {
|
|
287
|
+ mungedSsrcGroups.delete(ssrcGroup);
|
|
288
|
+ }
|
|
289
|
+ }
|
|
290
|
+ for (const ssrc of media.ssrcs) {
|
|
291
|
+ if (ssrc.id.toString() !== primarySsrc
|
|
292
|
+ && ssrc.id.toString() !== secondarySsrc) {
|
|
293
|
+ mungedSsrcs.delete(ssrc);
|
|
294
|
+ }
|
|
295
|
+ }
|
|
296
|
+
|
|
297
|
+ // When simulcast is enabled but RTX is disabled.
|
|
298
|
+ } else if (simGroup) {
|
|
299
|
+ mungedSsrcGroups.delete(simGroup);
|
|
300
|
+
|
|
301
|
+ for (const ssrc of media.ssrcs) {
|
|
302
|
+ if (ssrc.id.toString() !== primarySsrc) {
|
|
303
|
+ mungedSsrcs.delete(ssrc);
|
|
304
|
+ }
|
|
305
|
+ }
|
|
306
|
+ }
|
|
307
|
+
|
|
308
|
+ media.ssrcs = Array.from(mungedSsrcs);
|
|
309
|
+ media.ssrcGroups = Array.from(mungedSsrcGroups);
|
|
310
|
+ }
|
|
311
|
+
|
|
312
|
+ return new RTCSessionDescription ({
|
|
313
|
+ type: description.type,
|
|
314
|
+ sdp: transform.write(session)
|
|
315
|
+ });
|
|
316
|
+ }
|
|
317
|
+}
|