UNPKG

kod-temp

Version:

Workano Communications SDK

122 lines (121 loc) 4.72 kB
import IssueReporter from '../../service/IssueReporter'; import Workano from '../index'; import Room from './Room'; const logger = IssueReporter.loggerFor('sdk-sip-room'); class SipRoom extends Room { static async connect({ extension, constraints, audioOnly = false, extra, room, }) { logger.info('connecting to sip room', { extension, audioOnly, room: !!room, }); if (!room) { await Workano.Phone.connect({ media: constraints, }); const withCamera = constraints && !!constraints.video; const callSession = await Workano.Phone.call(extension, withCamera, null, audioOnly, true); // eslint-disable-next-line no-param-reassign room = new SipRoom(callSession, extension, null, null, extra); } if (room && room.callSession && room.callSession.call) { room.setCallId(room.callSession.call.id); } logger.info('connected to room', { extension: room.extension, }); return room; } mute() { logger.info('mute sip room'); Workano.Phone.mute(this.callSession, false); this.sendMuteStatus(); } unmute() { logger.info('unmute sip room'); Workano.Phone.unmute(this.callSession, false); this.sendUnMuteStatus(); } getLocalGuestName() { // @ts-ignore: private return Workano.Phone.phone?.client.userAgent?.options.displayName || null; } // Overridden to not listen to websocket messages _transferEvents() { // Phone events Workano.Phone.on(this.ON_MESSAGE, this._boundOnMessage); Workano.Phone.on(this.ON_CHAT, this._boundOnChat); Workano.Phone.on(this.ON_SIGNAL, this._boundOnSignal); Workano.Phone.on(this.ON_VIDEO_INPUT_CHANGE, this._boundSaveLocalVideoStream); [this.ON_AUDIO_STREAM, this.ON_VIDEO_STREAM, this.ON_REMOVE_STREAM].forEach(event => Workano.Phone.on(event, (...args) => this.eventEmitter.emit.apply(this.eventEmitter, [event, ...args]))); } _onMessage(message) { // eslint-disable-next-line no-underscore-dangle const body = super._onMessage(message); if (!body) { return; } const getChannel = () => body.channels[0]; switch (body.type) { case 'ConfbridgeWelcome': body.channels.forEach((channel) => { this._onParticipantJoined(channel); }); break; case 'ConfbridgeJoin': { const channel = getChannel(); this._onParticipantJoined(channel); break; } case 'ConfbridgeLeave': { const channel = getChannel(); this._onParticipantLeft({ data: { call_id: channel.id, }, }); break; } default: break; } } async _onParticipantJoined(channel) { const isLocal = channel.channelvars.WAZO_SIP_CALL_ID === this._getCurrentSipCallIs(); const callId = channel.id; const ParticipantClass = isLocal ? Workano.LocalParticipant : Workano.RemoteParticipant; const name = channel.caller ? channel.caller.name : null; const extra = isLocal ? { guestName: this.getLocalGuestName(), } : {}; const participant = new ParticipantClass(this, { caller_id_name: name, call_id: callId, }, extra); const participantIdx = this.participants.findIndex(other => other.callId === participant.callId); if (participantIdx !== -1 && name) { this.participants[participantIdx].name = name; return; } if (isLocal) { // Should be updated by the Workano WS but we don't have it for now if (this.callSession) { this.callSession.ringing = false; } this._onLocalParticipantJoined(participant); // Give some time for the stream to be updated setTimeout(() => { this.eventEmitter.emit(this.ON_JOINED, participant, this.participants); }, 1000); } this.participants.push(participant); this._isParticipantJoining(participant); return participant; } _getCurrentSipCallIs() { return Workano.Phone.getSipSessionId(Workano.Phone.phone?.currentSipSession); } } export default SipRoom;