AVVideoCompositionCoreAnimationTool and AVAssetExportSession freezes the app randomly

Hi there,

So I've been banging my head for the last week, because we have an issue in our app where it's freezing on export while the composition has a AVVideoCompositionCoreAnimationTool as animationTool

Before extracting the code to an app I can share, or submit a TSIs, I was wondering if this rings a bell to someone.

Basically, we're not doing something too crazy

We create an AVAssetExportSession from a composition, some audio and some layers to animate.

    func makeExportable(configuration: ExportConfiguration) throws -> AVAssetExportSession {
        if let animationLayer {
            let rect = CGRect(origin: .zero, size: configuration.videoSize)
            let videoAnimationLayer = CALayer()
            videoAnimationLayer.frame = rect

            let videoLayer = CALayer()
            videoLayer.frame = rect
            videoAnimationLayer.addSublayer(videoLayer)
            videoAnimationLayer.addSublayer(animationLayer)
            videoAnimationLayer.isGeometryFlipped = true

            let animationTool = AVVideoCompositionCoreAnimationTool(postProcessingAsVideoLayer: videoLayer,
                                                                    in: videoAnimationLayer)

            avVideoComposition.animationTool = animationTool
        }

        guard
            let session = AVAssetExportSession(asset: avComposition,
                                               presetName: configuration.avAssetExportPresetName) else {
            throw CompositionError.invalidExportSession
        }

        session.audioMix = audioMix
        session.videoComposition = avVideoComposition
        return session)

    }

Then we run await session.export()

If we do that multiple times in a row (there are no specific triggers, I can have a 3s video with 40 CALayers or a 30s with 1500 layers, it could happen on the first export or the 20th, ...), then at some point, the app will just have a severe hang with the following stack trace (if I pause the debug session).

If ever, I remove the avVideoComposition.animationTool = animationTool line from the export session creation, then the app does not hang...

Also, I encounter the issue with the fact that it's crashing on a sim but running fine on a device. Some api_misuse crash (see [here)]. Could this be related, idk...(https://developer.apple.com/forums/thread/133681)

Anyway... Completely stuck here, and surprised that such a thing would have an issue on AVFoundation. We're probably not the onliest using CoreAnimation on a video export...

Thanks!

Marian

Attaching the watch dog crash report

Feels like there is an issue with CoreAnimation

Thread 0 name:   Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0   libsystem_kernel.dylib        	       0x1f646fb48 mach_msg2_trap + 8
1   libsystem_kernel.dylib        	       0x1f6482008 mach_msg2_internal + 80
2   libsystem_kernel.dylib        	       0x1f6482248 mach_msg_overwrite + 388
3   libsystem_kernel.dylib        	       0x1f647008c mach_msg + 24
4   QuartzCore                    	       0x1bb4736b0 CA::Render::Message::send_message() + 84
5   QuartzCore                    	       0x1bb64a2ac CA::Render::Encoder::send_message(unsigned int, unsigned int, unsigned int*, unsigned long) + 940
6   QuartzCore                    	       0x1bb41a16c CA::Context::commit_transaction(CA::Transaction*, double, double*) + 8860
7   QuartzCore                    	       0x1bb44f3c4 CA::Transaction::commit() + 652
8   QuartzCore                    	       0x1bb436408 CA::Transaction::flush_as_runloop_observer(bool) + 88
9   CoreFoundation                	       0x1b9e16070 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 36
10  CoreFoundation                	       0x1b9d9e690 __CFRunLoopDoObservers + 532
11  CoreFoundation                	       0x1b9dfff8c __CFRunLoopRun + 1048
12  CoreFoundation                	       0x1b9e051e4 CFRunLoopRunSpecific + 612
13  GraphicsServices              	       0x1f2c25368 GSEventRunModal + 164
14  UIKitCore                     	       0x1bc2b4d88 -[UIApplication _run] + 888
15  UIKitCore                     	       0x1bc2b49ec UIApplicationMain + 340
16  libswiftUIKit.dylib           	       0x1c27642a0 UIApplicationMain(_:_:_:_:) + 104
17  MyApp                     	           0x1044f5bc8 static UIApplicationDelegate.main() + 104
18  MyApp                     	       	  0x1044f5b50 static AppDelegate.$main() + 44
19  MyApp                     	       	  0x1044f5c4c main + 28
20  dyld                          	       0x1d8129948 start + 2504

