blob: 287fb8d974c61722d01ece2075085b41d8d60f14 [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"
Andy Hung1f560f82021-03-31 19:08:52 -070035#include "StringUtils.h"
Ray Essick6ce27e52019-02-15 10:58:05 -080036#include "iface_statsd.h"
37
38#include <statslog.h>
39
Robert Shih5edf2452020-02-08 14:58:05 -080040#include <array>
41#include <string>
Robert Shih2e15aed2021-03-16 18:30:35 -070042#include <vector>
Robert Shih5edf2452020-02-08 14:58:05 -080043
Ray Essick6ce27e52019-02-15 10:58:05 -080044namespace android {
45
46// mediadrm
Andy Hung5be90c82021-03-30 14:30:20 -070047bool statsd_mediadrm(const std::shared_ptr<const mediametrics::Item>& item,
48 const std::shared_ptr<mediametrics::StatsdLog>& statsdLog)
Ray Essick6ce27e52019-02-15 10:58:05 -080049{
Andy Hung3ab1b322020-05-18 10:47:31 -070050 if (item == nullptr) return false;
Ray Essick6ce27e52019-02-15 10:58:05 -080051
Andy Hung5be90c82021-03-30 14:30:20 -070052 const nsecs_t timestamp_nanos = MediaMetricsService::roundTime(item->getTimestamp());
53 const std::string package_name = item->getPkgName();
54 const int64_t package_version_code = item->getPkgVersionCode();
55 const int64_t media_apex_version = 0;
Ray Essick6ce27e52019-02-15 10:58:05 -080056
Ray Essicka38704a2020-06-01 13:56:28 -070057 std::string vendor;
58 (void) item->getString("vendor", &vendor);
59 std::string description;
60 (void) item->getString("description", &description);
Ray Essick6ce27e52019-02-15 10:58:05 -080061
Robert Shih5e45c312021-04-22 16:27:43 -070062 std::string serialized_metrics;
63 (void) item->getString("serialized_metrics", &serialized_metrics);
64 if (serialized_metrics.empty()) {
65 ALOGD("statsd_mediadrm skipping empty entry");
66 return false;
67 }
68
Andy Hung5be90c82021-03-30 14:30:20 -070069 // This field is left here for backward compatibility.
70 // This field is not used anymore.
Robert Shih5e45c312021-04-22 16:27:43 -070071 const std::string kUnusedField("");
Andy Hung5be90c82021-03-30 14:30:20 -070072 android::util::BytesField bf_serialized(kUnusedField.c_str(), kUnusedField.size());
73 int result = android::util::stats_write(android::util::MEDIAMETRICS_MEDIADRM_REPORTED,
74 timestamp_nanos, package_name.c_str(), package_version_code,
75 media_apex_version,
76 vendor.c_str(),
77 description.c_str(),
78 bf_serialized);
Ray Essick6ce27e52019-02-15 10:58:05 -080079
Andy Hung5be90c82021-03-30 14:30:20 -070080 std::stringstream log;
81 log << "result:" << result << " {"
82 << " mediametrics_mediadrm_reported:"
83 << android::util::MEDIAMETRICS_MEDIADRM_REPORTED
84 << " timestamp_nanos:" << timestamp_nanos
85 << " package_name:" << package_name
86 << " package_version_code:" << package_version_code
87 << " media_apex_version:" << media_apex_version
88
89 << " vendor:" << vendor
90 << " description:" << description
91 // omitting serialized
92 << " }";
93 statsdLog->log(android::util::MEDIAMETRICS_MEDIADRM_REPORTED, log.str());
Ray Essick6ce27e52019-02-15 10:58:05 -080094 return true;
95}
96
Robert Shihaddf5ee2019-08-19 14:11:13 -070097// drmmanager
Andy Hung5be90c82021-03-30 14:30:20 -070098bool statsd_drmmanager(const std::shared_ptr<const mediametrics::Item>& item,
99 const std::shared_ptr<mediametrics::StatsdLog>& statsdLog)
Robert Shihaddf5ee2019-08-19 14:11:13 -0700100{
Robert Shih5edf2452020-02-08 14:58:05 -0800101 using namespace std::string_literals;
Andy Hung3ab1b322020-05-18 10:47:31 -0700102 if (item == nullptr) return false;
Robert Shihaddf5ee2019-08-19 14:11:13 -0700103
Andy Hung5be90c82021-03-30 14:30:20 -0700104 const nsecs_t timestamp_nanos = MediaMetricsService::roundTime(item->getTimestamp());
105 const std::string package_name = item->getPkgName();
106 const int64_t package_version_code = item->getPkgVersionCode();
107 const int64_t media_apex_version = 0;
Robert Shihaddf5ee2019-08-19 14:11:13 -0700108
Ray Essicka38704a2020-06-01 13:56:28 -0700109 std::string plugin_id;
110 (void) item->getString("plugin_id", &plugin_id);
111 std::string description;
112 (void) item->getString("description", &description);
Robert Shihaddf5ee2019-08-19 14:11:13 -0700113 int32_t method_id = -1;
114 (void) item->getInt32("method_id", &method_id);
Ray Essicka38704a2020-06-01 13:56:28 -0700115 std::string mime_types;
116 (void) item->getString("mime_types", &mime_types);
Robert Shihaddf5ee2019-08-19 14:11:13 -0700117
Robert Shih5edf2452020-02-08 14:58:05 -0800118 // Corresponds to the 13 APIs tracked in the MediametricsDrmManagerReported statsd proto
119 // Please see also DrmManager::kMethodIdMap
120 std::array<int64_t, 13> methodCounts{};
121 for (size_t i = 0; i < methodCounts.size() ; i++) {
122 item->getInt64(("method"s + std::to_string(i)).c_str(), &methodCounts[i]);
Robert Shihaddf5ee2019-08-19 14:11:13 -0700123 }
124
Andy Hung5be90c82021-03-30 14:30:20 -0700125 const int result = android::util::stats_write(android::util::MEDIAMETRICS_DRMMANAGER_REPORTED,
126 timestamp_nanos, package_name.c_str(), package_version_code,
127 media_apex_version,
Ray Essicka38704a2020-06-01 13:56:28 -0700128 plugin_id.c_str(), description.c_str(),
129 method_id, mime_types.c_str(),
Robert Shih5edf2452020-02-08 14:58:05 -0800130 methodCounts[0], methodCounts[1], methodCounts[2],
131 methodCounts[3], methodCounts[4], methodCounts[5],
132 methodCounts[6], methodCounts[7], methodCounts[8],
133 methodCounts[9], methodCounts[10], methodCounts[11],
134 methodCounts[12]);
135
Andy Hung5be90c82021-03-30 14:30:20 -0700136 std::stringstream log;
137 log << "result:" << result << " {"
138 << " mediametrics_drmmanager_reported:"
139 << android::util::MEDIAMETRICS_DRMMANAGER_REPORTED
140 << " timestamp_nanos:" << timestamp_nanos
141 << " package_name:" << package_name
142 << " package_version_code:" << package_version_code
143 << " media_apex_version:" << media_apex_version
144
145 << " plugin_id:" << plugin_id
146 << " description:" << description
147 << " method_id:" << method_id
148 << " mime_types:" << mime_types;
149
150 for (size_t i = 0; i < methodCounts.size(); ++i) {
151 log << " method_" << i << ":" << methodCounts[i];
152 }
153 log << " }";
154 statsdLog->log(android::util::MEDIAMETRICS_DRMMANAGER_REPORTED, log.str());
Robert Shihaddf5ee2019-08-19 14:11:13 -0700155 return true;
156}
Ray Essicka38704a2020-06-01 13:56:28 -0700157
Robert Shih2e15aed2021-03-16 18:30:35 -0700158namespace {
159std::vector<uint8_t> base64DecodeNoPad(std::string& str) {
160 if (str.empty()) {
161 return {};
162 }
163
164 switch (str.length() % 4) {
165 case 3: str += "="; break;
166 case 2: str += "=="; break;
167 case 1: str += "==="; break;
168 case 0: /* unchanged */ break;
169 }
170
171 std::vector<uint8_t> buf(str.length() / 4 * 3, 0);
172 size_t size = buf.size();
173 if (decodeBase64(buf.data(), &size, str.c_str()) && size <= buf.size()) {
174 buf.erase(buf.begin() + size, buf.end());
175 return buf;
176 }
177 return {};
178}
179} // namespace
180
181// |out| and its contents are memory-managed by statsd.
Andy Hung5be90c82021-03-30 14:30:20 -0700182bool statsd_mediadrm_puller(
Andy Hung1f560f82021-03-31 19:08:52 -0700183 const std::shared_ptr<const mediametrics::Item>& item, AStatsEventList* out,
184 const std::shared_ptr<mediametrics::StatsdLog>& statsdLog)
Robert Shih2e15aed2021-03-16 18:30:35 -0700185{
186 if (item == nullptr) {
187 return false;
188 }
189
Robert Shih2e15aed2021-03-16 18:30:35 -0700190 std::string serialized_metrics;
191 (void) item->getString("serialized_metrics", &serialized_metrics);
192 const auto framework_raw(base64DecodeNoPad(serialized_metrics));
193
194 std::string plugin_metrics;
195 (void) item->getString("plugin_metrics", &plugin_metrics);
196 const auto plugin_raw(base64DecodeNoPad(plugin_metrics));
197
Robert Shih58a37f82021-04-19 10:18:50 -0700198 if (serialized_metrics.size() == 0 && plugin_metrics.size() == 0) {
199 ALOGD("statsd_mediadrm_puller skipping empty entry");
200 return false;
201 }
202
Robert Shih2e15aed2021-03-16 18:30:35 -0700203 std::string vendor;
204 (void) item->getString("vendor", &vendor);
205 std::string description;
206 (void) item->getString("description", &description);
207
208 // Memory for |event| is internally managed by statsd.
209 AStatsEvent* event = AStatsEventList_addStatsEvent(out);
210 AStatsEvent_setAtomId(event, android::util::MEDIA_DRM_ACTIVITY_INFO);
211 AStatsEvent_writeString(event, item->getPkgName().c_str());
212 AStatsEvent_writeInt64(event, item->getPkgVersionCode());
213 AStatsEvent_writeString(event, vendor.c_str());
214 AStatsEvent_writeString(event, description.c_str());
215 AStatsEvent_writeByteArray(event, framework_raw.data(), framework_raw.size());
216 AStatsEvent_writeByteArray(event, plugin_raw.data(), plugin_raw.size());
217 AStatsEvent_build(event);
Andy Hung1f560f82021-03-31 19:08:52 -0700218
219 std::stringstream log;
220 log << "pulled:" << " {"
221 << " media_drm_activity_info:"
222 << android::util::MEDIA_DRM_ACTIVITY_INFO
223 << " package_name:" << item->getPkgName()
224 << " package_version_code:" << item->getPkgVersionCode()
225 << " vendor:" << vendor
226 << " description:" << description
227 << " framework_metrics:" << mediametrics::stringutils::bytesToString(framework_raw, 8)
228 << " vendor_metrics:" << mediametrics::stringutils::bytesToString(plugin_raw, 8)
229 << " }";
230 statsdLog->log(android::util::MEDIA_DRM_ACTIVITY_INFO, log.str());
Robert Shih2e15aed2021-03-16 18:30:35 -0700231 return true;
232}
233
Ray Essick6ce27e52019-02-15 10:58:05 -0800234} // namespace android