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 <absl/log/check.h>
8#include <absl/log/log.h>
9
10#include <packager/media/base/timestamp.h>
11
12namespace shaka {
13namespace media {
14
15AudioTimestampHelper::AudioTimestampHelper(int32_t timescale,
16 uint32_t samples_per_second)
17 : base_timestamp_(kNoTimestamp), frame_count_(0) {
18 DCHECK_GT(samples_per_second, 0u);
19 double fps = samples_per_second;
20 ticks_per_frame_ = timescale / fps;
21}
22
23void AudioTimestampHelper::SetBaseTimestamp(int64_t base_timestamp) {
24 base_timestamp_ = base_timestamp;
25 frame_count_ = 0;
26}
27
28int64_t AudioTimestampHelper::base_timestamp() const {
29 return base_timestamp_;
30}
31
32void AudioTimestampHelper::AddFrames(int64_t frame_count) {
33 DCHECK_GE(frame_count, 0);
34 DCHECK(base_timestamp_ != kNoTimestamp);
35 frame_count_ += frame_count;
36}
37
38int64_t AudioTimestampHelper::GetTimestamp() const {
39 return ComputeTimestamp(frame_count_);
40}
41
42int64_t AudioTimestampHelper::GetFrameDuration(int64_t frame_count) const {
43 DCHECK_GE(frame_count, 0);
44 int64_t end_timestamp = ComputeTimestamp(frame_count_ + frame_count);
45 return end_timestamp - GetTimestamp();
46}
47
48int64_t AudioTimestampHelper::GetFramesToTarget(int64_t target) const {
49 DCHECK(base_timestamp_ != kNoTimestamp);
50 DCHECK(target >= base_timestamp_);
51
52 int64_t delta_in_ticks = (target - GetTimestamp());
53 if (delta_in_ticks == 0)
54 return 0;
55
56 // Compute a timestamp relative to |base_timestamp_| since timestamps
57 // created from |frame_count_| are computed relative to this base.
58 // This ensures that the time to frame computation here is the proper inverse
59 // of the frame to time computation in ComputeTimestamp().
60 int64_t delta_from_base = target - base_timestamp_;
61
62 // Compute frame count for the time delta. This computation rounds to
63 // the nearest whole number of frames.
64 double threshold = ticks_per_frame_ / 2;
65 int64_t target_frame_count = (delta_from_base + threshold) / ticks_per_frame_;
66 return target_frame_count - frame_count_;
67}
68
69int64_t AudioTimestampHelper::ComputeTimestamp(int64_t frame_count) const {
70 DCHECK_GE(frame_count, 0);
71 DCHECK(base_timestamp_ != kNoTimestamp);
72 double frames_ticks = ticks_per_frame_ * frame_count;
73 return base_timestamp_ + frames_ticks;
74}
75
76} // namespace media
77} // namespace shaka
All the methods that are virtual are virtual for mocking.