ffplay音视频同步分析(一)

来源:互联网 发布:windows桌面安卓版 编辑:程序博客网 时间:2024/06/05 10:04

先给出与同步有关的Clock的结构体,为了简化分析,我把Clock的一些字段删掉了,如有有兴趣可查看源码:

typedef struct Clock {    double pts;           /* clock base */    double pts_drift;     /* clock base minus time at which we updated the clock */ //pts和上一次更新的时刻的差值    double last_updated;  //上一次更新的时刻(av_gettime_relative)} Clock;
  • 1
  • 2
  • 3
  • 4
  • 5

与Clock有关的操作如下(经过简化了,主要把speed去掉了):

static void init_clock(Clock *c, int *queue_serial){    c->speed = 1.0;    set_clock(c, NAN, -1);}static double get_clock(Clock *c){    double time = av_gettime_relative() / 1000000.0;    return c->pts_drift + time;}static void set_clock(Clock *c, double pts){    double time = av_gettime_relative() / 1000000.0;    set_clock_at(c, pts, time);}static void set_clock_at(Clock *c, double pts, double time){    c->pts = pts;    c->last_updated = time;    c->pts_drift = c->pts - time;}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

从代码可以看到,set_clock实际上是把pts设置到clock的pts中,然后调用av_gettime_relative()记录好当前时刻,赋给clodk的last_updated, 当调用get_clock时,实际上是把上次设置的pts加上 get_clock调用的时刻减去set_clock时刻的差值。

下面画了一个图辅助理解: 
这里写图片描述

原创粉丝点击