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

来源:https://www.cnblogs.com/thinking-better/archive/2019/02/16/10386950.html
-Advertisement-
Play Games

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'}; 

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 封裝就是要具有靈活性,樣式自適應,調用的時候傳入props就可以變成自己想要的樣式。 效果展示網址:https://1963331542.github.io/ 源代碼: 1 <template> 2 <div :style="mainBoxStyle"> 3 <div :style="topLine ...
  • 修改|新增jquery-easyui icon圖標 by:授客 QQ:1033553122 測試環境 jquery-easyui-1.5.3 修改配置文件 打開jquery-easyui-1.5.3\themes\icon.css文件,文件頭部添加目標圖標樣式,例子: .icon{ backgrou ...
  • 修改jquery validatebox為英文校驗提示為中文提示 by:授客 QQ:1033553122 測試環境 jquery-easyui-1.5.3 問題描述: 如圖,想把校驗提示由英文改成中文 解決方案 編輯jquery.easyui.min.js 文件,搜索以下內容, missingMes ...
  • 1,打卡簽到 —— 500米範圍限制 a,getLocation 獲取gcj02 == 騰訊系坐標,可以直接用來打開騰訊地圖 (獲取wgs84則需轉換) b,百度坐標轉騰訊坐標,引入鏈接配置 <script charset="utf-8" src="http://map.qq.com/api/js? ...
  • 1.字體圖標 1.字體圖標都是用svg圖片 1.svg圖片不失真 2.svg圖標由設計師提供 3.為了減少網路請求,會把svg圖標轉換成字體圖標,放到字體文件中,通過字體庫的方式使用 1.svg圖片不失真 2.svg圖標由設計師提供 3.為了減少網路請求,會把svg圖標轉換成字體圖標,放到字體文件中 ...
  • javascript加密字元串,md5加密庫,sha1加密庫 下載鏈接: 鏈接:https://pan.baidu.com/s/10pkLUZAWr6_mRHEx51Z5Vg 提取碼:xz8e ...
  • directory "代碼已上傳github, 地址" Detailed Webpack 就像一條生產線, 要經過一系列的處理流程才能將源文件轉換成輸出結果。這條生產線上的每個流程都是單一的, 多個流程之間存在依賴關係。只能完成當前處理後才會轉交到下一個流程。 插件就像一個插入到生產線中的一個功能, ...
  • 官網: 沒有賬號可以先註冊一個,右上角點擊“Sign Up",有賬號直接點擊“Login” 註冊與登錄賬號這些就不詳說了,不懂直接重新學習吧!!!!!!!! 打開終端:win+r 登錄命令:npm login查找用戶名命令:npm whoami 要上傳npm包步驟:1.先初始化npm包:npm in ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...