blob: 792b7f0879211caab83f90618a1a56d48331b2c2 [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 /**
Muhammad Qureshi087b37c2020-06-16 16:37:36 -070068 * Returns true if we should use uid for package name when uploading to statsd.
Andy Hunga85efab2019-12-23 11:41:29 -080069 */
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
Andy Hunga87e69c2019-10-18 10:07:40 -070080protected:
81
82 // Internal call where release is true if ownership of item is transferred
83 // to the service (that is, the service will eventually delete the item).
Ray Essickf27e9872019-12-07 06:28:46 -080084 status_t submitInternal(mediametrics::Item *item, bool release) override;
Andy Hunga87e69c2019-10-18 10:07:40 -070085
Andy Hung17dbaf22019-10-11 14:06:31 -070086private:
87 void processExpirations();
Andy Hung17dbaf22019-10-11 14:06:31 -070088 // input validation after arrival from client
Ray Essickf27e9872019-12-07 06:28:46 -080089 static bool isContentValid(const mediametrics::Item *item, bool isTrusted);
90 bool isRateLimited(mediametrics::Item *) const;
91 void saveItem(const std::shared_ptr<const mediametrics::Item>& item);
Ray Essick3938dc62016-11-01 08:56:56 -070092
Andy Hungf7c14102020-04-18 14:54:08 -070093 bool expirations(const std::shared_ptr<const mediametrics::Item>& item) REQUIRES(mLock);
Ray Essick3938dc62016-11-01 08:56:56 -070094
Andy Hung17dbaf22019-10-11 14:06:31 -070095 // support for generating output
Andy Hungf7c14102020-04-18 14:54:08 -070096 void dumpQueue(String8 &result, int64_t sinceNs, const char* prefix) REQUIRES(mLock);
97 void dumpHeaders(String8 &result, int64_t sinceNs, const char* prefix) REQUIRES(mLock);
Andy Hung17dbaf22019-10-11 14:06:31 -070098
99 // The following variables accessed without mLock
Ray Essick3938dc62016-11-01 08:56:56 -0700100
Ray Essickf65f4212017-08-31 11:41:19 -0700101 // limit how many records we'll retain
102 // by count (in each queue (open, finalized))
Andy Hung17dbaf22019-10-11 14:06:31 -0700103 const size_t mMaxRecords;
104 // by time (none older than this)
105 const nsecs_t mMaxRecordAgeNs;
Ray Essick72a436b2018-06-14 15:08:13 -0700106 // max to expire per expirations_l() invocation
Andy Hung17dbaf22019-10-11 14:06:31 -0700107 const size_t mMaxRecordsExpiredAtOnce;
Ray Essick3938dc62016-11-01 08:56:56 -0700108
Andy Hung17dbaf22019-10-11 14:06:31 -0700109 std::atomic<int64_t> mItemsSubmitted{}; // accessed outside of lock.
Ray Essickf65f4212017-08-31 11:41:19 -0700110
Andy Hungf7c14102020-04-18 14:54:08 -0700111 mediametrics::AudioAnalytics mAudioAnalytics; // mAudioAnalytics is locked internally.
Andy Hung06f3aba2019-12-03 16:36:42 -0800112
Andy Hung17dbaf22019-10-11 14:06:31 -0700113 std::mutex mLock;
114 // statistics about our analytics
Andy Hungf7c14102020-04-18 14:54:08 -0700115 int64_t mItemsFinalized GUARDED_BY(mLock) = 0;
116 int64_t mItemsDiscarded GUARDED_BY(mLock) = 0;
117 int64_t mItemsDiscardedExpire GUARDED_BY(mLock) = 0;
118 int64_t mItemsDiscardedCount GUARDED_BY(mLock) = 0;
Ray Essickf65f4212017-08-31 11:41:19 -0700119
Andy Hung17dbaf22019-10-11 14:06:31 -0700120 // If we have a worker thread to garbage collect
Andy Hungf7c14102020-04-18 14:54:08 -0700121 std::future<void> mExpireFuture GUARDED_BY(mLock);
Andy Hung17dbaf22019-10-11 14:06:31 -0700122
123 // Our item queue, generally (oldest at front)
124 // TODO: Make separate class, use segmented queue, write lock only end.
125 // Note: Another analytics module might have ownership of an item longer than the log.
Andy Hungf7c14102020-04-18 14:54:08 -0700126 std::deque<std::shared_ptr<const mediametrics::Item>> mItems GUARDED_BY(mLock);
Ray Essick3938dc62016-11-01 08:56:56 -0700127};
128
Andy Hung17dbaf22019-10-11 14:06:31 -0700129} // namespace android