index.html 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <html>
  2. <head>
  3. <title>Example webrtc.io</title>
  4. <link type="text/css" href="/style.css" rel="stylesheet"></link>
  5. <script src="/socket.io/socket.io.js"></script>
  6. <script src="/webrtc.io/webrtc.io.js"></script>
  7. </head>
  8. <body onload="init()">
  9. <div id="videos">
  10. <a href="https://github.com/webRTC"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_orange_ff7600.png" alt="Fork me on GitHub"></a>
  11. <video id="you" class="flip" autoplay></video>
  12. </div>
  13. <div id="chatbox">
  14. <button id="fullscreen">Enter Full Screen</button>
  15. <button id="newRoom">Create A New Room</button>
  16. <div id="messages">
  17. </div>
  18. <br>
  19. <input id="chatinput" type="text"/>
  20. </div>
  21. <script>
  22. var videos = [];
  23. var rooms = [1,2,3,4,5];
  24. var PeerConnection = window.PeerConnection || window.webkitPeerConnection00;
  25. function getNumPerRow() {
  26. var len = videos.length;
  27. var biggest;
  28. // Ensure length is even for better division.
  29. if (len % 2 === 1) {
  30. len++;
  31. }
  32. biggest = Math.ceil(Math.sqrt(len));
  33. while (len % biggest !== 0) {
  34. biggest++;
  35. }
  36. return biggest;
  37. }
  38. function subdivideVideos() {
  39. var perRow = getNumPerRow();
  40. var numInRow = 0;
  41. for (var i = 0, len = videos.length; i < len; i++) {
  42. var video = videos[i];
  43. setWH(video, i);
  44. numInRow = (numInRow + 1) % perRow;
  45. }
  46. }
  47. function setWH(video, i) {
  48. var perRow = getNumPerRow();
  49. var perColumn = Math.ceil(videos.length / perRow);
  50. var width = Math.floor((window.innerWidth) / perRow);
  51. var height = Math.floor((window.innerHeight - 190) / perColumn);
  52. console.log(width, height);
  53. video.width = width;
  54. video.height = height;
  55. video.style.position = "absolute";
  56. video.style.left = (i % perRow) * width + "px";
  57. video.style.top = Math.floor(i / perRow) * height + "px";
  58. }
  59. function cloneVideo(domId, socketId) {
  60. var video = document.getElementById(domId);
  61. var clone = video.cloneNode(false);
  62. clone.id = "remote" + socketId;
  63. document.getElementById('videos').appendChild(clone);
  64. videos.push(clone);
  65. return clone;
  66. }
  67. function removeVideo(socketId) {
  68. var video = document.getElementById('remote' + socketId);
  69. if (video) {
  70. videos.splice(videos.indexOf(video), 1);
  71. video.parentNode.removeChild(video);
  72. }
  73. }
  74. function initChat() {
  75. var input = document.getElementById("chatinput");
  76. input.addEventListener('keydown', function(event) {
  77. var key = event.which || event.keyCode;
  78. if (key === 13) {
  79. rtc._socket.emit('chat msg', input.value);
  80. addToChat(input.value);
  81. input.value = "";
  82. }
  83. }, false);
  84. rtc._socket.on('receive chat msg', function(data) {
  85. console.log(data.color);
  86. addToChat(data.msg, data.color.toString(16));
  87. });
  88. }
  89. function addToChat(msg, color) {
  90. var messages = document.getElementById('messages');
  91. msg = sanitize(msg);
  92. if (color) {
  93. msg = '<span style="color: #' + color + '; padding-left: 15px">' + msg + '</span>';
  94. } else {
  95. msg = '<strong style="padding-left: 15px">' + msg + '</strong>';
  96. }
  97. messages.innerHTML = messages.innerHTML + msg + '<br>';
  98. messages.scrollTop = 10000;
  99. }
  100. function sanitize(msg) {
  101. return msg.replace(/</g, '&lt;');
  102. }
  103. function initFullScreen() {
  104. var button = document.getElementById("fullscreen");
  105. button.addEventListener('click', function(event) {
  106. var elem = document.getElementById("videos");
  107. //show full screen
  108. elem.webkitRequestFullScreen();
  109. });
  110. }
  111. function initNewRoom() {
  112. var button = document.getElementById("newRoom");
  113. button.addEventListener('click', function(event) {
  114. var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
  115. var string_length = 8;
  116. var randomstring = '';
  117. for (var i=0; i<string_length; i++) {
  118. var rnum = Math.floor(Math.random() * chars.length);
  119. randomstring += chars.substring(rnum,rnum+1);
  120. }
  121. window.location.hash = randomstring;
  122. location.reload();
  123. })
  124. }
  125. function init() {
  126. if(PeerConnection){
  127. rtc.createStream('you', function(stream) {
  128. videos.push(document.getElementById('you'));
  129. rtc.attachStream(stream, 'you');
  130. subdivideVideos();
  131. });
  132. }else {
  133. 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');
  134. }
  135. var room = window.location.hash.slice(1);
  136. if (room === '') {
  137. room = Math.floor(Math.random()*rooms.length);
  138. window.location.hash = room;
  139. }
  140. //When connectiong to nodejitsu
  141. rtc.connect("http://multiwebrtc.nodejitsu.com/", room);
  142. //When using localhost
  143. //rtc.connect("http://localhost:8000/", room);
  144. rtc.on('add remote stream', function(stream, socketId) {
  145. console.log("ADDING REMOTE STREAM...");
  146. var clone = cloneVideo('you', socketId);
  147. document.getElementById(clone.id).setAttribute("class", "");
  148. rtc.attachStream(stream, clone.id);
  149. subdivideVideos();
  150. });
  151. rtc.on('disconnect stream', function(data) {
  152. console.log('remove ' + data);
  153. removeVideo(data);
  154. });
  155. initChat();
  156. initFullScreen();
  157. initNewRoom();
  158. }
  159. window.onresize = function(event) {
  160. subdivideVideos();
  161. };
  162. </script>
  163. </body>
  164. </html>