Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 1 | /* |
| 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 Essick | 783bd0d | 2018-01-11 11:10:35 -0800 | [diff] [blame^] | 21 | #include <string> |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 22 | #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 Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 29 | namespace android { |
| 30 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 31 | class IMediaAnalyticsService; |
Ray Essick | 783bd0d | 2018-01-11 11:10:35 -0800 | [diff] [blame^] | 32 | class Parcel; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 33 | |
| 34 | // the class interface |
| 35 | // |
| 36 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 37 | class MediaAnalyticsItem { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 38 | |
| 39 | friend class MediaAnalyticsService; |
| 40 | friend class IMediaAnalyticsService; |
Ray Essick | e08ab77 | 2017-01-25 17:47:51 -0800 | [diff] [blame] | 41 | friend class MediaMetricsJNI; |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 42 | friend class MetricsSummarizer; |
Ray Essick | 2ab3c43 | 2017-10-02 09:29:49 -0700 | [diff] [blame] | 43 | friend class MediaMetricsDeathNotifier; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 44 | |
| 45 | public: |
| 46 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 47 | enum Type { |
| 48 | kTypeNone = 0, |
| 49 | kTypeInt32 = 1, |
| 50 | kTypeInt64 = 2, |
| 51 | kTypeDouble = 3, |
| 52 | kTypeCString = 4, |
| 53 | kTypeRate = 5, |
| 54 | }; |
| 55 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 56 | // 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 Essick | 783bd0d | 2018-01-11 11:10:35 -0800 | [diff] [blame^] | 67 | typedef std::string Key; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 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 Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 74 | typedef const char *Attr; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 75 | |
| 76 | |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 77 | enum { |
| 78 | PROTO_V0 = 0, |
| 79 | PROTO_FIRST = PROTO_V0, |
| 80 | PROTO_V1 = 1, |
| 81 | PROTO_LAST = PROTO_V1, |
| 82 | }; |
| 83 | |
| 84 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 85 | public: |
| 86 | |
| 87 | // access functions for the class |
| 88 | MediaAnalyticsItem(); |
| 89 | MediaAnalyticsItem(Key); |
| 90 | ~MediaAnalyticsItem(); |
| 91 | |
| 92 | // so clients can send intermediate values to be overlaid later |
| 93 | MediaAnalyticsItem &setFinalized(bool); |
| 94 | bool getFinalized() const; |
| 95 | |
| 96 | // SessionID ties multiple submissions for same key together |
| 97 | // so that if video "height" and "width" are known at one point |
| 98 | // and "framerate" is only known later, they can be be brought |
| 99 | // together. |
| 100 | MediaAnalyticsItem &setSessionID(SessionID_t); |
| 101 | MediaAnalyticsItem &clearSessionID(); |
| 102 | SessionID_t getSessionID() const; |
| 103 | // generates and stores a new ID iff mSessionID == SessionIDNone |
| 104 | SessionID_t generateSessionID(); |
| 105 | |
| 106 | // reset all contents, discarding any extra data |
| 107 | void clear(); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 108 | MediaAnalyticsItem *dup(); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 109 | |
| 110 | // set the key discriminator for the record. |
| 111 | // most often initialized as part of the constructor |
| 112 | MediaAnalyticsItem &setKey(MediaAnalyticsItem::Key); |
| 113 | MediaAnalyticsItem::Key getKey(); |
| 114 | |
| 115 | // # of attributes in the record |
| 116 | int32_t count() const; |
| 117 | |
| 118 | // set values appropriately |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 119 | void setInt32(Attr, int32_t value); |
| 120 | void setInt64(Attr, int64_t value); |
| 121 | void setDouble(Attr, double value); |
| 122 | void setRate(Attr, int64_t count, int64_t duration); |
| 123 | void setCString(Attr, const char *value); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 124 | |
| 125 | // fused get/add/set; if attr wasn't there, it's a simple set. |
| 126 | // type-mismatch counts as "wasn't there". |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 127 | void addInt32(Attr, int32_t value); |
| 128 | void addInt64(Attr, int64_t value); |
| 129 | void addDouble(Attr, double value); |
| 130 | void addRate(Attr, int64_t count, int64_t duration); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 131 | |
| 132 | // find & extract values |
| 133 | // return indicates whether attr exists (and thus value filled in) |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 134 | // NULL parameter value suppresses storage of value. |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 135 | bool getInt32(Attr, int32_t *value); |
| 136 | bool getInt64(Attr, int64_t *value); |
| 137 | bool getDouble(Attr, double *value); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 138 | bool getRate(Attr, int64_t *count, int64_t *duration, double *rate); |
| 139 | // Caller owns the returned string |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 140 | bool getCString(Attr, char **value); |
| 141 | |
| 142 | // parameter indicates whether to close any existing open |
| 143 | // record with same key before establishing a new record |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 144 | // caller retains ownership of 'this'. |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 145 | bool selfrecord(bool); |
| 146 | bool selfrecord(); |
| 147 | |
| 148 | // remove indicated attributes and their values |
| 149 | // filterNot() could also be called keepOnly() |
| 150 | // return value is # attributes removed |
| 151 | // XXX: perhaps 'remove' instead of 'filter' |
| 152 | // XXX: filterNot would become 'keep' |
| 153 | int32_t filter(int count, Attr attrs[]); |
| 154 | int32_t filterNot(int count, Attr attrs[]); |
| 155 | int32_t filter(Attr attr); |
| 156 | |
| 157 | // below here are used on server side or to talk to server |
| 158 | // clients need not worry about these. |
| 159 | |
| 160 | // timestamp, pid, and uid only used on server side |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 161 | // timestamp is in 'nanoseconds, unix time' |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 162 | MediaAnalyticsItem &setTimestamp(nsecs_t); |
| 163 | nsecs_t getTimestamp() const; |
| 164 | |
| 165 | MediaAnalyticsItem &setPid(pid_t); |
| 166 | pid_t getPid() const; |
| 167 | |
| 168 | MediaAnalyticsItem &setUid(uid_t); |
| 169 | uid_t getUid() const; |
| 170 | |
Ray Essick | 783bd0d | 2018-01-11 11:10:35 -0800 | [diff] [blame^] | 171 | MediaAnalyticsItem &setPkgName(const std::string &pkgName); |
| 172 | std::string getPkgName() const { return mPkgName; } |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 173 | |
Dianne Hackborn | 4e2eeff | 2017-11-27 14:01:29 -0800 | [diff] [blame] | 174 | MediaAnalyticsItem &setPkgVersionCode(int64_t); |
| 175 | int64_t getPkgVersionCode() const; |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 176 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 177 | // our serialization code for binder calls |
| 178 | int32_t writeToParcel(Parcel *); |
| 179 | int32_t readFromParcel(const Parcel&); |
| 180 | |
Ray Essick | 783bd0d | 2018-01-11 11:10:35 -0800 | [diff] [blame^] | 181 | std::string toString(); |
| 182 | std::string toString(int version); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 183 | |
| 184 | // are we collecting analytics data |
| 185 | static bool isEnabled(); |
| 186 | |
| 187 | protected: |
| 188 | |
| 189 | // merge fields from arg into this |
| 190 | // with rules for first/last/add, etc |
| 191 | // XXX: document semantics and how they are indicated |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 192 | // caller continues to own 'incoming' |
| 193 | bool merge(MediaAnalyticsItem *incoming); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 194 | |
| 195 | // enabled 1, disabled 0 |
| 196 | static const char * const EnabledProperty; |
| 197 | static const char * const EnabledPropertyPersist; |
| 198 | static const int EnabledProperty_default; |
| 199 | |
| 200 | private: |
| 201 | |
| 202 | // to help validate that A doesn't mess with B's records |
| 203 | pid_t mPid; |
| 204 | uid_t mUid; |
Ray Essick | 783bd0d | 2018-01-11 11:10:35 -0800 | [diff] [blame^] | 205 | std::string mPkgName; |
Dianne Hackborn | 4e2eeff | 2017-11-27 14:01:29 -0800 | [diff] [blame] | 206 | int64_t mPkgVersionCode; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 207 | |
| 208 | // let's reuse a binder connection |
| 209 | static sp<IMediaAnalyticsService> sAnalyticsService; |
| 210 | static sp<IMediaAnalyticsService> getInstance(); |
Ray Essick | 2ab3c43 | 2017-10-02 09:29:49 -0700 | [diff] [blame] | 211 | static void dropInstance(); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 212 | |
| 213 | // tracking information |
| 214 | SessionID_t mSessionID; // grouping similar records |
| 215 | nsecs_t mTimestamp; // ns, system_time_monotonic |
| 216 | |
| 217 | // will this record accept further updates |
| 218 | bool mFinalized; |
| 219 | |
| 220 | Key mKey; |
| 221 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 222 | struct Prop { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 223 | |
| 224 | Type mType; |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 225 | const char *mName; |
| 226 | size_t mNameLen; // the strlen(), doesn't include the null |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 227 | union { |
| 228 | int32_t int32Value; |
| 229 | int64_t int64Value; |
| 230 | double doubleValue; |
| 231 | char *CStringValue; |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 232 | struct { int64_t count, duration; } rate; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 233 | } u; |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 234 | void setName(const char *name, size_t len); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 235 | }; |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 236 | |
| 237 | void initProp(Prop *item); |
| 238 | void clearProp(Prop *item); |
| 239 | void clearPropValue(Prop *item); |
| 240 | void copyProp(Prop *dst, const Prop *src); |
| 241 | enum { |
| 242 | kGrowProps = 10 |
| 243 | }; |
Ray Essick | 9bb7e3b | 2017-10-23 13:01:48 -0700 | [diff] [blame] | 244 | bool growProps(int increment = kGrowProps); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 245 | size_t findPropIndex(const char *name, size_t len); |
| 246 | Prop *findProp(const char *name); |
| 247 | Prop *allocateProp(const char *name); |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 248 | bool removeProp(const char *name); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 249 | |
| 250 | size_t mPropCount; |
| 251 | size_t mPropSize; |
| 252 | Prop *mProps; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 253 | }; |
| 254 | |
| 255 | } // namespace android |
| 256 | |
| 257 | #endif |