blob: 098f07b2d317837ac344bd333175996565fc9ec1 [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"
John W. Bruce33ecc4f2017-04-03 16:49:05 -070019
20#include <media/PluginMetricsReporting.h>
21
Adam Stone32494f52018-02-26 22:53:27 -080022#include <inttypes.h>
John W. Bruce33ecc4f2017-04-03 16:49:05 -070023
Adam Stone32494f52018-02-26 22:53:27 -080024#include <media/MediaAnalyticsItem.h>
25#include <utils/Log.h>
26
John W. Bruce33ecc4f2017-04-03 16:49:05 -070027
28namespace android {
29
30namespace {
31
Adam Stone32494f52018-02-26 22:53:27 -080032constexpr char kSerializedMetricsField[] = "serialized_metrics";
John W. Bruce33ecc4f2017-04-03 16:49:05 -070033
Adam Stone32494f52018-02-26 22:53:27 -080034status_t reportVendorMetrics(const std::string& metrics,
35 const String8& name,
36 const String8& appPackageName) {
Ray Essick6a305222019-01-28 20:33:18 -080037 std::unique_ptr<MediaAnalyticsItem> analyticsItem(MediaAnalyticsItem::create(name.c_str()));
Adam Stone32494f52018-02-26 22:53:27 -080038 std::string app_package_name(appPackageName.c_str(), appPackageName.size());
Ray Essick6a305222019-01-28 20:33:18 -080039 analyticsItem->setPkgName(app_package_name);
Adam Stone32494f52018-02-26 22:53:27 -080040 if (metrics.size() > 0) {
Ray Essick6a305222019-01-28 20:33:18 -080041 analyticsItem->setCString(kSerializedMetricsField, metrics.c_str());
John W. Bruce33ecc4f2017-04-03 16:49:05 -070042 }
43
Ray Essick6a305222019-01-28 20:33:18 -080044 if (!analyticsItem->selfrecord()) {
Andy Hunga87e69c2019-10-18 10:07:40 -070045 ALOGE("%s: selfrecord() returned false", __func__);
John W. Bruce33ecc4f2017-04-03 16:49:05 -070046 }
47
48 return OK;
49}
50
51String8 sanitize(const String8& input) {
52 // Filters the input string down to just alphanumeric characters.
53 String8 output;
54 for (size_t i = 0; i < input.size(); ++i) {
55 char candidate = input[i];
56 if ((candidate >= 'a' && candidate <= 'z') ||
57 (candidate >= 'A' && candidate <= 'Z') ||
58 (candidate >= '0' && candidate <= '9')) {
59 output.append(&candidate, 1);
60 }
61 }
62 return output;
63}
64
65} // namespace
66
Adam Stone32494f52018-02-26 22:53:27 -080067status_t reportDrmPluginMetrics(const std::string& b64EncodedMetrics,
John W. Bruce33ecc4f2017-04-03 16:49:05 -070068 const String8& vendor,
Adam Stone32494f52018-02-26 22:53:27 -080069 const String8& description,
70 const String8& appPackageName) {
John W. Bruce33ecc4f2017-04-03 16:49:05 -070071
72 String8 name = String8::format("drm.vendor.%s.%s",
73 sanitize(vendor).c_str(),
74 sanitize(description).c_str());
75
Adam Stone32494f52018-02-26 22:53:27 -080076 return reportVendorMetrics(b64EncodedMetrics, name, appPackageName);
John W. Bruce33ecc4f2017-04-03 16:49:05 -070077}
78
79} // namespace android