Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d1bdb20b authored by Phil Burk's avatar Phil Burk
Browse files

aaudio: unit test for IsochronousClockModel

Test: adb push ${OUT}/data/nativetest64/test_clock_model/test_clock_model /system/bin/.
Test: adb shell test_clock_model
Change-Id: I6736a02674b4f8f723799e76be7d441c46f01de6
parent fd771a4f
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -18,6 +18,18 @@ cc_test {
    ],
}

cc_test {
    name: "test_clock_model",
    defaults: ["libaaudio_tests_defaults"],
    srcs: ["test_clock_model.cpp"],
    shared_libs: [
        "libaaudio",
        "libaudioutils",
        "libcutils",
        "libutils",
    ],
}

cc_test {
    name: "test_block_adapter",
    defaults: ["libaaudio_tests_defaults"],
+115 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// Unit tests for Isochronous Clock Model

#include <math.h>
#include <stdlib.h>


#include <aaudio/AAudio.h>
#include <audio_utils/clock.h>
#include <client/IsochronousClockModel.h>
#include <gtest/gtest.h>

using namespace aaudio;

// We can use arbitrary values here because we are not opening a real audio stream.
#define SAMPLE_RATE             48000
#define HW_FRAMES_PER_BURST     48
#define NANOS_PER_BURST         (NANOS_PER_SECOND * HW_FRAMES_PER_BURST / SAMPLE_RATE)

class ClockModelTestFixture: public ::testing::Test {
public:
    ClockModelTestFixture() {
    }

    void SetUp() {
        model.setSampleRate(SAMPLE_RATE);
        model.setFramesPerBurst(HW_FRAMES_PER_BURST);
    }

    void TearDown() {

    }

    ~ClockModelTestFixture()  {
        // cleanup any pending stuff, but no exceptions allowed
    }

    IsochronousClockModel model;
};

// Check default setup.
TEST_F(ClockModelTestFixture, clock_setup) {
    ASSERT_EQ(SAMPLE_RATE, model.getSampleRate());
    ASSERT_EQ(HW_FRAMES_PER_BURST, model.getFramesPerBurst());
}

// Test delta calculations.
TEST_F(ClockModelTestFixture, clock_deltas) {
    int64_t position = model.convertDeltaTimeToPosition(NANOS_PER_SECOND);
    ASSERT_EQ(SAMPLE_RATE, position);

    // Deltas are not quantized.
    // Compare time to the equivalent position in frames.
    constexpr int64_t kNanosPerBurst = HW_FRAMES_PER_BURST * NANOS_PER_SECOND / SAMPLE_RATE;
    position = model.convertDeltaTimeToPosition(NANOS_PER_SECOND + (kNanosPerBurst / 2));
    ASSERT_EQ(SAMPLE_RATE + (HW_FRAMES_PER_BURST / 2), position);

    int64_t time = model.convertDeltaPositionToTime(SAMPLE_RATE);
    ASSERT_EQ(NANOS_PER_SECOND, time);

    // Compare position in frames to the equivalent time.
    time = model.convertDeltaPositionToTime(SAMPLE_RATE + (HW_FRAMES_PER_BURST / 2));
    ASSERT_EQ(NANOS_PER_SECOND + (kNanosPerBurst / 2), time);
}

// start() should force the internal markers
TEST_F(ClockModelTestFixture, clock_start) {
    const int64_t startTime = 100000;
    model.start(startTime);

    int64_t position = model.convertTimeToPosition(startTime);
    EXPECT_EQ(0, position);

    int64_t time = model.convertPositionToTime(position);
    EXPECT_EQ(startTime, time);

    time = startTime + (500 * NANOS_PER_MICROSECOND);
    position = model.convertTimeToPosition(time);
    EXPECT_EQ(0, position);
}

// timestamps moves the window if outside the bounds
// TODO test nudging the window
TEST_F(ClockModelTestFixture, clock_timestamp) {
    const int64_t startTime = 100000000;
    model.start(startTime);

    const int64_t position = HW_FRAMES_PER_BURST; // hardware
    int64_t markerTime = startTime + NANOS_PER_MILLISECOND + (200 * NANOS_PER_MICROSECOND);

    // Should set marker.
    model.processTimestamp(position, markerTime);
    EXPECT_EQ(position, model.convertTimeToPosition(markerTime));

    // convertTimeToPosition rounds down
    EXPECT_EQ(position, model.convertTimeToPosition(markerTime + (73 * NANOS_PER_MICROSECOND)));

    // convertPositionToTime rounds up
    EXPECT_EQ(markerTime + NANOS_PER_BURST, model.convertPositionToTime(position + 17));
}