• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2017 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 #ifndef CTS_MEDIA_TEST_AAUDIO_UTILS_H
17 #define CTS_MEDIA_TEST_AAUDIO_UTILS_H
18 
19 #include <dlfcn.h>
20 #include <map>
21 #include <sys/system_properties.h>
22 
23 #include <aaudio/AAudio.h>
24 
25 int64_t getNanoseconds(clockid_t clockId = CLOCK_MONOTONIC);
26 const char* performanceModeToString(aaudio_performance_mode_t mode);
27 const char* sharingModeToString(aaudio_sharing_mode_t mode);
28 
29 static constexpr const char* FEATURE_PLAYBACK = "android.hardware.audio.output";
30 static constexpr const char* FEATURE_RECORDING = "android.hardware.microphone";
31 static constexpr const char* FEATURE_LOW_LATENCY = "android.hardware.audio.low_latency";
32 bool deviceSupportsFeature(const char* feature);
33 
34 class StreamBuilderHelper {
35   public:
36     struct Parameters {
37         int32_t sampleRate;
38         int32_t channelCount;
39         aaudio_format_t dataFormat;
40         aaudio_sharing_mode_t sharingMode;
41         aaudio_performance_mode_t perfMode;
42     };
43 
44     void initBuilder();
45     void createAndVerifyStream(bool *success);
46     void close();
47 
startStream()48     void startStream() {
49         streamCommand(&AAudioStream_requestStart,
50                 AAUDIO_STREAM_STATE_STARTING, AAUDIO_STREAM_STATE_STARTED);
51     }
pauseStream()52     void pauseStream() {
53         streamCommand(&AAudioStream_requestPause,
54                 AAUDIO_STREAM_STATE_PAUSING, AAUDIO_STREAM_STATE_PAUSED);
55     }
stopStream()56     void stopStream() {
57         streamCommand(&AAudioStream_requestStop,
58                 AAUDIO_STREAM_STATE_STOPPING, AAUDIO_STREAM_STATE_STOPPED);
59     }
flushStream()60     void flushStream() {
61         streamCommand(&AAudioStream_requestFlush,
62                 AAUDIO_STREAM_STATE_FLUSHING, AAUDIO_STREAM_STATE_FLUSHED);
63     }
64 
builder()65     AAudioStreamBuilder* builder() const { return mBuilder; }
stream()66     AAudioStream* stream() const { return mStream; }
actual()67     const Parameters& actual() const { return mActual; }
framesPerBurst()68     int32_t framesPerBurst() const { return mFramesPerBurst; }
69 
70   protected:
71     StreamBuilderHelper(aaudio_direction_t direction, int32_t sampleRate,
72             int32_t channelCount, aaudio_format_t dataFormat,
73             aaudio_sharing_mode_t sharingMode, aaudio_performance_mode_t perfMode);
74     ~StreamBuilderHelper();
75 
76     typedef aaudio_result_t (StreamCommand)(AAudioStream*);
77     void streamCommand(
78             StreamCommand cmd, aaudio_stream_state_t fromState, aaudio_stream_state_t toState);
79 
80     static const std::map<aaudio_performance_mode_t, int64_t> sMaxFramesPerBurstMs;
81     const aaudio_direction_t mDirection;
82     const Parameters mRequested;
83     Parameters mActual;
84     int32_t mFramesPerBurst;
85     AAudioStreamBuilder *mBuilder;
86     AAudioStream *mStream;
87 };
88 
89 class InputStreamBuilderHelper : public StreamBuilderHelper {
90   public:
91     InputStreamBuilderHelper(
92             aaudio_sharing_mode_t requestedSharingMode,
93             aaudio_performance_mode_t requestedPerfMode,
94             aaudio_format_t requestedFormat = AAUDIO_FORMAT_PCM_FLOAT);
95 };
96 
97 class OutputStreamBuilderHelper : public StreamBuilderHelper {
98   public:
99     OutputStreamBuilderHelper(
100             aaudio_sharing_mode_t requestedSharingMode,
101             aaudio_performance_mode_t requestedPerfMode,
102             aaudio_format_t requestedFormat = AAUDIO_FORMAT_PCM_I16);
103     void initBuilder();
104     void createAndVerifyStream(bool *success);
105 
106   private:
107     const int32_t kBufferCapacityFrames = 2000;
108 };
109 
110 
111 #define LIB_AAUDIO_NAME          "libaaudio.so"
112 #define FUNCTION_IS_MMAP         "AAudioStream_isMMapUsed"
113 #define FUNCTION_SET_MMAP_POLICY "AAudio_setMMapPolicy"
114 #define FUNCTION_GET_MMAP_POLICY "AAudio_getMMapPolicy"
115 
116 enum {
117     AAUDIO_POLICY_UNSPECIFIED = 0,
118 /* These definitions are from aaudio/AAudioTesting.h */
119     AAUDIO_POLICY_NEVER = 1,
120     AAUDIO_POLICY_AUTO = 2,
121     AAUDIO_POLICY_ALWAYS = 3
122 };
123 typedef int32_t aaudio_policy_t;
124 
125 /**
126  * Call some AAudio test routines that are not part of the normal API.
127  */
128 class AAudioExtensions {
129 public:
130     AAudioExtensions();
131 
isPolicyEnabled(int32_t policy)132     static bool isPolicyEnabled(int32_t policy) {
133         return (policy == AAUDIO_POLICY_AUTO || policy == AAUDIO_POLICY_ALWAYS);
134     }
135 
getInstance()136     static AAudioExtensions &getInstance() {
137         static AAudioExtensions instance;
138         return instance;
139     }
140 
getMMapPolicyProperty()141     static int getMMapPolicyProperty() {
142         return getIntegerProperty("aaudio.mmap_policy", AAUDIO_POLICY_UNSPECIFIED);
143     }
144 
getMMapPolicy()145     aaudio_policy_t getMMapPolicy() {
146         if (!mFunctionsLoaded) return -1;
147         return mAAudio_getMMapPolicy();
148     }
149 
setMMapPolicy(aaudio_policy_t policy)150     int32_t setMMapPolicy(aaudio_policy_t policy) {
151         if (!mFunctionsLoaded) return -1;
152         return mAAudio_setMMapPolicy(policy);
153     }
154 
isMMapUsed(AAudioStream * aaudioStream)155     bool isMMapUsed(AAudioStream *aaudioStream) {
156         if (!mFunctionsLoaded) return false;
157         return mAAudioStream_isMMap(aaudioStream);
158     }
159 
setMMapEnabled(bool enabled)160     int32_t setMMapEnabled(bool enabled) {
161         return setMMapPolicy(enabled ? AAUDIO_POLICY_AUTO : AAUDIO_POLICY_NEVER);
162     }
163 
isMMapEnabled()164     bool isMMapEnabled() {
165         return isPolicyEnabled(mAAudio_getMMapPolicy());
166     }
167 
isMMapSupported()168     bool isMMapSupported() const {
169         return mMMapSupported;
170     }
171 
isMMapExclusiveSupported()172     bool isMMapExclusiveSupported() const {
173         return mMMapExclusiveSupported;
174     }
175 
176 private:
177 
178     static int getIntegerProperty(const char *name, int defaultValue);
179 
180     /**
181      * Load some AAudio test functions.
182      * This should only be called once from the constructor.
183      * @return true if it succeeds
184      */
185     bool loadLibrary();
186 
187     bool      mFunctionsLoaded = false;
188     void     *mLibHandle = nullptr;
189     bool    (*mAAudioStream_isMMap)(AAudioStream *stream) = nullptr;
190     int32_t (*mAAudio_setMMapPolicy)(aaudio_policy_t policy) = nullptr;
191     aaudio_policy_t (*mAAudio_getMMapPolicy)() = nullptr;
192 
193     const bool   mMMapSupported;
194     const bool   mMMapExclusiveSupported;
195 };
196 
197 #endif  // CTS_MEDIA_TEST_AAUDIO_UTILS_H
198