1 /*
2 * Copyright (C) 2022 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 #include <cstdint>
18
19 #include "chre/pal/audio.h"
20 #include "chre/platform/condition_variable.h"
21 #include "chre/platform/linux/task_util/task_manager.h"
22 #include "chre/platform/mutex.h"
23 #include "chre/platform/shared/pal_system_api.h"
24 #include "chre/util/lock_guard.h"
25 #include "chre/util/macros.h"
26 #include "chre/util/optional.h"
27 #include "chre/util/time.h"
28 #include "chre/util/unique_ptr.h"
29 #include "gmock/gmock.h"
30 #include "gtest/gtest.h"
31
32 namespace {
33
34 using ::chre::ConditionVariable;
35 using ::chre::gChrePalSystemApi;
36 using ::chre::kOneMillisecondInNanoseconds;
37 using ::chre::LockGuard;
38 using ::chre::MakeUnique;
39 using ::chre::Mutex;
40 using ::chre::Nanoseconds;
41 using ::chre::Optional;
42 using ::chre::UniquePtr;
43
44 class Callbacks {
45 public:
audioDataEventCallback(struct chreAudioDataEvent * event)46 void audioDataEventCallback(struct chreAudioDataEvent *event) {
47 LockGuard<Mutex> lock(mMutex);
48 if (!mDataEvent.has_value()) {
49 mDataEvent = event;
50 mCondVarDataEvents.notify_one();
51 }
52 }
53
audioAvailabilityCallback(uint32_t handle,bool available)54 void audioAvailabilityCallback(uint32_t handle, bool available) {
55 UNUSED_VAR(handle);
56 UNUSED_VAR(available);
57 }
58
59 Optional<struct chreAudioDataEvent *> mDataEvent;
60
61 //! Synchronize access to class members.
62 Mutex mMutex;
63 ConditionVariable mCondVarDataEvents;
64 };
65
66 UniquePtr<Callbacks> gCallbacks = nullptr;
67
audioDataEventCallback(struct chreAudioDataEvent * event)68 void audioDataEventCallback(struct chreAudioDataEvent *event) {
69 if (gCallbacks != nullptr) {
70 gCallbacks->audioDataEventCallback(event);
71 }
72 }
73
audioAvailabilityCallback(uint32_t handle,bool available)74 void audioAvailabilityCallback(uint32_t handle, bool available) {
75 if (gCallbacks != nullptr) {
76 gCallbacks->audioAvailabilityCallback(handle, available);
77 }
78 }
79
80 class PalAudioTest : public testing::Test {
81 protected:
SetUp()82 void SetUp() override {
83 gCallbacks = MakeUnique<Callbacks>();
84 chre::TaskManagerSingleton::deinit();
85 chre::TaskManagerSingleton::init();
86 mApi = chrePalAudioGetApi(CHRE_PAL_AUDIO_API_CURRENT_VERSION);
87 ASSERT_NE(mApi, nullptr);
88 EXPECT_EQ(mApi->moduleVersion, CHRE_PAL_AUDIO_API_CURRENT_VERSION);
89 ASSERT_TRUE(mApi->open(&gChrePalSystemApi, &mPalCallbacks));
90 }
91
TearDown()92 void TearDown() override {
93 if (mApi != nullptr) {
94 mApi->close();
95 }
96 chre::TaskManagerSingleton::deinit();
97 gCallbacks = nullptr;
98 }
99
100 //! CHRE PAL implementation API.
101 const struct chrePalAudioApi *mApi;
102
103 const struct chrePalAudioCallbacks mPalCallbacks = {
104 .audioDataEventCallback = audioDataEventCallback,
105 .audioAvailabilityCallback = audioAvailabilityCallback,
106 };
107 };
108
TEST_F(PalAudioTest,GetAudioSourceInfoForExistingSource)109 TEST_F(PalAudioTest, GetAudioSourceInfoForExistingSource) {
110 struct chreAudioSource audioSource;
111
112 EXPECT_EQ(mApi->getSourceCount(), 1);
113 EXPECT_TRUE(mApi->getAudioSource(0, &audioSource));
114 EXPECT_STREQ(audioSource.name, "Test Source");
115 }
116
TEST_F(PalAudioTest,GetAudioSourceInfoForNonExistingSource)117 TEST_F(PalAudioTest, GetAudioSourceInfoForNonExistingSource) {
118 struct chreAudioSource audioSource;
119
120 EXPECT_EQ(mApi->getSourceCount(), 1);
121 EXPECT_FALSE(mApi->getAudioSource(10, &audioSource));
122 }
123
TEST_F(PalAudioTest,GetDataEvent)124 TEST_F(PalAudioTest, GetDataEvent) {
125 LockGuard<Mutex> lock(gCallbacks->mMutex);
126 EXPECT_TRUE(mApi->requestAudioDataEvent(0 /*handle*/, 1000 /*numSamples*/,
127 100 /*eventDelaysNs*/));
128 gCallbacks->mCondVarDataEvents.wait(gCallbacks->mMutex);
129 ASSERT_TRUE(gCallbacks->mDataEvent.has_value());
130 struct chreAudioDataEvent *event = gCallbacks->mDataEvent.value();
131 EXPECT_EQ(event->handle, 0);
132 EXPECT_EQ(event->sampleCount, 1000);
133
134 mApi->releaseAudioDataEvent(event);
135 }
136
137 } // namespace