summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webrtc/legacy/RTCPeerConnection-createOffer-offerToReceive.html
blob: f710498e75f1be587c66d1d0dfe215cb136cc747 (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
<!doctype html>
<meta charset=utf-8>
<title>Test legacy offerToReceiveAudio/Video options</title>
<link rel="help" href="https://w3c.github.io/webrtc-pc/#legacy-configuration-extensions">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="../RTCPeerConnection-helper.js"></script>
<script>
  'use strict';

  /*
   *  4.3.3.2 Configuration data extensions
   *  partial dictionary RTCOfferOptions
   */

  /*
   *  offerToReceiveAudio of type boolean
   *    When this is given a non-false value, no outgoing track of type
   *    "audio" is attached to the PeerConnection, and the existing
   *    localDescription (if any) doesn't contain any sendrecv or recv
   *    audio media sections, createOffer() will behave as if
   *    addTransceiver("audio") had been called once prior to the createOffer() call.
   */
  promise_test(t => {
    const pc = new RTCPeerConnection();

    t.add_cleanup(() => pc.close());

    return pc.createOffer({ offerToReceiveAudio: true })
    .then(offer1 => {
      assert_equals(countAudioLine(offer1.sdp), 1,
        'Expect created offer to have audio line');

      // The first createOffer implicitly calls addTransceiver('audio'),
      // so all following offers will also have audio media section
      // in their SDP.
      return pc.createOffer({ offerToReceiveAudio: false })
      .then(offer2 => {
        assert_equals(countAudioLine(offer2.sdp), 1,
          'Expect audio line to remain in created offer');
      })
    });
  }, 'createOffer() with offerToReceiveAudio should add audio line to all subsequent created offers');

  /*
   *  offerToReceiveVideo of type boolean
   *    When this is given a non-false value, and no outgoing track
   *    of type "video" is attached to the PeerConnection, and the
   *    existing localDescription (if any) doesn't contain any sendecv
   *    or recv video media sections, createOffer() will behave as if
   *    addTransceiver("video") had been called prior to the createOffer() call.
   */
  promise_test(t => {
    const pc = new RTCPeerConnection();

    t.add_cleanup(() => pc.close());

    return pc.createOffer({ offerToReceiveVideo: true })
    .then(offer1 => {
      assert_equals(countVideoLine(offer1.sdp), 1,
      'Expect created offer to have video line');

      return pc.createOffer({ offerToReceiveVideo: false })
      .then(offer2 => {
        assert_equals(countVideoLine(offer2.sdp), 1,
          'Expect video line to remain in created offer');
      })
    });
  }, 'createOffer() with offerToReceiveVideo should add video line to all subsequent created offers');

  promise_test(t => {
    const pc = new RTCPeerConnection();

    t.add_cleanup(() => pc.close());

    return pc.createOffer({
      offerToReceiveAudio: true,
      offerToReceiveVideo: false
    }).then(offer1 => {
      assert_equals(countAudioLine(offer1.sdp), 1,
        'Expect audio line to be found in created offer');

      assert_equals(countVideoLine(offer1.sdp), 0,
        'Expect video line to not be found in create offer');

      return pc.createOffer({
        offerToReceiveAudio: false,
        offerToReceiveVideo: true
      }).then(offer2 => {
        assert_equals(countAudioLine(offer2.sdp), 1,
          'Expect audio line to remain in created offer');

        assert_equals(countVideoLine(offer2.sdp), 1,
          'Expect video line to be found in create offer');
      })
    });
  }, 'createOffer() with offerToReceiveAudio:true, then with offerToReceiveVideo:true, should have result offer with both audio and video line');


  // Run some tests for both audio and video kinds
  ['audio', 'video'].forEach((kind) => {
    const capsKind = kind[0].toUpperCase() + kind.slice(1);

    const offerToReceiveTrue = {};
    offerToReceiveTrue[`offerToReceive${capsKind}`] = true;

    const offerToReceiveFalse = {};
    offerToReceiveFalse[`offerToReceive${capsKind}`] = false;

    // Start testing
    promise_test(t => {
      const pc = new RTCPeerConnection();
      t.add_cleanup(() => pc.close());
      const dummy = pc.createDataChannel('foo'); // Just to have something to offer

      return pc.createOffer(offerToReceiveFalse)
      .then(() => {
        assert_equals(pc.getTransceivers().length, 0,
          'Expect pc to have no transceivers');
      });
    }, `createOffer() with offerToReceive${capsKind} set to false should not create a transceiver`);

    promise_test(t => {
      const pc = new RTCPeerConnection();

      t.add_cleanup(() => pc.close());

      return pc.createOffer(offerToReceiveTrue)
      .then(() => {
        assert_equals(pc.getTransceivers().length, 1,
          'Expect pc to have one transceiver');

        const transceiver = pc.getTransceivers()[0];
        assert_equals(transceiver.direction, 'recvonly',
          'Expect transceiver to have "recvonly" direction');
      });
    }, `createOffer() with offerToReceive${capsKind} should create a "recvonly" transceiver`);

    promise_test(t => {
      const pc = new RTCPeerConnection();

      t.add_cleanup(() => pc.close());

      return pc.createOffer(offerToReceiveTrue)
      .then(() => {
        assert_equals(pc.getTransceivers().length, 1,
          'Expect pc to have one transceiver');

        const transceiver = pc.getTransceivers()[0];
        assert_equals(transceiver.direction, 'recvonly',
          'Expect transceiver to have "recvonly" direction');
      })
      .then(() => pc.createOffer(offerToReceiveTrue))
      .then(() => {
        assert_equals(pc.getTransceivers().length, 1,
          'Expect pc to still have only one transceiver');
      })
      ;
    }, `offerToReceive${capsKind} option should be ignored if a non-stopped "recvonly" transceiver exists`);

    promise_test(t => {
      const pc = new RTCPeerConnection();

      t.add_cleanup(() => pc.close());

      return getTrackFromUserMedia(kind)
      .then(([track, stream]) => {
        pc.addTrack(track, stream);
        return pc.createOffer();
      })
      .then(() => {
        assert_equals(pc.getTransceivers().length, 1,
          'Expect pc to have one transceiver');

        const transceiver = pc.getTransceivers()[0];
        assert_equals(transceiver.direction, 'sendrecv',
          'Expect transceiver to have "sendrecv" direction');
      })
      .then(() => pc.createOffer(offerToReceiveTrue))
      .then(() => {
        assert_equals(pc.getTransceivers().length, 1,
          'Expect pc to still have only one transceiver');
      })
      ;
    }, `offerToReceive${capsKind} option should be ignored if a non-stopped "sendrecv" transceiver exists`);

    promise_test(t => {
      const pc = new RTCPeerConnection();

      t.add_cleanup(() => pc.close());

      return getTrackFromUserMedia(kind)
      .then(([track, stream]) => {
        pc.addTrack(track, stream);
        return pc.createOffer(offerToReceiveFalse);
      })
      .then(() => {
        assert_equals(pc.getTransceivers().length, 1,
          'Expect pc to have one transceiver');

        const transceiver = pc.getTransceivers()[0];
        assert_equals(transceiver.direction, 'sendonly',
          'Expect transceiver to have "sendonly" direction');
      })
      ;
    }, `offerToReceive${capsKind} set to false with a track should create a "sendonly" transceiver`);

    promise_test(t => {
      const pc = new RTCPeerConnection();

      t.add_cleanup(() => pc.close());

      pc.addTransceiver(kind, {direction: 'recvonly'});

      return pc.createOffer(offerToReceiveFalse)
      .then(() => {
        assert_equals(pc.getTransceivers().length, 1,
          'Expect pc to have one transceiver');

        const transceiver = pc.getTransceivers()[0];
        assert_equals(transceiver.direction, 'inactive',
          'Expect transceiver to have "inactive" direction');
      })
      ;
    }, `offerToReceive${capsKind} set to false with a "recvonly" transceiver should change the direction to "inactive"`);

    promise_test(t => {
      const pc = new RTCPeerConnection();
      t.add_cleanup(() => pc.close());
      const pc2 = new RTCPeerConnection();

      t.add_cleanup(() => pc2.close());

      return getTrackFromUserMedia(kind)
      .then(([track, stream]) => {
        pc.addTrack(track, stream);
        return pc.createOffer();
      })
      .then((offer) => pc.setLocalDescription(offer))
      .then(() => pc2.setRemoteDescription(pc.localDescription))
      .then(() => pc2.createAnswer())
      .then((answer) => pc2.setLocalDescription(answer))
      .then(() => pc.setRemoteDescription(pc2.localDescription))
      .then(() => pc.createOffer(offerToReceiveFalse))
      .then((offer) => {
        assert_equals(pc.getTransceivers().length, 1,
          'Expect pc to have one transceiver');

        const transceiver = pc.getTransceivers()[0];
        assert_equals(transceiver.direction, 'sendonly',
          'Expect transceiver to have "sendonly" direction');
      })
      ;
    }, `subsequent offerToReceive${capsKind} set to false with a track should change the direction to "sendonly"`);
  });

  promise_test(t => {
    const pc = new RTCPeerConnection();

    t.add_cleanup(() => pc.close());

    return pc.createOffer({ offerToReceiveAudio: true, offerToReceiveVideo: true })
    .then(() => {
      assert_equals(pc.getTransceivers().length, 2,
        'Expect pc to have two transceivers');

      assert_equals(pc.getTransceivers()[0].direction, 'recvonly',
        'Expect first transceiver to have "recvonly" direction');
      assert_equals(pc.getTransceivers()[1].direction, 'recvonly',
        'Expect second transceiver to have "recvonly" direction');
    });
  }, 'offerToReceiveAudio and Video should create two "recvonly" transceivers');

</script>