音视频项目—基于FFmpeg和SDL的音视频播放器解析(十二)

介绍

在本系列,我打算花大篇幅讲解我的 gitee 项目音视频播放器,在这个项目,您可以学到音视频解封装,解码,SDL渲染相关的知识。您对源代码感兴趣的话,请查看基于FFmpeg和SDL的音视频播放器

如果您不理解本文,可参考我的前一篇文章音视频项目---基于FFmpeg和SDL的音视频播放器解析(十一)

解析

我们今天要讲的和音视频同步有关,其中 async 主要负责时间的控制,未来 audiooutput 和 videooutput 这两个负责播放音频和视频的文件就依赖其实现音视频同步。

我们先看看 async 的代码

cpp 复制代码
#ifndef AVSYNC_H_
#define AVSYNC_H_

#include<chrono>
#include<ctime>
#include<time.h>
#include<math.h>
using namespace std::chrono;


class AVSync
{
public:
    AVSync();
    void InitClock(){
        
    }
    void SetClockAt(double pts, double time){
        this->pts = pts;
        pts_drift = this->pts - time;
    }
    double GetClock(){
        double time = GetMicroseconds() / 1000000.0;
        return pts_drift + time;
    }
    double SetClock(){
        double time = GetMicroseconds() / 1000000.0;
        SetClockAt(pts, time);
    }
    time_t GetMicroseconds(){
        system_clock::time_point time_point_new = system_clock::now();
        system_clock::duration duration = time_point_new.time_since_epoch();
        time_t us = duration_cast<microseconds>(duration).count();
        return us;
    }
    double pts = 0;
    double pts_drift = 0;
};


#endif

这个代码量不大,成员变量有 pts,pts_drift 这两个。成员函数主要是 GetMicroseconds,SetClock,GetClock,SetClockAt,我们接下来逐步解析。

我们先说成员变量的含义。pts(presentation timestamp),了解音视频的朋友应该知道,这是显示时间戳,表示帧应该在屏幕显示的时间。pts_drift,当前 pts 与系统时间的差值。

然后,我们看一下函数

GetMicroseconds:
cpp 复制代码
time_t GetMicroseconds(){
      system_clock::time_point time_point_new = system_clock::now();
      system_clock::duration duration = time_point_new.time_since_epoch();
      time_t us = duration_cast<microseconds>(duration).count();
      return us;
}

这个函数负责获取时间的间隔。

首先,第一行,system_clock::time_point time_point_new = system_clock::now(),我们获取了当前的时间。

然后,system_clock::duration duration = time_point_new.time_since_epoch(),通过这个函数我们获得了时间间隔。注意,先是得到 time_piont,我们才能计算 duration。

最后,time_t us = duration_cast<microseconds>(duration).count(),转换成毫秒并返回。

SetClockAt:
cpp 复制代码
void SetClockAt(double pts, double time){
    this->pts = pts;
    pts_drift = this->pts - time;
}

这个函数负责给 pts 和 pts_drift 赋值。这很好理解,因为 pts_drift 就是 pts 和当前时间的差值。

GetClock:
cpp 复制代码
double GetClock(){
      double time = GetMicroseconds() / 1000000.0;
      return pts_drift + time;
}

这个函数负责获取时间。获取了时间间隔后加上 pts_stamp 后就返回这个值。

SetClock:
cpp 复制代码
double SetClock(){
     double time = GetMicroseconds() / 1000000.0;
     SetClockAt(pts, time);
}

这个函数负责设置设置时钟,获取时间间隔,然后调用 SetClockAt 后就可以了。

我们这篇文章就讲讲了时间设置的操作,并没有深入讲音视频同步的原理。我们最后通过 audiooutput 和 videooutput 播放出音视频就好了,到时候也会深入讲同步机制的。

欲知后事如何,请听下回分解。

相关推荐
北极糊的狐1 小时前
用狸窝转换器转换视频后文件变大的问题排查
microsoft·音视频
碧海银沙音频科技研究院1 小时前
ES7243E ADC模拟音频转i2S到 BES I2S1 Master输出播放到SPK精准分析
人工智能·算法·音视频
音视频牛哥1 小时前
从“十五五”规划看中国视频基础设施的下一个五年:SmartMediaKit 的战略跃迁与时代机遇
人工智能·音视频·大牛直播sdk·十五五规划具身智能·十五五规划音视频·低空经济低延迟音视频方案·具身智能rtsp rtmp
eguid_11 小时前
【HLS】Java实现统计HLS的m3u8清单中所有ts切片的视频持续时长
java·音视频·hls·1024程序员节·m3u8·ts时长
mortimer2 小时前
牺牲质量换效率:视频翻译项目中音画同步模块的深度实现与思考
python·ffmpeg
EasyCVR4 小时前
浅述视频汇聚平台EasyCVR视频编解码与转码技术如何成就视频体验
音视频·视频编解码
EasyGBS6 小时前
EasyGBS视频实时监控系助力实现换热站全景可视化管理
音视频
zgyhc20506 小时前
【Android Audio】安卓音频中Surround mode切换流程
android·音视频
Jonathan Star17 小时前
用Python轻松提取视频音频并去除静音片段
开发语言·python·音视频
给大佬递杯卡布奇诺17 小时前
FFmpeg 基本数据结构 AVInputFormat 分析
数据结构·c++·ffmpeg·音视频