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.
|
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
#include <packager/media/base/audio_timestamp_helper.h>
|
|
|
|
|
|
|
|
#include <iterator>
|
|
|
|
|
2014-08-21 22:40:44 +00:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
#include <packager/macros/classes.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
|
|
|
static const uint32_t kDefaultSampleRate = 44100;
|
2021-08-04 18:56:44 +00:00
|
|
|
static const int32_t kTimescale = 1000000;
|
2014-04-07 17:27:07 +00:00
|
|
|
|
|
|
|
class AudioTimestampHelperTest : public ::testing::Test {
|
|
|
|
public:
|
2014-04-07 17:48:25 +00:00
|
|
|
AudioTimestampHelperTest() : helper_(kTimescale, kDefaultSampleRate) {
|
|
|
|
helper_.SetBaseTimestamp(0);
|
2014-04-07 17:27:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Adds frames to the helper and returns the current timestamp in
|
|
|
|
// microseconds.
|
2014-09-30 21:52:21 +00:00
|
|
|
int64_t AddFrames(int frames) {
|
2014-04-07 17:27:07 +00:00
|
|
|
helper_.AddFrames(frames);
|
2014-04-07 17:48:25 +00:00
|
|
|
return helper_.GetTimestamp();
|
2014-04-07 17:27:07 +00:00
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
int64_t FramesToTarget(int target_in_microseconds) {
|
2014-04-07 17:48:25 +00:00
|
|
|
return helper_.GetFramesToTarget(target_in_microseconds);
|
2014-04-07 17:27:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestGetFramesToTargetRange(int frame_count, int start, int end) {
|
|
|
|
for (int i = start; i <= end; ++i) {
|
|
|
|
EXPECT_EQ(frame_count, FramesToTarget(i)) << " Failure for timestamp "
|
|
|
|
<< i << " us.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
AudioTimestampHelper helper_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(AudioTimestampHelperTest);
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(AudioTimestampHelperTest, Basic) {
|
2014-04-07 17:48:25 +00:00
|
|
|
EXPECT_EQ(0, helper_.GetTimestamp());
|
2014-04-07 17:27:07 +00:00
|
|
|
|
|
|
|
// Verify that the output timestamp is always rounded down to the
|
|
|
|
// nearest microsecond. 1 frame @ 44100 is ~22.67573 microseconds,
|
|
|
|
// which is why the timestamp sometimes increments by 23 microseconds
|
|
|
|
// and other times it increments by 22 microseconds.
|
|
|
|
EXPECT_EQ(0, AddFrames(0));
|
|
|
|
EXPECT_EQ(22, AddFrames(1));
|
|
|
|
EXPECT_EQ(45, AddFrames(1));
|
|
|
|
EXPECT_EQ(68, AddFrames(1));
|
|
|
|
EXPECT_EQ(90, AddFrames(1));
|
|
|
|
EXPECT_EQ(113, AddFrames(1));
|
|
|
|
|
|
|
|
// Verify that adding frames one frame at a time matches the timestamp
|
|
|
|
// returned if the same number of frames are added all at once.
|
2014-09-30 21:52:21 +00:00
|
|
|
int64_t timestamp_1 = helper_.GetTimestamp();
|
2014-04-07 17:48:25 +00:00
|
|
|
helper_.SetBaseTimestamp(kNoTimestamp);
|
|
|
|
EXPECT_TRUE(kNoTimestamp == helper_.base_timestamp());
|
|
|
|
helper_.SetBaseTimestamp(0);
|
|
|
|
EXPECT_EQ(0, helper_.GetTimestamp());
|
2014-04-07 17:27:07 +00:00
|
|
|
|
|
|
|
helper_.AddFrames(5);
|
2014-04-07 17:48:25 +00:00
|
|
|
EXPECT_EQ(113, helper_.GetTimestamp());
|
2014-04-07 17:27:07 +00:00
|
|
|
EXPECT_TRUE(timestamp_1 == helper_.GetTimestamp());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST_F(AudioTimestampHelperTest, GetDuration) {
|
2014-04-07 17:48:25 +00:00
|
|
|
helper_.SetBaseTimestamp(100);
|
2014-04-07 17:27:07 +00:00
|
|
|
|
|
|
|
int frame_count = 5;
|
2014-09-30 21:52:21 +00:00
|
|
|
int64_t expected_durations[] = {113, 113, 114, 113, 113, 114};
|
2023-12-01 17:32:19 +00:00
|
|
|
for (size_t i = 0; i < std::size(expected_durations); ++i) {
|
2014-09-30 21:52:21 +00:00
|
|
|
int64_t duration = helper_.GetFrameDuration(frame_count);
|
2014-04-07 17:48:25 +00:00
|
|
|
EXPECT_EQ(expected_durations[i], duration);
|
2014-04-07 17:27:07 +00:00
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
int64_t timestamp_1 = helper_.GetTimestamp() + duration;
|
2014-04-07 17:27:07 +00:00
|
|
|
helper_.AddFrames(frame_count);
|
2014-09-30 21:52:21 +00:00
|
|
|
int64_t timestamp_2 = helper_.GetTimestamp();
|
2014-04-07 17:27:07 +00:00
|
|
|
EXPECT_TRUE(timestamp_1 == timestamp_2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(AudioTimestampHelperTest, GetFramesToTarget) {
|
|
|
|
// Verify GetFramesToTarget() rounding behavior.
|
|
|
|
// 1 frame @ 44100 is ~22.67573 microseconds,
|
|
|
|
|
|
|
|
// Test values less than half of the frame duration.
|
|
|
|
TestGetFramesToTargetRange(0, 0, 11);
|
|
|
|
|
|
|
|
// Test values between half the frame duration & the
|
|
|
|
// full frame duration.
|
|
|
|
TestGetFramesToTargetRange(1, 12, 22);
|
|
|
|
|
|
|
|
// Verify that the same number of frames is returned up
|
|
|
|
// to the next half a frame.
|
|
|
|
TestGetFramesToTargetRange(1, 23, 34);
|
|
|
|
|
|
|
|
// Verify the next 3 ranges.
|
|
|
|
TestGetFramesToTargetRange(2, 35, 56);
|
|
|
|
TestGetFramesToTargetRange(3, 57, 79);
|
|
|
|
TestGetFramesToTargetRange(4, 80, 102);
|
|
|
|
TestGetFramesToTargetRange(5, 103, 124);
|
|
|
|
|
|
|
|
// Add frames to the helper so negative frame counts can be tested.
|
|
|
|
helper_.AddFrames(5);
|
|
|
|
|
|
|
|
// Note: The timestamp ranges must match the positive values
|
|
|
|
// tested above to verify that the code is rounding properly.
|
|
|
|
TestGetFramesToTargetRange(0, 103, 124);
|
|
|
|
TestGetFramesToTargetRange(-1, 80, 102);
|
|
|
|
TestGetFramesToTargetRange(-2, 57, 79);
|
|
|
|
TestGetFramesToTargetRange(-3, 35, 56);
|
|
|
|
TestGetFramesToTargetRange(-4, 12, 34);
|
|
|
|
TestGetFramesToTargetRange(-5, 0, 11);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|