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;
}