This source file includes following definitions.
- frame_count_
- SetBaseTimestamp
- base_timestamp
- AddFrames
- GetTimestamp
- GetFrameDuration
- GetFramesToTarget
- ComputeTimestamp
#include "media/base/audio_timestamp_helper.h"
#include "base/logging.h"
#include "media/base/buffers.h"
namespace media {
AudioTimestampHelper::AudioTimestampHelper(int samples_per_second)
: base_timestamp_(kNoTimestamp()),
frame_count_(0) {
DCHECK_GT(samples_per_second, 0);
double fps = samples_per_second;
microseconds_per_frame_ = base::Time::kMicrosecondsPerSecond / fps;
}
void AudioTimestampHelper::SetBaseTimestamp(base::TimeDelta base_timestamp) {
base_timestamp_ = base_timestamp;
frame_count_ = 0;
}
base::TimeDelta AudioTimestampHelper::base_timestamp() const {
return base_timestamp_;
}
void AudioTimestampHelper::AddFrames(int frame_count) {
DCHECK_GE(frame_count, 0);
DCHECK(base_timestamp_ != kNoTimestamp());
frame_count_ += frame_count;
}
base::TimeDelta AudioTimestampHelper::GetTimestamp() const {
return ComputeTimestamp(frame_count_);
}
base::TimeDelta AudioTimestampHelper::GetFrameDuration(int frame_count) const {
DCHECK_GE(frame_count, 0);
base::TimeDelta end_timestamp = ComputeTimestamp(frame_count_ + frame_count);
return end_timestamp - GetTimestamp();
}
int64 AudioTimestampHelper::GetFramesToTarget(base::TimeDelta target) const {
DCHECK(base_timestamp_ != kNoTimestamp());
DCHECK(target >= base_timestamp_);
int64 delta_in_us = (target - GetTimestamp()).InMicroseconds();
if (delta_in_us == 0)
return 0;
base::TimeDelta delta_from_base = target - base_timestamp_;
double threshold = microseconds_per_frame_ / 2;
int64 target_frame_count =
(delta_from_base.InMicroseconds() + threshold) / microseconds_per_frame_;
return target_frame_count - frame_count_;
}
base::TimeDelta AudioTimestampHelper::ComputeTimestamp(
int64 frame_count) const {
DCHECK_GE(frame_count, 0);
DCHECK(base_timestamp_ != kNoTimestamp());
double frames_us = microseconds_per_frame_ * frame_count;
return base_timestamp_ + base::TimeDelta::FromMicroseconds(frames_us);
}
}