# Switch Camera Track Count Is Wrong On iPhone

**URL:** <https://openvidu.discourse.group/t/switch-camera-track-count-is-wrong-on-iphone/2577>\
**Category:** Issues developing apps\
**Tags:** v2\
**Created:** [January 20, 2021, 2:40pm UTC](https://openvidu.discourse.group/t/switch-camera-track-count-is-wrong-on-iphone/2577 "2021-01-20T14:40:00Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Glen\_Elkins](https://yyz2.discourse-cdn.com/free1/user_avatar/openvidu.discourse.group/glen_elkins/32/1274_2.png) [@Glen\_Elkins](https://openvidu.discourse.group/u/Glen_Elkins)\
**Post date:** [January 20, 2021, 2:40pm UTC](https://openvidu.discourse.group/t/switch-camera-track-count-is-wrong-on-iphone/2577/1 "2021-01-20T14:40:00Z")

</div>

Hi

I love open vidu, but i’m having a strange issue. I have followed this: [OpenVidu Docs](https://docs.openvidu.io/en/2.12.0/advanced-features/switch-camera/)

I loaded up on my iphone, and after getting the tracks array there is only showing up, my iphone has 2 cameras.

Can i please get some help on switching cameras? Here is the basic code i am using to detect the tracks and do switch on button press:

OVSession.connect(token).then(() =\> {

```
OVUserMedia = OV.getUserMedia({
  audioSource: false
}).then(mediaStream => {

  OVVideoTracks = mediaStream.getVideoTracks();
  console.log("MEDIA STREAM",mediaStream);
});
console.log("USER MEDIA",OVUserMedia)
OVPublisher = OV.initPublisher("publisher-video");
OVSession.publish(OVPublisher);

```

}).catch(error =\>{  
alert('There was an error connecting to the session: ’ + error.message);  
})

OVVideoTracks has only got 1element in it instead of two?

Also, when i actually tell it to switch the track to index 0, the video just goes black?

OVPublisher.replaceTrack(OVVideoTracks[0]).then(() =\> {  
console.log(“SWITCHED TRACK”);  
//OVCurrentCamera = 0;  
}).catch(()=\>{  
console.log(“TRACK SWITCH FAILED”);  
});

This is not a mobile app by the way, it’s a website and loaded up in safari on iphone.

---

<div class="post-metadata">

**Author:** ![vipin\_mishra](https://yyz2.discourse-cdn.com/free1/user_avatar/openvidu.discourse.group/vipin_mishra/32/32_2.png) [@vipin\_mishra](https://openvidu.discourse.group/u/vipin_mishra)\
**Post date:** [January 20, 2021, 4:46pm UTC](https://openvidu.discourse.group/t/switch-camera-track-count-is-wrong-on-iphone/2577/2 "2021-01-20T16:46:05Z")

</div>

hey instead of audiosource: false  
try this ```  
videoSource: true or use both

---

<div class="post-metadata">

**Author:** ![Glen\_Elkins](https://yyz2.discourse-cdn.com/free1/user_avatar/openvidu.discourse.group/glen_elkins/32/1274_2.png) [@Glen\_Elkins](https://openvidu.discourse.group/u/Glen_Elkins)\
**Post date:** [January 20, 2021, 5:01pm UTC](https://openvidu.discourse.group/t/switch-camera-track-count-is-wrong-on-iphone/2577/3 "2021-01-20T17:01:56Z")

</div>

Hi

Thanks. What actually do those settings mean?

Anyway, i tried it, and it has seemed to do something but now i get the following error in the console:

OpenViduError: {“name”:“DEVICE\_ACCESS\_DENIED”,“message”:“TypeError: Type error”}

I’m assuming this means the back camera has not been granted access? Yet it doesn’t ask me to give it access like it does with the front facing when the stream first starts.

---

<div class="post-metadata">

**Author:** ![vipin\_mishra](https://yyz2.discourse-cdn.com/free1/user_avatar/openvidu.discourse.group/vipin_mishra/32/32_2.png) [@vipin\_mishra](https://openvidu.discourse.group/u/vipin_mishra)\
**Post date:** [January 20, 2021, 6:13pm UTC](https://openvidu.discourse.group/t/switch-camera-track-count-is-wrong-on-iphone/2577/4 "2021-01-20T18:13:38Z")

</div>

yes give the access of cameras on first time and its need only once

---

<div class="post-metadata">

**Author:** ![Glen\_Elkins](https://yyz2.discourse-cdn.com/free1/user_avatar/openvidu.discourse.group/glen_elkins/32/1274_2.png) [@Glen\_Elkins](https://openvidu.discourse.group/u/Glen_Elkins)\
**Post date:** [January 21, 2021, 8:05am UTC](https://openvidu.discourse.group/t/switch-camera-track-count-is-wrong-on-iphone/2577/5 "2021-01-21T08:05:02Z")

</div>

That’s what I am saying, when the camera is first accessed the phone asks for permission then the front camera works. Then, I press my button to switch the camera to the back one and it says access denied

---

<div class="post-metadata">

**Author:** ![vipin\_mishra](https://yyz2.discourse-cdn.com/free1/user_avatar/openvidu.discourse.group/vipin_mishra/32/32_2.png) [@vipin\_mishra](https://openvidu.discourse.group/u/vipin_mishra)\
**Post date:** [January 21, 2021, 11:08am UTC](https://openvidu.discourse.group/t/switch-camera-track-count-is-wrong-on-iphone/2577/6 "2021-01-21T11:08:30Z")

</div>

Then something else wrong in your code.  
Without seeing complete code I can’t guess permission problem.  
But I think now your replace track code working. So just solve permission issue.

Thanks  
Regards  
Vipin

---

<div class="post-metadata">

**Author:** ![Glen\_Elkins](https://yyz2.discourse-cdn.com/free1/user_avatar/openvidu.discourse.group/glen_elkins/32/1274_2.png) [@Glen\_Elkins](https://openvidu.discourse.group/u/Glen_Elkins)\
**Post date:** [January 21, 2021, 11:18am UTC](https://openvidu.discourse.group/t/switch-camera-track-count-is-wrong-on-iphone/2577/7 "2021-01-21T11:18:57Z")

</div>

What do yoh mean? I don’t have any permission code. The phone asks for permission automatically when the publisher feed is started. When the switch camera code is run it doesn’t then ask for permission again it just shows that error in the console; yet already gave permission when activating the feed.

If it helps, here is the function to connect the publisher:

unction 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");
OVSession.publish(OVPublisher);

```

}).catch(error =\>{  
alert('There was an error connecting to the session: ’ + error.message);  
})  
}

And here is the code to switch the camera, it hits the catch statement with access denied:

function switchCam(){  
[//console.log](https://console.log)(“track”,OV.getUserMedia())

OVUserMedia = OV.getUserMedia({  
audioSource: false,  
videoSource: true  
}).then(mediaStream =\> {

```
OVVideoTracks = mediaStream.getVideoTracks();
console.log("TRACKS",OVVideoTracks);

```

}).catch((error)=\>{  
console.log(“TRACKS ERROR”,error)

```
switch(error.name){
  case 'DEVICE_ACCESS_DENIED':
    console.log("ACCESS DENIED");
    console.log("TRACKS DEVICE",OV.getDevices())
 
    break;
  default:
    console.log("DEFAULT ERROR");
    break;
}

```

})  
}

It’s the getUserMedia() causing the error

---

<div class="post-metadata">

**Author:** ![pabloFuente](https://yyz2.discourse-cdn.com/free1/user_avatar/openvidu.discourse.group/pablofuente/32/2536_2.png) [@pabloFuente](https://openvidu.discourse.group/u/pabloFuente)\
**Post date:** [January 21, 2021, 12:18pm UTC](https://openvidu.discourse.group/t/switch-camera-track-count-is-wrong-on-iphone/2577/8 "2021-01-21T12:18:55Z")

</div>

You are confusing two totally different concepts. The video input devices that your mobile or computer has available have nothing to do with the video stream tracks, which are the video stream feed inside an already initialized MediaStream. These are all basic WebRTC and web API concepts. OpenVidu allows you to list the available devices with method `OpenVidu.getDevices`: [https://docs.openvidu.io/en/latest/api/openvidu-browser/classes/openvidu.html#getdevices](https://docs.openvidu.io/en/latest/api/openvidu-browser/classes/openvidu.html#getdevices)

You initialize a MediaStream (the Stream objectusing one of your input devices). Then that MediaStream will have the desired video stream track.

Regards.

---

<div class="post-metadata">

**Author:** ![Glen\_Elkins](https://yyz2.discourse-cdn.com/free1/user_avatar/openvidu.discourse.group/glen_elkins/32/1274_2.png) [@Glen\_Elkins](https://openvidu.discourse.group/u/Glen_Elkins)\
**Post date:** [January 21, 2021, 12:25pm UTC](https://openvidu.discourse.group/t/switch-camera-track-count-is-wrong-on-iphone/2577/9 "2021-01-21T12:25:36Z")

</div>

I followed your document here [OpenVidu Docs](https://docs.openvidu.io/en/2.12.0/advanced-features/switch-camera/) which at the top to switch camera says to look at the media stream and set the track?

When the stream is first published it just uses the first available camera and asks permission.

If the code in that document at the top doesn’t work how it says, then how am i to make it switch the device without unpublushing the stream? We don’t want to unpublish it, if people are watching the video and we just want to change camera.

---

<div class="post-metadata">

**Author:** ![pabloFuente](https://yyz2.discourse-cdn.com/free1/user_avatar/openvidu.discourse.group/pablofuente/32/2536_2.png) [@pabloFuente](https://openvidu.discourse.group/u/pabloFuente)\
**Post date:** [January 21, 2021, 12:50pm UTC](https://openvidu.discourse.group/t/switch-camera-track-count-is-wrong-on-iphone/2577/10 "2021-01-21T12:50:02Z")

</div>

![image](https://global.discourse-cdn.com/free1/uploads/openvidu/original/2X/4/47b07afc94f344d12c531ea39517fc5b25997b3b.png)

---

<div class="post-metadata">

**Author:** ![pabloFuente](https://yyz2.discourse-cdn.com/free1/user_avatar/openvidu.discourse.group/pablofuente/32/2536_2.png) [@pabloFuente](https://openvidu.discourse.group/u/pabloFuente)\
**Post date:** [January 21, 2021, 12:50pm UTC](https://openvidu.discourse.group/t/switch-camera-track-count-is-wrong-on-iphone/2577/11 "2021-01-21T12:50:53Z")

</div>

If you want to replace the video track, then you need a new video track. You obtain a new video track using native Web API or using OpenVidu.getUserMedia method (which is a simple wrap around it). You can get a new video track from any video input device.

---

<div class="post-metadata">

**Author:** ![Glen\_Elkins](https://yyz2.discourse-cdn.com/free1/user_avatar/openvidu.discourse.group/glen_elkins/32/1274_2.png) [@Glen\_Elkins](https://openvidu.discourse.group/u/Glen_Elkins)\
**Post date:** [January 21, 2021, 12:59pm UTC](https://openvidu.discourse.group/t/switch-camera-track-count-is-wrong-on-iphone/2577/12 "2021-01-21T12:59:06Z")

</div>

So can you help me then, how on earth do we actually switch the camera?

I can get the devices and have some code to determine which is currently being used and then switch the one it’s using, but then how do we then make the stream itself use the other device?

Unpublishable the stream surely won’t work as it will just click off all the subscribers when all I want to do it change which camera on the phone is being used in the same stream . Like when I use FaceTime I can just switch cameras.

---

<div class="post-metadata">

**Author:** ![pabloFuente](https://yyz2.discourse-cdn.com/free1/user_avatar/openvidu.discourse.group/pablofuente/32/2536_2.png) [@pabloFuente](https://openvidu.discourse.group/u/pabloFuente)\
**Post date:** [January 21, 2021, 1:07pm UTC](https://openvidu.discourse.group/t/switch-camera-track-count-is-wrong-on-iphone/2577/13 "2021-01-21T13:07:43Z")

</div>

As the documentation sais, you call method `Publisher.replaceTrack` using an already published Publisher (so it is the same stream that is being currently published). You pass as parameter a new [MediaStreamTrack](https://developer.mozilla.org/en-US/docs/Web/API/MediaStreamTrack) object. How you get a MediaStreamTrack object? You get a new MediaStreamTrack object from a MediaStream object. How you get a MediaStream object? You can get it on your own using native web API or using `OpenVidu.getUserMedia`. Either way, you initilize that new MediaStream with the input devices or any other kind of option that you want for the new video stream track.

I cannot really tell you more than this, it is really not that complex. You have a Publisher object. You change its internal video track by calling Publisher.replaceTrack, providing a different native object MediaStreamTrack.

Best regards.

---

<div class="post-metadata">

**Author:** ![Glen\_Elkins](https://yyz2.discourse-cdn.com/free1/user_avatar/openvidu.discourse.group/glen_elkins/32/1274_2.png) [@Glen\_Elkins](https://openvidu.discourse.group/u/Glen_Elkins)\
**Post date:** [January 21, 2021, 1:20pm UTC](https://openvidu.discourse.group/t/switch-camera-track-count-is-wrong-on-iphone/2577/14 "2021-01-21T13:20:48Z")

</div>

That’s what my code is doing? And as i say when i try OV.getUserMedia() it gives me access denied?

---

<div class="post-metadata">

**Author:** ![pabloFuente](https://yyz2.discourse-cdn.com/free1/user_avatar/openvidu.discourse.group/pablofuente/32/2536_2.png) [@pabloFuente](https://openvidu.discourse.group/u/pabloFuente)\
**Post date:** [January 21, 2021, 1:28pm UTC](https://openvidu.discourse.group/t/switch-camera-track-count-is-wrong-on-iphone/2577/15 "2021-01-21T13:28:53Z")

</div>

Your code is trying to get a new MediaStream using the default video input device (with `videoSource: true`), which is already being used by your first Publisher initialization. Probably your device is not allowing you to do that, and that’s why you get the ACCESS DENIED error. To sum up, I don’t think your switchCam method is actually getting a different video feed. You must pass as videoSource other video input device.

---

<div class="post-metadata">

**Author:** ![Glen\_Elkins](https://yyz2.discourse-cdn.com/free1/user_avatar/openvidu.discourse.group/glen_elkins/32/1274_2.png) [@Glen\_Elkins](https://openvidu.discourse.group/u/Glen_Elkins)\
**Post date:** [January 21, 2021, 1:30pm UTC](https://openvidu.discourse.group/t/switch-camera-track-count-is-wrong-on-iphone/2577/16 "2021-01-21T13:30:05Z")

</div>

Ah so this:

OVUserMedia = OV.getUserMedia({  
audioSource: false,  
videoSource: VIDEO DEVICe ID TO SWITCH TO  
}).then(mediaStream =\> {

So i provide it with the device id i want to use?

---

<div class="post-metadata">

**Author:** ![vipin\_mishra](https://yyz2.discourse-cdn.com/free1/user_avatar/openvidu.discourse.group/vipin_mishra/32/32_2.png) [@vipin\_mishra](https://openvidu.discourse.group/u/vipin_mishra)\
**Post date:** [January 21, 2021, 1:51pm UTC](https://openvidu.discourse.group/t/switch-camera-track-count-is-wrong-on-iphone/2577/17 "2021-01-21T13:51:17Z")

</div>

[https://docs.openvidu.io/en/2.16.0/advanced-features/switch-camera/#by-initializing-a-new-publisher](https://docs.openvidu.io/en/2.16.0/advanced-features/switch-camera/#by-initializing-a-new-publisher)  
Here you will get another methods  
but i think in above code you have to filter your device name and pass different camera track  
Thanks  
Vipin

---

<div class="post-metadata">

**Author:** ![Glen\_Elkins](https://yyz2.discourse-cdn.com/free1/user_avatar/openvidu.discourse.group/glen_elkins/32/1274_2.png) [@Glen\_Elkins](https://openvidu.discourse.group/u/Glen_Elkins)\
**Post date:** [January 21, 2021, 2:29pm UTC](https://openvidu.discourse.group/t/switch-camera-track-count-is-wrong-on-iphone/2577/18 "2021-01-21T14:29:38Z")

</div>

I think it will work.

How would i know when the user has accepted permissions on the device? I ask because before they do the getDevices() method won’t give all the device info so i think i need to hook into an event when the user has allowed permissions.

---

<div class="post-metadata">

**Author:** ![vipin\_mishra](https://yyz2.discourse-cdn.com/free1/user_avatar/openvidu.discourse.group/vipin_mishra/32/32_2.png) [@vipin\_mishra](https://openvidu.discourse.group/u/vipin_mishra)\
**Post date:** [January 21, 2021, 2:36pm UTC](https://openvidu.discourse.group/t/switch-camera-track-count-is-wrong-on-iphone/2577/19 "2021-01-21T14:36:59Z")

</div>

Yes you have to wait till user accept and give permission

---

<div class="post-metadata">

**Author:** ![Glen\_Elkins](https://yyz2.discourse-cdn.com/free1/user_avatar/openvidu.discourse.group/glen_elkins/32/1274_2.png) [@Glen\_Elkins](https://openvidu.discourse.group/u/Glen_Elkins)\
**Post date:** [January 21, 2021, 2:48pm UTC](https://openvidu.discourse.group/t/switch-camera-track-count-is-wrong-on-iphone/2577/20 "2021-01-21T14:48:27Z")

</div>

Got it working thanks!

[Next page](https://openvidu.discourse.group/t/switch-camera-track-count-is-wrong-on-iphone/2577.md?page=2)
