-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathlivekit-store.ts
More file actions
784 lines (699 loc) · 28.7 KB
/
livekit-store.ts
File metadata and controls
784 lines (699 loc) · 28.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
146
147
148
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
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
import { AudioSession } from '@livekit/react-native';
import { RTCAudioSession } from '@livekit/react-native-webrtc';
import notifee, { AndroidForegroundServiceType, AndroidImportance } from '@notifee/react-native';
import { getRecordingPermissionsAsync, requestRecordingPermissionsAsync } from 'expo-audio';
import { Audio, InterruptionModeIOS } from 'expo-av';
import * as Device from 'expo-device';
import { Room, RoomEvent } from 'livekit-client';
import { Alert, Linking, PermissionsAndroid, Platform } from 'react-native';
import { create } from 'zustand';
import { getCanConnectToVoiceSession, getDepartmentVoiceSettings } from '../../api/voice';
import { logger } from '../../lib/logging';
import { type DepartmentVoiceChannelResultData } from '../../models/v4/voice/departmentVoiceResultData';
import { audioService } from '../../services/audio.service';
import { bluetoothAudioService } from '../../services/bluetooth-audio.service';
import { callKeepService } from '../../services/callkeep.service';
import { useBluetoothAudioStore } from './bluetooth-audio-store';
// Helper function to apply audio routing
export const applyAudioRouting = async (deviceType: 'bluetooth' | 'speaker' | 'earpiece' | 'default') => {
// Audio routing is native-only
if (Platform.OS === 'web') return;
try {
const normalizedDeviceType: 'bluetooth' | 'speaker' | 'earpiece' = deviceType === 'default' ? 'earpiece' : deviceType;
if (Platform.OS === 'android') {
logger.info({
message: 'Applying Android audio routing',
context: { deviceType: normalizedDeviceType },
});
const { NativeModules } = require('react-native');
const inCallAudioModule = NativeModules?.InCallAudioModule;
if (inCallAudioModule?.setAudioRoute) {
try {
await inCallAudioModule.setAudioRoute(normalizedDeviceType);
logger.debug({
message: 'Applied Android route via InCallAudioModule',
context: { deviceType: normalizedDeviceType },
});
} catch (routeError) {
logger.warn({
message: 'Failed to apply Android route via InCallAudioModule, falling back to Audio.setAudioModeAsync',
context: { deviceType: normalizedDeviceType, error: routeError },
});
}
}
// On Android, we use RTCAudioSession for precise control
// First, ensure the audio session is configured correctly
await Audio.setAudioModeAsync({
allowsRecordingIOS: true,
staysActiveInBackground: true,
playsInSilentModeIOS: true,
shouldDuckAndroid: false,
// For speaker, we want false (speaker). For others, simple routing.
playThroughEarpieceAndroid: normalizedDeviceType !== 'speaker',
interruptionModeIOS: InterruptionModeIOS.MixWithOthers,
});
// Use RTCAudioSession to force route selection for WebRTC (Not available on Android in this package)
// We rely on Audio.setAudioModeAsync and system behavior.
/*
const RTCAudioSessionAny = RTCAudioSession as any;
if (RTCAudioSessionAny.getAudioDevices && RTCAudioSessionAny.selectAudioDevice) {
// ... (Logic removed as it's iOS only)
} else {
logger.info({
message: 'RTCAudioSession Android methods not available (Relying on system routing)',
});
}
*/
logger.info({
message: 'Android audio routing applied via Audio.setAudioModeAsync',
context: { deviceType: normalizedDeviceType },
});
} else {
// iOS handling (Expo AV is usually sufficient, but CallKeep handles the session)
// Just ensure the mode is correct
await Audio.setAudioModeAsync({
allowsRecordingIOS: true,
staysActiveInBackground: true,
playsInSilentModeIOS: true,
shouldDuckAndroid: false,
playThroughEarpieceAndroid: true,
interruptionModeIOS: InterruptionModeIOS.MixWithOthers,
});
}
} catch (error) {
logger.error({
message: 'Failed to apply audio routing',
context: { error },
});
}
};
// Helper function to setup audio routing based on selected devices
const setupAudioRouting = async (room: Room): Promise<void> => {
try {
const bluetoothStore = useBluetoothAudioStore.getState();
const { selectedAudioDevices, connectedDevice } = bluetoothStore;
// Determine target device type
let targetType: 'bluetooth' | 'speaker' | 'earpiece' = 'earpiece';
// If we have a connected Bluetooth device, prioritize it
if (connectedDevice && connectedDevice.hasAudioCapability) {
logger.info({
message: 'Using Bluetooth device for audio routing',
context: { deviceName: connectedDevice.name },
});
// Update selected devices to use Bluetooth
const deviceName = connectedDevice.name || 'Bluetooth Device';
const bluetoothMicrophone = connectedDevice.supportsMicrophoneControl ? { id: connectedDevice.id, name: deviceName, type: 'bluetooth' as const, isAvailable: true } : selectedAudioDevices.microphone;
const bluetoothSpeaker = {
id: connectedDevice.id,
name: deviceName,
type: 'bluetooth' as const,
isAvailable: true,
};
bluetoothStore.setSelectedMicrophone(bluetoothMicrophone);
bluetoothStore.setSelectedSpeaker(bluetoothSpeaker);
targetType = 'bluetooth';
} else {
// Use default audio devices (selected devices or default)
logger.debug({
message: 'Using default audio devices',
context: { selectedAudioDevices },
});
if (selectedAudioDevices.speaker?.type === 'speaker') {
targetType = 'speaker';
}
}
// Apply the routing
await applyAudioRouting(targetType);
} catch (error) {
logger.error({
message: 'Failed to setup audio routing',
context: { error },
});
}
};
interface LiveKitState {
// Connection state
isConnected: boolean;
isConnecting: boolean;
currentRoom: Room | null;
currentRoomInfo: DepartmentVoiceChannelResultData | null;
isTalking: boolean;
isMicrophoneEnabled: boolean;
isVoiceEnabled: boolean;
voipServerWebsocketSslAddress: string;
callerIdName: string;
canConnectApiToken: string;
canConnectToVoiceSession: boolean;
// Available rooms
lastLocalMuteChangeTimestamp: number;
availableRooms: DepartmentVoiceChannelResultData[];
isBottomSheetVisible: boolean;
// Actions
setIsConnected: (isConnected: boolean) => void;
setIsConnecting: (isConnecting: boolean) => void;
setCurrentRoom: (room: Room | null) => void;
setCurrentRoomInfo: (roomInfo: DepartmentVoiceChannelResultData | null) => void;
setIsTalking: (isTalking: boolean) => void;
setAvailableRooms: (rooms: DepartmentVoiceChannelResultData[]) => void;
setIsBottomSheetVisible: (visible: boolean) => void;
// Microphone Control
setMicrophoneEnabled: (enabled: boolean) => Promise<void>;
toggleMicrophone: () => Promise<void>;
// Room operations
connectToRoom: (roomInfo: DepartmentVoiceChannelResultData, token: string) => Promise<void>;
disconnectFromRoom: () => void;
fetchVoiceSettings: () => Promise<void>;
fetchCanConnectToVoice: () => Promise<void>;
requestPermissions: () => Promise<boolean>;
}
export const useLiveKitStore = create<LiveKitState>((set, get) => ({
isConnected: false,
isConnecting: false,
currentRoom: null,
currentRoomInfo: null,
isTalking: false,
isMicrophoneEnabled: false,
availableRooms: [],
isBottomSheetVisible: false,
isVoiceEnabled: false,
voipServerWebsocketSslAddress: '',
callerIdName: '',
canConnectApiToken: '',
canConnectToVoiceSession: false,
lastLocalMuteChangeTimestamp: 0,
setIsConnected: (isConnected) => set({ isConnected }),
setIsConnecting: (isConnecting) => set({ isConnecting }),
setCurrentRoom: (room) => set({ currentRoom: room }),
setCurrentRoomInfo: (roomInfo) => set({ currentRoomInfo: roomInfo }),
setIsTalking: (isTalking) => set({ isTalking }),
setAvailableRooms: (rooms) => set({ availableRooms: rooms }),
setIsBottomSheetVisible: (visible) => set({ isBottomSheetVisible: visible }),
setMicrophoneEnabled: async (enabled: boolean) => {
const { currentRoom } = get();
if (!currentRoom) {
logger.warn({
message: 'Cannot set microphone - no active LiveKit room',
context: { enabled },
});
return;
}
try {
const currentMicEnabled = currentRoom.localParticipant.isMicrophoneEnabled;
// Skip if already in the desired state
if (enabled && currentMicEnabled) {
set({ isMicrophoneEnabled: true });
return;
}
if (!enabled && !currentMicEnabled) {
set({ isMicrophoneEnabled: false });
return;
}
await currentRoom.localParticipant.setMicrophoneEnabled(enabled);
set({
isMicrophoneEnabled: enabled,
lastLocalMuteChangeTimestamp: Date.now(),
});
logger.info({
message: 'Microphone state set via store',
context: { enabled },
});
useBluetoothAudioStore.getState().setLastButtonAction({
action: enabled ? 'unmute' : 'mute',
timestamp: Date.now(),
});
if (enabled) {
await audioService.playStartTransmittingSound();
} else {
await audioService.playStopTransmittingSound();
}
} catch (error) {
logger.error({
message: 'Failed to set microphone state',
context: { error, enabled },
});
}
},
toggleMicrophone: async () => {
const { currentRoom } = get();
if (!currentRoom) {
logger.warn({
message: 'Cannot toggle microphone - no active LiveKit room',
});
return;
}
const currentMuteState = !currentRoom.localParticipant.isMicrophoneEnabled;
await get().setMicrophoneEnabled(currentMuteState);
},
requestPermissions: async (): Promise<boolean> => {
try {
if (Platform.OS === 'android' || Platform.OS === 'ios') {
// Use expo-audio for both Android and iOS microphone permissions
const micPermission = await getRecordingPermissionsAsync();
if (!micPermission.granted) {
const result = await requestRecordingPermissionsAsync();
if (!result.granted) {
logger.error({
message: 'Microphone permission not granted',
context: { platform: Platform.OS },
});
return false;
}
}
logger.info({
message: 'Microphone permission granted successfully',
context: { platform: Platform.OS },
});
// Note: Foreground service permissions are typically handled at the manifest level
// and don't require runtime permission requests. They are automatically granted
// when the app is installed if declared in AndroidManifest.xml
if (Platform.OS === 'android') {
// Request phone state and phone numbers permissions separately
try {
// Check if device has telephony capability (phones vs tablets without SIM)
// On non-telephony devices, READ_PHONE_STATE always returns never_ask_again
const hasTelephony = await Device.hasPlatformFeatureAsync('android.hardware.telephony');
if (!hasTelephony) {
logger.info({
message: 'Device does not have telephony capability - skipping phone state permission request',
});
} else {
// Request READ_PHONE_STATE first
const phoneStateResult = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.READ_PHONE_STATE);
// Request READ_PHONE_NUMBERS
const phoneNumbersResult = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.READ_PHONE_NUMBERS);
logger.debug({
message: 'Android Phone permissions requested',
context: {
phoneStateResult,
phoneNumbersResult,
grantedValue: PermissionsAndroid.RESULTS.GRANTED,
},
});
// Only check READ_PHONE_STATE - this is the critical permission for CallKeep
if (phoneStateResult !== PermissionsAndroid.RESULTS.GRANTED) {
logger.warn({
message: 'Phone state permission not granted - voice functionality may be limited',
context: { phoneStateResult },
});
// On devices without telephony, never_ask_again is expected - don't alert
if (phoneStateResult === PermissionsAndroid.RESULTS.NEVER_ASK_AGAIN) {
Alert.alert(
'Voice Permissions Required',
'Phone state permission was permanently denied. Voice functionality may not work correctly. Please open Settings and grant the Phone permission for this app.',
[
{ text: 'Cancel', style: 'cancel' },
{ text: 'Open Settings', onPress: () => Linking.openSettings() },
]
);
} else {
Alert.alert('Voice Permissions Warning', 'Phone state permission was not granted. Voice functionality may not work correctly. You can grant this permission in your device settings.', [{ text: 'OK' }]);
}
}
}
} catch (err) {
logger.error({
message: 'Failed to request Android phone permissions - voice functionality may not work',
context: { error: err },
});
Alert.alert('Voice Permissions Error', 'Failed to request phone permissions. Voice functionality may not work correctly.', [{ text: 'OK' }]);
}
}
return true;
}
return true; // Web/other platforms don't need runtime permissions
} catch (error) {
logger.error({
message: 'Failed to request permissions',
context: { error, platform: Platform.OS },
});
return false;
}
},
connectToRoom: async (roomInfo, token) => {
// Prevent concurrent connection attempts — give instant visual feedback by
// setting isConnecting immediately before any async work begins.
if (get().isConnecting || get().isConnected) {
logger.warn({
message: 'Connection already in progress or active, ignoring duplicate request',
context: { roomName: roomInfo.Name },
});
return;
}
set({ isConnecting: true });
try {
bluetoothAudioService.ensurePttInputMonitoring('livekit-store connectToRoom start');
const { currentRoom, voipServerWebsocketSslAddress } = get();
// Validate connection parameters before attempting to connect
if (!voipServerWebsocketSslAddress) {
logger.error({
message: 'Cannot connect to room - no VoIP server address available',
context: { roomName: roomInfo.Name },
});
set({ isConnecting: false });
Alert.alert('Voice Connection Error', 'Voice server address is not available. Please try again later.');
return;
}
if (!token) {
logger.error({
message: 'Cannot connect to room - no token provided',
context: { roomName: roomInfo.Name },
});
set({ isConnecting: false });
Alert.alert('Voice Connection Error', 'Voice channel token is missing. Please try refreshing the voice channels.');
return;
}
// Request permissions before connecting (critical for Android foreground service).
// On Android 14+, the foreground service with microphone type requires RECORD_AUDIO
// permission to be granted BEFORE the service starts.
const permissionsGranted = await get().requestPermissions();
if (!permissionsGranted) {
logger.error({
message: 'Cannot connect to room - permissions not granted',
context: { roomName: roomInfo.Name },
});
set({ isConnecting: false });
Alert.alert('Voice Connection Error', 'Microphone permission is required to join a voice channel. Please grant the permission in your device settings.', [
{ text: 'Cancel', style: 'cancel' },
{ text: 'Open Settings', onPress: () => Linking.openSettings() },
]);
return;
}
// Disconnect from current room if connected
if (currentRoom) {
currentRoom.disconnect();
}
// Start the native audio session before connecting (required for production builds)
// In dev builds, the audio session may persist across hot reloads, but in production
// cold starts it must be explicitly started for WebRTC to function correctly
if (Platform.OS !== 'web') {
try {
await AudioSession.startAudioSession();
logger.info({
message: 'Audio session started successfully',
});
} catch (audioSessionError) {
logger.warn({
message: 'Failed to start audio session - continuing with connection attempt',
context: { error: audioSessionError },
});
}
}
// Create a new room
const room = new Room();
// Setup room event listeners
room.on(RoomEvent.ParticipantConnected, (participant) => {
logger.info({
message: 'A participant connected',
context: { participantIdentity: participant.identity },
});
// Play connection sound when others join
if (participant.identity !== room.localParticipant.identity) {
//audioService.playConnectToAudioRoomSound();
}
});
room.on(RoomEvent.ParticipantDisconnected, (participant) => {
logger.info({
message: 'A participant disconnected',
context: { participantIdentity: participant.identity },
});
// Play disconnection sound when others leave
//audioService.playDisconnectedFromAudioRoomSound();
});
room.on(RoomEvent.ActiveSpeakersChanged, (speakers) => {
// Check if local participant is speaking
const localParticipant = room.localParticipant;
const isTalking = speakers.some((speaker) => speaker.sid === localParticipant.sid);
set({ isTalking });
});
// Connect to the room
logger.info({
message: 'Connecting to LiveKit room',
context: {
roomName: roomInfo.Name,
hasServerUrl: !!voipServerWebsocketSslAddress,
serverUrlPrefix: voipServerWebsocketSslAddress.substring(0, 10),
hasToken: !!token,
},
});
await room.connect(voipServerWebsocketSslAddress, token);
logger.info({
message: 'LiveKit room connected successfully',
context: { roomName: roomInfo.Name },
});
// Commit room state to the store immediately after a successful connect so
// subsequent steps (setMicrophoneEnabled, setCameraEnabled, etc.) can't orphan
// a live room if they throw.
set({
currentRoom: room,
currentRoomInfo: roomInfo,
isConnected: true,
isConnecting: false,
isMicrophoneEnabled: false,
lastLocalMuteChangeTimestamp: Date.now(),
});
// Set microphone to muted by default, camera to disabled (audio-only call)
try {
await room.localParticipant.setMicrophoneEnabled(false);
await room.localParticipant.setCameraEnabled(false);
} catch (trackError) {
logger.warn({
message: 'Failed to set initial microphone/camera state - room is still connected',
context: { error: trackError },
});
}
// Setup CallKeep mute sync
callKeepService.setMuteStateCallback(async (muted) => {
logger.info({
message: 'Syncing mute state from CallKeep',
context: { muted },
});
if (room.localParticipant.isMicrophoneEnabled === muted) {
// If CallKeep says "muted" (true), and Mic is enabled (true), we need to disable mic.
// If CallKeep says "unmuted" (false), and Mic is disabled (false), we need to enable mic.
// Wait, logic check:
// isMicrophoneEnabled = true means NOT MUTED.
// muted = true means MUTED.
// So if isMicrophoneEnabled (true) and muted (true) -> mismatch, we must mute.
// if isMicrophoneEnabled (false) and muted (false) -> mismatch, we must unmute.
// Actually effectively: setMicrophoneEnabled(!muted)
const nextMicEnabled = !muted;
await room.localParticipant.setMicrophoneEnabled(nextMicEnabled);
set({
isMicrophoneEnabled: nextMicEnabled,
lastLocalMuteChangeTimestamp: Date.now(),
});
// Play sound
if (!muted) {
await audioService.playStartTransmittingSound();
} else {
await audioService.playStopTransmittingSound();
}
}
});
// Setup CallKeep End Call sync
callKeepService.setEndCallCallback(() => {
logger.info({
message: 'CallKeep end call event received, disconnecting room',
});
get().disconnectFromRoom();
});
// Also ensure reverse sync: When app mutes, tell CallKeep?
// CallKeep tracks its own state, usually triggered by UI or simple interactions.
// If we mute from within the app (e.g. on screen button), we might want to tell CallKeep we are muted.
// However, react-native-callkeep doesn't easily expose "setMuted" for the system call without ending logic or being complex?
// Actually RNCallKeep.setMutedCall(uuid, muted) exists.
const onLocalTrackMuted = () => {
// Update CallKeep state if needed?
// For now, let's just trust CallKeep's own state management or system UI.
};
// We attach these listeners to the local participant if needed for other UI sync
// Setup audio routing based on selected devices
// This may change audio modes/focus, so it comes after media button init
await setupAudioRouting(room);
await audioService.playConnectToAudioRoomSound();
// Android foreground service for background audio.
// Only needed on Android - iOS uses CallKeep, web browsers handle audio natively.
// NOTE: notifee.registerForegroundService() is called once at app startup
// (app-initialization.service.ts). Here we only display the notification
// that triggers the already-registered handler.
if (Platform.OS === 'android') {
try {
await notifee.displayNotification({
title: 'Active PTT Call',
body: 'There is an active PTT call in progress.',
android: {
channelId: 'notif',
asForegroundService: true,
foregroundServiceTypes: [AndroidForegroundServiceType.FOREGROUND_SERVICE_TYPE_MICROPHONE],
smallIcon: 'ic_launcher',
},
});
logger.info({
message: 'Android foreground service notification displayed',
});
} catch (error) {
logger.error({
message: 'Failed to display foreground service notification',
context: { error },
});
// Don't fail the connection if the foreground service display fails.
// The call will still work but may be killed when backgrounded.
}
}
// Start CallKeep call for background audio support
// On web, callKeepService provides no-op implementation but still tracks call state
try {
// On Android, CallKeep's VoiceConnectionService requires READ_PHONE_NUMBERS
// permission. If not granted, skip CallKeep to avoid a SecurityException crash.
let shouldStartCallKeep = true;
if (Platform.OS === 'android') {
const hasPhoneNumbers = await PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.READ_PHONE_NUMBERS);
const hasPhoneState = await PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.READ_PHONE_STATE);
if (!hasPhoneNumbers || !hasPhoneState) {
shouldStartCallKeep = false;
logger.warn({
message: 'Skipping CallKeep - phone permissions not granted (READ_PHONE_NUMBERS or READ_PHONE_STATE)',
context: { hasPhoneNumbers, hasPhoneState },
});
}
}
if (shouldStartCallKeep) {
const callUUID = await callKeepService.startCall(roomInfo.Name || 'Voice Channel');
logger.info({
message: 'CallKeep call started for background audio support',
context: { callUUID, roomName: roomInfo.Name, platform: Platform.OS },
});
}
} catch (callKeepError) {
logger.warn({
message: 'Failed to start CallKeep call - background audio may not work',
context: { error: callKeepError },
});
// Don't fail the connection if CallKeep fails
}
bluetoothAudioService.ensurePttInputMonitoring('livekit-store connectToRoom connected');
} catch (error) {
logger.error({
message: 'Failed to connect to room',
context: { error, roomName: roomInfo?.Name },
});
// Stop audio session on failure since we started it above
if (Platform.OS !== 'web') {
try {
await AudioSession.stopAudioSession();
} catch (stopError) {
logger.warn({
message: 'Failed to stop audio session after connection error',
context: { error: stopError },
});
}
}
set({ isConnecting: false });
// Show user-visible error so the failure is not silent in production builds
const errorMessage = error instanceof Error ? error.message : 'An unknown error occurred';
Alert.alert('Voice Connection Failed', `Unable to connect to voice channel "${roomInfo?.Name || 'Unknown'}". ${errorMessage}`, [{ text: 'OK' }]);
}
},
disconnectFromRoom: async () => {
const { currentRoom } = get();
if (currentRoom) {
await currentRoom.disconnect();
await audioService.playDisconnectedFromAudioRoomSound();
// Stop the native audio session that was started during connectToRoom
if (Platform.OS !== 'web') {
try {
await AudioSession.stopAudioSession();
logger.debug({
message: 'Audio session stopped',
});
} catch (audioSessionError) {
logger.warn({
message: 'Failed to stop audio session',
context: { error: audioSessionError },
});
}
}
// End CallKeep call (works on all platforms - web has no-op implementation)
try {
await callKeepService.endCall();
logger.debug({
message: 'CallKeep call ended',
});
} catch (callKeepError) {
logger.warn({
message: 'Failed to end CallKeep call',
context: { error: callKeepError },
});
}
// Stop Android foreground service
if (Platform.OS === 'android') {
try {
await notifee.stopForegroundService();
} catch (error) {
logger.error({
message: 'Failed to stop foreground service',
context: { error },
});
}
}
// Cleanup CallKeep Mute Callback
callKeepService.setMuteStateCallback(null);
callKeepService.setEndCallCallback(null);
set({
currentRoom: null,
currentRoomInfo: null,
isConnected: false,
isMicrophoneEnabled: false,
isTalking: false,
});
}
},
fetchVoiceSettings: async () => {
try {
const response = await getDepartmentVoiceSettings();
let rooms: DepartmentVoiceChannelResultData[] = [];
if (response.Data.VoiceEnabled && response.Data?.Channels) {
//rooms.push({
// id: '0',
// name: 'No Channel Selected',
//});
rooms.push(...response.Data.Channels);
} //else {
// rooms.push({
// id: '0',
// name: 'No Channel Selected',
// });
//}
set({
isVoiceEnabled: response.Data.VoiceEnabled,
voipServerWebsocketSslAddress: response.Data.VoipServerWebsocketSslAddress,
callerIdName: response.Data.CallerIdName,
canConnectApiToken: response.Data.CanConnectApiToken,
availableRooms: rooms,
});
} catch (error) {
logger.error({
message: 'Failed to fetch rooms',
context: { error },
});
}
},
fetchCanConnectToVoice: async () => {
try {
const { canConnectApiToken } = get();
const response = await getCanConnectToVoiceSession(canConnectApiToken);
if (response && response.Data && response.Data.CanConnect) {
set({
canConnectToVoiceSession: response.Data.CanConnect,
});
} else {
set({ canConnectToVoiceSession: false });
}
} catch (error) {
logger.error({
message: 'Failed to fetch can connect to voice',
context: { error },
});
}
},
}));