script.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. var videos = [];
  2. var PeerConnection = window.PeerConnection || window.webkitPeerConnection00 || window.webkitRTCPeerConnection || window.mozRTCPeerConnection || window.RTCPeerConnection;
  3. function getNumPerRow() {
  4. var len = videos.length;
  5. var biggest;
  6. // Ensure length is even for better division.
  7. if(len % 2 === 1) {
  8. len++;
  9. }
  10. biggest = Math.ceil(Math.sqrt(len));
  11. while(len % biggest !== 0) {
  12. biggest++;
  13. }
  14. return biggest;
  15. }
  16. function subdivideVideos() {
  17. var perRow = getNumPerRow();
  18. var numInRow = 0;
  19. for(var i = 0, len = videos.length; i < len; i++) {
  20. var video = videos[i];
  21. setWH(video, i);
  22. numInRow = (numInRow + 1) % perRow;
  23. }
  24. }
  25. function setWH(video, i) {
  26. var perRow = getNumPerRow();
  27. var perColumn = Math.ceil(videos.length / perRow);
  28. var width = Math.floor((window.innerWidth) / perRow);
  29. var height = Math.floor((window.innerHeight - 190) / perColumn);
  30. video.width = width;
  31. video.height = height;
  32. video.style.position = "absolute";
  33. video.style.left = (i % perRow) * width + "px";
  34. video.style.top = Math.floor(i / perRow) * height + "px";
  35. }
  36. function cloneVideo(domId, socketId) {
  37. var video = document.getElementById(domId);
  38. var clone = video.cloneNode(false);
  39. clone.id = "remote" + socketId;
  40. document.getElementById('videos').appendChild(clone);
  41. videos.push(clone);
  42. return clone;
  43. }
  44. function removeVideo(socketId) {
  45. var video = document.getElementById('remote' + socketId);
  46. if(video) {
  47. videos.splice(videos.indexOf(video), 1);
  48. video.parentNode.removeChild(video);
  49. }
  50. }
  51. function addToChat(msg, color) {
  52. var messages = document.getElementById('messages');
  53. msg = sanitize(msg);
  54. if(color) {
  55. msg = '<span style="color: ' + color + '; padding-left: 15px">' + msg + '</span>';
  56. } else {
  57. msg = '<strong style="padding-left: 15px">' + msg + '</strong>';
  58. }
  59. messages.innerHTML = messages.innerHTML + msg + '<br>';
  60. messages.scrollTop = 10000;
  61. }
  62. function sanitize(msg) {
  63. return msg.replace(/</g, '&lt;');
  64. }
  65. function initFullScreen() {
  66. var button = document.getElementById("fullscreen");
  67. button.addEventListener('click', function(event) {
  68. var elem = document.getElementById("videos");
  69. //show full screen
  70. elem.webkitRequestFullScreen();
  71. });
  72. }
  73. function initNewRoom() {
  74. var button = document.getElementById("newRoom");
  75. button.addEventListener('click', function(event) {
  76. var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
  77. var string_length = 8;
  78. var randomstring = '';
  79. for(var i = 0; i < string_length; i++) {
  80. var rnum = Math.floor(Math.random() * chars.length);
  81. randomstring += chars.substring(rnum, rnum + 1);
  82. }
  83. window.location.hash = randomstring;
  84. location.reload();
  85. })
  86. }
  87. var websocketChat = {
  88. send: function(message) {
  89. rtc._socket.send(message);
  90. },
  91. recv: function(message) {
  92. return message;
  93. },
  94. event: 'receive_chat_msg'
  95. };
  96. var dataChannelChat = {
  97. send: function(message) {
  98. for(var connection in rtc.dataChannels) {
  99. var channel = rtc.dataChannels[connection];
  100. channel.send(message);
  101. }
  102. },
  103. recv: function(channel, message) {
  104. return JSON.parse(message).data;
  105. },
  106. event: 'data stream data'
  107. };
  108. function initChat() {
  109. var chat;
  110. if(rtc.dataChannelSupport) {
  111. console.log('initializing data channel chat');
  112. chat = dataChannelChat;
  113. } else {
  114. console.log('initializing websocket chat');
  115. chat = websocketChat;
  116. }
  117. var input = document.getElementById("chatinput");
  118. var toggleHideShow = document.getElementById("hideShowMessages");
  119. var room = window.location.hash.slice(1);
  120. var color = "#" + ((1 << 24) * Math.random() | 0).toString(16);
  121. toggleHideShow.addEventListener('click', function() {
  122. var element = document.getElementById("messages");
  123. if(element.style.display === "block") {
  124. element.style.display = "none";
  125. }
  126. else {
  127. element.style.display = "block";
  128. }
  129. });
  130. input.addEventListener('keydown', function(event) {
  131. var key = event.which || event.keyCode;
  132. if(key === 13) {
  133. chat.send(JSON.stringify({
  134. "eventName": "chat_msg",
  135. "data": {
  136. "messages": input.value,
  137. "room": room,
  138. "color": color
  139. }
  140. }));
  141. addToChat(input.value);
  142. input.value = "";
  143. }
  144. }, false);
  145. rtc.on(chat.event, function() {
  146. var data = chat.recv.apply(this, arguments);
  147. console.log(data.color);
  148. addToChat(data.messages, data.color.toString(16));
  149. });
  150. }
  151. function init() {
  152. if(PeerConnection) {
  153. rtc.createStream({
  154. "video": {"mandatory": {}, "optional": []},
  155. "audio": true
  156. }, function(stream) {
  157. document.getElementById('you').src = URL.createObjectURL(stream);
  158. document.getElementById('you').play();
  159. //videos.push(document.getElementById('you'));
  160. //rtc.attachStream(stream, 'you');
  161. //subdivideVideos();
  162. });
  163. } else {
  164. alert('Your browser is not supported or you have to turn on flags. In chrome you go to chrome://flags and turn on Enable PeerConnection remember to restart chrome');
  165. }
  166. var room = window.location.hash.slice(1);
  167. rtc.connect("ws:" + window.location.href.substring(window.location.protocol.length).split('#')[0], room);
  168. rtc.on('add remote stream', function(stream, socketId) {
  169. console.log("ADDING REMOTE STREAM...");
  170. var clone = cloneVideo('you', socketId);
  171. document.getElementById(clone.id).setAttribute("class", "");
  172. rtc.attachStream(stream, clone.id);
  173. subdivideVideos();
  174. });
  175. rtc.on('disconnect stream', function(data) {
  176. console.log('remove ' + data);
  177. removeVideo(data);
  178. });
  179. initFullScreen();
  180. initNewRoom();
  181. initChat();
  182. }
  183. window.onresize = function(event) {
  184. subdivideVideos();
  185. };