blob: 5d12fadbd8db057118109bc85768855165723453 [file] [log] [blame]
Kevin Rocard4bcd67f2018-02-28 14:33:38 -08001/*
2 * Copyright (C) 2016 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#include <string.h>
18
19#define LOG_TAG "HalHidl"
20#include <media/AudioParameter.h>
21#include <utils/Log.h>
22
23#include "ConversionHelperHidl.h"
24
Kevin Rocard070e7512018-05-22 09:29:13 -070025using ::android::hardware::audio::CPP_VERSION::Result;
26
27#if MAJOR_VERSION == 4
28using ::android::hardware::audio::CPP_VERSION::AudioMicrophoneChannelMapping;
29using ::android::hardware::audio::CPP_VERSION::AudioMicrophoneDirectionality;
30using ::android::hardware::audio::CPP_VERSION::AudioMicrophoneLocation;
31using ::android::hardware::audio::CPP_VERSION::DeviceAddress;
32using ::android::hardware::audio::CPP_VERSION::MicrophoneInfo;
33#endif
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080034
35namespace android {
Kevin Rocard070e7512018-05-22 09:29:13 -070036namespace CPP_VERSION {
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080037
38// static
39status_t ConversionHelperHidl::keysFromHal(const String8& keys, hidl_vec<hidl_string> *hidlKeys) {
40 AudioParameter halKeys(keys);
41 if (halKeys.size() == 0) return BAD_VALUE;
42 hidlKeys->resize(halKeys.size());
43 //FIXME: keyStreamSupportedChannels and keyStreamSupportedSamplingRates come with a
44 // "keyFormat=<value>" pair. We need to transform it into a single key string so that it is
45 // carried over to the legacy HAL via HIDL.
46 String8 value;
47 bool keepFormatValue = halKeys.size() == 2 &&
48 (halKeys.get(String8(AudioParameter::keyStreamSupportedChannels), value) == NO_ERROR ||
49 halKeys.get(String8(AudioParameter::keyStreamSupportedSamplingRates), value) == NO_ERROR);
50
51 for (size_t i = 0; i < halKeys.size(); ++i) {
52 String8 key;
53 status_t status = halKeys.getAt(i, key);
54 if (status != OK) return status;
55 if (keepFormatValue && key == AudioParameter::keyFormat) {
56 AudioParameter formatParam;
57 halKeys.getAt(i, key, value);
58 formatParam.add(key, value);
59 key = formatParam.toString();
60 }
61 (*hidlKeys)[i] = key.string();
62 }
63 return OK;
64}
65
66// static
67status_t ConversionHelperHidl::parametersFromHal(
68 const String8& kvPairs, hidl_vec<ParameterValue> *hidlParams) {
69 AudioParameter params(kvPairs);
70 if (params.size() == 0) return BAD_VALUE;
71 hidlParams->resize(params.size());
72 for (size_t i = 0; i < params.size(); ++i) {
73 String8 key, value;
74 status_t status = params.getAt(i, key, value);
75 if (status != OK) return status;
76 (*hidlParams)[i].key = key.string();
77 (*hidlParams)[i].value = value.string();
78 }
79 return OK;
80}
81
82// static
83void ConversionHelperHidl::parametersToHal(
84 const hidl_vec<ParameterValue>& parameters, String8 *values) {
85 AudioParameter params;
86 for (size_t i = 0; i < parameters.size(); ++i) {
87 params.add(String8(parameters[i].key.c_str()), String8(parameters[i].value.c_str()));
88 }
89 values->setTo(params.toString());
90}
91
92ConversionHelperHidl::ConversionHelperHidl(const char* className)
93 : mClassName(className) {
94}
95
96// static
97status_t ConversionHelperHidl::analyzeResult(const Result& result) {
98 switch (result) {
99 case Result::OK: return OK;
100 case Result::INVALID_ARGUMENTS: return BAD_VALUE;
101 case Result::INVALID_STATE: return NOT_ENOUGH_DATA;
102 case Result::NOT_INITIALIZED: return NO_INIT;
103 case Result::NOT_SUPPORTED: return INVALID_OPERATION;
104 default: return NO_INIT;
105 }
106}
107
108void ConversionHelperHidl::emitError(const char* funcName, const char* description) {
109 ALOGE("%s %p %s: %s (from rpc)", mClassName, this, funcName, description);
110}
111
Kevin Rocard070e7512018-05-22 09:29:13 -0700112#if MAJOR_VERSION == 4
jiabin9ff780e2018-03-19 18:19:52 -0700113// TODO: Use the same implementation in the hal when it moves to a util library.
Kevin Rocarddf9b4202018-05-10 19:56:08 -0700114static std::string deviceAddressToHal(const DeviceAddress& address) {
jiabin9ff780e2018-03-19 18:19:52 -0700115 // HAL assumes that the address is NUL-terminated.
116 char halAddress[AUDIO_DEVICE_MAX_ADDRESS_LEN];
117 memset(halAddress, 0, sizeof(halAddress));
118 audio_devices_t halDevice = static_cast<audio_devices_t>(address.device);
119 const bool isInput = (halDevice & AUDIO_DEVICE_BIT_IN) != 0;
120 if (isInput) halDevice &= ~AUDIO_DEVICE_BIT_IN;
121 if ((!isInput && (halDevice & AUDIO_DEVICE_OUT_ALL_A2DP) != 0) ||
122 (isInput && (halDevice & AUDIO_DEVICE_IN_BLUETOOTH_A2DP) != 0)) {
123 snprintf(halAddress, sizeof(halAddress), "%02X:%02X:%02X:%02X:%02X:%02X",
124 address.address.mac[0], address.address.mac[1], address.address.mac[2],
125 address.address.mac[3], address.address.mac[4], address.address.mac[5]);
126 } else if ((!isInput && (halDevice & AUDIO_DEVICE_OUT_IP) != 0) ||
127 (isInput && (halDevice & AUDIO_DEVICE_IN_IP) != 0)) {
128 snprintf(halAddress, sizeof(halAddress), "%d.%d.%d.%d", address.address.ipv4[0],
129 address.address.ipv4[1], address.address.ipv4[2], address.address.ipv4[3]);
130 } else if ((!isInput && (halDevice & AUDIO_DEVICE_OUT_ALL_USB) != 0) ||
131 (isInput && (halDevice & AUDIO_DEVICE_IN_ALL_USB) != 0)) {
132 snprintf(halAddress, sizeof(halAddress), "card=%d;device=%d", address.address.alsa.card,
133 address.address.alsa.device);
134 } else if ((!isInput && (halDevice & AUDIO_DEVICE_OUT_BUS) != 0) ||
135 (isInput && (halDevice & AUDIO_DEVICE_IN_BUS) != 0)) {
136 snprintf(halAddress, sizeof(halAddress), "%s", address.busAddress.c_str());
137 } else if ((!isInput && (halDevice & AUDIO_DEVICE_OUT_REMOTE_SUBMIX)) != 0 ||
138 (isInput && (halDevice & AUDIO_DEVICE_IN_REMOTE_SUBMIX) != 0)) {
139 snprintf(halAddress, sizeof(halAddress), "%s", address.rSubmixAddress.c_str());
140 } else {
141 snprintf(halAddress, sizeof(halAddress), "%s", address.busAddress.c_str());
142 }
143 return halAddress;
144}
145
146//local conversion helpers
147
Kevin Rocarddf9b4202018-05-10 19:56:08 -0700148static audio_microphone_channel_mapping_t channelMappingToHal(AudioMicrophoneChannelMapping mapping) {
jiabin9ff780e2018-03-19 18:19:52 -0700149 switch (mapping) {
150 case AudioMicrophoneChannelMapping::UNUSED:
151 return AUDIO_MICROPHONE_CHANNEL_MAPPING_UNUSED;
152 case AudioMicrophoneChannelMapping::DIRECT:
153 return AUDIO_MICROPHONE_CHANNEL_MAPPING_DIRECT;
154 case AudioMicrophoneChannelMapping::PROCESSED:
155 return AUDIO_MICROPHONE_CHANNEL_MAPPING_PROCESSED;
156 default:
157 LOG_ALWAYS_FATAL("Unknown channelMappingToHal conversion %d", mapping);
158 }
159}
160
Kevin Rocarddf9b4202018-05-10 19:56:08 -0700161static audio_microphone_location_t locationToHal(AudioMicrophoneLocation location) {
jiabin9ff780e2018-03-19 18:19:52 -0700162 switch (location) {
163 case AudioMicrophoneLocation::UNKNOWN:
164 return AUDIO_MICROPHONE_LOCATION_UNKNOWN;
165 case AudioMicrophoneLocation::MAINBODY:
166 return AUDIO_MICROPHONE_LOCATION_MAINBODY;
167 case AudioMicrophoneLocation::MAINBODY_MOVABLE:
168 return AUDIO_MICROPHONE_LOCATION_MAINBODY_MOVABLE;
169 case AudioMicrophoneLocation::PERIPHERAL:
170 return AUDIO_MICROPHONE_LOCATION_PERIPHERAL;
171 default:
172 LOG_ALWAYS_FATAL("Unknown locationToHal conversion %d", location);
173 }
174}
Kevin Rocarddf9b4202018-05-10 19:56:08 -0700175static audio_microphone_directionality_t directionalityToHal(AudioMicrophoneDirectionality dir) {
jiabin9ff780e2018-03-19 18:19:52 -0700176 switch (dir) {
177 case AudioMicrophoneDirectionality::UNKNOWN:
178 return AUDIO_MICROPHONE_DIRECTIONALITY_UNKNOWN;
179 case AudioMicrophoneDirectionality::OMNI:
180 return AUDIO_MICROPHONE_DIRECTIONALITY_OMNI;
181 case AudioMicrophoneDirectionality::BI_DIRECTIONAL:
182 return AUDIO_MICROPHONE_DIRECTIONALITY_BI_DIRECTIONAL;
183 case AudioMicrophoneDirectionality::CARDIOID:
184 return AUDIO_MICROPHONE_DIRECTIONALITY_CARDIOID;
185 case AudioMicrophoneDirectionality::HYPER_CARDIOID:
186 return AUDIO_MICROPHONE_DIRECTIONALITY_HYPER_CARDIOID;
187 case AudioMicrophoneDirectionality::SUPER_CARDIOID:
188 return AUDIO_MICROPHONE_DIRECTIONALITY_SUPER_CARDIOID;
189 default:
190 LOG_ALWAYS_FATAL("Unknown directionalityToHal conversion %d", dir);
191 }
192}
193
Kevin Rocard070e7512018-05-22 09:29:13 -0700194void microphoneInfoToHal(const MicrophoneInfo& src,
195 audio_microphone_characteristic_t *pDst) {
jiabin9ff780e2018-03-19 18:19:52 -0700196 if (pDst != NULL) {
197 snprintf(pDst->device_id, sizeof(pDst->device_id),
198 "%s", src.deviceId.c_str());
199 pDst->device = static_cast<audio_devices_t>(src.deviceAddress.device);
200 snprintf(pDst->address, sizeof(pDst->address),
201 "%s", deviceAddressToHal(src.deviceAddress).c_str());
202 if (src.channelMapping.size() > AUDIO_CHANNEL_COUNT_MAX) {
203 ALOGW("microphoneInfoToStruct found %zu channelMapping elements. Max expected is %d",
204 src.channelMapping.size(), AUDIO_CHANNEL_COUNT_MAX);
205 }
206 size_t ch;
207 for (ch = 0; ch < src.channelMapping.size() && ch < AUDIO_CHANNEL_COUNT_MAX; ch++) {
208 pDst->channel_mapping[ch] = channelMappingToHal(src.channelMapping[ch]);
209 }
210 for (; ch < AUDIO_CHANNEL_COUNT_MAX; ch++) {
211 pDst->channel_mapping[ch] = AUDIO_MICROPHONE_CHANNEL_MAPPING_UNUSED;
212 }
213 pDst->location = locationToHal(src.location);
214 pDst->group = (audio_microphone_group_t)src.group;
215 pDst->index_in_the_group = (unsigned int)src.indexInTheGroup;
216 pDst->sensitivity = src.sensitivity;
217 pDst->max_spl = src.maxSpl;
218 pDst->min_spl = src.minSpl;
219 pDst->directionality = directionalityToHal(src.directionality);
220 pDst->num_frequency_responses = (unsigned int)src.frequencyResponse.size();
221 if (pDst->num_frequency_responses > AUDIO_MICROPHONE_MAX_FREQUENCY_RESPONSES) {
222 ALOGW("microphoneInfoToStruct found %d frequency responses. Max expected is %d",
223 pDst->num_frequency_responses, AUDIO_MICROPHONE_MAX_FREQUENCY_RESPONSES);
224 pDst->num_frequency_responses = AUDIO_MICROPHONE_MAX_FREQUENCY_RESPONSES;
225 }
226 for (size_t k = 0; k < pDst->num_frequency_responses; k++) {
227 pDst->frequency_responses[0][k] = src.frequencyResponse[k].frequency;
228 pDst->frequency_responses[1][k] = src.frequencyResponse[k].level;
229 }
230 pDst->geometric_location.x = src.position.x;
231 pDst->geometric_location.y = src.position.y;
232 pDst->geometric_location.z = src.position.z;
233 pDst->orientation.x = src.orientation.x;
234 pDst->orientation.y = src.orientation.y;
235 pDst->orientation.z = src.orientation.z;
236 }
237}
Kevin Rocard070e7512018-05-22 09:29:13 -0700238#endif
jiabin9ff780e2018-03-19 18:19:52 -0700239
Kevin Rocard070e7512018-05-22 09:29:13 -0700240} // namespace CPP_VERSION
Kevin Rocard4bcd67f2018-02-28 14:33:38 -0800241} // namespace android