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