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
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "audio_utils_timestampverifier_tests"
19
20 #include <audio_utils/TimestampVerifier.h>
21
22 #include <stdio.h>
23 #include <gtest/gtest.h>
24
25 // Ensure that all TimestampVerifier mutators are really constexpr and free from
26 // nasty system calls (in case called from a SCHED_FIFO thread).
makeVerifier(size_t N,uint32_t sampleRate,size_t errors,size_t discontinuities)27 static constexpr auto makeVerifier(
28 size_t N, uint32_t sampleRate, size_t errors, size_t discontinuities) {
29 android::TimestampVerifier<int64_t, int64_t> tv;
30
31 int64_t f = 0;
32 int64_t t = 0;
33 for (size_t i = 0; i < N; ++i) {
34 tv.add(f, t, sampleRate);
35 f += sampleRate;
36 t += (int64_t)1e9;
37 }
38 for (size_t i = 0; i < discontinuities; ++i) {
39 tv.discontinuity(tv.DISCONTINUITY_MODE_CONTINUOUS);
40 }
41 for (size_t i = 0; i < errors; ++i) {
42 tv.error();
43 }
44 return tv;
45 }
46
TEST(TimestampVerifier,sanity)47 TEST(TimestampVerifier, sanity)
48 {
49 constexpr android::TimestampVerifier<int64_t, int64_t> tv;
50
51 // The timestamp verifier must be embeddable in a memcpy structure just like pod.
52 // We use is_trivially_copyable and is_trivially_destructible for this test.
53 static_assert(std::is_trivially_copyable<decltype(tv)>::value,
54 "TimestampVerifier must be trivially copyable");
55 static_assert(std::is_trivially_destructible<decltype(tv)>::value,
56 "TimestampVerifier must be trivially destructible");
57
58 constexpr android::audio_utils::Statistics<double> s = tv.getJitterMs();
59
60 EXPECT_EQ(std::numeric_limits<double>::infinity(), s.getMin());
61 EXPECT_EQ(-std::numeric_limits<double>::infinity(), s.getMax());
62
63 constexpr int64_t frames[] { 0, 48000 };
64 constexpr int64_t timeNs[] { 0, 1000000000 };
65 constexpr android::TimestampVerifier<int64_t, int64_t> tv2(frames, timeNs, 48000);
66 EXPECT_EQ(0., tv2.getJitterMs().getMax());
67 EXPECT_EQ(0., tv2.getJitterMs().getMin());
68 EXPECT_EQ(0., tv2.getJitterMs().getMean());
69 EXPECT_EQ(1, tv2.getJitterMs().getN());
70
71 // We should get a perfect straight line estimate as there is no noise.
72 double a, b, r2;
73 tv2.estimateSampleRate(a, b, r2);
74 EXPECT_EQ(0., a);
75 EXPECT_EQ(48000., b);
76 EXPECT_NEAR(1., r2, std::numeric_limits<double>::epsilon());
77
78 constexpr android::TimestampVerifier<int64_t, int64_t> tv3 =
79 makeVerifier(8 /* N */, 48000 /* sampleRate */, 10 /* errors */, 10 /* disc */);
80 EXPECT_EQ(8, tv3.getN());
81 EXPECT_EQ(10, tv3.getErrors());
82 EXPECT_EQ(1, tv3.getDiscontinuities()); // consecutive discontinuities read as 1.
83 EXPECT_EQ(0., tv3.getJitterMs().getMax());
84 EXPECT_EQ(0., tv3.getJitterMs().getMin());
85 EXPECT_EQ(0., tv3.getJitterMs().getMean());
86
87 constexpr auto first = tv3.getFirstTimestamp();
88 constexpr auto last = tv3.getLastTimestamp();
89
90 EXPECT_EQ(0, first.mFrames);
91 EXPECT_EQ(0, first.mTimeNs);
92 EXPECT_EQ(48000 * (8 - 1), last.mFrames);
93 EXPECT_EQ((int64_t)1e9 * (8 - 1), last.mTimeNs);
94 EXPECT_EQ((uint32_t)48000, tv3.getSampleRate());
95 EXPECT_EQ(0, tv3.getColds());
96
97 tv3.estimateSampleRate(a, b, r2);
98 EXPECT_EQ(0., a);
99 EXPECT_EQ(48000., b);
100 EXPECT_NEAR(1., r2, std::numeric_limits<double>::epsilon());
101 }
102
TEST(TimestampVerifier,discontinuity_zero)103 TEST(TimestampVerifier, discontinuity_zero)
104 {
105 android::TimestampVerifier<int64_t, int64_t> tv;
106
107 // Add timestamps advancing at normal rate over 2 seconds
108 tv.add(0, 0, 48000);
109 tv.add(48000, 1000000000, 48000);
110 tv.add(96000, 2000000000, 48000);
111
112 // Raise (mode zero) discontinuity at "3 seconds"
113 tv.discontinuity(tv.DISCONTINUITY_MODE_ZERO);
114 // Add timestamp where frame count has reset to zero (and not advancing)
115 tv.add(0, 3000000000, 48000);
116
117 // The last corrected timestamp after discontinuity (mode zero) should be zeroed
118 EXPECT_EQ(0., tv.getLastCorrectedTimestamp().mFrames);
119 EXPECT_EQ(3000000000., tv.getLastCorrectedTimestamp().mTimeNs);
120
121 // Add timestamp where frame count has not advanced from zero, but time has advanced 100 ms more
122 tv.add(0, 3100000000, 48000);
123
124 // The last corrected frame should be the raw timestamp if not advancing at normal rate
125 EXPECT_EQ(0., tv.getLastCorrectedTimestamp().mFrames);
126 EXPECT_EQ(3100000000., tv.getLastCorrectedTimestamp().mTimeNs);
127
128 // Add imperfect normal advancing timestamps
129 tv.add(48000*0.9, 4100000000*1.1, 48000);
130 tv.add(96000*1.1, 5100000000*0.9, 48000);
131
132 // Last corrected timestamp frame count should not be raw (or zero) as timestamps are now
133 // advancing at a (imperfect) normal rate (but the time should, as implementation uses frame
134 // rather than time correction).
135 EXPECT_NE(0, tv.getLastCorrectedTimestamp().mFrames);
136 EXPECT_NE(96000*1.1, tv.getLastCorrectedTimestamp().mFrames);
137 EXPECT_EQ(5100000000*0.9, tv.getLastCorrectedTimestamp().mTimeNs);
138 }
139