blob: f050e7fd26a9455ddcee76e470920b79b6d84fc6 [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>
21#include <sys/types.h>
22#include <utils/Errors.h>
23#include <utils/KeyedVector.h>
24#include <utils/RefBase.h>
25#include <utils/StrongPointer.h>
26#include <utils/Timers.h>
27
28#include <media/stagefright/foundation/AString.h>
29
30namespace android {
31
32
33
34class IMediaAnalyticsService;
35
36// the class interface
37//
38
Ray Essickb5fac8e2016-12-12 11:33:56 -080039class MediaAnalyticsItem {
Ray Essick3938dc62016-11-01 08:56:56 -070040
41 friend class MediaAnalyticsService;
42 friend class IMediaAnalyticsService;
Ray Essicke08ab772017-01-25 17:47:51 -080043 friend class MediaMetricsJNI;
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
67 typedef AString Key;
68 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
77 public:
78
79 // access functions for the class
80 MediaAnalyticsItem();
81 MediaAnalyticsItem(Key);
82 ~MediaAnalyticsItem();
83
84 // so clients can send intermediate values to be overlaid later
85 MediaAnalyticsItem &setFinalized(bool);
86 bool getFinalized() const;
87
88 // SessionID ties multiple submissions for same key together
89 // so that if video "height" and "width" are known at one point
90 // and "framerate" is only known later, they can be be brought
91 // together.
92 MediaAnalyticsItem &setSessionID(SessionID_t);
93 MediaAnalyticsItem &clearSessionID();
94 SessionID_t getSessionID() const;
95 // generates and stores a new ID iff mSessionID == SessionIDNone
96 SessionID_t generateSessionID();
97
98 // reset all contents, discarding any extra data
99 void clear();
Ray Essickb5fac8e2016-12-12 11:33:56 -0800100 MediaAnalyticsItem *dup();
Ray Essick3938dc62016-11-01 08:56:56 -0700101
102 // set the key discriminator for the record.
103 // most often initialized as part of the constructor
104 MediaAnalyticsItem &setKey(MediaAnalyticsItem::Key);
105 MediaAnalyticsItem::Key getKey();
106
107 // # of attributes in the record
108 int32_t count() const;
109
110 // set values appropriately
Ray Essickb5fac8e2016-12-12 11:33:56 -0800111 void setInt32(Attr, int32_t value);
112 void setInt64(Attr, int64_t value);
113 void setDouble(Attr, double value);
114 void setRate(Attr, int64_t count, int64_t duration);
115 void setCString(Attr, const char *value);
Ray Essick3938dc62016-11-01 08:56:56 -0700116
117 // fused get/add/set; if attr wasn't there, it's a simple set.
118 // type-mismatch counts as "wasn't there".
Ray Essickb5fac8e2016-12-12 11:33:56 -0800119 void addInt32(Attr, int32_t value);
120 void addInt64(Attr, int64_t value);
121 void addDouble(Attr, double value);
122 void addRate(Attr, int64_t count, int64_t duration);
Ray Essick3938dc62016-11-01 08:56:56 -0700123
124 // find & extract values
125 // return indicates whether attr exists (and thus value filled in)
Ray Essickb5fac8e2016-12-12 11:33:56 -0800126 // NULL parameter value suppresses storage of value.
Ray Essick3938dc62016-11-01 08:56:56 -0700127 bool getInt32(Attr, int32_t *value);
128 bool getInt64(Attr, int64_t *value);
129 bool getDouble(Attr, double *value);
Ray Essickb5fac8e2016-12-12 11:33:56 -0800130 bool getRate(Attr, int64_t *count, int64_t *duration, double *rate);
131 // Caller owns the returned string
Ray Essick3938dc62016-11-01 08:56:56 -0700132 bool getCString(Attr, char **value);
133
134 // parameter indicates whether to close any existing open
135 // record with same key before establishing a new record
Ray Essickb5fac8e2016-12-12 11:33:56 -0800136 // caller retains ownership of 'this'.
Ray Essick3938dc62016-11-01 08:56:56 -0700137 bool selfrecord(bool);
138 bool selfrecord();
139
140 // remove indicated attributes and their values
141 // filterNot() could also be called keepOnly()
142 // return value is # attributes removed
143 // XXX: perhaps 'remove' instead of 'filter'
144 // XXX: filterNot would become 'keep'
145 int32_t filter(int count, Attr attrs[]);
146 int32_t filterNot(int count, Attr attrs[]);
147 int32_t filter(Attr attr);
148
149 // below here are used on server side or to talk to server
150 // clients need not worry about these.
151
152 // timestamp, pid, and uid only used on server side
Ray Essickb5fac8e2016-12-12 11:33:56 -0800153 // timestamp is in 'nanoseconds, unix time'
Ray Essick3938dc62016-11-01 08:56:56 -0700154 MediaAnalyticsItem &setTimestamp(nsecs_t);
155 nsecs_t getTimestamp() const;
156
157 MediaAnalyticsItem &setPid(pid_t);
158 pid_t getPid() const;
159
160 MediaAnalyticsItem &setUid(uid_t);
161 uid_t getUid() const;
162
163 // our serialization code for binder calls
164 int32_t writeToParcel(Parcel *);
165 int32_t readFromParcel(const Parcel&);
166
167 AString toString();
168
169 // are we collecting analytics data
170 static bool isEnabled();
171
172 protected:
173
174 // merge fields from arg into this
175 // with rules for first/last/add, etc
176 // XXX: document semantics and how they are indicated
Ray Essickb5fac8e2016-12-12 11:33:56 -0800177 // caller continues to own 'incoming'
178 bool merge(MediaAnalyticsItem *incoming);
Ray Essick3938dc62016-11-01 08:56:56 -0700179
180 // enabled 1, disabled 0
181 static const char * const EnabledProperty;
182 static const char * const EnabledPropertyPersist;
183 static const int EnabledProperty_default;
184
185 private:
186
187 // to help validate that A doesn't mess with B's records
188 pid_t mPid;
189 uid_t mUid;
190
191 // let's reuse a binder connection
192 static sp<IMediaAnalyticsService> sAnalyticsService;
193 static sp<IMediaAnalyticsService> getInstance();
194
195 // tracking information
196 SessionID_t mSessionID; // grouping similar records
197 nsecs_t mTimestamp; // ns, system_time_monotonic
198
199 // will this record accept further updates
200 bool mFinalized;
201
202 Key mKey;
203
Ray Essickb5fac8e2016-12-12 11:33:56 -0800204 struct Prop {
Ray Essick3938dc62016-11-01 08:56:56 -0700205
206 Type mType;
Ray Essickb5fac8e2016-12-12 11:33:56 -0800207 const char *mName;
208 size_t mNameLen; // the strlen(), doesn't include the null
Ray Essick3938dc62016-11-01 08:56:56 -0700209 union {
210 int32_t int32Value;
211 int64_t int64Value;
212 double doubleValue;
213 char *CStringValue;
Ray Essickb5fac8e2016-12-12 11:33:56 -0800214 struct { int64_t count, duration; } rate;
Ray Essick3938dc62016-11-01 08:56:56 -0700215 } u;
Ray Essickb5fac8e2016-12-12 11:33:56 -0800216 void setName(const char *name, size_t len);
Ray Essick3938dc62016-11-01 08:56:56 -0700217 };
Ray Essickb5fac8e2016-12-12 11:33:56 -0800218
219 void initProp(Prop *item);
220 void clearProp(Prop *item);
221 void clearPropValue(Prop *item);
222 void copyProp(Prop *dst, const Prop *src);
223 enum {
224 kGrowProps = 10
225 };
226 void growProps(int increment = kGrowProps);
227 size_t findPropIndex(const char *name, size_t len);
228 Prop *findProp(const char *name);
229 Prop *allocateProp(const char *name);
230
231 size_t mPropCount;
232 size_t mPropSize;
233 Prop *mProps;
Ray Essick3938dc62016-11-01 08:56:56 -0700234
235};
236
237} // namespace android
238
239#endif