5 #include "packager/media/base/audio_timestamp_helper.h" 7 #include "packager/base/logging.h" 8 #include "packager/media/base/timestamp.h" 13 AudioTimestampHelper::AudioTimestampHelper(uint32_t timescale,
14 uint32_t samples_per_second)
15 : base_timestamp_(kNoTimestamp), frame_count_(0) {
16 DCHECK_GT(samples_per_second, 0u);
17 double fps = samples_per_second;
18 ticks_per_frame_ = timescale / fps;
21 void AudioTimestampHelper::SetBaseTimestamp(int64_t base_timestamp) {
22 base_timestamp_ = base_timestamp;
26 int64_t AudioTimestampHelper::base_timestamp()
const {
27 return base_timestamp_;
30 void AudioTimestampHelper::AddFrames(int64_t frame_count) {
31 DCHECK_GE(frame_count, 0);
32 DCHECK(base_timestamp_ != kNoTimestamp);
33 frame_count_ += frame_count;
36 int64_t AudioTimestampHelper::GetTimestamp()
const {
37 return ComputeTimestamp(frame_count_);
40 int64_t AudioTimestampHelper::GetFrameDuration(int64_t frame_count)
const {
41 DCHECK_GE(frame_count, 0);
42 int64_t end_timestamp = ComputeTimestamp(frame_count_ + frame_count);
43 return end_timestamp - GetTimestamp();
46 int64_t AudioTimestampHelper::GetFramesToTarget(int64_t target)
const {
47 DCHECK(base_timestamp_ != kNoTimestamp);
48 DCHECK(target >= base_timestamp_);
50 int64_t delta_in_ticks = (target - GetTimestamp());
51 if (delta_in_ticks == 0)
58 int64_t delta_from_base = target - base_timestamp_;
62 double threshold = ticks_per_frame_ / 2;
63 int64_t target_frame_count = (delta_from_base + threshold) / ticks_per_frame_;
64 return target_frame_count - frame_count_;
67 int64_t AudioTimestampHelper::ComputeTimestamp(int64_t frame_count)
const {
68 DCHECK_GE(frame_count, 0);
69 DCHECK(base_timestamp_ != kNoTimestamp);
70 double frames_ticks = ticks_per_frame_ * frame_count;
71 return base_timestamp_ + frames_ticks;
All the methods that are virtual are virtual for mocking.