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 { |
| 73 | parcelable->deviceId = mDeviceId; |
| 74 | parcelable->portId = mPortId; |
| 75 | parcelable->type = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(mType)); |
| 76 | parcelable->address = mAddress; |
| 77 | parcelable->deviceGroup = mDeviceGroup; |
| 78 | parcelable->indexInTheGroup = mIndexInTheGroup; |
| 79 | parcelable->geometricLocation = mGeometricLocation; |
| 80 | parcelable->orientation = mOrientation; |
jiabin | 46a76fa | 2018-01-05 10:18:21 -0800 | [diff] [blame] | 81 | if (mFrequencyResponses.size() != 2) { |
| 82 | return BAD_VALUE; |
| 83 | } |
Ytai Ben-Tsvi | 71109da | 2020-11-03 15:11:13 -0800 | [diff] [blame] | 84 | parcelable->frequencies = mFrequencyResponses[0]; |
| 85 | parcelable->frequencyResponses = mFrequencyResponses[1]; |
| 86 | parcelable->channelMapping = mChannelMapping; |
| 87 | parcelable->sensitivity = mSensitivity; |
| 88 | parcelable->maxSpl = mMaxSpl; |
| 89 | parcelable->minSpl = mMinSpl; |
| 90 | parcelable->directionality = mDirectionality; |
jiabin | 46a76fa | 2018-01-05 10:18:21 -0800 | [diff] [blame] | 91 | return OK; |
| 92 | } |
| 93 | |
| 94 | virtual status_t readFromParcel(const Parcel* parcel) { |
Ytai Ben-Tsvi | 71109da | 2020-11-03 15:11:13 -0800 | [diff] [blame] | 95 | MicrophoneInfoData data; |
| 96 | return data.readFromParcel(parcel) |
| 97 | ?: readFromParcelable(data); |
| 98 | } |
| 99 | |
| 100 | virtual status_t readFromParcelable(const MicrophoneInfoData& parcelable) { |
| 101 | mDeviceId = parcelable.deviceId; |
| 102 | mPortId = parcelable.portId; |
| 103 | mType = VALUE_OR_RETURN_STATUS(convertReinterpret<uint32_t>(parcelable.type)); |
| 104 | mAddress = parcelable.address; |
| 105 | mDeviceLocation = parcelable.deviceLocation; |
| 106 | mDeviceGroup = parcelable.deviceGroup; |
| 107 | mIndexInTheGroup = parcelable.indexInTheGroup; |
| 108 | if (parcelable.geometricLocation.size() != 3) { |
jiabin | 46a76fa | 2018-01-05 10:18:21 -0800 | [diff] [blame] | 109 | return BAD_VALUE; |
| 110 | } |
Ytai Ben-Tsvi | 71109da | 2020-11-03 15:11:13 -0800 | [diff] [blame] | 111 | mGeometricLocation = parcelable.geometricLocation; |
| 112 | if (parcelable.orientation.size() != 3) { |
jiabin | 46a76fa | 2018-01-05 10:18:21 -0800 | [diff] [blame] | 113 | return BAD_VALUE; |
| 114 | } |
Ytai Ben-Tsvi | 71109da | 2020-11-03 15:11:13 -0800 | [diff] [blame] | 115 | mOrientation = parcelable.orientation; |
| 116 | if (parcelable.frequencies.size() != parcelable.frequencyResponses.size()) { |
| 117 | return BAD_VALUE; |
jiabin | 46a76fa | 2018-01-05 10:18:21 -0800 | [diff] [blame] | 118 | } |
Ytai Ben-Tsvi | 71109da | 2020-11-03 15:11:13 -0800 | [diff] [blame] | 119 | |
| 120 | mFrequencyResponses.push_back(parcelable.frequencies); |
| 121 | mFrequencyResponses.push_back(parcelable.frequencyResponses); |
| 122 | if (parcelable.channelMapping.size() != AUDIO_CHANNEL_COUNT_MAX) { |
| 123 | return BAD_VALUE; |
| 124 | } |
| 125 | mChannelMapping = parcelable.channelMapping; |
| 126 | mSensitivity = parcelable.sensitivity; |
| 127 | mMaxSpl = parcelable.maxSpl; |
| 128 | mMinSpl = parcelable.minSpl; |
| 129 | mDirectionality = parcelable.directionality; |
jiabin | 46a76fa | 2018-01-05 10:18:21 -0800 | [diff] [blame] | 130 | return OK; |
| 131 | } |
| 132 | |
Ytai Ben-Tsvi | 71109da | 2020-11-03 15:11:13 -0800 | [diff] [blame] | 133 | std::string getDeviceId() const { |
jiabin | 46a76fa | 2018-01-05 10:18:21 -0800 | [diff] [blame] | 134 | return mDeviceId; |
| 135 | } |
| 136 | |
| 137 | int getPortId() const { |
| 138 | return mPortId; |
| 139 | } |
| 140 | |
| 141 | unsigned int getType() const { |
| 142 | return mType; |
| 143 | } |
| 144 | |
Ytai Ben-Tsvi | 71109da | 2020-11-03 15:11:13 -0800 | [diff] [blame] | 145 | std::string getAddress() const { |
jiabin | 46a76fa | 2018-01-05 10:18:21 -0800 | [diff] [blame] | 146 | return mAddress; |
| 147 | } |
| 148 | |
| 149 | int getDeviceLocation() const { |
| 150 | return mDeviceLocation; |
| 151 | } |
| 152 | |
| 153 | int getDeviceGroup() const { |
| 154 | return mDeviceGroup; |
| 155 | } |
| 156 | |
| 157 | int getIndexInTheGroup() const { |
| 158 | return mIndexInTheGroup; |
| 159 | } |
| 160 | |
Ytai Ben-Tsvi | 71109da | 2020-11-03 15:11:13 -0800 | [diff] [blame] | 161 | const std::vector<float>& getGeometricLocation() const { |
jiabin | 46a76fa | 2018-01-05 10:18:21 -0800 | [diff] [blame] | 162 | return mGeometricLocation; |
| 163 | } |
| 164 | |
Ytai Ben-Tsvi | 71109da | 2020-11-03 15:11:13 -0800 | [diff] [blame] | 165 | const std::vector<float>& getOrientation() const { |
jiabin | 46a76fa | 2018-01-05 10:18:21 -0800 | [diff] [blame] | 166 | return mOrientation; |
| 167 | } |
| 168 | |
Ytai Ben-Tsvi | 71109da | 2020-11-03 15:11:13 -0800 | [diff] [blame] | 169 | const std::vector<std::vector<float>>& getFrequencyResponses() const { |
jiabin | 46a76fa | 2018-01-05 10:18:21 -0800 | [diff] [blame] | 170 | return mFrequencyResponses; |
| 171 | } |
| 172 | |
Ytai Ben-Tsvi | 71109da | 2020-11-03 15:11:13 -0800 | [diff] [blame] | 173 | const std::vector<int>& getChannelMapping() const { |
jiabin | 46a76fa | 2018-01-05 10:18:21 -0800 | [diff] [blame] | 174 | return mChannelMapping; |
| 175 | } |
| 176 | |
| 177 | float getSensitivity() const { |
| 178 | return mSensitivity; |
| 179 | } |
| 180 | |
| 181 | float getMaxSpl() const { |
| 182 | return mMaxSpl; |
| 183 | } |
| 184 | |
| 185 | float getMinSpl() const { |
| 186 | return mMinSpl; |
| 187 | } |
| 188 | |
| 189 | int getDirectionality() const { |
| 190 | return mDirectionality; |
| 191 | } |
| 192 | |
| 193 | private: |
Ytai Ben-Tsvi | 71109da | 2020-11-03 15:11:13 -0800 | [diff] [blame] | 194 | std::string mDeviceId; |
jiabin | 46a76fa | 2018-01-05 10:18:21 -0800 | [diff] [blame] | 195 | int32_t mPortId; |
| 196 | uint32_t mType; |
Ytai Ben-Tsvi | 71109da | 2020-11-03 15:11:13 -0800 | [diff] [blame] | 197 | std::string mAddress; |
jiabin | 46a76fa | 2018-01-05 10:18:21 -0800 | [diff] [blame] | 198 | int32_t mDeviceLocation; |
| 199 | int32_t mDeviceGroup; |
| 200 | int32_t mIndexInTheGroup; |
Ytai Ben-Tsvi | 71109da | 2020-11-03 15:11:13 -0800 | [diff] [blame] | 201 | std::vector<float> mGeometricLocation; |
| 202 | std::vector<float> mOrientation; |
| 203 | std::vector<std::vector<float>> mFrequencyResponses; |
| 204 | std::vector<int> mChannelMapping; |
jiabin | 46a76fa | 2018-01-05 10:18:21 -0800 | [diff] [blame] | 205 | float mSensitivity; |
| 206 | float mMaxSpl; |
| 207 | float mMinSpl; |
| 208 | int32_t mDirectionality; |
| 209 | }; |
| 210 | |
Ytai Ben-Tsvi | 50b8ccb | 2020-11-24 13:47:54 -0800 | [diff] [blame] | 211 | // Conversion routines, according to AidlConversion.h conventions. |
| 212 | inline ConversionResult<MicrophoneInfo> |
| 213 | aidl2legacy_MicrophoneInfo(const media::MicrophoneInfoData& aidl) { |
| 214 | MicrophoneInfo legacy; |
| 215 | RETURN_IF_ERROR(legacy.readFromParcelable(aidl)); |
| 216 | return legacy; |
| 217 | } |
| 218 | |
| 219 | inline ConversionResult<media::MicrophoneInfoData> |
| 220 | legacy2aidl_MicrophoneInfo(const MicrophoneInfo& legacy) { |
| 221 | media::MicrophoneInfoData aidl; |
| 222 | RETURN_IF_ERROR(legacy.writeToParcelable(&aidl)); |
| 223 | return aidl; |
| 224 | } |
| 225 | |
jiabin | 46a76fa | 2018-01-05 10:18:21 -0800 | [diff] [blame] | 226 | } // namespace media |
| 227 | } // namespace android |
| 228 | |
| 229 | #endif |