Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 17 | #define LOG_TAG "mediametrics::Item" |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 18 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 19 | #include <inttypes.h> |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | #include <sys/types.h> |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 23 | |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 24 | #include <mutex> |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 25 | #include <set> |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 26 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 27 | #include <binder/Parcel.h> |
| 28 | #include <utils/Errors.h> |
| 29 | #include <utils/Log.h> |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 30 | #include <utils/SortedVector.h> |
| 31 | #include <utils/threads.h> |
| 32 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 33 | #include <binder/IServiceManager.h> |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 34 | #include <media/IMediaMetricsService.h> |
| 35 | #include <media/MediaMetricsItem.h> |
Ray Essick | 79a89ef | 2017-04-24 15:52:54 -0700 | [diff] [blame] | 36 | #include <private/android_filesystem_config.h> |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 37 | |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 38 | // Max per-property string size before truncation in toString(). |
| 39 | // Do not make too large, as this is used for dumpsys purposes. |
| 40 | static constexpr size_t kMaxPropertyStringSize = 4096; |
| 41 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 42 | namespace android::mediametrics { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 43 | |
| 44 | #define DEBUG_SERVICEACCESS 0 |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 45 | #define DEBUG_API 0 |
| 46 | #define DEBUG_ALLOCATIONS 0 |
| 47 | |
| 48 | // after this many failed attempts, we stop trying [from this process] and just say that |
| 49 | // the service is off. |
| 50 | #define SVC_TRIES 2 |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 51 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 52 | mediametrics::Item* mediametrics::Item::convert(mediametrics_handle_t handle) { |
| 53 | mediametrics::Item *item = (android::mediametrics::Item *) handle; |
Ray Essick | bf536ac | 2019-08-26 11:04:28 -0700 | [diff] [blame] | 54 | return item; |
| 55 | } |
| 56 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 57 | mediametrics_handle_t mediametrics::Item::convert(mediametrics::Item *item ) { |
Ray Essick | bf536ac | 2019-08-26 11:04:28 -0700 | [diff] [blame] | 58 | mediametrics_handle_t handle = (mediametrics_handle_t) item; |
| 59 | return handle; |
| 60 | } |
| 61 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 62 | mediametrics::Item::~Item() { |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 63 | if (DEBUG_ALLOCATIONS) { |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 64 | ALOGD("Destroy mediametrics::Item @ %p", this); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 65 | } |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 68 | mediametrics::Item &mediametrics::Item::setTimestamp(nsecs_t ts) { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 69 | mTimestamp = ts; |
| 70 | return *this; |
| 71 | } |
| 72 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 73 | nsecs_t mediametrics::Item::getTimestamp() const { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 74 | return mTimestamp; |
| 75 | } |
| 76 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 77 | mediametrics::Item &mediametrics::Item::setPid(pid_t pid) { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 78 | mPid = pid; |
| 79 | return *this; |
| 80 | } |
| 81 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 82 | pid_t mediametrics::Item::getPid() const { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 83 | return mPid; |
| 84 | } |
| 85 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 86 | mediametrics::Item &mediametrics::Item::setUid(uid_t uid) { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 87 | mUid = uid; |
| 88 | return *this; |
| 89 | } |
| 90 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 91 | uid_t mediametrics::Item::getUid() const { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 92 | return mUid; |
| 93 | } |
| 94 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 95 | mediametrics::Item &mediametrics::Item::setPkgName(const std::string &pkgName) { |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 96 | mPkgName = pkgName; |
| 97 | return *this; |
| 98 | } |
| 99 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 100 | mediametrics::Item &mediametrics::Item::setPkgVersionCode(int64_t pkgVersionCode) { |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 101 | mPkgVersionCode = pkgVersionCode; |
| 102 | return *this; |
| 103 | } |
| 104 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 105 | int64_t mediametrics::Item::getPkgVersionCode() const { |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 106 | return mPkgVersionCode; |
| 107 | } |
| 108 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 109 | // remove indicated keys and their values |
| 110 | // return value is # keys removed |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 111 | size_t mediametrics::Item::filter(size_t n, const char *attrs[]) { |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 112 | size_t zapped = 0; |
| 113 | for (size_t i = 0; i < n; ++i) { |
Andy Hung | 47e58d6 | 2019-12-06 18:40:19 -0800 | [diff] [blame] | 114 | zapped += mProps.erase(attrs[i]); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 115 | } |
| 116 | return zapped; |
| 117 | } |
| 118 | |
| 119 | // remove any keys NOT in the provided list |
| 120 | // return value is # keys removed |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 121 | size_t mediametrics::Item::filterNot(size_t n, const char *attrs[]) { |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 122 | std::set<std::string> check(attrs, attrs + n); |
| 123 | size_t zapped = 0; |
Andy Hung | 47e58d6 | 2019-12-06 18:40:19 -0800 | [diff] [blame] | 124 | for (auto it = mProps.begin(); it != mProps.end();) { |
| 125 | if (check.find(it->first) != check.end()) { |
| 126 | ++it; |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 127 | } else { |
Andy Hung | 47e58d6 | 2019-12-06 18:40:19 -0800 | [diff] [blame] | 128 | it = mProps.erase(it); |
| 129 | ++zapped; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | return zapped; |
| 133 | } |
| 134 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 135 | // Parcel / serialize things for binder calls |
| 136 | // |
| 137 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 138 | status_t mediametrics::Item::readFromParcel(const Parcel& data) { |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 139 | int32_t version; |
| 140 | status_t status = data.readInt32(&version); |
| 141 | if (status != NO_ERROR) return status; |
Ray Essick | ba8c484 | 2019-01-18 11:35:33 -0800 | [diff] [blame] | 142 | |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 143 | switch (version) { |
| 144 | case 0: |
| 145 | return readFromParcel0(data); |
| 146 | default: |
| 147 | ALOGE("%s: unsupported parcel version: %d", __func__, version); |
| 148 | return INVALID_OPERATION; |
Ray Essick | ba8c484 | 2019-01-18 11:35:33 -0800 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 152 | status_t mediametrics::Item::readFromParcel0(const Parcel& data) { |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 153 | const char *s = data.readCString(); |
| 154 | mKey = s == nullptr ? "" : s; |
| 155 | int32_t pid, uid; |
| 156 | status_t status = data.readInt32(&pid) ?: data.readInt32(&uid); |
| 157 | if (status != NO_ERROR) return status; |
| 158 | mPid = (pid_t)pid; |
| 159 | mUid = (uid_t)uid; |
| 160 | s = data.readCString(); |
| 161 | mPkgName = s == nullptr ? "" : s; |
| 162 | int32_t count; |
| 163 | int64_t version, timestamp; |
| 164 | status = data.readInt64(&version) ?: data.readInt64(×tamp) ?: data.readInt32(&count); |
| 165 | if (status != NO_ERROR) return status; |
| 166 | if (count < 0) return BAD_VALUE; |
| 167 | mPkgVersionCode = version; |
| 168 | mTimestamp = timestamp; |
Andy Hung | 47e58d6 | 2019-12-06 18:40:19 -0800 | [diff] [blame] | 169 | for (int i = 0; i < count; i++) { |
| 170 | Prop prop; |
| 171 | status_t status = prop.readFromParcel(data); |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 172 | if (status != NO_ERROR) return status; |
Andy Hung | 47e58d6 | 2019-12-06 18:40:19 -0800 | [diff] [blame] | 173 | mProps[prop.getName()] = std::move(prop); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 174 | } |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 175 | return NO_ERROR; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 178 | status_t mediametrics::Item::writeToParcel(Parcel *data) const { |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 179 | if (data == nullptr) return BAD_VALUE; |
Ray Essick | ba8c484 | 2019-01-18 11:35:33 -0800 | [diff] [blame] | 180 | |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 181 | const int32_t version = 0; |
| 182 | status_t status = data->writeInt32(version); |
| 183 | if (status != NO_ERROR) return status; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 184 | |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 185 | switch (version) { |
| 186 | case 0: |
| 187 | return writeToParcel0(data); |
| 188 | default: |
| 189 | ALOGE("%s: unsupported parcel version: %d", __func__, version); |
| 190 | return INVALID_OPERATION; |
Ray Essick | ba8c484 | 2019-01-18 11:35:33 -0800 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 194 | status_t mediametrics::Item::writeToParcel0(Parcel *data) const { |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 195 | status_t status = |
| 196 | data->writeCString(mKey.c_str()) |
| 197 | ?: data->writeInt32(mPid) |
| 198 | ?: data->writeInt32(mUid) |
| 199 | ?: data->writeCString(mPkgName.c_str()) |
| 200 | ?: data->writeInt64(mPkgVersionCode) |
| 201 | ?: data->writeInt64(mTimestamp); |
| 202 | if (status != NO_ERROR) return status; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 203 | |
Andy Hung | 47e58d6 | 2019-12-06 18:40:19 -0800 | [diff] [blame] | 204 | data->writeInt32((int32_t)mProps.size()); |
| 205 | for (auto &prop : *this) { |
| 206 | status = prop.writeToParcel(data); |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 207 | if (status != NO_ERROR) return status; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 208 | } |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 209 | return NO_ERROR; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 212 | const char *mediametrics::Item::toCString() { |
Andy Hung | 3b4c1f0 | 2020-01-23 18:58:32 -0800 | [diff] [blame^] | 213 | std::string val = toString(); |
Ray Essick | 2014732 | 2018-11-17 09:08:39 -0800 | [diff] [blame] | 214 | return strdup(val.c_str()); |
| 215 | } |
| 216 | |
Andy Hung | 3b4c1f0 | 2020-01-23 18:58:32 -0800 | [diff] [blame^] | 217 | /* |
| 218 | * Similar to audio_utils/clock.h but customized for displaying mediametrics time. |
| 219 | */ |
| 220 | |
| 221 | void nsToString(int64_t ns, char *buffer, size_t bufferSize, PrintFormat format) |
| 222 | { |
| 223 | if (bufferSize == 0) return; |
| 224 | |
| 225 | const int one_second = 1000000000; |
| 226 | const time_t sec = ns / one_second; |
| 227 | struct tm tm; |
| 228 | |
| 229 | // Supported on bionic, glibc, and macOS, but not mingw. |
| 230 | if (localtime_r(&sec, &tm) == NULL) { |
| 231 | buffer[0] = '\0'; |
| 232 | return; |
| 233 | } |
| 234 | |
| 235 | switch (format) { |
| 236 | default: |
| 237 | case kPrintFormatLong: |
| 238 | if (snprintf(buffer, bufferSize, "%02d-%02d %02d:%02d:%02d.%03d", |
| 239 | tm.tm_mon + 1, // localtime_r uses months in 0 - 11 range |
| 240 | tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, |
| 241 | (int)(ns % one_second / 1000000)) < 0) { |
| 242 | buffer[0] = '\0'; // null terminate on format error, which should not happen |
| 243 | } |
| 244 | break; |
| 245 | case kPrintFormatShort: |
| 246 | if (snprintf(buffer, bufferSize, "%02d:%02d:%02d.%03d", |
| 247 | tm.tm_hour, tm.tm_min, tm.tm_sec, |
| 248 | (int)(ns % one_second / 1000000)) < 0) { |
| 249 | buffer[0] = '\0'; // null terminate on format error, which should not happen |
| 250 | } |
| 251 | break; |
| 252 | } |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 253 | } |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 254 | |
Andy Hung | 3b4c1f0 | 2020-01-23 18:58:32 -0800 | [diff] [blame^] | 255 | std::string mediametrics::Item::toString() const { |
Ray Essick | 783bd0d | 2018-01-11 11:10:35 -0800 | [diff] [blame] | 256 | std::string result; |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 257 | char buffer[kMaxPropertyStringSize]; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 258 | |
Andy Hung | 3b4c1f0 | 2020-01-23 18:58:32 -0800 | [diff] [blame^] | 259 | snprintf(buffer, sizeof(buffer), "{%s, (%s), (%s, %d, %d)", |
| 260 | mKey.c_str(), |
| 261 | timeStringFromNs(mTimestamp, kPrintFormatLong).time, |
| 262 | mPkgName.c_str(), mPid, mUid |
| 263 | ); |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 264 | result.append(buffer); |
Andy Hung | 3b4c1f0 | 2020-01-23 18:58:32 -0800 | [diff] [blame^] | 265 | bool first = true; |
Andy Hung | 47e58d6 | 2019-12-06 18:40:19 -0800 | [diff] [blame] | 266 | for (auto &prop : *this) { |
Andy Hung | b7aadb3 | 2019-12-09 19:40:42 -0800 | [diff] [blame] | 267 | prop.toStringBuffer(buffer, sizeof(buffer)); |
Andy Hung | 3b4c1f0 | 2020-01-23 18:58:32 -0800 | [diff] [blame^] | 268 | result += first ? ", (" : ", "; |
| 269 | result += buffer; |
| 270 | first = false; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 271 | } |
Andy Hung | 3b4c1f0 | 2020-01-23 18:58:32 -0800 | [diff] [blame^] | 272 | result.append(")}"); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 273 | return result; |
| 274 | } |
| 275 | |
| 276 | // for the lazy, we offer methods that finds the service and |
| 277 | // calls the appropriate daemon |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 278 | bool mediametrics::Item::selfrecord() { |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 279 | ALOGD_IF(DEBUG_API, "%s: delivering %s", __func__, this->toString().c_str()); |
Andy Hung | 47e58d6 | 2019-12-06 18:40:19 -0800 | [diff] [blame] | 280 | sp<IMediaMetricsService> svc = getService(); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 281 | if (svc != NULL) { |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 282 | status_t status = svc->submit(this); |
| 283 | if (status != NO_ERROR) { |
| 284 | ALOGW("%s: failed to record: %s", __func__, this->toString().c_str()); |
Ray Essick | 2ab3c43 | 2017-10-02 09:29:49 -0700 | [diff] [blame] | 285 | return false; |
| 286 | } |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 287 | return true; |
| 288 | } else { |
| 289 | return false; |
| 290 | } |
| 291 | } |
| 292 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 293 | //static |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 294 | bool BaseItem::isEnabled() { |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 295 | // completely skip logging from certain UIDs. We do this here |
| 296 | // to avoid the multi-second timeouts while we learn that |
| 297 | // sepolicy will not let us find the service. |
| 298 | // We do this only for a select set of UIDs |
| 299 | // The sepolicy protection is still in place, we just want a faster |
| 300 | // response from this specific, small set of uids. |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 301 | |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 302 | // This is checked only once in the lifetime of the process. |
| 303 | const uid_t uid = getuid(); |
| 304 | switch (uid) { |
| 305 | case AID_RADIO: // telephony subsystem, RIL |
| 306 | return false; |
| 307 | } |
| 308 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 309 | int enabled = property_get_int32(Item::EnabledProperty, -1); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 310 | if (enabled == -1) { |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 311 | enabled = property_get_int32(Item::EnabledPropertyPersist, -1); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 312 | } |
| 313 | if (enabled == -1) { |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 314 | enabled = Item::EnabledProperty_default; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 315 | } |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 316 | return enabled > 0; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 317 | } |
| 318 | |
Ray Essick | 2ab3c43 | 2017-10-02 09:29:49 -0700 | [diff] [blame] | 319 | // monitor health of our connection to the metrics service |
| 320 | class MediaMetricsDeathNotifier : public IBinder::DeathRecipient { |
| 321 | virtual void binderDied(const wp<IBinder> &) { |
| 322 | ALOGW("Reacquire service connection on next request"); |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 323 | BaseItem::dropInstance(); |
Ray Essick | 2ab3c43 | 2017-10-02 09:29:49 -0700 | [diff] [blame] | 324 | } |
| 325 | }; |
| 326 | |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 327 | static sp<MediaMetricsDeathNotifier> sNotifier; |
| 328 | // static |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 329 | sp<IMediaMetricsService> BaseItem::sMediaMetricsService; |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 330 | static std::mutex sServiceMutex; |
| 331 | static int sRemainingBindAttempts = SVC_TRIES; |
Ray Essick | 2ab3c43 | 2017-10-02 09:29:49 -0700 | [diff] [blame] | 332 | |
| 333 | // static |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 334 | void BaseItem::dropInstance() { |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 335 | std::lock_guard _l(sServiceMutex); |
| 336 | sRemainingBindAttempts = SVC_TRIES; |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 337 | sMediaMetricsService = nullptr; |
Ray Essick | 2ab3c43 | 2017-10-02 09:29:49 -0700 | [diff] [blame] | 338 | } |
| 339 | |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 340 | // static |
| 341 | bool BaseItem::submitBuffer(const char *buffer, size_t size) { |
| 342 | /* |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 343 | mediametrics::Item item; |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 344 | status_t status = item.readFromByteString(buffer, size); |
| 345 | ALOGD("%s: status:%d, size:%zu, item:%s", __func__, status, size, item.toString().c_str()); |
| 346 | return item.selfrecord(); |
| 347 | */ |
| 348 | |
| 349 | ALOGD_IF(DEBUG_API, "%s: delivering %zu bytes", __func__, size); |
Andy Hung | 47e58d6 | 2019-12-06 18:40:19 -0800 | [diff] [blame] | 350 | sp<IMediaMetricsService> svc = getService(); |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 351 | if (svc != nullptr) { |
| 352 | const status_t status = svc->submitBuffer(buffer, size); |
| 353 | if (status != NO_ERROR) { |
| 354 | ALOGW("%s: failed(%d) to record: %zu bytes", __func__, status, size); |
| 355 | return false; |
| 356 | } |
| 357 | return true; |
| 358 | } |
| 359 | return false; |
| 360 | } |
| 361 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 362 | //static |
Andy Hung | 47e58d6 | 2019-12-06 18:40:19 -0800 | [diff] [blame] | 363 | sp<IMediaMetricsService> BaseItem::getService() { |
Ray Essick | d38e174 | 2017-01-23 15:17:06 -0800 | [diff] [blame] | 364 | static const char *servicename = "media.metrics"; |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 365 | static const bool enabled = isEnabled(); // singleton initialized |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 366 | |
| 367 | if (enabled == false) { |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 368 | ALOGD_IF(DEBUG_SERVICEACCESS, "disabled"); |
| 369 | return nullptr; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 370 | } |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 371 | std::lock_guard _l(sServiceMutex); |
| 372 | // think of remainingBindAttempts as telling us whether service == nullptr because |
| 373 | // (1) we haven't tried to initialize it yet |
| 374 | // (2) we've tried to initialize it, but failed. |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 375 | if (sMediaMetricsService == nullptr && sRemainingBindAttempts > 0) { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 376 | const char *badness = ""; |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 377 | sp<IServiceManager> sm = defaultServiceManager(); |
| 378 | if (sm != nullptr) { |
| 379 | sp<IBinder> binder = sm->getService(String16(servicename)); |
| 380 | if (binder != nullptr) { |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 381 | sMediaMetricsService = interface_cast<IMediaMetricsService>(binder); |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 382 | sNotifier = new MediaMetricsDeathNotifier(); |
| 383 | binder->linkToDeath(sNotifier); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 384 | } else { |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 385 | badness = "did not find service"; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 386 | } |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 387 | } else { |
| 388 | badness = "No Service Manager access"; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 389 | } |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 390 | if (sMediaMetricsService == nullptr) { |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 391 | if (sRemainingBindAttempts > 0) { |
| 392 | sRemainingBindAttempts--; |
| 393 | } |
| 394 | ALOGD_IF(DEBUG_SERVICEACCESS, "%s: unable to bind to service %s: %s", |
| 395 | __func__, servicename, badness); |
| 396 | } |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 397 | } |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 398 | return sMediaMetricsService; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 399 | } |
| 400 | |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 401 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 402 | status_t mediametrics::Item::writeToByteString(char **pbuffer, size_t *plength) const |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 403 | { |
| 404 | if (pbuffer == nullptr || plength == nullptr) |
| 405 | return BAD_VALUE; |
| 406 | |
| 407 | // get size |
| 408 | const size_t keySizeZeroTerminated = strlen(mKey.c_str()) + 1; |
| 409 | if (keySizeZeroTerminated > UINT16_MAX) { |
| 410 | ALOGW("%s: key size %zu too large", __func__, keySizeZeroTerminated); |
| 411 | return INVALID_OPERATION; |
| 412 | } |
| 413 | const uint16_t version = 0; |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 414 | const uint32_t header_size = |
| 415 | sizeof(uint32_t) // total size |
| 416 | + sizeof(header_size) // header size |
| 417 | + sizeof(version) // encoding version |
| 418 | + sizeof(uint16_t) // key size |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 419 | + keySizeZeroTerminated // key, zero terminated |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 420 | + sizeof(int32_t) // pid |
| 421 | + sizeof(int32_t) // uid |
| 422 | + sizeof(int64_t) // timestamp |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 423 | ; |
| 424 | |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 425 | uint32_t size = header_size |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 426 | + sizeof(uint32_t) // # properties |
| 427 | ; |
Andy Hung | 47e58d6 | 2019-12-06 18:40:19 -0800 | [diff] [blame] | 428 | for (auto &prop : *this) { |
| 429 | const size_t propSize = prop.getByteStringSize(); |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 430 | if (propSize > UINT16_MAX) { |
Andy Hung | 47e58d6 | 2019-12-06 18:40:19 -0800 | [diff] [blame] | 431 | ALOGW("%s: prop %s size %zu too large", __func__, prop.getName(), propSize); |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 432 | return INVALID_OPERATION; |
| 433 | } |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 434 | if (__builtin_add_overflow(size, propSize, &size)) { |
Andy Hung | 47e58d6 | 2019-12-06 18:40:19 -0800 | [diff] [blame] | 435 | ALOGW("%s: item size overflow at property %s", __func__, prop.getName()); |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 436 | return INVALID_OPERATION; |
| 437 | } |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 438 | } |
| 439 | |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 440 | // since we fill every byte in the buffer (there is no padding), |
| 441 | // malloc is used here instead of calloc. |
| 442 | char * const build = (char *)malloc(size); |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 443 | if (build == nullptr) return NO_MEMORY; |
| 444 | |
| 445 | char *filling = build; |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 446 | char *buildmax = build + size; |
| 447 | if (insert((uint32_t)size, &filling, buildmax) != NO_ERROR |
| 448 | || insert(header_size, &filling, buildmax) != NO_ERROR |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 449 | || insert(version, &filling, buildmax) != NO_ERROR |
| 450 | || insert((uint16_t)keySizeZeroTerminated, &filling, buildmax) != NO_ERROR |
| 451 | || insert(mKey.c_str(), &filling, buildmax) != NO_ERROR |
| 452 | || insert((int32_t)mPid, &filling, buildmax) != NO_ERROR |
| 453 | || insert((int32_t)mUid, &filling, buildmax) != NO_ERROR |
| 454 | || insert((int64_t)mTimestamp, &filling, buildmax) != NO_ERROR |
Andy Hung | 47e58d6 | 2019-12-06 18:40:19 -0800 | [diff] [blame] | 455 | || insert((uint32_t)mProps.size(), &filling, buildmax) != NO_ERROR) { |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 456 | ALOGE("%s:could not write header", __func__); // shouldn't happen |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 457 | free(build); |
| 458 | return INVALID_OPERATION; |
| 459 | } |
Andy Hung | 47e58d6 | 2019-12-06 18:40:19 -0800 | [diff] [blame] | 460 | for (auto &prop : *this) { |
| 461 | if (prop.writeToByteString(&filling, buildmax) != NO_ERROR) { |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 462 | free(build); |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 463 | // shouldn't happen |
Andy Hung | 47e58d6 | 2019-12-06 18:40:19 -0800 | [diff] [blame] | 464 | ALOGE("%s:could not write prop %s", __func__, prop.getName()); |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 465 | return INVALID_OPERATION; |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | if (filling != buildmax) { |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 470 | ALOGE("%s: problems populating; wrote=%d planned=%d", |
| 471 | __func__, (int)(filling - build), (int)size); |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 472 | free(build); |
| 473 | return INVALID_OPERATION; |
| 474 | } |
| 475 | *pbuffer = build; |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 476 | *plength = size; |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 477 | return NO_ERROR; |
| 478 | } |
| 479 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 480 | status_t mediametrics::Item::readFromByteString(const char *bufferptr, size_t length) |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 481 | { |
| 482 | if (bufferptr == nullptr) return BAD_VALUE; |
| 483 | |
| 484 | const char *read = bufferptr; |
| 485 | const char *readend = bufferptr + length; |
| 486 | |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 487 | uint32_t size; |
| 488 | uint32_t header_size; |
| 489 | uint16_t version; |
| 490 | uint16_t key_size; |
Andy Hung | b7aadb3 | 2019-12-09 19:40:42 -0800 | [diff] [blame] | 491 | std::string key; |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 492 | int32_t pid; |
| 493 | int32_t uid; |
| 494 | int64_t timestamp; |
| 495 | uint32_t propCount; |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 496 | if (extract(&size, &read, readend) != NO_ERROR |
| 497 | || extract(&header_size, &read, readend) != NO_ERROR |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 498 | || extract(&version, &read, readend) != NO_ERROR |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 499 | || extract(&key_size, &read, readend) != NO_ERROR |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 500 | || extract(&key, &read, readend) != NO_ERROR |
| 501 | || extract(&pid, &read, readend) != NO_ERROR |
| 502 | || extract(&uid, &read, readend) != NO_ERROR |
| 503 | || extract(×tamp, &read, readend) != NO_ERROR |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 504 | || size > length |
Andy Hung | b7aadb3 | 2019-12-09 19:40:42 -0800 | [diff] [blame] | 505 | || key.size() + 1 != key_size |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 506 | || header_size > size) { |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 507 | ALOGW("%s: invalid header", __func__); |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 508 | return INVALID_OPERATION; |
| 509 | } |
Andy Hung | b7aadb3 | 2019-12-09 19:40:42 -0800 | [diff] [blame] | 510 | mKey = std::move(key); |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 511 | const size_t pos = read - bufferptr; |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 512 | if (pos > header_size) { |
| 513 | ALOGW("%s: invalid header pos:%zu > header_size:%u", |
| 514 | __func__, pos, header_size); |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 515 | return INVALID_OPERATION; |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 516 | } else if (pos < header_size) { |
| 517 | ALOGW("%s: mismatched header pos:%zu < header_size:%u, advancing", |
| 518 | __func__, pos, header_size); |
| 519 | read += (header_size - pos); |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 520 | } |
| 521 | if (extract(&propCount, &read, readend) != NO_ERROR) { |
| 522 | ALOGD("%s: cannot read prop count", __func__); |
| 523 | return INVALID_OPERATION; |
| 524 | } |
| 525 | mPid = pid; |
| 526 | mUid = uid; |
| 527 | mTimestamp = timestamp; |
| 528 | for (size_t i = 0; i < propCount; ++i) { |
Andy Hung | 47e58d6 | 2019-12-06 18:40:19 -0800 | [diff] [blame] | 529 | Prop prop; |
| 530 | if (prop.readFromByteString(&read, readend) != NO_ERROR) { |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 531 | ALOGW("%s: cannot read prop %zu", __func__, i); |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 532 | return INVALID_OPERATION; |
| 533 | } |
Andy Hung | 47e58d6 | 2019-12-06 18:40:19 -0800 | [diff] [blame] | 534 | mProps[prop.getName()] = std::move(prop); |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 535 | } |
| 536 | return NO_ERROR; |
| 537 | } |
| 538 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 539 | status_t mediametrics::Item::Prop::readFromParcel(const Parcel& data) |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 540 | { |
| 541 | const char *key = data.readCString(); |
| 542 | if (key == nullptr) return BAD_VALUE; |
| 543 | int32_t type; |
| 544 | status_t status = data.readInt32(&type); |
| 545 | if (status != NO_ERROR) return status; |
| 546 | switch (type) { |
Andy Hung | b7aadb3 | 2019-12-09 19:40:42 -0800 | [diff] [blame] | 547 | case mediametrics::kTypeInt32: { |
| 548 | int32_t value; |
| 549 | status = data.readInt32(&value); |
| 550 | if (status != NO_ERROR) return status; |
| 551 | mElem = value; |
| 552 | } break; |
| 553 | case mediametrics::kTypeInt64: { |
| 554 | int64_t value; |
| 555 | status = data.readInt64(&value); |
| 556 | if (status != NO_ERROR) return status; |
| 557 | mElem = value; |
| 558 | } break; |
| 559 | case mediametrics::kTypeDouble: { |
| 560 | double value; |
| 561 | status = data.readDouble(&value); |
| 562 | if (status != NO_ERROR) return status; |
| 563 | mElem = value; |
| 564 | } break; |
Andy Hung | 47e58d6 | 2019-12-06 18:40:19 -0800 | [diff] [blame] | 565 | case mediametrics::kTypeCString: { |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 566 | const char *s = data.readCString(); |
| 567 | if (s == nullptr) return BAD_VALUE; |
Andy Hung | b7aadb3 | 2019-12-09 19:40:42 -0800 | [diff] [blame] | 568 | mElem = s; |
| 569 | } break; |
Andy Hung | 47e58d6 | 2019-12-06 18:40:19 -0800 | [diff] [blame] | 570 | case mediametrics::kTypeRate: { |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 571 | std::pair<int64_t, int64_t> rate; |
| 572 | status = data.readInt64(&rate.first) |
| 573 | ?: data.readInt64(&rate.second); |
Andy Hung | b7aadb3 | 2019-12-09 19:40:42 -0800 | [diff] [blame] | 574 | if (status != NO_ERROR) return status; |
| 575 | mElem = rate; |
| 576 | } break; |
| 577 | case mediametrics::kTypeNone: { |
| 578 | mElem = std::monostate{}; |
| 579 | } break; |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 580 | default: |
Andy Hung | b7aadb3 | 2019-12-09 19:40:42 -0800 | [diff] [blame] | 581 | ALOGE("%s: reading bad item type: %d", __func__, type); |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 582 | return BAD_VALUE; |
| 583 | } |
Andy Hung | b7aadb3 | 2019-12-09 19:40:42 -0800 | [diff] [blame] | 584 | setName(key); |
| 585 | return NO_ERROR; |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 586 | } |
| 587 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 588 | status_t mediametrics::Item::Prop::readFromByteString( |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 589 | const char **bufferpptr, const char *bufferptrmax) |
| 590 | { |
| 591 | uint16_t len; |
Andy Hung | b7aadb3 | 2019-12-09 19:40:42 -0800 | [diff] [blame] | 592 | std::string name; |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 593 | uint8_t type; |
| 594 | status_t status = extract(&len, bufferpptr, bufferptrmax) |
| 595 | ?: extract(&type, bufferpptr, bufferptrmax) |
| 596 | ?: extract(&name, bufferpptr, bufferptrmax); |
| 597 | if (status != NO_ERROR) return status; |
Andy Hung | b7aadb3 | 2019-12-09 19:40:42 -0800 | [diff] [blame] | 598 | switch (type) { |
| 599 | case mediametrics::kTypeInt32: { |
| 600 | int32_t value; |
| 601 | status = extract(&value, bufferpptr, bufferptrmax); |
| 602 | if (status != NO_ERROR) return status; |
| 603 | mElem = value; |
| 604 | } break; |
| 605 | case mediametrics::kTypeInt64: { |
| 606 | int64_t value; |
| 607 | status = extract(&value, bufferpptr, bufferptrmax); |
| 608 | if (status != NO_ERROR) return status; |
| 609 | mElem = value; |
| 610 | } break; |
| 611 | case mediametrics::kTypeDouble: { |
| 612 | double value; |
| 613 | status = extract(&value, bufferpptr, bufferptrmax); |
| 614 | if (status != NO_ERROR) return status; |
| 615 | mElem = value; |
| 616 | } break; |
| 617 | case mediametrics::kTypeRate: { |
| 618 | std::pair<int64_t, int64_t> value; |
| 619 | status = extract(&value.first, bufferpptr, bufferptrmax) |
| 620 | ?: extract(&value.second, bufferpptr, bufferptrmax); |
| 621 | if (status != NO_ERROR) return status; |
| 622 | mElem = value; |
| 623 | } break; |
| 624 | case mediametrics::kTypeCString: { |
| 625 | std::string value; |
| 626 | status = extract(&value, bufferpptr, bufferptrmax); |
| 627 | if (status != NO_ERROR) return status; |
| 628 | mElem = std::move(value); |
| 629 | } break; |
| 630 | case mediametrics::kTypeNone: { |
| 631 | mElem = std::monostate{}; |
| 632 | } break; |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 633 | default: |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 634 | ALOGE("%s: found bad prop type: %d, name %s", |
Andy Hung | b7aadb3 | 2019-12-09 19:40:42 -0800 | [diff] [blame] | 635 | __func__, (int)type, mName.c_str()); // no payload sent |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 636 | return BAD_VALUE; |
| 637 | } |
Andy Hung | b7aadb3 | 2019-12-09 19:40:42 -0800 | [diff] [blame] | 638 | mName = name; |
| 639 | return NO_ERROR; |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 640 | } |
| 641 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 642 | } // namespace android::mediametrics |