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 #include <binder/IPCThreadState.h>
22
23 #include <cstdint>
24 #include <inttypes.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <sys/time.h>
28 #include <dirent.h>
29 #include <pthread.h>
30 #include <unistd.h>
31
32 #include <string.h>
33 #include <pwd.h>
34
35 #include "MediaMetricsService.h"
36 #include "MediaDrmStatsdHelper.h"
37 #include "StringUtils.h"
38 #include "iface_statsd.h"
39
40 #include <stats_media_metrics.h>
41
42 #include <array>
43 #include <string>
44 #include <vector>
45
46 namespace android {
47
48 // mediadrm
statsd_mediadrm(const std::shared_ptr<const mediametrics::Item> & item,const std::shared_ptr<mediametrics::StatsdLog> & statsdLog)49 bool statsd_mediadrm(const std::shared_ptr<const mediametrics::Item>& item,
50 const std::shared_ptr<mediametrics::StatsdLog>& statsdLog)
51 {
52 if (item == nullptr) return false;
53
54 const nsecs_t timestamp_nanos = MediaMetricsService::roundTime(item->getTimestamp());
55 const std::string package_name = item->getPkgName();
56 const int64_t package_version_code = item->getPkgVersionCode();
57 const int64_t media_apex_version = 0;
58
59 std::string vendor;
60 (void) item->getString("vendor", &vendor);
61 std::string description;
62 (void) item->getString("description", &description);
63
64 std::string serialized_metrics;
65 (void) item->getString("serialized_metrics", &serialized_metrics);
66 if (serialized_metrics.empty()) {
67 ALOGD("statsd_mediadrm skipping empty entry");
68 return false;
69 }
70
71 // This field is left here for backward compatibility.
72 // This field is not used anymore.
73 const std::string kUnusedField("");
74 const stats::media_metrics::BytesField bf_serialized(kUnusedField.c_str(), kUnusedField.size());
75 int result = 0;
76 if (__builtin_available(android 33, *)) {
77 result = stats::media_metrics::stats_write(
78 stats::media_metrics::MEDIAMETRICS_MEDIADRM_REPORTED,
79 timestamp_nanos, package_name.c_str(), package_version_code,
80 media_apex_version,
81 vendor.c_str(),
82 description.c_str(),
83 bf_serialized);
84 }
85
86 std::stringstream log;
87 log << "result:" << result << " {"
88 << " mediametrics_mediadrm_reported:"
89 << stats::media_metrics::MEDIAMETRICS_MEDIADRM_REPORTED
90 << " timestamp_nanos:" << timestamp_nanos
91 << " package_name:" << package_name
92 << " package_version_code:" << package_version_code
93 << " media_apex_version:" << media_apex_version
94
95 << " vendor:" << vendor
96 << " description:" << description
97 // omitting serialized
98 << " }";
99 statsdLog->log(stats::media_metrics::MEDIAMETRICS_MEDIADRM_REPORTED, log.str());
100 return true;
101 }
102
103 // drmmanager
statsd_drmmanager(const std::shared_ptr<const mediametrics::Item> & item,const std::shared_ptr<mediametrics::StatsdLog> & statsdLog)104 bool statsd_drmmanager(const std::shared_ptr<const mediametrics::Item>& item,
105 const std::shared_ptr<mediametrics::StatsdLog>& statsdLog)
106 {
107 using namespace std::string_literals;
108 if (item == nullptr) return false;
109
110 const nsecs_t timestamp_nanos = MediaMetricsService::roundTime(item->getTimestamp());
111 const std::string package_name = item->getPkgName();
112 const int64_t package_version_code = item->getPkgVersionCode();
113 const int64_t media_apex_version = 0;
114
115 std::string plugin_id;
116 (void) item->getString("plugin_id", &plugin_id);
117 std::string description;
118 (void) item->getString("description", &description);
119 int32_t method_id = -1;
120 (void) item->getInt32("method_id", &method_id);
121 std::string mime_types;
122 (void) item->getString("mime_types", &mime_types);
123
124 // Corresponds to the 13 APIs tracked in the MediametricsDrmManagerReported statsd proto
125 // Please see also DrmManager::kMethodIdMap
126 std::array<int64_t, 13> methodCounts{};
127 for (size_t i = 0; i < methodCounts.size() ; i++) {
128 item->getInt64(("method"s + std::to_string(i)).c_str(), &methodCounts[i]);
129 }
130
131 const int result = stats::media_metrics::stats_write(
132 stats::media_metrics::MEDIAMETRICS_DRMMANAGER_REPORTED,
133 timestamp_nanos, package_name.c_str(), package_version_code,
134 media_apex_version,
135 plugin_id.c_str(), description.c_str(),
136 method_id, mime_types.c_str(),
137 methodCounts[0], methodCounts[1], methodCounts[2],
138 methodCounts[3], methodCounts[4], methodCounts[5],
139 methodCounts[6], methodCounts[7], methodCounts[8],
140 methodCounts[9], methodCounts[10], methodCounts[11],
141 methodCounts[12]);
142
143 std::stringstream log;
144 log << "result:" << result << " {"
145 << " mediametrics_drmmanager_reported:"
146 << stats::media_metrics::MEDIAMETRICS_DRMMANAGER_REPORTED
147 << " timestamp_nanos:" << timestamp_nanos
148 << " package_name:" << package_name
149 << " package_version_code:" << package_version_code
150 << " media_apex_version:" << media_apex_version
151
152 << " plugin_id:" << plugin_id
153 << " description:" << description
154 << " method_id:" << method_id
155 << " mime_types:" << mime_types;
156
157 for (size_t i = 0; i < methodCounts.size(); ++i) {
158 log << " method_" << i << ":" << methodCounts[i];
159 }
160 log << " }";
161 statsdLog->log(stats::media_metrics::MEDIAMETRICS_DRMMANAGER_REPORTED, log.str());
162 return true;
163 }
164
165 namespace {
base64DecodeNoPad(std::string & str)166 std::vector<uint8_t> base64DecodeNoPad(std::string& str) {
167 if (str.empty()) {
168 return {};
169 }
170
171 switch (str.length() % 4) {
172 case 3: str += "="; break;
173 case 2: str += "=="; break;
174 case 1: str += "==="; break;
175 case 0: /* unchanged */ break;
176 }
177
178 std::vector<uint8_t> buf(str.length() / 4 * 3, 0);
179 size_t size = buf.size();
180 if (decodeBase64(buf.data(), &size, str.c_str()) && size <= buf.size()) {
181 buf.erase(buf.begin() + (ptrdiff_t)size, buf.end());
182 return buf;
183 }
184 return {};
185 }
186 } // namespace
187
188 // |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)189 bool statsd_mediadrm_puller(
190 const std::shared_ptr<const mediametrics::Item>& item, AStatsEventList* out,
191 const std::shared_ptr<mediametrics::StatsdLog>& statsdLog)
192 {
193 if (item == nullptr) {
194 return false;
195 }
196
197 std::string serialized_metrics;
198 (void) item->getString("serialized_metrics", &serialized_metrics);
199 const auto framework_raw(base64DecodeNoPad(serialized_metrics));
200
201 std::string plugin_metrics;
202 (void) item->getString("plugin_metrics", &plugin_metrics);
203 const auto plugin_raw(base64DecodeNoPad(plugin_metrics));
204
205 if (serialized_metrics.size() == 0 && plugin_metrics.size() == 0) {
206 ALOGD("statsd_mediadrm_puller skipping empty entry");
207 return false;
208 }
209
210 std::string vendor;
211 (void) item->getString("vendor", &vendor);
212 std::string description;
213 (void) item->getString("description", &description);
214
215 // Memory for |event| is internally managed by statsd.
216 AStatsEvent* event = AStatsEventList_addStatsEvent(out);
217 AStatsEvent_setAtomId(event, stats::media_metrics::MEDIA_DRM_ACTIVITY_INFO);
218 AStatsEvent_writeString(event, item->getPkgName().c_str());
219 AStatsEvent_writeInt64(event, item->getPkgVersionCode());
220 AStatsEvent_writeString(event, vendor.c_str());
221 AStatsEvent_writeString(event, description.c_str());
222 AStatsEvent_writeByteArray(event, framework_raw.data(), framework_raw.size());
223 AStatsEvent_writeByteArray(event, plugin_raw.data(), plugin_raw.size());
224 AStatsEvent_build(event);
225
226 std::stringstream log;
227 log << "pulled:" << " {"
228 << " media_drm_activity_info:"
229 << stats::media_metrics::MEDIA_DRM_ACTIVITY_INFO
230 << " package_name:" << item->getPkgName()
231 << " package_version_code:" << item->getPkgVersionCode()
232 << " vendor:" << vendor
233 << " description:" << description
234 << " framework_metrics:" << mediametrics::stringutils::bytesToString(framework_raw, 8)
235 << " vendor_metrics:" << mediametrics::stringutils::bytesToString(plugin_raw, 8)
236 << " }";
237 statsdLog->log(stats::media_metrics::MEDIA_DRM_ACTIVITY_INFO, log.str());
238 return true;
239 }
240
statsd_mediadrm_created(const std::shared_ptr<const mediametrics::Item> & item,const std::shared_ptr<mediametrics::StatsdLog> & statsdLog)241 bool statsd_mediadrm_created(const std::shared_ptr<const mediametrics::Item>& item,
242 const std::shared_ptr<mediametrics::StatsdLog>& statsdLog)
243 {
244 int64_t uuid_lsb = -1;
245 if (!item->getInt64("uuid_lsb", &uuid_lsb)) return false;
246 int64_t uuid_msb = -1;
247 if (!item->getInt64("uuid_msb", &uuid_msb)) return false;
248 const int32_t scheme = MediaDrmStatsdHelper::findDrmScheme(uuid_msb, uuid_lsb);
249 const int32_t uid = IPCThreadState::self()->getCallingUid();
250 int32_t frontend = 0;
251 if (!item->getInt32("frontend", &frontend)) return false;
252
253 // Optional to be included
254 std::string version = "";
255 item->getString("version", &version);
256 const int result = stats_write(stats::media_metrics::MEDIA_DRM_CREATED,
257 scheme, uuid_lsb, uuid_msb, uid, frontend, version.c_str());
258
259 std::stringstream log;
260 log << "result:" << result << " {"
261 << " media_drm_created:"
262 << stats::media_metrics::MEDIA_DRM_CREATED
263 << " scheme:" << scheme
264 << " uuid_lsb:" << uuid_lsb
265 << " uuid_msb:" << uuid_msb
266 << " uid:" << uid
267 << " frontend:" << frontend
268 << " version:" << version
269 << " }";
270 statsdLog->log(stats::media_metrics::MEDIA_DRM_CREATED, log.str());
271 return true;
272 }
273
statsd_mediadrm_session_opened(const std::shared_ptr<const mediametrics::Item> & item,const std::shared_ptr<mediametrics::StatsdLog> & statsdLog)274 bool statsd_mediadrm_session_opened(const std::shared_ptr<const mediametrics::Item>& item,
275 const std::shared_ptr<mediametrics::StatsdLog>& statsdLog)
276 {
277 int64_t uuid_lsb = -1;
278 if (!item->getInt64("uuid_lsb", &uuid_lsb)) return false;
279 int64_t uuid_msb = -1;
280 if (!item->getInt64("uuid_msb", &uuid_msb)) return false;
281 const int32_t scheme = MediaDrmStatsdHelper::findDrmScheme(uuid_msb, uuid_lsb);
282 std::string object_nonce = "";
283 if (!item->getString("object_nonce", &object_nonce)) return false;
284 const int32_t uid = IPCThreadState::self()->getCallingUid();
285 int32_t frontend = 0;
286 if (!item->getInt32("frontend", &frontend)) return false;
287 int32_t requested_security_level = 0;
288 if (!item->getInt32("requested_security_level", &requested_security_level)) return false;
289 int32_t opened_security_level = 0;
290 if (!item->getInt32("opened_security_level", &opened_security_level)) return false;
291
292 // Optional to be included
293 std::string version = "";
294 item->getString("version", &version);
295 const int result = stats_write(stats::media_metrics::MEDIA_DRM_SESSION_OPENED,
296 scheme, uuid_lsb, uuid_msb, uid, frontend, version.c_str(),
297 object_nonce.c_str(), requested_security_level,
298 opened_security_level);
299
300 std::stringstream log;
301 log << "result:" << result << " {"
302 << " media_drm_session_opened:"
303 << stats::media_metrics::MEDIA_DRM_SESSION_OPENED
304 << " scheme:" << scheme
305 << " uuid_lsb:" << uuid_lsb
306 << " uuid_msb:" << uuid_msb
307 << " uid:" << uid
308 << " frontend:" << frontend
309 << " version:" << version
310 << " object_nonce:" << object_nonce
311 << " requested_security_level:" << requested_security_level
312 << " opened_security_level:" << opened_security_level
313 << " }";
314 statsdLog->log(stats::media_metrics::MEDIA_DRM_SESSION_OPENED, log.str());
315 return true;
316 }
317
statsd_mediadrm_errored(const std::shared_ptr<const mediametrics::Item> & item,const std::shared_ptr<mediametrics::StatsdLog> & statsdLog)318 bool statsd_mediadrm_errored(const std::shared_ptr<const mediametrics::Item>& item,
319 const std::shared_ptr<mediametrics::StatsdLog>& statsdLog)
320 {
321 int64_t uuid_lsb = -1;
322 if (!item->getInt64("uuid_lsb", &uuid_lsb)) return false;
323 int64_t uuid_msb = -1;
324 if (!item->getInt64("uuid_msb", &uuid_msb)) return false;
325 const int32_t scheme = MediaDrmStatsdHelper::findDrmScheme(uuid_msb, uuid_lsb);
326 const int32_t uid = IPCThreadState::self()->getCallingUid();
327 int32_t frontend = 0;
328 if (!item->getInt32("frontend", &frontend)) return false;
329 std::string object_nonce = "";
330 if (!item->getString("object_nonce", &object_nonce)) return false;
331 std::string api_str = "";
332 if (!item->getString("api", &api_str)) return false;
333 const int32_t api = MediaDrmStatsdHelper::findDrmApi(api_str);
334 int32_t error_code = 0;
335 if (!item->getInt32("error_code", &error_code)) return false;
336
337 // Optional to be included
338 std::string version = "";
339 item->getString("version", &version);
340 std::string session_nonce = "";
341 item->getString("session_nonce", &session_nonce);
342 int32_t security_level = 0;
343 item->getInt32("security_level", &security_level);
344
345 int32_t cdm_err = 0;
346 item->getInt32("cdm_err", &cdm_err);
347 int32_t oem_err = 0;
348 item->getInt32("oem_err", &oem_err);
349 int32_t error_context = 0;
350 item->getInt32("error_context", &error_context);
351
352 const int result = stats_write(stats::media_metrics::MEDIA_DRM_ERRORED, scheme, uuid_lsb,
353 uuid_msb, uid, frontend, version.c_str(), object_nonce.c_str(),
354 session_nonce.c_str(), security_level, api, error_code, cdm_err,
355 oem_err, error_context);
356
357 std::stringstream log;
358 log << "result:" << result << " {"
359 << " media_drm_errored:"
360 << stats::media_metrics::MEDIA_DRM_ERRORED
361 << " scheme:" << scheme
362 << " uuid_lsb:" << uuid_lsb
363 << " uuid_msb:" << uuid_msb
364 << " uid:" << uid
365 << " frontend:" << frontend
366 << " version:" << version
367 << " object_nonce:" << object_nonce
368 << " session_nonce:" << session_nonce
369 << " security_level:" << security_level
370 << " api:" << api
371 << " error_code:" << error_code
372 << " cdm_err:" << cdm_err
373 << " oem_err:" << oem_err
374 << " error_context:" << error_context
375 << " }";
376 statsdLog->log(stats::media_metrics::MEDIA_DRM_ERRORED, log.str());
377 return true;
378 }
379
380 } // namespace android
381