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