5#include <packager/media/base/audio_timestamp_helper.h>
9#include <absl/log/check.h>
11#include <packager/media/base/timestamp.h>
16AudioTimestampHelper::AudioTimestampHelper(int32_t timescale,
17 uint32_t samples_per_second)
18 : base_timestamp_(kNoTimestamp), frame_count_(0) {
19 DCHECK_GT(samples_per_second, 0u);
20 double fps = samples_per_second;
21 ticks_per_frame_ = timescale / fps;
24void AudioTimestampHelper::SetBaseTimestamp(int64_t base_timestamp) {
25 base_timestamp_ = base_timestamp;
29int64_t AudioTimestampHelper::base_timestamp()
const {
30 return base_timestamp_;
33void AudioTimestampHelper::AddFrames(int64_t frame_count) {
34 DCHECK_GE(frame_count, 0);
35 DCHECK(base_timestamp_ != kNoTimestamp);
36 frame_count_ += frame_count;
39int64_t AudioTimestampHelper::GetTimestamp()
const {
40 return ComputeTimestamp(frame_count_);
43int64_t AudioTimestampHelper::GetFrameDuration(int64_t frame_count)
const {
44 DCHECK_GE(frame_count, 0);
45 int64_t end_timestamp = ComputeTimestamp(frame_count_ + frame_count);
46 return end_timestamp - GetTimestamp();
49int64_t AudioTimestampHelper::GetFramesToTarget(int64_t target)
const {
50 DCHECK(base_timestamp_ != kNoTimestamp);
51 DCHECK(target >= base_timestamp_);
53 int64_t delta_in_ticks = (target - GetTimestamp());
54 if (delta_in_ticks == 0)
61 int64_t delta_from_base = target - base_timestamp_;
65 double threshold = ticks_per_frame_ / 2;
66 int64_t target_frame_count = (delta_from_base + threshold) / ticks_per_frame_;
67 return target_frame_count - frame_count_;
70int64_t AudioTimestampHelper::ComputeTimestamp(int64_t frame_count)
const {
71 DCHECK_GE(frame_count, 0);
72 DCHECK(base_timestamp_ != kNoTimestamp);
73 double frames_ticks = ticks_per_frame_ * frame_count;
74 return base_timestamp_ + frames_ticks;
All the methods that are virtual are virtual for mocking.