blob: 36ab8c31f1cea497d7a15e73bcc8bc17b4481c36 [file] [log] [blame]
Ray Essick3938dc62016-11-01 08:56:56 -07001/*
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 Essickf27e9872019-12-07 06:28:46 -080017#define LOG_TAG "mediametrics::Item"
Ray Essick3938dc62016-11-01 08:56:56 -070018
Ray Essick3938dc62016-11-01 08:56:56 -070019#include <inttypes.h>
Ray Essickb5fac8e2016-12-12 11:33:56 -080020#include <stdlib.h>
21#include <string.h>
22#include <sys/types.h>
Ray Essick3938dc62016-11-01 08:56:56 -070023
Andy Hunga87e69c2019-10-18 10:07:40 -070024#include <mutex>
Andy Hung3253f2d2019-10-21 14:50:07 -070025#include <set>
Andy Hung5088e1a2021-11-11 09:17:21 -080026#include <unordered_map>
Andy Hunga87e69c2019-10-18 10:07:40 -070027
Ray Essick3938dc62016-11-01 08:56:56 -070028#include <binder/Parcel.h>
Andy Hung7d391082020-04-18 15:03:51 -070029#include <cutils/properties.h>
Ray Essick3938dc62016-11-01 08:56:56 -070030#include <utils/Errors.h>
31#include <utils/Log.h>
Ray Essick3938dc62016-11-01 08:56:56 -070032#include <utils/SortedVector.h>
33#include <utils/threads.h>
34
Andy Hung49ca44e2020-11-10 22:14:58 -080035#include <android/media/BnMediaMetricsService.h> // for direct Binder access
36#include <android/media/IMediaMetricsService.h>
Ray Essick3938dc62016-11-01 08:56:56 -070037#include <binder/IServiceManager.h>
Ray Essickf27e9872019-12-07 06:28:46 -080038#include <media/MediaMetricsItem.h>
Ray Essick79a89ef2017-04-24 15:52:54 -070039#include <private/android_filesystem_config.h>
Ray Essick3938dc62016-11-01 08:56:56 -070040
Andy Hung1efc9c62019-12-03 13:43:33 -080041// Max per-property string size before truncation in toString().
42// Do not make too large, as this is used for dumpsys purposes.
43static constexpr size_t kMaxPropertyStringSize = 4096;
44
Ray Essickf27e9872019-12-07 06:28:46 -080045namespace android::mediametrics {
Ray Essick3938dc62016-11-01 08:56:56 -070046
47#define DEBUG_SERVICEACCESS 0
Ray Essickb5fac8e2016-12-12 11:33:56 -080048#define DEBUG_API 0
49#define DEBUG_ALLOCATIONS 0
50
51// after this many failed attempts, we stop trying [from this process] and just say that
52// the service is off.
53#define SVC_TRIES 2
Ray Essick3938dc62016-11-01 08:56:56 -070054
Andy Hung5088e1a2021-11-11 09:17:21 -080055static const std::unordered_map<std::string, int32_t>& getErrorStringMap() {
56 // DO NOT MODIFY VALUES (OK to add new ones).
57 // This may be found in frameworks/av/media/libmediametrics/include/MediaMetricsConstants.h
58 static std::unordered_map<std::string, int32_t> map{
59 {"", NO_ERROR},
Andy Hungf1a23832021-12-13 09:31:55 -080060 {AMEDIAMETRICS_PROP_ERROR_VALUE_OK, NO_ERROR},
Andy Hung5088e1a2021-11-11 09:17:21 -080061 {AMEDIAMETRICS_PROP_ERROR_VALUE_ARGUMENT, BAD_VALUE},
62 {AMEDIAMETRICS_PROP_ERROR_VALUE_IO, DEAD_OBJECT},
63 {AMEDIAMETRICS_PROP_ERROR_VALUE_MEMORY, NO_MEMORY},
64 {AMEDIAMETRICS_PROP_ERROR_VALUE_SECURITY, PERMISSION_DENIED},
65 {AMEDIAMETRICS_PROP_ERROR_VALUE_STATE, INVALID_OPERATION},
66 {AMEDIAMETRICS_PROP_ERROR_VALUE_TIMEOUT, WOULD_BLOCK},
67 {AMEDIAMETRICS_PROP_ERROR_VALUE_UNKNOWN, UNKNOWN_ERROR},
68 };
69 return map;
70}
71
72status_t errorStringToStatus(const char *error) {
73 const auto& map = getErrorStringMap();
Greg Kaiser96206632021-11-29 06:52:17 -080074 if (error == nullptr || error[0] == '\0') return NO_ERROR;
Andy Hung5088e1a2021-11-11 09:17:21 -080075 auto it = map.find(error);
76 if (it != map.end()) {
77 return it->second;
78 }
79 return UNKNOWN_ERROR;
80}
81
Ray Essickf27e9872019-12-07 06:28:46 -080082mediametrics::Item* mediametrics::Item::convert(mediametrics_handle_t handle) {
83 mediametrics::Item *item = (android::mediametrics::Item *) handle;
Ray Essickbf536ac2019-08-26 11:04:28 -070084 return item;
85}
86
Ray Essickf27e9872019-12-07 06:28:46 -080087mediametrics_handle_t mediametrics::Item::convert(mediametrics::Item *item ) {
Ray Essickbf536ac2019-08-26 11:04:28 -070088 mediametrics_handle_t handle = (mediametrics_handle_t) item;
89 return handle;
90}
91
Ray Essickf27e9872019-12-07 06:28:46 -080092mediametrics::Item::~Item() {
Ray Essickb5fac8e2016-12-12 11:33:56 -080093 if (DEBUG_ALLOCATIONS) {
Ray Essickf27e9872019-12-07 06:28:46 -080094 ALOGD("Destroy mediametrics::Item @ %p", this);
Ray Essickb5fac8e2016-12-12 11:33:56 -080095 }
Ray Essickb5fac8e2016-12-12 11:33:56 -080096}
97
Ray Essickf27e9872019-12-07 06:28:46 -080098mediametrics::Item &mediametrics::Item::setTimestamp(nsecs_t ts) {
Ray Essick3938dc62016-11-01 08:56:56 -070099 mTimestamp = ts;
100 return *this;
101}
102
Ray Essickf27e9872019-12-07 06:28:46 -0800103nsecs_t mediametrics::Item::getTimestamp() const {
Ray Essick3938dc62016-11-01 08:56:56 -0700104 return mTimestamp;
105}
106
Ray Essickf27e9872019-12-07 06:28:46 -0800107mediametrics::Item &mediametrics::Item::setPid(pid_t pid) {
Ray Essick3938dc62016-11-01 08:56:56 -0700108 mPid = pid;
109 return *this;
110}
111
Ray Essickf27e9872019-12-07 06:28:46 -0800112pid_t mediametrics::Item::getPid() const {
Ray Essick3938dc62016-11-01 08:56:56 -0700113 return mPid;
114}
115
Ray Essickf27e9872019-12-07 06:28:46 -0800116mediametrics::Item &mediametrics::Item::setUid(uid_t uid) {
Ray Essick3938dc62016-11-01 08:56:56 -0700117 mUid = uid;
118 return *this;
119}
120
Ray Essickf27e9872019-12-07 06:28:46 -0800121uid_t mediametrics::Item::getUid() const {
Ray Essick3938dc62016-11-01 08:56:56 -0700122 return mUid;
123}
124
Ray Essickf27e9872019-12-07 06:28:46 -0800125mediametrics::Item &mediametrics::Item::setPkgName(const std::string &pkgName) {
Ray Essickf65f4212017-08-31 11:41:19 -0700126 mPkgName = pkgName;
127 return *this;
128}
129
Ray Essickf27e9872019-12-07 06:28:46 -0800130mediametrics::Item &mediametrics::Item::setPkgVersionCode(int64_t pkgVersionCode) {
Ray Essickf65f4212017-08-31 11:41:19 -0700131 mPkgVersionCode = pkgVersionCode;
132 return *this;
133}
134
Ray Essickf27e9872019-12-07 06:28:46 -0800135int64_t mediametrics::Item::getPkgVersionCode() const {
Ray Essickf65f4212017-08-31 11:41:19 -0700136 return mPkgVersionCode;
137}
138
Ray Essick3938dc62016-11-01 08:56:56 -0700139// remove indicated keys and their values
140// return value is # keys removed
Ray Essickf27e9872019-12-07 06:28:46 -0800141size_t mediametrics::Item::filter(size_t n, const char *attrs[]) {
Andy Hung3253f2d2019-10-21 14:50:07 -0700142 size_t zapped = 0;
143 for (size_t i = 0; i < n; ++i) {
Andy Hung47e58d62019-12-06 18:40:19 -0800144 zapped += mProps.erase(attrs[i]);
Ray Essick3938dc62016-11-01 08:56:56 -0700145 }
146 return zapped;
147}
148
149// remove any keys NOT in the provided list
150// return value is # keys removed
Ray Essickf27e9872019-12-07 06:28:46 -0800151size_t mediametrics::Item::filterNot(size_t n, const char *attrs[]) {
Andy Hung3253f2d2019-10-21 14:50:07 -0700152 std::set<std::string> check(attrs, attrs + n);
153 size_t zapped = 0;
Andy Hung47e58d62019-12-06 18:40:19 -0800154 for (auto it = mProps.begin(); it != mProps.end();) {
155 if (check.find(it->first) != check.end()) {
156 ++it;
Andy Hung3253f2d2019-10-21 14:50:07 -0700157 } else {
Andy Hung47e58d62019-12-06 18:40:19 -0800158 it = mProps.erase(it);
159 ++zapped;
Ray Essick3938dc62016-11-01 08:56:56 -0700160 }
161 }
162 return zapped;
163}
164
Ray Essick3938dc62016-11-01 08:56:56 -0700165// Parcel / serialize things for binder calls
166//
167
Ray Essickf27e9872019-12-07 06:28:46 -0800168status_t mediametrics::Item::readFromParcel(const Parcel& data) {
Andy Hung3253f2d2019-10-21 14:50:07 -0700169 int32_t version;
170 status_t status = data.readInt32(&version);
171 if (status != NO_ERROR) return status;
Ray Essickba8c4842019-01-18 11:35:33 -0800172
Andy Hung3253f2d2019-10-21 14:50:07 -0700173 switch (version) {
174 case 0:
175 return readFromParcel0(data);
176 default:
177 ALOGE("%s: unsupported parcel version: %d", __func__, version);
178 return INVALID_OPERATION;
Ray Essickba8c4842019-01-18 11:35:33 -0800179 }
180}
181
Ray Essickf27e9872019-12-07 06:28:46 -0800182status_t mediametrics::Item::readFromParcel0(const Parcel& data) {
Andy Hung3253f2d2019-10-21 14:50:07 -0700183 const char *s = data.readCString();
184 mKey = s == nullptr ? "" : s;
185 int32_t pid, uid;
186 status_t status = data.readInt32(&pid) ?: data.readInt32(&uid);
187 if (status != NO_ERROR) return status;
188 mPid = (pid_t)pid;
189 mUid = (uid_t)uid;
190 s = data.readCString();
191 mPkgName = s == nullptr ? "" : s;
192 int32_t count;
193 int64_t version, timestamp;
194 status = data.readInt64(&version) ?: data.readInt64(&timestamp) ?: data.readInt32(&count);
195 if (status != NO_ERROR) return status;
196 if (count < 0) return BAD_VALUE;
197 mPkgVersionCode = version;
198 mTimestamp = timestamp;
Andy Hung47e58d62019-12-06 18:40:19 -0800199 for (int i = 0; i < count; i++) {
200 Prop prop;
201 status_t status = prop.readFromParcel(data);
Andy Hung3253f2d2019-10-21 14:50:07 -0700202 if (status != NO_ERROR) return status;
Andy Hung47e58d62019-12-06 18:40:19 -0800203 mProps[prop.getName()] = std::move(prop);
Ray Essick3938dc62016-11-01 08:56:56 -0700204 }
Andy Hung3253f2d2019-10-21 14:50:07 -0700205 return NO_ERROR;
Ray Essick3938dc62016-11-01 08:56:56 -0700206}
207
Ray Essickf27e9872019-12-07 06:28:46 -0800208status_t mediametrics::Item::writeToParcel(Parcel *data) const {
Andy Hung3253f2d2019-10-21 14:50:07 -0700209 if (data == nullptr) return BAD_VALUE;
Ray Essickba8c4842019-01-18 11:35:33 -0800210
Andy Hung3253f2d2019-10-21 14:50:07 -0700211 const int32_t version = 0;
212 status_t status = data->writeInt32(version);
213 if (status != NO_ERROR) return status;
Ray Essick3938dc62016-11-01 08:56:56 -0700214
Andy Hung3253f2d2019-10-21 14:50:07 -0700215 switch (version) {
216 case 0:
217 return writeToParcel0(data);
218 default:
219 ALOGE("%s: unsupported parcel version: %d", __func__, version);
220 return INVALID_OPERATION;
Ray Essickba8c4842019-01-18 11:35:33 -0800221 }
222}
223
Ray Essickf27e9872019-12-07 06:28:46 -0800224status_t mediametrics::Item::writeToParcel0(Parcel *data) const {
Andy Hung3253f2d2019-10-21 14:50:07 -0700225 status_t status =
226 data->writeCString(mKey.c_str())
227 ?: data->writeInt32(mPid)
228 ?: data->writeInt32(mUid)
229 ?: data->writeCString(mPkgName.c_str())
230 ?: data->writeInt64(mPkgVersionCode)
231 ?: data->writeInt64(mTimestamp);
232 if (status != NO_ERROR) return status;
Ray Essick3938dc62016-11-01 08:56:56 -0700233
Andy Hung47e58d62019-12-06 18:40:19 -0800234 data->writeInt32((int32_t)mProps.size());
235 for (auto &prop : *this) {
236 status = prop.writeToParcel(data);
Andy Hung3253f2d2019-10-21 14:50:07 -0700237 if (status != NO_ERROR) return status;
Ray Essick3938dc62016-11-01 08:56:56 -0700238 }
Andy Hung3253f2d2019-10-21 14:50:07 -0700239 return NO_ERROR;
Ray Essick3938dc62016-11-01 08:56:56 -0700240}
241
Ray Essickf27e9872019-12-07 06:28:46 -0800242const char *mediametrics::Item::toCString() {
Andy Hung3b4c1f02020-01-23 18:58:32 -0800243 std::string val = toString();
Ray Essick20147322018-11-17 09:08:39 -0800244 return strdup(val.c_str());
245}
246
Andy Hung3b4c1f02020-01-23 18:58:32 -0800247/*
248 * Similar to audio_utils/clock.h but customized for displaying mediametrics time.
249 */
250
251void nsToString(int64_t ns, char *buffer, size_t bufferSize, PrintFormat format)
252{
253 if (bufferSize == 0) return;
254
255 const int one_second = 1000000000;
256 const time_t sec = ns / one_second;
257 struct tm tm;
258
259 // Supported on bionic, glibc, and macOS, but not mingw.
260 if (localtime_r(&sec, &tm) == NULL) {
261 buffer[0] = '\0';
262 return;
263 }
264
265 switch (format) {
266 default:
267 case kPrintFormatLong:
268 if (snprintf(buffer, bufferSize, "%02d-%02d %02d:%02d:%02d.%03d",
269 tm.tm_mon + 1, // localtime_r uses months in 0 - 11 range
270 tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
271 (int)(ns % one_second / 1000000)) < 0) {
272 buffer[0] = '\0'; // null terminate on format error, which should not happen
273 }
274 break;
275 case kPrintFormatShort:
276 if (snprintf(buffer, bufferSize, "%02d:%02d:%02d.%03d",
277 tm.tm_hour, tm.tm_min, tm.tm_sec,
278 (int)(ns % one_second / 1000000)) < 0) {
279 buffer[0] = '\0'; // null terminate on format error, which should not happen
280 }
281 break;
282 }
Ray Essickf65f4212017-08-31 11:41:19 -0700283}
Ray Essick3938dc62016-11-01 08:56:56 -0700284
Andy Hung3b4c1f02020-01-23 18:58:32 -0800285std::string mediametrics::Item::toString() const {
Ray Essick783bd0d2018-01-11 11:10:35 -0800286 std::string result;
Andy Hung1efc9c62019-12-03 13:43:33 -0800287 char buffer[kMaxPropertyStringSize];
Ray Essick3938dc62016-11-01 08:56:56 -0700288
Andy Hung3b4c1f02020-01-23 18:58:32 -0800289 snprintf(buffer, sizeof(buffer), "{%s, (%s), (%s, %d, %d)",
290 mKey.c_str(),
291 timeStringFromNs(mTimestamp, kPrintFormatLong).time,
292 mPkgName.c_str(), mPid, mUid
293 );
Ray Essickf65f4212017-08-31 11:41:19 -0700294 result.append(buffer);
Andy Hung3b4c1f02020-01-23 18:58:32 -0800295 bool first = true;
Andy Hung47e58d62019-12-06 18:40:19 -0800296 for (auto &prop : *this) {
Andy Hungb7aadb32019-12-09 19:40:42 -0800297 prop.toStringBuffer(buffer, sizeof(buffer));
Andy Hung3b4c1f02020-01-23 18:58:32 -0800298 result += first ? ", (" : ", ";
299 result += buffer;
300 first = false;
Ray Essick3938dc62016-11-01 08:56:56 -0700301 }
Andy Hung3b4c1f02020-01-23 18:58:32 -0800302 result.append(")}");
Ray Essick3938dc62016-11-01 08:56:56 -0700303 return result;
304}
305
306// for the lazy, we offer methods that finds the service and
307// calls the appropriate daemon
Ray Essickf27e9872019-12-07 06:28:46 -0800308bool mediametrics::Item::selfrecord() {
Andy Hunga87e69c2019-10-18 10:07:40 -0700309 ALOGD_IF(DEBUG_API, "%s: delivering %s", __func__, this->toString().c_str());
Andy Hung49ca44e2020-11-10 22:14:58 -0800310
311 char *str;
312 size_t size;
313 status_t status = writeToByteString(&str, &size);
314 if (status == NO_ERROR) {
315 status = submitBuffer(str, size);
George Burgess IV14dadb12021-02-03 14:04:09 -0800316 free(str);
Andy Hung49ca44e2020-11-10 22:14:58 -0800317 }
318 if (status != NO_ERROR) {
319 ALOGW("%s: failed to record: %s", __func__, this->toString().c_str());
Ray Essick3938dc62016-11-01 08:56:56 -0700320 return false;
321 }
Andy Hung49ca44e2020-11-10 22:14:58 -0800322 return true;
Ray Essick3938dc62016-11-01 08:56:56 -0700323}
324
Ray Essick3938dc62016-11-01 08:56:56 -0700325//static
Andy Hung1efc9c62019-12-03 13:43:33 -0800326bool BaseItem::isEnabled() {
Andy Hunga87e69c2019-10-18 10:07:40 -0700327 // completely skip logging from certain UIDs. We do this here
328 // to avoid the multi-second timeouts while we learn that
329 // sepolicy will not let us find the service.
330 // We do this only for a select set of UIDs
331 // The sepolicy protection is still in place, we just want a faster
332 // response from this specific, small set of uids.
Ray Essick3938dc62016-11-01 08:56:56 -0700333
Andy Hunga87e69c2019-10-18 10:07:40 -0700334 // This is checked only once in the lifetime of the process.
335 const uid_t uid = getuid();
336 switch (uid) {
337 case AID_RADIO: // telephony subsystem, RIL
338 return false;
Ahaan Ugaleb5aee612021-05-26 14:50:13 -0700339 default:
340 // Some isolated processes can access the audio system; see
341 // AudioSystem::setAudioFlingerBinder (currently only the HotwordDetectionService). Instead
342 // of also allowing access to the MediaMetrics service, it's simpler to just disable it for
343 // now.
344 // TODO(b/190151205): Either allow the HotwordDetectionService to access MediaMetrics or
345 // make this disabling specific to that process.
346 if (uid >= AID_ISOLATED_START && uid <= AID_ISOLATED_END) {
347 return false;
348 }
349 break;
Andy Hunga87e69c2019-10-18 10:07:40 -0700350 }
351
Ray Essickf27e9872019-12-07 06:28:46 -0800352 int enabled = property_get_int32(Item::EnabledProperty, -1);
Ray Essick3938dc62016-11-01 08:56:56 -0700353 if (enabled == -1) {
Ray Essickf27e9872019-12-07 06:28:46 -0800354 enabled = property_get_int32(Item::EnabledPropertyPersist, -1);
Ray Essick3938dc62016-11-01 08:56:56 -0700355 }
356 if (enabled == -1) {
Ray Essickf27e9872019-12-07 06:28:46 -0800357 enabled = Item::EnabledProperty_default;
Ray Essick3938dc62016-11-01 08:56:56 -0700358 }
Andy Hunga87e69c2019-10-18 10:07:40 -0700359 return enabled > 0;
Ray Essick3938dc62016-11-01 08:56:56 -0700360}
361
Ray Essick2ab3c432017-10-02 09:29:49 -0700362// monitor health of our connection to the metrics service
363class MediaMetricsDeathNotifier : public IBinder::DeathRecipient {
364 virtual void binderDied(const wp<IBinder> &) {
365 ALOGW("Reacquire service connection on next request");
Andy Hung1efc9c62019-12-03 13:43:33 -0800366 BaseItem::dropInstance();
Ray Essick2ab3c432017-10-02 09:29:49 -0700367 }
368};
369
Andy Hunga87e69c2019-10-18 10:07:40 -0700370static sp<MediaMetricsDeathNotifier> sNotifier;
371// static
Andy Hung49ca44e2020-11-10 22:14:58 -0800372sp<media::IMediaMetricsService> BaseItem::sMediaMetricsService;
Andy Hunga87e69c2019-10-18 10:07:40 -0700373static std::mutex sServiceMutex;
374static int sRemainingBindAttempts = SVC_TRIES;
Ray Essick2ab3c432017-10-02 09:29:49 -0700375
376// static
Andy Hung1efc9c62019-12-03 13:43:33 -0800377void BaseItem::dropInstance() {
Andy Hunga87e69c2019-10-18 10:07:40 -0700378 std::lock_guard _l(sServiceMutex);
379 sRemainingBindAttempts = SVC_TRIES;
Ray Essickf27e9872019-12-07 06:28:46 -0800380 sMediaMetricsService = nullptr;
Ray Essick2ab3c432017-10-02 09:29:49 -0700381}
382
Andy Hung1efc9c62019-12-03 13:43:33 -0800383// static
Andy Hung49ca44e2020-11-10 22:14:58 -0800384status_t BaseItem::submitBuffer(const char *buffer, size_t size) {
Andy Hung1efc9c62019-12-03 13:43:33 -0800385 ALOGD_IF(DEBUG_API, "%s: delivering %zu bytes", __func__, size);
Andy Hung49ca44e2020-11-10 22:14:58 -0800386
387 // Validate size
388 if (size > std::numeric_limits<int32_t>::max()) return BAD_VALUE;
389
390 // Do we have the service available?
391 sp<media::IMediaMetricsService> svc = getService();
392 if (svc == nullptr) return NO_INIT;
393
394 ::android::status_t status = NO_ERROR;
395 if constexpr (/* DISABLES CODE */ (false)) {
396 // THIS PATH IS FOR REFERENCE ONLY.
397 // It is compiled so that any changes to IMediaMetricsService::submitBuffer()
398 // will lead here. If this code is changed, the else branch must
399 // be changed as well.
400 //
401 // Use the AIDL calling interface - this is a bit slower as a byte vector must be
402 // constructed. As the call is one-way, the only a transaction error occurs.
403 status = svc->submitBuffer({buffer, buffer + size}).transactionError();
404 } else {
405 // Use the Binder calling interface - this direct implementation avoids
406 // malloc/copy/free for the vector and reduces the overhead for logging.
407 // We based this off of the AIDL generated file:
408 // out/soong/.intermediates/frameworks/av/media/libmediametrics/mediametricsservice-aidl-unstable-cpp-source/gen/android/media/IMediaMetricsService.cpp
409 // TODO: Create an AIDL C++ back end optimized form of vector writing.
410 ::android::Parcel _aidl_data;
411 ::android::Parcel _aidl_reply; // we don't care about this as it is one-way.
412
413 status = _aidl_data.writeInterfaceToken(svc->getInterfaceDescriptor());
414 if (status != ::android::OK) goto _aidl_error;
415
416 status = _aidl_data.writeInt32(static_cast<int32_t>(size));
417 if (status != ::android::OK) goto _aidl_error;
418
419 status = _aidl_data.write(buffer, static_cast<int32_t>(size));
420 if (status != ::android::OK) goto _aidl_error;
421
422 status = ::android::IInterface::asBinder(svc)->transact(
423 ::android::media::BnMediaMetricsService::TRANSACTION_submitBuffer,
424 _aidl_data, &_aidl_reply, ::android::IBinder::FLAG_ONEWAY);
425
426 // AIDL permits setting a default implementation for additional functionality.
427 // See go/aog/713984. This is not used here.
428 // if (status == ::android::UNKNOWN_TRANSACTION
429 // && ::android::media::IMediaMetricsService::getDefaultImpl()) {
430 // status = ::android::media::IMediaMetricsService::getDefaultImpl()
431 // ->submitBuffer(immutableByteVectorFromBuffer(buffer, size))
432 // .transactionError();
433 // }
Andy Hung1efc9c62019-12-03 13:43:33 -0800434 }
Andy Hung49ca44e2020-11-10 22:14:58 -0800435
436 if (status == NO_ERROR) return NO_ERROR;
437
438 _aidl_error:
439 ALOGW("%s: failed(%d) to record: %zu bytes", __func__, status, size);
440 return status;
Andy Hung1efc9c62019-12-03 13:43:33 -0800441}
442
Ray Essick3938dc62016-11-01 08:56:56 -0700443//static
Andy Hung49ca44e2020-11-10 22:14:58 -0800444sp<media::IMediaMetricsService> BaseItem::getService() {
Ray Essickd38e1742017-01-23 15:17:06 -0800445 static const char *servicename = "media.metrics";
Andy Hunga87e69c2019-10-18 10:07:40 -0700446 static const bool enabled = isEnabled(); // singleton initialized
Ray Essick3938dc62016-11-01 08:56:56 -0700447
448 if (enabled == false) {
Andy Hunga87e69c2019-10-18 10:07:40 -0700449 ALOGD_IF(DEBUG_SERVICEACCESS, "disabled");
450 return nullptr;
Ray Essick3938dc62016-11-01 08:56:56 -0700451 }
Andy Hunga87e69c2019-10-18 10:07:40 -0700452 std::lock_guard _l(sServiceMutex);
453 // think of remainingBindAttempts as telling us whether service == nullptr because
454 // (1) we haven't tried to initialize it yet
455 // (2) we've tried to initialize it, but failed.
Ray Essickf27e9872019-12-07 06:28:46 -0800456 if (sMediaMetricsService == nullptr && sRemainingBindAttempts > 0) {
Ray Essick3938dc62016-11-01 08:56:56 -0700457 const char *badness = "";
Andy Hunga87e69c2019-10-18 10:07:40 -0700458 sp<IServiceManager> sm = defaultServiceManager();
459 if (sm != nullptr) {
460 sp<IBinder> binder = sm->getService(String16(servicename));
461 if (binder != nullptr) {
Andy Hung49ca44e2020-11-10 22:14:58 -0800462 sMediaMetricsService = interface_cast<media::IMediaMetricsService>(binder);
Andy Hunga87e69c2019-10-18 10:07:40 -0700463 sNotifier = new MediaMetricsDeathNotifier();
464 binder->linkToDeath(sNotifier);
Ray Essick3938dc62016-11-01 08:56:56 -0700465 } else {
Andy Hunga87e69c2019-10-18 10:07:40 -0700466 badness = "did not find service";
Ray Essick3938dc62016-11-01 08:56:56 -0700467 }
Andy Hunga87e69c2019-10-18 10:07:40 -0700468 } else {
469 badness = "No Service Manager access";
Ray Essick3938dc62016-11-01 08:56:56 -0700470 }
Ray Essickf27e9872019-12-07 06:28:46 -0800471 if (sMediaMetricsService == nullptr) {
Andy Hunga87e69c2019-10-18 10:07:40 -0700472 if (sRemainingBindAttempts > 0) {
473 sRemainingBindAttempts--;
474 }
475 ALOGD_IF(DEBUG_SERVICEACCESS, "%s: unable to bind to service %s: %s",
476 __func__, servicename, badness);
477 }
Ray Essick3938dc62016-11-01 08:56:56 -0700478 }
Ray Essickf27e9872019-12-07 06:28:46 -0800479 return sMediaMetricsService;
Ray Essick3938dc62016-11-01 08:56:56 -0700480}
481
Andy Hung1efc9c62019-12-03 13:43:33 -0800482
Ray Essickf27e9872019-12-07 06:28:46 -0800483status_t mediametrics::Item::writeToByteString(char **pbuffer, size_t *plength) const
Andy Hung3253f2d2019-10-21 14:50:07 -0700484{
485 if (pbuffer == nullptr || plength == nullptr)
486 return BAD_VALUE;
487
488 // get size
489 const size_t keySizeZeroTerminated = strlen(mKey.c_str()) + 1;
490 if (keySizeZeroTerminated > UINT16_MAX) {
491 ALOGW("%s: key size %zu too large", __func__, keySizeZeroTerminated);
492 return INVALID_OPERATION;
493 }
494 const uint16_t version = 0;
Andy Hung1efc9c62019-12-03 13:43:33 -0800495 const uint32_t header_size =
496 sizeof(uint32_t) // total size
497 + sizeof(header_size) // header size
498 + sizeof(version) // encoding version
499 + sizeof(uint16_t) // key size
Andy Hung3253f2d2019-10-21 14:50:07 -0700500 + keySizeZeroTerminated // key, zero terminated
Andy Hung1efc9c62019-12-03 13:43:33 -0800501 + sizeof(int32_t) // pid
502 + sizeof(int32_t) // uid
503 + sizeof(int64_t) // timestamp
Andy Hung3253f2d2019-10-21 14:50:07 -0700504 ;
505
Andy Hung1efc9c62019-12-03 13:43:33 -0800506 uint32_t size = header_size
Andy Hung3253f2d2019-10-21 14:50:07 -0700507 + sizeof(uint32_t) // # properties
508 ;
Andy Hung47e58d62019-12-06 18:40:19 -0800509 for (auto &prop : *this) {
510 const size_t propSize = prop.getByteStringSize();
Andy Hung1efc9c62019-12-03 13:43:33 -0800511 if (propSize > UINT16_MAX) {
Andy Hung47e58d62019-12-06 18:40:19 -0800512 ALOGW("%s: prop %s size %zu too large", __func__, prop.getName(), propSize);
Andy Hung3253f2d2019-10-21 14:50:07 -0700513 return INVALID_OPERATION;
514 }
Andy Hung1efc9c62019-12-03 13:43:33 -0800515 if (__builtin_add_overflow(size, propSize, &size)) {
Andy Hung47e58d62019-12-06 18:40:19 -0800516 ALOGW("%s: item size overflow at property %s", __func__, prop.getName());
Andy Hung1efc9c62019-12-03 13:43:33 -0800517 return INVALID_OPERATION;
518 }
Andy Hung3253f2d2019-10-21 14:50:07 -0700519 }
520
Andy Hung1efc9c62019-12-03 13:43:33 -0800521 // since we fill every byte in the buffer (there is no padding),
522 // malloc is used here instead of calloc.
523 char * const build = (char *)malloc(size);
Andy Hung3253f2d2019-10-21 14:50:07 -0700524 if (build == nullptr) return NO_MEMORY;
525
526 char *filling = build;
Andy Hung1efc9c62019-12-03 13:43:33 -0800527 char *buildmax = build + size;
528 if (insert((uint32_t)size, &filling, buildmax) != NO_ERROR
529 || insert(header_size, &filling, buildmax) != NO_ERROR
Andy Hung3253f2d2019-10-21 14:50:07 -0700530 || insert(version, &filling, buildmax) != NO_ERROR
531 || insert((uint16_t)keySizeZeroTerminated, &filling, buildmax) != NO_ERROR
532 || insert(mKey.c_str(), &filling, buildmax) != NO_ERROR
533 || insert((int32_t)mPid, &filling, buildmax) != NO_ERROR
534 || insert((int32_t)mUid, &filling, buildmax) != NO_ERROR
535 || insert((int64_t)mTimestamp, &filling, buildmax) != NO_ERROR
Andy Hung47e58d62019-12-06 18:40:19 -0800536 || insert((uint32_t)mProps.size(), &filling, buildmax) != NO_ERROR) {
Andy Hung1efc9c62019-12-03 13:43:33 -0800537 ALOGE("%s:could not write header", __func__); // shouldn't happen
Andy Hung3253f2d2019-10-21 14:50:07 -0700538 free(build);
539 return INVALID_OPERATION;
540 }
Andy Hung47e58d62019-12-06 18:40:19 -0800541 for (auto &prop : *this) {
542 if (prop.writeToByteString(&filling, buildmax) != NO_ERROR) {
Andy Hung3253f2d2019-10-21 14:50:07 -0700543 free(build);
Andy Hung1efc9c62019-12-03 13:43:33 -0800544 // shouldn't happen
Andy Hung47e58d62019-12-06 18:40:19 -0800545 ALOGE("%s:could not write prop %s", __func__, prop.getName());
Andy Hung3253f2d2019-10-21 14:50:07 -0700546 return INVALID_OPERATION;
547 }
548 }
549
550 if (filling != buildmax) {
Andy Hung1efc9c62019-12-03 13:43:33 -0800551 ALOGE("%s: problems populating; wrote=%d planned=%d",
552 __func__, (int)(filling - build), (int)size);
Andy Hung3253f2d2019-10-21 14:50:07 -0700553 free(build);
554 return INVALID_OPERATION;
555 }
556 *pbuffer = build;
Andy Hung1efc9c62019-12-03 13:43:33 -0800557 *plength = size;
Andy Hung3253f2d2019-10-21 14:50:07 -0700558 return NO_ERROR;
559}
560
Ray Essickf27e9872019-12-07 06:28:46 -0800561status_t mediametrics::Item::readFromByteString(const char *bufferptr, size_t length)
Andy Hung3253f2d2019-10-21 14:50:07 -0700562{
563 if (bufferptr == nullptr) return BAD_VALUE;
564
565 const char *read = bufferptr;
566 const char *readend = bufferptr + length;
567
Andy Hung1efc9c62019-12-03 13:43:33 -0800568 uint32_t size;
569 uint32_t header_size;
570 uint16_t version;
571 uint16_t key_size;
Andy Hungb7aadb32019-12-09 19:40:42 -0800572 std::string key;
Andy Hung3253f2d2019-10-21 14:50:07 -0700573 int32_t pid;
574 int32_t uid;
575 int64_t timestamp;
576 uint32_t propCount;
Andy Hung1efc9c62019-12-03 13:43:33 -0800577 if (extract(&size, &read, readend) != NO_ERROR
578 || extract(&header_size, &read, readend) != NO_ERROR
Andy Hung3253f2d2019-10-21 14:50:07 -0700579 || extract(&version, &read, readend) != NO_ERROR
Andy Hung1efc9c62019-12-03 13:43:33 -0800580 || extract(&key_size, &read, readend) != NO_ERROR
Andy Hung3253f2d2019-10-21 14:50:07 -0700581 || extract(&key, &read, readend) != NO_ERROR
582 || extract(&pid, &read, readend) != NO_ERROR
583 || extract(&uid, &read, readend) != NO_ERROR
584 || extract(&timestamp, &read, readend) != NO_ERROR
Andy Hung1efc9c62019-12-03 13:43:33 -0800585 || size > length
Andy Hungb7aadb32019-12-09 19:40:42 -0800586 || key.size() + 1 != key_size
Andy Hung1efc9c62019-12-03 13:43:33 -0800587 || header_size > size) {
Andy Hung1efc9c62019-12-03 13:43:33 -0800588 ALOGW("%s: invalid header", __func__);
Andy Hung3253f2d2019-10-21 14:50:07 -0700589 return INVALID_OPERATION;
590 }
Andy Hungb7aadb32019-12-09 19:40:42 -0800591 mKey = std::move(key);
Andy Hung3253f2d2019-10-21 14:50:07 -0700592 const size_t pos = read - bufferptr;
Andy Hung1efc9c62019-12-03 13:43:33 -0800593 if (pos > header_size) {
594 ALOGW("%s: invalid header pos:%zu > header_size:%u",
595 __func__, pos, header_size);
Andy Hung3253f2d2019-10-21 14:50:07 -0700596 return INVALID_OPERATION;
Andy Hung1efc9c62019-12-03 13:43:33 -0800597 } else if (pos < header_size) {
598 ALOGW("%s: mismatched header pos:%zu < header_size:%u, advancing",
599 __func__, pos, header_size);
600 read += (header_size - pos);
Andy Hung3253f2d2019-10-21 14:50:07 -0700601 }
602 if (extract(&propCount, &read, readend) != NO_ERROR) {
603 ALOGD("%s: cannot read prop count", __func__);
604 return INVALID_OPERATION;
605 }
606 mPid = pid;
607 mUid = uid;
608 mTimestamp = timestamp;
609 for (size_t i = 0; i < propCount; ++i) {
Andy Hung47e58d62019-12-06 18:40:19 -0800610 Prop prop;
611 if (prop.readFromByteString(&read, readend) != NO_ERROR) {
Andy Hung1efc9c62019-12-03 13:43:33 -0800612 ALOGW("%s: cannot read prop %zu", __func__, i);
Andy Hung3253f2d2019-10-21 14:50:07 -0700613 return INVALID_OPERATION;
614 }
Andy Hung47e58d62019-12-06 18:40:19 -0800615 mProps[prop.getName()] = std::move(prop);
Andy Hung3253f2d2019-10-21 14:50:07 -0700616 }
617 return NO_ERROR;
618}
619
Ray Essickf27e9872019-12-07 06:28:46 -0800620status_t mediametrics::Item::Prop::readFromParcel(const Parcel& data)
Andy Hung3253f2d2019-10-21 14:50:07 -0700621{
622 const char *key = data.readCString();
623 if (key == nullptr) return BAD_VALUE;
624 int32_t type;
625 status_t status = data.readInt32(&type);
626 if (status != NO_ERROR) return status;
627 switch (type) {
Andy Hungb7aadb32019-12-09 19:40:42 -0800628 case mediametrics::kTypeInt32: {
629 int32_t value;
630 status = data.readInt32(&value);
631 if (status != NO_ERROR) return status;
632 mElem = value;
633 } break;
634 case mediametrics::kTypeInt64: {
635 int64_t value;
636 status = data.readInt64(&value);
637 if (status != NO_ERROR) return status;
638 mElem = value;
639 } break;
640 case mediametrics::kTypeDouble: {
641 double value;
642 status = data.readDouble(&value);
643 if (status != NO_ERROR) return status;
644 mElem = value;
645 } break;
Andy Hung47e58d62019-12-06 18:40:19 -0800646 case mediametrics::kTypeCString: {
Andy Hung3253f2d2019-10-21 14:50:07 -0700647 const char *s = data.readCString();
648 if (s == nullptr) return BAD_VALUE;
Andy Hungb7aadb32019-12-09 19:40:42 -0800649 mElem = s;
650 } break;
Andy Hung47e58d62019-12-06 18:40:19 -0800651 case mediametrics::kTypeRate: {
Andy Hung3253f2d2019-10-21 14:50:07 -0700652 std::pair<int64_t, int64_t> rate;
653 status = data.readInt64(&rate.first)
654 ?: data.readInt64(&rate.second);
Andy Hungb7aadb32019-12-09 19:40:42 -0800655 if (status != NO_ERROR) return status;
656 mElem = rate;
657 } break;
658 case mediametrics::kTypeNone: {
659 mElem = std::monostate{};
660 } break;
Andy Hung3253f2d2019-10-21 14:50:07 -0700661 default:
Andy Hungb7aadb32019-12-09 19:40:42 -0800662 ALOGE("%s: reading bad item type: %d", __func__, type);
Andy Hung3253f2d2019-10-21 14:50:07 -0700663 return BAD_VALUE;
664 }
Andy Hungb7aadb32019-12-09 19:40:42 -0800665 setName(key);
666 return NO_ERROR;
Andy Hung3253f2d2019-10-21 14:50:07 -0700667}
668
Ray Essickf27e9872019-12-07 06:28:46 -0800669status_t mediametrics::Item::Prop::readFromByteString(
Andy Hung3253f2d2019-10-21 14:50:07 -0700670 const char **bufferpptr, const char *bufferptrmax)
671{
672 uint16_t len;
Andy Hungb7aadb32019-12-09 19:40:42 -0800673 std::string name;
Andy Hung3253f2d2019-10-21 14:50:07 -0700674 uint8_t type;
675 status_t status = extract(&len, bufferpptr, bufferptrmax)
676 ?: extract(&type, bufferpptr, bufferptrmax)
677 ?: extract(&name, bufferpptr, bufferptrmax);
678 if (status != NO_ERROR) return status;
Andy Hungb7aadb32019-12-09 19:40:42 -0800679 switch (type) {
680 case mediametrics::kTypeInt32: {
681 int32_t value;
682 status = extract(&value, bufferpptr, bufferptrmax);
683 if (status != NO_ERROR) return status;
684 mElem = value;
685 } break;
686 case mediametrics::kTypeInt64: {
687 int64_t value;
688 status = extract(&value, bufferpptr, bufferptrmax);
689 if (status != NO_ERROR) return status;
690 mElem = value;
691 } break;
692 case mediametrics::kTypeDouble: {
693 double value;
694 status = extract(&value, bufferpptr, bufferptrmax);
695 if (status != NO_ERROR) return status;
696 mElem = value;
697 } break;
698 case mediametrics::kTypeRate: {
699 std::pair<int64_t, int64_t> value;
700 status = extract(&value.first, bufferpptr, bufferptrmax)
701 ?: extract(&value.second, bufferpptr, bufferptrmax);
702 if (status != NO_ERROR) return status;
703 mElem = value;
704 } break;
705 case mediametrics::kTypeCString: {
706 std::string value;
707 status = extract(&value, bufferpptr, bufferptrmax);
708 if (status != NO_ERROR) return status;
709 mElem = std::move(value);
710 } break;
711 case mediametrics::kTypeNone: {
712 mElem = std::monostate{};
713 } break;
Andy Hung3253f2d2019-10-21 14:50:07 -0700714 default:
Andy Hung3253f2d2019-10-21 14:50:07 -0700715 ALOGE("%s: found bad prop type: %d, name %s",
Andy Hungb7aadb32019-12-09 19:40:42 -0800716 __func__, (int)type, mName.c_str()); // no payload sent
Andy Hung3253f2d2019-10-21 14:50:07 -0700717 return BAD_VALUE;
718 }
Andy Hungb7aadb32019-12-09 19:40:42 -0800719 mName = name;
720 return NO_ERROR;
Andy Hung3253f2d2019-10-21 14:50:07 -0700721}
722
Ray Essickf27e9872019-12-07 06:28:46 -0800723} // namespace android::mediametrics