Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 1 | /* |
| 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 Kuang | 6606267 | 2020-12-03 19:30:25 +0000 | [diff] [blame] | 21 | #include <dirent.h> |
| 22 | #include <inttypes.h> |
| 23 | #include <pthread.h> |
| 24 | #include <pwd.h> |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 25 | #include <stdint.h> |
| 26 | #include <string.h> |
Hangyu Kuang | 6606267 | 2020-12-03 19:30:25 +0000 | [diff] [blame] | 27 | #include <sys/stat.h> |
| 28 | #include <sys/time.h> |
| 29 | #include <sys/types.h> |
| 30 | #include <unistd.h> |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 31 | |
| 32 | #include <statslog.h> |
| 33 | |
Ray Essick | 40e8e5e | 2019-12-05 20:19:40 -0800 | [diff] [blame] | 34 | #include "MediaMetricsService.h" |
Jeffrey Huang | 6adacdb | 2020-11-25 02:49:32 -0800 | [diff] [blame] | 35 | #include "frameworks/proto_logging/stats/enums/stats/mediametrics/mediametrics.pb.h" |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 36 | #include "iface_statsd.h" |
| 37 | |
| 38 | namespace android { |
| 39 | |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame] | 40 | bool statsd_extractor(const std::shared_ptr<const mediametrics::Item>& item, |
| 41 | const std::shared_ptr<mediametrics::StatsdLog>& statsdLog) |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 42 | { |
Andy Hung | 3ab1b32 | 2020-05-18 10:47:31 -0700 | [diff] [blame] | 43 | if (item == nullptr) return false; |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 44 | |
| 45 | // these go into the statsd wrapper |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame] | 46 | 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 Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 50 | |
| 51 | // the rest into our own proto |
| 52 | // |
| 53 | ::android::stats::mediametrics::ExtractorData metrics_proto; |
| 54 | |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame] | 55 | std::string format; |
| 56 | if (item->getString("android.media.mediaextractor.fmt", &format)) { |
| 57 | metrics_proto.set_format(format); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame] | 60 | std::string mime; |
| 61 | if (item->getString("android.media.mediaextractor.mime", &mime)) { |
| 62 | metrics_proto.set_mime(mime); |
| 63 | } |
| 64 | |
| 65 | int32_t tracks = -1; |
| 66 | if (item->getInt32("android.media.mediaextractor.ntrk", &tracks)) { |
| 67 | metrics_proto.set_tracks(tracks); |
| 68 | } |
| 69 | |
Santiago Seifert | 79c34f2 | 2020-11-11 23:49:28 +0000 | [diff] [blame] | 70 | std::string entry_point_string; |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame] | 71 | stats::mediametrics::ExtractorData::EntryPoint entry_point = |
| 72 | stats::mediametrics::ExtractorData_EntryPoint_OTHER; |
Santiago Seifert | 79c34f2 | 2020-11-11 23:49:28 +0000 | [diff] [blame] | 73 | if (item->getString("android.media.mediaextractor.entry", &entry_point_string)) { |
Santiago Seifert | 79c34f2 | 2020-11-11 23:49:28 +0000 | [diff] [blame] | 74 | if (entry_point_string == "sdk") { |
| 75 | entry_point = stats::mediametrics::ExtractorData_EntryPoint_SDK; |
| 76 | } else if (entry_point_string == "ndk-with-jvm") { |
| 77 | entry_point = stats::mediametrics::ExtractorData_EntryPoint_NDK_WITH_JVM; |
| 78 | } else if (entry_point_string == "ndk-no-jvm") { |
| 79 | entry_point = stats::mediametrics::ExtractorData_EntryPoint_NDK_NO_JVM; |
| 80 | } else { |
| 81 | entry_point = stats::mediametrics::ExtractorData_EntryPoint_OTHER; |
| 82 | } |
| 83 | metrics_proto.set_entry_point(entry_point); |
| 84 | } |
| 85 | |
Santiago Seifert | 6dbe0d0 | 2021-04-28 23:38:51 +0100 | [diff] [blame] | 86 | std::string log_session_id; |
| 87 | if (item->getString("android.media.mediaextractor.logSessionId", &log_session_id)) { |
| 88 | metrics_proto.set_log_session_id(log_session_id); |
| 89 | } |
| 90 | |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 91 | std::string serialized; |
| 92 | if (!metrics_proto.SerializeToString(&serialized)) { |
| 93 | ALOGE("Failed to serialize extractor metrics"); |
| 94 | return false; |
| 95 | } |
| 96 | |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame] | 97 | android::util::BytesField bf_serialized( serialized.c_str(), serialized.size()); |
| 98 | int result = android::util::stats_write(android::util::MEDIAMETRICS_EXTRACTOR_REPORTED, |
| 99 | timestamp_nanos, package_name.c_str(), package_version_code, |
| 100 | media_apex_version, |
| 101 | bf_serialized); |
| 102 | std::stringstream log; |
| 103 | log << "result:" << result << " {" |
| 104 | << " mediametrics_extractor_reported:" |
| 105 | << android::util::MEDIAMETRICS_EXTRACTOR_REPORTED |
| 106 | << " timestamp_nanos:" << timestamp_nanos |
| 107 | << " package_name:" << package_name |
| 108 | << " package_version_code:" << package_version_code |
| 109 | << " media_apex_version:" << media_apex_version |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 110 | |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame] | 111 | << " format:" << format |
| 112 | << " mime:" << mime |
| 113 | << " tracks:" << tracks |
| 114 | << " entry_point:" << entry_point_string << "(" << entry_point << ")" |
Santiago Seifert | 6dbe0d0 | 2021-04-28 23:38:51 +0100 | [diff] [blame] | 115 | << " log_session_id:" << log_session_id |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame] | 116 | << " }"; |
| 117 | statsdLog->log(android::util::MEDIAMETRICS_EXTRACTOR_REPORTED, log.str()); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 118 | return true; |
| 119 | } |
| 120 | |
Andy Hung | 3ab1b32 | 2020-05-18 10:47:31 -0700 | [diff] [blame] | 121 | } // namespace android |