Advertisement
Guest User

Untitled

a guest
Jan 19th, 2019
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.73 KB | None | 0 0
  1. import { Component, OnInit } from '@angular/core';
  2.  
  3. import { ICEService } from '@services/ice.service';
  4.  
  5. @Component({
  6. selector: 'root-contacts-action',
  7. templateUrl: './contacts-action.component.html',
  8. styleUrls: ['./contacts-action.component.scss']
  9. })
  10. export class ContactsActionComponent implements OnInit {
  11.  
  12. private peerConnection;
  13.  
  14. private configuration: Object;
  15.  
  16. private localStream: Object;
  17. private remoteStream: Object;
  18.  
  19. private hangupAction: Function;
  20. private getLocalMediaStream: Function;
  21. private callerAction: Function;
  22. private calleeAction: Function;
  23. private handleConnection: Function;
  24.  
  25. private disableCall: Boolean;
  26. private disableHangup: Boolean;
  27.  
  28. private execOnce: Boolean;
  29.  
  30. constructor(private ICE: ICEService) { }
  31.  
  32. ngOnInit() {
  33. const vm = this;
  34.  
  35. vm.execOnce = true;
  36.  
  37. // parameters for STUN and TURN servers (optional)
  38. vm.configuration = {};
  39.  
  40. // Create RTC object for local user
  41. vm.peerConnection = new RTCPeerConnection(vm.configuration);
  42.  
  43. // When ICE is done, add SDP to local peer connection object
  44. vm.peerConnection.onicecandidate = (event) => {
  45. const iceCandidate = event.candidate;
  46.  
  47. if (iceCandidate) {
  48. const newIceCandidate = new RTCIceCandidate(iceCandidate);
  49. vm.peerConnection.addIceCandidate(newIceCandidate);
  50. }
  51. };
  52.  
  53. // When remote user starts stream, capture it and display it
  54. vm.peerConnection.onaddstream = (event) => {
  55. // Add remoteStream to the DOM, so local user can see remote's mediaStream
  56. vm.remoteStream = event.stream;
  57. };
  58.  
  59. // End call
  60. vm.hangupAction = () => {
  61. vm.peerConnection.close();
  62. vm.peerConnection = null;
  63. vm.disableHangup = true;
  64. vm.disableCall = false;
  65. };
  66.  
  67. // Get local data from audio / video input devices
  68. vm.getLocalMediaStream = (callback) => {
  69. navigator.mediaDevices.getUserMedia({
  70. video: true,
  71. // audio: true
  72. }).then((mediaStream) => {
  73. vm.disableCall = true;
  74. vm.disableHangup = false;
  75.  
  76. // Add localStream to the DOM, so local user can see his mediaStream
  77. vm.localStream = mediaStream;
  78.  
  79. // Add localStream to connection object
  80. vm.peerConnection.addStream(mediaStream);
  81.  
  82. // Continue wih either caller or callee procedure
  83. callback();
  84. }).catch();
  85. };
  86.  
  87. // Define MediaStreams callbacks.
  88.  
  89. vm.callerAction = () => {
  90. vm.getLocalMediaStream(() => {
  91. // Create SDP with 'offer' type (for caller)
  92. vm.peerConnection.createOffer({
  93. offerToReceiveVideo: true
  94. })
  95. .then((description) => {
  96. // Send local SDP data to server
  97. vm.ICE.createCandidate(description)
  98. .subscribe(() => {
  99. // Send 'offer' (call) request to the callee
  100. vm.ICE.requestCall(2)
  101. .subscribe((remoteICE) => {
  102. // Call API until callee responds
  103. const interval = setInterval(() => {
  104. vm.ICE.statusCall(1)
  105. .subscribe((remoteSDP) => {
  106. // callee accepted the call, get his SDP data
  107. if (remoteSDP[0].remoteData) {
  108. // Set your local SDP as local SDP
  109. vm.peerConnection.setLocalDescription(description);
  110. // set Callee's local SDP as your remote SDP
  111. const remoteDesc = new RTCSessionDescription(JSON.parse(remoteSDP[0].remoteData));
  112. vm.peerConnection.setRemoteDescription(remoteDesc);
  113. // Stop calling API, callee responded
  114. clearInterval(interval);
  115. // HACK, call function twice (I don't like this solution)
  116. // because call wont start after first execuion
  117. if (vm.execOnce) {
  118. vm.execOnce = false;
  119. vm.callerAction();
  120. }
  121. }
  122. });
  123. }, 1500);
  124. // Bugfix for hack, interval is not cleared after second execution
  125. if (!vm.execOnce) {
  126. clearInterval(interval);
  127. }
  128. });
  129. });
  130. }).catch();
  131. });
  132. };
  133.  
  134. vm.calleeAction = (event) => {
  135. vm.getLocalMediaStream(() => {
  136. // Check if someone is calling you
  137. vm.ICE.fetchCall()
  138. .subscribe((remoteICE) => {
  139. // Someone is calling you, get his SDP and set it as your remote
  140. const remoteDesc = new RTCSessionDescription(JSON.parse(remoteICE[0].data.remoteSDP));
  141. vm.peerConnection.setRemoteDescription(remoteDesc);
  142. // Create SDP with 'answer' type (for callee)
  143. vm.peerConnection.createAnswer()
  144. .then((description) => {
  145. // Set it as your local SDP
  146. vm.peerConnection.setLocalDescription(description);
  147. // Send your SDP to the server
  148. vm.ICE.createCandidate(description)
  149. .subscribe((createCandidateStatus) => {
  150. // Accept the caller's call
  151. vm.ICE.acceptCall(2)
  152. .subscribe((callAcceptStatus) => {
  153. // HACK, call function twice (I don't like this solution)
  154. // because call wont start after first execuion
  155. if (vm.execOnce) {
  156. vm.execOnce = false;
  157. vm.calleeAction();
  158. }
  159. });
  160. });
  161. });
  162. });
  163. });
  164. };
  165.  
  166. }
  167.  
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement