当前位置: 移动技术网 > IT编程>开发语言>JavaScript > WebRTC | Failed to execute 'setLocalDescription' on 'RTCPeerConnection': Failed to parse SessionDescription. a=msid: Missing track ID

WebRTC | Failed to execute 'setLocalDescription' on 'RTCPeerConnection': Failed to parse SessionDescription. a=msid: Missing track ID

2019年02月17日  | 移动技术网IT编程  | 我要评论

1.问题回放

  使用如下代码获取局域网ip报错

  (代码来源: 日期: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://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'}; 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网