@wazo/sdk 1.0.0-alpha.1 → 1.0.0-alpha.2

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 (52) hide show
  1. package/dist/wazo-sdk.js +6 -6
  2. package/esm/__tests__/__voice__/call.test.js +44 -2
  3. package/esm/domain/Phone/WebRTCPhone.js +6 -7
  4. package/esm/simple/Phone.js +1 -0
  5. package/esm/state-machine/call-state-machine.js +1 -1
  6. package/esm/types/domain/Phone/WebRTCPhone.d.ts.map +1 -1
  7. package/esm/types/simple/Phone.d.ts.map +1 -1
  8. package/esm/types/state-machine/utils.d.ts +1 -1
  9. package/esm/types/state-machine/utils.d.ts.map +1 -1
  10. package/esm/types/utils/sdp.d.ts +2 -0
  11. package/esm/types/utils/sdp.d.ts.map +1 -1
  12. package/esm/types/utils/webrtc.d.ts +1 -0
  13. package/esm/types/utils/webrtc.d.ts.map +1 -1
  14. package/esm/types/voice/call.d.ts +19 -11
  15. package/esm/types/voice/call.d.ts.map +1 -1
  16. package/esm/types/voice/softphone.d.ts +18 -13
  17. package/esm/types/voice/softphone.d.ts.map +1 -1
  18. package/esm/utils/sdp.js +13 -0
  19. package/esm/utils/webrtc.js +20 -0
  20. package/esm/voice/call.js +90 -30
  21. package/esm/voice/softphone.js +94 -23
  22. package/lib/__tests__/__voice__/call.test.js +43 -1
  23. package/lib/domain/Phone/WebRTCPhone.js +5 -6
  24. package/lib/simple/Phone.js +1 -0
  25. package/lib/state-machine/call-state-machine.js +1 -1
  26. package/lib/types/domain/Phone/WebRTCPhone.d.ts.map +1 -1
  27. package/lib/types/simple/Phone.d.ts.map +1 -1
  28. package/lib/types/state-machine/utils.d.ts +1 -1
  29. package/lib/types/state-machine/utils.d.ts.map +1 -1
  30. package/lib/types/utils/sdp.d.ts +2 -0
  31. package/lib/types/utils/sdp.d.ts.map +1 -1
  32. package/lib/types/utils/webrtc.d.ts +1 -0
  33. package/lib/types/utils/webrtc.d.ts.map +1 -1
  34. package/lib/types/voice/call.d.ts +19 -11
  35. package/lib/types/voice/call.d.ts.map +1 -1
  36. package/lib/types/voice/softphone.d.ts +18 -13
  37. package/lib/types/voice/softphone.d.ts.map +1 -1
  38. package/lib/utils/sdp.js +16 -1
  39. package/lib/utils/webrtc.js +22 -1
  40. package/lib/voice/call.js +87 -28
  41. package/lib/voice/softphone.js +95 -23
  42. package/package.json +1 -1
  43. package/src/__tests__/__voice__/call.test.ts +47 -2
  44. package/src/domain/Phone/WebRTCPhone.ts +9 -9
  45. package/src/simple/Phone.ts +1 -0
  46. package/src/state-machine/call-state-machine.ts +1 -1
  47. package/src/state-machine/utils.ts +2 -2
  48. package/src/utils/sdp.ts +16 -0
  49. package/src/utils/webrtc.ts +23 -1
  50. package/src/voice/call.ts +118 -40
  51. package/src/voice/softphone.ts +116 -30
  52. package/src/web-rtc-client.ts +1 -1
@@ -10,7 +10,6 @@ import InvalidState from '../domain/InvalidState';
10
10
 
11
11
  import Wazo from '..';
12
12
  import softphoneStateMachine, { type SoftphoneActorRef, Actions, type StateTypes, States, ActionTypes } from '../state-machine/softphone-state-machine';
13
- import { Actions as CallActions } from '../state-machine/call-state-machine';
14
13
  import { WebRtcConfig, SipCall } from '../domain/types';
15
14
  import WazoWebRTCClient from '../web-rtc-client';
16
15
  import IssueReporter from '../service/IssueReporter';
