blob: 27fd089a3ac1ac59113563864d6964096b255eff [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
Andy Hung5be90c82021-03-30 14:30:20 -070062 // This field is left here for backward compatibility.
63 // This field is not used anymore.
64 const std::string kUnusedField("unused");
65 android::util::BytesField bf_serialized(kUnusedField.c_str(), kUnusedField.size());
66 int result = android::util::stats_write(android::util::MEDIAMETRICS_MEDIADRM_REPORTED,
67 timestamp_nanos, package_name.c_str(), package_version_code,
68 media_apex_version,
69 vendor.c_str(),
70 description.c_str(),
71 bf_serialized);
Ray Essick6ce27e52019-02-15 10:58:05 -080072
Andy Hung5be90c82021-03-30 14:30:20 -070073 std::stringstream log;
74 log << "result:" << result << " {"
75 << " mediametrics_mediadrm_reported:"
76 << android::util::MEDIAMETRICS_MEDIADRM_REPORTED
77 << " timestamp_nanos:" << timestamp_nanos
78 << " package_name:" << package_name
79 << " package_version_code:" << package_version_code
80 << " media_apex_version:" << media_apex_version
81
82 << " vendor:" << vendor
83 << " description:" << description
84 // omitting serialized
85 << " }";
86 statsdLog->log(android::util::MEDIAMETRICS_MEDIADRM_REPORTED, log.str());
Ray Essick6ce27e52019-02-15 10:58:05 -080087 return true;
88}
89
Robert Shihaddf5ee2019-08-19 14:11:13 -070090// drmmanager
Andy Hung5be90c82021-03-30 14:30:20 -070091bool statsd_drmmanager(const std::shared_ptr<const mediametrics::Item>& item,
92 const std::shared_ptr<mediametrics::StatsdLog>& statsdLog)
Robert Shihaddf5ee2019-08-19 14:11:13 -070093{
Robert Shih5edf2452020-02-08 14:58:05 -080094 using namespace std::string_literals;
Andy Hung3ab1b322020-05-18 10:47:31 -070095 if (item == nullptr) return false;
Robert Shihaddf5ee2019-08-19 14:11:13 -070096
Andy Hung5be90c82021-03-30 14:30:20 -070097 const nsecs_t timestamp_nanos = MediaMetricsService::roundTime(item->getTimestamp());
98 const std::string package_name = item->getPkgName();
99 const int64_t package_version_code = item->getPkgVersionCode();
100 const int64_t media_apex_version = 0;
Robert Shihaddf5ee2019-08-19 14:11:13 -0700101
Ray Essicka38704a2020-06-01 13:56:28 -0700102 std::string plugin_id;
103 (void) item->getString("plugin_id", &plugin_id);
104 std::string description;
105 (void) item->getString("description", &description);
Robert Shihaddf5ee2019-08-19 14:11:13 -0700106 int32_t method_id = -1;
107 (void) item->getInt32("method_id", &method_id);
Ray Essicka38704a2020-06-01 13:56:28 -0700108 std::string mime_types;
109 (void) item->getString("mime_types", &mime_types);
Robert Shihaddf5ee2019-08-19 14:11:13 -0700110
Robert Shih5edf2452020-02-08 14:58:05 -0800111 // Corresponds to the 13 APIs tracked in the MediametricsDrmManagerReported statsd proto
112 // Please see also DrmManager::kMethodIdMap
113 std::array<int64_t, 13> methodCounts{};
114 for (size_t i = 0; i < methodCounts.size() ; i++) {
115 item->getInt64(("method"s + std::to_string(i)).c_str(), &methodCounts[i]);
Robert Shihaddf5ee2019-08-19 14:11:13 -0700116 }
117
Andy Hung5be90c82021-03-30 14:30:20 -0700118 const int result = android::util::stats_write(android::util::MEDIAMETRICS_DRMMANAGER_REPORTED,
119 timestamp_nanos, package_name.c_str(), package_version_code,
120 media_apex_version,
Ray Essicka38704a2020-06-01 13:56:28 -0700121 plugin_id.c_str(), description.c_str(),
122 method_id, mime_types.c_str(),
Robert Shih5edf2452020-02-08 14:58:05 -0800123 methodCounts[0], methodCounts[1], methodCounts[2],
124 methodCounts[3], methodCounts[4], methodCounts[5],
125 methodCounts[6], methodCounts[7], methodCounts[8],
126 methodCounts[9], methodCounts[10], methodCounts[11],
127 methodCounts[12]);
128
Andy Hung5be90c82021-03-30 14:30:20 -0700129 std::stringstream log;
130 log << "result:" << result << " {"
131 << " mediametrics_drmmanager_reported:"
132 << android::util::MEDIAMETRICS_DRMMANAGER_REPORTED
133 << " timestamp_nanos:" << timestamp_nanos
134 << " package_name:" << package_name
135 << " package_version_code:" << package_version_code
136 << " media_apex_version:" << media_apex_version
137
138 << " plugin_id:" << plugin_id
139 << " description:" << description
140 << " method_id:" << method_id
141 << " mime_types:" << mime_types;
142
143 for (size_t i = 0; i < methodCounts.size(); ++i) {
144 log << " method_" << i << ":" << methodCounts[i];
145 }
146 log << " }";
147 statsdLog->log(android::util::MEDIAMETRICS_DRMMANAGER_REPORTED, log.str());
Robert Shihaddf5ee2019-08-19 14:11:13 -0700148 return true;
149}
Ray Essicka38704a2020-06-01 13:56:28 -0700150
Robert Shih2e15aed2021-03-16 18:30:35 -0700151namespace {
152std::vector<uint8_t> base64DecodeNoPad(std::string& str) {
153 if (str.empty()) {
154 return {};
155 }
156
157 switch (str.length() % 4) {
158 case 3: str += "="; break;
159 case 2: str += "=="; break;
160 case 1: str += "==="; break;
161 case 0: /* unchanged */ break;
162 }
163
164 std::vector<uint8_t> buf(str.length() / 4 * 3, 0);
165 size_t size = buf.size();
166 if (decodeBase64(buf.data(), &size, str.c_str()) && size <= buf.size()) {
167 buf.erase(buf.begin() + size, buf.end());
168 return buf;
169 }
170 return {};
171}
172} // namespace
173
174// |out| and its contents are memory-managed by statsd.
Andy Hung5be90c82021-03-30 14:30:20 -0700175bool statsd_mediadrm_puller(
Andy Hung1f560f82021-03-31 19:08:52 -0700176 const std::shared_ptr<const mediametrics::Item>& item, AStatsEventList* out,
177 const std::shared_ptr<mediametrics::StatsdLog>& statsdLog)
Robert Shih2e15aed2021-03-16 18:30:35 -0700178{
179 if (item == nullptr) {
180 return false;
181 }
182
Robert Shih2e15aed2021-03-16 18:30:35 -0700183 std::string serialized_metrics;
184 (void) item->getString("serialized_metrics", &serialized_metrics);
185 const auto framework_raw(base64DecodeNoPad(serialized_metrics));
186
187 std::string plugin_metrics;
188 (void) item->getString("plugin_metrics", &plugin_metrics);
189 const auto plugin_raw(base64DecodeNoPad(plugin_metrics));
190
191 std::string vendor;
192 (void) item->getString("vendor", &vendor);
193 std::string description;
194 (void) item->getString("description", &description);
195
196 // Memory for |event| is internally managed by statsd.
197 AStatsEvent* event = AStatsEventList_addStatsEvent(out);
198 AStatsEvent_setAtomId(event, android::util::MEDIA_DRM_ACTIVITY_INFO);
199 AStatsEvent_writeString(event, item->getPkgName().c_str());
200 AStatsEvent_writeInt64(event, item->getPkgVersionCode());
201 AStatsEvent_writeString(event, vendor.c_str());
202 AStatsEvent_writeString(event, description.c_str());
203 AStatsEvent_writeByteArray(event, framework_raw.data(), framework_raw.size());
204 AStatsEvent_writeByteArray(event, plugin_raw.data(), plugin_raw.size());
205 AStatsEvent_build(event);
Andy Hung1f560f82021-03-31 19:08:52 -0700206
207 std::stringstream log;
208 log << "pulled:" << " {"
209 << " media_drm_activity_info:"
210 << android::util::MEDIA_DRM_ACTIVITY_INFO
211 << " package_name:" << item->getPkgName()
212 << " package_version_code:" << item->getPkgVersionCode()
213 << " vendor:" << vendor
214 << " description:" << description
215 << " framework_metrics:" << mediametrics::stringutils::bytesToString(framework_raw, 8)
216 << " vendor_metrics:" << mediametrics::stringutils::bytesToString(plugin_raw, 8)
217 << " }";
218 statsdLog->log(android::util::MEDIA_DRM_ACTIVITY_INFO, log.str());
Robert Shih2e15aed2021-03-16 18:30:35 -0700219 return true;
220}
221
Ray Essick6ce27e52019-02-15 10:58:05 -0800222} // namespace android