-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathav_clock.cpp
More file actions
61 lines (51 loc) · 1.11 KB
/
av_clock.cpp
File metadata and controls
61 lines (51 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include "av_clock.h"
extern "C"
{
#include <libavutil/time.h>
}
av_clock::av_clock() { update_time(); }
void av_clock::set(double p, int serial)
{
pts_.store(p);
serial_.store(serial);
update_time();
}
double av_clock::get() const
{
if (paused_.load())
{
return pts_at_pause_;
}
auto now = static_cast<double>(av_gettime_relative());
const double time_elapsed = (now / 1000000.0) - last_updated_.load();
return pts_.load() + (time_elapsed * rate_.load());
}
int av_clock::serial() const { return serial_.load(); }
void av_clock::set_rate(double rate)
{
if (rate <= 0.0)
{
return;
}
const double current = get();
pts_.store(current);
rate_.store(rate);
update_time();
}
double av_clock::rate() const { return rate_.load(); }
void av_clock::pause()
{
pts_at_pause_ = get();
paused_.store(true);
}
void av_clock::resume()
{
pts_.store(pts_at_pause_);
update_time();
paused_.store(false);
}
void av_clock::update_time()
{
auto now = static_cast<double>(av_gettime_relative());
last_updated_.store(now / 1000000.0);
}