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 | // Proxy for media player implementations |
| 18 | |
| 19 | //#define LOG_NDEBUG 0 |
| 20 | #define LOG_TAG "MediaAnalyticsService" |
| 21 | #include <utils/Log.h> |
| 22 | |
| 23 | #include <inttypes.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <sys/stat.h> |
| 26 | #include <sys/time.h> |
| 27 | #include <dirent.h> |
| 28 | #include <unistd.h> |
| 29 | |
| 30 | #include <string.h> |
| 31 | |
| 32 | #include <cutils/atomic.h> |
| 33 | #include <cutils/properties.h> // for property_get |
| 34 | |
| 35 | #include <utils/misc.h> |
| 36 | |
| 37 | #include <binder/IPCThreadState.h> |
| 38 | #include <binder/IServiceManager.h> |
| 39 | #include <binder/MemoryHeapBase.h> |
| 40 | #include <binder/MemoryBase.h> |
| 41 | #include <gui/Surface.h> |
| 42 | #include <utils/Errors.h> // for status_t |
| 43 | #include <utils/List.h> |
| 44 | #include <utils/String8.h> |
| 45 | #include <utils/SystemClock.h> |
| 46 | #include <utils/Timers.h> |
| 47 | #include <utils/Vector.h> |
| 48 | |
| 49 | #include <media/AudioPolicyHelper.h> |
| 50 | #include <media/IMediaHTTPService.h> |
| 51 | #include <media/IRemoteDisplay.h> |
| 52 | #include <media/IRemoteDisplayClient.h> |
| 53 | #include <media/MediaPlayerInterface.h> |
| 54 | #include <media/mediarecorder.h> |
| 55 | #include <media/MediaMetadataRetrieverInterface.h> |
| 56 | #include <media/Metadata.h> |
| 57 | #include <media/AudioTrack.h> |
| 58 | #include <media/MemoryLeakTrackUtil.h> |
| 59 | #include <media/stagefright/MediaCodecList.h> |
| 60 | #include <media/stagefright/MediaErrors.h> |
| 61 | #include <media/stagefright/Utils.h> |
| 62 | #include <media/stagefright/foundation/ADebug.h> |
| 63 | #include <media/stagefright/foundation/ALooperRoster.h> |
| 64 | #include <mediautils/BatteryNotifier.h> |
| 65 | |
| 66 | //#include <memunreachable/memunreachable.h> |
| 67 | #include <system/audio.h> |
| 68 | |
| 69 | #include <private/android_filesystem_config.h> |
| 70 | |
| 71 | #include "MediaAnalyticsService.h" |
| 72 | |
| 73 | |
| 74 | namespace android { |
| 75 | |
| 76 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 77 | #define DEBUG_QUEUE 0 |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 78 | |
| 79 | //using android::status_t; |
| 80 | //using android::OK; |
| 81 | //using android::BAD_VALUE; |
| 82 | //using android::NOT_ENOUGH_DATA; |
| 83 | //using android::Parcel; |
| 84 | |
| 85 | |
| 86 | void MediaAnalyticsService::instantiate() { |
| 87 | defaultServiceManager()->addService( |
Ray Essick | d38e174 | 2017-01-23 15:17:06 -0800 | [diff] [blame] | 88 | String16("media.metrics"), new MediaAnalyticsService()); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | // XXX: add dynamic controls for mMaxRecords |
| 92 | MediaAnalyticsService::MediaAnalyticsService() |
| 93 | : mMaxRecords(100) { |
| 94 | |
| 95 | ALOGD("MediaAnalyticsService created"); |
| 96 | // clear our queues |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 97 | mOpen = new List<MediaAnalyticsItem *>(); |
| 98 | mFinalized = new List<MediaAnalyticsItem *>(); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 99 | |
| 100 | mItemsSubmitted = 0; |
| 101 | mItemsFinalized = 0; |
| 102 | mItemsDiscarded = 0; |
| 103 | |
| 104 | mLastSessionID = 0; |
| 105 | // recover any persistency we set up |
| 106 | // etc |
| 107 | } |
| 108 | |
| 109 | MediaAnalyticsService::~MediaAnalyticsService() { |
| 110 | ALOGD("MediaAnalyticsService destroyed"); |
| 111 | |
| 112 | // XXX: clean out mOpen and mFinalized |
| 113 | } |
| 114 | |
| 115 | |
| 116 | MediaAnalyticsItem::SessionID_t MediaAnalyticsService::generateUniqueSessionID() { |
| 117 | // generate a new sessionid |
| 118 | |
| 119 | Mutex::Autolock _l(mLock_ids); |
| 120 | return (++mLastSessionID); |
| 121 | } |
| 122 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 123 | // caller surrenders ownership of 'item' |
| 124 | MediaAnalyticsItem::SessionID_t MediaAnalyticsService::submit(MediaAnalyticsItem *item, bool forcenew) { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 125 | |
| 126 | MediaAnalyticsItem::SessionID_t id = MediaAnalyticsItem::SessionIDInvalid; |
| 127 | |
Ray Essick | d38e174 | 2017-01-23 15:17:06 -0800 | [diff] [blame] | 128 | // we control these, generally not trusting user input |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 129 | nsecs_t now = systemTime(SYSTEM_TIME_REALTIME); |
| 130 | item->setTimestamp(now); |
| 131 | int pid = IPCThreadState::self()->getCallingPid(); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 132 | int uid = IPCThreadState::self()->getCallingUid(); |
Ray Essick | d38e174 | 2017-01-23 15:17:06 -0800 | [diff] [blame] | 133 | |
| 134 | int uid_given = item->getUid(); |
| 135 | int pid_given = item->getPid(); |
| 136 | |
| 137 | // although we do make exceptions for particular client uids |
| 138 | // that we know we trust. |
| 139 | // |
| 140 | bool isTrusted = false; |
| 141 | |
| 142 | switch (uid) { |
| 143 | case AID_MEDIA: |
| 144 | case AID_MEDIA_CODEC: |
| 145 | case AID_MEDIA_EX: |
| 146 | case AID_MEDIA_DRM: |
| 147 | // trusted source, only override default values |
| 148 | isTrusted = true; |
| 149 | if (uid_given == (-1)) { |
| 150 | item->setUid(uid); |
| 151 | } |
| 152 | if (pid_given == (-1)) { |
| 153 | item->setPid(pid); |
| 154 | } |
| 155 | break; |
| 156 | default: |
| 157 | isTrusted = false; |
| 158 | item->setPid(pid); |
| 159 | item->setUid(uid); |
| 160 | break; |
| 161 | } |
| 162 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 163 | |
| 164 | mItemsSubmitted++; |
| 165 | |
| 166 | // validate the record; we discard if we don't like it |
Ray Essick | d38e174 | 2017-01-23 15:17:06 -0800 | [diff] [blame] | 167 | if (contentValid(item, isTrusted) == false) { |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 168 | delete item; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 169 | return MediaAnalyticsItem::SessionIDInvalid; |
| 170 | } |
| 171 | |
| 172 | |
| 173 | // if we have a sesisonid in the new record, look to make |
| 174 | // sure it doesn't appear in the finalized list. |
| 175 | // XXX: this is for security / DOS prevention. |
| 176 | // may also require that we persist the unique sessionIDs |
| 177 | // across boots [instead of within a single boot] |
| 178 | |
| 179 | |
| 180 | // match this new record up against records in the open |
| 181 | // list... |
| 182 | // if there's a match, merge them together |
| 183 | // deal with moving the old / merged record into the finalized que |
| 184 | |
| 185 | bool finalizing = item->getFinalized(); |
| 186 | |
| 187 | // if finalizing, we'll remove it |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 188 | MediaAnalyticsItem *oitem = findItem(mOpen, item, finalizing | forcenew); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 189 | if (oitem != NULL) { |
| 190 | if (forcenew) { |
| 191 | // old one gets finalized, then we insert the new one |
| 192 | // so we'll have 2 records at the end of this. |
| 193 | // but don't finalize an empty record |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 194 | if (oitem->count() == 0) { |
| 195 | // we're responsible for disposing of the dead record |
| 196 | delete oitem; |
| 197 | oitem = NULL; |
| 198 | } else { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 199 | oitem->setFinalized(true); |
| 200 | saveItem(mFinalized, oitem, 0); |
| 201 | } |
| 202 | // new record could itself be marked finalized... |
| 203 | if (finalizing) { |
| 204 | saveItem(mFinalized, item, 0); |
| 205 | mItemsFinalized++; |
| 206 | } else { |
| 207 | saveItem(mOpen, item, 1); |
| 208 | } |
| 209 | id = item->getSessionID(); |
| 210 | } else { |
| 211 | // combine the records, send it to finalized if appropriate |
| 212 | oitem->merge(item); |
| 213 | if (finalizing) { |
| 214 | saveItem(mFinalized, oitem, 0); |
| 215 | mItemsFinalized++; |
| 216 | } |
| 217 | id = oitem->getSessionID(); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 218 | |
| 219 | // we're responsible for disposing of the dead record |
| 220 | delete item; |
| 221 | item = NULL; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 222 | } |
| 223 | } else { |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 224 | // nothing to merge, save the new record |
| 225 | id = item->getSessionID(); |
| 226 | if (finalizing) { |
| 227 | if (item->count() == 0) { |
| 228 | // drop empty records |
| 229 | delete item; |
| 230 | item = NULL; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 231 | } else { |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 232 | saveItem(mFinalized, item, 0); |
| 233 | mItemsFinalized++; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 234 | } |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 235 | } else { |
| 236 | saveItem(mOpen, item, 1); |
| 237 | } |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 238 | } |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 239 | return id; |
| 240 | } |
| 241 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 242 | List<MediaAnalyticsItem *> *MediaAnalyticsService::getMediaAnalyticsItemList(bool finished, nsecs_t ts) { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 243 | // this might never get called; the binder interface maps to the full parm list |
| 244 | // on the client side before making the binder call. |
| 245 | // but this lets us be sure... |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 246 | List<MediaAnalyticsItem*> *list; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 247 | list = getMediaAnalyticsItemList(finished, ts, MediaAnalyticsItem::kKeyAny); |
| 248 | return list; |
| 249 | } |
| 250 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 251 | List<MediaAnalyticsItem *> *MediaAnalyticsService::getMediaAnalyticsItemList(bool , nsecs_t , MediaAnalyticsItem::Key ) { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 252 | |
| 253 | // XXX: implement the get-item-list semantics |
| 254 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 255 | List<MediaAnalyticsItem *> *list = NULL; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 256 | // set up our query on the persistent data |
| 257 | // slurp in all of the pieces |
| 258 | // return that |
| 259 | return list; |
| 260 | } |
| 261 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 262 | status_t MediaAnalyticsService::dump(int fd, const Vector<String16>& args) |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 263 | { |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 264 | const size_t SIZE = 512; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 265 | char buffer[SIZE]; |
| 266 | String8 result; |
| 267 | |
| 268 | if (checkCallingPermission(String16("android.permission.DUMP")) == false) { |
| 269 | snprintf(buffer, SIZE, "Permission Denial: " |
| 270 | "can't dump MediaAnalyticsService from pid=%d, uid=%d\n", |
| 271 | IPCThreadState::self()->getCallingPid(), |
| 272 | IPCThreadState::self()->getCallingUid()); |
| 273 | result.append(buffer); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 274 | write(fd, result.string(), result.size()); |
| 275 | return NO_ERROR; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 276 | } |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 277 | |
| 278 | // crack any parameters |
| 279 | bool clear = false; |
| 280 | nsecs_t ts_since = 0; |
| 281 | String16 clearOption("-clear"); |
| 282 | String16 sinceOption("-since"); |
| 283 | int n = args.size(); |
| 284 | for (int i = 0; i < n; i++) { |
| 285 | String8 myarg(args[i]); |
| 286 | if (args[i] == clearOption) { |
| 287 | clear = true; |
| 288 | } else if (args[i] == sinceOption) { |
| 289 | i++; |
| 290 | if (i < n) { |
| 291 | String8 value(args[i]); |
| 292 | char *endp; |
| 293 | const char *p = value.string(); |
| 294 | ts_since = strtoll(p, &endp, 10); |
| 295 | if (endp == p || *endp != '\0') { |
| 296 | ts_since = 0; |
| 297 | } |
| 298 | } else { |
| 299 | ts_since = 0; |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | Mutex::Autolock _l(mLock); |
| 305 | |
Ray Essick | d38e174 | 2017-01-23 15:17:06 -0800 | [diff] [blame] | 306 | snprintf(buffer, SIZE, "Dump of the mediametrics process:\n"); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 307 | result.append(buffer); |
| 308 | |
| 309 | int enabled = MediaAnalyticsItem::isEnabled(); |
| 310 | if (enabled) { |
Ray Essick | d38e174 | 2017-01-23 15:17:06 -0800 | [diff] [blame] | 311 | snprintf(buffer, SIZE, "Metrics gathering: enabled\n"); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 312 | } else { |
Ray Essick | d38e174 | 2017-01-23 15:17:06 -0800 | [diff] [blame] | 313 | snprintf(buffer, SIZE, "Metrics gathering: DISABLED via property\n"); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 314 | } |
| 315 | result.append(buffer); |
| 316 | |
| 317 | snprintf(buffer, SIZE, |
| 318 | "Since Boot: Submissions: %" PRId64 |
| 319 | " Finalizations: %" PRId64 |
| 320 | " Discarded: %" PRId64 "\n", |
| 321 | mItemsSubmitted, mItemsFinalized, mItemsDiscarded); |
| 322 | result.append(buffer); |
| 323 | if (ts_since != 0) { |
| 324 | snprintf(buffer, SIZE, |
| 325 | "Dumping Queue entries more recent than: %" PRId64 "\n", |
| 326 | (int64_t) ts_since); |
| 327 | result.append(buffer); |
| 328 | } |
| 329 | |
| 330 | // show the recently recorded records |
Ray Essick | d38e174 | 2017-01-23 15:17:06 -0800 | [diff] [blame] | 331 | snprintf(buffer, sizeof(buffer), "\nFinalized Metrics (oldest first):\n"); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 332 | result.append(buffer); |
| 333 | result.append(this->dumpQueue(mFinalized, ts_since)); |
| 334 | |
Ray Essick | d38e174 | 2017-01-23 15:17:06 -0800 | [diff] [blame] | 335 | snprintf(buffer, sizeof(buffer), "\nIn-Progress Metrics (newest first):\n"); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 336 | result.append(buffer); |
| 337 | result.append(this->dumpQueue(mOpen, ts_since)); |
| 338 | |
| 339 | // show who is connected and injecting records? |
| 340 | // talk about # records fed to the 'readers' |
| 341 | // talk about # records we discarded, perhaps "discarded w/o reading" too |
| 342 | |
| 343 | if (clear) { |
| 344 | // remove everything from the finalized queue |
| 345 | while (mFinalized->size() > 0) { |
| 346 | MediaAnalyticsItem * oitem = *(mFinalized->begin()); |
| 347 | if (DEBUG_QUEUE) { |
| 348 | ALOGD("zap old record: key %s sessionID %" PRId64 " ts %" PRId64 "", |
| 349 | oitem->getKey().c_str(), oitem->getSessionID(), |
| 350 | oitem->getTimestamp()); |
| 351 | } |
| 352 | mFinalized->erase(mFinalized->begin()); |
| 353 | mItemsDiscarded++; |
| 354 | } |
| 355 | } |
| 356 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 357 | write(fd, result.string(), result.size()); |
| 358 | return NO_ERROR; |
| 359 | } |
| 360 | |
| 361 | // caller has locked mLock... |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 362 | String8 MediaAnalyticsService::dumpQueue(List<MediaAnalyticsItem *> *theList) { |
| 363 | return dumpQueue(theList, (nsecs_t) 0); |
| 364 | } |
| 365 | |
| 366 | String8 MediaAnalyticsService::dumpQueue(List<MediaAnalyticsItem *> *theList, nsecs_t ts_since) { |
| 367 | const size_t SIZE = 512; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 368 | char buffer[SIZE]; |
| 369 | String8 result; |
| 370 | int slot = 0; |
| 371 | |
| 372 | if (theList->empty()) { |
| 373 | result.append("empty\n"); |
| 374 | } else { |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 375 | List<MediaAnalyticsItem *>::iterator it = theList->begin(); |
| 376 | for (; it != theList->end(); it++) { |
| 377 | nsecs_t when = (*it)->getTimestamp(); |
| 378 | if (when < ts_since) { |
| 379 | continue; |
| 380 | } |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 381 | AString entry = (*it)->toString(); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 382 | snprintf(buffer, sizeof(buffer), "%4d: %s", |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 383 | slot, entry.c_str()); |
| 384 | result.append(buffer); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 385 | buffer[0] = '\n'; |
| 386 | buffer[1] = '\0'; |
| 387 | result.append(buffer); |
| 388 | slot++; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 389 | } |
| 390 | } |
| 391 | |
| 392 | return result; |
| 393 | } |
| 394 | |
| 395 | // |
| 396 | // Our Cheap in-core, non-persistent records management. |
| 397 | // XXX: rewrite this to manage persistence, etc. |
| 398 | |
| 399 | // insert appropriately into queue |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 400 | void MediaAnalyticsService::saveItem(List<MediaAnalyticsItem *> *l, MediaAnalyticsItem * item, int front) { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 401 | |
| 402 | Mutex::Autolock _l(mLock); |
| 403 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 404 | if (DEBUG_QUEUE) { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 405 | ALOGD("Inject a record: session %" PRId64 " ts %" PRId64 "", |
| 406 | item->getSessionID(), item->getTimestamp()); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 407 | String8 before = dumpQueue(l); |
| 408 | ALOGD("Q before insert: %s", before.string()); |
| 409 | } |
| 410 | |
| 411 | // adding at back of queue (fifo order) |
| 412 | if (front) { |
| 413 | l->push_front(item); |
| 414 | } else { |
| 415 | l->push_back(item); |
| 416 | } |
| 417 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 418 | if (DEBUG_QUEUE) { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 419 | String8 after = dumpQueue(l); |
| 420 | ALOGD("Q after insert: %s", after.string()); |
| 421 | } |
| 422 | |
| 423 | // keep removing old records the front until we're in-bounds |
| 424 | if (mMaxRecords > 0) { |
| 425 | while (l->size() > (size_t) mMaxRecords) { |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 426 | MediaAnalyticsItem * oitem = *(l->begin()); |
| 427 | if (DEBUG_QUEUE) { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 428 | ALOGD("zap old record: key %s sessionID %" PRId64 " ts %" PRId64 "", |
| 429 | oitem->getKey().c_str(), oitem->getSessionID(), |
| 430 | oitem->getTimestamp()); |
| 431 | } |
| 432 | l->erase(l->begin()); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 433 | delete oitem; |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 434 | mItemsDiscarded++; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 435 | } |
| 436 | } |
| 437 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 438 | if (DEBUG_QUEUE) { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 439 | String8 after = dumpQueue(l); |
| 440 | ALOGD("Q after cleanup: %s", after.string()); |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | // are they alike enough that nitem can be folded into oitem? |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 445 | static bool compatibleItems(MediaAnalyticsItem * oitem, MediaAnalyticsItem * nitem) { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 446 | |
| 447 | if (0) { |
| 448 | ALOGD("Compare: o %s n %s", |
| 449 | oitem->toString().c_str(), nitem->toString().c_str()); |
| 450 | } |
| 451 | |
| 452 | // general safety |
| 453 | if (nitem->getUid() != oitem->getUid()) { |
| 454 | return false; |
| 455 | } |
| 456 | if (nitem->getPid() != oitem->getPid()) { |
| 457 | return false; |
| 458 | } |
| 459 | |
| 460 | // key -- needs to match |
| 461 | if (nitem->getKey() == oitem->getKey()) { |
| 462 | // still in the game. |
| 463 | } else { |
| 464 | return false; |
| 465 | } |
| 466 | |
| 467 | // session id -- empty field in new is allowed |
| 468 | MediaAnalyticsItem::SessionID_t osession = oitem->getSessionID(); |
| 469 | MediaAnalyticsItem::SessionID_t nsession = nitem->getSessionID(); |
| 470 | if (nsession != osession) { |
| 471 | // incoming '0' matches value in osession |
| 472 | if (nsession != 0) { |
| 473 | return false; |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | return true; |
| 478 | } |
| 479 | |
| 480 | // find the incomplete record that this will overlay |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 481 | MediaAnalyticsItem *MediaAnalyticsService::findItem(List<MediaAnalyticsItem*> *theList, MediaAnalyticsItem *nitem, bool removeit) { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 482 | if (nitem == NULL) { |
| 483 | return NULL; |
| 484 | } |
| 485 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 486 | MediaAnalyticsItem *item = NULL; |
| 487 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 488 | Mutex::Autolock _l(mLock); |
| 489 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 490 | for (List<MediaAnalyticsItem *>::iterator it = theList->begin(); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 491 | it != theList->end(); it++) { |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 492 | MediaAnalyticsItem *tmp = (*it); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 493 | |
| 494 | if (!compatibleItems(tmp, nitem)) { |
| 495 | continue; |
| 496 | } |
| 497 | |
| 498 | // we match! this is the one I want. |
| 499 | if (removeit) { |
| 500 | theList->erase(it); |
| 501 | } |
| 502 | item = tmp; |
| 503 | break; |
| 504 | } |
| 505 | return item; |
| 506 | } |
| 507 | |
| 508 | |
| 509 | // delete the indicated record |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 510 | void MediaAnalyticsService::deleteItem(List<MediaAnalyticsItem *> *l, MediaAnalyticsItem *item) { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 511 | |
| 512 | Mutex::Autolock _l(mLock); |
| 513 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 514 | if(DEBUG_QUEUE) { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 515 | String8 before = dumpQueue(l); |
| 516 | ALOGD("Q before delete: %s", before.string()); |
| 517 | } |
| 518 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 519 | for (List<MediaAnalyticsItem *>::iterator it = l->begin(); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 520 | it != l->end(); it++) { |
| 521 | if ((*it)->getSessionID() != item->getSessionID()) |
| 522 | continue; |
| 523 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 524 | if (DEBUG_QUEUE) { |
| 525 | ALOGD(" --- removing record for SessionID %" PRId64 "", item->getSessionID()); |
| 526 | ALOGD("drop record at %s:%d", __FILE__, __LINE__); |
| 527 | } |
| 528 | delete *it; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 529 | l->erase(it); |
| 530 | break; |
| 531 | } |
| 532 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 533 | if (DEBUG_QUEUE) { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 534 | String8 after = dumpQueue(l); |
| 535 | ALOGD("Q after delete: %s", after.string()); |
| 536 | } |
| 537 | } |
| 538 | |
Ray Essick | d38e174 | 2017-01-23 15:17:06 -0800 | [diff] [blame] | 539 | static AString allowedKeys[] = |
| 540 | { |
| 541 | "codec", |
| 542 | "extractor" |
| 543 | }; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 544 | |
Ray Essick | d38e174 | 2017-01-23 15:17:06 -0800 | [diff] [blame] | 545 | static const int nAllowedKeys = sizeof(allowedKeys) / sizeof(allowedKeys[0]); |
| 546 | |
| 547 | // are the contents good |
| 548 | bool MediaAnalyticsService::contentValid(MediaAnalyticsItem *item, bool isTrusted) { |
| 549 | |
| 550 | // untrusted uids can only send us a limited set of keys |
| 551 | if (isTrusted == false) { |
| 552 | // restrict to a specific set of keys |
| 553 | AString key = item->getKey(); |
| 554 | |
| 555 | size_t i; |
| 556 | for(i = 0; i < nAllowedKeys; i++) { |
| 557 | if (key == allowedKeys[i]) { |
| 558 | break; |
| 559 | } |
| 560 | } |
| 561 | if (i == nAllowedKeys) { |
| 562 | ALOGD("Ignoring (key): %s", item->toString().c_str()); |
| 563 | return false; |
| 564 | } |
| 565 | } |
| 566 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 567 | // internal consistency |
| 568 | |
| 569 | return true; |
| 570 | } |
| 571 | |
| 572 | // are we rate limited, normally false |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 573 | bool MediaAnalyticsService::rateLimited(MediaAnalyticsItem *) { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 574 | |
| 575 | return false; |
| 576 | } |
| 577 | |
| 578 | |
| 579 | } // namespace android |