在WebRTC视频聊天应用中,只有被叫方可以获取远程供稿

时间:2019-01-29 14:51:32

标签: reactjs redux webrtc pusher

我正在使用react,redux,webrtc和pusher作为我的信号提供程序来构建一个视频聊天应用程序,该应用程序几乎可以完美地工作,除了呼叫者没有获得远程流而被呼叫者可以获得远程流。 ontrack事件不会为呼叫者触发。

我尝试使用webrtc内部构件,但在我看来,描述和要约/答案已成功创建并设置,但是我并不真正理解很多隐秘消息。

const createAnswer = () => {
    caller.createAnswer().then(sdp => {
        const sessionDesc = new RTCSessionDescription(sdp);
        setLocalDescription(sessionDesc);
        channel.trigger("client-answer", {
            sdp: sdp,
            room: room
        });
    });;
}

const addTrack = (track, stream) => {
    caller.addTrack(track, stream);
}


channel.bind("client-sdp", msg => {
    if (msg.room === userId) {
      store.dispatch({type: actionTypes.ON_INCOMING_CALL});
      let answer = window.confirm("You have a call from " + msg.from + ": Would you like to answer?");
      if (!answer) {
        return channel.trigger("client-reject", { room: msg.room, rejected: userId });
      } else {
      room = msg.room;
      let sessionDesc = new RTCSessionDescription(msg.sdp);
      setRemoteDescription(sessionDesc);
      state = 'answerCall';
      getCam();
      createAnswer(); 
        }
    }
});

const getCam = () => {
    getMedia().then(stream => {
        stream.getTracks().forEach(track => addTrack(track, stream));    
        if (!state){
            createOffer();
        }
        store.dispatch(onLocalStream(stream));
    })
    .catch(error => {
      console.log("an error occured", error);
    });
}
export const getCallerReady = () => {
    return dispatch => {
     dispatch(prepareCaller(channel));
    //Listen for ICE Candidates and send them to remote peers
    caller.onicecandidate = evt => {
      if (!evt.candidate) return;
      onIceCandidate(caller, evt);
    };
    //ontrack handler to receive remote feed and show in remoteview video element
    caller.ontrack = evt => {
        dispatch(onTrack(evt));
    };
    }
}
const onTrack = track => {
    return {
        type: actionTypes.ON_TRACK,
        remoteStream: track.streams[0]
    }
}

我认为这是代码的相关部分。如果您想知道,这些功能位于redux动作创建者文件中,因为我从其中分发了一些动作。

我希望同时为调用者​​和被调用者调用caller.ontrack事件处理程序,但只会为被调用者调用

0 个答案:

没有答案