blob: 876c685383df293d806be2416e2b57fea39a437d [file] [log] [blame]
Ray Essick3938dc62016-11-01 08:56:56 -07001/*
Ray Essick2e9c63b2017-03-29 15:16:44 -07002 * Copyright (C) 2017 The Android Open Source Project
Ray Essick3938dc62016-11-01 08:56:56 -07003 *
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
Ray Essick2e9c63b2017-03-29 15:16:44 -070023#include <stdint.h>
Ray Essick3938dc62016-11-01 08:56:56 -070024#include <inttypes.h>
25#include <sys/types.h>
26#include <sys/stat.h>
27#include <sys/time.h>
28#include <dirent.h>
29#include <unistd.h>
30
31#include <string.h>
32
33#include <cutils/atomic.h>
34#include <cutils/properties.h> // for property_get
35
36#include <utils/misc.h>
37
38#include <binder/IPCThreadState.h>
39#include <binder/IServiceManager.h>
40#include <binder/MemoryHeapBase.h>
41#include <binder/MemoryBase.h>
42#include <gui/Surface.h>
43#include <utils/Errors.h> // for status_t
44#include <utils/List.h>
45#include <utils/String8.h>
46#include <utils/SystemClock.h>
47#include <utils/Timers.h>
48#include <utils/Vector.h>
49
50#include <media/AudioPolicyHelper.h>
51#include <media/IMediaHTTPService.h>
52#include <media/IRemoteDisplay.h>
53#include <media/IRemoteDisplayClient.h>
54#include <media/MediaPlayerInterface.h>
55#include <media/mediarecorder.h>
56#include <media/MediaMetadataRetrieverInterface.h>
57#include <media/Metadata.h>
58#include <media/AudioTrack.h>
59#include <media/MemoryLeakTrackUtil.h>
60#include <media/stagefright/MediaCodecList.h>
61#include <media/stagefright/MediaErrors.h>
62#include <media/stagefright/Utils.h>
63#include <media/stagefright/foundation/ADebug.h>
64#include <media/stagefright/foundation/ALooperRoster.h>
65#include <mediautils/BatteryNotifier.h>
66
67//#include <memunreachable/memunreachable.h>
68#include <system/audio.h>
69
70#include <private/android_filesystem_config.h>
71
72#include "MediaAnalyticsService.h"
73
Ray Essick2e9c63b2017-03-29 15:16:44 -070074#include "MetricsSummarizer.h"
75#include "MetricsSummarizerCodec.h"
76#include "MetricsSummarizerExtractor.h"
77#include "MetricsSummarizerPlayer.h"
78#include "MetricsSummarizerRecorder.h"
79
Ray Essick3938dc62016-11-01 08:56:56 -070080
81namespace android {
82
83
Ray Essick2e9c63b2017-03-29 15:16:44 -070084
85// summarized records
86// up to 48 sets, each covering an hour -- at least 2 days of coverage
87// (will be longer if there are hours without any media action)
88static const nsecs_t kNewSetIntervalNs = 3600*(1000*1000*1000ll);
89static const int kMaxRecordSets = 48;
90// individual records kept in memory
91static const int kMaxRecords = 100;
92
93
94static const char *kServiceName = "media.metrics";
95
Ray Essick3938dc62016-11-01 08:56:56 -070096
97//using android::status_t;
98//using android::OK;
99//using android::BAD_VALUE;
100//using android::NOT_ENOUGH_DATA;
101//using android::Parcel;
102
103
104void MediaAnalyticsService::instantiate() {
105 defaultServiceManager()->addService(
Ray Essick2e9c63b2017-03-29 15:16:44 -0700106 String16(kServiceName), new MediaAnalyticsService());
Ray Essick3938dc62016-11-01 08:56:56 -0700107}
108
Ray Essick2e9c63b2017-03-29 15:16:44 -0700109// handle sets of summarizers
110MediaAnalyticsService::SummarizerSet::SummarizerSet() {
111 mSummarizers = new List<MetricsSummarizer *>();
112}
113MediaAnalyticsService::SummarizerSet::~SummarizerSet() {
114 // empty the list
115 List<MetricsSummarizer *> *l = mSummarizers;
116 while (l->size() > 0) {
117 MetricsSummarizer *summarizer = *(l->begin());
118 l->erase(l->begin());
119 delete summarizer;
120 }
121}
122
123void MediaAnalyticsService::newSummarizerSet() {
124 ALOGD("MediaAnalyticsService::newSummarizerSet");
125 MediaAnalyticsService::SummarizerSet *set = new MediaAnalyticsService::SummarizerSet();
126 nsecs_t now = systemTime(SYSTEM_TIME_REALTIME);
127 set->setStarted(now);
128
129 set->appendSummarizer(new MetricsSummarizerExtractor("extractor"));
130 set->appendSummarizer(new MetricsSummarizerCodec("codec"));
131 set->appendSummarizer(new MetricsSummarizerPlayer("nuplayer"));
132 set->appendSummarizer(new MetricsSummarizerRecorder("recorder"));
133
134 // ALWAYS at the end, since it catches everything
135 set->appendSummarizer(new MetricsSummarizer(NULL));
136
137 // inject this set at the BACK of the list.
138 mSummarizerSets->push_back(set);
139 mCurrentSet = set;
140
141 // limit the # that we have
142 if (mMaxRecordSets > 0) {
143 List<SummarizerSet *> *l = mSummarizerSets;
144 while (l->size() > (size_t) mMaxRecordSets) {
145 ALOGD("Deleting oldest record set....");
146 MediaAnalyticsService::SummarizerSet *oset = *(l->begin());
147 l->erase(l->begin());
148 delete oset;
149 mSetsDiscarded++;
150 }
151 }
152}
153
Ray Essick3938dc62016-11-01 08:56:56 -0700154MediaAnalyticsService::MediaAnalyticsService()
Ray Essick2e9c63b2017-03-29 15:16:44 -0700155 : mMaxRecords(kMaxRecords),
156 mMaxRecordSets(kMaxRecordSets),
157 mNewSetInterval(kNewSetIntervalNs) {
Ray Essick3938dc62016-11-01 08:56:56 -0700158
159 ALOGD("MediaAnalyticsService created");
160 // clear our queues
Ray Essickb5fac8e2016-12-12 11:33:56 -0800161 mOpen = new List<MediaAnalyticsItem *>();
162 mFinalized = new List<MediaAnalyticsItem *>();
Ray Essick3938dc62016-11-01 08:56:56 -0700163
Ray Essick2e9c63b2017-03-29 15:16:44 -0700164 mSummarizerSets = new List<MediaAnalyticsService::SummarizerSet *>();
165 newSummarizerSet();
166
Ray Essick3938dc62016-11-01 08:56:56 -0700167 mItemsSubmitted = 0;
168 mItemsFinalized = 0;
169 mItemsDiscarded = 0;
170
171 mLastSessionID = 0;
172 // recover any persistency we set up
173 // etc
174}
175
176MediaAnalyticsService::~MediaAnalyticsService() {
177 ALOGD("MediaAnalyticsService destroyed");
178
Ray Essick2e9c63b2017-03-29 15:16:44 -0700179 // clean out mOpen and mFinalized
180 delete mOpen;
181 mOpen = NULL;
182 delete mFinalized;
183 mFinalized = NULL;
184
185 // XXX: clean out the summaries
Ray Essick3938dc62016-11-01 08:56:56 -0700186}
187
188
189MediaAnalyticsItem::SessionID_t MediaAnalyticsService::generateUniqueSessionID() {
190 // generate a new sessionid
191
192 Mutex::Autolock _l(mLock_ids);
193 return (++mLastSessionID);
194}
195
Ray Essickb5fac8e2016-12-12 11:33:56 -0800196// caller surrenders ownership of 'item'
197MediaAnalyticsItem::SessionID_t MediaAnalyticsService::submit(MediaAnalyticsItem *item, bool forcenew) {
Ray Essick3938dc62016-11-01 08:56:56 -0700198
199 MediaAnalyticsItem::SessionID_t id = MediaAnalyticsItem::SessionIDInvalid;
200
Ray Essickd38e1742017-01-23 15:17:06 -0800201 // we control these, generally not trusting user input
Ray Essick3938dc62016-11-01 08:56:56 -0700202 nsecs_t now = systemTime(SYSTEM_TIME_REALTIME);
203 item->setTimestamp(now);
204 int pid = IPCThreadState::self()->getCallingPid();
Ray Essick3938dc62016-11-01 08:56:56 -0700205 int uid = IPCThreadState::self()->getCallingUid();
Ray Essickd38e1742017-01-23 15:17:06 -0800206
207 int uid_given = item->getUid();
208 int pid_given = item->getPid();
209
210 // although we do make exceptions for particular client uids
211 // that we know we trust.
212 //
213 bool isTrusted = false;
214
215 switch (uid) {
216 case AID_MEDIA:
217 case AID_MEDIA_CODEC:
218 case AID_MEDIA_EX:
219 case AID_MEDIA_DRM:
220 // trusted source, only override default values
Ray Essick2e9c63b2017-03-29 15:16:44 -0700221 isTrusted = true;
Ray Essickd38e1742017-01-23 15:17:06 -0800222 if (uid_given == (-1)) {
223 item->setUid(uid);
224 }
225 if (pid_given == (-1)) {
226 item->setPid(pid);
227 }
228 break;
229 default:
230 isTrusted = false;
231 item->setPid(pid);
232 item->setUid(uid);
233 break;
234 }
235
Ray Essick3938dc62016-11-01 08:56:56 -0700236
237 mItemsSubmitted++;
238
239 // validate the record; we discard if we don't like it
Ray Essickd38e1742017-01-23 15:17:06 -0800240 if (contentValid(item, isTrusted) == false) {
Ray Essickb5fac8e2016-12-12 11:33:56 -0800241 delete item;
Ray Essick3938dc62016-11-01 08:56:56 -0700242 return MediaAnalyticsItem::SessionIDInvalid;
243 }
244
245
246 // if we have a sesisonid in the new record, look to make
247 // sure it doesn't appear in the finalized list.
248 // XXX: this is for security / DOS prevention.
249 // may also require that we persist the unique sessionIDs
250 // across boots [instead of within a single boot]
251
252
253 // match this new record up against records in the open
254 // list...
255 // if there's a match, merge them together
256 // deal with moving the old / merged record into the finalized que
257
258 bool finalizing = item->getFinalized();
259
260 // if finalizing, we'll remove it
Ray Essickb5fac8e2016-12-12 11:33:56 -0800261 MediaAnalyticsItem *oitem = findItem(mOpen, item, finalizing | forcenew);
Ray Essick3938dc62016-11-01 08:56:56 -0700262 if (oitem != NULL) {
263 if (forcenew) {
264 // old one gets finalized, then we insert the new one
265 // so we'll have 2 records at the end of this.
266 // but don't finalize an empty record
Ray Essickb5fac8e2016-12-12 11:33:56 -0800267 if (oitem->count() == 0) {
268 // we're responsible for disposing of the dead record
269 delete oitem;
270 oitem = NULL;
271 } else {
Ray Essick3938dc62016-11-01 08:56:56 -0700272 oitem->setFinalized(true);
Ray Essick2e9c63b2017-03-29 15:16:44 -0700273 summarize(oitem);
Ray Essick3938dc62016-11-01 08:56:56 -0700274 saveItem(mFinalized, oitem, 0);
275 }
276 // new record could itself be marked finalized...
277 if (finalizing) {
Ray Essick2e9c63b2017-03-29 15:16:44 -0700278 summarize(item);
Ray Essick3938dc62016-11-01 08:56:56 -0700279 saveItem(mFinalized, item, 0);
280 mItemsFinalized++;
281 } else {
282 saveItem(mOpen, item, 1);
283 }
284 id = item->getSessionID();
285 } else {
286 // combine the records, send it to finalized if appropriate
287 oitem->merge(item);
288 if (finalizing) {
Ray Essick2e9c63b2017-03-29 15:16:44 -0700289 summarize(oitem);
Ray Essick3938dc62016-11-01 08:56:56 -0700290 saveItem(mFinalized, oitem, 0);
291 mItemsFinalized++;
292 }
293 id = oitem->getSessionID();
Ray Essickb5fac8e2016-12-12 11:33:56 -0800294
295 // we're responsible for disposing of the dead record
296 delete item;
297 item = NULL;
Ray Essick3938dc62016-11-01 08:56:56 -0700298 }
299 } else {
Ray Essickb5fac8e2016-12-12 11:33:56 -0800300 // nothing to merge, save the new record
301 id = item->getSessionID();
302 if (finalizing) {
303 if (item->count() == 0) {
304 // drop empty records
305 delete item;
306 item = NULL;
Ray Essick3938dc62016-11-01 08:56:56 -0700307 } else {
Ray Essick2e9c63b2017-03-29 15:16:44 -0700308 summarize(item);
Ray Essickb5fac8e2016-12-12 11:33:56 -0800309 saveItem(mFinalized, item, 0);
310 mItemsFinalized++;
Ray Essick3938dc62016-11-01 08:56:56 -0700311 }
Ray Essickb5fac8e2016-12-12 11:33:56 -0800312 } else {
313 saveItem(mOpen, item, 1);
314 }
Ray Essick3938dc62016-11-01 08:56:56 -0700315 }
Ray Essick3938dc62016-11-01 08:56:56 -0700316 return id;
317}
318
Ray Essickb5fac8e2016-12-12 11:33:56 -0800319status_t MediaAnalyticsService::dump(int fd, const Vector<String16>& args)
Ray Essick3938dc62016-11-01 08:56:56 -0700320{
Ray Essickb5fac8e2016-12-12 11:33:56 -0800321 const size_t SIZE = 512;
Ray Essick3938dc62016-11-01 08:56:56 -0700322 char buffer[SIZE];
323 String8 result;
324
325 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
326 snprintf(buffer, SIZE, "Permission Denial: "
327 "can't dump MediaAnalyticsService from pid=%d, uid=%d\n",
328 IPCThreadState::self()->getCallingPid(),
329 IPCThreadState::self()->getCallingUid());
330 result.append(buffer);
Ray Essickb5fac8e2016-12-12 11:33:56 -0800331 write(fd, result.string(), result.size());
332 return NO_ERROR;
Ray Essick3938dc62016-11-01 08:56:56 -0700333 }
Ray Essickb5fac8e2016-12-12 11:33:56 -0800334
335 // crack any parameters
336 bool clear = false;
Ray Essick2e9c63b2017-03-29 15:16:44 -0700337 bool summary = false;
Ray Essickb5fac8e2016-12-12 11:33:56 -0800338 nsecs_t ts_since = 0;
Ray Essick2e9c63b2017-03-29 15:16:44 -0700339 String16 summaryOption("-summary");
Ray Essickb5fac8e2016-12-12 11:33:56 -0800340 String16 clearOption("-clear");
341 String16 sinceOption("-since");
Ray Essick35ad27f2017-01-30 14:04:11 -0800342 String16 helpOption("-help");
Ray Essick2e9c63b2017-03-29 15:16:44 -0700343 String16 onlyOption("-only");
344 const char *only = NULL;
Ray Essickb5fac8e2016-12-12 11:33:56 -0800345 int n = args.size();
346 for (int i = 0; i < n; i++) {
347 String8 myarg(args[i]);
348 if (args[i] == clearOption) {
349 clear = true;
Ray Essick2e9c63b2017-03-29 15:16:44 -0700350 } else if (args[i] == summaryOption) {
351 summary = true;
Ray Essickb5fac8e2016-12-12 11:33:56 -0800352 } else if (args[i] == sinceOption) {
353 i++;
354 if (i < n) {
355 String8 value(args[i]);
356 char *endp;
357 const char *p = value.string();
358 ts_since = strtoll(p, &endp, 10);
359 if (endp == p || *endp != '\0') {
360 ts_since = 0;
361 }
362 } else {
363 ts_since = 0;
364 }
Ray Essick35ad27f2017-01-30 14:04:11 -0800365 // command line is milliseconds; internal units are nano-seconds
366 ts_since *= 1000*1000;
Ray Essick2e9c63b2017-03-29 15:16:44 -0700367 } else if (args[i] == onlyOption) {
368 i++;
369 if (i < n) {
370 String8 value(args[i]);
371 const char *p = value.string();
372 char *q = strdup(p);
373 if (q != NULL) {
374 if (only != NULL) {
375 free((void*)only);
376 }
377 only = q;
378 }
379 }
Ray Essick35ad27f2017-01-30 14:04:11 -0800380 } else if (args[i] == helpOption) {
381 result.append("Recognized parameters:\n");
382 result.append("-help this help message\n");
Ray Essick2e9c63b2017-03-29 15:16:44 -0700383 result.append("-summary show summary info\n");
Ray Essick35ad27f2017-01-30 14:04:11 -0800384 result.append("-clear clears out saved records\n");
Ray Essick2e9c63b2017-03-29 15:16:44 -0700385 result.append("-only X process records for component X\n");
386 result.append("-since X include records since X\n");
387 result.append(" (X is milliseconds since the UNIX epoch)\n");
Ray Essick35ad27f2017-01-30 14:04:11 -0800388 write(fd, result.string(), result.size());
389 return NO_ERROR;
Ray Essickb5fac8e2016-12-12 11:33:56 -0800390 }
391 }
392
393 Mutex::Autolock _l(mLock);
394
Ray Essick2e9c63b2017-03-29 15:16:44 -0700395 // we ALWAYS dump this piece
396 snprintf(buffer, SIZE, "Dump of the %s process:\n", kServiceName);
Ray Essickb5fac8e2016-12-12 11:33:56 -0800397 result.append(buffer);
398
Ray Essick2e9c63b2017-03-29 15:16:44 -0700399 dumpHeaders(result, ts_since);
400
401 // only want 1, to avoid confusing folks that parse the output
402 if (summary) {
403 dumpSummaries(result, ts_since, only);
404 } else {
405 dumpRecent(result, ts_since, only);
406 }
407
408
409 if (clear) {
410 // remove everything from the finalized queue
411 while (mFinalized->size() > 0) {
412 MediaAnalyticsItem * oitem = *(mFinalized->begin());
413 mFinalized->erase(mFinalized->begin());
414 delete oitem;
415 mItemsDiscarded++;
416 }
417
418 // shall we clear the summary data too?
419
420 }
421
422 write(fd, result.string(), result.size());
423 return NO_ERROR;
424}
425
426// dump headers
427void MediaAnalyticsService::dumpHeaders(String8 &result, nsecs_t ts_since) {
428 const size_t SIZE = 512;
429 char buffer[SIZE];
430
Ray Essickb5fac8e2016-12-12 11:33:56 -0800431 int enabled = MediaAnalyticsItem::isEnabled();
432 if (enabled) {
Ray Essickd38e1742017-01-23 15:17:06 -0800433 snprintf(buffer, SIZE, "Metrics gathering: enabled\n");
Ray Essickb5fac8e2016-12-12 11:33:56 -0800434 } else {
Ray Essickd38e1742017-01-23 15:17:06 -0800435 snprintf(buffer, SIZE, "Metrics gathering: DISABLED via property\n");
Ray Essickb5fac8e2016-12-12 11:33:56 -0800436 }
437 result.append(buffer);
438
439 snprintf(buffer, SIZE,
440 "Since Boot: Submissions: %" PRId64
441 " Finalizations: %" PRId64
442 " Discarded: %" PRId64 "\n",
443 mItemsSubmitted, mItemsFinalized, mItemsDiscarded);
444 result.append(buffer);
Ray Essick2e9c63b2017-03-29 15:16:44 -0700445 snprintf(buffer, SIZE,
446 "Summary Sets Discarded: %" PRId64 "\n", mSetsDiscarded);
447 result.append(buffer);
Ray Essickb5fac8e2016-12-12 11:33:56 -0800448 if (ts_since != 0) {
449 snprintf(buffer, SIZE,
450 "Dumping Queue entries more recent than: %" PRId64 "\n",
451 (int64_t) ts_since);
452 result.append(buffer);
453 }
Ray Essick2e9c63b2017-03-29 15:16:44 -0700454}
455
456// dump summary info
457void MediaAnalyticsService::dumpSummaries(String8 &result, nsecs_t ts_since, const char *only) {
458 const size_t SIZE = 512;
459 char buffer[SIZE];
460 int slot = 0;
461
462 snprintf(buffer, SIZE, "\nSummarized Metrics:\n");
463 result.append(buffer);
464
465 // have each of the distillers dump records
466 if (mSummarizerSets != NULL) {
467 List<SummarizerSet *>::iterator itSet = mSummarizerSets->begin();
468 for (; itSet != mSummarizerSets->end(); itSet++) {
469 nsecs_t when = (*itSet)->getStarted();
470 if (when < ts_since) {
471 continue;
472 }
473 List<MetricsSummarizer *> *list = (*itSet)->getSummarizers();
474 List<MetricsSummarizer *>::iterator it = list->begin();
475 for (; it != list->end(); it++) {
476 if (only != NULL && strcmp(only, (*it)->getKey()) != 0) {
477 ALOGV("Told to omit '%s'", (*it)->getKey());
478 }
479 AString distilled = (*it)->dumpSummary(slot, only);
480 result.append(distilled.c_str());
481 }
482 }
483 }
484}
485
486// the recent, detailed queues
487void MediaAnalyticsService::dumpRecent(String8 &result, nsecs_t ts_since, const char * only) {
488 const size_t SIZE = 512;
489 char buffer[SIZE];
Ray Essickb5fac8e2016-12-12 11:33:56 -0800490
491 // show the recently recorded records
Ray Essickd38e1742017-01-23 15:17:06 -0800492 snprintf(buffer, sizeof(buffer), "\nFinalized Metrics (oldest first):\n");
Ray Essickb5fac8e2016-12-12 11:33:56 -0800493 result.append(buffer);
Ray Essick2e9c63b2017-03-29 15:16:44 -0700494 result.append(this->dumpQueue(mFinalized, ts_since, only));
Ray Essickb5fac8e2016-12-12 11:33:56 -0800495
Ray Essickd38e1742017-01-23 15:17:06 -0800496 snprintf(buffer, sizeof(buffer), "\nIn-Progress Metrics (newest first):\n");
Ray Essickb5fac8e2016-12-12 11:33:56 -0800497 result.append(buffer);
Ray Essick2e9c63b2017-03-29 15:16:44 -0700498 result.append(this->dumpQueue(mOpen, ts_since, only));
Ray Essickb5fac8e2016-12-12 11:33:56 -0800499
500 // show who is connected and injecting records?
501 // talk about # records fed to the 'readers'
502 // talk about # records we discarded, perhaps "discarded w/o reading" too
Ray Essick3938dc62016-11-01 08:56:56 -0700503}
Ray Essick3938dc62016-11-01 08:56:56 -0700504// caller has locked mLock...
Ray Essickb5fac8e2016-12-12 11:33:56 -0800505String8 MediaAnalyticsService::dumpQueue(List<MediaAnalyticsItem *> *theList) {
Ray Essick2e9c63b2017-03-29 15:16:44 -0700506 return dumpQueue(theList, (nsecs_t) 0, NULL);
Ray Essickb5fac8e2016-12-12 11:33:56 -0800507}
508
Ray Essick2e9c63b2017-03-29 15:16:44 -0700509String8 MediaAnalyticsService::dumpQueue(List<MediaAnalyticsItem *> *theList, nsecs_t ts_since, const char * only) {
Ray Essick3938dc62016-11-01 08:56:56 -0700510 String8 result;
511 int slot = 0;
512
513 if (theList->empty()) {
514 result.append("empty\n");
515 } else {
Ray Essickb5fac8e2016-12-12 11:33:56 -0800516 List<MediaAnalyticsItem *>::iterator it = theList->begin();
517 for (; it != theList->end(); it++) {
518 nsecs_t when = (*it)->getTimestamp();
519 if (when < ts_since) {
520 continue;
521 }
Ray Essick2e9c63b2017-03-29 15:16:44 -0700522 if (only != NULL &&
523 strcmp(only, (*it)->getKey().c_str()) != 0) {
524 ALOGV("Omit '%s', it's not '%s'", (*it)->getKey().c_str(), only);
525 continue;
526 }
Ray Essick3938dc62016-11-01 08:56:56 -0700527 AString entry = (*it)->toString();
Ray Essick35ad27f2017-01-30 14:04:11 -0800528 result.appendFormat("%5d: %s\n", slot, entry.c_str());
Ray Essickb5fac8e2016-12-12 11:33:56 -0800529 slot++;
Ray Essick3938dc62016-11-01 08:56:56 -0700530 }
531 }
532
533 return result;
534}
535
536//
537// Our Cheap in-core, non-persistent records management.
538// XXX: rewrite this to manage persistence, etc.
539
540// insert appropriately into queue
Ray Essickb5fac8e2016-12-12 11:33:56 -0800541void MediaAnalyticsService::saveItem(List<MediaAnalyticsItem *> *l, MediaAnalyticsItem * item, int front) {
Ray Essick3938dc62016-11-01 08:56:56 -0700542
543 Mutex::Autolock _l(mLock);
544
Ray Essick3938dc62016-11-01 08:56:56 -0700545 // adding at back of queue (fifo order)
546 if (front) {
547 l->push_front(item);
548 } else {
549 l->push_back(item);
550 }
551
Ray Essick3938dc62016-11-01 08:56:56 -0700552 // keep removing old records the front until we're in-bounds
553 if (mMaxRecords > 0) {
554 while (l->size() > (size_t) mMaxRecords) {
Ray Essickb5fac8e2016-12-12 11:33:56 -0800555 MediaAnalyticsItem * oitem = *(l->begin());
Ray Essick3938dc62016-11-01 08:56:56 -0700556 l->erase(l->begin());
Ray Essickb5fac8e2016-12-12 11:33:56 -0800557 delete oitem;
Ray Essickb5fac8e2016-12-12 11:33:56 -0800558 mItemsDiscarded++;
Ray Essick3938dc62016-11-01 08:56:56 -0700559 }
560 }
Ray Essick3938dc62016-11-01 08:56:56 -0700561}
562
563// are they alike enough that nitem can be folded into oitem?
Ray Essickb5fac8e2016-12-12 11:33:56 -0800564static bool compatibleItems(MediaAnalyticsItem * oitem, MediaAnalyticsItem * nitem) {
Ray Essick3938dc62016-11-01 08:56:56 -0700565
566 if (0) {
567 ALOGD("Compare: o %s n %s",
568 oitem->toString().c_str(), nitem->toString().c_str());
569 }
570
571 // general safety
572 if (nitem->getUid() != oitem->getUid()) {
573 return false;
574 }
575 if (nitem->getPid() != oitem->getPid()) {
576 return false;
577 }
578
579 // key -- needs to match
580 if (nitem->getKey() == oitem->getKey()) {
581 // still in the game.
582 } else {
583 return false;
584 }
585
586 // session id -- empty field in new is allowed
587 MediaAnalyticsItem::SessionID_t osession = oitem->getSessionID();
588 MediaAnalyticsItem::SessionID_t nsession = nitem->getSessionID();
589 if (nsession != osession) {
590 // incoming '0' matches value in osession
591 if (nsession != 0) {
592 return false;
593 }
594 }
595
596 return true;
597}
598
599// find the incomplete record that this will overlay
Ray Essickb5fac8e2016-12-12 11:33:56 -0800600MediaAnalyticsItem *MediaAnalyticsService::findItem(List<MediaAnalyticsItem*> *theList, MediaAnalyticsItem *nitem, bool removeit) {
Ray Essick3938dc62016-11-01 08:56:56 -0700601 if (nitem == NULL) {
602 return NULL;
603 }
604
Ray Essickb5fac8e2016-12-12 11:33:56 -0800605 MediaAnalyticsItem *item = NULL;
606
Ray Essick3938dc62016-11-01 08:56:56 -0700607 Mutex::Autolock _l(mLock);
608
Ray Essickb5fac8e2016-12-12 11:33:56 -0800609 for (List<MediaAnalyticsItem *>::iterator it = theList->begin();
Ray Essick3938dc62016-11-01 08:56:56 -0700610 it != theList->end(); it++) {
Ray Essickb5fac8e2016-12-12 11:33:56 -0800611 MediaAnalyticsItem *tmp = (*it);
Ray Essick3938dc62016-11-01 08:56:56 -0700612
613 if (!compatibleItems(tmp, nitem)) {
614 continue;
615 }
616
617 // we match! this is the one I want.
618 if (removeit) {
619 theList->erase(it);
620 }
621 item = tmp;
622 break;
623 }
624 return item;
625}
626
627
628// delete the indicated record
Ray Essickb5fac8e2016-12-12 11:33:56 -0800629void MediaAnalyticsService::deleteItem(List<MediaAnalyticsItem *> *l, MediaAnalyticsItem *item) {
Ray Essick3938dc62016-11-01 08:56:56 -0700630
631 Mutex::Autolock _l(mLock);
632
Ray Essickb5fac8e2016-12-12 11:33:56 -0800633 for (List<MediaAnalyticsItem *>::iterator it = l->begin();
Ray Essick3938dc62016-11-01 08:56:56 -0700634 it != l->end(); it++) {
635 if ((*it)->getSessionID() != item->getSessionID())
636 continue;
Ray Essickb5fac8e2016-12-12 11:33:56 -0800637 delete *it;
Ray Essick3938dc62016-11-01 08:56:56 -0700638 l->erase(it);
639 break;
640 }
Ray Essick3938dc62016-11-01 08:56:56 -0700641}
642
Ray Essickd38e1742017-01-23 15:17:06 -0800643static AString allowedKeys[] =
644{
645 "codec",
646 "extractor"
647};
Ray Essick3938dc62016-11-01 08:56:56 -0700648
Ray Essickd38e1742017-01-23 15:17:06 -0800649static const int nAllowedKeys = sizeof(allowedKeys) / sizeof(allowedKeys[0]);
650
651// are the contents good
652bool MediaAnalyticsService::contentValid(MediaAnalyticsItem *item, bool isTrusted) {
653
654 // untrusted uids can only send us a limited set of keys
655 if (isTrusted == false) {
656 // restrict to a specific set of keys
657 AString key = item->getKey();
658
659 size_t i;
660 for(i = 0; i < nAllowedKeys; i++) {
661 if (key == allowedKeys[i]) {
662 break;
663 }
664 }
665 if (i == nAllowedKeys) {
666 ALOGD("Ignoring (key): %s", item->toString().c_str());
667 return false;
668 }
669 }
670
Ray Essick3938dc62016-11-01 08:56:56 -0700671 // internal consistency
672
673 return true;
674}
675
676// are we rate limited, normally false
Ray Essickb5fac8e2016-12-12 11:33:56 -0800677bool MediaAnalyticsService::rateLimited(MediaAnalyticsItem *) {
Ray Essick3938dc62016-11-01 08:56:56 -0700678
679 return false;
680}
681
Ray Essick2e9c63b2017-03-29 15:16:44 -0700682// insert into the appropriate summarizer.
683// we make our own copy to save/summarize
684void MediaAnalyticsService::summarize(MediaAnalyticsItem *item) {
685
686 ALOGV("MediaAnalyticsService::summarize()");
687
688 if (item == NULL) {
689 return;
690 }
691
692 nsecs_t now = systemTime(SYSTEM_TIME_REALTIME);
693 if (mCurrentSet == NULL
694 || (mCurrentSet->getStarted() + mNewSetInterval < now)) {
695 newSummarizerSet();
696 }
697
698 if (mCurrentSet == NULL) {
699 return;
700 }
701
702 List<MetricsSummarizer *> *summarizers = mCurrentSet->getSummarizers();
703 List<MetricsSummarizer *>::iterator it = summarizers->begin();
704 for (; it != summarizers->end(); it++) {
705 if ((*it)->isMine(*item)) {
706 break;
707 }
708 }
709 if (it == summarizers->end()) {
710 ALOGD("no handler for type %s", item->getKey().c_str());
711 return; // no handler
712 }
713
714 // invoke the summarizer. summarizer will make whatever copies
715 // it wants; the caller retains ownership of item.
716
717 (*it)->handleRecord(item);
718
719}
Ray Essick3938dc62016-11-01 08:56:56 -0700720
721} // namespace android