This source file includes following definitions.
- AddFrames
- FramesToTarget
- TestGetFramesToTargetRange
- TEST_F
- TEST_F
- TEST_F
#include "media/base/audio_timestamp_helper.h"
#include "media/base/buffers.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace media {
static const int kDefaultSampleRate = 44100;
class AudioTimestampHelperTest : public ::testing::Test {
public:
AudioTimestampHelperTest() : helper_(kDefaultSampleRate) {
helper_.SetBaseTimestamp(base::TimeDelta());
}
int64 AddFrames(int frames) {
helper_.AddFrames(frames);
return helper_.GetTimestamp().InMicroseconds();
}
int64 FramesToTarget(int target_in_microseconds) {
return helper_.GetFramesToTarget(
base::TimeDelta::FromMicroseconds(target_in_microseconds));
}
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) {
EXPECT_EQ(0, helper_.GetTimestamp().InMicroseconds());
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));
base::TimeDelta timestamp_1 = helper_.GetTimestamp();
helper_.SetBaseTimestamp(kNoTimestamp());
EXPECT_TRUE(kNoTimestamp() == helper_.base_timestamp());
helper_.SetBaseTimestamp(base::TimeDelta());
EXPECT_EQ(0, helper_.GetTimestamp().InMicroseconds());
helper_.AddFrames(5);
EXPECT_EQ(113, helper_.GetTimestamp().InMicroseconds());
EXPECT_TRUE(timestamp_1 == helper_.GetTimestamp());
}
TEST_F(AudioTimestampHelperTest, GetDuration) {
helper_.SetBaseTimestamp(base::TimeDelta::FromMicroseconds(100));
int frame_count = 5;
int64 expected_durations[] = { 113, 113, 114, 113, 113, 114 };
for (size_t i = 0; i < arraysize(expected_durations); ++i) {
base::TimeDelta duration = helper_.GetFrameDuration(frame_count);
EXPECT_EQ(expected_durations[i], duration.InMicroseconds());
base::TimeDelta timestamp_1 = helper_.GetTimestamp() + duration;
helper_.AddFrames(frame_count);
base::TimeDelta timestamp_2 = helper_.GetTimestamp();
EXPECT_TRUE(timestamp_1 == timestamp_2);
}
}
TEST_F(AudioTimestampHelperTest, GetFramesToTarget) {
TestGetFramesToTargetRange(0, 0, 11);
TestGetFramesToTargetRange(1, 12, 22);
TestGetFramesToTargetRange(1, 23, 34);
TestGetFramesToTargetRange(2, 35, 56);
TestGetFramesToTargetRange(3, 57, 79);
TestGetFramesToTargetRange(4, 80, 102);
TestGetFramesToTargetRange(5, 103, 124);
helper_.AddFrames(5);
TestGetFramesToTargetRange(0, 103, 124);
TestGetFramesToTargetRange(-1, 80, 102);
TestGetFramesToTargetRange(-2, 57, 79);
TestGetFramesToTargetRange(-3, 35, 56);
TestGetFramesToTargetRange(-4, 12, 34);
TestGetFramesToTargetRange(-5, 0, 11);
}
}