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