aboutsummaryrefslogtreecommitdiff
path: root/website/mic.js
blob: 4ab1c37262479a33493d35cdf4d0fd85ca641f2a (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: [{urls: "turn:morla.jones.dk", username: "myturn", credential: "notsecure"},
  35. {urls: "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. if(msg["id"]) {
  268. myid = msg["id"];
  269. Janus.log("Successfully joined room " + msg["room"] + " with ID " + myid);
  270. if(!webrtcUp) {
  271. webrtcUp = true;
  272. // Publish our stream
  273. mixertest.createOffer(
  274. {
  275. media: { video: false}, // This is an audio only room
  276. success: function(jsep) {
  277. Janus.debug("Got SDP!");
  278. Janus.debug(jsep);
  279. var publish = { "request": "configure", "muted": true };
  280. mixertest.send({"message": publish, "jsep": jsep});
  281. },
  282. error: function(error) {
  283. Janus.error("WebRTC error:", error);
  284. bootbox.alert("WebRTC error... " + JSON.stringify(error));
  285. }
  286. });
  287. }
  288. }
  289. // Any room participant?
  290. if(msg["participants"] !== undefined && msg["participants"] !== null) {
  291. var list = msg["participants"];
  292. Janus.debug("Got a list of participants:");
  293. Janus.debug(list);
  294. for(var f in list) {
  295. var id = list[f]["id"];
  296. var display = list[f]["display"];
  297. var setup = list[f]["setup"];
  298. var muted = list[f]["muted"];
  299. Janus.debug(" >> [" + id + "] " + display + " (setup=" + setup + ", muted=" + muted + ")");
  300. if($('#rp'+id).length === 0) {
  301. // Add to the participants list
  302. $('#list').append('<li id="rp'+id+'" class="list-group-item">'+display+
  303. ' <i class="absetup fa fa-chain-broken"></i>' +
  304. ' <i class="abmuted fa fa-microphone-slash"></i></li>');
  305. $('#rp'+id + ' > i').hide();
  306. }
  307. if(muted === true || muted === "true")
  308. $('#rp'+id + ' > i.abmuted').removeClass('hide').show();
  309. else
  310. $('#rp'+id + ' > i.abmuted').hide();
  311. if(setup === true || setup === "true")
  312. $('#rp'+id + ' > i.absetup').hide();
  313. else
  314. $('#rp'+id + ' > i.absetup').removeClass('hide').show();
  315. }
  316. }
  317. } else if(event === "roomchanged") {
  318. // The user switched to a different room
  319. myid = msg["id"];
  320. Janus.log("Moved to room " + msg["room"] + ", new ID: " + myid);
  321. // Any room participant?
  322. $('#list').empty();
  323. if(msg["participants"] !== undefined && msg["participants"] !== null) {
  324. var list = msg["participants"];
  325. Janus.debug("Got a list of participants:");
  326. Janus.debug(list);
  327. for(var f in list) {
  328. var id = list[f]["id"];
  329. var display = list[f]["display"];
  330. var setup = list[f]["setup"];
  331. var muted = list[f]["muted"];
  332. Janus.debug(" >> [" + id + "] " + display + " (setup=" + setup + ", muted=" + muted + ")");
  333. if($('#rp'+id).length === 0) {
  334. // Add to the participants list
  335. $('#list').append('<li id="rp'+id+'" class="list-group-item">'+display+
  336. ' <i class="absetup fa fa-chain-broken"></i>' +
  337. ' <i class="abmuted fa fa-microphone-slash"></i></li>');
  338. $('#rp'+id + ' > i').hide();
  339. }
  340. if(muted === true || muted === "true")
  341. $('#rp'+id + ' > i.abmuted').removeClass('hide').show();
  342. else
  343. $('#rp'+id + ' > i.abmuted').hide();
  344. if(setup === true || setup === "true")
  345. $('#rp'+id + ' > i.absetup').hide();
  346. else
  347. $('#rp'+id + ' > i.absetup').removeClass('hide').show();
  348. }
  349. }
  350. } else if(event === "destroyed") {
  351. // The room has been destroyed
  352. Janus.warn("The room has been destroyed!");
  353. bootbox.alert("The room has been destroyed", function() {
  354. window.location.reload();
  355. });
  356. } else if(event === "event") {
  357. if(msg["participants"] !== undefined && msg["participants"] !== null) {
  358. var list = msg["participants"];
  359. Janus.debug("Got a list of participants:");
  360. Janus.debug(list);
  361. for(var f in list) {
  362. var id = list[f]["id"];
  363. var display = list[f]["display"];
  364. var setup = list[f]["setup"];
  365. var muted = list[f]["muted"];
  366. Janus.debug(" >> [" + id + "] " + display + " (setup=" + setup + ", muted=" + muted + ")");
  367. if($('#rp'+id).length === 0) {
  368. // Add to the participants list
  369. $('#list').append('<li id="rp'+id+'" class="list-group-item">'+display+
  370. ' <i class="absetup fa fa-chain-broken"></i>' +
  371. ' <i class="abmuted fa fa-microphone-slash"></i></li>');
  372. $('#rp'+id + ' > i').hide();
  373. }
  374. if(muted === true || muted === "true")
  375. $('#rp'+id + ' > i.abmuted').removeClass('hide').show();
  376. else
  377. $('#rp'+id + ' > i.abmuted').hide();
  378. if(setup === true || setup === "true")
  379. $('#rp'+id + ' > i.absetup').hide();
  380. else
  381. $('#rp'+id + ' > i.absetup').removeClass('hide').show();
  382. }
  383. } else if(msg["error"] !== undefined && msg["error"] !== null) {
  384. if(msg["error_code"] === 485) {
  385. // This is a "no such room" error: give a more meaningful description
  386. bootbox.alert(
  387. "<p>Server error: room <code>" + myroom + "</code> does not exist."
  388. );
  389. } else {
  390. bootbox.alert(msg["error"]);
  391. }
  392. return;
  393. }
  394. // Any new feed to attach to?
  395. if(msg["leaving"] !== undefined && msg["leaving"] !== null) {
  396. // One of the participants has gone away?
  397. var leaving = msg["leaving"];
  398. Janus.log("Participant left: " + leaving + " (we have " + $('#rp'+leaving).length + " elements with ID #rp" +leaving + ")");
  399. $('#rp'+leaving).remove();
  400. }
  401. }
  402. }
  403. if(jsep !== undefined && jsep !== null) {
  404. Janus.debug("Handling SDP as well...");
  405. Janus.debug(jsep);
  406. mixertest.handleRemoteJsep({jsep: jsep});
  407. }
  408. },
  409. onlocalstream: function(stream) {
  410. Janus.debug(" ::: Got a local stream :::");
  411. Janus.debug(stream);
  412. // We're not going to attach the local audio stream
  413. $('#audiojoin').hide();
  414. $('#room').removeClass('hide').show();
  415. $('#participant').removeClass('hide').html(myusername).show();
  416. },
  417. onremotestream: function(stream) {
  418. $('#room').removeClass('hide').show();
  419. var addButtons = false;
  420. if($('#roomaudio').length === 0) {
  421. addButtons = true;
  422. $('#mixedaudio').append('<audio class="rounded centered" id="roomaudio" width="100%" height="100%" autoplay/>');
  423. }
  424. Janus.attachMediaStream($('#roomaudio').get(0), stream);
  425. if(!addButtons)
  426. return;
  427. // Mute button
  428. audioenabled = false;
  429. $('#toggleaudio').click(
  430. function() {
  431. audioenabled = !audioenabled;
  432. if(audioenabled)
  433. $('#toggleaudio').html("Mute").removeClass("btn-success").addClass("btn-danger");
  434. else
  435. $('#toggleaudio').html("Unmute").removeClass("btn-danger").addClass("btn-success");
  436. mixertest.send({message: { "request": "configure", "muted": !audioenabled }});
  437. }).removeClass('hide').show();
  438. },
  439. oncleanup: function() {
  440. webrtcUp = false;
  441. Janus.log(" ::: Got a cleanup notification :::");
  442. $('#participant').empty().hide();
  443. $('#list').empty();
  444. $('#mixedaudio').empty();
  445. $('#room').hide();
  446. }
  447. });
  448. },
  449. error: function(error) {
  450. Janus.error(error);
  451. bootbox.alert(error, function() {
  452. window.location.reload();
  453. });
  454. },
  455. destroyed: function() {
  456. window.location.reload();
  457. }
  458. });
  459. });
  460. }});
  461. });
  462. function updateStreamsList() {
  463. $('#update-streams').unbind('click').addClass('fa-spin');
  464. var body = { "request": "list" };
  465. Janus.debug("Sending message (" + JSON.stringify(body) + ")");
  466. streaming.send({"message": body, success: function(result) {
  467. setTimeout(function() {
  468. $('#update-streams').removeClass('fa-spin').click(updateStreamsList);
  469. }, 500);
  470. if(result === null || result === undefined) {
  471. bootbox.alert("Got no response to our query for available streams");
  472. return;
  473. }
  474. if(result["list"] !== undefined && result["list"] !== null) {
  475. $('#streams').removeClass('hide').show();
  476. $('#streamslist').empty();
  477. $('#watch').attr('disabled', true).unbind('click');
  478. var list = result["list"];
  479. Janus.log("Got a list of available streams");
  480. Janus.debug(list);
  481. for(var mp in list) {
  482. Janus.debug(" >> [" + list[mp]["id"] + "] " + list[mp]["description"] + " (" + list[mp]["type"] + ")");
  483. $('#streamslist').append("<li><a href='#' id='" + list[mp]["id"] + "'>" + list[mp]["description"] + " (" + list[mp]["type"] + ")" + "</a></li>");
  484. }
  485. $('#streamslist a').unbind('click').click(function() {
  486. selectedStream = $(this).attr("id");
  487. $('#streamset').html($(this).html()).parent().removeClass('open');
  488. return false;
  489. });
  490. $('#watch').removeAttr('disabled').unbind('click').click(startStream);
  491. }
  492. }});
  493. }
  494. function startStream() {
  495. Janus.log("Selected video id #" + selectedStream);
  496. if(selectedStream === undefined || selectedStream === null) {
  497. bootbox.alert("Select a stream from the list");
  498. return;
  499. }
  500. $('#streamset').attr('disabled', true);
  501. $('#streamslist').attr('disabled', true);
  502. $('#watch').attr('disabled', true).unbind('click');
  503. var body = { "request": "watch", id: parseInt(selectedStream) };
  504. streaming.send({"message": body});
  505. // No remote video yet
  506. $('#stream').append('<video class="rounded centered" id="waitingvideo" width=320 height=240 />');
  507. if(spinner == null) {
  508. var target = document.getElementById('stream');
  509. spinner = new Spinner({top:100}).spin(target);
  510. } else {
  511. spinner.spin();
  512. }
  513. // Prepare the username registration
  514. myroom = parseInt(selectedStream);
  515. $('#audiojoin').removeClass('hide').show();
  516. $('#registernow').removeClass('hide').show();
  517. $('#register').click(registerUsername);
  518. $('#username').focus();
  519. $('#start').removeAttr('disabled').html("Stop")
  520. .click(function() {
  521. $(this).attr('disabled', true);
  522. janus.destroy();
  523. });
  524. }
  525. function stopStream() {
  526. $('#audiojoin').hide();
  527. $('#watch').attr('disabled', true).unbind('click');
  528. var body = { "request": "stop" };
  529. streaming.send({"message": body});
  530. streaming.hangup();
  531. $('#streamset').removeAttr('disabled');
  532. $('#streamslist').removeAttr('disabled');
  533. $('#watch').html("Watch or Listen").removeAttr('disabled').unbind('click').click(startStream);
  534. $('#status').empty().hide();
  535. $('#bitrate').attr('disabled', true);
  536. $('#bitrateset').html('Bandwidth<span class="caret"></span>');
  537. $('#curbitrate').hide();
  538. if(bitrateTimer !== null && bitrateTimer !== undefined)
  539. clearInterval(bitrateTimer);
  540. bitrateTimer = null;
  541. $('#curres').empty().hide();
  542. $('#simulcast').remove();
  543. simulcastStarted = false;
  544. }
  545. // Helpers to create Simulcast-related UI, if enabled
  546. function addSimulcastButtons(temporal) {
  547. $('#curres').parent().append(
  548. '<div id="simulcast" class="btn-group-vertical btn-group-vertical-xs pull-right">' +
  549. ' <div class"row">' +
  550. ' <div class="btn-group btn-group-xs" style="width: 100%">' +
  551. ' <button id="sl-2" type="button" class="btn btn-primary" data-toggle="tooltip" title="Switch to higher quality" style="width: 33%">SL 2</button>' +
  552. ' <button id="sl-1" type="button" class="btn btn-primary" data-toggle="tooltip" title="Switch to normal quality" style="width: 33%">SL 1</button>' +
  553. ' <button id="sl-0" type="button" class="btn btn-primary" data-toggle="tooltip" title="Switch to lower quality" style="width: 34%">SL 0</button>' +
  554. ' </div>' +
  555. ' </div>' +
  556. ' <div class"row">' +
  557. ' <div class="btn-group btn-group-xs hide" style="width: 100%">' +
  558. ' <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>' +
  559. ' <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>' +
  560. ' <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>' +
  561. ' </div>' +
  562. ' </div>' +
  563. '</div>');
  564. // Enable the simulcast selection buttons
  565. $('#sl-0').removeClass('btn-primary btn-success').addClass('btn-primary')
  566. .unbind('click').click(function() {
  567. toastr.info("Switching simulcast substream, wait for it... (lower quality)", null, {timeOut: 2000});
  568. if(!$('#sl-2').hasClass('btn-success'))
  569. $('#sl-2').removeClass('btn-primary btn-info').addClass('btn-primary');
  570. if(!$('#sl-1').hasClass('btn-success'))
  571. $('#sl-1').removeClass('btn-primary btn-info').addClass('btn-primary');
  572. $('#sl-0').removeClass('btn-primary btn-info btn-success').addClass('btn-info');
  573. streaming.send({message: { request: "configure", substream: 0 }});
  574. });
  575. $('#sl-1').removeClass('btn-primary btn-success').addClass('btn-primary')
  576. .unbind('click').click(function() {
  577. toastr.info("Switching simulcast substream, wait for it... (normal quality)", null, {timeOut: 2000});
  578. if(!$('#sl-2').hasClass('btn-success'))
  579. $('#sl-2').removeClass('btn-primary btn-info').addClass('btn-primary');
  580. $('#sl-1').removeClass('btn-primary btn-info btn-success').addClass('btn-info');
  581. if(!$('#sl-0').hasClass('btn-success'))
  582. $('#sl-0').removeClass('btn-primary btn-info').addClass('btn-primary');
  583. streaming.send({message: { request: "configure", substream: 1 }});
  584. });
  585. $('#sl-2').removeClass('btn-primary btn-success').addClass('btn-primary')
  586. .unbind('click').click(function() {
  587. toastr.info("Switching simulcast substream, wait for it... (higher quality)", null, {timeOut: 2000});
  588. $('#sl-2').removeClass('btn-primary btn-info btn-success').addClass('btn-info');
  589. if(!$('#sl-1').hasClass('btn-success'))
  590. $('#sl-1').removeClass('btn-primary btn-info').addClass('btn-primary');
  591. if(!$('#sl-0').hasClass('btn-success'))
  592. $('#sl-0').removeClass('btn-primary btn-info').addClass('btn-primary');
  593. streaming.send({message: { request: "configure", substream: 2 }});
  594. });
  595. if(!temporal) // No temporal layer support
  596. return;
  597. $('#tl-0').parent().removeClass('hide');
  598. $('#tl-0').removeClass('btn-primary btn-success').addClass('btn-primary')
  599. .unbind('click').click(function() {
  600. toastr.info("Capping simulcast temporal layer, wait for it... (lowest FPS)", null, {timeOut: 2000});
  601. if(!$('#tl-2').hasClass('btn-success'))
  602. $('#tl-2').removeClass('btn-primary btn-info').addClass('btn-primary');
  603. if(!$('#tl-1').hasClass('btn-success'))
  604. $('#tl-1').removeClass('btn-primary btn-info').addClass('btn-primary');
  605. $('#tl-0').removeClass('btn-primary btn-info btn-success').addClass('btn-info');
  606. streaming.send({message: { request: "configure", temporal: 0 }});
  607. });
  608. $('#tl-1').removeClass('btn-primary btn-success').addClass('btn-primary')
  609. .unbind('click').click(function() {
  610. toastr.info("Capping simulcast temporal layer, wait for it... (medium FPS)", null, {timeOut: 2000});
  611. if(!$('#tl-2').hasClass('btn-success'))
  612. $('#tl-2').removeClass('btn-primary btn-info').addClass('btn-primary');
  613. $('#tl-1').removeClass('btn-primary btn-info').addClass('btn-info');
  614. if(!$('#tl-0').hasClass('btn-success'))
  615. $('#tl-0').removeClass('btn-primary btn-info').addClass('btn-primary');
  616. streaming.send({message: { request: "configure", temporal: 1 }});
  617. });
  618. $('#tl-2').removeClass('btn-primary btn-success').addClass('btn-primary')
  619. .unbind('click').click(function() {
  620. toastr.info("Capping simulcast temporal layer, wait for it... (highest FPS)", null, {timeOut: 2000});
  621. $('#tl-2').removeClass('btn-primary btn-info btn-success').addClass('btn-info');
  622. if(!$('#tl-1').hasClass('btn-success'))
  623. $('#tl-1').removeClass('btn-primary btn-info').addClass('btn-primary');
  624. if(!$('#tl-0').hasClass('btn-success'))
  625. $('#tl-0').removeClass('btn-primary btn-info').addClass('btn-primary');
  626. streaming.send({message: { request: "configure", temporal: 2 }});
  627. });
  628. }
  629. function updateSimulcastButtons(substream, temporal) {
  630. // Check the substream
  631. if(substream === 0) {
  632. toastr.success("Switched simulcast substream! (lower quality)", null, {timeOut: 2000});
  633. $('#sl-2').removeClass('btn-primary btn-success').addClass('btn-primary');
  634. $('#sl-1').removeClass('btn-primary btn-success').addClass('btn-primary');
  635. $('#sl-0').removeClass('btn-primary btn-info btn-success').addClass('btn-success');
  636. } else if(substream === 1) {
  637. toastr.success("Switched simulcast substream! (normal quality)", null, {timeOut: 2000});
  638. $('#sl-2').removeClass('btn-primary btn-success').addClass('btn-primary');
  639. $('#sl-1').removeClass('btn-primary btn-info btn-success').addClass('btn-success');
  640. $('#sl-0').removeClass('btn-primary btn-success').addClass('btn-primary');
  641. } else if(substream === 2) {
  642. toastr.success("Switched simulcast substream! (higher quality)", null, {timeOut: 2000});
  643. $('#sl-2').removeClass('btn-primary btn-info btn-success').addClass('btn-success');
  644. $('#sl-1').removeClass('btn-primary btn-success').addClass('btn-primary');
  645. $('#sl-0').removeClass('btn-primary btn-success').addClass('btn-primary');
  646. }
  647. // Check the temporal layer
  648. if(temporal === 0) {
  649. toastr.success("Capped simulcast temporal layer! (lowest FPS)", null, {timeOut: 2000});
  650. $('#tl-2').removeClass('btn-primary btn-success').addClass('btn-primary');
  651. $('#tl-1').removeClass('btn-primary btn-success').addClass('btn-primary');
  652. $('#tl-0').removeClass('btn-primary btn-info btn-success').addClass('btn-success');
  653. } else if(temporal === 1) {
  654. toastr.success("Capped simulcast temporal layer! (medium FPS)", null, {timeOut: 2000});
  655. $('#tl-2').removeClass('btn-primary btn-success').addClass('btn-primary');
  656. $('#tl-1').removeClass('btn-primary btn-info btn-success').addClass('btn-success');
  657. $('#tl-0').removeClass('btn-primary btn-success').addClass('btn-primary');
  658. } else if(temporal === 2) {
  659. toastr.success("Capped simulcast temporal layer! (highest FPS)", null, {timeOut: 2000});
  660. $('#tl-2').removeClass('btn-primary btn-info btn-success').addClass('btn-success');
  661. $('#tl-1').removeClass('btn-primary btn-success').addClass('btn-primary');
  662. $('#tl-0').removeClass('btn-primary btn-success').addClass('btn-primary');
  663. }
  664. }
  665. // Helpers to create SVC-related UI for a new viewer
  666. function addSvcButtons() {
  667. if($('#svc').length > 0)
  668. return;
  669. $('#curres').parent().append(
  670. '<div id="svc" class="btn-group-vertical btn-group-vertical-xs pull-right">' +
  671. ' <div class"row">' +
  672. ' <div class="btn-group btn-group-xs" style="width: 100%">' +
  673. ' <button id="sl-1" type="button" class="btn btn-primary" data-toggle="tooltip" title="Switch to normal resolution" style="width: 50%">SL 1</button>' +
  674. ' <button id="sl-0" type="button" class="btn btn-primary" data-toggle="tooltip" title="Switch to low resolution" style="width: 50%">SL 0</button>' +
  675. ' </div>' +
  676. ' </div>' +
  677. ' <div class"row">' +
  678. ' <div class="btn-group btn-group-xs" style="width: 100%">' +
  679. ' <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>' +
  680. ' <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>' +
  681. ' <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>' +
  682. ' </div>' +
  683. ' </div>' +
  684. '</div>'
  685. );
  686. // Enable the VP8 simulcast selection buttons
  687. $('#sl-0').removeClass('btn-primary btn-success').addClass('btn-primary')
  688. .unbind('click').click(function() {
  689. toastr.info("Switching SVC spatial layer, wait for it... (low resolution)", null, {timeOut: 2000});
  690. if(!$('#sl-1').hasClass('btn-success'))
  691. $('#sl-1').removeClass('btn-primary btn-info').addClass('btn-primary');
  692. $('#sl-0').removeClass('btn-primary btn-info btn-success').addClass('btn-info');
  693. streaming.send({message: { request: "configure", spatial_layer: 0 }});
  694. });
  695. $('#sl-1').removeClass('btn-primary btn-success').addClass('btn-primary')
  696. .unbind('click').click(function() {
  697. toastr.info("Switching SVC spatial layer, wait for it... (normal resolution)", null, {timeOut: 2000});
  698. $('#sl-1').removeClass('btn-primary btn-info btn-success').addClass('btn-info');
  699. if(!$('#sl-0').hasClass('btn-success'))
  700. $('#sl-0').removeClass('btn-primary btn-info').addClass('btn-primary');
  701. streaming.send({message: { request: "configure", spatial_layer: 1 }});
  702. });
  703. $('#tl-0').removeClass('btn-primary btn-success').addClass('btn-primary')
  704. .unbind('click').click(function() {
  705. toastr.info("Capping SVC temporal layer, wait for it... (lowest FPS)", null, {timeOut: 2000});
  706. if(!$('#tl-2').hasClass('btn-success'))
  707. $('#tl-2').removeClass('btn-primary btn-info').addClass('btn-primary');
  708. if(!$('#tl-1').hasClass('btn-success'))
  709. $('#tl-1').removeClass('btn-primary btn-info').addClass('btn-primary');
  710. $('#tl-0').removeClass('btn-primary btn-info btn-success').addClass('btn-info');
  711. streaming.send({message: { request: "configure", temporal_layer: 0 }});
  712. });
  713. $('#tl-1').removeClass('btn-primary btn-success').addClass('btn-primary')
  714. .unbind('click').click(function() {
  715. toastr.info("Capping SVC temporal layer, wait for it... (medium FPS)", null, {timeOut: 2000});
  716. if(!$('#tl-2').hasClass('btn-success'))
  717. $('#tl-2').removeClass('btn-primary btn-info').addClass('btn-primary');
  718. $('#tl-1').removeClass('btn-primary btn-info').addClass('btn-info');
  719. if(!$('#tl-0').hasClass('btn-success'))
  720. $('#tl-0').removeClass('btn-primary btn-info').addClass('btn-primary');
  721. streaming.send({message: { request: "configure", temporal_layer: 1 }});
  722. });
  723. $('#tl-2').removeClass('btn-primary btn-success').addClass('btn-primary')
  724. .unbind('click').click(function() {
  725. toastr.info("Capping SVC temporal layer, wait for it... (highest FPS)", null, {timeOut: 2000});
  726. $('#tl-2').removeClass('btn-primary btn-info btn-success').addClass('btn-info');
  727. if(!$('#tl-1').hasClass('btn-success'))
  728. $('#tl-1').removeClass('btn-primary btn-info').addClass('btn-primary');
  729. if(!$('#tl-0').hasClass('btn-success'))
  730. $('#tl-0').removeClass('btn-primary btn-info').addClass('btn-primary');
  731. streaming.send({message: { request: "configure", temporal_layer: 2 }});
  732. });
  733. }
  734. function updateSvcButtons(spatial, temporal) {
  735. // Check the spatial layer
  736. if(spatial === 0) {
  737. toastr.success("Switched SVC spatial layer! (lower resolution)", null, {timeOut: 2000});
  738. $('#sl-1').removeClass('btn-primary btn-success').addClass('btn-primary');
  739. $('#sl-0').removeClass('btn-primary btn-info btn-success').addClass('btn-success');
  740. } else if(spatial === 1) {
  741. toastr.success("Switched SVC spatial layer! (normal resolution)", null, {timeOut: 2000});
  742. $('#sl-1').removeClass('btn-primary btn-info btn-success').addClass('btn-success');
  743. $('#sl-0').removeClass('btn-primary btn-success').addClass('btn-primary');
  744. }
  745. // Check the temporal layer
  746. if(temporal === 0) {
  747. toastr.success("Capped SVC temporal layer! (lowest FPS)", null, {timeOut: 2000});
  748. $('#tl-2').removeClass('btn-primary btn-success').addClass('btn-primary');
  749. $('#tl-1').removeClass('btn-primary btn-success').addClass('btn-primary');
  750. $('#tl-0').removeClass('btn-primary btn-info btn-success').addClass('btn-success');
  751. } else if(temporal === 1) {
  752. toastr.success("Capped SVC temporal layer! (medium FPS)", null, {timeOut: 2000});
  753. $('#tl-2').removeClass('btn-primary btn-success').addClass('btn-primary');
  754. $('#tl-1').removeClass('btn-primary btn-info btn-success').addClass('btn-success');
  755. $('#tl-0').removeClass('btn-primary btn-success').addClass('btn-primary');
  756. } else if(temporal === 2) {
  757. toastr.success("Capped SVC temporal layer! (highest FPS)", null, {timeOut: 2000});
  758. $('#tl-2').removeClass('btn-primary btn-info btn-success').addClass('btn-success');
  759. $('#tl-1').removeClass('btn-primary btn-success').addClass('btn-primary');
  760. $('#tl-0').removeClass('btn-primary btn-success').addClass('btn-primary');
  761. }
  762. }
  763. function checkEnter(field, event) {
  764. var theCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
  765. if(theCode == 13) {
  766. registerUsername();
  767. return false;
  768. } else {
  769. return true;
  770. }
  771. }
  772. function registerUsername() {
  773. if($('#username').length === 0) {
  774. // Create fields to register
  775. $('#register').click(registerUsername);
  776. $('#username').focus();
  777. } else {
  778. // Try a registration
  779. $('#username').attr('disabled', true);
  780. $('#register').attr('disabled', true).unbind('click');
  781. var username = $('#username').val();
  782. if(username === "") {
  783. $('#you')
  784. .removeClass().addClass('label label-warning')
  785. .html("Insert your display name (e.g., pippo)");
  786. $('#username').removeAttr('disabled');
  787. $('#register').removeAttr('disabled').click(registerUsername);
  788. return;
  789. }
  790. if(/[^a-zA-Z0-9]/.test(username)) {
  791. $('#you')
  792. .removeClass().addClass('label label-warning')
  793. .html('Input is not alphanumeric');
  794. $('#username').removeAttr('disabled').val("");
  795. $('#register').removeAttr('disabled').click(registerUsername);
  796. return;
  797. }
  798. var register = { "request": "join", "room": myroom, "display": username };
  799. myusername = username;
  800. mixertest.send({"message": register});
  801. }
  802. }