summaryrefslogtreecommitdiffstats
path: root/dom/media/webrtc/tests/mochitests/sdpUtils.js
blob: 51cae10dba77e053c2ad6b67d3b3a201adf82a76 (plain)
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
/* 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/. */

var sdputils = {
  // Finds the codec id / payload type given a codec format
  // (e.g., "VP8", "VP9/90000"). `offset` tells us which one to use in case of
  // multiple matches.
  findCodecId(sdp, format, offset = 0) {
    let regex = new RegExp("rtpmap:([0-9]+) " + format, "gi");
    let match;
    for (let i = 0; i <= offset; ++i) {
      match = regex.exec(sdp);
      if (!match) {
        throw new Error(
          "Couldn't find offset " +
            i +
            " of codec " +
            format +
            " while looking for offset " +
            offset +
            " in sdp:\n" +
            sdp
        );
      }
    }
    // match[0] is the full matched string
    // match[1] is the first parenthesis group
    return match[1];
  },

  // Returns a list of all payload types, excluding rtx, in an sdp.
  getPayloadTypes(sdp) {
    const regex = /^a=rtpmap:([0-9]+) (?:(?!rtx).)*$/gim;
    const pts = [];
    for (const [line, pt] of sdp.matchAll(regex)) {
      pts.push(pt);
    }
    return pts;
  },

  // Finds all the extmap ids in the given sdp.  Note that this does NOT
  // consider m-sections, so a more generic version would need to
  // look at each m-section separately.
  findExtmapIds(sdp) {
    var sdpExtmapIds = [];
    extmapRegEx = /^a=extmap:([0-9+])/gm;
    // must call exec on the regex to get each match in the string
    while ((searchResults = extmapRegEx.exec(sdp)) !== null) {
      // returned array has the matched text as the first item,
      // and then one item for each capturing parenthesis that
      // matched containing the text that was captured.
      sdpExtmapIds.push(searchResults[1]);
    }
    return sdpExtmapIds;
  },

  findExtmapIdsUrnsDirections(sdp) {
    var sdpExtmap = [];
    extmapRegEx = /^a=extmap:([0-9+])([A-Za-z/]*) ([A-Za-z0-9_:\-\/\.]+)/gm;
    // must call exec on the regex to get each match in the string
    while ((searchResults = extmapRegEx.exec(sdp)) !== null) {
      // returned array has the matched text as the first item,
      // and then one item for each capturing parenthesis that
      // matched containing the text that was captured.
      var idUrn = [];
      idUrn.push(searchResults[1]);
      idUrn.push(searchResults[3]);
      idUrn.push(searchResults[2].slice(1));
      sdpExtmap.push(idUrn);
    }
    return sdpExtmap;
  },

  verify_unique_extmap_ids(sdp) {
    const sdpExtmapIds = sdputils.findExtmapIdsUrnsDirections(sdp);

    return sdpExtmapIds.reduce(function (result, item, index) {
      const [id, urn, dir] = item;
      ok(
        !(id in result) || (result[id][0] === urn && result[id][1] === dir),
        "ID " + id + " is unique ID for " + urn + " and direction " + dir
      );
      result[id] = [urn, dir];
      return result;
    }, {});
  },

  getMSections(sdp) {
    return sdp
      .split(new RegExp("^m=", "gm"))
      .slice(1)
      .map(s => "m=" + s);
  },

  getAudioMSections(sdp) {
    return this.getMSections(sdp).filter(section =>
      section.startsWith("m=audio")
    );
  },

  getVideoMSections(sdp) {
    return this.getMSections(sdp).filter(section =>
      section.startsWith("m=video")
    );
  },

  checkSdpAfterEndOfTrickle(description, testOptions, label) {
    info("EOC-SDP: " + JSON.stringify(description));

    const checkForTransportAttributes = msection => {
      info("Checking msection: " + msection);
      ok(
        msection.includes("a=end-of-candidates"),
        label + ": SDP contains end-of-candidates"
      );

      if (!msection.startsWith("m=application")) {
        if (testOptions.rtcpmux) {
          ok(
            msection.includes("a=rtcp-mux"),
            label + ": SDP contains rtcp-mux"
          );
        } else {
          ok(msection.includes("a=rtcp:"), label + ": SDP contains rtcp port");
        }
      }
    };

    const hasOwnTransport = msection => {
      const port0Check = new RegExp(/^m=\S+ 0 /).exec(msection);
      if (port0Check) {
        return false;
      }
      const midMatch = new RegExp(/\r\na=mid:(\S+)/).exec(msection);
      if (!midMatch) {
        return true;
      }
      const mid = midMatch[1];
      const bundleGroupMatch = new RegExp(
        "\\r\\na=group:BUNDLE \\S.* " + mid + "\\s+"
      ).exec(description.sdp);
      return bundleGroupMatch == null;
    };

    const msectionsWithOwnTransports = this.getMSections(
      description.sdp
    ).filter(hasOwnTransport);

    ok(
      msectionsWithOwnTransports.length,
      "SDP should contain at least one msection with a transport"
    );
    msectionsWithOwnTransports.forEach(checkForTransportAttributes);

    if (testOptions.ssrc) {
      ok(description.sdp.includes("a=ssrc"), label + ": SDP contains a=ssrc");
    } else {
      ok(
        !description.sdp.includes("a=ssrc"),
        label + ": SDP does not contain a=ssrc"
      );
    }
  },

  // Note, we don't bother removing the fmtp lines, which makes a good test
  // for some SDP parsing issues.
  removeCodec(sdp, codec) {
    var updated_sdp = sdp.replace(
      new RegExp("a=rtpmap:" + codec + ".*\\/90000\\r\\n", ""),
      ""
    );
    updated_sdp = updated_sdp.replace(
      new RegExp("(RTP\\/SAVPF.*)( " + codec + ")(.*\\r\\n)", ""),
      "$1$3"
    );
    updated_sdp = updated_sdp.replace(
      new RegExp("a=rtcp-fb:" + codec + " nack\\r\\n", ""),
      ""
    );
    updated_sdp = updated_sdp.replace(
      new RegExp("a=rtcp-fb:" + codec + " nack pli\\r\\n", ""),
      ""
    );
    updated_sdp = updated_sdp.replace(
      new RegExp("a=rtcp-fb:" + codec + " ccm fir\\r\\n", ""),
      ""
    );
    return updated_sdp;
  },

  removeAllButPayloadType(sdp, pt) {
    return sdp.replace(
      new RegExp("m=(\\w+ \\w+) UDP/TLS/RTP/SAVPF .*" + pt + ".*\\r\\n", "gi"),
      "m=$1 UDP/TLS/RTP/SAVPF " + pt + "\r\n"
    );
  },

  removeRtpMapForPayloadType(sdp, pt) {
    return sdp.replace(new RegExp("a=rtpmap:" + pt + ".*\\r\\n", "gi"), "");
  },

  removeRtcpMux(sdp) {
    return sdp.replace(/a=rtcp-mux\r\n/g, "");
  },

  removeSSRCs(sdp) {
    return sdp.replace(/a=ssrc.*\r\n/g, "");
  },

  removeBundle(sdp) {
    return sdp.replace(/a=group:BUNDLE .*\r\n/g, "");
  },

  reduceAudioMLineToPcmuPcma(sdp) {
    return sdp.replace(
      /m=audio .*\r\n/g,
      "m=audio 9 UDP/TLS/RTP/SAVPF 0 8\r\n"
    );
  },

  setAllMsectionsInactive(sdp) {
    return sdp
      .replace(/\r\na=sendrecv/g, "\r\na=inactive")
      .replace(/\r\na=sendonly/g, "\r\na=inactive")
      .replace(/\r\na=recvonly/g, "\r\na=inactive");
  },

  removeAllRtpMaps(sdp) {
    return sdp.replace(/a=rtpmap:.*\r\n/g, "");
  },

  reduceAudioMLineToDynamicPtAndOpus(sdp) {
    return sdp.replace(
      /m=audio .*\r\n/g,
      "m=audio 9 UDP/TLS/RTP/SAVPF 101 109\r\n"
    );
  },

  addTiasBps(sdp, bps) {
    return sdp.replace(/c=IN (.*)\r\n/g, "c=IN $1\r\nb=TIAS:" + bps + "\r\n");
  },

  removeSimulcastProperties(sdp) {
    return sdp
      .replace(/a=simulcast:.*\r\n/g, "")
      .replace(/a=rid:.*\r\n/g, "")
      .replace(
        /a=extmap:[^\s]* urn:ietf:params:rtp-hdrext:sdes:rtp-stream-id.*\r\n/g,
        ""
      )
      .replace(
        /a=extmap:[^\s]* urn:ietf:params:rtp-hdrext:sdes:repaired-rtp-stream-id.*\r\n/g,
        ""
      );
  },

  transferSimulcastProperties(offer_sdp, answer_sdp) {
    if (!offer_sdp.includes("a=simulcast:")) {
      return answer_sdp;
    }
    ok(
      offer_sdp.includes("a=simulcast:send "),
      "Offer contains simulcast attribute"
    );
    var o_simul = offer_sdp.match(/simulcast:send (.*)([\n$])*/i);
    var new_answer_sdp = answer_sdp + "a=simulcast:recv " + o_simul[1] + "\r\n";
    ok(offer_sdp.includes("a=rid:"), "Offer contains RID attribute");
    var o_rids = offer_sdp.match(/a=rid:(.*)/gi);
    o_rids.forEach(o_rid => {
      new_answer_sdp = new_answer_sdp + o_rid.replace(/send/, "recv") + "\r\n";
    });
    var extmap_id = offer_sdp.match(
      "a=extmap:([0-9+])/sendonly urn:ietf:params:rtp-hdrext:sdes:rtp-stream-id"
    );
    ok(extmap_id != null, "Offer contains RID RTP header extension");
    new_answer_sdp =
      new_answer_sdp +
      "a=extmap:" +
      extmap_id[1] +
      "/recvonly urn:ietf:params:rtp-hdrext:sdes:rtp-stream-id\r\n";
    var extmap_id = offer_sdp.match(
      "a=extmap:([0-9+])/sendonly urn:ietf:params:rtp-hdrext:sdes:repaired-rtp-stream-id"
    );
    if (extmap_id != null) {
      new_answer_sdp =
        new_answer_sdp +
        "a=extmap:" +
        extmap_id[1] +
        "/recvonly urn:ietf:params:rtp-hdrext:sdes:repaired-rtp-stream-id\r\n";
    }

    return new_answer_sdp;
  },

  verifySdp(
    desc,
    expectedType,
    offerConstraintsList,
    offerOptions,
    testOptions
  ) {
    info("Examining this SessionDescription: " + JSON.stringify(desc));
    info("offerConstraintsList: " + JSON.stringify(offerConstraintsList));
    info("offerOptions: " + JSON.stringify(offerOptions));
    ok(desc, "SessionDescription is not null");
    is(desc.type, expectedType, "SessionDescription type is " + expectedType);
    ok(desc.sdp.length > 10, "SessionDescription body length is plausible");
    ok(desc.sdp.includes("a=ice-ufrag"), "ICE username is present in SDP");
    ok(desc.sdp.includes("a=ice-pwd"), "ICE password is present in SDP");
    ok(desc.sdp.includes("a=fingerprint"), "ICE fingerprint is present in SDP");
    //TODO: update this for loopback support bug 1027350
    ok(
      !desc.sdp.includes(LOOPBACK_ADDR),
      "loopback interface is absent from SDP"
    );
    var requiresTrickleIce = !desc.sdp.includes("a=candidate");
    if (requiresTrickleIce) {
      info("No ICE candidate in SDP -> requiring trickle ICE");
    } else {
      info("at least one ICE candidate is present in SDP");
    }

    //TODO: how can we check for absence/presence of m=application?

    var audioTracks =
      sdputils.countTracksInConstraint("audio", offerConstraintsList) ||
      (offerOptions && offerOptions.offerToReceiveAudio ? 1 : 0);

    info("expected audio tracks: " + audioTracks);
    if (audioTracks == 0) {
      ok(!desc.sdp.includes("m=audio"), "audio m-line is absent from SDP");
    } else {
      ok(desc.sdp.includes("m=audio"), "audio m-line is present in SDP");
      is(
        testOptions.opus,
        desc.sdp.includes("a=rtpmap:109 opus/48000/2"),
        "OPUS codec is present in SDP"
      );
      //TODO: ideally the rtcp-mux should be for the m=audio, and not just
      //      anywhere in the SDP (JS SDP parser bug 1045429)
      is(
        testOptions.rtcpmux,
        desc.sdp.includes("a=rtcp-mux"),
        "RTCP Mux is offered in SDP"
      );
    }

    var videoTracks =
      sdputils.countTracksInConstraint("video", offerConstraintsList) ||
      (offerOptions && offerOptions.offerToReceiveVideo ? 1 : 0);

    info("expected video tracks: " + videoTracks);
    if (videoTracks == 0) {
      ok(!desc.sdp.includes("m=video"), "video m-line is absent from SDP");
    } else {
      ok(desc.sdp.includes("m=video"), "video m-line is present in SDP");
      if (testOptions.h264) {
        ok(
          desc.sdp.includes("a=rtpmap:126 H264/90000") ||
            desc.sdp.includes("a=rtpmap:97 H264/90000"),
          "H.264 codec is present in SDP"
        );
      } else {
        ok(
          desc.sdp.includes("a=rtpmap:120 VP8/90000") ||
            desc.sdp.includes("a=rtpmap:121 VP9/90000"),
          "VP8 or VP9 codec is present in SDP"
        );
      }
      is(
        testOptions.rtcpmux,
        desc.sdp.includes("a=rtcp-mux"),
        "RTCP Mux is offered in SDP"
      );
      is(
        testOptions.ssrc,
        desc.sdp.includes("a=ssrc"),
        "a=ssrc signaled in SDP"
      );
    }

    return requiresTrickleIce;
  },

  /**
   * Counts the amount of audio tracks in a given media constraint.
   *
   * @param constraints
   *        The contraint to be examined.
   */
  countTracksInConstraint(type, constraints) {
    if (!Array.isArray(constraints)) {
      return 0;
    }
    return constraints.reduce((sum, c) => sum + (c[type] ? 1 : 0), 0);
  },
};