• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <stdio.h>
21 
22 #include <audio_utils/TimestampVerifier.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();
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