Image sticking Issue when using Broadcast Upload Extension

I am testing Broadcast Upload Extension under iOS11.4.1 or 12.0 with iPad6.

After extracting the YUV data from CMSampleBufferRef, and saving those data to a file, I get some bad images. The issue seems like that the last frame image still remains on the new frame, and not refreshes. Maybe the data is not ready, when coming out from callback.


Following is my code:

- (void)processSampleBuffer:(CMSampleBufferRef)sampleBuffer withType:(RPSampleBufferType)sampleBufferType
{
    switch (sampleBufferType) {
        case RPSampleBufferTypeVideo:
        {
            CFRetain(sampleBuffer);
            
            size_t bytes = 0;
            char* data = NULL;
            size_t bufwidth, bufheight, bufstride;
            CVPixelBufferRef pixelbuf = CMSampleBufferGetImageBuffer(sampleBuffer);
            CVReturn cr = CVPixelBufferLockBaseAddress(pixelbuf, kCVPixelBufferLock_ReadOnly);
            
            for (size_t i = 0; i < CVPixelBufferGetPlaneCount(pixelbuf); i++)
            {
                bufwidth = CVPixelBufferGetWidthOfPlane(pixelbuf, i);
                bufheight = CVPixelBufferGetHeightOfPlane(pixelbuf, i);
                bufstride = CVPixelBufferGetBytesPerRowOfPlane(pixelbuf,i);
                
                data = (char*)CVPixelBufferGetBaseAddressOfPlane(pixelbuf,i);
                
                if(bufwidth == bufstride)
                {
                    size_t ylen = bufwidth*bufheight;
                    fwrite(data, ylen, 1, _file_yuv);
                }
                else
                {
                    size_t factor = bufstride/bufwidth;
                    bytes = bufwidth * factor;
                    for (j = 0; j < bufheight; j++)
                    {
                        fwrite(data, bytes, 1, _file_yuv);
                        data += bufstride;
                    }
                }
            }
            CVPixelBufferUnlockBaseAddress(pixelbuf, kCVPixelBufferLock_ReadOnly);
            CFRelease(sampleBuffer);
            
        }
            break;
        case RPSampleBufferTypeAudioApp:
            break;
            
        default:
            break;
    }
    
}