Publishing audio only when there is no camera

Hi

Started using openVidu without problems so far. However, I have one question regarding audio only session. If a user has no camera I expect that session.connect will throw an error. What I would want to do then is publish audio only, since most users at least have a microphone - otherwise it might as well break down.

Would the correct way of handling this be to use the error bracket below and call Publisher.publishVideo(false); when the error is “missing video” and after that call session.publish(publisher); ?

And if I do, would I have to do anything on the subscriber side, or would the JS just show an empty frame and play sound for the subscriber in the other end? Using the basic Hello world code as a boilerplate.

Hope the question isn’t too specific, but I guess this is a common situation since many users will connect without having a camera available.

// connect to session
session.connect(token)
.then(() => {
var publisher = OV.initPublisher(“publisher”);
session.publish(publisher);
})
.catch(error => {
console.log(“There was an error connecting to the session:”, error.code, error.message);
});

You can initialize the publisher with specifics properties.
For example:

properties = {
    publishAudio: true,
    publishVideo: false,
}
OV.initPublisher("targetElement", properties)

You have more information about that in OpenVidu API

Yes, I have used properties for height and width. But in this case I don’t know whether I should set publishVideo: false until I have checked if the user does have a camera. Was wondering if this would be the right place and if the error clause would be activated if the user does not have a camera and I try to publish with the setting publishVideo: true.

Another solution would of course be to check that before OT.initPublisher.

Not sure which one is the proper way of doing it. But if I use the second option, how would I check for the camera? I don’t really know until the user has pushed the button ‘allow access to camera and microphone’ until their browser asks them to and that happens after initPublisher I guess.

Hi,

You can always check what devices the user has available before performing any other action. You can use native Web API or OpenVidu.getDevives method (https://openvidu.io/api/openvidu-browser/classes/openvidu.html#getdevices)

That way you will know what properties to provide to OpenVidu.initPublisher method

Ah, that’s perfect. It seems to work anyway, but it does trow an error in the console

“DOMException getMediaError @ openvidu-browser-2.1…n.js:formatted:3310”

This way I can avoid that also.

Thankyou very much,

Mattias