1 /*
2  * Copyright (C) 2019 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 #include <aaudio/AAudio.h>
17 #include <aaudio/AAudioTesting.h>
18 
19 #include "AudioGlobal.h"
20 
21 /******************************************
22  * Static globals.
23  */
24 namespace aaudio {
25 
26 static aaudio_policy_t g_MMapPolicy = AAUDIO_UNSPECIFIED;
27 
AudioGlobal_getMMapPolicy()28 aaudio_policy_t AudioGlobal_getMMapPolicy() {
29   return g_MMapPolicy;
30 }
31 
AudioGlobal_setMMapPolicy(aaudio_policy_t policy)32 aaudio_result_t AudioGlobal_setMMapPolicy(aaudio_policy_t policy) {
33     aaudio_result_t result = AAUDIO_OK;
34     switch(policy) {
35         case AAUDIO_UNSPECIFIED:
36         case AAUDIO_POLICY_NEVER:
37         case AAUDIO_POLICY_AUTO:
38         case AAUDIO_POLICY_ALWAYS:
39             g_MMapPolicy = policy;
40             break;
41         default:
42             result = AAUDIO_ERROR_ILLEGAL_ARGUMENT;
43             break;
44     }
45     return result;
46 }
47 
48 #define AAUDIO_CASE_ENUM(name) case name: return #name
49 
AudioGlobal_convertResultToText(aaudio_result_t returnCode)50 const char* AudioGlobal_convertResultToText(aaudio_result_t returnCode) {
51     switch (returnCode) {
52         AAUDIO_CASE_ENUM(AAUDIO_OK);
53         AAUDIO_CASE_ENUM(AAUDIO_ERROR_DISCONNECTED);
54         AAUDIO_CASE_ENUM(AAUDIO_ERROR_ILLEGAL_ARGUMENT);
55         // reserved
56         AAUDIO_CASE_ENUM(AAUDIO_ERROR_INTERNAL);
57         AAUDIO_CASE_ENUM(AAUDIO_ERROR_INVALID_STATE);
58         // reserved
59         // reserved
60         AAUDIO_CASE_ENUM(AAUDIO_ERROR_INVALID_HANDLE);
61          // reserved
62         AAUDIO_CASE_ENUM(AAUDIO_ERROR_UNIMPLEMENTED);
63         AAUDIO_CASE_ENUM(AAUDIO_ERROR_UNAVAILABLE);
64         AAUDIO_CASE_ENUM(AAUDIO_ERROR_NO_FREE_HANDLES);
65         AAUDIO_CASE_ENUM(AAUDIO_ERROR_NO_MEMORY);
66         AAUDIO_CASE_ENUM(AAUDIO_ERROR_NULL);
67         AAUDIO_CASE_ENUM(AAUDIO_ERROR_TIMEOUT);
68         AAUDIO_CASE_ENUM(AAUDIO_ERROR_WOULD_BLOCK);
69         AAUDIO_CASE_ENUM(AAUDIO_ERROR_INVALID_FORMAT);
70         AAUDIO_CASE_ENUM(AAUDIO_ERROR_OUT_OF_RANGE);
71         AAUDIO_CASE_ENUM(AAUDIO_ERROR_NO_SERVICE);
72         AAUDIO_CASE_ENUM(AAUDIO_ERROR_INVALID_RATE);
73     }
74     return "Unrecognized";
75 }
76 
AudioGlobal_convertFormatToText(aaudio_format_t format)77 const char* AudioGlobal_convertFormatToText(aaudio_format_t format) {
78       switch (format) {
79         AAUDIO_CASE_ENUM(AAUDIO_FORMAT_UNSPECIFIED);
80         AAUDIO_CASE_ENUM(AAUDIO_FORMAT_INVALID);
81         AAUDIO_CASE_ENUM(AAUDIO_FORMAT_PCM_I16);
82         AAUDIO_CASE_ENUM(AAUDIO_FORMAT_PCM_FLOAT);
83     }
84     return "Unrecognized";
85 }
86 
AudioGlobal_convertDirectionToText(aaudio_direction_t direction)87 const char* AudioGlobal_convertDirectionToText(aaudio_direction_t direction) {
88       switch (direction) {
89         AAUDIO_CASE_ENUM(AAUDIO_DIRECTION_INPUT);
90         AAUDIO_CASE_ENUM(AAUDIO_DIRECTION_OUTPUT);
91     }
92     return "Unrecognized";
93 }
94 
AudioGlobal_convertPerformanceModeToText(aaudio_performance_mode_t mode)95 const char* AudioGlobal_convertPerformanceModeToText(aaudio_performance_mode_t mode) {
96       switch (mode) {
97         AAUDIO_CASE_ENUM(AAUDIO_PERFORMANCE_MODE_POWER_SAVING);
98         AAUDIO_CASE_ENUM(AAUDIO_PERFORMANCE_MODE_NONE);
99         AAUDIO_CASE_ENUM(AAUDIO_PERFORMANCE_MODE_LOW_LATENCY);
100     }
101     return "Unrecognized";
102 }
103 
AudioGlobal_convertSharingModeToText(aaudio_sharing_mode_t mode)104 const char* AudioGlobal_convertSharingModeToText(aaudio_sharing_mode_t mode) {
105       switch (mode) {
106         AAUDIO_CASE_ENUM(AAUDIO_SHARING_MODE_SHARED);
107         AAUDIO_CASE_ENUM(AAUDIO_SHARING_MODE_EXCLUSIVE);
108     }
109     return "Unrecognized";
110 }
111 
AudioGlobal_convertStreamStateToText(aaudio_stream_state_t state)112 const char* AudioGlobal_convertStreamStateToText(aaudio_stream_state_t state) {
113       switch (state) {
114         AAUDIO_CASE_ENUM(AAUDIO_STREAM_STATE_UNINITIALIZED);
115         AAUDIO_CASE_ENUM(AAUDIO_STREAM_STATE_UNKNOWN);
116         AAUDIO_CASE_ENUM(AAUDIO_STREAM_STATE_OPEN);
117         AAUDIO_CASE_ENUM(AAUDIO_STREAM_STATE_STARTING);
118         AAUDIO_CASE_ENUM(AAUDIO_STREAM_STATE_STARTED);
119         AAUDIO_CASE_ENUM(AAUDIO_STREAM_STATE_PAUSING);
120         AAUDIO_CASE_ENUM(AAUDIO_STREAM_STATE_PAUSED);
121         AAUDIO_CASE_ENUM(AAUDIO_STREAM_STATE_FLUSHING);
122         AAUDIO_CASE_ENUM(AAUDIO_STREAM_STATE_FLUSHED);
123         AAUDIO_CASE_ENUM(AAUDIO_STREAM_STATE_STOPPING);
124         AAUDIO_CASE_ENUM(AAUDIO_STREAM_STATE_STOPPED);
125         AAUDIO_CASE_ENUM(AAUDIO_STREAM_STATE_CLOSING);
126         AAUDIO_CASE_ENUM(AAUDIO_STREAM_STATE_CLOSED);
127         AAUDIO_CASE_ENUM(AAUDIO_STREAM_STATE_DISCONNECTED);
128     }
129     return "Unrecognized";
130 }
131 
132 #undef AAUDIO_CASE_ENUM
133 
134 }  // namespace aaudio
135