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 #include "media/base/audio_timestamp_helper.h"
6 #include "media/base/buffers.h"
7 #include "testing/gtest/include/gtest/gtest.h"
8
9 namespace media {
10
11 static const int kDefaultSampleRate = 44100;
12
13 class AudioTimestampHelperTest : public ::testing::Test {
14 public:
AudioTimestampHelperTest()15 AudioTimestampHelperTest() : helper_(kDefaultSampleRate) {
16 helper_.SetBaseTimestamp(base::TimeDelta());
17 }
18
19 // Adds frames to the helper and returns the current timestamp in
20 // microseconds.
AddFrames(int frames)21 int64 AddFrames(int frames) {
22 helper_.AddFrames(frames);
23 return helper_.GetTimestamp().InMicroseconds();
24 }
25
FramesToTarget(int target_in_microseconds)26 int64 FramesToTarget(int target_in_microseconds) {
27 return helper_.GetFramesToTarget(
28 base::TimeDelta::FromMicroseconds(target_in_microseconds));
29 }
30
TestGetFramesToTargetRange(int frame_count,int start,int end)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.";
35 }
36 }
37
38 protected:
39 AudioTimestampHelper helper_;
40
41 DISALLOW_COPY_AND_ASSIGN(AudioTimestampHelperTest);
42 };
43
TEST_F(AudioTimestampHelperTest,Basic)44 TEST_F(AudioTimestampHelperTest, Basic) {
45 EXPECT_EQ(0, helper_.GetTimestamp().InMicroseconds());
46
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));
57
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());
65
66 helper_.AddFrames(5);
67 EXPECT_EQ(113, helper_.GetTimestamp().InMicroseconds());
68 EXPECT_TRUE(timestamp_1 == helper_.GetTimestamp());
69 }
70
71
TEST_F(AudioTimestampHelperTest,GetDuration)72 TEST_F(AudioTimestampHelperTest, GetDuration) {
73 helper_.SetBaseTimestamp(base::TimeDelta::FromMicroseconds(100));
74
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());
80
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);
85 }
86 }
87
TEST_F(AudioTimestampHelperTest,GetFramesToTarget)88 TEST_F(AudioTimestampHelperTest, GetFramesToTarget) {
89 // Verify GetFramesToTarget() rounding behavior.
90 // 1 frame @ 44100 is ~22.67573 microseconds,
91
92 // Test values less than half of the frame duration.
93 TestGetFramesToTargetRange(0, 0, 11);
94
95 // Test values between half the frame duration & the
96 // full frame duration.
97 TestGetFramesToTargetRange(1, 12, 22);
98
99 // Verify that the same number of frames is returned up
100 // to the next half a frame.
101 TestGetFramesToTargetRange(1, 23, 34);
102
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);
108
109 // Add frames to the helper so negative frame counts can be tested.
110 helper_.AddFrames(5);
111
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);
120 }
121
122 } // namespace media
123