@@ -19,12 +18,17 @@ import Call from './call';
19
18
  import { assertCan, can, getState, waitUntilState } from '../state-machine/utils';
20
19
 
21
20
  export const EVENT_INCOMING = 'incoming';
21
+ export const EVENT_OUTGOING = 'outgoing';
22
22
  export const EVENT_HEARTBEAT = 'heartbeat';
23
23
  export const EVENT_HEARTBEAT_TIMEOUT = 'heartbeatTimeout';
24
24
  export const EVENT_REGISTERED = 'registered';
25
25
  export const EVENT_UNREGISTERED = 'unregistered';
26
26
  export const EVENT_DISCONNECTED = 'disconnected';
27
27
  export const EVENT_VIDEO_INPUT_CHANGE = 'videoInputChange';
28
+ export const EVENT_TERMINATE_SOUND = 'terminateSound';
29
+ export const EVENT_PLAY_HANGUP_SOUND = 'playHangupSound';
30
+ export const EVENT_PLAY_RING_SOUND = 'playRingSound';
31
+ export const EVENT_PLAY_INBOUND_CALL_SOUND = 'playInboundCallSound';
28
32
  export const EVENT_ON_MESSAGE = 'message';
29
33
  export const EVENT_ON_CHAT = 'phone/ON_CHAT';
30
34
  export const EVENT_ON_SIGNAL = 'phone/ON_SIGNAL';
