Monitor crashes of audio hal service via linkToDeath
Hwbinder 'linkToDeath' mechanism is used to track abrupt
disappearance of server hal objects due to hal server crash.
Upon hal server crash, notifications can be sent to binder
clients if needed, then the audioserver crashes itself as the
current audio HAL design does not provide an easy way to restore
all the transient objects (devices, streams, effects) in their
pre-crash state.
Bug: 34158778
Test: In binderized mode, kill audio hal server
Change-Id: If9265711edc445984601fcb5c29d2c173a370762
diff --git a/include/media/audiohal/hidl/HalDeathHandler.h b/include/media/audiohal/hidl/HalDeathHandler.h
new file mode 100644
index 0000000..c9b7084
--- /dev/null
+++ b/include/media/audiohal/hidl/HalDeathHandler.h
@@ -0,0 +1,62 @@
+/*
+ * Copyright (C) 2017 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_HIDL_HAL_DEATH_HANDLER_H
+#define ANDROID_HARDWARE_HIDL_HAL_DEATH_HANDLER_H
+
+#include <functional>
+#include <mutex>
+#include <unordered_map>
+
+#include <hidl/HidlSupport.h>
+#include <utils/Singleton.h>
+
+using android::hardware::hidl_death_recipient;
+using android::hidl::base::V1_0::IBase;
+
+namespace android {
+
+class HalDeathHandler : public hidl_death_recipient, private Singleton<HalDeathHandler> {
+ public:
+ typedef std::function<void()> AtExitHandler;
+
+ // Note that the exit handler gets called using a thread from
+ // RPC threadpool, thus it needs to be thread-safe.
+ void registerAtExitHandler(void* cookie, AtExitHandler handler);
+ void unregisterAtExitHandler(void* cookie);
+
+ // hidl_death_recipient
+ virtual void serviceDied(uint64_t cookie, const wp<IBase>& who);
+
+ // Used both for (un)registering handlers, and for passing to
+ // '(un)linkToDeath'.
+ static sp<HalDeathHandler> getInstance();
+
+ private:
+ friend class Singleton<HalDeathHandler>;
+ typedef std::unordered_map<void*, AtExitHandler> Handlers;
+
+ HalDeathHandler();
+ virtual ~HalDeathHandler();
+
+ sp<HalDeathHandler> mSelf; // Allows the singleton instance to live forever.
+ std::mutex mHandlersLock;
+ Handlers mHandlers;
+};
+
+} // namespace android
+
+#endif // ANDROID_HARDWARE_HIDL_HAL_DEATH_HANDLER_H
diff --git a/media/libaudiohal/Android.mk b/media/libaudiohal/Android.mk
index 58b38a6..5e00b77 100644
--- a/media/libaudiohal/Android.mk
+++ b/media/libaudiohal/Android.mk
@@ -26,6 +26,7 @@
LOCAL_SRC_FILES := \
ConversionHelperHidl.cpp \
+ HalDeathHandlerHidl.cpp \
DeviceHalHidl.cpp \
DevicesFactoryHalHidl.cpp \
EffectBufferHalHidl.cpp \
diff --git a/media/libaudiohal/ConversionHelperHidl.h b/media/libaudiohal/ConversionHelperHidl.h
index 00d5b2c..23fb360 100644
--- a/media/libaudiohal/ConversionHelperHidl.h
+++ b/media/libaudiohal/ConversionHelperHidl.h
@@ -52,7 +52,7 @@
if (!ret.isOk()) {
emitError(funcName, ret.description().c_str());
}
- return ret.isOk() ? OK : UNKNOWN_ERROR;
+ return ret.isOk() ? OK : FAILED_TRANSACTION;
}
status_t processReturn(const char* funcName, const Return<hardware::audio::V2_0::Result>& ret) {
@@ -62,7 +62,7 @@
template<typename T>
status_t processReturn(
const char* funcName, const Return<T>& ret, hardware::audio::V2_0::Result retval) {
- const status_t st = ret.isOk() ? analyzeResult(retval) : UNKNOWN_ERROR;
+ const status_t st = ret.isOk() ? analyzeResult(retval) : FAILED_TRANSACTION;
if (!ret.isOk()) {
emitError(funcName, ret.description().c_str());
}
diff --git a/media/libaudiohal/DevicesFactoryHalHidl.cpp b/media/libaudiohal/DevicesFactoryHalHidl.cpp
index 6444079..a91f145 100644
--- a/media/libaudiohal/DevicesFactoryHalHidl.cpp
+++ b/media/libaudiohal/DevicesFactoryHalHidl.cpp
@@ -20,6 +20,7 @@
//#define LOG_NDEBUG 0
#include <android/hardware/audio/2.0/IDevice.h>
+#include <media/audiohal/hidl/HalDeathHandler.h>
#include <utils/Log.h>
#include "ConversionHelperHidl.h"
@@ -40,6 +41,11 @@
DevicesFactoryHalHidl::DevicesFactoryHalHidl() {
mDevicesFactory = IDevicesFactory::getService("audio_devices_factory");
+ if (mDevicesFactory != 0) {
+ // It is assumet that DevicesFactory is owned by AudioFlinger
+ // and thus have the same lifespan.
+ mDevicesFactory->linkToDeath(HalDeathHandler::getInstance(), 0 /*cookie*/);
+ }
}
DevicesFactoryHalHidl::~DevicesFactoryHalHidl() {
@@ -83,7 +89,7 @@
else if (retval == Result::INVALID_ARGUMENTS) return BAD_VALUE;
else return NO_INIT;
}
- return UNKNOWN_ERROR;
+ return FAILED_TRANSACTION;
}
} // namespace android
diff --git a/media/libaudiohal/EffectHalHidl.cpp b/media/libaudiohal/EffectHalHidl.cpp
index 3fb2f43..a11719a 100644
--- a/media/libaudiohal/EffectHalHidl.cpp
+++ b/media/libaudiohal/EffectHalHidl.cpp
@@ -160,7 +160,7 @@
mBuffersChanged = false;
return OK;
}
- return ret.isOk() ? analyzeResult(ret) : UNKNOWN_ERROR;
+ return ret.isOk() ? analyzeResult(ret) : FAILED_TRANSACTION;
}
status_t EffectHalHidl::command(uint32_t cmdCode, uint32_t cmdSize, void *pCmdData,
@@ -181,7 +181,7 @@
}
}
});
- return status;
+ return ret.isOk() ? status : FAILED_TRANSACTION;
}
status_t EffectHalHidl::getDescriptor(effect_descriptor_t *pDescriptor) {
@@ -194,13 +194,13 @@
effectDescriptorToHal(result, pDescriptor);
}
});
- return ret.isOk() ? analyzeResult(retval) : UNKNOWN_ERROR;
+ return ret.isOk() ? analyzeResult(retval) : FAILED_TRANSACTION;
}
status_t EffectHalHidl::close() {
if (mEffect == 0) return NO_INIT;
Return<Result> ret = mEffect->close();
- return ret.isOk() ? analyzeResult(ret) : UNKNOWN_ERROR;
+ return ret.isOk() ? analyzeResult(ret) : FAILED_TRANSACTION;
}
} // namespace android
diff --git a/media/libaudiohal/HalDeathHandlerHidl.cpp b/media/libaudiohal/HalDeathHandlerHidl.cpp
new file mode 100644
index 0000000..a742671
--- /dev/null
+++ b/media/libaudiohal/HalDeathHandlerHidl.cpp
@@ -0,0 +1,59 @@
+/*
+ * Copyright (C) 2017 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 "HalDeathHandler"
+//#define LOG_NDEBUG 0
+
+#include <utils/Log.h>
+
+#include <media/audiohal/hidl/HalDeathHandler.h>
+
+namespace android {
+
+ANDROID_SINGLETON_STATIC_INSTANCE(HalDeathHandler);
+
+// static
+sp<HalDeathHandler> HalDeathHandler::getInstance() {
+ return &Singleton<HalDeathHandler>::getInstance();
+}
+
+HalDeathHandler::HalDeathHandler() : mSelf(this) {
+}
+
+HalDeathHandler::~HalDeathHandler() {
+}
+
+void HalDeathHandler::registerAtExitHandler(void* cookie, AtExitHandler handler) {
+ std::lock_guard<std::mutex> guard(mHandlersLock);
+ mHandlers.insert({cookie, handler});
+}
+
+void HalDeathHandler::unregisterAtExitHandler(void* cookie) {
+ std::lock_guard<std::mutex> guard(mHandlersLock);
+ mHandlers.erase(cookie);
+}
+
+void HalDeathHandler::serviceDied(uint64_t /*cookie*/, const wp<IBase>& /*who*/) {
+ // No matter which of the service objects has died,
+ // we need to run all the registered handlers and crash our process.
+ std::lock_guard<std::mutex> guard(mHandlersLock);
+ for (const auto& handler : mHandlers) {
+ handler.second();
+ }
+ LOG_ALWAYS_FATAL("HAL server crashed, need to restart");
+}
+
+} // namespace android
diff --git a/services/radio/Android.mk b/services/radio/Android.mk
index 74f1fe0..1b50dc3 100644
--- a/services/radio/Android.mk
+++ b/services/radio/Android.mk
@@ -46,6 +46,7 @@
libhidlbase \
libhidltransport \
libbase \
+ libaudiohal \
android.hardware.broadcastradio@1.0
endif
diff --git a/services/radio/RadioHalHidl.cpp b/services/radio/RadioHalHidl.cpp
index 34a6db7..032d3fd 100644
--- a/services/radio/RadioHalHidl.cpp
+++ b/services/radio/RadioHalHidl.cpp
@@ -17,6 +17,7 @@
#define LOG_TAG "RadioHalHidl"
//#define LOG_NDEBUG 0
+#include <media/audiohal/hidl/HalDeathHandler.h>
#include <utils/Log.h>
#include <utils/misc.h>
#include <system/radio_metadata.h>
@@ -333,11 +334,27 @@
RadioHalHidl::Tuner::Tuner(sp<TunerCallbackInterface> callback, sp<RadioHalHidl> module)
: TunerInterface(), mHalTuner(NULL), mCallback(callback), mParentModule(module)
{
+ // Make sure the handler we are passing in only deals with const members,
+ // as it can be called on an arbitrary thread.
+ const auto& self = this;
+ HalDeathHandler::getInstance()->registerAtExitHandler(
+ this, [&self]() { self->sendHwFailureEvent(); });
}
RadioHalHidl::Tuner::~Tuner()
{
+ HalDeathHandler::getInstance()->unregisterAtExitHandler(this);
+}
+
+void RadioHalHidl::Tuner::setHalTuner(sp<ITuner>& halTuner) {
+ if (mHalTuner != 0) {
+ mHalTuner->unlinkToDeath(HalDeathHandler::getInstance());
+ }
+ mHalTuner = halTuner;
+ if (mHalTuner != 0) {
+ mHalTuner->linkToDeath(HalDeathHandler::getInstance(), 0 /*cookie*/);
+ }
}
void RadioHalHidl::Tuner::handleHwFailure()
@@ -347,14 +364,19 @@
if (parentModule != 0) {
parentModule->clearService();
}
+ sendHwFailureEvent();
+ mHalTuner.clear();
+}
+
+void RadioHalHidl::Tuner::sendHwFailureEvent() const
+{
radio_hal_event_t event;
memset(&event, 0, sizeof(radio_hal_event_t));
event.type = RADIO_EVENT_HW_FAILURE;
onCallback(&event);
- mHalTuner.clear();
}
-void RadioHalHidl::Tuner::onCallback(radio_hal_event_t *halEvent)
+void RadioHalHidl::Tuner::onCallback(radio_hal_event_t *halEvent) const
{
if (mCallback != 0) {
mCallback->onEvent(halEvent);
diff --git a/services/radio/RadioHalHidl.h b/services/radio/RadioHalHidl.h
index b60a95e..38e181a 100644
--- a/services/radio/RadioHalHidl.h
+++ b/services/radio/RadioHalHidl.h
@@ -78,17 +78,18 @@
virtual Return<void> newMetadata(uint32_t channel, uint32_t subChannel,
const ::android::hardware::hidl_vec<MetaData>& metadata);
- void setHalTuner(sp<ITuner>& halTuner) { mHalTuner = halTuner; }
+ void setHalTuner(sp<ITuner>& halTuner);
sp<ITuner> getHalTuner() { return mHalTuner; }
private:
virtual ~Tuner();
- void onCallback(radio_hal_event_t *halEvent);
+ void onCallback(radio_hal_event_t *halEvent) const;
void handleHwFailure();
+ void sendHwFailureEvent() const;
sp<ITuner> mHalTuner;
- sp<TunerCallbackInterface> mCallback;
+ const sp<TunerCallbackInterface> mCallback;
wp<RadioHalHidl> mParentModule;
};
diff --git a/services/soundtrigger/Android.mk b/services/soundtrigger/Android.mk
index 63a05a6..3e7a7ce 100644
--- a/services/soundtrigger/Android.mk
+++ b/services/soundtrigger/Android.mk
@@ -48,6 +48,7 @@
libhidlbase \
libhidltransport \
libbase \
+ libaudiohal \
android.hardware.soundtrigger@2.0 \
android.hardware.audio.common@2.0
endif
diff --git a/services/soundtrigger/SoundTriggerHalHidl.cpp b/services/soundtrigger/SoundTriggerHalHidl.cpp
index eb9d38d..7cc8a2b 100644
--- a/services/soundtrigger/SoundTriggerHalHidl.cpp
+++ b/services/soundtrigger/SoundTriggerHalHidl.cpp
@@ -17,6 +17,7 @@
#define LOG_TAG "SoundTriggerHalHidl"
//#define LOG_NDEBUG 0
+#include <media/audiohal/hidl/HalDeathHandler.h>
#include <utils/Log.h>
#include "SoundTriggerHalHidl.h"
#include <hwbinder/IPCThreadState.h>
@@ -59,7 +60,7 @@
}
} else {
ALOGE("getProperties error %s", hidlReturn.description().c_str());
- return UNKNOWN_ERROR;
+ return FAILED_TRANSACTION;
}
ALOGI("getProperties ret %d", ret);
return ret;
@@ -132,7 +133,7 @@
}
} else {
ALOGE("loadSoundModel error %s", hidlReturn.description().c_str());
- return UNKNOWN_ERROR;
+ return FAILED_TRANSACTION;
}
return ret;
@@ -159,7 +160,7 @@
if (!hidlReturn.isOk()) {
ALOGE("unloadSoundModel error %s", hidlReturn.description().c_str());
- return UNKNOWN_ERROR;
+ return FAILED_TRANSACTION;
}
return hidlReturn;
@@ -197,7 +198,7 @@
if (!hidlReturn.isOk()) {
ALOGE("startRecognition error %s", hidlReturn.description().c_str());
- return UNKNOWN_ERROR;
+ return FAILED_TRANSACTION;
}
return hidlReturn;
}
@@ -223,7 +224,7 @@
if (!hidlReturn.isOk()) {
ALOGE("stopRecognition error %s", hidlReturn.description().c_str());
- return UNKNOWN_ERROR;
+ return FAILED_TRANSACTION;
}
return hidlReturn;
}
@@ -243,7 +244,7 @@
if (!hidlReturn.isOk()) {
ALOGE("stopAllRecognitions error %s", hidlReturn.description().c_str());
- return UNKNOWN_ERROR;
+ return FAILED_TRANSACTION;
}
return hidlReturn;
}
@@ -267,6 +268,9 @@
std::string serviceName = "sound_trigger.";
serviceName.append(mModuleName);
mISoundTrigger = ISoundTriggerHw::getService(serviceName);
+ if (mISoundTrigger != 0) {
+ mISoundTrigger->linkToDeath(HalDeathHandler::getInstance(), 0 /*cookie*/);
+ }
}
return mISoundTrigger;
}