Shaka Packager SDK
Loading...
Searching...
No Matches
audio_timestamp_helper.h
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#ifndef PACKAGER_MEDIA_BASE_AUDIO_TIMESTAMP_HELPER_H_
6#define PACKAGER_MEDIA_BASE_AUDIO_TIMESTAMP_HELPER_H_
7
8#include <cstdint>
9
10#include <packager/macros/classes.h>
11
12namespace shaka {
13namespace media {
14
15// Generates timestamps for a sequence of audio sample frames. This class should
16// be used any place timestamps need to be calculated for a sequence of audio
17// samples. It helps avoid timestamps inaccuracies caused by rounding/truncation
18// in repeated sample count to timestamp conversions.
19//
20// The class is constructed with samples_per_second information so that it can
21// convert audio sample frame counts into timestamps. After the object is
22// constructed, SetBaseTimestamp() must be called to specify the starting
23// timestamp of the audio sequence. As audio samples are received, their frame
24// counts are added using AddFrames(). These frame counts are accumulated by
25// this class so GetTimestamp() can be used to determine the timestamp for the
26// samples that have been added. GetDuration() calculates the proper duration
27// values for samples added to the current timestamp. GetFramesToTarget()
28// determines the number of frames that need to be added/removed from the
29// accumulated frames to reach a target timestamp.
31 public:
32 explicit AudioTimestampHelper(int32_t timescale, uint32_t samples_per_second);
33
34 // Sets the base timestamp to |base_timestamp| and the sets count to 0.
35 void SetBaseTimestamp(int64_t base_timestamp);
36
37 int64_t base_timestamp() const;
38 int64_t frame_count() const { return frame_count_; }
39
40 // Adds |frame_count| to the frame counter.
41 // Note: SetBaseTimestamp() must be called with a value other than
42 // kNoTimestamp() before this method can be called.
43 void AddFrames(int64_t frame_count);
44
45 // Get the current timestamp. This value is computed from the base_timestamp()
46 // and the number of sample frames that have been added so far.
47 int64_t GetTimestamp() const;
48
49 // Gets the duration if |frame_count| frames were added to the current
50 // timestamp reported by GetTimestamp(). This method ensures that
51 // (GetTimestamp() + GetFrameDuration(n)) will equal the timestamp that
52 // GetTimestamp() will return if AddFrames(n) is called.
53 int64_t GetFrameDuration(int64_t frame_count) const;
54
55 // Returns the number of frames needed to reach the target timestamp.
56 // Note: |target| must be >= |base_timestamp_|.
57 int64_t GetFramesToTarget(int64_t target) const;
58
59 private:
60 int64_t ComputeTimestamp(int64_t frame_count) const;
61
62 double ticks_per_frame_;
63
64 int64_t base_timestamp_;
65
66 // Number of frames accumulated by AddFrames() calls.
67 int64_t frame_count_;
68
69 DISALLOW_IMPLICIT_CONSTRUCTORS(AudioTimestampHelper);
70};
71
72} // namespace media
73} // namespace shaka
74
75#endif
All the methods that are virtual are virtual for mocking.