Failed to execute 'setLocalDescription' on 'RTCPeerConnection': Failed to parse SessionDescription. a=msid: Missing track ID in msid attribute. ...
1.問題回放
使用如下代碼獲取區域網IP報錯
(代碼來源:https://github.com/diafygi/webrtc-ips 日期:2019-02-16)
Uncaught (in promise) DOMException: Failed to execute 'setLocalDescription' on 'RTCPeerConnection': Failed to parse SessionDescription. a=msid: Missing track ID in msid attribute.
1 //get the IP addresses associated with an account 2 function getIPs(callback){ 3 var ip_dups = {}; 4 5 //compatibility for firefox and chrome 6 var RTCPeerConnection = window.RTCPeerConnection 7 || window.mozRTCPeerConnection 8 || window.webkitRTCPeerConnection; 9 var useWebKit = !!window.webkitRTCPeerConnection; 10 11 //bypass naive webrtc blocking using an iframe 12 if(!RTCPeerConnection){ 13 //NOTE: you need to have an iframe in the page right above the script tag 14 // 15 //<iframe id="iframe" sandbox="allow-same-origin" style="display: none"></iframe> 16 //<script>...getIPs called in here... 17 // 18 var win = iframe.contentWindow; 19 RTCPeerConnection = win.RTCPeerConnection 20 || win.mozRTCPeerConnection 21 || win.webkitRTCPeerConnection; 22 useWebKit = !!win.webkitRTCPeerConnection; 23 } 24 25 //minimal requirements for data connection 26 var mediaConstraints = { 27 optional: [{RtpDataChannels: true}] 28 }; 29 30 var servers = {iceServers: [{urls: "stun:stun.services.mozilla.com"}]}; 31 32 //construct a new RTCPeerConnection 33 var pc = new RTCPeerConnection(servers, mediaConstraints); 34 35 function handleCandidate(candidate){ 36 //match just the IP address 37 var ip_regex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/ 38 var ip_addr = ip_regex.exec(candidate)[1]; 39 40 //remove duplicates 41 if(ip_dups[ip_addr] === undefined) 42 callback(ip_addr); 43 44 ip_dups[ip_addr] = true; 45 } 46 47 //listen for candidate events 48 pc.onicecandidate = function(ice){ 49 50 //skip non-candidate events 51 if(ice.candidate) 52 handleCandidate(ice.candidate.candidate); 53 }; 54 55 //create a bogus data channel 56 pc.createDataChannel(""); 57 58 //create an offer sdp 59 pc.createOffer(function(result){ 60 61 //trigger the stun server request 62 pc.setLocalDescription(result, function(){}, function(){}); 63 64 }, function(){}); 65 66 //wait for a while to let everything done 67 setTimeout(function(){ 68 //read candidate info from local description 69 var lines = pc.localDescription.sdp.split('\n'); 70 71 lines.forEach(function(line){ 72 if(line.indexOf('a=candidate:') === 0) 73 handleCandidate(line); 74 }); 75 }, 1000); 76 } 77 78 //Test: Print the IP addresses into the console 79 getIPs(function(ip){console.log(ip);});
2.分析
代碼在此處報錯:pc.setLocalDescription(result, function(){}, function(){})
即此API報錯:RTCPeerConnection.setLocalDescription()
源代碼在Firefox和Chrome71正常執行,Chrome升級到72穩定版後代碼報錯,瀏覽器debug調試發現Chrome71和Chrome72的offer(代碼中是result)的SDP格式/內容發生了變化,因此可以判斷是新版本Chrome的WebRTC相關API發生了變化,於是在https://www.chromestatus.com/features找到瞭解釋:
文中提供了此鏈接:https://docs.google.com/document/d/1-ZfikoUtoJa9k-GZG1daN0BU3IjIanQ_JSscHxQesvU/edit,劃重點:
WebRTC規範仍然在演進,Chrome上目前SDP格式有兩種:規範定義的SDP格式(Unified Plan)和Chrome自己定義的SDP格式(Plan B),火狐支持前者,如果不指定SDP格式Chrome72預設使用Unified Plan,這和Chrome71的行為不同,那麼如何指定SDP格式呢?
Controlling Which SDP Format Is Used
Specifying the sdpSemantics in Your Application
You can override the default behavior by explicitly specifying the sdpSemantics in the RTCPeerConnection constructor, allowing your application to control the SDP format of the client. This flag is Chrome-only; if the browser does not recognize the parameter it is simply ignored.
// Use Unified Plan or Plan B regardless of what the default browser behavior is.
new RTCPeerConnection({sdpSemantics:'unified-plan'});
new RTCPeerConnection({sdpSemantics:'plan-b'});
Unless your application is prepared for both cases, it is recommended that you explicitly set the sdpSemantics to avoid surprises when Chrome’s default behavior changes.
3.解決
將源代碼此行
var servers = {iceServers: [{urls: "stun:stun.services.mozilla.com"}]};
更改為
var servers = {iceServers: [{urls: "stun:stun.services.mozilla.com"}], sdpSemantics:'plan-b'};
4.其他
如果Public IP無法解析,請更換stun伺服器:
var servers = {iceServers: [{urls: "stun:stun.l.google.com:19302"}], sdpSemantics:'plan-b'};