From ebebc9c26611c0f29a88e44b49bacd7ce3f5e4e7 Mon Sep 17 00:00:00 2001 From: Hayden Gomes Date: Fri, 10 Jan 2020 08:49:23 -0800 Subject: [PATCH] Adding v2 of audiocontrol hal - Removes deprecated getBusForContext - Adds APIs for enabling HAL to request and abandon audio focus Bug: 148098383 Test: vts-tradefed -module VtsHalAudioControlV2_0TargetTest Change-Id: I353bbd8ea7708ffb59831a8e1008951146798034 --- automotive/audiocontrol/2.0/Android.bp | 20 +++ automotive/audiocontrol/2.0/IAudioControl.hal | 78 +++++++++ automotive/audiocontrol/2.0/ICloseHandle.hal | 34 ++++ .../audiocontrol/2.0/IFocusListener.hal | 55 +++++++ .../audiocontrol/2.0/default/Android.bp | 39 +++++ .../audiocontrol/2.0/default/AudioControl.cpp | 60 +++++++ .../audiocontrol/2.0/default/AudioControl.h | 49 ++++++ .../audiocontrol/2.0/default/CloseHandle.cpp | 35 ++++ .../audiocontrol/2.0/default/CloseHandle.h | 48 ++++++ ...are.automotive.audiocontrol@2.0-service.rc | 4 + .../2.0/default/audiocontrol2_manifest.xml | 11 ++ .../audiocontrol/2.0/default/service.cpp | 52 ++++++ automotive/audiocontrol/2.0/types.hal | 31 ++++ .../2.0/vts/functional/Android.bp | 29 ++++ .../VtsHalAudioControlV2_0TargetTest.cpp | 155 ++++++++++++++++++ 15 files changed, 700 insertions(+) create mode 100644 automotive/audiocontrol/2.0/Android.bp create mode 100644 automotive/audiocontrol/2.0/IAudioControl.hal create mode 100644 automotive/audiocontrol/2.0/ICloseHandle.hal create mode 100644 automotive/audiocontrol/2.0/IFocusListener.hal create mode 100644 automotive/audiocontrol/2.0/default/Android.bp create mode 100644 automotive/audiocontrol/2.0/default/AudioControl.cpp create mode 100644 automotive/audiocontrol/2.0/default/AudioControl.h create mode 100644 automotive/audiocontrol/2.0/default/CloseHandle.cpp create mode 100644 automotive/audiocontrol/2.0/default/CloseHandle.h create mode 100644 automotive/audiocontrol/2.0/default/android.hardware.automotive.audiocontrol@2.0-service.rc create mode 100644 automotive/audiocontrol/2.0/default/audiocontrol2_manifest.xml create mode 100644 automotive/audiocontrol/2.0/default/service.cpp create mode 100644 automotive/audiocontrol/2.0/types.hal create mode 100644 automotive/audiocontrol/2.0/vts/functional/Android.bp create mode 100644 automotive/audiocontrol/2.0/vts/functional/VtsHalAudioControlV2_0TargetTest.cpp diff --git a/automotive/audiocontrol/2.0/Android.bp b/automotive/audiocontrol/2.0/Android.bp new file mode 100644 index 0000000000..2a9f8499e3 --- /dev/null +++ b/automotive/audiocontrol/2.0/Android.bp @@ -0,0 +1,20 @@ +// This file is autogenerated by hidl-gen -Landroidbp. + +hidl_interface { + name: "android.hardware.automotive.audiocontrol@2.0", + root: "android.hardware", + vndk: { + enabled: true, + }, + srcs: [ + "types.hal", + "IAudioControl.hal", + "ICloseHandle.hal", + "IFocusListener.hal", + ], + interfaces: [ + "android.hidl.base@1.0", + "android.hardware.audio.common@6.0", + ], + gen_java: true, +} diff --git a/automotive/audiocontrol/2.0/IAudioControl.hal b/automotive/audiocontrol/2.0/IAudioControl.hal new file mode 100644 index 0000000000..1073498f61 --- /dev/null +++ b/automotive/audiocontrol/2.0/IAudioControl.hal @@ -0,0 +1,78 @@ +/* + * Copyright (C) 2020 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.hardware.automotive.audiocontrol@2.0; + +import ICloseHandle; +import IFocusListener; +import android.hardware.audio.common@6.0::AudioUsage; + +/** + * Interacts with the car's audio subsystem to manage audio sources and volumes + */ +interface IAudioControl { + /** + * Registers focus listener to be used by HAL for requesting and abandoning audio focus. + * + * It is expected that there will only ever be a single focus listener registered. If the + * observer dies, the HAL implementation must unregister observer automatically. If called when + * a listener is already registered, the existing one should be unregistered and replaced with + * the new listener. + * + * @param listener the listener interface + * @return closeHandle A handle to unregister observer. + */ + registerFocusListener(IFocusListener listener) generates (ICloseHandle closeHandle); + + /** + * Notifies HAL of changes in audio focus status for focuses requested or abandoned by the HAL. + * + * This will be called in response to IFocusListener's requestAudioFocus and + * abandonAudioFocus, as well as part of any change in focus being held by the HAL due focus + * request from other activities or services. + * + * The HAL is not required to wait for an callback of AUDIOFOCUS_GAIN before playing audio, nor + * is it required to stop playing audio in the event of a AUDIOFOCUS_LOSS callback is received. + * + * @param usage The audio usage associated with the focus change {@code AttributeUsage} + * @param zoneId The identifier for the audio zone that the HAL is playing the stream in + * @param focusChange the AudioFocusChange that has occurred + */ + oneway onAudioFocusChange(bitfield usage, int32_t zoneId, + bitfield focusChange); + + /** + * Control the right/left balance setting of the car speakers. + * + * This is intended to shift the speaker volume toward the right (+) or left (-) side of + * the car. 0.0 means "centered". +1.0 means fully right. -1.0 means fully left. + * + * A value outside the range -1 to 1 must be clamped by the implementation to the -1 to 1 + * range. + */ + oneway setBalanceTowardRight(float value); + + /** + * Control the fore/aft fade setting of the car speakers. + * + * This is intended to shift the speaker volume toward the front (+) or back (-) of the car. + * 0.0 means "centered". +1.0 means fully forward. -1.0 means fully rearward. + * + * A value outside the range -1 to 1 must be clamped by the implementation to the -1 to 1 + * range. + */ + oneway setFadeTowardFront(float value); +}; diff --git a/automotive/audiocontrol/2.0/ICloseHandle.hal b/automotive/audiocontrol/2.0/ICloseHandle.hal new file mode 100644 index 0000000000..537af6d635 --- /dev/null +++ b/automotive/audiocontrol/2.0/ICloseHandle.hal @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2020 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.hardware.automotive.audiocontrol@2.0; + +/** + * Represents a generic close handle to remove a callback that doesn't need + * active interface. + * + * When close() is called OR when the interface is released, the underlying + * resources must be freed. + */ +interface ICloseHandle { + /** + * Closes the handle. + * + * The call must not fail and must be issued by the client at most once. + * Otherwise, the server must ignore subsequent calls. + */ + close(); +}; diff --git a/automotive/audiocontrol/2.0/IFocusListener.hal b/automotive/audiocontrol/2.0/IFocusListener.hal new file mode 100644 index 0000000000..4fd5ef0425 --- /dev/null +++ b/automotive/audiocontrol/2.0/IFocusListener.hal @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2020 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.hardware.automotive.audiocontrol@2.0; + +import android.hardware.audio.common@6.0::AudioUsage; + +/** + * Callback interface for audio focus listener. + * + * For typical configuration, the listener the car audio service. + */ +interface IFocusListener { + /** + * Called whenever HAL is requesting focus as it is starting to play audio of a given usage in a + * specified zone. + * + * In response, IAudioControl#onAudioFocusChange will be called with focusChange status. This + * interaction is oneway to avoid blocking HAL so that it is not required to wait for a response + * before playing audio. + * + * @param usage The audio usage associated with the focus request {@code AttributeUsage} + * @param zoneId The identifier for the audio zone where the HAL is requesting focus + * @param focusGain The AudioFocusChange associated with this request. Should be one of the + * following: GAIN, GAIN_TRANSIENT, GAIN_TRANSIENT_MAY_DUCK, GAIN_TRANSIENT_EXCLUSIVE. + */ + oneway requestAudioFocus(bitfield usage, int32_t zoneId, + bitfield focusGain); + + /** + * Called whenever HAL is abandoning focus as it is finished playing audio of a given usage in a + * specific zone. + * + * In response, IAudioControl#onAudioFocusChange will be called with focusChange status. This + * interaction is oneway to avoid blocking HAL so that it is not required to wait for a response + * before stopping audio playback. + * + * @param usage The audio usage for which the HAL is abandoning focus {@code AttributeUsage} + * @param zoneId The identifier for the audio zone that the HAL abandoning focus + */ + oneway abandonAudioFocus(bitfield usage, int32_t zoneId); +}; diff --git a/automotive/audiocontrol/2.0/default/Android.bp b/automotive/audiocontrol/2.0/default/Android.bp new file mode 100644 index 0000000000..44ad0281fe --- /dev/null +++ b/automotive/audiocontrol/2.0/default/Android.bp @@ -0,0 +1,39 @@ +// Copyright (C) 2020 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +cc_binary { + name: "android.hardware.automotive.audiocontrol@2.0-service", + defaults: ["hidl_defaults"], + vendor: true, + relative_install_path: "hw", + srcs: [ + "AudioControl.cpp", + "service.cpp", + "CloseHandle.cpp", + ], + init_rc: ["android.hardware.automotive.audiocontrol@2.0-service.rc"], + + shared_libs: [ + "android.hardware.automotive.audiocontrol@2.0", + "libbase", + "libhidlbase", + "liblog", + "libutils", + ], + vintf_fragments: ["audiocontrol2_manifest.xml"], + cflags: [ + "-O0", + "-g", + ], +} diff --git a/automotive/audiocontrol/2.0/default/AudioControl.cpp b/automotive/audiocontrol/2.0/default/AudioControl.cpp new file mode 100644 index 0000000000..6505e34765 --- /dev/null +++ b/automotive/audiocontrol/2.0/default/AudioControl.cpp @@ -0,0 +1,60 @@ +#include "AudioControl.h" + +#include +#include + +#include "CloseHandle.h" + +namespace android::hardware::automotive::audiocontrol::V2_0::implementation { + +AudioControl::AudioControl() {} + +Return> AudioControl::registerFocusListener(const sp& listener) { + LOG(DEBUG) << "registering focus listener"; + sp closeHandle(nullptr); + + if (listener) { + mFocusListener = listener; + + closeHandle = new CloseHandle([this, listener]() { + if (mFocusListener == listener) { + mFocusListener = nullptr; + } + }); + } else { + LOG(ERROR) << "Unexpected nullptr for listener resulting in no-op."; + } + + return closeHandle; +} + +Return AudioControl::setBalanceTowardRight(float value) { + // For completeness, lets bounds check the input... + if (isValidValue(value)) { + LOG(ERROR) << "Balance value out of range -1 to 1 at " << value; + } else { + // Just log in this default mock implementation + LOG(INFO) << "Balance set to " << value; + } + return Void(); +} + +Return AudioControl::setFadeTowardFront(float value) { + // For completeness, lets bounds check the input... + if (isValidValue(value)) { + LOG(ERROR) << "Fader value out of range -1 to 1 at " << value; + } else { + // Just log in this default mock implementation + LOG(INFO) << "Fader set to " << value; + } + return Void(); +} + +Return AudioControl::onAudioFocusChange(hidl_bitfield usage, int zoneId, + hidl_bitfield focusChange) { + LOG(INFO) << "Focus changed: " << static_cast(focusChange) << " for usage " + << static_cast(usage) << " in zone " << zoneId; + return Void(); +} + +} // namespace android::hardware::automotive::audiocontrol::V2_0::implementation diff --git a/automotive/audiocontrol/2.0/default/AudioControl.h b/automotive/audiocontrol/2.0/default/AudioControl.h new file mode 100644 index 0000000000..475a693cb7 --- /dev/null +++ b/automotive/audiocontrol/2.0/default/AudioControl.h @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2020 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ANDROID_HARDWARE_AUTOMOTIVE_AUDIOCONTROL_V2_0_AUDIOCONTROL_H +#define ANDROID_HARDWARE_AUTOMOTIVE_AUDIOCONTROL_V2_0_AUDIOCONTROL_H + +#include +#include +#include +#include +#include + +using android::hardware::audio::common::V6_0::AudioUsage; + +namespace android::hardware::automotive::audiocontrol::V2_0::implementation { + +class AudioControl : public IAudioControl { + public: + // Methods from ::android::hardware::automotive::audiocontrol::V2_0::IAudioControl follow. + Return> registerFocusListener(const sp& listener); + Return onAudioFocusChange(hidl_bitfield usage, int zoneId, + hidl_bitfield focusChange); + Return setBalanceTowardRight(float value) override; + Return setFadeTowardFront(float value) override; + + // Implementation details + AudioControl(); + + private: + sp mFocusListener; + static bool isValidValue(float value) { return (value > 1.0f) || (value < -1.0f); } +}; + +} // namespace android::hardware::automotive::audiocontrol::V2_0::implementation + +#endif // ANDROID_HARDWARE_AUTOMOTIVE_AUDIOCONTROL_V2_0_AUDIOCONTROL_H diff --git a/automotive/audiocontrol/2.0/default/CloseHandle.cpp b/automotive/audiocontrol/2.0/default/CloseHandle.cpp new file mode 100644 index 0000000000..bc47931535 --- /dev/null +++ b/automotive/audiocontrol/2.0/default/CloseHandle.cpp @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2020 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "CloseHandle.h" + +namespace android::hardware::automotive::audiocontrol::V2_0::implementation { + +CloseHandle::CloseHandle(Callback callback) : mCallback(callback) {} + +CloseHandle::~CloseHandle() { + close(); +} + +Return CloseHandle::close() { + const auto wasClosed = mIsClosed.exchange(true); + if (wasClosed) return {}; + + if (mCallback) mCallback(); + return {}; +} + +} // namespace android::hardware::automotive::audiocontrol::V2_0::implementation diff --git a/automotive/audiocontrol/2.0/default/CloseHandle.h b/automotive/audiocontrol/2.0/default/CloseHandle.h new file mode 100644 index 0000000000..6caf0bfa7e --- /dev/null +++ b/automotive/audiocontrol/2.0/default/CloseHandle.h @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2020 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include +#include + +namespace android::hardware::automotive::audiocontrol::V2_0::implementation { + +/** Generic ICloseHandle implementation ignoring double-close events. */ +class CloseHandle : public ICloseHandle { + public: + using Callback = std::function; + + /** + * Create a handle with a callback. + * + * The callback is guaranteed to be called exactly once. + * + * \param callback Called on the first close() call, or on destruction of the handle + */ + CloseHandle(Callback callback = nullptr); + virtual ~CloseHandle(); + + Return close() override; + + private: + const Callback mCallback; + std::atomic mIsClosed = false; + + DISALLOW_COPY_AND_ASSIGN(CloseHandle); +}; + +} // namespace android::hardware::automotive::audiocontrol::V2_0::implementation diff --git a/automotive/audiocontrol/2.0/default/android.hardware.automotive.audiocontrol@2.0-service.rc b/automotive/audiocontrol/2.0/default/android.hardware.automotive.audiocontrol@2.0-service.rc new file mode 100644 index 0000000000..81c9be4460 --- /dev/null +++ b/automotive/audiocontrol/2.0/default/android.hardware.automotive.audiocontrol@2.0-service.rc @@ -0,0 +1,4 @@ +service vendor.audiocontrol-hal-2.0 /vendor/bin/hw/android.hardware.automotive.audiocontrol@2.0-service + class hal + user audioserver + group system diff --git a/automotive/audiocontrol/2.0/default/audiocontrol2_manifest.xml b/automotive/audiocontrol/2.0/default/audiocontrol2_manifest.xml new file mode 100644 index 0000000000..42d23ed62e --- /dev/null +++ b/automotive/audiocontrol/2.0/default/audiocontrol2_manifest.xml @@ -0,0 +1,11 @@ + + + android.hardware.automotive.audiocontrol + hwbinder + 2.0 + + IAudioControl + default + + + \ No newline at end of file diff --git a/automotive/audiocontrol/2.0/default/service.cpp b/automotive/audiocontrol/2.0/default/service.cpp new file mode 100644 index 0000000000..dcc46c31aa --- /dev/null +++ b/automotive/audiocontrol/2.0/default/service.cpp @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2020 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include + +#include +#include +#include +#include + +#include "AudioControl.h" + +// libhidl: +using android::hardware::configureRpcThreadpool; +using android::hardware::joinRpcThreadpool; + +// Generated HIDL files +using android::hardware::automotive::audiocontrol::V2_0::IAudioControl; + +// The namespace in which all our implementation code lives +using namespace android::hardware::automotive::audiocontrol::V2_0::implementation; +using namespace android; + +// Main service entry point +int main() { + // Create an instance of our service class + android::sp service = new AudioControl(); + configureRpcThreadpool(1, true /*callerWillJoin*/); + + if (service->registerAsService() != OK) { + LOG(ERROR) << "registerAsService failed"; + return 1; + } + + // Join (forever) the thread pool we created for the service above + joinRpcThreadpool(); + + // We don't ever actually expect to return, so return an error if we do get here + return 2; +} \ No newline at end of file diff --git a/automotive/audiocontrol/2.0/types.hal b/automotive/audiocontrol/2.0/types.hal new file mode 100644 index 0000000000..65b0988487 --- /dev/null +++ b/automotive/audiocontrol/2.0/types.hal @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2020 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.hardware.automotive.audiocontrol@2.0; + +/** + * Changes in audio focus that can be experienced + */ +enum AudioFocusChange : uint32_t { + NONE = 0, + GAIN = 1, + GAIN_TRANSIENT = 2, + GAIN_TRANSIENT_MAY_DUCK = 3, + GAIN_TRANSIENT_EXCLUSIVE = 4, + LOSS = -1 * GAIN, + LOSS_TRANSIENT = -1 * GAIN_TRANSIENT, + LOSS_TRANSIENT_CAN_DUCK = -1 * GAIN_TRANSIENT_MAY_DUCK, +}; diff --git a/automotive/audiocontrol/2.0/vts/functional/Android.bp b/automotive/audiocontrol/2.0/vts/functional/Android.bp new file mode 100644 index 0000000000..520b042e37 --- /dev/null +++ b/automotive/audiocontrol/2.0/vts/functional/Android.bp @@ -0,0 +1,29 @@ +// +// Copyright (C) 2020 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +cc_test { + name: "VtsHalAudioControlV2_0TargetTest", + defaults: ["VtsHalTargetTestDefaults"], + srcs: ["VtsHalAudioControlV2_0TargetTest.cpp"], + static_libs: [ + "android.hardware.automotive.audiocontrol@2.0", + "libgmock", + ], + test_suites: [ + "general-tests", + "vts-core", + ], +} diff --git a/automotive/audiocontrol/2.0/vts/functional/VtsHalAudioControlV2_0TargetTest.cpp b/automotive/audiocontrol/2.0/vts/functional/VtsHalAudioControlV2_0TargetTest.cpp new file mode 100644 index 0000000000..0c106647c5 --- /dev/null +++ b/automotive/audiocontrol/2.0/vts/functional/VtsHalAudioControlV2_0TargetTest.cpp @@ -0,0 +1,155 @@ +/* + * Copyright (C) 2020 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define LOG_TAG "VtsHalAudioControlTest" + +#include +#include +#include + +#include +#include + +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +using namespace ::android::hardware::automotive::audiocontrol::V2_0; +using ::android::sp; +using ::android::hardware::hidl_bitfield; +using ::android::hardware::hidl_enum_range; +using ::android::hardware::hidl_handle; +using ::android::hardware::hidl_string; +using ::android::hardware::hidl_vec; +using ::android::hardware::Return; +using ::android::hardware::Void; +using ::android::hardware::audio::common::V6_0::AudioUsage; + +// The main test class for the automotive AudioControl HAL +class CarAudioControlHidlTest : public testing::TestWithParam { + public: + virtual void SetUp() override { + // Make sure we can connect to the driver + pAudioControl = IAudioControl::getService(GetParam()); + ASSERT_NE(pAudioControl.get(), nullptr); + } + + virtual void TearDown() override {} + + protected: + sp pAudioControl; // Every test needs access to the service +}; + +// +// Tests start here... +// + +/* + * Fader exercise test. Note that only a subjective observer could determine if the + * fader actually works. The only thing we can do is exercise the HAL and if the HAL crashes, + * we _might_ get a test failure if that breaks the connection to the driver. + */ +TEST_P(CarAudioControlHidlTest, FaderExercise) { + ALOGI("Fader exercise test (silent)"); + + // Set the fader all the way to the back + pAudioControl->setFadeTowardFront(-1.0f); + + // Set the fader all the way to the front + pAudioControl->setFadeTowardFront(1.0f); + + // Set the fader part way toward the back + pAudioControl->setFadeTowardFront(-0.333f); + + // Set the fader to a out of bounds value (driver should clamp) + pAudioControl->setFadeTowardFront(99999.9f); + + // Set the fader back to the middle + pAudioControl->setFadeTowardFront(0.0f); +} + +/* + * Balance exercise test. + */ +TEST_P(CarAudioControlHidlTest, BalanceExercise) { + ALOGI("Balance exercise test (silent)"); + + // Set the balance all the way to the left + pAudioControl->setBalanceTowardRight(-1.0f); + + // Set the balance all the way to the right + pAudioControl->setBalanceTowardRight(1.0f); + + // Set the balance part way toward the left + pAudioControl->setBalanceTowardRight(-0.333f); + + // Set the balance to a out of bounds value (driver should clamp) + pAudioControl->setBalanceTowardRight(99999.9f); + + // Set the balance back to the middle + pAudioControl->setBalanceTowardRight(0.0f); +} + +struct FocusListenerMock : public IFocusListener { + MOCK_METHOD(Return, requestAudioFocus, + (hidl_bitfield usage, int zoneId, + hidl_bitfield focusGain)); + MOCK_METHOD(Return, abandonAudioFocus, (hidl_bitfield usage, int zoneId)); +}; + +/* + * Test focus listener registration. + * + * Verifies that: + * - registerFocusListener succeeds; + * - registering a second listener succeeds in replacing the first; + * - closing handle does not crash; + */ +TEST_P(CarAudioControlHidlTest, FocusListenerRegistration) { + ALOGI("Focus listener test"); + + sp listener = new FocusListenerMock(); + + auto hidlResult = pAudioControl->registerFocusListener(listener); + ASSERT_TRUE(hidlResult.isOk()); + + sp listener2 = new FocusListenerMock(); + + auto hidlResult2 = pAudioControl->registerFocusListener(listener2); + ASSERT_TRUE(hidlResult2.isOk()); + + const sp& closeHandle = hidlResult2; + closeHandle->close(); +}; + +TEST_P(CarAudioControlHidlTest, FocusChangeExercise) { + ALOGI("Focus Change test"); + + pAudioControl->onAudioFocusChange(AudioUsage::MEDIA | 0, 0, + AudioFocusChange::GAIN_TRANSIENT | 0); +}; + +INSTANTIATE_TEST_SUITE_P( + PerInstance, CarAudioControlHidlTest, + testing::ValuesIn(android::hardware::getAllHalInstanceNames(IAudioControl::descriptor)), + android::hardware::PrintInstanceNameToString); \ No newline at end of file