5 #include <packager/media/base/audio_timestamp_helper.h>
7 #include <absl/log/check.h>
8 #include <absl/log/log.h>
10 #include <packager/media/base/timestamp.h>
15 AudioTimestampHelper::AudioTimestampHelper(int32_t timescale,
16 uint32_t samples_per_second)
17 : base_timestamp_(kNoTimestamp), frame_count_(0) {
18 DCHECK_GT(samples_per_second, 0u);
19 double fps = samples_per_second;
20 ticks_per_frame_ = timescale / fps;
23 void AudioTimestampHelper::SetBaseTimestamp(int64_t base_timestamp) {
24 base_timestamp_ = base_timestamp;
28 int64_t AudioTimestampHelper::base_timestamp()
const {
29 return base_timestamp_;
32 void AudioTimestampHelper::AddFrames(int64_t frame_count) {
33 DCHECK_GE(frame_count, 0);
34 DCHECK(base_timestamp_ != kNoTimestamp);
35 frame_count_ += frame_count;
38 int64_t AudioTimestampHelper::GetTimestamp()
const {
39 return ComputeTimestamp(frame_count_);
42 int64_t AudioTimestampHelper::GetFrameDuration(int64_t frame_count)
const {
43 DCHECK_GE(frame_count, 0);
44 int64_t end_timestamp = ComputeTimestamp(frame_count_ + frame_count);
45 return end_timestamp - GetTimestamp();
48 int64_t AudioTimestampHelper::GetFramesToTarget(int64_t target)
const {
49 DCHECK(base_timestamp_ != kNoTimestamp);
50 DCHECK(target >= base_timestamp_);
52 int64_t delta_in_ticks = (target - GetTimestamp());
53 if (delta_in_ticks == 0)
60 int64_t delta_from_base = target - base_timestamp_;
64 double threshold = ticks_per_frame_ / 2;
65 int64_t target_frame_count = (delta_from_base + threshold) / ticks_per_frame_;
66 return target_frame_count - frame_count_;
69 int64_t AudioTimestampHelper::ComputeTimestamp(int64_t frame_count)
const {
70 DCHECK_GE(frame_count, 0);
71 DCHECK(base_timestamp_ != kNoTimestamp);
72 double frames_ticks = ticks_per_frame_ * frame_count;
73 return base_timestamp_ + frames_ticks;
All the methods that are virtual are virtual for mocking.