video-auth-js-sdk
Version:
A SDK to authenticate users with camera through a realtime stream
271 lines (270 loc) • 10.2 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.CallTopicManager = CallTopicManager;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _errorHandler = require("../errorHandler");
var _webrtcPeer = require("./webrtcPeer");
var _utility = _interopRequireDefault(require("../utility/utility"));
function CallTopicManager(_ref) {
var app = _ref.app,
callId = _ref.callId,
topic = _ref.topic,
mediaType = _ref.mediaType,
direction = _ref.direction,
deviceManager = _ref.deviceManager,
isScreenShare = _ref.isScreenShare,
onHTMLElement = _ref.onHTMLElement,
onPeerConnect = _ref.onPeerConnect;
var config = {
callId: callId,
state: 0,
//0: disconnected, 1: connecting, 2: failed, 3: connected, 4: disconnected
peer: null,
topic: topic,
mediaType: mediaType,
direction: direction,
isScreenShare: isScreenShare,
sdpOfferRequestSent: false,
htmlElement: null,
isDestroyed: false,
dataStream: null,
statusEventsInterval: null,
audioObject: null,
alreadyAddedStreamTrackToElement: false
};
var peerStates = {
DISCONNECTED: 0,
CONNECTING: 1,
FAILED: 3,
CONNECTED: 4
};
var publicized = {
setPeerState: function setPeerState(state) {
config.state = state;
},
setIsScreenShare: function setIsScreenShare() {
config.isScreenShare = true;
},
setDirection: function setDirection(direction) {
config.direction = direction;
},
getPeer: function getPeer() {
return config.peer;
},
isPeerConnecting: function isPeerConnecting() {
return config.state === peerStates.CONNECTING;
},
isPeerFailed: function isPeerFailed() {
return config.state === peerStates.FAILED;
},
isPeerConnected: function isPeerConnected() {
return config.state === peerStates.CONNECTED;
},
isPeerDisconnected: function isPeerDisconnected() {
return config.state === peerStates.DISCONNECTED;
},
createTopic: function createTopic() {
var manager = this;
if (config.peer) {
return;
}
this.generateSdpOfferOptions().then(function (options) {
console.debug("[SDK][generateSdpOfferOptions] Options for this request have been resolved: ", {
options: options
}, "topic: ", config.topic, "direction: ", config.direction);
manager.establishPeerConnection(options);
})["catch"](function (error) {
console.error(error);
});
},
generateSdpOfferOptions: function generateSdpOfferOptions() {
return new Promise(function (resolve, reject) {
var options = {
configuration: {
iceServers: app.authSessionInfo.turnsList
}
};
options.stream = app.store.localCameraStream;
resolve(options);
console.log("[SDK][getSdpOfferOptions] ", "topic: ", config.topic, "mediaType: ", config.mediaType, "direction: ", config.direction, "options: ", options);
});
},
establishPeerConnection: function establishPeerConnection(options) {
var manager = this;
config.state = peerStates.CONNECTING;
config.peer = new _webrtcPeer.WebrtcPeerConnection({
callId: config.callId,
direction: config.direction,
mediaType: config.mediaType,
stream: options.stream,
rtcPeerConfig: options.configuration,
connectionStateChange: publicized.onConnectionStateChange,
iceConnectionStateChange: publicized.onIceConnectionStateChange
}, function (err) {
if (err) {
var errorString = "[SDK][start/webRtc Peer] Error: " + err;
console.error(errorString);
app.publicCallbacks.onError({
code: _errorHandler.errorList.FAILED_TO_OPEN_PEER.code,
message: errorString
});
return;
}
config.peer.generateOffer(function (err, sdpOffer) {
// app.sdkParams.consoleLogging && console.debug("[SDK][establishPeerConnection][generateOffer] GenerateOffer:: ", " sdpOffer: ", sdpOffer, " err: ", err);
if (err) {
var _errorString = "[SDK][start/WebRtc Peer/generateOffer] " + err;
console.error(_errorString);
app.publicCallbacks.onError({
code: _errorHandler.errorList.FAILED_TO_OPEN_PEER.code,
message: _errorString
});
return;
}
if (!config.sdpOfferRequestSent) {
config.sdpOfferRequestSent = true;
manager.sendSDPOfferRequestMessage(sdpOffer, 1);
}
});
});
},
onConnectionStateChange: function onConnectionStateChange() {
if (!config.peer || publicized.isDestroyed()) {
return; //avoid log errors
}
console.log("[SDK][peerConnection.onconnectionstatechange] ", "peer: ", config.topic, " peerConnection.connectionState: ", config.peer.peerConnection.connectionState);
if (config.peer.peerConnection.connectionState === "failed") {
if (publicized.isPeerFailed() || publicized.isDestroyed()) return;
config.state = peerStates.FAILED;
app.publicCallbacks.onDebug({
type: 'CALL_STATUS',
message: "Stream Peer (".concat(config.topic, ") has failed!")
});
app.publicCallbacks.onError(_errorHandler.errorList.PEER_FAILED);
}
if (config.peer.peerConnection.connectionState === 'connected') {
config.state = peerStates.CONNECTED;
onPeerConnect && onPeerConnect();
onPeerConnect = null;
}
},
onIceConnectionStateChange: function onIceConnectionStateChange() {
if (!config.peer || publicized.isDestroyed()) {
return; //avoid log errors
}
console.log("[SDK][oniceconnectionstatechange] ", "peer: ", config.topic, " peerConnection.connectionState: ", config.peer.peerConnection.iceConnectionState);
if (config.peer.peerConnection.iceConnectionState === 'disconnected') {
config.state = peerStates.DISCONNECTED;
app.publicCallbacks.onDebug({
type: 'CALL_STATUS',
message: "Stream Peer (".concat(config.topic, ") is disconnected!"),
errorInfo: config.peer
});
}
if (config.peer.peerConnection.iceConnectionState === "failed") {
if (publicized.isPeerFailed() || publicized.isDestroyed()) return;
config.state = peerStates.FAILED;
app.publicCallbacks.onDebug({
type: 'CALL_STATUS',
message: "Stream Peer (".concat(config.topic, ") ice has failed!")
});
app.publicCallbacks.onError(_errorHandler.errorList.PEER_FAILED);
}
if (config.peer.peerConnection.iceConnectionState === "connected") {
config.state = peerStates.CONNECTED;
onPeerConnect && onPeerConnect();
onPeerConnect = null;
app.publicCallbacks.onDebug({
type: 'CALL_STATUS',
message: "Stream Peer (".concat(config.topic, ") ice has connected!")
});
}
},
sendSDPOfferRequestMessage: function sendSDPOfferRequestMessage(sdpOffer, retries) {
app.messenger.sendCallMessage({
id: 'SEND_SDP_OFFER',
sdpOffer: sdpOffer,
useComedia: true,
useSrtp: false,
topic: config.topic,
mediaType: 2,
chatId: app.authSessionInfo.callId
}, null, 4000, function (result) {
if (result.done === 'FALSE' && retries > 0) {
retries -= 1;
publicized.sendSDPOfferRequestMessage(sdpOffer);
}
});
},
stopTopicOnServer: function stopTopicOnServer() {
return new Promise(function (resolve) {
app.messenger.sendCallMessage({
id: 'STOP',
topic: config.topic,
chatId: app.authSessionInfo.callId
}, function (result) {
if (result.done === 'TRUE' || result.done === 'SKIP') {
// manager.reconnectTopic();
resolve();
} else {
console.warn("[SDK] SDK tried to stop the topic but failed.", config.topic);
}
}, {});
});
},
removeTopic: function removeTopic() {
var _this = this;
return (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee() {
var manager;
return _regenerator["default"].wrap(function _callee$(_context) {
while (1) switch (_context.prev = _context.next) {
case 0:
manager = _this;
app.messenger.sendCallMessage({
id: 'CLOSE'
}, null, {});
if (config.peer) {
config.peer.dispose();
config.peer = null;
config.state = peerStates.DISCONNECTED;
}
case 3:
case "end":
return _context.stop();
}
}, _callee);
}))();
},
updateStream: function updateStream(stream) {
config.dataStream = stream;
config.peer.updateStream(stream);
},
stopStatusPrint: function stopStatusPrint() {
config.statusEventsInterval && clearInterval(config.statusEventsInterval);
},
isDestroyed: function isDestroyed() {
return config.isDestroyed;
},
destroy: function destroy() {
return (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2() {
return _regenerator["default"].wrap(function _callee2$(_context2) {
while (1) switch (_context2.prev = _context2.next) {
case 0:
config.isDestroyed = true;
// publicized.removeStreamHTML();
_context2.next = 3;
return publicized.removeTopic();
case 3:
case "end":
return _context2.stop();
}
}, _callee2);
}))();
}
};
return publicized;
}