blob: 071c549a71444b402d739a547373eb605a08c948 [file] [log] [blame]
Ray Essick6ce27e52019-02-15 10:58:05 -08001/*
2 * Copyright (C) 2019 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 "statsd_drm"
19#include <utils/Log.h>
Robert Shih2e15aed2021-03-16 18:30:35 -070020#include <media/stagefright/foundation/base64.h>
Ray Essick6ce27e52019-02-15 10:58:05 -080021
22#include <stdint.h>
23#include <inttypes.h>
24#include <sys/types.h>
25#include <sys/stat.h>
26#include <sys/time.h>
27#include <dirent.h>
28#include <pthread.h>
29#include <unistd.h>
30
31#include <string.h>
32#include <pwd.h>
33
Ray Essick40e8e5e2019-12-05 20:19:40 -080034#include "MediaMetricsService.h"
Ray Essick6ce27e52019-02-15 10:58:05 -080035#include "iface_statsd.h"
36
37#include <statslog.h>
38
Robert Shih5edf2452020-02-08 14:58:05 -080039#include <array>
40#include <string>
Robert Shih2e15aed2021-03-16 18:30:35 -070041#include <vector>
Robert Shih5edf2452020-02-08 14:58:05 -080042
Ray Essick6ce27e52019-02-15 10:58:05 -080043namespace android {
44
45// mediadrm
Ray Essickf27e9872019-12-07 06:28:46 -080046bool statsd_mediadrm(const mediametrics::Item *item)
Ray Essick6ce27e52019-02-15 10:58:05 -080047{
Andy Hung3ab1b322020-05-18 10:47:31 -070048 if (item == nullptr) return false;
Ray Essick6ce27e52019-02-15 10:58:05 -080049
Ray Essickf27e9872019-12-07 06:28:46 -080050 const nsecs_t timestamp = MediaMetricsService::roundTime(item->getTimestamp());
Ray Essick6ce27e52019-02-15 10:58:05 -080051 std::string pkgName = item->getPkgName();
52 int64_t pkgVersionCode = item->getPkgVersionCode();
53 int64_t mediaApexVersion = 0;
54
Ray Essicka38704a2020-06-01 13:56:28 -070055 std::string vendor;
56 (void) item->getString("vendor", &vendor);
57 std::string description;
58 (void) item->getString("description", &description);
Ray Essick6ce27e52019-02-15 10:58:05 -080059
60 if (enabled_statsd) {
Robert Shih943123d2021-03-10 02:37:01 -080061 // This field is left here for backward compatibility.
62 // This field is not used anymore.
63 const std::string kUnusedField("unused");
64 android::util::BytesField bf_serialized(kUnusedField.c_str(), kUnusedField.size());
Ray Essick6ce27e52019-02-15 10:58:05 -080065 android::util::stats_write(android::util::MEDIAMETRICS_MEDIADRM_REPORTED,
66 timestamp, pkgName.c_str(), pkgVersionCode,
67 mediaApexVersion,
Ray Essicka38704a2020-06-01 13:56:28 -070068 vendor.c_str(),
69 description.c_str(),
Ray Essick6ce27e52019-02-15 10:58:05 -080070 bf_serialized);
71 } else {
Robert Shih943123d2021-03-10 02:37:01 -080072 ALOGV("NOT sending: mediadrm data(%s, %s)", vendor.c_str(), description.c_str());
Ray Essick6ce27e52019-02-15 10:58:05 -080073 }
74
Ray Essick6ce27e52019-02-15 10:58:05 -080075 return true;
76}
77
Robert Shihaddf5ee2019-08-19 14:11:13 -070078// drmmanager
Ray Essickf27e9872019-12-07 06:28:46 -080079bool statsd_drmmanager(const mediametrics::Item *item)
Robert Shihaddf5ee2019-08-19 14:11:13 -070080{
Robert Shih5edf2452020-02-08 14:58:05 -080081 using namespace std::string_literals;
Andy Hung3ab1b322020-05-18 10:47:31 -070082 if (item == nullptr) return false;
Robert Shihaddf5ee2019-08-19 14:11:13 -070083
Robert Shih5edf2452020-02-08 14:58:05 -080084 if (!enabled_statsd) {
85 ALOGV("NOT sending: drmmanager data");
86 return true;
87 }
88
Ray Essickf27e9872019-12-07 06:28:46 -080089 const nsecs_t timestamp = MediaMetricsService::roundTime(item->getTimestamp());
Robert Shihaddf5ee2019-08-19 14:11:13 -070090 std::string pkgName = item->getPkgName();
91 int64_t pkgVersionCode = item->getPkgVersionCode();
92 int64_t mediaApexVersion = 0;
93
Ray Essicka38704a2020-06-01 13:56:28 -070094 std::string plugin_id;
95 (void) item->getString("plugin_id", &plugin_id);
96 std::string description;
97 (void) item->getString("description", &description);
Robert Shihaddf5ee2019-08-19 14:11:13 -070098 int32_t method_id = -1;
99 (void) item->getInt32("method_id", &method_id);
Ray Essicka38704a2020-06-01 13:56:28 -0700100 std::string mime_types;
101 (void) item->getString("mime_types", &mime_types);
Robert Shihaddf5ee2019-08-19 14:11:13 -0700102
Robert Shih5edf2452020-02-08 14:58:05 -0800103 // Corresponds to the 13 APIs tracked in the MediametricsDrmManagerReported statsd proto
104 // Please see also DrmManager::kMethodIdMap
105 std::array<int64_t, 13> methodCounts{};
106 for (size_t i = 0; i < methodCounts.size() ; i++) {
107 item->getInt64(("method"s + std::to_string(i)).c_str(), &methodCounts[i]);
Robert Shihaddf5ee2019-08-19 14:11:13 -0700108 }
109
Robert Shih5edf2452020-02-08 14:58:05 -0800110 android::util::stats_write(android::util::MEDIAMETRICS_DRMMANAGER_REPORTED,
111 timestamp, pkgName.c_str(), pkgVersionCode, mediaApexVersion,
Ray Essicka38704a2020-06-01 13:56:28 -0700112 plugin_id.c_str(), description.c_str(),
113 method_id, mime_types.c_str(),
Robert Shih5edf2452020-02-08 14:58:05 -0800114 methodCounts[0], methodCounts[1], methodCounts[2],
115 methodCounts[3], methodCounts[4], methodCounts[5],
116 methodCounts[6], methodCounts[7], methodCounts[8],
117 methodCounts[9], methodCounts[10], methodCounts[11],
118 methodCounts[12]);
119
Robert Shihaddf5ee2019-08-19 14:11:13 -0700120 return true;
121}
Ray Essicka38704a2020-06-01 13:56:28 -0700122
Robert Shih2e15aed2021-03-16 18:30:35 -0700123namespace {
124std::vector<uint8_t> base64DecodeNoPad(std::string& str) {
125 if (str.empty()) {
126 return {};
127 }
128
129 switch (str.length() % 4) {
130 case 3: str += "="; break;
131 case 2: str += "=="; break;
132 case 1: str += "==="; break;
133 case 0: /* unchanged */ break;
134 }
135
136 std::vector<uint8_t> buf(str.length() / 4 * 3, 0);
137 size_t size = buf.size();
138 if (decodeBase64(buf.data(), &size, str.c_str()) && size <= buf.size()) {
139 buf.erase(buf.begin() + size, buf.end());
140 return buf;
141 }
142 return {};
143}
144} // namespace
145
146// |out| and its contents are memory-managed by statsd.
147bool statsd_mediadrm_puller(const mediametrics::Item* item, AStatsEventList* out)
148{
149 if (item == nullptr) {
150 return false;
151 }
152
153 if (!enabled_statsd) {
154 ALOGV("NOT pulling: mediadrm activity");
155 return true;
156 }
157
158 std::string serialized_metrics;
159 (void) item->getString("serialized_metrics", &serialized_metrics);
160 const auto framework_raw(base64DecodeNoPad(serialized_metrics));
161
162 std::string plugin_metrics;
163 (void) item->getString("plugin_metrics", &plugin_metrics);
164 const auto plugin_raw(base64DecodeNoPad(plugin_metrics));
165
166 std::string vendor;
167 (void) item->getString("vendor", &vendor);
168 std::string description;
169 (void) item->getString("description", &description);
170
171 // Memory for |event| is internally managed by statsd.
172 AStatsEvent* event = AStatsEventList_addStatsEvent(out);
173 AStatsEvent_setAtomId(event, android::util::MEDIA_DRM_ACTIVITY_INFO);
174 AStatsEvent_writeString(event, item->getPkgName().c_str());
175 AStatsEvent_writeInt64(event, item->getPkgVersionCode());
176 AStatsEvent_writeString(event, vendor.c_str());
177 AStatsEvent_writeString(event, description.c_str());
178 AStatsEvent_writeByteArray(event, framework_raw.data(), framework_raw.size());
179 AStatsEvent_writeByteArray(event, plugin_raw.data(), plugin_raw.size());
180 AStatsEvent_build(event);
181 return true;
182}
183
Ray Essick6ce27e52019-02-15 10:58:05 -0800184} // namespace android