Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 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> |
Robert Shih | 2e15aed | 2021-03-16 18:30:35 -0700 | [diff] [blame^] | 20 | #include <media/stagefright/foundation/base64.h> |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 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 | |
Ray Essick | 40e8e5e | 2019-12-05 20:19:40 -0800 | [diff] [blame] | 34 | #include "MediaMetricsService.h" |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 35 | #include "iface_statsd.h" |
| 36 | |
| 37 | #include <statslog.h> |
| 38 | |
Robert Shih | 5edf245 | 2020-02-08 14:58:05 -0800 | [diff] [blame] | 39 | #include <array> |
| 40 | #include <string> |
Robert Shih | 2e15aed | 2021-03-16 18:30:35 -0700 | [diff] [blame^] | 41 | #include <vector> |
Robert Shih | 5edf245 | 2020-02-08 14:58:05 -0800 | [diff] [blame] | 42 | |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 43 | namespace android { |
| 44 | |
| 45 | // mediadrm |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 46 | bool statsd_mediadrm(const mediametrics::Item *item) |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 47 | { |
Andy Hung | 3ab1b32 | 2020-05-18 10:47:31 -0700 | [diff] [blame] | 48 | if (item == nullptr) return false; |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 49 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 50 | const nsecs_t timestamp = MediaMetricsService::roundTime(item->getTimestamp()); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 51 | std::string pkgName = item->getPkgName(); |
| 52 | int64_t pkgVersionCode = item->getPkgVersionCode(); |
| 53 | int64_t mediaApexVersion = 0; |
| 54 | |
Ray Essick | a38704a | 2020-06-01 13:56:28 -0700 | [diff] [blame] | 55 | std::string vendor; |
| 56 | (void) item->getString("vendor", &vendor); |
| 57 | std::string description; |
| 58 | (void) item->getString("description", &description); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 59 | |
| 60 | if (enabled_statsd) { |
Robert Shih | 943123d | 2021-03-10 02:37:01 -0800 | [diff] [blame] | 61 | // 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()); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 65 | android::util::stats_write(android::util::MEDIAMETRICS_MEDIADRM_REPORTED, |
| 66 | timestamp, pkgName.c_str(), pkgVersionCode, |
| 67 | mediaApexVersion, |
Ray Essick | a38704a | 2020-06-01 13:56:28 -0700 | [diff] [blame] | 68 | vendor.c_str(), |
| 69 | description.c_str(), |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 70 | bf_serialized); |
| 71 | } else { |
Robert Shih | 943123d | 2021-03-10 02:37:01 -0800 | [diff] [blame] | 72 | ALOGV("NOT sending: mediadrm data(%s, %s)", vendor.c_str(), description.c_str()); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 73 | } |
| 74 | |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 75 | return true; |
| 76 | } |
| 77 | |
Robert Shih | addf5ee | 2019-08-19 14:11:13 -0700 | [diff] [blame] | 78 | // drmmanager |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 79 | bool statsd_drmmanager(const mediametrics::Item *item) |
Robert Shih | addf5ee | 2019-08-19 14:11:13 -0700 | [diff] [blame] | 80 | { |
Robert Shih | 5edf245 | 2020-02-08 14:58:05 -0800 | [diff] [blame] | 81 | using namespace std::string_literals; |
Andy Hung | 3ab1b32 | 2020-05-18 10:47:31 -0700 | [diff] [blame] | 82 | if (item == nullptr) return false; |
Robert Shih | addf5ee | 2019-08-19 14:11:13 -0700 | [diff] [blame] | 83 | |
Robert Shih | 5edf245 | 2020-02-08 14:58:05 -0800 | [diff] [blame] | 84 | if (!enabled_statsd) { |
| 85 | ALOGV("NOT sending: drmmanager data"); |
| 86 | return true; |
| 87 | } |
| 88 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 89 | const nsecs_t timestamp = MediaMetricsService::roundTime(item->getTimestamp()); |
Robert Shih | addf5ee | 2019-08-19 14:11:13 -0700 | [diff] [blame] | 90 | std::string pkgName = item->getPkgName(); |
| 91 | int64_t pkgVersionCode = item->getPkgVersionCode(); |
| 92 | int64_t mediaApexVersion = 0; |
| 93 | |
Ray Essick | a38704a | 2020-06-01 13:56:28 -0700 | [diff] [blame] | 94 | std::string plugin_id; |
| 95 | (void) item->getString("plugin_id", &plugin_id); |
| 96 | std::string description; |
| 97 | (void) item->getString("description", &description); |
Robert Shih | addf5ee | 2019-08-19 14:11:13 -0700 | [diff] [blame] | 98 | int32_t method_id = -1; |
| 99 | (void) item->getInt32("method_id", &method_id); |
Ray Essick | a38704a | 2020-06-01 13:56:28 -0700 | [diff] [blame] | 100 | std::string mime_types; |
| 101 | (void) item->getString("mime_types", &mime_types); |
Robert Shih | addf5ee | 2019-08-19 14:11:13 -0700 | [diff] [blame] | 102 | |
Robert Shih | 5edf245 | 2020-02-08 14:58:05 -0800 | [diff] [blame] | 103 | // Corresponds to the 13 APIs tracked in the MediametricsDrmManagerReported statsd proto |
| 104 | // Please see also DrmManager::kMethodIdMap |
| 105 | std::array<int64_t, 13> methodCounts{}; |
| 106 | for (size_t i = 0; i < methodCounts.size() ; i++) { |
| 107 | item->getInt64(("method"s + std::to_string(i)).c_str(), &methodCounts[i]); |
Robert Shih | addf5ee | 2019-08-19 14:11:13 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Robert Shih | 5edf245 | 2020-02-08 14:58:05 -0800 | [diff] [blame] | 110 | android::util::stats_write(android::util::MEDIAMETRICS_DRMMANAGER_REPORTED, |
| 111 | timestamp, pkgName.c_str(), pkgVersionCode, mediaApexVersion, |
Ray Essick | a38704a | 2020-06-01 13:56:28 -0700 | [diff] [blame] | 112 | plugin_id.c_str(), description.c_str(), |
| 113 | method_id, mime_types.c_str(), |
Robert Shih | 5edf245 | 2020-02-08 14:58:05 -0800 | [diff] [blame] | 114 | methodCounts[0], methodCounts[1], methodCounts[2], |
| 115 | methodCounts[3], methodCounts[4], methodCounts[5], |
| 116 | methodCounts[6], methodCounts[7], methodCounts[8], |
| 117 | methodCounts[9], methodCounts[10], methodCounts[11], |
| 118 | methodCounts[12]); |
| 119 | |
Robert Shih | addf5ee | 2019-08-19 14:11:13 -0700 | [diff] [blame] | 120 | return true; |
| 121 | } |
Ray Essick | a38704a | 2020-06-01 13:56:28 -0700 | [diff] [blame] | 122 | |
Robert Shih | 2e15aed | 2021-03-16 18:30:35 -0700 | [diff] [blame^] | 123 | namespace { |
| 124 | std::vector<uint8_t> base64DecodeNoPad(std::string& str) { |
| 125 | if (str.empty()) { |
| 126 | return {}; |
| 127 | } |
| 128 | |
| 129 | switch (str.length() % 4) { |
| 130 | case 3: str += "="; break; |
| 131 | case 2: str += "=="; break; |
| 132 | case 1: str += "==="; break; |
| 133 | case 0: /* unchanged */ break; |
| 134 | } |
| 135 | |
| 136 | std::vector<uint8_t> buf(str.length() / 4 * 3, 0); |
| 137 | size_t size = buf.size(); |
| 138 | if (decodeBase64(buf.data(), &size, str.c_str()) && size <= buf.size()) { |
| 139 | buf.erase(buf.begin() + size, buf.end()); |
| 140 | return buf; |
| 141 | } |
| 142 | return {}; |
| 143 | } |
| 144 | } // namespace |
| 145 | |
| 146 | // |out| and its contents are memory-managed by statsd. |
| 147 | bool statsd_mediadrm_puller(const mediametrics::Item* item, AStatsEventList* out) |
| 148 | { |
| 149 | if (item == nullptr) { |
| 150 | return false; |
| 151 | } |
| 152 | |
| 153 | if (!enabled_statsd) { |
| 154 | ALOGV("NOT pulling: mediadrm activity"); |
| 155 | return true; |
| 156 | } |
| 157 | |
| 158 | std::string serialized_metrics; |
| 159 | (void) item->getString("serialized_metrics", &serialized_metrics); |
| 160 | const auto framework_raw(base64DecodeNoPad(serialized_metrics)); |
| 161 | |
| 162 | std::string plugin_metrics; |
| 163 | (void) item->getString("plugin_metrics", &plugin_metrics); |
| 164 | const auto plugin_raw(base64DecodeNoPad(plugin_metrics)); |
| 165 | |
| 166 | std::string vendor; |
| 167 | (void) item->getString("vendor", &vendor); |
| 168 | std::string description; |
| 169 | (void) item->getString("description", &description); |
| 170 | |
| 171 | // Memory for |event| is internally managed by statsd. |
| 172 | AStatsEvent* event = AStatsEventList_addStatsEvent(out); |
| 173 | AStatsEvent_setAtomId(event, android::util::MEDIA_DRM_ACTIVITY_INFO); |
| 174 | AStatsEvent_writeString(event, item->getPkgName().c_str()); |
| 175 | AStatsEvent_writeInt64(event, item->getPkgVersionCode()); |
| 176 | AStatsEvent_writeString(event, vendor.c_str()); |
| 177 | AStatsEvent_writeString(event, description.c_str()); |
| 178 | AStatsEvent_writeByteArray(event, framework_raw.data(), framework_raw.size()); |
| 179 | AStatsEvent_writeByteArray(event, plugin_raw.data(), plugin_raw.size()); |
| 180 | AStatsEvent_build(event); |
| 181 | return true; |
| 182 | } |
| 183 | |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 184 | } // namespace android |