Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(356)

Side by Side Diff: media/base/audio_timestamp_helper_unittest.cc

Issue 2101303004: Pass delay and timestamp to AudioSourceCallback::OnMoreData. (Closed) Base URL: https://chromium.googlesource.com/chromium/src@master
Patch Set: Fix Mac CQ errors. Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <stddef.h> 5 #include <stddef.h>
6 #include <stdint.h> 6 #include <stdint.h>
7 7
8 #include "base/macros.h" 8 #include "base/macros.h"
9 #include "media/base/audio_timestamp_helper.h" 9 #include "media/base/audio_timestamp_helper.h"
10 #include "media/base/timestamp_constants.h" 10 #include "media/base/timestamp_constants.h"
11 #include "testing/gtest/include/gtest/gtest.h" 11 #include "testing/gtest/include/gtest/gtest.h"
12 12
13 namespace media { 13 namespace media {
14 14
15 namespace {
16
17 const int k48kHz = 48000;
18
19 } // namespace
20
15 static const int kDefaultSampleRate = 44100; 21 static const int kDefaultSampleRate = 44100;
16 22
17 class AudioTimestampHelperTest : public ::testing::Test { 23 class AudioTimestampHelperTest : public ::testing::Test {
18 public: 24 public:
19 AudioTimestampHelperTest() : helper_(kDefaultSampleRate) { 25 AudioTimestampHelperTest() : helper_(kDefaultSampleRate) {
20 helper_.SetBaseTimestamp(base::TimeDelta()); 26 helper_.SetBaseTimestamp(base::TimeDelta());
21 } 27 }
22 28
23 // Adds frames to the helper and returns the current timestamp in 29 // Adds frames to the helper and returns the current timestamp in
24 // microseconds. 30 // microseconds.
(...skipping 10 matching lines...) Expand all
35 void TestGetFramesToTargetRange(int frame_count, int start, int end) { 41 void TestGetFramesToTargetRange(int frame_count, int start, int end) {
36 for (int i = start; i <= end; ++i) { 42 for (int i = start; i <= end; ++i) {
37 EXPECT_EQ(frame_count, FramesToTarget(i)) << " Failure for timestamp " 43 EXPECT_EQ(frame_count, FramesToTarget(i)) << " Failure for timestamp "
38 << i << " us."; 44 << i << " us.";
39 } 45 }
40 } 46 }
41 47
42 protected: 48 protected:
43 AudioTimestampHelper helper_; 49 AudioTimestampHelper helper_;
44 50
51 private:
45 DISALLOW_COPY_AND_ASSIGN(AudioTimestampHelperTest); 52 DISALLOW_COPY_AND_ASSIGN(AudioTimestampHelperTest);
46 }; 53 };
47 54
55 TEST_F(AudioTimestampHelperTest, FramesToTime) {
56 // Negative value.
57 EXPECT_EQ(base::TimeDelta::FromSeconds(-1),
58 AudioTimestampHelper::FramesToTime(-48000, k48kHz));
59 // Zero.
60 EXPECT_EQ(base::TimeDelta::FromMicroseconds(0),
61 AudioTimestampHelper::FramesToTime(0, k48kHz));
62 // One frame.
63 EXPECT_EQ(base::TimeDelta::FromMicroseconds(20),
64 AudioTimestampHelper::FramesToTime(1, k48kHz));
65 // Exact value with maximum precision of TimeDelta.
66 EXPECT_EQ(base::TimeDelta::FromMicroseconds(15625),
67 AudioTimestampHelper::FramesToTime(750, k48kHz));
68 // One second.
69 EXPECT_EQ(base::TimeDelta::FromSeconds(1),
70 AudioTimestampHelper::FramesToTime(48000, k48kHz));
71 // Argument and return value exceeding 32 bits.
72 EXPECT_EQ(base::TimeDelta::FromSeconds(1000000),
73 AudioTimestampHelper::FramesToTime(48000000000, k48kHz));
74 }
75
76 TEST_F(AudioTimestampHelperTest, TimeToFrames) {
77 // Negative value.
78 EXPECT_EQ(-48000, AudioTimestampHelper::TimeToFrames(
79 base::TimeDelta::FromSeconds(-1), k48kHz));
80 // Zero.
81 EXPECT_EQ(0, AudioTimestampHelper::TimeToFrames(
82 base::TimeDelta::FromMicroseconds(0), k48kHz));
83 // Any duration less than 21 microseconds will return zero frames at 48 kHz
84 // because each frame is 20.833 microseconds.
85 EXPECT_EQ(0, AudioTimestampHelper::TimeToFrames(
86 base::TimeDelta::FromMicroseconds(20), k48kHz));
87 EXPECT_EQ(1, AudioTimestampHelper::TimeToFrames(
88 base::TimeDelta::FromMicroseconds(21), k48kHz));
89 // Exact value with maximum precision of TimeDelta.
90 EXPECT_EQ(750, AudioTimestampHelper::TimeToFrames(
91 base::TimeDelta::FromMicroseconds(15625), k48kHz));
92 // One second.
93 EXPECT_EQ(48000, AudioTimestampHelper::TimeToFrames(
94 base::TimeDelta::FromSeconds(1), k48kHz));
95 // Argument and return value exceeding 32 bits.
96 EXPECT_EQ(48000000000, AudioTimestampHelper::TimeToFrames(
97 base::TimeDelta::FromSeconds(1000000), k48kHz));
98 }
99
48 TEST_F(AudioTimestampHelperTest, Basic) { 100 TEST_F(AudioTimestampHelperTest, Basic) {
49 EXPECT_EQ(0, helper_.GetTimestamp().InMicroseconds()); 101 EXPECT_EQ(0, helper_.GetTimestamp().InMicroseconds());
50 102
51 // Verify that the output timestamp is always rounded down to the 103 // Verify that the output timestamp is always rounded down to the
52 // nearest microsecond. 1 frame @ 44100 is ~22.67573 microseconds, 104 // nearest microsecond. 1 frame @ 44100 is ~22.67573 microseconds,
53 // which is why the timestamp sometimes increments by 23 microseconds 105 // which is why the timestamp sometimes increments by 23 microseconds
54 // and other times it increments by 22 microseconds. 106 // and other times it increments by 22 microseconds.
55 EXPECT_EQ(0, AddFrames(0)); 107 EXPECT_EQ(0, AddFrames(0));
56 EXPECT_EQ(22, AddFrames(1)); 108 EXPECT_EQ(22, AddFrames(1));
57 EXPECT_EQ(45, AddFrames(1)); 109 EXPECT_EQ(45, AddFrames(1));
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
117 // tested above to verify that the code is rounding properly. 169 // tested above to verify that the code is rounding properly.
118 TestGetFramesToTargetRange(0, 103, 124); 170 TestGetFramesToTargetRange(0, 103, 124);
119 TestGetFramesToTargetRange(-1, 80, 102); 171 TestGetFramesToTargetRange(-1, 80, 102);
120 TestGetFramesToTargetRange(-2, 57, 79); 172 TestGetFramesToTargetRange(-2, 57, 79);
121 TestGetFramesToTargetRange(-3, 35, 56); 173 TestGetFramesToTargetRange(-3, 35, 56);
122 TestGetFramesToTargetRange(-4, 12, 34); 174 TestGetFramesToTargetRange(-4, 12, 34);
123 TestGetFramesToTargetRange(-5, 0, 11); 175 TestGetFramesToTargetRange(-5, 0, 11);
124 } 176 }
125 177
126 } // namespace media 178 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698