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.
|
|
|
|
|
2017-12-20 00:56:36 +00:00
|
|
|
#ifndef PACKAGER_MEDIA_BASE_AUDIO_TIMESTAMP_HELPER_H_
|
|
|
|
#define PACKAGER_MEDIA_BASE_AUDIO_TIMESTAMP_HELPER_H_
|
2014-04-07 17:27:07 +00:00
|
|
|
|
2023-10-11 08:49:50 +00:00
|
|
|
#include <cstdint>
|
2014-09-30 23:52:58 +00:00
|
|
|
|
2023-10-10 23:51:11 +00:00
|
|
|
#include <packager/macros.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 {
|
|
|
|
|
|
|
|
// Generates timestamps for a sequence of audio sample frames. This class should
|
|
|
|
// be used any place timestamps need to be calculated for a sequence of audio
|
|
|
|
// samples. It helps avoid timestamps inaccuracies caused by rounding/truncation
|
|
|
|
// in repeated sample count to timestamp conversions.
|
|
|
|
//
|
|
|
|
// The class is constructed with samples_per_second information so that it can
|
|
|
|
// convert audio sample frame counts into timestamps. After the object is
|
|
|
|
// constructed, SetBaseTimestamp() must be called to specify the starting
|
|
|
|
// timestamp of the audio sequence. As audio samples are received, their frame
|
|
|
|
// counts are added using AddFrames(). These frame counts are accumulated by
|
|
|
|
// this class so GetTimestamp() can be used to determine the timestamp for the
|
|
|
|
// samples that have been added. GetDuration() calculates the proper duration
|
|
|
|
// values for samples added to the current timestamp. GetFramesToTarget()
|
|
|
|
// determines the number of frames that need to be added/removed from the
|
|
|
|
// accumulated frames to reach a target timestamp.
|
2014-04-07 17:48:25 +00:00
|
|
|
class AudioTimestampHelper {
|
2014-04-07 17:27:07 +00:00
|
|
|
public:
|
2021-08-04 18:56:44 +00:00
|
|
|
explicit AudioTimestampHelper(int32_t timescale, uint32_t samples_per_second);
|
2014-04-07 17:27:07 +00:00
|
|
|
|
|
|
|
// Sets the base timestamp to |base_timestamp| and the sets count to 0.
|
2014-09-30 21:52:21 +00:00
|
|
|
void SetBaseTimestamp(int64_t base_timestamp);
|
2014-04-07 17:27:07 +00:00
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
int64_t base_timestamp() const;
|
|
|
|
int64_t frame_count() const { return frame_count_; }
|
2014-04-07 17:27:07 +00:00
|
|
|
|
|
|
|
// Adds |frame_count| to the frame counter.
|
|
|
|
// Note: SetBaseTimestamp() must be called with a value other than
|
|
|
|
// kNoTimestamp() before this method can be called.
|
2014-09-30 21:52:21 +00:00
|
|
|
void AddFrames(int64_t frame_count);
|
2014-04-07 17:27:07 +00:00
|
|
|
|
|
|
|
// Get the current timestamp. This value is computed from the base_timestamp()
|
|
|
|
// and the number of sample frames that have been added so far.
|
2014-09-30 21:52:21 +00:00
|
|
|
int64_t GetTimestamp() const;
|
2014-04-07 17:27:07 +00:00
|
|
|
|
|
|
|
// Gets the duration if |frame_count| frames were added to the current
|
|
|
|
// timestamp reported by GetTimestamp(). This method ensures that
|
|
|
|
// (GetTimestamp() + GetFrameDuration(n)) will equal the timestamp that
|
|
|
|
// GetTimestamp() will return if AddFrames(n) is called.
|
2014-09-30 21:52:21 +00:00
|
|
|
int64_t GetFrameDuration(int64_t frame_count) const;
|
2014-04-07 17:27:07 +00:00
|
|
|
|
|
|
|
// Returns the number of frames needed to reach the target timestamp.
|
|
|
|
// Note: |target| must be >= |base_timestamp_|.
|
2014-09-30 21:52:21 +00:00
|
|
|
int64_t GetFramesToTarget(int64_t target) const;
|
2014-04-07 17:27:07 +00:00
|
|
|
|
|
|
|
private:
|
2014-09-30 21:52:21 +00:00
|
|
|
int64_t ComputeTimestamp(int64_t frame_count) const;
|
2014-04-07 17:27:07 +00:00
|
|
|
|
2014-04-07 17:48:25 +00:00
|
|
|
double ticks_per_frame_;
|
2014-04-07 17:27:07 +00:00
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
int64_t base_timestamp_;
|
2014-04-07 17:27:07 +00:00
|
|
|
|
|
|
|
// Number of frames accumulated by AddFrames() calls.
|
2014-09-30 21:52:21 +00:00
|
|
|
int64_t frame_count_;
|
2014-04-07 17:27:07 +00:00
|
|
|
|
|
|
|
DISALLOW_IMPLICIT_CONSTRUCTORS(AudioTimestampHelper);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|
2014-04-07 17:27:07 +00:00
|
|
|
|
|
|
|
#endif
|