I recently came across the same issue in my application。

I've also been struggling to solve this issue and frankly even to recreate the hang consistently. Instruments can capture the hang but it always ends in a similar mach_msg2_trap.

I am using the AnimationTool to add a few CATextLayers on top of my videos - and haven't even thought to trace the issue there. I use it below: videoComposition.animationTool = AVVideoCompositionCoreAnimationTool(postProcessingAsVideoLayer: videoLayer, in: parentLayer)

This is part of a video compilation pipeline that's running in a background thread.

Here's an example thread backtrace when the app is in a hung state:

  • thread #1, queue = 'com.apple.main-thread', stop reason = signal SIGSTOP
    • frame #0: 0x00000001ed345178 libsystem_kernel.dylibmach_msg2_trap + 8 frame #1: 0x00000001ed344f10 libsystem_kernel.dylibmach_msg2_internal + 80 frame #2: 0x00000001ed344e28 libsystem_kernel.dylibmach_msg_overwrite + 436 frame #3: 0x00000001ed344c68 libsystem_kernel.dylibmach_msg + 24 frame #4: 0x00000001a7245f04 QuartzCoreCA::Render::Message::send_message() + 84 frame #5: 0x00000001a72108c4 QuartzCoreCA::Render::Encoder::send_message(unsigned int, unsigned int, unsigned int*, unsigned long) + 868 frame #6: 0x00000001a7255d64 QuartzCoreCA::Context::commit_transaction(CA::Transaction*, double, double*) + 14036 frame #7: 0x00000001a724bb80 QuartzCoreCA::Transaction::commit() + 648 frame #8: 0x00000001a724b828 QuartzCoreCA::Transaction::flush_as_runloop_observer(bool) + 88 frame #9: 0x00000001a7eb1f34 UIKitCore_UIApplicationFlushCATransaction + 52 frame #10: 0x00000001a7eb1a4c UIKitCore_UIUpdateSequenceRun + 84 frame #11: 0x00000001a7eb113c UIKitCoreschedulerStepScheduledMainSection + 144 frame #12: 0x00000001a7eb11f8 UIKitCorerunloopSourceCallback + 92 frame #13: 0x00000001a5c110ac CoreFoundationCFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 28 frame #14: 0x00000001a5c10328 CoreFoundation__CFRunLoopDoSource0 + 176 frame #15: 0x00000001a5c0eadc CoreFoundation__CFRunLoopDoSources0 + 244 frame #16: 0x00000001a5c0d818 CoreFoundation__CFRunLoopRun + 828 frame #17: 0x00000001a5c0d3f8 CoreFoundationCFRunLoopRunSpecific + 608 frame #18: 0x00000001e919b4f8 GraphicsServicesGSEventRunModal + 164 frame #19: 0x00000001a80338a0 UIKitCore-[UIApplication _run] + 888 frame #20: 0x00000001a8032edc UIKitCoreUIApplicationMain + 340 frame #21: 0x00000001aaa44898 SwiftUI___lldb_unnamed_symbol172585 + 176 frame #22: 0x00000001aaa446dc SwiftUI___lldb_unnamed_symbol172583 + 152 frame #23: 0x00000001aa6b525c SwiftUI___lldb_unnamed_symbol143448 + 128 frame #24: 0x0000000103216690 appstatic MainApp.$main(self=app.MainApp) at <compiler-generated>:0 frame #25: 0x0000000103216740 appmain at MainApp.swift:17:8 frame #26: 0x00000001c8962dcc dyld`start + 2240

Has anyone found an alternate method for adding text overlays to videos? When disabling the AnimationTool layer I haven't been able to get it to crash - but those layers are pretty important to our app =).

Thank you for finding this issue - I was going crazy tracking it down. But now I need to find a work-around!

I'm still stuck here, any one found solution?

AVVideoCompositionCoreAnimationTool and AVAssetExportSession freezes the app randomly
 
 
Q