blob: b8eb26731f26fe71ecd755b817d9fa55d0bb77c2 [file] [log] [blame]
Ray Essick3938dc62016-11-01 08:56:56 -07001/*
Ray Essick2e9c63b2017-03-29 15:16:44 -07002 * Copyright (C) 2017 The Android Open Source Project
Ray Essick3938dc62016-11-01 08:56:56 -07003 *
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
Andy Hung17dbaf22019-10-11 14:06:31 -070017#pragma once
Ray Essick3938dc62016-11-01 08:56:56 -070018
Andy Hung17dbaf22019-10-11 14:06:31 -070019#include <atomic>
20#include <deque>
Ray Essick72a436b2018-06-14 15:08:13 -070021#include <future>
Andy Hung17dbaf22019-10-11 14:06:31 -070022#include <mutex>
23#include <unordered_map>
Ray Essick72a436b2018-06-14 15:08:13 -070024
Ray Essickf27e9872019-12-07 06:28:46 -080025// IMediaMetricsService must include Vector, String16, Errors
Andy Hungf7c14102020-04-18 14:54:08 -070026#include <android-base/thread_annotations.h>
Ray Essickf27e9872019-12-07 06:28:46 -080027#include <media/IMediaMetricsService.h>
Andy Hunga85efab2019-12-23 11:41:29 -080028#include <mediautils/ServiceUtilities.h>
Andy Hung17dbaf22019-10-11 14:06:31 -070029#include <utils/String8.h>
Ray Essick3938dc62016-11-01 08:56:56 -070030
Andy Hung06f3aba2019-12-03 16:36:42 -080031#include "AudioAnalytics.h"
32
Ray Essick3938dc62016-11-01 08:56:56 -070033namespace android {
34
Ray Essickf27e9872019-12-07 06:28:46 -080035class MediaMetricsService : public BnMediaMetricsService
Ray Essick3938dc62016-11-01 08:56:56 -070036{
Andy Hung17dbaf22019-10-11 14:06:31 -070037public:
Ray Essickf27e9872019-12-07 06:28:46 -080038 MediaMetricsService();
39 ~MediaMetricsService() override;
Ray Essick3938dc62016-11-01 08:56:56 -070040
Andy Hungc89c8dc2019-10-16 17:48:21 -070041 /**
Andy Hunga87e69c2019-10-18 10:07:40 -070042 * Submits the indicated record to the mediaanalytics service.
Andy Hungc89c8dc2019-10-16 17:48:21 -070043 *
44 * \param item the item to submit.
Andy Hunga87e69c2019-10-18 10:07:40 -070045 * \return status failure, which is negative on binder transaction failure.
46 * As the transaction is one-way, remote failures will not be reported.
Andy Hungc89c8dc2019-10-16 17:48:21 -070047 */
Ray Essickf27e9872019-12-07 06:28:46 -080048 status_t submit(mediametrics::Item *item) override {
Andy Hunga87e69c2019-10-18 10:07:40 -070049 return submitInternal(item, false /* release */);
50 }
Ray Essick3938dc62016-11-01 08:56:56 -070051
Andy Hung1efc9c62019-12-03 13:43:33 -080052 status_t submitBuffer(const char *buffer, size_t length) override {
Ray Essickf27e9872019-12-07 06:28:46 -080053 mediametrics::Item *item = new mediametrics::Item();
Andy Hung1efc9c62019-12-03 13:43:33 -080054 return item->readFromByteString(buffer, length)
55 ?: submitInternal(item, true /* release */);
56 }
57
Andy Hung17dbaf22019-10-11 14:06:31 -070058 status_t dump(int fd, const Vector<String16>& args) override;
Ray Essick3938dc62016-11-01 08:56:56 -070059
Andy Hung17dbaf22019-10-11 14:06:31 -070060 static constexpr const char * const kServiceName = "media.metrics";
Ray Essick3938dc62016-11-01 08:56:56 -070061
Andy Hung55aaf522019-12-03 15:07:51 -080062 /**
63 * Rounds time to the nearest second.
64 */
65 static nsecs_t roundTime(nsecs_t timeNs);
66
Andy Hunga85efab2019-12-23 11:41:29 -080067 /**
68 * Returns true if we should use uid for package name when uploading to WestWorld.
69 */
70 static bool useUidForPackage(const std::string& package, const std::string& installer);
71
Andy Hungce9b6632020-04-28 20:15:17 -070072 /**
73 * Returns a std::pair of packageName and versionCode for a given uid.
74 *
75 * The value is sanitized - i.e. if the result is not approved to send,
76 * we use the uid as a string and a version code of 0.
77 */
78 static std::pair<std::string, int64_t> getSanitizedPackageNameAndVersionCode(uid_t uid);
79
80 /**
81 * Return string tokens from iterator, separated by spaces and reserved chars.
82 */
83 static std::string tokenizer(std::string::const_iterator& it,
84 const std::string::const_iterator& end, const char *reserved);
85
86 /**
87 * Parse the devices string and return a vector of device address pairs.
88 *
89 * A failure to parse returns early with the contents that were able to be parsed.
90 */
91 static std::vector<std::pair<std::string, std::string>>
92 getDeviceAddressPairs(const std::string &devices);
93
Andy Hunga87e69c2019-10-18 10:07:40 -070094protected:
95
96 // Internal call where release is true if ownership of item is transferred
97 // to the service (that is, the service will eventually delete the item).
Ray Essickf27e9872019-12-07 06:28:46 -080098 status_t submitInternal(mediametrics::Item *item, bool release) override;
Andy Hunga87e69c2019-10-18 10:07:40 -070099
Andy Hung17dbaf22019-10-11 14:06:31 -0700100private:
101 void processExpirations();
Andy Hung17dbaf22019-10-11 14:06:31 -0700102 // input validation after arrival from client
Ray Essickf27e9872019-12-07 06:28:46 -0800103 static bool isContentValid(const mediametrics::Item *item, bool isTrusted);
104 bool isRateLimited(mediametrics::Item *) const;
105 void saveItem(const std::shared_ptr<const mediametrics::Item>& item);
Ray Essick3938dc62016-11-01 08:56:56 -0700106
Andy Hungf7c14102020-04-18 14:54:08 -0700107 bool expirations(const std::shared_ptr<const mediametrics::Item>& item) REQUIRES(mLock);
Ray Essick3938dc62016-11-01 08:56:56 -0700108
Andy Hung17dbaf22019-10-11 14:06:31 -0700109 // support for generating output
Andy Hungf7c14102020-04-18 14:54:08 -0700110 void dumpQueue(String8 &result, int64_t sinceNs, const char* prefix) REQUIRES(mLock);
111 void dumpHeaders(String8 &result, int64_t sinceNs, const char* prefix) REQUIRES(mLock);
Andy Hung17dbaf22019-10-11 14:06:31 -0700112
113 // The following variables accessed without mLock
Ray Essick3938dc62016-11-01 08:56:56 -0700114
Ray Essickf65f4212017-08-31 11:41:19 -0700115 // limit how many records we'll retain
116 // by count (in each queue (open, finalized))
Andy Hung17dbaf22019-10-11 14:06:31 -0700117 const size_t mMaxRecords;
118 // by time (none older than this)
119 const nsecs_t mMaxRecordAgeNs;
Ray Essick72a436b2018-06-14 15:08:13 -0700120 // max to expire per expirations_l() invocation
Andy Hung17dbaf22019-10-11 14:06:31 -0700121 const size_t mMaxRecordsExpiredAtOnce;
Ray Essick3938dc62016-11-01 08:56:56 -0700122
Andy Hung17dbaf22019-10-11 14:06:31 -0700123 std::atomic<int64_t> mItemsSubmitted{}; // accessed outside of lock.
Ray Essickf65f4212017-08-31 11:41:19 -0700124
Andy Hungf7c14102020-04-18 14:54:08 -0700125 mediametrics::AudioAnalytics mAudioAnalytics; // mAudioAnalytics is locked internally.
Andy Hung06f3aba2019-12-03 16:36:42 -0800126
Andy Hung17dbaf22019-10-11 14:06:31 -0700127 std::mutex mLock;
128 // statistics about our analytics
Andy Hungf7c14102020-04-18 14:54:08 -0700129 int64_t mItemsFinalized GUARDED_BY(mLock) = 0;
130 int64_t mItemsDiscarded GUARDED_BY(mLock) = 0;
131 int64_t mItemsDiscardedExpire GUARDED_BY(mLock) = 0;
132 int64_t mItemsDiscardedCount GUARDED_BY(mLock) = 0;
Ray Essickf65f4212017-08-31 11:41:19 -0700133
Andy Hung17dbaf22019-10-11 14:06:31 -0700134 // If we have a worker thread to garbage collect
Andy Hungf7c14102020-04-18 14:54:08 -0700135 std::future<void> mExpireFuture GUARDED_BY(mLock);
Andy Hung17dbaf22019-10-11 14:06:31 -0700136
137 // Our item queue, generally (oldest at front)
138 // TODO: Make separate class, use segmented queue, write lock only end.
139 // Note: Another analytics module might have ownership of an item longer than the log.
Andy Hungf7c14102020-04-18 14:54:08 -0700140 std::deque<std::shared_ptr<const mediametrics::Item>> mItems GUARDED_BY(mLock);
Ray Essick3938dc62016-11-01 08:56:56 -0700141};
142
Andy Hung17dbaf22019-10-11 14:06:31 -0700143} // namespace android