blob: f9c776defc3cc07fa8497c2eb00ec92aeb305692 [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 Hungce9b6632020-04-28 20:15:17 -070019#include <android-base/thread_annotations.h>
Andy Hung0f7ad8c2020-01-03 13:24:34 -080020#include "AnalyticsActions.h"
21#include "AnalyticsState.h"
Joey Poomarin52989982020-03-05 17:40:49 +080022#include "AudioPowerUsage.h"
Andy Hungce9b6632020-04-28 20:15:17 -070023#include "TimedAction.h"
Andy Hung0f7ad8c2020-01-03 13:24:34 -080024#include "Wrap.h"
Andy Hung06f3aba2019-12-03 16:36:42 -080025
26namespace android::mediametrics {
27
28class AudioAnalytics
29{
Joey Poomarin52989982020-03-05 17:40:49 +080030 // AudioAnalytics action / state helper classes
31 friend AudioPowerUsage;
32
Andy Hung06f3aba2019-12-03 16:36:42 -080033public:
34 AudioAnalytics();
35 ~AudioAnalytics();
36
Andy Hung06f3aba2019-12-03 16:36:42 -080037 /**
38 * Returns success if AudioAnalytics recognizes item.
39 *
40 * AudioAnalytics requires the item key to start with "audio.".
41 *
42 * A trusted source can create a new key, an untrusted source
43 * can only modify the key if the uid will match that authorized
44 * on the existing key.
45 *
46 * \param item the item to be submitted.
47 * \param isTrusted whether the transaction comes from a trusted source.
48 * In this case, a trusted source is verified by binder
49 * UID to be a system service by MediaMetrics service.
50 * Do not use true if you haven't really checked!
Andy Hung0f7ad8c2020-01-03 13:24:34 -080051 *
52 * \return NO_ERROR on success,
53 * PERMISSION_DENIED if the item cannot be put into the AnalyticsState,
54 * BAD_VALUE if the item key does not start with "audio.".
Andy Hung06f3aba2019-12-03 16:36:42 -080055 */
Ray Essickf27e9872019-12-07 06:28:46 -080056 status_t submit(const std::shared_ptr<const mediametrics::Item>& item, bool isTrusted);
Andy Hung06f3aba2019-12-03 16:36:42 -080057
58 /**
59 * Returns a pair consisting of the dump string, and the number of lines in the string.
60 *
61 * The number of lines in the returned pair is used as an optimization
62 * for subsequent line limiting.
63 *
64 * The TimeMachine and the TransactionLog are dumped separately under
65 * different locks, so may not be 100% consistent with the last data
66 * delivered.
67 *
68 * \param lines the maximum number of lines in the string returned.
Andy Hung709b91e2020-04-04 14:23:36 -070069 * \param sinceNs the nanoseconds since Unix epoch to start dump (0 shows all)
70 * \param prefix the desired key prefix to match (nullptr shows all)
Andy Hung06f3aba2019-12-03 16:36:42 -080071 */
Andy Hung709b91e2020-04-04 14:23:36 -070072 std::pair<std::string, int32_t> dump(
73 int32_t lines = INT32_MAX, int64_t sinceNs = 0, const char *prefix = nullptr) const;
74
75 void clear() {
76 // underlying state is locked.
77 mPreviousAnalyticsState->clear();
78 mAnalyticsState->clear();
Joey Poomarin52989982020-03-05 17:40:49 +080079
80 // Clear power usage state.
81 mAudioPowerUsage.clear();
Andy Hung709b91e2020-04-04 14:23:36 -070082 }
Andy Hung06f3aba2019-12-03 16:36:42 -080083
84private:
Andy Hung0f7ad8c2020-01-03 13:24:34 -080085
86 /**
87 * Checks for any pending actions for a particular item.
88 *
89 * \param item to check against the current AnalyticsActions.
90 */
91 void checkActions(const std::shared_ptr<const mediametrics::Item>& item);
92
Andy Hungea186fa2020-01-09 18:13:15 -080093 // HELPER METHODS
94 /**
95 * Return the audio thread associated with an audio track name.
96 * e.g. "audio.track.32" -> "audio.thread.10" if the associated
97 * threadId for the audio track is 10.
98 */
99 std::string getThreadFromTrack(const std::string& track) const;
100
Andy Hungce9b6632020-04-28 20:15:17 -0700101 const bool mDeliverStatistics __unused = true;
102
Andy Hung0f7ad8c2020-01-03 13:24:34 -0800103 // Actions is individually locked
104 AnalyticsActions mActions;
105
106 // AnalyticsState is individually locked, and we use SharedPtrWrap
107 // to allow safe access even if the shared pointer changes underneath.
108
109 SharedPtrWrap<AnalyticsState> mAnalyticsState;
110 SharedPtrWrap<AnalyticsState> mPreviousAnalyticsState;
Andy Hungce9b6632020-04-28 20:15:17 -0700111
112 TimedAction mTimedAction; // locked internally
113
114 // DeviceUse is a nested class which handles audio device usage accounting.
115 // We define this class at the end to ensure prior variables all properly constructed.
116 // TODO: Track / Thread interaction
117 // TODO: Consider statistics aggregation.
118 class DeviceUse {
119 public:
120 explicit DeviceUse(AudioAnalytics &audioAnalytics) : mAudioAnalytics{audioAnalytics} {}
121
122 // Called every time an endAudioIntervalGroup message is received.
123 void endAudioIntervalGroup(
124 const std::shared_ptr<const android::mediametrics::Item> &item,
125 bool isTrack) const;
126 private:
127 AudioAnalytics &mAudioAnalytics;
128 } mDeviceUse{*this};
129
130 // DeviceConnected is a nested class which handles audio device connection
131 // We define this class at the end to ensure prior variables all properly constructed.
132 // TODO: Track / Thread interaction
133 // TODO: Consider statistics aggregation.
134 class DeviceConnection {
135 public:
136 explicit DeviceConnection(AudioAnalytics &audioAnalytics)
137 : mAudioAnalytics{audioAnalytics} {}
138
139 // Called every time an endAudioIntervalGroup message is received.
140 void a2dpConnected(
141 const std::shared_ptr<const android::mediametrics::Item> &item);
142
143 // Called when we have an AudioFlinger createPatch
144 void createPatch(
145 const std::shared_ptr<const android::mediametrics::Item> &item);
146
147 // When the timer expires.
148 void expire();
149
150 private:
151 AudioAnalytics &mAudioAnalytics;
152
153 mutable std::mutex mLock;
154 int64_t mA2dpTimeConnectedNs GUARDED_BY(mLock) = 0;
155 int32_t mA2dpConnectedAttempts GUARDED_BY(mLock) = 0;
156 int32_t mA2dpConnectedSuccesses GUARDED_BY(mLock) = 0;
157 int32_t mA2dpConnectedFailures GUARDED_BY(mLock) = 0;
158 } mDeviceConnection{*this};
Joey Poomarin52989982020-03-05 17:40:49 +0800159
160 AudioPowerUsage mAudioPowerUsage{this};
Andy Hung06f3aba2019-12-03 16:36:42 -0800161};
162
163} // namespace android::mediametrics