blob: 281a4ce394cb7e17ed5a7eedd6126dbb806acbfd [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
Andy Hung5be90c82021-03-30 14:30:20 -070040bool statsd_extractor(const std::shared_ptr<const mediametrics::Item>& item,
41 const std::shared_ptr<mediametrics::StatsdLog>& statsdLog)
Ray Essick6ce27e52019-02-15 10:58:05 -080042{
Andy Hung3ab1b322020-05-18 10:47:31 -070043 if (item == nullptr) return false;
Ray Essick6ce27e52019-02-15 10:58:05 -080044
45 // these go into the statsd wrapper
Andy Hung5be90c82021-03-30 14:30:20 -070046 const nsecs_t timestamp_nanos = MediaMetricsService::roundTime(item->getTimestamp());
47 const std::string package_name = item->getPkgName();
48 const int64_t package_version_code = item->getPkgVersionCode();
49 const int64_t media_apex_version = 0;
Ray Essick6ce27e52019-02-15 10:58:05 -080050
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
Andy Hung5be90c82021-03-30 14:30:20 -070058 std::string format;
59 if (item->getString("android.media.mediaextractor.fmt", &format)) {
60 metrics_proto.set_format(format);
Ray Essick6ce27e52019-02-15 10:58:05 -080061 }
62
Andy Hung5be90c82021-03-30 14:30:20 -070063 std::string mime;
64 if (item->getString("android.media.mediaextractor.mime", &mime)) {
65 metrics_proto.set_mime(mime);
66 }
67
68 int32_t tracks = -1;
69 if (item->getInt32("android.media.mediaextractor.ntrk", &tracks)) {
70 metrics_proto.set_tracks(tracks);
71 }
72
Santiago Seifert79c34f22020-11-11 23:49:28 +000073 std::string entry_point_string;
Andy Hung5be90c82021-03-30 14:30:20 -070074 stats::mediametrics::ExtractorData::EntryPoint entry_point =
75 stats::mediametrics::ExtractorData_EntryPoint_OTHER;
Santiago Seifert79c34f22020-11-11 23:49:28 +000076 if (item->getString("android.media.mediaextractor.entry", &entry_point_string)) {
Santiago Seifert79c34f22020-11-11 23:49:28 +000077 if (entry_point_string == "sdk") {
78 entry_point = stats::mediametrics::ExtractorData_EntryPoint_SDK;
79 } else if (entry_point_string == "ndk-with-jvm") {
80 entry_point = stats::mediametrics::ExtractorData_EntryPoint_NDK_WITH_JVM;
81 } else if (entry_point_string == "ndk-no-jvm") {
82 entry_point = stats::mediametrics::ExtractorData_EntryPoint_NDK_NO_JVM;
83 } else {
84 entry_point = stats::mediametrics::ExtractorData_EntryPoint_OTHER;
85 }
86 metrics_proto.set_entry_point(entry_point);
87 }
88
Santiago Seifert6dbe0d02021-04-28 23:38:51 +010089 // android.media.mediaextractor.logSessionId string
90 std::string log_session_id;
91 if (item->getString("android.media.mediaextractor.logSessionId", &log_session_id)) {
92 metrics_proto.set_log_session_id(log_session_id);
93 }
94
Ray Essick6ce27e52019-02-15 10:58:05 -080095 std::string serialized;
96 if (!metrics_proto.SerializeToString(&serialized)) {
97 ALOGE("Failed to serialize extractor metrics");
98 return false;
99 }
100
Andy Hung5be90c82021-03-30 14:30:20 -0700101 android::util::BytesField bf_serialized( serialized.c_str(), serialized.size());
102 int result = android::util::stats_write(android::util::MEDIAMETRICS_EXTRACTOR_REPORTED,
103 timestamp_nanos, package_name.c_str(), package_version_code,
104 media_apex_version,
105 bf_serialized);
106 std::stringstream log;
107 log << "result:" << result << " {"
108 << " mediametrics_extractor_reported:"
109 << android::util::MEDIAMETRICS_EXTRACTOR_REPORTED
110 << " timestamp_nanos:" << timestamp_nanos
111 << " package_name:" << package_name
112 << " package_version_code:" << package_version_code
113 << " media_apex_version:" << media_apex_version
Ray Essick6ce27e52019-02-15 10:58:05 -0800114
Andy Hung5be90c82021-03-30 14:30:20 -0700115 << " format:" << format
116 << " mime:" << mime
117 << " tracks:" << tracks
118 << " entry_point:" << entry_point_string << "(" << entry_point << ")"
Santiago Seifert6dbe0d02021-04-28 23:38:51 +0100119 << " log_session_id:" << log_session_id
Andy Hung5be90c82021-03-30 14:30:20 -0700120 << " }";
121 statsdLog->log(android::util::MEDIAMETRICS_EXTRACTOR_REPORTED, log.str());
Ray Essick6ce27e52019-02-15 10:58:05 -0800122 return true;
123}
124
Andy Hung3ab1b322020-05-18 10:47:31 -0700125} // namespace android