Dear All,
I am using Openvidu browser 2.16.0. I am publishing screen using following code.
var OV;
var session;
var publisher;
var screenShared = true;
var mySessionId = document.getElementById(“sessionId”).value;
OV = new OpenVidu();
session = OV.initSession();
session.on(“streamCreated”, function (event) {
session.subscribe(event.stream, “subscriber”);
});
getToken(mySessionId).then(token => {
session.connect(token).then(() => {
publisher = OV.initPublisher(“publisher”, { videoSource: “screen” , publishAudio: false,});
publisher.on(‘accessAllowed’, (event) => {
console.warn(‘ScreenShare: Access allowed’);
publisher.stream.getMediaStream().getVideoTracks()[0].addEventListener(‘ended’, () => {
console.log(‘User pressed the “Stop sharing” button’);
});
session.publish(publisher);
});
publisher.on(‘accessDenied’, (event) => {
console.warn(‘ScreenShare: Access Denied’);
});
})
.catch(error => {
console.log(“There was an error connecting to the session:”, error.code, error.message);
});
});
I have written toggle screen function in following way -
$("#toggleScreenShare").click( function(){
if(!screenShared){
session.publish(publisher);
}else{
session.unpublish(publisher);
}
screenShared = !screenShared;
});
My problem is that when I unpublish the screen and publish it again. The local video is not getting displayed while the remote party can see the shared screen. I could not find any error in console.
@vipin_mishra, I tried to call following code before publishing again
session.on(“streamCreated”, function (event) {
subscriber = session.subscribe(event.stream, “subscriber”);
});
But it did not work. Is there any other way to subscribe to local stream specifically ?
Meanwhile, I figured out that adding following line before publishing again does the trick for second time-
publisher.addVideoElement(videoElement); // video element is a video html element
Now if I again try to toggle screen share I get following error-
@vipin_mishra I figured out that I have call following code every time before publishing the screen
publisher = OV.initPublisher(“publisher”, { videoSource: “screen” , publishAudio: false,});
publisher.on(‘accessAllowed’, (event) => {
console.warn(‘ScreenShare: Access allowed’);
publisher.stream.getMediaStream().getVideoTracks()[0].addEventListener(‘ended’, () => {
console.log(‘User pressed the “Stop sharing” button’);
});
session.publish(publisher);
});
publisher.on(‘accessDenied’, (event) => {
console.warn(‘ScreenShare: Access Denied’);
});