stasel / WebRTC-iOS

A simple native WebRTC demo iOS app using swift
Apache License 2.0
1.11k stars 241 forks source link

Is it possible to flip a camera (back or front) while video streaming? if yes please provide the eg. #115

Closed sachin6216 closed 1 year ago

MostafaALZOUBI1994 commented 2 years ago

i hope anyone post solution

stasel commented 2 years ago

Hi, this is not too hard to do. You can create new video track with the back camera and sent it over instead the front camera video track. you can also enable and disable video tracks during the connection. Good luck.

alistair-gemmell commented 1 year ago

I've been having problems with this, so if anyone could offer pointers I would appreciate it! At the moment I have:

if let localPeerConnection: RTCPeerConnection = self.localPeerConnection{ localPeerConnection.add(self.localStream.videoTracks[0], streamIds: [self.videoTrackDefaultID]) }

The above is similar to how I set up the connection in the first place with the front camera, which works just fine.

If I then iterate over localPeerConnection.senders, I now have three. The first two are the ones I originally added, and the new one: id = videoID, streamIds = ["videoID"] id = audioID, streamIds = ["audioID"] id = 9ce691b7-7ba3-4ca0-8674-5da44ff2c45b, streamIds = ["audioID"]

I can try and removeTrack, but that doesn't actually remove the track - looking at the code this is apparently to be expected, as I'm using UnifiedPlan semantics, rather than PlanB semantics

alistair-gemmell commented 1 year ago

As always, I managed to solve this (potentially in a bad way) shortly after asking for help. What I've done for the moment is get the RTCPeerConnection, find the matching entry in the connection's senders, and then set the track of that sender to the new video track. I then tell the connection to restartIce() and that seems to work OK!