#ifndef AVCLOCK_H
#define AVCLOCK_H

#include "ffmpeg_compat.h"

class AVClock
{
public:
    AVClock()
        : m_pts(0.0)
        , m_drift(0.0)
    {}

    inline void reset()
    {
        m_pts = 0.0;
        m_drift = 0.0;
    }

    inline void setClock(double pts) { setCloctAt(pts); }

    inline double getClock() { 
        // 使用高精度时间计算，减少累积误差
        double currentTime = av_gettime_relative() / 1000000.0;
        return m_drift + currentTime; 
    }

private:
    inline void setCloctAt(double pts)
    {
        // 使用高精度时间设置，减少设置时的精度损失
        double currentTime = av_gettime_relative() / 1000000.0;
        m_drift = pts - currentTime;
        m_pts = pts;
    }

    double m_pts;
    double m_drift;
};

#endif // AVCLOCK_H
