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"); |
Ray Essick | 35ad27f | 2017-01-30 14:04:11 -0800 | [diff] [blame] | 283 | String16 helpOption("-help"); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 284 | int n = args.size(); |
| 285 | for (int i = 0; i < n; i++) { |
| 286 | String8 myarg(args[i]); |
| 287 | if (args[i] == clearOption) { |
| 288 | clear = true; |
| 289 | } else if (args[i] == sinceOption) { |
| 290 | i++; |
| 291 | if (i < n) { |
| 292 | String8 value(args[i]); |
| 293 | char *endp; |
| 294 | const char *p = value.string(); |
| 295 | ts_since = strtoll(p, &endp, 10); |
| 296 | if (endp == p || *endp != '\0') { |
| 297 | ts_since = 0; |
| 298 | } |
| 299 | } else { |
| 300 | ts_since = 0; |
| 301 | } |
Ray Essick | 35ad27f | 2017-01-30 14:04:11 -0800 | [diff] [blame] | 302 | // command line is milliseconds; internal units are nano-seconds |
| 303 | ts_since *= 1000*1000; |
| 304 | } else if (args[i] == helpOption) { |
| 305 | result.append("Recognized parameters:\n"); |
| 306 | result.append("-help this help message\n"); |
| 307 | result.append("-clear clears out saved records\n"); |
| 308 | result.append("-since XXX include records since XXX\n"); |
| 309 | result.append(" (XXX is milliseconds since the UNIX epoch)\n"); |
| 310 | write(fd, result.string(), result.size()); |
| 311 | return NO_ERROR; |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | |
| 315 | Mutex::Autolock _l(mLock); |
| 316 | |
Ray Essick | d38e174 | 2017-01-23 15:17:06 -0800 | [diff] [blame] | 317 | snprintf(buffer, SIZE, "Dump of the mediametrics process:\n"); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 318 | result.append(buffer); |
| 319 | |
| 320 | int enabled = MediaAnalyticsItem::isEnabled(); |
| 321 | if (enabled) { |
Ray Essick | d38e174 | 2017-01-23 15:17:06 -0800 | [diff] [blame] | 322 | snprintf(buffer, SIZE, "Metrics gathering: enabled\n"); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 323 | } else { |
Ray Essick | d38e174 | 2017-01-23 15:17:06 -0800 | [diff] [blame] | 324 | snprintf(buffer, SIZE, "Metrics gathering: DISABLED via property\n"); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 325 | } |
| 326 | result.append(buffer); |
| 327 | |
| 328 | snprintf(buffer, SIZE, |
| 329 | "Since Boot: Submissions: %" PRId64 |
| 330 | " Finalizations: %" PRId64 |
| 331 | " Discarded: %" PRId64 "\n", |
| 332 | mItemsSubmitted, mItemsFinalized, mItemsDiscarded); |
| 333 | result.append(buffer); |
| 334 | if (ts_since != 0) { |
| 335 | snprintf(buffer, SIZE, |
| 336 | "Dumping Queue entries more recent than: %" PRId64 "\n", |
| 337 | (int64_t) ts_since); |
| 338 | result.append(buffer); |
| 339 | } |
| 340 | |
| 341 | // show the recently recorded records |
Ray Essick | d38e174 | 2017-01-23 15:17:06 -0800 | [diff] [blame] | 342 | snprintf(buffer, sizeof(buffer), "\nFinalized Metrics (oldest first):\n"); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 343 | result.append(buffer); |
| 344 | result.append(this->dumpQueue(mFinalized, ts_since)); |
| 345 | |
Ray Essick | d38e174 | 2017-01-23 15:17:06 -0800 | [diff] [blame] | 346 | snprintf(buffer, sizeof(buffer), "\nIn-Progress Metrics (newest first):\n"); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 347 | result.append(buffer); |
| 348 | result.append(this->dumpQueue(mOpen, ts_since)); |
| 349 | |
| 350 | // show who is connected and injecting records? |
| 351 | // talk about # records fed to the 'readers' |
| 352 | // talk about # records we discarded, perhaps "discarded w/o reading" too |
| 353 | |
| 354 | if (clear) { |
| 355 | // remove everything from the finalized queue |
| 356 | while (mFinalized->size() > 0) { |
| 357 | MediaAnalyticsItem * oitem = *(mFinalized->begin()); |
| 358 | if (DEBUG_QUEUE) { |
| 359 | ALOGD("zap old record: key %s sessionID %" PRId64 " ts %" PRId64 "", |
| 360 | oitem->getKey().c_str(), oitem->getSessionID(), |
| 361 | oitem->getTimestamp()); |
| 362 | } |
| 363 | mFinalized->erase(mFinalized->begin()); |
| 364 | mItemsDiscarded++; |
| 365 | } |
| 366 | } |
| 367 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 368 | write(fd, result.string(), result.size()); |
| 369 | return NO_ERROR; |
| 370 | } |
| 371 | |
| 372 | // caller has locked mLock... |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 373 | String8 MediaAnalyticsService::dumpQueue(List<MediaAnalyticsItem *> *theList) { |
| 374 | return dumpQueue(theList, (nsecs_t) 0); |
| 375 | } |
| 376 | |
| 377 | String8 MediaAnalyticsService::dumpQueue(List<MediaAnalyticsItem *> *theList, nsecs_t ts_since) { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 378 | String8 result; |
| 379 | int slot = 0; |
| 380 | |
| 381 | if (theList->empty()) { |
| 382 | result.append("empty\n"); |
| 383 | } else { |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 384 | List<MediaAnalyticsItem *>::iterator it = theList->begin(); |
| 385 | for (; it != theList->end(); it++) { |
| 386 | nsecs_t when = (*it)->getTimestamp(); |
| 387 | if (when < ts_since) { |
| 388 | continue; |
| 389 | } |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 390 | AString entry = (*it)->toString(); |
Ray Essick | 35ad27f | 2017-01-30 14:04:11 -0800 | [diff] [blame] | 391 | result.appendFormat("%5d: %s\n", slot, entry.c_str()); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 392 | slot++; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 393 | } |
| 394 | } |
| 395 | |
| 396 | return result; |
| 397 | } |
| 398 | |
| 399 | // |
| 400 | // Our Cheap in-core, non-persistent records management. |
| 401 | // XXX: rewrite this to manage persistence, etc. |
| 402 | |
| 403 | // insert appropriately into queue |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 404 | void MediaAnalyticsService::saveItem(List<MediaAnalyticsItem *> *l, MediaAnalyticsItem * item, int front) { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 405 | |
| 406 | Mutex::Autolock _l(mLock); |
| 407 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 408 | if (DEBUG_QUEUE) { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 409 | ALOGD("Inject a record: session %" PRId64 " ts %" PRId64 "", |
| 410 | item->getSessionID(), item->getTimestamp()); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 411 | String8 before = dumpQueue(l); |
| 412 | ALOGD("Q before insert: %s", before.string()); |
| 413 | } |
| 414 | |
| 415 | // adding at back of queue (fifo order) |
| 416 | if (front) { |
| 417 | l->push_front(item); |
| 418 | } else { |
| 419 | l->push_back(item); |
| 420 | } |
| 421 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 422 | if (DEBUG_QUEUE) { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 423 | String8 after = dumpQueue(l); |
| 424 | ALOGD("Q after insert: %s", after.string()); |
| 425 | } |
| 426 | |
| 427 | // keep removing old records the front until we're in-bounds |
| 428 | if (mMaxRecords > 0) { |
| 429 | while (l->size() > (size_t) mMaxRecords) { |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 430 | MediaAnalyticsItem * oitem = *(l->begin()); |
| 431 | if (DEBUG_QUEUE) { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 432 | ALOGD("zap old record: key %s sessionID %" PRId64 " ts %" PRId64 "", |
| 433 | oitem->getKey().c_str(), oitem->getSessionID(), |
| 434 | oitem->getTimestamp()); |
| 435 | } |
| 436 | l->erase(l->begin()); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 437 | delete oitem; |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 438 | mItemsDiscarded++; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 439 | } |
| 440 | } |
| 441 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 442 | if (DEBUG_QUEUE) { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 443 | String8 after = dumpQueue(l); |
| 444 | ALOGD("Q after cleanup: %s", after.string()); |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | // are they alike enough that nitem can be folded into oitem? |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 449 | static bool compatibleItems(MediaAnalyticsItem * oitem, MediaAnalyticsItem * nitem) { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 450 | |
| 451 | if (0) { |
| 452 | ALOGD("Compare: o %s n %s", |
| 453 | oitem->toString().c_str(), nitem->toString().c_str()); |
| 454 | } |
| 455 | |
| 456 | // general safety |
| 457 | if (nitem->getUid() != oitem->getUid()) { |
| 458 | return false; |
| 459 | } |
| 460 | if (nitem->getPid() != oitem->getPid()) { |
| 461 | return false; |
| 462 | } |
| 463 | |
| 464 | // key -- needs to match |
| 465 | if (nitem->getKey() == oitem->getKey()) { |
| 466 | // still in the game. |
| 467 | } else { |
| 468 | return false; |
| 469 | } |
| 470 | |
| 471 | // session id -- empty field in new is allowed |
| 472 | MediaAnalyticsItem::SessionID_t osession = oitem->getSessionID(); |
| 473 | MediaAnalyticsItem::SessionID_t nsession = nitem->getSessionID(); |
| 474 | if (nsession != osession) { |
| 475 | // incoming '0' matches value in osession |
| 476 | if (nsession != 0) { |
| 477 | return false; |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | return true; |
| 482 | } |
| 483 | |
| 484 | // find the incomplete record that this will overlay |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 485 | MediaAnalyticsItem *MediaAnalyticsService::findItem(List<MediaAnalyticsItem*> *theList, MediaAnalyticsItem *nitem, bool removeit) { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 486 | if (nitem == NULL) { |
| 487 | return NULL; |
| 488 | } |
| 489 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 490 | MediaAnalyticsItem *item = NULL; |
| 491 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 492 | Mutex::Autolock _l(mLock); |
| 493 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 494 | for (List<MediaAnalyticsItem *>::iterator it = theList->begin(); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 495 | it != theList->end(); it++) { |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 496 | MediaAnalyticsItem *tmp = (*it); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 497 | |
| 498 | if (!compatibleItems(tmp, nitem)) { |
| 499 | continue; |
| 500 | } |
| 501 | |
| 502 | // we match! this is the one I want. |
| 503 | if (removeit) { |
| 504 | theList->erase(it); |
| 505 | } |
| 506 | item = tmp; |
| 507 | break; |
| 508 | } |
| 509 | return item; |
| 510 | } |
| 511 | |
| 512 | |
| 513 | // delete the indicated record |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 514 | void MediaAnalyticsService::deleteItem(List<MediaAnalyticsItem *> *l, MediaAnalyticsItem *item) { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 515 | |
| 516 | Mutex::Autolock _l(mLock); |
| 517 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 518 | if(DEBUG_QUEUE) { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 519 | String8 before = dumpQueue(l); |
| 520 | ALOGD("Q before delete: %s", before.string()); |
| 521 | } |
| 522 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 523 | for (List<MediaAnalyticsItem *>::iterator it = l->begin(); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 524 | it != l->end(); it++) { |
| 525 | if ((*it)->getSessionID() != item->getSessionID()) |
| 526 | continue; |
| 527 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 528 | if (DEBUG_QUEUE) { |
| 529 | ALOGD(" --- removing record for SessionID %" PRId64 "", item->getSessionID()); |
| 530 | ALOGD("drop record at %s:%d", __FILE__, __LINE__); |
| 531 | } |
| 532 | delete *it; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 533 | l->erase(it); |
| 534 | break; |
| 535 | } |
| 536 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 537 | if (DEBUG_QUEUE) { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 538 | String8 after = dumpQueue(l); |
| 539 | ALOGD("Q after delete: %s", after.string()); |
| 540 | } |
| 541 | } |
| 542 | |
Ray Essick | d38e174 | 2017-01-23 15:17:06 -0800 | [diff] [blame] | 543 | static AString allowedKeys[] = |
| 544 | { |
| 545 | "codec", |
| 546 | "extractor" |
| 547 | }; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 548 | |
Ray Essick | d38e174 | 2017-01-23 15:17:06 -0800 | [diff] [blame] | 549 | static const int nAllowedKeys = sizeof(allowedKeys) / sizeof(allowedKeys[0]); |
| 550 | |
| 551 | // are the contents good |
| 552 | bool MediaAnalyticsService::contentValid(MediaAnalyticsItem *item, bool isTrusted) { |
| 553 | |
| 554 | // untrusted uids can only send us a limited set of keys |
| 555 | if (isTrusted == false) { |
| 556 | // restrict to a specific set of keys |
| 557 | AString key = item->getKey(); |
| 558 | |
| 559 | size_t i; |
| 560 | for(i = 0; i < nAllowedKeys; i++) { |
| 561 | if (key == allowedKeys[i]) { |
| 562 | break; |
| 563 | } |
| 564 | } |
| 565 | if (i == nAllowedKeys) { |
| 566 | ALOGD("Ignoring (key): %s", item->toString().c_str()); |
| 567 | return false; |
| 568 | } |
| 569 | } |
| 570 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 571 | // internal consistency |
| 572 | |
| 573 | return true; |
| 574 | } |
| 575 | |
| 576 | // are we rate limited, normally false |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 577 | bool MediaAnalyticsService::rateLimited(MediaAnalyticsItem *) { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 578 | |
| 579 | return false; |
| 580 | } |
| 581 | |
| 582 | |
| 583 | } // namespace android |