3

我有下面的代码,它以 30fps 的速度捕获 jpeg 帧并以 mp4 格式记录视频。我正在尝试将 processFrame 方法包装在 dispatch_async 调用中,以便录制过程不会锁定视频播放器。问题是我收到了 2 级内存警告,应用程序最终会在几秒钟后崩溃。我可以看到 dispatch_async 方法将队列加载到内存中,因为它试图将每一帧附加到录制的视频输出中,并且在 30fps 时,它没有足够的时间来处理帧并释放使用的内存。我尝试使用 dispatch_after 来延迟 processFrame 的执行,但它没有帮助。有任何想法吗?我应该以不同的方式做这件事吗?

这个方法每秒被调用大约 30 次。

//Process the data sent by the server and send follow-up commands if needed
-(void)processServerData:(NSData *)data{    

    //render the video in the UIImage control 
    UIImage *image =[UIImage imageWithData:data];
    imageCtrl.image = image;


        //record the frame in the background
        dispatch_async(recordingQueue,^{[self processFrame:image];});
    }
}

处理框架方法

//function for processing each frame for recording
-(void) processFrame:(UIImage *) image {

    if (myRecorder.frameCounter < myRecorder.maxFrames)
    {
        if([myRecorder.writerInput isReadyForMoreMediaData])
        {
            CMTime frameTime = CMTimeMake(1, myRecorder.timeScale);
            CMTime lastTime=CMTimeMake(myRecorder.frameCounter, myRecorder.timeScale); 
            CMTime presentTime=CMTimeAdd(lastTime, frameTime);

            buffer = [Recorder pixelBufferFromCGImage:image.CGImage size:myRecorder.imageSize];

            if(buffer)
            {
                [myRecorder.adaptor appendPixelBuffer:buffer withPresentationTime:presentTime];

                myRecorder.frameCounter++;

                CVBufferRelease(buffer);

                if (myRecorder.frameCounter==myRecorder.maxFrames)
                {
                    [myRecorder finishSession];

                    myRecorder.frameCounter=0;
                    myRecorder.isRecording = NO;
                }
            }
            else
            {
                NSLog(@"Buffer is empty");
            }
        }
        else
        {
            NSLog(@"adaptor not ready frameCounter=%d ",myRecorder.frameCounter );
        }
    }

}
4

1 回答 1

3

解决了!我发现我可以使用 dispatch_async_semaphore 来防止队列因过多的请求而过载,以至于它没有足够的时间来释放分配的资源。

这是我更新的代码:

    long success = dispatch_semaphore_wait(recordingSemaphore, DISPATCH_TIME_FOREVER);

    if (success != 0 )
    {
        NSLog(@"Frame skipped");
    }
    else
    {
        dispatch_async(recordingQueue,^{
            dispatch_semaphore_signal(recordingSemaphore);
            [self processFrame:image];
        });

    }

dispatch_semaphore 在我的代码中的某处创建。在这里,我告诉信号量先接受最多 50 个请求,并在接受更多请求之前完成处理。

dispatch_semaphore_t recordingSemaphore =  dispatch_semaphore_create((long) 50); //so far stable at 50
于 2011-04-05T23:18:41.240 回答