MicrophoneInfo.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. #ifndef ANDROID_MICROPHONE_INFO_H
  17. #define ANDROID_MICROPHONE_INFO_H
  18. #include <binder/Parcel.h>
  19. #include <binder/Parcelable.h>
  20. #include <system/audio.h>
  21. #include <utils/String16.h>
  22. #include <utils/Vector.h>
  23. namespace android {
  24. namespace media {
  25. #define RETURN_IF_FAILED(calledOnce) \
  26. { \
  27. status_t returnStatus = calledOnce; \
  28. if (returnStatus) { \
  29. ALOGE("Failed at %s:%d (%s)", __FILE__, __LINE__, __func__); \
  30. return returnStatus; \
  31. } \
  32. }
  33. class MicrophoneInfo : public Parcelable {
  34. public:
  35. MicrophoneInfo() = default;
  36. MicrophoneInfo(const MicrophoneInfo& microphoneInfo) = default;
  37. MicrophoneInfo(audio_microphone_characteristic_t& characteristic) {
  38. mDeviceId = String16(&characteristic.device_id[0]);
  39. mPortId = characteristic.id;
  40. mType = characteristic.device;
  41. mAddress = String16(&characteristic.address[0]);
  42. mDeviceLocation = characteristic.location;
  43. mDeviceGroup = characteristic.group;
  44. mIndexInTheGroup = characteristic.index_in_the_group;
  45. mGeometricLocation.push_back(characteristic.geometric_location.x);
  46. mGeometricLocation.push_back(characteristic.geometric_location.y);
  47. mGeometricLocation.push_back(characteristic.geometric_location.z);
  48. mOrientation.push_back(characteristic.orientation.x);
  49. mOrientation.push_back(characteristic.orientation.y);
  50. mOrientation.push_back(characteristic.orientation.z);
  51. Vector<float> frequencies;
  52. Vector<float> responses;
  53. for (size_t i = 0; i < characteristic.num_frequency_responses; i++) {
  54. frequencies.push_back(characteristic.frequency_responses[0][i]);
  55. responses.push_back(characteristic.frequency_responses[1][i]);
  56. }
  57. mFrequencyResponses.push_back(frequencies);
  58. mFrequencyResponses.push_back(responses);
  59. for (size_t i = 0; i < AUDIO_CHANNEL_COUNT_MAX; i++) {
  60. mChannelMapping.push_back(characteristic.channel_mapping[i]);
  61. }
  62. mSensitivity = characteristic.sensitivity;
  63. mMaxSpl = characteristic.max_spl;
  64. mMinSpl = characteristic.min_spl;
  65. mDirectionality = characteristic.directionality;
  66. }
  67. virtual ~MicrophoneInfo() = default;
  68. virtual status_t writeToParcel(Parcel* parcel) const {
  69. RETURN_IF_FAILED(parcel->writeString16(mDeviceId));
  70. RETURN_IF_FAILED(parcel->writeInt32(mPortId));
  71. RETURN_IF_FAILED(parcel->writeUint32(mType));
  72. RETURN_IF_FAILED(parcel->writeString16(mAddress));
  73. RETURN_IF_FAILED(parcel->writeInt32(mDeviceLocation));
  74. RETURN_IF_FAILED(parcel->writeInt32(mDeviceGroup));
  75. RETURN_IF_FAILED(parcel->writeInt32(mIndexInTheGroup));
  76. RETURN_IF_FAILED(writeFloatVector(parcel, mGeometricLocation));
  77. RETURN_IF_FAILED(writeFloatVector(parcel, mOrientation));
  78. if (mFrequencyResponses.size() != 2) {
  79. return BAD_VALUE;
  80. }
  81. for (size_t i = 0; i < mFrequencyResponses.size(); i++) {
  82. RETURN_IF_FAILED(parcel->writeInt32(mFrequencyResponses[i].size()));
  83. RETURN_IF_FAILED(writeFloatVector(parcel, mFrequencyResponses[i]));
  84. }
  85. std::vector<int> channelMapping;
  86. for (size_t i = 0; i < mChannelMapping.size(); ++i) {
  87. channelMapping.push_back(mChannelMapping[i]);
  88. }
  89. RETURN_IF_FAILED(parcel->writeInt32Vector(channelMapping));
  90. RETURN_IF_FAILED(parcel->writeFloat(mSensitivity));
  91. RETURN_IF_FAILED(parcel->writeFloat(mMaxSpl));
  92. RETURN_IF_FAILED(parcel->writeFloat(mMinSpl));
  93. RETURN_IF_FAILED(parcel->writeInt32(mDirectionality));
  94. return OK;
  95. }
  96. virtual status_t readFromParcel(const Parcel* parcel) {
  97. RETURN_IF_FAILED(parcel->readString16(&mDeviceId));
  98. RETURN_IF_FAILED(parcel->readInt32(&mPortId));
  99. RETURN_IF_FAILED(parcel->readUint32(&mType));
  100. RETURN_IF_FAILED(parcel->readString16(&mAddress));
  101. RETURN_IF_FAILED(parcel->readInt32(&mDeviceLocation));
  102. RETURN_IF_FAILED(parcel->readInt32(&mDeviceGroup));
  103. RETURN_IF_FAILED(parcel->readInt32(&mIndexInTheGroup));
  104. RETURN_IF_FAILED(readFloatVector(parcel, &mGeometricLocation, 3));
  105. RETURN_IF_FAILED(readFloatVector(parcel, &mOrientation, 3));
  106. int32_t frequenciesNum;
  107. RETURN_IF_FAILED(parcel->readInt32(&frequenciesNum));
  108. Vector<float> frequencies;
  109. RETURN_IF_FAILED(readFloatVector(parcel, &frequencies, frequenciesNum));
  110. int32_t responsesNum;
  111. RETURN_IF_FAILED(parcel->readInt32(&responsesNum));
  112. Vector<float> responses;
  113. RETURN_IF_FAILED(readFloatVector(parcel, &responses, responsesNum));
  114. if (frequencies.size() != responses.size()) {
  115. return BAD_VALUE;
  116. }
  117. mFrequencyResponses.push_back(frequencies);
  118. mFrequencyResponses.push_back(responses);
  119. std::vector<int> channelMapping;
  120. status_t result = parcel->readInt32Vector(&channelMapping);
  121. if (result != OK) {
  122. return result;
  123. }
  124. if (channelMapping.size() != AUDIO_CHANNEL_COUNT_MAX) {
  125. return BAD_VALUE;
  126. }
  127. for (size_t i = 0; i < channelMapping.size(); i++) {
  128. mChannelMapping.push_back(channelMapping[i]);
  129. }
  130. RETURN_IF_FAILED(parcel->readFloat(&mSensitivity));
  131. RETURN_IF_FAILED(parcel->readFloat(&mMaxSpl));
  132. RETURN_IF_FAILED(parcel->readFloat(&mMinSpl));
  133. RETURN_IF_FAILED(parcel->readInt32(&mDirectionality));
  134. return OK;
  135. }
  136. String16 getDeviceId() const {
  137. return mDeviceId;
  138. }
  139. int getPortId() const {
  140. return mPortId;
  141. }
  142. unsigned int getType() const {
  143. return mType;
  144. }
  145. String16 getAddress() const {
  146. return mAddress;
  147. }
  148. int getDeviceLocation() const {
  149. return mDeviceLocation;
  150. }
  151. int getDeviceGroup() const {
  152. return mDeviceGroup;
  153. }
  154. int getIndexInTheGroup() const {
  155. return mIndexInTheGroup;
  156. }
  157. const Vector<float>& getGeometricLocation() const {
  158. return mGeometricLocation;
  159. }
  160. const Vector<float>& getOrientation() const {
  161. return mOrientation;
  162. }
  163. const Vector<Vector<float>>& getFrequencyResponses() const {
  164. return mFrequencyResponses;
  165. }
  166. const Vector<int>& getChannelMapping() const {
  167. return mChannelMapping;
  168. }
  169. float getSensitivity() const {
  170. return mSensitivity;
  171. }
  172. float getMaxSpl() const {
  173. return mMaxSpl;
  174. }
  175. float getMinSpl() const {
  176. return mMinSpl;
  177. }
  178. int getDirectionality() const {
  179. return mDirectionality;
  180. }
  181. private:
  182. status_t readFloatVector(
  183. const Parcel* parcel, Vector<float> *vectorPtr, size_t defaultLength) {
  184. std::unique_ptr<std::vector<float>> v;
  185. status_t result = parcel->readFloatVector(&v);
  186. if (result != OK) return result;
  187. vectorPtr->clear();
  188. if (v.get() != nullptr) {
  189. for (const auto& iter : *v) {
  190. vectorPtr->push_back(iter);
  191. }
  192. } else {
  193. vectorPtr->resize(defaultLength);
  194. }
  195. return OK;
  196. }
  197. status_t writeFloatVector(Parcel* parcel, const Vector<float>& vector) const {
  198. std::vector<float> v;
  199. for (size_t i = 0; i < vector.size(); i++) {
  200. v.push_back(vector[i]);
  201. }
  202. return parcel->writeFloatVector(v);
  203. }
  204. String16 mDeviceId;
  205. int32_t mPortId;
  206. uint32_t mType;
  207. String16 mAddress;
  208. int32_t mDeviceLocation;
  209. int32_t mDeviceGroup;
  210. int32_t mIndexInTheGroup;
  211. Vector<float> mGeometricLocation;
  212. Vector<float> mOrientation;
  213. Vector<Vector<float>> mFrequencyResponses;
  214. Vector<int> mChannelMapping;
  215. float mSensitivity;
  216. float mMaxSpl;
  217. float mMinSpl;
  218. int32_t mDirectionality;
  219. };
  220. } // namespace media
  221. } // namespace android
  222. #endif