const [remoteStream, setRemoteStream] = useState(null);
const [localStream, setLocalStream] = useState(null);
const [creater, setCreater] = useState(false);
const [candidates, setCandidates] = useState<any>();
const answer = useSelector((state: any) => state.answer.answer);
const getOfferData = useSelector((state: any) => state.offer.offer);
const getCandidates = useSelector((state: any) => state.candidates?.payload);
const dispatch = useDispatch();
const offer = getOfferData?.offer;
const getData = getOfferData;
delete getData?.offer;
const peerConnection = new RTCPeerConnection(servers)
const channel = peerConnection.createDataChannel(‘test’)
useEffect(() => {
offer && Object.keys(offer).length !== 0 && !creater && createAnswer()
}, [getOfferData]);
useEffect(() => {
creater && getAnswer();
}, [answer])
peerConnection.onicecandidate = (event: any) => {
if (event.candidate !== null) {
peerConnection.addIceCandidate(new RTCIceCandidate(event.candidate.candidate));
candidates.push(event.candidate)
setCandidates(candidates)
}
};
peerConnection.onmessage = (event: any) => {
console.log('message', event.message);
};
peerConnection.onopen = (event: any) => {
console.log('onopen', event);
};
const startConection = async () => {
setCreater(true)
console.log('creater', creater);
if(!creater){
const local: any = await mediaDevices.getUserMedia({
video: true,
audio: true,
})
setLocalStream(local);
// @ts-ignore
const offerDescription: any = await peerConnection.createOffer();
await peerConnection.setLocalDescription(new RTCSessionDescription(offerDescription));
sendOfferFunction();
local.getTracks().forEach((track: any) => peerConnection.addTrack(track));
})};
};
const createAnswer = async () => {
const remoteDescripton = new RTCSessionDescription(offer)
await peerConnection.setRemoteDescription(remoteDescripton);
getCandidates && addCandidates(getCandidates);
const createdAnswer: any = await peerConnection.createAnswer();
const localDescription = new RTCSessionDescription(createdAnswer)
await peerConnection.setLocalDescription(localDescription);
const local: any = await mediaDevices.getUserMedia({
video: true,
audio: true,
});
setLocalStream(local);
//@ts-ignore
const remote: any = new MediaStream();
peerConnection.ontrack = (event: any) => {
event.streams[0].getTracks().forEach((track: any) => {
remote.addTrack(track);
});
};
setRemoteStream(remote);
sendAnswerFunction
setStartCalling(true)
dispatch(saveAnswer({}))
};
const getAnswer = async () => {
const remoteDescripton = new RTCSessionDescription(answer);
await peerConnection.setRemoteDescription(remoteDescripton)
.catch((err : any) => console.log('error', err))
addCandidates(getCandidates);
dispatch(saveCandidates({}));
};
const addCandidates = (getCandidates: any) => {
getCandidates.map((candidate: any) => {
peerConnection.addIceCandidate(new RTCIceCandidate(candidate.candidate))
})
}
this is my consols LOG rn-webrtc:pc:DEBUG 0 ctor +0ms
LOG rn-webrtc:pc:DEBUG 1 ctor +300ms
LOG rn-webrtc:pc:DEBUG 2 ctor +409ms
LOG rn-webrtc:pc:DEBUG 0 ctor +0ms
LOG rn-webrtc:pc:DEBUG 1 ctor +249ms
LOG rn-webrtc:pc:DEBUG 2 ctor +174ms
LOG rn-webrtc:pc:DEBUG 3 ctor +9s
LOG creater false
LOG rn-webrtc:pc:DEBUG 4 ctor +2s
LOG rn-webrtc:pc:DEBUG 5 ctor +72ms
LOG rn-webrtc:pc:DEBUG 3 createOffer +31ms
LOG rn-webrtc:pc:DEBUG 3 createOffer OK +9ms
LOG rn-webrtc:pc:DEBUG 3 setLocalDescription +2ms
LOG rn-webrtc:pc:DEBUG 3 setLocalDescription OK +10ms
LOG rn-webrtc:pc:DEBUG 3 addIceCandidate +44ms
LOG rn-webrtc:pc:DEBUG 6 ctor +14ms
LOG sendOffer {“event_id”: “$fPy_2cpBng9kQLZ4aFNyNSdujJj6Gx_jfAyJPhIvx7o”}
LOG rn-webrtc:pc:DEBUG 7 ctor +287ms
LOG rn-webrtc:pc:DEBUG 3 ctor +11s
LOG rn-webrtc:pc:DEBUG 3 setRemoteDescription +31ms
LOG rn-webrtc:pc:DEBUG 8 ctor +141ms
LOG rn-webrtc:pc:DEBUG 4 ctor +172ms
LOG sendCandidates {“event_id”:“$H0x-7E3iOlBr3pIq2JxmaIEMR6v26t7m0uFJpEmoTLs”}
LOG rn-webrtc:pc:DEBUG 3 addTrack +200ms
LOG rn-webrtc:pc:DEBUG 3 addTrack +55ms
LOG rn-webrtc:pc:DEBUG 9 ctor +91ms
LOG rn-webrtc:pc:DEBUG 3 setRemoteDescription OK +287ms
LOG rn-webrtc:pc:DEBUG 3 createAnswer +1ms
LOG rn-webrtc:pc:DEBUG 3 createAnswer OK +8ms
LOG rn-webrtc:pc:DEBUG 3 setLocalDescription +1ms
LOG rn-webrtc:pc:DEBUG 3 setLocalDescription OK +7ms
LOG rn-webrtc:pc:DEBUG 10 ctor +123ms
LOG rn-webrtc:pc:DEBUG 3 addIceCandidate +45ms
LOG rn-webrtc:pc:DEBUG 5 ctor +92ms
LOG rn-webrtc:pc:DEBUG 6 ctor +82ms
LOG rn-webrtc:pc:DEBUG 7 ctor +88ms
LOG rn-webrtc:pc:DEBUG 8 ctor +213ms
LOG rn-webrtc:pc:DEBUG 9 ctor +169ms
LOG rn-webrtc:pc:DEBUG 10 ctor +191ms
LOG sendAnswer {“event_id”: “$ntsGxUuBKRxoAqlLIHfOY1ClVL1txw3MCTjZ8J83Cj4”}
LOG rn-webrtc:pc:DEBUG 11 ctor +633ms
LOG rn-webrtc:pc:DEBUG 12 ctor +123ms
LOG sendCandidates {“event_id”:“$9JwdasoiEgf76v7_3FvYKZPPdhygqjH_KX4qhxxBGlA”}
LOG rn-webrtc:pc:DEBUG 13 ctor +519ms
LOG rn-webrtc:pc:DEBUG 14 ctor +194ms
LOG rn-webrtc:pc:DEBUG 11 ctor +2s
LOG rn-webrtc:pc:DEBUG 11 setRemoteDescription +31ms
LOG rn-webrtc:pc:DEBUG 12 ctor +95ms
LOG error Failed to set remote answer sdp: Called in wrong state: stable
LOG rn-webrtc:pc:DEBUG 11 addIceCandidate +190ms
LOG rn-webrtc:pc:DEBUG 13 ctor +17ms
LOG rn-webrtc:pc:DEBUG 14 ctor +145ms
LOG rn-webrtc:pc:DEBUG 15 ctor +134ms
help find a solution)