blob: f3bb35ccbd8bdd706fb4340f71f6f4c0e70a4659 [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 }
Yunlian Jiang8caa24e2017-06-06 16:18:31 -0700407 free((void*)only);
408 only=NULL;
Ray Essick2e9c63b2017-03-29 15:16:44 -0700409
410
411 if (clear) {
412 // remove everything from the finalized queue
413 while (mFinalized->size() > 0) {
414 MediaAnalyticsItem * oitem = *(mFinalized->begin());
415 mFinalized->erase(mFinalized->begin());
416 delete oitem;
417 mItemsDiscarded++;
418 }
419
420 // shall we clear the summary data too?
421
422 }
423
424 write(fd, result.string(), result.size());
425 return NO_ERROR;
426}
427
428// dump headers
429void MediaAnalyticsService::dumpHeaders(String8 &result, nsecs_t ts_since) {
430 const size_t SIZE = 512;
431 char buffer[SIZE];
432
Ray Essickb5fac8e2016-12-12 11:33:56 -0800433 int enabled = MediaAnalyticsItem::isEnabled();
434 if (enabled) {
Ray Essickd38e1742017-01-23 15:17:06 -0800435 snprintf(buffer, SIZE, "Metrics gathering: enabled\n");
Ray Essickb5fac8e2016-12-12 11:33:56 -0800436 } else {
Ray Essickd38e1742017-01-23 15:17:06 -0800437 snprintf(buffer, SIZE, "Metrics gathering: DISABLED via property\n");
Ray Essickb5fac8e2016-12-12 11:33:56 -0800438 }
439 result.append(buffer);
440
441 snprintf(buffer, SIZE,
442 "Since Boot: Submissions: %" PRId64
443 " Finalizations: %" PRId64
444 " Discarded: %" PRId64 "\n",
445 mItemsSubmitted, mItemsFinalized, mItemsDiscarded);
446 result.append(buffer);
Ray Essick2e9c63b2017-03-29 15:16:44 -0700447 snprintf(buffer, SIZE,
448 "Summary Sets Discarded: %" PRId64 "\n", mSetsDiscarded);
449 result.append(buffer);
Ray Essickb5fac8e2016-12-12 11:33:56 -0800450 if (ts_since != 0) {
451 snprintf(buffer, SIZE,
452 "Dumping Queue entries more recent than: %" PRId64 "\n",
453 (int64_t) ts_since);
454 result.append(buffer);
455 }
Ray Essick2e9c63b2017-03-29 15:16:44 -0700456}
457
458// dump summary info
459void MediaAnalyticsService::dumpSummaries(String8 &result, nsecs_t ts_since, const char *only) {
460 const size_t SIZE = 512;
461 char buffer[SIZE];
462 int slot = 0;
463
464 snprintf(buffer, SIZE, "\nSummarized Metrics:\n");
465 result.append(buffer);
466
467 // have each of the distillers dump records
468 if (mSummarizerSets != NULL) {
469 List<SummarizerSet *>::iterator itSet = mSummarizerSets->begin();
470 for (; itSet != mSummarizerSets->end(); itSet++) {
471 nsecs_t when = (*itSet)->getStarted();
472 if (when < ts_since) {
473 continue;
474 }
475 List<MetricsSummarizer *> *list = (*itSet)->getSummarizers();
476 List<MetricsSummarizer *>::iterator it = list->begin();
477 for (; it != list->end(); it++) {
478 if (only != NULL && strcmp(only, (*it)->getKey()) != 0) {
479 ALOGV("Told to omit '%s'", (*it)->getKey());
480 }
481 AString distilled = (*it)->dumpSummary(slot, only);
482 result.append(distilled.c_str());
483 }
484 }
485 }
486}
487
488// the recent, detailed queues
489void MediaAnalyticsService::dumpRecent(String8 &result, nsecs_t ts_since, const char * only) {
490 const size_t SIZE = 512;
491 char buffer[SIZE];
Ray Essickb5fac8e2016-12-12 11:33:56 -0800492
493 // show the recently recorded records
Ray Essickd38e1742017-01-23 15:17:06 -0800494 snprintf(buffer, sizeof(buffer), "\nFinalized Metrics (oldest first):\n");
Ray Essickb5fac8e2016-12-12 11:33:56 -0800495 result.append(buffer);
Ray Essick2e9c63b2017-03-29 15:16:44 -0700496 result.append(this->dumpQueue(mFinalized, ts_since, only));
Ray Essickb5fac8e2016-12-12 11:33:56 -0800497
Ray Essickd38e1742017-01-23 15:17:06 -0800498 snprintf(buffer, sizeof(buffer), "\nIn-Progress Metrics (newest first):\n");
Ray Essickb5fac8e2016-12-12 11:33:56 -0800499 result.append(buffer);
Ray Essick2e9c63b2017-03-29 15:16:44 -0700500 result.append(this->dumpQueue(mOpen, ts_since, only));
Ray Essickb5fac8e2016-12-12 11:33:56 -0800501
502 // show who is connected and injecting records?
503 // talk about # records fed to the 'readers'
504 // talk about # records we discarded, perhaps "discarded w/o reading" too
Ray Essick3938dc62016-11-01 08:56:56 -0700505}
Ray Essick3938dc62016-11-01 08:56:56 -0700506// caller has locked mLock...
Ray Essickb5fac8e2016-12-12 11:33:56 -0800507String8 MediaAnalyticsService::dumpQueue(List<MediaAnalyticsItem *> *theList) {
Ray Essick2e9c63b2017-03-29 15:16:44 -0700508 return dumpQueue(theList, (nsecs_t) 0, NULL);
Ray Essickb5fac8e2016-12-12 11:33:56 -0800509}
510
Ray Essick2e9c63b2017-03-29 15:16:44 -0700511String8 MediaAnalyticsService::dumpQueue(List<MediaAnalyticsItem *> *theList, nsecs_t ts_since, const char * only) {
Ray Essick3938dc62016-11-01 08:56:56 -0700512 String8 result;
513 int slot = 0;
514
515 if (theList->empty()) {
516 result.append("empty\n");
517 } else {
Ray Essickb5fac8e2016-12-12 11:33:56 -0800518 List<MediaAnalyticsItem *>::iterator it = theList->begin();
519 for (; it != theList->end(); it++) {
520 nsecs_t when = (*it)->getTimestamp();
521 if (when < ts_since) {
522 continue;
523 }
Ray Essick2e9c63b2017-03-29 15:16:44 -0700524 if (only != NULL &&
525 strcmp(only, (*it)->getKey().c_str()) != 0) {
526 ALOGV("Omit '%s', it's not '%s'", (*it)->getKey().c_str(), only);
527 continue;
528 }
Ray Essick3938dc62016-11-01 08:56:56 -0700529 AString entry = (*it)->toString();
Ray Essick35ad27f2017-01-30 14:04:11 -0800530 result.appendFormat("%5d: %s\n", slot, entry.c_str());
Ray Essickb5fac8e2016-12-12 11:33:56 -0800531 slot++;
Ray Essick3938dc62016-11-01 08:56:56 -0700532 }
533 }
534
535 return result;
536}
537
538//
539// Our Cheap in-core, non-persistent records management.
540// XXX: rewrite this to manage persistence, etc.
541
542// insert appropriately into queue
Ray Essickb5fac8e2016-12-12 11:33:56 -0800543void MediaAnalyticsService::saveItem(List<MediaAnalyticsItem *> *l, MediaAnalyticsItem * item, int front) {
Ray Essick3938dc62016-11-01 08:56:56 -0700544
545 Mutex::Autolock _l(mLock);
546
Ray Essick3938dc62016-11-01 08:56:56 -0700547 // adding at back of queue (fifo order)
548 if (front) {
549 l->push_front(item);
550 } else {
551 l->push_back(item);
552 }
553
Ray Essick3938dc62016-11-01 08:56:56 -0700554 // keep removing old records the front until we're in-bounds
555 if (mMaxRecords > 0) {
556 while (l->size() > (size_t) mMaxRecords) {
Ray Essickb5fac8e2016-12-12 11:33:56 -0800557 MediaAnalyticsItem * oitem = *(l->begin());
Ray Essick3938dc62016-11-01 08:56:56 -0700558 l->erase(l->begin());
Ray Essickb5fac8e2016-12-12 11:33:56 -0800559 delete oitem;
Ray Essickb5fac8e2016-12-12 11:33:56 -0800560 mItemsDiscarded++;
Ray Essick3938dc62016-11-01 08:56:56 -0700561 }
562 }
Ray Essick3938dc62016-11-01 08:56:56 -0700563}
564
565// are they alike enough that nitem can be folded into oitem?
Ray Essickb5fac8e2016-12-12 11:33:56 -0800566static bool compatibleItems(MediaAnalyticsItem * oitem, MediaAnalyticsItem * nitem) {
Ray Essick3938dc62016-11-01 08:56:56 -0700567
568 if (0) {
569 ALOGD("Compare: o %s n %s",
570 oitem->toString().c_str(), nitem->toString().c_str());
571 }
572
573 // general safety
574 if (nitem->getUid() != oitem->getUid()) {
575 return false;
576 }
577 if (nitem->getPid() != oitem->getPid()) {
578 return false;
579 }
580
581 // key -- needs to match
582 if (nitem->getKey() == oitem->getKey()) {
583 // still in the game.
584 } else {
585 return false;
586 }
587
588 // session id -- empty field in new is allowed
589 MediaAnalyticsItem::SessionID_t osession = oitem->getSessionID();
590 MediaAnalyticsItem::SessionID_t nsession = nitem->getSessionID();
591 if (nsession != osession) {
592 // incoming '0' matches value in osession
593 if (nsession != 0) {
594 return false;
595 }
596 }
597
598 return true;
599}
600
601// find the incomplete record that this will overlay
Ray Essickb5fac8e2016-12-12 11:33:56 -0800602MediaAnalyticsItem *MediaAnalyticsService::findItem(List<MediaAnalyticsItem*> *theList, MediaAnalyticsItem *nitem, bool removeit) {
Ray Essick3938dc62016-11-01 08:56:56 -0700603 if (nitem == NULL) {
604 return NULL;
605 }
606
Ray Essickb5fac8e2016-12-12 11:33:56 -0800607 MediaAnalyticsItem *item = NULL;
608
Ray Essick3938dc62016-11-01 08:56:56 -0700609 Mutex::Autolock _l(mLock);
610
Ray Essickb5fac8e2016-12-12 11:33:56 -0800611 for (List<MediaAnalyticsItem *>::iterator it = theList->begin();
Ray Essick3938dc62016-11-01 08:56:56 -0700612 it != theList->end(); it++) {
Ray Essickb5fac8e2016-12-12 11:33:56 -0800613 MediaAnalyticsItem *tmp = (*it);
Ray Essick3938dc62016-11-01 08:56:56 -0700614
615 if (!compatibleItems(tmp, nitem)) {
616 continue;
617 }
618
619 // we match! this is the one I want.
620 if (removeit) {
621 theList->erase(it);
622 }
623 item = tmp;
624 break;
625 }
626 return item;
627}
628
629
630// delete the indicated record
Ray Essickb5fac8e2016-12-12 11:33:56 -0800631void MediaAnalyticsService::deleteItem(List<MediaAnalyticsItem *> *l, MediaAnalyticsItem *item) {
Ray Essick3938dc62016-11-01 08:56:56 -0700632
633 Mutex::Autolock _l(mLock);
634
Ray Essickb5fac8e2016-12-12 11:33:56 -0800635 for (List<MediaAnalyticsItem *>::iterator it = l->begin();
Ray Essick3938dc62016-11-01 08:56:56 -0700636 it != l->end(); it++) {
637 if ((*it)->getSessionID() != item->getSessionID())
638 continue;
Ray Essickb5fac8e2016-12-12 11:33:56 -0800639 delete *it;
Ray Essick3938dc62016-11-01 08:56:56 -0700640 l->erase(it);
641 break;
642 }
Ray Essick3938dc62016-11-01 08:56:56 -0700643}
644
Ray Essickd38e1742017-01-23 15:17:06 -0800645static AString allowedKeys[] =
646{
647 "codec",
648 "extractor"
649};
Ray Essick3938dc62016-11-01 08:56:56 -0700650
Ray Essickd38e1742017-01-23 15:17:06 -0800651static const int nAllowedKeys = sizeof(allowedKeys) / sizeof(allowedKeys[0]);
652
653// are the contents good
654bool MediaAnalyticsService::contentValid(MediaAnalyticsItem *item, bool isTrusted) {
655
656 // untrusted uids can only send us a limited set of keys
657 if (isTrusted == false) {
658 // restrict to a specific set of keys
659 AString key = item->getKey();
660
661 size_t i;
662 for(i = 0; i < nAllowedKeys; i++) {
663 if (key == allowedKeys[i]) {
664 break;
665 }
666 }
667 if (i == nAllowedKeys) {
668 ALOGD("Ignoring (key): %s", item->toString().c_str());
669 return false;
670 }
671 }
672
Ray Essick3938dc62016-11-01 08:56:56 -0700673 // internal consistency
674
675 return true;
676}
677
678// are we rate limited, normally false
Ray Essickb5fac8e2016-12-12 11:33:56 -0800679bool MediaAnalyticsService::rateLimited(MediaAnalyticsItem *) {
Ray Essick3938dc62016-11-01 08:56:56 -0700680
681 return false;
682}
683
Ray Essick2e9c63b2017-03-29 15:16:44 -0700684// insert into the appropriate summarizer.
685// we make our own copy to save/summarize
686void MediaAnalyticsService::summarize(MediaAnalyticsItem *item) {
687
688 ALOGV("MediaAnalyticsService::summarize()");
689
690 if (item == NULL) {
691 return;
692 }
693
694 nsecs_t now = systemTime(SYSTEM_TIME_REALTIME);
695 if (mCurrentSet == NULL
696 || (mCurrentSet->getStarted() + mNewSetInterval < now)) {
697 newSummarizerSet();
698 }
699
700 if (mCurrentSet == NULL) {
701 return;
702 }
703
704 List<MetricsSummarizer *> *summarizers = mCurrentSet->getSummarizers();
705 List<MetricsSummarizer *>::iterator it = summarizers->begin();
706 for (; it != summarizers->end(); it++) {
707 if ((*it)->isMine(*item)) {
708 break;
709 }
710 }
711 if (it == summarizers->end()) {
712 ALOGD("no handler for type %s", item->getKey().c_str());
713 return; // no handler
714 }
715
716 // invoke the summarizer. summarizer will make whatever copies
717 // it wants; the caller retains ownership of item.
718
719 (*it)->handleRecord(item);
720
721}
Ray Essick3938dc62016-11-01 08:56:56 -0700722
723} // namespace android