Marshal Metrics

Because DRM Plugins will not be allowed to call the AMI API under
Treble, the mediadrmservice will need to marshal the metrics out of the
plugin in protobuf format and report them to the AMI API instead. This
patch implements the pulling and interpreting of metrics from DRM
Plugins.

Bug: 36497276
Test: Played Google Play Movies, verified that playback still worked and
      Widevine metrics appeared in a dump of the system media metrics.
Change-Id: If07717c1b87022bc1fcdedfbc62b9193899742d5
diff --git a/drm/libmediadrm/DrmHal.cpp b/drm/libmediadrm/DrmHal.cpp
index 6d54fa5..3150e3c 100644
--- a/drm/libmediadrm/DrmHal.cpp
+++ b/drm/libmediadrm/DrmHal.cpp
@@ -30,6 +30,7 @@
 #include <media/DrmHal.h>
 #include <media/DrmSessionClientInterface.h>
 #include <media/DrmSessionManager.h>
+#include <media/PluginMetricsReporting.h>
 #include <media/drm/DrmAPI.h>
 #include <media/stagefright/foundation/ADebug.h>
 #include <media/stagefright/foundation/AString.h>
@@ -421,6 +422,7 @@
     }
 
     closeOpenSessions();
+    reportMetrics();
     setListener(NULL);
     if (mPlugin != NULL) {
         mPlugin->setListener(NULL);
@@ -494,6 +496,7 @@
             }
         }
     }
+    reportMetrics();
     return toStatusT(status);
 }
 
@@ -745,6 +748,12 @@
 
 status_t DrmHal::getPropertyString(String8 const &name, String8 &value ) const {
     Mutex::Autolock autoLock(mLock);
+    return getPropertyStringInternal(name, value);
+}
+
+status_t DrmHal::getPropertyStringInternal(String8 const &name, String8 &value) const {
+    // This function is internal to the class and should only be called while
+    // mLock is already held.
 
     if (mInitCheck != OK) {
         return mInitCheck;
@@ -766,6 +775,12 @@
 
 status_t DrmHal::getPropertyByteArray(String8 const &name, Vector<uint8_t> &value ) const {
     Mutex::Autolock autoLock(mLock);
+    return getPropertyByteArrayInternal(name, value);
+}
+
+status_t DrmHal::getPropertyByteArrayInternal(String8 const &name, Vector<uint8_t> &value ) const {
+    // This function is internal to the class and should only be called while
+    // mLock is already held.
 
     if (mInitCheck != OK) {
         return mInitCheck;
@@ -999,4 +1014,20 @@
     }
 }
 
+void DrmHal::reportMetrics() const
+{
+    Vector<uint8_t> metrics;
+    String8 vendor;
+    String8 description;
+    if (getPropertyStringInternal(String8("vendor"), vendor) == OK &&
+            getPropertyStringInternal(String8("description"), description) == OK &&
+            getPropertyByteArrayInternal(String8("metrics"), metrics) == OK) {
+        status_t res = android::reportDrmPluginMetrics(
+                metrics, vendor, description);
+        if (res != OK) {
+            ALOGE("Metrics were retrieved but could not be reported: %i", res);
+        }
+    }
+}
+
 }  // namespace android