UNPKG

ikanbi-react-sip

Version:

React wrapper for jssip

573 lines 31 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; Object.defineProperty(exports, "__esModule", { value: true }); var enums_1 = require("./../../lib/enums"); var JsSIP = require("jssip"); var PropTypes = require("prop-types"); var React = require("react"); var dummyLogger_1 = require("../../lib/dummyLogger"); var enums_2 = require("../../lib/enums"); var types_1 = require("../../lib/types"); var SipProvider = (function (_super) { __extends(SipProvider, _super); function SipProvider(props) { var _this = _super.call(this, props) || this; _this.registerSip = function () { if (_this.props.autoRegister) { throw new Error("Calling registerSip is not allowed when autoRegister === true"); } if (_this.state.sipStatus !== enums_2.SIP_STATUS_CONNECTED) { throw new Error("Calling registerSip is not allowed when sip status is " + _this.state.sipStatus + " (expected " + enums_2.SIP_STATUS_CONNECTED + ")"); } return _this.ua.register(); }; _this.unregisterSip = function () { if (_this.props.autoRegister) { throw new Error("Calling registerSip is not allowed when autoRegister === true"); } if (_this.state.sipStatus !== enums_2.SIP_STATUS_REGISTERED) { throw new Error("Calling unregisterSip is not allowed when sip status is " + _this.state.sipStatus + " (expected " + enums_2.SIP_STATUS_CONNECTED + ")"); } return _this.ua.unregister(); }; _this.answerCall = function () { }; _this.startCall = function (destination, callExtraHeaders) { if (!destination) { throw new Error("Destination must be defined (" + destination + " given)"); } if (_this.state.sipStatus !== enums_2.SIP_STATUS_CONNECTED) { throw new Error("Calling startCall() is not allowed when sip status is " + _this.state.sipStatus + " (expected " + enums_2.SIP_STATUS_CONNECTED + ")"); } if (_this.state.callStatus !== enums_2.CALL_STATUS_IDLE && _this.state.callStatus !== enums_1.CALL_STATUS_HOLDING) { throw new Error("Calling startCall() is not allowed when call status is " + _this.state.callStatus + " (expected " + enums_2.CALL_STATUS_IDLE + ")"); } var _a = _this.props, iceServers = _a.iceServers, sessionTimersExpires = _a.sessionTimersExpires; var extraHeaders = _this.props.extraHeaders.invite; if (extraHeaders) { if (callExtraHeaders && Array.isArray(callExtraHeaders) && Array.isArray(extraHeaders)) { extraHeaders = extraHeaders.concat(callExtraHeaders); } } else { if (callExtraHeaders && Array.isArray(callExtraHeaders)) { extraHeaders = callExtraHeaders; } } console.log('ExtraHeaders', extraHeaders); var options = { extraHeaders: extraHeaders, mediaConstraints: { audio: true, video: false }, rtcOfferConstraints: { iceRestart: _this.props.iceRestart }, pcConfig: { iceServers: iceServers, }, sessionTimersExpires: sessionTimersExpires, }; _this.ua.call(destination, options); _this.setState({ callStatus: enums_2.CALL_STATUS_STARTING }); }; _this.stopCall = function () { _this.setState({ callStatus: enums_2.CALL_STATUS_STOPPING }); _this.state.prospectRtcSession.terminate(); }; _this.holdCall = function () { _this.setState({ callStatus: enums_1.CALL_STATUS_HOLDING }); _this.state.prospectRtcSession.hold(); }; _this.unHoldCall = function () { _this.setState({ callStatus: enums_1.CALL_STATUS_UNHOLDING }); _this.state.prospectRtcSession.unhold(); }; _this.sendDtmf = function (dtmf) { if (dtmf) { if (_this.state.prospectRtcSession && _this.state.callStatus === enums_2.CALL_STATUS_ACTIVE) { console.log('Sending DTMF: ' + dtmf); _this.state.prospectRtcSession.sendDTMF(dtmf); } else { console.log('No Prospect Call is Active to send DTMF!'); } } else { console.log('No DTMF provided to SEND!'); } }; _this.startSupportCall = function (destination, callExtraHeaders) { if (!destination) { throw new Error("Destination must be defined (" + destination + " given)"); } if (_this.state.sipStatus !== enums_2.SIP_STATUS_CONNECTED) { throw new Error("Calling startCall() is not allowed when sip status is " + _this.state.sipStatus + " (expected " + enums_2.SIP_STATUS_CONNECTED + ")"); } if (_this.state.callStatus == enums_2.CALL_STATUS_STARTING || _this.state.callStatus == enums_2.CALL_STATUS_ACTIVE || _this.state.supportCallStatus == enums_2.CALL_STATUS_ACTIVE) { throw new Error("Calling Support is not allowed when Primary Call Status is " + _this.state.callStatus + " and Support Call Status is " + _this.state.supportCallStatus); } var _a = _this.props, iceServers = _a.iceServers, sessionTimersExpires = _a.sessionTimersExpires; var extraHeaders = _this.props.extraHeaders.invite; if (extraHeaders) { if (callExtraHeaders && Array.isArray(callExtraHeaders) && Array.isArray(extraHeaders)) { extraHeaders = extraHeaders.concat(callExtraHeaders); } } else { if (callExtraHeaders && Array.isArray(callExtraHeaders)) { extraHeaders = callExtraHeaders; } } console.log('ExtraHeaders', extraHeaders); var options = { extraHeaders: extraHeaders, mediaConstraints: { audio: true, video: false }, rtcOfferConstraints: { iceRestart: _this.props.iceRestart }, pcConfig: { iceServers: iceServers, }, sessionTimersExpires: sessionTimersExpires, }; _this.ua.call(destination, options); _this.setState({ supportCallStatus: enums_2.CALL_STATUS_STARTING }); }; _this.stopSupportCall = function () { _this.setState({ supportCallStatus: enums_2.CALL_STATUS_STOPPING }); _this.state.supportRtcSession.terminate(); }; _this.holdSupportCall = function () { _this.setState({ supportCallStatus: enums_1.CALL_STATUS_HOLDING }); _this.state.supportRtcSession.hold(); }; _this.unHoldSupportCall = function () { _this.setState({ supportCallStatus: enums_2.CALL_STATUS_ACTIVE }); _this.state.supportRtcSession.unhold(); }; _this.state = { sipStatus: enums_2.SIP_STATUS_DISCONNECTED, sipErrorType: null, sipErrorMessage: null, prospectRtcSession: null, supportRtcSession: null, callStatus: enums_2.CALL_STATUS_IDLE, callDirection: null, callCounterpart: null, supportCallStatus: enums_2.CALL_STATUS_IDLE, supportCallDirection: null, supportCallCounterpart: null, }; _this.ua = null; return _this; } SipProvider.prototype.getChildContext = function () { return { sip: __assign(__assign({}, this.props), { status: this.state.sipStatus, errorType: this.state.sipErrorType, errorMessage: this.state.sipErrorMessage }), call: { id: "??", status: this.state.callStatus, direction: this.state.callDirection, counterpart: this.state.callCounterpart, }, supportCall: { id: "??", status: this.state.supportCallStatus, direction: this.state.supportCallDirection, counterpart: this.state.supportCallCounterpart, }, registerSip: this.registerSip, unregisterSip: this.unregisterSip, startCall: this.startCall, stopCall: this.stopCall, holdCall: this.holdCall, unHoldCall: this.unHoldCall, sendDtmf: this.sendDtmf, startSupportCall: this.startSupportCall, stopSupportCall: this.stopSupportCall, holdSupportCall: this.holdSupportCall, unHoldSupportCall: this.unHoldSupportCall, }; }; SipProvider.prototype.componentDidMount = function () { this.remoteAudio = window.document.createElement("audio"); this.remoteAudio.id = "sip-provider-audio"; window.document.body.appendChild(this.remoteAudio); this.ringBack = window.document.createElement("audio"); this.ringBack.id = "ring-back"; this.ringBack.volume = 0.2; this.ringBack.loop = true; this.ringBack.src = "data:audio/mpeg;base64,SUQzBAAAAAAAI1RTU0UAAAAPAAADTGF2ZjU4LjM1LjEwMAAAAAAAAAAAAAAA/+MgxAAAAANIAAAAABr/wQBAEMQAgGbi4PhjgmD7wQDGXgmH8uCGGCjgfB8oD/BMPlAQBAHw//fWD/5cHwfNZc/EByUDGoH5QIAN/8Bg+D4Pn//5cH9d2THOPTxzjn2jUDY/CwwNFAb/4yLEWwA4BAAAMAAAn99MA8xTBAANI+gx5OFszQTuO0LK/0jRNE3QFeG6OYYBc4Fkf8uLTZ2TFQFfJIdg5Y9kU//U2god5aH2RNMY0ZIeCY//01akOmQ0vDjIOQchpJGKZZLBe//6Dpf/4yDEthIoBaQBQBgAfb6zcsEaWy6TRUMC+RRZmbp///+gt31f/NzMzNEjCmapuZn0DiCB4rmi/YeOU5RYAGhEbBZWwxI4NIHkljQdQ1h1Y4ZzqninQkLtYibqg4hjWFIOiC9bO3vhy//jIsTIJawWTAGNkABSu1f5Etzcr7J79q1jL8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YQwmlHauVc3//11QGVfPYKik9CfKXrGVj9Yy0mOlGU/uziSAsDMGx4CJHiwlIiUi+SeKBU728kW+VZ9T3HiOWLGtRrX//3SqP6/X9BHUPLFUxBTUUzLjEwMFVVVVX/4yLEshfI/tY+A9gmVVVVVVVVVVVVVVVVVVVVVVU511VMQU1FMy4xMDBVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVX/4yDErhFA1lAEGEZIVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVf/jIsSmAJAEAADQAABVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVQ=="; window.document.body.appendChild(this.ringBack); this.reconfigureDebug(); this.reinitializeJsSIP(); }; SipProvider.prototype.componentDidUpdate = function (prevProps) { if (this.props.debug !== prevProps.debug) { this.reconfigureDebug(); } if (this.props.host !== prevProps.host || this.props.port !== prevProps.port || this.props.pathname !== prevProps.pathname || this.props.user !== prevProps.user || this.props.password !== prevProps.password || this.props.autoRegister !== prevProps.autoRegister) { this.reinitializeJsSIP(); } }; SipProvider.prototype.componentWillUnmount = function () { if (this.ua) { this.ua.stop(); this.ua = null; } }; SipProvider.prototype.reconfigureDebug = function () { var debug = this.props.debug; if (debug) { JsSIP.debug.enable("JsSIP:*"); this.logger = console; } else { JsSIP.debug.disable(); this.logger = dummyLogger_1.default; } }; SipProvider.prototype.reinitializeJsSIP = function () { var _this = this; if (this.ua) { this.ua.stop(); this.ua = null; } var _a = this.props, host = _a.host, port = _a.port, pathname = _a.pathname, user = _a.user, password = _a.password, autoRegister = _a.autoRegister, displayName = _a.displayName; if (!host || !port || !user) { this.setState({ sipStatus: enums_2.SIP_STATUS_DISCONNECTED, sipErrorType: null, sipErrorMessage: null, }); return; } try { var socket = new JsSIP.WebSocketInterface("wss://" + host + ":" + port + pathname); this.ua = new JsSIP.UA({ uri: "sip:" + user + "@" + host, password: password, sockets: [socket], register: autoRegister, display_name: displayName }); } catch (error) { this.logger.debug("Error", error.message, error); this.setState({ sipStatus: enums_2.SIP_STATUS_ERROR, sipErrorType: enums_2.SIP_ERROR_TYPE_CONFIGURATION, sipErrorMessage: error.message, }); return; } var ua = this.ua; ua.on("connecting", function () { console.log('connecting'); _this.logger.debug('UA "connecting" event'); if (_this.ua !== ua) { return; } _this.setState({ sipStatus: enums_2.SIP_STATUS_CONNECTING, sipErrorType: null, sipErrorMessage: null, }); }); ua.on("connected", function (data) { console.log('REACT_SIP 1.4.7'); console.log('connected data', data); var eventHandlers = { 'succeeded': function (e) { console.log('OPTIONS succeeded', e); }, 'failed': function (e) { console.log('OPTIONS failed', e); } }; var options = { 'eventHandlers': eventHandlers }; ua.sendOptions('sip:bob@example.com', null, options); _this.logger.debug('UA "connected" event'); if (_this.ua !== ua) { return; } _this.setState({ sipStatus: enums_2.SIP_STATUS_CONNECTED, sipErrorType: null, sipErrorMessage: null, }); }); ua.on("disconnected", function () { console.log('disconnected'); _this.logger.debug('UA "disconnected" event'); if (_this.ua !== ua) { return; } _this.setState({ sipStatus: enums_2.SIP_STATUS_ERROR, sipErrorType: enums_2.SIP_ERROR_TYPE_CONNECTION, sipErrorMessage: "disconnected", }); }); ua.on("registered", function (data) { console.log('registered'); _this.logger.debug('UA "registered" event', data); if (_this.ua !== ua) { return; } _this.setState({ sipStatus: enums_2.SIP_STATUS_REGISTERED, callStatus: enums_2.CALL_STATUS_IDLE, supportCallStatus: enums_2.CALL_STATUS_IDLE, }); }); ua.on("unregistered", function () { console.log('unregistered'); _this.logger.debug('UA "unregistered" event'); if (_this.ua !== ua) { return; } if (ua.isConnected()) { _this.setState({ sipStatus: enums_2.SIP_STATUS_CONNECTED, callStatus: enums_2.CALL_STATUS_IDLE, supportCallStatus: enums_2.CALL_STATUS_IDLE, callDirection: null, supportCallDirection: null, }); } else { _this.setState({ sipStatus: enums_2.SIP_STATUS_DISCONNECTED, callStatus: enums_2.CALL_STATUS_IDLE, supportCallStatus: enums_2.CALL_STATUS_IDLE, callDirection: null, supportCallDirection: null, }); } }); ua.on("registrationFailed", function (data) { console.log('registrationFailed'); _this.logger.debug('UA "registrationFailed" event'); if (_this.ua !== ua) { return; } _this.setState({ sipStatus: enums_2.SIP_STATUS_ERROR, sipErrorType: enums_2.SIP_ERROR_TYPE_REGISTRATION, sipErrorMessage: data, }); }); ua.on("newRTCSession", function (_a) { var originator = _a.originator, rtcSession = _a.session, rtcRequest = _a.request; console.log('newRTCSession', rtcSession); console.log('request', rtcRequest); _this.ringBack.play(); if (!_this || _this.ua !== ua) { return; } var callType = rtcRequest.getHeader('X-CALL-TYPE'); console.log('CALL TYPE: ' + callType); if (originator === "local") { var foundUri = rtcRequest.to.toString(); var delimiterPosition = foundUri.indexOf(";") || null; if (callType === enums_1.CALL_TYPE_SUPPORT) { _this.setState({ supportCallDirection: enums_2.CALL_DIRECTION_OUTGOING, supportCallStatus: enums_2.CALL_STATUS_STARTING, supportCallCounterpart: foundUri.substring(0, delimiterPosition) || foundUri, }); } else { _this.setState({ callDirection: enums_2.CALL_DIRECTION_OUTGOING, callStatus: enums_2.CALL_STATUS_STARTING, callCounterpart: foundUri.substring(0, delimiterPosition) || foundUri, }); } } if (callType === enums_1.CALL_TYPE_SUPPORT) { console.log('Setting SUPPORT Session'); _this.setState({ supportRtcSession: rtcSession }); } else { console.log('Setting PROSPECT Session'); _this.setState({ prospectRtcSession: rtcSession }); } rtcSession.on("failed", function () { console.log('failed'); if (_this.ua !== ua) { return; } _this.ringBack.pause(); if (callType === enums_1.CALL_TYPE_SUPPORT) { _this.setState({ supportRtcSession: null, supportCallStatus: enums_2.CALL_STATUS_IDLE, supportCallDirection: null, supportCallCounterpart: null, }); } else { _this.setState({ prospectRtcSession: null, callStatus: enums_2.CALL_STATUS_IDLE, callDirection: null, callCounterpart: null, }); } }); rtcSession.on("ended", function () { console.log('ended'); _this.ringBack.pause(); if (_this.ua !== ua) { return; } if (callType == enums_1.CALL_TYPE_SUPPORT) { _this.setState({ supportRtcSession: null, supportCallStatus: enums_2.CALL_STATUS_IDLE, supportCallDirection: null, supportCallCounterpart: null, }); } else { _this.setState({ prospectRtcSession: null, callStatus: enums_2.CALL_STATUS_IDLE, callDirection: null, callCounterpart: null, }); } }); rtcSession.on("accepted", function () { console.log('accepted'); if (_this.ua !== ua) { return; } _this.ringBack.pause(); if (callType === enums_1.CALL_TYPE_SUPPORT) { _this.setState({ supportCallStatus: enums_2.CALL_STATUS_ACTIVE }); } else { _this.setState({ callStatus: enums_2.CALL_STATUS_ACTIVE }); } }); if (callType === enums_1.CALL_TYPE_SUPPORT) { rtcSession.connection.addEventListener('addstream', function (event) { console.log(event); _this.remoteAudio.srcObject = event.stream; _this.remoteAudio.play(); }); } else { rtcSession.connection.addEventListener('addstream', function (event) { console.log(event); _this.remoteAudio.srcObject = event.stream; _this.remoteAudio.play(); }); } rtcSession.on("hold", function () { if (callType === enums_1.CALL_TYPE_SUPPORT) { _this.setState({ supportCallStatus: enums_1.CALL_STATUS_HOLD }); } else { _this.setState({ callStatus: enums_1.CALL_STATUS_HOLD }); } }); rtcSession.on("unhold", function () { console.log('unhold'); if (callType === enums_1.CALL_TYPE_SUPPORT) { _this.setState({ supportCallStatus: enums_2.CALL_STATUS_ACTIVE }); } else { _this.setState({ callStatus: enums_2.CALL_STATUS_ACTIVE }); } }); }); var extraHeadersRegister = this.props.extraHeaders.register || []; if (extraHeadersRegister.length) { ua.registrator().setExtraHeaders(extraHeadersRegister); } ua.start(); }; SipProvider.prototype.render = function () { return this.props.children; }; SipProvider.childContextTypes = { sip: types_1.sipPropType, call: types_1.callPropType, supportCall: types_1.callPropType, registerSip: PropTypes.func, unregisterSip: PropTypes.func, startCall: PropTypes.func, stopCall: PropTypes.func, holdCall: PropTypes.func, unHoldCall: PropTypes.func, sendDtmf: PropTypes.func, startSupportCall: PropTypes.func, stopSupportCall: PropTypes.func, holdSupportCall: PropTypes.func, unHoldSupportCall: PropTypes.func, }; SipProvider.propTypes = { host: PropTypes.string, port: PropTypes.number, pathname: PropTypes.string, user: PropTypes.string, password: PropTypes.string, autoRegister: PropTypes.bool, autoAnswer: PropTypes.bool, iceRestart: PropTypes.bool, sessionTimersExpires: PropTypes.number, extraHeaders: types_1.extraHeadersPropType, iceServers: types_1.iceServersPropType, debug: PropTypes.bool, displayName: PropTypes.string, children: PropTypes.node, }; SipProvider.defaultProps = { host: null, port: null, pathname: "", user: null, password: null, autoRegister: true, autoAnswer: false, iceRestart: false, sessionTimersExpires: 120, extraHeaders: { register: [], invite: [] }, iceServers: [], debug: false, displayName: "", children: null, }; return SipProvider; }(React.Component)); exports.default = SipProvider; //# sourceMappingURL=index.js.map