blob: d043da5fc621548b0df281f0e6f3806fa48149a0 [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
Andy Hung5be90c82021-03-30 14:30:20 -070046bool statsd_mediadrm(const std::shared_ptr<const mediametrics::Item>& item,
47 const std::shared_ptr<mediametrics::StatsdLog>& statsdLog)
Ray Essick6ce27e52019-02-15 10:58:05 -080048{
Andy Hung3ab1b322020-05-18 10:47:31 -070049 if (item == nullptr) return false;
Ray Essick6ce27e52019-02-15 10:58:05 -080050
Andy Hung5be90c82021-03-30 14:30:20 -070051 const nsecs_t timestamp_nanos = MediaMetricsService::roundTime(item->getTimestamp());
52 const std::string package_name = item->getPkgName();
53 const int64_t package_version_code = item->getPkgVersionCode();
54 const int64_t media_apex_version = 0;
Ray Essick6ce27e52019-02-15 10:58:05 -080055
Ray Essicka38704a2020-06-01 13:56:28 -070056 std::string vendor;
57 (void) item->getString("vendor", &vendor);
58 std::string description;
59 (void) item->getString("description", &description);
Ray Essick6ce27e52019-02-15 10:58:05 -080060
Andy Hung5be90c82021-03-30 14:30:20 -070061 // 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());
65 int result = android::util::stats_write(android::util::MEDIAMETRICS_MEDIADRM_REPORTED,
66 timestamp_nanos, package_name.c_str(), package_version_code,
67 media_apex_version,
68 vendor.c_str(),
69 description.c_str(),
70 bf_serialized);
Ray Essick6ce27e52019-02-15 10:58:05 -080071
Andy Hung5be90c82021-03-30 14:30:20 -070072 std::stringstream log;
73 log << "result:" << result << " {"
74 << " mediametrics_mediadrm_reported:"
75 << android::util::MEDIAMETRICS_MEDIADRM_REPORTED
76 << " timestamp_nanos:" << timestamp_nanos
77 << " package_name:" << package_name
78 << " package_version_code:" << package_version_code
79 << " media_apex_version:" << media_apex_version
80
81 << " vendor:" << vendor
82 << " description:" << description
83 // omitting serialized
84 << " }";
85 statsdLog->log(android::util::MEDIAMETRICS_MEDIADRM_REPORTED, log.str());
Ray Essick6ce27e52019-02-15 10:58:05 -080086 return true;
87}
88
Robert Shihaddf5ee2019-08-19 14:11:13 -070089// drmmanager
Andy Hung5be90c82021-03-30 14:30:20 -070090bool statsd_drmmanager(const std::shared_ptr<const mediametrics::Item>& item,
91 const std::shared_ptr<mediametrics::StatsdLog>& statsdLog)
Robert Shihaddf5ee2019-08-19 14:11:13 -070092{
Robert Shih5edf2452020-02-08 14:58:05 -080093 using namespace std::string_literals;
Andy Hung3ab1b322020-05-18 10:47:31 -070094 if (item == nullptr) return false;
Robert Shihaddf5ee2019-08-19 14:11:13 -070095
Andy Hung5be90c82021-03-30 14:30:20 -070096 const nsecs_t timestamp_nanos = MediaMetricsService::roundTime(item->getTimestamp());
97 const std::string package_name = item->getPkgName();
98 const int64_t package_version_code = item->getPkgVersionCode();
99 const int64_t media_apex_version = 0;
Robert Shihaddf5ee2019-08-19 14:11:13 -0700100
Ray Essicka38704a2020-06-01 13:56:28 -0700101 std::string plugin_id;
102 (void) item->getString("plugin_id", &plugin_id);
103 std::string description;
104 (void) item->getString("description", &description);
Robert Shihaddf5ee2019-08-19 14:11:13 -0700105 int32_t method_id = -1;
106 (void) item->getInt32("method_id", &method_id);
Ray Essicka38704a2020-06-01 13:56:28 -0700107 std::string mime_types;
108 (void) item->getString("mime_types", &mime_types);
Robert Shihaddf5ee2019-08-19 14:11:13 -0700109
Robert Shih5edf2452020-02-08 14:58:05 -0800110 // Corresponds to the 13 APIs tracked in the MediametricsDrmManagerReported statsd proto
111 // Please see also DrmManager::kMethodIdMap
112 std::array<int64_t, 13> methodCounts{};
113 for (size_t i = 0; i < methodCounts.size() ; i++) {
114 item->getInt64(("method"s + std::to_string(i)).c_str(), &methodCounts[i]);
Robert Shihaddf5ee2019-08-19 14:11:13 -0700115 }
116
Andy Hung5be90c82021-03-30 14:30:20 -0700117 const int result = android::util::stats_write(android::util::MEDIAMETRICS_DRMMANAGER_REPORTED,
118 timestamp_nanos, package_name.c_str(), package_version_code,
119 media_apex_version,
Ray Essicka38704a2020-06-01 13:56:28 -0700120 plugin_id.c_str(), description.c_str(),
121 method_id, mime_types.c_str(),
Robert Shih5edf2452020-02-08 14:58:05 -0800122 methodCounts[0], methodCounts[1], methodCounts[2],
123 methodCounts[3], methodCounts[4], methodCounts[5],
124 methodCounts[6], methodCounts[7], methodCounts[8],
125 methodCounts[9], methodCounts[10], methodCounts[11],
126 methodCounts[12]);
127
Andy Hung5be90c82021-03-30 14:30:20 -0700128 std::stringstream log;
129 log << "result:" << result << " {"
130 << " mediametrics_drmmanager_reported:"
131 << android::util::MEDIAMETRICS_DRMMANAGER_REPORTED
132 << " timestamp_nanos:" << timestamp_nanos
133 << " package_name:" << package_name
134 << " package_version_code:" << package_version_code
135 << " media_apex_version:" << media_apex_version
136
137 << " plugin_id:" << plugin_id
138 << " description:" << description
139 << " method_id:" << method_id
140 << " mime_types:" << mime_types;
141
142 for (size_t i = 0; i < methodCounts.size(); ++i) {
143 log << " method_" << i << ":" << methodCounts[i];
144 }
145 log << " }";
146 statsdLog->log(android::util::MEDIAMETRICS_DRMMANAGER_REPORTED, log.str());
Robert Shihaddf5ee2019-08-19 14:11:13 -0700147 return true;
148}
Ray Essicka38704a2020-06-01 13:56:28 -0700149
Robert Shih2e15aed2021-03-16 18:30:35 -0700150namespace {
151std::vector<uint8_t> base64DecodeNoPad(std::string& str) {
152 if (str.empty()) {
153 return {};
154 }
155
156 switch (str.length() % 4) {
157 case 3: str += "="; break;
158 case 2: str += "=="; break;
159 case 1: str += "==="; break;
160 case 0: /* unchanged */ break;
161 }
162
163 std::vector<uint8_t> buf(str.length() / 4 * 3, 0);
164 size_t size = buf.size();
165 if (decodeBase64(buf.data(), &size, str.c_str()) && size <= buf.size()) {
166 buf.erase(buf.begin() + size, buf.end());
167 return buf;
168 }
169 return {};
170}
171} // namespace
172
173// |out| and its contents are memory-managed by statsd.
Andy Hung5be90c82021-03-30 14:30:20 -0700174bool statsd_mediadrm_puller(
175 const std::shared_ptr<const mediametrics::Item>& item, AStatsEventList* out)
Robert Shih2e15aed2021-03-16 18:30:35 -0700176{
177 if (item == nullptr) {
178 return false;
179 }
180
Robert Shih2e15aed2021-03-16 18:30:35 -0700181 std::string serialized_metrics;
182 (void) item->getString("serialized_metrics", &serialized_metrics);
183 const auto framework_raw(base64DecodeNoPad(serialized_metrics));
184
185 std::string plugin_metrics;
186 (void) item->getString("plugin_metrics", &plugin_metrics);
187 const auto plugin_raw(base64DecodeNoPad(plugin_metrics));
188
189 std::string vendor;
190 (void) item->getString("vendor", &vendor);
191 std::string description;
192 (void) item->getString("description", &description);
193
194 // Memory for |event| is internally managed by statsd.
195 AStatsEvent* event = AStatsEventList_addStatsEvent(out);
196 AStatsEvent_setAtomId(event, android::util::MEDIA_DRM_ACTIVITY_INFO);
197 AStatsEvent_writeString(event, item->getPkgName().c_str());
198 AStatsEvent_writeInt64(event, item->getPkgVersionCode());
199 AStatsEvent_writeString(event, vendor.c_str());
200 AStatsEvent_writeString(event, description.c_str());
201 AStatsEvent_writeByteArray(event, framework_raw.data(), framework_raw.size());
202 AStatsEvent_writeByteArray(event, plugin_raw.data(), plugin_raw.size());
203 AStatsEvent_build(event);
204 return true;
205}
206
Ray Essick6ce27e52019-02-15 10:58:05 -0800207} // namespace android