0

I'm building a video editor that lets you apply a CIFilter to a video. And it works well.

The only problem I'm facing is that when I dismiss the ViewController I get this error:

Unfinished AVAsynchronousVideoCompositionRequest deallocated - should have called finishWithComposedVideoFrame:, finishWithError: or finishCancelledRequest

This error doesn't make the app crash or slower, but when I try to edit another video the preview in the AVPlayer becomes black.

This is my current code:

var mutableComposition = AVMutableVideoComposition()
let exposureFilter = CIFilter.exposureAdjust()

override func viewDidLoad() {
    updateComposition()
}

func updateComposition() {
    mutableComposition = AVMutableVideoComposition(asset: player.currentItem!.asset, applyingCIFiltersWithHandler: { [weak self] request in
    
        guard let self = self else {
            return
        }
        
        self.exposureFilter.inputImage = request.sourceImage.clampedToExtent()
        self.exposureFilter.ev = 5
        let output = self.exposureFilter.outputImage!.cropped(to: request.sourceImage.extent)

        request.finish(with: output, context: nil)
    })
    
    player.currentItem?.videoComposition = mutableComposition
}

If I remove the [weak self] no error it's printed, but it keeps the ViewController in memory when I dismiss it, creating an unwanted memory leak.

El Tecla
  • 37
  • 8
  • 1
    Have you tried pausing the player on `deinit` of the view controller? – Frank Rupprecht Aug 19 '22 at 05:12
  • Hi Frank, I just tried that and it works. The preview doesn't become black and there's no memory leak. Anyway, I'm still getting the error printed (3 times instead of 1) but it doesn't bother me as long as it works. The problem I'm facing now is that if the app enters background, then foreground, I dismiss the VC and then I try to edit another video, the AVPlayer becomes black again. Did I explain well? – El Tecla Aug 19 '22 at 17:05
  • Is the player's lifetime also bound to the view controller? Or is it the same player for the other video? – Frank Rupprecht Aug 20 '22 at 15:28
  • I have two view controllers. The first one has a video gallery. And the second one has an AVPlayer for preview and some controls to edit the video. So, I guess the player's lifetime is bound to the view controller. – El Tecla Aug 20 '22 at 19:39
  • @FrankRupprecht I realized the preview it's black because `videoCompositionWithAsset:applyingCIFiltersWithHandler:completionHandler:` is not being executed when the app enters background, then foreground, I dismiss the view controller and then I try to edit another video. Do you have any idea what could be going on? – El Tecla Aug 22 '22 at 21:01
  • Hmm, have you tried to explicitly pause the player when the app is about to enter background? – Frank Rupprecht Aug 24 '22 at 05:28
  • Yes, nothing changed... – El Tecla Aug 28 '22 at 18:14

0 Answers0