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 20 #include <android/media/MicrophoneInfoData.h> 21 #include <binder/Parcel.h> 22 #include <binder/Parcelable.h> 23 #include <media/AidlConversionUtil.h> 24 #include <system/audio.h> 25 26 namespace android { 27 namespace media { 28 29 class MicrophoneInfo : public Parcelable { 30 public: 31 MicrophoneInfo() = default; 32 MicrophoneInfo(const MicrophoneInfo& microphoneInfo) = default; MicrophoneInfo(audio_microphone_characteristic_t & characteristic)33 MicrophoneInfo(audio_microphone_characteristic_t& characteristic) { 34 mDeviceId = std::string(&characteristic.device_id[0]); 35 mPortId = characteristic.id; 36 mType = characteristic.device; 37 mAddress = std::string(&characteristic.address[0]); 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); 47 std::vector<float> frequencies; 48 std::vector<float> responses; 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 writeToParcel(Parcel * parcel)66 virtual status_t writeToParcel(Parcel* parcel) const { 67 MicrophoneInfoData parcelable; 68 return writeToParcelable(&parcelable) 69 ?: parcelable.writeToParcel(parcel); 70 } 71 writeToParcelable(MicrophoneInfoData * parcelable)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; 81 if (mFrequencyResponses.size() != 2) { 82 return BAD_VALUE; 83 } 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; 91 return OK; 92 } 93 readFromParcel(const Parcel * parcel)94 virtual status_t readFromParcel(const Parcel* parcel) { 95 MicrophoneInfoData data; 96 return data.readFromParcel(parcel) 97 ?: readFromParcelable(data); 98 } 99 readFromParcelable(const MicrophoneInfoData & parcelable)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) { 109 return BAD_VALUE; 110 } 111 mGeometricLocation = parcelable.geometricLocation; 112 if (parcelable.orientation.size() != 3) { 113 return BAD_VALUE; 114 } 115 mOrientation = parcelable.orientation; 116 if (parcelable.frequencies.size() != parcelable.frequencyResponses.size()) { 117 return BAD_VALUE; 118 } 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; 130 return OK; 131 } 132 getDeviceId()133 std::string getDeviceId() const { 134 return mDeviceId; 135 } 136 getPortId()137 int getPortId() const { 138 return mPortId; 139 } 140 getType()141 unsigned int getType() const { 142 return mType; 143 } 144 getAddress()145 std::string getAddress() const { 146 return mAddress; 147 } 148 getDeviceLocation()149 int getDeviceLocation() const { 150 return mDeviceLocation; 151 } 152 getDeviceGroup()153 int getDeviceGroup() const { 154 return mDeviceGroup; 155 } 156 getIndexInTheGroup()157 int getIndexInTheGroup() const { 158 return mIndexInTheGroup; 159 } 160 getGeometricLocation()161 const std::vector<float>& getGeometricLocation() const { 162 return mGeometricLocation; 163 } 164 getOrientation()165 const std::vector<float>& getOrientation() const { 166 return mOrientation; 167 } 168 getFrequencyResponses()169 const std::vector<std::vector<float>>& getFrequencyResponses() const { 170 return mFrequencyResponses; 171 } 172 getChannelMapping()173 const std::vector<int>& getChannelMapping() const { 174 return mChannelMapping; 175 } 176 getSensitivity()177 float getSensitivity() const { 178 return mSensitivity; 179 } 180 getMaxSpl()181 float getMaxSpl() const { 182 return mMaxSpl; 183 } 184 getMinSpl()185 float getMinSpl() const { 186 return mMinSpl; 187 } 188 getDirectionality()189 int getDirectionality() const { 190 return mDirectionality; 191 } 192 193 private: 194 std::string mDeviceId; 195 int32_t mPortId; 196 uint32_t mType; 197 std::string mAddress; 198 int32_t mDeviceLocation; 199 int32_t mDeviceGroup; 200 int32_t mIndexInTheGroup; 201 std::vector<float> mGeometricLocation; 202 std::vector<float> mOrientation; 203 std::vector<std::vector<float>> mFrequencyResponses; 204 std::vector<int> mChannelMapping; 205 float mSensitivity; 206 float mMaxSpl; 207 float mMinSpl; 208 int32_t mDirectionality; 209 }; 210 211 // Conversion routines, according to AidlConversion.h conventions. 212 inline ConversionResult<MicrophoneInfo> aidl2legacy_MicrophoneInfo(const media::MicrophoneInfoData & aidl)213aidl2legacy_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> legacy2aidl_MicrophoneInfo(const MicrophoneInfo & legacy)220legacy2aidl_MicrophoneInfo(const MicrophoneInfo& legacy) { 221 media::MicrophoneInfoData aidl; 222 RETURN_IF_ERROR(legacy.writeToParcelable(&aidl)); 223 return aidl; 224 } 225 226 } // namespace media 227 } // namespace android 228 229 #endif 230