|
|
|
@ -1,22 +1,22 @@
@@ -1,22 +1,22 @@
|
|
|
|
|
import { |
|
|
|
|
MediaStream, |
|
|
|
|
RTCPeerConnection, |
|
|
|
|
RTCSessionDescription, |
|
|
|
|
} from 'react-native-webrtc' |
|
|
|
|
import { MediaStream, RTCPeerConnection } from 'react-native-webrtc' |
|
|
|
|
import { create } from 'zustand' |
|
|
|
|
import { iceServers } from '../configs' |
|
|
|
|
import inCallManager from 'react-native-incall-manager' |
|
|
|
|
import { answerCallReq, iceCandidateReq } from '@/api/calls/requests' |
|
|
|
|
import { Alert } from 'react-native' |
|
|
|
|
import { iceCandidateReq } from '@/api/calls/requests' |
|
|
|
|
import { AcceptCall, StartCall } from '../core' |
|
|
|
|
import { useCallFromStore } from './use-call-from.hook' |
|
|
|
|
import { NavigationService } from '@/services/system' |
|
|
|
|
import { RouteKey } from '@/shared' |
|
|
|
|
import InCallManager from 'react-native-incall-manager' |
|
|
|
|
import { StopCall } from '../core/stop-call' |
|
|
|
|
|
|
|
|
|
export enum CallMod { |
|
|
|
|
Temporary, |
|
|
|
|
Incoming, |
|
|
|
|
Outgoing, |
|
|
|
|
Speaking, |
|
|
|
|
Finished, |
|
|
|
|
Incoming, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
interface CallDataStore { |
|
|
|
|
export interface CallDataStore { |
|
|
|
|
peerConnection: RTCPeerConnection |
|
|
|
|
mod: CallMod |
|
|
|
|
targetUserId?: number |
|
|
|
@ -24,6 +24,7 @@ interface CallDataStore {
@@ -24,6 +24,7 @@ interface CallDataStore {
|
|
|
|
|
remoteRTCMessage?: any |
|
|
|
|
remoteStream?: any |
|
|
|
|
icecandidates: any[] |
|
|
|
|
connectedStatus: RTCIceConnectionState |
|
|
|
|
|
|
|
|
|
changeMod: (mod: CallMod) => void |
|
|
|
|
startCall: (targetUserId: number) => void |
|
|
|
@ -35,6 +36,7 @@ interface CallDataStore {
@@ -35,6 +36,7 @@ interface CallDataStore {
|
|
|
|
|
setRemoteStream: (stream: any) => void |
|
|
|
|
addIcecanidate: (item: any) => void |
|
|
|
|
cleanIcecandidates: () => void |
|
|
|
|
setConnectedStatus: (connectedStatus: RTCIceConnectionState) => void |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export const useCallDataStore = create<CallDataStore>()(set => ({ |
|
|
|
@ -45,6 +47,8 @@ export const useCallDataStore = create<CallDataStore>()(set => ({
@@ -45,6 +47,8 @@ export const useCallDataStore = create<CallDataStore>()(set => ({
|
|
|
|
|
remoteRTCMessage: null, |
|
|
|
|
icecandidates: [], |
|
|
|
|
|
|
|
|
|
connectedStatus: null, |
|
|
|
|
|
|
|
|
|
changeMod: mod => { |
|
|
|
|
set({ mod }) |
|
|
|
|
}, |
|
|
|
@ -75,48 +79,42 @@ export const useCallDataStore = create<CallDataStore>()(set => ({
@@ -75,48 +79,42 @@ export const useCallDataStore = create<CallDataStore>()(set => ({
|
|
|
|
|
cleanIcecandidates() { |
|
|
|
|
set({ icecandidates: [] }) |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
setConnectedStatus(connectedStatus) { |
|
|
|
|
set({ connectedStatus }) |
|
|
|
|
}, |
|
|
|
|
})) |
|
|
|
|
|
|
|
|
|
export const callDataStoreHelper = { |
|
|
|
|
peerConnection: () => useCallDataStore.getState().peerConnection, |
|
|
|
|
processAccept: async () => { |
|
|
|
|
try { |
|
|
|
|
useCallDataStore.getState().changeMod(CallMod.Speaking) |
|
|
|
|
inCallManager.stopRingtone() |
|
|
|
|
callDataStoreHelper |
|
|
|
|
.peerConnection() |
|
|
|
|
.setRemoteDescription( |
|
|
|
|
new RTCSessionDescription( |
|
|
|
|
useCallDataStore.getState().remoteRTCMessage, |
|
|
|
|
), |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
const sessionDescription = await callDataStoreHelper |
|
|
|
|
.peerConnection() |
|
|
|
|
.createAnswer() |
|
|
|
|
|
|
|
|
|
await callDataStoreHelper |
|
|
|
|
.peerConnection() |
|
|
|
|
.setLocalDescription(sessionDescription) |
|
|
|
|
|
|
|
|
|
await answerCallReq({ |
|
|
|
|
callId: useCallDataStore.getState().callId, |
|
|
|
|
rtcMessage: sessionDescription, |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
const existIcecandidates = useCallDataStore.getState().icecandidates |
|
|
|
|
|
|
|
|
|
if (existIcecandidates.length) { |
|
|
|
|
existIcecandidates.map(candidate => |
|
|
|
|
callDataStoreHelper |
|
|
|
|
.peerConnection() |
|
|
|
|
.addIceCandidate(candidate), |
|
|
|
|
) |
|
|
|
|
useCallDataStore.getState().cleanIcecandidates() |
|
|
|
|
} |
|
|
|
|
} catch (e) { |
|
|
|
|
console.log(e) |
|
|
|
|
new AcceptCall(useCallDataStore.getState).accept() |
|
|
|
|
}, |
|
|
|
|
processStart: async (targetUserId: number) => { |
|
|
|
|
InCallManager.start({ media: 'audio', ringback: '_BUNDLE_' }) |
|
|
|
|
useCallDataStore.getState().startCall(targetUserId) |
|
|
|
|
NavigationService.navigate(RouteKey.Call, {}) |
|
|
|
|
|
|
|
|
|
setTimeout(() => { |
|
|
|
|
new StartCall( |
|
|
|
|
useCallDataStore.getState, |
|
|
|
|
useCallFromStore.getState, |
|
|
|
|
).start() |
|
|
|
|
}, 999) |
|
|
|
|
}, |
|
|
|
|
stop: () => { |
|
|
|
|
new StopCall(useCallDataStore.getState).stop() |
|
|
|
|
}, |
|
|
|
|
finishCall: () => { |
|
|
|
|
const state = useCallDataStore.getState() |
|
|
|
|
if ( |
|
|
|
|
state.peerConnection.iceConnectionState === 'disconnected' || |
|
|
|
|
state.peerConnection.iceConnectionState === 'failed' |
|
|
|
|
) { |
|
|
|
|
new StopCall(useCallDataStore.getState).stop() |
|
|
|
|
} |
|
|
|
|
useCallDataStore.setState(useCallDataStore.getInitialState()) |
|
|
|
|
NavigationService.goBack() |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -127,12 +125,10 @@ function createPeerConnection() {
@@ -127,12 +125,10 @@ function createPeerConnection() {
|
|
|
|
|
|
|
|
|
|
peerConnection.addEventListener('track', event => { |
|
|
|
|
try { |
|
|
|
|
console.log('accepted track', event.track.id) |
|
|
|
|
const existremoteStream = useCallDataStore.getState().remoteStream |
|
|
|
|
const remoteMediaStream = existremoteStream || new MediaStream() |
|
|
|
|
remoteMediaStream.addTrack(event.track, remoteMediaStream) |
|
|
|
|
useCallDataStore.getState().setRemoteStream(remoteMediaStream) |
|
|
|
|
Alert.alert('track cyka') |
|
|
|
|
} catch (e) { |
|
|
|
|
console.log(e) |
|
|
|
|
} |
|
|
|
@ -154,7 +150,20 @@ function createPeerConnection() {
@@ -154,7 +150,20 @@ function createPeerConnection() {
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
peerConnection.addEventListener('iceconnectionstatechange', event => { |
|
|
|
|
console.log(peerConnection.iceConnectionState) |
|
|
|
|
console.log( |
|
|
|
|
'iceconnectionstatechange', |
|
|
|
|
peerConnection.iceConnectionState, |
|
|
|
|
) |
|
|
|
|
useCallDataStore |
|
|
|
|
.getState() |
|
|
|
|
.setConnectedStatus(peerConnection.iceConnectionState) |
|
|
|
|
|
|
|
|
|
if ( |
|
|
|
|
peerConnection.iceConnectionState === 'closed' || |
|
|
|
|
peerConnection.iceConnectionState === 'disconnected' |
|
|
|
|
) { |
|
|
|
|
useCallDataStore.getState().changeMod(CallMod.Finished) |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
peerConnection.addEventListener('icecandidateerror', event => { |
|
|
|
|