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 | |
| 39 | class MediaAnalyticsItem : public RefBase { |
| 40 | |
| 41 | friend class MediaAnalyticsService; |
| 42 | friend class IMediaAnalyticsService; |
| 43 | |
| 44 | public: |
| 45 | |
| 46 | // sessionid |
| 47 | // unique within device, within boot, |
| 48 | typedef int64_t SessionID_t; |
| 49 | static constexpr SessionID_t SessionIDInvalid = -1; |
| 50 | static constexpr SessionID_t SessionIDNone = 0; |
| 51 | |
| 52 | // Key: the record descriminator |
| 53 | // values for the record discriminator |
| 54 | // values can be "component/component" |
| 55 | // basic values: "video", "audio", "drm" |
| 56 | // XXX: need to better define the format |
| 57 | typedef AString Key; |
| 58 | static const Key kKeyNone; // "" |
| 59 | static const Key kKeyAny; // "*" |
| 60 | |
| 61 | // Attr: names for attributes within a record |
| 62 | // format "prop1" or "prop/subprop" |
| 63 | // XXX: need to better define the format |
| 64 | typedef AString Attr; |
| 65 | |
| 66 | |
| 67 | public: |
| 68 | |
| 69 | // access functions for the class |
| 70 | MediaAnalyticsItem(); |
| 71 | MediaAnalyticsItem(Key); |
| 72 | ~MediaAnalyticsItem(); |
| 73 | |
| 74 | // so clients can send intermediate values to be overlaid later |
| 75 | MediaAnalyticsItem &setFinalized(bool); |
| 76 | bool getFinalized() const; |
| 77 | |
| 78 | // SessionID ties multiple submissions for same key together |
| 79 | // so that if video "height" and "width" are known at one point |
| 80 | // and "framerate" is only known later, they can be be brought |
| 81 | // together. |
| 82 | MediaAnalyticsItem &setSessionID(SessionID_t); |
| 83 | MediaAnalyticsItem &clearSessionID(); |
| 84 | SessionID_t getSessionID() const; |
| 85 | // generates and stores a new ID iff mSessionID == SessionIDNone |
| 86 | SessionID_t generateSessionID(); |
| 87 | |
| 88 | // reset all contents, discarding any extra data |
| 89 | void clear(); |
| 90 | |
| 91 | // set the key discriminator for the record. |
| 92 | // most often initialized as part of the constructor |
| 93 | MediaAnalyticsItem &setKey(MediaAnalyticsItem::Key); |
| 94 | MediaAnalyticsItem::Key getKey(); |
| 95 | |
| 96 | // # of attributes in the record |
| 97 | int32_t count() const; |
| 98 | |
| 99 | // set values appropriately |
| 100 | // return values tell us whether we overwrote an existing value |
| 101 | bool setInt32(Attr, int32_t value); |
| 102 | bool setInt64(Attr, int64_t value); |
| 103 | bool setDouble(Attr, double value); |
| 104 | bool setCString(Attr, const char *value); |
| 105 | |
| 106 | // fused get/add/set; if attr wasn't there, it's a simple set. |
| 107 | // type-mismatch counts as "wasn't there". |
| 108 | // return value tells us whether we overwrote an existing value |
| 109 | bool addInt32(Attr, int32_t value); |
| 110 | bool addInt64(Attr, int64_t value); |
| 111 | bool addDouble(Attr, double value); |
| 112 | |
| 113 | // find & extract values |
| 114 | // return indicates whether attr exists (and thus value filled in) |
| 115 | bool getInt32(Attr, int32_t *value); |
| 116 | bool getInt64(Attr, int64_t *value); |
| 117 | bool getDouble(Attr, double *value); |
| 118 | bool getCString(Attr, char **value); |
| 119 | |
| 120 | // parameter indicates whether to close any existing open |
| 121 | // record with same key before establishing a new record |
| 122 | bool selfrecord(bool); |
| 123 | bool selfrecord(); |
| 124 | |
| 125 | // remove indicated attributes and their values |
| 126 | // filterNot() could also be called keepOnly() |
| 127 | // return value is # attributes removed |
| 128 | // XXX: perhaps 'remove' instead of 'filter' |
| 129 | // XXX: filterNot would become 'keep' |
| 130 | int32_t filter(int count, Attr attrs[]); |
| 131 | int32_t filterNot(int count, Attr attrs[]); |
| 132 | int32_t filter(Attr attr); |
| 133 | |
| 134 | // below here are used on server side or to talk to server |
| 135 | // clients need not worry about these. |
| 136 | |
| 137 | // timestamp, pid, and uid only used on server side |
| 138 | // timestamp is in 'nanoseconds, unix time' |
| 139 | MediaAnalyticsItem &setTimestamp(nsecs_t); |
| 140 | nsecs_t getTimestamp() const; |
| 141 | |
| 142 | MediaAnalyticsItem &setPid(pid_t); |
| 143 | pid_t getPid() const; |
| 144 | |
| 145 | MediaAnalyticsItem &setUid(uid_t); |
| 146 | uid_t getUid() const; |
| 147 | |
| 148 | // our serialization code for binder calls |
| 149 | int32_t writeToParcel(Parcel *); |
| 150 | int32_t readFromParcel(const Parcel&); |
| 151 | |
| 152 | AString toString(); |
| 153 | |
| 154 | // are we collecting analytics data |
| 155 | static bool isEnabled(); |
| 156 | |
| 157 | protected: |
| 158 | |
| 159 | // merge fields from arg into this |
| 160 | // with rules for first/last/add, etc |
| 161 | // XXX: document semantics and how they are indicated |
| 162 | bool merge(sp<MediaAnalyticsItem> ); |
| 163 | |
| 164 | // enabled 1, disabled 0 |
| 165 | static const char * const EnabledProperty; |
| 166 | static const char * const EnabledPropertyPersist; |
| 167 | static const int EnabledProperty_default; |
| 168 | |
| 169 | private: |
| 170 | |
| 171 | // to help validate that A doesn't mess with B's records |
| 172 | pid_t mPid; |
| 173 | uid_t mUid; |
| 174 | |
| 175 | // let's reuse a binder connection |
| 176 | static sp<IMediaAnalyticsService> sAnalyticsService; |
| 177 | static sp<IMediaAnalyticsService> getInstance(); |
| 178 | |
| 179 | // tracking information |
| 180 | SessionID_t mSessionID; // grouping similar records |
| 181 | nsecs_t mTimestamp; // ns, system_time_monotonic |
| 182 | |
| 183 | // will this record accept further updates |
| 184 | bool mFinalized; |
| 185 | |
| 186 | Key mKey; |
| 187 | |
| 188 | class Item : public RefBase { |
| 189 | |
| 190 | public: |
| 191 | |
| 192 | enum Type { |
| 193 | kTypeNone = 0, |
| 194 | kTypeInt32 = 1, |
| 195 | kTypeInt64 = 2, |
| 196 | kTypeDouble = 3, |
| 197 | kTypeCString = 4, |
| 198 | }; |
| 199 | |
| 200 | Item(); |
| 201 | ~Item(); |
| 202 | void clear(); |
| 203 | |
| 204 | Type mType; |
| 205 | union { |
| 206 | int32_t int32Value; |
| 207 | int64_t int64Value; |
| 208 | double doubleValue; |
| 209 | char *CStringValue; |
| 210 | } u; |
| 211 | }; |
| 212 | KeyedVector<Attr, sp<Item>> mItems; |
| 213 | |
| 214 | }; |
| 215 | |
| 216 | } // namespace android |
| 217 | |
| 218 | #endif |