Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Component, OnInit } from '@angular/core';
- import { ICEService } from '@services/ice.service';
- @Component({
- selector: 'root-contacts-action',
- templateUrl: './contacts-action.component.html',
- styleUrls: ['./contacts-action.component.scss']
- })
- export class ContactsActionComponent implements OnInit {
- private peerConnection;
- private configuration: Object;
- private localStream: Object;
- private remoteStream: Object;
- private hangupAction: Function;
- private getLocalMediaStream: Function;
- private callerAction: Function;
- private calleeAction: Function;
- private handleConnection: Function;
- private disableCall: Boolean;
- private disableHangup: Boolean;
- private execOnce: Boolean;
- constructor(private ICE: ICEService) { }
- ngOnInit() {
- const vm = this;
- vm.execOnce = true;
- // parameters for STUN and TURN servers (optional)
- vm.configuration = {};
- // Create RTC object for local user
- vm.peerConnection = new RTCPeerConnection(vm.configuration);
- // When ICE is done, add SDP to local peer connection object
- vm.peerConnection.onicecandidate = (event) => {
- const iceCandidate = event.candidate;
- if (iceCandidate) {
- const newIceCandidate = new RTCIceCandidate(iceCandidate);
- vm.peerConnection.addIceCandidate(newIceCandidate);
- }
- };
- // When remote user starts stream, capture it and display it
- vm.peerConnection.onaddstream = (event) => {
- // Add remoteStream to the DOM, so local user can see remote's mediaStream
- vm.remoteStream = event.stream;
- };
- // End call
- vm.hangupAction = () => {
- vm.peerConnection.close();
- vm.peerConnection = null;
- vm.disableHangup = true;
- vm.disableCall = false;
- };
- // Get local data from audio / video input devices
- vm.getLocalMediaStream = (callback) => {
- navigator.mediaDevices.getUserMedia({
- video: true,
- // audio: true
- }).then((mediaStream) => {
- vm.disableCall = true;
- vm.disableHangup = false;
- // Add localStream to the DOM, so local user can see his mediaStream
- vm.localStream = mediaStream;
- // Add localStream to connection object
- vm.peerConnection.addStream(mediaStream);
- // Continue wih either caller or callee procedure
- callback();
- }).catch();
- };
- // Define MediaStreams callbacks.
- vm.callerAction = () => {
- vm.getLocalMediaStream(() => {
- // Create SDP with 'offer' type (for caller)
- vm.peerConnection.createOffer({
- offerToReceiveVideo: true
- })
- .then((description) => {
- // Send local SDP data to server
- vm.ICE.createCandidate(description)
- .subscribe(() => {
- // Send 'offer' (call) request to the callee
- vm.ICE.requestCall(2)
- .subscribe((remoteICE) => {
- // Call API until callee responds
- const interval = setInterval(() => {
- vm.ICE.statusCall(1)
- .subscribe((remoteSDP) => {
- // callee accepted the call, get his SDP data
- if (remoteSDP[0].remoteData) {
- // Set your local SDP as local SDP
- vm.peerConnection.setLocalDescription(description);
- // set Callee's local SDP as your remote SDP
- const remoteDesc = new RTCSessionDescription(JSON.parse(remoteSDP[0].remoteData));
- vm.peerConnection.setRemoteDescription(remoteDesc);
- // Stop calling API, callee responded
- clearInterval(interval);
- // HACK, call function twice (I don't like this solution)
- // because call wont start after first execuion
- if (vm.execOnce) {
- vm.execOnce = false;
- vm.callerAction();
- }
- }
- });
- }, 1500);
- // Bugfix for hack, interval is not cleared after second execution
- if (!vm.execOnce) {
- clearInterval(interval);
- }
- });
- });
- }).catch();
- });
- };
- vm.calleeAction = (event) => {
- vm.getLocalMediaStream(() => {
- // Check if someone is calling you
- vm.ICE.fetchCall()
- .subscribe((remoteICE) => {
- // Someone is calling you, get his SDP and set it as your remote
- const remoteDesc = new RTCSessionDescription(JSON.parse(remoteICE[0].data.remoteSDP));
- vm.peerConnection.setRemoteDescription(remoteDesc);
- // Create SDP with 'answer' type (for callee)
- vm.peerConnection.createAnswer()
- .then((description) => {
- // Set it as your local SDP
- vm.peerConnection.setLocalDescription(description);
- // Send your SDP to the server
- vm.ICE.createCandidate(description)
- .subscribe((createCandidateStatus) => {
- // Accept the caller's call
- vm.ICE.acceptCall(2)
- .subscribe((callAcceptStatus) => {
- // HACK, call function twice (I don't like this solution)
- // because call wont start after first execuion
- if (vm.execOnce) {
- vm.execOnce = false;
- vm.calleeAction();
- }
- });
- });
- });
- });
- });
- };
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement