Search code examples
ioscameraframecmsamplebuffer

what methods of SampleBuffer do I must use for measurement of frame rate?


I work with AvFoundation. I need to measure accurately frame rate from ios camera.

Algoritm:

Frame rate = 1/(time(f2)-time(f1)) = __ (frame per second);

Where time(f2) – it's time when come second frame, and (f1) – it's time of first frame. How do it with using sampleBuffer?


Solution

  • You need to call CMSampleBufferGetPresentationTimeStamp(sampleBuffer)

    Something like this (in swift, a little awkward because I couldn't find a CMTime 1/x):

    let delta = CMTimeSubtract(CMSampleBufferGetPresentationTimeStamp(buf2), CMSampleBufferGetPresentationTimeStamp(buf1))
    
    // awkward 1/x, beware that delta.value may overflow as a timescale
    // what's the right way?
    let frameRate = CMTime(value: CMTimeValue(delta.timescale), timescale: CMTimeScale(delta.value))
    
    // maybe you want floating point instead of CMTime:
    let frameRateAsFloat64 = CMTimeGetSeconds(frameRate)