test_peerConnection_removeThenAddAudioTrack.html (3426B)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <script type="application/javascript" src="pc.js"></script> 5 </head> 6 <body> 7 <pre id="test"> 8 <script type="application/javascript"> 9 createHTML({ 10 bug: "1017888", 11 title: "Renegotiation: remove then add audio track" 12 }); 13 14 runNetworkTest(function (options) { 15 const test = new PeerConnectionTest(options); 16 const audioContext = new AudioContext(); 17 // Start a tone so that the gUM call will record something even with 18 // --use-test-media-devices. 19 const tone = new LoopbackTone(audioContext, TEST_AUDIO_FREQ); 20 tone.start(); 21 let originalTrack; 22 let haveMuteEvent = new Promise(() => {}); 23 let haveUnmuteEvent = new Promise(() => {}); 24 addRenegotiation(test.chain, 25 [ 26 function PC_REMOTE_FIND_RECEIVER(test) { 27 is(test.pcRemote._pc.getReceivers().length, 1, 28 "pcRemote should have one receiver"); 29 originalTrack = test.pcRemote._pc.getReceivers()[0].track; 30 }, 31 function PC_LOCAL_REMOVE_AUDIO_TRACK(test) { 32 return test.pcLocal.removeSender(0); 33 }, 34 function PC_LOCAL_ADD_AUDIO_TRACK(test) { 35 // The new track's pipeline will start with a packet count of 36 // 0, but the remote side will keep its old pipeline and packet 37 // count. 38 test.pcLocal.disableRtpCountChecking = true; 39 return test.pcLocal.getAllUserMediaAndAddStreams([{audio: true}]); 40 }, 41 ], 42 [ 43 function PC_REMOTE_WAIT_FOR_UNMUTE() { 44 return haveUnmuteEvent; 45 }, 46 function PC_REMOTE_CHECK_ADDED_TRACK(test) { 47 is(test.pcRemote._pc.getTransceivers().length, 2, 48 "pcRemote should have two transceivers"); 49 const track = test.pcRemote._pc.getTransceivers()[1].receiver.track; 50 51 const analyser = new AudioStreamAnalyser( 52 audioContext, new MediaStream([track])); 53 const freq = analyser.binIndexForFrequency(TEST_AUDIO_FREQ); 54 return analyser.waitForAnalysisSuccess(arr => arr[freq] > 200); 55 }, 56 function PC_REMOTE_WAIT_FOR_MUTE() { 57 return haveMuteEvent; 58 }, 59 function PC_REMOTE_CHECK_REMOVED_TRACK(test) { 60 is(test.pcRemote._pc.getTransceivers().length, 2, 61 "pcRemote should have two transceivers"); 62 const track = test.pcRemote._pc.getTransceivers()[0].receiver.track; 63 64 const analyser = new AudioStreamAnalyser( 65 audioContext, new MediaStream([track])); 66 const freq = analyser.binIndexForFrequency(TEST_AUDIO_FREQ); 67 return analyser.waitForAnalysisSuccess(arr => arr[freq] < 50); 68 } 69 ] 70 ); 71 72 // The first track should mute when the connection is closed. 73 test.chain.insertBefore("PC_REMOTE_SET_REMOTE_DESCRIPTION", [ 74 function PC_REMOTE_SETUP_ONMUTE(test) { 75 haveMuteEvent = haveEvent(test.pcRemote._pc.getReceivers()[0].track, "mute"); 76 } 77 ]); 78 79 // Second negotiation should cause the second track to unmute. 80 test.chain.insertAfter("PC_REMOTE_SET_REMOTE_DESCRIPTION", [ 81 function PC_REMOTE_SETUP_ONUNMUTE(test) { 82 haveUnmuteEvent = haveEvent(test.pcRemote._pc.getReceivers()[1].track, "unmute"); 83 } 84 ], false, 1); 85 86 test.setMediaConstraints([{audio: true}], [{audio: true}]); 87 return test.run() 88 .finally(() => tone.stop()); 89 }); 90 </script> 91 </pre> 92 </body> 93 </html>