jiabin | 46a76fa | 2018-01-05 10:18:21 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 ANDROID_MICROPHONE_INFO_H |
| 18 | #define ANDROID_MICROPHONE_INFO_H |
| 19 | |
Ytai Ben-Tsvi | 71109da | 2020-11-03 15:11:13 -0800 | [diff] [blame] | 20 | #include <android/media/MicrophoneInfoData.h> |
jiabin | 46a76fa | 2018-01-05 10:18:21 -0800 | [diff] [blame] | 21 | #include <binder/Parcel.h> |
| 22 | #include <binder/Parcelable.h> |
Ytai Ben-Tsvi | 643783e | 2020-11-18 14:04:15 -0800 | [diff] [blame] | 23 | #include <media/AidlConversionUtil.h> |
jiabin | 46a76fa | 2018-01-05 10:18:21 -0800 | [diff] [blame] | 24 | #include <system/audio.h> |
jiabin | 46a76fa | 2018-01-05 10:18:21 -0800 | [diff] [blame] | 25 | |
| 26 | namespace android { |
| 27 | namespace media { |
| 28 | |
jiabin | 46a76fa | 2018-01-05 10:18:21 -0800 | [diff] [blame] | 29 | class MicrophoneInfo : public Parcelable { |
| 30 | public: |
| 31 | MicrophoneInfo() = default; |
| 32 | MicrophoneInfo(const MicrophoneInfo& microphoneInfo) = default; |
| 33 | MicrophoneInfo(audio_microphone_characteristic_t& characteristic) { |
Ytai Ben-Tsvi | 71109da | 2020-11-03 15:11:13 -0800 | [diff] [blame] | 34 | mDeviceId = std::string(&characteristic.device_id[0]); |
jiabin | 46a76fa | 2018-01-05 10:18:21 -0800 | [diff] [blame] | 35 | mPortId = characteristic.id; |
rago | 1de79cf | 2018-02-01 15:21:02 -0800 | [diff] [blame] | 36 | mType = characteristic.device; |
Ytai Ben-Tsvi | 71109da | 2020-11-03 15:11:13 -0800 | [diff] [blame] | 37 | mAddress = std::string(&characteristic.address[0]); |
jiabin | 46a76fa | 2018-01-05 10:18:21 -0800 | [diff] [blame] | 38 | mDeviceLocation = characteristic.location; |
| 39 | mDeviceGroup = characteristic.group; |
| 40 | mIndexInTheGroup = characteristic.index_in_the_group; |
| 41 | mGeometricLocation.push_back(characteristic.geometric_location.x); |
| 42 | mGeometricLocation.push_back(characteristic.geometric_location.y); |
| 43 | mGeometricLocation.push_back(characteristic.geometric_location.z); |
| 44 | mOrientation.push_back(characteristic.orientation.x); |
| 45 | mOrientation.push_back(characteristic.orientation.y); |
| 46 | mOrientation.push_back(characteristic.orientation.z); |
Ytai Ben-Tsvi | 71109da | 2020-11-03 15:11:13 -0800 | [diff] [blame] | 47 | std::vector<float> frequencies; |
| 48 | std::vector<float> responses; |
jiabin | 46a76fa | 2018-01-05 10:18:21 -0800 | [diff] [blame] | 49 | for (size_t i = 0; i < characteristic.num_frequency_responses; i++) { |
| 50 | frequencies.push_back(characteristic.frequency_responses[0][i]); |
| 51 | responses.push_back(characteristic.frequency_responses[1][i]); |
| 52 | } |
| 53 | mFrequencyResponses.push_back(frequencies); |
| 54 | mFrequencyResponses.push_back(responses); |
| 55 | for (size_t i = 0; i < AUDIO_CHANNEL_COUNT_MAX; i++) { |
| 56 | mChannelMapping.push_back(characteristic.channel_mapping[i]); |
| 57 | } |
| 58 | mSensitivity = characteristic.sensitivity; |
| 59 | mMaxSpl = characteristic.max_spl; |
| 60 | mMinSpl = characteristic.min_spl; |
| 61 | mDirectionality = characteristic.directionality; |
| 62 | } |
| 63 | |
| 64 | virtual ~MicrophoneInfo() = default; |
| 65 | |
| 66 | virtual status_t writeToParcel(Parcel* parcel) const { |
Ytai Ben-Tsvi | 71109da | 2020-11-03 15:11:13 -0800 | [diff] [blame] | 67 | MicrophoneInfoData parcelable; |
| 68 | return writeToParcelable(&parcelable) |
| 69 | ?: parcelable.writeToParcel(parcel); |
| 70 | } |
| 71 | |
| 72 | virtual status_t writeToParcelable(MicrophoneInfoData* parcelable) const { |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 73 | #if defined(BACKEND_NDK) |
| 74 | using ::aidl::android::convertReinterpret; |
| 75 | #endif |
Ytai Ben-Tsvi | 71109da | 2020-11-03 15:11:13 -0800 | [diff] [blame] | 76 | parcelable->deviceId = mDeviceId; |
| 77 | parcelable->portId = mPortId; |
| 78 | parcelable->type = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(mType)); |
| 79 | parcelable->address = mAddress; |
| 80 | parcelable->deviceGroup = mDeviceGroup; |
| 81 | parcelable->indexInTheGroup = mIndexInTheGroup; |
| 82 | parcelable->geometricLocation = mGeometricLocation; |
| 83 | parcelable->orientation = mOrientation; |
jiabin | 46a76fa | 2018-01-05 10:18:21 -0800 | [diff] [blame] | 84 | if (mFrequencyResponses.size() != 2) { |
| 85 | return BAD_VALUE; |
| 86 | } |
Ytai Ben-Tsvi | 71109da | 2020-11-03 15:11:13 -0800 | [diff] [blame] | 87 | parcelable->frequencies = mFrequencyResponses[0]; |
| 88 | parcelable->frequencyResponses = mFrequencyResponses[1]; |
| 89 | parcelable->channelMapping = mChannelMapping; |
| 90 | parcelable->sensitivity = mSensitivity; |
| 91 | parcelable->maxSpl = mMaxSpl; |
| 92 | parcelable->minSpl = mMinSpl; |
| 93 | parcelable->directionality = mDirectionality; |
jiabin | 46a76fa | 2018-01-05 10:18:21 -0800 | [diff] [blame] | 94 | return OK; |
| 95 | } |
| 96 | |
| 97 | virtual status_t readFromParcel(const Parcel* parcel) { |
Ytai Ben-Tsvi | 71109da | 2020-11-03 15:11:13 -0800 | [diff] [blame] | 98 | MicrophoneInfoData data; |
| 99 | return data.readFromParcel(parcel) |
| 100 | ?: readFromParcelable(data); |
| 101 | } |
| 102 | |
| 103 | virtual status_t readFromParcelable(const MicrophoneInfoData& parcelable) { |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 104 | #if defined(BACKEND_NDK) |
| 105 | using ::aidl::android::convertReinterpret; |
| 106 | #endif |
Ytai Ben-Tsvi | 71109da | 2020-11-03 15:11:13 -0800 | [diff] [blame] | 107 | mDeviceId = parcelable.deviceId; |
| 108 | mPortId = parcelable.portId; |
| 109 | mType = VALUE_OR_RETURN_STATUS(convertReinterpret<uint32_t>(parcelable.type)); |
| 110 | mAddress = parcelable.address; |
| 111 | mDeviceLocation = parcelable.deviceLocation; |
| 112 | mDeviceGroup = parcelable.deviceGroup; |
| 113 | mIndexInTheGroup = parcelable.indexInTheGroup; |
| 114 | if (parcelable.geometricLocation.size() != 3) { |
jiabin | 46a76fa | 2018-01-05 10:18:21 -0800 | [diff] [blame] | 115 | return BAD_VALUE; |
| 116 | } |
Ytai Ben-Tsvi | 71109da | 2020-11-03 15:11:13 -0800 | [diff] [blame] | 117 | mGeometricLocation = parcelable.geometricLocation; |
| 118 | if (parcelable.orientation.size() != 3) { |
jiabin | 46a76fa | 2018-01-05 10:18:21 -0800 | [diff] [blame] | 119 | return BAD_VALUE; |
| 120 | } |
Ytai Ben-Tsvi | 71109da | 2020-11-03 15:11:13 -0800 | [diff] [blame] | 121 | mOrientation = parcelable.orientation; |
| 122 | if (parcelable.frequencies.size() != parcelable.frequencyResponses.size()) { |
| 123 | return BAD_VALUE; |
jiabin | 46a76fa | 2018-01-05 10:18:21 -0800 | [diff] [blame] | 124 | } |
Ytai Ben-Tsvi | 71109da | 2020-11-03 15:11:13 -0800 | [diff] [blame] | 125 | |
| 126 | mFrequencyResponses.push_back(parcelable.frequencies); |
| 127 | mFrequencyResponses.push_back(parcelable.frequencyResponses); |
| 128 | if (parcelable.channelMapping.size() != AUDIO_CHANNEL_COUNT_MAX) { |
| 129 | return BAD_VALUE; |
| 130 | } |
| 131 | mChannelMapping = parcelable.channelMapping; |
| 132 | mSensitivity = parcelable.sensitivity; |
| 133 | mMaxSpl = parcelable.maxSpl; |
| 134 | mMinSpl = parcelable.minSpl; |
| 135 | mDirectionality = parcelable.directionality; |
jiabin | 46a76fa | 2018-01-05 10:18:21 -0800 | [diff] [blame] | 136 | return OK; |
| 137 | } |
| 138 | |
Ytai Ben-Tsvi | 71109da | 2020-11-03 15:11:13 -0800 | [diff] [blame] | 139 | std::string getDeviceId() const { |
jiabin | 46a76fa | 2018-01-05 10:18:21 -0800 | [diff] [blame] | 140 | return mDeviceId; |
| 141 | } |
| 142 | |
| 143 | int getPortId() const { |
| 144 | return mPortId; |
| 145 | } |
| 146 | |
| 147 | unsigned int getType() const { |
| 148 | return mType; |
| 149 | } |
| 150 | |
Ytai Ben-Tsvi | 71109da | 2020-11-03 15:11:13 -0800 | [diff] [blame] | 151 | std::string getAddress() const { |
jiabin | 46a76fa | 2018-01-05 10:18:21 -0800 | [diff] [blame] | 152 | return mAddress; |
| 153 | } |
| 154 | |
| 155 | int getDeviceLocation() const { |
| 156 | return mDeviceLocation; |
| 157 | } |
| 158 | |
| 159 | int getDeviceGroup() const { |
| 160 | return mDeviceGroup; |
| 161 | } |
| 162 | |
| 163 | int getIndexInTheGroup() const { |
| 164 | return mIndexInTheGroup; |
| 165 | } |
| 166 | |
Ytai Ben-Tsvi | 71109da | 2020-11-03 15:11:13 -0800 | [diff] [blame] | 167 | const std::vector<float>& getGeometricLocation() const { |
jiabin | 46a76fa | 2018-01-05 10:18:21 -0800 | [diff] [blame] | 168 | return mGeometricLocation; |
| 169 | } |
| 170 | |
Ytai Ben-Tsvi | 71109da | 2020-11-03 15:11:13 -0800 | [diff] [blame] | 171 | const std::vector<float>& getOrientation() const { |
jiabin | 46a76fa | 2018-01-05 10:18:21 -0800 | [diff] [blame] | 172 | return mOrientation; |
| 173 | } |
| 174 | |
Ytai Ben-Tsvi | 71109da | 2020-11-03 15:11:13 -0800 | [diff] [blame] | 175 | const std::vector<std::vector<float>>& getFrequencyResponses() const { |
jiabin | 46a76fa | 2018-01-05 10:18:21 -0800 | [diff] [blame] | 176 | return mFrequencyResponses; |
| 177 | } |
| 178 | |
Ytai Ben-Tsvi | 71109da | 2020-11-03 15:11:13 -0800 | [diff] [blame] | 179 | const std::vector<int>& getChannelMapping() const { |
jiabin | 46a76fa | 2018-01-05 10:18:21 -0800 | [diff] [blame] | 180 | return mChannelMapping; |
| 181 | } |
| 182 | |
| 183 | float getSensitivity() const { |
| 184 | return mSensitivity; |
| 185 | } |
| 186 | |
| 187 | float getMaxSpl() const { |
| 188 | return mMaxSpl; |
| 189 | } |
| 190 | |
| 191 | float getMinSpl() const { |
| 192 | return mMinSpl; |
| 193 | } |
| 194 | |
| 195 | int getDirectionality() const { |
| 196 | return mDirectionality; |
| 197 | } |
| 198 | |
| 199 | private: |
Ytai Ben-Tsvi | 71109da | 2020-11-03 15:11:13 -0800 | [diff] [blame] | 200 | std::string mDeviceId; |
jiabin | 46a76fa | 2018-01-05 10:18:21 -0800 | [diff] [blame] | 201 | int32_t mPortId; |
| 202 | uint32_t mType; |
Ytai Ben-Tsvi | 71109da | 2020-11-03 15:11:13 -0800 | [diff] [blame] | 203 | std::string mAddress; |
jiabin | 46a76fa | 2018-01-05 10:18:21 -0800 | [diff] [blame] | 204 | int32_t mDeviceLocation; |
| 205 | int32_t mDeviceGroup; |
| 206 | int32_t mIndexInTheGroup; |
Ytai Ben-Tsvi | 71109da | 2020-11-03 15:11:13 -0800 | [diff] [blame] | 207 | std::vector<float> mGeometricLocation; |
| 208 | std::vector<float> mOrientation; |
| 209 | std::vector<std::vector<float>> mFrequencyResponses; |
| 210 | std::vector<int> mChannelMapping; |
jiabin | 46a76fa | 2018-01-05 10:18:21 -0800 | [diff] [blame] | 211 | float mSensitivity; |
| 212 | float mMaxSpl; |
| 213 | float mMinSpl; |
| 214 | int32_t mDirectionality; |
| 215 | }; |
| 216 | |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 217 | #if defined(BACKEND_NDK) |
| 218 | using ::aidl::ConversionResult; |
| 219 | #endif |
| 220 | |
Ytai Ben-Tsvi | 50b8ccb | 2020-11-24 13:47:54 -0800 | [diff] [blame] | 221 | // Conversion routines, according to AidlConversion.h conventions. |
| 222 | inline ConversionResult<MicrophoneInfo> |
| 223 | aidl2legacy_MicrophoneInfo(const media::MicrophoneInfoData& aidl) { |
| 224 | MicrophoneInfo legacy; |
| 225 | RETURN_IF_ERROR(legacy.readFromParcelable(aidl)); |
| 226 | return legacy; |
| 227 | } |
| 228 | |
| 229 | inline ConversionResult<media::MicrophoneInfoData> |
| 230 | legacy2aidl_MicrophoneInfo(const MicrophoneInfo& legacy) { |
| 231 | media::MicrophoneInfoData aidl; |
| 232 | RETURN_IF_ERROR(legacy.writeToParcelable(&aidl)); |
| 233 | return aidl; |
| 234 | } |
| 235 | |
jiabin | 46a76fa | 2018-01-05 10:18:21 -0800 | [diff] [blame] | 236 | } // namespace media |
| 237 | } // namespace android |
| 238 | |
| 239 | #endif |