@wazo/sdk 0.43.5 → 1.0.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (155) hide show
  1. package/dist/wazo-sdk.js +3 -3
  2. package/esm/__tests__/__voice__/call.test.js +72 -0
  3. package/esm/__tests__/__voice__/softphone.test.js +66 -0
  4. package/esm/api/auth.js +1 -5
  5. package/esm/checker/checks/webrtc.js +2 -1
  6. package/esm/domain/InvalidState.js +8 -0
  7. package/esm/domain/InvalidStateTransition.js +10 -0
  8. package/esm/domain/Phone/WebRTCPhone.js +33 -35
  9. package/esm/simple/Auth.js +12 -6
  10. package/esm/simple/Phone.js +2 -1
  11. package/esm/simple/__tests__/Auth.test.js +64 -8
  12. package/esm/simple/index.js +8 -2
  13. package/esm/simple/room/Room.js +2 -1
  14. package/esm/simple/room/SipRoom.js +2 -1
  15. package/esm/state-machine/call-state-machine.js +114 -0
  16. package/esm/state-machine/softphone-state-machine.js +39 -0
  17. package/esm/state-machine/utils.js +40 -0
  18. package/esm/types/__tests__/__voice__/call.test.d.ts +2 -0
  19. package/esm/types/__tests__/__voice__/call.test.d.ts.map +1 -0
  20. package/esm/types/__tests__/__voice__/softphone.test.d.ts +2 -0
  21. package/esm/types/__tests__/__voice__/softphone.test.d.ts.map +1 -0
  22. package/esm/types/api/auth.d.ts.map +1 -1
  23. package/esm/types/checker/checks/webrtc.d.ts.map +1 -1
  24. package/esm/types/domain/InvalidState.d.ts +6 -0
  25. package/esm/types/domain/InvalidState.d.ts.map +1 -0
  26. package/esm/types/domain/InvalidStateTransition.d.ts +7 -0
  27. package/esm/types/domain/InvalidStateTransition.d.ts.map +1 -0
  28. package/esm/types/domain/Phone/WebRTCPhone.d.ts +0 -1
  29. package/esm/types/domain/Phone/WebRTCPhone.d.ts.map +1 -1
  30. package/esm/types/domain/types.d.ts +4 -0
  31. package/esm/types/domain/types.d.ts.map +1 -1
  32. package/esm/types/simple/Auth.d.ts.map +1 -1
  33. package/esm/types/simple/Phone.d.ts.map +1 -1
  34. package/esm/types/simple/index.d.ts +7 -2
  35. package/esm/types/simple/index.d.ts.map +1 -1
  36. package/esm/types/simple/room/Room.d.ts.map +1 -1
  37. package/esm/types/simple/room/SipRoom.d.ts +1 -1
  38. package/esm/types/simple/room/SipRoom.d.ts.map +1 -1
  39. package/esm/types/state-machine/call-state-machine.d.ts +43 -0
  40. package/esm/types/state-machine/call-state-machine.d.ts.map +1 -0
  41. package/esm/types/state-machine/softphone-state-machine.d.ts +19 -0
  42. package/esm/types/state-machine/softphone-state-machine.d.ts.map +1 -0
  43. package/esm/types/state-machine/utils.d.ts +9 -0
  44. package/esm/types/state-machine/utils.d.ts.map +1 -0
  45. package/esm/types/utils/api-requester.d.ts.map +1 -1
  46. package/esm/types/utils/sdp.d.ts +6 -0
  47. package/esm/types/utils/sdp.d.ts.map +1 -1
  48. package/esm/types/utils/sip-logger.d.ts +4 -0
  49. package/esm/types/utils/sip-logger.d.ts.map +1 -0
  50. package/esm/types/utils/webrtc.d.ts +40 -0
  51. package/esm/types/utils/webrtc.d.ts.map +1 -1
  52. package/esm/types/voice/call.d.ts +110 -0
  53. package/esm/types/voice/call.d.ts.map +1 -0
  54. package/esm/types/voice/softphone.d.ts +77 -0
  55. package/esm/types/voice/softphone.d.ts.map +1 -0
  56. package/esm/types/web-rtc-client.d.ts +1 -36
  57. package/esm/types/web-rtc-client.d.ts.map +1 -1
  58. package/esm/utils/__tests__/api-requester.test.js +0 -17
  59. package/esm/utils/api-requester.js +1 -9
  60. package/esm/utils/sdp.js +26 -0
  61. package/esm/utils/sip-logger.js +32 -0
  62. package/esm/utils/webrtc.js +465 -0
  63. package/esm/voice/call.js +527 -0
  64. package/esm/voice/softphone.js +347 -0
  65. package/esm/web-rtc-client.js +86 -584
  66. package/lib/__tests__/__voice__/call.test.js +86 -0
  67. package/lib/__tests__/__voice__/softphone.test.js +80 -0
  68. package/lib/api/auth.js +1 -5
  69. package/lib/checker/checks/webrtc.js +2 -1
  70. package/lib/domain/InvalidState.js +9 -0
  71. package/lib/domain/InvalidStateTransition.js +10 -0
  72. package/lib/domain/Phone/WebRTCPhone.js +33 -35
  73. package/lib/simple/Auth.js +12 -6
  74. package/lib/simple/Phone.js +2 -1
  75. package/lib/simple/__tests__/Auth.test.js +64 -11
  76. package/lib/simple/index.js +6 -0
  77. package/lib/simple/room/Room.js +4 -3
  78. package/lib/simple/room/SipRoom.js +2 -1
  79. package/lib/state-machine/call-state-machine.js +120 -0
  80. package/lib/state-machine/softphone-state-machine.js +42 -0
  81. package/lib/state-machine/utils.js +52 -0
  82. package/lib/types/__tests__/__voice__/call.test.d.ts +2 -0
  83. package/lib/types/__tests__/__voice__/call.test.d.ts.map +1 -0
  84. package/lib/types/__tests__/__voice__/softphone.test.d.ts +2 -0
  85. package/lib/types/__tests__/__voice__/softphone.test.d.ts.map +1 -0
  86. package/lib/types/api/auth.d.ts.map +1 -1
  87. package/lib/types/checker/checks/webrtc.d.ts.map +1 -1
  88. package/lib/types/domain/InvalidState.d.ts +6 -0
  89. package/lib/types/domain/InvalidState.d.ts.map +1 -0
  90. package/lib/types/domain/InvalidStateTransition.d.ts +7 -0
  91. package/lib/types/domain/InvalidStateTransition.d.ts.map +1 -0
  92. package/lib/types/domain/Phone/WebRTCPhone.d.ts +0 -1
  93. package/lib/types/domain/Phone/WebRTCPhone.d.ts.map +1 -1
  94. package/lib/types/domain/types.d.ts +4 -0
  95. package/lib/types/domain/types.d.ts.map +1 -1
  96. package/lib/types/simple/Auth.d.ts.map +1 -1
  97. package/lib/types/simple/Phone.d.ts.map +1 -1
  98. package/lib/types/simple/index.d.ts +7 -2
  99. package/lib/types/simple/index.d.ts.map +1 -1
  100. package/lib/types/simple/room/Room.d.ts.map +1 -1
  101. package/lib/types/simple/room/SipRoom.d.ts +1 -1
  102. package/lib/types/simple/room/SipRoom.d.ts.map +1 -1
  103. package/lib/types/state-machine/call-state-machine.d.ts +43 -0
  104. package/lib/types/state-machine/call-state-machine.d.ts.map +1 -0
  105. package/lib/types/state-machine/softphone-state-machine.d.ts +19 -0
  106. package/lib/types/state-machine/softphone-state-machine.d.ts.map +1 -0
  107. package/lib/types/state-machine/utils.d.ts +9 -0
  108. package/lib/types/state-machine/utils.d.ts.map +1 -0
  109. package/lib/types/utils/api-requester.d.ts.map +1 -1
  110. package/lib/types/utils/sdp.d.ts +6 -0
  111. package/lib/types/utils/sdp.d.ts.map +1 -1
  112. package/lib/types/utils/sip-logger.d.ts +4 -0
  113. package/lib/types/utils/sip-logger.d.ts.map +1 -0
  114. package/lib/types/utils/webrtc.d.ts +40 -0
  115. package/lib/types/utils/webrtc.d.ts.map +1 -1
  116. package/lib/types/voice/call.d.ts +110 -0
  117. package/lib/types/voice/call.d.ts.map +1 -0
  118. package/lib/types/voice/softphone.d.ts +77 -0
  119. package/lib/types/voice/softphone.d.ts.map +1 -0
  120. package/lib/types/web-rtc-client.d.ts +1 -36
  121. package/lib/types/web-rtc-client.d.ts.map +1 -1
  122. package/lib/utils/__tests__/api-requester.test.js +0 -17
  123. package/lib/utils/api-requester.js +1 -9
  124. package/lib/utils/sdp.js +27 -1
  125. package/lib/utils/sip-logger.js +37 -0
  126. package/lib/utils/webrtc.js +521 -1
  127. package/lib/voice/call.js +577 -0
  128. package/lib/voice/softphone.js +375 -0
  129. package/lib/web-rtc-client.js +89 -601
  130. package/package.json +3 -2
  131. package/src/__tests__/__voice__/call.test.ts +90 -0
  132. package/src/__tests__/__voice__/softphone.test.ts +82 -0
  133. package/src/api/auth.ts +2 -13
  134. package/src/checker/checks/webrtc.ts +2 -1
  135. package/src/domain/InvalidState.ts +13 -0
  136. package/src/domain/InvalidStateTransition.ts +17 -0
  137. package/src/domain/Phone/WebRTCPhone.ts +34 -36
  138. package/src/domain/types.ts +6 -1
  139. package/src/simple/Auth.ts +13 -6
  140. package/src/simple/Phone.ts +2 -1
  141. package/src/simple/__tests__/Auth.test.ts +87 -9
  142. package/src/simple/index.ts +9 -2
  143. package/src/simple/room/Room.ts +2 -1
  144. package/src/simple/room/SipRoom.ts +2 -1
  145. package/src/state-machine/call-state-machine.ts +131 -0
  146. package/src/state-machine/softphone-state-machine.ts +49 -0
  147. package/src/state-machine/utils.ts +53 -0
  148. package/src/utils/__tests__/api-requester.test.ts +0 -20
  149. package/src/utils/api-requester.ts +1 -9
  150. package/src/utils/sdp.ts +36 -0
  151. package/src/utils/sip-logger.ts +40 -0
  152. package/src/utils/webrtc.ts +571 -0
  153. package/src/voice/call.ts +713 -0
  154. package/src/voice/softphone.ts +485 -0
  155. package/src/web-rtc-client.ts +90 -690
