xref: /aosp_15_r20/frameworks/av/media/libaudiohal/impl/DeviceHalHidl.cpp (revision ec779b8e0859a360c3d303172224686826e6e0e1)
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <stdio.h>
18 
19 #define LOG_TAG "DeviceHalHidl"
20 // #define LOG_NDEBUG 0
21 
22 #include <cutils/native_handle.h>
23 #include <cutils/properties.h>
24 #include <hwbinder/IPCThreadState.h>
25 #include <media/AudioContainers.h>
26 #include <mediautils/TimeCheck.h>
27 #include <utils/Log.h>
28 
29 #include PATH(android/hardware/audio/FILE_VERSION/IPrimaryDevice.h)
30 #include <HidlUtils.h>
31 #include <common/all-versions/VersionUtils.h>
32 #include <util/CoreUtils.h>
33 
34 #include "DeviceHalHidl.h"
35 #include "EffectHalHidl.h"
36 #include "ParameterUtils.h"
37 #include "StreamHalHidl.h"
38 
39 #if MAJOR_VERSION == 7 && MINOR_VERSION == 1
40 #include <aidl/android/hardware/audio/core/sounddose/BpSoundDose.h>
41 #include <aidl/android/hardware/audio/sounddose/BpSoundDoseFactory.h>
42 #include <android/binder_manager.h>
43 
44 constexpr std::string_view kSoundDoseInterfaceModule = "/default";
45 
46 using aidl::android::hardware::audio::core::sounddose::ISoundDose;
47 using aidl::android::hardware::audio::sounddose::ISoundDoseFactory;
48 #endif
49 
50 using ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION::implementation::HidlUtils;
51 using ::android::hardware::audio::common::utils::EnumBitfield;
52 using ::android::hardware::audio::CORE_TYPES_CPP_VERSION::implementation::CoreUtils;
53 using ::android::hardware::hidl_string;
54 using ::android::hardware::hidl_vec;
55 
56 namespace android {
57 
58 using namespace ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION;
59 using namespace ::android::hardware::audio::CORE_TYPES_CPP_VERSION;
60 
61 class DeviceHalHidl::SoundDoseWrapper {
62 public:
63     SoundDoseWrapper() = default;
64     ~SoundDoseWrapper() = default;
65 
66 #if MAJOR_VERSION == 7 && MINOR_VERSION == 1
67     std::shared_ptr<ISoundDoseFactory> mSoundDoseFactory;
68     std::shared_ptr<ISoundDose> mSoundDose;
69 #endif
70 };
71 
DeviceHalHidl(const sp<::android::hardware::audio::CPP_VERSION::IDevice> & device)72 DeviceHalHidl::DeviceHalHidl(const sp<::android::hardware::audio::CPP_VERSION::IDevice>& device)
73         : CoreConversionHelperHidl("DeviceHalHidl"),
74           mDevice(device),
75           mSoundDoseWrapper(std::make_unique<DeviceHalHidl::SoundDoseWrapper>()) {
76 }
77 
DeviceHalHidl(const sp<::android::hardware::audio::CPP_VERSION::IPrimaryDevice> & device)78 DeviceHalHidl::DeviceHalHidl(
79         const sp<::android::hardware::audio::CPP_VERSION::IPrimaryDevice>& device)
80         : CoreConversionHelperHidl("DeviceHalHidl"),
81 #if MAJOR_VERSION <= 6 || (MAJOR_VERSION == 7 && MINOR_VERSION == 0)
82           mDevice(device),
83 #endif
84           mPrimaryDevice(device),
85           mSoundDoseWrapper(std::make_unique<DeviceHalHidl::SoundDoseWrapper>()) {
86 #if MAJOR_VERSION == 7 && MINOR_VERSION == 1
87     auto getDeviceRet = mPrimaryDevice->getDevice();
88     if (getDeviceRet.isOk()) {
89         mDevice = getDeviceRet;
90     } else {
91         ALOGE("Call to IPrimaryDevice.getDevice has failed: %s",
92                 getDeviceRet.description().c_str());
93     }
94 #endif
95 }
96 
~DeviceHalHidl()97 DeviceHalHidl::~DeviceHalHidl() {
98     if (mDevice != 0) {
99 #if MAJOR_VERSION <= 5
100         mDevice.clear();
101         hardware::IPCThreadState::self()->flushCommands();
102 #elif MAJOR_VERSION >= 6
103         mDevice->close();
104 #endif
105     }
106 }
107 
getAudioPorts(std::vector<media::audio::common::AudioPort> * ports __unused)108 status_t DeviceHalHidl::getAudioPorts(
109         std::vector<media::audio::common::AudioPort> *ports __unused) {
110     return INVALID_OPERATION;
111 }
112 
getAudioRoutes(std::vector<media::AudioRoute> * routes __unused)113 status_t DeviceHalHidl::getAudioRoutes(std::vector<media::AudioRoute> *routes __unused) {
114     return INVALID_OPERATION;
115 }
116 
getSupportedModes(std::vector<media::audio::common::AudioMode> * modes __unused)117 status_t DeviceHalHidl::getSupportedModes(
118         std::vector<media::audio::common::AudioMode> *modes __unused) {
119     return INVALID_OPERATION;
120 }
121 
getSupportedDevices(uint32_t *)122 status_t DeviceHalHidl::getSupportedDevices(uint32_t*) {
123     // Obsolete.
124     return INVALID_OPERATION;
125 }
126 
initCheck()127 status_t DeviceHalHidl::initCheck() {
128     TIME_CHECK();
129     if (mDevice == 0) return NO_INIT;
130     return processReturn("initCheck", mDevice->initCheck());
131 }
132 
setVoiceVolume(float volume)133 status_t DeviceHalHidl::setVoiceVolume(float volume) {
134     TIME_CHECK();
135     if (mDevice == 0) return NO_INIT;
136     if (mPrimaryDevice == 0) return INVALID_OPERATION;
137     return processReturn("setVoiceVolume", mPrimaryDevice->setVoiceVolume(volume));
138 }
139 
setMasterVolume(float volume)140 status_t DeviceHalHidl::setMasterVolume(float volume) {
141     TIME_CHECK();
142     if (mDevice == 0) return NO_INIT;
143     return processReturn("setMasterVolume", mDevice->setMasterVolume(volume));
144 }
145 
getMasterVolume(float * volume)146 status_t DeviceHalHidl::getMasterVolume(float *volume) {
147     TIME_CHECK();
148     if (mDevice == 0) return NO_INIT;
149     Result retval;
150     Return<void> ret = mDevice->getMasterVolume(
151             [&](Result r, float v) {
152                 retval = r;
153                 if (retval == Result::OK) {
154                     *volume = v;
155                 }
156             });
157     return processReturn("getMasterVolume", ret, retval);
158 }
159 
setMode(audio_mode_t mode)160 status_t DeviceHalHidl::setMode(audio_mode_t mode) {
161     TIME_CHECK();
162     if (mDevice == 0) return NO_INIT;
163     if (mPrimaryDevice == 0) return INVALID_OPERATION;
164     return processReturn("setMode", mPrimaryDevice->setMode(AudioMode(mode)));
165 }
166 
setMicMute(bool state)167 status_t DeviceHalHidl::setMicMute(bool state) {
168     TIME_CHECK();
169     if (mDevice == 0) return NO_INIT;
170     return processReturn("setMicMute", mDevice->setMicMute(state));
171 }
172 
getMicMute(bool * state)173 status_t DeviceHalHidl::getMicMute(bool *state) {
174     TIME_CHECK();
175     if (mDevice == 0) return NO_INIT;
176     Result retval;
177     Return<void> ret = mDevice->getMicMute(
178             [&](Result r, bool mute) {
179                 retval = r;
180                 if (retval == Result::OK) {
181                     *state = mute;
182                 }
183             });
184     return processReturn("getMicMute", ret, retval);
185 }
186 
setMasterMute(bool state)187 status_t DeviceHalHidl::setMasterMute(bool state) {
188     TIME_CHECK();
189     if (mDevice == 0) return NO_INIT;
190     return processReturn("setMasterMute", mDevice->setMasterMute(state));
191 }
192 
getMasterMute(bool * state)193 status_t DeviceHalHidl::getMasterMute(bool *state) {
194     TIME_CHECK();
195     if (mDevice == 0) return NO_INIT;
196     Result retval;
197     Return<void> ret = mDevice->getMasterMute(
198             [&](Result r, bool mute) {
199                 retval = r;
200                 if (retval == Result::OK) {
201                     *state = mute;
202                 }
203             });
204     return processReturn("getMasterMute", ret, retval);
205 }
206 
setParameters(const String8 & kvPairs)207 status_t DeviceHalHidl::setParameters(const String8& kvPairs) {
208     TIME_CHECK();
209     if (mDevice == 0) return NO_INIT;
210     hidl_vec<ParameterValue> hidlParams;
211     status_t status = parametersFromHal(kvPairs, &hidlParams);
212     if (status != OK) return status;
213     // TODO: change the API so that context and kvPairs are separated
214     return processReturn("setParameters",
215                          utils::setParameters(mDevice, {} /* context */, hidlParams));
216 }
217 
getParameters(const String8 & keys,String8 * values)218 status_t DeviceHalHidl::getParameters(const String8& keys, String8 *values) {
219     TIME_CHECK();
220     values->clear();
221     if (mDevice == 0) return NO_INIT;
222     hidl_vec<hidl_string> hidlKeys;
223     status_t status = keysFromHal(keys, &hidlKeys);
224     if (status != OK) return status;
225     Result retval;
226     Return<void> ret = utils::getParameters(mDevice,
227             {} /* context */,
228             hidlKeys,
229             [&](Result r, const hidl_vec<ParameterValue>& parameters) {
230                 retval = r;
231                 if (retval == Result::OK) {
232                     parametersToHal(parameters, values);
233                 }
234             });
235     return processReturn("getParameters", ret, retval);
236 }
237 
getInputBufferSize(struct audio_config * config,size_t * size)238 status_t DeviceHalHidl::getInputBufferSize(
239         struct audio_config *config, size_t *size) {
240     TIME_CHECK();
241     if (mDevice == 0) return NO_INIT;
242     AudioConfig hidlConfig;
243     HidlUtils::audioConfigFromHal(*config, true /*isInput*/, &hidlConfig);
244     Result retval;
245     Return<void> ret = mDevice->getInputBufferSize(
246             hidlConfig,
247             [&](Result r, uint64_t bufferSize) {
248                 retval = r;
249                 if (retval == Result::OK) {
250                     *size = static_cast<size_t>(bufferSize);
251                 }
252             });
253     return processReturn("getInputBufferSize", ret, retval);
254 }
255 
openOutputStream(audio_io_handle_t handle,audio_devices_t deviceType,audio_output_flags_t flags,struct audio_config * config,const char * address,sp<StreamOutHalInterface> * outStream,const std::vector<playback_track_metadata_v7_t> & sourceMetadata)256 status_t DeviceHalHidl::openOutputStream(
257         audio_io_handle_t handle,
258         audio_devices_t deviceType,
259         audio_output_flags_t flags,
260         struct audio_config *config,
261         const char *address,
262         sp<StreamOutHalInterface> *outStream,
263         const std::vector<playback_track_metadata_v7_t>& sourceMetadata) {
264     TIME_CHECK();
265     if (mDevice == 0) return NO_INIT;
266     DeviceAddress hidlDevice;
267     if (status_t status = CoreUtils::deviceAddressFromHal(deviceType, address, &hidlDevice);
268             status != OK) {
269         return status;
270     }
271     AudioConfig hidlConfig;
272     if (status_t status = HidlUtils::audioConfigFromHal(*config, false /*isInput*/, &hidlConfig);
273             status != OK) {
274         return status;
275     }
276 
277 #if MAJOR_VERSION == 4
278     ::android::hardware::audio::CORE_TYPES_CPP_VERSION::SourceMetadata hidlMetadata;
279 #else
280     ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION::SourceMetadata hidlMetadata;
281 #endif
282 
283     RETURN_STATUS_IF_ERROR(CoreUtils::sourceMetadataFromHalV7(
284             sourceMetadata, true /*ignoreNonVendorTags*/, &hidlMetadata
285             ));
286 
287 #if !(MAJOR_VERSION == 7 && MINOR_VERSION == 1)
288     //TODO: b/193496180 use spatializer flag at audio HAL when available
289     if ((flags & AUDIO_OUTPUT_FLAG_SPATIALIZER) != 0) {
290         flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_SPATIALIZER);
291         flags = (audio_output_flags_t)
292                 (flags | AUDIO_OUTPUT_FLAG_FAST | AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
293     }
294 #endif
295 
296     CoreUtils::AudioOutputFlags hidlFlags;
297     if (status_t status = CoreUtils::audioOutputFlagsFromHal(flags, &hidlFlags); status != OK) {
298         return status;
299     }
300     Result retval = Result::NOT_INITIALIZED;
301 #if MAJOR_VERSION == 7 && MINOR_VERSION == 1
302     Return<void> ret = mDevice->openOutputStream_7_1(
303 #else
304     Return<void> ret = mDevice->openOutputStream(
305 #endif
306             handle, hidlDevice, hidlConfig, hidlFlags,
307 #if MAJOR_VERSION >= 4
308             hidlMetadata /* metadata */,
309 #endif
310             [&](Result r, const sp<::android::hardware::audio::CPP_VERSION::IStreamOut>& result,
311                     const AudioConfig& suggestedConfig) {
312                 retval = r;
313                 if (retval == Result::OK) {
314                     *outStream = new StreamOutHalHidl(result);
315                 }
316                 HidlUtils::audioConfigToHal(suggestedConfig, config);
317             });
318     const status_t status = processReturn("openOutputStream", ret, retval);
319     cleanupStreams();
320     if (status == NO_ERROR) {
321         mStreams.insert({handle, *outStream});
322     }
323     return status;
324 }
325 
openInputStream(audio_io_handle_t handle,audio_devices_t devices,struct audio_config * config,audio_input_flags_t flags,const char * address,audio_source_t source,audio_devices_t outputDevice,const char * outputDeviceAddress,sp<StreamInHalInterface> * inStream)326 status_t DeviceHalHidl::openInputStream(
327         audio_io_handle_t handle,
328         audio_devices_t devices,
329         struct audio_config *config,
330         audio_input_flags_t flags,
331         const char *address,
332         audio_source_t source,
333         audio_devices_t outputDevice,
334         const char *outputDeviceAddress,
335         sp<StreamInHalInterface> *inStream) {
336     TIME_CHECK();
337     if (mDevice == 0) return NO_INIT;
338     DeviceAddress hidlDevice;
339     if (status_t status = CoreUtils::deviceAddressFromHal(devices, address, &hidlDevice);
340             status != OK) {
341         return status;
342     }
343     AudioConfig hidlConfig;
344     if (status_t status = HidlUtils::audioConfigFromHal(*config, true /*isInput*/, &hidlConfig);
345             status != OK) {
346         return status;
347     }
348     CoreUtils::AudioInputFlags hidlFlags;
349 #if MAJOR_VERSION <= 5
350     // Some flags were specific to framework and must not leak to the HAL.
351     flags = static_cast<audio_input_flags_t>(flags & ~AUDIO_INPUT_FLAG_DIRECT);
352 #endif
353     if (status_t status = CoreUtils::audioInputFlagsFromHal(flags, &hidlFlags); status != OK) {
354         return status;
355     }
356     Result retval = Result::NOT_INITIALIZED;
357 #if MAJOR_VERSION == 2
358     auto sinkMetadata = AudioSource(source);
359 #elif MAJOR_VERSION >= 4
360     // TODO: correctly propagate the tracks sources and volume
361     //       for now, only send the main source at 1dbfs
362     AudioSource hidlSource;
363     if (status_t status = HidlUtils::audioSourceFromHal(source, &hidlSource); status != OK) {
364         return status;
365     }
366     SinkMetadata sinkMetadata = {{{ .source = std::move(hidlSource), .gain = 1 }}};
367 #endif
368 #if MAJOR_VERSION < 5
369     (void)outputDevice;
370     (void)outputDeviceAddress;
371 #else
372 #if MAJOR_VERSION >= 7
373     (void)HidlUtils::audioChannelMaskFromHal(
374             AUDIO_CHANNEL_NONE, true /*isInput*/, &sinkMetadata.tracks[0].channelMask);
375 #endif
376     if (outputDevice != AUDIO_DEVICE_NONE) {
377         DeviceAddress hidlOutputDevice;
378         if (status_t status = CoreUtils::deviceAddressFromHal(
379                         outputDevice, outputDeviceAddress, &hidlOutputDevice); status != OK) {
380             return status;
381         }
382         sinkMetadata.tracks[0].destination.device(std::move(hidlOutputDevice));
383     }
384 #endif
385     Return<void> ret = mDevice->openInputStream(
386             handle, hidlDevice, hidlConfig, hidlFlags, sinkMetadata,
387             [&](Result r,
388                 const sp<::android::hardware::audio::CORE_TYPES_CPP_VERSION::IStreamIn>& result,
389                     const AudioConfig& suggestedConfig) {
390                 retval = r;
391                 if (retval == Result::OK) {
392                     *inStream = new StreamInHalHidl(result);
393                 }
394                 HidlUtils::audioConfigToHal(suggestedConfig, config);
395             });
396     const status_t status = processReturn("openInputStream", ret, retval);
397     cleanupStreams();
398     if (status == NO_ERROR) {
399         mStreams.insert({handle, *inStream});
400     }
401     return status;
402 }
403 
supportsAudioPatches(bool * supportsPatches)404 status_t DeviceHalHidl::supportsAudioPatches(bool *supportsPatches) {
405     TIME_CHECK();
406     if (mDevice == 0) return NO_INIT;
407     return processReturn("supportsAudioPatches", mDevice->supportsAudioPatches(), supportsPatches);
408 }
409 
createAudioPatch(unsigned int num_sources,const struct audio_port_config * sources,unsigned int num_sinks,const struct audio_port_config * sinks,audio_patch_handle_t * patch)410 status_t DeviceHalHidl::createAudioPatch(
411         unsigned int num_sources,
412         const struct audio_port_config *sources,
413         unsigned int num_sinks,
414         const struct audio_port_config *sinks,
415         audio_patch_handle_t *patch) {
416     TIME_CHECK();
417     if (mDevice == 0) return NO_INIT;
418     if (patch == nullptr) return BAD_VALUE;
419 
420 #if MAJOR_VERSION < 6
421     if (*patch != AUDIO_PATCH_HANDLE_NONE) {
422         status_t status = releaseAudioPatch(*patch);
423         ALOGW_IF(status != NO_ERROR, "%s error %d releasing patch handle %d",
424             __func__, status, *patch);
425         *patch = AUDIO_PATCH_HANDLE_NONE;
426     }
427 #endif
428 
429     hidl_vec<AudioPortConfig> hidlSources, hidlSinks;
430     HidlUtils::audioPortConfigsFromHal(num_sources, sources, &hidlSources);
431     HidlUtils::audioPortConfigsFromHal(num_sinks, sinks, &hidlSinks);
432     Result retval = Result::OK;
433     Return<void> ret;
434     std::string methodName = "createAudioPatch";
435     if (*patch == AUDIO_PATCH_HANDLE_NONE) {  // always true for MAJOR_VERSION < 6
436         ret = mDevice->createAudioPatch(
437                 hidlSources, hidlSinks,
438                 [&](Result r, AudioPatchHandle hidlPatch) {
439                     retval = r;
440                     if (retval == Result::OK) {
441                         *patch = static_cast<audio_patch_handle_t>(hidlPatch);
442                     }
443                 });
444     } else {
445 #if MAJOR_VERSION >= 6
446         ret = mDevice->updateAudioPatch(
447                 *patch,
448                 hidlSources, hidlSinks,
449                 [&](Result r, AudioPatchHandle hidlPatch) {
450                     retval = r;
451                     if (retval == Result::OK) {
452                         *patch = static_cast<audio_patch_handle_t>(hidlPatch);
453                     }
454                 });
455         methodName = "updateAudioPatch";
456 #endif
457     }
458     return processReturn(methodName.c_str(), ret, retval);
459 }
460 
releaseAudioPatch(audio_patch_handle_t patch)461 status_t DeviceHalHidl::releaseAudioPatch(audio_patch_handle_t patch) {
462     TIME_CHECK();
463     if (mDevice == 0) return NO_INIT;
464     return processReturn("releaseAudioPatch", mDevice->releaseAudioPatch(patch));
465 }
466 
467 template <typename HalPort>
getAudioPortImpl(HalPort * port)468 status_t DeviceHalHidl::getAudioPortImpl(HalPort *port) {
469     using ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION::AudioPort;
470     if (mDevice == 0) return NO_INIT;
471     AudioPort hidlPort;
472     HidlUtils::audioPortFromHal(*port, &hidlPort);
473     Result retval;
474     Return<void> ret = mDevice->getAudioPort(
475             hidlPort,
476             [&](Result r, const AudioPort& p) {
477                 retval = r;
478                 if (retval == Result::OK) {
479                     HidlUtils::audioPortToHal(p, port);
480                 }
481             });
482     return processReturn("getAudioPort", ret, retval);
483 }
484 
getAudioPort(struct audio_port * port)485 status_t DeviceHalHidl::getAudioPort(struct audio_port *port) {
486     TIME_CHECK();
487     return getAudioPortImpl(port);
488 }
489 
getAudioPort(struct audio_port_v7 * port)490 status_t DeviceHalHidl::getAudioPort(struct audio_port_v7 *port) {
491     TIME_CHECK();
492 #if MAJOR_VERSION >= 7
493     return getAudioPortImpl(port);
494 #else
495     struct audio_port audioPort = {};
496     status_t result = NO_ERROR;
497     if (!audio_populate_audio_port(port, &audioPort)) {
498         ALOGE("Failed to populate legacy audio port from audio_port_v7");
499         result = BAD_VALUE;
500     }
501     status_t status = getAudioPort(&audioPort);
502     if (status == NO_ERROR) {
503         audio_populate_audio_port_v7(&audioPort, port);
504     } else {
505         result = status;
506     }
507     return result;
508 #endif
509 }
510 
setAudioPortConfig(const struct audio_port_config * config)511 status_t DeviceHalHidl::setAudioPortConfig(const struct audio_port_config *config) {
512     using ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION::AudioPortConfig;
513     TIME_CHECK();
514     if (mDevice == 0) return NO_INIT;
515     AudioPortConfig hidlConfig;
516     HidlUtils::audioPortConfigFromHal(*config, &hidlConfig);
517     return processReturn("setAudioPortConfig", mDevice->setAudioPortConfig(hidlConfig));
518 }
519 
520 #if MAJOR_VERSION == 2
getMicrophones(std::vector<audio_microphone_characteristic_t> * microphonesInfo __unused)521 status_t DeviceHalHidl::getMicrophones(
522         std::vector<audio_microphone_characteristic_t> *microphonesInfo __unused) {
523     if (mDevice == 0) return NO_INIT;
524     return INVALID_OPERATION;
525 }
526 #elif MAJOR_VERSION >= 4
getMicrophones(std::vector<audio_microphone_characteristic_t> * microphonesInfo)527 status_t DeviceHalHidl::getMicrophones(
528         std::vector<audio_microphone_characteristic_t> *microphonesInfo) {
529     TIME_CHECK();
530     if (mDevice == 0) return NO_INIT;
531     Result retval;
532     Return<void> ret = mDevice->getMicrophones(
533             [&](Result r, hidl_vec<MicrophoneInfo> micArrayHal) {
534         retval = r;
535         for (size_t k = 0; k < micArrayHal.size(); k++) {
536             audio_microphone_characteristic_t dst;
537             //convert
538             (void)CoreUtils::microphoneInfoToHal(micArrayHal[k], &dst);
539             microphonesInfo->push_back(dst);
540         }
541     });
542     return processReturn("getMicrophones", ret, retval);
543 }
544 #endif
545 
546 #if MAJOR_VERSION >= 6
addDeviceEffect(const struct audio_port_config * device,sp<EffectHalInterface> effect)547 status_t DeviceHalHidl::addDeviceEffect(
548         const struct audio_port_config *device, sp<EffectHalInterface> effect) {
549     TIME_CHECK();
550     if (mDevice == 0) return NO_INIT;
551     auto hidlEffect = sp<effect::EffectHalHidl>::cast(effect);
552     return processReturn("addDeviceEffect", mDevice->addDeviceEffect(
553             static_cast<AudioPortHandle>(device->id), hidlEffect->effectId()));
554 }
555 #else
addDeviceEffect(const struct audio_port_config * device __unused,sp<EffectHalInterface> effect __unused)556 status_t DeviceHalHidl::addDeviceEffect(
557         const struct audio_port_config *device __unused, sp<EffectHalInterface> effect __unused) {
558     return INVALID_OPERATION;
559 }
560 #endif
561 
562 #if MAJOR_VERSION >= 6
removeDeviceEffect(const struct audio_port_config * device,sp<EffectHalInterface> effect)563 status_t DeviceHalHidl::removeDeviceEffect(
564         const struct audio_port_config *device, sp<EffectHalInterface> effect) {
565     TIME_CHECK();
566     if (mDevice == 0) return NO_INIT;
567     auto hidlEffect = sp<effect::EffectHalHidl>::cast(effect);
568     return processReturn("removeDeviceEffect", mDevice->removeDeviceEffect(
569             static_cast<AudioPortHandle>(device->id), hidlEffect->effectId()));
570 }
571 #else
removeDeviceEffect(const struct audio_port_config * device __unused,sp<EffectHalInterface> effect __unused)572 status_t DeviceHalHidl::removeDeviceEffect(
573         const struct audio_port_config *device __unused, sp<EffectHalInterface> effect __unused) {
574     return INVALID_OPERATION;
575 }
576 #endif
577 
prepareToDisconnectExternalDevice(const struct audio_port_v7 * port)578 status_t DeviceHalHidl::prepareToDisconnectExternalDevice(const struct audio_port_v7* port) {
579     // For HIDL HAL, there is not API to call notify the HAL to prepare for device connected
580     // state changed. Call `setConnectedState` directly.
581     const status_t status = setConnectedState(port, false /*connected*/);
582     if (status == NO_ERROR) {
583         // Cache the port id so that it won't disconnect twice.
584         mDeviceDisconnectionNotified.insert(port->id);
585     }
586     return status;
587 }
588 
setConnectedState(const struct audio_port_v7 * port,bool connected)589 status_t DeviceHalHidl::setConnectedState(const struct audio_port_v7 *port, bool connected) {
590     using ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION::AudioPort;
591     TIME_CHECK();
592     if (mDevice == 0) return NO_INIT;
593     if (!connected && mDeviceDisconnectionNotified.erase(port->id) > 0) {
594         // For device disconnection, APM will first call `prepareToDisconnectExternalDevice` and
595         // then call `setConnectedState`. However, in HIDL HAL, there is no API for
596         // `prepareToDisconnectExternalDevice`. In that case, HIDL HAL will call `setConnectedState`
597         // when calling `prepareToDisconnectExternalDevice`. Do not call to the HAL if previous
598         // call is successful. Also remove the cache here to avoid a large cache after a long run.
599         return NO_ERROR;
600     }
601 #if MAJOR_VERSION == 7 && MINOR_VERSION == 1
602     if (supportsSetConnectedState7_1) {
603         AudioPort hidlPort;
604         if (status_t result = HidlUtils::audioPortFromHal(*port, &hidlPort); result != NO_ERROR) {
605             return result;
606         }
607         Return<Result> ret = mDevice->setConnectedState_7_1(hidlPort, connected);
608         if (!ret.isOk() || ret != Result::NOT_SUPPORTED) {
609             return processReturn("setConnectedState_7_1", ret);
610         } else if (ret == Result::OK) {
611             return NO_ERROR;
612         }
613         supportsSetConnectedState7_1 = false;
614     }
615 #endif
616     DeviceAddress hidlAddress;
617     if (status_t result = CoreUtils::deviceAddressFromHal(
618                     port->ext.device.type, port->ext.device.address, &hidlAddress);
619             result != NO_ERROR) {
620         return result;
621     }
622     Return<Result> ret = mDevice->setConnectedState(hidlAddress, connected);
623     if (ret.isOk() || ret == Result::NOT_SUPPORTED) {
624         // The framework is only interested in errors occurring due to connection state handling,
625         // so it can decide whether retrying is needed. If the HAL does not support this operation,
626         // it's not an error.
627         return NO_ERROR;
628     }
629     return processReturn("setConnectedState", ret);
630 }
631 
getHwAvSync()632 error::Result<audio_hw_sync_t> DeviceHalHidl::getHwAvSync() {
633     TIME_CHECK();
634     if (mDevice == 0) return NO_INIT;
635     audio_hw_sync_t value;
636     Result result;
637     Return<void> ret = mDevice->getHwAvSync([&value, &result](Result r, audio_hw_sync_t v) {
638         value = v;
639         result = r;
640     });
641     RETURN_IF_ERROR(processReturn("getHwAvSync", ret, result));
642     return value;
643 }
644 
dump(int fd,const Vector<String16> & args)645 status_t DeviceHalHidl::dump(int fd, const Vector<String16>& args) {
646     TIME_CHECK();
647     if (mDevice == 0) return NO_INIT;
648     native_handle_t* hidlHandle = native_handle_create(1, 0);
649     hidlHandle->data[0] = fd;
650     hidl_vec<hidl_string> hidlArgs;
651     argsFromHal(args, &hidlArgs);
652     Return<void> ret = mDevice->debug(hidlHandle, hidlArgs);
653     native_handle_delete(hidlHandle);
654 
655     // TODO(b/111997867, b/177271958)  Workaround - remove when fixed.
656     // A Binder transmitted fd may not close immediately due to a race condition b/111997867
657     // when the remote binder thread removes the last refcount to the fd blocks in the
658     // kernel for binder activity. We send a Binder ping() command to unblock the thread
659     // and complete the fd close / release.
660     //
661     // See DeviceHalHidl::dump(), EffectHalHidl::dump(), StreamHalHidl::dump(),
662     //     EffectsFactoryHalHidl::dumpEffects().
663 
664     (void)mDevice->ping(); // synchronous Binder call
665 
666     return processReturn("dump", ret);
667 }
668 
669 #if MAJOR_VERSION == 7 && MINOR_VERSION == 1
getSoundDoseInterface(const std::string & module,::ndk::SpAIBinder * soundDoseBinder)670 status_t DeviceHalHidl::getSoundDoseInterface(const std::string& module,
671                                               ::ndk::SpAIBinder* soundDoseBinder) {
672     if (mSoundDoseWrapper->mSoundDose != nullptr) {
673         *soundDoseBinder = mSoundDoseWrapper->mSoundDose->asBinder();
674         return OK;
675     }
676 
677     if (mSoundDoseWrapper->mSoundDoseFactory == nullptr) {
678         std::string interface =
679             std::string(ISoundDoseFactory::descriptor) + kSoundDoseInterfaceModule.data();
680         AIBinder* binder = AServiceManager_checkService(interface.c_str());
681         if (binder == nullptr) {
682             ALOGW("%s service %s doesn't exist", __func__, interface.c_str());
683             return NO_INIT;
684         }
685         mSoundDoseWrapper->mSoundDoseFactory =
686                 ISoundDoseFactory::fromBinder(ndk::SpAIBinder(binder));
687     }
688 
689     auto result = mSoundDoseWrapper->mSoundDoseFactory->getSoundDose(
690                         module, &mSoundDoseWrapper->mSoundDose);
691     if (!result.isOk()) {
692         ALOGW("%s could not get sound dose interface: %s", __func__, result.getMessage());
693         return BAD_VALUE;
694     }
695 
696     if (mSoundDoseWrapper->mSoundDose == nullptr) {
697         ALOGW("%s standalone sound dose interface is not implemented", __func__);
698         *soundDoseBinder = nullptr;
699         return OK;
700     }
701 
702     *soundDoseBinder = mSoundDoseWrapper->mSoundDose->asBinder();
703     ALOGI("%s using standalone sound dose interface", __func__);
704     return OK;
705 }
706 #else
getSoundDoseInterface(const std::string & module,::ndk::SpAIBinder * soundDoseBinder)707 status_t DeviceHalHidl::getSoundDoseInterface(const std::string& module,
708                                               ::ndk::SpAIBinder* soundDoseBinder) {
709     (void)module;  // avoid unused param
710     (void)soundDoseBinder;  // avoid unused param
711     return INVALID_OPERATION;
712 }
713 #endif
714 
supportsBluetoothVariableLatency(bool * supports)715 status_t DeviceHalHidl::supportsBluetoothVariableLatency(bool* supports) {
716     if (supports == nullptr) {
717         return BAD_VALUE;
718     }
719     *supports = false;
720 
721     String8 reply;
722     status_t status = getParameters(
723             String8(AUDIO_PARAMETER_BT_VARIABLE_LATENCY_SUPPORTED), &reply);
724     if (status != NO_ERROR) {
725         return status;
726     }
727     AudioParameter replyParams(reply);
728     String8 trueOrFalse;
729     status = replyParams.get(
730             String8(AUDIO_PARAMETER_BT_VARIABLE_LATENCY_SUPPORTED), trueOrFalse);
731     if (status != NO_ERROR) {
732         return status;
733     }
734     *supports = trueOrFalse == AudioParameter::valueTrue;
735     return NO_ERROR;
736 }
737 
738 namespace {
739 
getParametersFromStream(sp<StreamHalInterface> stream,const char * parameters,const char * extraParameters,String8 * reply)740 status_t getParametersFromStream(
741         sp<StreamHalInterface> stream,
742         const char* parameters,
743         const char* extraParameters,
744         String8* reply) {
745     String8 request(parameters);
746     if (extraParameters != nullptr) {
747         request.append(";");
748         request.append(extraParameters);
749     }
750     status_t status = stream->getParameters(request, reply);
751     if (status != NO_ERROR) {
752         ALOGW("%s, failed to query %s, status=%d", __func__, parameters, status);
753         return status;
754     }
755     AudioParameter repliedParameters(*reply);
756     status = repliedParameters.get(String8(parameters), *reply);
757     if (status != NO_ERROR) {
758         ALOGW("%s: failed to retrieve %s, bailing out", __func__, parameters);
759     }
760     return status;
761 }
762 
763 } // namespace
764 
getAudioMixPort(const struct audio_port_v7 * devicePort,struct audio_port_v7 * mixPort)765 status_t DeviceHalHidl::getAudioMixPort(const struct audio_port_v7 *devicePort,
766                                         struct audio_port_v7 *mixPort) {
767     // For HIDL HAL, querying mix port information is not supported. If the HAL supports
768     // `getAudioPort` API to query the device port attributes, use the structured audio profiles
769     // that have the same attributes reported by the `getParameters` API. Otherwise, only use
770     // the attributes reported by `getParameters` API.
771     struct audio_port_v7 temp = *devicePort;
772     AudioProfileAttributesMultimap attrsFromDevice;
773     bool supportsPatches;
774     if (supportsAudioPatches(&supportsPatches) == OK && supportsPatches) {
775         // The audio patches are supported since HAL 3.0, which is the same HAL version
776         // requirement for 'getAudioPort' API.
777         if (getAudioPort(&temp) == NO_ERROR) {
778             attrsFromDevice = createAudioProfilesAttrMap(temp.audio_profiles, 0 /*first*/,
779                                                          temp.num_audio_profiles);
780         }
781     }
782     auto streamIt = mStreams.find(mixPort->ext.mix.handle);
783     if (streamIt == mStreams.end()) {
784         return BAD_VALUE;
785     }
786     auto stream = streamIt->second.promote();
787     if (stream == nullptr) {
788         return BAD_VALUE;
789     }
790 
791     String8 formatsStr;
792     status_t status = getParametersFromStream(
793             stream, AudioParameter::keyStreamSupportedFormats, nullptr /*extraParameters*/,
794             &formatsStr);
795     if (status != NO_ERROR) {
796         return status;
797     }
798     FormatVector formats = formatsFromString(formatsStr.c_str());
799 
800     mixPort->num_audio_profiles = 0;
801     for (audio_format_t format : formats) {
802         if (mixPort->num_audio_profiles >= AUDIO_PORT_MAX_AUDIO_PROFILES) {
803             ALOGW("%s, too many audio profiles", __func__);
804             break;
805         }
806         AudioParameter formatParameter;
807         formatParameter.addInt(String8(AudioParameter::keyFormat), format);
808 
809         String8 samplingRatesStr;
810         status = getParametersFromStream(
811                 stream, AudioParameter::keyStreamSupportedSamplingRates,
812                 formatParameter.toString(), &samplingRatesStr);
813         if (status != NO_ERROR) {
814             // Failed to query supported sample rate for current format, may succeed with
815             // other formats.
816             ALOGW("Skip adding format=%#x, status=%d", format, status);
817             continue;
818         }
819         SampleRateSet sampleRatesFromStream = samplingRatesFromString(samplingRatesStr.c_str());
820         if (sampleRatesFromStream.empty()) {
821             ALOGW("Skip adding format=%#x as the returned sampling rates are empty", format);
822             continue;
823         }
824         String8 channelMasksStr;
825         status = getParametersFromStream(
826                 stream, AudioParameter::keyStreamSupportedChannels,
827                 formatParameter.toString(), &channelMasksStr);
828         if (status != NO_ERROR) {
829             // Failed to query supported channel masks for current format, may succeed with
830             // other formats.
831             ALOGW("Skip adding format=%#x, status=%d", format, status);
832             continue;
833         }
834         ChannelMaskSet channelMasksFromStream = channelMasksFromString(channelMasksStr.c_str());
835         if (channelMasksFromStream.empty()) {
836             ALOGW("Skip adding format=%#x as the returned channel masks are empty", format);
837             continue;
838         }
839 
840         // For an audio format, all audio profiles from the device port with the same format will
841         // be added to mix port after filtering sample rates, channel masks according to the reply
842         // of getParameters API. If there is any sample rate or channel mask reported by
843         // getParameters API but not reported by the device, additional audio profiles will be
844         // added.
845         populateAudioProfiles(attrsFromDevice, format, channelMasksFromStream,
846                               sampleRatesFromStream, mixPort->audio_profiles,
847                               &mixPort->num_audio_profiles);
848     }
849 
850     return NO_ERROR;
851 }
852 
cleanupStreams()853 void DeviceHalHidl::cleanupStreams() {
854     for (auto it = mStreams.begin(); it != mStreams.end();) {
855         if (it->second.promote() == nullptr) {
856             it = mStreams.erase(it);
857         } else {
858             ++it;
859         }
860     }
861 }
862 
863 } // namespace android
864