Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Video not streamed when switch the camera from front to back or Vice versa #85

Open
sachinpatel3 opened this issue Jul 17, 2024 · 0 comments

Comments

@sachinpatel3
Copy link

Here is the function for toggle camera:-
public func toggleCamera() {
do {
// Access device manager cameras directly, catching any potential nil
let availableCameras = try sharedState.deviceManager!.cameras

      // Define scaling mode for the preview view
      let scalingMode: ScalingMode = .crop

      // Initialize local video streams if nil
      if (sharedState.localVideoStreams == nil) {
          sharedState.localVideoStreams = [LocalVideoStream]()
      }
    if(sharedState.isUsingFrontCamera){
      // Create and append local video stream from first available camera
      let localVideoStream = LocalVideoStream(camera: availableCameras.first{ $0.cameraFacing == .back }!)
      sharedState.localVideoStreams!.append(localVideoStream)
      
      // Create preview renderer and view for the local video stream
      sharedState.previewRenderer = try VideoStreamRenderer(localVideoStream: sharedState.localVideoStreams!.first!)
      sharedState.previewView = try sharedState.previewRenderer!.createView(withOptions: CreateViewOptions(scalingMode: scalingMode))
      sharedState.isUsingFrontCamera.toggle()
    }else{
      let localVideoStream = LocalVideoStream(camera: availableCameras.first{ $0.cameraFacing == .front }!)
      sharedState.localVideoStreams!.append(localVideoStream)
      
      // Create preview renderer and view for the local video stream
      sharedState.previewRenderer = try VideoStreamRenderer(localVideoStream: sharedState.localVideoStreams!.first!)
      sharedState.previewView = try sharedState.previewRenderer!.createView(withOptions: CreateViewOptions(scalingMode: scalingMode))
      sharedState.isUsingFrontCamera.toggle()
    }

      // Start video stream with error handling
      sharedState.call!.startVideo(stream: sharedState.localVideoStreams!.first!) { (error) in
          if (error != nil) {
              sharedState.alertMessage = "Could not share video"
              sharedState.showAlert = true
          }
          else {
              sharedState.sendingLocalVideo = true
          }
      }
  } catch let error {
      // Handle any errors caught during the process
      sharedState.alertMessage = "Error toggling camera: \(error.localizedDescription)"
      sharedState.showAlert = true
  }

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant