Failed to set remote answer sdp: Called in wrong state: stable in react native

问题描述 投票:0回答:0

我在本机反应中使用此代码,每当我处理“negotiationneeded”事件时,我都会收到此错误。我正在为具有单独界面的 2 台设备制作此应用程序,因此此代码的某些部分看起来没有处理传入报价的方法

  const { ready, socketId, socket, peer, getAnswer, setLocalDescription, getOffer } = webrtc;
  const [myStream, setMyStream] = useState<any>();
  const [remoteStream, setRemoteStream] = useState<any>();

  useEffect(() => {
    handleCallUser()
  }, [])

  /////////// SEND STREAM ////////////////////
  const sendStreams = useCallback(() => {
    for (const track of myStream.getTracks()) {
      peer.addTrack(track, myStream);
    }
  }, [myStream]);

  /////////////////// SEND OFFER //////////////////
  const handleCallUser = useCallback(async () => {
    const stream = await mediaDevices.getUserMedia({
      audio: true,
      video: {
        frameRate: 30,
        facingMode: 'user'
      },
    });
    notifee.cancelAllNotifications();
    startCall()
    const offer = await getOffer();
    socket.emit("student-offer", { to: socketId.mentor, offer });
    setMyStream(stream);
    sendStreams();
  }, [socketId, socket]);

  /////////// HANDLE CALL ACCEPTED ////////////////////
  const handleCallAccepted = useCallback(
    ({ from, ans }: any) => {
      setLocalDescription(ans);
    },
    [sendStreams]
  );

  /////////// HANDLE NEGOCIATION ////////////////////
  const handleNegoNeeded = useCallback(async () => {
    const offer = await getOffer();
    socket.emit("peer:nego:needed", { offer, to: socketId.mentor });
  }, [socketId, socket]);

  const handleNegoNeedIncomming = useCallback(
    async ({ from, offer }: any) => {
      const ans = await getAnswer(offer);
      socket.emit("peer:nego:done", { to: from, ans });
    },
    [socket]);

  const handleNegoNeedFinal = useCallback(async ({ ans }: any) => {
    await setLocalDescription(ans);
  }, []);

  const handleTracks = async (ev: any) => {
    const remoteStream = ev.streams;
    setRemoteStream(remoteStream[0]);
  }

  useEffect(() => {
    peer.addEventListener("negotiationneeded", handleNegoNeeded);
    peer.addEventListener("track", handleTracks);
    return () => {
      peer.removeEventListener("negotiationneeded", handleNegoNeeded);
      peer.removeEventListener("track", handleTracks);
    };
  }, [handleNegoNeeded]);


  /////////// HANDLE SOCKETS ////////////////////
  useEffect(() => {
    socket.on("offer-accepted", handleCallAccepted);
    socket.on("peer:nego:needed", handleNegoNeedIncomming);
    socket.on("peer:nego:final", handleNegoNeedFinal);

    return () => {
      socket.off("call:accepted", handleCallAccepted);
      socket.off("peer:nego:needed", handleNegoNeedIncomming);
      socket.off("peer:nego:final", handleNegoNeedFinal);
    };
  }, [socket, handleCallAccepted, handleNegoNeedIncomming, handleNegoNeedFinal]);

!!!!!!!!!!!!!!忽略此文本!!!!!!!!!!!忽略此文本 !!!!!!!!!!!

react-native webrtc react-native-webrtc
© www.soinside.com 2019 - 2024. All rights reserved.