Shaka Packager SDK
Loading...
Searching...
No Matches
audio_timestamp_helper.cc
1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <packager/media/base/audio_timestamp_helper.h>
6
7#include <cstdint>
8
9#include <absl/log/check.h>
10
11#include <packager/media/base/timestamp.h>
12
13namespace shaka {
14namespace media {
15
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;
22}
23
24void AudioTimestampHelper::SetBaseTimestamp(int64_t base_timestamp) {
25 base_timestamp_ = base_timestamp;
26 frame_count_ = 0;
27}
28
29int64_t AudioTimestampHelper::base_timestamp() const {
30 return base_timestamp_;
31}
32
33void AudioTimestampHelper::AddFrames(int64_t frame_count) {
34 DCHECK_GE(frame_count, 0);
35 DCHECK(base_timestamp_ != kNoTimestamp);
36 frame_count_ += frame_count;
37}
38
39int64_t AudioTimestampHelper::GetTimestamp() const {
40 return ComputeTimestamp(frame_count_);
41}
42
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();
47}
48
49int64_t AudioTimestampHelper::GetFramesToTarget(int64_t target) const {
50 DCHECK(base_timestamp_ != kNoTimestamp);
51 DCHECK(target >= base_timestamp_);
52
53 int64_t delta_in_ticks = (target - GetTimestamp());
54 if (delta_in_ticks == 0)
55 return 0;
56
57 // Compute a timestamp relative to |base_timestamp_| since timestamps
58 // created from |frame_count_| are computed relative to this base.
59 // This ensures that the time to frame computation here is the proper inverse
60 // of the frame to time computation in ComputeTimestamp().
61 int64_t delta_from_base = target - base_timestamp_;
62
63 // Compute frame count for the time delta. This computation rounds to
64 // the nearest whole number of frames.
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_;
68}
69
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;
75}
76
77} // namespace media
78} // namespace shaka
All the methods that are virtual are virtual for mocking.