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 
17 #ifndef AAUDIO_STREAM_PARAMETERS_H
18 #define AAUDIO_STREAM_PARAMETERS_H
19 
20 #include <stdint.h>
21 
22 #include <aaudio/AAudio.h>
23 #include <utility/AAudioUtilities.h>
24 
25 namespace aaudio {
26 
27 class AAudioStreamParameters {
28 public:
29     AAudioStreamParameters();
30     virtual ~AAudioStreamParameters();
31 
getDeviceId()32     int32_t getDeviceId() const {
33         return mDeviceId;
34     }
35 
setDeviceId(int32_t deviceId)36     void setDeviceId(int32_t deviceId) {
37         mDeviceId = deviceId;
38     }
39 
getSampleRate()40     int32_t getSampleRate() const {
41         return mSampleRate;
42     }
43 
setSampleRate(int32_t sampleRate)44     void setSampleRate(int32_t sampleRate) {
45         mSampleRate = sampleRate;
46     }
47 
getSamplesPerFrame()48     int32_t getSamplesPerFrame() const {
49         return mSamplesPerFrame;
50     }
51 
getFormat()52     audio_format_t getFormat() const {
53         return mAudioFormat;
54     }
55 
setFormat(audio_format_t audioFormat)56     void setFormat(audio_format_t audioFormat) {
57         mAudioFormat = audioFormat;
58     }
59 
getSharingMode()60     aaudio_sharing_mode_t getSharingMode() const {
61         return mSharingMode;
62     }
63 
setSharingMode(aaudio_sharing_mode_t sharingMode)64     void setSharingMode(aaudio_sharing_mode_t sharingMode) {
65         mSharingMode = sharingMode;
66     }
67 
getBufferCapacity()68     int32_t getBufferCapacity() const {
69         return mBufferCapacity;
70     }
71 
setBufferCapacity(int32_t frames)72     void setBufferCapacity(int32_t frames) {
73         mBufferCapacity = frames;
74     }
75 
getDirection()76     aaudio_direction_t getDirection() const {
77         return mDirection;
78     }
79 
setDirection(aaudio_direction_t direction)80     void setDirection(aaudio_direction_t direction) {
81         mDirection = direction;
82     }
83 
getUsage()84     aaudio_usage_t getUsage() const {
85         return mUsage;
86     }
87 
setUsage(aaudio_usage_t usage)88     void setUsage(aaudio_usage_t usage) {
89         mUsage = usage;
90     }
91 
getContentType()92     aaudio_content_type_t getContentType() const {
93         return mContentType;
94     }
95 
setContentType(aaudio_content_type_t contentType)96     void setContentType(aaudio_content_type_t contentType) {
97         mContentType = contentType;
98     }
99 
getSpatializationBehavior()100     aaudio_spatialization_behavior_t getSpatializationBehavior() const {
101         return mSpatializationBehavior;
102     }
103 
setSpatializationBehavior(aaudio_spatialization_behavior_t spatializationBehavior)104     void setSpatializationBehavior(aaudio_spatialization_behavior_t spatializationBehavior) {
105         mSpatializationBehavior = spatializationBehavior;
106     }
107 
isContentSpatialized()108     bool isContentSpatialized() const {
109         return mIsContentSpatialized;
110     }
111 
setIsContentSpatialized(bool isSpatialized)112     void setIsContentSpatialized(bool isSpatialized) {
113         mIsContentSpatialized = isSpatialized;
114     }
115 
getInputPreset()116     aaudio_input_preset_t getInputPreset() const {
117         return mInputPreset;
118     }
119 
setInputPreset(aaudio_input_preset_t inputPreset)120     void setInputPreset(aaudio_input_preset_t inputPreset) {
121         mInputPreset = inputPreset;
122     }
123 
getAllowedCapturePolicy()124     aaudio_allowed_capture_policy_t getAllowedCapturePolicy() const {
125         return mAllowedCapturePolicy;
126     }
127 
setAllowedCapturePolicy(aaudio_allowed_capture_policy_t policy)128     void setAllowedCapturePolicy(aaudio_allowed_capture_policy_t policy) {
129         mAllowedCapturePolicy = policy;
130     }
131 
getSessionId()132     aaudio_session_id_t getSessionId() const {
133         return mSessionId;
134     }
135 
setSessionId(aaudio_session_id_t sessionId)136     void setSessionId(aaudio_session_id_t sessionId) {
137         mSessionId = sessionId;
138     }
139 
isPrivacySensitive()140     bool isPrivacySensitive() const {
141         return mIsPrivacySensitive;
142     }
143 
setPrivacySensitive(bool privacySensitive)144     void setPrivacySensitive(bool privacySensitive) {
145         mIsPrivacySensitive = privacySensitive;
146     }
147 
getOpPackageName()148     const std::optional<std::string> getOpPackageName() const {
149         return mOpPackageName;
150     }
151 
152     // TODO b/182392769: reexamine if Identity can be used
setOpPackageName(const std::optional<std::string> opPackageName)153     void setOpPackageName(const std::optional<std::string> opPackageName) {
154         mOpPackageName = opPackageName;
155     }
156 
getAttributionTag()157     const std::optional<std::string> getAttributionTag() const {
158         return mAttributionTag;
159     }
160 
setAttributionTag(const std::optional<std::string> attributionTag)161     void setAttributionTag(const std::optional<std::string> attributionTag) {
162         mAttributionTag = attributionTag;
163     }
164 
getChannelMask()165     aaudio_channel_mask_t getChannelMask() const {
166         return mChannelMask;
167     }
168 
setChannelMask(aaudio_channel_mask_t channelMask)169     void setChannelMask(aaudio_channel_mask_t channelMask) {
170         mChannelMask = channelMask;
171         mSamplesPerFrame = AAudioConvert_channelMaskToCount(channelMask);
172     }
173 
174     /**
175      * @return bytes per frame of getFormat()
176      */
calculateBytesPerFrame()177     int32_t calculateBytesPerFrame() const {
178         return getSamplesPerFrame() * audio_bytes_per_sample(getFormat());
179     }
180 
181     /**
182      * Copy variables defined in other AAudioStreamParameters instance to this one.
183      * @param other
184      */
185     void copyFrom(const AAudioStreamParameters &other);
186 
187     virtual aaudio_result_t validate() const;
188 
189     void dump() const;
190 
191 private:
192     bool validateChannelMask() const;
193 
194     int32_t                         mSamplesPerFrame      = AAUDIO_UNSPECIFIED;
195     int32_t                         mSampleRate           = AAUDIO_UNSPECIFIED;
196     int32_t                         mDeviceId             = AAUDIO_UNSPECIFIED;
197     aaudio_sharing_mode_t           mSharingMode          = AAUDIO_SHARING_MODE_SHARED;
198     audio_format_t                  mAudioFormat          = AUDIO_FORMAT_DEFAULT;
199     aaudio_direction_t              mDirection            = AAUDIO_DIRECTION_OUTPUT;
200     aaudio_usage_t                  mUsage                = AAUDIO_UNSPECIFIED;
201     aaudio_content_type_t           mContentType          = AAUDIO_UNSPECIFIED;
202     aaudio_spatialization_behavior_t mSpatializationBehavior
203                                                           = AAUDIO_UNSPECIFIED;
204     bool                            mIsContentSpatialized = false;
205     aaudio_input_preset_t           mInputPreset          = AAUDIO_UNSPECIFIED;
206     int32_t                         mBufferCapacity       = AAUDIO_UNSPECIFIED;
207     aaudio_allowed_capture_policy_t mAllowedCapturePolicy = AAUDIO_UNSPECIFIED;
208     aaudio_session_id_t             mSessionId            = AAUDIO_SESSION_ID_NONE;
209     bool                            mIsPrivacySensitive   = false;
210     std::optional<std::string>      mOpPackageName        = {};
211     std::optional<std::string>      mAttributionTag       = {};
212     aaudio_channel_mask_t           mChannelMask          = AAUDIO_UNSPECIFIED;
213 };
214 
215 } /* namespace aaudio */
216 
217 #endif //AAUDIO_STREAM_PARAMETERS_H
218