index.html 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>SimpleWebRTC Demo</title>
  5. </head>
  6. <body>
  7. <h1 id="title">Start a room</h1>
  8. <style>
  9. .videoContainer {
  10. position: relative;
  11. width: 200px;
  12. height: 150px;
  13. }
  14. .videoContainer video {
  15. position: absolute;
  16. width: 100%;
  17. height: 100%;
  18. }
  19. .volume_bar {
  20. position: absolute;
  21. width: 5px;
  22. height: 0px;
  23. right: 0px;
  24. bottom: 0px;
  25. background-color: #12acef;
  26. }
  27. </style>
  28. <button id="screenShareButton"></button>
  29. <p id="subTitle"></p>
  30. <form id="createRoom">
  31. <input id="sessionInput"/>
  32. <button type="submit">Create it!</button>
  33. </form>
  34. <div class="videoContainer">
  35. <video id="localVideo" style="height: 150px;" oncontextmenu="return false;"></video>
  36. <div id="localVolume" class="volume_bar"></div>
  37. </div>
  38. <div id="remotes"></div>
  39. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
  40. <script src="simplewebrtc.bundle.js"></script>
  41. <script>
  42. // grab the room from the URL
  43. var room = location.search && location.search.split('?')[1];
  44. // create our webrtc connection
  45. var webrtc = new SimpleWebRTC({
  46. // the id/element dom element that will hold "our" video
  47. localVideoEl: 'localVideo',
  48. // the id/element dom element that will hold remote videos
  49. remoteVideosEl: '',
  50. // immediately ask for camera access
  51. autoRequestMedia: true,
  52. debug: false,
  53. detectSpeakingEvents: true
  54. });
  55. // when it's ready, join if we got a room from the URL
  56. webrtc.on('readyToCall', function () {
  57. // you can name it anything
  58. if (room) webrtc.joinRoom(room);
  59. });
  60. function showVolume(el, volume) {
  61. if (!el) return;
  62. if (volume < -45) { // vary between -45 and -20
  63. el.style.height = '0px';
  64. } else if (volume > -20) {
  65. el.style.height = '100%';
  66. } else {
  67. el.style.height = '' + Math.floor((volume + 100) * 100 / 25 - 220) + '%';
  68. }
  69. }
  70. webrtc.on('channelMessage', function (peer, label, data) {
  71. if (data.type == 'volume') {
  72. showVolume(document.getElementById('volume_' + peer.id), data.volume);
  73. }
  74. });
  75. webrtc.on('videoAdded', function (video, peer) {
  76. console.log('video added', peer);
  77. var remotes = document.getElementById('remotes');
  78. if (remotes) {
  79. var d = document.createElement('div');
  80. d.className = 'videoContainer';
  81. d.id = 'container_' + webrtc.getDomId(peer);
  82. d.appendChild(video);
  83. var vol = document.createElement('div');
  84. vol.id = 'volume_' + peer.id;
  85. vol.className = 'volume_bar';
  86. video.onclick = function () {
  87. video.style.width = video.videoWidth + 'px';
  88. video.style.height = video.videoHeight + 'px';
  89. };
  90. d.appendChild(vol);
  91. remotes.appendChild(d);
  92. }
  93. });
  94. webrtc.on('videoRemoved', function (video, peer) {
  95. console.log('video removed ', peer);
  96. var remotes = document.getElementById('remotes');
  97. var el = document.getElementById('container_' + webrtc.getDomId(peer));
  98. if (remotes && el) {
  99. remotes.removeChild(el);
  100. }
  101. });
  102. webrtc.on('volumeChange', function (volume, treshold) {
  103. //console.log('own volume', volume);
  104. showVolume(document.getElementById('localVolume'), volume);
  105. });
  106. // Since we use this twice we put it here
  107. function setRoom(name) {
  108. $('form').remove();
  109. $('h1').text(name);
  110. $('#subTitle').text('Link to join: ' + location.href);
  111. $('body').addClass('active');
  112. }
  113. if (room) {
  114. setRoom(room);
  115. } else {
  116. $('form').submit(function () {
  117. var val = $('#sessionInput').val().toLowerCase().replace(/\s/g, '-').replace(/[^A-Za-z0-9_\-]/g, '');
  118. webrtc.createRoom(val, function (err, name) {
  119. console.log(' create room cb', arguments);
  120. var newUrl = location.pathname + '?' + name;
  121. if (!err) {
  122. history.replaceState({foo: 'bar'}, null, newUrl);
  123. setRoom(name);
  124. } else {
  125. console.log(err);
  126. }
  127. });
  128. return false;
  129. });
  130. }
  131. var button = $('#screenShareButton'),
  132. setButton = function (bool) {
  133. button.text(bool ? 'share screen' : 'stop sharing');
  134. };
  135. webrtc.on('localScreenStopped', function () {
  136. setButton(true);
  137. });
  138. setButton(true);
  139. button.click(function () {
  140. if (webrtc.getLocalScreen()) {
  141. webrtc.stopScreenShare();
  142. setButton(true);
  143. } else {
  144. webrtc.shareScreen(function (err) {
  145. if (err) {
  146. setButton(true);
  147. } else {
  148. setButton(false);
  149. }
  150. });
  151. }
  152. });
  153. </script>
  154. </body>
  155. </html>