aboutsummaryrefslogtreecommitdiff
path: root/website/mic.js
blob: 76d30051fef06e8fca775ab81ef9dacd5ea460bc (plain)
  1. var websocket_server = null;
  2. if(window.location.protocol === 'http:')
  3. websocket_server = "ws://" + window.location.hostname + "/janus-ws/janus";
  4. else
  5. websocket_server = "wss://" + window.location.hostname + "/janus-ws/janus";
  6. var janus = null;
  7. var streaming = null;
  8. var mixertest = null;
  9. var opaqueId = "streamingwithfeedback-"+Janus.randomString(12);
  10. var bitrateTimer = null;
  11. var spinner = null;
  12. var simulcastStarted = false, svcStarted = false;
  13. var selectedStream = null;
  14. var myroom = null;
  15. var myusername = null;
  16. var myid = null;
  17. var webrtcUp = false;
  18. var audioenabled = false;
  19. $(document).ready(function() {
  20. // Initialize the library (all console debuggers enabled)
  21. Janus.init({debug: "all", callback: function() {
  22. // Use a button to start the demo
  23. $('#start').one('click', function() {
  24. $(this).attr('disabled', true).unbind('click');
  25. // Make sure the browser supports WebRTC
  26. if(!Janus.isWebrtcSupported()) {
  27. bootbox.alert("No WebRTC support... ");
  28. return;
  29. }
  30. // Create session
  31. janus = new Janus(
  32. {
  33. server: [websocket_server, "/janus"],
  34. iceServers: [{url: "turn:morla.jones.dk", username: "myturn", credential: "notsecure"},
  35. {url: "turn:jawa.homebase.dk", username: "myturn", credential: "notsecure"}],
  36. success: function() {
  37. // Attach to streaming plugin
  38. janus.attach(
  39. {
  40. plugin: "janus.plugin.streaming",
  41. opaqueId: opaqueId,
  42. success: function(pluginHandle) {
  43. $('#details').remove();
  44. streaming = pluginHandle;
  45. Janus.log("Plugin attached! (" + streaming.getPlugin() + ", id=" + streaming.getId() + ")");
  46. // Setup streaming session
  47. $('#update-streams').click(updateStreamsList);
  48. updateStreamsList();
  49. $('#start').removeAttr('disabled').html("Stop")
  50. .click(function() {
  51. $(this).attr('disabled', true);
  52. clearInterval(bitrateTimer);
  53. janus.destroy();
  54. $('#streamslist').attr('disabled', true);
  55. $('#watch').attr('disabled', true).unbind('click');
  56. $('#start').attr('disabled', true).html("Bye").unbind('click');
  57. });
  58. },
  59. error: function(error) {
  60. Janus.error(" -- Error attaching plugin... ", error);
  61. bootbox.alert("Error attaching plugin... " + error);
  62. },
  63. onmessage: function(msg, jsep) {
  64. Janus.debug(" ::: Got a message :::");
  65. Janus.debug(msg);
  66. var result = msg["result"];
  67. if(result !== null && result !== undefined) {
  68. if(result["status"] !== undefined && result["status"] !== null) {
  69. var status = result["status"];
  70. if(status === 'starting')
  71. $('#status').removeClass('hide').text("Starting, please wait...").show();
  72. else if(status === 'started')
  73. $('#status').removeClass('hide').text("Started").show();
  74. else if(status === 'stopped')
  75. stopStream();
  76. } else if(msg["streaming"] === "event") {
  77. // Is simulcast in place?
  78. var substream = result["substream"];
  79. var temporal = result["temporal"];
  80. if((substream !== null && substream !== undefined) || (temporal !== null && temporal !== undefined)) {
  81. if(!simulcastStarted) {
  82. simulcastStarted = true;
  83. addSimulcastButtons(temporal !== null && temporal !== undefined);
  84. }
  85. // We just received notice that there's been a switch, update the buttons
  86. updateSimulcastButtons(substream, temporal);
  87. }
  88. // Is VP9/SVC in place?
  89. var spatial = result["spatial_layer"];
  90. temporal = result["temporal_layer"];
  91. if((spatial !== null && spatial !== undefined) || (temporal !== null && temporal !== undefined)) {
  92. if(!svcStarted) {
  93. svcStarted = true;
  94. addSvcButtons();
  95. }
  96. // We just received notice that there's been a switch, update the buttons
  97. updateSvcButtons(spatial, temporal);
  98. }
  99. }
  100. } else if(msg["error"] !== undefined && msg["error"] !== null) {
  101. bootbox.alert(msg["error"]);
  102. stopStream();
  103. return;
  104. }
  105. if(jsep !== undefined && jsep !== null) {
  106. Janus.debug("Handling SDP as well...");
  107. Janus.debug(jsep);
  108. // Offer from the plugin, let's answer
  109. streaming.createAnswer(
  110. {
  111. jsep: jsep,
  112. // We want recvonly audio/video and, if negotiated, datachannels
  113. media: { audioSend: false, videoSend: false, data: true },
  114. success: function(jsep) {
  115. Janus.debug("Got SDP!");
  116. Janus.debug(jsep);
  117. var body = { "request": "start" };
  118. streaming.send({"message": body, "jsep": jsep});
  119. $('#watch').html("Stop").removeAttr('disabled').click(stopStream);
  120. },
  121. error: function(error) {
  122. Janus.error("WebRTC error:", error);
  123. bootbox.alert("WebRTC error... " + JSON.stringify(error));
  124. }
  125. });
  126. }
  127. },
  128. onremotestream: function(stream) {
  129. Janus.debug(" ::: Got a remote stream :::");
  130. Janus.debug(stream);
  131. var addButtons = false;
  132. if($('#remotevideo').length === 0) {
  133. addButtons = true;
  134. $('#stream').append('<video class="rounded centered hide" id="remotevideo" width=320 height=240 autoplay playsinline/>');
  135. // Show the stream and hide the spinner when we get a playing event
  136. $("#remotevideo").bind("playing", function () {
  137. $('#waitingvideo').remove();
  138. if(this.videoWidth)
  139. $('#remotevideo').removeClass('hide').show();
  140. if(spinner !== null && spinner !== undefined)
  141. spinner.stop();
  142. spinner = null;
  143. var videoTracks = stream.getVideoTracks();
  144. if(videoTracks === null || videoTracks === undefined || videoTracks.length === 0)
  145. return;
  146. var width = this.videoWidth;
  147. var height = this.videoHeight;
  148. $('#curres').removeClass('hide').text(width+'x'+height).show();
  149. if(Janus.webRTCAdapter.browserDetails.browser === "firefox") {
  150. // Firefox Stable has a bug: width and height are not immediately available after a playing
  151. setTimeout(function() {
  152. var width = $("#remotevideo").get(0).videoWidth;
  153. var height = $("#remotevideo").get(0).videoHeight;
  154. $('#curres').removeClass('hide').text(width+'x'+height).show();
  155. }, 2000);
  156. }
  157. });
  158. }
  159. Janus.attachMediaStream($('#remotevideo').get(0), stream);
  160. var videoTracks = stream.getVideoTracks();
  161. if(videoTracks === null || videoTracks === undefined || videoTracks.length === 0) {
  162. // No remote video
  163. $('#remotevideo').hide();
  164. if($('#stream .no-video-container').length === 0) {
  165. $('#stream').append(
  166. '<div class="no-video-container">' +
  167. '<i class="fa fa-video-camera fa-5 no-video-icon"></i>' +
  168. '<span class="no-video-text">No remote video available</span>' +
  169. '</div>');
  170. }
  171. } else {
  172. $('#stream .no-video-container').remove();
  173. $('#remotevideo').removeClass('hide').show();
  174. }
  175. if(!addButtons)
  176. return;
  177. if(videoTracks && videoTracks.length &&
  178. (Janus.webRTCAdapter.browserDetails.browser === "chrome" ||
  179. Janus.webRTCAdapter.browserDetails.browser === "firefox" ||
  180. Janus.webRTCAdapter.browserDetails.browser === "safari")) {
  181. $('#curbitrate').removeClass('hide').show();
  182. bitrateTimer = setInterval(function() {
  183. // Display updated bitrate, if supported
  184. var bitrate = streaming.getBitrate();
  185. //~ Janus.debug("Current bitrate is " + streaming.getBitrate());
  186. $('#curbitrate').text(bitrate);
  187. // Check if the resolution changed too
  188. var width = $("#remotevideo").get(0).videoWidth;
  189. var height = $("#remotevideo").get(0).videoHeight;
  190. if(width > 0 && height > 0)
  191. $('#curres').removeClass('hide').text(width+'x'+height).show();
  192. }, 1000);
  193. }
  194. },
  195. ondataopen: function(data) {
  196. Janus.log("The DataChannel is available!");
  197. $('#waitingvideo').remove();
  198. $('#stream').append(
  199. '<input class="form-control" type="text" id="datarecv" disabled></input>'
  200. );
  201. if(spinner !== null && spinner !== undefined)
  202. spinner.stop();
  203. spinner = null;
  204. },
  205. ondata: function(data) {
  206. Janus.debug("We got data from the DataChannel! " + data);
  207. $('#datarecv').val(data);
  208. },
  209. oncleanup: function() {
  210. Janus.log(" ::: Got a cleanup notification :::");
  211. $('#waitingvideo').remove();
  212. $('#remotevideo').remove();
  213. $('#datarecv').remove();
  214. $('.no-video-container').remove();
  215. $('#bitrate').attr('disabled', true);
  216. $('#bitrateset').html('Bandwidth<span class="caret"></span>');
  217. $('#curbitrate').hide();
  218. if(bitrateTimer !== null && bitrateTimer !== undefined)
  219. clearInterval(bitrateTimer);
  220. bitrateTimer = null;
  221. $('#curres').hide();
  222. $('#simulcast').remove();
  223. simulcastStarted = false;
  224. }
  225. });
  226. // Attach to Audio Bridge test plugin
  227. janus.attach(
  228. {
  229. plugin: "janus.plugin.audiobridge",
  230. opaqueId: opaqueId,
  231. success: function(pluginHandle) {
  232. $('#details').remove();
  233. mixertest = pluginHandle;
  234. Janus.log("Plugin attached! (" + mixertest.getPlugin() + ", id=" + mixertest.getId() + ")");
  235. },
  236. error: function(error) {
  237. Janus.error(" -- Error attaching plugin...", error);
  238. bootbox.alert("Error attaching plugin... " + error);
  239. },
  240. consentDialog: function(on) {
  241. Janus.debug("Consent dialog should be " + (on ? "on" : "off") + " now");
  242. if(on) {
  243. // Darken screen and show hint
  244. $.blockUI({
  245. message: '<div><img src="img/up_arrow.png"/></div>',
  246. css: {
  247. border: 'none',
  248. padding: '15px',
  249. backgroundColor: 'transparent',
  250. color: '#aaa',
  251. top: '10px',
  252. left: (navigator.mozGetUserMedia ? '-100px' : '300px')
  253. } });
  254. } else {
  255. // Restore screen
  256. $.unblockUI();
  257. }
  258. },
  259. onmessage: function(msg, jsep) {
  260. Janus.debug(" ::: Got a message :::");
  261. Janus.debug(msg);
  262. var event = msg["audiobridge"];
  263. Janus.debug("Event: " + event);
  264. if(event != undefined && event != null) {
  265. if(event === "joined") {
  266. // Successfully joined, negotiate WebRTC now
  267. myid = msg["id"];
  268. Janus.log("Successfully joined room " + msg["room"] + " with ID " + myid);
  269. if(!webrtcUp) {
  270. webrtcUp = true;
  271. // Publish our stream
  272. mixertest.createOffer(
  273. {
  274. media: { video: false}, // This is an audio only room
  275. success: function(jsep) {
  276. Janus.debug("Got SDP!");
  277. Janus.debug(jsep);
  278. var publish = { "request": "configure", "muted": false };
  279. mixertest.send({"message": publish, "jsep": jsep});
  280. },
  281. error: function(error) {
  282. Janus.error("WebRTC error:", error);
  283. bootbox.alert("WebRTC error... " + JSON.stringify(error));
  284. }
  285. });
  286. }
  287. // Any room participant?
  288. if(msg["participants"] !== undefined && msg["participants"] !== null) {
  289. var list = msg["participants"];
  290. Janus.debug("Got a list of participants:");
  291. Janus.debug(list);
  292. for(var f in list) {
  293. var id = list[f]["id"];
  294. var display = list[f]["display"];
  295. var setup = list[f]["setup"];
  296. var muted = list[f]["muted"];
  297. Janus.debug(" >> [" + id + "] " + display + " (setup=" + setup + ", muted=" + muted + ")");
  298. if($('#rp'+id).length === 0) {
  299. // Add to the participants list
  300. $('#list').append('<li id="rp'+id+'" class="list-group-item">'+display+
  301. ' <i class="absetup fa fa-chain-broken"></i>' +
  302. ' <i class="abmuted fa fa-microphone-slash"></i></li>');
  303. $('#rp'+id + ' > i').hide();
  304. }
  305. if(muted === true || muted === "true")
  306. $('#rp'+id + ' > i.abmuted').removeClass('hide').show();
  307. else
  308. $('#rp'+id + ' > i.abmuted').hide();
  309. if(setup === true || setup === "true")
  310. $('#rp'+id + ' > i.absetup').hide();
  311. else
  312. $('#rp'+id + ' > i.absetup').removeClass('hide').show();
  313. }
  314. }
  315. } else if(event === "roomchanged") {
  316. // The user switched to a different room
  317. myid = msg["id"];
  318. Janus.log("Moved to room " + msg["room"] + ", new ID: " + myid);
  319. // Any room participant?
  320. $('#list').empty();
  321. if(msg["participants"] !== undefined && msg["participants"] !== null) {
  322. var list = msg["participants"];
  323. Janus.debug("Got a list of participants:");
  324. Janus.debug(list);
  325. for(var f in list) {
  326. var id = list[f]["id"];
  327. var display = list[f]["display"];
  328. var setup = list[f]["setup"];
  329. var muted = list[f]["muted"];
  330. Janus.debug(" >> [" + id + "] " + display + " (setup=" + setup + ", muted=" + muted + ")");
  331. if($('#rp'+id).length === 0) {
  332. // Add to the participants list
  333. $('#list').append('<li id="rp'+id+'" class="list-group-item">'+display+
  334. ' <i class="absetup fa fa-chain-broken"></i>' +
  335. ' <i class="abmuted fa fa-microphone-slash"></i></li>');
  336. $('#rp'+id + ' > i').hide();
  337. }
  338. if(muted === true || muted === "true")
  339. $('#rp'+id + ' > i.abmuted').removeClass('hide').show();
  340. else
  341. $('#rp'+id + ' > i.abmuted').hide();
  342. if(setup === true || setup === "true")
  343. $('#rp'+id + ' > i.absetup').hide();
  344. else
  345. $('#rp'+id + ' > i.absetup').removeClass('hide').show();
  346. }
  347. }
  348. } else if(event === "destroyed") {
  349. // The room has been destroyed
  350. Janus.warn("The room has been destroyed!");
  351. bootbox.alert("The room has been destroyed", function() {
  352. window.location.reload();
  353. });
  354. } else if(event === "event") {
  355. if(msg["participants"] !== undefined && msg["participants"] !== null) {
  356. var list = msg["participants"];
  357. Janus.debug("Got a list of participants:");
  358. Janus.debug(list);
  359. for(var f in list) {
  360. var id = list[f]["id"];
  361. var display = list[f]["display"];
  362. var setup = list[f]["setup"];
  363. var muted = list[f]["muted"];
  364. Janus.debug(" >> [" + id + "] " + display + " (setup=" + setup + ", muted=" + muted + ")");
  365. if($('#rp'+id).length === 0) {
  366. // Add to the participants list
  367. $('#list').append('<li id="rp'+id+'" class="list-group-item">'+display+
  368. ' <i class="absetup fa fa-chain-broken"></i>' +
  369. ' <i class="abmuted fa fa-microphone-slash"></i></li>');
  370. $('#rp'+id + ' > i').hide();
  371. }
  372. if(muted === true || muted === "true")
  373. $('#rp'+id + ' > i.abmuted').removeClass('hide').show();
  374. else
  375. $('#rp'+id + ' > i.abmuted').hide();
  376. if(setup === true || setup === "true")
  377. $('#rp'+id + ' > i.absetup').hide();
  378. else
  379. $('#rp'+id + ' > i.absetup').removeClass('hide').show();
  380. }
  381. } else if(msg["error"] !== undefined && msg["error"] !== null) {
  382. if(msg["error_code"] === 485) {
  383. // This is a "no such room" error: give a more meaningful description
  384. bootbox.alert(
  385. "<p>Server error: room <code>" + myroom + "</code> does not exist."
  386. );
  387. } else {
  388. bootbox.alert(msg["error"]);
  389. }
  390. return;
  391. }
  392. // Any new feed to attach to?
  393. if(msg["leaving"] !== undefined && msg["leaving"] !== null) {
  394. // One of the participants has gone away?
  395. var leaving = msg["leaving"];
  396. Janus.log("Participant left: " + leaving + " (we have " + $('#rp'+leaving).length + " elements with ID #rp" +leaving + ")");
  397. $('#rp'+leaving).remove();
  398. }
  399. }
  400. }
  401. if(jsep !== undefined && jsep !== null) {
  402. Janus.debug("Handling SDP as well...");
  403. Janus.debug(jsep);
  404. mixertest.handleRemoteJsep({jsep: jsep});
  405. }
  406. },
  407. onlocalstream: function(stream) {
  408. Janus.debug(" ::: Got a local stream :::");
  409. Janus.debug(stream);
  410. // We're not going to attach the local audio stream
  411. $('#audiojoin').hide();
  412. $('#room').removeClass('hide').show();
  413. $('#participant').removeClass('hide').html(myusername).show();
  414. },
  415. onremotestream: function(stream) {
  416. $('#room').removeClass('hide').show();
  417. var addButtons = false;
  418. if($('#roomaudio').length === 0) {
  419. addButtons = true;
  420. $('#mixedaudio').append('<audio class="rounded centered" id="roomaudio" width="100%" height="100%" autoplay/>');
  421. }
  422. Janus.attachMediaStream($('#roomaudio').get(0), stream);
  423. if(!addButtons)
  424. return;
  425. // Mute button
  426. audioenabled = true;
  427. $('#toggleaudio').click(
  428. function() {
  429. audioenabled = !audioenabled;
  430. if(audioenabled)
  431. $('#toggleaudio').html("Mute").removeClass("btn-success").addClass("btn-danger");
  432. else
  433. $('#toggleaudio').html("Unmute").removeClass("btn-danger").addClass("btn-success");
  434. mixertest.send({message: { "request": "configure", "muted": !audioenabled }});
  435. }).removeClass('hide').show();
  436. },
  437. oncleanup: function() {
  438. webrtcUp = false;
  439. Janus.log(" ::: Got a cleanup notification :::");
  440. $('#participant').empty().hide();
  441. $('#list').empty();
  442. $('#mixedaudio').empty();
  443. $('#room').hide();
  444. }
  445. });
  446. },
  447. error: function(error) {
  448. Janus.error(error);
  449. bootbox.alert(error, function() {
  450. window.location.reload();
  451. });
  452. },
  453. destroyed: function() {
  454. window.location.reload();
  455. }
  456. });
  457. });
  458. }});
  459. });
  460. function updateStreamsList() {
  461. $('#audiojoin').hide();
  462. $('#update-streams').unbind('click').addClass('fa-spin');
  463. var body = { "request": "list" };
  464. Janus.debug("Sending message (" + JSON.stringify(body) + ")");
  465. streaming.send({"message": body, success: function(result) {
  466. setTimeout(function() {
  467. $('#update-streams').removeClass('fa-spin').click(updateStreamsList);
  468. }, 500);
  469. if(result === null || result === undefined) {
  470. bootbox.alert("Got no response to our query for available streams");
  471. return;
  472. }
  473. if(result["list"] !== undefined && result["list"] !== null) {
  474. $('#streams').removeClass('hide').show();
  475. $('#streamslist').empty();
  476. $('#watch').attr('disabled', true).unbind('click');
  477. var list = result["list"];
  478. Janus.log("Got a list of available streams");
  479. Janus.debug(list);
  480. for(var mp in list) {
  481. Janus.debug(" >> [" + list[mp]["id"] + "] " + list[mp]["description"] + " (" + list[mp]["type"] + ")");
  482. $('#streamslist').append("<li><a href='#' id='" + list[mp]["id"] + "'>" + list[mp]["description"] + " (" + list[mp]["type"] + ")" + "</a></li>");
  483. }
  484. $('#streamslist a').unbind('click').click(function() {
  485. selectedStream = $(this).attr("id");
  486. $('#streamset').html($(this).html()).parent().removeClass('open');
  487. return false;
  488. });
  489. $('#watch').removeAttr('disabled').unbind('click').click(startStream);
  490. }
  491. }});
  492. }
  493. function startStream() {
  494. Janus.log("Selected video id #" + selectedStream);
  495. if(selectedStream === undefined || selectedStream === null) {
  496. bootbox.alert("Select a stream from the list");
  497. return;
  498. }
  499. $('#streamset').attr('disabled', true);
  500. $('#streamslist').attr('disabled', true);
  501. $('#watch').attr('disabled', true).unbind('click');
  502. var body = { "request": "watch", id: parseInt(selectedStream) };
  503. streaming.send({"message": body});
  504. // No remote video yet
  505. $('#stream').append('<video class="rounded centered" id="waitingvideo" width=320 height=240 />');
  506. if(spinner == null) {
  507. var target = document.getElementById('stream');
  508. spinner = new Spinner({top:100}).spin(target);
  509. } else {
  510. spinner.spin();
  511. }
  512. // Prepare the username registration
  513. myroom = parseInt(selectedStream);
  514. $('#audiojoin').removeClass('hide').show();
  515. $('#registernow').removeClass('hide').show();
  516. $('#register').click(registerUsername);
  517. $('#username').focus();
  518. $('#start').removeAttr('disabled').html("Stop")
  519. .click(function() {
  520. $(this).attr('disabled', true);
  521. janus.destroy();
  522. });
  523. }
  524. function stopStream() {
  525. $('#audiojoin').hide();
  526. $('#watch').attr('disabled', true).unbind('click');
  527. var body = { "request": "stop" };
  528. streaming.send({"message": body});
  529. streaming.hangup();
  530. $('#streamset').removeAttr('disabled');
  531. $('#streamslist').removeAttr('disabled');
  532. $('#watch').html("Watch or Listen").removeAttr('disabled').unbind('click').click(startStream);
  533. $('#status').empty().hide();
  534. $('#bitrate').attr('disabled', true);
  535. $('#bitrateset').html('Bandwidth<span class="caret"></span>');
  536. $('#curbitrate').hide();
  537. if(bitrateTimer !== null && bitrateTimer !== undefined)
  538. clearInterval(bitrateTimer);
  539. bitrateTimer = null;
  540. $('#curres').empty().hide();
  541. $('#simulcast').remove();
  542. simulcastStarted = false;
  543. }
  544. // Helpers to create Simulcast-related UI, if enabled
  545. function addSimulcastButtons(temporal) {
  546. $('#curres').parent().append(
  547. '<div id="simulcast" class="btn-group-vertical btn-group-vertical-xs pull-right">' +
  548. ' <div class"row">' +
  549. ' <div class="btn-group btn-group-xs" style="width: 100%">' +
  550. ' <button id="sl-2" type="button" class="btn btn-primary" data-toggle="tooltip" title="Switch to higher quality" style="width: 33%">SL 2</button>' +
  551. ' <button id="sl-1" type="button" class="btn btn-primary" data-toggle="tooltip" title="Switch to normal quality" style="width: 33%">SL 1</button>' +
  552. ' <button id="sl-0" type="button" class="btn btn-primary" data-toggle="tooltip" title="Switch to lower quality" style="width: 34%">SL 0</button>' +
  553. ' </div>' +
  554. ' </div>' +
  555. ' <div class"row">' +
  556. ' <div class="btn-group btn-group-xs hide" style="width: 100%">' +
  557. ' <button id="tl-2" type="button" class="btn btn-primary" data-toggle="tooltip" title="Cap to temporal layer 2" style="width: 34%">TL 2</button>' +
  558. ' <button id="tl-1" type="button" class="btn btn-primary" data-toggle="tooltip" title="Cap to temporal layer 1" style="width: 33%">TL 1</button>' +
  559. ' <button id="tl-0" type="button" class="btn btn-primary" data-toggle="tooltip" title="Cap to temporal layer 0" style="width: 33%">TL 0</button>' +
  560. ' </div>' +
  561. ' </div>' +
  562. '</div>');
  563. // Enable the simulcast selection buttons
  564. $('#sl-0').removeClass('btn-primary btn-success').addClass('btn-primary')
  565. .unbind('click').click(function() {
  566. toastr.info("Switching simulcast substream, wait for it... (lower quality)", null, {timeOut: 2000});
  567. if(!$('#sl-2').hasClass('btn-success'))
  568. $('#sl-2').removeClass('btn-primary btn-info').addClass('btn-primary');
  569. if(!$('#sl-1').hasClass('btn-success'))
  570. $('#sl-1').removeClass('btn-primary btn-info').addClass('btn-primary');
  571. $('#sl-0').removeClass('btn-primary btn-info btn-success').addClass('btn-info');
  572. streaming.send({message: { request: "configure", substream: 0 }});
  573. });
  574. $('#sl-1').removeClass('btn-primary btn-success').addClass('btn-primary')
  575. .unbind('click').click(function() {
  576. toastr.info("Switching simulcast substream, wait for it... (normal quality)", null, {timeOut: 2000});
  577. if(!$('#sl-2').hasClass('btn-success'))
  578. $('#sl-2').removeClass('btn-primary btn-info').addClass('btn-primary');
  579. $('#sl-1').removeClass('btn-primary btn-info btn-success').addClass('btn-info');
  580. if(!$('#sl-0').hasClass('btn-success'))
  581. $('#sl-0').removeClass('btn-primary btn-info').addClass('btn-primary');
  582. streaming.send({message: { request: "configure", substream: 1 }});
  583. });
  584. $('#sl-2').removeClass('btn-primary btn-success').addClass('btn-primary')
  585. .unbind('click').click(function() {
  586. toastr.info("Switching simulcast substream, wait for it... (higher quality)", null, {timeOut: 2000});
  587. $('#sl-2').removeClass('btn-primary btn-info btn-success').addClass('btn-info');
  588. if(!$('#sl-1').hasClass('btn-success'))
  589. $('#sl-1').removeClass('btn-primary btn-info').addClass('btn-primary');
  590. if(!$('#sl-0').hasClass('btn-success'))
  591. $('#sl-0').removeClass('btn-primary btn-info').addClass('btn-primary');
  592. streaming.send({message: { request: "configure", substream: 2 }});
  593. });
  594. if(!temporal) // No temporal layer support
  595. return;
  596. $('#tl-0').parent().removeClass('hide');
  597. $('#tl-0').removeClass('btn-primary btn-success').addClass('btn-primary')
  598. .unbind('click').click(function() {
  599. toastr.info("Capping simulcast temporal layer, wait for it... (lowest FPS)", null, {timeOut: 2000});
  600. if(!$('#tl-2').hasClass('btn-success'))
  601. $('#tl-2').removeClass('btn-primary btn-info').addClass('btn-primary');
  602. if(!$('#tl-1').hasClass('btn-success'))
  603. $('#tl-1').removeClass('btn-primary btn-info').addClass('btn-primary');
  604. $('#tl-0').removeClass('btn-primary btn-info btn-success').addClass('btn-info');
  605. streaming.send({message: { request: "configure", temporal: 0 }});
  606. });
  607. $('#tl-1').removeClass('btn-primary btn-success').addClass('btn-primary')
  608. .unbind('click').click(function() {
  609. toastr.info("Capping simulcast temporal layer, wait for it... (medium FPS)", null, {timeOut: 2000});
  610. if(!$('#tl-2').hasClass('btn-success'))
  611. $('#tl-2').removeClass('btn-primary btn-info').addClass('btn-primary');
  612. $('#tl-1').removeClass('btn-primary btn-info').addClass('btn-info');
  613. if(!$('#tl-0').hasClass('btn-success'))
  614. $('#tl-0').removeClass('btn-primary btn-info').addClass('btn-primary');
  615. streaming.send({message: { request: "configure", temporal: 1 }});
  616. });
  617. $('#tl-2').removeClass('btn-primary btn-success').addClass('btn-primary')
  618. .unbind('click').click(function() {
  619. toastr.info("Capping simulcast temporal layer, wait for it... (highest FPS)", null, {timeOut: 2000});
  620. $('#tl-2').removeClass('btn-primary btn-info btn-success').addClass('btn-info');
  621. if(!$('#tl-1').hasClass('btn-success'))
  622. $('#tl-1').removeClass('btn-primary btn-info').addClass('btn-primary');
  623. if(!$('#tl-0').hasClass('btn-success'))
  624. $('#tl-0').removeClass('btn-primary btn-info').addClass('btn-primary');
  625. streaming.send({message: { request: "configure", temporal: 2 }});
  626. });
  627. }
  628. function updateSimulcastButtons(substream, temporal) {
  629. // Check the substream
  630. if(substream === 0) {
  631. toastr.success("Switched simulcast substream! (lower quality)", null, {timeOut: 2000});
  632. $('#sl-2').removeClass('btn-primary btn-success').addClass('btn-primary');
  633. $('#sl-1').removeClass('btn-primary btn-success').addClass('btn-primary');
  634. $('#sl-0').removeClass('btn-primary btn-info btn-success').addClass('btn-success');
  635. } else if(substream === 1) {
  636. toastr.success("Switched simulcast substream! (normal quality)", null, {timeOut: 2000});
  637. $('#sl-2').removeClass('btn-primary btn-success').addClass('btn-primary');
  638. $('#sl-1').removeClass('btn-primary btn-info btn-success').addClass('btn-success');
  639. $('#sl-0').removeClass('btn-primary btn-success').addClass('btn-primary');
  640. } else if(substream === 2) {
  641. toastr.success("Switched simulcast substream! (higher quality)", null, {timeOut: 2000});
  642. $('#sl-2').removeClass('btn-primary btn-info btn-success').addClass('btn-success');
  643. $('#sl-1').removeClass('btn-primary btn-success').addClass('btn-primary');
  644. $('#sl-0').removeClass('btn-primary btn-success').addClass('btn-primary');
  645. }
  646. // Check the temporal layer
  647. if(temporal === 0) {
  648. toastr.success("Capped simulcast temporal layer! (lowest FPS)", null, {timeOut: 2000});
  649. $('#tl-2').removeClass('btn-primary btn-success').addClass('btn-primary');
  650. $('#tl-1').removeClass('btn-primary btn-success').addClass('btn-primary');
  651. $('#tl-0').removeClass('btn-primary btn-info btn-success').addClass('btn-success');
  652. } else if(temporal === 1) {
  653. toastr.success("Capped simulcast temporal layer! (medium FPS)", null, {timeOut: 2000});
  654. $('#tl-2').removeClass('btn-primary btn-success').addClass('btn-primary');
  655. $('#tl-1').removeClass('btn-primary btn-info btn-success').addClass('btn-success');
  656. $('#tl-0').removeClass('btn-primary btn-success').addClass('btn-primary');
  657. } else if(temporal === 2) {
  658. toastr.success("Capped simulcast temporal layer! (highest FPS)", null, {timeOut: 2000});
  659. $('#tl-2').removeClass('btn-primary btn-info btn-success').addClass('btn-success');
  660. $('#tl-1').removeClass('btn-primary btn-success').addClass('btn-primary');
  661. $('#tl-0').removeClass('btn-primary btn-success').addClass('btn-primary');
  662. }
  663. }
  664. // Helpers to create SVC-related UI for a new viewer
  665. function addSvcButtons() {
  666. if($('#svc').length > 0)
  667. return;
  668. $('#curres').parent().append(
  669. '<div id="svc" class="btn-group-vertical btn-group-vertical-xs pull-right">' +
  670. ' <div class"row">' +
  671. ' <div class="btn-group btn-group-xs" style="width: 100%">' +
  672. ' <button id="sl-1" type="button" class="btn btn-primary" data-toggle="tooltip" title="Switch to normal resolution" style="width: 50%">SL 1</button>' +
  673. ' <button id="sl-0" type="button" class="btn btn-primary" data-toggle="tooltip" title="Switch to low resolution" style="width: 50%">SL 0</button>' +
  674. ' </div>' +
  675. ' </div>' +
  676. ' <div class"row">' +
  677. ' <div class="btn-group btn-group-xs" style="width: 100%">' +
  678. ' <button id="tl-2" type="button" class="btn btn-primary" data-toggle="tooltip" title="Cap to temporal layer 2 (high FPS)" style="width: 34%">TL 2</button>' +
  679. ' <button id="tl-1" type="button" class="btn btn-primary" data-toggle="tooltip" title="Cap to temporal layer 1 (medium FPS)" style="width: 33%">TL 1</button>' +
  680. ' <button id="tl-0" type="button" class="btn btn-primary" data-toggle="tooltip" title="Cap to temporal layer 0 (low FPS)" style="width: 33%">TL 0</button>' +
  681. ' </div>' +
  682. ' </div>' +
  683. '</div>'
  684. );
  685. // Enable the VP8 simulcast selection buttons
  686. $('#sl-0').removeClass('btn-primary btn-success').addClass('btn-primary')
  687. .unbind('click').click(function() {
  688. toastr.info("Switching SVC spatial layer, wait for it... (low resolution)", null, {timeOut: 2000});
  689. if(!$('#sl-1').hasClass('btn-success'))
  690. $('#sl-1').removeClass('btn-primary btn-info').addClass('btn-primary');
  691. $('#sl-0').removeClass('btn-primary btn-info btn-success').addClass('btn-info');
  692. streaming.send({message: { request: "configure", spatial_layer: 0 }});
  693. });
  694. $('#sl-1').removeClass('btn-primary btn-success').addClass('btn-primary')
  695. .unbind('click').click(function() {
  696. toastr.info("Switching SVC spatial layer, wait for it... (normal resolution)", null, {timeOut: 2000});
  697. $('#sl-1').removeClass('btn-primary btn-info btn-success').addClass('btn-info');
  698. if(!$('#sl-0').hasClass('btn-success'))
  699. $('#sl-0').removeClass('btn-primary btn-info').addClass('btn-primary');
  700. streaming.send({message: { request: "configure", spatial_layer: 1 }});
  701. });
  702. $('#tl-0').removeClass('btn-primary btn-success').addClass('btn-primary')
  703. .unbind('click').click(function() {
  704. toastr.info("Capping SVC temporal layer, wait for it... (lowest FPS)", null, {timeOut: 2000});
  705. if(!$('#tl-2').hasClass('btn-success'))
  706. $('#tl-2').removeClass('btn-primary btn-info').addClass('btn-primary');
  707. if(!$('#tl-1').hasClass('btn-success'))
  708. $('#tl-1').removeClass('btn-primary btn-info').addClass('btn-primary');
  709. $('#tl-0').removeClass('btn-primary btn-info btn-success').addClass('btn-info');
  710. streaming.send({message: { request: "configure", temporal_layer: 0 }});
  711. });
  712. $('#tl-1').removeClass('btn-primary btn-success').addClass('btn-primary')
  713. .unbind('click').click(function() {
  714. toastr.info("Capping SVC temporal layer, wait for it... (medium FPS)", null, {timeOut: 2000});
  715. if(!$('#tl-2').hasClass('btn-success'))
  716. $('#tl-2').removeClass('btn-primary btn-info').addClass('btn-primary');
  717. $('#tl-1').removeClass('btn-primary btn-info').addClass('btn-info');
  718. if(!$('#tl-0').hasClass('btn-success'))
  719. $('#tl-0').removeClass('btn-primary btn-info').addClass('btn-primary');
  720. streaming.send({message: { request: "configure", temporal_layer: 1 }});
  721. });
  722. $('#tl-2').removeClass('btn-primary btn-success').addClass('btn-primary')
  723. .unbind('click').click(function() {
  724. toastr.info("Capping SVC temporal layer, wait for it... (highest FPS)", null, {timeOut: 2000});
  725. $('#tl-2').removeClass('btn-primary btn-info btn-success').addClass('btn-info');
  726. if(!$('#tl-1').hasClass('btn-success'))
  727. $('#tl-1').removeClass('btn-primary btn-info').addClass('btn-primary');
  728. if(!$('#tl-0').hasClass('btn-success'))
  729. $('#tl-0').removeClass('btn-primary btn-info').addClass('btn-primary');
  730. streaming.send({message: { request: "configure", temporal_layer: 2 }});
  731. });
  732. }
  733. function updateSvcButtons(spatial, temporal) {
  734. // Check the spatial layer
  735. if(spatial === 0) {
  736. toastr.success("Switched SVC spatial layer! (lower resolution)", null, {timeOut: 2000});
  737. $('#sl-1').removeClass('btn-primary btn-success').addClass('btn-primary');
  738. $('#sl-0').removeClass('btn-primary btn-info btn-success').addClass('btn-success');
  739. } else if(spatial === 1) {
  740. toastr.success("Switched SVC spatial layer! (normal resolution)", null, {timeOut: 2000});
  741. $('#sl-1').removeClass('btn-primary btn-info btn-success').addClass('btn-success');
  742. $('#sl-0').removeClass('btn-primary btn-success').addClass('btn-primary');
  743. }
  744. // Check the temporal layer
  745. if(temporal === 0) {
  746. toastr.success("Capped SVC temporal layer! (lowest FPS)", null, {timeOut: 2000});
  747. $('#tl-2').removeClass('btn-primary btn-success').addClass('btn-primary');
  748. $('#tl-1').removeClass('btn-primary btn-success').addClass('btn-primary');
  749. $('#tl-0').removeClass('btn-primary btn-info btn-success').addClass('btn-success');
  750. } else if(temporal === 1) {
  751. toastr.success("Capped SVC temporal layer! (medium FPS)", null, {timeOut: 2000});
  752. $('#tl-2').removeClass('btn-primary btn-success').addClass('btn-primary');
  753. $('#tl-1').removeClass('btn-primary btn-info btn-success').addClass('btn-success');
  754. $('#tl-0').removeClass('btn-primary btn-success').addClass('btn-primary');
  755. } else if(temporal === 2) {
  756. toastr.success("Capped SVC temporal layer! (highest FPS)", null, {timeOut: 2000});
  757. $('#tl-2').removeClass('btn-primary btn-info btn-success').addClass('btn-success');
  758. $('#tl-1').removeClass('btn-primary btn-success').addClass('btn-primary');
  759. $('#tl-0').removeClass('btn-primary btn-success').addClass('btn-primary');
  760. }
  761. }
  762. function checkEnter(field, event) {
  763. var theCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
  764. if(theCode == 13) {
  765. registerUsername();
  766. return false;
  767. } else {
  768. return true;
  769. }
  770. }
  771. function registerUsername() {
  772. if($('#username').length === 0) {
  773. // Create fields to register
  774. $('#register').click(registerUsername);
  775. $('#username').focus();
  776. } else {
  777. // Try a registration
  778. $('#username').attr('disabled', true);
  779. $('#register').attr('disabled', true).unbind('click');
  780. var username = $('#username').val();
  781. if(username === "") {
  782. $('#you')
  783. .removeClass().addClass('label label-warning')
  784. .html("Insert your display name (e.g., pippo)");
  785. $('#username').removeAttr('disabled');
  786. $('#register').removeAttr('disabled').click(registerUsername);
  787. return;
  788. }
  789. if(/[^a-zA-Z0-9]/.test(username)) {
  790. $('#you')
  791. .removeClass().addClass('label label-warning')
  792. .html('Input is not alphanumeric');
  793. $('#username').removeAttr('disabled').val("");
  794. $('#register').removeAttr('disabled').click(registerUsername);
  795. return;
  796. }
  797. var register = { "request": "join", "room": myroom, "display": username };
  798. myusername = username;
  799. mixertest.send({"message": register});
  800. }
  801. }