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