index.html 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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="//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. autoAdjustMic: false
  55. });
  56. // when it's ready, join if we got a room from the URL
  57. webrtc.on('readyToCall', function () {
  58. // you can name it anything
  59. if (room) webrtc.joinRoom(room);
  60. });
  61. function showVolume(el, volume) {
  62. if (!el) return;
  63. if (volume < -45) { // vary between -45 and -20
  64. el.style.height = '0px';
  65. } else if (volume > -20) {
  66. el.style.height = '100%';
  67. } else {
  68. el.style.height = '' + Math.floor((volume + 100) * 100 / 25 - 220) + '%';
  69. }
  70. }
  71. webrtc.on('channelMessage', function (peer, label, data) {
  72. if (data.type == 'volume') {
  73. showVolume(document.getElementById('volume_' + peer.id), data.volume);
  74. }
  75. });
  76. webrtc.on('videoAdded', function (video, peer) {
  77. console.log('video added', peer);
  78. var remotes = document.getElementById('remotes');
  79. if (remotes) {
  80. var d = document.createElement('div');
  81. d.className = 'videoContainer';
  82. d.id = 'container_' + webrtc.getDomId(peer);
  83. d.appendChild(video);
  84. var vol = document.createElement('div');
  85. vol.id = 'volume_' + peer.id;
  86. vol.className = 'volume_bar';
  87. video.onclick = function () {
  88. video.style.width = video.videoWidth + 'px';
  89. video.style.height = video.videoHeight + 'px';
  90. };
  91. d.appendChild(vol);
  92. remotes.appendChild(d);
  93. }
  94. });
  95. webrtc.on('videoRemoved', function (video, peer) {
  96. console.log('video removed ', peer);
  97. var remotes = document.getElementById('remotes');
  98. var el = document.getElementById('container_' + webrtc.getDomId(peer));
  99. if (remotes && el) {
  100. remotes.removeChild(el);
  101. }
  102. });
  103. webrtc.on('volumeChange', function (volume, treshold) {
  104. //console.log('own volume', volume);
  105. showVolume(document.getElementById('localVolume'), volume);
  106. });
  107. // Since we use this twice we put it here
  108. function setRoom(name) {
  109. $('form').remove();
  110. $('h1').text(name);
  111. $('#subTitle').text('Link to join: ' + location.href);
  112. $('body').addClass('active');
  113. }
  114. if (room) {
  115. setRoom(room);
  116. } else {
  117. $('form').submit(function () {
  118. var val = $('#sessionInput').val().toLowerCase().replace(/\s/g, '-').replace(/[^A-Za-z0-9_\-]/g, '');
  119. webrtc.createRoom(val, function (err, name) {
  120. console.log(' create room cb', arguments);
  121. var newUrl = location.pathname + '?' + name;
  122. if (!err) {
  123. history.replaceState({foo: 'bar'}, null, newUrl);
  124. setRoom(name);
  125. } else {
  126. console.log(err);
  127. }
  128. });
  129. return false;
  130. });
  131. }
  132. var button = $('#screenShareButton'),
  133. setButton = function (bool) {
  134. button.text(bool ? 'share screen' : 'stop sharing');
  135. };
  136. webrtc.on('localScreenStopped', function () {
  137. setButton(true);
  138. });
  139. setButton(true);
  140. button.click(function () {
  141. if (webrtc.getLocalScreen()) {
  142. webrtc.stopScreenShare();
  143. setButton(true);
  144. } else {
  145. webrtc.shareScreen(function (err) {
  146. if (err) {
  147. setButton(true);
  148. } else {
  149. setButton(false);
  150. }
  151. });
  152. }
  153. });
  154. </script>
  155. </body>
  156. </html>