当前位置: 首页 > news >正文

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

介绍

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

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

解析

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

我们先看看 async 的代码

#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:
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:
void SetClockAt(double pts, double time){this->pts = pts;pts_drift = this->pts - time;
}

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

GetClock:
double GetClock(){double time = GetMicroseconds() / 1000000.0;return pts_drift + time;
}

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

SetClock:
double SetClock(){double time = GetMicroseconds() / 1000000.0;SetClockAt(pts, time);
}

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

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

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

http://www.lryc.cn/news/235345.html

相关文章:

  • 键鼠自动化2.0树形结构讲解
  • 2023年【金属非金属矿山安全检查(地下矿山)】考试报名及金属非金属矿山安全检查(地下矿山)最新解析
  • Java 12 及Tomcat 部署配置
  • pandas教程:Date Ranges, Frequencies, and Shifting 日期范围,频度,和位移
  • 设计模式 - 概览
  • 【Linux】Makefile
  • TS的函数如何定义类型
  • xstream实现xml和java bean 互相转换
  • 斯坦福机器学习 Lecture1 (机器学习,监督学习、回归问题、分类问题定义)
  • 五、Linux目录结构
  • C/C++数据结构之中缀表达式转换为后缀表达式,删除堆栈元素
  • uni-app下,页面跳转后wacth持续监听的问题处理
  • Python技术栈 —— 语言基础
  • redis cluster搭建
  • windows 11 本地运行ER-NeRF及pytorch3D安装
  • mysql客户端navicat的一些错误合集
  • 【力扣面试经典150题】(链表)K 个一组翻转链表
  • 数据结构刷题
  • 【Android】设置全局标题栏
  • R语言的入门学习
  • 【开源】基于Vue和SpringBoot的民宿预定管理系统
  • nacos集群部署
  • 9、传统计算机视觉 —— 边缘检测
  • Linux tc 使用
  • 从0开始学习JavaScript--JavaScript 数字与日期
  • 从关键新闻和最新技术看AI行业发展(2023.11.6-11.19第十期) |【WeThinkIn老实人报】
  • 计算机硬件的基本组成
  • 【算法-哈希表3】四数相加2 和 赎金信
  • wpf devexpress自定义编辑器
  • 文档向量化工具(一):Apache Tika介绍