blob: a78aa8b0bcd0713d468bbdcf67a24a73c351b9a2 [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;
43
44 public:
45
Ray Essickb5fac8e2016-12-12 11:33:56 -080046 enum Type {
47 kTypeNone = 0,
48 kTypeInt32 = 1,
49 kTypeInt64 = 2,
50 kTypeDouble = 3,
51 kTypeCString = 4,
52 kTypeRate = 5,
53 };
54
Ray Essick3938dc62016-11-01 08:56:56 -070055 // sessionid
56 // unique within device, within boot,
57 typedef int64_t SessionID_t;
58 static constexpr SessionID_t SessionIDInvalid = -1;
59 static constexpr SessionID_t SessionIDNone = 0;
60
61 // Key: the record descriminator
62 // values for the record discriminator
63 // values can be "component/component"
64 // basic values: "video", "audio", "drm"
65 // XXX: need to better define the format
66 typedef AString Key;
67 static const Key kKeyNone; // ""
68 static const Key kKeyAny; // "*"
69
70 // Attr: names for attributes within a record
71 // format "prop1" or "prop/subprop"
72 // XXX: need to better define the format
Ray Essickb5fac8e2016-12-12 11:33:56 -080073 typedef const char *Attr;
Ray Essick3938dc62016-11-01 08:56:56 -070074
75
76 public:
77
78 // access functions for the class
79 MediaAnalyticsItem();
80 MediaAnalyticsItem(Key);
81 ~MediaAnalyticsItem();
82
83 // so clients can send intermediate values to be overlaid later
84 MediaAnalyticsItem &setFinalized(bool);
85 bool getFinalized() const;
86
87 // SessionID ties multiple submissions for same key together
88 // so that if video "height" and "width" are known at one point
89 // and "framerate" is only known later, they can be be brought
90 // together.
91 MediaAnalyticsItem &setSessionID(SessionID_t);
92 MediaAnalyticsItem &clearSessionID();
93 SessionID_t getSessionID() const;
94 // generates and stores a new ID iff mSessionID == SessionIDNone
95 SessionID_t generateSessionID();
96
97 // reset all contents, discarding any extra data
98 void clear();
Ray Essickb5fac8e2016-12-12 11:33:56 -080099 MediaAnalyticsItem *dup();
Ray Essick3938dc62016-11-01 08:56:56 -0700100
101 // set the key discriminator for the record.
102 // most often initialized as part of the constructor
103 MediaAnalyticsItem &setKey(MediaAnalyticsItem::Key);
104 MediaAnalyticsItem::Key getKey();
105
106 // # of attributes in the record
107 int32_t count() const;
108
109 // set values appropriately
Ray Essickb5fac8e2016-12-12 11:33:56 -0800110 void setInt32(Attr, int32_t value);
111 void setInt64(Attr, int64_t value);
112 void setDouble(Attr, double value);
113 void setRate(Attr, int64_t count, int64_t duration);
114 void setCString(Attr, const char *value);
Ray Essick3938dc62016-11-01 08:56:56 -0700115
116 // fused get/add/set; if attr wasn't there, it's a simple set.
117 // type-mismatch counts as "wasn't there".
Ray Essickb5fac8e2016-12-12 11:33:56 -0800118 void addInt32(Attr, int32_t value);
119 void addInt64(Attr, int64_t value);
120 void addDouble(Attr, double value);
121 void addRate(Attr, int64_t count, int64_t duration);
Ray Essick3938dc62016-11-01 08:56:56 -0700122
123 // find & extract values
124 // return indicates whether attr exists (and thus value filled in)
Ray Essickb5fac8e2016-12-12 11:33:56 -0800125 // NULL parameter value suppresses storage of value.
Ray Essick3938dc62016-11-01 08:56:56 -0700126 bool getInt32(Attr, int32_t *value);
127 bool getInt64(Attr, int64_t *value);
128 bool getDouble(Attr, double *value);
Ray Essickb5fac8e2016-12-12 11:33:56 -0800129 bool getRate(Attr, int64_t *count, int64_t *duration, double *rate);
130 // Caller owns the returned string
Ray Essick3938dc62016-11-01 08:56:56 -0700131 bool getCString(Attr, char **value);
132
133 // parameter indicates whether to close any existing open
134 // record with same key before establishing a new record
Ray Essickb5fac8e2016-12-12 11:33:56 -0800135 // caller retains ownership of 'this'.
Ray Essick3938dc62016-11-01 08:56:56 -0700136 bool selfrecord(bool);
137 bool selfrecord();
138
139 // remove indicated attributes and their values
140 // filterNot() could also be called keepOnly()
141 // return value is # attributes removed
142 // XXX: perhaps 'remove' instead of 'filter'
143 // XXX: filterNot would become 'keep'
144 int32_t filter(int count, Attr attrs[]);
145 int32_t filterNot(int count, Attr attrs[]);
146 int32_t filter(Attr attr);
147
148 // below here are used on server side or to talk to server
149 // clients need not worry about these.
150
151 // timestamp, pid, and uid only used on server side
Ray Essickb5fac8e2016-12-12 11:33:56 -0800152 // timestamp is in 'nanoseconds, unix time'
Ray Essick3938dc62016-11-01 08:56:56 -0700153 MediaAnalyticsItem &setTimestamp(nsecs_t);
154 nsecs_t getTimestamp() const;
155
156 MediaAnalyticsItem &setPid(pid_t);
157 pid_t getPid() const;
158
159 MediaAnalyticsItem &setUid(uid_t);
160 uid_t getUid() const;
161
162 // our serialization code for binder calls
163 int32_t writeToParcel(Parcel *);
164 int32_t readFromParcel(const Parcel&);
165
166 AString toString();
167
168 // are we collecting analytics data
169 static bool isEnabled();
170
171 protected:
172
173 // merge fields from arg into this
174 // with rules for first/last/add, etc
175 // XXX: document semantics and how they are indicated
Ray Essickb5fac8e2016-12-12 11:33:56 -0800176 // caller continues to own 'incoming'
177 bool merge(MediaAnalyticsItem *incoming);
Ray Essick3938dc62016-11-01 08:56:56 -0700178
179 // enabled 1, disabled 0
180 static const char * const EnabledProperty;
181 static const char * const EnabledPropertyPersist;
182 static const int EnabledProperty_default;
183
184 private:
185
186 // to help validate that A doesn't mess with B's records
187 pid_t mPid;
188 uid_t mUid;
189
190 // let's reuse a binder connection
191 static sp<IMediaAnalyticsService> sAnalyticsService;
192 static sp<IMediaAnalyticsService> getInstance();
193
194 // tracking information
195 SessionID_t mSessionID; // grouping similar records
196 nsecs_t mTimestamp; // ns, system_time_monotonic
197
198 // will this record accept further updates
199 bool mFinalized;
200
201 Key mKey;
202
Ray Essickb5fac8e2016-12-12 11:33:56 -0800203 struct Prop {
Ray Essick3938dc62016-11-01 08:56:56 -0700204
205 Type mType;
Ray Essickb5fac8e2016-12-12 11:33:56 -0800206 const char *mName;
207 size_t mNameLen; // the strlen(), doesn't include the null
Ray Essick3938dc62016-11-01 08:56:56 -0700208 union {
209 int32_t int32Value;
210 int64_t int64Value;
211 double doubleValue;
212 char *CStringValue;
Ray Essickb5fac8e2016-12-12 11:33:56 -0800213 struct { int64_t count, duration; } rate;
Ray Essick3938dc62016-11-01 08:56:56 -0700214 } u;
Ray Essickb5fac8e2016-12-12 11:33:56 -0800215 void setName(const char *name, size_t len);
Ray Essick3938dc62016-11-01 08:56:56 -0700216 };
Ray Essickb5fac8e2016-12-12 11:33:56 -0800217
218 void initProp(Prop *item);
219 void clearProp(Prop *item);
220 void clearPropValue(Prop *item);
221 void copyProp(Prop *dst, const Prop *src);
222 enum {
223 kGrowProps = 10
224 };
225 void growProps(int increment = kGrowProps);
226 size_t findPropIndex(const char *name, size_t len);
227 Prop *findProp(const char *name);
228 Prop *allocateProp(const char *name);
229
230 size_t mPropCount;
231 size_t mPropSize;
232 Prop *mProps;
Ray Essick3938dc62016-11-01 08:56:56 -0700233
234};
235
236} // namespace android
237
238#endif