Delete unused downloads page asset.
[chromium-blink-merge.git] / media / base / audio_timestamp_helper_unittest.cc
bloba0cfa3bbfa587b1956083a51796548f96b950da9
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.
5 #include "media/base/audio_timestamp_helper.h"
6 #include "media/base/buffers.h"
7 #include "testing/gtest/include/gtest/gtest.h"
9 namespace media {
11 static const int kDefaultSampleRate = 44100;
13 class AudioTimestampHelperTest : public ::testing::Test {
14 public:
15 AudioTimestampHelperTest() : helper_(kDefaultSampleRate) {
16 helper_.SetBaseTimestamp(base::TimeDelta());
19 // Adds frames to the helper and returns the current timestamp in
20 // microseconds.
21 int64 AddFrames(int frames) {
22 helper_.AddFrames(frames);
23 return helper_.GetTimestamp().InMicroseconds();
26 int64 FramesToTarget(int target_in_microseconds) {
27 return helper_.GetFramesToTarget(
28 base::TimeDelta::FromMicroseconds(target_in_microseconds));
31 void TestGetFramesToTargetRange(int frame_count, int start, int end) {
32 for (int i = start; i <= end; ++i) {
33 EXPECT_EQ(frame_count, FramesToTarget(i)) << " Failure for timestamp "
34 << i << " us.";
38 protected:
39 AudioTimestampHelper helper_;
41 DISALLOW_COPY_AND_ASSIGN(AudioTimestampHelperTest);
44 TEST_F(AudioTimestampHelperTest, Basic) {
45 EXPECT_EQ(0, helper_.GetTimestamp().InMicroseconds());
47 // Verify that the output timestamp is always rounded down to the
48 // nearest microsecond. 1 frame @ 44100 is ~22.67573 microseconds,
49 // which is why the timestamp sometimes increments by 23 microseconds
50 // and other times it increments by 22 microseconds.
51 EXPECT_EQ(0, AddFrames(0));
52 EXPECT_EQ(22, AddFrames(1));
53 EXPECT_EQ(45, AddFrames(1));
54 EXPECT_EQ(68, AddFrames(1));
55 EXPECT_EQ(90, AddFrames(1));
56 EXPECT_EQ(113, AddFrames(1));
58 // Verify that adding frames one frame at a time matches the timestamp
59 // returned if the same number of frames are added all at once.
60 base::TimeDelta timestamp_1 = helper_.GetTimestamp();
61 helper_.SetBaseTimestamp(kNoTimestamp());
62 EXPECT_TRUE(kNoTimestamp() == helper_.base_timestamp());
63 helper_.SetBaseTimestamp(base::TimeDelta());
64 EXPECT_EQ(0, helper_.GetTimestamp().InMicroseconds());
66 helper_.AddFrames(5);
67 EXPECT_EQ(113, helper_.GetTimestamp().InMicroseconds());
68 EXPECT_TRUE(timestamp_1 == helper_.GetTimestamp());
72 TEST_F(AudioTimestampHelperTest, GetDuration) {
73 helper_.SetBaseTimestamp(base::TimeDelta::FromMicroseconds(100));
75 int frame_count = 5;
76 int64 expected_durations[] = { 113, 113, 114, 113, 113, 114 };
77 for (size_t i = 0; i < arraysize(expected_durations); ++i) {
78 base::TimeDelta duration = helper_.GetFrameDuration(frame_count);
79 EXPECT_EQ(expected_durations[i], duration.InMicroseconds());
81 base::TimeDelta timestamp_1 = helper_.GetTimestamp() + duration;
82 helper_.AddFrames(frame_count);
83 base::TimeDelta timestamp_2 = helper_.GetTimestamp();
84 EXPECT_TRUE(timestamp_1 == timestamp_2);
88 TEST_F(AudioTimestampHelperTest, GetFramesToTarget) {
89 // Verify GetFramesToTarget() rounding behavior.
90 // 1 frame @ 44100 is ~22.67573 microseconds,
92 // Test values less than half of the frame duration.
93 TestGetFramesToTargetRange(0, 0, 11);
95 // Test values between half the frame duration & the
96 // full frame duration.
97 TestGetFramesToTargetRange(1, 12, 22);
99 // Verify that the same number of frames is returned up
100 // to the next half a frame.
101 TestGetFramesToTargetRange(1, 23, 34);
103 // Verify the next 3 ranges.
104 TestGetFramesToTargetRange(2, 35, 56);
105 TestGetFramesToTargetRange(3, 57, 79);
106 TestGetFramesToTargetRange(4, 80, 102);
107 TestGetFramesToTargetRange(5, 103, 124);
109 // Add frames to the helper so negative frame counts can be tested.
110 helper_.AddFrames(5);
112 // Note: The timestamp ranges must match the positive values
113 // tested above to verify that the code is rounding properly.
114 TestGetFramesToTargetRange(0, 103, 124);
115 TestGetFramesToTargetRange(-1, 80, 102);
116 TestGetFramesToTargetRange(-2, 57, 79);
117 TestGetFramesToTargetRange(-3, 35, 56);
118 TestGetFramesToTargetRange(-4, 12, 34);
119 TestGetFramesToTargetRange(-5, 0, 11);
122 } // namespace media