blob: b99cd91c1cab5e3ceb74416eeadf7b71abc29369 [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
20#include <cutils/properties.h>
Ray Essick783bd0d2018-01-11 11:10:35 -080021#include <string>
Ray Essick3938dc62016-11-01 08:56:56 -070022#include <sys/types.h>
23#include <utils/Errors.h>
24#include <utils/KeyedVector.h>
25#include <utils/RefBase.h>
26#include <utils/StrongPointer.h>
27#include <utils/Timers.h>
28
Ray Essick3938dc62016-11-01 08:56:56 -070029namespace android {
30
Ray Essick3938dc62016-11-01 08:56:56 -070031class IMediaAnalyticsService;
Ray Essick783bd0d2018-01-11 11:10:35 -080032class Parcel;
Ray Essick3938dc62016-11-01 08:56:56 -070033
34// the class interface
35//
36
Ray Essickb5fac8e2016-12-12 11:33:56 -080037class MediaAnalyticsItem {
Ray Essick3938dc62016-11-01 08:56:56 -070038
39 friend class MediaAnalyticsService;
40 friend class IMediaAnalyticsService;
Ray Essicke08ab772017-01-25 17:47:51 -080041 friend class MediaMetricsJNI;
Ray Essick2e9c63b2017-03-29 15:16:44 -070042 friend class MetricsSummarizer;
Ray Essick2ab3c432017-10-02 09:29:49 -070043 friend class MediaMetricsDeathNotifier;
Ray Essick3938dc62016-11-01 08:56:56 -070044
45 public:
46
Ray Essickb5fac8e2016-12-12 11:33:56 -080047 enum Type {
48 kTypeNone = 0,
49 kTypeInt32 = 1,
50 kTypeInt64 = 2,
51 kTypeDouble = 3,
52 kTypeCString = 4,
53 kTypeRate = 5,
54 };
55
Ray Essick3938dc62016-11-01 08:56:56 -070056 // sessionid
57 // unique within device, within boot,
58 typedef int64_t SessionID_t;
59 static constexpr SessionID_t SessionIDInvalid = -1;
60 static constexpr SessionID_t SessionIDNone = 0;
61
62 // Key: the record descriminator
63 // values for the record discriminator
64 // values can be "component/component"
65 // basic values: "video", "audio", "drm"
66 // XXX: need to better define the format
Ray Essick783bd0d2018-01-11 11:10:35 -080067 typedef std::string Key;
Ray Essick3938dc62016-11-01 08:56:56 -070068 static const Key kKeyNone; // ""
69 static const Key kKeyAny; // "*"
70
71 // Attr: names for attributes within a record
72 // format "prop1" or "prop/subprop"
73 // XXX: need to better define the format
Ray Essickb5fac8e2016-12-12 11:33:56 -080074 typedef const char *Attr;
Ray Essick3938dc62016-11-01 08:56:56 -070075
76
Ray Essickf65f4212017-08-31 11:41:19 -070077 enum {
78 PROTO_V0 = 0,
79 PROTO_FIRST = PROTO_V0,
80 PROTO_V1 = 1,
81 PROTO_LAST = PROTO_V1,
82 };
83
84
Ray Essick3938dc62016-11-01 08:56:56 -070085 public:
86
87 // access functions for the class
88 MediaAnalyticsItem();
89 MediaAnalyticsItem(Key);
90 ~MediaAnalyticsItem();
91
Ray Essick3938dc62016-11-01 08:56:56 -070092 // SessionID ties multiple submissions for same key together
93 // so that if video "height" and "width" are known at one point
94 // and "framerate" is only known later, they can be be brought
95 // together.
96 MediaAnalyticsItem &setSessionID(SessionID_t);
97 MediaAnalyticsItem &clearSessionID();
98 SessionID_t getSessionID() const;
99 // generates and stores a new ID iff mSessionID == SessionIDNone
100 SessionID_t generateSessionID();
101
102 // reset all contents, discarding any extra data
103 void clear();
Ray Essickb5fac8e2016-12-12 11:33:56 -0800104 MediaAnalyticsItem *dup();
Ray Essick3938dc62016-11-01 08:56:56 -0700105
106 // set the key discriminator for the record.
107 // most often initialized as part of the constructor
108 MediaAnalyticsItem &setKey(MediaAnalyticsItem::Key);
109 MediaAnalyticsItem::Key getKey();
110
111 // # of attributes in the record
112 int32_t count() const;
113
114 // set values appropriately
Ray Essickb5fac8e2016-12-12 11:33:56 -0800115 void setInt32(Attr, int32_t value);
116 void setInt64(Attr, int64_t value);
117 void setDouble(Attr, double value);
118 void setRate(Attr, int64_t count, int64_t duration);
119 void setCString(Attr, const char *value);
Ray Essick3938dc62016-11-01 08:56:56 -0700120
121 // fused get/add/set; if attr wasn't there, it's a simple set.
122 // type-mismatch counts as "wasn't there".
Ray Essickb5fac8e2016-12-12 11:33:56 -0800123 void addInt32(Attr, int32_t value);
124 void addInt64(Attr, int64_t value);
125 void addDouble(Attr, double value);
126 void addRate(Attr, int64_t count, int64_t duration);
Ray Essick3938dc62016-11-01 08:56:56 -0700127
128 // find & extract values
129 // return indicates whether attr exists (and thus value filled in)
Ray Essickb5fac8e2016-12-12 11:33:56 -0800130 // NULL parameter value suppresses storage of value.
Ray Essick3938dc62016-11-01 08:56:56 -0700131 bool getInt32(Attr, int32_t *value);
132 bool getInt64(Attr, int64_t *value);
133 bool getDouble(Attr, double *value);
Ray Essickb5fac8e2016-12-12 11:33:56 -0800134 bool getRate(Attr, int64_t *count, int64_t *duration, double *rate);
135 // Caller owns the returned string
Ray Essick3938dc62016-11-01 08:56:56 -0700136 bool getCString(Attr, char **value);
Ray Essick20147322018-11-17 09:08:39 -0800137 bool getString(Attr, std::string *value);
Ray Essick3938dc62016-11-01 08:56:56 -0700138
139 // parameter indicates whether to close any existing open
140 // record with same key before establishing a new record
Ray Essickb5fac8e2016-12-12 11:33:56 -0800141 // caller retains ownership of 'this'.
Ray Essick3938dc62016-11-01 08:56:56 -0700142 bool selfrecord(bool);
143 bool selfrecord();
144
145 // remove indicated attributes and their values
146 // filterNot() could also be called keepOnly()
147 // return value is # attributes removed
148 // XXX: perhaps 'remove' instead of 'filter'
149 // XXX: filterNot would become 'keep'
150 int32_t filter(int count, Attr attrs[]);
151 int32_t filterNot(int count, Attr attrs[]);
152 int32_t filter(Attr attr);
153
154 // below here are used on server side or to talk to server
155 // clients need not worry about these.
156
157 // timestamp, pid, and uid only used on server side
Ray Essickb5fac8e2016-12-12 11:33:56 -0800158 // timestamp is in 'nanoseconds, unix time'
Ray Essick3938dc62016-11-01 08:56:56 -0700159 MediaAnalyticsItem &setTimestamp(nsecs_t);
160 nsecs_t getTimestamp() const;
161
162 MediaAnalyticsItem &setPid(pid_t);
163 pid_t getPid() const;
164
165 MediaAnalyticsItem &setUid(uid_t);
166 uid_t getUid() const;
167
Ray Essick783bd0d2018-01-11 11:10:35 -0800168 MediaAnalyticsItem &setPkgName(const std::string &pkgName);
169 std::string getPkgName() const { return mPkgName; }
Ray Essickf65f4212017-08-31 11:41:19 -0700170
Dianne Hackborn4e2eeff2017-11-27 14:01:29 -0800171 MediaAnalyticsItem &setPkgVersionCode(int64_t);
172 int64_t getPkgVersionCode() const;
Ray Essickf65f4212017-08-31 11:41:19 -0700173
Ray Essick3938dc62016-11-01 08:56:56 -0700174 // our serialization code for binder calls
175 int32_t writeToParcel(Parcel *);
176 int32_t readFromParcel(const Parcel&);
177
Ray Essick783bd0d2018-01-11 11:10:35 -0800178 std::string toString();
179 std::string toString(int version);
Ray Essick20147322018-11-17 09:08:39 -0800180 const char *toCString();
181 const char *toCString(int version);
Ray Essick3938dc62016-11-01 08:56:56 -0700182
183 // are we collecting analytics data
184 static bool isEnabled();
185
186 protected:
187
188 // merge fields from arg into this
189 // with rules for first/last/add, etc
190 // XXX: document semantics and how they are indicated
Ray Essickb5fac8e2016-12-12 11:33:56 -0800191 // caller continues to own 'incoming'
192 bool merge(MediaAnalyticsItem *incoming);
Ray Essick3938dc62016-11-01 08:56:56 -0700193
194 // enabled 1, disabled 0
195 static const char * const EnabledProperty;
196 static const char * const EnabledPropertyPersist;
197 static const int EnabledProperty_default;
198
199 private:
200
201 // to help validate that A doesn't mess with B's records
202 pid_t mPid;
203 uid_t mUid;
Ray Essick783bd0d2018-01-11 11:10:35 -0800204 std::string mPkgName;
Dianne Hackborn4e2eeff2017-11-27 14:01:29 -0800205 int64_t mPkgVersionCode;
Ray Essick3938dc62016-11-01 08:56:56 -0700206
207 // let's reuse a binder connection
208 static sp<IMediaAnalyticsService> sAnalyticsService;
209 static sp<IMediaAnalyticsService> getInstance();
Ray Essick2ab3c432017-10-02 09:29:49 -0700210 static void dropInstance();
Ray Essick3938dc62016-11-01 08:56:56 -0700211
212 // tracking information
213 SessionID_t mSessionID; // grouping similar records
214 nsecs_t mTimestamp; // ns, system_time_monotonic
215
216 // will this record accept further updates
217 bool mFinalized;
218
219 Key mKey;
220
Ray Essickb5fac8e2016-12-12 11:33:56 -0800221 struct Prop {
Ray Essick3938dc62016-11-01 08:56:56 -0700222
223 Type mType;
Ray Essickb5fac8e2016-12-12 11:33:56 -0800224 const char *mName;
225 size_t mNameLen; // the strlen(), doesn't include the null
Ray Essick3938dc62016-11-01 08:56:56 -0700226 union {
227 int32_t int32Value;
228 int64_t int64Value;
229 double doubleValue;
230 char *CStringValue;
Ray Essickb5fac8e2016-12-12 11:33:56 -0800231 struct { int64_t count, duration; } rate;
Ray Essick3938dc62016-11-01 08:56:56 -0700232 } u;
Ray Essickb5fac8e2016-12-12 11:33:56 -0800233 void setName(const char *name, size_t len);
Ray Essick3938dc62016-11-01 08:56:56 -0700234 };
Ray Essickb5fac8e2016-12-12 11:33:56 -0800235
236 void initProp(Prop *item);
237 void clearProp(Prop *item);
238 void clearPropValue(Prop *item);
239 void copyProp(Prop *dst, const Prop *src);
240 enum {
241 kGrowProps = 10
242 };
Ray Essick9bb7e3b2017-10-23 13:01:48 -0700243 bool growProps(int increment = kGrowProps);
Ray Essickb5fac8e2016-12-12 11:33:56 -0800244 size_t findPropIndex(const char *name, size_t len);
245 Prop *findProp(const char *name);
246 Prop *allocateProp(const char *name);
Ray Essickf65f4212017-08-31 11:41:19 -0700247 bool removeProp(const char *name);
Ray Essickb5fac8e2016-12-12 11:33:56 -0800248
249 size_t mPropCount;
250 size_t mPropSize;
251 Prop *mProps;
Ray Essick3938dc62016-11-01 08:56:56 -0700252};
253
254} // namespace android
255
256#endif