blob: 16814d9e4d8c18c37ffcebbd79a695a9ebd40d7a [file] [log] [blame]
Ray Essick6ce27e52019-02-15 10:58:05 -08001/*
2 * Copyright (C) 2019 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//#define LOG_NDEBUG 0
18#define LOG_TAG "statsd_extractor"
19#include <utils/Log.h>
20
Hangyu Kuang66062672020-12-03 19:30:25 +000021#include <dirent.h>
22#include <inttypes.h>
23#include <pthread.h>
24#include <pwd.h>
Ray Essick6ce27e52019-02-15 10:58:05 -080025#include <stdint.h>
26#include <string.h>
Hangyu Kuang66062672020-12-03 19:30:25 +000027#include <sys/stat.h>
28#include <sys/time.h>
29#include <sys/types.h>
30#include <unistd.h>
Ray Essick6ce27e52019-02-15 10:58:05 -080031
32#include <statslog.h>
33
Ray Essick40e8e5e2019-12-05 20:19:40 -080034#include "MediaMetricsService.h"
Jeffrey Huang6adacdb2020-11-25 02:49:32 -080035#include "frameworks/proto_logging/stats/enums/stats/mediametrics/mediametrics.pb.h"
Ray Essick6ce27e52019-02-15 10:58:05 -080036#include "iface_statsd.h"
37
38namespace android {
39
Ray Essickf27e9872019-12-07 06:28:46 -080040bool statsd_extractor(const mediametrics::Item *item)
Ray Essick6ce27e52019-02-15 10:58:05 -080041{
Andy Hung3ab1b322020-05-18 10:47:31 -070042 if (item == nullptr) return false;
Ray Essick6ce27e52019-02-15 10:58:05 -080043
44 // these go into the statsd wrapper
Ray Essickf27e9872019-12-07 06:28:46 -080045 const nsecs_t timestamp = MediaMetricsService::roundTime(item->getTimestamp());
Ray Essick6ce27e52019-02-15 10:58:05 -080046 std::string pkgName = item->getPkgName();
47 int64_t pkgVersionCode = item->getPkgVersionCode();
48 int64_t mediaApexVersion = 0;
49
50
51 // the rest into our own proto
52 //
53 ::android::stats::mediametrics::ExtractorData metrics_proto;
54
55 // flesh out the protobuf we'll hand off with our data
56 //
57
58 // android.media.mediaextractor.fmt string
George Burgess IV0d814432019-10-23 11:32:26 -070059 std::string fmt;
Hangyu Kuang66062672020-12-03 19:30:25 +000060 if (item->getString("android.media.mediaextractor.fmt", &fmt)) {
George Burgess IV0d814432019-10-23 11:32:26 -070061 metrics_proto.set_format(std::move(fmt));
Ray Essick6ce27e52019-02-15 10:58:05 -080062 }
63 // android.media.mediaextractor.mime string
George Burgess IV0d814432019-10-23 11:32:26 -070064 std::string mime;
Hangyu Kuang66062672020-12-03 19:30:25 +000065 if (item->getString("android.media.mediaextractor.mime", &mime)) {
George Burgess IV0d814432019-10-23 11:32:26 -070066 metrics_proto.set_mime(std::move(mime));
Ray Essick6ce27e52019-02-15 10:58:05 -080067 }
68 // android.media.mediaextractor.ntrk int32
69 int32_t ntrk = -1;
Hangyu Kuang66062672020-12-03 19:30:25 +000070 if (item->getInt32("android.media.mediaextractor.ntrk", &ntrk)) {
Ray Essick6ce27e52019-02-15 10:58:05 -080071 metrics_proto.set_tracks(ntrk);
72 }
73
74 std::string serialized;
75 if (!metrics_proto.SerializeToString(&serialized)) {
76 ALOGE("Failed to serialize extractor metrics");
77 return false;
78 }
79
80 if (enabled_statsd) {
81 android::util::BytesField bf_serialized( serialized.c_str(), serialized.size());
82 (void)android::util::stats_write(android::util::MEDIAMETRICS_EXTRACTOR_REPORTED,
83 timestamp, pkgName.c_str(), pkgVersionCode,
84 mediaApexVersion,
85 bf_serialized);
86
87 } else {
88 ALOGV("NOT sending: private data (len=%zu)", strlen(serialized.c_str()));
89 }
90
Ray Essick6ce27e52019-02-15 10:58:05 -080091 return true;
92}
93
Andy Hung3ab1b322020-05-18 10:47:31 -070094} // namespace android