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> |
| 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 | |
| 30 | namespace android { |
| 31 | |
| 32 | |
| 33 | |
| 34 | class IMediaAnalyticsService; |
| 35 | |
| 36 | // the class interface |
| 37 | // |
| 38 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame^] | 39 | class MediaAnalyticsItem { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 40 | |
| 41 | friend class MediaAnalyticsService; |
| 42 | friend class IMediaAnalyticsService; |
| 43 | |
| 44 | public: |
| 45 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame^] | 46 | enum Type { |
| 47 | kTypeNone = 0, |
| 48 | kTypeInt32 = 1, |
| 49 | kTypeInt64 = 2, |
| 50 | kTypeDouble = 3, |
| 51 | kTypeCString = 4, |
| 52 | kTypeRate = 5, |
| 53 | }; |
| 54 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 55 | // 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 Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame^] | 73 | typedef const char *Attr; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 74 | |
| 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 Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame^] | 99 | MediaAnalyticsItem *dup(); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 100 | |
| 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 Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame^] | 110 | 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 Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 115 | |
| 116 | // fused get/add/set; if attr wasn't there, it's a simple set. |
| 117 | // type-mismatch counts as "wasn't there". |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame^] | 118 | 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 Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 122 | |
| 123 | // find & extract values |
| 124 | // return indicates whether attr exists (and thus value filled in) |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame^] | 125 | // NULL parameter value suppresses storage of value. |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 126 | bool getInt32(Attr, int32_t *value); |
| 127 | bool getInt64(Attr, int64_t *value); |
| 128 | bool getDouble(Attr, double *value); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame^] | 129 | bool getRate(Attr, int64_t *count, int64_t *duration, double *rate); |
| 130 | // Caller owns the returned string |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 131 | 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 Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame^] | 135 | // caller retains ownership of 'this'. |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 136 | 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 Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame^] | 152 | // timestamp is in 'nanoseconds, unix time' |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 153 | 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 Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame^] | 176 | // caller continues to own 'incoming' |
| 177 | bool merge(MediaAnalyticsItem *incoming); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 178 | |
| 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 Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame^] | 203 | struct Prop { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 204 | |
| 205 | Type mType; |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame^] | 206 | const char *mName; |
| 207 | size_t mNameLen; // the strlen(), doesn't include the null |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 208 | union { |
| 209 | int32_t int32Value; |
| 210 | int64_t int64Value; |
| 211 | double doubleValue; |
| 212 | char *CStringValue; |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame^] | 213 | struct { int64_t count, duration; } rate; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 214 | } u; |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame^] | 215 | void setName(const char *name, size_t len); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 216 | }; |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame^] | 217 | |
| 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 Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 233 | |
| 234 | }; |
| 235 | |
| 236 | } // namespace android |
| 237 | |
| 238 | #endif |