Camera is still turned on when I set publishVideo to false in openvidu

34 Views Asked by At

I have taken a look at this openvidu forum link but I'm just not sure how to implement it. I'm guessing that openvidu has not included this in its updates. Is there a working example that could help me solve this?

What I have right now looks something like this (where isUseVideo is the state which tells the app whether the user wants to turn on camera or not):

useEffect(() => {
    if (!isInMeetingRoom || !openvidu || !publisher) return

    if (!isUseVideo) {
        publisher.publishVideo(false);
        return
    }

    console.log("MeetingRoom - Replace Publisher Stream - Video")

    const currentVideoDeviceId = publisher?.stream
        ?.getMediaStream()
        ?.getVideoTracks()[0]
        ?.getSettings()
        ?.deviceId

    if (!currentVideoDeviceId) return

    if (currentVideoDeviceId === selectedVideoDeviceId) {
        publisher.publishVideo(true)
        return
    }

    openvidu.getUserMedia({
        ...defaultPublisherProperties,
        videoSource: selectedVideoDeviceId // The source of video. If undefined default webcam
    }).then(mediaStream => {
        console.log(`MeetingRoom - Replace Publisher Stream - Video ${mediaStream.id}`)
        const newVideoTrack = mediaStream.getVideoTracks()[0]
        publisher.replaceTrack(newVideoTrack)
            .then(() => {
                console.log("MeetingRoom - Replace Publisher Stream - Video - Success")
                publisher.publishVideo(true)
                stopStreamTracks(publisherVideoStream)
                dispatch(setPublisherVideoStream(mediaStream))
            })
            .catch(error => {
                console.log("MeetingRoom - Error Replace Publisher Stream - Video", error)
            })
    }).catch(error => {
        console.log("MeetingRoom - Error Replace Publisher Stream - getUserMedia Video", error)
    })
}, [isInMeetingRoom, openvidu, publisher, isUseVideo, selectedVideoDeviceId])
0

There are 0 best solutions below