@@ -0,0 +1,375 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ var __importDefault = (this && this.__importDefault) || function (mod) {
35
+ return (mod && mod.__esModule) ? mod : { "default": mod };
36
+ };
37
+ Object.defineProperty(exports, "__esModule", { value: true });
38
+ exports.Softphone = exports.MESSAGE_TYPE_SIGNAL = exports.MESSAGE_TYPE_CHAT = exports.EVENT_ON_SIGNAL = exports.EVENT_ON_CHAT = exports.EVENT_ON_MESSAGE = exports.EVENT_VIDEO_INPUT_CHANGE = exports.EVENT_DISCONNECTED = exports.EVENT_UNREGISTERED = exports.EVENT_REGISTERED = exports.EVENT_HEARTBEAT_TIMEOUT = exports.EVENT_HEARTBEAT = exports.EVENT_INCOMING = void 0;
39
+ /* eslint-disable no-underscore-dangle */
40
+ const events_1 = require("events");
41
+ const xstate_1 = require("xstate");
42
+ const InvalidState_1 = __importDefault(require("../domain/InvalidState"));
43
+ const __1 = __importDefault(require(".."));
44
+ const softphone_state_machine_1 = __importStar(require("../state-machine/softphone-state-machine"));
45
+ const call_state_machine_1 = require("../state-machine/call-state-machine");
46
+ const web_rtc_client_1 = __importDefault(require("../web-rtc-client"));
47
+ const IssueReporter_1 = __importDefault(require("../service/IssueReporter"));
48
+ const sip_logger_1 = __importDefault(require("../utils/sip-logger"));
49
+ const call_1 = __importDefault(require("./call"));
50
+ const utils_1 = require("../state-machine/utils");
51
+ exports.EVENT_INCOMING = 'incoming';
52
+ exports.EVENT_HEARTBEAT = 'heartbeat';
53
+ exports.EVENT_HEARTBEAT_TIMEOUT = 'heartbeatTimeout';
54
+ exports.EVENT_REGISTERED = 'registered';
55
+ exports.EVENT_UNREGISTERED = 'unregistered';
56
+ exports.EVENT_DISCONNECTED = 'disconnected';
57
+ exports.EVENT_VIDEO_INPUT_CHANGE = 'videoInputChange';
58
+ exports.EVENT_ON_MESSAGE = 'message';
59
+ exports.EVENT_ON_CHAT = 'phone/ON_CHAT';
60
+ exports.EVENT_ON_SIGNAL = 'phone/ON_SIGNAL';
61
+ exports.MESSAGE_TYPE_CHAT = 'message/TYPE_CHAT';
62
+ exports.MESSAGE_TYPE_SIGNAL = 'message/TYPE_SIGNAL';
63
+ const logger = IssueReporter_1.default.loggerFor('softphone');
64
+ class Softphone extends events_1.EventEmitter {
65
+ constructor() {
66
+ super();
67
+ this.calls = [];
68
+ this.softphoneActor = (0, xstate_1.createActor)(softphone_state_machine_1.default);
69
+ this.softphoneActor.start();
70
+ }
71
+ connect(options = {}) {
72
+ this.calls = [];
73
+ this.options = options;
74
+ this.audioOutputVolume = 1;
75
+ this.audioRingVolume = 1;
76
+ const server = __1.default.Auth.getHost();
77
+ const session = __1.default.Auth.getSession();
78
+ if (!server || !session) {
79
+ throw new Error('Please connect to the server using `Wazo.Auth.logIn` or `Wazo.Auth.authenticate` before using Room.connect().');
80
+ }
81
+ this.session = session;
82
+ this.sipLine = this.getPrimaryWebRtcLine();
83
+ if (!this.sipLine) {
84
+ throw new Error('Sorry, no sip lines found for this user');
85
+ }
86
+ this.options.userUuid = session.uuid || '';
87
+ return this.connectWithCredentials(server, this.sipLine, session.displayName(), options);
88
+ }
89
+ disconnect() {
90
+ return __awaiter(this, void 0, void 0, function* () {
91
+ (0, utils_1.assertCan)(this.softphoneActor, softphone_state_machine_1.Actions.UNREGISTER);
92
+ if (!this.client) {
93
+ return null;
94
+ }
95
+ // Terminate all calls
96
+ yield Promise.all(this.calls.map(call => call.hangup()));
97
+ return this.client.unregister();
98
+ });
99
+ }
100
+ getPrimaryWebRtcLine() {
101
+ var _a;
102
+ return ((_a = this.session) === null || _a === void 0 ? void 0 : _a.primaryWebRtcLine()) || null;
103
+ }
104
+ connectWithCredentials(server_1, sipLine_1, displayName_1) {
105
+ return __awaiter(this, arguments, void 0, function* (server, sipLine, displayName, rawOptions = {}) {
106
+ (0, utils_1.assertCan)(this.softphoneActor, softphone_state_machine_1.Actions.REGISTER);
107
+ this.softphoneActor.send({ type: softphone_state_machine_1.Actions.REGISTER });
108
+ const [host, port = 443] = server.split(':');
109
+ let options = Object.assign({}, rawOptions);
110
+ options.media = options.media || {
111
+ audio: true,
112
+ video: false,
113
+ };
114
+ options.uaConfigOverrides = options.uaConfigOverrides || {};
115
+ if (IssueReporter_1.default.enabled) {
116
+ options = (0, sip_logger_1.default)(options);
117
+ }
118
+ this.client = new web_rtc_client_1.default(Object.assign({ host, port: typeof port === 'string' ? parseInt(port, 10) : port, displayName, authorizationUser: sipLine.username, password: sipLine.secret, uri: `${sipLine.username}@${server}` }, options), undefined, options.uaConfigOverrides);
119
+ this._bindEvents();
120
+ return this.client.register().catch(error => {
121
+ // Avoid exception on `t.server.scheme` in sip transport when losing the webrtc socket connection
122
+ logger.error('softphone - register error', { message: error.message, stack: error.stack });
123
+ this._sendAction(softphone_state_machine_1.Actions.UNREGISTER);
124
+ });
125
+ });
126
+ }
127
+ reconnect() {
128
+ logger.info('softphone - reconnect', { client: !!this.client });
129
+ if (!this.client) {
130
+ return;
131
+ }
132
+ this.client.attemptReconnection();
133
+ }
134
+ call(options) {
135
+ return __awaiter(this, void 0, void 0, function* () {
136
+ logger.info('softphone - call', options);
137
+ if (!options.params.To) {
138
+ logger.warn('softphone - calling with empty number, bailing.');
139
+ return Promise.resolve(null);
140
+ }
141
+ if (this.getState() !== softphone_state_machine_1.States.UNREGISTERED) {
142
+ throw new InvalidState_1.default(`Invalid state ${softphone_state_machine_1.States.UNREGISTERED}`, softphone_state_machine_1.States.UNREGISTERED);
143
+ }
144
+ if (this.getState() !== softphone_state_machine_1.States.REGISTERING) {
145
+ logger.info('softphone - calling when registering, waiting for Softphone to be registered ...');
146
+ yield (0, utils_1.waitUntilState)(this.softphoneActor, softphone_state_machine_1.States.REGISTERED);
147
+ }
148
+ // Hold other calls
149
+ this.holdAllCalls();
150
+ let sipCall;
151
+ try {
152
+ sipCall = this.client.call(options.params.To, options.withCamera, options.audioOnly, options.conference, options);
153
+ const call = new call_1.default(sipCall, this);
154
+ return call;
155
+ }
156
+ catch (error) {
157
+ logger.warn('softphone - call, error', { message: error.message, stack: error.stack });
158
+ return Promise.resolve(null);
159
+ }
160
+ });
161
+ }
162
+ removeCall(callToRemove) {
163
+ this.calls = this.calls.filter((call) => call.id !== callToRemove.id);
164
+ }
165
+ holdAllCalls(exceptCall = null) {
166
+ this.calls.filter(call => !call.isHeld() && call.id === (exceptCall === null || exceptCall === void 0 ? void 0 : exceptCall.id)).forEach(call => call.hold());
167
+ }
168
+ startHeartbeat() {
169
+ logger.info('softphone - start heartbeat', { client: !!this.client, hasHeartbeat: this.client.hasHeartbeat() });
170
+ if (!this.client || this.client.hasHeartbeat()) {
171
+ return;
172
+ }
173
+ this.client.startHeartbeat();
174
+ }
175
+ stopHeartbeat() {
176
+ logger.info('softphone - stop heartbeat', { client: !!this.client });
177
+ if (!this.client) {
178
+ return;
179
+ }
180
+ this.client.stopHeartbeat();
181
+ }
182
+ setMediaConstraints(media) {
183
+ this.client.setMediaConstraints(media);
184
+ }
185
+ changeAudioOutputDevice(id) {
186
+ logger.info('softphone - change audio device', { deviceId: id });
187
+ this.audioOutputDeviceId = id;
188
+ return this.client.changeAudioOutputDevice(id);
189
+ }
190
+ changeAudioInputDevice(id, force) {
191
+ logger.info('softphone - changeAudio input device', { deviceId: id });
192
+ const currentCall = this.getCurrentCall();
193
+ return this.client.changeAudioInputDevice(id, currentCall === null || currentCall === void 0 ? void 0 : currentCall.sipCall, force);
194
+ }
195
+ changeVideoInputDevice(id) {
196
+ logger.info('softphone - change video input device', { deviceId: id });
197
+ const currentCall = this.getCurrentCall();
198
+ return this.client.changeVideoInputDevice(id, currentCall === null || currentCall === void 0 ? void 0 : currentCall.sipCall);
199
+ }
200
+ changeRingDevice(id) {
201
+ logger.info('softphone - changing ring device', { id });
202
+ this.audioRingDeviceId = id;
203
+ }
204
+ hasSfu() {
205
+ var _a;
206
+ return ((_a = this.sipLine) === null || _a === void 0 ? void 0 : _a.hasVideoConference()) || false;
207
+ }
208
+ // volume is a value between 0 and 1
209
+ changeAudioVolume(volume) {
210
+ logger.info('softphone - changing audio volume', { volume });
211
+ this.audioOutputVolume = volume;
212
+ this.client.changeAudioOutputVolume(volume);
213
+ }
214
+ // volume is a value between 0 and 1
215
+ changeRingVolume(volume) {
216
+ logger.info('softphone - changing ring volume', { volume });
217
+ this.audioRingVolume = volume;
218
+ }
219
+ getCalls() {
220
+ return this.calls;
221
+ }
222
+ getCurrentCall() {
223
+ return this.calls.find(call => call.isEstablished());
224
+ }
225
+ getState() {
226
+ return this.softphoneActor.getSnapshot().value;
227
+ }
228
+ getUserAgent() {
229
+ var _a, _b;
230
+ return ((_b = (_a = this.client) === null || _a === void 0 ? void 0 : _a.config) === null || _b === void 0 ? void 0 : _b.userAgentString) || 'softphone';
231
+ }
232
+ _bindEvents() {
233
+ this.client.on(this.client.INVITE, (sipSession) => {
234
+ const call = new call_1.default(sipSession, this);
235
+ call._sendAction(call_state_machine_1.Actions.INCOMING_CALL);
236
+ this.calls.push(call);
237
+ this.eventEmitter.emit(exports.EVENT_INCOMING, call);
238
+ });
239
+ this.client.on(this.client.ON_REINVITE, (sipCall, request, updatedCalleeName, updatedNumber, hadRemoteVideo) => {
240
+ logger.info('softphone - on reinvite', {
241
+ callId: sipCall.id,
242
+ inviteId: request.callId,
243
+ updatedCalleeName,
244
+ updatedNumber,
245
+ hadRemoteVideo,
246
+ });
247
+ this._onCallEvent(sipCall, 'onReinvite', request, updatedCalleeName, updatedNumber, hadRemoteVideo);
248
+ });
249
+ this.client.on(this.client.ACCEPTED, (sipCall) => __awaiter(this, void 0, void 0, function* () {
250
+ this._onCallEvent(sipCall, 'onAccepted');
251
+ if (this.audioOutputDeviceId) {
252
+ yield this.client.changeAudioOutputDevice(this.audioOutputDeviceId);
253
+ }
254
+ }));
255
+ this.client.on(this.client.ON_ERROR, (e, sipCall) => {
256
+ this._onCallEvent(sipCall, 'onError', e);
257
+ });
258
+ this.client.on(this.client.REJECTED, (sipCall) => {
259
+ this._onCallEvent(sipCall, 'onRejected');
260
+ });
261
+ this.client.on(this.client.ON_PROGRESS, (sipCall) => {
262
+ this._onCallEvent(sipCall, 'onProgress');
263
+ });
264
+ this.client.on(this.client.ON_EARLY_MEDIA, (sipCall) => {
265
+ this._onCallEvent(sipCall, 'onEarlyMedia');
266
+ });
267
+ this.client.on(this.client.ON_TRACK, (sipCall, event) => {
268
+ this._onCallEvent(sipCall, 'onTrack', event);
269
+ });
270
+ this.client.on(this.client.ON_NETWORK_STATS, (sipCall, stats, previousStats) => {
271
+ this._onCallEvent(sipCall, 'onNetworkStats', stats, previousStats);
272
+ });
273
+ // Used when upgrading directly in screenshare mode
274
+ this.client.on(this.client.ON_SCREEN_SHARING_REINVITE, (sipCall, response, desktop) => {
275
+ this._onCallEvent(sipCall, 'onScreenshareReinvite', response, desktop);
276
+ });
277
+ this.client.on(this.client.UNREGISTERED, () => {
278
+ logger.info('softphone - unregistered');
279
+ this.eventEmitter.emit(exports.EVENT_UNREGISTERED);
280
+ this._sendAction(softphone_state_machine_1.Actions.UNREGISTERED);
281
+ });
282
+ this.client.on(this.client.ON_DISCONNECTED, () => {
283
+ logger.info('softphone - disconnected');
284
+ this.eventEmitter.emit(exports.EVENT_DISCONNECTED);
285
+ this._sendAction(softphone_state_machine_1.Actions.TRANSPORT_CLOSED);
286
+ });
287
+ this.client.on(this.client.REGISTERED, () => {
288
+ logger.info('softphone - registered', { state: (0, utils_1.getState)(this.softphoneActor) });
289
+ if (!(0, utils_1.can)(this.softphoneActor, softphone_state_machine_1.Actions.REGISTER_DONE)) {
290
+ logger.warn('softphone - registered but not in REGISTERING state', { state: (0, utils_1.getState)(this.softphoneActor) });
291
+ return;
292
+ }
293
+ this.stopHeartbeat();
294
+ this._sendAction(softphone_state_machine_1.Actions.REGISTER_DONE);
295
+ this.eventEmitter.emit(exports.EVENT_REGISTERED);
296
+ // If the phone registered with a current callSession (eg: when switching network):
297
+ // send a reinvite to renegociate ICE with new IP
298
+ if (this.shouldSendReinvite) {
299
+ this.shouldSendReinvite = false;
300
+ // Send reinvite for all active calls
301
+ this.calls.filter(call => call.isEstablished())
302
+ .forEach(call => { call.sendReinvite({ conference: call.isConference(), iceRestart: true }); });
303
+ }
304
+ });
305
+ this.client.on(this.client.CONNECTED, () => {
306
+ logger.info('softphone - connected');
307
+ this.stopHeartbeat();
308
+ });
309
+ this.client.on(this.client.DISCONNECTED, () => {
310
+ logger.info('softphone - disconnected');
311
+ this.eventEmitter.emit(exports.EVENT_DISCONNECTED);
312
+ // Do not trigger heartbeat if already running
313
+ if (!this.client.hasHeartbeat()) {
314
+ this.startHeartbeat();
315
+ }
316
+ // Tell to send reinvite when reconnecting
317
+ this.shouldSendReinvite = this.calls.length > 0;
318
+ });
319
+ this.client.on('onVideoInputChange', stream => {
320
+ this.eventEmitter.emit(exports.EVENT_VIDEO_INPUT_CHANGE, stream);
321
+ });
322
+ this.client.on(this.client.MESSAGE, (message) => {
323
+ this._onMessage(message);
324
+ this.eventEmitter.emit(exports.EVENT_ON_MESSAGE, message);
325
+ });
326
+ this.client.setOnHeartbeatTimeout(() => {
327
+ this.eventEmitter.emit(exports.EVENT_HEARTBEAT_TIMEOUT);
328
+ });
329
+ this.client.setOnHeartbeatCallback(() => {
330
+ this.eventEmitter.emit(exports.EVENT_HEARTBEAT);
331
+ });
332
+ }
333
+ _sendAction(action) {
334
+ this.softphoneActor.send({ type: action });
335
+ }
336
+ _getCall(sipCall) {
337
+ return this.calls.find(call => call.id === sipCall.id);
338
+ }
339
+ _onCallEvent(sipCall, eventName, ...args) {
340
+ logger.warn('softphone - received call event', { eventName, sipId: sipCall.id });
341
+ const call = this._getCall(sipCall);
342
+ if (!call) {
343
+ logger.warn('softphone - no call found to send the event', { eventName, sipId: sipCall.id });
344
+ return;
345
+ }
346
+ // @ts-ignore
347
+ call[eventName](...args);
348
+ }
349
+ _onMessage(message) {
350
+ if (!message || message.method !== 'MESSAGE') {
351
+ return;
352
+ }
353
+ let body;
354
+ try {
355
+ body = JSON.parse(message.body || '');
356
+ }
357
+ catch (e) {
358
+ return;
359
+ }
360
+ const { type, content, } = body;
361
+ switch (type) {
362
+ case exports.MESSAGE_TYPE_CHAT:
363
+ this.eventEmitter.emit(exports.EVENT_ON_CHAT, content);
364
+ break;
365
+ case exports.MESSAGE_TYPE_SIGNAL:
366
+ {
367
+ this.eventEmitter.emit(exports.EVENT_ON_SIGNAL, content);
368
+ break;
369
+ }
370
+ default:
371
+ }
372
+ }
373
+ }
374
+ exports.Softphone = Softphone;
375
+ exports.default = new Softphone();