UNPKG

vertinho

Version:

Library to make conference apps and softphones through WebSockets with FreeSWITCH mod_verto.

123 lines (104 loc) 3.14 kB
/** * _ _ _ * | | (_) | | * __ _____ _ __| |_ _ _ __ | |__ ___ * \ \ / / _ \ '__| __| | '_ \| '_ \ / _ \ * \ V / __/ | | |_| | | | | | | | (_) | * \_/ \___|_| \__|_|_| |_|_| |_|\___/ * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * And see https://github.com/Mazuh/vertinho for the full license details. */ import 'webrtc-adapter'; export default class FSRTCPeerConnection { constructor(options) { this.options = options; const { constraints, iceServers, onICEComplete, type, onICESDP, onICE, onRemoteStream, attachStream, onPeerStreamingError, onOfferSDP, onAnswerSDP, offerSDP, } = options; const defaultIceServers = [{ urls: ['stun:stun.l.google.com:19302'] }]; const peerConfig = { iceServers: typeof iceServers === 'boolean' ? defaultIceServers : iceServers, }; const peer = new RTCPeerConnection(peerConfig); this.peer = peer; this.gathering = false; 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 = 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.ontrack = (event) => { const remoteMediaStream = event.streams[0]; if (onRemoteStream) { onRemoteStream(remoteMediaStream); } }; attachStream.getTracks().forEach(track => peer.addTrack(track, attachStream)); if (onOfferSDP) { peer.createOffer(constraints).then((sessionDescription) => { peer.setLocalDescription(sessionDescription); onOfferSDP(sessionDescription); }).catch(onPeerStreamingError); } if (type === 'answer') { peer.setRemoteDescription(new RTCSessionDescription(offerSDP)) .then(() => {}) .catch(onPeerStreamingError); peer.createAnswer().then((sessionDescription) => { peer.setLocalDescription(sessionDescription); if (onAnswerSDP) { onAnswerSDP(sessionDescription); } }).catch(onPeerStreamingError); } } addAnswerSDP(sdp, cbSuccess, cbError) { const { onPeerStreamingError } = this.options; this.peer.setRemoteDescription(new RTCSessionDescription(sdp)) .then(cbSuccess || (() => {})) .catch(cbError || onPeerStreamingError); } stop() { this.peer.close(); } }