blob: 2f9e7c204f7bacb7055bfe8060b47246c7e9d8cb [file] [log] [blame]
Ray Essick3938dc62016-11-01 08:56:56 -07001/*
2 * Copyright (C) 2016 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#ifndef ANDROID_MEDIA_MEDIAANALYTICSITEM_H
18#define ANDROID_MEDIA_MEDIAANALYTICSITEM_H
19
Ray Essick783bd0d2018-01-11 11:10:35 -080020#include <string>
Ray Essick3938dc62016-11-01 08:56:56 -070021#include <sys/types.h>
Ray Essickba8c4842019-01-18 11:35:33 -080022
23#include <cutils/properties.h>
Ray Essick3938dc62016-11-01 08:56:56 -070024#include <utils/Errors.h>
25#include <utils/KeyedVector.h>
26#include <utils/RefBase.h>
27#include <utils/StrongPointer.h>
28#include <utils/Timers.h>
29
Ray Essick3938dc62016-11-01 08:56:56 -070030namespace android {
31
Ray Essick3938dc62016-11-01 08:56:56 -070032class IMediaAnalyticsService;
Ray Essick783bd0d2018-01-11 11:10:35 -080033class Parcel;
Ray Essick3938dc62016-11-01 08:56:56 -070034
35// the class interface
36//
37
Ray Essickb5fac8e2016-12-12 11:33:56 -080038class MediaAnalyticsItem {
Ray Essick3938dc62016-11-01 08:56:56 -070039
40 friend class MediaAnalyticsService;
41 friend class IMediaAnalyticsService;
Ray Essicke08ab772017-01-25 17:47:51 -080042 friend class MediaMetricsJNI;
Ray Essick2e9c63b2017-03-29 15:16:44 -070043 friend class MetricsSummarizer;
Ray Essick2ab3c432017-10-02 09:29:49 -070044 friend class MediaMetricsDeathNotifier;
Ray Essick3938dc62016-11-01 08:56:56 -070045
46 public:
47
Ray Essickb5fac8e2016-12-12 11:33:56 -080048 enum Type {
49 kTypeNone = 0,
50 kTypeInt32 = 1,
51 kTypeInt64 = 2,
52 kTypeDouble = 3,
53 kTypeCString = 4,
54 kTypeRate = 5,
55 };
56
Ray Essick3938dc62016-11-01 08:56:56 -070057 // sessionid
58 // unique within device, within boot,
59 typedef int64_t SessionID_t;
60 static constexpr SessionID_t SessionIDInvalid = -1;
61 static constexpr SessionID_t SessionIDNone = 0;
62
63 // Key: the record descriminator
64 // values for the record discriminator
65 // values can be "component/component"
66 // basic values: "video", "audio", "drm"
67 // XXX: need to better define the format
Ray Essick783bd0d2018-01-11 11:10:35 -080068 typedef std::string Key;
Ray Essick3938dc62016-11-01 08:56:56 -070069 static const Key kKeyNone; // ""
70 static const Key kKeyAny; // "*"
71
72 // Attr: names for attributes within a record
73 // format "prop1" or "prop/subprop"
74 // XXX: need to better define the format
Ray Essickb5fac8e2016-12-12 11:33:56 -080075 typedef const char *Attr;
Ray Essick3938dc62016-11-01 08:56:56 -070076
77
Ray Essickf65f4212017-08-31 11:41:19 -070078 enum {
79 PROTO_V0 = 0,
80 PROTO_FIRST = PROTO_V0,
81 PROTO_V1 = 1,
82 PROTO_LAST = PROTO_V1,
83 };
84
85
Ray Essick3938dc62016-11-01 08:56:56 -070086 public:
87
Ray Essickba8c4842019-01-18 11:35:33 -080088 // so clients do not need to know size details
89 static MediaAnalyticsItem* create(Key key);
90 static MediaAnalyticsItem* create();
91
Ray Essick3938dc62016-11-01 08:56:56 -070092 // access functions for the class
93 MediaAnalyticsItem();
94 MediaAnalyticsItem(Key);
95 ~MediaAnalyticsItem();
96
Ray Essick3938dc62016-11-01 08:56:56 -070097 // SessionID ties multiple submissions for same key together
98 // so that if video "height" and "width" are known at one point
99 // and "framerate" is only known later, they can be be brought
100 // together.
101 MediaAnalyticsItem &setSessionID(SessionID_t);
102 MediaAnalyticsItem &clearSessionID();
103 SessionID_t getSessionID() const;
104 // generates and stores a new ID iff mSessionID == SessionIDNone
105 SessionID_t generateSessionID();
106
107 // reset all contents, discarding any extra data
108 void clear();
Ray Essickb5fac8e2016-12-12 11:33:56 -0800109 MediaAnalyticsItem *dup();
Ray Essick3938dc62016-11-01 08:56:56 -0700110
111 // set the key discriminator for the record.
112 // most often initialized as part of the constructor
113 MediaAnalyticsItem &setKey(MediaAnalyticsItem::Key);
114 MediaAnalyticsItem::Key getKey();
115
116 // # of attributes in the record
117 int32_t count() const;
118
119 // set values appropriately
Ray Essickb5fac8e2016-12-12 11:33:56 -0800120 void setInt32(Attr, int32_t value);
121 void setInt64(Attr, int64_t value);
122 void setDouble(Attr, double value);
123 void setRate(Attr, int64_t count, int64_t duration);
124 void setCString(Attr, const char *value);
Ray Essick3938dc62016-11-01 08:56:56 -0700125
126 // fused get/add/set; if attr wasn't there, it's a simple set.
127 // type-mismatch counts as "wasn't there".
Ray Essickb5fac8e2016-12-12 11:33:56 -0800128 void addInt32(Attr, int32_t value);
129 void addInt64(Attr, int64_t value);
130 void addDouble(Attr, double value);
131 void addRate(Attr, int64_t count, int64_t duration);
Ray Essick3938dc62016-11-01 08:56:56 -0700132
133 // find & extract values
134 // return indicates whether attr exists (and thus value filled in)
Ray Essickb5fac8e2016-12-12 11:33:56 -0800135 // NULL parameter value suppresses storage of value.
Ray Essick3938dc62016-11-01 08:56:56 -0700136 bool getInt32(Attr, int32_t *value);
137 bool getInt64(Attr, int64_t *value);
138 bool getDouble(Attr, double *value);
Ray Essickb5fac8e2016-12-12 11:33:56 -0800139 bool getRate(Attr, int64_t *count, int64_t *duration, double *rate);
140 // Caller owns the returned string
Ray Essick3938dc62016-11-01 08:56:56 -0700141 bool getCString(Attr, char **value);
Ray Essick20147322018-11-17 09:08:39 -0800142 bool getString(Attr, std::string *value);
Ray Essick3938dc62016-11-01 08:56:56 -0700143
144 // parameter indicates whether to close any existing open
145 // record with same key before establishing a new record
Ray Essickb5fac8e2016-12-12 11:33:56 -0800146 // caller retains ownership of 'this'.
Ray Essick3938dc62016-11-01 08:56:56 -0700147 bool selfrecord(bool);
148 bool selfrecord();
149
150 // remove indicated attributes and their values
151 // filterNot() could also be called keepOnly()
152 // return value is # attributes removed
153 // XXX: perhaps 'remove' instead of 'filter'
154 // XXX: filterNot would become 'keep'
155 int32_t filter(int count, Attr attrs[]);
156 int32_t filterNot(int count, Attr attrs[]);
157 int32_t filter(Attr attr);
158
159 // below here are used on server side or to talk to server
160 // clients need not worry about these.
161
162 // timestamp, pid, and uid only used on server side
Ray Essickb5fac8e2016-12-12 11:33:56 -0800163 // timestamp is in 'nanoseconds, unix time'
Ray Essick3938dc62016-11-01 08:56:56 -0700164 MediaAnalyticsItem &setTimestamp(nsecs_t);
165 nsecs_t getTimestamp() const;
166
167 MediaAnalyticsItem &setPid(pid_t);
168 pid_t getPid() const;
169
170 MediaAnalyticsItem &setUid(uid_t);
171 uid_t getUid() const;
172
Ray Essick783bd0d2018-01-11 11:10:35 -0800173 MediaAnalyticsItem &setPkgName(const std::string &pkgName);
174 std::string getPkgName() const { return mPkgName; }
Ray Essickf65f4212017-08-31 11:41:19 -0700175
Dianne Hackborn4e2eeff2017-11-27 14:01:29 -0800176 MediaAnalyticsItem &setPkgVersionCode(int64_t);
177 int64_t getPkgVersionCode() const;
Ray Essickf65f4212017-08-31 11:41:19 -0700178
Ray Essick3938dc62016-11-01 08:56:56 -0700179 // our serialization code for binder calls
180 int32_t writeToParcel(Parcel *);
181 int32_t readFromParcel(const Parcel&);
182
Ray Essickba8c4842019-01-18 11:35:33 -0800183 // supports the stable interface
184 bool dumpAttributes(char **pbuffer, size_t *plength);
185
Ray Essick783bd0d2018-01-11 11:10:35 -0800186 std::string toString();
187 std::string toString(int version);
Ray Essick20147322018-11-17 09:08:39 -0800188 const char *toCString();
189 const char *toCString(int version);
Ray Essick3938dc62016-11-01 08:56:56 -0700190
191 // are we collecting analytics data
192 static bool isEnabled();
193
Ray Essickba8c4842019-01-18 11:35:33 -0800194 private:
195 // handle Parcel version 0
196 int32_t writeToParcel0(Parcel *);
197 int32_t readFromParcel0(const Parcel&);
198
Ray Essick3938dc62016-11-01 08:56:56 -0700199 protected:
200
201 // merge fields from arg into this
202 // with rules for first/last/add, etc
203 // XXX: document semantics and how they are indicated
Ray Essickb5fac8e2016-12-12 11:33:56 -0800204 // caller continues to own 'incoming'
205 bool merge(MediaAnalyticsItem *incoming);
Ray Essick3938dc62016-11-01 08:56:56 -0700206
207 // enabled 1, disabled 0
208 static const char * const EnabledProperty;
209 static const char * const EnabledPropertyPersist;
210 static const int EnabledProperty_default;
211
212 private:
213
214 // to help validate that A doesn't mess with B's records
215 pid_t mPid;
216 uid_t mUid;
Ray Essick783bd0d2018-01-11 11:10:35 -0800217 std::string mPkgName;
Dianne Hackborn4e2eeff2017-11-27 14:01:29 -0800218 int64_t mPkgVersionCode;
Ray Essick3938dc62016-11-01 08:56:56 -0700219
220 // let's reuse a binder connection
221 static sp<IMediaAnalyticsService> sAnalyticsService;
222 static sp<IMediaAnalyticsService> getInstance();
Ray Essick2ab3c432017-10-02 09:29:49 -0700223 static void dropInstance();
Ray Essick3938dc62016-11-01 08:56:56 -0700224
225 // tracking information
226 SessionID_t mSessionID; // grouping similar records
227 nsecs_t mTimestamp; // ns, system_time_monotonic
228
229 // will this record accept further updates
230 bool mFinalized;
231
232 Key mKey;
233
Ray Essickb5fac8e2016-12-12 11:33:56 -0800234 struct Prop {
Ray Essick3938dc62016-11-01 08:56:56 -0700235
236 Type mType;
Ray Essickb5fac8e2016-12-12 11:33:56 -0800237 const char *mName;
238 size_t mNameLen; // the strlen(), doesn't include the null
Ray Essick3938dc62016-11-01 08:56:56 -0700239 union {
240 int32_t int32Value;
241 int64_t int64Value;
242 double doubleValue;
243 char *CStringValue;
Ray Essickb5fac8e2016-12-12 11:33:56 -0800244 struct { int64_t count, duration; } rate;
Ray Essick3938dc62016-11-01 08:56:56 -0700245 } u;
Ray Essickb5fac8e2016-12-12 11:33:56 -0800246 void setName(const char *name, size_t len);
Ray Essick3938dc62016-11-01 08:56:56 -0700247 };
Ray Essickb5fac8e2016-12-12 11:33:56 -0800248
249 void initProp(Prop *item);
250 void clearProp(Prop *item);
251 void clearPropValue(Prop *item);
252 void copyProp(Prop *dst, const Prop *src);
253 enum {
254 kGrowProps = 10
255 };
Ray Essick9bb7e3b2017-10-23 13:01:48 -0700256 bool growProps(int increment = kGrowProps);
Ray Essickb5fac8e2016-12-12 11:33:56 -0800257 size_t findPropIndex(const char *name, size_t len);
258 Prop *findProp(const char *name);
259 Prop *allocateProp(const char *name);
Ray Essickf65f4212017-08-31 11:41:19 -0700260 bool removeProp(const char *name);
Ray Essickb5fac8e2016-12-12 11:33:56 -0800261
262 size_t mPropCount;
263 size_t mPropSize;
264 Prop *mProps;
Ray Essick3938dc62016-11-01 08:56:56 -0700265};
266
267} // namespace android
268
269#endif