                     }
                     break
                 case .auto:
-                    if let vc = videoConnection, let device = self.currentDevice {
+                    if let vc = videoOutput.connection(withMediaType: AVMediaTypeVideo), let device = self.currentDevice {
                         if vc.isVideoMirroringSupported {
                             vc.isVideoMirrored = (device.position == .front)
                         }
                     }
-                    if let pc = previewConnection {
+                    if let pc = self.previewLayer.connection {
                         if pc.isVideoMirroringSupported {
                             pc.isVideoMirrored = true
                             pc.automaticallyAdjustsVideoMirroring = true

        
        let currentOrientation = AVCaptureVideoOrientation.avorientationFromUIDeviceOrientation(UIDevice.current.orientation)
    
        if self.previewLayer.connection.isVideoOrientationSupported {
            self.previewLayer.connection.videoOrientation = currentOrientation
        }

        if let videoOutput = self.videoOutput, let videoConnection = videoOutput.connection(withMediaType: AVMediaTypeVideo) {
            if videoConnection.isVideoOrientationSupported {
                videoConnection.videoOrientation = currentOrientation

    // preview
    
    public func freezePreview() {
        if let previewConnection = self.previewLayer.connection {
            previewConnection.isEnabled = false
        }
    }
    
    public func unfreezePreview() {
        if let previewConnection = self.previewLayer.connection {
            previewConnection.isEnabled = true
        }
    }
    
    // flash and torch
