rn-verto-typescript
Version:
Verto Free Switch with React Native Type Script
99 lines (98 loc) • 3.68 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const react_native_webrtc_1 = require("react-native-webrtc");
const react_native_background_timer_1 = __importDefault(require("react-native-background-timer"));
react_native_background_timer_1.default.start();
class FSRTCPeerConnection {
constructor(options) {
this.options = options;
const { constraints, iceServers, onICEComplete, type, onICESDP, onICE, onRemoteStream, attachStream, onPeerStreamingError, onOfferSDP, onAnswerSDP, offerSDP, } = options;
console.log("attachStream: ", attachStream);
const defaultIceServers = [{ urls: ['stun:stun.l.google.com:19302'] }];
const peerConfig = {
iceServers: typeof iceServers === 'boolean' ? defaultIceServers : iceServers,
};
const peer = new react_native_webrtc_1.RTCPeerConnection(peerConfig);
this.peer = peer;
this.gathering = null;
this.done = false;
const iceHandler = () => {
this.done = true;
this.gathering = null;
if (onICEComplete) {
onICEComplete();
}
if (type === 'offer') {
onICESDP(peer.localDescription);
}
else if (onICESDP) {
onICESDP(peer.localDescription);
}
};
peer.onicecandidate = event => {
if (this.done) {
return;
}
if (!this.gathering) {
this.gathering = react_native_background_timer_1.default.setTimeout(iceHandler, 1000);
}
if (!event) {
this.done = true;
if (this.gathering) {
clearTimeout(this.gathering);
this.gathering = null;
}
iceHandler();
}
else if (event.candidate) {
onICE(event.candidate);
}
};
peer.onaddstream = (event) => {
const remoteMediaStream = event.stream;
if (onRemoteStream) {
onRemoteStream(remoteMediaStream);
}
};
peer.addStream(attachStream);
if (onOfferSDP) {
peer
.createOffer(constraints)
.then(sessionDescription => {
peer.setLocalDescription(sessionDescription);
onOfferSDP(sessionDescription);
})
.catch(onPeerStreamingError);
}
if (type === 'answer') {
peer
.setRemoteDescription(new react_native_webrtc_1.RTCSessionDescription(offerSDP))
.then(() => { })
.catch(onPeerStreamingError);
peer
.createAnswer()
.then(sessionDescription => {
peer.setLocalDescription(sessionDescription);
if (onAnswerSDP) {
onAnswerSDP(sessionDescription);
}
})
.catch(onPeerStreamingError);
}
}
// TODO Convert cbSuccess type to function
addAnswerSDP(sdp, cbSuccess, cbError) {
const { onPeerStreamingError } = this.options;
this.peer
.setRemoteDescription(new react_native_webrtc_1.RTCSessionDescription(sdp))
.then(cbSuccess || (() => { }))
.catch(cbError || onPeerStreamingError);
}
stop() {
this.peer.close();
}
}
exports.default = FSRTCPeerConnection;