blob: ba4c3f23f987d96e7fa930af560a297bb6ca3ab2 [file] [log] [blame]
Andy Hung06f3aba2019-12-03 16:36:42 -08001/*
2 * Copyright (C) 2019 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
17#pragma once
18
Andy Hung0f7ad8c2020-01-03 13:24:34 -080019#include "AnalyticsActions.h"
20#include "AnalyticsState.h"
21#include "Wrap.h"
Andy Hung06f3aba2019-12-03 16:36:42 -080022
23namespace android::mediametrics {
24
25class AudioAnalytics
26{
27public:
28 AudioAnalytics();
29 ~AudioAnalytics();
30
Andy Hung06f3aba2019-12-03 16:36:42 -080031 /**
32 * Returns success if AudioAnalytics recognizes item.
33 *
34 * AudioAnalytics requires the item key to start with "audio.".
35 *
36 * A trusted source can create a new key, an untrusted source
37 * can only modify the key if the uid will match that authorized
38 * on the existing key.
39 *
40 * \param item the item to be submitted.
41 * \param isTrusted whether the transaction comes from a trusted source.
42 * In this case, a trusted source is verified by binder
43 * UID to be a system service by MediaMetrics service.
44 * Do not use true if you haven't really checked!
Andy Hung0f7ad8c2020-01-03 13:24:34 -080045 *
46 * \return NO_ERROR on success,
47 * PERMISSION_DENIED if the item cannot be put into the AnalyticsState,
48 * BAD_VALUE if the item key does not start with "audio.".
Andy Hung06f3aba2019-12-03 16:36:42 -080049 */
Ray Essickf27e9872019-12-07 06:28:46 -080050 status_t submit(const std::shared_ptr<const mediametrics::Item>& item, bool isTrusted);
Andy Hung06f3aba2019-12-03 16:36:42 -080051
52 /**
53 * Returns a pair consisting of the dump string, and the number of lines in the string.
54 *
55 * The number of lines in the returned pair is used as an optimization
56 * for subsequent line limiting.
57 *
58 * The TimeMachine and the TransactionLog are dumped separately under
59 * different locks, so may not be 100% consistent with the last data
60 * delivered.
61 *
62 * \param lines the maximum number of lines in the string returned.
Andy Hung709b91e2020-04-04 14:23:36 -070063 * \param sinceNs the nanoseconds since Unix epoch to start dump (0 shows all)
64 * \param prefix the desired key prefix to match (nullptr shows all)
Andy Hung06f3aba2019-12-03 16:36:42 -080065 */
Andy Hung709b91e2020-04-04 14:23:36 -070066 std::pair<std::string, int32_t> dump(
67 int32_t lines = INT32_MAX, int64_t sinceNs = 0, const char *prefix = nullptr) const;
68
69 void clear() {
70 // underlying state is locked.
71 mPreviousAnalyticsState->clear();
72 mAnalyticsState->clear();
73 }
Andy Hung06f3aba2019-12-03 16:36:42 -080074
75private:
Andy Hung0f7ad8c2020-01-03 13:24:34 -080076
77 /**
78 * Checks for any pending actions for a particular item.
79 *
80 * \param item to check against the current AnalyticsActions.
81 */
82 void checkActions(const std::shared_ptr<const mediametrics::Item>& item);
83
Andy Hungea186fa2020-01-09 18:13:15 -080084 // HELPER METHODS
85 /**
86 * Return the audio thread associated with an audio track name.
87 * e.g. "audio.track.32" -> "audio.thread.10" if the associated
88 * threadId for the audio track is 10.
89 */
90 std::string getThreadFromTrack(const std::string& track) const;
91
Andy Hung0f7ad8c2020-01-03 13:24:34 -080092 // Actions is individually locked
93 AnalyticsActions mActions;
94
95 // AnalyticsState is individually locked, and we use SharedPtrWrap
96 // to allow safe access even if the shared pointer changes underneath.
97
98 SharedPtrWrap<AnalyticsState> mAnalyticsState;
99 SharedPtrWrap<AnalyticsState> mPreviousAnalyticsState;
Andy Hung06f3aba2019-12-03 16:36:42 -0800100};
101
102} // namespace android::mediametrics