blob: a9302ea128d6a716494c1bcccc6e42a0389d5698 [file] [log] [blame]
John W. Bruce33ecc4f2017-04-03 16:49:05 -07001/*
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>
20
21#include <media/PluginMetricsReporting.h>
22
23#include <media/MediaAnalyticsItem.h>
24
25#include "protos/plugin_metrics.pb.h"
26
27namespace android {
28
29namespace {
30
31using android::drm_metrics::MetricsGroup;
32using android::drm_metrics::MetricsGroup_Metric;
33using android::drm_metrics::MetricsGroup_Metric_MetricValue;
34
35const char* const kParentAttribute = "/parent/external";
36
37status_t reportMetricsGroup(const MetricsGroup& metricsGroup,
38 const String8& batchName,
39 const int64_t* parentId) {
40 MediaAnalyticsItem analyticsItem(batchName.c_str());
41 analyticsItem.generateSessionID();
42 int64_t sessionId = analyticsItem.getSessionID();
43 if (parentId != NULL) {
44 analyticsItem.setInt64(kParentAttribute, *parentId);
45 }
46
47 for (int i = 0; i < metricsGroup.metric_size(); ++i) {
48 const MetricsGroup_Metric& metric = metricsGroup.metric(i);
49 if (!metric.has_name()) {
50 ALOGE("Metric with no name.");
51 return BAD_VALUE;
52 }
53
54 if (!metric.has_value()) {
55 ALOGE("Metric with no value.");
56 return BAD_VALUE;
57 }
58
59 const MetricsGroup_Metric_MetricValue& value = metric.value();
60 if (value.has_int_value()) {
61 analyticsItem.setInt64(metric.name().c_str(),
62 value.int_value());
63 } else if (value.has_double_value()) {
64 analyticsItem.setDouble(metric.name().c_str(),
65 value.double_value());
66 } else if (value.has_string_value()) {
67 analyticsItem.setCString(metric.name().c_str(),
68 value.string_value().c_str());
69 } else {
70 ALOGE("Metric Value with no actual value.");
71 return BAD_VALUE;
72 }
73 }
74
75 analyticsItem.setFinalized(true);
76 analyticsItem.selfrecord();
77
78 for (int i = 0; i < metricsGroup.metric_sub_group_size(); ++i) {
79 const MetricsGroup& subGroup = metricsGroup.metric_sub_group(i);
80 status_t res = reportMetricsGroup(subGroup, batchName, &sessionId);
81 if (res != OK) {
82 return res;
83 }
84 }
85
86 return OK;
87}
88
89String8 sanitize(const String8& input) {
90 // Filters the input string down to just alphanumeric characters.
91 String8 output;
92 for (size_t i = 0; i < input.size(); ++i) {
93 char candidate = input[i];
94 if ((candidate >= 'a' && candidate <= 'z') ||
95 (candidate >= 'A' && candidate <= 'Z') ||
96 (candidate >= '0' && candidate <= '9')) {
97 output.append(&candidate, 1);
98 }
99 }
100 return output;
101}
102
103} // namespace
104
105status_t reportDrmPluginMetrics(const Vector<uint8_t>& serializedMetrics,
106 const String8& vendor,
107 const String8& description) {
108 MetricsGroup root_metrics_group;
109 if (!root_metrics_group.ParseFromArray(serializedMetrics.array(),
110 serializedMetrics.size())) {
111 ALOGE("Failure to parse.");
112 return BAD_VALUE;
113 }
114
115 String8 name = String8::format("drm.vendor.%s.%s",
116 sanitize(vendor).c_str(),
117 sanitize(description).c_str());
118
119 return reportMetricsGroup(root_metrics_group, name, NULL);
120}
121
122} // namespace android