音视频项目—基于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 播放出音视频就好了,到时候也会深入讲同步机制的。

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

相关推荐
Android系统攻城狮3 分钟前
Linux FFmpeg深度解析之avcodec_flush_buffers调用流程与实战(十九)
linux·运维·服务器·ffmpeg·车载音视频·ffmpeg音视频进阶
AI视频工具指南1 小时前
怎么用 AI 做节日祝福视频?
人工智能·音视频·节日
Flynn Liang6 小时前
Spring AI 多模态实战(下):文生图 + TTS 语音合成(含 2.0 流式音频分片)
人工智能·spring·文生图·音视频·语音合成·tts·springai
自养Agent9 小时前
AI 私有化实战:多模态本地化·视觉与音频怎么装下
人工智能·大模型·llm·音视频·语音识别
lm down9 小时前
IndexTTS 2/2.5 轻量版、Qwen3-TTS 轻量版:支持AMD、Intel、NVIDIA显卡和Mac,2GB显存可跑
人工智能·ai·音视频·tts
youdexiang10 小时前
IOS录音APP拍照记录能和音频同步吗
ios·音视频
重生之我是萧炎10 小时前
ffmpeg官方下载安装教程(提供安装包)
ffmpeg
9527华安10 小时前
FPGA基于ADV7182解码PAL视频,Video Processing Subsystem去隔行,提供6套工程源码和技术支持
fpga开发·音视频·pal·adv7182
vjmap11 小时前
AI 地图动画-以真实地图为画布的 AI 视频编辑器-唯杰 AI 地图动画正式上线
人工智能·编辑器·音视频·gis·cad
星核0penstarry11 小时前
视频理解模型选型指南:Gemini、Qwen、SeedRealtime 怎么选(附 API 示例)
人工智能·音视频·实时音视频·视频·gemini·vs qwen