audio_timestamp_helper_unittest.cc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. #include "media/base/audio_timestamp_helper.h"
  5. #include <stddef.h>
  6. #include <stdint.h>
  7. #include "media/base/timestamp_constants.h"
  8. #include "testing/gtest/include/gtest/gtest.h"
  9. namespace media {
  10. namespace {
  11. const int k48kHz = 48000;
  12. } // namespace
  13. static const int kDefaultSampleRate = 44100;
  14. class AudioTimestampHelperTest : public ::testing::Test {
  15. public:
  16. AudioTimestampHelperTest() : helper_(kDefaultSampleRate) {
  17. helper_.SetBaseTimestamp(base::TimeDelta());
  18. }
  19. AudioTimestampHelperTest(const AudioTimestampHelperTest&) = delete;
  20. AudioTimestampHelperTest& operator=(const AudioTimestampHelperTest&) = delete;
  21. // Adds frames to the helper and returns the current timestamp in
  22. // microseconds.
  23. int64_t AddFrames(int frames) {
  24. helper_.AddFrames(frames);
  25. return helper_.GetTimestamp().InMicroseconds();
  26. }
  27. int64_t FramesToTarget(int target_in_microseconds) {
  28. return helper_.GetFramesToTarget(
  29. base::Microseconds(target_in_microseconds));
  30. }
  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. protected:
  38. AudioTimestampHelper helper_;
  39. };
  40. TEST_F(AudioTimestampHelperTest, FramesToTime) {
  41. // Negative value.
  42. EXPECT_EQ(base::Seconds(-1),
  43. AudioTimestampHelper::FramesToTime(-48000, k48kHz));
  44. // Zero.
  45. EXPECT_EQ(base::Microseconds(0),
  46. AudioTimestampHelper::FramesToTime(0, k48kHz));
  47. // One frame.
  48. EXPECT_EQ(base::Microseconds(20),
  49. AudioTimestampHelper::FramesToTime(1, k48kHz));
  50. // Exact value with maximum precision of TimeDelta.
  51. EXPECT_EQ(base::Microseconds(15625),
  52. AudioTimestampHelper::FramesToTime(750, k48kHz));
  53. // One second.
  54. EXPECT_EQ(base::Seconds(1),
  55. AudioTimestampHelper::FramesToTime(48000, k48kHz));
  56. // Argument and return value exceeding 32 bits.
  57. EXPECT_EQ(base::Seconds(1000000),
  58. AudioTimestampHelper::FramesToTime(48000000000, k48kHz));
  59. }
  60. TEST_F(AudioTimestampHelperTest, TimeToFrames) {
  61. // Negative value.
  62. EXPECT_EQ(-48000,
  63. AudioTimestampHelper::TimeToFrames(base::Seconds(-1), k48kHz));
  64. // Zero.
  65. EXPECT_EQ(0,
  66. AudioTimestampHelper::TimeToFrames(base::Microseconds(0), k48kHz));
  67. // Duration of each frame is 20.833 microseconds. The result is rounded to
  68. // integral.
  69. EXPECT_EQ(0,
  70. AudioTimestampHelper::TimeToFrames(base::Microseconds(10), k48kHz));
  71. EXPECT_EQ(1,
  72. AudioTimestampHelper::TimeToFrames(base::Microseconds(20), k48kHz));
  73. EXPECT_EQ(1,
  74. AudioTimestampHelper::TimeToFrames(base::Microseconds(21), k48kHz));
  75. // Exact value with maximum precision of TimeDelta.
  76. EXPECT_EQ(750, AudioTimestampHelper::TimeToFrames(base::Microseconds(15625),
  77. k48kHz));
  78. // One second.
  79. EXPECT_EQ(48000,
  80. AudioTimestampHelper::TimeToFrames(base::Seconds(1), k48kHz));
  81. // Argument and return value exceeding 32 bits.
  82. EXPECT_EQ(48000000000,
  83. AudioTimestampHelper::TimeToFrames(base::Seconds(1000000), k48kHz));
  84. }
  85. TEST_F(AudioTimestampHelperTest, Basic) {
  86. EXPECT_EQ(0, helper_.GetTimestamp().InMicroseconds());
  87. // Verify that the output timestamp is always rounded down to the
  88. // nearest microsecond. 1 frame @ 44100 is ~22.67573 microseconds,
  89. // which is why the timestamp sometimes increments by 23 microseconds
  90. // and other times it increments by 22 microseconds.
  91. EXPECT_EQ(0, AddFrames(0));
  92. EXPECT_EQ(22, AddFrames(1));
  93. EXPECT_EQ(45, AddFrames(1));
  94. EXPECT_EQ(68, AddFrames(1));
  95. EXPECT_EQ(90, AddFrames(1));
  96. EXPECT_EQ(113, AddFrames(1));
  97. // Verify that adding frames one frame at a time matches the timestamp
  98. // returned if the same number of frames are added all at once.
  99. base::TimeDelta timestamp_1 = helper_.GetTimestamp();
  100. helper_.SetBaseTimestamp(kNoTimestamp);
  101. EXPECT_TRUE(kNoTimestamp == helper_.base_timestamp());
  102. helper_.SetBaseTimestamp(base::TimeDelta());
  103. EXPECT_EQ(0, helper_.GetTimestamp().InMicroseconds());
  104. helper_.AddFrames(5);
  105. EXPECT_EQ(113, helper_.GetTimestamp().InMicroseconds());
  106. EXPECT_TRUE(timestamp_1 == helper_.GetTimestamp());
  107. }
  108. TEST_F(AudioTimestampHelperTest, GetDuration) {
  109. helper_.SetBaseTimestamp(base::Microseconds(100));
  110. int frame_count = 5;
  111. int64_t expected_durations[] = {113, 113, 114, 113, 113, 114};
  112. for (size_t i = 0; i < std::size(expected_durations); ++i) {
  113. base::TimeDelta duration = helper_.GetFrameDuration(frame_count);
  114. EXPECT_EQ(expected_durations[i], duration.InMicroseconds());
  115. base::TimeDelta timestamp_1 = helper_.GetTimestamp() + duration;
  116. helper_.AddFrames(frame_count);
  117. base::TimeDelta timestamp_2 = helper_.GetTimestamp();
  118. EXPECT_TRUE(timestamp_1 == timestamp_2);
  119. }
  120. }
  121. TEST_F(AudioTimestampHelperTest, GetFramesToTarget) {
  122. // Verify GetFramesToTarget() rounding behavior.
  123. // 1 frame @ 44100 is ~22.67573 microseconds,
  124. // Test values less than half of the frame duration.
  125. TestGetFramesToTargetRange(0, 0, 11);
  126. // Test values between half the frame duration & the
  127. // full frame duration.
  128. TestGetFramesToTargetRange(1, 12, 22);
  129. // Verify that the same number of frames is returned up
  130. // to the next half a frame.
  131. TestGetFramesToTargetRange(1, 23, 34);
  132. // Verify the next 3 ranges.
  133. TestGetFramesToTargetRange(2, 35, 56);
  134. TestGetFramesToTargetRange(3, 57, 79);
  135. TestGetFramesToTargetRange(4, 80, 102);
  136. TestGetFramesToTargetRange(5, 103, 124);
  137. // Add frames to the helper so negative frame counts can be tested.
  138. helper_.AddFrames(5);
  139. // Note: The timestamp ranges must match the positive values
  140. // tested above to verify that the code is rounding properly.
  141. TestGetFramesToTargetRange(0, 103, 124);
  142. TestGetFramesToTargetRange(-1, 80, 102);
  143. TestGetFramesToTargetRange(-2, 57, 79);
  144. TestGetFramesToTargetRange(-3, 35, 56);
  145. TestGetFramesToTargetRange(-4, 12, 34);
  146. TestGetFramesToTargetRange(-5, 0, 11);
  147. }
  148. } // namespace media