blob: bcae397e13c8beaac6ab40e839a2307af5a0e1ee [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>
Andy Hung49ca44e2020-11-10 22:14:58 -080027#include <android/media/BnMediaMetricsService.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
Andy Hung49ca44e2020-11-10 22:14:58 -080035class MediaMetricsService : public media::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 Hung49ca44e2020-11-10 22:14:58 -080041 // AIDL interface
42 binder::Status submitBuffer(const std::vector<uint8_t>& buffer) override {
43 status_t status = submitBuffer((char *)buffer.data(), buffer.size());
44 return binder::Status::fromStatusT(status);
45 }
46
Andy Hungc89c8dc2019-10-16 17:48:21 -070047 /**
Andy Hunga87e69c2019-10-18 10:07:40 -070048 * Submits the indicated record to the mediaanalytics service.
Andy Hungc89c8dc2019-10-16 17:48:21 -070049 *
50 * \param item the item to submit.
Andy Hunga87e69c2019-10-18 10:07:40 -070051 * \return status failure, which is negative on binder transaction failure.
52 * As the transaction is one-way, remote failures will not be reported.
Andy Hungc89c8dc2019-10-16 17:48:21 -070053 */
Andy Hung49ca44e2020-11-10 22:14:58 -080054 status_t submit(mediametrics::Item *item) {
Andy Hunga87e69c2019-10-18 10:07:40 -070055 return submitInternal(item, false /* release */);
56 }
Ray Essick3938dc62016-11-01 08:56:56 -070057
Andy Hung49ca44e2020-11-10 22:14:58 -080058 status_t submitBuffer(const char *buffer, size_t length) {
Ray Essickf27e9872019-12-07 06:28:46 -080059 mediametrics::Item *item = new mediametrics::Item();
Andy Hung1efc9c62019-12-03 13:43:33 -080060 return item->readFromByteString(buffer, length)
61 ?: submitInternal(item, true /* release */);
62 }
63
Andy Hung17dbaf22019-10-11 14:06:31 -070064 status_t dump(int fd, const Vector<String16>& args) override;
Ray Essick3938dc62016-11-01 08:56:56 -070065
Andy Hung17dbaf22019-10-11 14:06:31 -070066 static constexpr const char * const kServiceName = "media.metrics";
Ray Essick3938dc62016-11-01 08:56:56 -070067
Andy Hung55aaf522019-12-03 15:07:51 -080068 /**
69 * Rounds time to the nearest second.
70 */
71 static nsecs_t roundTime(nsecs_t timeNs);
72
Andy Hunga85efab2019-12-23 11:41:29 -080073 /**
Muhammad Qureshi087b37c2020-06-16 16:37:36 -070074 * Returns true if we should use uid for package name when uploading to statsd.
Andy Hunga85efab2019-12-23 11:41:29 -080075 */
76 static bool useUidForPackage(const std::string& package, const std::string& installer);
77
Andy Hungce9b6632020-04-28 20:15:17 -070078 /**
79 * Returns a std::pair of packageName and versionCode for a given uid.
80 *
81 * The value is sanitized - i.e. if the result is not approved to send,
82 * we use the uid as a string and a version code of 0.
83 */
84 static std::pair<std::string, int64_t> getSanitizedPackageNameAndVersionCode(uid_t uid);
85
Andy Hunga87e69c2019-10-18 10:07:40 -070086protected:
87
88 // Internal call where release is true if ownership of item is transferred
89 // to the service (that is, the service will eventually delete the item).
Andy Hung49ca44e2020-11-10 22:14:58 -080090 status_t submitInternal(mediametrics::Item *item, bool release);
Andy Hunga87e69c2019-10-18 10:07:40 -070091
Andy Hung17dbaf22019-10-11 14:06:31 -070092private:
93 void processExpirations();
Andy Hung17dbaf22019-10-11 14:06:31 -070094 // input validation after arrival from client
Ray Essickf27e9872019-12-07 06:28:46 -080095 static bool isContentValid(const mediametrics::Item *item, bool isTrusted);
96 bool isRateLimited(mediametrics::Item *) const;
97 void saveItem(const std::shared_ptr<const mediametrics::Item>& item);
Ray Essick3938dc62016-11-01 08:56:56 -070098
Andy Hungf7c14102020-04-18 14:54:08 -070099 bool expirations(const std::shared_ptr<const mediametrics::Item>& item) REQUIRES(mLock);
Ray Essick3938dc62016-11-01 08:56:56 -0700100
Andy Hung17dbaf22019-10-11 14:06:31 -0700101 // support for generating output
Andy Hungf7c14102020-04-18 14:54:08 -0700102 void dumpQueue(String8 &result, int64_t sinceNs, const char* prefix) REQUIRES(mLock);
103 void dumpHeaders(String8 &result, int64_t sinceNs, const char* prefix) REQUIRES(mLock);
Andy Hung17dbaf22019-10-11 14:06:31 -0700104
105 // The following variables accessed without mLock
Ray Essick3938dc62016-11-01 08:56:56 -0700106
Ray Essickf65f4212017-08-31 11:41:19 -0700107 // limit how many records we'll retain
108 // by count (in each queue (open, finalized))
Andy Hung17dbaf22019-10-11 14:06:31 -0700109 const size_t mMaxRecords;
110 // by time (none older than this)
111 const nsecs_t mMaxRecordAgeNs;
Ray Essick72a436b2018-06-14 15:08:13 -0700112 // max to expire per expirations_l() invocation
Andy Hung17dbaf22019-10-11 14:06:31 -0700113 const size_t mMaxRecordsExpiredAtOnce;
Ray Essick3938dc62016-11-01 08:56:56 -0700114
Andy Hung17dbaf22019-10-11 14:06:31 -0700115 std::atomic<int64_t> mItemsSubmitted{}; // accessed outside of lock.
Ray Essickf65f4212017-08-31 11:41:19 -0700116
Andy Hungf7c14102020-04-18 14:54:08 -0700117 mediametrics::AudioAnalytics mAudioAnalytics; // mAudioAnalytics is locked internally.
Andy Hung06f3aba2019-12-03 16:36:42 -0800118
Andy Hung17dbaf22019-10-11 14:06:31 -0700119 std::mutex mLock;
120 // statistics about our analytics
Andy Hungf7c14102020-04-18 14:54:08 -0700121 int64_t mItemsFinalized GUARDED_BY(mLock) = 0;
122 int64_t mItemsDiscarded GUARDED_BY(mLock) = 0;
123 int64_t mItemsDiscardedExpire GUARDED_BY(mLock) = 0;
124 int64_t mItemsDiscardedCount GUARDED_BY(mLock) = 0;
Ray Essickf65f4212017-08-31 11:41:19 -0700125
Andy Hung17dbaf22019-10-11 14:06:31 -0700126 // If we have a worker thread to garbage collect
Andy Hungf7c14102020-04-18 14:54:08 -0700127 std::future<void> mExpireFuture GUARDED_BY(mLock);
Andy Hung17dbaf22019-10-11 14:06:31 -0700128
129 // Our item queue, generally (oldest at front)
130 // TODO: Make separate class, use segmented queue, write lock only end.
131 // Note: Another analytics module might have ownership of an item longer than the log.
Andy Hungf7c14102020-04-18 14:54:08 -0700132 std::deque<std::shared_ptr<const mediametrics::Item>> mItems GUARDED_BY(mLock);
Ray Essick3938dc62016-11-01 08:56:56 -0700133};
134
Andy Hung17dbaf22019-10-11 14:06:31 -0700135} // namespace android