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