aboutsummaryrefslogtreecommitdiff
path: root/website/mic.js
blob: 4b8761572d70f5fad8cdde643641d08e03f3b089 (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. $('#update-streams').unbind('click').addClass('fa-spin');
  462. var body = { "request": "list" };
  463. Janus.debug("Sending message (" + JSON.stringify(body) + ")");
  464. streaming.send({"message": body, success: function(result) {
  465. setTimeout(function() {
  466. $('#update-streams').removeClass('fa-spin').click(updateStreamsList);
  467. }, 500);
  468. if(result === null || result === undefined) {
  469. bootbox.alert("Got no response to our query for available streams");
  470. return;
  471. }
  472. if(result["list"] !== undefined && result["list"] !== null) {
  473. $('#streams').removeClass('hide').show();
  474. $('#streamslist').empty();
  475. $('#watch').attr('disabled', true).unbind('click');
  476. var list = result["list"];
  477. Janus.log("Got a list of available streams");
  478. Janus.debug(list);
  479. for(var mp in list) {
  480. Janus.debug(" >> [" + list[mp]["id"] + "] " + list[mp]["description"] + " (" + list[mp]["type"] + ")");
  481. $('#streamslist').append("<li><a href='#' id='" + list[mp]["id"] + "'>" + list[mp]["description"] + " (" + list[mp]["type"] + ")" + "</a></li>");
  482. }
  483. $('#streamslist a').unbind('click').click(function() {
  484. selectedStream = $(this).attr("id");
  485. $('#streamset').html($(this).html()).parent().removeClass('open');
  486. return false;
  487. });
  488. $('#watch').removeAttr('disabled').unbind('click').click(startStream);
  489. }
  490. }});
  491. }
  492. function startStream() {
  493. Janus.log("Selected video id #" + selectedStream);
  494. if(selectedStream === undefined || selectedStream === null) {
  495. bootbox.alert("Select a stream from the list");
  496. return;
  497. }
  498. $('#streamset').attr('disabled', true);
  499. $('#streamslist').attr('disabled', true);
  500. $('#watch').attr('disabled', true).unbind('click');
  501. var body = { "request": "watch", id: parseInt(selectedStream) };
  502. streaming.send({"message": body});
  503. // No remote video yet
  504. $('#stream').append('<video class="rounded centered" id="waitingvideo" width=320 height=240 />');
  505. if(spinner == null) {
  506. var target = document.getElementById('stream');
  507. spinner = new Spinner({top:100}).spin(target);
  508. } else {
  509. spinner.spin();
  510. }
  511. // Prepare the username registration
  512. myroom = parseInt(selectedStream);
  513. $('#audiojoin').removeClass('hide').show();
  514. $('#registernow').removeClass('hide').show();
  515. $('#register').click(registerUsername);
  516. $('#username').focus();
  517. $('#start').removeAttr('disabled').html("Stop")
  518. .click(function() {
  519. $(this).attr('disabled', true);
  520. janus.destroy();
  521. });
  522. }
  523. function stopStream() {
  524. $('#audiojoin').hide();
  525. $('#watch').attr('disabled', true).unbind('click');
  526. var body = { "request": "stop" };
  527. streaming.send({"message": body});
  528. streaming.hangup();
  529. $('#streamset').removeAttr('disabled');
  530. $('#streamslist').removeAttr('disabled');
  531. $('#watch').html("Watch or Listen").removeAttr('disabled').unbind('click').click(startStream);
  532. $('#status').empty().hide();
  533. $('#bitrate').attr('disabled', true);
  534. $('#bitrateset').html('Bandwidth<span class="caret"></span>');
  535. $('#curbitrate').hide();
  536. if(bitrateTimer !== null && bitrateTimer !== undefined)
  537. clearInterval(bitrateTimer);
  538. bitrateTimer = null;
  539. $('#curres').empty().hide();
  540. $('#simulcast').remove();
  541. simulcastStarted = false;
  542. }
  543. // Helpers to create Simulcast-related UI, if enabled
  544. function addSimulcastButtons(temporal) {
  545. $('#curres').parent().append(
  546. '<div id="simulcast" class="btn-group-vertical btn-group-vertical-xs pull-right">' +
  547. ' <div class"row">' +
  548. ' <div class="btn-group btn-group-xs" style="width: 100%">' +
  549. ' <button id="sl-2" type="button" class="btn btn-primary" data-toggle="tooltip" title="Switch to higher quality" style="width: 33%">SL 2</button>' +
  550. ' <button id="sl-1" type="button" class="btn btn-primary" data-toggle="tooltip" title="Switch to normal quality" style="width: 33%">SL 1</button>' +
  551. ' <button id="sl-0" type="button" class="btn btn-primary" data-toggle="tooltip" title="Switch to lower quality" style="width: 34%">SL 0</button>' +
  552. ' </div>' +
  553. ' </div>' +
  554. ' <div class"row">' +
  555. ' <div class="btn-group btn-group-xs hide" style="width: 100%">' +
  556. ' <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>' +
  557. ' <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>' +
  558. ' <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>' +
  559. ' </div>' +
  560. ' </div>' +
  561. '</div>');
  562. // Enable the simulcast selection buttons
  563. $('#sl-0').removeClass('btn-primary btn-success').addClass('btn-primary')
  564. .unbind('click').click(function() {
  565. toastr.info("Switching simulcast substream, wait for it... (lower quality)", null, {timeOut: 2000});
  566. if(!$('#sl-2').hasClass('btn-success'))
  567. $('#sl-2').removeClass('btn-primary btn-info').addClass('btn-primary');
  568. if(!$('#sl-1').hasClass('btn-success'))
  569. $('#sl-1').removeClass('btn-primary btn-info').addClass('btn-primary');
  570. $('#sl-0').removeClass('btn-primary btn-info btn-success').addClass('btn-info');
  571. streaming.send({message: { request: "configure", substream: 0 }});
  572. });
  573. $('#sl-1').removeClass('btn-primary btn-success').addClass('btn-primary')
  574. .unbind('click').click(function() {
  575. toastr.info("Switching simulcast substream, wait for it... (normal quality)", null, {timeOut: 2000});
  576. if(!$('#sl-2').hasClass('btn-success'))
  577. $('#sl-2').removeClass('btn-primary btn-info').addClass('btn-primary');
  578. $('#sl-1').removeClass('btn-primary btn-info btn-success').addClass('btn-info');
  579. if(!$('#sl-0').hasClass('btn-success'))
  580. $('#sl-0').removeClass('btn-primary btn-info').addClass('btn-primary');
  581. streaming.send({message: { request: "configure", substream: 1 }});
  582. });
  583. $('#sl-2').removeClass('btn-primary btn-success').addClass('btn-primary')
  584. .unbind('click').click(function() {
  585. toastr.info("Switching simulcast substream, wait for it... (higher quality)", null, {timeOut: 2000});
  586. $('#sl-2').removeClass('btn-primary btn-info btn-success').addClass('btn-info');
  587. if(!$('#sl-1').hasClass('btn-success'))
  588. $('#sl-1').removeClass('btn-primary btn-info').addClass('btn-primary');
  589. if(!$('#sl-0').hasClass('btn-success'))
  590. $('#sl-0').removeClass('btn-primary btn-info').addClass('btn-primary');
  591. streaming.send({message: { request: "configure", substream: 2 }});
  592. });
  593. if(!temporal) // No temporal layer support
  594. return;
  595. $('#tl-0').parent().removeClass('hide');
  596. $('#tl-0').removeClass('btn-primary btn-success').addClass('btn-primary')
  597. .unbind('click').click(function() {
  598. toastr.info("Capping simulcast temporal layer, wait for it... (lowest FPS)", null, {timeOut: 2000});
  599. if(!$('#tl-2').hasClass('btn-success'))
  600. $('#tl-2').removeClass('btn-primary btn-info').addClass('btn-primary');
  601. if(!$('#tl-1').hasClass('btn-success'))
  602. $('#tl-1').removeClass('btn-primary btn-info').addClass('btn-primary');
  603. $('#tl-0').removeClass('btn-primary btn-info btn-success').addClass('btn-info');
  604. streaming.send({message: { request: "configure", temporal: 0 }});
  605. });
  606. $('#tl-1').removeClass('btn-primary btn-success').addClass('btn-primary')
  607. .unbind('click').click(function() {
  608. toastr.info("Capping simulcast temporal layer, wait for it... (medium FPS)", null, {timeOut: 2000});
  609. if(!$('#tl-2').hasClass('btn-success'))
  610. $('#tl-2').removeClass('btn-primary btn-info').addClass('btn-primary');
  611. $('#tl-1').removeClass('btn-primary btn-info').addClass('btn-info');
  612. if(!$('#tl-0').hasClass('btn-success'))
  613. $('#tl-0').removeClass('btn-primary btn-info').addClass('btn-primary');
  614. streaming.send({message: { request: "configure", temporal: 1 }});
  615. });
  616. $('#tl-2').removeClass('btn-primary btn-success').addClass('btn-primary')
  617. .unbind('click').click(function() {
  618. toastr.info("Capping simulcast temporal layer, wait for it... (highest FPS)", null, {timeOut: 2000});
  619. $('#tl-2').removeClass('btn-primary btn-info btn-success').addClass('btn-info');
  620. if(!$('#tl-1').hasClass('btn-success'))
  621. $('#tl-1').removeClass('btn-primary btn-info').addClass('btn-primary');
  622. if(!$('#tl-0').hasClass('btn-success'))
  623. $('#tl-0').removeClass('btn-primary btn-info').addClass('btn-primary');
  624. streaming.send({message: { request: "configure", temporal: 2 }});
  625. });
  626. }
  627. function updateSimulcastButtons(substream, temporal) {
  628. // Check the substream
  629. if(substream === 0) {
  630. toastr.success("Switched simulcast substream! (lower quality)", null, {timeOut: 2000});
  631. $('#sl-2').removeClass('btn-primary btn-success').addClass('btn-primary');
  632. $('#sl-1').removeClass('btn-primary btn-success').addClass('btn-primary');
  633. $('#sl-0').removeClass('btn-primary btn-info btn-success').addClass('btn-success');
  634. } else if(substream === 1) {
  635. toastr.success("Switched simulcast substream! (normal quality)", null, {timeOut: 2000});
  636. $('#sl-2').removeClass('btn-primary btn-success').addClass('btn-primary');
  637. $('#sl-1').removeClass('btn-primary btn-info btn-success').addClass('btn-success');
  638. $('#sl-0').removeClass('btn-primary btn-success').addClass('btn-primary');
  639. } else if(substream === 2) {
  640. toastr.success("Switched simulcast substream! (higher quality)", null, {timeOut: 2000});
  641. $('#sl-2').removeClass('btn-primary btn-info btn-success').addClass('btn-success');
  642. $('#sl-1').removeClass('btn-primary btn-success').addClass('btn-primary');
  643. $('#sl-0').removeClass('btn-primary btn-success').addClass('btn-primary');
  644. }
  645. // Check the temporal layer
  646. if(temporal === 0) {
  647. toastr.success("Capped simulcast temporal layer! (lowest FPS)", null, {timeOut: 2000});
  648. $('#tl-2').removeClass('btn-primary btn-success').addClass('btn-primary');
  649. $('#tl-1').removeClass('btn-primary btn-success').addClass('btn-primary');
  650. $('#tl-0').removeClass('btn-primary btn-info btn-success').addClass('btn-success');
  651. } else if(temporal === 1) {
  652. toastr.success("Capped simulcast temporal layer! (medium FPS)", null, {timeOut: 2000});
  653. $('#tl-2').removeClass('btn-primary btn-success').addClass('btn-primary');
  654. $('#tl-1').removeClass('btn-primary btn-info btn-success').addClass('btn-success');
  655. $('#tl-0').removeClass('btn-primary btn-success').addClass('btn-primary');
  656. } else if(temporal === 2) {
  657. toastr.success("Capped simulcast temporal layer! (highest FPS)", null, {timeOut: 2000});
  658. $('#tl-2').removeClass('btn-primary btn-info btn-success').addClass('btn-success');
  659. $('#tl-1').removeClass('btn-primary btn-success').addClass('btn-primary');
  660. $('#tl-0').removeClass('btn-primary btn-success').addClass('btn-primary');
  661. }
  662. }
  663. // Helpers to create SVC-related UI for a new viewer
  664. function addSvcButtons() {
  665. if($('#svc').length > 0)
  666. return;
  667. $('#curres').parent().append(
  668. '<div id="svc" class="btn-group-vertical btn-group-vertical-xs pull-right">' +
  669. ' <div class"row">' +
  670. ' <div class="btn-group btn-group-xs" style="width: 100%">' +
  671. ' <button id="sl-1" type="button" class="btn btn-primary" data-toggle="tooltip" title="Switch to normal resolution" style="width: 50%">SL 1</button>' +
  672. ' <button id="sl-0" type="button" class="btn btn-primary" data-toggle="tooltip" title="Switch to low resolution" style="width: 50%">SL 0</button>' +
  673. ' </div>' +
  674. ' </div>' +
  675. ' <div class"row">' +
  676. ' <div class="btn-group btn-group-xs" style="width: 100%">' +
  677. ' <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>' +
  678. ' <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>' +
  679. ' <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>' +
  680. ' </div>' +
  681. ' </div>' +
  682. '</div>'
  683. );
  684. // Enable the VP8 simulcast selection buttons
  685. $('#sl-0').removeClass('btn-primary btn-success').addClass('btn-primary')
  686. .unbind('click').click(function() {
  687. toastr.info("Switching SVC spatial layer, wait for it... (low resolution)", null, {timeOut: 2000});
  688. if(!$('#sl-1').hasClass('btn-success'))
  689. $('#sl-1').removeClass('btn-primary btn-info').addClass('btn-primary');
  690. $('#sl-0').removeClass('btn-primary btn-info btn-success').addClass('btn-info');
  691. streaming.send({message: { request: "configure", spatial_layer: 0 }});
  692. });
  693. $('#sl-1').removeClass('btn-primary btn-success').addClass('btn-primary')
  694. .unbind('click').click(function() {
  695. toastr.info("Switching SVC spatial layer, wait for it... (normal resolution)", null, {timeOut: 2000});
  696. $('#sl-1').removeClass('btn-primary btn-info btn-success').addClass('btn-info');
  697. if(!$('#sl-0').hasClass('btn-success'))
  698. $('#sl-0').removeClass('btn-primary btn-info').addClass('btn-primary');
  699. streaming.send({message: { request: "configure", spatial_layer: 1 }});
  700. });
  701. $('#tl-0').removeClass('btn-primary btn-success').addClass('btn-primary')
  702. .unbind('click').click(function() {
  703. toastr.info("Capping SVC temporal layer, wait for it... (lowest FPS)", null, {timeOut: 2000});
  704. if(!$('#tl-2').hasClass('btn-success'))
  705. $('#tl-2').removeClass('btn-primary btn-info').addClass('btn-primary');
  706. if(!$('#tl-1').hasClass('btn-success'))
  707. $('#tl-1').removeClass('btn-primary btn-info').addClass('btn-primary');
  708. $('#tl-0').removeClass('btn-primary btn-info btn-success').addClass('btn-info');
  709. streaming.send({message: { request: "configure", temporal_layer: 0 }});
  710. });
  711. $('#tl-1').removeClass('btn-primary btn-success').addClass('btn-primary')
  712. .unbind('click').click(function() {
  713. toastr.info("Capping SVC temporal layer, wait for it... (medium FPS)", null, {timeOut: 2000});
  714. if(!$('#tl-2').hasClass('btn-success'))
  715. $('#tl-2').removeClass('btn-primary btn-info').addClass('btn-primary');
  716. $('#tl-1').removeClass('btn-primary btn-info').addClass('btn-info');
  717. if(!$('#tl-0').hasClass('btn-success'))
  718. $('#tl-0').removeClass('btn-primary btn-info').addClass('btn-primary');
  719. streaming.send({message: { request: "configure", temporal_layer: 1 }});
  720. });
  721. $('#tl-2').removeClass('btn-primary btn-success').addClass('btn-primary')
  722. .unbind('click').click(function() {
  723. toastr.info("Capping SVC temporal layer, wait for it... (highest FPS)", null, {timeOut: 2000});
  724. $('#tl-2').removeClass('btn-primary btn-info btn-success').addClass('btn-info');
  725. if(!$('#tl-1').hasClass('btn-success'))
  726. $('#tl-1').removeClass('btn-primary btn-info').addClass('btn-primary');
  727. if(!$('#tl-0').hasClass('btn-success'))
  728. $('#tl-0').removeClass('btn-primary btn-info').addClass('btn-primary');
  729. streaming.send({message: { request: "configure", temporal_layer: 2 }});
  730. });
  731. }
  732. function updateSvcButtons(spatial, temporal) {
  733. // Check the spatial layer
  734. if(spatial === 0) {
  735. toastr.success("Switched SVC spatial layer! (lower resolution)", null, {timeOut: 2000});
  736. $('#sl-1').removeClass('btn-primary btn-success').addClass('btn-primary');
  737. $('#sl-0').removeClass('btn-primary btn-info btn-success').addClass('btn-success');
  738. } else if(spatial === 1) {
  739. toastr.success("Switched SVC spatial layer! (normal resolution)", null, {timeOut: 2000});
  740. $('#sl-1').removeClass('btn-primary btn-info btn-success').addClass('btn-success');
  741. $('#sl-0').removeClass('btn-primary btn-success').addClass('btn-primary');
  742. }
  743. // Check the temporal layer
  744. if(temporal === 0) {
  745. toastr.success("Capped SVC temporal layer! (lowest FPS)", null, {timeOut: 2000});
  746. $('#tl-2').removeClass('btn-primary btn-success').addClass('btn-primary');
  747. $('#tl-1').removeClass('btn-primary btn-success').addClass('btn-primary');
  748. $('#tl-0').removeClass('btn-primary btn-info btn-success').addClass('btn-success');
  749. } else if(temporal === 1) {
  750. toastr.success("Capped SVC temporal layer! (medium FPS)", null, {timeOut: 2000});
  751. $('#tl-2').removeClass('btn-primary btn-success').addClass('btn-primary');
  752. $('#tl-1').removeClass('btn-primary btn-info btn-success').addClass('btn-success');
  753. $('#tl-0').removeClass('btn-primary btn-success').addClass('btn-primary');
  754. } else if(temporal === 2) {
  755. toastr.success("Capped SVC temporal layer! (highest FPS)", null, {timeOut: 2000});
  756. $('#tl-2').removeClass('btn-primary btn-info btn-success').addClass('btn-success');
  757. $('#tl-1').removeClass('btn-primary btn-success').addClass('btn-primary');
  758. $('#tl-0').removeClass('btn-primary btn-success').addClass('btn-primary');
  759. }
  760. }
  761. function checkEnter(field, event) {
  762. var theCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
  763. if(theCode == 13) {
  764. registerUsername();
  765. return false;
  766. } else {
  767. return true;
  768. }
  769. }
  770. function registerUsername() {
  771. if($('#username').length === 0) {
  772. // Create fields to register
  773. $('#register').click(registerUsername);
  774. $('#username').focus();
  775. } else {
  776. // Try a registration
  777. $('#username').attr('disabled', true);
  778. $('#register').attr('disabled', true).unbind('click');
  779. var username = $('#username').val();
  780. if(username === "") {
  781. $('#you')
  782. .removeClass().addClass('label label-warning')
  783. .html("Insert your display name (e.g., pippo)");
  784. $('#username').removeAttr('disabled');
  785. $('#register').removeAttr('disabled').click(registerUsername);
  786. return;
  787. }
  788. if(/[^a-zA-Z0-9]/.test(username)) {
  789. $('#you')
  790. .removeClass().addClass('label label-warning')
  791. .html('Input is not alphanumeric');
  792. $('#username').removeAttr('disabled').val("");
  793. $('#register').removeAttr('disabled').click(registerUsername);
  794. return;
  795. }
  796. var register = { "request": "join", "room": myroom, "display": username };
  797. myusername = username;
  798. mixertest.send({"message": register});
  799. }
  800. }