2

I am capturing the user's screen and publishing that track to the room's localparticipant object but the feed continues to display the user's camera. If I log the room object to the console it does not display multiple tracks. Is it necessary that I unpublish the user's camera track before attempting to publish the user's screen track? I've included my share screen method for you to have a look at. Thanks in advance for any help you can provide!

I've gone through these references and the code seems quite similar to what is written here but alas it still isn't working

-https://www.twilio.com/blog/screen-sharing-javascript-twilio-programmable-video

-https://www.twilio.com/blog/2018/01/screen-sharing-twilio-video.html

export default function useScreenShareToggle() {
  const { room, onError } = useVideoContext();
  const [isSharing, setIsSharing] = useState(false);
  const stopScreenShareRef = useRef<() => void>(null!);

  const shareScreen = useCallback(() => {
    navigator.mediaDevices
      .getDisplayMedia({
        audio: false,
        video: {
          frameRate: 10,
          height: 1080,
          width: 1920,
        },
      })
      .then(stream => {
        const track = stream.getTracks()[0];

        // All video tracks are published with 'low' priority. This works because the video
        // track that is displayed in the 'MainParticipant' component will have it's priority
        // set to 'high' via track.setPriority()
        room.localParticipant
          .publishTrack(track, {
            name: 'screen', // Tracks can be named to easily find them later
            priority: 'low', // Priority is set to high by the subscriber when the video track is 
rendered
          } as MediaStreamTrackPublishOptions)
          .then(trackPublication => {
            stopScreenShareRef.current = () => {
              room.localParticipant.unpublishTrack(track);
              // TODO: remove this if the SDK is updated to emit this event
              room.localParticipant.emit('trackUnpublished', trackPublication);
              track.stop();
              setIsSharing(false);
            };

            track.onended = stopScreenShareRef.current;
            setIsSharing(true);
          })
          .catch(onError);
      })
      .catch(error => {
        // Don't display an error if the user closes the screen share dialog
        if (error.name !== 'AbortError' && error.name !== 'NotAllowedError') {
          onError(error);
        }
      });
  }, [room, onError]);

  const toggleScreenShare = useCallback(() => {
    !isSharing ? shareScreen() : stopScreenShareRef.current();
  }, [isSharing, shareScreen, stopScreenShareRef]);

  return [isSharing, toggleScreenShare] as const;
}
Digglit
  • 576
  • 1
  • 4
  • 11
  • Did you find any solution? I am also, stuck in this problem. – Skaranjit Jul 05 '21 at 14:10
  • @Skaranjit I don't remember the exact solution to this but I do know that it wasn't necessary to unpublish the user's camera track. Hope that helps. – Digglit Jul 07 '21 at 04:19
  • 1
    I actually did figure out the solution to my problem. And as you said, it wasn't necessary to unpublish the user's camera. My issue was an underlying event for `videoTrack` (user's camera) which wasn't removed. When the user shared the screen, there was a listener to capture that which updated the `tracks` state in my component. This replaced the previous user video track with a new one but its events were not removed and it was active in the background. – Skaranjit Jul 07 '21 at 15:05

0 Answers0