John W. Bruce | 33ecc4f | 2017-04-03 16:49:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | //#define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "PluginMetricsReporting" |
| 19 | #include <utils/Log.h> |
Ray Essick | 3692d9d | 2018-01-09 15:55:44 -0800 | [diff] [blame] | 20 | #include <inttypes.h> |
John W. Bruce | 33ecc4f | 2017-04-03 16:49:05 -0700 | [diff] [blame] | 21 | |
| 22 | #include <media/PluginMetricsReporting.h> |
| 23 | |
| 24 | #include <media/MediaAnalyticsItem.h> |
| 25 | |
| 26 | #include "protos/plugin_metrics.pb.h" |
| 27 | |
| 28 | namespace android { |
| 29 | |
| 30 | namespace { |
| 31 | |
| 32 | using android::drm_metrics::MetricsGroup; |
| 33 | using android::drm_metrics::MetricsGroup_Metric; |
| 34 | using android::drm_metrics::MetricsGroup_Metric_MetricValue; |
| 35 | |
| 36 | const char* const kParentAttribute = "/parent/external"; |
| 37 | |
| 38 | status_t reportMetricsGroup(const MetricsGroup& metricsGroup, |
| 39 | const String8& batchName, |
| 40 | const int64_t* parentId) { |
| 41 | MediaAnalyticsItem analyticsItem(batchName.c_str()); |
| 42 | analyticsItem.generateSessionID(); |
| 43 | int64_t sessionId = analyticsItem.getSessionID(); |
| 44 | if (parentId != NULL) { |
| 45 | analyticsItem.setInt64(kParentAttribute, *parentId); |
| 46 | } |
| 47 | |
Adam Stone | 21c7212 | 2017-09-05 19:02:06 -0700 | [diff] [blame] | 48 | // Report the package name. |
| 49 | if (metricsGroup.has_app_package_name()) { |
Ray Essick | 783bd0d | 2018-01-11 11:10:35 -0800 | [diff] [blame] | 50 | std::string app_package_name(metricsGroup.app_package_name().c_str(), |
Adam Stone | 21c7212 | 2017-09-05 19:02:06 -0700 | [diff] [blame] | 51 | metricsGroup.app_package_name().size()); |
| 52 | analyticsItem.setPkgName(app_package_name); |
| 53 | } |
| 54 | |
John W. Bruce | 33ecc4f | 2017-04-03 16:49:05 -0700 | [diff] [blame] | 55 | for (int i = 0; i < metricsGroup.metric_size(); ++i) { |
| 56 | const MetricsGroup_Metric& metric = metricsGroup.metric(i); |
| 57 | if (!metric.has_name()) { |
| 58 | ALOGE("Metric with no name."); |
| 59 | return BAD_VALUE; |
| 60 | } |
| 61 | |
| 62 | if (!metric.has_value()) { |
| 63 | ALOGE("Metric with no value."); |
| 64 | return BAD_VALUE; |
| 65 | } |
| 66 | |
| 67 | const MetricsGroup_Metric_MetricValue& value = metric.value(); |
| 68 | if (value.has_int_value()) { |
| 69 | analyticsItem.setInt64(metric.name().c_str(), |
| 70 | value.int_value()); |
| 71 | } else if (value.has_double_value()) { |
| 72 | analyticsItem.setDouble(metric.name().c_str(), |
| 73 | value.double_value()); |
| 74 | } else if (value.has_string_value()) { |
| 75 | analyticsItem.setCString(metric.name().c_str(), |
| 76 | value.string_value().c_str()); |
| 77 | } else { |
| 78 | ALOGE("Metric Value with no actual value."); |
| 79 | return BAD_VALUE; |
| 80 | } |
| 81 | } |
| 82 | |
Adam Stone | 21c7212 | 2017-09-05 19:02:06 -0700 | [diff] [blame] | 83 | if (!analyticsItem.selfrecord()) { |
Ray Essick | 3692d9d | 2018-01-09 15:55:44 -0800 | [diff] [blame] | 84 | ALOGE("selfrecord() returned false. sessioId %" PRId64, sessionId); |
Adam Stone | 21c7212 | 2017-09-05 19:02:06 -0700 | [diff] [blame] | 85 | } |
John W. Bruce | 33ecc4f | 2017-04-03 16:49:05 -0700 | [diff] [blame] | 86 | |
| 87 | for (int i = 0; i < metricsGroup.metric_sub_group_size(); ++i) { |
| 88 | const MetricsGroup& subGroup = metricsGroup.metric_sub_group(i); |
| 89 | status_t res = reportMetricsGroup(subGroup, batchName, &sessionId); |
| 90 | if (res != OK) { |
| 91 | return res; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | return OK; |
| 96 | } |
| 97 | |
| 98 | String8 sanitize(const String8& input) { |
| 99 | // Filters the input string down to just alphanumeric characters. |
| 100 | String8 output; |
| 101 | for (size_t i = 0; i < input.size(); ++i) { |
| 102 | char candidate = input[i]; |
| 103 | if ((candidate >= 'a' && candidate <= 'z') || |
| 104 | (candidate >= 'A' && candidate <= 'Z') || |
| 105 | (candidate >= '0' && candidate <= '9')) { |
| 106 | output.append(&candidate, 1); |
| 107 | } |
| 108 | } |
| 109 | return output; |
| 110 | } |
| 111 | |
| 112 | } // namespace |
| 113 | |
| 114 | status_t reportDrmPluginMetrics(const Vector<uint8_t>& serializedMetrics, |
| 115 | const String8& vendor, |
| 116 | const String8& description) { |
| 117 | MetricsGroup root_metrics_group; |
| 118 | if (!root_metrics_group.ParseFromArray(serializedMetrics.array(), |
| 119 | serializedMetrics.size())) { |
| 120 | ALOGE("Failure to parse."); |
| 121 | return BAD_VALUE; |
| 122 | } |
| 123 | |
| 124 | String8 name = String8::format("drm.vendor.%s.%s", |
| 125 | sanitize(vendor).c_str(), |
| 126 | sanitize(description).c_str()); |
| 127 | |
| 128 | return reportMetricsGroup(root_metrics_group, name, NULL); |
| 129 | } |
| 130 | |
| 131 | } // namespace android |