1 /*
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>
20 #include <media/stagefright/foundation/base64.h>
21
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
34 #include "MediaMetricsService.h"
35 #include "StringUtils.h"
36 #include "iface_statsd.h"
37
38 #include <statslog.h>
39
40 #include <array>
41 #include <string>
42 #include <vector>
43
44 namespace android {
45
46 // mediadrm
statsd_mediadrm(const std::shared_ptr<const mediametrics::Item> & item,const std::shared_ptr<mediametrics::StatsdLog> & statsdLog)47 bool statsd_mediadrm(const std::shared_ptr<const mediametrics::Item>& item,
48 const std::shared_ptr<mediametrics::StatsdLog>& statsdLog)
49 {
50 if (item == nullptr) return false;
51
52 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;
56
57 std::string vendor;
58 (void) item->getString("vendor", &vendor);
59 std::string description;
60 (void) item->getString("description", &description);
61
62 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
69 // This field is left here for backward compatibility.
70 // This field is not used anymore.
71 const std::string kUnusedField("");
72 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);
79
80 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());
94 return true;
95 }
96
97 // drmmanager
statsd_drmmanager(const std::shared_ptr<const mediametrics::Item> & item,const std::shared_ptr<mediametrics::StatsdLog> & statsdLog)98 bool statsd_drmmanager(const std::shared_ptr<const mediametrics::Item>& item,
99 const std::shared_ptr<mediametrics::StatsdLog>& statsdLog)
100 {
101 using namespace std::string_literals;
102 if (item == nullptr) return false;
103
104 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;
108
109 std::string plugin_id;
110 (void) item->getString("plugin_id", &plugin_id);
111 std::string description;
112 (void) item->getString("description", &description);
113 int32_t method_id = -1;
114 (void) item->getInt32("method_id", &method_id);
115 std::string mime_types;
116 (void) item->getString("mime_types", &mime_types);
117
118 // 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]);
123 }
124
125 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,
128 plugin_id.c_str(), description.c_str(),
129 method_id, mime_types.c_str(),
130 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
136 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());
155 return true;
156 }
157
158 namespace {
base64DecodeNoPad(std::string & str)159 std::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.
statsd_mediadrm_puller(const std::shared_ptr<const mediametrics::Item> & item,AStatsEventList * out,const std::shared_ptr<mediametrics::StatsdLog> & statsdLog)182 bool statsd_mediadrm_puller(
183 const std::shared_ptr<const mediametrics::Item>& item, AStatsEventList* out,
184 const std::shared_ptr<mediametrics::StatsdLog>& statsdLog)
185 {
186 if (item == nullptr) {
187 return false;
188 }
189
190 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
198 if (serialized_metrics.size() == 0 && plugin_metrics.size() == 0) {
199 ALOGD("statsd_mediadrm_puller skipping empty entry");
200 return false;
201 }
202
203 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);
218
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());
231 return true;
232 }
233
234 } // namespace android
235