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