Part of fix for TEE resource leaks when app crashes

bug: 9672606
Change-Id: I98cf65260d5e43d34e3897452f6f26ed2c89294e
diff --git a/media/libmediaplayerservice/Drm.cpp b/media/libmediaplayerservice/Drm.cpp
index 1e6cd94..f00f488 100644
--- a/media/libmediaplayerservice/Drm.cpp
+++ b/media/libmediaplayerservice/Drm.cpp
@@ -71,6 +71,12 @@
 status_t Drm::setListener(const sp<IDrmClient>& listener)
 {
     Mutex::Autolock lock(mEventLock);
+    if (mListener != NULL){
+        mListener->asBinder()->unlinkToDeath(this);
+    }
+    if (listener != NULL) {
+        listener->asBinder()->linkToDeath(this);
+    }
     mListener = listener;
     return NO_ERROR;
 }
@@ -576,4 +582,12 @@
     return mPlugin->verify(sessionId, keyId, message, signature, match);
 }
 
+void Drm::binderDied(const wp<IBinder> &the_late_who)
+{
+    delete mPlugin;
+    mPlugin = NULL;
+    closeFactory();
+    mListener.clear();
+}
+
 }  // namespace android
diff --git a/media/libmediaplayerservice/Drm.h b/media/libmediaplayerservice/Drm.h
index 3da8ad4..3f460f1 100644
--- a/media/libmediaplayerservice/Drm.h
+++ b/media/libmediaplayerservice/Drm.h
@@ -29,7 +29,9 @@
 struct DrmFactory;
 struct DrmPlugin;
 
-struct Drm : public BnDrm, public DrmPluginListener {
+struct Drm : public BnDrm,
+             public IBinder::DeathRecipient,
+             public DrmPluginListener {
     Drm();
     virtual ~Drm();
 
@@ -115,6 +117,8 @@
                            Vector<uint8_t> const *sessionId,
                            Vector<uint8_t> const *data);
 
+    virtual void binderDied(const wp<IBinder> &the_late_who);
+
 private:
     mutable Mutex mLock;