1 /*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define LOG_TAG "AidlConversion"
18 //#define LOG_NDEBUG 0
19 #include <utils/Log.h>
20
21 #include "media/AidlConversion.h"
22
23 #include <media/ShmemCompat.h>
24
25 ////////////////////////////////////////////////////////////////////////////////////////////////////
26 // Utilities
27
28 namespace android {
29
30 using base::unexpected;
31
32 namespace {
33
34 enum class Direction {
35 INPUT, OUTPUT
36 };
37
direction(media::AudioPortRole role,media::AudioPortType type)38 ConversionResult<Direction> direction(media::AudioPortRole role, media::AudioPortType type) {
39 switch (type) {
40 case media::AudioPortType::NONE:
41 case media::AudioPortType::SESSION:
42 break; // must be listed -Werror,-Wswitch
43 case media::AudioPortType::DEVICE:
44 switch (role) {
45 case media::AudioPortRole::NONE:
46 break; // must be listed -Werror,-Wswitch
47 case media::AudioPortRole::SOURCE:
48 return Direction::INPUT;
49 case media::AudioPortRole::SINK:
50 return Direction::OUTPUT;
51 }
52 break;
53 case media::AudioPortType::MIX:
54 switch (role) {
55 case media::AudioPortRole::NONE:
56 break; // must be listed -Werror,-Wswitch
57 case media::AudioPortRole::SOURCE:
58 return Direction::OUTPUT;
59 case media::AudioPortRole::SINK:
60 return Direction::INPUT;
61 }
62 break;
63 }
64 return unexpected(BAD_VALUE);
65 }
66
direction(audio_port_role_t role,audio_port_type_t type)67 ConversionResult<Direction> direction(audio_port_role_t role, audio_port_type_t type) {
68 switch (type) {
69 case AUDIO_PORT_TYPE_NONE:
70 case AUDIO_PORT_TYPE_SESSION:
71 break; // must be listed -Werror,-Wswitch
72 case AUDIO_PORT_TYPE_DEVICE:
73 switch (role) {
74 case AUDIO_PORT_ROLE_NONE:
75 break; // must be listed -Werror,-Wswitch
76 case AUDIO_PORT_ROLE_SOURCE:
77 return Direction::INPUT;
78 case AUDIO_PORT_ROLE_SINK:
79 return Direction::OUTPUT;
80 }
81 break;
82 case AUDIO_PORT_TYPE_MIX:
83 switch (role) {
84 case AUDIO_PORT_ROLE_NONE:
85 break; // must be listed -Werror,-Wswitch
86 case AUDIO_PORT_ROLE_SOURCE:
87 return Direction::OUTPUT;
88 case AUDIO_PORT_ROLE_SINK:
89 return Direction::INPUT;
90 }
91 break;
92 }
93 return unexpected(BAD_VALUE);
94 }
95
96 } // namespace
97
98 ////////////////////////////////////////////////////////////////////////////////////////////////////
99 // Converters
100
aidl2legacy_string(std::string_view aidl,char * dest,size_t maxSize)101 status_t aidl2legacy_string(std::string_view aidl, char* dest, size_t maxSize) {
102 if (aidl.size() > maxSize - 1) {
103 return BAD_VALUE;
104 }
105 aidl.copy(dest, aidl.size());
106 dest[aidl.size()] = '\0';
107 return OK;
108 }
109
legacy2aidl_string(const char * legacy,size_t maxSize)110 ConversionResult<std::string> legacy2aidl_string(const char* legacy, size_t maxSize) {
111 if (legacy == nullptr) {
112 return unexpected(BAD_VALUE);
113 }
114 if (strnlen(legacy, maxSize) == maxSize) {
115 // No null-terminator.
116 return unexpected(BAD_VALUE);
117 }
118 return std::string(legacy);
119 }
120
aidl2legacy_int32_t_audio_module_handle_t(int32_t aidl)121 ConversionResult<audio_module_handle_t> aidl2legacy_int32_t_audio_module_handle_t(int32_t aidl) {
122 return convertReinterpret<audio_module_handle_t>(aidl);
123 }
124
legacy2aidl_audio_module_handle_t_int32_t(audio_module_handle_t legacy)125 ConversionResult<int32_t> legacy2aidl_audio_module_handle_t_int32_t(audio_module_handle_t legacy) {
126 return convertReinterpret<int32_t>(legacy);
127 }
128
aidl2legacy_int32_t_audio_io_handle_t(int32_t aidl)129 ConversionResult<audio_io_handle_t> aidl2legacy_int32_t_audio_io_handle_t(int32_t aidl) {
130 return convertReinterpret<audio_io_handle_t>(aidl);
131 }
132
legacy2aidl_audio_io_handle_t_int32_t(audio_io_handle_t legacy)133 ConversionResult<int32_t> legacy2aidl_audio_io_handle_t_int32_t(audio_io_handle_t legacy) {
134 return convertReinterpret<int32_t>(legacy);
135 }
136
aidl2legacy_int32_t_audio_port_handle_t(int32_t aidl)137 ConversionResult<audio_port_handle_t> aidl2legacy_int32_t_audio_port_handle_t(int32_t aidl) {
138 return convertReinterpret<audio_port_handle_t>(aidl);
139 }
140
legacy2aidl_audio_port_handle_t_int32_t(audio_port_handle_t legacy)141 ConversionResult<int32_t> legacy2aidl_audio_port_handle_t_int32_t(audio_port_handle_t legacy) {
142 return convertReinterpret<int32_t>(legacy);
143 }
144
aidl2legacy_int32_t_audio_patch_handle_t(int32_t aidl)145 ConversionResult<audio_patch_handle_t> aidl2legacy_int32_t_audio_patch_handle_t(int32_t aidl) {
146 return convertReinterpret<audio_patch_handle_t>(aidl);
147 }
148
legacy2aidl_audio_patch_handle_t_int32_t(audio_patch_handle_t legacy)149 ConversionResult<int32_t> legacy2aidl_audio_patch_handle_t_int32_t(audio_patch_handle_t legacy) {
150 return convertReinterpret<int32_t>(legacy);
151 }
152
aidl2legacy_int32_t_audio_unique_id_t(int32_t aidl)153 ConversionResult<audio_unique_id_t> aidl2legacy_int32_t_audio_unique_id_t(int32_t aidl) {
154 return convertReinterpret<audio_unique_id_t>(aidl);
155 }
156
legacy2aidl_audio_unique_id_t_int32_t(audio_unique_id_t legacy)157 ConversionResult<int32_t> legacy2aidl_audio_unique_id_t_int32_t(audio_unique_id_t legacy) {
158 return convertReinterpret<int32_t>(legacy);
159 }
160
aidl2legacy_int32_t_audio_hw_sync_t(int32_t aidl)161 ConversionResult<audio_hw_sync_t> aidl2legacy_int32_t_audio_hw_sync_t(int32_t aidl) {
162 return convertReinterpret<audio_hw_sync_t>(aidl);
163 }
164
legacy2aidl_audio_hw_sync_t_int32_t(audio_hw_sync_t legacy)165 ConversionResult<int32_t> legacy2aidl_audio_hw_sync_t_int32_t(audio_hw_sync_t legacy) {
166 return convertReinterpret<int32_t>(legacy);
167 }
168
aidl2legacy_int32_t_pid_t(int32_t aidl)169 ConversionResult<pid_t> aidl2legacy_int32_t_pid_t(int32_t aidl) {
170 return convertReinterpret<pid_t>(aidl);
171 }
172
legacy2aidl_pid_t_int32_t(pid_t legacy)173 ConversionResult<int32_t> legacy2aidl_pid_t_int32_t(pid_t legacy) {
174 return convertReinterpret<int32_t>(legacy);
175 }
176
aidl2legacy_int32_t_uid_t(int32_t aidl)177 ConversionResult<uid_t> aidl2legacy_int32_t_uid_t(int32_t aidl) {
178 return convertReinterpret<uid_t>(aidl);
179 }
180
legacy2aidl_uid_t_int32_t(uid_t legacy)181 ConversionResult<int32_t> legacy2aidl_uid_t_int32_t(uid_t legacy) {
182 return convertReinterpret<int32_t>(legacy);
183 }
184
aidl2legacy_string_view_String16(std::string_view aidl)185 ConversionResult<String16> aidl2legacy_string_view_String16(std::string_view aidl) {
186 return String16(aidl.data(), aidl.size());
187 }
188
legacy2aidl_String16_string(const String16 & legacy)189 ConversionResult<std::string> legacy2aidl_String16_string(const String16& legacy) {
190 return std::string(String8(legacy).c_str());
191 }
192
193 // TODO b/182392769: create an optional -> optional util
194 ConversionResult<std::optional<String16>>
aidl2legacy_optional_string_view_optional_String16(std::optional<std::string_view> aidl)195 aidl2legacy_optional_string_view_optional_String16(std::optional<std::string_view> aidl) {
196 if (!aidl.has_value()) {
197 return std::nullopt;
198 }
199 ConversionResult<String16> conversion =
200 VALUE_OR_RETURN(aidl2legacy_string_view_String16(aidl.value()));
201 return conversion.value();
202 }
203
204 ConversionResult<std::optional<std::string_view>>
legacy2aidl_optional_String16_optional_string(std::optional<String16> legacy)205 legacy2aidl_optional_String16_optional_string(std::optional<String16> legacy) {
206 if (!legacy.has_value()) {
207 return std::nullopt;
208 }
209 ConversionResult<std::string> conversion =
210 VALUE_OR_RETURN(legacy2aidl_String16_string(legacy.value()));
211 return conversion.value();
212 }
213
aidl2legacy_string_view_String8(std::string_view aidl)214 ConversionResult<String8> aidl2legacy_string_view_String8(std::string_view aidl) {
215 return String8(aidl.data(), aidl.size());
216 }
217
legacy2aidl_String8_string(const String8 & legacy)218 ConversionResult<std::string> legacy2aidl_String8_string(const String8& legacy) {
219 return std::string(legacy.c_str());
220 }
221
222 // The legacy enum is unnamed. Thus, we use int32_t.
aidl2legacy_AudioPortConfigType_int32_t(media::AudioPortConfigType aidl)223 ConversionResult<int32_t> aidl2legacy_AudioPortConfigType_int32_t(
224 media::AudioPortConfigType aidl) {
225 switch (aidl) {
226 case media::AudioPortConfigType::SAMPLE_RATE:
227 return AUDIO_PORT_CONFIG_SAMPLE_RATE;
228 case media::AudioPortConfigType::CHANNEL_MASK:
229 return AUDIO_PORT_CONFIG_CHANNEL_MASK;
230 case media::AudioPortConfigType::FORMAT:
231 return AUDIO_PORT_CONFIG_FORMAT;
232 case media::AudioPortConfigType::GAIN:
233 return AUDIO_PORT_CONFIG_GAIN;
234 case media::AudioPortConfigType::FLAGS:
235 return AUDIO_PORT_CONFIG_FLAGS;
236 }
237 return unexpected(BAD_VALUE);
238 }
239
240 // The legacy enum is unnamed. Thus, we use int32_t.
legacy2aidl_int32_t_AudioPortConfigType(int32_t legacy)241 ConversionResult<media::AudioPortConfigType> legacy2aidl_int32_t_AudioPortConfigType(
242 int32_t legacy) {
243 switch (legacy) {
244 case AUDIO_PORT_CONFIG_SAMPLE_RATE:
245 return media::AudioPortConfigType::SAMPLE_RATE;
246 case AUDIO_PORT_CONFIG_CHANNEL_MASK:
247 return media::AudioPortConfigType::CHANNEL_MASK;
248 case AUDIO_PORT_CONFIG_FORMAT:
249 return media::AudioPortConfigType::FORMAT;
250 case AUDIO_PORT_CONFIG_GAIN:
251 return media::AudioPortConfigType::GAIN;
252 case AUDIO_PORT_CONFIG_FLAGS:
253 return media::AudioPortConfigType::FLAGS;
254 }
255 return unexpected(BAD_VALUE);
256 }
257
aidl2legacy_int32_t_config_mask(int32_t aidl)258 ConversionResult<unsigned int> aidl2legacy_int32_t_config_mask(int32_t aidl) {
259 return convertBitmask<unsigned int, int32_t, int, media::AudioPortConfigType>(
260 aidl, aidl2legacy_AudioPortConfigType_int32_t,
261 // AudioPortConfigType enum is index-based.
262 indexToEnum_index<media::AudioPortConfigType>,
263 // AUDIO_PORT_CONFIG_* flags are mask-based.
264 enumToMask_bitmask<unsigned int, int>);
265 }
266
legacy2aidl_config_mask_int32_t(unsigned int legacy)267 ConversionResult<int32_t> legacy2aidl_config_mask_int32_t(unsigned int legacy) {
268 return convertBitmask<int32_t, unsigned int, media::AudioPortConfigType, int>(
269 legacy, legacy2aidl_int32_t_AudioPortConfigType,
270 // AUDIO_PORT_CONFIG_* flags are mask-based.
271 indexToEnum_bitmask<unsigned>,
272 // AudioPortConfigType enum is index-based.
273 enumToMask_index<int32_t, media::AudioPortConfigType>);
274 }
275
aidl2legacy_int32_t_audio_channel_mask_t(int32_t aidl)276 ConversionResult<audio_channel_mask_t> aidl2legacy_int32_t_audio_channel_mask_t(int32_t aidl) {
277 // TODO(ytai): should we convert bit-by-bit?
278 // One problem here is that the representation is both opaque and is different based on the
279 // context (input vs. output). Can determine based on type and role, as per useInChannelMask().
280 return convertReinterpret<audio_channel_mask_t>(aidl);
281 }
282
legacy2aidl_audio_channel_mask_t_int32_t(audio_channel_mask_t legacy)283 ConversionResult<int32_t> legacy2aidl_audio_channel_mask_t_int32_t(audio_channel_mask_t legacy) {
284 // TODO(ytai): should we convert bit-by-bit?
285 // One problem here is that the representation is both opaque and is different based on the
286 // context (input vs. output). Can determine based on type and role, as per useInChannelMask().
287 return convertReinterpret<int32_t>(legacy);
288 }
289
aidl2legacy_AudioIoConfigEvent_audio_io_config_event(media::AudioIoConfigEvent aidl)290 ConversionResult<audio_io_config_event> aidl2legacy_AudioIoConfigEvent_audio_io_config_event(
291 media::AudioIoConfigEvent aidl) {
292 switch (aidl) {
293 case media::AudioIoConfigEvent::OUTPUT_REGISTERED:
294 return AUDIO_OUTPUT_REGISTERED;
295 case media::AudioIoConfigEvent::OUTPUT_OPENED:
296 return AUDIO_OUTPUT_OPENED;
297 case media::AudioIoConfigEvent::OUTPUT_CLOSED:
298 return AUDIO_OUTPUT_CLOSED;
299 case media::AudioIoConfigEvent::OUTPUT_CONFIG_CHANGED:
300 return AUDIO_OUTPUT_CONFIG_CHANGED;
301 case media::AudioIoConfigEvent::INPUT_REGISTERED:
302 return AUDIO_INPUT_REGISTERED;
303 case media::AudioIoConfigEvent::INPUT_OPENED:
304 return AUDIO_INPUT_OPENED;
305 case media::AudioIoConfigEvent::INPUT_CLOSED:
306 return AUDIO_INPUT_CLOSED;
307 case media::AudioIoConfigEvent::INPUT_CONFIG_CHANGED:
308 return AUDIO_INPUT_CONFIG_CHANGED;
309 case media::AudioIoConfigEvent::CLIENT_STARTED:
310 return AUDIO_CLIENT_STARTED;
311 }
312 return unexpected(BAD_VALUE);
313 }
314
legacy2aidl_audio_io_config_event_AudioIoConfigEvent(audio_io_config_event legacy)315 ConversionResult<media::AudioIoConfigEvent> legacy2aidl_audio_io_config_event_AudioIoConfigEvent(
316 audio_io_config_event legacy) {
317 switch (legacy) {
318 case AUDIO_OUTPUT_REGISTERED:
319 return media::AudioIoConfigEvent::OUTPUT_REGISTERED;
320 case AUDIO_OUTPUT_OPENED:
321 return media::AudioIoConfigEvent::OUTPUT_OPENED;
322 case AUDIO_OUTPUT_CLOSED:
323 return media::AudioIoConfigEvent::OUTPUT_CLOSED;
324 case AUDIO_OUTPUT_CONFIG_CHANGED:
325 return media::AudioIoConfigEvent::OUTPUT_CONFIG_CHANGED;
326 case AUDIO_INPUT_REGISTERED:
327 return media::AudioIoConfigEvent::INPUT_REGISTERED;
328 case AUDIO_INPUT_OPENED:
329 return media::AudioIoConfigEvent::INPUT_OPENED;
330 case AUDIO_INPUT_CLOSED:
331 return media::AudioIoConfigEvent::INPUT_CLOSED;
332 case AUDIO_INPUT_CONFIG_CHANGED:
333 return media::AudioIoConfigEvent::INPUT_CONFIG_CHANGED;
334 case AUDIO_CLIENT_STARTED:
335 return media::AudioIoConfigEvent::CLIENT_STARTED;
336 }
337 return unexpected(BAD_VALUE);
338 }
339
aidl2legacy_AudioPortRole_audio_port_role_t(media::AudioPortRole aidl)340 ConversionResult<audio_port_role_t> aidl2legacy_AudioPortRole_audio_port_role_t(
341 media::AudioPortRole aidl) {
342 switch (aidl) {
343 case media::AudioPortRole::NONE:
344 return AUDIO_PORT_ROLE_NONE;
345 case media::AudioPortRole::SOURCE:
346 return AUDIO_PORT_ROLE_SOURCE;
347 case media::AudioPortRole::SINK:
348 return AUDIO_PORT_ROLE_SINK;
349 }
350 return unexpected(BAD_VALUE);
351 }
352
legacy2aidl_audio_port_role_t_AudioPortRole(audio_port_role_t legacy)353 ConversionResult<media::AudioPortRole> legacy2aidl_audio_port_role_t_AudioPortRole(
354 audio_port_role_t legacy) {
355 switch (legacy) {
356 case AUDIO_PORT_ROLE_NONE:
357 return media::AudioPortRole::NONE;
358 case AUDIO_PORT_ROLE_SOURCE:
359 return media::AudioPortRole::SOURCE;
360 case AUDIO_PORT_ROLE_SINK:
361 return media::AudioPortRole::SINK;
362 }
363 return unexpected(BAD_VALUE);
364 }
365
aidl2legacy_AudioPortType_audio_port_type_t(media::AudioPortType aidl)366 ConversionResult<audio_port_type_t> aidl2legacy_AudioPortType_audio_port_type_t(
367 media::AudioPortType aidl) {
368 switch (aidl) {
369 case media::AudioPortType::NONE:
370 return AUDIO_PORT_TYPE_NONE;
371 case media::AudioPortType::DEVICE:
372 return AUDIO_PORT_TYPE_DEVICE;
373 case media::AudioPortType::MIX:
374 return AUDIO_PORT_TYPE_MIX;
375 case media::AudioPortType::SESSION:
376 return AUDIO_PORT_TYPE_SESSION;
377 }
378 return unexpected(BAD_VALUE);
379 }
380
legacy2aidl_audio_port_type_t_AudioPortType(audio_port_type_t legacy)381 ConversionResult<media::AudioPortType> legacy2aidl_audio_port_type_t_AudioPortType(
382 audio_port_type_t legacy) {
383 switch (legacy) {
384 case AUDIO_PORT_TYPE_NONE:
385 return media::AudioPortType::NONE;
386 case AUDIO_PORT_TYPE_DEVICE:
387 return media::AudioPortType::DEVICE;
388 case AUDIO_PORT_TYPE_MIX:
389 return media::AudioPortType::MIX;
390 case AUDIO_PORT_TYPE_SESSION:
391 return media::AudioPortType::SESSION;
392 }
393 return unexpected(BAD_VALUE);
394 }
395
aidl2legacy_AudioFormat_audio_format_t(media::audio::common::AudioFormat aidl)396 ConversionResult<audio_format_t> aidl2legacy_AudioFormat_audio_format_t(
397 media::audio::common::AudioFormat aidl) {
398 // This relies on AudioFormat being kept in sync with audio_format_t.
399 static_assert(sizeof(media::audio::common::AudioFormat) == sizeof(audio_format_t));
400 return static_cast<audio_format_t>(aidl);
401 }
402
legacy2aidl_audio_format_t_AudioFormat(audio_format_t legacy)403 ConversionResult<media::audio::common::AudioFormat> legacy2aidl_audio_format_t_AudioFormat(
404 audio_format_t legacy) {
405 // This relies on AudioFormat being kept in sync with audio_format_t.
406 static_assert(sizeof(media::audio::common::AudioFormat) == sizeof(audio_format_t));
407 return static_cast<media::audio::common::AudioFormat>(legacy);
408 }
409
aidl2legacy_AudioGainMode_audio_gain_mode_t(media::AudioGainMode aidl)410 ConversionResult<audio_gain_mode_t> aidl2legacy_AudioGainMode_audio_gain_mode_t(media::AudioGainMode aidl) {
411 switch (aidl) {
412 case media::AudioGainMode::JOINT:
413 return AUDIO_GAIN_MODE_JOINT;
414 case media::AudioGainMode::CHANNELS:
415 return AUDIO_GAIN_MODE_CHANNELS;
416 case media::AudioGainMode::RAMP:
417 return AUDIO_GAIN_MODE_RAMP;
418 }
419 return unexpected(BAD_VALUE);
420 }
421
legacy2aidl_audio_gain_mode_t_AudioGainMode(audio_gain_mode_t legacy)422 ConversionResult<media::AudioGainMode> legacy2aidl_audio_gain_mode_t_AudioGainMode(audio_gain_mode_t legacy) {
423 switch (legacy) {
424 case AUDIO_GAIN_MODE_JOINT:
425 return media::AudioGainMode::JOINT;
426 case AUDIO_GAIN_MODE_CHANNELS:
427 return media::AudioGainMode::CHANNELS;
428 case AUDIO_GAIN_MODE_RAMP:
429 return media::AudioGainMode::RAMP;
430 }
431 return unexpected(BAD_VALUE);
432 }
433
aidl2legacy_int32_t_audio_gain_mode_t_mask(int32_t aidl)434 ConversionResult<audio_gain_mode_t> aidl2legacy_int32_t_audio_gain_mode_t_mask(int32_t aidl) {
435 return convertBitmask<audio_gain_mode_t, int32_t, audio_gain_mode_t, media::AudioGainMode>(
436 aidl, aidl2legacy_AudioGainMode_audio_gain_mode_t,
437 // AudioGainMode is index-based.
438 indexToEnum_index<media::AudioGainMode>,
439 // AUDIO_GAIN_MODE_* constants are mask-based.
440 enumToMask_bitmask<audio_gain_mode_t, audio_gain_mode_t>);
441 }
442
legacy2aidl_audio_gain_mode_t_int32_t_mask(audio_gain_mode_t legacy)443 ConversionResult<int32_t> legacy2aidl_audio_gain_mode_t_int32_t_mask(audio_gain_mode_t legacy) {
444 return convertBitmask<int32_t, audio_gain_mode_t, media::AudioGainMode, audio_gain_mode_t>(
445 legacy, legacy2aidl_audio_gain_mode_t_AudioGainMode,
446 // AUDIO_GAIN_MODE_* constants are mask-based.
447 indexToEnum_bitmask<audio_gain_mode_t>,
448 // AudioGainMode is index-based.
449 enumToMask_index<int32_t, media::AudioGainMode>);
450 }
451
aidl2legacy_int32_t_audio_devices_t(int32_t aidl)452 ConversionResult<audio_devices_t> aidl2legacy_int32_t_audio_devices_t(int32_t aidl) {
453 // TODO(ytai): bitfield?
454 return convertReinterpret<audio_devices_t>(aidl);
455 }
456
legacy2aidl_audio_devices_t_int32_t(audio_devices_t legacy)457 ConversionResult<int32_t> legacy2aidl_audio_devices_t_int32_t(audio_devices_t legacy) {
458 // TODO(ytai): bitfield?
459 return convertReinterpret<int32_t>(legacy);
460 }
461
aidl2legacy_AudioGainConfig_audio_gain_config(const media::AudioGainConfig & aidl,media::AudioPortRole role,media::AudioPortType type)462 ConversionResult<audio_gain_config> aidl2legacy_AudioGainConfig_audio_gain_config(
463 const media::AudioGainConfig& aidl, media::AudioPortRole role, media::AudioPortType type) {
464 audio_gain_config legacy;
465 legacy.index = VALUE_OR_RETURN(convertIntegral<int>(aidl.index));
466 legacy.mode = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_gain_mode_t_mask(aidl.mode));
467 legacy.channel_mask =
468 VALUE_OR_RETURN(aidl2legacy_int32_t_audio_channel_mask_t(aidl.channelMask));
469 const bool isInput = VALUE_OR_RETURN(direction(role, type)) == Direction::INPUT;
470 const bool isJoint = bitmaskIsSet(aidl.mode, media::AudioGainMode::JOINT);
471 size_t numValues = isJoint ? 1
472 : isInput ? audio_channel_count_from_in_mask(legacy.channel_mask)
473 : audio_channel_count_from_out_mask(legacy.channel_mask);
474 if (aidl.values.size() != numValues || aidl.values.size() > std::size(legacy.values)) {
475 return unexpected(BAD_VALUE);
476 }
477 for (size_t i = 0; i < numValues; ++i) {
478 legacy.values[i] = VALUE_OR_RETURN(convertIntegral<int>(aidl.values[i]));
479 }
480 legacy.ramp_duration_ms = VALUE_OR_RETURN(convertIntegral<unsigned int>(aidl.rampDurationMs));
481 return legacy;
482 }
483
legacy2aidl_audio_gain_config_AudioGainConfig(const audio_gain_config & legacy,audio_port_role_t role,audio_port_type_t type)484 ConversionResult<media::AudioGainConfig> legacy2aidl_audio_gain_config_AudioGainConfig(
485 const audio_gain_config& legacy, audio_port_role_t role, audio_port_type_t type) {
486 media::AudioGainConfig aidl;
487 aidl.index = VALUE_OR_RETURN(convertIntegral<int32_t>(legacy.index));
488 aidl.mode = VALUE_OR_RETURN(legacy2aidl_audio_gain_mode_t_int32_t_mask(legacy.mode));
489 aidl.channelMask =
490 VALUE_OR_RETURN(legacy2aidl_audio_channel_mask_t_int32_t(legacy.channel_mask));
491 const bool isInput = VALUE_OR_RETURN(direction(role, type)) == Direction::INPUT;
492 const bool isJoint = (legacy.mode & AUDIO_GAIN_MODE_JOINT) != 0;
493 size_t numValues = isJoint ? 1
494 : isInput ? audio_channel_count_from_in_mask(legacy.channel_mask)
495 : audio_channel_count_from_out_mask(legacy.channel_mask);
496 aidl.values.resize(numValues);
497 for (size_t i = 0; i < numValues; ++i) {
498 aidl.values[i] = VALUE_OR_RETURN(convertIntegral<int32_t>(legacy.values[i]));
499 }
500 aidl.rampDurationMs = VALUE_OR_RETURN(convertIntegral<int32_t>(legacy.ramp_duration_ms));
501 return aidl;
502 }
503
aidl2legacy_AudioInputFlags_audio_input_flags_t(media::AudioInputFlags aidl)504 ConversionResult<audio_input_flags_t> aidl2legacy_AudioInputFlags_audio_input_flags_t(
505 media::AudioInputFlags aidl) {
506 switch (aidl) {
507 case media::AudioInputFlags::FAST:
508 return AUDIO_INPUT_FLAG_FAST;
509 case media::AudioInputFlags::HW_HOTWORD:
510 return AUDIO_INPUT_FLAG_HW_HOTWORD;
511 case media::AudioInputFlags::RAW:
512 return AUDIO_INPUT_FLAG_RAW;
513 case media::AudioInputFlags::SYNC:
514 return AUDIO_INPUT_FLAG_SYNC;
515 case media::AudioInputFlags::MMAP_NOIRQ:
516 return AUDIO_INPUT_FLAG_MMAP_NOIRQ;
517 case media::AudioInputFlags::VOIP_TX:
518 return AUDIO_INPUT_FLAG_VOIP_TX;
519 case media::AudioInputFlags::HW_AV_SYNC:
520 return AUDIO_INPUT_FLAG_HW_AV_SYNC;
521 case media::AudioInputFlags::DIRECT:
522 return AUDIO_INPUT_FLAG_DIRECT;
523 }
524 return unexpected(BAD_VALUE);
525 }
526
legacy2aidl_audio_input_flags_t_AudioInputFlags(audio_input_flags_t legacy)527 ConversionResult<media::AudioInputFlags> legacy2aidl_audio_input_flags_t_AudioInputFlags(
528 audio_input_flags_t legacy) {
529 switch (legacy) {
530 case AUDIO_INPUT_FLAG_NONE:
531 break; // shouldn't get here. must be listed -Werror,-Wswitch
532 case AUDIO_INPUT_FLAG_FAST:
533 return media::AudioInputFlags::FAST;
534 case AUDIO_INPUT_FLAG_HW_HOTWORD:
535 return media::AudioInputFlags::HW_HOTWORD;
536 case AUDIO_INPUT_FLAG_RAW:
537 return media::AudioInputFlags::RAW;
538 case AUDIO_INPUT_FLAG_SYNC:
539 return media::AudioInputFlags::SYNC;
540 case AUDIO_INPUT_FLAG_MMAP_NOIRQ:
541 return media::AudioInputFlags::MMAP_NOIRQ;
542 case AUDIO_INPUT_FLAG_VOIP_TX:
543 return media::AudioInputFlags::VOIP_TX;
544 case AUDIO_INPUT_FLAG_HW_AV_SYNC:
545 return media::AudioInputFlags::HW_AV_SYNC;
546 case AUDIO_INPUT_FLAG_DIRECT:
547 return media::AudioInputFlags::DIRECT;
548 }
549 return unexpected(BAD_VALUE);
550 }
551
aidl2legacy_AudioOutputFlags_audio_output_flags_t(media::AudioOutputFlags aidl)552 ConversionResult<audio_output_flags_t> aidl2legacy_AudioOutputFlags_audio_output_flags_t(
553 media::AudioOutputFlags aidl) {
554 switch (aidl) {
555 case media::AudioOutputFlags::DIRECT:
556 return AUDIO_OUTPUT_FLAG_DIRECT;
557 case media::AudioOutputFlags::PRIMARY:
558 return AUDIO_OUTPUT_FLAG_PRIMARY;
559 case media::AudioOutputFlags::FAST:
560 return AUDIO_OUTPUT_FLAG_FAST;
561 case media::AudioOutputFlags::DEEP_BUFFER:
562 return AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
563 case media::AudioOutputFlags::COMPRESS_OFFLOAD:
564 return AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD;
565 case media::AudioOutputFlags::NON_BLOCKING:
566 return AUDIO_OUTPUT_FLAG_NON_BLOCKING;
567 case media::AudioOutputFlags::HW_AV_SYNC:
568 return AUDIO_OUTPUT_FLAG_HW_AV_SYNC;
569 case media::AudioOutputFlags::TTS:
570 return AUDIO_OUTPUT_FLAG_TTS;
571 case media::AudioOutputFlags::RAW:
572 return AUDIO_OUTPUT_FLAG_RAW;
573 case media::AudioOutputFlags::SYNC:
574 return AUDIO_OUTPUT_FLAG_SYNC;
575 case media::AudioOutputFlags::IEC958_NONAUDIO:
576 return AUDIO_OUTPUT_FLAG_IEC958_NONAUDIO;
577 case media::AudioOutputFlags::DIRECT_PCM:
578 return AUDIO_OUTPUT_FLAG_DIRECT_PCM;
579 case media::AudioOutputFlags::MMAP_NOIRQ:
580 return AUDIO_OUTPUT_FLAG_MMAP_NOIRQ;
581 case media::AudioOutputFlags::VOIP_RX:
582 return AUDIO_OUTPUT_FLAG_VOIP_RX;
583 case media::AudioOutputFlags::INCALL_MUSIC:
584 return AUDIO_OUTPUT_FLAG_INCALL_MUSIC;
585 case media::AudioOutputFlags::GAPLESS_OFFLOAD:
586 return AUDIO_OUTPUT_FLAG_GAPLESS_OFFLOAD;
587 }
588 return unexpected(BAD_VALUE);
589 }
590
legacy2aidl_audio_output_flags_t_AudioOutputFlags(audio_output_flags_t legacy)591 ConversionResult<media::AudioOutputFlags> legacy2aidl_audio_output_flags_t_AudioOutputFlags(
592 audio_output_flags_t legacy) {
593 switch (legacy) {
594 case AUDIO_OUTPUT_FLAG_NONE:
595 break; // shouldn't get here. must be listed -Werror,-Wswitch
596 case AUDIO_OUTPUT_FLAG_DIRECT:
597 return media::AudioOutputFlags::DIRECT;
598 case AUDIO_OUTPUT_FLAG_PRIMARY:
599 return media::AudioOutputFlags::PRIMARY;
600 case AUDIO_OUTPUT_FLAG_FAST:
601 return media::AudioOutputFlags::FAST;
602 case AUDIO_OUTPUT_FLAG_DEEP_BUFFER:
603 return media::AudioOutputFlags::DEEP_BUFFER;
604 case AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD:
605 return media::AudioOutputFlags::COMPRESS_OFFLOAD;
606 case AUDIO_OUTPUT_FLAG_NON_BLOCKING:
607 return media::AudioOutputFlags::NON_BLOCKING;
608 case AUDIO_OUTPUT_FLAG_HW_AV_SYNC:
609 return media::AudioOutputFlags::HW_AV_SYNC;
610 case AUDIO_OUTPUT_FLAG_TTS:
611 return media::AudioOutputFlags::TTS;
612 case AUDIO_OUTPUT_FLAG_RAW:
613 return media::AudioOutputFlags::RAW;
614 case AUDIO_OUTPUT_FLAG_SYNC:
615 return media::AudioOutputFlags::SYNC;
616 case AUDIO_OUTPUT_FLAG_IEC958_NONAUDIO:
617 return media::AudioOutputFlags::IEC958_NONAUDIO;
618 case AUDIO_OUTPUT_FLAG_DIRECT_PCM:
619 return media::AudioOutputFlags::DIRECT_PCM;
620 case AUDIO_OUTPUT_FLAG_MMAP_NOIRQ:
621 return media::AudioOutputFlags::MMAP_NOIRQ;
622 case AUDIO_OUTPUT_FLAG_VOIP_RX:
623 return media::AudioOutputFlags::VOIP_RX;
624 case AUDIO_OUTPUT_FLAG_INCALL_MUSIC:
625 return media::AudioOutputFlags::INCALL_MUSIC;
626 case AUDIO_OUTPUT_FLAG_GAPLESS_OFFLOAD:
627 return media::AudioOutputFlags::GAPLESS_OFFLOAD;
628 }
629 return unexpected(BAD_VALUE);
630 }
631
aidl2legacy_int32_t_audio_input_flags_t_mask(int32_t aidl)632 ConversionResult<audio_input_flags_t> aidl2legacy_int32_t_audio_input_flags_t_mask(
633 int32_t aidl) {
634 using LegacyMask = std::underlying_type_t<audio_input_flags_t>;
635
636 LegacyMask converted = VALUE_OR_RETURN(
637 (convertBitmask<LegacyMask, int32_t, audio_input_flags_t, media::AudioInputFlags>(
638 aidl, aidl2legacy_AudioInputFlags_audio_input_flags_t,
639 indexToEnum_index<media::AudioInputFlags>,
640 enumToMask_bitmask<LegacyMask, audio_input_flags_t>)));
641 return static_cast<audio_input_flags_t>(converted);
642 }
643
legacy2aidl_audio_input_flags_t_int32_t_mask(audio_input_flags_t legacy)644 ConversionResult<int32_t> legacy2aidl_audio_input_flags_t_int32_t_mask(
645 audio_input_flags_t legacy) {
646 using LegacyMask = std::underlying_type_t<audio_input_flags_t>;
647
648 LegacyMask legacyMask = static_cast<LegacyMask>(legacy);
649 return convertBitmask<int32_t, LegacyMask, media::AudioInputFlags, audio_input_flags_t>(
650 legacyMask, legacy2aidl_audio_input_flags_t_AudioInputFlags,
651 indexToEnum_bitmask<audio_input_flags_t>,
652 enumToMask_index<int32_t, media::AudioInputFlags>);
653 }
654
aidl2legacy_int32_t_audio_output_flags_t_mask(int32_t aidl)655 ConversionResult<audio_output_flags_t> aidl2legacy_int32_t_audio_output_flags_t_mask(
656 int32_t aidl) {
657 return convertBitmask<audio_output_flags_t,
658 int32_t,
659 audio_output_flags_t,
660 media::AudioOutputFlags>(
661 aidl, aidl2legacy_AudioOutputFlags_audio_output_flags_t,
662 indexToEnum_index<media::AudioOutputFlags>,
663 enumToMask_bitmask<audio_output_flags_t, audio_output_flags_t>);
664 }
665
legacy2aidl_audio_output_flags_t_int32_t_mask(audio_output_flags_t legacy)666 ConversionResult<int32_t> legacy2aidl_audio_output_flags_t_int32_t_mask(
667 audio_output_flags_t legacy) {
668 using LegacyMask = std::underlying_type_t<audio_output_flags_t>;
669
670 LegacyMask legacyMask = static_cast<LegacyMask>(legacy);
671 return convertBitmask<int32_t, LegacyMask, media::AudioOutputFlags, audio_output_flags_t>(
672 legacyMask, legacy2aidl_audio_output_flags_t_AudioOutputFlags,
673 indexToEnum_bitmask<audio_output_flags_t>,
674 enumToMask_index<int32_t, media::AudioOutputFlags>);
675 }
676
aidl2legacy_AudioIoFlags_audio_io_flags(const media::AudioIoFlags & aidl,media::AudioPortRole role,media::AudioPortType type)677 ConversionResult<audio_io_flags> aidl2legacy_AudioIoFlags_audio_io_flags(
678 const media::AudioIoFlags& aidl, media::AudioPortRole role, media::AudioPortType type) {
679 audio_io_flags legacy;
680 Direction dir = VALUE_OR_RETURN(direction(role, type));
681 switch (dir) {
682 case Direction::INPUT: {
683 legacy.input = VALUE_OR_RETURN(
684 aidl2legacy_int32_t_audio_input_flags_t_mask(
685 VALUE_OR_RETURN(UNION_GET(aidl, input))));
686 }
687 break;
688
689 case Direction::OUTPUT: {
690 legacy.output = VALUE_OR_RETURN(
691 aidl2legacy_int32_t_audio_output_flags_t_mask(
692 VALUE_OR_RETURN(UNION_GET(aidl, output))));
693 }
694 break;
695 }
696
697 return legacy;
698 }
699
legacy2aidl_audio_io_flags_AudioIoFlags(const audio_io_flags & legacy,audio_port_role_t role,audio_port_type_t type)700 ConversionResult<media::AudioIoFlags> legacy2aidl_audio_io_flags_AudioIoFlags(
701 const audio_io_flags& legacy, audio_port_role_t role, audio_port_type_t type) {
702 media::AudioIoFlags aidl;
703
704 Direction dir = VALUE_OR_RETURN(direction(role, type));
705 switch (dir) {
706 case Direction::INPUT:
707 UNION_SET(aidl, input,
708 VALUE_OR_RETURN(legacy2aidl_audio_input_flags_t_int32_t_mask(
709 legacy.input)));
710 break;
711 case Direction::OUTPUT:
712 UNION_SET(aidl, output,
713 VALUE_OR_RETURN(legacy2aidl_audio_output_flags_t_int32_t_mask(
714 legacy.output)));
715 break;
716 }
717 return aidl;
718 }
719
720 ConversionResult<audio_port_config_device_ext>
aidl2legacy_AudioPortConfigDeviceExt_audio_port_config_device_ext(const media::AudioPortConfigDeviceExt & aidl)721 aidl2legacy_AudioPortConfigDeviceExt_audio_port_config_device_ext(
722 const media::AudioPortConfigDeviceExt& aidl) {
723 audio_port_config_device_ext legacy;
724 legacy.hw_module = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_module_handle_t(aidl.hwModule));
725 legacy.type = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_devices_t(aidl.type));
726 RETURN_IF_ERROR(aidl2legacy_string(aidl.address, legacy.address, AUDIO_DEVICE_MAX_ADDRESS_LEN));
727 return legacy;
728 }
729
730 ConversionResult<media::AudioPortConfigDeviceExt>
legacy2aidl_audio_port_config_device_ext_AudioPortConfigDeviceExt(const audio_port_config_device_ext & legacy)731 legacy2aidl_audio_port_config_device_ext_AudioPortConfigDeviceExt(
732 const audio_port_config_device_ext& legacy) {
733 media::AudioPortConfigDeviceExt aidl;
734 aidl.hwModule = VALUE_OR_RETURN(legacy2aidl_audio_module_handle_t_int32_t(legacy.hw_module));
735 aidl.type = VALUE_OR_RETURN(legacy2aidl_audio_devices_t_int32_t(legacy.type));
736 aidl.address = VALUE_OR_RETURN(
737 legacy2aidl_string(legacy.address, AUDIO_DEVICE_MAX_ADDRESS_LEN));
738 return aidl;
739 }
740
aidl2legacy_AudioStreamType_audio_stream_type_t(media::AudioStreamType aidl)741 ConversionResult<audio_stream_type_t> aidl2legacy_AudioStreamType_audio_stream_type_t(
742 media::AudioStreamType aidl) {
743 switch (aidl) {
744 case media::AudioStreamType::DEFAULT:
745 return AUDIO_STREAM_DEFAULT;
746 case media::AudioStreamType::VOICE_CALL:
747 return AUDIO_STREAM_VOICE_CALL;
748 case media::AudioStreamType::SYSTEM:
749 return AUDIO_STREAM_SYSTEM;
750 case media::AudioStreamType::RING:
751 return AUDIO_STREAM_RING;
752 case media::AudioStreamType::MUSIC:
753 return AUDIO_STREAM_MUSIC;
754 case media::AudioStreamType::ALARM:
755 return AUDIO_STREAM_ALARM;
756 case media::AudioStreamType::NOTIFICATION:
757 return AUDIO_STREAM_NOTIFICATION;
758 case media::AudioStreamType::BLUETOOTH_SCO:
759 return AUDIO_STREAM_BLUETOOTH_SCO;
760 case media::AudioStreamType::ENFORCED_AUDIBLE:
761 return AUDIO_STREAM_ENFORCED_AUDIBLE;
762 case media::AudioStreamType::DTMF:
763 return AUDIO_STREAM_DTMF;
764 case media::AudioStreamType::TTS:
765 return AUDIO_STREAM_TTS;
766 case media::AudioStreamType::ACCESSIBILITY:
767 return AUDIO_STREAM_ACCESSIBILITY;
768 case media::AudioStreamType::ASSISTANT:
769 return AUDIO_STREAM_ASSISTANT;
770 case media::AudioStreamType::REROUTING:
771 return AUDIO_STREAM_REROUTING;
772 case media::AudioStreamType::PATCH:
773 return AUDIO_STREAM_PATCH;
774 case media::AudioStreamType::CALL_ASSISTANT:
775 return AUDIO_STREAM_CALL_ASSISTANT;
776 }
777 return unexpected(BAD_VALUE);
778 }
779
legacy2aidl_audio_stream_type_t_AudioStreamType(audio_stream_type_t legacy)780 ConversionResult<media::AudioStreamType> legacy2aidl_audio_stream_type_t_AudioStreamType(
781 audio_stream_type_t legacy) {
782 switch (legacy) {
783 case AUDIO_STREAM_DEFAULT:
784 return media::AudioStreamType::DEFAULT;
785 case AUDIO_STREAM_VOICE_CALL:
786 return media::AudioStreamType::VOICE_CALL;
787 case AUDIO_STREAM_SYSTEM:
788 return media::AudioStreamType::SYSTEM;
789 case AUDIO_STREAM_RING:
790 return media::AudioStreamType::RING;
791 case AUDIO_STREAM_MUSIC:
792 return media::AudioStreamType::MUSIC;
793 case AUDIO_STREAM_ALARM:
794 return media::AudioStreamType::ALARM;
795 case AUDIO_STREAM_NOTIFICATION:
796 return media::AudioStreamType::NOTIFICATION;
797 case AUDIO_STREAM_BLUETOOTH_SCO:
798 return media::AudioStreamType::BLUETOOTH_SCO;
799 case AUDIO_STREAM_ENFORCED_AUDIBLE:
800 return media::AudioStreamType::ENFORCED_AUDIBLE;
801 case AUDIO_STREAM_DTMF:
802 return media::AudioStreamType::DTMF;
803 case AUDIO_STREAM_TTS:
804 return media::AudioStreamType::TTS;
805 case AUDIO_STREAM_ACCESSIBILITY:
806 return media::AudioStreamType::ACCESSIBILITY;
807 case AUDIO_STREAM_ASSISTANT:
808 return media::AudioStreamType::ASSISTANT;
809 case AUDIO_STREAM_REROUTING:
810 return media::AudioStreamType::REROUTING;
811 case AUDIO_STREAM_PATCH:
812 return media::AudioStreamType::PATCH;
813 case AUDIO_STREAM_CALL_ASSISTANT:
814 return media::AudioStreamType::CALL_ASSISTANT;
815 }
816 return unexpected(BAD_VALUE);
817 }
818
aidl2legacy_AudioSourceType_audio_source_t(media::AudioSourceType aidl)819 ConversionResult<audio_source_t> aidl2legacy_AudioSourceType_audio_source_t(
820 media::AudioSourceType aidl) {
821 switch (aidl) {
822 case media::AudioSourceType::INVALID:
823 // This value does not have an enum
824 return AUDIO_SOURCE_INVALID;
825 case media::AudioSourceType::DEFAULT:
826 return AUDIO_SOURCE_DEFAULT;
827 case media::AudioSourceType::MIC:
828 return AUDIO_SOURCE_MIC;
829 case media::AudioSourceType::VOICE_UPLINK:
830 return AUDIO_SOURCE_VOICE_UPLINK;
831 case media::AudioSourceType::VOICE_DOWNLINK:
832 return AUDIO_SOURCE_VOICE_DOWNLINK;
833 case media::AudioSourceType::VOICE_CALL:
834 return AUDIO_SOURCE_VOICE_CALL;
835 case media::AudioSourceType::CAMCORDER:
836 return AUDIO_SOURCE_CAMCORDER;
837 case media::AudioSourceType::VOICE_RECOGNITION:
838 return AUDIO_SOURCE_VOICE_RECOGNITION;
839 case media::AudioSourceType::VOICE_COMMUNICATION:
840 return AUDIO_SOURCE_VOICE_COMMUNICATION;
841 case media::AudioSourceType::REMOTE_SUBMIX:
842 return AUDIO_SOURCE_REMOTE_SUBMIX;
843 case media::AudioSourceType::UNPROCESSED:
844 return AUDIO_SOURCE_UNPROCESSED;
845 case media::AudioSourceType::VOICE_PERFORMANCE:
846 return AUDIO_SOURCE_VOICE_PERFORMANCE;
847 case media::AudioSourceType::ECHO_REFERENCE:
848 return AUDIO_SOURCE_ECHO_REFERENCE;
849 case media::AudioSourceType::FM_TUNER:
850 return AUDIO_SOURCE_FM_TUNER;
851 case media::AudioSourceType::HOTWORD:
852 return AUDIO_SOURCE_HOTWORD;
853 }
854 return unexpected(BAD_VALUE);
855 }
856
legacy2aidl_audio_source_t_AudioSourceType(audio_source_t legacy)857 ConversionResult<media::AudioSourceType> legacy2aidl_audio_source_t_AudioSourceType(
858 audio_source_t legacy) {
859 switch (legacy) {
860 case AUDIO_SOURCE_INVALID:
861 return media::AudioSourceType::INVALID;
862 case AUDIO_SOURCE_DEFAULT:
863 return media::AudioSourceType::DEFAULT;
864 case AUDIO_SOURCE_MIC:
865 return media::AudioSourceType::MIC;
866 case AUDIO_SOURCE_VOICE_UPLINK:
867 return media::AudioSourceType::VOICE_UPLINK;
868 case AUDIO_SOURCE_VOICE_DOWNLINK:
869 return media::AudioSourceType::VOICE_DOWNLINK;
870 case AUDIO_SOURCE_VOICE_CALL:
871 return media::AudioSourceType::VOICE_CALL;
872 case AUDIO_SOURCE_CAMCORDER:
873 return media::AudioSourceType::CAMCORDER;
874 case AUDIO_SOURCE_VOICE_RECOGNITION:
875 return media::AudioSourceType::VOICE_RECOGNITION;
876 case AUDIO_SOURCE_VOICE_COMMUNICATION:
877 return media::AudioSourceType::VOICE_COMMUNICATION;
878 case AUDIO_SOURCE_REMOTE_SUBMIX:
879 return media::AudioSourceType::REMOTE_SUBMIX;
880 case AUDIO_SOURCE_UNPROCESSED:
881 return media::AudioSourceType::UNPROCESSED;
882 case AUDIO_SOURCE_VOICE_PERFORMANCE:
883 return media::AudioSourceType::VOICE_PERFORMANCE;
884 case AUDIO_SOURCE_ECHO_REFERENCE:
885 return media::AudioSourceType::ECHO_REFERENCE;
886 case AUDIO_SOURCE_FM_TUNER:
887 return media::AudioSourceType::FM_TUNER;
888 case AUDIO_SOURCE_HOTWORD:
889 return media::AudioSourceType::HOTWORD;
890 }
891 return unexpected(BAD_VALUE);
892 }
893
aidl2legacy_int32_t_audio_session_t(int32_t aidl)894 ConversionResult<audio_session_t> aidl2legacy_int32_t_audio_session_t(int32_t aidl) {
895 return convertReinterpret<audio_session_t>(aidl);
896 }
897
legacy2aidl_audio_session_t_int32_t(audio_session_t legacy)898 ConversionResult<int32_t> legacy2aidl_audio_session_t_int32_t(audio_session_t legacy) {
899 return convertReinterpret<int32_t>(legacy);
900 }
901
902 // This type is unnamed in the original definition, thus we name it here.
903 using audio_port_config_mix_ext_usecase = decltype(audio_port_config_mix_ext::usecase);
904
aidl2legacy_AudioPortConfigMixExtUseCase(const media::AudioPortConfigMixExtUseCase & aidl,media::AudioPortRole role)905 ConversionResult<audio_port_config_mix_ext_usecase> aidl2legacy_AudioPortConfigMixExtUseCase(
906 const media::AudioPortConfigMixExtUseCase& aidl, media::AudioPortRole role) {
907 audio_port_config_mix_ext_usecase legacy;
908
909 switch (role) {
910 case media::AudioPortRole::NONE:
911 // Just verify that the union is empty.
912 VALUE_OR_RETURN(UNION_GET(aidl, unspecified));
913 return legacy;
914
915 case media::AudioPortRole::SOURCE:
916 // This is not a bug. A SOURCE role corresponds to the stream field.
917 legacy.stream = VALUE_OR_RETURN(aidl2legacy_AudioStreamType_audio_stream_type_t(
918 VALUE_OR_RETURN(UNION_GET(aidl, stream))));
919 return legacy;
920
921 case media::AudioPortRole::SINK:
922 // This is not a bug. A SINK role corresponds to the source field.
923 legacy.source = VALUE_OR_RETURN(aidl2legacy_AudioSourceType_audio_source_t(
924 VALUE_OR_RETURN(UNION_GET(aidl, source))));
925 return legacy;
926 }
927 LOG_ALWAYS_FATAL("Shouldn't get here"); // with -Werror,-Wswitch may compile-time fail
928 }
929
legacy2aidl_AudioPortConfigMixExtUseCase(const audio_port_config_mix_ext_usecase & legacy,audio_port_role_t role)930 ConversionResult<media::AudioPortConfigMixExtUseCase> legacy2aidl_AudioPortConfigMixExtUseCase(
931 const audio_port_config_mix_ext_usecase& legacy, audio_port_role_t role) {
932 media::AudioPortConfigMixExtUseCase aidl;
933
934 switch (role) {
935 case AUDIO_PORT_ROLE_NONE:
936 UNION_SET(aidl, unspecified, false);
937 return aidl;
938 case AUDIO_PORT_ROLE_SOURCE:
939 // This is not a bug. A SOURCE role corresponds to the stream field.
940 UNION_SET(aidl, stream, VALUE_OR_RETURN(
941 legacy2aidl_audio_stream_type_t_AudioStreamType(legacy.stream)));
942 return aidl;
943 case AUDIO_PORT_ROLE_SINK:
944 // This is not a bug. A SINK role corresponds to the source field.
945 UNION_SET(aidl, source,
946 VALUE_OR_RETURN(legacy2aidl_audio_source_t_AudioSourceType(legacy.source)));
947 return aidl;
948 }
949 LOG_ALWAYS_FATAL("Shouldn't get here"); // with -Werror,-Wswitch may compile-time fail
950 }
951
aidl2legacy_AudioPortConfigMixExt(const media::AudioPortConfigMixExt & aidl,media::AudioPortRole role)952 ConversionResult<audio_port_config_mix_ext> aidl2legacy_AudioPortConfigMixExt(
953 const media::AudioPortConfigMixExt& aidl, media::AudioPortRole role) {
954 audio_port_config_mix_ext legacy;
955 legacy.hw_module = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_module_handle_t(aidl.hwModule));
956 legacy.handle = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_io_handle_t(aidl.handle));
957 legacy.usecase = VALUE_OR_RETURN(aidl2legacy_AudioPortConfigMixExtUseCase(aidl.usecase, role));
958 return legacy;
959 }
960
legacy2aidl_AudioPortConfigMixExt(const audio_port_config_mix_ext & legacy,audio_port_role_t role)961 ConversionResult<media::AudioPortConfigMixExt> legacy2aidl_AudioPortConfigMixExt(
962 const audio_port_config_mix_ext& legacy, audio_port_role_t role) {
963 media::AudioPortConfigMixExt aidl;
964 aidl.hwModule = VALUE_OR_RETURN(legacy2aidl_audio_module_handle_t_int32_t(legacy.hw_module));
965 aidl.handle = VALUE_OR_RETURN(legacy2aidl_audio_io_handle_t_int32_t(legacy.handle));
966 aidl.usecase = VALUE_OR_RETURN(legacy2aidl_AudioPortConfigMixExtUseCase(legacy.usecase, role));
967 return aidl;
968 }
969
970 ConversionResult<audio_port_config_session_ext>
aidl2legacy_AudioPortConfigSessionExt_audio_port_config_session_ext(const media::AudioPortConfigSessionExt & aidl)971 aidl2legacy_AudioPortConfigSessionExt_audio_port_config_session_ext(
972 const media::AudioPortConfigSessionExt& aidl) {
973 audio_port_config_session_ext legacy;
974 legacy.session = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_session_t(aidl.session));
975 return legacy;
976 }
977
978 ConversionResult<media::AudioPortConfigSessionExt>
legacy2aidl_audio_port_config_session_ext_AudioPortConfigSessionExt(const audio_port_config_session_ext & legacy)979 legacy2aidl_audio_port_config_session_ext_AudioPortConfigSessionExt(
980 const audio_port_config_session_ext& legacy) {
981 media::AudioPortConfigSessionExt aidl;
982 aidl.session = VALUE_OR_RETURN(legacy2aidl_audio_session_t_int32_t(legacy.session));
983 return aidl;
984 }
985
986 // This type is unnamed in the original definition, thus we name it here.
987 using audio_port_config_ext = decltype(audio_port_config::ext);
988
aidl2legacy_AudioPortConfigExt(const media::AudioPortConfigExt & aidl,media::AudioPortType type,media::AudioPortRole role)989 ConversionResult<audio_port_config_ext> aidl2legacy_AudioPortConfigExt(
990 const media::AudioPortConfigExt& aidl, media::AudioPortType type,
991 media::AudioPortRole role) {
992 audio_port_config_ext legacy;
993 switch (type) {
994 case media::AudioPortType::NONE:
995 // Just verify that the union is empty.
996 VALUE_OR_RETURN(UNION_GET(aidl, unspecified));
997 return legacy;
998 case media::AudioPortType::DEVICE:
999 legacy.device = VALUE_OR_RETURN(
1000 aidl2legacy_AudioPortConfigDeviceExt_audio_port_config_device_ext(
1001 VALUE_OR_RETURN(UNION_GET(aidl, device))));
1002 return legacy;
1003 case media::AudioPortType::MIX:
1004 legacy.mix = VALUE_OR_RETURN(
1005 aidl2legacy_AudioPortConfigMixExt(VALUE_OR_RETURN(UNION_GET(aidl, mix)), role));
1006 return legacy;
1007 case media::AudioPortType::SESSION:
1008 legacy.session = VALUE_OR_RETURN(
1009 aidl2legacy_AudioPortConfigSessionExt_audio_port_config_session_ext(
1010 VALUE_OR_RETURN(UNION_GET(aidl, session))));
1011 return legacy;
1012
1013 }
1014 LOG_ALWAYS_FATAL("Shouldn't get here"); // with -Werror,-Wswitch may compile-time fail
1015 }
1016
legacy2aidl_AudioPortConfigExt(const audio_port_config_ext & legacy,audio_port_type_t type,audio_port_role_t role)1017 ConversionResult<media::AudioPortConfigExt> legacy2aidl_AudioPortConfigExt(
1018 const audio_port_config_ext& legacy, audio_port_type_t type, audio_port_role_t role) {
1019 media::AudioPortConfigExt aidl;
1020
1021 switch (type) {
1022 case AUDIO_PORT_TYPE_NONE:
1023 UNION_SET(aidl, unspecified, false);
1024 return aidl;
1025 case AUDIO_PORT_TYPE_DEVICE:
1026 UNION_SET(aidl, device,
1027 VALUE_OR_RETURN(
1028 legacy2aidl_audio_port_config_device_ext_AudioPortConfigDeviceExt(
1029 legacy.device)));
1030 return aidl;
1031 case AUDIO_PORT_TYPE_MIX:
1032 UNION_SET(aidl, mix,
1033 VALUE_OR_RETURN(legacy2aidl_AudioPortConfigMixExt(legacy.mix, role)));
1034 return aidl;
1035 case AUDIO_PORT_TYPE_SESSION:
1036 UNION_SET(aidl, session,
1037 VALUE_OR_RETURN(
1038 legacy2aidl_audio_port_config_session_ext_AudioPortConfigSessionExt(
1039 legacy.session)));
1040 return aidl;
1041 }
1042 LOG_ALWAYS_FATAL("Shouldn't get here"); // with -Werror,-Wswitch may compile-time fail
1043 }
1044
aidl2legacy_AudioPortConfig_audio_port_config(const media::AudioPortConfig & aidl)1045 ConversionResult<audio_port_config> aidl2legacy_AudioPortConfig_audio_port_config(
1046 const media::AudioPortConfig& aidl) {
1047 audio_port_config legacy;
1048 legacy.id = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_port_handle_t(aidl.id));
1049 legacy.role = VALUE_OR_RETURN(aidl2legacy_AudioPortRole_audio_port_role_t(aidl.role));
1050 legacy.type = VALUE_OR_RETURN(aidl2legacy_AudioPortType_audio_port_type_t(aidl.type));
1051 legacy.config_mask = VALUE_OR_RETURN(aidl2legacy_int32_t_config_mask(aidl.configMask));
1052 if (bitmaskIsSet(aidl.configMask, media::AudioPortConfigType::SAMPLE_RATE)) {
1053 legacy.sample_rate = VALUE_OR_RETURN(convertIntegral<unsigned int>(aidl.sampleRate));
1054 }
1055 if (bitmaskIsSet(aidl.configMask, media::AudioPortConfigType::CHANNEL_MASK)) {
1056 legacy.channel_mask =
1057 VALUE_OR_RETURN(aidl2legacy_int32_t_audio_channel_mask_t(aidl.channelMask));
1058 }
1059 if (bitmaskIsSet(aidl.configMask, media::AudioPortConfigType::FORMAT)) {
1060 legacy.format = VALUE_OR_RETURN(aidl2legacy_AudioFormat_audio_format_t(aidl.format));
1061 }
1062 if (bitmaskIsSet(aidl.configMask, media::AudioPortConfigType::GAIN)) {
1063 legacy.gain = VALUE_OR_RETURN(
1064 aidl2legacy_AudioGainConfig_audio_gain_config(aidl.gain, aidl.role, aidl.type));
1065 }
1066 if (bitmaskIsSet(aidl.configMask, media::AudioPortConfigType::FLAGS)) {
1067 legacy.flags = VALUE_OR_RETURN(
1068 aidl2legacy_AudioIoFlags_audio_io_flags(aidl.flags, aidl.role, aidl.type));
1069 }
1070 legacy.ext = VALUE_OR_RETURN(aidl2legacy_AudioPortConfigExt(aidl.ext, aidl.type, aidl.role));
1071 return legacy;
1072 }
1073
legacy2aidl_audio_port_config_AudioPortConfig(const audio_port_config & legacy)1074 ConversionResult<media::AudioPortConfig> legacy2aidl_audio_port_config_AudioPortConfig(
1075 const audio_port_config& legacy) {
1076 media::AudioPortConfig aidl;
1077 aidl.id = VALUE_OR_RETURN(legacy2aidl_audio_port_handle_t_int32_t(legacy.id));
1078 aidl.role = VALUE_OR_RETURN(legacy2aidl_audio_port_role_t_AudioPortRole(legacy.role));
1079 aidl.type = VALUE_OR_RETURN(legacy2aidl_audio_port_type_t_AudioPortType(legacy.type));
1080 aidl.configMask = VALUE_OR_RETURN(legacy2aidl_config_mask_int32_t(legacy.config_mask));
1081 if (legacy.config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) {
1082 aidl.sampleRate = VALUE_OR_RETURN(convertIntegral<int32_t>(legacy.sample_rate));
1083 }
1084 if (legacy.config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) {
1085 aidl.channelMask =
1086 VALUE_OR_RETURN(legacy2aidl_audio_channel_mask_t_int32_t(legacy.channel_mask));
1087 }
1088 if (legacy.config_mask & AUDIO_PORT_CONFIG_FORMAT) {
1089 aidl.format = VALUE_OR_RETURN(legacy2aidl_audio_format_t_AudioFormat(legacy.format));
1090 }
1091 if (legacy.config_mask & AUDIO_PORT_CONFIG_GAIN) {
1092 aidl.gain = VALUE_OR_RETURN(legacy2aidl_audio_gain_config_AudioGainConfig(
1093 legacy.gain, legacy.role, legacy.type));
1094 }
1095 if (legacy.config_mask & AUDIO_PORT_CONFIG_FLAGS) {
1096 aidl.flags = VALUE_OR_RETURN(
1097 legacy2aidl_audio_io_flags_AudioIoFlags(legacy.flags, legacy.role, legacy.type));
1098 }
1099 aidl.ext =
1100 VALUE_OR_RETURN(legacy2aidl_AudioPortConfigExt(legacy.ext, legacy.type, legacy.role));
1101 return aidl;
1102 }
1103
aidl2legacy_AudioPatch_audio_patch(const media::AudioPatch & aidl)1104 ConversionResult<struct audio_patch> aidl2legacy_AudioPatch_audio_patch(
1105 const media::AudioPatch& aidl) {
1106 struct audio_patch legacy;
1107 legacy.id = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_patch_handle_t(aidl.id));
1108 legacy.num_sinks = VALUE_OR_RETURN(convertIntegral<unsigned int>(aidl.sinks.size()));
1109 if (legacy.num_sinks > AUDIO_PATCH_PORTS_MAX) {
1110 return unexpected(BAD_VALUE);
1111 }
1112 for (size_t i = 0; i < legacy.num_sinks; ++i) {
1113 legacy.sinks[i] =
1114 VALUE_OR_RETURN(aidl2legacy_AudioPortConfig_audio_port_config(aidl.sinks[i]));
1115 }
1116 legacy.num_sources = VALUE_OR_RETURN(convertIntegral<unsigned int>(aidl.sources.size()));
1117 if (legacy.num_sources > AUDIO_PATCH_PORTS_MAX) {
1118 return unexpected(BAD_VALUE);
1119 }
1120 for (size_t i = 0; i < legacy.num_sources; ++i) {
1121 legacy.sources[i] =
1122 VALUE_OR_RETURN(aidl2legacy_AudioPortConfig_audio_port_config(aidl.sources[i]));
1123 }
1124 return legacy;
1125 }
1126
legacy2aidl_audio_patch_AudioPatch(const struct audio_patch & legacy)1127 ConversionResult<media::AudioPatch> legacy2aidl_audio_patch_AudioPatch(
1128 const struct audio_patch& legacy) {
1129 media::AudioPatch aidl;
1130 aidl.id = VALUE_OR_RETURN(legacy2aidl_audio_patch_handle_t_int32_t(legacy.id));
1131
1132 if (legacy.num_sinks > AUDIO_PATCH_PORTS_MAX) {
1133 return unexpected(BAD_VALUE);
1134 }
1135 for (unsigned int i = 0; i < legacy.num_sinks; ++i) {
1136 aidl.sinks.push_back(
1137 VALUE_OR_RETURN(legacy2aidl_audio_port_config_AudioPortConfig(legacy.sinks[i])));
1138 }
1139 if (legacy.num_sources > AUDIO_PATCH_PORTS_MAX) {
1140 return unexpected(BAD_VALUE);
1141 }
1142 for (unsigned int i = 0; i < legacy.num_sources; ++i) {
1143 aidl.sources.push_back(
1144 VALUE_OR_RETURN(legacy2aidl_audio_port_config_AudioPortConfig(legacy.sources[i])));
1145 }
1146 return aidl;
1147 }
1148
aidl2legacy_AudioIoDescriptor_AudioIoDescriptor(const media::AudioIoDescriptor & aidl)1149 ConversionResult<sp<AudioIoDescriptor>> aidl2legacy_AudioIoDescriptor_AudioIoDescriptor(
1150 const media::AudioIoDescriptor& aidl) {
1151 sp<AudioIoDescriptor> legacy(new AudioIoDescriptor());
1152 legacy->mIoHandle = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_io_handle_t(aidl.ioHandle));
1153 legacy->mPatch = VALUE_OR_RETURN(aidl2legacy_AudioPatch_audio_patch(aidl.patch));
1154 legacy->mSamplingRate = VALUE_OR_RETURN(convertIntegral<uint32_t>(aidl.samplingRate));
1155 legacy->mFormat = VALUE_OR_RETURN(aidl2legacy_AudioFormat_audio_format_t(aidl.format));
1156 legacy->mChannelMask =
1157 VALUE_OR_RETURN(aidl2legacy_int32_t_audio_channel_mask_t(aidl.channelMask));
1158 legacy->mFrameCount = VALUE_OR_RETURN(convertIntegral<size_t>(aidl.frameCount));
1159 legacy->mFrameCountHAL = VALUE_OR_RETURN(convertIntegral<size_t>(aidl.frameCountHAL));
1160 legacy->mLatency = VALUE_OR_RETURN(convertIntegral<uint32_t>(aidl.latency));
1161 legacy->mPortId = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_port_handle_t(aidl.portId));
1162 return legacy;
1163 }
1164
legacy2aidl_AudioIoDescriptor_AudioIoDescriptor(const sp<AudioIoDescriptor> & legacy)1165 ConversionResult<media::AudioIoDescriptor> legacy2aidl_AudioIoDescriptor_AudioIoDescriptor(
1166 const sp<AudioIoDescriptor>& legacy) {
1167 media::AudioIoDescriptor aidl;
1168 aidl.ioHandle = VALUE_OR_RETURN(legacy2aidl_audio_io_handle_t_int32_t(legacy->mIoHandle));
1169 aidl.patch = VALUE_OR_RETURN(legacy2aidl_audio_patch_AudioPatch(legacy->mPatch));
1170 aidl.samplingRate = VALUE_OR_RETURN(convertIntegral<int32_t>(legacy->mSamplingRate));
1171 aidl.format = VALUE_OR_RETURN(legacy2aidl_audio_format_t_AudioFormat(legacy->mFormat));
1172 aidl.channelMask = VALUE_OR_RETURN(
1173 legacy2aidl_audio_channel_mask_t_int32_t(legacy->mChannelMask));
1174 aidl.frameCount = VALUE_OR_RETURN(convertIntegral<int64_t>(legacy->mFrameCount));
1175 aidl.frameCountHAL = VALUE_OR_RETURN(convertIntegral<int64_t>(legacy->mFrameCountHAL));
1176 aidl.latency = VALUE_OR_RETURN(convertIntegral<int32_t>(legacy->mLatency));
1177 aidl.portId = VALUE_OR_RETURN(legacy2aidl_audio_port_handle_t_int32_t(legacy->mPortId));
1178 return aidl;
1179 }
1180
aidl2legacy_AudioClient_AudioClient(const media::AudioClient & aidl)1181 ConversionResult<AudioClient> aidl2legacy_AudioClient_AudioClient(
1182 const media::AudioClient& aidl) {
1183 AudioClient legacy;
1184 legacy.clientTid = VALUE_OR_RETURN(aidl2legacy_int32_t_pid_t(aidl.clientTid));
1185 legacy.attributionSource = aidl.attributionSource;
1186 return legacy;
1187 }
1188
legacy2aidl_AudioClient_AudioClient(const AudioClient & legacy)1189 ConversionResult<media::AudioClient> legacy2aidl_AudioClient_AudioClient(
1190 const AudioClient& legacy) {
1191 media::AudioClient aidl;
1192 aidl.clientTid = VALUE_OR_RETURN(legacy2aidl_pid_t_int32_t(legacy.clientTid));
1193 aidl.attributionSource = legacy.attributionSource;
1194 return aidl;
1195 }
1196
1197 ConversionResult<audio_content_type_t>
aidl2legacy_AudioContentType_audio_content_type_t(media::AudioContentType aidl)1198 aidl2legacy_AudioContentType_audio_content_type_t(media::AudioContentType aidl) {
1199 switch (aidl) {
1200 case media::AudioContentType::UNKNOWN:
1201 return AUDIO_CONTENT_TYPE_UNKNOWN;
1202 case media::AudioContentType::SPEECH:
1203 return AUDIO_CONTENT_TYPE_SPEECH;
1204 case media::AudioContentType::MUSIC:
1205 return AUDIO_CONTENT_TYPE_MUSIC;
1206 case media::AudioContentType::MOVIE:
1207 return AUDIO_CONTENT_TYPE_MOVIE;
1208 case media::AudioContentType::SONIFICATION:
1209 return AUDIO_CONTENT_TYPE_SONIFICATION;
1210 }
1211 return unexpected(BAD_VALUE);
1212 }
1213
1214 ConversionResult<media::AudioContentType>
legacy2aidl_audio_content_type_t_AudioContentType(audio_content_type_t legacy)1215 legacy2aidl_audio_content_type_t_AudioContentType(audio_content_type_t legacy) {
1216 switch (legacy) {
1217 case AUDIO_CONTENT_TYPE_UNKNOWN:
1218 return media::AudioContentType::UNKNOWN;
1219 case AUDIO_CONTENT_TYPE_SPEECH:
1220 return media::AudioContentType::SPEECH;
1221 case AUDIO_CONTENT_TYPE_MUSIC:
1222 return media::AudioContentType::MUSIC;
1223 case AUDIO_CONTENT_TYPE_MOVIE:
1224 return media::AudioContentType::MOVIE;
1225 case AUDIO_CONTENT_TYPE_SONIFICATION:
1226 return media::AudioContentType::SONIFICATION;
1227 }
1228 return unexpected(BAD_VALUE);
1229 }
1230
1231 ConversionResult<audio_usage_t>
aidl2legacy_AudioUsage_audio_usage_t(media::AudioUsage aidl)1232 aidl2legacy_AudioUsage_audio_usage_t(media::AudioUsage aidl) {
1233 switch (aidl) {
1234 case media::AudioUsage::UNKNOWN:
1235 return AUDIO_USAGE_UNKNOWN;
1236 case media::AudioUsage::MEDIA:
1237 return AUDIO_USAGE_MEDIA;
1238 case media::AudioUsage::VOICE_COMMUNICATION:
1239 return AUDIO_USAGE_VOICE_COMMUNICATION;
1240 case media::AudioUsage::VOICE_COMMUNICATION_SIGNALLING:
1241 return AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING;
1242 case media::AudioUsage::ALARM:
1243 return AUDIO_USAGE_ALARM;
1244 case media::AudioUsage::NOTIFICATION:
1245 return AUDIO_USAGE_NOTIFICATION;
1246 case media::AudioUsage::NOTIFICATION_TELEPHONY_RINGTONE:
1247 return AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE;
1248 case media::AudioUsage::NOTIFICATION_COMMUNICATION_REQUEST:
1249 return AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST;
1250 case media::AudioUsage::NOTIFICATION_COMMUNICATION_INSTANT:
1251 return AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT;
1252 case media::AudioUsage::NOTIFICATION_COMMUNICATION_DELAYED:
1253 return AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED;
1254 case media::AudioUsage::NOTIFICATION_EVENT:
1255 return AUDIO_USAGE_NOTIFICATION_EVENT;
1256 case media::AudioUsage::ASSISTANCE_ACCESSIBILITY:
1257 return AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY;
1258 case media::AudioUsage::ASSISTANCE_NAVIGATION_GUIDANCE:
1259 return AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE;
1260 case media::AudioUsage::ASSISTANCE_SONIFICATION:
1261 return AUDIO_USAGE_ASSISTANCE_SONIFICATION;
1262 case media::AudioUsage::GAME:
1263 return AUDIO_USAGE_GAME;
1264 case media::AudioUsage::VIRTUAL_SOURCE:
1265 return AUDIO_USAGE_VIRTUAL_SOURCE;
1266 case media::AudioUsage::ASSISTANT:
1267 return AUDIO_USAGE_ASSISTANT;
1268 case media::AudioUsage::CALL_ASSISTANT:
1269 return AUDIO_USAGE_CALL_ASSISTANT;
1270 case media::AudioUsage::EMERGENCY:
1271 return AUDIO_USAGE_EMERGENCY;
1272 case media::AudioUsage::SAFETY:
1273 return AUDIO_USAGE_SAFETY;
1274 case media::AudioUsage::VEHICLE_STATUS:
1275 return AUDIO_USAGE_VEHICLE_STATUS;
1276 case media::AudioUsage::ANNOUNCEMENT:
1277 return AUDIO_USAGE_ANNOUNCEMENT;
1278 }
1279 return unexpected(BAD_VALUE);
1280 }
1281
1282 ConversionResult<media::AudioUsage>
legacy2aidl_audio_usage_t_AudioUsage(audio_usage_t legacy)1283 legacy2aidl_audio_usage_t_AudioUsage(audio_usage_t legacy) {
1284 switch (legacy) {
1285 case AUDIO_USAGE_UNKNOWN:
1286 return media::AudioUsage::UNKNOWN;
1287 case AUDIO_USAGE_MEDIA:
1288 return media::AudioUsage::MEDIA;
1289 case AUDIO_USAGE_VOICE_COMMUNICATION:
1290 return media::AudioUsage::VOICE_COMMUNICATION;
1291 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
1292 return media::AudioUsage::VOICE_COMMUNICATION_SIGNALLING;
1293 case AUDIO_USAGE_ALARM:
1294 return media::AudioUsage::ALARM;
1295 case AUDIO_USAGE_NOTIFICATION:
1296 return media::AudioUsage::NOTIFICATION;
1297 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
1298 return media::AudioUsage::NOTIFICATION_TELEPHONY_RINGTONE;
1299 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
1300 return media::AudioUsage::NOTIFICATION_COMMUNICATION_REQUEST;
1301 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
1302 return media::AudioUsage::NOTIFICATION_COMMUNICATION_INSTANT;
1303 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
1304 return media::AudioUsage::NOTIFICATION_COMMUNICATION_DELAYED;
1305 case AUDIO_USAGE_NOTIFICATION_EVENT:
1306 return media::AudioUsage::NOTIFICATION_EVENT;
1307 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
1308 return media::AudioUsage::ASSISTANCE_ACCESSIBILITY;
1309 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
1310 return media::AudioUsage::ASSISTANCE_NAVIGATION_GUIDANCE;
1311 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
1312 return media::AudioUsage::ASSISTANCE_SONIFICATION;
1313 case AUDIO_USAGE_GAME:
1314 return media::AudioUsage::GAME;
1315 case AUDIO_USAGE_VIRTUAL_SOURCE:
1316 return media::AudioUsage::VIRTUAL_SOURCE;
1317 case AUDIO_USAGE_ASSISTANT:
1318 return media::AudioUsage::ASSISTANT;
1319 case AUDIO_USAGE_CALL_ASSISTANT:
1320 return media::AudioUsage::CALL_ASSISTANT;
1321 case AUDIO_USAGE_EMERGENCY:
1322 return media::AudioUsage::EMERGENCY;
1323 case AUDIO_USAGE_SAFETY:
1324 return media::AudioUsage::SAFETY;
1325 case AUDIO_USAGE_VEHICLE_STATUS:
1326 return media::AudioUsage::VEHICLE_STATUS;
1327 case AUDIO_USAGE_ANNOUNCEMENT:
1328 return media::AudioUsage::ANNOUNCEMENT;
1329 }
1330 return unexpected(BAD_VALUE);
1331 }
1332
1333 ConversionResult<audio_flags_mask_t>
aidl2legacy_AudioFlag_audio_flags_mask_t(media::AudioFlag aidl)1334 aidl2legacy_AudioFlag_audio_flags_mask_t(media::AudioFlag aidl) {
1335 switch (aidl) {
1336 case media::AudioFlag::AUDIBILITY_ENFORCED:
1337 return AUDIO_FLAG_AUDIBILITY_ENFORCED;
1338 case media::AudioFlag::SECURE:
1339 return AUDIO_FLAG_SECURE;
1340 case media::AudioFlag::SCO:
1341 return AUDIO_FLAG_SCO;
1342 case media::AudioFlag::BEACON:
1343 return AUDIO_FLAG_BEACON;
1344 case media::AudioFlag::HW_AV_SYNC:
1345 return AUDIO_FLAG_HW_AV_SYNC;
1346 case media::AudioFlag::HW_HOTWORD:
1347 return AUDIO_FLAG_HW_HOTWORD;
1348 case media::AudioFlag::BYPASS_INTERRUPTION_POLICY:
1349 return AUDIO_FLAG_BYPASS_INTERRUPTION_POLICY;
1350 case media::AudioFlag::BYPASS_MUTE:
1351 return AUDIO_FLAG_BYPASS_MUTE;
1352 case media::AudioFlag::LOW_LATENCY:
1353 return AUDIO_FLAG_LOW_LATENCY;
1354 case media::AudioFlag::DEEP_BUFFER:
1355 return AUDIO_FLAG_DEEP_BUFFER;
1356 case media::AudioFlag::NO_MEDIA_PROJECTION:
1357 return AUDIO_FLAG_NO_MEDIA_PROJECTION;
1358 case media::AudioFlag::MUTE_HAPTIC:
1359 return AUDIO_FLAG_MUTE_HAPTIC;
1360 case media::AudioFlag::NO_SYSTEM_CAPTURE:
1361 return AUDIO_FLAG_NO_SYSTEM_CAPTURE;
1362 case media::AudioFlag::CAPTURE_PRIVATE:
1363 return AUDIO_FLAG_CAPTURE_PRIVATE;
1364 }
1365 return unexpected(BAD_VALUE);
1366 }
1367
1368 ConversionResult<media::AudioFlag>
legacy2aidl_audio_flags_mask_t_AudioFlag(audio_flags_mask_t legacy)1369 legacy2aidl_audio_flags_mask_t_AudioFlag(audio_flags_mask_t legacy) {
1370 switch (legacy) {
1371 case AUDIO_FLAG_NONE:
1372 return unexpected(BAD_VALUE);
1373 case AUDIO_FLAG_AUDIBILITY_ENFORCED:
1374 return media::AudioFlag::AUDIBILITY_ENFORCED;
1375 case AUDIO_FLAG_SECURE:
1376 return media::AudioFlag::SECURE;
1377 case AUDIO_FLAG_SCO:
1378 return media::AudioFlag::SCO;
1379 case AUDIO_FLAG_BEACON:
1380 return media::AudioFlag::BEACON;
1381 case AUDIO_FLAG_HW_AV_SYNC:
1382 return media::AudioFlag::HW_AV_SYNC;
1383 case AUDIO_FLAG_HW_HOTWORD:
1384 return media::AudioFlag::HW_HOTWORD;
1385 case AUDIO_FLAG_BYPASS_INTERRUPTION_POLICY:
1386 return media::AudioFlag::BYPASS_INTERRUPTION_POLICY;
1387 case AUDIO_FLAG_BYPASS_MUTE:
1388 return media::AudioFlag::BYPASS_MUTE;
1389 case AUDIO_FLAG_LOW_LATENCY:
1390 return media::AudioFlag::LOW_LATENCY;
1391 case AUDIO_FLAG_DEEP_BUFFER:
1392 return media::AudioFlag::DEEP_BUFFER;
1393 case AUDIO_FLAG_NO_MEDIA_PROJECTION:
1394 return media::AudioFlag::NO_MEDIA_PROJECTION;
1395 case AUDIO_FLAG_MUTE_HAPTIC:
1396 return media::AudioFlag::MUTE_HAPTIC;
1397 case AUDIO_FLAG_NO_SYSTEM_CAPTURE:
1398 return media::AudioFlag::NO_SYSTEM_CAPTURE;
1399 case AUDIO_FLAG_CAPTURE_PRIVATE:
1400 return media::AudioFlag::CAPTURE_PRIVATE;
1401 }
1402 return unexpected(BAD_VALUE);
1403 }
1404
1405 ConversionResult<audio_flags_mask_t>
aidl2legacy_int32_t_audio_flags_mask_t_mask(int32_t aidl)1406 aidl2legacy_int32_t_audio_flags_mask_t_mask(int32_t aidl) {
1407 return convertBitmask<audio_flags_mask_t, int32_t, audio_flags_mask_t, media::AudioFlag>(
1408 aidl, aidl2legacy_AudioFlag_audio_flags_mask_t, indexToEnum_index<media::AudioFlag>,
1409 enumToMask_bitmask<audio_flags_mask_t, audio_flags_mask_t>);
1410 }
1411
1412 ConversionResult<int32_t>
legacy2aidl_audio_flags_mask_t_int32_t_mask(audio_flags_mask_t legacy)1413 legacy2aidl_audio_flags_mask_t_int32_t_mask(audio_flags_mask_t legacy) {
1414 return convertBitmask<int32_t, audio_flags_mask_t, media::AudioFlag, audio_flags_mask_t>(
1415 legacy, legacy2aidl_audio_flags_mask_t_AudioFlag,
1416 indexToEnum_bitmask<audio_flags_mask_t>,
1417 enumToMask_index<int32_t, media::AudioFlag>);
1418 }
1419
1420 ConversionResult<audio_attributes_t>
aidl2legacy_AudioAttributesInternal_audio_attributes_t(const media::AudioAttributesInternal & aidl)1421 aidl2legacy_AudioAttributesInternal_audio_attributes_t(const media::AudioAttributesInternal& aidl) {
1422 audio_attributes_t legacy;
1423 legacy.content_type = VALUE_OR_RETURN(
1424 aidl2legacy_AudioContentType_audio_content_type_t(aidl.contentType));
1425 legacy.usage = VALUE_OR_RETURN(aidl2legacy_AudioUsage_audio_usage_t(aidl.usage));
1426 legacy.source = VALUE_OR_RETURN(aidl2legacy_AudioSourceType_audio_source_t(aidl.source));
1427 legacy.flags = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_flags_mask_t_mask(aidl.flags));
1428 RETURN_IF_ERROR(aidl2legacy_string(aidl.tags, legacy.tags, sizeof(legacy.tags)));
1429 return legacy;
1430 }
1431
1432 ConversionResult<media::AudioAttributesInternal>
legacy2aidl_audio_attributes_t_AudioAttributesInternal(const audio_attributes_t & legacy)1433 legacy2aidl_audio_attributes_t_AudioAttributesInternal(const audio_attributes_t& legacy) {
1434 media::AudioAttributesInternal aidl;
1435 aidl.contentType = VALUE_OR_RETURN(
1436 legacy2aidl_audio_content_type_t_AudioContentType(legacy.content_type));
1437 aidl.usage = VALUE_OR_RETURN(legacy2aidl_audio_usage_t_AudioUsage(legacy.usage));
1438 aidl.source = VALUE_OR_RETURN(legacy2aidl_audio_source_t_AudioSourceType(legacy.source));
1439 aidl.flags = VALUE_OR_RETURN(legacy2aidl_audio_flags_mask_t_int32_t_mask(legacy.flags));
1440 aidl.tags = VALUE_OR_RETURN(legacy2aidl_string(legacy.tags, sizeof(legacy.tags)));
1441 return aidl;
1442 }
1443
1444 ConversionResult<audio_encapsulation_mode_t>
aidl2legacy_AudioEncapsulationMode_audio_encapsulation_mode_t(media::AudioEncapsulationMode aidl)1445 aidl2legacy_AudioEncapsulationMode_audio_encapsulation_mode_t(media::AudioEncapsulationMode aidl) {
1446 switch (aidl) {
1447 case media::AudioEncapsulationMode::NONE:
1448 return AUDIO_ENCAPSULATION_MODE_NONE;
1449 case media::AudioEncapsulationMode::ELEMENTARY_STREAM:
1450 return AUDIO_ENCAPSULATION_MODE_ELEMENTARY_STREAM;
1451 case media::AudioEncapsulationMode::HANDLE:
1452 return AUDIO_ENCAPSULATION_MODE_HANDLE;
1453 }
1454 return unexpected(BAD_VALUE);
1455 }
1456
1457 ConversionResult<media::AudioEncapsulationMode>
legacy2aidl_audio_encapsulation_mode_t_AudioEncapsulationMode(audio_encapsulation_mode_t legacy)1458 legacy2aidl_audio_encapsulation_mode_t_AudioEncapsulationMode(audio_encapsulation_mode_t legacy) {
1459 switch (legacy) {
1460 case AUDIO_ENCAPSULATION_MODE_NONE:
1461 return media::AudioEncapsulationMode::NONE;
1462 case AUDIO_ENCAPSULATION_MODE_ELEMENTARY_STREAM:
1463 return media::AudioEncapsulationMode::ELEMENTARY_STREAM;
1464 case AUDIO_ENCAPSULATION_MODE_HANDLE:
1465 return media::AudioEncapsulationMode::HANDLE;
1466 }
1467 return unexpected(BAD_VALUE);
1468 }
1469
1470 ConversionResult<audio_offload_info_t>
aidl2legacy_AudioOffloadInfo_audio_offload_info_t(const media::AudioOffloadInfo & aidl)1471 aidl2legacy_AudioOffloadInfo_audio_offload_info_t(const media::AudioOffloadInfo& aidl) {
1472 audio_offload_info_t legacy;
1473 legacy.version = VALUE_OR_RETURN(convertIntegral<uint16_t>(aidl.version));
1474 legacy.size = sizeof(audio_offload_info_t);
1475 audio_config_base_t config = VALUE_OR_RETURN(
1476 aidl2legacy_AudioConfigBase_audio_config_base_t(aidl.config));
1477 legacy.sample_rate = config.sample_rate;
1478 legacy.channel_mask = config.channel_mask;
1479 legacy.format = config.format;
1480 legacy.stream_type = VALUE_OR_RETURN(
1481 aidl2legacy_AudioStreamType_audio_stream_type_t(aidl.streamType));
1482 legacy.bit_rate = VALUE_OR_RETURN(convertIntegral<uint32_t>(aidl.bitRate));
1483 legacy.duration_us = VALUE_OR_RETURN(convertIntegral<int64_t>(aidl.durationUs));
1484 legacy.has_video = aidl.hasVideo;
1485 legacy.is_streaming = aidl.isStreaming;
1486 legacy.bit_width = VALUE_OR_RETURN(convertIntegral<uint32_t>(aidl.bitWidth));
1487 legacy.offload_buffer_size = VALUE_OR_RETURN(convertIntegral<uint32_t>(aidl.offloadBufferSize));
1488 legacy.usage = VALUE_OR_RETURN(aidl2legacy_AudioUsage_audio_usage_t(aidl.usage));
1489 legacy.encapsulation_mode = VALUE_OR_RETURN(
1490 aidl2legacy_AudioEncapsulationMode_audio_encapsulation_mode_t(aidl.encapsulationMode));
1491 legacy.content_id = VALUE_OR_RETURN(convertReinterpret<int32_t>(aidl.contentId));
1492 legacy.sync_id = VALUE_OR_RETURN(convertReinterpret<int32_t>(aidl.syncId));
1493 return legacy;
1494 }
1495
1496 ConversionResult<media::AudioOffloadInfo>
legacy2aidl_audio_offload_info_t_AudioOffloadInfo(const audio_offload_info_t & legacy)1497 legacy2aidl_audio_offload_info_t_AudioOffloadInfo(const audio_offload_info_t& legacy) {
1498 media::AudioOffloadInfo aidl;
1499 // Version 0.1 fields.
1500 if (legacy.size < offsetof(audio_offload_info_t, usage) + sizeof(audio_offload_info_t::usage)) {
1501 return unexpected(BAD_VALUE);
1502 }
1503 aidl.version = VALUE_OR_RETURN(convertIntegral<int32_t>(legacy.version));
1504 aidl.config.sampleRate = VALUE_OR_RETURN(convertIntegral<int32_t>(legacy.sample_rate));
1505 aidl.config.channelMask = VALUE_OR_RETURN(
1506 legacy2aidl_audio_channel_mask_t_int32_t(legacy.channel_mask));
1507 aidl.config.format = VALUE_OR_RETURN(legacy2aidl_audio_format_t_AudioFormat(legacy.format));
1508 aidl.streamType = VALUE_OR_RETURN(
1509 legacy2aidl_audio_stream_type_t_AudioStreamType(legacy.stream_type));
1510 aidl.bitRate = VALUE_OR_RETURN(convertIntegral<int32_t>(legacy.bit_rate));
1511 aidl.durationUs = VALUE_OR_RETURN(convertIntegral<int64_t>(legacy.duration_us));
1512 aidl.hasVideo = legacy.has_video;
1513 aidl.isStreaming = legacy.is_streaming;
1514 aidl.bitWidth = VALUE_OR_RETURN(convertIntegral<int32_t>(legacy.bit_width));
1515 aidl.offloadBufferSize = VALUE_OR_RETURN(convertIntegral<int32_t>(legacy.offload_buffer_size));
1516 aidl.usage = VALUE_OR_RETURN(legacy2aidl_audio_usage_t_AudioUsage(legacy.usage));
1517
1518 // Version 0.2 fields.
1519 if (legacy.version >= AUDIO_OFFLOAD_INFO_VERSION_0_2) {
1520 if (legacy.size <
1521 offsetof(audio_offload_info_t, sync_id) + sizeof(audio_offload_info_t::sync_id)) {
1522 return unexpected(BAD_VALUE);
1523 }
1524 aidl.encapsulationMode = VALUE_OR_RETURN(
1525 legacy2aidl_audio_encapsulation_mode_t_AudioEncapsulationMode(
1526 legacy.encapsulation_mode));
1527 aidl.contentId = VALUE_OR_RETURN(convertReinterpret<int32_t>(legacy.content_id));
1528 aidl.syncId = VALUE_OR_RETURN(convertReinterpret<int32_t>(legacy.sync_id));
1529 }
1530 return aidl;
1531 }
1532
1533 ConversionResult<audio_config_t>
aidl2legacy_AudioConfig_audio_config_t(const media::AudioConfig & aidl)1534 aidl2legacy_AudioConfig_audio_config_t(const media::AudioConfig& aidl) {
1535 audio_config_t legacy;
1536 legacy.sample_rate = VALUE_OR_RETURN(convertIntegral<uint32_t>(aidl.sampleRate));
1537 legacy.channel_mask = VALUE_OR_RETURN(
1538 aidl2legacy_int32_t_audio_channel_mask_t(aidl.channelMask));
1539 legacy.format = VALUE_OR_RETURN(aidl2legacy_AudioFormat_audio_format_t(aidl.format));
1540 legacy.offload_info = VALUE_OR_RETURN(
1541 aidl2legacy_AudioOffloadInfo_audio_offload_info_t(aidl.offloadInfo));
1542 legacy.frame_count = VALUE_OR_RETURN(convertIntegral<uint32_t>(aidl.frameCount));
1543 return legacy;
1544 }
1545
1546 ConversionResult<media::AudioConfig>
legacy2aidl_audio_config_t_AudioConfig(const audio_config_t & legacy)1547 legacy2aidl_audio_config_t_AudioConfig(const audio_config_t& legacy) {
1548 media::AudioConfig aidl;
1549 aidl.sampleRate = VALUE_OR_RETURN(convertIntegral<int32_t>(legacy.sample_rate));
1550 aidl.channelMask = VALUE_OR_RETURN(
1551 legacy2aidl_audio_channel_mask_t_int32_t(legacy.channel_mask));
1552 aidl.format = VALUE_OR_RETURN(legacy2aidl_audio_format_t_AudioFormat(legacy.format));
1553 aidl.offloadInfo = VALUE_OR_RETURN(
1554 legacy2aidl_audio_offload_info_t_AudioOffloadInfo(legacy.offload_info));
1555 aidl.frameCount = VALUE_OR_RETURN(convertIntegral<int64_t>(legacy.frame_count));
1556 return aidl;
1557 }
1558
1559 ConversionResult<audio_config_base_t>
aidl2legacy_AudioConfigBase_audio_config_base_t(const media::AudioConfigBase & aidl)1560 aidl2legacy_AudioConfigBase_audio_config_base_t(const media::AudioConfigBase& aidl) {
1561 audio_config_base_t legacy;
1562 legacy.sample_rate = VALUE_OR_RETURN(convertIntegral<uint32_t>(aidl.sampleRate));
1563 legacy.channel_mask = VALUE_OR_RETURN(
1564 aidl2legacy_int32_t_audio_channel_mask_t(aidl.channelMask));
1565 legacy.format = VALUE_OR_RETURN(aidl2legacy_AudioFormat_audio_format_t(aidl.format));
1566 return legacy;
1567 }
1568
1569 ConversionResult<media::AudioConfigBase>
legacy2aidl_audio_config_base_t_AudioConfigBase(const audio_config_base_t & legacy)1570 legacy2aidl_audio_config_base_t_AudioConfigBase(const audio_config_base_t& legacy) {
1571 media::AudioConfigBase aidl;
1572 aidl.sampleRate = VALUE_OR_RETURN(convertIntegral<int32_t>(legacy.sample_rate));
1573 aidl.channelMask = VALUE_OR_RETURN(
1574 legacy2aidl_audio_channel_mask_t_int32_t(legacy.channel_mask));
1575 aidl.format = VALUE_OR_RETURN(legacy2aidl_audio_format_t_AudioFormat(legacy.format));
1576 return aidl;
1577 }
1578
1579 ConversionResult<sp<IMemory>>
aidl2legacy_SharedFileRegion_IMemory(const media::SharedFileRegion & aidl)1580 aidl2legacy_SharedFileRegion_IMemory(const media::SharedFileRegion& aidl) {
1581 sp<IMemory> legacy;
1582 if (!convertSharedFileRegionToIMemory(aidl, &legacy)) {
1583 return unexpected(BAD_VALUE);
1584 }
1585 return legacy;
1586 }
1587
1588 ConversionResult<media::SharedFileRegion>
legacy2aidl_IMemory_SharedFileRegion(const sp<IMemory> & legacy)1589 legacy2aidl_IMemory_SharedFileRegion(const sp<IMemory>& legacy) {
1590 media::SharedFileRegion aidl;
1591 if (!convertIMemoryToSharedFileRegion(legacy, &aidl)) {
1592 return unexpected(BAD_VALUE);
1593 }
1594 return aidl;
1595 }
1596
1597 ConversionResult<sp<IMemory>>
aidl2legacy_NullableSharedFileRegion_IMemory(const std::optional<media::SharedFileRegion> & aidl)1598 aidl2legacy_NullableSharedFileRegion_IMemory(const std::optional<media::SharedFileRegion>& aidl) {
1599 sp<IMemory> legacy;
1600 if (!convertNullableSharedFileRegionToIMemory(aidl, &legacy)) {
1601 return unexpected(BAD_VALUE);
1602 }
1603 return legacy;
1604 }
1605
1606 ConversionResult<std::optional<media::SharedFileRegion>>
legacy2aidl_NullableIMemory_SharedFileRegion(const sp<IMemory> & legacy)1607 legacy2aidl_NullableIMemory_SharedFileRegion(const sp<IMemory>& legacy) {
1608 std::optional<media::SharedFileRegion> aidl;
1609 if (!convertNullableIMemoryToSharedFileRegion(legacy, &aidl)) {
1610 return unexpected(BAD_VALUE);
1611 }
1612 return aidl;
1613 }
1614
1615 ConversionResult<AudioTimestamp>
aidl2legacy_AudioTimestampInternal_AudioTimestamp(const media::AudioTimestampInternal & aidl)1616 aidl2legacy_AudioTimestampInternal_AudioTimestamp(const media::AudioTimestampInternal& aidl) {
1617 AudioTimestamp legacy;
1618 legacy.mPosition = VALUE_OR_RETURN(convertIntegral<uint32_t>(aidl.position));
1619 legacy.mTime.tv_sec = VALUE_OR_RETURN(convertIntegral<uint32_t>(aidl.sec));
1620 legacy.mTime.tv_nsec = VALUE_OR_RETURN(convertIntegral<uint32_t>(aidl.nsec));
1621 return legacy;
1622 }
1623
1624 ConversionResult<media::AudioTimestampInternal>
legacy2aidl_AudioTimestamp_AudioTimestampInternal(const AudioTimestamp & legacy)1625 legacy2aidl_AudioTimestamp_AudioTimestampInternal(const AudioTimestamp& legacy) {
1626 media::AudioTimestampInternal aidl;
1627 aidl.position = VALUE_OR_RETURN(convertIntegral<int32_t>(legacy.mPosition));
1628 aidl.sec = VALUE_OR_RETURN(convertIntegral<int64_t>(legacy.mTime.tv_sec));
1629 aidl.nsec = VALUE_OR_RETURN(convertIntegral<int32_t>(legacy.mTime.tv_nsec));
1630 return aidl;
1631 }
1632
1633 ConversionResult<audio_uuid_t>
aidl2legacy_AudioUuid_audio_uuid_t(const media::AudioUuid & aidl)1634 aidl2legacy_AudioUuid_audio_uuid_t(const media::AudioUuid& aidl) {
1635 audio_uuid_t legacy;
1636 legacy.timeLow = VALUE_OR_RETURN(convertReinterpret<uint32_t>(aidl.timeLow));
1637 legacy.timeMid = VALUE_OR_RETURN(convertIntegral<uint16_t>(aidl.timeMid));
1638 legacy.timeHiAndVersion = VALUE_OR_RETURN(convertIntegral<uint16_t>(aidl.timeHiAndVersion));
1639 legacy.clockSeq = VALUE_OR_RETURN(convertIntegral<uint16_t>(aidl.clockSeq));
1640 if (aidl.node.size() != std::size(legacy.node)) {
1641 return unexpected(BAD_VALUE);
1642 }
1643 std::copy(aidl.node.begin(), aidl.node.end(), legacy.node);
1644 return legacy;
1645 }
1646
1647 ConversionResult<media::AudioUuid>
legacy2aidl_audio_uuid_t_AudioUuid(const audio_uuid_t & legacy)1648 legacy2aidl_audio_uuid_t_AudioUuid(const audio_uuid_t& legacy) {
1649 media::AudioUuid aidl;
1650 aidl.timeLow = VALUE_OR_RETURN(convertReinterpret<int32_t>(legacy.timeLow));
1651 aidl.timeMid = VALUE_OR_RETURN(convertIntegral<int32_t>(legacy.timeMid));
1652 aidl.timeHiAndVersion = VALUE_OR_RETURN(convertIntegral<int32_t>(legacy.timeHiAndVersion));
1653 aidl.clockSeq = VALUE_OR_RETURN(convertIntegral<int32_t>(legacy.clockSeq));
1654 std::copy(legacy.node, legacy.node + std::size(legacy.node), std::back_inserter(aidl.node));
1655 return aidl;
1656 }
1657
1658 ConversionResult<effect_descriptor_t>
aidl2legacy_EffectDescriptor_effect_descriptor_t(const media::EffectDescriptor & aidl)1659 aidl2legacy_EffectDescriptor_effect_descriptor_t(const media::EffectDescriptor& aidl) {
1660 effect_descriptor_t legacy;
1661 legacy.type = VALUE_OR_RETURN(aidl2legacy_AudioUuid_audio_uuid_t(aidl.type));
1662 legacy.uuid = VALUE_OR_RETURN(aidl2legacy_AudioUuid_audio_uuid_t(aidl.uuid));
1663 legacy.apiVersion = VALUE_OR_RETURN(convertReinterpret<uint32_t>(aidl.apiVersion));
1664 legacy.flags = VALUE_OR_RETURN(convertReinterpret<uint32_t>(aidl.flags));
1665 legacy.cpuLoad = VALUE_OR_RETURN(convertIntegral<uint16_t>(aidl.cpuLoad));
1666 legacy.memoryUsage = VALUE_OR_RETURN(convertIntegral<uint16_t>(aidl.memoryUsage));
1667 RETURN_IF_ERROR(aidl2legacy_string(aidl.name, legacy.name, sizeof(legacy.name)));
1668 RETURN_IF_ERROR(
1669 aidl2legacy_string(aidl.implementor, legacy.implementor, sizeof(legacy.implementor)));
1670 return legacy;
1671 }
1672
1673 ConversionResult<media::EffectDescriptor>
legacy2aidl_effect_descriptor_t_EffectDescriptor(const effect_descriptor_t & legacy)1674 legacy2aidl_effect_descriptor_t_EffectDescriptor(const effect_descriptor_t& legacy) {
1675 media::EffectDescriptor aidl;
1676 aidl.type = VALUE_OR_RETURN(legacy2aidl_audio_uuid_t_AudioUuid(legacy.type));
1677 aidl.uuid = VALUE_OR_RETURN(legacy2aidl_audio_uuid_t_AudioUuid(legacy.uuid));
1678 aidl.apiVersion = VALUE_OR_RETURN(convertReinterpret<int32_t>(legacy.apiVersion));
1679 aidl.flags = VALUE_OR_RETURN(convertReinterpret<int32_t>(legacy.flags));
1680 aidl.cpuLoad = VALUE_OR_RETURN(convertIntegral<int32_t>(legacy.cpuLoad));
1681 aidl.memoryUsage = VALUE_OR_RETURN(convertIntegral<int32_t>(legacy.memoryUsage));
1682 aidl.name = VALUE_OR_RETURN(legacy2aidl_string(legacy.name, sizeof(legacy.name)));
1683 aidl.implementor = VALUE_OR_RETURN(
1684 legacy2aidl_string(legacy.implementor, sizeof(legacy.implementor)));
1685 return aidl;
1686 }
1687
1688 ConversionResult<audio_encapsulation_metadata_type_t>
aidl2legacy_AudioEncapsulationMetadataType_audio_encapsulation_metadata_type_t(media::AudioEncapsulationMetadataType aidl)1689 aidl2legacy_AudioEncapsulationMetadataType_audio_encapsulation_metadata_type_t(
1690 media::AudioEncapsulationMetadataType aidl) {
1691 switch (aidl) {
1692 case media::AudioEncapsulationMetadataType::NONE:
1693 return AUDIO_ENCAPSULATION_METADATA_TYPE_NONE;
1694 case media::AudioEncapsulationMetadataType::FRAMEWORK_TUNER:
1695 return AUDIO_ENCAPSULATION_METADATA_TYPE_FRAMEWORK_TUNER;
1696 case media::AudioEncapsulationMetadataType::DVB_AD_DESCRIPTOR:
1697 return AUDIO_ENCAPSULATION_METADATA_TYPE_DVB_AD_DESCRIPTOR;
1698 }
1699 return unexpected(BAD_VALUE);
1700 }
1701
1702 ConversionResult<media::AudioEncapsulationMetadataType>
legacy2aidl_audio_encapsulation_metadata_type_t_AudioEncapsulationMetadataType(audio_encapsulation_metadata_type_t legacy)1703 legacy2aidl_audio_encapsulation_metadata_type_t_AudioEncapsulationMetadataType(
1704 audio_encapsulation_metadata_type_t legacy) {
1705 switch (legacy) {
1706 case AUDIO_ENCAPSULATION_METADATA_TYPE_NONE:
1707 return media::AudioEncapsulationMetadataType::NONE;
1708 case AUDIO_ENCAPSULATION_METADATA_TYPE_FRAMEWORK_TUNER:
1709 return media::AudioEncapsulationMetadataType::FRAMEWORK_TUNER;
1710 case AUDIO_ENCAPSULATION_METADATA_TYPE_DVB_AD_DESCRIPTOR:
1711 return media::AudioEncapsulationMetadataType::DVB_AD_DESCRIPTOR;
1712 }
1713 return unexpected(BAD_VALUE);
1714 }
1715
1716 ConversionResult<uint32_t>
aidl2legacy_AudioEncapsulationMode_mask(int32_t aidl)1717 aidl2legacy_AudioEncapsulationMode_mask(int32_t aidl) {
1718 return convertBitmask<uint32_t,
1719 int32_t,
1720 audio_encapsulation_mode_t,
1721 media::AudioEncapsulationMode>(
1722 aidl, aidl2legacy_AudioEncapsulationMode_audio_encapsulation_mode_t,
1723 indexToEnum_index<media::AudioEncapsulationMode>,
1724 enumToMask_index<uint32_t, audio_encapsulation_mode_t>);
1725 }
1726
1727 ConversionResult<int32_t>
legacy2aidl_AudioEncapsulationMode_mask(uint32_t legacy)1728 legacy2aidl_AudioEncapsulationMode_mask(uint32_t legacy) {
1729 return convertBitmask<int32_t,
1730 uint32_t,
1731 media::AudioEncapsulationMode,
1732 audio_encapsulation_mode_t>(
1733 legacy, legacy2aidl_audio_encapsulation_mode_t_AudioEncapsulationMode,
1734 indexToEnum_index<audio_encapsulation_mode_t>,
1735 enumToMask_index<int32_t, media::AudioEncapsulationMode>);
1736 }
1737
1738 ConversionResult<uint32_t>
aidl2legacy_AudioEncapsulationMetadataType_mask(int32_t aidl)1739 aidl2legacy_AudioEncapsulationMetadataType_mask(int32_t aidl) {
1740 return convertBitmask<uint32_t,
1741 int32_t,
1742 audio_encapsulation_metadata_type_t,
1743 media::AudioEncapsulationMetadataType>(
1744 aidl, aidl2legacy_AudioEncapsulationMetadataType_audio_encapsulation_metadata_type_t,
1745 indexToEnum_index<media::AudioEncapsulationMetadataType>,
1746 enumToMask_index<uint32_t, audio_encapsulation_metadata_type_t>);
1747 }
1748
1749 ConversionResult<int32_t>
legacy2aidl_AudioEncapsulationMetadataType_mask(uint32_t legacy)1750 legacy2aidl_AudioEncapsulationMetadataType_mask(uint32_t legacy) {
1751 return convertBitmask<int32_t,
1752 uint32_t,
1753 media::AudioEncapsulationMetadataType,
1754 audio_encapsulation_metadata_type_t>(
1755 legacy, legacy2aidl_audio_encapsulation_metadata_type_t_AudioEncapsulationMetadataType,
1756 indexToEnum_index<audio_encapsulation_metadata_type_t>,
1757 enumToMask_index<int32_t, media::AudioEncapsulationMetadataType>);
1758 }
1759
1760 ConversionResult<audio_mix_latency_class_t>
aidl2legacy_AudioMixLatencyClass_audio_mix_latency_class_t(media::AudioMixLatencyClass aidl)1761 aidl2legacy_AudioMixLatencyClass_audio_mix_latency_class_t(
1762 media::AudioMixLatencyClass aidl) {
1763 switch (aidl) {
1764 case media::AudioMixLatencyClass::LOW:
1765 return AUDIO_LATENCY_LOW;
1766 case media::AudioMixLatencyClass::NORMAL:
1767 return AUDIO_LATENCY_NORMAL;
1768 }
1769 return unexpected(BAD_VALUE);
1770 }
1771
1772 ConversionResult<media::AudioMixLatencyClass>
legacy2aidl_audio_mix_latency_class_t_AudioMixLatencyClass(audio_mix_latency_class_t legacy)1773 legacy2aidl_audio_mix_latency_class_t_AudioMixLatencyClass(
1774 audio_mix_latency_class_t legacy) {
1775 switch (legacy) {
1776 case AUDIO_LATENCY_LOW:
1777 return media::AudioMixLatencyClass::LOW;
1778 case AUDIO_LATENCY_NORMAL:
1779 return media::AudioMixLatencyClass::NORMAL;
1780 }
1781 return unexpected(BAD_VALUE);
1782 }
1783
1784 ConversionResult<audio_port_device_ext>
aidl2legacy_AudioPortDeviceExt_audio_port_device_ext(const media::AudioPortDeviceExt & aidl)1785 aidl2legacy_AudioPortDeviceExt_audio_port_device_ext(const media::AudioPortDeviceExt& aidl) {
1786 audio_port_device_ext legacy;
1787 legacy.hw_module = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_module_handle_t(aidl.hwModule));
1788 legacy.type = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_devices_t(aidl.device.type));
1789 RETURN_IF_ERROR(
1790 aidl2legacy_string(aidl.device.address, legacy.address, sizeof(legacy.address)));
1791 legacy.encapsulation_modes = VALUE_OR_RETURN(
1792 aidl2legacy_AudioEncapsulationMode_mask(aidl.encapsulationModes));
1793 legacy.encapsulation_metadata_types = VALUE_OR_RETURN(
1794 aidl2legacy_AudioEncapsulationMetadataType_mask(aidl.encapsulationMetadataTypes));
1795 return legacy;
1796 }
1797
1798 ConversionResult<media::AudioPortDeviceExt>
legacy2aidl_audio_port_device_ext_AudioPortDeviceExt(const audio_port_device_ext & legacy)1799 legacy2aidl_audio_port_device_ext_AudioPortDeviceExt(const audio_port_device_ext& legacy) {
1800 media::AudioPortDeviceExt aidl;
1801 aidl.hwModule = VALUE_OR_RETURN(legacy2aidl_audio_module_handle_t_int32_t(legacy.hw_module));
1802 aidl.device.type = VALUE_OR_RETURN(legacy2aidl_audio_devices_t_int32_t(legacy.type));
1803 aidl.device.address = VALUE_OR_RETURN(
1804 legacy2aidl_string(legacy.address, sizeof(legacy.address)));
1805 aidl.encapsulationModes = VALUE_OR_RETURN(
1806 legacy2aidl_AudioEncapsulationMode_mask(legacy.encapsulation_modes));
1807 aidl.encapsulationMetadataTypes = VALUE_OR_RETURN(
1808 legacy2aidl_AudioEncapsulationMetadataType_mask(legacy.encapsulation_metadata_types));
1809 return aidl;
1810 }
1811
1812 ConversionResult<audio_port_mix_ext>
aidl2legacy_AudioPortMixExt_audio_port_mix_ext(const media::AudioPortMixExt & aidl)1813 aidl2legacy_AudioPortMixExt_audio_port_mix_ext(const media::AudioPortMixExt& aidl) {
1814 audio_port_mix_ext legacy;
1815 legacy.hw_module = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_module_handle_t(aidl.hwModule));
1816 legacy.handle = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_io_handle_t(aidl.handle));
1817 legacy.latency_class = VALUE_OR_RETURN(
1818 aidl2legacy_AudioMixLatencyClass_audio_mix_latency_class_t(aidl.latencyClass));
1819 return legacy;
1820 }
1821
1822 ConversionResult<media::AudioPortMixExt>
legacy2aidl_audio_port_mix_ext_AudioPortMixExt(const audio_port_mix_ext & legacy)1823 legacy2aidl_audio_port_mix_ext_AudioPortMixExt(const audio_port_mix_ext& legacy) {
1824 media::AudioPortMixExt aidl;
1825 aidl.hwModule = VALUE_OR_RETURN(legacy2aidl_audio_module_handle_t_int32_t(legacy.hw_module));
1826 aidl.handle = VALUE_OR_RETURN(legacy2aidl_audio_io_handle_t_int32_t(legacy.handle));
1827 aidl.latencyClass = VALUE_OR_RETURN(
1828 legacy2aidl_audio_mix_latency_class_t_AudioMixLatencyClass(legacy.latency_class));
1829 return aidl;
1830 }
1831
1832 ConversionResult<audio_port_session_ext>
aidl2legacy_AudioPortSessionExt_audio_port_session_ext(const media::AudioPortSessionExt & aidl)1833 aidl2legacy_AudioPortSessionExt_audio_port_session_ext(const media::AudioPortSessionExt& aidl) {
1834 audio_port_session_ext legacy;
1835 legacy.session = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_session_t(aidl.session));
1836 return legacy;
1837 }
1838
1839 ConversionResult<media::AudioPortSessionExt>
legacy2aidl_audio_port_session_ext_AudioPortSessionExt(const audio_port_session_ext & legacy)1840 legacy2aidl_audio_port_session_ext_AudioPortSessionExt(const audio_port_session_ext& legacy) {
1841 media::AudioPortSessionExt aidl;
1842 aidl.session = VALUE_OR_RETURN(legacy2aidl_audio_session_t_int32_t(legacy.session));
1843 return aidl;
1844 }
1845
1846 // This type is unnamed in the original definition, thus we name it here.
1847 using audio_port_v7_ext = decltype(audio_port_v7::ext);
1848
aidl2legacy_AudioPortExt(const media::AudioPortExt & aidl,media::AudioPortType type)1849 ConversionResult<audio_port_v7_ext> aidl2legacy_AudioPortExt(
1850 const media::AudioPortExt& aidl, media::AudioPortType type) {
1851 audio_port_v7_ext legacy;
1852 switch (type) {
1853 case media::AudioPortType::NONE:
1854 // Just verify that the union is empty.
1855 VALUE_OR_RETURN(UNION_GET(aidl, unspecified));
1856 return legacy;
1857 case media::AudioPortType::DEVICE:
1858 legacy.device = VALUE_OR_RETURN(
1859 aidl2legacy_AudioPortDeviceExt_audio_port_device_ext(
1860 VALUE_OR_RETURN(UNION_GET(aidl, device))));
1861 return legacy;
1862 case media::AudioPortType::MIX:
1863 legacy.mix = VALUE_OR_RETURN(
1864 aidl2legacy_AudioPortMixExt_audio_port_mix_ext(
1865 VALUE_OR_RETURN(UNION_GET(aidl, mix))));
1866 return legacy;
1867 case media::AudioPortType::SESSION:
1868 legacy.session = VALUE_OR_RETURN(aidl2legacy_AudioPortSessionExt_audio_port_session_ext(
1869 VALUE_OR_RETURN(UNION_GET(aidl, session))));
1870 return legacy;
1871
1872 }
1873 LOG_ALWAYS_FATAL("Shouldn't get here"); // with -Werror,-Wswitch may compile-time fail
1874 }
1875
legacy2aidl_AudioPortExt(const audio_port_v7_ext & legacy,audio_port_type_t type)1876 ConversionResult<media::AudioPortExt> legacy2aidl_AudioPortExt(
1877 const audio_port_v7_ext& legacy, audio_port_type_t type) {
1878 media::AudioPortExt aidl;
1879 switch (type) {
1880 case AUDIO_PORT_TYPE_NONE:
1881 UNION_SET(aidl, unspecified, false);
1882 return aidl;
1883 case AUDIO_PORT_TYPE_DEVICE:
1884 UNION_SET(aidl, device,
1885 VALUE_OR_RETURN(
1886 legacy2aidl_audio_port_device_ext_AudioPortDeviceExt(legacy.device)));
1887 return aidl;
1888 case AUDIO_PORT_TYPE_MIX:
1889 UNION_SET(aidl, mix,
1890 VALUE_OR_RETURN(legacy2aidl_audio_port_mix_ext_AudioPortMixExt(legacy.mix)));
1891 return aidl;
1892 case AUDIO_PORT_TYPE_SESSION:
1893 UNION_SET(aidl, session,
1894 VALUE_OR_RETURN(legacy2aidl_audio_port_session_ext_AudioPortSessionExt(
1895 legacy.session)));
1896 return aidl;
1897 }
1898 LOG_ALWAYS_FATAL("Shouldn't get here"); // with -Werror,-Wswitch may compile-time fail
1899 }
1900
1901 ConversionResult<audio_profile>
aidl2legacy_AudioProfile_audio_profile(const media::AudioProfile & aidl)1902 aidl2legacy_AudioProfile_audio_profile(const media::AudioProfile& aidl) {
1903 audio_profile legacy;
1904 legacy.format = VALUE_OR_RETURN(aidl2legacy_AudioFormat_audio_format_t(aidl.format));
1905
1906 if (aidl.samplingRates.size() > std::size(legacy.sample_rates)) {
1907 return unexpected(BAD_VALUE);
1908 }
1909 RETURN_IF_ERROR(
1910 convertRange(aidl.samplingRates.begin(), aidl.samplingRates.end(), legacy.sample_rates,
1911 convertIntegral<int32_t, unsigned int>));
1912 legacy.num_sample_rates = aidl.samplingRates.size();
1913
1914 if (aidl.channelMasks.size() > std::size(legacy.channel_masks)) {
1915 return unexpected(BAD_VALUE);
1916 }
1917 RETURN_IF_ERROR(
1918 convertRange(aidl.channelMasks.begin(), aidl.channelMasks.end(), legacy.channel_masks,
1919 aidl2legacy_int32_t_audio_channel_mask_t));
1920 legacy.num_channel_masks = aidl.channelMasks.size();
1921
1922 legacy.encapsulation_type = VALUE_OR_RETURN(
1923 aidl2legacy_AudioEncapsulationType_audio_encapsulation_type_t(aidl.encapsulationType));
1924 return legacy;
1925 }
1926
1927 ConversionResult<media::AudioProfile>
legacy2aidl_audio_profile_AudioProfile(const audio_profile & legacy)1928 legacy2aidl_audio_profile_AudioProfile(const audio_profile& legacy) {
1929 media::AudioProfile aidl;
1930 aidl.format = VALUE_OR_RETURN(legacy2aidl_audio_format_t_AudioFormat(legacy.format));
1931
1932 if (legacy.num_sample_rates > std::size(legacy.sample_rates)) {
1933 return unexpected(BAD_VALUE);
1934 }
1935 RETURN_IF_ERROR(
1936 convertRange(legacy.sample_rates, legacy.sample_rates + legacy.num_sample_rates,
1937 std::back_inserter(aidl.samplingRates),
1938 convertIntegral<unsigned int, int32_t>));
1939
1940 if (legacy.num_channel_masks > std::size(legacy.channel_masks)) {
1941 return unexpected(BAD_VALUE);
1942 }
1943 RETURN_IF_ERROR(
1944 convertRange(legacy.channel_masks, legacy.channel_masks + legacy.num_channel_masks,
1945 std::back_inserter(aidl.channelMasks),
1946 legacy2aidl_audio_channel_mask_t_int32_t));
1947
1948 aidl.encapsulationType = VALUE_OR_RETURN(
1949 legacy2aidl_audio_encapsulation_type_t_AudioEncapsulationType(
1950 legacy.encapsulation_type));
1951 return aidl;
1952 }
1953
1954 ConversionResult<audio_gain>
aidl2legacy_AudioGain_audio_gain(const media::AudioGain & aidl)1955 aidl2legacy_AudioGain_audio_gain(const media::AudioGain& aidl) {
1956 audio_gain legacy;
1957 legacy.mode = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_gain_mode_t_mask(aidl.mode));
1958 legacy.channel_mask = VALUE_OR_RETURN(
1959 aidl2legacy_int32_t_audio_channel_mask_t(aidl.channelMask));
1960 legacy.min_value = VALUE_OR_RETURN(convertIntegral<int>(aidl.minValue));
1961 legacy.max_value = VALUE_OR_RETURN(convertIntegral<int>(aidl.maxValue));
1962 legacy.default_value = VALUE_OR_RETURN(convertIntegral<int>(aidl.defaultValue));
1963 legacy.step_value = VALUE_OR_RETURN(convertIntegral<unsigned int>(aidl.stepValue));
1964 legacy.min_ramp_ms = VALUE_OR_RETURN(convertIntegral<unsigned int>(aidl.minRampMs));
1965 legacy.max_ramp_ms = VALUE_OR_RETURN(convertIntegral<unsigned int>(aidl.maxRampMs));
1966 return legacy;
1967 }
1968
1969 ConversionResult<media::AudioGain>
legacy2aidl_audio_gain_AudioGain(const audio_gain & legacy)1970 legacy2aidl_audio_gain_AudioGain(const audio_gain& legacy) {
1971 media::AudioGain aidl;
1972 aidl.mode = VALUE_OR_RETURN(legacy2aidl_audio_gain_mode_t_int32_t_mask(legacy.mode));
1973 aidl.channelMask = VALUE_OR_RETURN(
1974 legacy2aidl_audio_channel_mask_t_int32_t(legacy.channel_mask));
1975 aidl.minValue = VALUE_OR_RETURN(convertIntegral<int32_t>(legacy.min_value));
1976 aidl.maxValue = VALUE_OR_RETURN(convertIntegral<int32_t>(legacy.max_value));
1977 aidl.defaultValue = VALUE_OR_RETURN(convertIntegral<int32_t>(legacy.default_value));
1978 aidl.stepValue = VALUE_OR_RETURN(convertIntegral<int32_t>(legacy.step_value));
1979 aidl.minRampMs = VALUE_OR_RETURN(convertIntegral<int32_t>(legacy.min_ramp_ms));
1980 aidl.maxRampMs = VALUE_OR_RETURN(convertIntegral<int32_t>(legacy.max_ramp_ms));
1981 return aidl;
1982 }
1983
1984 ConversionResult<audio_port_v7>
aidl2legacy_AudioPort_audio_port_v7(const media::AudioPort & aidl)1985 aidl2legacy_AudioPort_audio_port_v7(const media::AudioPort& aidl) {
1986 audio_port_v7 legacy;
1987 legacy.id = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_port_handle_t(aidl.id));
1988 legacy.role = VALUE_OR_RETURN(aidl2legacy_AudioPortRole_audio_port_role_t(aidl.role));
1989 legacy.type = VALUE_OR_RETURN(aidl2legacy_AudioPortType_audio_port_type_t(aidl.type));
1990 RETURN_IF_ERROR(aidl2legacy_string(aidl.name, legacy.name, sizeof(legacy.name)));
1991
1992 if (aidl.profiles.size() > std::size(legacy.audio_profiles)) {
1993 return unexpected(BAD_VALUE);
1994 }
1995 RETURN_IF_ERROR(convertRange(aidl.profiles.begin(), aidl.profiles.end(), legacy.audio_profiles,
1996 aidl2legacy_AudioProfile_audio_profile));
1997 legacy.num_audio_profiles = aidl.profiles.size();
1998
1999 if (aidl.extraAudioDescriptors.size() > std::size(legacy.extra_audio_descriptors)) {
2000 return unexpected(BAD_VALUE);
2001 }
2002 RETURN_IF_ERROR(
2003 convertRange(aidl.extraAudioDescriptors.begin(), aidl.extraAudioDescriptors.end(),
2004 legacy.extra_audio_descriptors,
2005 aidl2legacy_ExtraAudioDescriptor_audio_extra_audio_descriptor));
2006 legacy.num_extra_audio_descriptors = aidl.extraAudioDescriptors.size();
2007
2008 if (aidl.gains.size() > std::size(legacy.gains)) {
2009 return unexpected(BAD_VALUE);
2010 }
2011 RETURN_IF_ERROR(convertRange(aidl.gains.begin(), aidl.gains.end(), legacy.gains,
2012 aidl2legacy_AudioGain_audio_gain));
2013 legacy.num_gains = aidl.gains.size();
2014
2015 legacy.active_config = VALUE_OR_RETURN(
2016 aidl2legacy_AudioPortConfig_audio_port_config(aidl.activeConfig));
2017 legacy.ext = VALUE_OR_RETURN(aidl2legacy_AudioPortExt(aidl.ext, aidl.type));
2018 return legacy;
2019 }
2020
2021 ConversionResult<media::AudioPort>
legacy2aidl_audio_port_v7_AudioPort(const audio_port_v7 & legacy)2022 legacy2aidl_audio_port_v7_AudioPort(const audio_port_v7& legacy) {
2023 media::AudioPort aidl;
2024 aidl.id = VALUE_OR_RETURN(legacy2aidl_audio_port_handle_t_int32_t(legacy.id));
2025 aidl.role = VALUE_OR_RETURN(legacy2aidl_audio_port_role_t_AudioPortRole(legacy.role));
2026 aidl.type = VALUE_OR_RETURN(legacy2aidl_audio_port_type_t_AudioPortType(legacy.type));
2027 aidl.name = VALUE_OR_RETURN(legacy2aidl_string(legacy.name, sizeof(legacy.name)));
2028
2029 if (legacy.num_audio_profiles > std::size(legacy.audio_profiles)) {
2030 return unexpected(BAD_VALUE);
2031 }
2032 RETURN_IF_ERROR(
2033 convertRange(legacy.audio_profiles, legacy.audio_profiles + legacy.num_audio_profiles,
2034 std::back_inserter(aidl.profiles),
2035 legacy2aidl_audio_profile_AudioProfile));
2036
2037 if (legacy.num_extra_audio_descriptors > std::size(legacy.extra_audio_descriptors)) {
2038 return unexpected(BAD_VALUE);
2039 }
2040 RETURN_IF_ERROR(
2041 convertRange(legacy.extra_audio_descriptors,
2042 legacy.extra_audio_descriptors + legacy.num_extra_audio_descriptors,
2043 std::back_inserter(aidl.extraAudioDescriptors),
2044 legacy2aidl_audio_extra_audio_descriptor_ExtraAudioDescriptor));
2045
2046 if (legacy.num_gains > std::size(legacy.gains)) {
2047 return unexpected(BAD_VALUE);
2048 }
2049 RETURN_IF_ERROR(
2050 convertRange(legacy.gains, legacy.gains + legacy.num_gains,
2051 std::back_inserter(aidl.gains),
2052 legacy2aidl_audio_gain_AudioGain));
2053
2054 aidl.activeConfig = VALUE_OR_RETURN(
2055 legacy2aidl_audio_port_config_AudioPortConfig(legacy.active_config));
2056 aidl.ext = VALUE_OR_RETURN(legacy2aidl_AudioPortExt(legacy.ext, legacy.type));
2057 return aidl;
2058 }
2059
2060 ConversionResult<audio_mode_t>
aidl2legacy_AudioMode_audio_mode_t(media::AudioMode aidl)2061 aidl2legacy_AudioMode_audio_mode_t(media::AudioMode aidl) {
2062 switch (aidl) {
2063 case media::AudioMode::INVALID:
2064 return AUDIO_MODE_INVALID;
2065 case media::AudioMode::CURRENT:
2066 return AUDIO_MODE_CURRENT;
2067 case media::AudioMode::NORMAL:
2068 return AUDIO_MODE_NORMAL;
2069 case media::AudioMode::RINGTONE:
2070 return AUDIO_MODE_RINGTONE;
2071 case media::AudioMode::IN_CALL:
2072 return AUDIO_MODE_IN_CALL;
2073 case media::AudioMode::IN_COMMUNICATION:
2074 return AUDIO_MODE_IN_COMMUNICATION;
2075 case media::AudioMode::CALL_SCREEN:
2076 return AUDIO_MODE_CALL_SCREEN;
2077 }
2078 return unexpected(BAD_VALUE);
2079 }
2080
2081 ConversionResult<media::AudioMode>
legacy2aidl_audio_mode_t_AudioMode(audio_mode_t legacy)2082 legacy2aidl_audio_mode_t_AudioMode(audio_mode_t legacy) {
2083 switch (legacy) {
2084 case AUDIO_MODE_INVALID:
2085 return media::AudioMode::INVALID;
2086 case AUDIO_MODE_CURRENT:
2087 return media::AudioMode::CURRENT;
2088 case AUDIO_MODE_NORMAL:
2089 return media::AudioMode::NORMAL;
2090 case AUDIO_MODE_RINGTONE:
2091 return media::AudioMode::RINGTONE;
2092 case AUDIO_MODE_IN_CALL:
2093 return media::AudioMode::IN_CALL;
2094 case AUDIO_MODE_IN_COMMUNICATION:
2095 return media::AudioMode::IN_COMMUNICATION;
2096 case AUDIO_MODE_CALL_SCREEN:
2097 return media::AudioMode::CALL_SCREEN;
2098 case AUDIO_MODE_CNT:
2099 break;
2100 }
2101 return unexpected(BAD_VALUE);
2102 }
2103
2104 ConversionResult<audio_unique_id_use_t>
aidl2legacy_AudioUniqueIdUse_audio_unique_id_use_t(media::AudioUniqueIdUse aidl)2105 aidl2legacy_AudioUniqueIdUse_audio_unique_id_use_t(media::AudioUniqueIdUse aidl) {
2106 switch (aidl) {
2107 case media::AudioUniqueIdUse::UNSPECIFIED:
2108 return AUDIO_UNIQUE_ID_USE_UNSPECIFIED;
2109 case media::AudioUniqueIdUse::SESSION:
2110 return AUDIO_UNIQUE_ID_USE_SESSION;
2111 case media::AudioUniqueIdUse::MODULE:
2112 return AUDIO_UNIQUE_ID_USE_MODULE;
2113 case media::AudioUniqueIdUse::EFFECT:
2114 return AUDIO_UNIQUE_ID_USE_EFFECT;
2115 case media::AudioUniqueIdUse::PATCH:
2116 return AUDIO_UNIQUE_ID_USE_PATCH;
2117 case media::AudioUniqueIdUse::OUTPUT:
2118 return AUDIO_UNIQUE_ID_USE_OUTPUT;
2119 case media::AudioUniqueIdUse::INPUT:
2120 return AUDIO_UNIQUE_ID_USE_INPUT;
2121 case media::AudioUniqueIdUse::CLIENT:
2122 return AUDIO_UNIQUE_ID_USE_CLIENT;
2123 }
2124 return unexpected(BAD_VALUE);
2125 }
2126
2127 ConversionResult<media::AudioUniqueIdUse>
legacy2aidl_audio_unique_id_use_t_AudioUniqueIdUse(audio_unique_id_use_t legacy)2128 legacy2aidl_audio_unique_id_use_t_AudioUniqueIdUse(audio_unique_id_use_t legacy) {
2129 switch (legacy) {
2130 case AUDIO_UNIQUE_ID_USE_UNSPECIFIED:
2131 return media::AudioUniqueIdUse::UNSPECIFIED;
2132 case AUDIO_UNIQUE_ID_USE_SESSION:
2133 return media::AudioUniqueIdUse::SESSION;
2134 case AUDIO_UNIQUE_ID_USE_MODULE:
2135 return media::AudioUniqueIdUse::MODULE;
2136 case AUDIO_UNIQUE_ID_USE_EFFECT:
2137 return media::AudioUniqueIdUse::EFFECT;
2138 case AUDIO_UNIQUE_ID_USE_PATCH:
2139 return media::AudioUniqueIdUse::PATCH;
2140 case AUDIO_UNIQUE_ID_USE_OUTPUT:
2141 return media::AudioUniqueIdUse::OUTPUT;
2142 case AUDIO_UNIQUE_ID_USE_INPUT:
2143 return media::AudioUniqueIdUse::INPUT;
2144 case AUDIO_UNIQUE_ID_USE_CLIENT:
2145 return media::AudioUniqueIdUse::CLIENT;
2146 case AUDIO_UNIQUE_ID_USE_MAX:
2147 break;
2148 }
2149 return unexpected(BAD_VALUE);
2150 }
2151
2152 ConversionResult<volume_group_t>
aidl2legacy_int32_t_volume_group_t(int32_t aidl)2153 aidl2legacy_int32_t_volume_group_t(int32_t aidl) {
2154 return convertReinterpret<volume_group_t>(aidl);
2155 }
2156
2157 ConversionResult<int32_t>
legacy2aidl_volume_group_t_int32_t(volume_group_t legacy)2158 legacy2aidl_volume_group_t_int32_t(volume_group_t legacy) {
2159 return convertReinterpret<int32_t>(legacy);
2160 }
2161
2162 ConversionResult<product_strategy_t>
aidl2legacy_int32_t_product_strategy_t(int32_t aidl)2163 aidl2legacy_int32_t_product_strategy_t(int32_t aidl) {
2164 return convertReinterpret<product_strategy_t>(aidl);
2165 }
2166
2167 ConversionResult<int32_t>
legacy2aidl_product_strategy_t_int32_t(product_strategy_t legacy)2168 legacy2aidl_product_strategy_t_int32_t(product_strategy_t legacy) {
2169 return convertReinterpret<int32_t>(legacy);
2170 }
2171
2172 ConversionResult<audio_dual_mono_mode_t>
aidl2legacy_AudioDualMonoMode_audio_dual_mono_mode_t(media::AudioDualMonoMode aidl)2173 aidl2legacy_AudioDualMonoMode_audio_dual_mono_mode_t(media::AudioDualMonoMode aidl) {
2174 switch (aidl) {
2175 case media::AudioDualMonoMode::OFF:
2176 return AUDIO_DUAL_MONO_MODE_OFF;
2177 case media::AudioDualMonoMode::LR:
2178 return AUDIO_DUAL_MONO_MODE_LR;
2179 case media::AudioDualMonoMode::LL:
2180 return AUDIO_DUAL_MONO_MODE_LL;
2181 case media::AudioDualMonoMode::RR:
2182 return AUDIO_DUAL_MONO_MODE_RR;
2183 }
2184 return unexpected(BAD_VALUE);
2185 }
2186
2187 ConversionResult<media::AudioDualMonoMode>
legacy2aidl_audio_dual_mono_mode_t_AudioDualMonoMode(audio_dual_mono_mode_t legacy)2188 legacy2aidl_audio_dual_mono_mode_t_AudioDualMonoMode(audio_dual_mono_mode_t legacy) {
2189 switch (legacy) {
2190 case AUDIO_DUAL_MONO_MODE_OFF:
2191 return media::AudioDualMonoMode::OFF;
2192 case AUDIO_DUAL_MONO_MODE_LR:
2193 return media::AudioDualMonoMode::LR;
2194 case AUDIO_DUAL_MONO_MODE_LL:
2195 return media::AudioDualMonoMode::LL;
2196 case AUDIO_DUAL_MONO_MODE_RR:
2197 return media::AudioDualMonoMode::RR;
2198 }
2199 return unexpected(BAD_VALUE);
2200 }
2201
2202 ConversionResult<audio_timestretch_fallback_mode_t>
aidl2legacy_int32_t_audio_timestretch_fallback_mode_t(int32_t aidl)2203 aidl2legacy_int32_t_audio_timestretch_fallback_mode_t(int32_t aidl) {
2204 return convertReinterpret<audio_timestretch_fallback_mode_t>(aidl);
2205 }
2206
2207 ConversionResult<int32_t>
legacy2aidl_audio_timestretch_fallback_mode_t_int32_t(audio_timestretch_fallback_mode_t legacy)2208 legacy2aidl_audio_timestretch_fallback_mode_t_int32_t(audio_timestretch_fallback_mode_t legacy) {
2209 return convertReinterpret<int32_t>(legacy);
2210 }
2211
2212 ConversionResult<audio_timestretch_stretch_mode_t>
aidl2legacy_int32_t_audio_timestretch_stretch_mode_t(int32_t aidl)2213 aidl2legacy_int32_t_audio_timestretch_stretch_mode_t(int32_t aidl) {
2214 return convertReinterpret<audio_timestretch_stretch_mode_t>(aidl);
2215 }
2216
2217 ConversionResult<int32_t>
legacy2aidl_audio_timestretch_stretch_mode_t_int32_t(audio_timestretch_stretch_mode_t legacy)2218 legacy2aidl_audio_timestretch_stretch_mode_t_int32_t(audio_timestretch_stretch_mode_t legacy) {
2219 return convertReinterpret<int32_t>(legacy);
2220 }
2221
2222 ConversionResult<audio_playback_rate_t>
aidl2legacy_AudioPlaybackRate_audio_playback_rate_t(const media::AudioPlaybackRate & aidl)2223 aidl2legacy_AudioPlaybackRate_audio_playback_rate_t(const media::AudioPlaybackRate& aidl) {
2224 audio_playback_rate_t legacy;
2225 legacy.mSpeed = aidl.speed;
2226 legacy.mPitch = aidl.pitch;
2227 legacy.mFallbackMode = VALUE_OR_RETURN(
2228 aidl2legacy_int32_t_audio_timestretch_fallback_mode_t(aidl.fallbackMode));
2229 legacy.mStretchMode = VALUE_OR_RETURN(
2230 aidl2legacy_int32_t_audio_timestretch_stretch_mode_t(aidl.stretchMode));
2231 return legacy;
2232 }
2233
2234 ConversionResult<media::AudioPlaybackRate>
legacy2aidl_audio_playback_rate_t_AudioPlaybackRate(const audio_playback_rate_t & legacy)2235 legacy2aidl_audio_playback_rate_t_AudioPlaybackRate(const audio_playback_rate_t& legacy) {
2236 media::AudioPlaybackRate aidl;
2237 aidl.speed = legacy.mSpeed;
2238 aidl.pitch = legacy.mPitch;
2239 aidl.fallbackMode = VALUE_OR_RETURN(
2240 legacy2aidl_audio_timestretch_fallback_mode_t_int32_t(legacy.mFallbackMode));
2241 aidl.stretchMode = VALUE_OR_RETURN(
2242 legacy2aidl_audio_timestretch_stretch_mode_t_int32_t(legacy.mStretchMode));
2243 return aidl;
2244 }
2245
2246 ConversionResult<audio_standard_t>
aidl2legacy_AudioStandard_audio_standard_t(media::AudioStandard aidl)2247 aidl2legacy_AudioStandard_audio_standard_t(media::AudioStandard aidl) {
2248 switch (aidl) {
2249 case media::AudioStandard::NONE:
2250 return AUDIO_STANDARD_NONE;
2251 case media::AudioStandard::EDID:
2252 return AUDIO_STANDARD_EDID;
2253 }
2254 return unexpected(BAD_VALUE);
2255 }
2256
2257 ConversionResult<media::AudioStandard>
legacy2aidl_audio_standard_t_AudioStandard(audio_standard_t legacy)2258 legacy2aidl_audio_standard_t_AudioStandard(audio_standard_t legacy) {
2259 switch (legacy) {
2260 case AUDIO_STANDARD_NONE:
2261 return media::AudioStandard::NONE;
2262 case AUDIO_STANDARD_EDID:
2263 return media::AudioStandard::EDID;
2264 }
2265 return unexpected(BAD_VALUE);
2266 }
2267
2268 ConversionResult<audio_extra_audio_descriptor>
aidl2legacy_ExtraAudioDescriptor_audio_extra_audio_descriptor(const media::ExtraAudioDescriptor & aidl)2269 aidl2legacy_ExtraAudioDescriptor_audio_extra_audio_descriptor(
2270 const media::ExtraAudioDescriptor& aidl) {
2271 audio_extra_audio_descriptor legacy;
2272 legacy.standard = VALUE_OR_RETURN(aidl2legacy_AudioStandard_audio_standard_t(aidl.standard));
2273 if (aidl.audioDescriptor.size() > EXTRA_AUDIO_DESCRIPTOR_SIZE) {
2274 return unexpected(BAD_VALUE);
2275 }
2276 legacy.descriptor_length = aidl.audioDescriptor.size();
2277 std::copy(aidl.audioDescriptor.begin(), aidl.audioDescriptor.end(),
2278 std::begin(legacy.descriptor));
2279 legacy.encapsulation_type =
2280 VALUE_OR_RETURN(aidl2legacy_AudioEncapsulationType_audio_encapsulation_type_t(
2281 aidl.encapsulationType));
2282 return legacy;
2283 }
2284
2285 ConversionResult<media::ExtraAudioDescriptor>
legacy2aidl_audio_extra_audio_descriptor_ExtraAudioDescriptor(const audio_extra_audio_descriptor & legacy)2286 legacy2aidl_audio_extra_audio_descriptor_ExtraAudioDescriptor(
2287 const audio_extra_audio_descriptor& legacy) {
2288 media::ExtraAudioDescriptor aidl;
2289 aidl.standard = VALUE_OR_RETURN(legacy2aidl_audio_standard_t_AudioStandard(legacy.standard));
2290 if (legacy.descriptor_length > EXTRA_AUDIO_DESCRIPTOR_SIZE) {
2291 return unexpected(BAD_VALUE);
2292 }
2293 aidl.audioDescriptor.resize(legacy.descriptor_length);
2294 std::copy(legacy.descriptor, legacy.descriptor + legacy.descriptor_length,
2295 aidl.audioDescriptor.begin());
2296 aidl.encapsulationType =
2297 VALUE_OR_RETURN(legacy2aidl_audio_encapsulation_type_t_AudioEncapsulationType(
2298 legacy.encapsulation_type));
2299 return aidl;
2300 }
2301
2302 ConversionResult<audio_encapsulation_type_t>
aidl2legacy_AudioEncapsulationType_audio_encapsulation_type_t(const media::AudioEncapsulationType & aidl)2303 aidl2legacy_AudioEncapsulationType_audio_encapsulation_type_t(
2304 const media::AudioEncapsulationType& aidl) {
2305 switch (aidl) {
2306 case media::AudioEncapsulationType::NONE:
2307 return AUDIO_ENCAPSULATION_TYPE_NONE;
2308 case media::AudioEncapsulationType::IEC61937:
2309 return AUDIO_ENCAPSULATION_TYPE_IEC61937;
2310 }
2311 return unexpected(BAD_VALUE);
2312 }
2313
2314 ConversionResult<media::AudioEncapsulationType>
legacy2aidl_audio_encapsulation_type_t_AudioEncapsulationType(const audio_encapsulation_type_t & legacy)2315 legacy2aidl_audio_encapsulation_type_t_AudioEncapsulationType(
2316 const audio_encapsulation_type_t & legacy) {
2317 switch (legacy) {
2318 case AUDIO_ENCAPSULATION_TYPE_NONE:
2319 return media::AudioEncapsulationType::NONE;
2320 case AUDIO_ENCAPSULATION_TYPE_IEC61937:
2321 return media::AudioEncapsulationType::IEC61937;
2322 }
2323 return unexpected(BAD_VALUE);
2324 }
2325
2326 ConversionResult<TrackSecondaryOutputInfoPair>
aidl2legacy_TrackSecondaryOutputInfo_TrackSecondaryOutputInfoPair(const media::TrackSecondaryOutputInfo & aidl)2327 aidl2legacy_TrackSecondaryOutputInfo_TrackSecondaryOutputInfoPair(
2328 const media::TrackSecondaryOutputInfo& aidl) {
2329 TrackSecondaryOutputInfoPair trackSecondaryOutputInfoPair;
2330 trackSecondaryOutputInfoPair.first =
2331 VALUE_OR_RETURN(aidl2legacy_int32_t_audio_port_handle_t(aidl.portId));
2332 trackSecondaryOutputInfoPair.second =
2333 VALUE_OR_RETURN(convertContainer<std::vector<audio_port_handle_t>>(
2334 aidl.secondaryOutputIds, aidl2legacy_int32_t_audio_io_handle_t));
2335 return trackSecondaryOutputInfoPair;
2336 }
2337
2338 ConversionResult<media::TrackSecondaryOutputInfo>
legacy2aidl_TrackSecondaryOutputInfoPair_TrackSecondaryOutputInfo(const TrackSecondaryOutputInfoPair & legacy)2339 legacy2aidl_TrackSecondaryOutputInfoPair_TrackSecondaryOutputInfo(
2340 const TrackSecondaryOutputInfoPair& legacy) {
2341 media::TrackSecondaryOutputInfo trackSecondaryOutputInfo;
2342 trackSecondaryOutputInfo.portId =
2343 VALUE_OR_RETURN(legacy2aidl_audio_port_handle_t_int32_t(legacy.first));
2344 trackSecondaryOutputInfo.secondaryOutputIds =
2345 VALUE_OR_RETURN(convertContainer<std::vector<int32_t>>(
2346 legacy.second, legacy2aidl_audio_io_handle_t_int32_t));
2347 return trackSecondaryOutputInfo;
2348 }
2349
2350 } // namespace android
2351