From 5f6df6340126fe9694a156ade658eead86f79855 Mon Sep 17 00:00:00 2001 From: Developer-Ecosystem-Engineering <65677710+Developer-Ecosystem-Engineering@users.noreply.github.com> Date: Thu, 2 Jun 2022 15:33:29 -0700 Subject: [PATCH] Buffers in pool may not always point at the Metal video compositors output We received a report of black frames in an applications video content once it was encoded with NextLevel. Investigation revealed a similar issue as https://github.com/NextLevel/NextLevelSessionExporter/issues/38. It appears NextLevelSessionExporter is pulling buffers from the pool that was created, and sending those buffers back to the API. The assumption a buffer pulled from that pool will contain the output of the Metal video compositor is not always correct. This change will pass the pixelBuffer directly, or use the toBuffer if a renderHandler exists. --- Sources/NextLevelSessionExporter.swift | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/Sources/NextLevelSessionExporter.swift b/Sources/NextLevelSessionExporter.swift index b56338f..0f55d7e 100644 --- a/Sources/NextLevelSessionExporter.swift +++ b/Sources/NextLevelSessionExporter.swift @@ -453,11 +453,21 @@ extension NextLevelSessionExporter { let result = CVPixelBufferPoolCreatePixelBuffer(kCFAllocatorDefault, pixelBufferPool, &toRenderBuffer) if result == kCVReturnSuccess { if let toBuffer = toRenderBuffer { - self._renderHandler?(pixelBuffer, self._lastSamplePresentationTime, toBuffer) - if pixelBufferAdaptor.append(toBuffer, withPresentationTime:self._lastSamplePresentationTime) == false { - error = true + // if _renderHandler is nil, then we can save time by just passing pixelBuffer directly to pixelBufferAdaptor.append + if (self._renderHandler == nil) { + if pixelBufferAdaptor.append(pixelBuffer, withPresentationTime:self._lastSamplePresentationTime) == false { + error = true + } + handled = true + } + // otherwise, we need to call into _renderHandler to preprocess the frames + else { + self._renderHandler?(pixelBuffer, self._lastSamplePresentationTime, toBuffer) + if pixelBufferAdaptor.append(toBuffer, withPresentationTime:self._lastSamplePresentationTime) == false { + error = true + } + handled = true } - handled = true } } }