2014-04-07 17:27:07 +00:00
|
|
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/media/base/audio_timestamp_helper.h"
|
2014-04-07 17:27:07 +00:00
|
|
|
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/base/logging.h"
|
|
|
|
#include "packager/media/base/timestamp.h"
|
2014-04-07 17:27:07 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2014-04-07 17:27:07 +00:00
|
|
|
namespace media {
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
AudioTimestampHelper::AudioTimestampHelper(uint32_t timescale,
|
|
|
|
uint32_t samples_per_second)
|
|
|
|
: base_timestamp_(kNoTimestamp), frame_count_(0) {
|
2014-04-07 17:48:25 +00:00
|
|
|
DCHECK_GT(samples_per_second, 0u);
|
2014-04-07 17:27:07 +00:00
|
|
|
double fps = samples_per_second;
|
2014-04-07 17:48:25 +00:00
|
|
|
ticks_per_frame_ = timescale / fps;
|
2014-04-07 17:27:07 +00:00
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
void AudioTimestampHelper::SetBaseTimestamp(int64_t base_timestamp) {
|
2014-04-07 17:27:07 +00:00
|
|
|
base_timestamp_ = base_timestamp;
|
|
|
|
frame_count_ = 0;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
int64_t AudioTimestampHelper::base_timestamp() const {
|
2014-04-07 17:27:07 +00:00
|
|
|
return base_timestamp_;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
void AudioTimestampHelper::AddFrames(int64_t frame_count) {
|
2014-04-07 17:27:07 +00:00
|
|
|
DCHECK_GE(frame_count, 0);
|
2014-04-07 17:48:25 +00:00
|
|
|
DCHECK(base_timestamp_ != kNoTimestamp);
|
2014-04-07 17:27:07 +00:00
|
|
|
frame_count_ += frame_count;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
int64_t AudioTimestampHelper::GetTimestamp() const {
|
2014-04-07 17:27:07 +00:00
|
|
|
return ComputeTimestamp(frame_count_);
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
int64_t AudioTimestampHelper::GetFrameDuration(int64_t frame_count) const {
|
2014-04-07 17:27:07 +00:00
|
|
|
DCHECK_GE(frame_count, 0);
|
2014-09-30 21:52:21 +00:00
|
|
|
int64_t end_timestamp = ComputeTimestamp(frame_count_ + frame_count);
|
2014-04-07 17:27:07 +00:00
|
|
|
return end_timestamp - GetTimestamp();
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
int64_t AudioTimestampHelper::GetFramesToTarget(int64_t target) const {
|
2014-04-07 17:48:25 +00:00
|
|
|
DCHECK(base_timestamp_ != kNoTimestamp);
|
2014-04-07 17:27:07 +00:00
|
|
|
DCHECK(target >= base_timestamp_);
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
int64_t delta_in_ticks = (target - GetTimestamp());
|
2014-04-07 17:48:25 +00:00
|
|
|
if (delta_in_ticks == 0)
|
2014-04-07 17:27:07 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
// Compute a timestamp relative to |base_timestamp_| since timestamps
|
|
|
|
// created from |frame_count_| are computed relative to this base.
|
|
|
|
// This ensures that the time to frame computation here is the proper inverse
|
|
|
|
// of the frame to time computation in ComputeTimestamp().
|
2014-09-30 21:52:21 +00:00
|
|
|
int64_t delta_from_base = target - base_timestamp_;
|
2014-04-07 17:27:07 +00:00
|
|
|
|
|
|
|
// Compute frame count for the time delta. This computation rounds to
|
|
|
|
// the nearest whole number of frames.
|
2014-04-07 17:48:25 +00:00
|
|
|
double threshold = ticks_per_frame_ / 2;
|
2014-09-30 21:52:21 +00:00
|
|
|
int64_t target_frame_count = (delta_from_base + threshold) / ticks_per_frame_;
|
2014-04-07 17:27:07 +00:00
|
|
|
return target_frame_count - frame_count_;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
int64_t AudioTimestampHelper::ComputeTimestamp(int64_t frame_count) const {
|
2014-04-07 17:27:07 +00:00
|
|
|
DCHECK_GE(frame_count, 0);
|
2014-04-07 17:48:25 +00:00
|
|
|
DCHECK(base_timestamp_ != kNoTimestamp);
|
|
|
|
double frames_ticks = ticks_per_frame_ * frame_count;
|
|
|
|
return base_timestamp_ + frames_ticks;
|
2014-04-07 17:27:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|