script.js 5.2 KB

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