1 /*
2  * Copyright (C) 2021 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 #define LOG_TAG "PolicyAidlConversion"
18 //#define LOG_NDEBUG 0
19 #include <utils/Log.h>
20 
21 #include "media/PolicyAidlConversion.h"
22 
23 #include "media/AidlConversion.h"
24 
25 namespace android {
26 
27 using base::unexpected;
28 using media::audio::common::AudioDeviceAddress;
29 
30 ConversionResult<uint32_t>
aidl2legacy_AudioMixType_uint32_t(media::AudioMixType aidl)31 aidl2legacy_AudioMixType_uint32_t(media::AudioMixType aidl) {
32     switch (aidl) {
33         case media::AudioMixType::PLAYERS:
34             return MIX_TYPE_PLAYERS;
35         case media::AudioMixType::RECORDERS:
36             return MIX_TYPE_RECORDERS;
37     }
38     return unexpected(BAD_VALUE);
39 }
40 
41 ConversionResult<media::AudioMixType>
legacy2aidl_uint32_t_AudioMixType(uint32_t legacy)42 legacy2aidl_uint32_t_AudioMixType(uint32_t legacy) {
43     switch (legacy) {
44         case MIX_TYPE_PLAYERS:
45             return media::AudioMixType::PLAYERS;
46         case MIX_TYPE_RECORDERS:
47             return media::AudioMixType::RECORDERS;
48     }
49     return unexpected(BAD_VALUE);
50 }
51 
52 ConversionResult<uint32_t>
aidl2legacy_AudioMixCallbackFlag_uint32_t(media::AudioMixCallbackFlag aidl)53 aidl2legacy_AudioMixCallbackFlag_uint32_t(media::AudioMixCallbackFlag aidl) {
54     switch (aidl) {
55         case media::AudioMixCallbackFlag::NOTIFY_ACTIVITY:
56             return AudioMix::kCbFlagNotifyActivity;
57     }
58     return unexpected(BAD_VALUE);
59 }
60 
61 ConversionResult<media::AudioMixCallbackFlag>
legacy2aidl_uint32_t_AudioMixCallbackFlag(uint32_t legacy)62 legacy2aidl_uint32_t_AudioMixCallbackFlag(uint32_t legacy) {
63     switch (legacy) {
64         case AudioMix::kCbFlagNotifyActivity:
65             return media::AudioMixCallbackFlag::NOTIFY_ACTIVITY;
66     }
67     return unexpected(BAD_VALUE);
68 }
69 
70 ConversionResult<uint32_t>
aidl2legacy_AudioMixCallbackFlag_uint32_t_mask(int32_t aidl)71 aidl2legacy_AudioMixCallbackFlag_uint32_t_mask(int32_t aidl) {
72     return convertBitmask<uint32_t, int32_t, uint32_t, media::AudioMixCallbackFlag>(
73             aidl,
74             aidl2legacy_AudioMixCallbackFlag_uint32_t,
75             indexToEnum_index<media::AudioMixCallbackFlag>,
76             enumToMask_bitmask<uint32_t, uint32_t>);
77 }
78 
79 ConversionResult<int32_t>
legacy2aidl_uint32_t_AudioMixCallbackFlag_mask(uint32_t legacy)80 legacy2aidl_uint32_t_AudioMixCallbackFlag_mask(uint32_t legacy) {
81     return convertBitmask<int32_t, uint32_t, media::AudioMixCallbackFlag, uint32_t>(
82             legacy,
83             legacy2aidl_uint32_t_AudioMixCallbackFlag,
84             indexToEnum_bitmask<uint32_t>,
85             enumToMask_index<int32_t, media::AudioMixCallbackFlag>);
86 }
87 
88 ConversionResult<uint32_t>
aidl2legacy_AudioMixRouteFlag_uint32_t(media::AudioMixRouteFlag aidl)89 aidl2legacy_AudioMixRouteFlag_uint32_t(media::AudioMixRouteFlag aidl) {
90     switch (aidl) {
91         case media::AudioMixRouteFlag::RENDER:
92             return MIX_ROUTE_FLAG_RENDER;
93         case media::AudioMixRouteFlag::LOOP_BACK:
94             return MIX_ROUTE_FLAG_LOOP_BACK;
95     }
96     return unexpected(BAD_VALUE);
97 }
98 
99 ConversionResult<media::AudioMixRouteFlag>
legacy2aidl_uint32_t_AudioMixRouteFlag(uint32_t legacy)100 legacy2aidl_uint32_t_AudioMixRouteFlag(uint32_t legacy) {
101     switch (legacy) {
102         case MIX_ROUTE_FLAG_RENDER:
103             return media::AudioMixRouteFlag::RENDER;
104         case MIX_ROUTE_FLAG_LOOP_BACK:
105             return media::AudioMixRouteFlag::LOOP_BACK;
106     }
107     return unexpected(BAD_VALUE);
108 }
109 
110 ConversionResult<uint32_t>
aidl2legacy_AudioMixRouteFlag_uint32_t_mask(int32_t aidl)111 aidl2legacy_AudioMixRouteFlag_uint32_t_mask(int32_t aidl) {
112     return convertBitmask<uint32_t, int32_t, uint32_t, media::AudioMixRouteFlag>(
113             aidl,
114             aidl2legacy_AudioMixRouteFlag_uint32_t,
115             indexToEnum_index<media::AudioMixRouteFlag>,
116             enumToMask_bitmask<uint32_t, uint32_t>);
117 }
118 
119 ConversionResult<int32_t>
legacy2aidl_uint32_t_AudioMixRouteFlag_mask(uint32_t legacy)120 legacy2aidl_uint32_t_AudioMixRouteFlag_mask(uint32_t legacy) {
121     return convertBitmask<int32_t, uint32_t, media::AudioMixRouteFlag, uint32_t>(
122             legacy,
123             legacy2aidl_uint32_t_AudioMixRouteFlag,
124             indexToEnum_bitmask<uint32_t>,
125             enumToMask_index<int32_t, media::AudioMixRouteFlag>);
126 }
127 
128 // This type is unnamed in the original definition, thus we name it here.
129 using AudioMixMatchCriterionValue = decltype(AudioMixMatchCriterion::mValue);
130 
131 ConversionResult<AudioMixMatchCriterionValue>
aidl2legacy_AudioMixMatchCriterionValue(const media::AudioMixMatchCriterionValue & aidl,uint32_t * rule)132 aidl2legacy_AudioMixMatchCriterionValue(
133         const media::AudioMixMatchCriterionValue& aidl,
134         uint32_t* rule) {
135     AudioMixMatchCriterionValue legacy;
136     *rule = 0;
137     switch (aidl.getTag()) {
138         case media::AudioMixMatchCriterionValue::usage:
139             legacy.mUsage = VALUE_OR_RETURN(
140                     aidl2legacy_AudioUsage_audio_usage_t(UNION_GET(aidl, usage).value()));
141             *rule |= RULE_MATCH_ATTRIBUTE_USAGE;
142             return legacy;
143 
144         case media::AudioMixMatchCriterionValue::source:
145             legacy.mSource = VALUE_OR_RETURN(
146                     aidl2legacy_AudioSource_audio_source_t(UNION_GET(aidl, source).value()));
147             *rule |= RULE_MATCH_ATTRIBUTE_CAPTURE_PRESET;
148             return legacy;
149 
150         case media::AudioMixMatchCriterionValue::uid:
151             legacy.mUid = VALUE_OR_RETURN(
152                     aidl2legacy_int32_t_uid_t(UNION_GET(aidl, uid).value()));
153             *rule |= RULE_MATCH_UID;
154             return legacy;
155 
156         case media::AudioMixMatchCriterionValue::userId:
157             legacy.mUserId = VALUE_OR_RETURN(
158                     convertIntegral<int>(UNION_GET(aidl, userId).value()));
159             *rule |= RULE_MATCH_USERID;
160             return legacy;
161         case media::AudioMixMatchCriterionValue::audioSessionId:
162             legacy.mAudioSessionId = VALUE_OR_RETURN(
163                     aidl2legacy_int32_t_audio_session_t(UNION_GET(aidl, audioSessionId).value()));
164             *rule |= RULE_MATCH_AUDIO_SESSION_ID;
165             return legacy;
166     }
167     return unexpected(BAD_VALUE);
168 }
169 
170 ConversionResult<media::AudioMixMatchCriterionValue>
legacy2aidl_AudioMixMatchCriterionValue(const AudioMixMatchCriterionValue & legacy,uint32_t rule)171 legacy2aidl_AudioMixMatchCriterionValue(
172         const AudioMixMatchCriterionValue& legacy,
173         uint32_t rule) {
174     media::AudioMixMatchCriterionValue aidl;
175     switch (rule) {
176         case RULE_MATCH_ATTRIBUTE_USAGE:
177             UNION_SET(aidl, usage,
178                       VALUE_OR_RETURN(legacy2aidl_audio_usage_t_AudioUsage(legacy.mUsage)));
179             break;
180 
181         case RULE_MATCH_ATTRIBUTE_CAPTURE_PRESET:
182             UNION_SET(aidl, source,
183                       VALUE_OR_RETURN(legacy2aidl_audio_source_t_AudioSource(legacy.mSource)));
184             break;
185 
186         case RULE_MATCH_UID:
187             UNION_SET(aidl, uid, VALUE_OR_RETURN(legacy2aidl_uid_t_int32_t(legacy.mUid)));
188             break;
189 
190         case RULE_MATCH_USERID:
191             UNION_SET(aidl, userId, VALUE_OR_RETURN(convertReinterpret<uint32_t>(legacy.mUserId)));
192             break;
193         case RULE_MATCH_AUDIO_SESSION_ID:
194             UNION_SET(aidl, audioSessionId,
195                 VALUE_OR_RETURN(legacy2aidl_audio_session_t_int32_t(legacy.mAudioSessionId)));
196             break;
197         default:
198             return unexpected(BAD_VALUE);
199     }
200     return aidl;
201 }
202 
203 
204 ConversionResult<AudioMixMatchCriterion>
aidl2legacy_AudioMixMatchCriterion(const media::AudioMixMatchCriterion & aidl)205 aidl2legacy_AudioMixMatchCriterion(const media::AudioMixMatchCriterion& aidl) {
206     AudioMixMatchCriterion legacy;
207     legacy.mValue = VALUE_OR_RETURN(
208             aidl2legacy_AudioMixMatchCriterionValue(aidl.value, &legacy.mRule));
209     if (aidl.invert) {
210         legacy.mRule |= RULE_EXCLUSION_MASK;
211     }
212     return legacy;
213 }
214 
215 ConversionResult<media::AudioMixMatchCriterion>
legacy2aidl_AudioMixMatchCriterion(const AudioMixMatchCriterion & legacy)216 legacy2aidl_AudioMixMatchCriterion(const AudioMixMatchCriterion& legacy) {
217     media::AudioMixMatchCriterion aidl;
218     uint32_t rule = legacy.mRule;
219     if (rule & RULE_EXCLUSION_MASK) {
220         aidl.invert = true;
221         rule &= ~RULE_EXCLUSION_MASK;
222     }
223     aidl.value = VALUE_OR_RETURN(legacy2aidl_AudioMixMatchCriterionValue(legacy.mValue, rule));
224     return aidl;
225 }
226 
227 ConversionResult<AudioMix>
aidl2legacy_AudioMix(const media::AudioMix & aidl)228 aidl2legacy_AudioMix(const media::AudioMix& aidl) {
229     AudioMix legacy;
230     RETURN_IF_ERROR(convertRange(aidl.criteria.begin(), aidl.criteria.end(),
231                                  std::back_inserter(legacy.mCriteria),
232                                  aidl2legacy_AudioMixMatchCriterion));
233     legacy.mMixType = VALUE_OR_RETURN(aidl2legacy_AudioMixType_uint32_t(aidl.mixType));
234     // See 'convertAudioMixToNative' in 'android_media_AudioSystem.cpp' -- only
235     // an output mask is expected here.
236     legacy.mFormat = VALUE_OR_RETURN(aidl2legacy_AudioConfig_audio_config_t(
237                     aidl.format, false /*isInput*/));
238     legacy.mRouteFlags = VALUE_OR_RETURN(
239             aidl2legacy_AudioMixRouteFlag_uint32_t_mask(aidl.routeFlags));
240     RETURN_IF_ERROR(aidl2legacy_AudioDevice_audio_device(
241                     aidl.device, &legacy.mDeviceType, &legacy.mDeviceAddress));
242     legacy.mCbFlags = VALUE_OR_RETURN(aidl2legacy_AudioMixCallbackFlag_uint32_t_mask(aidl.cbFlags));
243     legacy.mAllowPrivilegedMediaPlaybackCapture = aidl.allowPrivilegedMediaPlaybackCapture;
244     legacy.mVoiceCommunicationCaptureAllowed = aidl.voiceCommunicationCaptureAllowed;
245     legacy.mToken = aidl.mToken;
246     legacy.mVirtualDeviceId = aidl.mVirtualDeviceId;
247     return legacy;
248 }
249 
250 ConversionResult<media::AudioMix>
legacy2aidl_AudioMix(const AudioMix & legacy)251 legacy2aidl_AudioMix(const AudioMix& legacy) {
252     media::AudioMix aidl;
253     aidl.criteria = VALUE_OR_RETURN(
254             convertContainer<std::vector<media::AudioMixMatchCriterion>>(
255                     legacy.mCriteria,
256                     legacy2aidl_AudioMixMatchCriterion));
257     aidl.mixType = VALUE_OR_RETURN(legacy2aidl_uint32_t_AudioMixType(legacy.mMixType));
258     // See 'convertAudioMixToNative' in 'android_media_AudioSystem.cpp' -- only
259     // an output mask is expected here.
260     aidl.format = VALUE_OR_RETURN(legacy2aidl_audio_config_t_AudioConfig(
261                     legacy.mFormat, false /*isInput*/));
262     aidl.routeFlags = VALUE_OR_RETURN(
263             legacy2aidl_uint32_t_AudioMixRouteFlag_mask(legacy.mRouteFlags));
264     aidl.device = VALUE_OR_RETURN(
265             legacy2aidl_audio_device_AudioDevice(
266                     legacy.mDeviceType, legacy.mDeviceAddress));
267     aidl.cbFlags = VALUE_OR_RETURN(legacy2aidl_uint32_t_AudioMixCallbackFlag_mask(legacy.mCbFlags));
268     aidl.allowPrivilegedMediaPlaybackCapture = legacy.mAllowPrivilegedMediaPlaybackCapture;
269     aidl.voiceCommunicationCaptureAllowed = legacy.mVoiceCommunicationCaptureAllowed;
270     aidl.mToken = legacy.mToken;
271     aidl.mVirtualDeviceId = legacy.mVirtualDeviceId;
272     return aidl;
273 }
274 
275 ConversionResult<audio_policy_dev_state_t>
aidl2legacy_AudioPolicyDeviceState_audio_policy_dev_state_t(media::AudioPolicyDeviceState aidl)276 aidl2legacy_AudioPolicyDeviceState_audio_policy_dev_state_t(media::AudioPolicyDeviceState aidl) {
277     switch (aidl) {
278         case media::AudioPolicyDeviceState::UNAVAILABLE:
279             return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
280         case media::AudioPolicyDeviceState::AVAILABLE:
281             return AUDIO_POLICY_DEVICE_STATE_AVAILABLE;
282     }
283     return unexpected(BAD_VALUE);
284 }
285 
286 ConversionResult<media::AudioPolicyDeviceState>
legacy2aidl_audio_policy_dev_state_t_AudioPolicyDeviceState(audio_policy_dev_state_t legacy)287 legacy2aidl_audio_policy_dev_state_t_AudioPolicyDeviceState(audio_policy_dev_state_t legacy) {
288     switch (legacy) {
289         case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE:
290             return media::AudioPolicyDeviceState::UNAVAILABLE;
291         case AUDIO_POLICY_DEVICE_STATE_AVAILABLE:
292             return media::AudioPolicyDeviceState::AVAILABLE;
293         case AUDIO_POLICY_DEVICE_STATE_CNT:
294             break;
295     }
296     return unexpected(BAD_VALUE);
297 }
298 
299 ConversionResult<audio_policy_force_use_t>
aidl2legacy_AudioPolicyForceUse_audio_policy_force_use_t(media::AudioPolicyForceUse aidl)300 aidl2legacy_AudioPolicyForceUse_audio_policy_force_use_t(media::AudioPolicyForceUse aidl) {
301     switch (aidl) {
302         case media::AudioPolicyForceUse::COMMUNICATION:
303             return AUDIO_POLICY_FORCE_FOR_COMMUNICATION;
304         case media::AudioPolicyForceUse::MEDIA:
305             return AUDIO_POLICY_FORCE_FOR_MEDIA;
306         case media::AudioPolicyForceUse::RECORD:
307             return AUDIO_POLICY_FORCE_FOR_RECORD;
308         case media::AudioPolicyForceUse::DOCK:
309             return AUDIO_POLICY_FORCE_FOR_DOCK;
310         case media::AudioPolicyForceUse::SYSTEM:
311             return AUDIO_POLICY_FORCE_FOR_SYSTEM;
312         case media::AudioPolicyForceUse::HDMI_SYSTEM_AUDIO:
313             return AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO;
314         case media::AudioPolicyForceUse::ENCODED_SURROUND:
315             return AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND;
316         case media::AudioPolicyForceUse::VIBRATE_RINGING:
317             return AUDIO_POLICY_FORCE_FOR_VIBRATE_RINGING;
318     }
319     return unexpected(BAD_VALUE);
320 }
321 
322 ConversionResult<media::AudioPolicyForceUse>
legacy2aidl_audio_policy_force_use_t_AudioPolicyForceUse(audio_policy_force_use_t legacy)323 legacy2aidl_audio_policy_force_use_t_AudioPolicyForceUse(audio_policy_force_use_t legacy) {
324     switch (legacy) {
325         case AUDIO_POLICY_FORCE_FOR_COMMUNICATION:
326             return media::AudioPolicyForceUse::COMMUNICATION;
327         case AUDIO_POLICY_FORCE_FOR_MEDIA:
328             return media::AudioPolicyForceUse::MEDIA;
329         case AUDIO_POLICY_FORCE_FOR_RECORD:
330             return media::AudioPolicyForceUse::RECORD;
331         case AUDIO_POLICY_FORCE_FOR_DOCK:
332             return media::AudioPolicyForceUse::DOCK;
333         case AUDIO_POLICY_FORCE_FOR_SYSTEM:
334             return media::AudioPolicyForceUse::SYSTEM;
335         case AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO:
336             return media::AudioPolicyForceUse::HDMI_SYSTEM_AUDIO;
337         case AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND:
338             return media::AudioPolicyForceUse::ENCODED_SURROUND;
339         case AUDIO_POLICY_FORCE_FOR_VIBRATE_RINGING:
340             return media::AudioPolicyForceUse::VIBRATE_RINGING;
341         case AUDIO_POLICY_FORCE_USE_CNT:
342             break;
343     }
344     return unexpected(BAD_VALUE);
345 }
346 
347 ConversionResult<audio_policy_forced_cfg_t>
aidl2legacy_AudioPolicyForcedConfig_audio_policy_forced_cfg_t(media::AudioPolicyForcedConfig aidl)348 aidl2legacy_AudioPolicyForcedConfig_audio_policy_forced_cfg_t(media::AudioPolicyForcedConfig aidl) {
349     switch (aidl) {
350         case media::AudioPolicyForcedConfig::NONE:
351             return AUDIO_POLICY_FORCE_NONE;
352         case media::AudioPolicyForcedConfig::SPEAKER:
353             return AUDIO_POLICY_FORCE_SPEAKER;
354         case media::AudioPolicyForcedConfig::HEADPHONES:
355             return AUDIO_POLICY_FORCE_HEADPHONES;
356         case media::AudioPolicyForcedConfig::BT_SCO:
357             return AUDIO_POLICY_FORCE_BT_SCO;
358         case media::AudioPolicyForcedConfig::BT_A2DP:
359             return AUDIO_POLICY_FORCE_BT_A2DP;
360         case media::AudioPolicyForcedConfig::WIRED_ACCESSORY:
361             return AUDIO_POLICY_FORCE_WIRED_ACCESSORY;
362         case media::AudioPolicyForcedConfig::BT_CAR_DOCK:
363             return AUDIO_POLICY_FORCE_BT_CAR_DOCK;
364         case media::AudioPolicyForcedConfig::BT_DESK_DOCK:
365             return AUDIO_POLICY_FORCE_BT_DESK_DOCK;
366         case media::AudioPolicyForcedConfig::ANALOG_DOCK:
367             return AUDIO_POLICY_FORCE_ANALOG_DOCK;
368         case media::AudioPolicyForcedConfig::DIGITAL_DOCK:
369             return AUDIO_POLICY_FORCE_DIGITAL_DOCK;
370         case media::AudioPolicyForcedConfig::NO_BT_A2DP:
371             return AUDIO_POLICY_FORCE_NO_BT_A2DP;
372         case media::AudioPolicyForcedConfig::SYSTEM_ENFORCED:
373             return AUDIO_POLICY_FORCE_SYSTEM_ENFORCED;
374         case media::AudioPolicyForcedConfig::HDMI_SYSTEM_AUDIO_ENFORCED:
375             return AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED;
376         case media::AudioPolicyForcedConfig::ENCODED_SURROUND_NEVER:
377             return AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER;
378         case media::AudioPolicyForcedConfig::ENCODED_SURROUND_ALWAYS:
379             return AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS;
380         case media::AudioPolicyForcedConfig::ENCODED_SURROUND_MANUAL:
381             return AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL;
382     }
383     return unexpected(BAD_VALUE);
384 }
385 
386 ConversionResult<media::AudioPolicyForcedConfig>
legacy2aidl_audio_policy_forced_cfg_t_AudioPolicyForcedConfig(audio_policy_forced_cfg_t legacy)387 legacy2aidl_audio_policy_forced_cfg_t_AudioPolicyForcedConfig(audio_policy_forced_cfg_t legacy) {
388     switch (legacy) {
389         case AUDIO_POLICY_FORCE_NONE:
390             return media::AudioPolicyForcedConfig::NONE;
391         case AUDIO_POLICY_FORCE_SPEAKER:
392             return media::AudioPolicyForcedConfig::SPEAKER;
393         case AUDIO_POLICY_FORCE_HEADPHONES:
394             return media::AudioPolicyForcedConfig::HEADPHONES;
395         case AUDIO_POLICY_FORCE_BT_SCO:
396             return media::AudioPolicyForcedConfig::BT_SCO;
397         case AUDIO_POLICY_FORCE_BT_A2DP:
398             return media::AudioPolicyForcedConfig::BT_A2DP;
399         case AUDIO_POLICY_FORCE_WIRED_ACCESSORY:
400             return media::AudioPolicyForcedConfig::WIRED_ACCESSORY;
401         case AUDIO_POLICY_FORCE_BT_CAR_DOCK:
402             return media::AudioPolicyForcedConfig::BT_CAR_DOCK;
403         case AUDIO_POLICY_FORCE_BT_DESK_DOCK:
404             return media::AudioPolicyForcedConfig::BT_DESK_DOCK;
405         case AUDIO_POLICY_FORCE_ANALOG_DOCK:
406             return media::AudioPolicyForcedConfig::ANALOG_DOCK;
407         case AUDIO_POLICY_FORCE_DIGITAL_DOCK:
408             return media::AudioPolicyForcedConfig::DIGITAL_DOCK;
409         case AUDIO_POLICY_FORCE_NO_BT_A2DP:
410             return media::AudioPolicyForcedConfig::NO_BT_A2DP;
411         case AUDIO_POLICY_FORCE_SYSTEM_ENFORCED:
412             return media::AudioPolicyForcedConfig::SYSTEM_ENFORCED;
413         case AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED:
414             return media::AudioPolicyForcedConfig::HDMI_SYSTEM_AUDIO_ENFORCED;
415         case AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER:
416             return media::AudioPolicyForcedConfig::ENCODED_SURROUND_NEVER;
417         case AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS:
418             return media::AudioPolicyForcedConfig::ENCODED_SURROUND_ALWAYS;
419         case AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL:
420             return media::AudioPolicyForcedConfig::ENCODED_SURROUND_MANUAL;
421         case AUDIO_POLICY_FORCE_CFG_CNT:
422             break;
423     }
424     return unexpected(BAD_VALUE);
425 }
426 
427 ConversionResult<device_role_t>
aidl2legacy_DeviceRole_device_role_t(media::DeviceRole aidl)428 aidl2legacy_DeviceRole_device_role_t(media::DeviceRole aidl) {
429     switch (aidl) {
430         case media::DeviceRole::NONE:
431             return DEVICE_ROLE_NONE;
432         case media::DeviceRole::PREFERRED:
433             return DEVICE_ROLE_PREFERRED;
434         case media::DeviceRole::DISABLED:
435             return DEVICE_ROLE_DISABLED;
436     }
437     return unexpected(BAD_VALUE);
438 }
439 
440 ConversionResult<media::DeviceRole>
legacy2aidl_device_role_t_DeviceRole(device_role_t legacy)441 legacy2aidl_device_role_t_DeviceRole(device_role_t legacy) {
442     switch (legacy) {
443         case DEVICE_ROLE_NONE:
444             return media::DeviceRole::NONE;
445         case DEVICE_ROLE_PREFERRED:
446             return media::DeviceRole::PREFERRED;
447         case DEVICE_ROLE_DISABLED:
448             return media::DeviceRole::DISABLED;
449     }
450     return unexpected(BAD_VALUE);
451 }
452 
453 ConversionResult<audio_offload_mode_t>
aidl2legacy_AudioOffloadMode_audio_offload_mode_t(media::AudioOffloadMode aidl)454 aidl2legacy_AudioOffloadMode_audio_offload_mode_t(media::AudioOffloadMode aidl) {
455     switch (aidl) {
456         case media::AudioOffloadMode::NOT_SUPPORTED:
457             return AUDIO_OFFLOAD_NOT_SUPPORTED;
458         case media::AudioOffloadMode::SUPPORTED:
459             return AUDIO_OFFLOAD_SUPPORTED;
460         case media::AudioOffloadMode::GAPLESS_SUPPORTED:
461             return AUDIO_OFFLOAD_GAPLESS_SUPPORTED;
462     }
463     return unexpected(BAD_VALUE);
464 }
465 
466 ConversionResult<media::AudioOffloadMode>
legacy2aidl_audio_offload_mode_t_AudioOffloadMode(audio_offload_mode_t legacy)467 legacy2aidl_audio_offload_mode_t_AudioOffloadMode(audio_offload_mode_t legacy) {
468     switch (legacy) {
469         case AUDIO_OFFLOAD_NOT_SUPPORTED:
470             return media::AudioOffloadMode::NOT_SUPPORTED;
471         case AUDIO_OFFLOAD_SUPPORTED:
472             return media::AudioOffloadMode::SUPPORTED;
473         case AUDIO_OFFLOAD_GAPLESS_SUPPORTED:
474             return media::AudioOffloadMode::GAPLESS_SUPPORTED;
475     }
476     return unexpected(BAD_VALUE);
477 }
478 
479 ConversionResult<audio_mixer_behavior_t>
aidl2legacy_AudioMixerBehavior_audio_mixer_behavior_t(media::AudioMixerBehavior aidl)480 aidl2legacy_AudioMixerBehavior_audio_mixer_behavior_t(media::AudioMixerBehavior aidl) {
481     switch (aidl) {
482         case media::AudioMixerBehavior::DEFAULT:
483             return AUDIO_MIXER_BEHAVIOR_DEFAULT;
484         case media::AudioMixerBehavior::BIT_PERFECT:
485             return AUDIO_MIXER_BEHAVIOR_BIT_PERFECT;
486         case media::AudioMixerBehavior::INVALID:
487             return AUDIO_MIXER_BEHAVIOR_INVALID;
488     }
489     return unexpected(BAD_VALUE);
490 }
491 ConversionResult<media::AudioMixerBehavior>
legacy2aidl_audio_mixer_behavior_t_AudioMixerBehavior(audio_mixer_behavior_t legacy)492 legacy2aidl_audio_mixer_behavior_t_AudioMixerBehavior(audio_mixer_behavior_t legacy) {
493     switch (legacy) {
494         case AUDIO_MIXER_BEHAVIOR_DEFAULT:
495             return media::AudioMixerBehavior::DEFAULT;
496         case AUDIO_MIXER_BEHAVIOR_BIT_PERFECT:
497             return media::AudioMixerBehavior::BIT_PERFECT;
498         case AUDIO_MIXER_BEHAVIOR_INVALID:
499             return media::AudioMixerBehavior::INVALID;
500     }
501     return unexpected(BAD_VALUE);
502 }
503 
504 ConversionResult<audio_mixer_attributes_t>
aidl2legacy_AudioMixerAttributesInternal_audio_mixer_attributes_t(const media::AudioMixerAttributesInternal & aidl)505 aidl2legacy_AudioMixerAttributesInternal_audio_mixer_attributes_t(
506         const media::AudioMixerAttributesInternal& aidl) {
507     audio_mixer_attributes_t legacy = AUDIO_MIXER_ATTRIBUTES_INITIALIZER;
508     legacy.config = VALUE_OR_RETURN(
509             aidl2legacy_AudioConfigBase_audio_config_base_t(aidl.config, false /*isInput*/));
510     legacy.mixer_behavior = VALUE_OR_RETURN(
511             aidl2legacy_AudioMixerBehavior_audio_mixer_behavior_t(aidl.mixerBehavior));
512     return legacy;
513 }
514 ConversionResult<media::AudioMixerAttributesInternal>
legacy2aidl_audio_mixer_attributes_t_AudioMixerAttributesInternal(const audio_mixer_attributes & legacy)515 legacy2aidl_audio_mixer_attributes_t_AudioMixerAttributesInternal(
516         const audio_mixer_attributes& legacy) {
517     media::AudioMixerAttributesInternal aidl;
518     aidl.config = VALUE_OR_RETURN(
519             legacy2aidl_audio_config_base_t_AudioConfigBase(legacy.config, false /*isInput*/));
520     aidl.mixerBehavior = VALUE_OR_RETURN(
521             legacy2aidl_audio_mixer_behavior_t_AudioMixerBehavior(legacy.mixer_behavior));
522     return aidl;
523 }
524 
525 
526 }  // namespace android
527