Home > Mobile >  Can't set AVCaptureVideoPreviewLayer as diffuse contents in SceneKit on Apple Silicon (M1)
Can't set AVCaptureVideoPreviewLayer as diffuse contents in SceneKit on Apple Silicon (M1)

Time:09-18

I'm trying to show an AVCaptureVideoPreviewLayer on a SceneKit material.

EDIT: The contents of the preview layer comes from my USB connected iOS device.

  1. The code linked below works when I do it with an NSView's layer, so that part should be ok
  2. The whole thing seems to work on Intel, but not Apple Silicon
  3. The entire enter image description here enter image description here

    What have I tried?

    1. Using an NSView's layer - works
    2. Not setting a size on the layer - Metal crash (height is 0, width is 0 - this is expected in Big Sur)
    3. Setting the layer's size to exactly the size derived from the input's ports dimensions - same crash
    4. Making sure the layer isn't shown else where (as you'll see in the code)

    UPDATE: Here's the crash log from running detach in lldb: https://gist.github.com/mortenjust/4470c8e9e8ca1dc3043388b8b2886703

    CodePudding user response:

    I see a crash too but it's a simple nil unwrapping in the following line:

    AVCaptureDevice.devices(for: .muxed).first!
    

    Changing .muxed to .video fixed the crash.


    Then for a better use of resources it's possible to directly use the device as follows:

    @IBAction func start(_ sender: Any) {
        AVCaptureDevice.requestAccess(for: .video) { granted in
            print("granted")
        }
        
        let discoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: .video, position: .front)
        let device = discoverySession.devices.first!
        boxNode.geometry?.firstMaterial?.diffuse.contents = device
    }
    

    Just make sure the view is continuously redrawing so that new video frames are drawn:

    sceneView.rendersContinuously = true
    

    CodePudding user response:

    From the crash log when learn that

    1. The crash was triggered by ASan (see AddressSanitizer)
    2. Under libMTLCapture.dylib which is the library used to debug Metal in Xcode

    I would disable the GPU Frame Capture debug option in Xcode and see if the crash goes away. If it does I would file a Feedback to Apple.

    Thread 2 Crashed:: Dispatch queue: com.apple.root.default-qos
    0   libsystem_kernel.dylib          0x00000001826c0cec __pthread_kill   8
    1   libsystem_pthread.dylib         0x0000000103beb384 pthread_kill   292
    2   libsystem_c.dylib               0x0000000182639864 abort   104
    3   libclang_rt.asan_osx_dynamic.dylib  0x0000000102be5e4c __sanitizer::Abort()   64
    4   libclang_rt.asan_osx_dynamic.dylib  0x0000000102be5788 __sanitizer::Die()   208
    5   libclang_rt.asan_osx_dynamic.dylib  0x0000000102bcf238 __asan::ScopedInErrorReport::~ScopedInErrorReport()   420
    6   libclang_rt.asan_osx_dynamic.dylib  0x0000000102bcd06c __asan::ReportDeadlySignal(__sanitizer::SignalContext const&)   148
    7   libclang_rt.asan_osx_dynamic.dylib  0x0000000102bcc950 __asan::AsanOnDeadlySignal(int, void*, void*)   96
    8   libsystem_platform.dylib        0x0000000182739c44 _sigtramp   56
    9   libMTLCapture.dylib             0x0000000103a96eb0 0x1039e0000   749232
    10  com.apple.SceneKit              0x00000001a0ac1d50 __92-[SCNTextureCoreAnimationSource _resizeLayer:toSize:updateLayer:updateTransform:caRenderer:]_block_invoke   824
    11  libclang_rt.asan_osx_dynamic.dylib  0x0000000102bc7e28 __wrap_dispatch_group_async_block_invoke   188
    12  libdispatch.dylib               0x0000000103b401d8 _dispatch_call_block_and_release   32
    13  libdispatch.dylib               0x0000000103b41d20 _dispatch_client_callout   20
    14  libdispatch.dylib               0x0000000103b58d04 _dispatch_root_queue_drain   1664
    15  libdispatch.dylib               0x0000000103b5939c _dispatch_worker_thread2   140
    16  libsystem_pthread.dylib         0x0000000103be74b8 _pthread_wqthread   216
    17  libsystem_pthread.dylib         0x0000000103be6228 start_wqthread   8
    
  • Related