@telnyx/react-voice-commons-sdk 0.1.2 → 0.1.4
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.
- package/CHANGELOG.md +60 -0
- package/README.md +469 -483
- package/ios/CallKitBridge.swift +2 -7
- package/lib/callkit/callkit-coordinator.d.ts +110 -117
- package/lib/callkit/callkit-coordinator.js +664 -727
- package/lib/callkit/callkit.d.ts +41 -41
- package/lib/callkit/callkit.js +252 -242
- package/lib/callkit/index.js +15 -47
- package/lib/callkit/use-callkit.d.ts +19 -19
- package/lib/callkit/use-callkit.js +270 -310
- package/lib/context/TelnyxVoiceContext.d.ts +9 -9
- package/lib/context/TelnyxVoiceContext.js +10 -13
- package/lib/hooks/use-callkit-coordinator.d.ts +9 -17
- package/lib/hooks/use-callkit-coordinator.js +45 -50
- package/lib/hooks/useAppReadyNotifier.js +13 -15
- package/lib/hooks/useAppStateHandler.d.ts +6 -11
- package/lib/hooks/useAppStateHandler.js +95 -110
- package/lib/hooks/useNetworkStateHandler.d.ts +0 -0
- package/lib/hooks/useNetworkStateHandler.js +0 -0
- package/lib/index.d.ts +3 -21
- package/lib/index.js +50 -201
- package/lib/internal/CallKitHandler.d.ts +6 -6
- package/lib/internal/CallKitHandler.js +96 -104
- package/lib/internal/callkit-manager.d.ts +57 -57
- package/lib/internal/callkit-manager.js +299 -316
- package/lib/internal/calls/call-state-controller.d.ts +73 -86
- package/lib/internal/calls/call-state-controller.js +263 -307
- package/lib/internal/session/session-manager.d.ts +71 -75
- package/lib/internal/session/session-manager.js +360 -424
- package/lib/internal/user-defaults-helpers.js +49 -39
- package/lib/internal/voice-pn-bridge.d.ts +114 -12
- package/lib/internal/voice-pn-bridge.js +212 -5
- package/lib/models/call-state.d.ts +46 -44
- package/lib/models/call-state.js +70 -68
- package/lib/models/call.d.ts +161 -133
- package/lib/models/call.js +454 -382
- package/lib/models/config.d.ts +11 -18
- package/lib/models/config.js +37 -35
- package/lib/models/connection-state.d.ts +10 -10
- package/lib/models/connection-state.js +16 -16
- package/lib/telnyx-voice-app.d.ts +28 -28
- package/lib/telnyx-voice-app.js +513 -480
- package/lib/telnyx-voip-client.d.ts +167 -167
- package/lib/telnyx-voip-client.js +385 -390
- package/package.json +115 -104
- package/src/callkit/callkit-coordinator.ts +830 -846
- package/src/hooks/useNetworkStateHandler.ts +0 -0
- package/src/internal/calls/call-state-controller.ts +407 -384
- package/src/internal/session/session-manager.ts +483 -467
- package/src/internal/voice-pn-bridge.ts +266 -18
- package/src/models/call-state.ts +105 -98
- package/src/models/call.ts +502 -388
- package/src/telnyx-voice-app.tsx +788 -690
- package/src/telnyx-voip-client.ts +551 -539
- package/src/types/telnyx-sdk.d.ts +93 -79
package/src/models/call.ts
CHANGED
|
@@ -1,388 +1,502 @@
|
|
|
1
|
-
import { BehaviorSubject, Observable } from 'rxjs';
|
|
2
|
-
import { distinctUntilChanged, map } from 'rxjs/operators';
|
|
3
|
-
import { TelnyxCallState, CallStateHelpers } from './call-state';
|
|
4
|
-
import { Call as TelnyxCall } from '@telnyx/react-native-voice-sdk';
|
|
5
|
-
import { Platform } from 'react-native';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Represents a call with reactive state streams.
|
|
9
|
-
*
|
|
10
|
-
* This class wraps the underlying Telnyx Call object and provides
|
|
11
|
-
* reactive streams for all call state changes, making it easy to
|
|
12
|
-
* integrate with any state management solution.
|
|
13
|
-
*/
|
|
14
|
-
export class Call {
|
|
15
|
-
private readonly _callState = new BehaviorSubject<TelnyxCallState>(TelnyxCallState.RINGING);
|
|
16
|
-
private readonly _isMuted = new BehaviorSubject<boolean>(false);
|
|
17
|
-
private readonly _isHeld = new BehaviorSubject<boolean>(false);
|
|
18
|
-
private readonly _duration = new BehaviorSubject<number>(0);
|
|
19
|
-
|
|
20
|
-
private _durationTimer?: NodeJS.Timeout;
|
|
21
|
-
private _startTime?: Date;
|
|
22
|
-
|
|
23
|
-
constructor(
|
|
24
|
-
private readonly _telnyxCall: TelnyxCall,
|
|
25
|
-
private readonly _callId: string,
|
|
26
|
-
private readonly _destination: string,
|
|
27
|
-
private readonly _isIncoming: boolean
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
);
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* Observable
|
|
146
|
-
*/
|
|
147
|
-
get
|
|
148
|
-
return this.
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
throw
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
}
|
|
1
|
+
import { BehaviorSubject, Observable } from 'rxjs';
|
|
2
|
+
import { distinctUntilChanged, map } from 'rxjs/operators';
|
|
3
|
+
import { TelnyxCallState, CallStateHelpers } from './call-state';
|
|
4
|
+
import { Call as TelnyxCall } from '@telnyx/react-native-voice-sdk';
|
|
5
|
+
import { Platform } from 'react-native';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Represents a call with reactive state streams.
|
|
9
|
+
*
|
|
10
|
+
* This class wraps the underlying Telnyx Call object and provides
|
|
11
|
+
* reactive streams for all call state changes, making it easy to
|
|
12
|
+
* integrate with any state management solution.
|
|
13
|
+
*/
|
|
14
|
+
export class Call {
|
|
15
|
+
private readonly _callState = new BehaviorSubject<TelnyxCallState>(TelnyxCallState.RINGING);
|
|
16
|
+
private readonly _isMuted = new BehaviorSubject<boolean>(false);
|
|
17
|
+
private readonly _isHeld = new BehaviorSubject<boolean>(false);
|
|
18
|
+
private readonly _duration = new BehaviorSubject<number>(0);
|
|
19
|
+
|
|
20
|
+
private _durationTimer?: NodeJS.Timeout;
|
|
21
|
+
private _startTime?: Date;
|
|
22
|
+
|
|
23
|
+
constructor(
|
|
24
|
+
private readonly _telnyxCall: TelnyxCall,
|
|
25
|
+
private readonly _callId: string,
|
|
26
|
+
private readonly _destination: string,
|
|
27
|
+
private readonly _isIncoming: boolean,
|
|
28
|
+
isReattached: boolean = false,
|
|
29
|
+
private readonly _originalCallerName?: string,
|
|
30
|
+
private readonly _originalCallerNumber?: string
|
|
31
|
+
) {
|
|
32
|
+
// Set initial state based on whether this is a reattached call
|
|
33
|
+
if (isReattached) {
|
|
34
|
+
console.log('Call: Setting initial state to ACTIVE for reattached call');
|
|
35
|
+
this._callState.next(TelnyxCallState.ACTIVE);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
this._setupCallListeners();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Unique identifier for this call
|
|
43
|
+
*/
|
|
44
|
+
get callId(): string {
|
|
45
|
+
return this._callId;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* The destination number or SIP URI
|
|
50
|
+
*/
|
|
51
|
+
get destination(): string {
|
|
52
|
+
return this._destination;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Whether this is an incoming call
|
|
57
|
+
*/
|
|
58
|
+
get isIncoming(): boolean {
|
|
59
|
+
return this._isIncoming;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Whether this is an outgoing call
|
|
64
|
+
*/
|
|
65
|
+
get isOutgoing(): boolean {
|
|
66
|
+
return !this._isIncoming;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Current call state (synchronous access)
|
|
71
|
+
*/
|
|
72
|
+
get currentState(): TelnyxCallState {
|
|
73
|
+
return this._callState.value;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Current mute state (synchronous access)
|
|
78
|
+
*/
|
|
79
|
+
get currentIsMuted(): boolean {
|
|
80
|
+
return this._isMuted.value;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Current hold state (synchronous access)
|
|
85
|
+
*/
|
|
86
|
+
get currentIsHeld(): boolean {
|
|
87
|
+
return this._isHeld.value;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Current call duration in seconds (synchronous access)
|
|
92
|
+
*/
|
|
93
|
+
get currentDuration(): number {
|
|
94
|
+
return this._duration.value;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Custom headers received from the WebRTC INVITE message.
|
|
99
|
+
* These headers are passed during call initiation and can contain application-specific information.
|
|
100
|
+
* Format should be [{"name": "X-Header-Name", "value": "Value"}] where header names must start with "X-".
|
|
101
|
+
*/
|
|
102
|
+
get inviteCustomHeaders(): { name: string; value: string }[] | null {
|
|
103
|
+
return this._telnyxCall.inviteCustomHeaders;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Custom headers received from the WebRTC ANSWER message.
|
|
108
|
+
* These headers are passed during call acceptance and can contain application-specific information.
|
|
109
|
+
* Format should be [{"name": "X-Header-Name", "value": "Value"}] where header names must start with "X-".
|
|
110
|
+
*/
|
|
111
|
+
get answerCustomHeaders(): { name: string; value: string }[] | null {
|
|
112
|
+
return this._telnyxCall.answerCustomHeaders;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Get the underlying Telnyx Call object (for internal use)
|
|
117
|
+
* @internal
|
|
118
|
+
*/
|
|
119
|
+
get telnyxCall(): TelnyxCall {
|
|
120
|
+
return this._telnyxCall;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Observable stream of call state changes
|
|
125
|
+
*/
|
|
126
|
+
get callState$(): Observable<TelnyxCallState> {
|
|
127
|
+
return this._callState.asObservable().pipe(distinctUntilChanged());
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Observable stream of mute state changes
|
|
132
|
+
*/
|
|
133
|
+
get isMuted$(): Observable<boolean> {
|
|
134
|
+
return this._isMuted.asObservable().pipe(distinctUntilChanged());
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Observable stream of hold state changes
|
|
139
|
+
*/
|
|
140
|
+
get isHeld$(): Observable<boolean> {
|
|
141
|
+
return this._isHeld.asObservable().pipe(distinctUntilChanged());
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Observable stream of call duration changes (in seconds)
|
|
146
|
+
*/
|
|
147
|
+
get duration$(): Observable<number> {
|
|
148
|
+
return this._duration.asObservable().pipe(distinctUntilChanged());
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Observable that emits true when the call can be answered
|
|
153
|
+
*/
|
|
154
|
+
get canAnswer$(): Observable<boolean> {
|
|
155
|
+
return this.callState$.pipe(
|
|
156
|
+
map((state) => CallStateHelpers.canAnswer(state)),
|
|
157
|
+
distinctUntilChanged()
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Observable that emits true when the call can be hung up
|
|
163
|
+
*/
|
|
164
|
+
get canHangup$(): Observable<boolean> {
|
|
165
|
+
return this.callState$.pipe(
|
|
166
|
+
map((state) => CallStateHelpers.canHangup(state)),
|
|
167
|
+
distinctUntilChanged()
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Observable that emits true when the call can be put on hold
|
|
173
|
+
*/
|
|
174
|
+
get canHold$(): Observable<boolean> {
|
|
175
|
+
return this.callState$.pipe(
|
|
176
|
+
map((state) => CallStateHelpers.canHold(state)),
|
|
177
|
+
distinctUntilChanged()
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Observable that emits true when the call can be resumed from hold
|
|
183
|
+
*/
|
|
184
|
+
get canResume$(): Observable<boolean> {
|
|
185
|
+
return this.callState$.pipe(
|
|
186
|
+
map((state) => CallStateHelpers.canResume(state)),
|
|
187
|
+
distinctUntilChanged()
|
|
188
|
+
);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Answer the incoming call
|
|
193
|
+
* @param customHeaders Optional custom headers to include with the answer
|
|
194
|
+
*/
|
|
195
|
+
async answer(customHeaders?: { name: string; value: string }[]): Promise<void> {
|
|
196
|
+
if (!CallStateHelpers.canAnswer(this.currentState)) {
|
|
197
|
+
throw new Error(`Cannot answer call in state: ${this.currentState}`);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
try {
|
|
201
|
+
// On iOS, use CallKit coordinator for proper audio session handling
|
|
202
|
+
if (Platform.OS === 'ios') {
|
|
203
|
+
const { callKitCoordinator } = await import('../callkit/callkit-coordinator');
|
|
204
|
+
if (callKitCoordinator.isAvailable()) {
|
|
205
|
+
console.log('Call: Using CallKit coordinator to answer call (iOS)');
|
|
206
|
+
await callKitCoordinator.answerCallFromUI(this._telnyxCall);
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// Fallback for Android or when CallKit is not available
|
|
212
|
+
console.log('Call: Setting state to CONNECTING before answering');
|
|
213
|
+
this._callState.next(TelnyxCallState.CONNECTING);
|
|
214
|
+
|
|
215
|
+
// Pass custom headers to the underlying Telnyx call
|
|
216
|
+
await this._telnyxCall.answer(customHeaders);
|
|
217
|
+
} catch (error) {
|
|
218
|
+
console.error('Failed to answer call:', error);
|
|
219
|
+
throw error;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Hang up the call
|
|
225
|
+
* @param customHeaders Optional custom headers to include with the hangup request
|
|
226
|
+
*/
|
|
227
|
+
async hangup(customHeaders?: { name: string; value: string }[]): Promise<void> {
|
|
228
|
+
if (!CallStateHelpers.canHangup(this.currentState)) {
|
|
229
|
+
throw new Error(`Cannot hang up call in state: ${this.currentState}`);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
try {
|
|
233
|
+
// On iOS, use CallKit coordinator for proper CallKit cleanup
|
|
234
|
+
if (Platform.OS === 'ios') {
|
|
235
|
+
const { callKitCoordinator } = await import('../callkit/callkit-coordinator');
|
|
236
|
+
if (callKitCoordinator.isAvailable()) {
|
|
237
|
+
console.log('Call: Using CallKit coordinator to end call (iOS)');
|
|
238
|
+
await callKitCoordinator.endCallFromUI(this._telnyxCall);
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// Fallback for Android or when CallKit is not available
|
|
244
|
+
await this._telnyxCall.hangup(customHeaders);
|
|
245
|
+
|
|
246
|
+
// On Android, also notify the native side to hide ongoing notification
|
|
247
|
+
if (Platform.OS === 'android') {
|
|
248
|
+
try {
|
|
249
|
+
const { VoicePnBridge } = await import('../internal/voice-pn-bridge');
|
|
250
|
+
await VoicePnBridge.endCall(this._callId);
|
|
251
|
+
console.log('Call: Notified Android to hide ongoing notification');
|
|
252
|
+
} catch (error) {
|
|
253
|
+
console.error('Call: Failed to notify Android about call end:', error);
|
|
254
|
+
// Don't fail the hangup if notification hiding fails
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
} catch (error) {
|
|
258
|
+
console.error('Failed to hang up call:', error);
|
|
259
|
+
throw error;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Put the call on hold
|
|
265
|
+
*/
|
|
266
|
+
async hold(): Promise<void> {
|
|
267
|
+
if (!CallStateHelpers.canHold(this.currentState)) {
|
|
268
|
+
throw new Error(`Cannot hold call in state: ${this.currentState}`);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
try {
|
|
272
|
+
await this._telnyxCall.hold();
|
|
273
|
+
} catch (error) {
|
|
274
|
+
console.error('Failed to hold call:', error);
|
|
275
|
+
throw error;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* Resume the call from hold
|
|
281
|
+
*/
|
|
282
|
+
async resume(): Promise<void> {
|
|
283
|
+
if (!CallStateHelpers.canResume(this.currentState)) {
|
|
284
|
+
throw new Error(`Cannot resume call in state: ${this.currentState}`);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
try {
|
|
288
|
+
await this._telnyxCall.unhold();
|
|
289
|
+
} catch (error) {
|
|
290
|
+
console.error('Failed to resume call:', error);
|
|
291
|
+
throw error;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Mute the call
|
|
297
|
+
*/
|
|
298
|
+
async mute(): Promise<void> {
|
|
299
|
+
if (!CallStateHelpers.canToggleMute(this.currentState)) {
|
|
300
|
+
throw new Error(`Cannot mute call in state: ${this.currentState}`);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
try {
|
|
304
|
+
this._telnyxCall.mute();
|
|
305
|
+
this._isMuted.next(true);
|
|
306
|
+
} catch (error) {
|
|
307
|
+
console.error('Failed to mute call:', error);
|
|
308
|
+
throw error;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* Unmute the call
|
|
314
|
+
*/
|
|
315
|
+
async unmute(): Promise<void> {
|
|
316
|
+
if (!CallStateHelpers.canToggleMute(this.currentState)) {
|
|
317
|
+
throw new Error(`Cannot unmute call in state: ${this.currentState}`);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
try {
|
|
321
|
+
this._telnyxCall.unmute();
|
|
322
|
+
this._isMuted.next(false);
|
|
323
|
+
} catch (error) {
|
|
324
|
+
console.error('Failed to unmute call:', error);
|
|
325
|
+
throw error;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* Toggle mute state
|
|
331
|
+
*/
|
|
332
|
+
async toggleMute(): Promise<void> {
|
|
333
|
+
if (this.currentIsMuted) {
|
|
334
|
+
await this.unmute();
|
|
335
|
+
} else {
|
|
336
|
+
await this.mute();
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* Set the call to connecting state (used for push notification calls when answered via CallKit)
|
|
342
|
+
* @internal
|
|
343
|
+
*/
|
|
344
|
+
setConnecting(): void {
|
|
345
|
+
console.log('Call: Setting state to CONNECTING for push notification answer');
|
|
346
|
+
this._callState.next(TelnyxCallState.CONNECTING);
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* Clean up resources when the call is disposed
|
|
351
|
+
*/
|
|
352
|
+
dispose(): void {
|
|
353
|
+
this._stopDurationTimer();
|
|
354
|
+
this._callState.complete();
|
|
355
|
+
this._isMuted.complete();
|
|
356
|
+
this._isHeld.complete();
|
|
357
|
+
this._duration.complete();
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* Set up listeners for the underlying Telnyx call
|
|
362
|
+
*/
|
|
363
|
+
private _setupCallListeners(): void {
|
|
364
|
+
// Map Telnyx call states to our simplified states
|
|
365
|
+
this._telnyxCall.on('telnyx.call.state', (call: any, state: any) => {
|
|
366
|
+
const telnyxState = this._mapToTelnyxCallState(state);
|
|
367
|
+
this._callState.next(telnyxState);
|
|
368
|
+
|
|
369
|
+
// Start duration timer when call becomes active
|
|
370
|
+
if (telnyxState === TelnyxCallState.ACTIVE && !this._startTime) {
|
|
371
|
+
this._startDurationTimer();
|
|
372
|
+
|
|
373
|
+
// Show ongoing call notification on Android when call becomes active
|
|
374
|
+
// This covers both locally answered calls and calls that become active from remote side
|
|
375
|
+
if (Platform.OS === 'android') {
|
|
376
|
+
(async () => {
|
|
377
|
+
try {
|
|
378
|
+
const { VoicePnBridge } = await import('../internal/voice-pn-bridge');
|
|
379
|
+
|
|
380
|
+
// Extract caller information based on call direction
|
|
381
|
+
let callerNumber: string | undefined;
|
|
382
|
+
let callerName: string | undefined;
|
|
383
|
+
|
|
384
|
+
if (this._isIncoming) {
|
|
385
|
+
// For incoming calls, use the remote caller ID (who's calling us)
|
|
386
|
+
callerNumber = this._telnyxCall.remoteCallerIdNumber;
|
|
387
|
+
callerName = this._telnyxCall.remoteCallerIdName;
|
|
388
|
+
} else {
|
|
389
|
+
// For outgoing calls, use our own caller ID (what we're showing to them)
|
|
390
|
+
// These are the values we set when making the call
|
|
391
|
+
callerNumber = this._telnyxCall.localCallerIdNumber || this._originalCallerNumber;
|
|
392
|
+
callerName = this._telnyxCall.localCallerIdName || this._originalCallerName;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
// Fallback logic for better notification display - avoid "Unknown" when possible
|
|
396
|
+
let displayName: string;
|
|
397
|
+
let displayNumber: string;
|
|
398
|
+
|
|
399
|
+
if (this._isIncoming) {
|
|
400
|
+
// For incoming calls: use caller name or fall back to caller number, then destination
|
|
401
|
+
displayName = callerName || callerNumber || this._destination;
|
|
402
|
+
displayNumber = callerNumber || this._destination;
|
|
403
|
+
} else {
|
|
404
|
+
// For outgoing calls: use our caller ID or descriptive text
|
|
405
|
+
displayName = callerName || `${this._destination}`;
|
|
406
|
+
displayNumber = callerNumber || this._destination;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
await VoicePnBridge.showOngoingCallNotification(
|
|
410
|
+
displayName,
|
|
411
|
+
displayNumber,
|
|
412
|
+
this._callId
|
|
413
|
+
);
|
|
414
|
+
console.log('Call: Showed ongoing call notification on Android (call active)', {
|
|
415
|
+
isIncoming: this._isIncoming,
|
|
416
|
+
callerName: displayName,
|
|
417
|
+
callerNumber: displayNumber,
|
|
418
|
+
});
|
|
419
|
+
} catch (error) {
|
|
420
|
+
console.error('Call: Failed to show ongoing call notification on active:', error);
|
|
421
|
+
}
|
|
422
|
+
})();
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
// Stop duration timer when call ends
|
|
427
|
+
if (CallStateHelpers.isTerminated(telnyxState)) {
|
|
428
|
+
this._stopDurationTimer();
|
|
429
|
+
|
|
430
|
+
// Clean up ongoing call notification on Android when call ends
|
|
431
|
+
if (Platform.OS === 'android') {
|
|
432
|
+
(async () => {
|
|
433
|
+
try {
|
|
434
|
+
const { VoicePnBridge } = await import('../internal/voice-pn-bridge');
|
|
435
|
+
await VoicePnBridge.endCall(this._callId);
|
|
436
|
+
console.log('Call: Cleaned up ongoing call notification (call terminated)');
|
|
437
|
+
} catch (error) {
|
|
438
|
+
console.error(
|
|
439
|
+
'Call: Failed to clean up ongoing call notification on termination:',
|
|
440
|
+
error
|
|
441
|
+
);
|
|
442
|
+
}
|
|
443
|
+
})();
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
});
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
/**
|
|
450
|
+
* Map Telnyx SDK call states to our simplified call states
|
|
451
|
+
*/
|
|
452
|
+
private _mapToTelnyxCallState(telnyxState: any): TelnyxCallState {
|
|
453
|
+
// This mapping will depend on the actual Telnyx SDK call states
|
|
454
|
+
// For now, using a basic mapping - this should be updated based on actual SDK
|
|
455
|
+
switch (telnyxState) {
|
|
456
|
+
case 'ringing':
|
|
457
|
+
case 'new':
|
|
458
|
+
return TelnyxCallState.RINGING;
|
|
459
|
+
case 'connecting':
|
|
460
|
+
return TelnyxCallState.CONNECTING;
|
|
461
|
+
case 'active':
|
|
462
|
+
case 'answered':
|
|
463
|
+
return TelnyxCallState.ACTIVE;
|
|
464
|
+
case 'held':
|
|
465
|
+
return TelnyxCallState.HELD;
|
|
466
|
+
case 'ended':
|
|
467
|
+
case 'hangup':
|
|
468
|
+
return TelnyxCallState.ENDED;
|
|
469
|
+
case 'failed':
|
|
470
|
+
case 'rejected':
|
|
471
|
+
return TelnyxCallState.FAILED;
|
|
472
|
+
case 'dropped':
|
|
473
|
+
return TelnyxCallState.DROPPED;
|
|
474
|
+
default:
|
|
475
|
+
console.warn(`Unknown call state: ${telnyxState}`);
|
|
476
|
+
return TelnyxCallState.RINGING;
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
/**
|
|
481
|
+
* Start the duration timer
|
|
482
|
+
*/
|
|
483
|
+
private _startDurationTimer(): void {
|
|
484
|
+
this._startTime = new Date();
|
|
485
|
+
this._durationTimer = setInterval(() => {
|
|
486
|
+
if (this._startTime) {
|
|
487
|
+
const duration = Math.floor((Date.now() - this._startTime.getTime()) / 1000);
|
|
488
|
+
this._duration.next(duration);
|
|
489
|
+
}
|
|
490
|
+
}, 1000);
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
/**
|
|
494
|
+
* Stop the duration timer
|
|
495
|
+
*/
|
|
496
|
+
private _stopDurationTimer(): void {
|
|
497
|
+
if (this._durationTimer) {
|
|
498
|
+
clearInterval(this._durationTimer);
|
|
499
|
+
this._durationTimer = undefined;
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
}
|