aboutsummaryrefslogtreecommitdiff
path: root/website/mic.js
blob: ecbd682e27be821bc865f2e073a074b61633bcb1 (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 = 1234; // Demo room
  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. // Prepare the username registration
  236. $('#audiojoin').removeClass('hide').show();
  237. $('#registernow').removeClass('hide').show();
  238. $('#register').click(registerUsername);
  239. $('#username').focus();
  240. $('#start').removeAttr('disabled').html("Stop")
  241. .click(function() {
  242. $(this).attr('disabled', true);
  243. janus.destroy();
  244. });
  245. },
  246. error: function(error) {
  247. Janus.error(" -- Error attaching plugin...", error);
  248. bootbox.alert("Error attaching plugin... " + error);
  249. },
  250. consentDialog: function(on) {
  251. Janus.debug("Consent dialog should be " + (on ? "on" : "off") + " now");
  252. if(on) {
  253. // Darken screen and show hint
  254. $.blockUI({
  255. message: '<div><img src="img/up_arrow.png"/></div>',
  256. css: {
  257. border: 'none',
  258. padding: '15px',
  259. backgroundColor: 'transparent',
  260. color: '#aaa',
  261. top: '10px',
  262. left: (navigator.mozGetUserMedia ? '-100px' : '300px')
  263. } });
  264. } else {
  265. // Restore screen
  266. $.unblockUI();
  267. }
  268. },
  269. onmessage: function(msg, jsep) {
  270. Janus.debug(" ::: Got a message :::");
  271. Janus.debug(msg);
  272. var event = msg["audiobridge"];
  273. Janus.debug("Event: " + event);
  274. if(event != undefined && event != null) {
  275. if(event === "joined") {
  276. // Successfully joined, negotiate WebRTC now
  277. myid = msg["id"];
  278. Janus.log("Successfully joined room " + msg["room"] + " with ID " + myid);
  279. if(!webrtcUp) {
  280. webrtcUp = true;
  281. // Publish our stream
  282. mixertest.createOffer(
  283. {
  284. media: { video: false}, // This is an audio only room
  285. success: function(jsep) {
  286. Janus.debug("Got SDP!");
  287. Janus.debug(jsep);
  288. var publish = { "request": "configure", "muted": false };
  289. mixertest.send({"message": publish, "jsep": jsep});
  290. },
  291. error: function(error) {
  292. Janus.error("WebRTC error:", error);
  293. bootbox.alert("WebRTC error... " + JSON.stringify(error));
  294. }
  295. });
  296. }
  297. // Any room participant?
  298. if(msg["participants"] !== undefined && msg["participants"] !== null) {
  299. var list = msg["participants"];
  300. Janus.debug("Got a list of participants:");
  301. Janus.debug(list);
  302. for(var f in list) {
  303. var id = list[f]["id"];
  304. var display = list[f]["display"];
  305. var setup = list[f]["setup"];
  306. var muted = list[f]["muted"];
  307. Janus.debug(" >> [" + id + "] " + display + " (setup=" + setup + ", muted=" + muted + ")");
  308. if($('#rp'+id).length === 0) {
  309. // Add to the participants list
  310. $('#list').append('<li id="rp'+id+'" class="list-group-item">'+display+
  311. ' <i class="absetup fa fa-chain-broken"></i>' +
  312. ' <i class="abmuted fa fa-microphone-slash"></i></li>');
  313. $('#rp'+id + ' > i').hide();
  314. }
  315. if(muted === true || muted === "true")
  316. $('#rp'+id + ' > i.abmuted').removeClass('hide').show();
  317. else
  318. $('#rp'+id + ' > i.abmuted').hide();
  319. if(setup === true || setup === "true")
  320. $('#rp'+id + ' > i.absetup').hide();
  321. else
  322. $('#rp'+id + ' > i.absetup').removeClass('hide').show();
  323. }
  324. }
  325. } else if(event === "roomchanged") {
  326. // The user switched to a different room
  327. myid = msg["id"];
  328. Janus.log("Moved to room " + msg["room"] + ", new ID: " + myid);
  329. // Any room participant?
  330. $('#list').empty();
  331. if(msg["participants"] !== undefined && msg["participants"] !== null) {
  332. var list = msg["participants"];
  333. Janus.debug("Got a list of participants:");
  334. Janus.debug(list);
  335. for(var f in list) {
  336. var id = list[f]["id"];
  337. var display = list[f]["display"];
  338. var setup = list[f]["setup"];
  339. var muted = list[f]["muted"];
  340. Janus.debug(" >> [" + id + "] " + display + " (setup=" + setup + ", muted=" + muted + ")");
  341. if($('#rp'+id).length === 0) {
  342. // Add to the participants list
  343. $('#list').append('<li id="rp'+id+'" class="list-group-item">'+display+
  344. ' <i class="absetup fa fa-chain-broken"></i>' +
  345. ' <i class="abmuted fa fa-microphone-slash"></i></li>');
  346. $('#rp'+id + ' > i').hide();
  347. }
  348. if(muted === true || muted === "true")
  349. $('#rp'+id + ' > i.abmuted').removeClass('hide').show();
  350. else
  351. $('#rp'+id + ' > i.abmuted').hide();
  352. if(setup === true || setup === "true")
  353. $('#rp'+id + ' > i.absetup').hide();
  354. else
  355. $('#rp'+id + ' > i.absetup').removeClass('hide').show();
  356. }
  357. }
  358. } else if(event === "destroyed") {
  359. // The room has been destroyed
  360. Janus.warn("The room has been destroyed!");
  361. bootbox.alert("The room has been destroyed", function() {
  362. window.location.reload();
  363. });
  364. } else if(event === "event") {
  365. if(msg["participants"] !== undefined && msg["participants"] !== null) {
  366. var list = msg["participants"];
  367. Janus.debug("Got a list of participants:");
  368. Janus.debug(list);
  369. for(var f in list) {
  370. var id = list[f]["id"];
  371. var display = list[f]["display"];
  372. var setup = list[f]["setup"];
  373. var muted = list[f]["muted"];
  374. Janus.debug(" >> [" + id + "] " + display + " (setup=" + setup + ", muted=" + muted + ")");
  375. if($('#rp'+id).length === 0) {
  376. // Add to the participants list
  377. $('#list').append('<li id="rp'+id+'" class="list-group-item">'+display+
  378. ' <i class="absetup fa fa-chain-broken"></i>' +
  379. ' <i class="abmuted fa fa-microphone-slash"></i></li>');
  380. $('#rp'+id + ' > i').hide();
  381. }
  382. if(muted === true || muted === "true")
  383. $('#rp'+id + ' > i.abmuted').removeClass('hide').show();
  384. else
  385. $('#rp'+id + ' > i.abmuted').hide();
  386. if(setup === true || setup === "true")
  387. $('#rp'+id + ' > i.absetup').hide();
  388. else
  389. $('#rp'+id + ' > i.absetup').removeClass('hide').show();
  390. }
  391. } else if(msg["error"] !== undefined && msg["error"] !== null) {
  392. if(msg["error_code"] === 485) {
  393. // This is a "no such room" error: give a more meaningful description
  394. bootbox.alert(
  395. "<p>Apparently room <code>" + myroom + "</code> (the one this demo uses as a test room) " +
  396. "does not exist...</p><p>Do you have an updated <code>janus.plugin.audiobridge.cfg</code> " +
  397. "configuration file? If not, make sure you copy the details of room <code>" + myroom + "</code> " +
  398. "from that sample in your current configuration file, then restart Janus and try again."
  399. );
  400. } else {
  401. bootbox.alert(msg["error"]);
  402. }
  403. return;
  404. }
  405. // Any new feed to attach to?
  406. if(msg["leaving"] !== undefined && msg["leaving"] !== null) {
  407. // One of the participants has gone away?
  408. var leaving = msg["leaving"];
  409. Janus.log("Participant left: " + leaving + " (we have " + $('#rp'+leaving).length + " elements with ID #rp" +leaving + ")");
  410. $('#rp'+leaving).remove();
  411. }
  412. }
  413. }
  414. if(jsep !== undefined && jsep !== null) {
  415. Janus.debug("Handling SDP as well...");
  416. Janus.debug(jsep);
  417. mixertest.handleRemoteJsep({jsep: jsep});
  418. }
  419. },
  420. onlocalstream: function(stream) {
  421. Janus.debug(" ::: Got a local stream :::");
  422. Janus.debug(stream);
  423. // We're not going to attach the local audio stream
  424. $('#audiojoin').hide();
  425. $('#room').removeClass('hide').show();
  426. $('#participant').removeClass('hide').html(myusername).show();
  427. },
  428. onremotestream: function(stream) {
  429. $('#room').removeClass('hide').show();
  430. var addButtons = false;
  431. if($('#roomaudio').length === 0) {
  432. addButtons = true;
  433. $('#mixedaudio').append('<audio class="rounded centered" id="roomaudio" width="100%" height="100%" autoplay/>');
  434. }
  435. Janus.attachMediaStream($('#roomaudio').get(0), stream);
  436. if(!addButtons)
  437. return;
  438. // Mute button
  439. audioenabled = true;
  440. $('#toggleaudio').click(
  441. function() {
  442. audioenabled = !audioenabled;
  443. if(audioenabled)
  444. $('#toggleaudio').html("Mute").removeClass("btn-success").addClass("btn-danger");
  445. else
  446. $('#toggleaudio').html("Unmute").removeClass("btn-danger").addClass("btn-success");
  447. mixertest.send({message: { "request": "configure", "muted": !audioenabled }});
  448. }).removeClass('hide').show();
  449. },
  450. oncleanup: function() {
  451. webrtcUp = false;
  452. Janus.log(" ::: Got a cleanup notification :::");
  453. $('#participant').empty().hide();
  454. $('#list').empty();
  455. $('#mixedaudio').empty();
  456. $('#room').hide();
  457. }
  458. });
  459. },
  460. error: function(error) {
  461. Janus.error(error);
  462. bootbox.alert(error, function() {
  463. window.location.reload();
  464. });
  465. },
  466. destroyed: function() {
  467. window.location.reload();
  468. }
  469. });
  470. });
  471. }});
  472. });
  473. function updateStreamsList() {
  474. $('#update-streams').unbind('click').addClass('fa-spin');
  475. var body = { "request": "list" };
  476. Janus.debug("Sending message (" + JSON.stringify(body) + ")");
  477. streaming.send({"message": body, success: function(result) {
  478. setTimeout(function() {
  479. $('#update-streams').removeClass('fa-spin').click(updateStreamsList);
  480. }, 500);
  481. if(result === null || result === undefined) {
  482. bootbox.alert("Got no response to our query for available streams");
  483. return;
  484. }
  485. if(result["list"] !== undefined && result["list"] !== null) {
  486. $('#streams').removeClass('hide').show();
  487. $('#streamslist').empty();
  488. $('#watch').attr('disabled', true).unbind('click');
  489. var list = result["list"];
  490. Janus.log("Got a list of available streams");
  491. Janus.debug(list);
  492. for(var mp in list) {
  493. Janus.debug(" >> [" + list[mp]["id"] + "] " + list[mp]["description"] + " (" + list[mp]["type"] + ")");
  494. $('#streamslist').append("<li><a href='#' id='" + list[mp]["id"] + "'>" + list[mp]["description"] + " (" + list[mp]["type"] + ")" + "</a></li>");
  495. }
  496. $('#streamslist a').unbind('click').click(function() {
  497. selectedStream = $(this).attr("id");
  498. $('#streamset').html($(this).html()).parent().removeClass('open');
  499. return false;
  500. });
  501. $('#watch').removeAttr('disabled').unbind('click').click(startStream);
  502. }
  503. }});
  504. }
  505. function startStream() {
  506. Janus.log("Selected video id #" + selectedStream);
  507. if(selectedStream === undefined || selectedStream === null) {
  508. bootbox.alert("Select a stream from the list");
  509. return;
  510. }
  511. $('#streamset').attr('disabled', true);
  512. $('#streamslist').attr('disabled', true);
  513. $('#watch').attr('disabled', true).unbind('click');
  514. var body = { "request": "watch", id: parseInt(selectedStream) };
  515. streaming.send({"message": body});
  516. // No remote video yet
  517. $('#stream').append('<video class="rounded centered" id="waitingvideo" width=320 height=240 />');
  518. if(spinner == null) {
  519. var target = document.getElementById('stream');
  520. spinner = new Spinner({top:100}).spin(target);
  521. } else {
  522. spinner.spin();
  523. }
  524. }
  525. function stopStream() {
  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. }