iOS Development

ios – App freezes when calling AVAssetExportSession.cancelExport() utilizing customVideoCompositorClass

Spread the love

I am exporting video utilizing AVMutableVideoComposition and customVideoCompositorClass which works nice. However by cancelling the AVAssetExportSession by way of cancelExport() my app goes hanging and never responding in any respect.

func cancel() {
  exportSession = nil

I attempted to bypass the customVideoCompositorClass for testing functions and was capable of cancel the export as supposed. No hangs, every part clean. Nevertheless I am unable to bypass the customized compositor because it handles all of the drawing logic.

Right here is the configuration of AVMutableVideoComposition:

let mainComposition = AVMutableVideoComposition()
mainComposition.customVideoCompositorClass = CustomVideoCompositor.self
mainComposition.frameDuration = // time vary
mainComposition.renderSize = // render dimension
mainComposition.directions = [ ... ]

and that is the interior a part of customized video compositor which I borrowed from AVCustomEdit repository in GitHub:


  personal var renderingQueue = DispatchQueue(label: "com.identifier.CustomVideoCompositor.RenderingQueue")
  personal var shouldCancelAllRequests = false

  func startRequest(_ request: AVAsynchronousVideoCompositionRequest) {
    autoreleasepool {
      renderingQueue.async {
        if self.shouldCancelAllRequests {

        guard let body = self.newRenderedPixelBuffer(forRequest: request) else {
          request.end(with: errorForInvalidPixelBuffer)

        request.end(withComposedVideoFrame: body)

  func cancelAllPendingVideoCompositionRequests() {
    // Pending requests will name `finishCancelledRequest`, these already rendering
    // will name `finishWithComposedVideoFrame`.
    renderingQueue.sync {
      shouldCancelAllRequests = true

    // Begin accepting requests once more.
    renderingQueue.async { [self] in
      shouldCancelAllRequests = false

So far as perceive my customized compositor needs to be triggered with cancelAllPendingVideoCompositionRequests() technique when calling AVAssetExportSession.cancelExport() which ought to cancel all of the upcoming frames and find yourself with request.finishCancelledRequest(). However in apply I could not even hit the debug breakpoint on this line.

Any ideas on this very unusual behaviour?

Leave a Reply

Your email address will not be published. Required fields are marked *