timestampverifier_tests.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright 2018 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. //#define LOG_NDEBUG 0
  17. #define LOG_TAG "audio_utils_timestampverifier_tests"
  18. #include <stdio.h>
  19. #include <audio_utils/TimestampVerifier.h>
  20. #include <gtest/gtest.h>
  21. // Ensure that all TimestampVerifier mutators are really constexpr and free from
  22. // nasty system calls (in case called from a SCHED_FIFO thread).
  23. static constexpr auto makeVerifier(
  24. size_t N, uint32_t sampleRate, size_t errors, size_t discontinuities) {
  25. android::TimestampVerifier<int64_t, int64_t> tv;
  26. int64_t f = 0;
  27. int64_t t = 0;
  28. for (size_t i = 0; i < N; ++i) {
  29. tv.add(f, t, sampleRate);
  30. f += sampleRate;
  31. t += (int64_t)1e9;
  32. }
  33. for (size_t i = 0; i < discontinuities; ++i) {
  34. tv.discontinuity();
  35. }
  36. for (size_t i = 0; i < errors; ++i) {
  37. tv.error();
  38. }
  39. return tv;
  40. }
  41. TEST(TimestampVerifier, sanity)
  42. {
  43. constexpr android::TimestampVerifier<int64_t, int64_t> tv;
  44. // The timestamp verifier must be embeddable in a memcpy structure just like pod.
  45. // We use is_trivially_copyable and is_trivially_destructible for this test.
  46. static_assert(std::is_trivially_copyable<decltype(tv)>::value,
  47. "TimestampVerifier must be trivially copyable");
  48. static_assert(std::is_trivially_destructible<decltype(tv)>::value,
  49. "TimestampVerifier must be trivially destructible");
  50. constexpr android::audio_utils::Statistics<double> s = tv.getJitterMs();
  51. EXPECT_EQ(std::numeric_limits<double>::infinity(), s.getMin());
  52. EXPECT_EQ(-std::numeric_limits<double>::infinity(), s.getMax());
  53. constexpr int64_t frames[] { 0, 48000 };
  54. constexpr int64_t timeNs[] { 0, 1000000000 };
  55. constexpr android::TimestampVerifier<int64_t, int64_t> tv2(frames, timeNs, 48000);
  56. EXPECT_EQ(0., tv2.getJitterMs().getMax());
  57. EXPECT_EQ(0., tv2.getJitterMs().getMin());
  58. EXPECT_EQ(0., tv2.getJitterMs().getMean());
  59. EXPECT_EQ(1, tv2.getJitterMs().getN());
  60. // We should get a perfect straight line estimate as there is no noise.
  61. double a, b, r2;
  62. tv2.estimateSampleRate(a, b, r2);
  63. EXPECT_EQ(0., a);
  64. EXPECT_EQ(48000., b);
  65. EXPECT_NEAR(1., r2, std::numeric_limits<double>::epsilon());
  66. constexpr android::TimestampVerifier<int64_t, int64_t> tv3 =
  67. makeVerifier(8 /* N */, 48000 /* sampleRate */, 10 /* errors */, 10 /* disc */);
  68. EXPECT_EQ(8, tv3.getN());
  69. EXPECT_EQ(10, tv3.getErrors());
  70. EXPECT_EQ(1, tv3.getDiscontinuities()); // consecutive discontinuities read as 1.
  71. EXPECT_EQ(0., tv3.getJitterMs().getMax());
  72. EXPECT_EQ(0., tv3.getJitterMs().getMin());
  73. EXPECT_EQ(0., tv3.getJitterMs().getMean());
  74. constexpr auto first = tv3.getFirstTimestamp();
  75. constexpr auto last = tv3.getLastTimestamp();
  76. EXPECT_EQ(0, first.mFrames);
  77. EXPECT_EQ(0, first.mTimeNs);
  78. EXPECT_EQ(48000 * (8 - 1), last.mFrames);
  79. EXPECT_EQ((int64_t)1e9 * (8 - 1), last.mTimeNs);
  80. EXPECT_EQ((uint32_t)48000, tv3.getSampleRate());
  81. EXPECT_EQ(0, tv3.getColds());
  82. tv3.estimateSampleRate(a, b, r2);
  83. EXPECT_EQ(0., a);
  84. EXPECT_EQ(48000., b);
  85. EXPECT_NEAR(1., r2, std::numeric_limits<double>::epsilon());
  86. }