2
0

index.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. "use strict";
  2. var isWebRTCSupported = false;
  3. ['RTCPeerConnection', 'webkitRTCPeerConnection', 'mozRTCPeerConnection', 'RTCIceGatherer'].forEach(function(item) {
  4. if (isWebRTCSupported) {
  5. return;
  6. }
  7. if (item in window) {
  8. isWebRTCSupported = true;
  9. }
  10. });
  11. try {
  12. window.AudioContext = window.AudioContext || window.webkitAudioContext;
  13. var audioContext = new AudioContext();
  14. } catch (e) {
  15. console.log('Failed to instantiate an audio context, error: ' + e);
  16. }
  17. function onTitleClick(id) {
  18. var targetId = "#";
  19. if (id == "camera-title") {
  20. targetId += "camera-detail";
  21. } else if (id == "mic-title") {
  22. targetId += "mic-detail";
  23. } else if (id == "connection-title") {
  24. targetId += "connection-detail";
  25. } else if (id == "support-title") {
  26. targetId += "support-detail";
  27. } else if (id == "browser-title") {
  28. targetId += "browser-detail";
  29. } else {
  30. console.error("wrong id : " + id);
  31. return;
  32. }
  33. if ($(targetId).is(":visible")) {
  34. $(targetId).hide();
  35. } else {
  36. $(targetId).show();
  37. }
  38. }
  39. function resetUI() {
  40. var camera = $("#camera-title");
  41. var mic = $("#mic-title");
  42. var connection = $("#connection-title");
  43. var support = $("#support-title");
  44. var browser = $("#browser-title");
  45. camera.off("click").on("click", function () {
  46. onTitleClick(this.id);
  47. });
  48. mic.off("click").on("click", function () {
  49. onTitleClick(this.id);
  50. });
  51. connection.off("click").on("click", function () {
  52. onTitleClick(this.id);
  53. });
  54. support.off("click").on("click", function () {
  55. onTitleClick(this.id);
  56. });
  57. browser.off("click").on("click", function () {
  58. onTitleClick(this.id);
  59. });
  60. camera[0].innerText = text.Camera;
  61. mic[0].innerText = text.Mic;
  62. connection[0].innerText = text.Connection;
  63. support[0].innerText = text.Support;
  64. camera.css("background", "#E2E2E2");
  65. mic.css("background", "#E2E2E2");
  66. connection.css("background", "#E2E2E2");
  67. support.css("background", "#E2E2E2");
  68. $("#camera-detail").hide();
  69. $("#browser-detail").hide();
  70. $("#mic-detail").hide();
  71. $("#connection-detail").hide();
  72. $("#support-detail").hide();
  73. $("#camera-detail")[0].innerText = "";
  74. $("#mic-detail")[0].innerText = "";
  75. $("#connection-detail")[0].innerText = "";
  76. $("#support-detail")[0].innerText = "";
  77. $("#browser-detail")[0].innerText = "";
  78. }
  79. function checkTBSVersion(ua) {
  80. //ua = "Mozilla/5.0 (Linux; Android 7.1.1; vivo X9 Build/NMF26F; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/53.0.2785.49 Mobile MQQBrowser/6.2 TBS/043501 Safari/537.36 MicroMessenger/6.5.13.1100 NetType/WIFI Language/zh_CN";
  81. var list = ua.split(" ");
  82. for (var i = 0; i < list.length; i++) {
  83. var item = list[i];
  84. if (item.indexOf("TBS") !== -1 || item.indexOf("tbs") !== -1) {
  85. var versionStr = item.split("/")[1];
  86. var version = parseInt(versionStr) || 0;
  87. if (version <= 43600) {
  88. alert("您的TBS版本号(" + versionStr + ")过低,不支持WebRTC,请升级!");
  89. }
  90. }
  91. }
  92. }
  93. function init() {
  94. resetUI();
  95. // $("#us")[0].innerText = navigator.userAgent;
  96. $("#stunserver").val("stun:webrtc.qq.com:8800");
  97. $("#start-btn").off("click").on("click", function () {
  98. resetUI();
  99. startAudioTest();
  100. startBrowserTest();
  101. });
  102. checkTBSVersion(navigator.userAgent);
  103. }
  104. function startLoadingInner(text, dom, step) {
  105. for (var i = 0; i < step; i++) {
  106. text += ".";
  107. }
  108. dom.innerText = text;
  109. }
  110. function startLoading(text, dom) {
  111. intervalID = setInterval(function (text, dom) {
  112. startLoadingInner(text, dom, intervalStep);
  113. intervalStep++;
  114. if (intervalStep > 3) {
  115. intervalStep = 1;
  116. }
  117. }, 500, text, dom)
  118. }
  119. function stopLoading() {
  120. clearInterval(intervalID);
  121. }
  122. var audioMessage = "";
  123. var videoMessage = "";
  124. var supportMessage = "";
  125. var connectionMessage = "";
  126. var intervalID = 0;
  127. var intervalStep = 1;
  128. var text = {
  129. Mic : "音频输入",
  130. MicTest : "音频输入检测(请对着麦克风说话)",
  131. Camera : "视频输入",
  132. CameraTest : "视频输入检测",
  133. Support : "分辨率",
  134. SupportTest : "支持的分辨率检测",
  135. Connection : "连通性",
  136. ConnectionTest : "连通性测试",
  137. Browser : "浏览器"
  138. };
  139. init();
  140. function onAudioTestMessage(str) {
  141. audioMessage += str + "\r\n";
  142. }
  143. function onAudioTestDone(result) {
  144. stopLoading();
  145. $("#mic-detail")[0].innerText = audioMessage;
  146. audioMessage = "";
  147. var titleText = null;
  148. if (result == 0) {
  149. $("#mic-title").css("background", "#90dc90");
  150. titleText = text.MicTest + " 成功 !!!";
  151. } else {
  152. $("#mic-title").css("background", "#dc9090");
  153. titleText = text.MicTest + " 失败 !!!";
  154. }
  155. $("#mic-title")[0].innerText = titleText;
  156. startVideoTest();
  157. }
  158. function onVideoTestMessage(str) {
  159. videoMessage += str + "\r\n";
  160. }
  161. function onVideoTestDone(result) {
  162. stopLoading();
  163. $("#camera-detail")[0].innerText = videoMessage;
  164. videoMessage = "";
  165. var titleText = null;
  166. if (result == 0) {
  167. $("#camera-title").css("background", "#90dc90");
  168. titleText = text.CameraTest + " 成功 !!!";
  169. } else {
  170. $("#camera-title").css("background", "#dc9090");
  171. titleText = text.CameraTest + " 失败 !!!";
  172. }
  173. $("#camera-title")[0].innerText = titleText;
  174. startSupportTest();
  175. }
  176. function onSupportTestMessage(str) {
  177. supportMessage += str + "\r\n";
  178. }
  179. function onSupportTestDone(result) {
  180. stopLoading();
  181. $("#support-detail")[0].innerText = supportMessage;
  182. supportMessage = "";
  183. var titleText = null;
  184. if (result == 0) {
  185. $("#support-title").css("background", "#90dc90");
  186. titleText = text.SupportTest + " 成功 !!!";
  187. } else {
  188. $("#support-title").css("background", "#dc9090");
  189. titleText = text.SupportTest + " 失败 !!!";
  190. }
  191. $("#support-title")[0].innerText = titleText;
  192. startConnectionTest();
  193. }
  194. function onConnectionTestMessage(str) {
  195. connectionMessage += str + "\r\n";
  196. }
  197. function onConnectionTestDone(result) {
  198. stopLoading();
  199. $("#connection-detail")[0].innerText = connectionMessage;
  200. connectionMessage = "";
  201. var titleText = null;
  202. if (result == 0) {
  203. $("#connection-title").css("background", "#90dc90");
  204. titleText = text.ConnectionTest + " 成功 !!!";
  205. } else {
  206. $("#connection-title").css("background", "#dc9090");
  207. titleText = text.ConnectionTest + " 失败 !!!";
  208. }
  209. $("#connection-title")[0].innerText = titleText;
  210. disableButton(false, "start-btn");
  211. }
  212. function disableButton(isDisable, id) {
  213. var queryId = "#" + id;
  214. $(queryId).prop("disable", isDisable);
  215. }
  216. function startAudioTest() {
  217. disableButton(true, "start-btn");
  218. startLoading(text.MicTest, $("#mic-title")[0]);
  219. if(!isWebRTCSupported){
  220. onAudioTestDone(-1);
  221. return;
  222. }
  223. if(false && isMobile.iOS()){
  224. //IOS无法遍历设备,默认认为支持
  225. onAudioTestDone(0);
  226. return;
  227. }
  228. var audioTest = new WebRTCTest();
  229. audioTest.setListener({
  230. onMessage : onAudioTestMessage,
  231. done : onAudioTestDone
  232. });
  233. var micTest = new MicTest(audioTest);
  234. micTest.run();
  235. }
  236. function startVideoTest() {
  237. if(!isWebRTCSupported){
  238. onVideoTestDone(-1);
  239. return;
  240. }
  241. startLoading(text.CameraTest, $("#camera-title")[0]);
  242. var videoTest = new WebRTCTest();
  243. videoTest.setListener({
  244. onMessage : onVideoTestMessage,
  245. done : onVideoTestDone
  246. });
  247. var camTest = new CameraTest(videoTest, [ [640,360], [640,480]]);
  248. camTest.run();
  249. }
  250. function startSupportTest() {
  251. if(!isWebRTCSupported || (!isMobile.safari() && isMobile.iOS() )){
  252. onSupportTestDone(-1);
  253. return;
  254. }
  255. startLoading(text.SupportTest, $("#support-title")[0]);
  256. var supportTest = new WebRTCTest();
  257. supportTest.setListener({
  258. onMessage : onSupportTestMessage,
  259. done : onSupportTestDone
  260. });
  261. var resolutionArray = [
  262. // [160, 120], [320, 180], [320, 240], [640, 360], [640, 480], [768, 576],
  263. // [1024, 576], [1280, 720], [1280, 768], [1280, 800], [1920, 1080],
  264. // [1920, 1200], [3840, 2160], [4096, 2160]
  265. [1280,720], [640,360], [640,480], [480,264], [320,180]
  266. ];
  267. var supTest = new CameraTest(supportTest, resolutionArray);
  268. supTest.run();
  269. }
  270. var websocket = null;
  271. function getRelayIp(callback ,error){
  272. var url = "wss://webrtc.qq.com:8687/?userSig=eJxlj8tOwzAQRff5CivbImo7cXCRWDSVqQpFtBQWZGOlsUMmrxrXNCDEv1NCJSJxt*fMXN1PDyHkPy4352mW7d5aJ92H0T66RD72z-6gMaBk6mRg1T*o3w1YLdPcadtDwhijGA8dULp1kMPJaHVa19AOhL2qZN-y*yE8ntMLHk6GCrz08E48zRbiUDR8-gpiZGgYrO83olEhr4u85EYQawJKr5O44-MxnYKYdjOe0eVDZXT8TEaLpFRbtSqCKmKT8TaJ4lsR4tVNUbKouxpUOmj0aRLmhLKIkQE9aLuHXdsLFBNGaIB-4ntf3jcuhluu&sdkAppid=1400027849&identifier=neallin";
  273. if (websocket) {
  274. try {
  275. websocket.close();
  276. websocket = null;
  277. } catch (e) {
  278. console.error(e);
  279. }
  280. }
  281. websocket = new WebSocket(url);
  282. websocket.onmessage = callback;
  283. websocket.onopen = wsonopen;
  284. websocket.onerror = error;
  285. websocket.onclose = wsonclose;
  286. }
  287. // getRelayIp();
  288. function wsonopen(data){
  289. console.debug(data);
  290. }
  291. function wsonclose(data){
  292. console.debug(data);
  293. }
  294. function wsonclose(data){
  295. console.debug(data);
  296. }
  297. function wsonclose(data){
  298. console.debug(data);
  299. }
  300. function startConnectionTest() {
  301. if(!isWebRTCSupported){
  302. onConnectionTestDone(-1);
  303. return;
  304. }
  305. getRelayIp(function(data){
  306. //IOS默认认为支持
  307. if(isMobile.iOS() && isMobile.safari()){
  308. onConnectionTestDone(0);
  309. return;
  310. }
  311. var json = JSON.parse(data.data);
  312. var ip = json.content.relayip;
  313. startLoading(text.ConnectionTest, $("#connection-title")[0]);
  314. var connectionTest = new WebRTCTest();
  315. connectionTest.setListener({
  316. onMessage : onConnectionTestMessage,
  317. done : onConnectionTestDone
  318. });
  319. var conTest = new ConnectionTest(connectionTest , 'stun:'+ip+":8800");
  320. conTest.run();
  321. },function(){
  322. onConnectionTestDone(-1);
  323. });
  324. }
  325. function checkTBSVersion(ua) {
  326. var list = ua.split(" ");
  327. for (var i = 0; i < list.length; i++) {
  328. var item = list[i];
  329. if (item.indexOf("TBS") !== -1 || item.indexOf("tbs") !== -1) {
  330. var versionStr = item.split("/")[1];
  331. var version = parseInt(versionStr) || 0;
  332. return version
  333. }
  334. }
  335. return null;
  336. }
  337. function startBrowserTest(){
  338. $("#browser-detail")[0].innerText = "";
  339. var titleText = null;
  340. $("#browser-title")[0].innerText = titleText;
  341. $("#browser-detail")[0].innerText = navigator.userAgent;
  342. var isMobileBrowser = false;
  343. for(var a in isMobile){
  344. if( isMobile[a]() ){
  345. isMobileBrowser = true
  346. console.debug(a)
  347. console.debug(isMobile[a]())
  348. titleText = a + ":";
  349. var version = checkTBSVersion(navigator.userAgent);
  350. if( a === 'Android' && version && version < 43600 ){
  351. $("#browser-title").css("background", "#dc9090");
  352. titleText = "TBS (version:"+ version + ") 不支持 !!!";
  353. }
  354. else if(!isWebRTCSupported || (!isMobile.safari() && isMobile.iOS())) {
  355. $("#browser-title").css("background", "#dc9090");
  356. titleText = a + "当前浏览器不支持 !!!";
  357. onVideoTestDone(-1);
  358. onSupportTestDone(-1);
  359. }else{
  360. if( isMobile.iOS() ){
  361. //ios 11 版本 11.0.3 以下不支持
  362. var matches = (navigator.userAgent).match(/OS (\d+)_(\d+)_?(\d+)?/);
  363. if(matches && matches[1]>=11 && matches[3] >= 3){
  364. $("#browser-title").css("background", "#90dc90");
  365. titleText = a + " 当前浏览器支持 !!!";
  366. }else{
  367. $("#browser-title").css("background", "#dc9090");
  368. titleText = "IOS (version:"+ matches[1] + ") 不支持 !!!";
  369. }
  370. }else{
  371. $("#browser-title").css("background", "#90dc90");
  372. titleText = a + " 当前浏览器支持 !!!";
  373. }
  374. }
  375. $("#browser-title")[0].innerText = titleText;
  376. break;
  377. }
  378. }
  379. if( !isMobileBrowser){
  380. if(isWebRTCSupported){
  381. titleText = "当前浏览器 支持 !!!";
  382. $("#browser-title").css("background", "#90dc90");
  383. $("#browser-title")[0].innerText = titleText;
  384. }else{
  385. titleText = "当前浏览器 不支持 !!!";
  386. $("#browser-title").css("background", "#dc9090");
  387. $("#browser-title")[0].innerText = titleText;
  388. }
  389. }
  390. }