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)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     TIME_CHECK();
264     if (mDevice == 0) return NO_INIT;
265     DeviceAddress hidlDevice;
266     if (status_t status = CoreUtils::deviceAddressFromHal(deviceType, address, &hidlDevice);
267             status != OK) {
268         return status;
269     }
270     AudioConfig hidlConfig;
271     if (status_t status = HidlUtils::audioConfigFromHal(*config, false /*isInput*/, &hidlConfig);
272             status != OK) {
273         return status;
274     }
275 
276 #if !(MAJOR_VERSION == 7 && MINOR_VERSION == 1)
277     //TODO: b/193496180 use spatializer flag at audio HAL when available
278     if ((flags & AUDIO_OUTPUT_FLAG_SPATIALIZER) != 0) {
279         flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_SPATIALIZER);
280         flags = (audio_output_flags_t)
281                 (flags | AUDIO_OUTPUT_FLAG_FAST | AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
282     }
283 #endif
284 
285     CoreUtils::AudioOutputFlags hidlFlags;
286     if (status_t status = CoreUtils::audioOutputFlagsFromHal(flags, &hidlFlags); status != OK) {
287         return status;
288     }
289     Result retval = Result::NOT_INITIALIZED;
290 #if MAJOR_VERSION == 7 && MINOR_VERSION == 1
291     Return<void> ret = mDevice->openOutputStream_7_1(
292 #else
293     Return<void> ret = mDevice->openOutputStream(
294 #endif
295             handle, hidlDevice, hidlConfig, hidlFlags,
296 #if MAJOR_VERSION >= 4
297             {} /* metadata */,
298 #endif
299             [&](Result r, const sp<::android::hardware::audio::CPP_VERSION::IStreamOut>& result,
300                     const AudioConfig& suggestedConfig) {
301                 retval = r;
302                 if (retval == Result::OK) {
303                     *outStream = new StreamOutHalHidl(result);
304                 }
305                 HidlUtils::audioConfigToHal(suggestedConfig, config);
306             });
307     const status_t status = processReturn("openOutputStream", ret, retval);
308     cleanupStreams();
309     if (status == NO_ERROR) {
310         mStreams.insert({handle, *outStream});
311     }
312     return status;
313 }
314 
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)315 status_t DeviceHalHidl::openInputStream(
316         audio_io_handle_t handle,
317         audio_devices_t devices,
318         struct audio_config *config,
319         audio_input_flags_t flags,
320         const char *address,
321         audio_source_t source,
322         audio_devices_t outputDevice,
323         const char *outputDeviceAddress,
324         sp<StreamInHalInterface> *inStream) {
325     TIME_CHECK();
326     if (mDevice == 0) return NO_INIT;
327     DeviceAddress hidlDevice;
328     if (status_t status = CoreUtils::deviceAddressFromHal(devices, address, &hidlDevice);
329             status != OK) {
330         return status;
331     }
332     AudioConfig hidlConfig;
333     if (status_t status = HidlUtils::audioConfigFromHal(*config, true /*isInput*/, &hidlConfig);
334             status != OK) {
335         return status;
336     }
337     CoreUtils::AudioInputFlags hidlFlags;
338 #if MAJOR_VERSION <= 5
339     // Some flags were specific to framework and must not leak to the HAL.
340     flags = static_cast<audio_input_flags_t>(flags & ~AUDIO_INPUT_FLAG_DIRECT);
341 #endif
342     if (status_t status = CoreUtils::audioInputFlagsFromHal(flags, &hidlFlags); status != OK) {
343         return status;
344     }
345     Result retval = Result::NOT_INITIALIZED;
346 #if MAJOR_VERSION == 2
347     auto sinkMetadata = AudioSource(source);
348 #elif MAJOR_VERSION >= 4
349     // TODO: correctly propagate the tracks sources and volume
350     //       for now, only send the main source at 1dbfs
351     AudioSource hidlSource;
352     if (status_t status = HidlUtils::audioSourceFromHal(source, &hidlSource); status != OK) {
353         return status;
354     }
355     SinkMetadata sinkMetadata = {{{ .source = std::move(hidlSource), .gain = 1 }}};
356 #endif
357 #if MAJOR_VERSION < 5
358     (void)outputDevice;
359     (void)outputDeviceAddress;
360 #else
361 #if MAJOR_VERSION >= 7
362     (void)HidlUtils::audioChannelMaskFromHal(
363             AUDIO_CHANNEL_NONE, true /*isInput*/, &sinkMetadata.tracks[0].channelMask);
364 #endif
365     if (outputDevice != AUDIO_DEVICE_NONE) {
366         DeviceAddress hidlOutputDevice;
367         if (status_t status = CoreUtils::deviceAddressFromHal(
368                         outputDevice, outputDeviceAddress, &hidlOutputDevice); status != OK) {
369             return status;
370         }
371         sinkMetadata.tracks[0].destination.device(std::move(hidlOutputDevice));
372     }
373 #endif
374     Return<void> ret = mDevice->openInputStream(
375             handle, hidlDevice, hidlConfig, hidlFlags, sinkMetadata,
376             [&](Result r,
377                 const sp<::android::hardware::audio::CORE_TYPES_CPP_VERSION::IStreamIn>& result,
378                     const AudioConfig& suggestedConfig) {
379                 retval = r;
380                 if (retval == Result::OK) {
381                     *inStream = new StreamInHalHidl(result);
382                 }
383                 HidlUtils::audioConfigToHal(suggestedConfig, config);
384             });
385     const status_t status = processReturn("openInputStream", ret, retval);
386     cleanupStreams();
387     if (status == NO_ERROR) {
388         mStreams.insert({handle, *inStream});
389     }
390     return status;
391 }
392 
supportsAudioPatches(bool * supportsPatches)393 status_t DeviceHalHidl::supportsAudioPatches(bool *supportsPatches) {
394     TIME_CHECK();
395     if (mDevice == 0) return NO_INIT;
396     return processReturn("supportsAudioPatches", mDevice->supportsAudioPatches(), supportsPatches);
397 }
398 
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)399 status_t DeviceHalHidl::createAudioPatch(
400         unsigned int num_sources,
401         const struct audio_port_config *sources,
402         unsigned int num_sinks,
403         const struct audio_port_config *sinks,
404         audio_patch_handle_t *patch) {
405     TIME_CHECK();
406     if (mDevice == 0) return NO_INIT;
407     if (patch == nullptr) return BAD_VALUE;
408 
409 #if MAJOR_VERSION < 6
410     if (*patch != AUDIO_PATCH_HANDLE_NONE) {
411         status_t status = releaseAudioPatch(*patch);
412         ALOGW_IF(status != NO_ERROR, "%s error %d releasing patch handle %d",
413             __func__, status, *patch);
414         *patch = AUDIO_PATCH_HANDLE_NONE;
415     }
416 #endif
417 
418     hidl_vec<AudioPortConfig> hidlSources, hidlSinks;
419     HidlUtils::audioPortConfigsFromHal(num_sources, sources, &hidlSources);
420     HidlUtils::audioPortConfigsFromHal(num_sinks, sinks, &hidlSinks);
421     Result retval = Result::OK;
422     Return<void> ret;
423     std::string methodName = "createAudioPatch";
424     if (*patch == AUDIO_PATCH_HANDLE_NONE) {  // always true for MAJOR_VERSION < 6
425         ret = mDevice->createAudioPatch(
426                 hidlSources, hidlSinks,
427                 [&](Result r, AudioPatchHandle hidlPatch) {
428                     retval = r;
429                     if (retval == Result::OK) {
430                         *patch = static_cast<audio_patch_handle_t>(hidlPatch);
431                     }
432                 });
433     } else {
434 #if MAJOR_VERSION >= 6
435         ret = mDevice->updateAudioPatch(
436                 *patch,
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         methodName = "updateAudioPatch";
445 #endif
446     }
447     return processReturn(methodName.c_str(), ret, retval);
448 }
449 
releaseAudioPatch(audio_patch_handle_t patch)450 status_t DeviceHalHidl::releaseAudioPatch(audio_patch_handle_t patch) {
451     TIME_CHECK();
452     if (mDevice == 0) return NO_INIT;
453     return processReturn("releaseAudioPatch", mDevice->releaseAudioPatch(patch));
454 }
455 
456 template <typename HalPort>
getAudioPortImpl(HalPort * port)457 status_t DeviceHalHidl::getAudioPortImpl(HalPort *port) {
458     using ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION::AudioPort;
459     if (mDevice == 0) return NO_INIT;
460     AudioPort hidlPort;
461     HidlUtils::audioPortFromHal(*port, &hidlPort);
462     Result retval;
463     Return<void> ret = mDevice->getAudioPort(
464             hidlPort,
465             [&](Result r, const AudioPort& p) {
466                 retval = r;
467                 if (retval == Result::OK) {
468                     HidlUtils::audioPortToHal(p, port);
469                 }
470             });
471     return processReturn("getAudioPort", ret, retval);
472 }
473 
getAudioPort(struct audio_port * port)474 status_t DeviceHalHidl::getAudioPort(struct audio_port *port) {
475     TIME_CHECK();
476     return getAudioPortImpl(port);
477 }
478 
getAudioPort(struct audio_port_v7 * port)479 status_t DeviceHalHidl::getAudioPort(struct audio_port_v7 *port) {
480     TIME_CHECK();
481 #if MAJOR_VERSION >= 7
482     return getAudioPortImpl(port);
483 #else
484     struct audio_port audioPort = {};
485     status_t result = NO_ERROR;
486     if (!audio_populate_audio_port(port, &audioPort)) {
487         ALOGE("Failed to populate legacy audio port from audio_port_v7");
488         result = BAD_VALUE;
489     }
490     status_t status = getAudioPort(&audioPort);
491     if (status == NO_ERROR) {
492         audio_populate_audio_port_v7(&audioPort, port);
493     } else {
494         result = status;
495     }
496     return result;
497 #endif
498 }
499 
setAudioPortConfig(const struct audio_port_config * config)500 status_t DeviceHalHidl::setAudioPortConfig(const struct audio_port_config *config) {
501     using ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION::AudioPortConfig;
502     TIME_CHECK();
503     if (mDevice == 0) return NO_INIT;
504     AudioPortConfig hidlConfig;
505     HidlUtils::audioPortConfigFromHal(*config, &hidlConfig);
506     return processReturn("setAudioPortConfig", mDevice->setAudioPortConfig(hidlConfig));
507 }
508 
509 #if MAJOR_VERSION == 2
getMicrophones(std::vector<audio_microphone_characteristic_t> * microphonesInfo __unused)510 status_t DeviceHalHidl::getMicrophones(
511         std::vector<audio_microphone_characteristic_t> *microphonesInfo __unused) {
512     if (mDevice == 0) return NO_INIT;
513     return INVALID_OPERATION;
514 }
515 #elif MAJOR_VERSION >= 4
getMicrophones(std::vector<audio_microphone_characteristic_t> * microphonesInfo)516 status_t DeviceHalHidl::getMicrophones(
517         std::vector<audio_microphone_characteristic_t> *microphonesInfo) {
518     TIME_CHECK();
519     if (mDevice == 0) return NO_INIT;
520     Result retval;
521     Return<void> ret = mDevice->getMicrophones(
522             [&](Result r, hidl_vec<MicrophoneInfo> micArrayHal) {
523         retval = r;
524         for (size_t k = 0; k < micArrayHal.size(); k++) {
525             audio_microphone_characteristic_t dst;
526             //convert
527             (void)CoreUtils::microphoneInfoToHal(micArrayHal[k], &dst);
528             microphonesInfo->push_back(dst);
529         }
530     });
531     return processReturn("getMicrophones", ret, retval);
532 }
533 #endif
534 
535 #if MAJOR_VERSION >= 6
addDeviceEffect(const struct audio_port_config * device,sp<EffectHalInterface> effect)536 status_t DeviceHalHidl::addDeviceEffect(
537         const struct audio_port_config *device, sp<EffectHalInterface> effect) {
538     TIME_CHECK();
539     if (mDevice == 0) return NO_INIT;
540     auto hidlEffect = sp<effect::EffectHalHidl>::cast(effect);
541     return processReturn("addDeviceEffect", mDevice->addDeviceEffect(
542             static_cast<AudioPortHandle>(device->id), hidlEffect->effectId()));
543 }
544 #else
addDeviceEffect(const struct audio_port_config * device __unused,sp<EffectHalInterface> effect __unused)545 status_t DeviceHalHidl::addDeviceEffect(
546         const struct audio_port_config *device __unused, sp<EffectHalInterface> effect __unused) {
547     return INVALID_OPERATION;
548 }
549 #endif
550 
551 #if MAJOR_VERSION >= 6
removeDeviceEffect(const struct audio_port_config * device,sp<EffectHalInterface> effect)552 status_t DeviceHalHidl::removeDeviceEffect(
553         const struct audio_port_config *device, sp<EffectHalInterface> effect) {
554     TIME_CHECK();
555     if (mDevice == 0) return NO_INIT;
556     auto hidlEffect = sp<effect::EffectHalHidl>::cast(effect);
557     return processReturn("removeDeviceEffect", mDevice->removeDeviceEffect(
558             static_cast<AudioPortHandle>(device->id), hidlEffect->effectId()));
559 }
560 #else
removeDeviceEffect(const struct audio_port_config * device __unused,sp<EffectHalInterface> effect __unused)561 status_t DeviceHalHidl::removeDeviceEffect(
562         const struct audio_port_config *device __unused, sp<EffectHalInterface> effect __unused) {
563     return INVALID_OPERATION;
564 }
565 #endif
566 
prepareToDisconnectExternalDevice(const struct audio_port_v7 * port)567 status_t DeviceHalHidl::prepareToDisconnectExternalDevice(const struct audio_port_v7* port) {
568     // For HIDL HAL, there is not API to call notify the HAL to prepare for device connected
569     // state changed. Call `setConnectedState` directly.
570     const status_t status = setConnectedState(port, false /*connected*/);
571     if (status == NO_ERROR) {
572         // Cache the port id so that it won't disconnect twice.
573         mDeviceDisconnectionNotified.insert(port->id);
574     }
575     return status;
576 }
577 
setConnectedState(const struct audio_port_v7 * port,bool connected)578 status_t DeviceHalHidl::setConnectedState(const struct audio_port_v7 *port, bool connected) {
579     using ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION::AudioPort;
580     TIME_CHECK();
581     if (mDevice == 0) return NO_INIT;
582     if (!connected && mDeviceDisconnectionNotified.erase(port->id) > 0) {
583         // For device disconnection, APM will first call `prepareToDisconnectExternalDevice` and
584         // then call `setConnectedState`. However, in HIDL HAL, there is no API for
585         // `prepareToDisconnectExternalDevice`. In that case, HIDL HAL will call `setConnectedState`
586         // when calling `prepareToDisconnectExternalDevice`. Do not call to the HAL if previous
587         // call is successful. Also remove the cache here to avoid a large cache after a long run.
588         return NO_ERROR;
589     }
590 #if MAJOR_VERSION == 7 && MINOR_VERSION == 1
591     if (supportsSetConnectedState7_1) {
592         AudioPort hidlPort;
593         if (status_t result = HidlUtils::audioPortFromHal(*port, &hidlPort); result != NO_ERROR) {
594             return result;
595         }
596         Return<Result> ret = mDevice->setConnectedState_7_1(hidlPort, connected);
597         if (!ret.isOk() || ret != Result::NOT_SUPPORTED) {
598             return processReturn("setConnectedState_7_1", ret);
599         } else if (ret == Result::OK) {
600             return NO_ERROR;
601         }
602         supportsSetConnectedState7_1 = false;
603     }
604 #endif
605     DeviceAddress hidlAddress;
606     if (status_t result = CoreUtils::deviceAddressFromHal(
607                     port->ext.device.type, port->ext.device.address, &hidlAddress);
608             result != NO_ERROR) {
609         return result;
610     }
611     return processReturn("setConnectedState", mDevice->setConnectedState(hidlAddress, connected));
612 }
613 
getHwAvSync()614 error::Result<audio_hw_sync_t> DeviceHalHidl::getHwAvSync() {
615     TIME_CHECK();
616     if (mDevice == 0) return NO_INIT;
617     audio_hw_sync_t value;
618     Result result;
619     Return<void> ret = mDevice->getHwAvSync([&value, &result](Result r, audio_hw_sync_t v) {
620         value = v;
621         result = r;
622     });
623     RETURN_IF_ERROR(processReturn("getHwAvSync", ret, result));
624     return value;
625 }
626 
dump(int fd,const Vector<String16> & args)627 status_t DeviceHalHidl::dump(int fd, const Vector<String16>& args) {
628     TIME_CHECK();
629     if (mDevice == 0) return NO_INIT;
630     native_handle_t* hidlHandle = native_handle_create(1, 0);
631     hidlHandle->data[0] = fd;
632     hidl_vec<hidl_string> hidlArgs;
633     argsFromHal(args, &hidlArgs);
634     Return<void> ret = mDevice->debug(hidlHandle, hidlArgs);
635     native_handle_delete(hidlHandle);
636 
637     // TODO(b/111997867, b/177271958)  Workaround - remove when fixed.
638     // A Binder transmitted fd may not close immediately due to a race condition b/111997867
639     // when the remote binder thread removes the last refcount to the fd blocks in the
640     // kernel for binder activity. We send a Binder ping() command to unblock the thread
641     // and complete the fd close / release.
642     //
643     // See DeviceHalHidl::dump(), EffectHalHidl::dump(), StreamHalHidl::dump(),
644     //     EffectsFactoryHalHidl::dumpEffects().
645 
646     (void)mDevice->ping(); // synchronous Binder call
647 
648     return processReturn("dump", ret);
649 }
650 
651 #if MAJOR_VERSION == 7 && MINOR_VERSION == 1
getSoundDoseInterface(const std::string & module,::ndk::SpAIBinder * soundDoseBinder)652 status_t DeviceHalHidl::getSoundDoseInterface(const std::string& module,
653                                               ::ndk::SpAIBinder* soundDoseBinder) {
654     if (mSoundDoseWrapper->mSoundDose != nullptr) {
655         *soundDoseBinder = mSoundDoseWrapper->mSoundDose->asBinder();
656         return OK;
657     }
658 
659     if (mSoundDoseWrapper->mSoundDoseFactory == nullptr) {
660         std::string interface =
661             std::string(ISoundDoseFactory::descriptor) + kSoundDoseInterfaceModule.data();
662         AIBinder* binder = AServiceManager_checkService(interface.c_str());
663         if (binder == nullptr) {
664             ALOGW("%s service %s doesn't exist", __func__, interface.c_str());
665             return NO_INIT;
666         }
667         mSoundDoseWrapper->mSoundDoseFactory =
668                 ISoundDoseFactory::fromBinder(ndk::SpAIBinder(binder));
669     }
670 
671     auto result = mSoundDoseWrapper->mSoundDoseFactory->getSoundDose(
672                         module, &mSoundDoseWrapper->mSoundDose);
673     if (!result.isOk()) {
674         ALOGW("%s could not get sound dose interface: %s", __func__, result.getMessage());
675         return BAD_VALUE;
676     }
677 
678     if (mSoundDoseWrapper->mSoundDose == nullptr) {
679         ALOGW("%s standalone sound dose interface is not implemented", __func__);
680         *soundDoseBinder = nullptr;
681         return OK;
682     }
683 
684     *soundDoseBinder = mSoundDoseWrapper->mSoundDose->asBinder();
685     ALOGI("%s using standalone sound dose interface", __func__);
686     return OK;
687 }
688 #else
getSoundDoseInterface(const std::string & module,::ndk::SpAIBinder * soundDoseBinder)689 status_t DeviceHalHidl::getSoundDoseInterface(const std::string& module,
690                                               ::ndk::SpAIBinder* soundDoseBinder) {
691     (void)module;  // avoid unused param
692     (void)soundDoseBinder;  // avoid unused param
693     return INVALID_OPERATION;
694 }
695 #endif
696 
supportsBluetoothVariableLatency(bool * supports)697 status_t DeviceHalHidl::supportsBluetoothVariableLatency(bool* supports) {
698     if (supports == nullptr) {
699         return BAD_VALUE;
700     }
701     *supports = false;
702 
703     String8 reply;
704     status_t status = getParameters(
705             String8(AUDIO_PARAMETER_BT_VARIABLE_LATENCY_SUPPORTED), &reply);
706     if (status != NO_ERROR) {
707         return status;
708     }
709     AudioParameter replyParams(reply);
710     String8 trueOrFalse;
711     status = replyParams.get(
712             String8(AUDIO_PARAMETER_BT_VARIABLE_LATENCY_SUPPORTED), trueOrFalse);
713     if (status != NO_ERROR) {
714         return status;
715     }
716     *supports = trueOrFalse == AudioParameter::valueTrue;
717     return NO_ERROR;
718 }
719 
720 namespace {
721 
getParametersFromStream(sp<StreamHalInterface> stream,const char * parameters,const char * extraParameters,String8 * reply)722 status_t getParametersFromStream(
723         sp<StreamHalInterface> stream,
724         const char* parameters,
725         const char* extraParameters,
726         String8* reply) {
727     String8 request(parameters);
728     if (extraParameters != nullptr) {
729         request.append(";");
730         request.append(extraParameters);
731     }
732     status_t status = stream->getParameters(request, reply);
733     if (status != NO_ERROR) {
734         ALOGW("%s, failed to query %s, status=%d", __func__, parameters, status);
735         return status;
736     }
737     AudioParameter repliedParameters(*reply);
738     status = repliedParameters.get(String8(parameters), *reply);
739     if (status != NO_ERROR) {
740         ALOGW("%s: failed to retrieve %s, bailing out", __func__, parameters);
741     }
742     return status;
743 }
744 
745 } // namespace
746 
getAudioMixPort(const struct audio_port_v7 * devicePort,struct audio_port_v7 * mixPort)747 status_t DeviceHalHidl::getAudioMixPort(const struct audio_port_v7 *devicePort,
748                                         struct audio_port_v7 *mixPort) {
749     // For HIDL HAL, querying mix port information is not supported. If the HAL supports
750     // `getAudioPort` API to query the device port attributes, use the structured audio profiles
751     // that have the same attributes reported by the `getParameters` API. Otherwise, only use
752     // the attributes reported by `getParameters` API.
753     struct audio_port_v7 temp = *devicePort;
754     AudioProfileAttributesMultimap attrsFromDevice;
755     bool supportsPatches;
756     if (supportsAudioPatches(&supportsPatches) == OK && supportsPatches) {
757         // The audio patches are supported since HAL 3.0, which is the same HAL version
758         // requirement for 'getAudioPort' API.
759         if (getAudioPort(&temp) == NO_ERROR) {
760             attrsFromDevice = createAudioProfilesAttrMap(temp.audio_profiles, 0 /*first*/,
761                                                          temp.num_audio_profiles);
762         }
763     }
764     auto streamIt = mStreams.find(mixPort->ext.mix.handle);
765     if (streamIt == mStreams.end()) {
766         return BAD_VALUE;
767     }
768     auto stream = streamIt->second.promote();
769     if (stream == nullptr) {
770         return BAD_VALUE;
771     }
772 
773     String8 formatsStr;
774     status_t status = getParametersFromStream(
775             stream, AudioParameter::keyStreamSupportedFormats, nullptr /*extraParameters*/,
776             &formatsStr);
777     if (status != NO_ERROR) {
778         return status;
779     }
780     FormatVector formats = formatsFromString(formatsStr.c_str());
781 
782     mixPort->num_audio_profiles = 0;
783     for (audio_format_t format : formats) {
784         if (mixPort->num_audio_profiles >= AUDIO_PORT_MAX_AUDIO_PROFILES) {
785             ALOGW("%s, too many audio profiles", __func__);
786             break;
787         }
788         AudioParameter formatParameter;
789         formatParameter.addInt(String8(AudioParameter::keyFormat), format);
790 
791         String8 samplingRatesStr;
792         status = getParametersFromStream(
793                 stream, AudioParameter::keyStreamSupportedSamplingRates,
794                 formatParameter.toString(), &samplingRatesStr);
795         if (status != NO_ERROR) {
796             // Failed to query supported sample rate for current format, may succeed with
797             // other formats.
798             ALOGW("Skip adding format=%#x, status=%d", format, status);
799             continue;
800         }
801         SampleRateSet sampleRatesFromStream = samplingRatesFromString(samplingRatesStr.c_str());
802         if (sampleRatesFromStream.empty()) {
803             ALOGW("Skip adding format=%#x as the returned sampling rates are empty", format);
804             continue;
805         }
806         String8 channelMasksStr;
807         status = getParametersFromStream(
808                 stream, AudioParameter::keyStreamSupportedChannels,
809                 formatParameter.toString(), &channelMasksStr);
810         if (status != NO_ERROR) {
811             // Failed to query supported channel masks for current format, may succeed with
812             // other formats.
813             ALOGW("Skip adding format=%#x, status=%d", format, status);
814             continue;
815         }
816         ChannelMaskSet channelMasksFromStream = channelMasksFromString(channelMasksStr.c_str());
817         if (channelMasksFromStream.empty()) {
818             ALOGW("Skip adding format=%#x as the returned channel masks are empty", format);
819             continue;
820         }
821 
822         // For an audio format, all audio profiles from the device port with the same format will
823         // be added to mix port after filtering sample rates, channel masks according to the reply
824         // of getParameters API. If there is any sample rate or channel mask reported by
825         // getParameters API but not reported by the device, additional audio profiles will be
826         // added.
827         populateAudioProfiles(attrsFromDevice, format, channelMasksFromStream,
828                               sampleRatesFromStream, mixPort->audio_profiles,
829                               &mixPort->num_audio_profiles);
830     }
831 
832     return NO_ERROR;
833 }
834 
cleanupStreams()835 void DeviceHalHidl::cleanupStreams() {
836     for (auto it = mStreams.begin(); it != mStreams.end();) {
837         if (it->second.promote() == nullptr) {
838             it = mStreams.erase(it);
839         } else {
840             ++it;
841         }
842     }
843 }
844 
845 } // namespace android
846