blob: 74a114a783579283f5e22386af248af34c3e94da [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
26#include <media/IMediaMetricsService.h>
Andy Hunga85efab2019-12-23 11:41:29 -080027#include <mediautils/ServiceUtilities.h>
Andy Hung17dbaf22019-10-11 14:06:31 -070028#include <utils/String8.h>
Ray Essick3938dc62016-11-01 08:56:56 -070029
Andy Hung06f3aba2019-12-03 16:36:42 -080030#include "AudioAnalytics.h"
31
Ray Essick3938dc62016-11-01 08:56:56 -070032namespace android {
33
Ray Essickf27e9872019-12-07 06:28:46 -080034class MediaMetricsService : public BnMediaMetricsService
Ray Essick3938dc62016-11-01 08:56:56 -070035{
Andy Hung17dbaf22019-10-11 14:06:31 -070036public:
Ray Essickf27e9872019-12-07 06:28:46 -080037 MediaMetricsService();
38 ~MediaMetricsService() override;
Ray Essick3938dc62016-11-01 08:56:56 -070039
Andy Hungc89c8dc2019-10-16 17:48:21 -070040 /**
Andy Hunga87e69c2019-10-18 10:07:40 -070041 * Submits the indicated record to the mediaanalytics service.
Andy Hungc89c8dc2019-10-16 17:48:21 -070042 *
43 * \param item the item to submit.
Andy Hunga87e69c2019-10-18 10:07:40 -070044 * \return status failure, which is negative on binder transaction failure.
45 * As the transaction is one-way, remote failures will not be reported.
Andy Hungc89c8dc2019-10-16 17:48:21 -070046 */
Ray Essickf27e9872019-12-07 06:28:46 -080047 status_t submit(mediametrics::Item *item) override {
Andy Hunga87e69c2019-10-18 10:07:40 -070048 return submitInternal(item, false /* release */);
49 }
Ray Essick3938dc62016-11-01 08:56:56 -070050
Andy Hung1efc9c62019-12-03 13:43:33 -080051 status_t submitBuffer(const char *buffer, size_t length) override {
Ray Essickf27e9872019-12-07 06:28:46 -080052 mediametrics::Item *item = new mediametrics::Item();
Andy Hung1efc9c62019-12-03 13:43:33 -080053 return item->readFromByteString(buffer, length)
54 ?: submitInternal(item, true /* release */);
55 }
56
Andy Hung17dbaf22019-10-11 14:06:31 -070057 status_t dump(int fd, const Vector<String16>& args) override;
Ray Essick3938dc62016-11-01 08:56:56 -070058
Andy Hung17dbaf22019-10-11 14:06:31 -070059 static constexpr const char * const kServiceName = "media.metrics";
Ray Essick3938dc62016-11-01 08:56:56 -070060
Andy Hung55aaf522019-12-03 15:07:51 -080061 /**
62 * Rounds time to the nearest second.
63 */
64 static nsecs_t roundTime(nsecs_t timeNs);
65
Andy Hunga85efab2019-12-23 11:41:29 -080066 /**
67 * Returns true if we should use uid for package name when uploading to WestWorld.
68 */
69 static bool useUidForPackage(const std::string& package, const std::string& installer);
70
Andy Hunga87e69c2019-10-18 10:07:40 -070071protected:
72
73 // Internal call where release is true if ownership of item is transferred
74 // to the service (that is, the service will eventually delete the item).
Ray Essickf27e9872019-12-07 06:28:46 -080075 status_t submitInternal(mediametrics::Item *item, bool release) override;
Andy Hunga87e69c2019-10-18 10:07:40 -070076
Andy Hung17dbaf22019-10-11 14:06:31 -070077private:
78 void processExpirations();
Andy Hung17dbaf22019-10-11 14:06:31 -070079 // input validation after arrival from client
Ray Essickf27e9872019-12-07 06:28:46 -080080 static bool isContentValid(const mediametrics::Item *item, bool isTrusted);
81 bool isRateLimited(mediametrics::Item *) const;
82 void saveItem(const std::shared_ptr<const mediametrics::Item>& item);
Ray Essick3938dc62016-11-01 08:56:56 -070083
Andy Hung17dbaf22019-10-11 14:06:31 -070084 // The following methods are GUARDED_BY(mLock)
Ray Essickf27e9872019-12-07 06:28:46 -080085 bool expirations_l(const std::shared_ptr<const mediametrics::Item>& item);
Ray Essick3938dc62016-11-01 08:56:56 -070086
Andy Hung17dbaf22019-10-11 14:06:31 -070087 // support for generating output
88 void dumpQueue_l(String8 &result, int dumpProto);
89 void dumpQueue_l(String8 &result, int dumpProto, nsecs_t, const char *only);
90 void dumpHeaders_l(String8 &result, int dumpProto, nsecs_t ts_since);
91 void dumpSummaries_l(String8 &result, int dumpProto, nsecs_t ts_since, const char * only);
92 void dumpRecent_l(String8 &result, int dumpProto, nsecs_t ts_since, const char * only);
93
94 // The following variables accessed without mLock
Ray Essick3938dc62016-11-01 08:56:56 -070095
Ray Essickf65f4212017-08-31 11:41:19 -070096 // limit how many records we'll retain
97 // by count (in each queue (open, finalized))
Andy Hung17dbaf22019-10-11 14:06:31 -070098 const size_t mMaxRecords;
99 // by time (none older than this)
100 const nsecs_t mMaxRecordAgeNs;
Ray Essick72a436b2018-06-14 15:08:13 -0700101 // max to expire per expirations_l() invocation
Andy Hung17dbaf22019-10-11 14:06:31 -0700102 const size_t mMaxRecordsExpiredAtOnce;
103 const int mDumpProtoDefault;
Ray Essick3938dc62016-11-01 08:56:56 -0700104
Andy Hung17dbaf22019-10-11 14:06:31 -0700105 std::atomic<int64_t> mItemsSubmitted{}; // accessed outside of lock.
Ray Essickf65f4212017-08-31 11:41:19 -0700106
Andy Hunga85efab2019-12-23 11:41:29 -0800107 mediautils::UidInfo mUidInfo; // mUidInfo can be accessed without lock (locked internally)
108
Andy Hung06f3aba2019-12-03 16:36:42 -0800109 mediametrics::AudioAnalytics mAudioAnalytics;
110
Andy Hung17dbaf22019-10-11 14:06:31 -0700111 std::mutex mLock;
112 // statistics about our analytics
113 int64_t mItemsFinalized = 0; // GUARDED_BY(mLock)
114 int64_t mItemsDiscarded = 0; // GUARDED_BY(mLock)
115 int64_t mItemsDiscardedExpire = 0; // GUARDED_BY(mLock)
116 int64_t mItemsDiscardedCount = 0; // GUARDED_BY(mLock)
Ray Essickf65f4212017-08-31 11:41:19 -0700117
Andy Hung17dbaf22019-10-11 14:06:31 -0700118 // If we have a worker thread to garbage collect
119 std::future<void> mExpireFuture; // GUARDED_BY(mLock)
120
121 // Our item queue, generally (oldest at front)
122 // TODO: Make separate class, use segmented queue, write lock only end.
123 // Note: Another analytics module might have ownership of an item longer than the log.
Ray Essickf27e9872019-12-07 06:28:46 -0800124 std::deque<std::shared_ptr<const mediametrics::Item>> mItems; // GUARDED_BY(mLock)
Ray Essick3938dc62016-11-01 08:56:56 -0700125};
126
Andy Hung17dbaf22019-10-11 14:06:31 -0700127} // namespace android