You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

310 lines
8.3 KiB

  1. var DtlsSrtpKeyAgreement = {
  2. DtlsSrtpKeyAgreement: true
  3. };
  4. var optional = {
  5. optional: [DtlsSrtpKeyAgreement]
  6. };
  7. var pc;
  8. var remoteStream;
  9. var localStream;
  10. // The RTCPeerConnection configuration
  11. var configuration = {"iceServers":[{"url": "stun:stun.services.mozilla.com"}]};
  12. // Set up audio and video regardless of what devices are present.
  13. var sdpConstraints = {'mandatory': {
  14. 'OfferToReceiveAudio': true,
  15. 'OfferToReceiveVideo': true }};
  16. function onIceConnectionStateChanged(event) {
  17. Visio.log('onIceConnectionStateChanged');
  18. Visio.log(event);
  19. }
  20. function onSignalingStateChanged(event) {
  21. Visio.log('onSignalingStateChanged');
  22. Visio.log(event);
  23. }
  24. function onIceCandidateAdded(event) {
  25. Visio.log('onIceCandidateAdded');
  26. Visio.log(event);
  27. }
  28. function onRemoteIceCandidateAdded(event) {
  29. Visio.log('onRemoteIceCandidateAdded');
  30. Visio.log(event);
  31. }
  32. function onRemoteIceCandidateError(event) {
  33. Visio.log('onRemoteIceCandidateError');
  34. Visio.log(event);
  35. }
  36. function onSignalingStateChanged(event) {
  37. Visio.log('onSignalingStateChanged');
  38. Visio.log(event);
  39. }
  40. function onRemoteStreamAdded(event) {
  41. var vid = document.getElementById('remote-video');
  42. vid.src = window.URL.createObjectURL(event.stream);
  43. remoteStream = event.stream;
  44. //console.log(remoteStream);
  45. //console.log(vid);
  46. /*
  47. audioTracks = remoteStream.getAudioTracks();
  48. for (i = 0; i < audioTracks.length; i++) {
  49. audioTracks[i].enabled = true;
  50. }*/
  51. }
  52. function onError(err) {
  53. console.log(err);
  54. }
  55. function onOfferCreated(offer) {
  56. pc.setLocalDescription(offer,onSetSessionDescriptionSuccess, onSetSessionDescriptionError);
  57. sendMessage(offer);
  58. }
  59. function onAnswerCreated(offer) {
  60. pc.setLocalDescription(offer,onSetSessionDescriptionSuccess, onSetSessionDescriptionError);
  61. sendMessage(offer, true);
  62. }
  63. function onIceCandidate(event) {
  64. Visio.log('onIceCandidate');
  65. console.log('CANDIDATE');
  66. console.log(event);
  67. candidate = {};
  68. if(event.candidate != null) {
  69. candidate.sdp = event.candidate.candidate;
  70. candidate.mid = event.candidate.sdpMid;
  71. candidate.line = event.candidate.sdpMLineIndex;
  72. candidate.jid = VISIO_JID;
  73. candidate.ressource = VISIO_RESSOURCE;
  74. var msgString = JSON.stringify(candidate);
  75. Visio.call(['VisioExt_ajaxSendCandidate', msgString]);
  76. }
  77. }
  78. function sendTerminate(reason) {
  79. Visio.call(['VisioExt_ajaxSendSessionTerminate', VISIO_JID, VISIO_RESSOURCE, reason]);
  80. }
  81. function sendMessage(msg, accept) {
  82. offer = {};
  83. offer.sdp = msg.sdp;
  84. offer.jid = VISIO_JID;
  85. offer.ressource = VISIO_RESSOURCE;
  86. document.getElementById('visio').className = 'calling';
  87. if(webrtcDetectedBrowser == 'chrome') {
  88. setTimeout(function() {
  89. if(!accept)
  90. offer.sdp = pc.localDescription.sdp;
  91. var msgString = JSON.stringify(offer);
  92. if(accept) {
  93. Visio.log('Send the acceptance.');
  94. Visio.log('ACCEPTANCE ' + msg.sdp);
  95. Visio.call(['VisioExt_ajaxSendAcceptance', msgString]);
  96. } else {
  97. Visio.log('Send the proposal.');
  98. Visio.log('PROPOSAL ' + msg.sdp);
  99. console.log(msg.sdp);
  100. Visio.call(['VisioExt_ajaxSendProposal', msgString]);
  101. }
  102. }, 1000);
  103. } else {
  104. var msgString = JSON.stringify(offer);
  105. if(accept) {
  106. Visio.log('Send the acceptance.');
  107. Visio.log('ACCEPTANCE ' + msg.sdp);
  108. Visio.call(['VisioExt_ajaxSendAcceptance', msgString]);
  109. } else {
  110. Visio.log('Send the proposal.');
  111. Visio.log('PROPOSAL ' + msg.sdp);
  112. Visio.call(['VisioExt_ajaxSendProposal', msgString]);
  113. }
  114. }
  115. }
  116. function onSetSessionDescriptionSuccess() {
  117. Visio.log('Set local session description success.');
  118. }
  119. function onSetSessionDescriptionError(error) {
  120. Visio.log('Failed to set local session description: ' + error.toString());
  121. sendTerminate('failed-application');
  122. }
  123. function onSetRemoteSessionDescriptionSuccess() {
  124. Visio.log('Set remote session description success.');
  125. }
  126. function onSetRemoteSessionDescriptionError(error) {
  127. Visio.log('Failed to set remote session description: ' + error.message);
  128. sendTerminate('failed-application');
  129. }
  130. function onOffer(offer) {
  131. offer = offer[0];
  132. Visio.log('Offer received.');
  133. Visio.log('OFFER ' + offer);
  134. if(!pc)
  135. init(false);
  136. if(offer != null) {
  137. var message = {};
  138. message.sdp = offer;
  139. message.type = 'offer';
  140. var desc = new RTCSessionDescription(message);
  141. pc.setRemoteDescription(desc,
  142. onSetRemoteSessionDescriptionSuccess, onSetRemoteSessionDescriptionError);
  143. }
  144. }
  145. function onAccept(offer) {
  146. offer = offer[0];
  147. Visio.log('Accept received.');
  148. Visio.log('ACCEPT ' + offer);
  149. if(offer != null) {
  150. var desc = new RTCSessionDescription();
  151. desc.sdp = offer;
  152. desc.type = 'answer';
  153. pc.setRemoteDescription(desc,
  154. onSetRemoteSessionDescriptionSuccess, onSetRemoteSessionDescriptionError);
  155. }
  156. }
  157. function onCandidate(message) {
  158. var label = {
  159. 'audio' : 0,
  160. 'video' : 1
  161. };
  162. var candidate = new RTCIceCandidate({sdpMLineIndex: label[message[1]],
  163. candidate: message[0]});
  164. pc.addIceCandidate(candidate, onRemoteIceCandidateAdded, onRemoteIceCandidateError);
  165. }
  166. function preInit(isCaller) {
  167. // We try to grab TURN servers, init() is called here
  168. maybeRequestTurn(isCaller);
  169. }
  170. function init(isCaller) {
  171. try {
  172. console.log(configuration);
  173. pc = new RTCPeerConnection(configuration, optional);
  174. console.log('Created RTCPeerConnnection with:\n' +
  175. ' config: \'' + JSON.stringify(configuration) + '\';\n' +
  176. ' constraints: \'' + JSON.stringify(optional) + '\'.');
  177. pc.onicecandidate = onIceCandidate;
  178. pc.onsignalingstatechange = onSignalingStateChanged;
  179. pc.oniceconnectionstatechange = onIceConnectionStateChanged;
  180. pc.onaddstream = onRemoteStreamAdded;
  181. } catch (e) {
  182. Visio.log('Failed to create PeerConnection, exception: ' + e.message);
  183. alert('Cannot create RTCPeerConnection object; \
  184. WebRTC is not supported by this browser.');
  185. return;
  186. }
  187. if(getUserMedia) {
  188. if (getUserMedia) {
  189. getUserMedia = getUserMedia.bind(navigator);
  190. }
  191. // Request the camera.
  192. getUserMedia(
  193. // Constraints
  194. {
  195. video: true, audio: true
  196. },
  197. // Success Callback
  198. function(localMediaStream) {
  199. // Get a reference to the video element on the page.
  200. var vid = document.getElementById('local-video');
  201. var avatar = document.getElementById('avatar');
  202. // Create an object URL for the video stream and use this
  203. // to set the video source.
  204. vid.src = window.URL.createObjectURL(localMediaStream);
  205. localStream = localMediaStream;
  206. pc.addStream(localStream);
  207. channel = pc.createDataChannel("visio");
  208. if(isCaller)
  209. pc.createOffer(onOfferCreated, onError);
  210. else
  211. pc.createAnswer(onAnswerCreated, onError);
  212. },
  213. // Error Callback
  214. function(err) {
  215. // Log the error to the console.
  216. Visio.log('The following error occurred when trying to use getUserMedia: ' + err);
  217. sendTerminate('decline');
  218. }
  219. );
  220. } else {
  221. alert('Sorry, your browser does not support getUserMedia');
  222. }
  223. }
  224. function terminate() {
  225. // We close the RTCPeerConnection
  226. if(pc != null && pc.signalingState != 'closed')
  227. pc.close();
  228. // We close the local webcam and microphone
  229. if(localStream != null)
  230. localStream.stop();
  231. remoteStream = null;
  232. // Get a reference to the video elements on the page.
  233. var vid = document.getElementById('local-video');
  234. var rvid = document.getElementById('remote-video');
  235. document.getElementById('visio').className = '';
  236. }