1 /*
2  * Copyright (C) 2020 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 <algorithm>
18 #include <unordered_map>
19 #include <utility>
20 #include <vector>
21 
22 #define LOG_TAG "AidlConversion"
23 //#define LOG_NDEBUG 0
24 #include <utils/Log.h>
25 
26 #include "media/AidlConversion.h"
27 
28 #include <media/ShmemCompat.h>
29 #include <media/stagefright/foundation/MediaDefs.h>
30 
31 ////////////////////////////////////////////////////////////////////////////////////////////////////
32 // Utilities
33 
34 namespace android {
35 
36 using base::unexpected;
37 using media::audio::common::AudioChannelLayout;
38 using media::audio::common::AudioConfig;
39 using media::audio::common::AudioConfigBase;
40 using media::audio::common::AudioContentType;
41 using media::audio::common::AudioDevice;
42 using media::audio::common::AudioDeviceAddress;
43 using media::audio::common::AudioDeviceDescription;
44 using media::audio::common::AudioDeviceType;
45 using media::audio::common::AudioFormatDescription;
46 using media::audio::common::AudioFormatType;
47 using media::audio::common::AudioGain;
48 using media::audio::common::AudioGainConfig;
49 using media::audio::common::AudioGainMode;
50 using media::audio::common::AudioInputFlags;
51 using media::audio::common::AudioIoFlags;
52 using media::audio::common::AudioMode;
53 using media::audio::common::AudioOffloadInfo;
54 using media::audio::common::AudioOutputFlags;
55 using media::audio::common::AudioPortDeviceExt;
56 using media::audio::common::AudioPortExt;
57 using media::audio::common::AudioPortMixExt;
58 using media::audio::common::AudioPortMixExtUseCase;
59 using media::audio::common::AudioProfile;
60 using media::audio::common::AudioSource;
61 using media::audio::common::AudioStandard;
62 using media::audio::common::AudioStreamType;
63 using media::audio::common::AudioUsage;
64 using media::audio::common::AudioUuid;
65 using media::audio::common::ExtraAudioDescriptor;
66 using media::audio::common::Int;
67 using media::audio::common::PcmType;
68 
aidl2legacy_AudioPortRole_audio_port_role_t(media::AudioPortRole aidl)69 ConversionResult<audio_port_role_t> aidl2legacy_AudioPortRole_audio_port_role_t(
70         media::AudioPortRole aidl) {
71     switch (aidl) {
72         case media::AudioPortRole::NONE:
73             return AUDIO_PORT_ROLE_NONE;
74         case media::AudioPortRole::SOURCE:
75             return AUDIO_PORT_ROLE_SOURCE;
76         case media::AudioPortRole::SINK:
77             return AUDIO_PORT_ROLE_SINK;
78     }
79     return unexpected(BAD_VALUE);
80 }
81 
legacy2aidl_audio_port_role_t_AudioPortRole(audio_port_role_t legacy)82 ConversionResult<media::AudioPortRole> legacy2aidl_audio_port_role_t_AudioPortRole(
83         audio_port_role_t legacy) {
84     switch (legacy) {
85         case AUDIO_PORT_ROLE_NONE:
86             return media::AudioPortRole::NONE;
87         case AUDIO_PORT_ROLE_SOURCE:
88             return media::AudioPortRole::SOURCE;
89         case AUDIO_PORT_ROLE_SINK:
90             return media::AudioPortRole::SINK;
91     }
92     return unexpected(BAD_VALUE);
93 }
94 
aidl2legacy_AudioPortType_audio_port_type_t(media::AudioPortType aidl)95 ConversionResult<audio_port_type_t> aidl2legacy_AudioPortType_audio_port_type_t(
96         media::AudioPortType aidl) {
97     switch (aidl) {
98         case media::AudioPortType::NONE:
99             return AUDIO_PORT_TYPE_NONE;
100         case media::AudioPortType::DEVICE:
101             return AUDIO_PORT_TYPE_DEVICE;
102         case media::AudioPortType::MIX:
103             return AUDIO_PORT_TYPE_MIX;
104         case media::AudioPortType::SESSION:
105             return AUDIO_PORT_TYPE_SESSION;
106     }
107     return unexpected(BAD_VALUE);
108 }
109 
legacy2aidl_audio_port_type_t_AudioPortType(audio_port_type_t legacy)110 ConversionResult<media::AudioPortType> legacy2aidl_audio_port_type_t_AudioPortType(
111         audio_port_type_t legacy) {
112     switch (legacy) {
113         case AUDIO_PORT_TYPE_NONE:
114             return media::AudioPortType::NONE;
115         case AUDIO_PORT_TYPE_DEVICE:
116             return media::AudioPortType::DEVICE;
117         case AUDIO_PORT_TYPE_MIX:
118             return media::AudioPortType::MIX;
119         case AUDIO_PORT_TYPE_SESSION:
120             return media::AudioPortType::SESSION;
121     }
122     return unexpected(BAD_VALUE);
123 }
124 
portDirection(media::AudioPortRole role,media::AudioPortType type)125 ConversionResult<AudioPortDirection> portDirection(
126         media::AudioPortRole role, media::AudioPortType type) {
127     audio_port_role_t legacyRole = VALUE_OR_RETURN(
128             aidl2legacy_AudioPortRole_audio_port_role_t(role));
129     audio_port_type_t legacyType = VALUE_OR_RETURN(
130             aidl2legacy_AudioPortType_audio_port_type_t(type));
131     return portDirection(legacyRole, legacyType);
132 }
133 
aidl2legacy_AudioIoConfigEvent_audio_io_config_event_t(media::AudioIoConfigEvent aidl)134 ConversionResult<audio_io_config_event_t> aidl2legacy_AudioIoConfigEvent_audio_io_config_event_t(
135         media::AudioIoConfigEvent aidl) {
136     switch (aidl) {
137         case media::AudioIoConfigEvent::OUTPUT_REGISTERED:
138             return AUDIO_OUTPUT_REGISTERED;
139         case media::AudioIoConfigEvent::OUTPUT_OPENED:
140             return AUDIO_OUTPUT_OPENED;
141         case media::AudioIoConfigEvent::OUTPUT_CLOSED:
142             return AUDIO_OUTPUT_CLOSED;
143         case media::AudioIoConfigEvent::OUTPUT_CONFIG_CHANGED:
144             return AUDIO_OUTPUT_CONFIG_CHANGED;
145         case media::AudioIoConfigEvent::INPUT_REGISTERED:
146             return AUDIO_INPUT_REGISTERED;
147         case media::AudioIoConfigEvent::INPUT_OPENED:
148             return AUDIO_INPUT_OPENED;
149         case media::AudioIoConfigEvent::INPUT_CLOSED:
150             return AUDIO_INPUT_CLOSED;
151         case media::AudioIoConfigEvent::INPUT_CONFIG_CHANGED:
152             return AUDIO_INPUT_CONFIG_CHANGED;
153         case media::AudioIoConfigEvent::CLIENT_STARTED:
154             return AUDIO_CLIENT_STARTED;
155     }
156     return unexpected(BAD_VALUE);
157 }
158 
legacy2aidl_audio_io_config_event_t_AudioIoConfigEvent(audio_io_config_event_t legacy)159 ConversionResult<media::AudioIoConfigEvent> legacy2aidl_audio_io_config_event_t_AudioIoConfigEvent(
160         audio_io_config_event_t legacy) {
161     switch (legacy) {
162         case AUDIO_OUTPUT_REGISTERED:
163             return media::AudioIoConfigEvent::OUTPUT_REGISTERED;
164         case AUDIO_OUTPUT_OPENED:
165             return media::AudioIoConfigEvent::OUTPUT_OPENED;
166         case AUDIO_OUTPUT_CLOSED:
167             return media::AudioIoConfigEvent::OUTPUT_CLOSED;
168         case AUDIO_OUTPUT_CONFIG_CHANGED:
169             return media::AudioIoConfigEvent::OUTPUT_CONFIG_CHANGED;
170         case AUDIO_INPUT_REGISTERED:
171             return media::AudioIoConfigEvent::INPUT_REGISTERED;
172         case AUDIO_INPUT_OPENED:
173             return media::AudioIoConfigEvent::INPUT_OPENED;
174         case AUDIO_INPUT_CLOSED:
175             return media::AudioIoConfigEvent::INPUT_CLOSED;
176         case AUDIO_INPUT_CONFIG_CHANGED:
177             return media::AudioIoConfigEvent::INPUT_CONFIG_CHANGED;
178         case AUDIO_CLIENT_STARTED:
179             return media::AudioIoConfigEvent::CLIENT_STARTED;
180     }
181     return unexpected(BAD_VALUE);
182 }
aidl2legacy_AudioPortMixExtUseCase(const AudioPortMixExtUseCase & aidl,media::AudioPortRole role)183 ConversionResult<audio_port_config_mix_ext_usecase> aidl2legacy_AudioPortMixExtUseCase(
184         const AudioPortMixExtUseCase& aidl, media::AudioPortRole role) {
185     switch (role) {
186         case media::AudioPortRole::NONE: {
187             audio_port_config_mix_ext_usecase legacy;
188             // Just verify that the union is empty.
189             VALUE_OR_RETURN(UNION_GET(aidl, unspecified));
190             return legacy;
191         }
192         case media::AudioPortRole::SOURCE:
193             return aidl2legacy_AudioPortMixExtUseCase_audio_port_config_mix_ext_usecase(
194                     aidl, false /*isInput*/);
195         case media::AudioPortRole::SINK:
196             return aidl2legacy_AudioPortMixExtUseCase_audio_port_config_mix_ext_usecase(
197                     aidl, true /*isInput*/);
198     }
199     LOG_ALWAYS_FATAL("Shouldn't get here"); // with -Werror,-Wswitch may compile-time fail
200 }
201 
legacy2aidl_AudioPortMixExtUseCase(const audio_port_config_mix_ext_usecase & legacy,audio_port_role_t role)202 ConversionResult<AudioPortMixExtUseCase> legacy2aidl_AudioPortMixExtUseCase(
203         const audio_port_config_mix_ext_usecase& legacy, audio_port_role_t role) {
204     switch (role) {
205         case AUDIO_PORT_ROLE_NONE: {
206             AudioPortMixExtUseCase aidl;
207             UNION_SET(aidl, unspecified, false);
208             return aidl;
209         }
210         case AUDIO_PORT_ROLE_SOURCE:
211             return legacy2aidl_audio_port_config_mix_ext_usecase_AudioPortMixExtUseCase(
212                     legacy, false /*isInput*/);
213         case AUDIO_PORT_ROLE_SINK:
214             return legacy2aidl_audio_port_config_mix_ext_usecase_AudioPortMixExtUseCase(
215                     legacy, true /*isInput*/);
216     }
217     LOG_ALWAYS_FATAL("Shouldn't get here"); // with -Werror,-Wswitch may compile-time fail
218 }
219 
aidl2legacy_AudioPortMixExt(const AudioPortMixExt & aidl,media::AudioPortRole role,const media::AudioPortMixExtSys & aidlMixExt)220 ConversionResult<audio_port_config_mix_ext> aidl2legacy_AudioPortMixExt(
221         const AudioPortMixExt& aidl, media::AudioPortRole role,
222         const media::AudioPortMixExtSys& aidlMixExt) {
223     // Not using HAL-level 'aidl2legacy_AudioPortMixExt' as it does not support
224     // 'media::AudioPortRole::NONE'.
225     audio_port_config_mix_ext legacy;
226     legacy.hw_module = VALUE_OR_RETURN(
227             aidl2legacy_int32_t_audio_module_handle_t(aidlMixExt.hwModule));
228     legacy.handle = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_io_handle_t(aidl.handle));
229     legacy.usecase = VALUE_OR_RETURN(aidl2legacy_AudioPortMixExtUseCase(aidl.usecase, role));
230     return legacy;
231 }
232 
legacy2aidl_AudioPortMixExt(const audio_port_config_mix_ext & legacy,audio_port_role_t role,AudioPortMixExt * aidl,media::AudioPortMixExtSys * aidlMixExt)233 status_t legacy2aidl_AudioPortMixExt(
234         const audio_port_config_mix_ext& legacy, audio_port_role_t role,
235         AudioPortMixExt* aidl, media::AudioPortMixExtSys* aidlMixExt) {
236     // Not using HAL-level 'legacy2aidl_AudioPortMixExt' as it does not support
237     // 'AUDIO_PORT_ROLE_NONE'.
238     aidlMixExt->hwModule = VALUE_OR_RETURN_STATUS(
239             legacy2aidl_audio_module_handle_t_int32_t(legacy.hw_module));
240     aidl->handle = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_io_handle_t_int32_t(legacy.handle));
241     aidl->usecase = VALUE_OR_RETURN_STATUS(
242             legacy2aidl_AudioPortMixExtUseCase(legacy.usecase, role));
243     return OK;
244 }
245 
246 ConversionResult<audio_port_config_session_ext>
aidl2legacy_int32_t_audio_port_config_session_ext(int32_t aidl)247 aidl2legacy_int32_t_audio_port_config_session_ext(int32_t aidl) {
248     audio_port_config_session_ext legacy;
249     legacy.session = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_session_t(aidl));
250     return legacy;
251 }
252 
253 ConversionResult<int32_t>
legacy2aidl_audio_port_config_session_ext_int32_t(const audio_port_config_session_ext & legacy)254 legacy2aidl_audio_port_config_session_ext_int32_t(
255         const audio_port_config_session_ext& legacy) {
256     return legacy2aidl_audio_session_t_int32_t(legacy.session);
257 }
258 
259 ConversionResult<audio_port_config_device_ext>
aidl2legacy_AudioPortDeviceExt_audio_port_config_device_ext(const AudioPortDeviceExt & aidl,const media::AudioPortDeviceExtSys & aidlDeviceExt)260 aidl2legacy_AudioPortDeviceExt_audio_port_config_device_ext(
261         const AudioPortDeviceExt& aidl, const media::AudioPortDeviceExtSys& aidlDeviceExt) {
262     audio_port_config_device_ext legacy = VALUE_OR_RETURN(
263             aidl2legacy_AudioPortDeviceExt_audio_port_config_device_ext(aidl));
264     legacy.hw_module = VALUE_OR_RETURN(
265             aidl2legacy_int32_t_audio_module_handle_t(aidlDeviceExt.hwModule));
266     return legacy;
267 }
268 
legacy2aidl_audio_port_config_device_ext_AudioPortDeviceExt(const audio_port_config_device_ext & legacy,AudioPortDeviceExt * aidl,media::AudioPortDeviceExtSys * aidlDeviceExt)269 status_t legacy2aidl_audio_port_config_device_ext_AudioPortDeviceExt(
270         const audio_port_config_device_ext& legacy,
271         AudioPortDeviceExt* aidl, media::AudioPortDeviceExtSys* aidlDeviceExt) {
272     *aidl = VALUE_OR_RETURN_STATUS(
273             legacy2aidl_audio_port_config_device_ext_AudioPortDeviceExt(legacy));
274     aidlDeviceExt->hwModule = VALUE_OR_RETURN_STATUS(
275             legacy2aidl_audio_module_handle_t_int32_t(legacy.hw_module));
276     return OK;
277 }
278 
279 // This type is unnamed in the original definition, thus we name it here.
280 using audio_port_config_ext = decltype(audio_port_config::ext);
281 
aidl2legacy_AudioPortExt_audio_port_config_ext(const AudioPortExt & aidl,media::AudioPortType type,media::AudioPortRole role,const media::AudioPortExtSys & aidlSys)282 ConversionResult<audio_port_config_ext> aidl2legacy_AudioPortExt_audio_port_config_ext(
283         const AudioPortExt& aidl, media::AudioPortType type,
284         media::AudioPortRole role, const media::AudioPortExtSys& aidlSys) {
285     // Not using HAL-level 'aidl2legacy_AudioPortExt_audio_port_config_ext' as it does not support
286     // 'media::AudioPortType::SESSION'.
287     audio_port_config_ext legacy;
288     switch (type) {
289         case media::AudioPortType::NONE:
290             // Just verify that the union is empty.
291             VALUE_OR_RETURN(UNION_GET(aidl, unspecified));
292             return legacy;
293         case media::AudioPortType::DEVICE:
294             legacy.device = VALUE_OR_RETURN(
295                     aidl2legacy_AudioPortDeviceExt_audio_port_config_device_ext(
296                             VALUE_OR_RETURN(UNION_GET(aidl, device)),
297                             VALUE_OR_RETURN(UNION_GET(aidlSys, device))));
298             return legacy;
299         case media::AudioPortType::MIX:
300             legacy.mix = VALUE_OR_RETURN(
301                     aidl2legacy_AudioPortMixExt(
302                             VALUE_OR_RETURN(UNION_GET(aidl, mix)), role,
303                             VALUE_OR_RETURN(UNION_GET(aidlSys, mix))));
304             return legacy;
305         case media::AudioPortType::SESSION:
306             legacy.session = VALUE_OR_RETURN(
307                     aidl2legacy_int32_t_audio_port_config_session_ext(
308                             VALUE_OR_RETURN(UNION_GET(aidlSys, session))));
309             return legacy;
310 
311     }
312     LOG_ALWAYS_FATAL("Shouldn't get here"); // with -Werror,-Wswitch may compile-time fail
313 }
314 
legacy2aidl_AudioPortExt(const audio_port_config_ext & legacy,audio_port_type_t type,audio_port_role_t role,AudioPortExt * aidl,media::AudioPortExtSys * aidlSys)315 status_t legacy2aidl_AudioPortExt(
316         const audio_port_config_ext& legacy, audio_port_type_t type, audio_port_role_t role,
317         AudioPortExt* aidl, media::AudioPortExtSys* aidlSys) {
318     // Not using HAL-level 'aidl2legacy_AudioPortExt_audio_port_config_ext' as it does not support
319     // 'AUDIO_PORT_TYPE_SESSION'.
320     switch (type) {
321         case AUDIO_PORT_TYPE_NONE:
322             UNION_SET(*aidl, unspecified, false);
323             UNION_SET(*aidlSys, unspecified, false);
324             return OK;
325         case AUDIO_PORT_TYPE_DEVICE: {
326             AudioPortDeviceExt device;
327             media::AudioPortDeviceExtSys deviceSys;
328             RETURN_STATUS_IF_ERROR(
329                     legacy2aidl_audio_port_config_device_ext_AudioPortDeviceExt(
330                             legacy.device, &device, &deviceSys));
331             UNION_SET(*aidl, device, device);
332             UNION_SET(*aidlSys, device, deviceSys);
333             return OK;
334         }
335         case AUDIO_PORT_TYPE_MIX: {
336             AudioPortMixExt mix;
337             media::AudioPortMixExtSys mixSys;
338             RETURN_STATUS_IF_ERROR(legacy2aidl_AudioPortMixExt(legacy.mix, role, &mix, &mixSys));
339             UNION_SET(*aidl, mix, mix);
340             UNION_SET(*aidlSys, mix, mixSys);
341             return OK;
342         }
343         case AUDIO_PORT_TYPE_SESSION:
344             UNION_SET(*aidl, unspecified, false);
345             UNION_SET(*aidlSys, session, VALUE_OR_RETURN_STATUS(
346                             legacy2aidl_audio_port_config_session_ext_int32_t(legacy.session)));
347             return OK;
348     }
349     LOG_ALWAYS_FATAL("Shouldn't get here"); // with -Werror,-Wswitch may compile-time fail
350 }
351 
aidl2legacy_AudioPortConfigFw_audio_port_config(const media::AudioPortConfigFw & aidl,int32_t * aidlPortId)352 ConversionResult<audio_port_config> aidl2legacy_AudioPortConfigFw_audio_port_config(
353         const media::AudioPortConfigFw& aidl, int32_t* aidlPortId) {
354     const bool isInput = VALUE_OR_RETURN(
355             portDirection(aidl.sys.role, aidl.sys.type)) == AudioPortDirection::INPUT;
356     audio_port_config legacy;
357     int32_t aidlPortIdHolder;
358     RETURN_IF_ERROR(aidl2legacy_AudioPortConfig_audio_port_config(
359                     aidl.hal, isInput, &legacy, &aidlPortIdHolder));
360     if (aidlPortId != nullptr) *aidlPortId = aidlPortIdHolder;
361     legacy.role = VALUE_OR_RETURN(aidl2legacy_AudioPortRole_audio_port_role_t(aidl.sys.role));
362     legacy.type = VALUE_OR_RETURN(aidl2legacy_AudioPortType_audio_port_type_t(aidl.sys.type));
363     legacy.ext = VALUE_OR_RETURN(
364             aidl2legacy_AudioPortExt_audio_port_config_ext(
365                     aidl.hal.ext, aidl.sys.type, aidl.sys.role, aidl.sys.ext));
366     return legacy;
367 }
368 
legacy2aidl_audio_port_config_AudioPortConfigFw(const audio_port_config & legacy,int32_t portId)369 ConversionResult<media::AudioPortConfigFw> legacy2aidl_audio_port_config_AudioPortConfigFw(
370         const audio_port_config& legacy, int32_t portId) {
371     const bool isInput = VALUE_OR_RETURN(
372             portDirection(legacy.role, legacy.type)) == AudioPortDirection::INPUT;
373     media::AudioPortConfigFw aidl;
374     aidl.hal = VALUE_OR_RETURN(
375             legacy2aidl_audio_port_config_AudioPortConfig(legacy, isInput, portId));
376     aidl.sys.role = VALUE_OR_RETURN(legacy2aidl_audio_port_role_t_AudioPortRole(legacy.role));
377     aidl.sys.type = VALUE_OR_RETURN(legacy2aidl_audio_port_type_t_AudioPortType(legacy.type));
378     RETURN_IF_ERROR(legacy2aidl_AudioPortExt(legacy.ext, legacy.type, legacy.role,
379                     &aidl.hal.ext, &aidl.sys.ext));
380     return aidl;
381 }
382 
aidl2legacy_AudioPatchFw_audio_patch(const media::AudioPatchFw & aidl)383 ConversionResult<struct audio_patch> aidl2legacy_AudioPatchFw_audio_patch(
384         const media::AudioPatchFw& aidl) {
385     struct audio_patch legacy;
386     legacy.id = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_patch_handle_t(aidl.id));
387     legacy.num_sinks = VALUE_OR_RETURN(convertIntegral<unsigned int>(aidl.sinks.size()));
388     if (legacy.num_sinks > AUDIO_PATCH_PORTS_MAX) {
389         return unexpected(BAD_VALUE);
390     }
391     for (size_t i = 0; i < legacy.num_sinks; ++i) {
392         legacy.sinks[i] =
393                 VALUE_OR_RETURN(aidl2legacy_AudioPortConfigFw_audio_port_config(aidl.sinks[i]));
394     }
395     legacy.num_sources = VALUE_OR_RETURN(convertIntegral<unsigned int>(aidl.sources.size()));
396     if (legacy.num_sources > AUDIO_PATCH_PORTS_MAX) {
397         return unexpected(BAD_VALUE);
398     }
399     for (size_t i = 0; i < legacy.num_sources; ++i) {
400         legacy.sources[i] =
401                 VALUE_OR_RETURN(aidl2legacy_AudioPortConfigFw_audio_port_config(aidl.sources[i]));
402     }
403     return legacy;
404 }
405 
legacy2aidl_audio_patch_AudioPatchFw(const struct audio_patch & legacy)406 ConversionResult<media::AudioPatchFw> legacy2aidl_audio_patch_AudioPatchFw(
407         const struct audio_patch& legacy) {
408     media::AudioPatchFw aidl;
409     aidl.id = VALUE_OR_RETURN(legacy2aidl_audio_patch_handle_t_int32_t(legacy.id));
410 
411     if (legacy.num_sinks > AUDIO_PATCH_PORTS_MAX) {
412         return unexpected(BAD_VALUE);
413     }
414     for (unsigned int i = 0; i < legacy.num_sinks; ++i) {
415         aidl.sinks.push_back(
416                 VALUE_OR_RETURN(legacy2aidl_audio_port_config_AudioPortConfigFw(legacy.sinks[i])));
417     }
418     if (legacy.num_sources > AUDIO_PATCH_PORTS_MAX) {
419         return unexpected(BAD_VALUE);
420     }
421     for (unsigned int i = 0; i < legacy.num_sources; ++i) {
422         aidl.sources.push_back(
423                 VALUE_OR_RETURN(legacy2aidl_audio_port_config_AudioPortConfigFw(legacy.sources[i])));
424     }
425     return aidl;
426 }
427 
aidl2legacy_AudioIoDescriptor_AudioIoDescriptor(const media::AudioIoDescriptor & aidl)428 ConversionResult<sp<AudioIoDescriptor>> aidl2legacy_AudioIoDescriptor_AudioIoDescriptor(
429         const media::AudioIoDescriptor& aidl) {
430     const audio_io_handle_t io_handle = VALUE_OR_RETURN(
431             aidl2legacy_int32_t_audio_io_handle_t(aidl.ioHandle));
432     const struct audio_patch patch = VALUE_OR_RETURN(
433             aidl2legacy_AudioPatchFw_audio_patch(aidl.patch));
434     const bool isInput = aidl.isInput;
435     const uint32_t sampling_rate = VALUE_OR_RETURN(convertIntegral<uint32_t>(aidl.samplingRate));
436     const audio_format_t format = VALUE_OR_RETURN(
437             aidl2legacy_AudioFormatDescription_audio_format_t(aidl.format));
438     const audio_channel_mask_t channel_mask = VALUE_OR_RETURN(
439             aidl2legacy_AudioChannelLayout_audio_channel_mask_t(aidl.channelMask, isInput));
440     const size_t frame_count = VALUE_OR_RETURN(convertIntegral<size_t>(aidl.frameCount));
441     const size_t frame_count_hal = VALUE_OR_RETURN(convertIntegral<size_t>(aidl.frameCountHAL));
442     const uint32_t latency = VALUE_OR_RETURN(convertIntegral<uint32_t>(aidl.latency));
443     const audio_port_handle_t port_id = VALUE_OR_RETURN(
444             aidl2legacy_int32_t_audio_port_handle_t(aidl.portId));
445     return sp<AudioIoDescriptor>::make(io_handle, patch, isInput, sampling_rate, format,
446             channel_mask, frame_count, frame_count_hal, latency, port_id);
447 }
448 
legacy2aidl_AudioIoDescriptor_AudioIoDescriptor(const sp<AudioIoDescriptor> & legacy)449 ConversionResult<media::AudioIoDescriptor> legacy2aidl_AudioIoDescriptor_AudioIoDescriptor(
450         const sp<AudioIoDescriptor>& legacy) {
451     media::AudioIoDescriptor aidl;
452     aidl.ioHandle = VALUE_OR_RETURN(legacy2aidl_audio_io_handle_t_int32_t(legacy->getIoHandle()));
453     aidl.patch = VALUE_OR_RETURN(legacy2aidl_audio_patch_AudioPatchFw(legacy->getPatch()));
454     aidl.isInput = legacy->getIsInput();
455     aidl.samplingRate = VALUE_OR_RETURN(convertIntegral<int32_t>(legacy->getSamplingRate()));
456     aidl.format = VALUE_OR_RETURN(
457             legacy2aidl_audio_format_t_AudioFormatDescription(legacy->getFormat()));
458     aidl.channelMask = VALUE_OR_RETURN(legacy2aidl_audio_channel_mask_t_AudioChannelLayout(
459                     legacy->getChannelMask(), legacy->getIsInput()));
460     aidl.frameCount = VALUE_OR_RETURN(convertIntegral<int64_t>(legacy->getFrameCount()));
461     aidl.frameCountHAL = VALUE_OR_RETURN(convertIntegral<int64_t>(legacy->getFrameCountHAL()));
462     aidl.latency = VALUE_OR_RETURN(convertIntegral<int32_t>(legacy->getLatency()));
463     aidl.portId = VALUE_OR_RETURN(legacy2aidl_audio_port_handle_t_int32_t(legacy->getPortId()));
464     return aidl;
465 }
466 
aidl2legacy_AudioClient_AudioClient(const media::AudioClient & aidl)467 ConversionResult<AudioClient> aidl2legacy_AudioClient_AudioClient(
468         const media::AudioClient& aidl) {
469     AudioClient legacy;
470     legacy.clientTid = VALUE_OR_RETURN(aidl2legacy_int32_t_pid_t(aidl.clientTid));
471     legacy.attributionSource = aidl.attributionSource;
472     return legacy;
473 }
474 
legacy2aidl_AudioClient_AudioClient(const AudioClient & legacy)475 ConversionResult<media::AudioClient> legacy2aidl_AudioClient_AudioClient(
476         const AudioClient& legacy) {
477     media::AudioClient aidl;
478     aidl.clientTid = VALUE_OR_RETURN(legacy2aidl_pid_t_int32_t(legacy.clientTid));
479     aidl.attributionSource = legacy.attributionSource;
480     return aidl;
481 }
482 
483 ConversionResult<sp<IMemory>>
aidl2legacy_SharedFileRegion_IMemory(const media::SharedFileRegion & aidl)484 aidl2legacy_SharedFileRegion_IMemory(const media::SharedFileRegion& aidl) {
485     sp<IMemory> legacy;
486     if (!convertSharedFileRegionToIMemory(aidl, &legacy)) {
487         return unexpected(BAD_VALUE);
488     }
489     return legacy;
490 }
491 
492 ConversionResult<media::SharedFileRegion>
legacy2aidl_IMemory_SharedFileRegion(const sp<IMemory> & legacy)493 legacy2aidl_IMemory_SharedFileRegion(const sp<IMemory>& legacy) {
494     media::SharedFileRegion aidl;
495     if (!convertIMemoryToSharedFileRegion(legacy, &aidl)) {
496         return unexpected(BAD_VALUE);
497     }
498     return aidl;
499 }
500 
aidl2legacy_NullableSharedFileRegion_IMemory(const std::optional<media::SharedFileRegion> & aidl)501 ConversionResult<sp<IMemory>> aidl2legacy_NullableSharedFileRegion_IMemory(
502         const std::optional<media::SharedFileRegion>& aidl) {
503     sp<IMemory> legacy;
504     if (!convertNullableSharedFileRegionToIMemory(aidl, &legacy)) {
505         return unexpected(BAD_VALUE);
506     }
507     return legacy;
508 }
509 
510 ConversionResult<std::optional<media::SharedFileRegion>>
legacy2aidl_NullableIMemory_SharedFileRegion(const sp<IMemory> & legacy)511 legacy2aidl_NullableIMemory_SharedFileRegion(const sp<IMemory>& legacy) {
512     std::optional<media::SharedFileRegion> aidl;
513     if (!convertNullableIMemoryToSharedFileRegion(legacy, &aidl)) {
514         return unexpected(BAD_VALUE);
515     }
516     return aidl;
517 }
518 
519 ConversionResult<AudioTimestamp>
aidl2legacy_AudioTimestampInternal_AudioTimestamp(const media::AudioTimestampInternal & aidl)520 aidl2legacy_AudioTimestampInternal_AudioTimestamp(const media::AudioTimestampInternal& aidl) {
521     AudioTimestamp legacy;
522     legacy.mPosition = VALUE_OR_RETURN(convertIntegral<uint32_t>(aidl.position));
523     legacy.mTime.tv_sec = VALUE_OR_RETURN(convertIntegral<uint32_t>(aidl.sec));
524     legacy.mTime.tv_nsec = VALUE_OR_RETURN(convertIntegral<uint32_t>(aidl.nsec));
525     return legacy;
526 }
527 
528 ConversionResult<media::AudioTimestampInternal>
legacy2aidl_AudioTimestamp_AudioTimestampInternal(const AudioTimestamp & legacy)529 legacy2aidl_AudioTimestamp_AudioTimestampInternal(const AudioTimestamp& legacy) {
530     media::AudioTimestampInternal aidl;
531     aidl.position = VALUE_OR_RETURN(convertIntegral<int64_t>(legacy.mPosition));
532     aidl.sec = VALUE_OR_RETURN(convertIntegral<int64_t>(legacy.mTime.tv_sec));
533     aidl.nsec = VALUE_OR_RETURN(convertIntegral<int32_t>(legacy.mTime.tv_nsec));
534     return aidl;
535 }
536 
537 ConversionResult<effect_descriptor_t>
aidl2legacy_EffectDescriptor_effect_descriptor_t(const media::EffectDescriptor & aidl)538 aidl2legacy_EffectDescriptor_effect_descriptor_t(const media::EffectDescriptor& aidl) {
539     effect_descriptor_t legacy;
540     legacy.type = VALUE_OR_RETURN(aidl2legacy_AudioUuid_audio_uuid_t(aidl.type));
541     legacy.uuid = VALUE_OR_RETURN(aidl2legacy_AudioUuid_audio_uuid_t(aidl.uuid));
542     legacy.apiVersion = VALUE_OR_RETURN(convertReinterpret<uint32_t>(aidl.apiVersion));
543     legacy.flags = VALUE_OR_RETURN(convertReinterpret<uint32_t>(aidl.flags));
544     legacy.cpuLoad = VALUE_OR_RETURN(convertIntegral<uint16_t>(aidl.cpuLoad));
545     legacy.memoryUsage = VALUE_OR_RETURN(convertIntegral<uint16_t>(aidl.memoryUsage));
546     RETURN_IF_ERROR(aidl2legacy_string(aidl.name, legacy.name, sizeof(legacy.name)));
547     RETURN_IF_ERROR(
548             aidl2legacy_string(aidl.implementor, legacy.implementor, sizeof(legacy.implementor)));
549     return legacy;
550 }
551 
552 ConversionResult<media::EffectDescriptor>
legacy2aidl_effect_descriptor_t_EffectDescriptor(const effect_descriptor_t & legacy)553 legacy2aidl_effect_descriptor_t_EffectDescriptor(const effect_descriptor_t& legacy) {
554     media::EffectDescriptor aidl;
555     aidl.type = VALUE_OR_RETURN(legacy2aidl_audio_uuid_t_AudioUuid(legacy.type));
556     aidl.uuid = VALUE_OR_RETURN(legacy2aidl_audio_uuid_t_AudioUuid(legacy.uuid));
557     aidl.apiVersion = VALUE_OR_RETURN(convertReinterpret<int32_t>(legacy.apiVersion));
558     aidl.flags = VALUE_OR_RETURN(convertReinterpret<int32_t>(legacy.flags));
559     aidl.cpuLoad = VALUE_OR_RETURN(convertIntegral<int32_t>(legacy.cpuLoad));
560     aidl.memoryUsage = VALUE_OR_RETURN(convertIntegral<int32_t>(legacy.memoryUsage));
561     aidl.name = VALUE_OR_RETURN(legacy2aidl_string(legacy.name, sizeof(legacy.name)));
562     aidl.implementor = VALUE_OR_RETURN(
563             legacy2aidl_string(legacy.implementor, sizeof(legacy.implementor)));
564     return aidl;
565 }
566 
567 ConversionResult<audio_port_mix_ext>
aidl2legacy_AudioPortMixExt_audio_port_mix_ext(const AudioPortMixExt & aidl,const media::AudioPortMixExtSys & aidlSys)568 aidl2legacy_AudioPortMixExt_audio_port_mix_ext(
569         const AudioPortMixExt& aidl, const media::AudioPortMixExtSys& aidlSys) {
570     audio_port_mix_ext legacy = VALUE_OR_RETURN(
571             aidl2legacy_AudioPortMixExt_audio_port_mix_ext(aidl));
572     legacy.hw_module = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_module_handle_t(aidlSys.hwModule));
573     return legacy;
574 }
575 
576 status_t
legacy2aidl_audio_port_mix_ext_AudioPortMixExt(const audio_port_mix_ext & legacy,AudioPortMixExt * aidl,media::AudioPortMixExtSys * aidlMixExt)577 legacy2aidl_audio_port_mix_ext_AudioPortMixExt(const audio_port_mix_ext& legacy,
578         AudioPortMixExt* aidl, media::AudioPortMixExtSys* aidlMixExt) {
579     *aidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_mix_ext_AudioPortMixExt(legacy));
580     aidlMixExt->hwModule = VALUE_OR_RETURN_STATUS(
581             legacy2aidl_audio_module_handle_t_int32_t(legacy.hw_module));
582     return OK;
583 }
584 
585 ConversionResult<audio_port_session_ext>
aidl2legacy_int32_t_audio_port_session_ext(int32_t aidl)586 aidl2legacy_int32_t_audio_port_session_ext(int32_t aidl) {
587     audio_port_session_ext legacy;
588     legacy.session = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_session_t(aidl));
589     return legacy;
590 }
591 
592 ConversionResult<int32_t>
legacy2aidl_audio_port_session_ext_int32_t(const audio_port_session_ext & legacy)593 legacy2aidl_audio_port_session_ext_int32_t(const audio_port_session_ext& legacy) {
594     return legacy2aidl_audio_session_t_int32_t(legacy.session);
595 }
596 
597 ConversionResult<audio_port_device_ext>
aidl2legacy_AudioPortDeviceExt_audio_port_device_ext(const AudioPortDeviceExt & aidl,const media::AudioPortDeviceExtSys & aidlSys)598 aidl2legacy_AudioPortDeviceExt_audio_port_device_ext(
599         const AudioPortDeviceExt& aidl, const media::AudioPortDeviceExtSys& aidlSys) {
600     audio_port_device_ext legacy = VALUE_OR_RETURN(
601             aidl2legacy_AudioPortDeviceExt_audio_port_device_ext(aidl));
602     legacy.hw_module = VALUE_OR_RETURN(
603             aidl2legacy_int32_t_audio_module_handle_t(aidlSys.hwModule));
604     return legacy;
605 }
606 
legacy2aidl_audio_port_device_ext_AudioPortDeviceExt(const audio_port_device_ext & legacy,AudioPortDeviceExt * aidl,media::AudioPortDeviceExtSys * aidlDeviceExt)607 status_t legacy2aidl_audio_port_device_ext_AudioPortDeviceExt(
608         const audio_port_device_ext& legacy,
609         AudioPortDeviceExt* aidl, media::AudioPortDeviceExtSys* aidlDeviceExt) {
610     *aidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_device_ext_AudioPortDeviceExt(legacy));
611     aidlDeviceExt->hwModule = VALUE_OR_RETURN_STATUS(
612             legacy2aidl_audio_module_handle_t_int32_t(legacy.hw_module));
613     return OK;
614 }
615 
616 // This type is unnamed in the original definition, thus we name it here.
617 using audio_port_v7_ext = decltype(audio_port_v7::ext);
618 
aidl2legacy_AudioPortExt_audio_port_v7_ext(const AudioPortExt & aidl,media::AudioPortType type,const media::AudioPortExtSys & aidlSys)619 ConversionResult<audio_port_v7_ext> aidl2legacy_AudioPortExt_audio_port_v7_ext(
620         const AudioPortExt& aidl, media::AudioPortType type,
621         const media::AudioPortExtSys& aidlSys) {
622     audio_port_v7_ext legacy;
623     switch (type) {
624         case media::AudioPortType::NONE:
625             // Just verify that the union is empty.
626             VALUE_OR_RETURN(UNION_GET(aidl, unspecified));
627             return legacy;
628         case media::AudioPortType::DEVICE:
629             legacy.device = VALUE_OR_RETURN(
630                     aidl2legacy_AudioPortDeviceExt_audio_port_device_ext(
631                             VALUE_OR_RETURN(UNION_GET(aidl, device)),
632                             VALUE_OR_RETURN(UNION_GET(aidlSys, device))));
633             return legacy;
634         case media::AudioPortType::MIX:
635             legacy.mix = VALUE_OR_RETURN(
636                     aidl2legacy_AudioPortMixExt_audio_port_mix_ext(
637                             VALUE_OR_RETURN(UNION_GET(aidl, mix)),
638                             VALUE_OR_RETURN(UNION_GET(aidlSys, mix))));
639             return legacy;
640         case media::AudioPortType::SESSION:
641             legacy.session = VALUE_OR_RETURN(
642                     aidl2legacy_int32_t_audio_port_session_ext(
643                             VALUE_OR_RETURN(UNION_GET(aidlSys, session))));
644             return legacy;
645 
646     }
647     LOG_ALWAYS_FATAL("Shouldn't get here"); // with -Werror,-Wswitch may compile-time fail
648 }
649 
legacy2aidl_AudioPortExt(const audio_port_v7_ext & legacy,audio_port_type_t type,AudioPortExt * aidl,media::AudioPortExtSys * aidlSys)650 status_t legacy2aidl_AudioPortExt(
651         const audio_port_v7_ext& legacy, audio_port_type_t type,
652         AudioPortExt* aidl, media::AudioPortExtSys* aidlSys) {
653     switch (type) {
654         case AUDIO_PORT_TYPE_NONE:
655             UNION_SET(*aidl, unspecified, false);
656             UNION_SET(*aidlSys, unspecified, false);
657             return OK;
658         case AUDIO_PORT_TYPE_DEVICE: {
659             AudioPortDeviceExt device;
660             media::AudioPortDeviceExtSys deviceSys;
661             RETURN_STATUS_IF_ERROR(
662                     legacy2aidl_audio_port_device_ext_AudioPortDeviceExt(
663                             legacy.device, &device, &deviceSys));
664             UNION_SET(*aidl, device, device);
665             UNION_SET(*aidlSys, device, deviceSys);
666             return OK;
667         }
668         case AUDIO_PORT_TYPE_MIX: {
669             AudioPortMixExt mix;
670             media::AudioPortMixExtSys mixSys;
671             RETURN_STATUS_IF_ERROR(
672                     legacy2aidl_audio_port_mix_ext_AudioPortMixExt(
673                             legacy.mix, &mix, &mixSys));
674             UNION_SET(*aidl, mix, mix);
675             UNION_SET(*aidlSys, mix, mixSys);
676             return OK;
677         }
678         case AUDIO_PORT_TYPE_SESSION:
679             UNION_SET(*aidl, unspecified, false);
680             UNION_SET(*aidlSys, session, VALUE_OR_RETURN_STATUS(
681                             legacy2aidl_audio_port_session_ext_int32_t(legacy.session)));
682             return OK;
683     }
684     LOG_ALWAYS_FATAL("Shouldn't get here"); // with -Werror,-Wswitch may compile-time fail
685 }
686 
687 ConversionResult<audio_port_v7>
aidl2legacy_AudioPortFw_audio_port_v7(const media::AudioPortFw & aidl)688 aidl2legacy_AudioPortFw_audio_port_v7(const media::AudioPortFw& aidl) {
689     const bool isInput = VALUE_OR_RETURN(
690             portDirection(aidl.sys.role, aidl.sys.type)) == AudioPortDirection::INPUT;
691     audio_port_v7 legacy = VALUE_OR_RETURN(aidl2legacy_AudioPort_audio_port_v7(aidl.hal, isInput));
692     legacy.role = VALUE_OR_RETURN(aidl2legacy_AudioPortRole_audio_port_role_t(aidl.sys.role));
693     legacy.type = VALUE_OR_RETURN(aidl2legacy_AudioPortType_audio_port_type_t(aidl.sys.type));
694 
695     legacy.active_config = VALUE_OR_RETURN(
696             aidl2legacy_AudioPortConfigFw_audio_port_config(aidl.sys.activeConfig));
697     legacy.ext = VALUE_OR_RETURN(
698             aidl2legacy_AudioPortExt_audio_port_v7_ext(aidl.hal.ext, aidl.sys.type, aidl.sys.ext));
699     return legacy;
700 }
701 
702 ConversionResult<media::AudioPortFw>
legacy2aidl_audio_port_v7_AudioPortFw(const audio_port_v7 & legacy)703 legacy2aidl_audio_port_v7_AudioPortFw(const audio_port_v7& legacy) {
704     const bool isInput = VALUE_OR_RETURN(
705             portDirection(legacy.role, legacy.type)) == AudioPortDirection::INPUT;
706     media::AudioPortFw aidl;
707     aidl.hal = VALUE_OR_RETURN(legacy2aidl_audio_port_v7_AudioPort(legacy, isInput));
708     aidl.sys.role = VALUE_OR_RETURN(legacy2aidl_audio_port_role_t_AudioPortRole(legacy.role));
709     aidl.sys.type = VALUE_OR_RETURN(legacy2aidl_audio_port_type_t_AudioPortType(legacy.type));
710     // These get filled by the call to 'legacy2aidl_AudioPortExt' below.
711     aidl.sys.profiles.resize(legacy.num_audio_profiles);
712     aidl.sys.gains.resize(legacy.num_gains);
713     aidl.sys.activeConfig = VALUE_OR_RETURN(
714             legacy2aidl_audio_port_config_AudioPortConfigFw(legacy.active_config, legacy.id));
715     aidl.sys.activeConfig.hal.portId = aidl.hal.id;
716     RETURN_IF_ERROR(
717             legacy2aidl_AudioPortExt(legacy.ext, legacy.type, &aidl.hal.ext, &aidl.sys.ext));
718     return aidl;
719 }
720 
721 ConversionResult<audio_unique_id_use_t>
aidl2legacy_AudioUniqueIdUse_audio_unique_id_use_t(media::AudioUniqueIdUse aidl)722 aidl2legacy_AudioUniqueIdUse_audio_unique_id_use_t(media::AudioUniqueIdUse aidl) {
723     switch (aidl) {
724         case media::AudioUniqueIdUse::UNSPECIFIED:
725             return AUDIO_UNIQUE_ID_USE_UNSPECIFIED;
726         case media::AudioUniqueIdUse::SESSION:
727             return AUDIO_UNIQUE_ID_USE_SESSION;
728         case media::AudioUniqueIdUse::MODULE:
729             return AUDIO_UNIQUE_ID_USE_MODULE;
730         case media::AudioUniqueIdUse::EFFECT:
731             return AUDIO_UNIQUE_ID_USE_EFFECT;
732         case media::AudioUniqueIdUse::PATCH:
733             return AUDIO_UNIQUE_ID_USE_PATCH;
734         case media::AudioUniqueIdUse::OUTPUT:
735             return AUDIO_UNIQUE_ID_USE_OUTPUT;
736         case media::AudioUniqueIdUse::INPUT:
737             return AUDIO_UNIQUE_ID_USE_INPUT;
738         case media::AudioUniqueIdUse::CLIENT:
739             return AUDIO_UNIQUE_ID_USE_CLIENT;
740     }
741     return unexpected(BAD_VALUE);
742 }
743 
744 ConversionResult<media::AudioUniqueIdUse>
legacy2aidl_audio_unique_id_use_t_AudioUniqueIdUse(audio_unique_id_use_t legacy)745 legacy2aidl_audio_unique_id_use_t_AudioUniqueIdUse(audio_unique_id_use_t legacy) {
746     switch (legacy) {
747         case AUDIO_UNIQUE_ID_USE_UNSPECIFIED:
748             return media::AudioUniqueIdUse::UNSPECIFIED;
749         case AUDIO_UNIQUE_ID_USE_SESSION:
750             return media::AudioUniqueIdUse::SESSION;
751         case AUDIO_UNIQUE_ID_USE_MODULE:
752             return media::AudioUniqueIdUse::MODULE;
753         case AUDIO_UNIQUE_ID_USE_EFFECT:
754             return media::AudioUniqueIdUse::EFFECT;
755         case AUDIO_UNIQUE_ID_USE_PATCH:
756             return media::AudioUniqueIdUse::PATCH;
757         case AUDIO_UNIQUE_ID_USE_OUTPUT:
758             return media::AudioUniqueIdUse::OUTPUT;
759         case AUDIO_UNIQUE_ID_USE_INPUT:
760             return media::AudioUniqueIdUse::INPUT;
761         case AUDIO_UNIQUE_ID_USE_CLIENT:
762             return media::AudioUniqueIdUse::CLIENT;
763         case AUDIO_UNIQUE_ID_USE_MAX:
764             break;
765     }
766     return unexpected(BAD_VALUE);
767 }
768 
769 ConversionResult<volume_group_t>
aidl2legacy_int32_t_volume_group_t(int32_t aidl)770 aidl2legacy_int32_t_volume_group_t(int32_t aidl) {
771     return convertReinterpret<volume_group_t>(aidl);
772 }
773 
774 ConversionResult<int32_t>
legacy2aidl_volume_group_t_int32_t(volume_group_t legacy)775 legacy2aidl_volume_group_t_int32_t(volume_group_t legacy) {
776     return convertReinterpret<int32_t>(legacy);
777 }
778 
779 ConversionResult<product_strategy_t>
aidl2legacy_int32_t_product_strategy_t(int32_t aidl)780 aidl2legacy_int32_t_product_strategy_t(int32_t aidl) {
781     return convertReinterpret<product_strategy_t>(aidl);
782 }
783 
784 ConversionResult<int32_t>
legacy2aidl_product_strategy_t_int32_t(product_strategy_t legacy)785 legacy2aidl_product_strategy_t_int32_t(product_strategy_t legacy) {
786     return convertReinterpret<int32_t>(legacy);
787 }
788 
789 ConversionResult<TrackSecondaryOutputInfoPair>
aidl2legacy_TrackSecondaryOutputInfo_TrackSecondaryOutputInfoPair(const media::TrackSecondaryOutputInfo & aidl)790 aidl2legacy_TrackSecondaryOutputInfo_TrackSecondaryOutputInfoPair(
791         const media::TrackSecondaryOutputInfo& aidl) {
792     TrackSecondaryOutputInfoPair trackSecondaryOutputInfoPair;
793     trackSecondaryOutputInfoPair.first =
794             VALUE_OR_RETURN(aidl2legacy_int32_t_audio_port_handle_t(aidl.portId));
795     trackSecondaryOutputInfoPair.second =
796             VALUE_OR_RETURN(convertContainer<std::vector<audio_port_handle_t>>(
797                     aidl.secondaryOutputIds, aidl2legacy_int32_t_audio_io_handle_t));
798     return trackSecondaryOutputInfoPair;
799 }
800 
801 ConversionResult<media::TrackSecondaryOutputInfo>
legacy2aidl_TrackSecondaryOutputInfoPair_TrackSecondaryOutputInfo(const TrackSecondaryOutputInfoPair & legacy)802 legacy2aidl_TrackSecondaryOutputInfoPair_TrackSecondaryOutputInfo(
803         const TrackSecondaryOutputInfoPair& legacy) {
804     media::TrackSecondaryOutputInfo trackSecondaryOutputInfo;
805     trackSecondaryOutputInfo.portId =
806             VALUE_OR_RETURN(legacy2aidl_audio_port_handle_t_int32_t(legacy.first));
807     trackSecondaryOutputInfo.secondaryOutputIds =
808             VALUE_OR_RETURN(convertContainer<std::vector<int32_t>>(
809                     legacy.second, legacy2aidl_audio_io_handle_t_int32_t));
810     return trackSecondaryOutputInfo;
811 }
812 
813 ConversionResult<audio_direct_mode_t>
aidl2legacy_AudioDirectMode_audio_direct_mode_t(media::AudioDirectMode aidl)814 aidl2legacy_AudioDirectMode_audio_direct_mode_t(media::AudioDirectMode aidl) {
815     switch (aidl) {
816         case media::AudioDirectMode::NONE:
817             return AUDIO_DIRECT_NOT_SUPPORTED;
818         case media::AudioDirectMode::OFFLOAD:
819             return AUDIO_DIRECT_OFFLOAD_SUPPORTED;
820         case media::AudioDirectMode::OFFLOAD_GAPLESS:
821             return AUDIO_DIRECT_OFFLOAD_GAPLESS_SUPPORTED;
822         case media::AudioDirectMode::BITSTREAM:
823             return AUDIO_DIRECT_BITSTREAM_SUPPORTED;
824     }
825     return unexpected(BAD_VALUE);
826 }
827 ConversionResult<media::AudioDirectMode>
legacy2aidl_audio_direct_mode_t_AudioDirectMode(audio_direct_mode_t legacy)828 legacy2aidl_audio_direct_mode_t_AudioDirectMode(audio_direct_mode_t legacy) {
829     switch (legacy) {
830         case AUDIO_DIRECT_NOT_SUPPORTED:
831             return media::AudioDirectMode::NONE;
832         case AUDIO_DIRECT_OFFLOAD_SUPPORTED:
833             return media::AudioDirectMode::OFFLOAD;
834         case AUDIO_DIRECT_OFFLOAD_GAPLESS_SUPPORTED:
835             return media::AudioDirectMode::OFFLOAD_GAPLESS;
836         case AUDIO_DIRECT_BITSTREAM_SUPPORTED:
837             return media::AudioDirectMode::BITSTREAM;
838     }
839     return unexpected(BAD_VALUE);
840 }
841 
aidl2legacy_int32_t_audio_direct_mode_t_mask(int32_t aidl)842 ConversionResult<audio_direct_mode_t> aidl2legacy_int32_t_audio_direct_mode_t_mask(int32_t aidl) {
843     using LegacyMask = std::underlying_type_t<audio_direct_mode_t>;
844 
845     LegacyMask converted = VALUE_OR_RETURN(
846             (convertBitmask<LegacyMask, int32_t, audio_direct_mode_t, media::AudioDirectMode>(
847                     aidl, aidl2legacy_AudioDirectMode_audio_direct_mode_t,
848                     indexToEnum_index<media::AudioDirectMode>,
849                     enumToMask_bitmask<LegacyMask, audio_direct_mode_t>)));
850     return static_cast<audio_direct_mode_t>(converted);
851 }
legacy2aidl_audio_direct_mode_t_int32_t_mask(audio_direct_mode_t legacy)852 ConversionResult<int32_t> legacy2aidl_audio_direct_mode_t_int32_t_mask(audio_direct_mode_t legacy) {
853     using LegacyMask = std::underlying_type_t<audio_direct_mode_t>;
854 
855     LegacyMask legacyMask = static_cast<LegacyMask>(legacy);
856     return convertBitmask<int32_t, LegacyMask, media::AudioDirectMode, audio_direct_mode_t>(
857             legacyMask, legacy2aidl_audio_direct_mode_t_AudioDirectMode,
858             indexToEnum_bitmask<audio_direct_mode_t>,
859             enumToMask_index<int32_t, media::AudioDirectMode>);
860 }
861 
862 ConversionResult<audio_microphone_characteristic_t>
aidl2legacy_MicrophoneInfoFw_audio_microphone_characteristic_t(const media::MicrophoneInfoFw & aidl)863 aidl2legacy_MicrophoneInfoFw_audio_microphone_characteristic_t(
864         const media::MicrophoneInfoFw& aidl) {
865     audio_microphone_characteristic_t legacy =
866             VALUE_OR_RETURN(aidl2legacy_MicrophoneInfos_audio_microphone_characteristic_t(
867                             aidl.info, aidl.dynamic));
868     legacy.id = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_port_handle_t(aidl.portId));
869     return legacy;
870 }
871 ConversionResult<media::MicrophoneInfoFw>
legacy2aidl_audio_microphone_characteristic_t_MicrophoneInfoFw(const audio_microphone_characteristic_t & legacy)872 legacy2aidl_audio_microphone_characteristic_t_MicrophoneInfoFw(
873         const audio_microphone_characteristic_t& legacy) {
874     media::MicrophoneInfoFw aidl;
875     RETURN_IF_ERROR(legacy2aidl_audio_microphone_characteristic_t_MicrophoneInfos(
876                     legacy, &aidl.info, &aidl.dynamic));
877     aidl.portId = VALUE_OR_RETURN(legacy2aidl_audio_port_handle_t_int32_t(legacy.id));
878     return aidl;
879 }
880 
881 }  // namespace android
882