RTCRtpTransceiver-with-OfferToReceive-options.optional.https.html (5159B)
1 <!doctype html> 2 <meta charset=utf-8> 3 <title>RTCRtpTransceiver with OfferToReceive legacy options</title> 4 <script src="/resources/testharness.js"></script> 5 <script src="/resources/testharnessreport.js"></script> 6 <script src=/resources/testdriver.js></script> 7 <script src=/resources/testdriver-vendor.js></script> 8 <script src='/mediacapture-streams/permission-helper.js'></script> 9 <script src="../RTCPeerConnection-helper.js"></script> 10 <script> 11 'use strict'; 12 13 const stopTracks = (...streams) => { 14 streams.forEach(stream => stream.getTracks().forEach(track => track.stop())); 15 }; 16 17 // comparable() - produces copy of object that is JSON comparable. 18 // o = original object (required) 19 // t = template of what to examine. Useful if o is non-enumerable (optional) 20 21 const comparable = (o, t = o) => { 22 if (typeof o != 'object' || !o) { 23 return o; 24 } 25 if (Array.isArray(t) && Array.isArray(o)) { 26 return o.map((n, i) => comparable(n, t[i])); 27 } 28 return Object.keys(t).sort() 29 .reduce((r, key) => (r[key] = comparable(o[key], t[key]), r), {}); 30 }; 31 32 const stripKeyQuotes = s => s.replace(/"(\w+)":/g, "$1:"); 33 34 const hasProps = (observed, expected) => { 35 const observable = comparable(observed, expected); 36 assert_equals(stripKeyQuotes(JSON.stringify(observable)), 37 stripKeyQuotes(JSON.stringify(comparable(expected)))); 38 }; 39 40 const checkAddTransceiverWithStream = async t => { 41 const pc = new RTCPeerConnection(); 42 t.add_cleanup(() => pc.close()); 43 await setMediaPermission(); 44 const audioStream = await navigator.mediaDevices.getUserMedia({audio: true}); 45 const videoStream = await navigator.mediaDevices.getUserMedia({video: true}); 46 t.add_cleanup(() => stopTracks(audioStream, videoStream)); 47 48 const audio = audioStream.getAudioTracks()[0]; 49 const video = videoStream.getVideoTracks()[0]; 50 51 pc.addTransceiver(audio, {streams: [audioStream]}); 52 pc.addTransceiver(video, {streams: [videoStream]}); 53 54 hasProps(pc.getTransceivers(), 55 [ 56 { 57 receiver: {track: {kind: "audio"}}, 58 sender: {track: audio}, 59 direction: "sendrecv", 60 mid: null, 61 currentDirection: null, 62 stopped: false 63 }, 64 { 65 receiver: {track: {kind: "video"}}, 66 sender: {track: video}, 67 direction: "sendrecv", 68 mid: null, 69 currentDirection: null, 70 stopped: false 71 } 72 ]); 73 74 const offer = await pc.createOffer(); 75 assert_true(offer.sdp.includes("a=msid:" + audioStream.id), 76 "offer contains the expected audio msid"); 77 assert_true(offer.sdp.includes("a=msid:" + videoStream.id), 78 "offer contains the expected video msid"); 79 }; 80 81 const checkAddTransceiverWithOfferToReceive = async (t, kinds) => { 82 const pc = new RTCPeerConnection(); 83 t.add_cleanup(() => pc.close()); 84 85 const propsToSet = kinds.map(kind => { 86 if (kind == "audio") { 87 return "offerToReceiveAudio"; 88 } else if (kind == "video") { 89 return "offerToReceiveVideo"; 90 } 91 }); 92 93 const options = {}; 94 95 for (const prop of propsToSet) { 96 options[prop] = true; 97 } 98 99 let offer = await pc.createOffer(options); 100 101 const expected = []; 102 103 if (options.offerToReceiveAudio) { 104 expected.push( 105 { 106 receiver: {track: {kind: "audio"}}, 107 sender: {track: null}, 108 direction: "recvonly", 109 mid: null, 110 currentDirection: null, 111 stopped: false 112 }); 113 } 114 115 if (options.offerToReceiveVideo) { 116 expected.push( 117 { 118 receiver: {track: {kind: "video"}}, 119 sender: {track: null}, 120 direction: "recvonly", 121 mid: null, 122 currentDirection: null, 123 stopped: false 124 }); 125 } 126 127 hasProps(pc.getTransceivers(), expected); 128 129 // Test offerToReceive: false 130 for (const prop of propsToSet) { 131 options[prop] = false; 132 } 133 134 // Check that sendrecv goes to sendonly 135 for (const transceiver of pc.getTransceivers()) { 136 transceiver.direction = "sendrecv"; 137 } 138 139 for (const transceiverCheck of expected) { 140 transceiverCheck.direction = "sendonly"; 141 } 142 143 offer = await pc.createOffer(options); 144 hasProps(pc.getTransceivers(), expected); 145 146 // Check that recvonly goes to inactive 147 for (const transceiver of pc.getTransceivers()) { 148 transceiver.direction = "recvonly"; 149 } 150 151 for (const transceiverCheck of expected) { 152 transceiverCheck.direction = "inactive"; 153 } 154 155 offer = await pc.createOffer(options); 156 hasProps(pc.getTransceivers(), expected); 157 }; 158 159 const tests = [ 160 checkAddTransceiverWithStream, 161 function checkAddTransceiverWithOfferToReceiveAudio(t) { 162 return checkAddTransceiverWithOfferToReceive(t, ["audio"]); 163 }, 164 function checkAddTransceiverWithOfferToReceiveVideo(t) { 165 return checkAddTransceiverWithOfferToReceive(t, ["video"]); 166 }, 167 function checkAddTransceiverWithOfferToReceiveBoth(t) { 168 return checkAddTransceiverWithOfferToReceive(t, ["audio", "video"]); 169 } 170 ].forEach(test => promise_test(test, test.name)); 171 172 </script>