script.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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:5px">' + msg + '</span>';
  56. } else {
  57. msg = '<span>' + msg + '</span>';
  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 initNewRoom() {
  66. var button = document.getElementById("newRoom");
  67. button.addEventListener('click', function(event) {
  68. var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
  69. var string_length = 8;
  70. var randomstring = '';
  71. for(var i = 0; i < string_length; i++) {
  72. var rnum = Math.floor(Math.random() * chars.length);
  73. randomstring += chars.substring(rnum, rnum + 1);
  74. }
  75. window.location.hash = randomstring;
  76. location.reload();
  77. })
  78. }
  79. var websocketChat = {
  80. send: function(message) {
  81. rtc._socket.send(message);
  82. },
  83. recv: function(message) {
  84. return message;
  85. },
  86. event: 'receive_chat_msg'
  87. };
  88. var dataChannelChat = {
  89. send: function(message) {
  90. for(var connection in rtc.dataChannels) {
  91. var channel = rtc.dataChannels[connection];
  92. channel.send(message);
  93. }
  94. },
  95. recv: function(channel, message) {
  96. return JSON.parse(message).data;
  97. },
  98. event: 'data stream data'
  99. };
  100. function initChat() {
  101. var chat;
  102. if(rtc.dataChannelSupport) {
  103. console.log('initializing data channel chat');
  104. chat = dataChannelChat;
  105. } else {
  106. console.log('initializing websocket chat');
  107. chat = websocketChat;
  108. }
  109. var input = document.getElementById("chatinput");
  110. var toggleHideShow = document.getElementById("hideShowMessages");
  111. var room = window.location.hash.slice(1);
  112. var color = "#" + ((1 << 24) * Math.random() | 0).toString(16);
  113. toggleHideShow.addEventListener('click', function() {
  114. var element = document.getElementById("messages");
  115. if(element.style.display === "block") {
  116. element.style.display = "none";
  117. }
  118. else {
  119. element.style.display = "block";
  120. }
  121. });
  122. input.addEventListener('keydown', function(event) {
  123. var key = event.which || event.keyCode;
  124. if(key === 13) {
  125. chat.send(JSON.stringify({
  126. "eventName": "chat_msg",
  127. "data": {
  128. "messages": input.value,
  129. "room": room,
  130. "color": color
  131. }
  132. }));
  133. addToChat(input.value);
  134. input.value = "";
  135. }
  136. }, false);
  137. rtc.on(chat.event, function() {
  138. var data = chat.recv.apply(this, arguments);
  139. console.log(data.color);
  140. addToChat(data.messages, data.color.toString(16));
  141. });
  142. }
  143. function init() {
  144. if(PeerConnection) {
  145. rtc.createStream({
  146. "video": {"mandatory": {}, "optional": []},
  147. "audio": true
  148. }, function(stream) {
  149. document.getElementById('you').src = URL.createObjectURL(stream);
  150. document.getElementById('you').play();
  151. //videos.push(document.getElementById('you'));
  152. //rtc.attachStream(stream, 'you');
  153. //subdivideVideos();
  154. });
  155. } else {
  156. alert('Your browser is not supported');
  157. }
  158. var room = window.location.hash.slice(1);
  159. rtc.connect("ws:" + window.location.href.substring(window.location.protocol.length).split('#')[0], room);
  160. rtc.on('add remote stream', function(stream, socketId) {
  161. console.log("ADDING REMOTE STREAM...");
  162. var clone = cloneVideo('you', socketId);
  163. document.getElementById(clone.id).setAttribute("class", "");
  164. rtc.attachStream(stream, clone.id);
  165. subdivideVideos();
  166. });
  167. rtc.on('disconnect stream', function(data) {
  168. console.log('remove ' + data);
  169. removeVideo(data);
  170. });
  171. initNewRoom();
  172. initChat();
  173. }
  174. window.onresize = function(event) {
  175. subdivideVideos();
  176. };