• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 <stdio.h>
18 
19 #include <algorithm>
20 #include <map>
21 #include <sstream>
22 #include <utility>
23 #include <vector>
24 
25 #define LOG_TAG "AidlConversionCppNdk"
26 //#define LOG_NDEBUG 0
27 #include <utils/Log.h>
28 
29 #include "media/AidlConversionCppNdk.h"
30 
31 #include <media/stagefright/foundation/MediaDefs.h>
32 
33 ////////////////////////////////////////////////////////////////////////////////////////////////////
34 // AIDL CPP/NDK backend to legacy audio data structure conversion utilities.
35 
36 #if defined(BACKEND_NDK)
37 /* AIDL String generated in NDK is different than CPP */
38 #define GET_DEVICE_DESC_CONNECTION(x)  AudioDeviceDescription::CONNECTION_##x
39 namespace aidl {
40 #else
41 #define GET_DEVICE_DESC_CONNECTION(x)  AudioDeviceDescription::CONNECTION_##x()
42 #endif
43 
44 namespace android {
45 
46 using ::android::BAD_VALUE;
47 using ::android::OK;
48 using ::android::String16;
49 using ::android::String8;
50 using ::android::status_t;
51 using ::android::base::unexpected;
52 
53 using media::audio::common::AudioAttributes;
54 using media::audio::common::AudioChannelLayout;
55 using media::audio::common::AudioConfig;
56 using media::audio::common::AudioConfigBase;
57 using media::audio::common::AudioContentType;
58 using media::audio::common::AudioDevice;
59 using media::audio::common::AudioDeviceAddress;
60 using media::audio::common::AudioDeviceDescription;
61 using media::audio::common::AudioDeviceType;
62 using media::audio::common::AudioDualMonoMode;
63 using media::audio::common::AudioEncapsulationMetadataType;
64 using media::audio::common::AudioEncapsulationMode;
65 using media::audio::common::AudioEncapsulationType;
66 using media::audio::common::AudioFlag;
67 using media::audio::common::AudioFormatDescription;
68 using media::audio::common::AudioFormatType;
69 using media::audio::common::AudioGain;
70 using media::audio::common::AudioGainConfig;
71 using media::audio::common::AudioGainMode;
72 using media::audio::common::AudioInputFlags;
73 using media::audio::common::AudioIoFlags;
74 using media::audio::common::AudioLatencyMode;
75 using media::audio::common::AudioMode;
76 using media::audio::common::AudioOffloadInfo;
77 using media::audio::common::AudioOutputFlags;
78 using media::audio::common::AudioPlaybackRate;
79 using media::audio::common::AudioPort;
80 using media::audio::common::AudioPortConfig;
81 using media::audio::common::AudioPortDeviceExt;
82 using media::audio::common::AudioPortExt;
83 using media::audio::common::AudioPortMixExt;
84 using media::audio::common::AudioPortMixExtUseCase;
85 using media::audio::common::AudioProfile;
86 using media::audio::common::AudioSource;
87 using media::audio::common::AudioStandard;
88 using media::audio::common::AudioStreamType;
89 using media::audio::common::AudioUsage;
90 using media::audio::common::AudioUuid;
91 using media::audio::common::ExtraAudioDescriptor;
92 using media::audio::common::Int;
93 using media::audio::common::MicrophoneDynamicInfo;
94 using media::audio::common::MicrophoneInfo;
95 using media::audio::common::PcmType;
96 
97 ////////////////////////////////////////////////////////////////////////////////////////////////////
98 // Converters
99 
100 namespace {
101 
splitString(const std::string & s,char separator)102 std::vector<std::string> splitString(const std::string& s, char separator) {
103     std::istringstream iss(s);
104     std::string t;
105     std::vector<std::string> result;
106     while (std::getline(iss, t, separator)) {
107         result.push_back(std::move(t));
108     }
109     return result;
110 }
111 
112 }  // namespace
113 
aidl2legacy_string(std::string_view aidl,char * dest,size_t maxSize)114 ::android::status_t aidl2legacy_string(std::string_view aidl, char* dest, size_t maxSize) {
115     if (aidl.size() > maxSize - 1) {
116         return BAD_VALUE;
117     }
118     aidl.copy(dest, aidl.size());
119     dest[aidl.size()] = '\0';
120     return OK;
121 }
122 
legacy2aidl_string(const char * legacy,size_t maxSize)123 ConversionResult<std::string> legacy2aidl_string(const char* legacy, size_t maxSize) {
124     if (legacy == nullptr) {
125         return unexpected(BAD_VALUE);
126     }
127     if (strnlen(legacy, maxSize) == maxSize) {
128         // No null-terminator.
129         return unexpected(BAD_VALUE);
130     }
131     return std::string(legacy);
132 }
133 
aidl2legacy_int32_t_audio_module_handle_t(int32_t aidl)134 ConversionResult<audio_module_handle_t> aidl2legacy_int32_t_audio_module_handle_t(int32_t aidl) {
135     return convertReinterpret<audio_module_handle_t>(aidl);
136 }
137 
legacy2aidl_audio_module_handle_t_int32_t(audio_module_handle_t legacy)138 ConversionResult<int32_t> legacy2aidl_audio_module_handle_t_int32_t(audio_module_handle_t legacy) {
139     return convertReinterpret<int32_t>(legacy);
140 }
141 
aidl2legacy_int32_t_audio_io_handle_t(int32_t aidl)142 ConversionResult<audio_io_handle_t> aidl2legacy_int32_t_audio_io_handle_t(int32_t aidl) {
143     return convertReinterpret<audio_io_handle_t>(aidl);
144 }
145 
legacy2aidl_audio_io_handle_t_int32_t(audio_io_handle_t legacy)146 ConversionResult<int32_t> legacy2aidl_audio_io_handle_t_int32_t(audio_io_handle_t legacy) {
147     return convertReinterpret<int32_t>(legacy);
148 }
149 
aidl2legacy_int32_t_audio_port_handle_t(int32_t aidl)150 ConversionResult<audio_port_handle_t> aidl2legacy_int32_t_audio_port_handle_t(int32_t aidl) {
151     return convertReinterpret<audio_port_handle_t>(aidl);
152 }
153 
legacy2aidl_audio_port_handle_t_int32_t(audio_port_handle_t legacy)154 ConversionResult<int32_t> legacy2aidl_audio_port_handle_t_int32_t(audio_port_handle_t legacy) {
155     return convertReinterpret<int32_t>(legacy);
156 }
157 
aidl2legacy_int32_t_audio_patch_handle_t(int32_t aidl)158 ConversionResult<audio_patch_handle_t> aidl2legacy_int32_t_audio_patch_handle_t(int32_t aidl) {
159     return convertReinterpret<audio_patch_handle_t>(aidl);
160 }
161 
legacy2aidl_audio_patch_handle_t_int32_t(audio_patch_handle_t legacy)162 ConversionResult<int32_t> legacy2aidl_audio_patch_handle_t_int32_t(audio_patch_handle_t legacy) {
163     return convertReinterpret<int32_t>(legacy);
164 }
165 
aidl2legacy_int32_t_audio_unique_id_t(int32_t aidl)166 ConversionResult<audio_unique_id_t> aidl2legacy_int32_t_audio_unique_id_t(int32_t aidl) {
167     return convertReinterpret<audio_unique_id_t>(aidl);
168 }
169 
legacy2aidl_audio_unique_id_t_int32_t(audio_unique_id_t legacy)170 ConversionResult<int32_t> legacy2aidl_audio_unique_id_t_int32_t(audio_unique_id_t legacy) {
171     return convertReinterpret<int32_t>(legacy);
172 }
173 
aidl2legacy_int32_t_audio_hw_sync_t(int32_t aidl)174 ConversionResult<audio_hw_sync_t> aidl2legacy_int32_t_audio_hw_sync_t(int32_t aidl) {
175     return convertReinterpret<audio_hw_sync_t>(aidl);
176 }
177 
legacy2aidl_audio_hw_sync_t_int32_t(audio_hw_sync_t legacy)178 ConversionResult<int32_t> legacy2aidl_audio_hw_sync_t_int32_t(audio_hw_sync_t legacy) {
179     return convertReinterpret<int32_t>(legacy);
180 }
181 
aidl2legacy_int32_t_pid_t(int32_t aidl)182 ConversionResult<pid_t> aidl2legacy_int32_t_pid_t(int32_t aidl) {
183     return convertReinterpret<pid_t>(aidl);
184 }
185 
legacy2aidl_pid_t_int32_t(pid_t legacy)186 ConversionResult<int32_t> legacy2aidl_pid_t_int32_t(pid_t legacy) {
187     return convertReinterpret<int32_t>(legacy);
188 }
189 
aidl2legacy_int32_t_uid_t(int32_t aidl)190 ConversionResult<uid_t> aidl2legacy_int32_t_uid_t(int32_t aidl) {
191     return convertReinterpret<uid_t>(aidl);
192 }
193 
legacy2aidl_uid_t_int32_t(uid_t legacy)194 ConversionResult<int32_t> legacy2aidl_uid_t_int32_t(uid_t legacy) {
195     return convertReinterpret<int32_t>(legacy);
196 }
197 
aidl2legacy_string_view_String16(std::string_view aidl)198 ConversionResult<String16> aidl2legacy_string_view_String16(std::string_view aidl) {
199     return String16(aidl.data(), aidl.size());
200 }
201 
legacy2aidl_String16_string(const String16 & legacy)202 ConversionResult<std::string> legacy2aidl_String16_string(const String16& legacy) {
203     return std::string(String8(legacy).c_str());
204 }
205 
206 // TODO b/182392769: create an optional -> optional util
207 ConversionResult<std::optional<String16>>
aidl2legacy_optional_string_view_optional_String16(std::optional<std::string_view> aidl)208 aidl2legacy_optional_string_view_optional_String16(std::optional<std::string_view> aidl) {
209     if (!aidl.has_value()) {
210         return std::nullopt;
211     }
212     ConversionResult<String16> conversion =
213         VALUE_OR_RETURN(aidl2legacy_string_view_String16(aidl.value()));
214     return conversion.value();
215 }
216 
217 ConversionResult<std::optional<std::string_view>>
legacy2aidl_optional_String16_optional_string(std::optional<String16> legacy)218 legacy2aidl_optional_String16_optional_string(std::optional<String16> legacy) {
219   if (!legacy.has_value()) {
220     return std::nullopt;
221   }
222   ConversionResult<std::string> conversion =
223       VALUE_OR_RETURN(legacy2aidl_String16_string(legacy.value()));
224   return conversion.value();
225 }
226 
aidl2legacy_string_view_String8(std::string_view aidl)227 ConversionResult<String8> aidl2legacy_string_view_String8(std::string_view aidl) {
228     return String8(aidl.data(), aidl.size());
229 }
230 
legacy2aidl_String8_string(const String8 & legacy)231 ConversionResult<std::string> legacy2aidl_String8_string(const String8& legacy) {
232     return std::string(legacy.c_str());
233 }
234 
235 namespace {
236 
237 namespace detail {
238 using AudioChannelBitPair = std::pair<audio_channel_mask_t, int>;
239 using AudioChannelBitPairs = std::vector<AudioChannelBitPair>;
240 using AudioChannelPair = std::pair<audio_channel_mask_t, AudioChannelLayout>;
241 using AudioChannelPairs = std::vector<AudioChannelPair>;
242 using AudioDevicePair = std::pair<audio_devices_t, AudioDeviceDescription>;
243 using AudioDevicePairs = std::vector<AudioDevicePair>;
244 using AudioFormatPair = std::pair<audio_format_t, AudioFormatDescription>;
245 using AudioFormatPairs = std::vector<AudioFormatPair>;
246 }
247 
getInAudioChannelBits()248 const detail::AudioChannelBitPairs& getInAudioChannelBits() {
249     static const detail::AudioChannelBitPairs pairs = {
250         { AUDIO_CHANNEL_IN_LEFT, AudioChannelLayout::CHANNEL_FRONT_LEFT },
251         { AUDIO_CHANNEL_IN_RIGHT, AudioChannelLayout::CHANNEL_FRONT_RIGHT },
252         // AUDIO_CHANNEL_IN_FRONT is at the end
253         { AUDIO_CHANNEL_IN_BACK, AudioChannelLayout::CHANNEL_BACK_CENTER },
254         // AUDIO_CHANNEL_IN_*_PROCESSED not supported
255         // AUDIO_CHANNEL_IN_PRESSURE not supported
256         // AUDIO_CHANNEL_IN_*_AXIS not supported
257         // AUDIO_CHANNEL_IN_VOICE_* not supported
258         { AUDIO_CHANNEL_IN_BACK_LEFT, AudioChannelLayout::CHANNEL_BACK_LEFT },
259         { AUDIO_CHANNEL_IN_BACK_RIGHT, AudioChannelLayout::CHANNEL_BACK_RIGHT },
260         { AUDIO_CHANNEL_IN_CENTER, AudioChannelLayout::CHANNEL_FRONT_CENTER },
261         { AUDIO_CHANNEL_IN_LOW_FREQUENCY, AudioChannelLayout::CHANNEL_LOW_FREQUENCY },
262         { AUDIO_CHANNEL_IN_TOP_LEFT, AudioChannelLayout::CHANNEL_TOP_SIDE_LEFT },
263         { AUDIO_CHANNEL_IN_TOP_RIGHT, AudioChannelLayout::CHANNEL_TOP_SIDE_RIGHT },
264         // When going from aidl to legacy, IN_CENTER is used
265         { AUDIO_CHANNEL_IN_FRONT, AudioChannelLayout::CHANNEL_FRONT_CENTER }
266     };
267     return pairs;
268 }
269 
getInAudioChannelPairs()270 const detail::AudioChannelPairs& getInAudioChannelPairs() {
271     static const detail::AudioChannelPairs pairs = {
272 #define DEFINE_INPUT_LAYOUT(n)                                                 \
273             {                                                                  \
274                 AUDIO_CHANNEL_IN_##n,                                          \
275                 AudioChannelLayout::make<AudioChannelLayout::Tag::layoutMask>( \
276                         AudioChannelLayout::LAYOUT_##n)                        \
277             }
278 
279         DEFINE_INPUT_LAYOUT(MONO),
280         DEFINE_INPUT_LAYOUT(STEREO),
281         DEFINE_INPUT_LAYOUT(2POINT1),
282         DEFINE_INPUT_LAYOUT(FRONT_BACK),
283         DEFINE_INPUT_LAYOUT(TRI),
284         DEFINE_INPUT_LAYOUT(3POINT1),
285         // AUDIO_CHANNEL_IN_6 not supported
286         DEFINE_INPUT_LAYOUT(2POINT0POINT2),
287         DEFINE_INPUT_LAYOUT(2POINT1POINT2),
288         DEFINE_INPUT_LAYOUT(3POINT0POINT2),
289         DEFINE_INPUT_LAYOUT(3POINT1POINT2),
290         DEFINE_INPUT_LAYOUT(QUAD),
291         DEFINE_INPUT_LAYOUT(PENTA),
292         DEFINE_INPUT_LAYOUT(5POINT1)
293 #undef DEFINE_INPUT_LAYOUT
294     };
295     return pairs;
296 }
297 
getOutAudioChannelBits()298 const detail::AudioChannelBitPairs& getOutAudioChannelBits() {
299     static const detail::AudioChannelBitPairs pairs = {
300 #define DEFINE_OUTPUT_BITS(n)                                                  \
301             { AUDIO_CHANNEL_OUT_##n, AudioChannelLayout::CHANNEL_##n }
302 
303         DEFINE_OUTPUT_BITS(FRONT_LEFT),
304         DEFINE_OUTPUT_BITS(FRONT_RIGHT),
305         DEFINE_OUTPUT_BITS(FRONT_CENTER),
306         DEFINE_OUTPUT_BITS(LOW_FREQUENCY),
307         DEFINE_OUTPUT_BITS(BACK_LEFT),
308         DEFINE_OUTPUT_BITS(BACK_RIGHT),
309         DEFINE_OUTPUT_BITS(FRONT_LEFT_OF_CENTER),
310         DEFINE_OUTPUT_BITS(FRONT_RIGHT_OF_CENTER),
311         DEFINE_OUTPUT_BITS(BACK_CENTER),
312         DEFINE_OUTPUT_BITS(SIDE_LEFT),
313         DEFINE_OUTPUT_BITS(SIDE_RIGHT),
314         DEFINE_OUTPUT_BITS(TOP_CENTER),
315         DEFINE_OUTPUT_BITS(TOP_FRONT_LEFT),
316         DEFINE_OUTPUT_BITS(TOP_FRONT_CENTER),
317         DEFINE_OUTPUT_BITS(TOP_FRONT_RIGHT),
318         DEFINE_OUTPUT_BITS(TOP_BACK_LEFT),
319         DEFINE_OUTPUT_BITS(TOP_BACK_CENTER),
320         DEFINE_OUTPUT_BITS(TOP_BACK_RIGHT),
321         DEFINE_OUTPUT_BITS(TOP_SIDE_LEFT),
322         DEFINE_OUTPUT_BITS(TOP_SIDE_RIGHT),
323         DEFINE_OUTPUT_BITS(BOTTOM_FRONT_LEFT),
324         DEFINE_OUTPUT_BITS(BOTTOM_FRONT_CENTER),
325         DEFINE_OUTPUT_BITS(BOTTOM_FRONT_RIGHT),
326         DEFINE_OUTPUT_BITS(LOW_FREQUENCY_2),
327         DEFINE_OUTPUT_BITS(FRONT_WIDE_LEFT),
328         DEFINE_OUTPUT_BITS(FRONT_WIDE_RIGHT),
329 #undef DEFINE_OUTPUT_BITS
330         { AUDIO_CHANNEL_OUT_HAPTIC_A, AudioChannelLayout::CHANNEL_HAPTIC_A },
331         { AUDIO_CHANNEL_OUT_HAPTIC_B, AudioChannelLayout::CHANNEL_HAPTIC_B }
332     };
333     return pairs;
334 }
335 
getOutAudioChannelPairs()336 const detail::AudioChannelPairs& getOutAudioChannelPairs() {
337     static const detail::AudioChannelPairs pairs = {
338 #define DEFINE_OUTPUT_LAYOUT(n)                                                \
339             {                                                                  \
340                 AUDIO_CHANNEL_OUT_##n,                                         \
341                 AudioChannelLayout::make<AudioChannelLayout::Tag::layoutMask>( \
342                         AudioChannelLayout::LAYOUT_##n)                        \
343             }
344 
345         DEFINE_OUTPUT_LAYOUT(MONO),
346         DEFINE_OUTPUT_LAYOUT(STEREO),
347         DEFINE_OUTPUT_LAYOUT(2POINT1),
348         DEFINE_OUTPUT_LAYOUT(TRI),
349         DEFINE_OUTPUT_LAYOUT(TRI_BACK),
350         DEFINE_OUTPUT_LAYOUT(3POINT1),
351         DEFINE_OUTPUT_LAYOUT(2POINT0POINT2),
352         DEFINE_OUTPUT_LAYOUT(2POINT1POINT2),
353         DEFINE_OUTPUT_LAYOUT(3POINT0POINT2),
354         DEFINE_OUTPUT_LAYOUT(3POINT1POINT2),
355         DEFINE_OUTPUT_LAYOUT(QUAD),
356         DEFINE_OUTPUT_LAYOUT(QUAD_SIDE),
357         DEFINE_OUTPUT_LAYOUT(SURROUND),
358         DEFINE_OUTPUT_LAYOUT(PENTA),
359         DEFINE_OUTPUT_LAYOUT(5POINT1),
360         DEFINE_OUTPUT_LAYOUT(5POINT1_SIDE),
361         DEFINE_OUTPUT_LAYOUT(5POINT1POINT2),
362         DEFINE_OUTPUT_LAYOUT(5POINT1POINT4),
363         DEFINE_OUTPUT_LAYOUT(6POINT1),
364         DEFINE_OUTPUT_LAYOUT(7POINT1),
365         DEFINE_OUTPUT_LAYOUT(7POINT1POINT2),
366         DEFINE_OUTPUT_LAYOUT(7POINT1POINT4),
367         DEFINE_OUTPUT_LAYOUT(13POINT_360RA),
368         DEFINE_OUTPUT_LAYOUT(22POINT2),
369         DEFINE_OUTPUT_LAYOUT(MONO_HAPTIC_A),
370         DEFINE_OUTPUT_LAYOUT(STEREO_HAPTIC_A),
371         DEFINE_OUTPUT_LAYOUT(HAPTIC_AB),
372         DEFINE_OUTPUT_LAYOUT(MONO_HAPTIC_AB),
373         DEFINE_OUTPUT_LAYOUT(STEREO_HAPTIC_AB)
374 #undef DEFINE_OUTPUT_LAYOUT
375     };
376     return pairs;
377 }
378 
getVoiceAudioChannelPairs()379 const detail::AudioChannelPairs& getVoiceAudioChannelPairs() {
380     static const detail::AudioChannelPairs pairs = {
381 #define DEFINE_VOICE_LAYOUT(n)                                                 \
382             {                                                                  \
383                 AUDIO_CHANNEL_IN_VOICE_##n,                                    \
384                 AudioChannelLayout::make<AudioChannelLayout::Tag::voiceMask>(  \
385                         AudioChannelLayout::VOICE_##n)                         \
386             }
387         DEFINE_VOICE_LAYOUT(UPLINK_MONO),
388         DEFINE_VOICE_LAYOUT(DNLINK_MONO),
389         DEFINE_VOICE_LAYOUT(CALL_MONO)
390 #undef DEFINE_VOICE_LAYOUT
391     };
392     return pairs;
393 }
394 
make_AudioDeviceDescription(AudioDeviceType type,const std::string & connection="")395 AudioDeviceDescription make_AudioDeviceDescription(AudioDeviceType type,
396         const std::string& connection = "") {
397     AudioDeviceDescription result;
398     result.type = type;
399     result.connection = connection;
400     return result;
401 }
402 
append_AudioDeviceDescription(detail::AudioDevicePairs & pairs,audio_devices_t inputType,audio_devices_t outputType,AudioDeviceType inType,AudioDeviceType outType,const std::string & connection="")403 void append_AudioDeviceDescription(detail::AudioDevicePairs& pairs,
404         audio_devices_t inputType, audio_devices_t outputType,
405         AudioDeviceType inType, AudioDeviceType outType,
406         const std::string& connection = "") {
407     pairs.push_back(std::make_pair(inputType, make_AudioDeviceDescription(inType, connection)));
408     pairs.push_back(std::make_pair(outputType, make_AudioDeviceDescription(outType, connection)));
409 }
410 
getAudioDevicePairs()411 const detail::AudioDevicePairs& getAudioDevicePairs() {
412     static const detail::AudioDevicePairs pairs = []() {
413         detail::AudioDevicePairs pairs = {{
414             {
415                 AUDIO_DEVICE_NONE, AudioDeviceDescription{}
416             },
417             {
418                 AUDIO_DEVICE_OUT_EARPIECE, make_AudioDeviceDescription(
419                         AudioDeviceType::OUT_SPEAKER_EARPIECE)
420             },
421             {
422                 AUDIO_DEVICE_OUT_SPEAKER, make_AudioDeviceDescription(
423                         AudioDeviceType::OUT_SPEAKER)
424             },
425             {
426                 AUDIO_DEVICE_OUT_WIRED_HEADPHONE, make_AudioDeviceDescription(
427                         AudioDeviceType::OUT_HEADPHONE,
428                         GET_DEVICE_DESC_CONNECTION(ANALOG))
429             },
430             {
431                 AUDIO_DEVICE_OUT_BLUETOOTH_SCO, make_AudioDeviceDescription(
432                         AudioDeviceType::OUT_DEVICE,
433                         GET_DEVICE_DESC_CONNECTION(BT_SCO))
434             },
435             {
436                 AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT, make_AudioDeviceDescription(
437                         AudioDeviceType::OUT_CARKIT,
438                         GET_DEVICE_DESC_CONNECTION(BT_SCO))
439             },
440             {
441                 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES, make_AudioDeviceDescription(
442                         AudioDeviceType::OUT_HEADPHONE,
443                         GET_DEVICE_DESC_CONNECTION(BT_A2DP))
444             },
445             {
446                 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER, make_AudioDeviceDescription(
447                         AudioDeviceType::OUT_SPEAKER,
448                         GET_DEVICE_DESC_CONNECTION(BT_A2DP))
449             },
450             {
451                 AUDIO_DEVICE_OUT_TELEPHONY_TX, make_AudioDeviceDescription(
452                         AudioDeviceType::OUT_TELEPHONY_TX)
453             },
454             {
455                 AUDIO_DEVICE_OUT_AUX_LINE, make_AudioDeviceDescription(
456                         AudioDeviceType::OUT_LINE_AUX)
457             },
458             {
459                 AUDIO_DEVICE_OUT_SPEAKER_SAFE, make_AudioDeviceDescription(
460                         AudioDeviceType::OUT_SPEAKER_SAFE)
461             },
462             {
463                 AUDIO_DEVICE_OUT_HEARING_AID, make_AudioDeviceDescription(
464                         AudioDeviceType::OUT_HEARING_AID,
465                         GET_DEVICE_DESC_CONNECTION(WIRELESS))
466             },
467             {
468                 AUDIO_DEVICE_OUT_ECHO_CANCELLER, make_AudioDeviceDescription(
469                         AudioDeviceType::OUT_ECHO_CANCELLER)
470             },
471             {
472                 AUDIO_DEVICE_OUT_BLE_SPEAKER, make_AudioDeviceDescription(
473                         AudioDeviceType::OUT_SPEAKER,
474                         GET_DEVICE_DESC_CONNECTION(BT_LE))
475             },
476             {
477                 AUDIO_DEVICE_OUT_BLE_BROADCAST, make_AudioDeviceDescription(
478                         AudioDeviceType::OUT_BROADCAST,
479                         GET_DEVICE_DESC_CONNECTION(BT_LE))
480             },
481             // AUDIO_DEVICE_IN_AMBIENT and IN_COMMUNICATION are removed since they were deprecated.
482             {
483                 AUDIO_DEVICE_IN_BUILTIN_MIC, make_AudioDeviceDescription(
484                         AudioDeviceType::IN_MICROPHONE)
485             },
486             {
487                 AUDIO_DEVICE_IN_BACK_MIC, make_AudioDeviceDescription(
488                         AudioDeviceType::IN_MICROPHONE_BACK)
489             },
490             {
491                 AUDIO_DEVICE_IN_TELEPHONY_RX, make_AudioDeviceDescription(
492                         AudioDeviceType::IN_TELEPHONY_RX)
493             },
494             {
495                 AUDIO_DEVICE_IN_TV_TUNER, make_AudioDeviceDescription(
496                         AudioDeviceType::IN_TV_TUNER)
497             },
498             {
499                 AUDIO_DEVICE_IN_LOOPBACK, make_AudioDeviceDescription(
500                         AudioDeviceType::IN_LOOPBACK)
501             },
502             {
503                 AUDIO_DEVICE_IN_BLUETOOTH_BLE, make_AudioDeviceDescription(
504                         AudioDeviceType::IN_DEVICE,
505                         GET_DEVICE_DESC_CONNECTION(BT_LE))
506             },
507             {
508                 AUDIO_DEVICE_IN_ECHO_REFERENCE, make_AudioDeviceDescription(
509                         AudioDeviceType::IN_ECHO_REFERENCE)
510             }
511         }};
512         append_AudioDeviceDescription(pairs,
513                 AUDIO_DEVICE_IN_DEFAULT, AUDIO_DEVICE_OUT_DEFAULT,
514                 AudioDeviceType::IN_DEFAULT, AudioDeviceType::OUT_DEFAULT);
515         append_AudioDeviceDescription(pairs,
516                 AUDIO_DEVICE_IN_WIRED_HEADSET, AUDIO_DEVICE_OUT_WIRED_HEADSET,
517                 AudioDeviceType::IN_HEADSET, AudioDeviceType::OUT_HEADSET,
518                 GET_DEVICE_DESC_CONNECTION(ANALOG));
519         append_AudioDeviceDescription(pairs,
520                 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET,
521                 AudioDeviceType::IN_HEADSET, AudioDeviceType::OUT_HEADSET,
522                 GET_DEVICE_DESC_CONNECTION(BT_SCO));
523         append_AudioDeviceDescription(pairs,
524                 AUDIO_DEVICE_IN_HDMI, AUDIO_DEVICE_OUT_HDMI,
525                 AudioDeviceType::IN_DEVICE, AudioDeviceType::OUT_DEVICE,
526                 GET_DEVICE_DESC_CONNECTION(HDMI));
527         append_AudioDeviceDescription(pairs,
528                 AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET, AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET,
529                 AudioDeviceType::IN_DOCK, AudioDeviceType::OUT_DOCK,
530                 GET_DEVICE_DESC_CONNECTION(ANALOG));
531         append_AudioDeviceDescription(pairs,
532                 AUDIO_DEVICE_IN_DGTL_DOCK_HEADSET, AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET,
533                 AudioDeviceType::IN_DOCK, AudioDeviceType::OUT_DOCK,
534                 GET_DEVICE_DESC_CONNECTION(USB));
535         append_AudioDeviceDescription(pairs,
536                 AUDIO_DEVICE_IN_USB_ACCESSORY, AUDIO_DEVICE_OUT_USB_ACCESSORY,
537                 AudioDeviceType::IN_ACCESSORY, AudioDeviceType::OUT_ACCESSORY,
538                 GET_DEVICE_DESC_CONNECTION(USB));
539         append_AudioDeviceDescription(pairs,
540                 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_OUT_USB_DEVICE,
541                 AudioDeviceType::IN_DEVICE, AudioDeviceType::OUT_DEVICE,
542                 GET_DEVICE_DESC_CONNECTION(USB));
543         append_AudioDeviceDescription(pairs,
544                 AUDIO_DEVICE_IN_FM_TUNER, AUDIO_DEVICE_OUT_FM,
545                 AudioDeviceType::IN_FM_TUNER, AudioDeviceType::OUT_FM);
546         append_AudioDeviceDescription(pairs,
547                 AUDIO_DEVICE_IN_LINE, AUDIO_DEVICE_OUT_LINE,
548                 AudioDeviceType::IN_DEVICE, AudioDeviceType::OUT_DEVICE,
549                 GET_DEVICE_DESC_CONNECTION(ANALOG));
550         append_AudioDeviceDescription(pairs,
551                 AUDIO_DEVICE_IN_SPDIF, AUDIO_DEVICE_OUT_SPDIF,
552                 AudioDeviceType::IN_DEVICE, AudioDeviceType::OUT_DEVICE,
553                 GET_DEVICE_DESC_CONNECTION(SPDIF));
554         append_AudioDeviceDescription(pairs,
555                 AUDIO_DEVICE_IN_BLUETOOTH_A2DP, AUDIO_DEVICE_OUT_BLUETOOTH_A2DP,
556                 AudioDeviceType::IN_DEVICE, AudioDeviceType::OUT_DEVICE,
557                 GET_DEVICE_DESC_CONNECTION(BT_A2DP));
558         append_AudioDeviceDescription(pairs,
559                 AUDIO_DEVICE_IN_IP, AUDIO_DEVICE_OUT_IP,
560                 AudioDeviceType::IN_DEVICE, AudioDeviceType::OUT_DEVICE,
561                 GET_DEVICE_DESC_CONNECTION(IP_V4));
562         append_AudioDeviceDescription(pairs,
563                 AUDIO_DEVICE_IN_BUS, AUDIO_DEVICE_OUT_BUS,
564                 AudioDeviceType::IN_BUS, AudioDeviceType::OUT_BUS);
565         append_AudioDeviceDescription(pairs,
566                 AUDIO_DEVICE_IN_PROXY, AUDIO_DEVICE_OUT_PROXY,
567                 AudioDeviceType::IN_AFE_PROXY, AudioDeviceType::OUT_AFE_PROXY,
568                 GET_DEVICE_DESC_CONNECTION(VIRTUAL));
569         append_AudioDeviceDescription(pairs,
570                 AUDIO_DEVICE_IN_USB_HEADSET, AUDIO_DEVICE_OUT_USB_HEADSET,
571                 AudioDeviceType::IN_HEADSET, AudioDeviceType::OUT_HEADSET,
572                 GET_DEVICE_DESC_CONNECTION(USB));
573         append_AudioDeviceDescription(pairs,
574                 AUDIO_DEVICE_IN_HDMI_ARC, AUDIO_DEVICE_OUT_HDMI_ARC,
575                 AudioDeviceType::IN_DEVICE, AudioDeviceType::OUT_DEVICE,
576                 GET_DEVICE_DESC_CONNECTION(HDMI_ARC));
577         append_AudioDeviceDescription(pairs,
578                 AUDIO_DEVICE_IN_HDMI_EARC, AUDIO_DEVICE_OUT_HDMI_EARC,
579                 AudioDeviceType::IN_DEVICE, AudioDeviceType::OUT_DEVICE,
580                 GET_DEVICE_DESC_CONNECTION(HDMI_EARC));
581         append_AudioDeviceDescription(pairs,
582                 AUDIO_DEVICE_IN_BLE_HEADSET, AUDIO_DEVICE_OUT_BLE_HEADSET,
583                 AudioDeviceType::IN_HEADSET, AudioDeviceType::OUT_HEADSET,
584                 GET_DEVICE_DESC_CONNECTION(BT_LE));
585         append_AudioDeviceDescription(pairs,
586                 AUDIO_DEVICE_IN_REMOTE_SUBMIX, AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
587                 AudioDeviceType::IN_SUBMIX, AudioDeviceType::OUT_SUBMIX,
588                 GET_DEVICE_DESC_CONNECTION(VIRTUAL));
589 
590         return pairs;
591     }();
592     return pairs;
593 }
594 
make_AudioFormatDescription(AudioFormatType type)595 AudioFormatDescription make_AudioFormatDescription(AudioFormatType type) {
596     AudioFormatDescription result;
597     result.type = type;
598     return result;
599 }
600 
make_AudioFormatDescription(PcmType pcm)601 AudioFormatDescription make_AudioFormatDescription(PcmType pcm) {
602     auto result = make_AudioFormatDescription(AudioFormatType::PCM);
603     result.pcm = pcm;
604     return result;
605 }
606 
make_AudioFormatDescription(const std::string & encoding)607 AudioFormatDescription make_AudioFormatDescription(const std::string& encoding) {
608     AudioFormatDescription result;
609     result.encoding = encoding;
610     return result;
611 }
612 
make_AudioFormatDescription(PcmType transport,const std::string & encoding)613 AudioFormatDescription make_AudioFormatDescription(PcmType transport,
614         const std::string& encoding) {
615     auto result = make_AudioFormatDescription(encoding);
616     result.pcm = transport;
617     return result;
618 }
619 
getAudioFormatPairs()620 const detail::AudioFormatPairs& getAudioFormatPairs() {
621     static const detail::AudioFormatPairs pairs = {{
622             {AUDIO_FORMAT_INVALID,
623              make_AudioFormatDescription(AudioFormatType::SYS_RESERVED_INVALID)},
624             {AUDIO_FORMAT_DEFAULT, AudioFormatDescription{}},
625             {AUDIO_FORMAT_PCM_16_BIT, make_AudioFormatDescription(PcmType::INT_16_BIT)},
626             {AUDIO_FORMAT_PCM_8_BIT, make_AudioFormatDescription(PcmType::UINT_8_BIT)},
627             {AUDIO_FORMAT_PCM_32_BIT, make_AudioFormatDescription(PcmType::INT_32_BIT)},
628             {AUDIO_FORMAT_PCM_8_24_BIT, make_AudioFormatDescription(PcmType::FIXED_Q_8_24)},
629             {AUDIO_FORMAT_PCM_FLOAT, make_AudioFormatDescription(PcmType::FLOAT_32_BIT)},
630             {AUDIO_FORMAT_PCM_24_BIT_PACKED, make_AudioFormatDescription(PcmType::INT_24_BIT)},
631             {AUDIO_FORMAT_MP3, make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_MPEG)},
632             {AUDIO_FORMAT_AMR_NB,
633              make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_AMR_NB)},
634             {AUDIO_FORMAT_AMR_WB,
635              make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_AMR_WB)},
636             {AUDIO_FORMAT_AAC,
637              make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_AAC_MP4)},
638             {AUDIO_FORMAT_AAC_MAIN,
639              make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_AAC_MAIN)},
640             {AUDIO_FORMAT_AAC_LC,
641              make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_AAC_LC)},
642             {AUDIO_FORMAT_AAC_SSR,
643              make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_AAC_SSR)},
644             {AUDIO_FORMAT_AAC_LTP,
645              make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_AAC_LTP)},
646             {AUDIO_FORMAT_AAC_HE_V1,
647              make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_AAC_HE_V1)},
648             {AUDIO_FORMAT_AAC_SCALABLE,
649              make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_AAC_SCALABLE)},
650             {AUDIO_FORMAT_AAC_ERLC,
651              make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_AAC_ERLC)},
652             {AUDIO_FORMAT_AAC_LD,
653              make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_AAC_LD)},
654             {AUDIO_FORMAT_AAC_HE_V2,
655              make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_AAC_HE_V2)},
656             {AUDIO_FORMAT_AAC_ELD,
657              make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_AAC_ELD)},
658             {AUDIO_FORMAT_AAC_XHE,
659              make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_AAC_XHE)
660 
661             },
662             // AUDIO_FORMAT_HE_AAC_V1 and HE_AAC_V2 are removed since they were deprecated long time
663             // ago.
664             {AUDIO_FORMAT_VORBIS,
665              make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_VORBIS)},
666             {AUDIO_FORMAT_OPUS, make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_OPUS)},
667             {AUDIO_FORMAT_AC3, make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_AC3)},
668             {AUDIO_FORMAT_E_AC3, make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_EAC3)},
669             {AUDIO_FORMAT_E_AC3_JOC,
670              make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_EAC3_JOC)},
671             {AUDIO_FORMAT_DTS, make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_DTS)},
672             {AUDIO_FORMAT_DTS_HD,
673              make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_DTS_HD)},
674             {AUDIO_FORMAT_DTS_HD_MA,
675              make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_DTS_HD_MA)},
676             {AUDIO_FORMAT_DTS_UHD,
677              make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_DTS_UHD_P1)},
678             {AUDIO_FORMAT_DTS_UHD_P2,
679              make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_DTS_UHD_P2)},
680             // In the future, we would like to represent encapsulated bitstreams as
681             // nested AudioFormatDescriptions. The legacy 'AUDIO_FORMAT_IEC61937' type doesn't
682             // specify the format of the encapsulated bitstream.
683             {AUDIO_FORMAT_IEC61937,
684              make_AudioFormatDescription(PcmType::INT_16_BIT,
685                                          ::android::MEDIA_MIMETYPE_AUDIO_IEC61937)},
686             {AUDIO_FORMAT_DOLBY_TRUEHD,
687              make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_DOLBY_TRUEHD)},
688             {AUDIO_FORMAT_EVRC, make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_EVRC)},
689             {AUDIO_FORMAT_EVRCB,
690              make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_EVRCB)},
691             {AUDIO_FORMAT_EVRCWB,
692              make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_EVRCWB)},
693             {AUDIO_FORMAT_EVRCNW,
694              make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_EVRCNW)},
695             {AUDIO_FORMAT_AAC_ADIF,
696              make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_AAC_ADIF)},
697             {AUDIO_FORMAT_WMA, make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_WMA)},
698             {// Note: not in the IANA registry.
699              AUDIO_FORMAT_WMA_PRO, make_AudioFormatDescription("audio/x-ms-wma.pro")},
700             {AUDIO_FORMAT_AMR_WB_PLUS,
701              make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_AMR_WB_PLUS)},
702             {AUDIO_FORMAT_MP2,
703              make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_MPEG_LAYER_II)},
704             {AUDIO_FORMAT_QCELP,
705              make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_QCELP)},
706             {// Note: not in the IANA registry.
707              AUDIO_FORMAT_DSD, make_AudioFormatDescription("audio/vnd.sony.dsd")},
708             {AUDIO_FORMAT_FLAC, make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_FLAC)},
709             {AUDIO_FORMAT_ALAC, make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_ALAC)},
710             {// Note: not in the IANA registry.
711              AUDIO_FORMAT_APE, make_AudioFormatDescription("audio/x-ape")},
712             {AUDIO_FORMAT_AAC_ADTS,
713              make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_AAC_ADTS)},
714             {AUDIO_FORMAT_AAC_ADTS_MAIN,
715              make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_AAC_ADTS_MAIN)},
716             {AUDIO_FORMAT_AAC_ADTS_LC,
717              make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_AAC_ADTS_LC)},
718             {AUDIO_FORMAT_AAC_ADTS_SSR,
719              make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_AAC_ADTS_SSR)},
720             {AUDIO_FORMAT_AAC_ADTS_LTP,
721              make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_AAC_ADTS_LTP)},
722             {AUDIO_FORMAT_AAC_ADTS_HE_V1,
723              make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_AAC_ADTS_HE_V1)},
724             {AUDIO_FORMAT_AAC_ADTS_SCALABLE,
725              make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_AAC_ADTS_SCALABLE)},
726             {AUDIO_FORMAT_AAC_ADTS_ERLC,
727              make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_AAC_ADTS_ERLC)},
728             {AUDIO_FORMAT_AAC_ADTS_LD,
729              make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_AAC_ADTS_LD)},
730             {AUDIO_FORMAT_AAC_ADTS_HE_V2,
731              make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_AAC_ADTS_HE_V2)},
732             {AUDIO_FORMAT_AAC_ADTS_ELD,
733              make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_AAC_ADTS_ELD)},
734             {AUDIO_FORMAT_AAC_ADTS_XHE,
735              make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_AAC_ADTS_XHE)},
736             {// Note: not in the IANA registry. "vnd.octel.sbc" is not BT SBC.
737              AUDIO_FORMAT_SBC, make_AudioFormatDescription("audio/x-sbc")},
738             {AUDIO_FORMAT_APTX, make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_APTX)},
739             {// Note: not in the IANA registry.
740              AUDIO_FORMAT_APTX_HD, make_AudioFormatDescription("audio/vnd.qcom.aptx.hd")},
741             {AUDIO_FORMAT_AC4, make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_AC4)},
742             {// Note: not in the IANA registry.
743              AUDIO_FORMAT_LDAC, make_AudioFormatDescription("audio/vnd.sony.ldac")},
744             {AUDIO_FORMAT_MAT,
745              make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_DOLBY_MAT)},
746             {// Note: not in the IANA registry.
747              AUDIO_FORMAT_MAT_1_0,
748              make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_DOLBY_MAT +
749                                          std::string(".1.0"))},
750             {// Note: not in the IANA registry.
751              AUDIO_FORMAT_MAT_2_0,
752              make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_DOLBY_MAT +
753                                          std::string(".2.0"))},
754             {// Note: not in the IANA registry.
755              AUDIO_FORMAT_MAT_2_1,
756              make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_DOLBY_MAT +
757                                          std::string(".2.1"))},
758             {AUDIO_FORMAT_AAC_LATM,
759              make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_AAC)},
760             {AUDIO_FORMAT_AAC_LATM_LC,
761              make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_AAC_LATM_LC)},
762             {AUDIO_FORMAT_AAC_LATM_HE_V1,
763              make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_AAC_LATM_HE_V1)},
764             {AUDIO_FORMAT_AAC_LATM_HE_V2,
765              make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_AAC_LATM_HE_V2)},
766             {// Note: not in the IANA registry.
767              AUDIO_FORMAT_CELT, make_AudioFormatDescription("audio/x-celt")},
768             {// Note: not in the IANA registry.
769              AUDIO_FORMAT_APTX_ADAPTIVE,
770              make_AudioFormatDescription("audio/vnd.qcom.aptx.adaptive")},
771             {// Note: not in the IANA registry.
772              AUDIO_FORMAT_LHDC, make_AudioFormatDescription("audio/vnd.savitech.lhdc")},
773             {// Note: not in the IANA registry.
774              AUDIO_FORMAT_LHDC_LL, make_AudioFormatDescription("audio/vnd.savitech.lhdc.ll")},
775             {// Note: not in the IANA registry.
776              AUDIO_FORMAT_APTX_TWSP, make_AudioFormatDescription("audio/vnd.qcom.aptx.twsp")},
777             {// Note: not in the IANA registry.
778              AUDIO_FORMAT_LC3, make_AudioFormatDescription("audio/x-lc3")},
779             {AUDIO_FORMAT_MPEGH,
780              make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_MPEGH_MHM1)},
781             {AUDIO_FORMAT_MPEGH_BL_L3,
782              make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_MPEGH_BL_L3)},
783             {AUDIO_FORMAT_MPEGH_BL_L4,
784              make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_MPEGH_BL_L4)},
785             {AUDIO_FORMAT_MPEGH_LC_L3,
786              make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_MPEGH_LC_L3)},
787             {AUDIO_FORMAT_MPEGH_LC_L4,
788              make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_MPEGH_LC_L4)},
789             {AUDIO_FORMAT_IEC60958,
790              make_AudioFormatDescription(PcmType::INT_24_BIT,
791                                          ::android::MEDIA_MIMETYPE_AUDIO_IEC60958)},
792             {AUDIO_FORMAT_DRA, make_AudioFormatDescription(::android::MEDIA_MIMETYPE_AUDIO_DRA)},
793             {// Note: not in the IANA registry.
794              AUDIO_FORMAT_APTX_ADAPTIVE_QLEA,
795              make_AudioFormatDescription("audio/vnd.qcom.aptx.adaptive.r3")},
796             {// Note: not in the IANA registry.
797              AUDIO_FORMAT_APTX_ADAPTIVE_R4,
798              make_AudioFormatDescription("audio/vnd.qcom.aptx.adaptive.r4")},
799     }};
800     return pairs;
801 }
802 
803 template<typename S, typename T>
make_DirectMap(const std::vector<std::pair<S,T>> & v)804 std::map<S, T> make_DirectMap(const std::vector<std::pair<S, T>>& v) {
805     std::map<S, T> result(v.begin(), v.end());
806     LOG_ALWAYS_FATAL_IF(result.size() != v.size(), "Duplicate key elements detected");
807     return result;
808 }
809 
810 template<typename S, typename T>
make_DirectMap(const std::vector<std::pair<S,T>> & v1,const std::vector<std::pair<S,T>> & v2)811 std::map<S, T> make_DirectMap(
812         const std::vector<std::pair<S, T>>& v1, const std::vector<std::pair<S, T>>& v2) {
813     std::map<S, T> result(v1.begin(), v1.end());
814     LOG_ALWAYS_FATAL_IF(result.size() != v1.size(), "Duplicate key elements detected in v1");
815     result.insert(v2.begin(), v2.end());
816     LOG_ALWAYS_FATAL_IF(result.size() != v1.size() + v2.size(),
817             "Duplicate key elements detected in v1+v2");
818     return result;
819 }
820 
821 template<typename S, typename T>
make_ReverseMap(const std::vector<std::pair<S,T>> & v)822 std::map<T, S> make_ReverseMap(const std::vector<std::pair<S, T>>& v) {
823     std::map<T, S> result;
824     std::transform(v.begin(), v.end(), std::inserter(result, result.begin()),
825             [](const std::pair<S, T>& p) {
826                 return std::make_pair(p.second, p.first);
827             });
828     LOG_ALWAYS_FATAL_IF(result.size() != v.size(), "Duplicate key elements detected");
829     return result;
830 }
831 
832 }  // namespace
833 
aidl2legacy_AudioChannelLayout_layout_audio_channel_mask_t_bits(int aidlLayout,bool isInput)834 audio_channel_mask_t aidl2legacy_AudioChannelLayout_layout_audio_channel_mask_t_bits(
835         int aidlLayout, bool isInput) {
836     auto& bitMapping = isInput ? getInAudioChannelBits() : getOutAudioChannelBits();
837     const int aidlLayoutInitial = aidlLayout; // for error message
838     audio_channel_mask_t legacy = AUDIO_CHANNEL_NONE;
839     for (const auto& bitPair : bitMapping) {
840         if ((aidlLayout & bitPair.second) == bitPair.second) {
841             legacy = static_cast<audio_channel_mask_t>(legacy | bitPair.first);
842             aidlLayout &= ~bitPair.second;
843             if (aidlLayout == 0) {
844                 return legacy;
845             }
846         }
847     }
848     ALOGE("%s: aidl layout 0x%x contains bits 0x%x that have no match to legacy %s bits",
849             __func__, aidlLayoutInitial, aidlLayout, isInput ? "input" : "output");
850     return AUDIO_CHANNEL_NONE;
851 }
852 
aidl2legacy_AudioChannelLayout_audio_channel_mask_t(const AudioChannelLayout & aidl,bool isInput)853 ConversionResult<audio_channel_mask_t> aidl2legacy_AudioChannelLayout_audio_channel_mask_t(
854         const AudioChannelLayout& aidl, bool isInput) {
855     using ReverseMap = std::map<AudioChannelLayout, audio_channel_mask_t>;
856     using Tag = AudioChannelLayout::Tag;
857     static const ReverseMap mIn = make_ReverseMap(getInAudioChannelPairs());
858     static const ReverseMap mOut = make_ReverseMap(getOutAudioChannelPairs());
859     static const ReverseMap mVoice = make_ReverseMap(getVoiceAudioChannelPairs());
860 
861     auto convert = [](const AudioChannelLayout& aidl, const ReverseMap& m,
862             const char* func, const char* type) -> ConversionResult<audio_channel_mask_t> {
863         if (auto it = m.find(aidl); it != m.end()) {
864             return it->second;
865         } else {
866             ALOGW("%s: no legacy %s audio_channel_mask_t found for %s", func, type,
867                     aidl.toString().c_str());
868             return unexpected(BAD_VALUE);
869         }
870     };
871 
872     switch (aidl.getTag()) {
873         case Tag::none:
874             return AUDIO_CHANNEL_NONE;
875         case Tag::invalid:
876             return AUDIO_CHANNEL_INVALID;
877         case Tag::indexMask:
878             // Index masks do not have pre-defined values.
879             if (const int bits = aidl.get<Tag::indexMask>();
880                 __builtin_popcount(bits) != 0 &&
881                 __builtin_popcount(bits) <= (int)AUDIO_CHANNEL_COUNT_MAX) {
882                 return audio_channel_mask_from_representation_and_bits(
883                         AUDIO_CHANNEL_REPRESENTATION_INDEX, bits);
884             } else {
885                 ALOGE("%s: invalid indexMask value 0x%x in %s",
886                         __func__, bits, aidl.toString().c_str());
887                 return unexpected(BAD_VALUE);
888             }
889         case Tag::layoutMask:
890             // The fast path is to find a direct match for some known layout mask.
891             if (const auto layoutMatch = convert(aidl, isInput ? mIn : mOut, __func__,
892                     isInput ? "input" : "output");
893                     layoutMatch.ok()) {
894                 return layoutMatch;
895             }
896             // If a match for a predefined layout wasn't found, make a custom one from bits.
897             if (audio_channel_mask_t bitMask =
898                     aidl2legacy_AudioChannelLayout_layout_audio_channel_mask_t_bits(
899                             aidl.get<Tag::layoutMask>(), isInput);
900                     bitMask != AUDIO_CHANNEL_NONE) {
901                 return bitMask;
902             }
903             return unexpected(BAD_VALUE);
904         case Tag::voiceMask:
905             return convert(aidl, mVoice, __func__, "voice");
906     }
907     ALOGE("%s: unexpected tag value %d", __func__, static_cast<int>(aidl.getTag()));
908     return unexpected(BAD_VALUE);
909 }
910 
legacy2aidl_audio_channel_mask_t_bits_AudioChannelLayout_layout(audio_channel_mask_t legacy,bool isInput)911 int legacy2aidl_audio_channel_mask_t_bits_AudioChannelLayout_layout(
912         audio_channel_mask_t legacy, bool isInput) {
913     auto& bitMapping = isInput ? getInAudioChannelBits() : getOutAudioChannelBits();
914     const int legacyInitial = legacy; // for error message
915     int aidlLayout = 0;
916     for (const auto& bitPair : bitMapping) {
917         if ((legacy & bitPair.first) == bitPair.first) {
918             aidlLayout |= bitPair.second;
919             legacy = static_cast<audio_channel_mask_t>(legacy & ~bitPair.first);
920             if (legacy == 0) {
921                 return aidlLayout;
922             }
923         }
924     }
925     ALOGE("%s: legacy %s audio_channel_mask_t 0x%x contains unrecognized bits 0x%x",
926             __func__, isInput ? "input" : "output", legacyInitial, legacy);
927     return 0;
928 }
929 
legacy2aidl_audio_channel_mask_t_AudioChannelLayout(audio_channel_mask_t legacy,bool isInput)930 ConversionResult<AudioChannelLayout> legacy2aidl_audio_channel_mask_t_AudioChannelLayout(
931         audio_channel_mask_t legacy, bool isInput) {
932     using DirectMap = std::map<audio_channel_mask_t, AudioChannelLayout>;
933     using Tag = AudioChannelLayout::Tag;
934     static const DirectMap mInAndVoice = make_DirectMap(
935             getInAudioChannelPairs(), getVoiceAudioChannelPairs());
936     static const DirectMap mOut = make_DirectMap(getOutAudioChannelPairs());
937 
938     auto convert = [](const audio_channel_mask_t legacy, const DirectMap& m,
939             const char* func, const char* type) -> ConversionResult<AudioChannelLayout> {
940         if (auto it = m.find(legacy); it != m.end()) {
941             return it->second;
942         } else {
943             ALOGW("%s: no AudioChannelLayout found for legacy %s audio_channel_mask_t value 0x%x",
944                     func, type, legacy);
945             return unexpected(BAD_VALUE);
946         }
947     };
948 
949     if (legacy == AUDIO_CHANNEL_NONE) {
950         return AudioChannelLayout{};
951     } else if (legacy == AUDIO_CHANNEL_INVALID) {
952         return AudioChannelLayout::make<Tag::invalid>(0);
953     }
954 
955     const audio_channel_representation_t repr = audio_channel_mask_get_representation(legacy);
956     if (repr == AUDIO_CHANNEL_REPRESENTATION_INDEX) {
957         if (audio_channel_mask_is_valid(legacy)) {
958             const int indexMask = VALUE_OR_RETURN(
959                     convertIntegral<int>(audio_channel_mask_get_bits(legacy)));
960             return AudioChannelLayout::make<Tag::indexMask>(indexMask);
961         } else {
962             ALOGE("%s: legacy audio_channel_mask_t value 0x%x is invalid", __func__, legacy);
963             return unexpected(BAD_VALUE);
964         }
965     } else if (repr == AUDIO_CHANNEL_REPRESENTATION_POSITION) {
966         // The fast path is to find a direct match for some known layout mask.
967         if (const auto layoutMatch = convert(legacy, isInput ? mInAndVoice : mOut, __func__,
968                 isInput ? "input / voice" : "output");
969                 layoutMatch.ok()) {
970             return layoutMatch;
971         }
972         // If a match for a predefined layout wasn't found, make a custom one from bits,
973         // rejecting those with voice channel bits.
974         if (!isInput ||
975                 (legacy & (AUDIO_CHANNEL_IN_VOICE_UPLINK | AUDIO_CHANNEL_IN_VOICE_DNLINK)) == 0) {
976             if (int bitMaskLayout =
977                     legacy2aidl_audio_channel_mask_t_bits_AudioChannelLayout_layout(
978                             legacy, isInput);
979                     bitMaskLayout != 0) {
980                 return AudioChannelLayout::make<Tag::layoutMask>(bitMaskLayout);
981             }
982         } else {
983             ALOGE("%s: legacy audio_channel_mask_t value 0x%x contains voice bits",
984                     __func__, legacy);
985         }
986         return unexpected(BAD_VALUE);
987     }
988 
989     ALOGE("%s: unknown representation %d in audio_channel_mask_t value 0x%x",
990             __func__, repr, legacy);
991     return unexpected(BAD_VALUE);
992 }
993 
aidl2legacy_AudioDeviceDescription_audio_devices_t(const AudioDeviceDescription & aidl)994 ConversionResult<audio_devices_t> aidl2legacy_AudioDeviceDescription_audio_devices_t(
995         const AudioDeviceDescription& aidl) {
996     static const std::map<AudioDeviceDescription, audio_devices_t> m =
997             make_ReverseMap(getAudioDevicePairs());
998     if (auto it = m.find(aidl); it != m.end()) {
999         return it->second;
1000     } else {
1001         ALOGE("%s: no legacy audio_devices_t found for %s", __func__, aidl.toString().c_str());
1002         return unexpected(BAD_VALUE);
1003     }
1004 }
1005 
legacy2aidl_audio_devices_t_AudioDeviceDescription(audio_devices_t legacy)1006 ConversionResult<AudioDeviceDescription> legacy2aidl_audio_devices_t_AudioDeviceDescription(
1007         audio_devices_t legacy) {
1008     static const std::map<audio_devices_t, AudioDeviceDescription> m =
1009             make_DirectMap(getAudioDevicePairs());
1010     if (auto it = m.find(legacy); it != m.end()) {
1011         return it->second;
1012     } else {
1013         ALOGE("%s: no AudioDeviceDescription found for legacy audio_devices_t value 0x%x",
1014                 __func__, legacy);
1015         return unexpected(BAD_VALUE);
1016     }
1017 }
1018 
suggestDeviceAddressTag(const AudioDeviceDescription & description)1019 AudioDeviceAddress::Tag suggestDeviceAddressTag(const AudioDeviceDescription& description) {
1020     using Tag = AudioDeviceAddress::Tag;
1021     if (std::string connection = description.connection;
1022             connection == GET_DEVICE_DESC_CONNECTION(BT_A2DP) ||
1023             // Note: BT LE Broadcast uses a "group id".
1024             (description.type != AudioDeviceType::OUT_BROADCAST &&
1025                     connection == GET_DEVICE_DESC_CONNECTION(BT_LE)) ||
1026             connection == GET_DEVICE_DESC_CONNECTION(BT_SCO) ||
1027             connection == GET_DEVICE_DESC_CONNECTION(WIRELESS)) {
1028         return Tag::mac;
1029     } else if (connection == GET_DEVICE_DESC_CONNECTION(IP_V4)) {
1030         return Tag::ipv4;
1031     } else if (connection == GET_DEVICE_DESC_CONNECTION(USB)) {
1032         return Tag::alsa;
1033     }
1034     return Tag::id;
1035 }
1036 
aidl2legacy_AudioDevice_audio_device(const AudioDevice & aidl,audio_devices_t * legacyType,char * legacyAddress)1037 ::android::status_t aidl2legacy_AudioDevice_audio_device(
1038         const AudioDevice& aidl,
1039         audio_devices_t* legacyType, char* legacyAddress) {
1040     std::string stringAddress;
1041     RETURN_STATUS_IF_ERROR(aidl2legacy_AudioDevice_audio_device(
1042                     aidl, legacyType, &stringAddress));
1043     return aidl2legacy_string(stringAddress, legacyAddress, AUDIO_DEVICE_MAX_ADDRESS_LEN);
1044 }
1045 
aidl2legacy_AudioDevice_audio_device(const AudioDevice & aidl,audio_devices_t * legacyType,String8 * legacyAddress)1046 ::android::status_t aidl2legacy_AudioDevice_audio_device(
1047         const AudioDevice& aidl,
1048         audio_devices_t* legacyType, String8* legacyAddress) {
1049     std::string stringAddress;
1050     RETURN_STATUS_IF_ERROR(aidl2legacy_AudioDevice_audio_device(
1051                     aidl, legacyType, &stringAddress));
1052     *legacyAddress = VALUE_OR_RETURN_STATUS(aidl2legacy_string_view_String8(stringAddress));
1053     return OK;
1054 }
1055 
aidl2legacy_AudioDevice_audio_device(const AudioDevice & aidl,audio_devices_t * legacyType,std::string * legacyAddress)1056 ::android::status_t aidl2legacy_AudioDevice_audio_device(
1057         const AudioDevice& aidl,
1058         audio_devices_t* legacyType, std::string* legacyAddress) {
1059     using Tag = AudioDeviceAddress::Tag;
1060     *legacyType = VALUE_OR_RETURN_STATUS(
1061             aidl2legacy_AudioDeviceDescription_audio_devices_t(aidl.type));
1062     char addressBuffer[AUDIO_DEVICE_MAX_ADDRESS_LEN]{};
1063     // 'aidl.address' can be empty even when the connection type is not.
1064     // This happens for device ports that act as "blueprints". In this case
1065     // we pass an empty string using the 'id' variant.
1066     switch (aidl.address.getTag()) {
1067         case Tag::mac: {
1068             const std::vector<uint8_t>& mac = aidl.address.get<AudioDeviceAddress::mac>();
1069             if (mac.size() != 6) return BAD_VALUE;
1070             snprintf(addressBuffer, AUDIO_DEVICE_MAX_ADDRESS_LEN, "%02X:%02X:%02X:%02X:%02X:%02X",
1071                     mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
1072         } break;
1073         case Tag::ipv4: {
1074             const std::vector<uint8_t>& ipv4 = aidl.address.get<AudioDeviceAddress::ipv4>();
1075             if (ipv4.size() != 4) return BAD_VALUE;
1076             snprintf(addressBuffer, AUDIO_DEVICE_MAX_ADDRESS_LEN, "%u.%u.%u.%u",
1077                     ipv4[0], ipv4[1], ipv4[2], ipv4[3]);
1078         } break;
1079         case Tag::ipv6: {
1080             const std::vector<int32_t>& ipv6 = aidl.address.get<AudioDeviceAddress::ipv6>();
1081             if (ipv6.size() != 8) return BAD_VALUE;
1082 // FIXME: Code warning found by clang-r510928
1083 #pragma clang diagnostic push
1084 #pragma clang diagnostic ignored "-Wfortify-source"
1085             snprintf(addressBuffer, AUDIO_DEVICE_MAX_ADDRESS_LEN,
1086                     "%04X:%04X:%04X:%04X:%04X:%04X:%04X:%04X",
1087                     ipv6[0], ipv6[1], ipv6[2], ipv6[3], ipv6[4], ipv6[5], ipv6[6], ipv6[7]);
1088 #pragma clang diagnostic pop
1089         } break;
1090         case Tag::alsa: {
1091             const std::vector<int32_t>& alsa = aidl.address.get<AudioDeviceAddress::alsa>();
1092             if (alsa.size() != 2) return BAD_VALUE;
1093             snprintf(addressBuffer, AUDIO_DEVICE_MAX_ADDRESS_LEN, "card=%d;device=%d",
1094                     alsa[0], alsa[1]);
1095         } break;
1096         case Tag::id: {
1097             RETURN_STATUS_IF_ERROR(aidl2legacy_string(aidl.address.get<AudioDeviceAddress::id>(),
1098                             addressBuffer, AUDIO_DEVICE_MAX_ADDRESS_LEN));
1099         } break;
1100     }
1101     *legacyAddress = addressBuffer;
1102     return OK;
1103 }
1104 
legacy2aidl_audio_device_AudioDevice(audio_devices_t legacyType,const char * legacyAddress)1105 ConversionResult<AudioDevice> legacy2aidl_audio_device_AudioDevice(
1106         audio_devices_t legacyType, const char* legacyAddress) {
1107     const std::string stringAddress = VALUE_OR_RETURN(
1108             legacy2aidl_string(legacyAddress, AUDIO_DEVICE_MAX_ADDRESS_LEN));
1109     return legacy2aidl_audio_device_AudioDevice(legacyType, stringAddress);
1110 }
1111 
1112 ConversionResult<AudioDevice>
legacy2aidl_audio_device_AudioDevice(audio_devices_t legacyType,const String8 & legacyAddress)1113 legacy2aidl_audio_device_AudioDevice(
1114         audio_devices_t legacyType, const String8& legacyAddress) {
1115     const std::string stringAddress = VALUE_OR_RETURN(legacy2aidl_String8_string(legacyAddress));
1116     return legacy2aidl_audio_device_AudioDevice(legacyType, stringAddress);
1117 }
1118 
1119 ConversionResult<AudioDevice>
legacy2aidl_audio_device_AudioDevice(audio_devices_t legacyType,const std::string & legacyAddress)1120 legacy2aidl_audio_device_AudioDevice(
1121         audio_devices_t legacyType, const std::string& legacyAddress) {
1122     using Tag = AudioDeviceAddress::Tag;
1123     AudioDevice aidl;
1124     aidl.type = VALUE_OR_RETURN(
1125             legacy2aidl_audio_devices_t_AudioDeviceDescription(legacyType));
1126     // 'legacyAddress' can be empty even when the connection type is not.
1127     // This happens for device ports that act as "blueprints". In this case
1128     // we pass an empty string using the 'id' variant.
1129     if (!legacyAddress.empty()) {
1130         switch (suggestDeviceAddressTag(aidl.type)) {
1131             case Tag::mac: {
1132                 std::vector<uint8_t> mac(6);
1133                 int status = sscanf(legacyAddress.c_str(), "%hhX:%hhX:%hhX:%hhX:%hhX:%hhX",
1134                         &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]);
1135                 if (status != mac.size()) {
1136                     ALOGE("%s: malformed MAC address: \"%s\"", __func__, legacyAddress.c_str());
1137                     return unexpected(BAD_VALUE);
1138                 }
1139                 aidl.address = AudioDeviceAddress::make<AudioDeviceAddress::mac>(std::move(mac));
1140             } break;
1141             case Tag::ipv4: {
1142                 std::vector<uint8_t> ipv4(4);
1143                 int status = sscanf(legacyAddress.c_str(), "%hhu.%hhu.%hhu.%hhu",
1144                         &ipv4[0], &ipv4[1], &ipv4[2], &ipv4[3]);
1145                 if (status != ipv4.size()) {
1146                     ALOGE("%s: malformed IPv4 address: \"%s\"", __func__, legacyAddress.c_str());
1147                     return unexpected(BAD_VALUE);
1148                 }
1149                 aidl.address = AudioDeviceAddress::make<AudioDeviceAddress::ipv4>(std::move(ipv4));
1150             } break;
1151             case Tag::ipv6: {
1152                 std::vector<int32_t> ipv6(8);
1153                 int status = sscanf(legacyAddress.c_str(), "%X:%X:%X:%X:%X:%X:%X:%X",
1154                         &ipv6[0], &ipv6[1], &ipv6[2], &ipv6[3], &ipv6[4], &ipv6[5], &ipv6[6],
1155                         &ipv6[7]);
1156                 if (status != ipv6.size()) {
1157                     ALOGE("%s: malformed IPv6 address: \"%s\"", __func__, legacyAddress.c_str());
1158                     return unexpected(BAD_VALUE);
1159                 }
1160                 aidl.address = AudioDeviceAddress::make<AudioDeviceAddress::ipv6>(std::move(ipv6));
1161             } break;
1162             case Tag::alsa: {
1163                 std::vector<int32_t> alsa(2);
1164                 int status = sscanf(legacyAddress.c_str(), "card=%d;device=%d", &alsa[0], &alsa[1]);
1165                 if (status != alsa.size()) {
1166                     ALOGE("%s: malformed ALSA address: \"%s\"", __func__, legacyAddress.c_str());
1167                     return unexpected(BAD_VALUE);
1168                 }
1169                 aidl.address = AudioDeviceAddress::make<AudioDeviceAddress::alsa>(std::move(alsa));
1170             } break;
1171             case Tag::id: {
1172                 aidl.address = AudioDeviceAddress::make<AudioDeviceAddress::id>(legacyAddress);
1173             } break;
1174         }
1175     } else {
1176         aidl.address = AudioDeviceAddress::make<AudioDeviceAddress::id>(legacyAddress);
1177     }
1178     return aidl;
1179 }
1180 
aidl2legacy_AudioFormatDescription_audio_format_t(const AudioFormatDescription & aidl)1181 ConversionResult<audio_format_t> aidl2legacy_AudioFormatDescription_audio_format_t(
1182         const AudioFormatDescription& aidl) {
1183     static const std::map<AudioFormatDescription, audio_format_t> m =
1184             make_ReverseMap(getAudioFormatPairs());
1185     if (auto it = m.find(aidl); it != m.end()) {
1186         return it->second;
1187     } else {
1188         ALOGE("%s: no legacy audio_format_t found for %s", __func__, aidl.toString().c_str());
1189         return unexpected(BAD_VALUE);
1190     }
1191 }
1192 
legacy2aidl_audio_format_t_AudioFormatDescription(audio_format_t legacy)1193 ConversionResult<AudioFormatDescription> legacy2aidl_audio_format_t_AudioFormatDescription(
1194         audio_format_t legacy) {
1195     static const std::map<audio_format_t, AudioFormatDescription> m =
1196             make_DirectMap(getAudioFormatPairs());
1197     if (auto it = m.find(legacy); it != m.end()) {
1198         return it->second;
1199     } else {
1200         ALOGE("%s: no AudioFormatDescription found for legacy audio_format_t value 0x%x",
1201                 __func__, legacy);
1202         return unexpected(BAD_VALUE);
1203     }
1204 }
1205 
aidl2legacy_AudioGainMode_audio_gain_mode_t(AudioGainMode aidl)1206 ConversionResult<audio_gain_mode_t> aidl2legacy_AudioGainMode_audio_gain_mode_t(
1207         AudioGainMode aidl) {
1208     switch (aidl) {
1209         case AudioGainMode::JOINT:
1210             return AUDIO_GAIN_MODE_JOINT;
1211         case AudioGainMode::CHANNELS:
1212             return AUDIO_GAIN_MODE_CHANNELS;
1213         case AudioGainMode::RAMP:
1214             return AUDIO_GAIN_MODE_RAMP;
1215     }
1216     return unexpected(BAD_VALUE);
1217 }
1218 
legacy2aidl_audio_gain_mode_t_AudioGainMode(audio_gain_mode_t legacy)1219 ConversionResult<AudioGainMode> legacy2aidl_audio_gain_mode_t_AudioGainMode(
1220         audio_gain_mode_t legacy) {
1221     switch (legacy) {
1222         case AUDIO_GAIN_MODE_JOINT:
1223             return AudioGainMode::JOINT;
1224         case AUDIO_GAIN_MODE_CHANNELS:
1225             return AudioGainMode::CHANNELS;
1226         case AUDIO_GAIN_MODE_RAMP:
1227             return AudioGainMode::RAMP;
1228     }
1229     return unexpected(BAD_VALUE);
1230 }
1231 
aidl2legacy_int32_t_audio_gain_mode_t_mask(int32_t aidl)1232 ConversionResult<audio_gain_mode_t> aidl2legacy_int32_t_audio_gain_mode_t_mask(int32_t aidl) {
1233     return convertBitmask<audio_gain_mode_t, int32_t, audio_gain_mode_t, AudioGainMode>(
1234             aidl, aidl2legacy_AudioGainMode_audio_gain_mode_t,
1235             // AudioGainMode is index-based.
1236             indexToEnum_index<AudioGainMode>,
1237             // AUDIO_GAIN_MODE_* constants are mask-based.
1238             enumToMask_bitmask<audio_gain_mode_t, audio_gain_mode_t>);
1239 }
1240 
legacy2aidl_audio_gain_mode_t_int32_t_mask(audio_gain_mode_t legacy)1241 ConversionResult<int32_t> legacy2aidl_audio_gain_mode_t_int32_t_mask(audio_gain_mode_t legacy) {
1242     return convertBitmask<int32_t, audio_gain_mode_t, AudioGainMode, audio_gain_mode_t>(
1243             legacy, legacy2aidl_audio_gain_mode_t_AudioGainMode,
1244             // AUDIO_GAIN_MODE_* constants are mask-based.
1245             indexToEnum_bitmask<audio_gain_mode_t>,
1246             // AudioGainMode is index-based.
1247             enumToMask_index<int32_t, AudioGainMode>);
1248 }
1249 
aidl2legacy_AudioGainConfig_audio_gain_config(const AudioGainConfig & aidl,bool isInput)1250 ConversionResult<audio_gain_config> aidl2legacy_AudioGainConfig_audio_gain_config(
1251         const AudioGainConfig& aidl, bool isInput) {
1252     audio_gain_config legacy;
1253     legacy.index = VALUE_OR_RETURN(convertIntegral<int>(aidl.index));
1254     legacy.mode = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_gain_mode_t_mask(aidl.mode));
1255     legacy.channel_mask = VALUE_OR_RETURN(
1256             aidl2legacy_AudioChannelLayout_audio_channel_mask_t(aidl.channelMask, isInput));
1257     const bool isJoint = bitmaskIsSet(aidl.mode, AudioGainMode::JOINT);
1258     size_t numValues = isJoint ? 1
1259                                : isInput ? audio_channel_count_from_in_mask(legacy.channel_mask)
1260                                          : audio_channel_count_from_out_mask(legacy.channel_mask);
1261     if (aidl.values.size() != numValues || aidl.values.size() > std::size(legacy.values)) {
1262         return unexpected(BAD_VALUE);
1263     }
1264     for (size_t i = 0; i < numValues; ++i) {
1265         legacy.values[i] = VALUE_OR_RETURN(convertIntegral<int>(aidl.values[i]));
1266     }
1267     legacy.ramp_duration_ms = VALUE_OR_RETURN(convertIntegral<int>(aidl.rampDurationMs));
1268     return legacy;
1269 }
1270 
legacy2aidl_audio_gain_config_AudioGainConfig(const audio_gain_config & legacy,bool isInput)1271 ConversionResult<AudioGainConfig> legacy2aidl_audio_gain_config_AudioGainConfig(
1272         const audio_gain_config& legacy, bool isInput) {
1273     AudioGainConfig aidl;
1274     aidl.index = VALUE_OR_RETURN(convertIntegral<int32_t>(legacy.index));
1275     aidl.mode = VALUE_OR_RETURN(legacy2aidl_audio_gain_mode_t_int32_t_mask(legacy.mode));
1276     aidl.channelMask = VALUE_OR_RETURN(
1277             legacy2aidl_audio_channel_mask_t_AudioChannelLayout(legacy.channel_mask, isInput));
1278     const bool isJoint = (legacy.mode & AUDIO_GAIN_MODE_JOINT) != 0;
1279     size_t numValues = isJoint ? 1
1280                                : isInput ? audio_channel_count_from_in_mask(legacy.channel_mask)
1281                                          : audio_channel_count_from_out_mask(legacy.channel_mask);
1282     aidl.values.resize(numValues);
1283     for (size_t i = 0; i < numValues; ++i) {
1284         aidl.values[i] = VALUE_OR_RETURN(convertIntegral<int32_t>(legacy.values[i]));
1285     }
1286     aidl.rampDurationMs = VALUE_OR_RETURN(convertIntegral<int32_t>(legacy.ramp_duration_ms));
1287     return aidl;
1288 }
1289 
aidl2legacy_AudioInputFlags_audio_input_flags_t(AudioInputFlags aidl)1290 ConversionResult<audio_input_flags_t> aidl2legacy_AudioInputFlags_audio_input_flags_t(
1291         AudioInputFlags aidl) {
1292     switch (aidl) {
1293         case AudioInputFlags::FAST:
1294             return AUDIO_INPUT_FLAG_FAST;
1295         case AudioInputFlags::HW_HOTWORD:
1296             return AUDIO_INPUT_FLAG_HW_HOTWORD;
1297         case AudioInputFlags::RAW:
1298             return AUDIO_INPUT_FLAG_RAW;
1299         case AudioInputFlags::SYNC:
1300             return AUDIO_INPUT_FLAG_SYNC;
1301         case AudioInputFlags::MMAP_NOIRQ:
1302             return AUDIO_INPUT_FLAG_MMAP_NOIRQ;
1303         case AudioInputFlags::VOIP_TX:
1304             return AUDIO_INPUT_FLAG_VOIP_TX;
1305         case AudioInputFlags::HW_AV_SYNC:
1306             return AUDIO_INPUT_FLAG_HW_AV_SYNC;
1307         case AudioInputFlags::DIRECT:
1308             return AUDIO_INPUT_FLAG_DIRECT;
1309         case AudioInputFlags::ULTRASOUND:
1310             return AUDIO_INPUT_FLAG_ULTRASOUND;
1311         case AudioInputFlags::HOTWORD_TAP:
1312             return AUDIO_INPUT_FLAG_HOTWORD_TAP;
1313         case AudioInputFlags::HW_LOOKBACK:
1314             return AUDIO_INPUT_FLAG_HW_LOOKBACK;
1315     }
1316     return unexpected(BAD_VALUE);
1317 }
1318 
legacy2aidl_audio_input_flags_t_AudioInputFlags(audio_input_flags_t legacy)1319 ConversionResult<AudioInputFlags> legacy2aidl_audio_input_flags_t_AudioInputFlags(
1320         audio_input_flags_t legacy) {
1321     switch (legacy) {
1322         case AUDIO_INPUT_FLAG_NONE:
1323             break; // shouldn't get here. must be listed  -Werror,-Wswitch
1324         case AUDIO_INPUT_FLAG_FAST:
1325             return AudioInputFlags::FAST;
1326         case AUDIO_INPUT_FLAG_HW_HOTWORD:
1327             return AudioInputFlags::HW_HOTWORD;
1328         case AUDIO_INPUT_FLAG_RAW:
1329             return AudioInputFlags::RAW;
1330         case AUDIO_INPUT_FLAG_SYNC:
1331             return AudioInputFlags::SYNC;
1332         case AUDIO_INPUT_FLAG_MMAP_NOIRQ:
1333             return AudioInputFlags::MMAP_NOIRQ;
1334         case AUDIO_INPUT_FLAG_VOIP_TX:
1335             return AudioInputFlags::VOIP_TX;
1336         case AUDIO_INPUT_FLAG_HW_AV_SYNC:
1337             return AudioInputFlags::HW_AV_SYNC;
1338         case AUDIO_INPUT_FLAG_DIRECT:
1339             return AudioInputFlags::DIRECT;
1340         case AUDIO_INPUT_FLAG_ULTRASOUND:
1341             return AudioInputFlags::ULTRASOUND;
1342         case AUDIO_INPUT_FLAG_HOTWORD_TAP:
1343             return AudioInputFlags::HOTWORD_TAP;
1344         case AUDIO_INPUT_FLAG_HW_LOOKBACK:
1345             return AudioInputFlags::HW_LOOKBACK;
1346     }
1347     return unexpected(BAD_VALUE);
1348 }
1349 
aidl2legacy_AudioOutputFlags_audio_output_flags_t(AudioOutputFlags aidl)1350 ConversionResult<audio_output_flags_t> aidl2legacy_AudioOutputFlags_audio_output_flags_t(
1351         AudioOutputFlags aidl) {
1352     switch (aidl) {
1353         case AudioOutputFlags::DIRECT:
1354             return AUDIO_OUTPUT_FLAG_DIRECT;
1355         case AudioOutputFlags::PRIMARY:
1356             return AUDIO_OUTPUT_FLAG_PRIMARY;
1357         case AudioOutputFlags::FAST:
1358             return AUDIO_OUTPUT_FLAG_FAST;
1359         case AudioOutputFlags::DEEP_BUFFER:
1360             return AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1361         case AudioOutputFlags::COMPRESS_OFFLOAD:
1362             return AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD;
1363         case AudioOutputFlags::NON_BLOCKING:
1364             return AUDIO_OUTPUT_FLAG_NON_BLOCKING;
1365         case AudioOutputFlags::HW_AV_SYNC:
1366             return AUDIO_OUTPUT_FLAG_HW_AV_SYNC;
1367         case AudioOutputFlags::TTS:
1368             return AUDIO_OUTPUT_FLAG_TTS;
1369         case AudioOutputFlags::RAW:
1370             return AUDIO_OUTPUT_FLAG_RAW;
1371         case AudioOutputFlags::SYNC:
1372             return AUDIO_OUTPUT_FLAG_SYNC;
1373         case AudioOutputFlags::IEC958_NONAUDIO:
1374             return AUDIO_OUTPUT_FLAG_IEC958_NONAUDIO;
1375         case AudioOutputFlags::DIRECT_PCM:
1376             return AUDIO_OUTPUT_FLAG_DIRECT_PCM;
1377         case AudioOutputFlags::MMAP_NOIRQ:
1378             return AUDIO_OUTPUT_FLAG_MMAP_NOIRQ;
1379         case AudioOutputFlags::VOIP_RX:
1380             return AUDIO_OUTPUT_FLAG_VOIP_RX;
1381         case AudioOutputFlags::INCALL_MUSIC:
1382             return AUDIO_OUTPUT_FLAG_INCALL_MUSIC;
1383         case AudioOutputFlags::GAPLESS_OFFLOAD:
1384             return AUDIO_OUTPUT_FLAG_GAPLESS_OFFLOAD;
1385         case AudioOutputFlags::ULTRASOUND:
1386             return AUDIO_OUTPUT_FLAG_ULTRASOUND;
1387         case AudioOutputFlags::SPATIALIZER:
1388             return AUDIO_OUTPUT_FLAG_SPATIALIZER;
1389         case AudioOutputFlags::BIT_PERFECT:
1390             return AUDIO_OUTPUT_FLAG_BIT_PERFECT;
1391     }
1392     return unexpected(BAD_VALUE);
1393 }
1394 
legacy2aidl_audio_output_flags_t_AudioOutputFlags(audio_output_flags_t legacy)1395 ConversionResult<AudioOutputFlags> legacy2aidl_audio_output_flags_t_AudioOutputFlags(
1396         audio_output_flags_t legacy) {
1397     switch (legacy) {
1398         case AUDIO_OUTPUT_FLAG_NONE:
1399             break; // shouldn't get here. must be listed  -Werror,-Wswitch
1400         case AUDIO_OUTPUT_FLAG_DIRECT:
1401             return AudioOutputFlags::DIRECT;
1402         case AUDIO_OUTPUT_FLAG_PRIMARY:
1403             return AudioOutputFlags::PRIMARY;
1404         case AUDIO_OUTPUT_FLAG_FAST:
1405             return AudioOutputFlags::FAST;
1406         case AUDIO_OUTPUT_FLAG_DEEP_BUFFER:
1407             return AudioOutputFlags::DEEP_BUFFER;
1408         case AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD:
1409             return AudioOutputFlags::COMPRESS_OFFLOAD;
1410         case AUDIO_OUTPUT_FLAG_NON_BLOCKING:
1411             return AudioOutputFlags::NON_BLOCKING;
1412         case AUDIO_OUTPUT_FLAG_HW_AV_SYNC:
1413             return AudioOutputFlags::HW_AV_SYNC;
1414         case AUDIO_OUTPUT_FLAG_TTS:
1415             return AudioOutputFlags::TTS;
1416         case AUDIO_OUTPUT_FLAG_RAW:
1417             return AudioOutputFlags::RAW;
1418         case AUDIO_OUTPUT_FLAG_SYNC:
1419             return AudioOutputFlags::SYNC;
1420         case AUDIO_OUTPUT_FLAG_IEC958_NONAUDIO:
1421             return AudioOutputFlags::IEC958_NONAUDIO;
1422         case AUDIO_OUTPUT_FLAG_DIRECT_PCM:
1423             return AudioOutputFlags::DIRECT_PCM;
1424         case AUDIO_OUTPUT_FLAG_MMAP_NOIRQ:
1425             return AudioOutputFlags::MMAP_NOIRQ;
1426         case AUDIO_OUTPUT_FLAG_VOIP_RX:
1427             return AudioOutputFlags::VOIP_RX;
1428         case AUDIO_OUTPUT_FLAG_INCALL_MUSIC:
1429             return AudioOutputFlags::INCALL_MUSIC;
1430         case AUDIO_OUTPUT_FLAG_GAPLESS_OFFLOAD:
1431             return AudioOutputFlags::GAPLESS_OFFLOAD;
1432         case AUDIO_OUTPUT_FLAG_ULTRASOUND:
1433             return AudioOutputFlags::ULTRASOUND;
1434         case AUDIO_OUTPUT_FLAG_SPATIALIZER:
1435             return AudioOutputFlags::SPATIALIZER;
1436         case AUDIO_OUTPUT_FLAG_BIT_PERFECT:
1437             return AudioOutputFlags::BIT_PERFECT;
1438     }
1439     return unexpected(BAD_VALUE);
1440 }
1441 
aidl2legacy_int32_t_audio_input_flags_t_mask(int32_t aidl)1442 ConversionResult<audio_input_flags_t> aidl2legacy_int32_t_audio_input_flags_t_mask(
1443         int32_t aidl) {
1444     using LegacyMask = std::underlying_type_t<audio_input_flags_t>;
1445 
1446     LegacyMask converted = VALUE_OR_RETURN(
1447             (convertBitmask<LegacyMask, int32_t, audio_input_flags_t, AudioInputFlags>(
1448                     aidl, aidl2legacy_AudioInputFlags_audio_input_flags_t,
1449                     indexToEnum_index<AudioInputFlags>,
1450                     enumToMask_bitmask<LegacyMask, audio_input_flags_t>)));
1451     return static_cast<audio_input_flags_t>(converted);
1452 }
1453 
legacy2aidl_audio_input_flags_t_int32_t_mask(audio_input_flags_t legacy)1454 ConversionResult<int32_t> legacy2aidl_audio_input_flags_t_int32_t_mask(
1455         audio_input_flags_t legacy) {
1456     using LegacyMask = std::underlying_type_t<audio_input_flags_t>;
1457 
1458     LegacyMask legacyMask = static_cast<LegacyMask>(legacy);
1459     return convertBitmask<int32_t, LegacyMask, AudioInputFlags, audio_input_flags_t>(
1460             legacyMask, legacy2aidl_audio_input_flags_t_AudioInputFlags,
1461             indexToEnum_bitmask<audio_input_flags_t>,
1462             enumToMask_index<int32_t, AudioInputFlags>);
1463 }
1464 
aidl2legacy_int32_t_audio_output_flags_t_mask(int32_t aidl)1465 ConversionResult<audio_output_flags_t> aidl2legacy_int32_t_audio_output_flags_t_mask(
1466         int32_t aidl) {
1467     return convertBitmask<audio_output_flags_t,
1468             int32_t,
1469             audio_output_flags_t,
1470             AudioOutputFlags>(
1471             aidl, aidl2legacy_AudioOutputFlags_audio_output_flags_t,
1472             indexToEnum_index<AudioOutputFlags>,
1473             enumToMask_bitmask<audio_output_flags_t, audio_output_flags_t>);
1474 }
1475 
legacy2aidl_audio_output_flags_t_int32_t_mask(audio_output_flags_t legacy)1476 ConversionResult<int32_t> legacy2aidl_audio_output_flags_t_int32_t_mask(
1477         audio_output_flags_t legacy) {
1478     using LegacyMask = std::underlying_type_t<audio_output_flags_t>;
1479 
1480     LegacyMask legacyMask = static_cast<LegacyMask>(legacy);
1481     return convertBitmask<int32_t, LegacyMask, AudioOutputFlags, audio_output_flags_t>(
1482             legacyMask, legacy2aidl_audio_output_flags_t_AudioOutputFlags,
1483             indexToEnum_bitmask<audio_output_flags_t>,
1484             enumToMask_index<int32_t, AudioOutputFlags>);
1485 }
1486 
aidl2legacy_AudioIoFlags_audio_io_flags(const AudioIoFlags & aidl,bool isInput)1487 ConversionResult<audio_io_flags> aidl2legacy_AudioIoFlags_audio_io_flags(
1488         const AudioIoFlags& aidl, bool isInput) {
1489     audio_io_flags legacy;
1490     if (isInput) {
1491         legacy.input = VALUE_OR_RETURN(
1492                 aidl2legacy_int32_t_audio_input_flags_t_mask(
1493                         VALUE_OR_RETURN(UNION_GET(aidl, input))));
1494     } else {
1495         legacy.output = VALUE_OR_RETURN(
1496                 aidl2legacy_int32_t_audio_output_flags_t_mask(
1497                         VALUE_OR_RETURN(UNION_GET(aidl, output))));
1498     }
1499     return legacy;
1500 }
1501 
legacy2aidl_audio_io_flags_AudioIoFlags(const audio_io_flags & legacy,bool isInput)1502 ConversionResult<AudioIoFlags> legacy2aidl_audio_io_flags_AudioIoFlags(
1503         const audio_io_flags& legacy, bool isInput) {
1504     AudioIoFlags aidl;
1505     if (isInput) {
1506         UNION_SET(aidl, input,
1507                 VALUE_OR_RETURN(legacy2aidl_audio_input_flags_t_int32_t_mask(legacy.input)));
1508     } else {
1509         UNION_SET(aidl, output,
1510                 VALUE_OR_RETURN(legacy2aidl_audio_output_flags_t_int32_t_mask(legacy.output)));
1511     }
1512     return aidl;
1513 }
1514 
aidl2legacy_AudioStreamType_audio_stream_type_t(AudioStreamType aidl)1515 ConversionResult<audio_stream_type_t> aidl2legacy_AudioStreamType_audio_stream_type_t(
1516         AudioStreamType aidl) {
1517     switch (aidl) {
1518         case AudioStreamType::INVALID:
1519             break;  // return error
1520         case AudioStreamType::SYS_RESERVED_DEFAULT:
1521             return AUDIO_STREAM_DEFAULT;
1522         case AudioStreamType::VOICE_CALL:
1523             return AUDIO_STREAM_VOICE_CALL;
1524         case AudioStreamType::SYSTEM:
1525             return AUDIO_STREAM_SYSTEM;
1526         case AudioStreamType::RING:
1527             return AUDIO_STREAM_RING;
1528         case AudioStreamType::MUSIC:
1529             return AUDIO_STREAM_MUSIC;
1530         case AudioStreamType::ALARM:
1531             return AUDIO_STREAM_ALARM;
1532         case AudioStreamType::NOTIFICATION:
1533             return AUDIO_STREAM_NOTIFICATION;
1534         case AudioStreamType::BLUETOOTH_SCO:
1535             return AUDIO_STREAM_BLUETOOTH_SCO;
1536         case AudioStreamType::ENFORCED_AUDIBLE:
1537             return AUDIO_STREAM_ENFORCED_AUDIBLE;
1538         case AudioStreamType::DTMF:
1539             return AUDIO_STREAM_DTMF;
1540         case AudioStreamType::TTS:
1541             return AUDIO_STREAM_TTS;
1542         case AudioStreamType::ACCESSIBILITY:
1543             return AUDIO_STREAM_ACCESSIBILITY;
1544         case AudioStreamType::ASSISTANT:
1545             return AUDIO_STREAM_ASSISTANT;
1546         case AudioStreamType::SYS_RESERVED_REROUTING:
1547             return AUDIO_STREAM_REROUTING;
1548         case AudioStreamType::SYS_RESERVED_PATCH:
1549             return AUDIO_STREAM_PATCH;
1550         case AudioStreamType::CALL_ASSISTANT:
1551             return AUDIO_STREAM_CALL_ASSISTANT;
1552     }
1553     return unexpected(BAD_VALUE);
1554 }
1555 
legacy2aidl_audio_stream_type_t_AudioStreamType(audio_stream_type_t legacy)1556 ConversionResult<AudioStreamType> legacy2aidl_audio_stream_type_t_AudioStreamType(
1557         audio_stream_type_t legacy) {
1558     switch (legacy) {
1559         case AUDIO_STREAM_DEFAULT:
1560             return AudioStreamType::SYS_RESERVED_DEFAULT;
1561         case AUDIO_STREAM_VOICE_CALL:
1562             return AudioStreamType::VOICE_CALL;
1563         case AUDIO_STREAM_SYSTEM:
1564             return AudioStreamType::SYSTEM;
1565         case AUDIO_STREAM_RING:
1566             return AudioStreamType::RING;
1567         case AUDIO_STREAM_MUSIC:
1568             return AudioStreamType::MUSIC;
1569         case AUDIO_STREAM_ALARM:
1570             return AudioStreamType::ALARM;
1571         case AUDIO_STREAM_NOTIFICATION:
1572             return AudioStreamType::NOTIFICATION;
1573         case AUDIO_STREAM_BLUETOOTH_SCO:
1574             return AudioStreamType::BLUETOOTH_SCO;
1575         case AUDIO_STREAM_ENFORCED_AUDIBLE:
1576             return AudioStreamType::ENFORCED_AUDIBLE;
1577         case AUDIO_STREAM_DTMF:
1578             return AudioStreamType::DTMF;
1579         case AUDIO_STREAM_TTS:
1580             return AudioStreamType::TTS;
1581         case AUDIO_STREAM_ACCESSIBILITY:
1582             return AudioStreamType::ACCESSIBILITY;
1583         case AUDIO_STREAM_ASSISTANT:
1584             return AudioStreamType::ASSISTANT;
1585         case AUDIO_STREAM_REROUTING:
1586             return AudioStreamType::SYS_RESERVED_REROUTING;
1587         case AUDIO_STREAM_PATCH:
1588             return AudioStreamType::SYS_RESERVED_PATCH;
1589         case AUDIO_STREAM_CALL_ASSISTANT:
1590             return AudioStreamType::CALL_ASSISTANT;
1591     }
1592     return unexpected(BAD_VALUE);
1593 }
1594 
aidl2legacy_AudioSource_audio_source_t(AudioSource aidl)1595 ConversionResult<audio_source_t> aidl2legacy_AudioSource_audio_source_t(
1596         AudioSource aidl) {
1597     switch (aidl) {
1598         case AudioSource::SYS_RESERVED_INVALID:
1599             return AUDIO_SOURCE_INVALID;
1600         case AudioSource::DEFAULT:
1601             return AUDIO_SOURCE_DEFAULT;
1602         case AudioSource::MIC:
1603             return AUDIO_SOURCE_MIC;
1604         case AudioSource::VOICE_UPLINK:
1605             return AUDIO_SOURCE_VOICE_UPLINK;
1606         case AudioSource::VOICE_DOWNLINK:
1607             return AUDIO_SOURCE_VOICE_DOWNLINK;
1608         case AudioSource::VOICE_CALL:
1609             return AUDIO_SOURCE_VOICE_CALL;
1610         case AudioSource::CAMCORDER:
1611             return AUDIO_SOURCE_CAMCORDER;
1612         case AudioSource::VOICE_RECOGNITION:
1613             return AUDIO_SOURCE_VOICE_RECOGNITION;
1614         case AudioSource::VOICE_COMMUNICATION:
1615             return AUDIO_SOURCE_VOICE_COMMUNICATION;
1616         case AudioSource::REMOTE_SUBMIX:
1617             return AUDIO_SOURCE_REMOTE_SUBMIX;
1618         case AudioSource::UNPROCESSED:
1619             return AUDIO_SOURCE_UNPROCESSED;
1620         case AudioSource::VOICE_PERFORMANCE:
1621             return AUDIO_SOURCE_VOICE_PERFORMANCE;
1622         case AudioSource::ULTRASOUND:
1623             return AUDIO_SOURCE_ULTRASOUND;
1624         case AudioSource::ECHO_REFERENCE:
1625             return AUDIO_SOURCE_ECHO_REFERENCE;
1626         case AudioSource::FM_TUNER:
1627             return AUDIO_SOURCE_FM_TUNER;
1628         case AudioSource::HOTWORD:
1629             return AUDIO_SOURCE_HOTWORD;
1630     }
1631     return unexpected(BAD_VALUE);
1632 }
1633 
legacy2aidl_audio_source_t_AudioSource(audio_source_t legacy)1634 ConversionResult<AudioSource> legacy2aidl_audio_source_t_AudioSource(
1635         audio_source_t legacy) {
1636     switch (legacy) {
1637         case AUDIO_SOURCE_INVALID:
1638             return AudioSource::SYS_RESERVED_INVALID;
1639         case AUDIO_SOURCE_DEFAULT:
1640             return AudioSource::DEFAULT;
1641         case AUDIO_SOURCE_MIC:
1642             return AudioSource::MIC;
1643         case AUDIO_SOURCE_VOICE_UPLINK:
1644             return AudioSource::VOICE_UPLINK;
1645         case AUDIO_SOURCE_VOICE_DOWNLINK:
1646             return AudioSource::VOICE_DOWNLINK;
1647         case AUDIO_SOURCE_VOICE_CALL:
1648             return AudioSource::VOICE_CALL;
1649         case AUDIO_SOURCE_CAMCORDER:
1650             return AudioSource::CAMCORDER;
1651         case AUDIO_SOURCE_VOICE_RECOGNITION:
1652             return AudioSource::VOICE_RECOGNITION;
1653         case AUDIO_SOURCE_VOICE_COMMUNICATION:
1654             return AudioSource::VOICE_COMMUNICATION;
1655         case AUDIO_SOURCE_REMOTE_SUBMIX:
1656             return AudioSource::REMOTE_SUBMIX;
1657         case AUDIO_SOURCE_UNPROCESSED:
1658             return AudioSource::UNPROCESSED;
1659         case AUDIO_SOURCE_VOICE_PERFORMANCE:
1660             return AudioSource::VOICE_PERFORMANCE;
1661         case AUDIO_SOURCE_ULTRASOUND:
1662             return AudioSource::ULTRASOUND;
1663         case AUDIO_SOURCE_ECHO_REFERENCE:
1664             return AudioSource::ECHO_REFERENCE;
1665         case AUDIO_SOURCE_FM_TUNER:
1666             return AudioSource::FM_TUNER;
1667         case AUDIO_SOURCE_HOTWORD:
1668             return AudioSource::HOTWORD;
1669     }
1670     return unexpected(BAD_VALUE);
1671 }
1672 
aidl2legacy_int32_t_audio_session_t(int32_t aidl)1673 ConversionResult<audio_session_t> aidl2legacy_int32_t_audio_session_t(int32_t aidl) {
1674     return convertReinterpret<audio_session_t>(aidl);
1675 }
1676 
legacy2aidl_audio_session_t_int32_t(audio_session_t legacy)1677 ConversionResult<int32_t> legacy2aidl_audio_session_t_int32_t(audio_session_t legacy) {
1678     return convertReinterpret<int32_t>(legacy);
1679 }
1680 
1681 ConversionResult<audio_content_type_t>
aidl2legacy_AudioContentType_audio_content_type_t(AudioContentType aidl)1682 aidl2legacy_AudioContentType_audio_content_type_t(AudioContentType aidl) {
1683     switch (aidl) {
1684         case AudioContentType::UNKNOWN:
1685             return AUDIO_CONTENT_TYPE_UNKNOWN;
1686         case AudioContentType::SPEECH:
1687             return AUDIO_CONTENT_TYPE_SPEECH;
1688         case AudioContentType::MUSIC:
1689             return AUDIO_CONTENT_TYPE_MUSIC;
1690         case AudioContentType::MOVIE:
1691             return AUDIO_CONTENT_TYPE_MOVIE;
1692         case AudioContentType::SONIFICATION:
1693             return AUDIO_CONTENT_TYPE_SONIFICATION;
1694         case AudioContentType::ULTRASOUND:
1695             return AUDIO_CONTENT_TYPE_ULTRASOUND;
1696     }
1697     return unexpected(BAD_VALUE);
1698 }
1699 
1700 ConversionResult<AudioContentType>
legacy2aidl_audio_content_type_t_AudioContentType(audio_content_type_t legacy)1701 legacy2aidl_audio_content_type_t_AudioContentType(audio_content_type_t legacy) {
1702     switch (legacy) {
1703         case AUDIO_CONTENT_TYPE_UNKNOWN:
1704             return AudioContentType::UNKNOWN;
1705         case AUDIO_CONTENT_TYPE_SPEECH:
1706             return AudioContentType::SPEECH;
1707         case AUDIO_CONTENT_TYPE_MUSIC:
1708             return AudioContentType::MUSIC;
1709         case AUDIO_CONTENT_TYPE_MOVIE:
1710             return AudioContentType::MOVIE;
1711         case AUDIO_CONTENT_TYPE_SONIFICATION:
1712             return AudioContentType::SONIFICATION;
1713         case AUDIO_CONTENT_TYPE_ULTRASOUND:
1714             return AudioContentType::ULTRASOUND;
1715     }
1716     return unexpected(BAD_VALUE);
1717 }
1718 
1719 ConversionResult<audio_usage_t>
aidl2legacy_AudioUsage_audio_usage_t(AudioUsage aidl)1720 aidl2legacy_AudioUsage_audio_usage_t(AudioUsage aidl) {
1721     switch (aidl) {
1722         case AudioUsage::INVALID:
1723             break;  // return error
1724         case AudioUsage::UNKNOWN:
1725             return AUDIO_USAGE_UNKNOWN;
1726         case AudioUsage::MEDIA:
1727             return AUDIO_USAGE_MEDIA;
1728         case AudioUsage::VOICE_COMMUNICATION:
1729             return AUDIO_USAGE_VOICE_COMMUNICATION;
1730         case AudioUsage::VOICE_COMMUNICATION_SIGNALLING:
1731             return AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING;
1732         case AudioUsage::ALARM:
1733             return AUDIO_USAGE_ALARM;
1734         case AudioUsage::NOTIFICATION:
1735             return AUDIO_USAGE_NOTIFICATION;
1736         case AudioUsage::NOTIFICATION_TELEPHONY_RINGTONE:
1737             return AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE;
1738         case AudioUsage::SYS_RESERVED_NOTIFICATION_COMMUNICATION_REQUEST:
1739             return AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST;
1740         case AudioUsage::SYS_RESERVED_NOTIFICATION_COMMUNICATION_INSTANT:
1741             return AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT;
1742         case AudioUsage::SYS_RESERVED_NOTIFICATION_COMMUNICATION_DELAYED:
1743             return AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED;
1744         case AudioUsage::NOTIFICATION_EVENT:
1745             return AUDIO_USAGE_NOTIFICATION_EVENT;
1746         case AudioUsage::ASSISTANCE_ACCESSIBILITY:
1747             return AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY;
1748         case AudioUsage::ASSISTANCE_NAVIGATION_GUIDANCE:
1749             return AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE;
1750         case AudioUsage::ASSISTANCE_SONIFICATION:
1751             return AUDIO_USAGE_ASSISTANCE_SONIFICATION;
1752         case AudioUsage::GAME:
1753             return AUDIO_USAGE_GAME;
1754         case AudioUsage::VIRTUAL_SOURCE:
1755             return AUDIO_USAGE_VIRTUAL_SOURCE;
1756         case AudioUsage::ASSISTANT:
1757             return AUDIO_USAGE_ASSISTANT;
1758         case AudioUsage::CALL_ASSISTANT:
1759             return AUDIO_USAGE_CALL_ASSISTANT;
1760         case AudioUsage::EMERGENCY:
1761             return AUDIO_USAGE_EMERGENCY;
1762         case AudioUsage::SAFETY:
1763             return AUDIO_USAGE_SAFETY;
1764         case AudioUsage::VEHICLE_STATUS:
1765             return AUDIO_USAGE_VEHICLE_STATUS;
1766         case AudioUsage::ANNOUNCEMENT:
1767             return AUDIO_USAGE_ANNOUNCEMENT;
1768     }
1769     return unexpected(BAD_VALUE);
1770 }
1771 
1772 ConversionResult<AudioUsage>
legacy2aidl_audio_usage_t_AudioUsage(audio_usage_t legacy)1773 legacy2aidl_audio_usage_t_AudioUsage(audio_usage_t legacy) {
1774     switch (legacy) {
1775         case AUDIO_USAGE_UNKNOWN:
1776             return AudioUsage::UNKNOWN;
1777         case AUDIO_USAGE_MEDIA:
1778             return AudioUsage::MEDIA;
1779         case AUDIO_USAGE_VOICE_COMMUNICATION:
1780             return AudioUsage::VOICE_COMMUNICATION;
1781         case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
1782             return AudioUsage::VOICE_COMMUNICATION_SIGNALLING;
1783         case AUDIO_USAGE_ALARM:
1784             return AudioUsage::ALARM;
1785         case AUDIO_USAGE_NOTIFICATION:
1786             return AudioUsage::NOTIFICATION;
1787         case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
1788             return AudioUsage::NOTIFICATION_TELEPHONY_RINGTONE;
1789         case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
1790             return AudioUsage::SYS_RESERVED_NOTIFICATION_COMMUNICATION_REQUEST;
1791         case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
1792             return AudioUsage::SYS_RESERVED_NOTIFICATION_COMMUNICATION_INSTANT;
1793         case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
1794             return AudioUsage::SYS_RESERVED_NOTIFICATION_COMMUNICATION_DELAYED;
1795         case AUDIO_USAGE_NOTIFICATION_EVENT:
1796             return AudioUsage::NOTIFICATION_EVENT;
1797         case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
1798             return AudioUsage::ASSISTANCE_ACCESSIBILITY;
1799         case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
1800             return AudioUsage::ASSISTANCE_NAVIGATION_GUIDANCE;
1801         case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
1802             return AudioUsage::ASSISTANCE_SONIFICATION;
1803         case AUDIO_USAGE_GAME:
1804             return AudioUsage::GAME;
1805         case AUDIO_USAGE_VIRTUAL_SOURCE:
1806             return AudioUsage::VIRTUAL_SOURCE;
1807         case AUDIO_USAGE_ASSISTANT:
1808             return AudioUsage::ASSISTANT;
1809         case AUDIO_USAGE_CALL_ASSISTANT:
1810             return AudioUsage::CALL_ASSISTANT;
1811         case AUDIO_USAGE_EMERGENCY:
1812             return AudioUsage::EMERGENCY;
1813         case AUDIO_USAGE_SAFETY:
1814             return AudioUsage::SAFETY;
1815         case AUDIO_USAGE_VEHICLE_STATUS:
1816             return AudioUsage::VEHICLE_STATUS;
1817         case AUDIO_USAGE_ANNOUNCEMENT:
1818             return AudioUsage::ANNOUNCEMENT;
1819     }
1820     return unexpected(BAD_VALUE);
1821 }
1822 
1823 ConversionResult<audio_flags_mask_t>
aidl2legacy_AudioFlag_audio_flags_mask_t(AudioFlag aidl)1824 aidl2legacy_AudioFlag_audio_flags_mask_t(AudioFlag aidl) {
1825     switch (aidl) {
1826         case AudioFlag::NONE:
1827             return AUDIO_FLAG_NONE;
1828         case AudioFlag::AUDIBILITY_ENFORCED:
1829             return AUDIO_FLAG_AUDIBILITY_ENFORCED;
1830         // The is no AudioFlag::SECURE, see the comment in the AudioFlag.aidl
1831         //  return AUDIO_FLAG_SECURE;
1832         case AudioFlag::SCO:
1833             return AUDIO_FLAG_SCO;
1834         case AudioFlag::BEACON:
1835             return AUDIO_FLAG_BEACON;
1836         case AudioFlag::HW_AV_SYNC:
1837             return AUDIO_FLAG_HW_AV_SYNC;
1838         case AudioFlag::HW_HOTWORD:
1839             return AUDIO_FLAG_HW_HOTWORD;
1840         case AudioFlag::BYPASS_INTERRUPTION_POLICY:
1841             return AUDIO_FLAG_BYPASS_INTERRUPTION_POLICY;
1842         case AudioFlag::BYPASS_MUTE:
1843             return AUDIO_FLAG_BYPASS_MUTE;
1844         case AudioFlag::LOW_LATENCY:
1845             return AUDIO_FLAG_LOW_LATENCY;
1846         case AudioFlag::DEEP_BUFFER:
1847             return AUDIO_FLAG_DEEP_BUFFER;
1848         case AudioFlag::NO_MEDIA_PROJECTION:
1849             return AUDIO_FLAG_NO_MEDIA_PROJECTION;
1850         case AudioFlag::MUTE_HAPTIC:
1851             return AUDIO_FLAG_MUTE_HAPTIC;
1852         case AudioFlag::NO_SYSTEM_CAPTURE:
1853             return AUDIO_FLAG_NO_SYSTEM_CAPTURE;
1854         case AudioFlag::CAPTURE_PRIVATE:
1855             return AUDIO_FLAG_CAPTURE_PRIVATE;
1856         case AudioFlag::CONTENT_SPATIALIZED:
1857             return AUDIO_FLAG_CONTENT_SPATIALIZED;
1858         case AudioFlag::NEVER_SPATIALIZE:
1859             return AUDIO_FLAG_NEVER_SPATIALIZE;
1860         case AudioFlag::CALL_REDIRECTION:
1861             return AUDIO_FLAG_CALL_REDIRECTION;
1862     }
1863     return unexpected(BAD_VALUE);
1864 }
1865 
1866 ConversionResult<AudioFlag>
legacy2aidl_audio_flags_mask_t_AudioFlag(audio_flags_mask_t legacy)1867 legacy2aidl_audio_flags_mask_t_AudioFlag(audio_flags_mask_t legacy) {
1868     switch (legacy) {
1869         case AUDIO_FLAG_NONE:
1870             return AudioFlag::NONE;
1871         case AUDIO_FLAG_AUDIBILITY_ENFORCED:
1872             return AudioFlag::AUDIBILITY_ENFORCED;
1873         case AUDIO_FLAG_SECURE:
1874             return unexpected(BAD_VALUE);
1875         case AUDIO_FLAG_SCO:
1876             return AudioFlag::SCO;
1877         case AUDIO_FLAG_BEACON:
1878             return AudioFlag::BEACON;
1879         case AUDIO_FLAG_HW_AV_SYNC:
1880             return AudioFlag::HW_AV_SYNC;
1881         case AUDIO_FLAG_HW_HOTWORD:
1882             return AudioFlag::HW_HOTWORD;
1883         case AUDIO_FLAG_BYPASS_INTERRUPTION_POLICY:
1884             return AudioFlag::BYPASS_INTERRUPTION_POLICY;
1885         case AUDIO_FLAG_BYPASS_MUTE:
1886             return AudioFlag::BYPASS_MUTE;
1887         case AUDIO_FLAG_LOW_LATENCY:
1888             return AudioFlag::LOW_LATENCY;
1889         case AUDIO_FLAG_DEEP_BUFFER:
1890             return AudioFlag::DEEP_BUFFER;
1891         case AUDIO_FLAG_NO_MEDIA_PROJECTION:
1892             return AudioFlag::NO_MEDIA_PROJECTION;
1893         case AUDIO_FLAG_MUTE_HAPTIC:
1894             return AudioFlag::MUTE_HAPTIC;
1895         case AUDIO_FLAG_NO_SYSTEM_CAPTURE:
1896             return AudioFlag::NO_SYSTEM_CAPTURE;
1897         case AUDIO_FLAG_CAPTURE_PRIVATE:
1898             return AudioFlag::CAPTURE_PRIVATE;
1899         case AUDIO_FLAG_CONTENT_SPATIALIZED:
1900             return AudioFlag::CONTENT_SPATIALIZED;
1901         case AUDIO_FLAG_NEVER_SPATIALIZE:
1902             return AudioFlag::NEVER_SPATIALIZE;
1903         case AUDIO_FLAG_CALL_REDIRECTION:
1904             return AudioFlag::CALL_REDIRECTION;
1905     }
1906     return unexpected(BAD_VALUE);
1907 }
1908 
1909 ConversionResult<audio_flags_mask_t>
aidl2legacy_int32_t_audio_flags_mask_t_mask(int32_t aidl)1910 aidl2legacy_int32_t_audio_flags_mask_t_mask(int32_t aidl) {
1911     return convertBitmask<audio_flags_mask_t, int32_t, audio_flags_mask_t, AudioFlag>(
1912             aidl, aidl2legacy_AudioFlag_audio_flags_mask_t, indexToEnum_bitmask<AudioFlag>,
1913             enumToMask_bitmask<audio_flags_mask_t, audio_flags_mask_t>);
1914 }
1915 
1916 ConversionResult<int32_t>
legacy2aidl_audio_flags_mask_t_int32_t_mask(audio_flags_mask_t legacy)1917 legacy2aidl_audio_flags_mask_t_int32_t_mask(audio_flags_mask_t legacy) {
1918     return convertBitmask<int32_t, audio_flags_mask_t, AudioFlag, audio_flags_mask_t>(
1919             legacy, legacy2aidl_audio_flags_mask_t_AudioFlag,
1920             indexToEnum_bitmask<audio_flags_mask_t>,
1921             enumToMask_bitmask<int32_t, AudioFlag>);
1922 }
1923 
1924 ConversionResult<std::string>
aidl2legacy_AudioTags_string(const std::vector<std::string> & aidl)1925 aidl2legacy_AudioTags_string(const std::vector<std::string>& aidl) {
1926     std::ostringstream tagsBuffer;
1927     bool hasValue = false;
1928     for (const auto& tag : aidl) {
1929         if (hasValue) {
1930             tagsBuffer << AUDIO_ATTRIBUTES_TAGS_SEPARATOR;
1931         }
1932         if (strchr(tag.c_str(), AUDIO_ATTRIBUTES_TAGS_SEPARATOR) == nullptr) {
1933             tagsBuffer << tag;
1934             hasValue = true;
1935         } else {
1936             ALOGE("Tag is ill-formed: \"%s\"", tag.c_str());
1937             return unexpected(BAD_VALUE);
1938         }
1939     }
1940     return tagsBuffer.str();
1941 }
1942 
1943 ConversionResult<std::vector<std::string>>
legacy2aidl_string_AudioTags(const std::string & legacy)1944 legacy2aidl_string_AudioTags(const std::string& legacy) {
1945     return splitString(legacy, AUDIO_ATTRIBUTES_TAGS_SEPARATOR);
1946 }
1947 
1948 ConversionResult<audio_attributes_t>
aidl2legacy_AudioAttributes_audio_attributes_t(const AudioAttributes & aidl)1949 aidl2legacy_AudioAttributes_audio_attributes_t(const AudioAttributes& aidl) {
1950     audio_attributes_t legacy;
1951     legacy.content_type = VALUE_OR_RETURN(
1952             aidl2legacy_AudioContentType_audio_content_type_t(aidl.contentType));
1953     legacy.usage = VALUE_OR_RETURN(aidl2legacy_AudioUsage_audio_usage_t(aidl.usage));
1954     legacy.source = VALUE_OR_RETURN(aidl2legacy_AudioSource_audio_source_t(aidl.source));
1955     legacy.flags = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_flags_mask_t_mask(aidl.flags));
1956     auto tagsString = VALUE_OR_RETURN(aidl2legacy_AudioTags_string(aidl.tags));
1957     RETURN_IF_ERROR(aidl2legacy_string(tagsString, legacy.tags, sizeof(legacy.tags)));
1958     return legacy;
1959 }
1960 
1961 ConversionResult<AudioAttributes>
legacy2aidl_audio_attributes_t_AudioAttributes(const audio_attributes_t & legacy)1962 legacy2aidl_audio_attributes_t_AudioAttributes(const audio_attributes_t& legacy) {
1963     AudioAttributes aidl;
1964     aidl.contentType = VALUE_OR_RETURN(
1965             legacy2aidl_audio_content_type_t_AudioContentType(legacy.content_type));
1966     aidl.usage = VALUE_OR_RETURN(legacy2aidl_audio_usage_t_AudioUsage(legacy.usage));
1967     aidl.source = VALUE_OR_RETURN(legacy2aidl_audio_source_t_AudioSource(legacy.source));
1968     aidl.flags = VALUE_OR_RETURN(legacy2aidl_audio_flags_mask_t_int32_t_mask(legacy.flags));
1969     auto tagsString = VALUE_OR_RETURN(legacy2aidl_string(legacy.tags, sizeof(legacy.tags)));
1970     aidl.tags = VALUE_OR_RETURN(legacy2aidl_string_AudioTags(tagsString));
1971     return aidl;
1972 }
1973 
1974 ConversionResult<audio_encapsulation_mode_t>
aidl2legacy_AudioEncapsulationMode_audio_encapsulation_mode_t(AudioEncapsulationMode aidl)1975 aidl2legacy_AudioEncapsulationMode_audio_encapsulation_mode_t(AudioEncapsulationMode aidl) {
1976     switch (aidl) {
1977         case AudioEncapsulationMode::INVALID:
1978             break;  // return error
1979         case AudioEncapsulationMode::NONE:
1980             return AUDIO_ENCAPSULATION_MODE_NONE;
1981         case AudioEncapsulationMode::ELEMENTARY_STREAM:
1982             return AUDIO_ENCAPSULATION_MODE_ELEMENTARY_STREAM;
1983         case AudioEncapsulationMode::HANDLE:
1984             return AUDIO_ENCAPSULATION_MODE_HANDLE;
1985     }
1986     return unexpected(BAD_VALUE);
1987 }
1988 
1989 ConversionResult<AudioEncapsulationMode>
legacy2aidl_audio_encapsulation_mode_t_AudioEncapsulationMode(audio_encapsulation_mode_t legacy)1990 legacy2aidl_audio_encapsulation_mode_t_AudioEncapsulationMode(audio_encapsulation_mode_t legacy) {
1991     switch (legacy) {
1992         case AUDIO_ENCAPSULATION_MODE_NONE:
1993             return AudioEncapsulationMode::NONE;
1994         case AUDIO_ENCAPSULATION_MODE_ELEMENTARY_STREAM:
1995             return AudioEncapsulationMode::ELEMENTARY_STREAM;
1996         case AUDIO_ENCAPSULATION_MODE_HANDLE:
1997             return AudioEncapsulationMode::HANDLE;
1998     }
1999     return unexpected(BAD_VALUE);
2000 }
2001 
2002 ConversionResult<audio_offload_info_t>
aidl2legacy_AudioOffloadInfo_audio_offload_info_t(const AudioOffloadInfo & aidl)2003 aidl2legacy_AudioOffloadInfo_audio_offload_info_t(const AudioOffloadInfo& aidl) {
2004     audio_offload_info_t legacy = AUDIO_INFO_INITIALIZER;
2005     audio_config_base_t base = VALUE_OR_RETURN(
2006             aidl2legacy_AudioConfigBase_audio_config_base_t(aidl.base, false /*isInput*/));
2007     legacy.sample_rate = base.sample_rate;
2008     legacy.channel_mask = base.channel_mask;
2009     legacy.format = base.format;
2010     legacy.stream_type = VALUE_OR_RETURN(
2011             aidl2legacy_AudioStreamType_audio_stream_type_t(aidl.streamType));
2012     legacy.bit_rate = VALUE_OR_RETURN(convertIntegral<int32_t>(aidl.bitRatePerSecond));
2013     legacy.duration_us = VALUE_OR_RETURN(convertIntegral<int64_t>(aidl.durationUs));
2014     legacy.has_video = aidl.hasVideo;
2015     legacy.is_streaming = aidl.isStreaming;
2016     legacy.bit_width = VALUE_OR_RETURN(convertIntegral<int32_t>(aidl.bitWidth));
2017     legacy.offload_buffer_size = VALUE_OR_RETURN(convertIntegral<int32_t>(aidl.offloadBufferSize));
2018     legacy.usage = VALUE_OR_RETURN(aidl2legacy_AudioUsage_audio_usage_t(aidl.usage));
2019     legacy.encapsulation_mode = VALUE_OR_RETURN(
2020             aidl2legacy_AudioEncapsulationMode_audio_encapsulation_mode_t(aidl.encapsulationMode));
2021     legacy.content_id = VALUE_OR_RETURN(convertReinterpret<int32_t>(aidl.contentId));
2022     legacy.sync_id = VALUE_OR_RETURN(convertReinterpret<int32_t>(aidl.syncId));
2023     return legacy;
2024 }
2025 
2026 ConversionResult<AudioOffloadInfo>
legacy2aidl_audio_offload_info_t_AudioOffloadInfo(const audio_offload_info_t & legacy)2027 legacy2aidl_audio_offload_info_t_AudioOffloadInfo(const audio_offload_info_t& legacy) {
2028     AudioOffloadInfo aidl;
2029     // Version 0.1 fields.
2030     if (legacy.size < offsetof(audio_offload_info_t, usage) + sizeof(audio_offload_info_t::usage)) {
2031         return unexpected(BAD_VALUE);
2032     }
2033     const audio_config_base_t base = { .sample_rate = legacy.sample_rate,
2034         .channel_mask = legacy.channel_mask, .format = legacy.format };
2035     aidl.base = VALUE_OR_RETURN(legacy2aidl_audio_config_base_t_AudioConfigBase(
2036                     base, false /*isInput*/));
2037     aidl.streamType = VALUE_OR_RETURN(
2038             legacy2aidl_audio_stream_type_t_AudioStreamType(legacy.stream_type));
2039     aidl.bitRatePerSecond = VALUE_OR_RETURN(convertIntegral<int32_t>(legacy.bit_rate));
2040     aidl.durationUs = VALUE_OR_RETURN(convertIntegral<int64_t>(legacy.duration_us));
2041     aidl.hasVideo = legacy.has_video;
2042     aidl.isStreaming = legacy.is_streaming;
2043     aidl.bitWidth = VALUE_OR_RETURN(convertIntegral<int32_t>(legacy.bit_width));
2044     aidl.offloadBufferSize = VALUE_OR_RETURN(convertIntegral<int32_t>(legacy.offload_buffer_size));
2045     aidl.usage = VALUE_OR_RETURN(legacy2aidl_audio_usage_t_AudioUsage(legacy.usage));
2046 
2047     // Version 0.2 fields.
2048     if (legacy.version >= AUDIO_OFFLOAD_INFO_VERSION_0_2) {
2049         if (legacy.size <
2050             offsetof(audio_offload_info_t, sync_id) + sizeof(audio_offload_info_t::sync_id)) {
2051             return unexpected(BAD_VALUE);
2052         }
2053         aidl.encapsulationMode = VALUE_OR_RETURN(
2054                 legacy2aidl_audio_encapsulation_mode_t_AudioEncapsulationMode(
2055                         legacy.encapsulation_mode));
2056         aidl.contentId = VALUE_OR_RETURN(convertReinterpret<int32_t>(legacy.content_id));
2057         aidl.syncId = VALUE_OR_RETURN(convertReinterpret<int32_t>(legacy.sync_id));
2058     }
2059     return aidl;
2060 }
2061 
portDirection(audio_port_role_t role,audio_port_type_t type)2062 ConversionResult<AudioPortDirection> portDirection(audio_port_role_t role, audio_port_type_t type) {
2063     switch (type) {
2064         case AUDIO_PORT_TYPE_NONE:
2065         case AUDIO_PORT_TYPE_SESSION:
2066             break;  // must be listed  -Werror,-Wswitch
2067         case AUDIO_PORT_TYPE_DEVICE:
2068             switch (role) {
2069                 case AUDIO_PORT_ROLE_NONE:
2070                      break;  // must be listed  -Werror,-Wswitch
2071                 case AUDIO_PORT_ROLE_SOURCE:
2072                     return AudioPortDirection::INPUT;
2073                 case AUDIO_PORT_ROLE_SINK:
2074                     return AudioPortDirection::OUTPUT;
2075             }
2076             break;
2077         case AUDIO_PORT_TYPE_MIX:
2078             switch (role) {
2079                 case AUDIO_PORT_ROLE_NONE:
2080                      break;  // must be listed  -Werror,-Wswitch
2081                 case AUDIO_PORT_ROLE_SOURCE:
2082                     return AudioPortDirection::OUTPUT;
2083                 case AUDIO_PORT_ROLE_SINK:
2084                     return AudioPortDirection::INPUT;
2085             }
2086             break;
2087     }
2088     return unexpected(BAD_VALUE);
2089 }
2090 
portRole(AudioPortDirection direction,audio_port_type_t type)2091 ConversionResult<audio_port_role_t> portRole(AudioPortDirection direction, audio_port_type_t type) {
2092     switch (type) {
2093         case AUDIO_PORT_TYPE_NONE:
2094         case AUDIO_PORT_TYPE_SESSION:
2095             break;  // must be listed  -Werror,-Wswitch
2096         case AUDIO_PORT_TYPE_DEVICE:
2097             switch (direction) {
2098                 case AudioPortDirection::INPUT:
2099                     return AUDIO_PORT_ROLE_SOURCE;
2100                 case AudioPortDirection::OUTPUT:
2101                     return AUDIO_PORT_ROLE_SINK;
2102             }
2103             break;
2104         case AUDIO_PORT_TYPE_MIX:
2105             switch (direction) {
2106                 case AudioPortDirection::OUTPUT:
2107                     return AUDIO_PORT_ROLE_SOURCE;
2108                 case AudioPortDirection::INPUT:
2109                     return AUDIO_PORT_ROLE_SINK;
2110             }
2111             break;
2112     }
2113     return unexpected(BAD_VALUE);
2114 }
2115 
2116 ConversionResult<audio_config_t>
aidl2legacy_AudioConfig_audio_config_t(const AudioConfig & aidl,bool isInput)2117 aidl2legacy_AudioConfig_audio_config_t(const AudioConfig& aidl, bool isInput) {
2118     const audio_config_base_t legacyBase = VALUE_OR_RETURN(
2119             aidl2legacy_AudioConfigBase_audio_config_base_t(aidl.base, isInput));
2120     audio_config_t legacy = AUDIO_CONFIG_INITIALIZER;
2121     legacy.sample_rate = legacyBase.sample_rate;
2122     legacy.channel_mask = legacyBase.channel_mask;
2123     legacy.format = legacyBase.format;
2124     legacy.offload_info = VALUE_OR_RETURN(
2125             aidl2legacy_AudioOffloadInfo_audio_offload_info_t(aidl.offloadInfo));
2126     legacy.frame_count = VALUE_OR_RETURN(convertIntegral<uint32_t>(aidl.frameCount));
2127     return legacy;
2128 }
2129 
2130 ConversionResult<AudioConfig>
legacy2aidl_audio_config_t_AudioConfig(const audio_config_t & legacy,bool isInput)2131 legacy2aidl_audio_config_t_AudioConfig(const audio_config_t& legacy, bool isInput) {
2132     const audio_config_base_t base = { .sample_rate = legacy.sample_rate,
2133         .channel_mask = legacy.channel_mask, .format = legacy.format };
2134     AudioConfig aidl;
2135     aidl.base = VALUE_OR_RETURN(legacy2aidl_audio_config_base_t_AudioConfigBase(base, isInput));
2136     aidl.offloadInfo = VALUE_OR_RETURN(
2137             legacy2aidl_audio_offload_info_t_AudioOffloadInfo(legacy.offload_info));
2138     aidl.frameCount = VALUE_OR_RETURN(convertIntegral<int64_t>(legacy.frame_count));
2139     return aidl;
2140 }
2141 
2142 ConversionResult<audio_config_base_t>
aidl2legacy_AudioConfigBase_audio_config_base_t(const AudioConfigBase & aidl,bool isInput)2143 aidl2legacy_AudioConfigBase_audio_config_base_t(const AudioConfigBase& aidl, bool isInput) {
2144     audio_config_base_t legacy;
2145     legacy.sample_rate = VALUE_OR_RETURN(convertIntegral<int>(aidl.sampleRate));
2146     legacy.channel_mask = VALUE_OR_RETURN(
2147             aidl2legacy_AudioChannelLayout_audio_channel_mask_t(aidl.channelMask, isInput));
2148     legacy.format = VALUE_OR_RETURN(aidl2legacy_AudioFormatDescription_audio_format_t(aidl.format));
2149     return legacy;
2150 }
2151 
2152 ConversionResult<AudioConfigBase>
legacy2aidl_audio_config_base_t_AudioConfigBase(const audio_config_base_t & legacy,bool isInput)2153 legacy2aidl_audio_config_base_t_AudioConfigBase(const audio_config_base_t& legacy, bool isInput) {
2154     AudioConfigBase aidl;
2155     aidl.sampleRate = VALUE_OR_RETURN(convertIntegral<int32_t>(legacy.sample_rate));
2156     aidl.channelMask = VALUE_OR_RETURN(
2157             legacy2aidl_audio_channel_mask_t_AudioChannelLayout(legacy.channel_mask, isInput));
2158     aidl.format = VALUE_OR_RETURN(legacy2aidl_audio_format_t_AudioFormatDescription(legacy.format));
2159     return aidl;
2160 }
2161 
2162 ConversionResult<audio_uuid_t>
aidl2legacy_AudioUuid_audio_uuid_t(const AudioUuid & aidl)2163 aidl2legacy_AudioUuid_audio_uuid_t(const AudioUuid& aidl) {
2164     audio_uuid_t legacy;
2165     legacy.timeLow = VALUE_OR_RETURN(convertReinterpret<uint32_t>(aidl.timeLow));
2166     legacy.timeMid = VALUE_OR_RETURN(convertIntegral<uint16_t>(aidl.timeMid));
2167     legacy.timeHiAndVersion = VALUE_OR_RETURN(convertIntegral<uint16_t>(aidl.timeHiAndVersion));
2168     legacy.clockSeq = VALUE_OR_RETURN(convertIntegral<uint16_t>(aidl.clockSeq));
2169     if (aidl.node.size() != std::size(legacy.node)) {
2170         return unexpected(BAD_VALUE);
2171     }
2172     std::copy(aidl.node.begin(), aidl.node.end(), legacy.node);
2173     return legacy;
2174 }
2175 
2176 ConversionResult<AudioUuid>
legacy2aidl_audio_uuid_t_AudioUuid(const audio_uuid_t & legacy)2177 legacy2aidl_audio_uuid_t_AudioUuid(const audio_uuid_t& legacy) {
2178     AudioUuid aidl;
2179     aidl.timeLow = VALUE_OR_RETURN(convertReinterpret<int32_t>(legacy.timeLow));
2180     aidl.timeMid = VALUE_OR_RETURN(convertIntegral<int32_t>(legacy.timeMid));
2181     aidl.timeHiAndVersion = VALUE_OR_RETURN(convertIntegral<int32_t>(legacy.timeHiAndVersion));
2182     aidl.clockSeq = VALUE_OR_RETURN(convertIntegral<int32_t>(legacy.clockSeq));
2183     std::copy(legacy.node, legacy.node + std::size(legacy.node), std::back_inserter(aidl.node));
2184     return aidl;
2185 }
2186 
2187 ConversionResult<audio_encapsulation_metadata_type_t>
aidl2legacy_AudioEncapsulationMetadataType_audio_encapsulation_metadata_type_t(AudioEncapsulationMetadataType aidl)2188 aidl2legacy_AudioEncapsulationMetadataType_audio_encapsulation_metadata_type_t(
2189         AudioEncapsulationMetadataType aidl) {
2190     switch (aidl) {
2191         case AudioEncapsulationMetadataType::NONE:
2192             return AUDIO_ENCAPSULATION_METADATA_TYPE_NONE;
2193         case AudioEncapsulationMetadataType::FRAMEWORK_TUNER:
2194             return AUDIO_ENCAPSULATION_METADATA_TYPE_FRAMEWORK_TUNER;
2195         case AudioEncapsulationMetadataType::DVB_AD_DESCRIPTOR:
2196             return AUDIO_ENCAPSULATION_METADATA_TYPE_DVB_AD_DESCRIPTOR;
2197     }
2198     return unexpected(BAD_VALUE);
2199 }
2200 
2201 ConversionResult<AudioEncapsulationMetadataType>
legacy2aidl_audio_encapsulation_metadata_type_t_AudioEncapsulationMetadataType(audio_encapsulation_metadata_type_t legacy)2202 legacy2aidl_audio_encapsulation_metadata_type_t_AudioEncapsulationMetadataType(
2203         audio_encapsulation_metadata_type_t legacy) {
2204     switch (legacy) {
2205         case AUDIO_ENCAPSULATION_METADATA_TYPE_NONE:
2206             return AudioEncapsulationMetadataType::NONE;
2207         case AUDIO_ENCAPSULATION_METADATA_TYPE_FRAMEWORK_TUNER:
2208             return AudioEncapsulationMetadataType::FRAMEWORK_TUNER;
2209         case AUDIO_ENCAPSULATION_METADATA_TYPE_DVB_AD_DESCRIPTOR:
2210             return AudioEncapsulationMetadataType::DVB_AD_DESCRIPTOR;
2211     }
2212     return unexpected(BAD_VALUE);
2213 }
2214 
2215 ConversionResult<uint32_t>
aidl2legacy_AudioEncapsulationMode_mask(int32_t aidl)2216 aidl2legacy_AudioEncapsulationMode_mask(int32_t aidl) {
2217     return convertBitmask<uint32_t,
2218             int32_t,
2219             audio_encapsulation_mode_t,
2220             AudioEncapsulationMode>(
2221             aidl, aidl2legacy_AudioEncapsulationMode_audio_encapsulation_mode_t,
2222             indexToEnum_index<AudioEncapsulationMode>,
2223             enumToMask_index<uint32_t, audio_encapsulation_mode_t>);
2224 }
2225 
2226 ConversionResult<int32_t>
legacy2aidl_AudioEncapsulationMode_mask(uint32_t legacy)2227 legacy2aidl_AudioEncapsulationMode_mask(uint32_t legacy) {
2228     return convertBitmask<int32_t,
2229             uint32_t,
2230             AudioEncapsulationMode,
2231             audio_encapsulation_mode_t>(
2232             legacy, legacy2aidl_audio_encapsulation_mode_t_AudioEncapsulationMode,
2233             indexToEnum_index<audio_encapsulation_mode_t>,
2234             enumToMask_index<int32_t, AudioEncapsulationMode>);
2235 }
2236 
2237 ConversionResult<uint32_t>
aidl2legacy_AudioEncapsulationMetadataType_mask(int32_t aidl)2238 aidl2legacy_AudioEncapsulationMetadataType_mask(int32_t aidl) {
2239     return convertBitmask<uint32_t,
2240             int32_t,
2241             audio_encapsulation_metadata_type_t,
2242             AudioEncapsulationMetadataType>(
2243             aidl, aidl2legacy_AudioEncapsulationMetadataType_audio_encapsulation_metadata_type_t,
2244             indexToEnum_index<AudioEncapsulationMetadataType>,
2245             enumToMask_index<uint32_t, audio_encapsulation_metadata_type_t>);
2246 }
2247 
2248 ConversionResult<int32_t>
legacy2aidl_AudioEncapsulationMetadataType_mask(uint32_t legacy)2249 legacy2aidl_AudioEncapsulationMetadataType_mask(uint32_t legacy) {
2250     return convertBitmask<int32_t,
2251             uint32_t,
2252             AudioEncapsulationMetadataType,
2253             audio_encapsulation_metadata_type_t>(
2254             legacy, legacy2aidl_audio_encapsulation_metadata_type_t_AudioEncapsulationMetadataType,
2255             indexToEnum_index<audio_encapsulation_metadata_type_t>,
2256             enumToMask_index<int32_t, AudioEncapsulationMetadataType>);
2257 }
2258 
2259 ConversionResult<audio_port_config_mix_ext_usecase>
aidl2legacy_AudioPortMixExtUseCase_audio_port_config_mix_ext_usecase(const AudioPortMixExtUseCase & aidl,bool isInput)2260 aidl2legacy_AudioPortMixExtUseCase_audio_port_config_mix_ext_usecase(
2261         const AudioPortMixExtUseCase& aidl, bool isInput) {
2262     audio_port_config_mix_ext_usecase legacy{};
2263     if (aidl.getTag() != AudioPortMixExtUseCase::Tag::unspecified) {
2264         if (!isInput) {
2265             legacy.stream = VALUE_OR_RETURN(aidl2legacy_AudioStreamType_audio_stream_type_t(
2266                             VALUE_OR_RETURN(UNION_GET(aidl, stream))));
2267         } else {
2268             legacy.source = VALUE_OR_RETURN(aidl2legacy_AudioSource_audio_source_t(
2269                             VALUE_OR_RETURN(UNION_GET(aidl, source))));
2270         }
2271     }
2272     return legacy;
2273 }
2274 
2275 ConversionResult<AudioPortMixExtUseCase>
legacy2aidl_audio_port_config_mix_ext_usecase_AudioPortMixExtUseCase(const audio_port_config_mix_ext_usecase & legacy,bool isInput)2276 legacy2aidl_audio_port_config_mix_ext_usecase_AudioPortMixExtUseCase(
2277         const audio_port_config_mix_ext_usecase& legacy, bool isInput) {
2278     AudioPortMixExtUseCase aidl;
2279     if (!isInput) {
2280         UNION_SET(aidl, stream, VALUE_OR_RETURN(
2281                         legacy2aidl_audio_stream_type_t_AudioStreamType(legacy.stream)));
2282     } else {
2283         UNION_SET(aidl, source, VALUE_OR_RETURN(
2284                         legacy2aidl_audio_source_t_AudioSource(legacy.source)));
2285     }
2286     return aidl;
2287 }
2288 
aidl2legacy_AudioPortMixExt_audio_port_config_mix_ext(const AudioPortMixExt & aidl,bool isInput)2289 ConversionResult<audio_port_config_mix_ext> aidl2legacy_AudioPortMixExt_audio_port_config_mix_ext(
2290         const AudioPortMixExt& aidl, bool isInput) {
2291     audio_port_config_mix_ext legacy{};
2292     legacy.handle = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_io_handle_t(aidl.handle));
2293     legacy.usecase = VALUE_OR_RETURN(
2294             aidl2legacy_AudioPortMixExtUseCase_audio_port_config_mix_ext_usecase(
2295                     aidl.usecase, isInput));
2296     return legacy;
2297 }
2298 
legacy2aidl_audio_port_config_mix_ext_AudioPortMixExt(const audio_port_config_mix_ext & legacy,bool isInput)2299 ConversionResult<AudioPortMixExt> legacy2aidl_audio_port_config_mix_ext_AudioPortMixExt(
2300         const audio_port_config_mix_ext& legacy, bool isInput) {
2301     AudioPortMixExt aidl;
2302     aidl.handle = VALUE_OR_RETURN(legacy2aidl_audio_io_handle_t_int32_t(legacy.handle));
2303     aidl.usecase = VALUE_OR_RETURN(
2304             legacy2aidl_audio_port_config_mix_ext_usecase_AudioPortMixExtUseCase(
2305                     legacy.usecase, isInput));
2306     return aidl;
2307 }
2308 
2309 ConversionResult<audio_port_config_device_ext>
aidl2legacy_AudioPortDeviceExt_audio_port_config_device_ext(const AudioPortDeviceExt & aidl)2310 aidl2legacy_AudioPortDeviceExt_audio_port_config_device_ext(const AudioPortDeviceExt& aidl) {
2311     audio_port_config_device_ext legacy{};
2312     RETURN_IF_ERROR(aidl2legacy_AudioDevice_audio_device(
2313                     aidl.device, &legacy.type, legacy.address));
2314     return legacy;
2315 }
2316 
legacy2aidl_audio_port_config_device_ext_AudioPortDeviceExt(const audio_port_config_device_ext & legacy)2317 ConversionResult<AudioPortDeviceExt> legacy2aidl_audio_port_config_device_ext_AudioPortDeviceExt(
2318         const audio_port_config_device_ext& legacy) {
2319     AudioPortDeviceExt aidl;
2320     aidl.device = VALUE_OR_RETURN(
2321             legacy2aidl_audio_device_AudioDevice(legacy.type, legacy.address));
2322     return aidl;
2323 }
2324 
2325 // This type is unnamed in the original definition, thus we name it here.
2326 using audio_port_config_ext = decltype(audio_port_config::ext);
2327 
aidl2legacy_AudioPortExt_audio_port_config_ext(const AudioPortExt & aidl,bool isInput,audio_port_config_ext * legacy,audio_port_type_t * type)2328 status_t aidl2legacy_AudioPortExt_audio_port_config_ext(
2329         const AudioPortExt& aidl, bool isInput,
2330         audio_port_config_ext* legacy, audio_port_type_t* type) {
2331     switch (aidl.getTag()) {
2332         case AudioPortExt::Tag::unspecified:
2333             // Just verify that the union is empty.
2334             VALUE_OR_RETURN_STATUS(UNION_GET(aidl, unspecified));
2335             *legacy = {};
2336             *type = AUDIO_PORT_TYPE_NONE;
2337             return OK;
2338         case AudioPortExt::Tag::device:
2339             legacy->device = VALUE_OR_RETURN_STATUS(
2340                     aidl2legacy_AudioPortDeviceExt_audio_port_config_device_ext(
2341                             VALUE_OR_RETURN_STATUS(UNION_GET(aidl, device))));
2342             *type = AUDIO_PORT_TYPE_DEVICE;
2343             return OK;
2344         case AudioPortExt::Tag::mix:
2345             legacy->mix = VALUE_OR_RETURN_STATUS(
2346                     aidl2legacy_AudioPortMixExt_audio_port_config_mix_ext(
2347                             VALUE_OR_RETURN_STATUS(UNION_GET(aidl, mix)), isInput));
2348             *type = AUDIO_PORT_TYPE_MIX;
2349             return OK;
2350         case AudioPortExt::Tag::session:
2351             // This variant is not used in the HAL scenario.
2352             legacy->session.session = AUDIO_SESSION_NONE;
2353             *type = AUDIO_PORT_TYPE_SESSION;
2354             return OK;
2355 
2356     }
2357     LOG_ALWAYS_FATAL("Shouldn't get here"); // with -Werror,-Wswitch may compile-time fail
2358 }
2359 
legacy2aidl_audio_port_config_ext_AudioPortExt(const audio_port_config_ext & legacy,audio_port_type_t type,bool isInput)2360 ConversionResult<AudioPortExt> legacy2aidl_audio_port_config_ext_AudioPortExt(
2361         const audio_port_config_ext& legacy, audio_port_type_t type, bool isInput) {
2362     AudioPortExt aidl;
2363     switch (type) {
2364         case AUDIO_PORT_TYPE_NONE:
2365             UNION_SET(aidl, unspecified, false);
2366             return aidl;
2367         case AUDIO_PORT_TYPE_DEVICE: {
2368             AudioPortDeviceExt device = VALUE_OR_RETURN(
2369                     legacy2aidl_audio_port_config_device_ext_AudioPortDeviceExt(legacy.device));
2370             UNION_SET(aidl, device, device);
2371             return aidl;
2372         }
2373         case AUDIO_PORT_TYPE_MIX: {
2374             AudioPortMixExt mix = VALUE_OR_RETURN(
2375                     legacy2aidl_audio_port_config_mix_ext_AudioPortMixExt(legacy.mix, isInput));
2376             UNION_SET(aidl, mix, mix);
2377             return aidl;
2378         }
2379         case AUDIO_PORT_TYPE_SESSION:
2380             // This variant is not used in the HAL scenario.
2381             UNION_SET(aidl, unspecified, false);
2382             return aidl;
2383     }
2384     LOG_ALWAYS_FATAL("Shouldn't get here"); // with -Werror,-Wswitch may compile-time fail
2385 }
2386 
aidl2legacy_AudioPortConfig_audio_port_config(const AudioPortConfig & aidl,bool isInput,audio_port_config * legacy,int32_t * portId)2387 status_t aidl2legacy_AudioPortConfig_audio_port_config(
2388         const AudioPortConfig& aidl, bool isInput, audio_port_config* legacy, int32_t* portId) {
2389     legacy->id = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_port_handle_t(aidl.id));
2390     *portId = aidl.portId;
2391     if (aidl.sampleRate.has_value()) {
2392         legacy->sample_rate = VALUE_OR_RETURN_STATUS(
2393                 convertIntegral<unsigned int>(aidl.sampleRate.value().value));
2394         legacy->config_mask |= AUDIO_PORT_CONFIG_SAMPLE_RATE;
2395     }
2396     if (aidl.channelMask.has_value()) {
2397         legacy->channel_mask =
2398                 VALUE_OR_RETURN_STATUS(
2399                         aidl2legacy_AudioChannelLayout_audio_channel_mask_t(
2400                                 aidl.channelMask.value(), isInput));
2401         legacy->config_mask |= AUDIO_PORT_CONFIG_CHANNEL_MASK;
2402     }
2403     if (aidl.format.has_value()) {
2404         legacy->format = VALUE_OR_RETURN_STATUS(
2405                 aidl2legacy_AudioFormatDescription_audio_format_t(aidl.format.value()));
2406         legacy->config_mask |= AUDIO_PORT_CONFIG_FORMAT;
2407     }
2408     if (aidl.gain.has_value()) {
2409         legacy->gain = VALUE_OR_RETURN_STATUS(aidl2legacy_AudioGainConfig_audio_gain_config(
2410                         aidl.gain.value(), isInput));
2411         legacy->config_mask |= AUDIO_PORT_CONFIG_GAIN;
2412     }
2413     if (aidl.flags.has_value()) {
2414         legacy->flags = VALUE_OR_RETURN_STATUS(
2415                 aidl2legacy_AudioIoFlags_audio_io_flags(aidl.flags.value(), isInput));
2416         legacy->config_mask |= AUDIO_PORT_CONFIG_FLAGS;
2417     }
2418     RETURN_STATUS_IF_ERROR(aidl2legacy_AudioPortExt_audio_port_config_ext(
2419                     aidl.ext, isInput, &legacy->ext, &legacy->type));
2420     legacy->role = VALUE_OR_RETURN_STATUS(portRole(isInput ?
2421                     AudioPortDirection::INPUT : AudioPortDirection::OUTPUT, legacy->type));
2422     return OK;
2423 }
2424 
2425 ConversionResult<AudioPortConfig>
legacy2aidl_audio_port_config_AudioPortConfig(const audio_port_config & legacy,bool isInput,int32_t portId)2426 legacy2aidl_audio_port_config_AudioPortConfig(
2427         const audio_port_config& legacy, bool isInput, int32_t portId) {
2428     AudioPortConfig aidl;
2429     aidl.id = VALUE_OR_RETURN(legacy2aidl_audio_port_handle_t_int32_t(legacy.id));
2430     aidl.portId = portId;
2431     if (legacy.config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) {
2432         Int aidl_sampleRate;
2433         aidl_sampleRate.value = VALUE_OR_RETURN(convertIntegral<int32_t>(legacy.sample_rate));
2434         aidl.sampleRate = aidl_sampleRate;
2435     }
2436     if (legacy.config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) {
2437         aidl.channelMask = VALUE_OR_RETURN(
2438                 legacy2aidl_audio_channel_mask_t_AudioChannelLayout(legacy.channel_mask, isInput));
2439     }
2440     if (legacy.config_mask & AUDIO_PORT_CONFIG_FORMAT) {
2441         aidl.format = VALUE_OR_RETURN(
2442                 legacy2aidl_audio_format_t_AudioFormatDescription(legacy.format));
2443     }
2444     if (legacy.config_mask & AUDIO_PORT_CONFIG_GAIN) {
2445         aidl.gain = VALUE_OR_RETURN(
2446                 legacy2aidl_audio_gain_config_AudioGainConfig(legacy.gain, isInput));
2447     }
2448     if (legacy.config_mask & AUDIO_PORT_CONFIG_FLAGS) {
2449         aidl.flags = VALUE_OR_RETURN(
2450                 legacy2aidl_audio_io_flags_AudioIoFlags(legacy.flags, isInput));
2451     }
2452     aidl.ext = VALUE_OR_RETURN(
2453             legacy2aidl_audio_port_config_ext_AudioPortExt(legacy.ext, legacy.type, isInput));
2454     return aidl;
2455 }
2456 
aidl2legacy_AudioPortMixExt_audio_port_mix_ext(const AudioPortMixExt & aidl)2457 ConversionResult<audio_port_mix_ext> aidl2legacy_AudioPortMixExt_audio_port_mix_ext(
2458         const AudioPortMixExt& aidl) {
2459     audio_port_mix_ext legacy{};
2460     legacy.handle = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_io_handle_t(aidl.handle));
2461     return legacy;
2462 }
2463 
legacy2aidl_audio_port_mix_ext_AudioPortMixExt(const audio_port_mix_ext & legacy)2464 ConversionResult<AudioPortMixExt> legacy2aidl_audio_port_mix_ext_AudioPortMixExt(
2465         const audio_port_mix_ext& legacy) {
2466     AudioPortMixExt aidl;
2467     aidl.handle = VALUE_OR_RETURN(legacy2aidl_audio_io_handle_t_int32_t(legacy.handle));
2468     return aidl;
2469 }
2470 
2471 ConversionResult<audio_port_device_ext>
aidl2legacy_AudioPortDeviceExt_audio_port_device_ext(const AudioPortDeviceExt & aidl)2472 aidl2legacy_AudioPortDeviceExt_audio_port_device_ext(const AudioPortDeviceExt& aidl) {
2473     audio_port_device_ext legacy{};
2474     RETURN_IF_ERROR(aidl2legacy_AudioDevice_audio_device(
2475                     aidl.device, &legacy.type, legacy.address));
2476     legacy.encapsulation_modes = VALUE_OR_RETURN(
2477             aidl2legacy_AudioEncapsulationMode_mask(aidl.encapsulationModes));
2478     legacy.encapsulation_metadata_types = VALUE_OR_RETURN(
2479             aidl2legacy_AudioEncapsulationMetadataType_mask(
2480                     aidl.encapsulationMetadataTypes));
2481     return legacy;
2482 }
2483 
legacy2aidl_audio_port_device_ext_AudioPortDeviceExt(const audio_port_device_ext & legacy)2484 ConversionResult<AudioPortDeviceExt> legacy2aidl_audio_port_device_ext_AudioPortDeviceExt(
2485         const audio_port_device_ext& legacy) {
2486     AudioPortDeviceExt aidl;
2487     aidl.device = VALUE_OR_RETURN(
2488             legacy2aidl_audio_device_AudioDevice(legacy.type, legacy.address));
2489     aidl.encapsulationModes = VALUE_OR_RETURN(
2490             legacy2aidl_AudioEncapsulationMode_mask(legacy.encapsulation_modes));
2491     aidl.encapsulationMetadataTypes = VALUE_OR_RETURN(
2492             legacy2aidl_AudioEncapsulationMetadataType_mask(legacy.encapsulation_metadata_types));
2493     return aidl;
2494 }
2495 
2496 // This type is unnamed in the original definition, thus we name it here.
2497 using audio_port_v7_ext = decltype(audio_port_v7::ext);
2498 
aidl2legacy_AudioPortExt_audio_port_v7_ext(const AudioPortExt & aidl,audio_port_v7_ext * legacy,audio_port_type_t * type)2499 status_t aidl2legacy_AudioPortExt_audio_port_v7_ext(
2500         const AudioPortExt& aidl, audio_port_v7_ext* legacy, audio_port_type_t* type) {
2501     switch (aidl.getTag()) {
2502         case AudioPortExt::Tag::unspecified:
2503             // Just verify that the union is empty.
2504             VALUE_OR_RETURN_STATUS(UNION_GET(aidl, unspecified));
2505             *legacy = {};
2506             *type = AUDIO_PORT_TYPE_NONE;
2507             return OK;
2508         case AudioPortExt::Tag::device:
2509             legacy->device = VALUE_OR_RETURN_STATUS(
2510                     aidl2legacy_AudioPortDeviceExt_audio_port_device_ext(
2511                             VALUE_OR_RETURN_STATUS(UNION_GET(aidl, device))));
2512             *type = AUDIO_PORT_TYPE_DEVICE;
2513             return OK;
2514         case AudioPortExt::Tag::mix:
2515             legacy->mix = VALUE_OR_RETURN_STATUS(
2516                     aidl2legacy_AudioPortMixExt_audio_port_mix_ext(
2517                             VALUE_OR_RETURN_STATUS(UNION_GET(aidl, mix))));
2518             *type = AUDIO_PORT_TYPE_MIX;
2519             return OK;
2520         case AudioPortExt::Tag::session:
2521             // This variant is not used in the HAL scenario.
2522             legacy->session.session = AUDIO_SESSION_NONE;
2523             *type = AUDIO_PORT_TYPE_SESSION;
2524             return OK;
2525 
2526     }
2527     LOG_ALWAYS_FATAL("Shouldn't get here"); // with -Werror,-Wswitch may compile-time fail
2528 }
2529 
legacy2aidl_audio_port_v7_ext_AudioPortExt(const audio_port_v7_ext & legacy,audio_port_type_t type)2530 ConversionResult<AudioPortExt> legacy2aidl_audio_port_v7_ext_AudioPortExt(
2531         const audio_port_v7_ext& legacy, audio_port_type_t type) {
2532     AudioPortExt aidl;
2533     switch (type) {
2534         case AUDIO_PORT_TYPE_NONE:
2535             UNION_SET(aidl, unspecified, false);
2536             return aidl;
2537         case AUDIO_PORT_TYPE_DEVICE: {
2538             AudioPortDeviceExt device = VALUE_OR_RETURN(
2539                     legacy2aidl_audio_port_device_ext_AudioPortDeviceExt(legacy.device));
2540             UNION_SET(aidl, device, device);
2541             return aidl;
2542         }
2543         case AUDIO_PORT_TYPE_MIX: {
2544             AudioPortMixExt mix = VALUE_OR_RETURN(
2545                     legacy2aidl_audio_port_mix_ext_AudioPortMixExt(legacy.mix));
2546             UNION_SET(aidl, mix, mix);
2547             return aidl;
2548         }
2549         case AUDIO_PORT_TYPE_SESSION:
2550             // This variant is not used in the HAL scenario.
2551             UNION_SET(aidl, unspecified, false);
2552             return aidl;
2553     }
2554     LOG_ALWAYS_FATAL("Shouldn't get here"); // with -Werror,-Wswitch may compile-time fail
2555 }
2556 
2557 ConversionResult<audio_port_v7>
aidl2legacy_AudioPort_audio_port_v7(const AudioPort & aidl,bool isInput)2558 aidl2legacy_AudioPort_audio_port_v7(const AudioPort& aidl, bool isInput) {
2559     audio_port_v7 legacy;
2560     legacy.id = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_port_handle_t(aidl.id));
2561     RETURN_IF_ERROR(aidl2legacy_string(aidl.name, legacy.name, sizeof(legacy.name)));
2562 
2563     if (aidl.profiles.size() > std::size(legacy.audio_profiles)) {
2564         return unexpected(BAD_VALUE);
2565     }
2566     RETURN_IF_ERROR(convertRange(
2567                     aidl.profiles.begin(), aidl.profiles.end(), legacy.audio_profiles,
2568                     [isInput](const AudioProfile& p) {
2569                         return aidl2legacy_AudioProfile_audio_profile(p, isInput);
2570                     }));
2571     legacy.num_audio_profiles = aidl.profiles.size();
2572 
2573     if (aidl.extraAudioDescriptors.size() > std::size(legacy.extra_audio_descriptors)) {
2574         return unexpected(BAD_VALUE);
2575     }
2576     RETURN_IF_ERROR(
2577             convertRange(
2578                     aidl.extraAudioDescriptors.begin(), aidl.extraAudioDescriptors.end(),
2579                     legacy.extra_audio_descriptors,
2580                     aidl2legacy_ExtraAudioDescriptor_audio_extra_audio_descriptor));
2581     legacy.num_extra_audio_descriptors = aidl.extraAudioDescriptors.size();
2582 
2583     if (aidl.gains.size() > std::size(legacy.gains)) {
2584         return unexpected(BAD_VALUE);
2585     }
2586     RETURN_IF_ERROR(convertRange(aidl.gains.begin(), aidl.gains.end(), legacy.gains,
2587                                  [isInput](const AudioGain& g) {
2588                                      return aidl2legacy_AudioGain_audio_gain(g, isInput);
2589                                  }));
2590     legacy.num_gains = aidl.gains.size();
2591 
2592     RETURN_IF_ERROR(aidl2legacy_AudioPortExt_audio_port_v7_ext(
2593                     aidl.ext, &legacy.ext, &legacy.type));
2594     legacy.role = VALUE_OR_RETURN(portRole(
2595                     isInput ? AudioPortDirection::INPUT : AudioPortDirection::OUTPUT, legacy.type));
2596 
2597     AudioPortConfig aidlPortConfig;
2598     int32_t portId;
2599     aidlPortConfig.flags = aidl.flags;
2600     aidlPortConfig.ext = aidl.ext;
2601     RETURN_IF_ERROR(aidl2legacy_AudioPortConfig_audio_port_config(
2602                     aidlPortConfig, isInput, &legacy.active_config, &portId));
2603     return legacy;
2604 }
2605 
2606 ConversionResult<AudioPort>
legacy2aidl_audio_port_v7_AudioPort(const audio_port_v7 & legacy,bool isInput)2607 legacy2aidl_audio_port_v7_AudioPort(const audio_port_v7& legacy, bool isInput) {
2608     AudioPort aidl;
2609     aidl.id = VALUE_OR_RETURN(legacy2aidl_audio_port_handle_t_int32_t(legacy.id));
2610     aidl.name = VALUE_OR_RETURN(legacy2aidl_string(legacy.name, sizeof(legacy.name)));
2611 
2612     if (legacy.num_audio_profiles > std::size(legacy.audio_profiles)) {
2613         return unexpected(BAD_VALUE);
2614     }
2615     RETURN_IF_ERROR(
2616             convertRange(legacy.audio_profiles, legacy.audio_profiles + legacy.num_audio_profiles,
2617                          std::back_inserter(aidl.profiles),
2618                          [isInput](const audio_profile& p) {
2619                              return legacy2aidl_audio_profile_AudioProfile(p, isInput);
2620                          }));
2621 
2622     if (legacy.num_extra_audio_descriptors > std::size(legacy.extra_audio_descriptors)) {
2623         return unexpected(BAD_VALUE);
2624     }
2625     aidl.profiles.resize(legacy.num_audio_profiles);
2626     RETURN_IF_ERROR(
2627             convertRange(legacy.extra_audio_descriptors,
2628                     legacy.extra_audio_descriptors + legacy.num_extra_audio_descriptors,
2629                     std::back_inserter(aidl.extraAudioDescriptors),
2630                     legacy2aidl_audio_extra_audio_descriptor_ExtraAudioDescriptor));
2631 
2632     if (legacy.num_gains > std::size(legacy.gains)) {
2633         return unexpected(BAD_VALUE);
2634     }
2635     RETURN_IF_ERROR(
2636             convertRange(legacy.gains, legacy.gains + legacy.num_gains,
2637                          std::back_inserter(aidl.gains),
2638                          [isInput](const audio_gain& g) {
2639                              return legacy2aidl_audio_gain_AudioGain(g, isInput);
2640                          }));
2641     aidl.gains.resize(legacy.num_gains);
2642 
2643     aidl.ext = VALUE_OR_RETURN(
2644             legacy2aidl_audio_port_v7_ext_AudioPortExt(legacy.ext, legacy.type));
2645 
2646     AudioPortConfig aidlPortConfig = VALUE_OR_RETURN(legacy2aidl_audio_port_config_AudioPortConfig(
2647                     legacy.active_config, isInput, aidl.id));
2648     if (aidlPortConfig.flags.has_value()) {
2649         aidl.flags = aidlPortConfig.flags.value();
2650     } else {
2651         aidl.flags = isInput ?
2652                 AudioIoFlags::make<AudioIoFlags::Tag::input>(0) :
2653                 AudioIoFlags::make<AudioIoFlags::Tag::output>(0);
2654     }
2655     return aidl;
2656 }
2657 
2658 ConversionResult<audio_profile>
aidl2legacy_AudioProfile_audio_profile(const AudioProfile & aidl,bool isInput)2659 aidl2legacy_AudioProfile_audio_profile(const AudioProfile& aidl, bool isInput) {
2660     audio_profile legacy;
2661     legacy.format = VALUE_OR_RETURN(aidl2legacy_AudioFormatDescription_audio_format_t(aidl.format));
2662 
2663     if (aidl.sampleRates.size() > std::size(legacy.sample_rates)) {
2664         return unexpected(BAD_VALUE);
2665     }
2666     RETURN_IF_ERROR(
2667             convertRange(aidl.sampleRates.begin(), aidl.sampleRates.end(), legacy.sample_rates,
2668                          convertIntegral<int32_t, unsigned int>));
2669     legacy.num_sample_rates = aidl.sampleRates.size();
2670 
2671     if (aidl.channelMasks.size() > std::size(legacy.channel_masks)) {
2672         return unexpected(BAD_VALUE);
2673     }
2674     RETURN_IF_ERROR(
2675             convertRange(aidl.channelMasks.begin(), aidl.channelMasks.end(), legacy.channel_masks,
2676                     [isInput](const AudioChannelLayout& l) {
2677                         return aidl2legacy_AudioChannelLayout_audio_channel_mask_t(l, isInput);
2678                     }));
2679     legacy.num_channel_masks = aidl.channelMasks.size();
2680 
2681     legacy.encapsulation_type = VALUE_OR_RETURN(
2682             aidl2legacy_AudioEncapsulationType_audio_encapsulation_type_t(aidl.encapsulationType));
2683     return legacy;
2684 }
2685 
2686 ConversionResult<AudioProfile>
legacy2aidl_audio_profile_AudioProfile(const audio_profile & legacy,bool isInput)2687 legacy2aidl_audio_profile_AudioProfile(const audio_profile& legacy, bool isInput) {
2688     AudioProfile aidl;
2689     aidl.format = VALUE_OR_RETURN(legacy2aidl_audio_format_t_AudioFormatDescription(legacy.format));
2690 
2691     if (legacy.num_sample_rates > std::size(legacy.sample_rates)) {
2692         return unexpected(BAD_VALUE);
2693     }
2694     RETURN_IF_ERROR(
2695             convertRange(legacy.sample_rates, legacy.sample_rates + legacy.num_sample_rates,
2696                          std::back_inserter(aidl.sampleRates),
2697                          convertIntegral<unsigned int, int32_t>));
2698 
2699     if (legacy.num_channel_masks > std::size(legacy.channel_masks)) {
2700         return unexpected(BAD_VALUE);
2701     }
2702     RETURN_IF_ERROR(
2703             convertRange(legacy.channel_masks, legacy.channel_masks + legacy.num_channel_masks,
2704                          std::back_inserter(aidl.channelMasks),
2705                     [isInput](audio_channel_mask_t m) {
2706                         return legacy2aidl_audio_channel_mask_t_AudioChannelLayout(m, isInput);
2707                     }));
2708 
2709     aidl.encapsulationType = VALUE_OR_RETURN(
2710             legacy2aidl_audio_encapsulation_type_t_AudioEncapsulationType(
2711                     legacy.encapsulation_type));
2712     return aidl;
2713 }
2714 
2715 ConversionResult<audio_gain>
aidl2legacy_AudioGain_audio_gain(const AudioGain & aidl,bool isInput)2716 aidl2legacy_AudioGain_audio_gain(const AudioGain& aidl, bool isInput) {
2717     audio_gain legacy;
2718     legacy.mode = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_gain_mode_t_mask(aidl.mode));
2719     legacy.channel_mask = VALUE_OR_RETURN(aidl2legacy_AudioChannelLayout_audio_channel_mask_t(
2720                     aidl.channelMask, isInput));
2721     legacy.min_value = VALUE_OR_RETURN(convertIntegral<int>(aidl.minValue));
2722     legacy.max_value = VALUE_OR_RETURN(convertIntegral<int>(aidl.maxValue));
2723     legacy.default_value = VALUE_OR_RETURN(convertIntegral<int>(aidl.defaultValue));
2724     legacy.step_value = VALUE_OR_RETURN(convertIntegral<unsigned int>(aidl.stepValue));
2725     legacy.min_ramp_ms = VALUE_OR_RETURN(convertIntegral<unsigned int>(aidl.minRampMs));
2726     legacy.max_ramp_ms = VALUE_OR_RETURN(convertIntegral<unsigned int>(aidl.maxRampMs));
2727     return legacy;
2728 }
2729 
2730 ConversionResult<AudioGain>
legacy2aidl_audio_gain_AudioGain(const audio_gain & legacy,bool isInput)2731 legacy2aidl_audio_gain_AudioGain(const audio_gain& legacy, bool isInput) {
2732     AudioGain aidl;
2733     aidl.mode = VALUE_OR_RETURN(legacy2aidl_audio_gain_mode_t_int32_t_mask(legacy.mode));
2734     aidl.channelMask = VALUE_OR_RETURN(
2735             legacy2aidl_audio_channel_mask_t_AudioChannelLayout(legacy.channel_mask, isInput));
2736     aidl.minValue = VALUE_OR_RETURN(convertIntegral<int32_t>(legacy.min_value));
2737     aidl.maxValue = VALUE_OR_RETURN(convertIntegral<int32_t>(legacy.max_value));
2738     aidl.defaultValue = VALUE_OR_RETURN(convertIntegral<int32_t>(legacy.default_value));
2739     aidl.stepValue = VALUE_OR_RETURN(convertIntegral<int32_t>(legacy.step_value));
2740     aidl.minRampMs = VALUE_OR_RETURN(convertIntegral<int32_t>(legacy.min_ramp_ms));
2741     aidl.maxRampMs = VALUE_OR_RETURN(convertIntegral<int32_t>(legacy.max_ramp_ms));
2742     return aidl;
2743 }
2744 
2745 ConversionResult<audio_mode_t>
aidl2legacy_AudioMode_audio_mode_t(AudioMode aidl)2746 aidl2legacy_AudioMode_audio_mode_t(AudioMode aidl) {
2747     switch (aidl) {
2748         case AudioMode::SYS_RESERVED_INVALID:
2749             return AUDIO_MODE_INVALID;
2750         case AudioMode::SYS_RESERVED_CURRENT:
2751             return AUDIO_MODE_CURRENT;
2752         case AudioMode::NORMAL:
2753             return AUDIO_MODE_NORMAL;
2754         case AudioMode::RINGTONE:
2755             return AUDIO_MODE_RINGTONE;
2756         case AudioMode::IN_CALL:
2757             return AUDIO_MODE_IN_CALL;
2758         case AudioMode::IN_COMMUNICATION:
2759             return AUDIO_MODE_IN_COMMUNICATION;
2760         case AudioMode::CALL_SCREEN:
2761             return AUDIO_MODE_CALL_SCREEN;
2762         case AudioMode::SYS_RESERVED_CALL_REDIRECT:
2763             return AUDIO_MODE_CALL_REDIRECT;
2764         case AudioMode::SYS_RESERVED_COMMUNICATION_REDIRECT:
2765             return AUDIO_MODE_COMMUNICATION_REDIRECT;
2766     }
2767     return unexpected(BAD_VALUE);
2768 }
2769 
2770 ConversionResult<AudioMode>
legacy2aidl_audio_mode_t_AudioMode(audio_mode_t legacy)2771 legacy2aidl_audio_mode_t_AudioMode(audio_mode_t legacy) {
2772     switch (legacy) {
2773         case AUDIO_MODE_INVALID:
2774             return AudioMode::SYS_RESERVED_INVALID;
2775         case AUDIO_MODE_CURRENT:
2776             return AudioMode::SYS_RESERVED_CURRENT;
2777         case AUDIO_MODE_NORMAL:
2778             return AudioMode::NORMAL;
2779         case AUDIO_MODE_RINGTONE:
2780             return AudioMode::RINGTONE;
2781         case AUDIO_MODE_IN_CALL:
2782             return AudioMode::IN_CALL;
2783         case AUDIO_MODE_IN_COMMUNICATION:
2784             return AudioMode::IN_COMMUNICATION;
2785         case AUDIO_MODE_CALL_SCREEN:
2786             return AudioMode::CALL_SCREEN;
2787         case AUDIO_MODE_CALL_REDIRECT:
2788             return AudioMode::SYS_RESERVED_CALL_REDIRECT;
2789         case AUDIO_MODE_COMMUNICATION_REDIRECT:
2790             return AudioMode::SYS_RESERVED_COMMUNICATION_REDIRECT;
2791         case AUDIO_MODE_CNT:
2792             break;
2793     }
2794     return unexpected(BAD_VALUE);
2795 }
2796 
2797 ConversionResult<audio_standard_t>
aidl2legacy_AudioStandard_audio_standard_t(AudioStandard aidl)2798 aidl2legacy_AudioStandard_audio_standard_t(AudioStandard aidl) {
2799     switch (aidl) {
2800         case AudioStandard::NONE:
2801             return AUDIO_STANDARD_NONE;
2802         case AudioStandard::EDID:
2803             return AUDIO_STANDARD_EDID;
2804         case AudioStandard::SADB:
2805             return AUDIO_STANDARD_SADB;
2806         case AudioStandard::VSADB:
2807             return AUDIO_STANDARD_VSADB;
2808     }
2809     return unexpected(BAD_VALUE);
2810 }
2811 
2812 ConversionResult<AudioStandard>
legacy2aidl_audio_standard_t_AudioStandard(audio_standard_t legacy)2813 legacy2aidl_audio_standard_t_AudioStandard(audio_standard_t legacy) {
2814     switch (legacy) {
2815         case AUDIO_STANDARD_NONE:
2816             return AudioStandard::NONE;
2817         case AUDIO_STANDARD_EDID:
2818             return AudioStandard::EDID;
2819         case AUDIO_STANDARD_SADB:
2820             return AudioStandard::SADB;
2821         case AUDIO_STANDARD_VSADB:
2822             return AudioStandard::VSADB;
2823     }
2824     return unexpected(BAD_VALUE);
2825 }
2826 
2827 ConversionResult<audio_extra_audio_descriptor>
aidl2legacy_ExtraAudioDescriptor_audio_extra_audio_descriptor(const ExtraAudioDescriptor & aidl)2828 aidl2legacy_ExtraAudioDescriptor_audio_extra_audio_descriptor(
2829         const ExtraAudioDescriptor& aidl) {
2830     audio_extra_audio_descriptor legacy;
2831     legacy.standard = VALUE_OR_RETURN(aidl2legacy_AudioStandard_audio_standard_t(aidl.standard));
2832     if (aidl.audioDescriptor.size() > EXTRA_AUDIO_DESCRIPTOR_SIZE) {
2833         return unexpected(BAD_VALUE);
2834     }
2835     legacy.descriptor_length = aidl.audioDescriptor.size();
2836     std::copy(aidl.audioDescriptor.begin(), aidl.audioDescriptor.end(),
2837               std::begin(legacy.descriptor));
2838     legacy.encapsulation_type =
2839             VALUE_OR_RETURN(aidl2legacy_AudioEncapsulationType_audio_encapsulation_type_t(
2840                     aidl.encapsulationType));
2841     return legacy;
2842 }
2843 
2844 ConversionResult<ExtraAudioDescriptor>
legacy2aidl_audio_extra_audio_descriptor_ExtraAudioDescriptor(const audio_extra_audio_descriptor & legacy)2845 legacy2aidl_audio_extra_audio_descriptor_ExtraAudioDescriptor(
2846         const audio_extra_audio_descriptor& legacy) {
2847     ExtraAudioDescriptor aidl;
2848     aidl.standard = VALUE_OR_RETURN(legacy2aidl_audio_standard_t_AudioStandard(legacy.standard));
2849     if (legacy.descriptor_length > EXTRA_AUDIO_DESCRIPTOR_SIZE) {
2850         return unexpected(BAD_VALUE);
2851     }
2852     aidl.audioDescriptor.resize(legacy.descriptor_length);
2853     std::copy(legacy.descriptor, legacy.descriptor + legacy.descriptor_length,
2854               aidl.audioDescriptor.begin());
2855     aidl.encapsulationType =
2856             VALUE_OR_RETURN(legacy2aidl_audio_encapsulation_type_t_AudioEncapsulationType(
2857                     legacy.encapsulation_type));
2858     return aidl;
2859 }
2860 
2861 ConversionResult<audio_encapsulation_type_t>
aidl2legacy_AudioEncapsulationType_audio_encapsulation_type_t(const AudioEncapsulationType & aidl)2862 aidl2legacy_AudioEncapsulationType_audio_encapsulation_type_t(
2863         const AudioEncapsulationType& aidl) {
2864     switch (aidl) {
2865         case AudioEncapsulationType::NONE:
2866             return AUDIO_ENCAPSULATION_TYPE_NONE;
2867         case AudioEncapsulationType::IEC61937:
2868             return AUDIO_ENCAPSULATION_TYPE_IEC61937;
2869         case AudioEncapsulationType::PCM:
2870             return AUDIO_ENCAPSULATION_TYPE_PCM;
2871     }
2872     return unexpected(BAD_VALUE);
2873 }
2874 
2875 ConversionResult<AudioEncapsulationType>
legacy2aidl_audio_encapsulation_type_t_AudioEncapsulationType(const audio_encapsulation_type_t & legacy)2876 legacy2aidl_audio_encapsulation_type_t_AudioEncapsulationType(
2877         const audio_encapsulation_type_t & legacy) {
2878     switch (legacy) {
2879         case AUDIO_ENCAPSULATION_TYPE_NONE:
2880             return AudioEncapsulationType::NONE;
2881         case AUDIO_ENCAPSULATION_TYPE_IEC61937:
2882             return AudioEncapsulationType::IEC61937;
2883         case AUDIO_ENCAPSULATION_TYPE_PCM:
2884             return AudioEncapsulationType::PCM;
2885     }
2886     return unexpected(BAD_VALUE);
2887 }
2888 
2889 ConversionResult<audio_dual_mono_mode_t>
aidl2legacy_AudioDualMonoMode_audio_dual_mono_mode_t(AudioDualMonoMode aidl)2890 aidl2legacy_AudioDualMonoMode_audio_dual_mono_mode_t(AudioDualMonoMode aidl) {
2891     switch (aidl) {
2892         case AudioDualMonoMode::OFF:
2893             return AUDIO_DUAL_MONO_MODE_OFF;
2894         case AudioDualMonoMode::LR:
2895             return AUDIO_DUAL_MONO_MODE_LR;
2896         case AudioDualMonoMode::LL:
2897             return AUDIO_DUAL_MONO_MODE_LL;
2898         case AudioDualMonoMode::RR:
2899             return AUDIO_DUAL_MONO_MODE_RR;
2900     }
2901     return unexpected(BAD_VALUE);
2902 }
2903 
2904 ConversionResult<AudioDualMonoMode>
legacy2aidl_audio_dual_mono_mode_t_AudioDualMonoMode(audio_dual_mono_mode_t legacy)2905 legacy2aidl_audio_dual_mono_mode_t_AudioDualMonoMode(audio_dual_mono_mode_t legacy) {
2906     switch (legacy) {
2907         case AUDIO_DUAL_MONO_MODE_OFF:
2908             return AudioDualMonoMode::OFF;
2909         case AUDIO_DUAL_MONO_MODE_LR:
2910             return AudioDualMonoMode::LR;
2911         case AUDIO_DUAL_MONO_MODE_LL:
2912             return AudioDualMonoMode::LL;
2913         case AUDIO_DUAL_MONO_MODE_RR:
2914             return AudioDualMonoMode::RR;
2915     }
2916     return unexpected(BAD_VALUE);
2917 }
2918 
2919 ConversionResult<audio_timestretch_fallback_mode_t>
aidl2legacy_TimestretchFallbackMode_audio_timestretch_fallback_mode_t(AudioPlaybackRate::TimestretchFallbackMode aidl)2920 aidl2legacy_TimestretchFallbackMode_audio_timestretch_fallback_mode_t(
2921         AudioPlaybackRate::TimestretchFallbackMode aidl) {
2922     switch (aidl) {
2923         case AudioPlaybackRate::TimestretchFallbackMode::SYS_RESERVED_CUT_REPEAT:
2924             return AUDIO_TIMESTRETCH_FALLBACK_CUT_REPEAT;
2925         case AudioPlaybackRate::TimestretchFallbackMode::SYS_RESERVED_DEFAULT:
2926             return AUDIO_TIMESTRETCH_FALLBACK_DEFAULT;
2927         case AudioPlaybackRate::TimestretchFallbackMode::MUTE:
2928             return AUDIO_TIMESTRETCH_FALLBACK_MUTE;
2929         case AudioPlaybackRate::TimestretchFallbackMode::FAIL:
2930             return AUDIO_TIMESTRETCH_FALLBACK_FAIL;
2931     }
2932     return unexpected(BAD_VALUE);
2933 }
2934 
2935 ConversionResult<AudioPlaybackRate::TimestretchFallbackMode>
legacy2aidl_audio_timestretch_fallback_mode_t_TimestretchFallbackMode(audio_timestretch_fallback_mode_t legacy)2936 legacy2aidl_audio_timestretch_fallback_mode_t_TimestretchFallbackMode(
2937         audio_timestretch_fallback_mode_t legacy) {
2938     switch (legacy) {
2939         case AUDIO_TIMESTRETCH_FALLBACK_CUT_REPEAT:
2940             return AudioPlaybackRate::TimestretchFallbackMode::SYS_RESERVED_CUT_REPEAT;
2941         case AUDIO_TIMESTRETCH_FALLBACK_DEFAULT:
2942             return AudioPlaybackRate::TimestretchFallbackMode::SYS_RESERVED_DEFAULT;
2943         case AUDIO_TIMESTRETCH_FALLBACK_MUTE:
2944             return AudioPlaybackRate::TimestretchFallbackMode::MUTE;
2945         case AUDIO_TIMESTRETCH_FALLBACK_FAIL:
2946             return AudioPlaybackRate::TimestretchFallbackMode::FAIL;
2947     }
2948     return unexpected(BAD_VALUE);
2949 }
2950 
2951 ConversionResult<audio_timestretch_stretch_mode_t>
aidl2legacy_TimestretchMode_audio_timestretch_stretch_mode_t(AudioPlaybackRate::TimestretchMode aidl)2952 aidl2legacy_TimestretchMode_audio_timestretch_stretch_mode_t(
2953         AudioPlaybackRate::TimestretchMode aidl) {
2954     switch (aidl) {
2955         case AudioPlaybackRate::TimestretchMode::DEFAULT:
2956             return AUDIO_TIMESTRETCH_STRETCH_DEFAULT;
2957         case AudioPlaybackRate::TimestretchMode::VOICE:
2958             return AUDIO_TIMESTRETCH_STRETCH_VOICE;
2959     }
2960     return unexpected(BAD_VALUE);
2961 }
2962 
2963 ConversionResult<AudioPlaybackRate::TimestretchMode>
legacy2aidl_audio_timestretch_stretch_mode_t_TimestretchMode(audio_timestretch_stretch_mode_t legacy)2964 legacy2aidl_audio_timestretch_stretch_mode_t_TimestretchMode(
2965         audio_timestretch_stretch_mode_t legacy) {
2966     switch (legacy) {
2967         case AUDIO_TIMESTRETCH_STRETCH_DEFAULT:
2968             return AudioPlaybackRate::TimestretchMode::DEFAULT;
2969         case AUDIO_TIMESTRETCH_STRETCH_VOICE:
2970             return AudioPlaybackRate::TimestretchMode::VOICE;
2971     }
2972     return unexpected(BAD_VALUE);
2973 }
2974 
2975 ConversionResult<audio_playback_rate_t>
aidl2legacy_AudioPlaybackRate_audio_playback_rate_t(const AudioPlaybackRate & aidl)2976 aidl2legacy_AudioPlaybackRate_audio_playback_rate_t(const AudioPlaybackRate& aidl) {
2977     audio_playback_rate_t legacy;
2978     legacy.mSpeed = aidl.speed;
2979     legacy.mPitch = aidl.pitch;
2980     legacy.mFallbackMode = VALUE_OR_RETURN(
2981             aidl2legacy_TimestretchFallbackMode_audio_timestretch_fallback_mode_t(
2982                     aidl.fallbackMode));
2983     legacy.mStretchMode = VALUE_OR_RETURN(
2984             aidl2legacy_TimestretchMode_audio_timestretch_stretch_mode_t(aidl.timestretchMode));
2985     return legacy;
2986 }
2987 
2988 ConversionResult<AudioPlaybackRate>
legacy2aidl_audio_playback_rate_t_AudioPlaybackRate(const audio_playback_rate_t & legacy)2989 legacy2aidl_audio_playback_rate_t_AudioPlaybackRate(const audio_playback_rate_t& legacy) {
2990     AudioPlaybackRate aidl;
2991     aidl.speed = legacy.mSpeed;
2992     aidl.pitch = legacy.mPitch;
2993     aidl.fallbackMode = VALUE_OR_RETURN(
2994             legacy2aidl_audio_timestretch_fallback_mode_t_TimestretchFallbackMode(
2995                     legacy.mFallbackMode));
2996     aidl.timestretchMode = VALUE_OR_RETURN(
2997             legacy2aidl_audio_timestretch_stretch_mode_t_TimestretchMode(legacy.mStretchMode));
2998     return aidl;
2999 }
3000 
3001 ConversionResult<audio_latency_mode_t>
aidl2legacy_AudioLatencyMode_audio_latency_mode_t(AudioLatencyMode aidl)3002 aidl2legacy_AudioLatencyMode_audio_latency_mode_t(AudioLatencyMode aidl) {
3003     switch (aidl) {
3004         case AudioLatencyMode::FREE:
3005             return AUDIO_LATENCY_MODE_FREE;
3006         case AudioLatencyMode::LOW:
3007             return AUDIO_LATENCY_MODE_LOW;
3008         case AudioLatencyMode::DYNAMIC_SPATIAL_AUDIO_SOFTWARE:
3009             return AUDIO_LATENCY_MODE_DYNAMIC_SPATIAL_AUDIO_SOFTWARE;
3010         case AudioLatencyMode::DYNAMIC_SPATIAL_AUDIO_HARDWARE:
3011             return AUDIO_LATENCY_MODE_DYNAMIC_SPATIAL_AUDIO_HARDWARE;
3012     }
3013     return unexpected(BAD_VALUE);
3014 }
3015 ConversionResult<AudioLatencyMode>
legacy2aidl_audio_latency_mode_t_AudioLatencyMode(audio_latency_mode_t legacy)3016 legacy2aidl_audio_latency_mode_t_AudioLatencyMode(audio_latency_mode_t legacy) {
3017     switch (legacy) {
3018         case AUDIO_LATENCY_MODE_FREE:
3019             return AudioLatencyMode::FREE;
3020         case AUDIO_LATENCY_MODE_LOW:
3021             return AudioLatencyMode::LOW;
3022         case AUDIO_LATENCY_MODE_DYNAMIC_SPATIAL_AUDIO_SOFTWARE:
3023             return AudioLatencyMode::DYNAMIC_SPATIAL_AUDIO_SOFTWARE;
3024         case AUDIO_LATENCY_MODE_DYNAMIC_SPATIAL_AUDIO_HARDWARE:
3025             return AudioLatencyMode::DYNAMIC_SPATIAL_AUDIO_HARDWARE;
3026     }
3027     return unexpected(BAD_VALUE);
3028 }
3029 
3030 ConversionResult<audio_microphone_location_t>
aidl2legacy_MicrophoneInfoLocation_audio_microphone_location_t(MicrophoneInfo::Location aidl)3031 aidl2legacy_MicrophoneInfoLocation_audio_microphone_location_t(MicrophoneInfo::Location aidl) {
3032     switch (aidl) {
3033         case MicrophoneInfo::Location::UNKNOWN:
3034             return AUDIO_MICROPHONE_LOCATION_UNKNOWN;
3035         case MicrophoneInfo::Location::MAINBODY:
3036             return AUDIO_MICROPHONE_LOCATION_MAINBODY;
3037         case MicrophoneInfo::Location::MAINBODY_MOVABLE:
3038             return AUDIO_MICROPHONE_LOCATION_MAINBODY_MOVABLE;
3039         case MicrophoneInfo::Location::PERIPHERAL:
3040             return AUDIO_MICROPHONE_LOCATION_PERIPHERAL;
3041     }
3042     return unexpected(BAD_VALUE);
3043 }
3044 ConversionResult<MicrophoneInfo::Location>
legacy2aidl_audio_microphone_location_t_MicrophoneInfoLocation(audio_microphone_location_t legacy)3045 legacy2aidl_audio_microphone_location_t_MicrophoneInfoLocation(audio_microphone_location_t legacy) {
3046     switch (legacy) {
3047         case AUDIO_MICROPHONE_LOCATION_UNKNOWN:
3048             return MicrophoneInfo::Location::UNKNOWN;
3049         case AUDIO_MICROPHONE_LOCATION_MAINBODY:
3050             return MicrophoneInfo::Location::MAINBODY;
3051         case AUDIO_MICROPHONE_LOCATION_MAINBODY_MOVABLE:
3052             return MicrophoneInfo::Location::MAINBODY_MOVABLE;
3053         case AUDIO_MICROPHONE_LOCATION_PERIPHERAL:
3054             return MicrophoneInfo::Location::PERIPHERAL;
3055     }
3056     return unexpected(BAD_VALUE);
3057 }
3058 
aidl2legacy_int32_t_audio_microphone_group_t(int32_t aidl)3059 ConversionResult<audio_microphone_group_t> aidl2legacy_int32_t_audio_microphone_group_t(
3060         int32_t aidl) {
3061     return convertReinterpret<audio_microphone_group_t>(aidl);
3062 }
3063 
legacy2aidl_audio_microphone_group_t_int32_t(audio_microphone_group_t legacy)3064 ConversionResult<int32_t> legacy2aidl_audio_microphone_group_t_int32_t(
3065         audio_microphone_group_t legacy) {
3066     return convertReinterpret<int32_t>(legacy);
3067 }
3068 
3069 ConversionResult<audio_microphone_directionality_t>
aidl2legacy_MicrophoneInfoDirectionality_audio_microphone_directionality_t(MicrophoneInfo::Directionality aidl)3070 aidl2legacy_MicrophoneInfoDirectionality_audio_microphone_directionality_t(
3071         MicrophoneInfo::Directionality aidl) {
3072     switch (aidl) {
3073         case MicrophoneInfo::Directionality::UNKNOWN:
3074             return AUDIO_MICROPHONE_DIRECTIONALITY_UNKNOWN;
3075         case MicrophoneInfo::Directionality::OMNI:
3076             return AUDIO_MICROPHONE_DIRECTIONALITY_OMNI;
3077         case MicrophoneInfo::Directionality::BI_DIRECTIONAL:
3078             return AUDIO_MICROPHONE_DIRECTIONALITY_BI_DIRECTIONAL;
3079         case MicrophoneInfo::Directionality::CARDIOID:
3080             return AUDIO_MICROPHONE_DIRECTIONALITY_CARDIOID;
3081         case MicrophoneInfo::Directionality::HYPER_CARDIOID:
3082             return AUDIO_MICROPHONE_DIRECTIONALITY_HYPER_CARDIOID;
3083         case MicrophoneInfo::Directionality::SUPER_CARDIOID:
3084             return AUDIO_MICROPHONE_DIRECTIONALITY_SUPER_CARDIOID;
3085     }
3086     return unexpected(BAD_VALUE);
3087 }
3088 ConversionResult<MicrophoneInfo::Directionality>
legacy2aidl_audio_microphone_directionality_t_MicrophoneInfoDirectionality(audio_microphone_directionality_t legacy)3089 legacy2aidl_audio_microphone_directionality_t_MicrophoneInfoDirectionality(
3090         audio_microphone_directionality_t legacy) {
3091     switch (legacy) {
3092         case AUDIO_MICROPHONE_DIRECTIONALITY_UNKNOWN:
3093             return MicrophoneInfo::Directionality::UNKNOWN;
3094         case AUDIO_MICROPHONE_DIRECTIONALITY_OMNI:
3095             return MicrophoneInfo::Directionality::OMNI;
3096         case AUDIO_MICROPHONE_DIRECTIONALITY_BI_DIRECTIONAL:
3097             return MicrophoneInfo::Directionality::BI_DIRECTIONAL;
3098         case AUDIO_MICROPHONE_DIRECTIONALITY_CARDIOID:
3099             return MicrophoneInfo::Directionality::CARDIOID;
3100         case AUDIO_MICROPHONE_DIRECTIONALITY_HYPER_CARDIOID:
3101             return MicrophoneInfo::Directionality::HYPER_CARDIOID;
3102         case AUDIO_MICROPHONE_DIRECTIONALITY_SUPER_CARDIOID:
3103             return MicrophoneInfo::Directionality::SUPER_CARDIOID;
3104     }
3105     return unexpected(BAD_VALUE);
3106 }
3107 
3108 ConversionResult<audio_microphone_coordinate>
aidl2legacy_MicrophoneInfoCoordinate_audio_microphone_coordinate(const MicrophoneInfo::Coordinate & aidl)3109 aidl2legacy_MicrophoneInfoCoordinate_audio_microphone_coordinate(
3110         const MicrophoneInfo::Coordinate& aidl) {
3111     audio_microphone_coordinate legacy;
3112     legacy.x = aidl.x;
3113     legacy.y = aidl.y;
3114     legacy.z = aidl.z;
3115     return legacy;
3116 }
3117 ConversionResult<MicrophoneInfo::Coordinate>
legacy2aidl_audio_microphone_coordinate_MicrophoneInfoCoordinate(const audio_microphone_coordinate & legacy)3118 legacy2aidl_audio_microphone_coordinate_MicrophoneInfoCoordinate(
3119         const audio_microphone_coordinate& legacy) {
3120     MicrophoneInfo::Coordinate aidl;
3121     aidl.x = legacy.x;
3122     aidl.y = legacy.y;
3123     aidl.z = legacy.z;
3124     return aidl;
3125 }
3126 
3127 ConversionResult<audio_microphone_channel_mapping_t>
aidl2legacy_MicrophoneDynamicInfoChannelMapping_audio_microphone_channel_mapping_t(MicrophoneDynamicInfo::ChannelMapping aidl)3128 aidl2legacy_MicrophoneDynamicInfoChannelMapping_audio_microphone_channel_mapping_t(
3129         MicrophoneDynamicInfo::ChannelMapping aidl) {
3130     switch (aidl) {
3131         case MicrophoneDynamicInfo::ChannelMapping::UNUSED:
3132             return AUDIO_MICROPHONE_CHANNEL_MAPPING_UNUSED;
3133         case MicrophoneDynamicInfo::ChannelMapping::DIRECT:
3134             return AUDIO_MICROPHONE_CHANNEL_MAPPING_DIRECT;
3135         case MicrophoneDynamicInfo::ChannelMapping::PROCESSED:
3136             return AUDIO_MICROPHONE_CHANNEL_MAPPING_PROCESSED;
3137     }
3138     return unexpected(BAD_VALUE);
3139 }
3140 ConversionResult<MicrophoneDynamicInfo::ChannelMapping>
legacy2aidl_audio_microphone_channel_mapping_t_MicrophoneDynamicInfoChannelMapping(audio_microphone_channel_mapping_t legacy)3141 legacy2aidl_audio_microphone_channel_mapping_t_MicrophoneDynamicInfoChannelMapping(
3142         audio_microphone_channel_mapping_t legacy) {
3143     switch (legacy) {
3144         case AUDIO_MICROPHONE_CHANNEL_MAPPING_UNUSED:
3145             return MicrophoneDynamicInfo::ChannelMapping::UNUSED;
3146         case AUDIO_MICROPHONE_CHANNEL_MAPPING_DIRECT:
3147             return MicrophoneDynamicInfo::ChannelMapping::DIRECT;
3148         case AUDIO_MICROPHONE_CHANNEL_MAPPING_PROCESSED:
3149             return MicrophoneDynamicInfo::ChannelMapping::PROCESSED;
3150     }
3151     return unexpected(BAD_VALUE);
3152 }
3153 
3154 ConversionResult<audio_microphone_characteristic_t>
aidl2legacy_MicrophoneInfos_audio_microphone_characteristic_t(const MicrophoneInfo & aidlInfo,const MicrophoneDynamicInfo & aidlDynamic)3155 aidl2legacy_MicrophoneInfos_audio_microphone_characteristic_t(
3156         const MicrophoneInfo& aidlInfo, const MicrophoneDynamicInfo& aidlDynamic) {
3157     static const audio_microphone_coordinate kCoordinateUnknown = {
3158         AUDIO_MICROPHONE_COORDINATE_UNKNOWN, AUDIO_MICROPHONE_COORDINATE_UNKNOWN,
3159         AUDIO_MICROPHONE_COORDINATE_UNKNOWN };
3160     audio_microphone_characteristic_t legacy{};
3161     if (aidlInfo.id != aidlDynamic.id) {
3162         return unexpected(BAD_VALUE);
3163     }
3164     // Note: in the legacy structure, 'device_id' is the mic's ID, 'id' is APM port id.
3165     RETURN_IF_ERROR(aidl2legacy_string(aidlInfo.id, legacy.device_id, AUDIO_MICROPHONE_ID_MAX_LEN));
3166     RETURN_IF_ERROR(aidl2legacy_AudioDevice_audio_device(
3167                     aidlInfo.device, &legacy.device, legacy.address));
3168     legacy.location = VALUE_OR_RETURN(
3169             aidl2legacy_MicrophoneInfoLocation_audio_microphone_location_t(aidlInfo.location));
3170     legacy.group = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_microphone_group_t(aidlInfo.group));
3171     // For some reason, the legacy field is unsigned, however in the SDK layer it is signed,
3172     // as it is in AIDL. So, use UINT_MAX for INDEX_IN_THE_GROUP_UNKNOWN which is -1.
3173     if (aidlInfo.indexInTheGroup != MicrophoneInfo::INDEX_IN_THE_GROUP_UNKNOWN) {
3174         legacy.index_in_the_group = VALUE_OR_RETURN(
3175                 convertReinterpret<unsigned int>(aidlInfo.indexInTheGroup));
3176     } else {
3177         legacy.index_in_the_group = UINT_MAX;
3178     }
3179     if (aidlInfo.sensitivity.has_value()) {
3180         legacy.sensitivity = aidlInfo.sensitivity.value().leveldBFS;
3181         legacy.max_spl = aidlInfo.sensitivity.value().maxSpldB;
3182         legacy.min_spl = aidlInfo.sensitivity.value().minSpldB;
3183     } else {
3184         legacy.sensitivity = AUDIO_MICROPHONE_SENSITIVITY_UNKNOWN;
3185         legacy.max_spl = AUDIO_MICROPHONE_SPL_UNKNOWN;
3186         legacy.min_spl = AUDIO_MICROPHONE_SPL_UNKNOWN;
3187     }
3188     legacy.directionality = VALUE_OR_RETURN(
3189             aidl2legacy_MicrophoneInfoDirectionality_audio_microphone_directionality_t(
3190                     aidlInfo.directionality));
3191     if (aidlInfo.frequencyResponse.size() > AUDIO_MICROPHONE_MAX_FREQUENCY_RESPONSES) {
3192         return unexpected(BAD_VALUE);
3193     }
3194     legacy.num_frequency_responses = 0;
3195     for (const auto& p: aidlInfo.frequencyResponse) {
3196         legacy.frequency_responses[0][legacy.num_frequency_responses] = p.frequencyHz;
3197         legacy.frequency_responses[1][legacy.num_frequency_responses++] = p.leveldB;
3198     }
3199     if (aidlInfo.position.has_value()) {
3200         legacy.geometric_location = VALUE_OR_RETURN(
3201                 aidl2legacy_MicrophoneInfoCoordinate_audio_microphone_coordinate(
3202                         aidlInfo.position.value()));
3203     } else {
3204         legacy.geometric_location = kCoordinateUnknown;
3205     }
3206     if (aidlInfo.orientation.has_value()) {
3207         legacy.orientation = VALUE_OR_RETURN(
3208                 aidl2legacy_MicrophoneInfoCoordinate_audio_microphone_coordinate(
3209                         aidlInfo.orientation.value()));
3210     } else {
3211         legacy.orientation = kCoordinateUnknown;
3212     }
3213     if (aidlDynamic.channelMapping.size() > AUDIO_CHANNEL_COUNT_MAX) {
3214         return unexpected(BAD_VALUE);
3215     }
3216     size_t i = 0;
3217     for (; i < aidlDynamic.channelMapping.size(); ++i) {
3218         legacy.channel_mapping[i] = VALUE_OR_RETURN(
3219                 aidl2legacy_MicrophoneDynamicInfoChannelMapping_audio_microphone_channel_mapping_t(
3220                         aidlDynamic.channelMapping[i]));
3221     }
3222     for (; i < AUDIO_CHANNEL_COUNT_MAX; ++i) {
3223         legacy.channel_mapping[i] = AUDIO_MICROPHONE_CHANNEL_MAPPING_UNUSED;
3224     }
3225     return legacy;
3226 }
3227 
3228 status_t
legacy2aidl_audio_microphone_characteristic_t_MicrophoneInfos(const audio_microphone_characteristic_t & legacy,MicrophoneInfo * aidlInfo,MicrophoneDynamicInfo * aidlDynamic)3229 legacy2aidl_audio_microphone_characteristic_t_MicrophoneInfos(
3230         const audio_microphone_characteristic_t& legacy,
3231         MicrophoneInfo* aidlInfo, MicrophoneDynamicInfo* aidlDynamic) {
3232     aidlInfo->id = VALUE_OR_RETURN_STATUS(
3233             legacy2aidl_string(legacy.device_id, AUDIO_MICROPHONE_ID_MAX_LEN));
3234     aidlDynamic->id = aidlInfo->id;
3235     aidlInfo->device = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_device_AudioDevice(
3236                     legacy.device, legacy.address));
3237     aidlInfo->location = VALUE_OR_RETURN_STATUS(
3238             legacy2aidl_audio_microphone_location_t_MicrophoneInfoLocation(legacy.location));
3239     aidlInfo->group = VALUE_OR_RETURN_STATUS(
3240             legacy2aidl_audio_microphone_group_t_int32_t(legacy.group));
3241     // For some reason, the legacy field is unsigned, however in the SDK layer it is signed,
3242     // as it is in AIDL. So, use UINT_MAX for INDEX_IN_THE_GROUP_UNKNOWN which is -1.
3243     if (legacy.index_in_the_group != UINT_MAX) {
3244         aidlInfo->indexInTheGroup = VALUE_OR_RETURN_STATUS(
3245                 convertReinterpret<int32_t>(legacy.index_in_the_group));
3246     } else {
3247         aidlInfo->indexInTheGroup = MicrophoneInfo::INDEX_IN_THE_GROUP_UNKNOWN;
3248     }
3249     if (legacy.sensitivity != AUDIO_MICROPHONE_SENSITIVITY_UNKNOWN &&
3250             legacy.max_spl != AUDIO_MICROPHONE_SPL_UNKNOWN &&
3251             legacy.min_spl != AUDIO_MICROPHONE_SPL_UNKNOWN) {
3252         MicrophoneInfo::Sensitivity sensitivity;
3253         sensitivity.leveldBFS = legacy.sensitivity;
3254         sensitivity.maxSpldB = legacy.max_spl;
3255         sensitivity.minSpldB = legacy.min_spl;
3256         aidlInfo->sensitivity = std::move(sensitivity);
3257     } else {
3258         aidlInfo->sensitivity = {};
3259     }
3260     aidlInfo->directionality = VALUE_OR_RETURN_STATUS(
3261             legacy2aidl_audio_microphone_directionality_t_MicrophoneInfoDirectionality(
3262                     legacy.directionality));
3263     if (legacy.num_frequency_responses > AUDIO_MICROPHONE_MAX_FREQUENCY_RESPONSES) {
3264         return BAD_VALUE;
3265     }
3266     aidlInfo->frequencyResponse.resize(legacy.num_frequency_responses);
3267     for (size_t i = 0; i < legacy.num_frequency_responses; ++i) {
3268         aidlInfo->frequencyResponse[i].frequencyHz = legacy.frequency_responses[0][i];
3269         aidlInfo->frequencyResponse[i].leveldB = legacy.frequency_responses[1][i];
3270     }
3271     if (legacy.geometric_location.x != AUDIO_MICROPHONE_COORDINATE_UNKNOWN &&
3272             legacy.geometric_location.y != AUDIO_MICROPHONE_COORDINATE_UNKNOWN &&
3273             legacy.geometric_location.z != AUDIO_MICROPHONE_COORDINATE_UNKNOWN) {
3274         aidlInfo->position = VALUE_OR_RETURN_STATUS(
3275                 legacy2aidl_audio_microphone_coordinate_MicrophoneInfoCoordinate(
3276                         legacy.geometric_location));
3277     } else {
3278         aidlInfo->position = {};
3279     }
3280     if (legacy.orientation.x != AUDIO_MICROPHONE_COORDINATE_UNKNOWN &&
3281             legacy.orientation.y != AUDIO_MICROPHONE_COORDINATE_UNKNOWN &&
3282             legacy.orientation.z != AUDIO_MICROPHONE_COORDINATE_UNKNOWN) {
3283         aidlInfo->orientation = VALUE_OR_RETURN_STATUS(
3284                 legacy2aidl_audio_microphone_coordinate_MicrophoneInfoCoordinate(
3285                         legacy.orientation));
3286     } else {
3287         aidlInfo->orientation = {};
3288     }
3289     size_t channelsUsed = AUDIO_CHANNEL_COUNT_MAX;
3290     while (channelsUsed != 0 &&
3291             legacy.channel_mapping[--channelsUsed] == AUDIO_MICROPHONE_CHANNEL_MAPPING_UNUSED) {}
3292     // Doing an increment is correct even when channel 0 is 'UNUSED',
3293     // that's because AIDL requires to have at least 1 element in the mapping.
3294     ++channelsUsed;
3295     aidlDynamic->channelMapping.resize(channelsUsed);
3296     for (size_t i = 0; i < channelsUsed; ++i) {
3297         aidlDynamic->channelMapping[i] = VALUE_OR_RETURN_STATUS(
3298                 legacy2aidl_audio_microphone_channel_mapping_t_MicrophoneDynamicInfoChannelMapping(
3299                         legacy.channel_mapping[i]));
3300     }
3301     return OK;
3302 }
3303 
3304 }  // namespace android
3305 
3306 #undef GET_DEVICE_DESC_CONNECTION
3307 
3308 #if defined(BACKEND_NDK)
3309 }  // aidl
3310 #endif
3311