Broadcast upload extension crash when enabling mic

I am working with ReplayKit broadcast extension to record the screen on iOS device. When I start recording with mic off, the entire recording process is working perfectly. But when I enable the mic while starting the broadcast, in some cases the extension crashes with 10 seconds with the below error:


Error code: Thread 7: EXC_RESOURCE RESOURCE_TYPE_MEMORY (limit = 50 MB, unused = 0x0)


I am facing the same error everytime when I start the recording with Mic off and then start the mic from control centre during the broadcast.


Below is crash report:

Device: iPhone 7

OS: iOS 13.3.1

CPU: 15%

Memory: 50.1MB

Threads:


Thread 1 Queue : com.apple.main-thread (serial)
Thread 2 Queue : com.apple.NSXPCConnection.user.endpoint (serial)
#0 0x0000000197e8c1d4 in _platform_memset ()
#1 0x0000000197e6d528 in small_malloc_should_clear ()
#2 0x0000000197e6a260 in szone_malloc_should_clear ()
#3 0x0000000197e73928 in malloc_zone_calloc ()
#4 0x0000000197e7420c in calloc ()
#5 0x0000000197eb7978 in class_createInstance ()
#6 0x0000000198541388 in NSAllocateObject ()
#7 0x000000019843c248 in -[_NSPlaceholderData initWithBytes:length:copy:deallocator:] ()
#8 0x0000000198680e94 in _NSXPCSerializationDataForObject ()
#9 0x000000019866a6a4 in __44-[NSXPCDecoder _decodeArrayOfObjectsForKey:]_block_invoke ()
#10 0x0000000198681e9c in _NSXPCSerializationIterateArrayObject ()
#11 0x0000000198450024 in -[NSXPCDecoder _decodeArrayOfObjectsForKey:] ()
#12 0x000000019844f950 in -[NSDictionary(NSDictionary) initWithCoder:] ()
#13 0x000000019866a248 in _decodeObject ()
#14 0x0000000198443444 in -[NSXPCDecoder _decodeObjectOfClasses:atObject:] ()
#15 0x000000019868c5d4 in _NSXPCSerializationDecodeInvocationObjectOnlyArgumentArray ()
#16 0x000000019849858c in -[NSXPCDecoder __decodeXPCObject:allowingSimpleMessageSend:outInvocation:outArguments:outArgumentsMaxCount:outMethodSignature:outSelector:isReply:replySelector:interface:] ()
#17 0x00000001984988f4 in -[NSXPCDecoder _decodeMessageFromXPCObject:allowingSimpleMessageSend:outInvocation:outArguments:outArgumentsMaxCount:outMethodSignature:outSelector:interface:] ()
#18 0x0000000198482cd0 in -[NSXPCConnection _decodeAndInvokeMessageWithEvent:flags:] ()
#19 0x00000001986681b4 in message_handler ()
#20 0x0000000197d4b3d0 in _xpc_connection_call_event_handler ()
#21 0x0000000197d4b748 in _xpc_connection_mach_event ()
#22 0x0000000197e4b244 in _dispatch_client_callout4 ()
#23 0x0000000197e07b78 in _dispatch_mach_msg_invoke$VARIANT$mp ()
#24 0x0000000197df72d0 in _dispatch_lane_serial_drain$VARIANT$mp ()
#25 0x0000000197e08778 in _dispatch_mach_invoke$VARIANT$mp ()
#26 0x0000000197df72d0 in _dispatch_lane_serial_drain$VARIANT$mp ()
#27 0x0000000197df7e28 in _dispatch_lane_invoke$VARIANT$mp ()
#28 0x0000000197e01314 in _dispatch_workloop_worker_thread ()
#29 0x0000000197e9ab88 in _pthread_wqthread ()
Thread 3Thread 4 Queue : com.replaykit.broadcast.AudioSampleQueue (serial)
Thread 5 Queue : com.replaykit.broadcast.VideoSampleQueue (serial)
Thread 7com.apple.uikit.eventfetch-thread (8)Thread 10


Any advice or suggestion will be greatful.

Replies

What is you code to save audio baffer from microfone, seems you have reached the memory limit of your app extension

Yes, the app extension sometimes reaches its memory limit in 5-10 sec after I start the recording with mic on.


switch type {
        case RPSampleBufferType.video:
            // Handle video sample buffer
            if activeAssetWriter.inputs[0].isReadyForMoreMediaData {
                activeAssetWriter.inputs[0].append(buffer)
            }
        case RPSampleBufferType.audioApp:
            // Handle audio sample buffer for app audio
            if activeAssetWriter.inputs[1].isReadyForMoreMediaData {
                activeAssetWriter.inputs[1].append(buffer)
            }
        case RPSampleBufferType.audioMic:
            // Handle audio sample buffer for mic audio
            if activeAssetWriter.inputs[2].isReadyForMoreMediaData {
                activeAssetWriter.inputs[2].append(buffer)
            }
        @unknown default:
            // Handle other sample buffer types
            fatalError("Unknown type of sample buffer")
        }


I am simply writing the buffer data to AVAssetWriterInput.


From the log I understood that the audio buffer queue fills up to 40mb in just 2-3 seconds that leads to this crash.

I have the same crash... The part with "everytime when I start the recording with Mic off and then start the mic from control centre during the broadcast." is easy to reproduce because it happens even without having any code in processSampleBuffer or anywhere in the extension. Also using an iPhone 7 with 13.4.1.