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/Android.bp b/drm/libmediadrm/Android.bp
index 1dd5139..f906564 100644
--- a/drm/libmediadrm/Android.bp
+++ b/drm/libmediadrm/Android.bp
@@ -13,16 +13,23 @@
         "IDrm.cpp",
         "IDrmClient.cpp",
         "IMediaDrmService.cpp",
+        "PluginMetricsReporting.cpp",
         "SharedLibrary.cpp",
         "DrmHal.cpp",
         "CryptoHal.cpp",
+        "protos/plugin_metrics.proto",
     ],
 
+    proto: {
+        type: "lite",
+    },
+
     shared_libs: [
         "libbinder",
         "libcutils",
         "libdl",
         "liblog",
+        "libmediametrics",
         "libmediautils",
         "libstagefright_foundation",
         "libutils",
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
diff --git a/drm/libmediadrm/PluginMetricsReporting.cpp b/drm/libmediadrm/PluginMetricsReporting.cpp
new file mode 100644
index 0000000..a9302ea
--- /dev/null
+++ b/drm/libmediadrm/PluginMetricsReporting.cpp
@@ -0,0 +1,122 @@
+/*
+ * 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_NDEBUG 0
+#define LOG_TAG "PluginMetricsReporting"
+#include <utils/Log.h>
+
+#include <media/PluginMetricsReporting.h>
+
+#include <media/MediaAnalyticsItem.h>
+
+#include "protos/plugin_metrics.pb.h"
+
+namespace android {
+
+namespace {
+
+using android::drm_metrics::MetricsGroup;
+using android::drm_metrics::MetricsGroup_Metric;
+using android::drm_metrics::MetricsGroup_Metric_MetricValue;
+
+const char* const kParentAttribute = "/parent/external";
+
+status_t reportMetricsGroup(const MetricsGroup& metricsGroup,
+                            const String8& batchName,
+                            const int64_t* parentId) {
+    MediaAnalyticsItem analyticsItem(batchName.c_str());
+    analyticsItem.generateSessionID();
+    int64_t sessionId = analyticsItem.getSessionID();
+    if (parentId != NULL) {
+        analyticsItem.setInt64(kParentAttribute, *parentId);
+    }
+
+    for (int i = 0; i < metricsGroup.metric_size(); ++i) {
+        const MetricsGroup_Metric& metric = metricsGroup.metric(i);
+        if (!metric.has_name()) {
+            ALOGE("Metric with no name.");
+            return BAD_VALUE;
+        }
+
+        if (!metric.has_value()) {
+            ALOGE("Metric with no value.");
+            return BAD_VALUE;
+        }
+
+        const MetricsGroup_Metric_MetricValue& value = metric.value();
+        if (value.has_int_value()) {
+            analyticsItem.setInt64(metric.name().c_str(),
+                                   value.int_value());
+        } else if (value.has_double_value()) {
+            analyticsItem.setDouble(metric.name().c_str(),
+                                    value.double_value());
+        } else if (value.has_string_value()) {
+            analyticsItem.setCString(metric.name().c_str(),
+                                     value.string_value().c_str());
+        } else {
+            ALOGE("Metric Value with no actual value.");
+            return BAD_VALUE;
+        }
+    }
+
+    analyticsItem.setFinalized(true);
+    analyticsItem.selfrecord();
+
+    for (int i = 0; i < metricsGroup.metric_sub_group_size(); ++i) {
+        const MetricsGroup& subGroup = metricsGroup.metric_sub_group(i);
+        status_t res = reportMetricsGroup(subGroup, batchName, &sessionId);
+        if (res != OK) {
+            return res;
+        }
+    }
+
+    return OK;
+}
+
+String8 sanitize(const String8& input) {
+    // Filters the input string down to just alphanumeric characters.
+    String8 output;
+    for (size_t i = 0; i < input.size(); ++i) {
+        char candidate = input[i];
+        if ((candidate >= 'a' && candidate <= 'z') ||
+                (candidate >= 'A' && candidate <= 'Z') ||
+                (candidate >= '0' && candidate <= '9')) {
+            output.append(&candidate, 1);
+        }
+    }
+    return output;
+}
+
+}  // namespace
+
+status_t reportDrmPluginMetrics(const Vector<uint8_t>& serializedMetrics,
+                                const String8& vendor,
+                                const String8& description) {
+    MetricsGroup root_metrics_group;
+    if (!root_metrics_group.ParseFromArray(serializedMetrics.array(),
+                                           serializedMetrics.size())) {
+        ALOGE("Failure to parse.");
+        return BAD_VALUE;
+    }
+
+    String8 name = String8::format("drm.vendor.%s.%s",
+                                   sanitize(vendor).c_str(),
+                                   sanitize(description).c_str());
+
+    return reportMetricsGroup(root_metrics_group, name, NULL);
+}
+
+}  // namespace android
diff --git a/drm/libmediadrm/protos/plugin_metrics.proto b/drm/libmediadrm/protos/plugin_metrics.proto
new file mode 100644
index 0000000..2d26f14
--- /dev/null
+++ b/drm/libmediadrm/protos/plugin_metrics.proto
@@ -0,0 +1,47 @@
+/*
+ * 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.
+ */
+
+syntax = "proto2";
+
+package android.drm_metrics;
+
+// need this if we are using libprotobuf-cpp-2.3.0-lite
+option optimize_for = LITE_RUNTIME;
+
+// The MetricsGroup is a collection of metric name/value pair instances
+// that can be serialized and provided to a caller.
+message MetricsGroup {
+  message Metric {
+    message MetricValue {
+      // Exactly one of the following values must be set.
+      optional int64 int_value = 1;
+      optional double double_value = 2;
+      optional string string_value = 3;
+    }
+
+    // The name of the metric. Must be valid UTF-8. Required.
+    optional string name = 1;
+
+    // The value of the metric. Required.
+    optional MetricValue value = 2;
+  }
+
+  // The list of name/value pairs of metrics.
+  repeated Metric metric = 1;
+
+  // Allow multiple sub groups of metrics.
+  repeated MetricsGroup metric_sub_group = 2;
+}
diff --git a/include/media/PluginMetricsReporting.h b/include/media/PluginMetricsReporting.h
new file mode 120000
index 0000000..7d9a7a0
--- /dev/null
+++ b/include/media/PluginMetricsReporting.h
@@ -0,0 +1 @@
+../../media/libmedia/include/media/PluginMetricsReporting.h
\ No newline at end of file
diff --git a/media/libmedia/include/media/DrmHal.h b/media/libmedia/include/media/DrmHal.h
index 991ccc8..5d25e4d 100644
--- a/media/libmedia/include/media/DrmHal.h
+++ b/media/libmedia/include/media/DrmHal.h
@@ -183,6 +183,11 @@
 
     void writeByteArray(Parcel &obj, const hidl_vec<uint8_t>& array);
 
+    void reportMetrics() const;
+    status_t getPropertyStringInternal(String8 const &name, String8 &value) const;
+    status_t getPropertyByteArrayInternal(String8 const &name,
+                                          Vector<uint8_t> &value) const;
+
     DISALLOW_EVIL_CONSTRUCTORS(DrmHal);
 };
 
diff --git a/media/libmedia/include/media/PluginMetricsReporting.h b/media/libmedia/include/media/PluginMetricsReporting.h
new file mode 100644
index 0000000..4a5a363
--- /dev/null
+++ b/media/libmedia/include/media/PluginMetricsReporting.h
@@ -0,0 +1,33 @@
+/*
+ * 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 PLUGIN_METRICS_REPORTING_H_
+
+#define PLUGIN_METRICS_REPORTING_H_
+
+#include <utils/Errors.h>
+#include <utils/String8.h>
+#include <utils/Vector.h>
+
+namespace android {
+
+status_t reportDrmPluginMetrics(const Vector<uint8_t>& serializedMetrics,
+                                const String8& vendorName,
+                                const String8& description);
+
+}  // namespace android
+
+#endif  // PLUGIN_METRICS_REPORTING_H_