@@ -53,8 +57,6 @@ export class Softphone extends EventEmitter {
53
57
 
54
58
  audioRingVolume: number;
55
59
 
56
- eventEmitter: EventEmitter;
57
-
58
60
  calls: Call[];
59
61
 
60
62
  client: WazoWebRTCClient;
@@ -69,6 +71,8 @@ export class Softphone extends EventEmitter {
69
71
 
70
72
  shouldSendReinvite: boolean;
71
73
 
74
+ ringingEnabled = true;
75
+
72
76
  constructor() {
73
77
  super();
74
78
 
@@ -179,16 +183,16 @@ export class Softphone extends EventEmitter {
179
183
  return Promise.resolve(null);
180
184
  }
181
185
 
182
- if (this.getState() !== States.UNREGISTERED) {
183
- throw new InvalidState(`Invalid state ${States.UNREGISTERED}`, States.UNREGISTERED);
184
- }
185
-
186
- if (this.getState() !== States.REGISTERING) {
186
+ if (this.state === States.REGISTERING) {
187
187
  logger.info('softphone - calling when registering, waiting for Softphone to be registered ...');
188
188
 
189
189
  await waitUntilState(this.softphoneActor, States.REGISTERED);
190
190
  }
191
191
 
192
+ if (this.state !== States.UNREGISTERED) {
193
+ throw new InvalidState(`Invalid state ${States.UNREGISTERED}`, States.UNREGISTERED);
194
+ }
195
+
192
196
  // Hold other calls
193
197
  this.holdAllCalls();
194
198
 
@@ -198,6 +202,8 @@ export class Softphone extends EventEmitter {
198
202
  sipCall = this.client.call(options.params.To, options.withCamera, options.audioOnly, options.conference, options) as SipCall;
199
203
  const call = new Call(sipCall, this);
200
204
 
205
+ this.emit(EVENT_OUTGOING, call);
206
+
201
207
  return call;
202
208
  } catch (error: any) {
203
209
  logger.warn('softphone - call, error', { message: error.message, stack: error.stack });
@@ -209,6 +215,69 @@ export class Softphone extends EventEmitter {
209
215
  this.calls = this.calls.filter((call: Call) => call.id !== callToRemove.id);
210
216
  }
211
217
 
218
+ onCallTerminated(call: Call) {
219
+ logger.info('softphone - on call terminated', { id: call.id });
220
+
221
+ const currentCall = this.getCurrentCall();
222
+ const isCurrentCall = currentCall?.id === call.id;
223
+ const isRinging = call.isRinging();
224
+ const isCurrentIncomingCall = isCurrentCall && isRinging;
225
+ const incomingCalls = this.getIncomingCalls();
226
+ const nbIncomingCalls = incomingCalls.length;
227
+
228
+ // Send an event to terminate ringing sound
229
+ if (isCurrentIncomingCall) {
230
+ setTimeout(() => {
231
+ // Avoid race condition when the other is calling and hanging up immediately
232
+ this.emit(EVENT_TERMINATE_SOUND, call, 'call terminated');
233
+ }, 5);
234
+ }
235
+
236
+ // If the terminated call was an incoming call, we have to re-trigger if it's was the first incoming call
237
+ // Otherwise, we have to re-trigger an incoming call event if another call is incoming
238
+ const shouldRetrigger = isCurrentCall ? nbIncomingCalls : isCurrentIncomingCall;
239
+
240
+ // Re-trigger incoming call event for remaining incoming calls
241
+ logger.info('Softphone - check to re-trigger incoming call', { shouldRetrigger, nbIncomingCalls });
242
+
243
+ if (shouldRetrigger && nbIncomingCalls > 1) {
244
+ const nextCall = incomingCalls.find(c => c.id !== call.id);
245
+ // Avoid race condition
246
+ setTimeout(() => {
247
+ if (this.ringingEnabled) {
248
+ if (currentCall) {
249
+ this.emit(EVENT_PLAY_RING_SOUND, this.audioRingDeviceId, this.audioRingVolume, nextCall);
250
+ } else {
251
+ this.emit(EVENT_PLAY_INBOUND_CALL_SOUND, this.audioOutputDeviceId, this.audioOutputVolume, nextCall);
252
+ }
253
+ }
254
+
255
+ this.emit(EVENT_INCOMING, nextCall);
256
+ }, 100);
257
+ }
258
+
259
+ // @ts-ignore: does not exist
260
+ if (!call.sipCall.isCanceled) {
261
+ setTimeout(() => {
262
+ this.emit(EVENT_PLAY_HANGUP_SOUND, this.audioOutputDeviceId, this.audioOutputVolume, call);
263
+ }, 10);
264
+ }
265
+
266
+ this.client.onCallEnded(call.sipCall);
267
+
268
+ this.removeCall(call);
269
+ }
270
+
271
+ enableRinging(): Promise<void> | void {
272
+ logger.info('softphone - enable ringing');
273
+ this.ringingEnabled = true;
274
+ }
275
+
276
+ disableRinging(): Promise<void> | void {
277
+ logger.info('softphone - disable ringing');
278
+ this.ringingEnabled = false;
279
+ }
280
+
212
281
  holdAllCalls(exceptCall: Call | null = null) {
213
282
  this.calls.filter(call => !call.isHeld() && call.id === exceptCall?.id).forEach(call => call.hold());
214
283
  }
@@ -278,29 +347,43 @@ export class Softphone extends EventEmitter {
278
347
  this.audioRingVolume = volume;
279
348
  }
280
349
 
281
- getCalls(): Call[] {
282
- return this.calls;
283
- }
284
-
285
350
  getCurrentCall(): Call | undefined {
286
351
  return this.calls.find(call => call.isEstablished());
287
352
  }
288
353
 
289
- getState(): StateTypes {
290
- return this.softphoneActor.getSnapshot().value;
354
+ getIncomingCalls(): Call[] {
355
+ return this.calls.filter(call => call.isRinging());
356
+ }
357
+
358
+ get state(): StateTypes {
359
+ return getState(this.softphoneActor) as StateTypes;
291
360
  }
292
361
 
293
362
  getUserAgent() {
294
363
  return this.client?.config?.userAgentString || 'softphone';
295
364
  }
296
365
 
366
+ isRegistered() {
367
+ return this.state === States.REGISTERED;
368
+ }
369
+
297
370
  _bindEvents(): void {
298
371
  this.client.on(this.client.INVITE, (sipSession: SipCall) => {
299
372
  const call = new Call(sipSession, this);
300
- call._sendAction(CallActions.INCOMING_CALL);
373
+ logger.info('softphone - on invite', { callId: call.id, number: call.number });
374
+
375
+ call.onCallIncoming();
301
376
  this.calls.push(call);
302
377
 
303
- this.eventEmitter.emit(EVENT_INCOMING, call);
378
+ this.emit(EVENT_INCOMING, call);
379
+
380
+ if (!this.getCurrentCall()) {
381
+ if (this.ringingEnabled) {
382
+ this.emit(EVENT_PLAY_RING_SOUND, this.audioRingDeviceId, this.audioRingVolume, call);
383
+ }
384
+ } else {
385
+ this.emit(EVENT_PLAY_INBOUND_CALL_SOUND, this.audioOutputDeviceId, this.audioOutputVolume, call);
386
+ }
304
387
  });
305
388
 
306
389
  this.client.on(this.client.ON_REINVITE, (sipCall: SipCall, request: IncomingRequestMessage, updatedCalleeName: string, updatedNumber: string, hadRemoteVideo: boolean) => {
@@ -316,6 +399,9 @@ export class Softphone extends EventEmitter {
316
399
  });
317
400
 
318
401
  this.client.on(this.client.ACCEPTED, async (sipCall: SipCall) => {
402
+ const call = this._getCall(sipCall);
403
+ this.emit(EVENT_TERMINATE_SOUND, call, 'call accepted');
404
+
319
405
  this._onCallEvent(sipCall, 'onAccepted');
320
406
 
321
407
  if (this.audioOutputDeviceId) {
@@ -354,14 +440,14 @@ export class Softphone extends EventEmitter {
354
440
 
355
441
  this.client.on(this.client.UNREGISTERED, () => {
356
442
  logger.info('softphone - unregistered');
357
- this.eventEmitter.emit(EVENT_UNREGISTERED);
443
+ this.emit(EVENT_UNREGISTERED);
358
444
 
359
445
  this._sendAction(Actions.UNREGISTERED);
360
446
  });
361
447
 
362
448
  this.client.on(this.client.ON_DISCONNECTED, () => {
363
449
  logger.info('softphone - disconnected');
364
- this.eventEmitter.emit(EVENT_DISCONNECTED);
450
+ this.emit(EVENT_DISCONNECTED);
365
451
 
366
452
  this._sendAction(Actions.TRANSPORT_CLOSED);
367
453
  });
@@ -376,7 +462,7 @@ export class Softphone extends EventEmitter {
376
462
  this.stopHeartbeat();
377
463
  this._sendAction(Actions.REGISTER_DONE);
378
464
 
379
- this.eventEmitter.emit(EVENT_REGISTERED);
465
+ this.emit(EVENT_REGISTERED);
380
466
 
381
467
  // If the phone registered with a current callSession (eg: when switching network):
382
468
  // send a reinvite to renegociate ICE with new IP
@@ -396,7 +482,7 @@ export class Softphone extends EventEmitter {
396
482
 
397
483
  this.client.on(this.client.DISCONNECTED, () => {
398
484
  logger.info('softphone - disconnected');
399
- this.eventEmitter.emit(EVENT_DISCONNECTED);
485
+ this.emit(EVENT_DISCONNECTED);
400
486
 
401
487
  // Do not trigger heartbeat if already running
402
488
  if (!this.client.hasHeartbeat()) {
@@ -408,33 +494,33 @@ export class Softphone extends EventEmitter {
408
494
  });
409
495
 
410
496
  this.client.on('onVideoInputChange', stream => {
411
- this.eventEmitter.emit(EVENT_VIDEO_INPUT_CHANGE, stream);
497
+ this.emit(EVENT_VIDEO_INPUT_CHANGE, stream);
412
498
  });
413
499
 
414
500
  this.client.on(this.client.MESSAGE, (message: Message) => {
415
501
  this._onMessage(message);
416
502
 
417
- this.eventEmitter.emit(EVENT_ON_MESSAGE, message);
503
+ this.emit(EVENT_ON_MESSAGE, message);
418
504
  });
419
505
 
420
506
  this.client.setOnHeartbeatTimeout(() => {
421
- this.eventEmitter.emit(EVENT_HEARTBEAT_TIMEOUT);
507
+ this.emit(EVENT_HEARTBEAT_TIMEOUT);
422
508
  });
423
509
 
424
510
  this.client.setOnHeartbeatCallback(() => {
425
- this.eventEmitter.emit(EVENT_HEARTBEAT);
511
+ this.emit(EVENT_HEARTBEAT);
426
512
  });
427
513
  }
428
514
 
429
- _sendAction(action: ActionTypes) {
515
+ private _sendAction(action: ActionTypes) {
430
516
  this.softphoneActor.send({ type: action });
431
517
  }
432
518
 
433
- _getCall(sipCall: SipCall): Call | undefined {
519
+ private _getCall(sipCall: SipCall): Call | undefined {
434
520
  return this.calls.find(call => call.id === sipCall.id);
435
521
  }
436
522
 
437
- _onCallEvent(sipCall: SipCall, eventName: string, ...args: any[]) {
523
+ private _onCallEvent(sipCall: SipCall, eventName: string, ...args: any[]) {
438
524
  logger.warn('softphone - received call event', { eventName, sipId: sipCall.id });
439
525
 
440
526
  const call = this._getCall(sipCall);
@@ -447,7 +533,7 @@ export class Softphone extends EventEmitter {
447
533
  call[eventName](...args);
448
534
  }
449
535
 
450
- _onMessage(message: Message & { method?: string, body?: string }): void {
536
+ private _onMessage(message: Message & { method?: string, body?: string }): void {
451
537
  if (!message || message.method !== 'MESSAGE') {
452
538
  return;
453
539
  }
@@ -467,12 +553,12 @@ export class Softphone extends EventEmitter {
467
553
 
468
554
  switch (type) {
469
555
  case MESSAGE_TYPE_CHAT:
470
- this.eventEmitter.emit(EVENT_ON_CHAT, content);
556
+ this.emit(EVENT_ON_CHAT, content);
471
557
  break;
472
558
 
473
559
  case MESSAGE_TYPE_SIGNAL:
474
560
  {
475
- this.eventEmitter.emit(EVENT_ON_SIGNAL, content);
561
+ this.emit(EVENT_ON_SIGNAL, content);
476
562
  break;
477
563
  }
478
564
 
@@ -31,7 +31,7 @@ import IssueReporter from './service/IssueReporter';
31
31
  import Heartbeat from './utils/Heartbeat';
32
32
  import { getSipSessionId, makeURI, replaceLocalIpModifier, SIP_ID_LENGTH } from './utils/sdp';
33
33
  import type { MediaConfig, UserAgentConfigOverrides, WebRtcConfig, IncomingResponse, PeerConnection, WazoSession, WazoTransport } from './domain/types';
34
- import { cleanupStream, createUaOptions, fetchNetworkStats, getAudioConstraints, getLocalStream, getLocalTracks, getLocalVideoStream, getMediaConfiguration, getPeerConnection, getRemoteStream, getRemoteTracks, getRemoteVideoStream, getRemoteVideoStreamFromPc, getStreamFromConstraints, getVideoConstraints, hasALocalVideoTrack, hasARemoteVideoTrack, hasAVideoTrack, hasLocalVideo, hasRemoteVideo, hasVideoTrack, isAudioOnly, isVideoRemotelyHeld, isWeb, sessionWantsToDoVideo, setLocalMediaStream, toggleAudio, toggleVideo, updateRemoteStream } from './utils/webrtc';
34
+ import { cleanupStream, createUaOptions, fetchNetworkStats, getAudioConstraints, getLocalStream, getLocalTracks, getLocalVideoStream, getMediaConfiguration, getPeerConnection, getRemoteStream, getRemoteTracks, getRemoteVideoStream, getRemoteVideoStreamFromPc, getVideoConstraints, hasALocalVideoTrack, hasARemoteVideoTrack, hasAVideoTrack, hasLocalVideo, hasRemoteVideo, hasVideoTrack, isAudioOnly, isVideoRemotelyHeld, isWeb, sessionWantsToDoVideo, setLocalMediaStream, toggleAudio, toggleVideo, updateRemoteStream } from './utils/webrtc';
35
35
 
36
36
  const SEND_STATS_DELAY = 5000;
37
37
  const states = ['STATUS_NULL', 'STATUS_NEW', 'STATUS_CONNECTING', 'STATUS_CONNECTED', 'STATUS_COMPLETED'];