Hi
I love open vidu and have it working nicely, except i’m not sure how to go about allowing the publisher to view and hear the subscriber.
So generally the publisher sees themselves and subscriber sees the publisher in my application, so one publisher can be watched by many subscribers.
What i need to do, is have it so it’s a one-on-one where publisher and subscriber can see and speak to each other. How can i go about this?
This is the code i have at the moment:
var OV;
var OVSession;
var OVPublisher;
var OVSubscriber;
var OVUserMedia;
var OVVideoTracks;
var OVCurrentCamera = ‘front’;
var OVFrontCamID;
var OVBackCamID;
var OVAudioInputID;
function stopCam() {
if(OVSession) OVSession.disconnect();
}
function switchCam(){
//console.log(“track”,OV.getUserMedia())
console.log(“CURRENT CAMERA”,OVCurrentCamera)
OVUserMedia = OV.getUserMedia({
videoSource: OVCurrentCamera === ‘front’ ? OVBackCamID : OVFrontCamID,
audioSource: OVAudioInputID
}).then(mediaStream => {
let track = mediaStream.getVideoTracks()[0];
OVPublisher.replaceTrack(track).then(()=>{
console.log("SWITCHED");
OVCurrentCamera = OVCurrentCamera === 'front' ? 'back' : 'front';
}).catch(()=>{
console.log("ERROR")
alert('Could Not Switch Camera');
})
console.log("TRACKS",OVVideoTracks);
}).catch((error)=>{
console.log(“TRACKS ERROR”,error)
})
}
function hostCam(sessionId,token,videoId)
{
OV = new OpenVidu();
OVSession = OV.initSession();
OVSession.on(‘streamCreated’,function (event) {
OVSession.subscribe(event.stream,‘publisher-video’);
})
console.log(sessionId+’ ‘+token+’ '+videoId);
OVSession.connect(token).then(() => {
OVPublisher = OV.initPublisher("publisher-video",{
videoSource:OVFrontCamID ? OVFrontCamID : OVBackCamID,
audioSource:true,
publishAudio:true,
publishVideo:true,
resolution:'1280x720'
});
OVPublisher.once('accessAllowed',(event)=>{
console.log("ACCESS ALLOWED")
// get all devices
let devices = OV.getDevices().then(devices=>{
console.log("AVAIL DEVICES",devices)
for(let d = 0; d < devices.length; d++){
console.log("LABEL ID",devices[d].label,devices[d].deviceId)
if(devices[d].label === 'Back Camera'){
OVBackCamID = devices[d].deviceId;
}else if(devices[d].label === 'Front Camera'){
OVFrontCamID = devices[d].deviceId;
}else if(devices[d].kind === 'audioinput'){
OVAudioInputID = devices[d].deviceId;
}
}
OVPublisher.publishAudio(OVAudioInputID);
console.log("CAM IDS",OVFrontCamID,OVBackCamID,OVAudioInputID)
if(OVFrontCamID){
OVCurrentCamera = 'front';
}else{
OVCurrentCamera = 'back';
}
})
})
OVSession.publish(OVPublisher);
}).catch(error =>{
alert('There was an error connecting to the session: ’ + error.message);
})
}
function joinCam(sessionId,token,videoId)
{
OV = new OpenVidu();
OVSession = OV.initSession();
// when stream is created, show in post
OVSession.on(‘streamCreated’,function (event) {
OVSubscriber = OVSession.subscribe(event.stream,‘vidu-video-’+videoId);
OVSubscriber.subscribeToVideo(true);
OVSubscriber.subscribeToAudio(true);
})
OVSession.connect(token).then(() => {
}).catch(error =>{
alert('There was an error connecting to the session: ’ + error.message);
})
}
function leaveSession() {
if (OVSession) OVSession.disconnect();
}
window.onbeforeunload = function () {
leaveSession();
};