2

What I Did:-

I have tried to enable stabilization and HDR but it's not working.I think I was in right path.When I was trying to check that the current device supports stabilization and HDR in that both case I had got only false case in all devices.

Please guide me if any mistakes had done in the below code snippet.

Thanks in advance!!

My Code Snippet:-

func createAVSession() throws -> AVCaptureSession {
        AppLog.LogFunction(object: LOG_Start)

        // Start out with low quality
        let session = AVCaptureSession()
        session.sessionPreset = AVCaptureSessionPresetPhoto

        // Input from video camera
        let device = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
        let currentFormat = device?.activeFormat.isVideoHDRSupported
        try device?.lockForConfiguration()
        if device?.activeFormat.isVideoHDRSupported == true {
            device?.automaticallyAdjustsVideoHDREnabled = false
            device?.isVideoHDREnabled = true
            print("device?.isVideoHDREnabled\(device?.isVideoHDREnabled)")
        }

        if (device?.isFocusModeSupported(.continuousAutoFocus))! {
            device?.focusMode = AVCaptureFocusMode.continuousAutoFocus
            print("device?.focusMode\(device?.focusMode.rawValue)")
        }
        if (device?.isSmoothAutoFocusSupported)! {
            device?.isSmoothAutoFocusEnabled = true
            print("device?.isSmoothAutoFocusEnabled\(device?.isSmoothAutoFocusEnabled)")
        }

        if (device?.isExposureModeSupported(.continuousAutoExposure))! {
            device?.exposureMode = .continuousAutoExposure
            print("device?.exposureMode\(device?.exposureMode.rawValue)")
        }
        device?.unlockForConfiguration()

        let input = try AVCaptureDeviceInput(device: device)
        do {
            try input.device.lockForConfiguration()
            input.device.activeVideoMaxFrameDuration =  CMTimeMake(1, 30)
            input.device.activeVideoMinFrameDuration =  CMTimeMake(1, 30)
            input.device.unlockForConfiguration()
        }
        catch {
            print("Failed to set FPS")
        }

        // Output
        let videoOutput = AVCaptureVideoDataOutput()

        videoOutput.videoSettings = [ kCVPixelBufferPixelFormatTypeKey as AnyHashable: kCVPixelFormatType_32BGRA]
        videoOutput.alwaysDiscardsLateVideoFrames = true
        videoOutput.setSampleBufferDelegate(self, queue: sessionQueue)

        let stillImageOutput: AVCaptureStillImageOutput = AVCaptureStillImageOutput()
        stillImageOutput.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG]

        //stillImageOutput.isHighResolutionStillImageOutputEnabled = true

        if stillImageOutput.isStillImageStabilizationSupported {
            stillImageOutput.automaticallyEnablesStillImageStabilizationWhenAvailable = true
            print("stillImageOutput.isStillImageStabilizationActive\(stillImageOutput.isStillImageStabilizationActive)")
        }

        // Join it all together
        session.addInput(input)
        session.addOutput(videoOutput)

      if session.canAddOutput(stillImageOutput) {
            session.addOutput(stillImageOutput)
          self.stillImageOutput = stillImageOutput

      }



        if let connection = videoOutput.connection(withMediaType: AVMediaTypeVideo) {
            if connection.isVideoOrientationSupported {
                connection.videoOrientation = .portrait
            }
            if connection.isVideoStabilizationSupported {
                connection.preferredVideoStabilizationMode = .standard
                print("connection.activeVideoStabilizationMode\(connection.activeVideoStabilizationMode.rawValue)")
            }
        }


        AppLog.LogFunction(object: LOG_End)
        return session
    }
Murugesh
  • 59
  • 1
  • 9
  • I face the same issue. Within captureDevice.formats none of items has format.isVideoHDRSupported == true – Modo Ltunzher Jan 16 '18 at 19:54
  • I have the same issue. Have either of you found any resolution to the problem. I can get stabilization to work only so long as I do not use AVCaptureVideoDataOutputSampleBufferDelegate and so it more traditionally, but I have to use the delegate for other functions in my app – Joyful Machines Apr 18 '18 at 20:40

1 Answers1

0

What worked for me on the stabilization issue was to test for it in the delegate. In my project, I use the AVCaptureVideoDataOutputSampleBufferDelegate to write to my file as I test for certain things in the pixel buffer before I decide to write. It was the one place I found where it would say stabilization is allowed. Anyway, here is how I did it for the stabilization issue. Hope it helps.

func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, from connection: AVCaptureConnection!){
    self.lockQueue.sync {
        if !self.isCapturing || self.isPaused {
            return
        }
        
        let isVideo = captureOutput is AVCaptureVideoDataOutput
        
        if isVideo && self.videoWriter == nil {
            // testing to make sure dealing with video and not audio

            let connection = captureOutput.connection(withMediaType: AVMediaTypeVideo)
          
            if (connection?.isVideoStabilizationSupported)! {
                connection?.preferredVideoStabilizationMode = AVCaptureVideoStabilizationMode.cinematic
            }
            
            //other work excluded as irrelevant
        }
    }
}
Vukašin Manojlović
  • 3,717
  • 3
  • 19
  • 31
Joyful Machines
  • 120
  • 2
  • 8