How To Have Publisher View Subscriber?

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

If you follow the first tutorial you will get a 1 to 1 call, right?

https://docs.openvidu.io/en/2.17.0/tutorials/openvidu-hello-world/

What is the issue with that?

Looking at that doesn’t look anything different to what I’ve done, subscribers join the punisher. I want the publisher to be able to see subscriber and talk to them. So be one publisher and one subscriber on a call to each other but I don’t follow how to allow the publisher to see and hear the subscriber instead of themselves. What is it specifically I have to do to allow this to happen?

If you want a publisher to see to a subscriber, the subscriber has to be converted to a publisher (invoking publishing code) and the original publisher has to be converted to a subscriber (this is done in the tutorial just subscribing to the event streamCreated

session.on(‘streamCreated’, function (event) {
session.subscribe(event.stream, ‘subscriber’);
});

Can you give me an example? I just don’t follow at all how to do this. If i am a publisher i create the stream and publish as publisher, but then i do the same for the subscriber? So how does the subscriber see the publisher if they are a publisher as well???

You need to understand the concepts of Publisher and Subscriber.
OpenVidu has Sessions. A Session is a virtual room where users can connect. A user connecting to a Session has a predefined role depending on the capabilities you want for him or her: see OpenVidu Role documentation.

If a user has a Subscriber role, then the only thing he can do regarding media is to subscribe to other user’s streams. If a user has a Publisher role, he can subscribe to other user’s streams, but also to publish his own streams. If a user has a Moderator role, he can do everything a Subscriber and a Publisher can, but also he can manage other user’s connections and streams.

That is regarding user roles. Now, using openvidu-browser library on the client side, you have 2 concepts that are also named Subsriber and Publisher. You create these objects to subscribe to some other user’s stream, or tu publish your own stream. This is clearly explained in OpenVidu Docs: Subscribe/Unsubscribe from other user’s stream / Publish/Unpublish a stream.
Besides this, all of the OpenVidu tutorials work in this way, because this is just the core functionality of OpenVidu: receiving and sending media streams. Any user can try calling any openvidu-browser method: subscribing, publishing, forcing disconnections… The only limitation is imposed by their predefined role.