diff --git a/plugins/mac-virtualcam/src/dal-plugin/OBSDALMachClient.mm b/plugins/mac-virtualcam/src/dal-plugin/OBSDALMachClient.mm index 452ee6fbc35ec7..632e0743380dcc 100644 --- a/plugins/mac-virtualcam/src/dal-plugin/OBSDALMachClient.mm +++ b/plugins/mac-virtualcam/src/dal-plugin/OBSDALMachClient.mm @@ -101,14 +101,30 @@ - (void)handlePortMessage:(NSPortMessage *)message break; case MachMsgIdFrame: VLog(@"Received frame message"); - if (components.count >= 4) { + + if (components.count < 4) + return; + + @autoreleasepool { NSMachPort *framePort = (NSMachPort *)components[0]; + + if (!framePort) + return; + IOSurfaceRef surface = IOSurfaceLookupFromMachPort( [framePort machPort]); + mach_port_deallocate(mach_task_self(), + [framePort machPort]); + + if (!surface) { + ELog(@"Failed to obtain IOSurface from Mach port"); + return; + } CVPixelBufferRef frame; CVPixelBufferCreateWithIOSurface(kCFAllocatorDefault, surface, NULL, &frame); + CFRelease(surface); uint64_t timestamp; [components[1] getBytes:×tamp @@ -131,7 +147,6 @@ - (void)handlePortMessage:(NSPortMessage *)message fpsDenominator:fpsDenominator]; CVPixelBufferRelease(frame); - CFRelease(surface); } break; case MachMsgIdStop: diff --git a/plugins/mac-virtualcam/src/dal-plugin/OBSDALStream.mm b/plugins/mac-virtualcam/src/dal-plugin/OBSDALStream.mm index b318f6fb73c874..571eefd70a6f2d 100644 --- a/plugins/mac-virtualcam/src/dal-plugin/OBSDALStream.mm +++ b/plugins/mac-virtualcam/src/dal-plugin/OBSDALStream.mm @@ -397,7 +397,10 @@ - (void)queuePixelBuffer:(CVPixelBufferRef)frame } err = CMSimpleQueueEnqueue(self.queue, sampleBuffer); + if (err != noErr) { + CFRelease(sampleBuffer); + DLog(@"CMSimpleQueueEnqueue err %d", err); return; } diff --git a/plugins/mac-virtualcam/src/obs-plugin/OBSDALMachServer.mm b/plugins/mac-virtualcam/src/obs-plugin/OBSDALMachServer.mm index d2857766ee8721..efd33c1b44beae 100644 --- a/plugins/mac-virtualcam/src/obs-plugin/OBSDALMachServer.mm +++ b/plugins/mac-virtualcam/src/obs-plugin/OBSDALMachServer.mm @@ -147,23 +147,33 @@ - (void)sendPixelBuffer:(CVPixelBufferRef)frame dataWithBytes:&fpsDenominator length:sizeof(fpsDenominator)]; - NSPort *framePort = [NSMachPort - portWithMachPort:IOSurfaceCreateMachPort( - CVPixelBufferGetIOSurface( - frame))]; + IOSurfaceRef surface = CVPixelBufferGetIOSurface(frame); + + if (!surface) { + blog(LOG_ERROR, + "unable to access IOSurface associated with CVPixelBuffer"); + return; + } + + mach_port_t framePort = IOSurfaceCreateMachPort(surface); if (!framePort) { blog(LOG_ERROR, - "unable to allocate mach port for pixel buffer"); + "unable to allocate mach port for IOSurface"); return; } [self sendMessageToClientsWithMsgId:MachMsgIdFrame components:@[ - framePort, timestampData, + [NSMachPort + portWithMachPort:framePort + options:NSMachPortDeallocateNone], + timestampData, fpsNumeratorData, fpsDenominatorData ]]; + + mach_port_deallocate(mach_task_self(), framePort); } }