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 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 40 | bool statsd_extractor(const mediametrics::Item *item) |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 41 | { |
Andy Hung | 3ab1b32 | 2020-05-18 10:47:31 -0700 | [diff] [blame] | 42 | if (item == nullptr) return false; |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 43 | |
| 44 | // these go into the statsd wrapper |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 45 | const nsecs_t timestamp = MediaMetricsService::roundTime(item->getTimestamp()); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 46 | 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 IV | 0d81443 | 2019-10-23 11:32:26 -0700 | [diff] [blame] | 59 | std::string fmt; |
Hangyu Kuang | 6606267 | 2020-12-03 19:30:25 +0000 | [diff] [blame] | 60 | if (item->getString("android.media.mediaextractor.fmt", &fmt)) { |
George Burgess IV | 0d81443 | 2019-10-23 11:32:26 -0700 | [diff] [blame] | 61 | metrics_proto.set_format(std::move(fmt)); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 62 | } |
| 63 | // android.media.mediaextractor.mime string |
George Burgess IV | 0d81443 | 2019-10-23 11:32:26 -0700 | [diff] [blame] | 64 | std::string mime; |
Hangyu Kuang | 6606267 | 2020-12-03 19:30:25 +0000 | [diff] [blame] | 65 | if (item->getString("android.media.mediaextractor.mime", &mime)) { |
George Burgess IV | 0d81443 | 2019-10-23 11:32:26 -0700 | [diff] [blame] | 66 | metrics_proto.set_mime(std::move(mime)); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 67 | } |
| 68 | // android.media.mediaextractor.ntrk int32 |
| 69 | int32_t ntrk = -1; |
Hangyu Kuang | 6606267 | 2020-12-03 19:30:25 +0000 | [diff] [blame] | 70 | if (item->getInt32("android.media.mediaextractor.ntrk", &ntrk)) { |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 71 | metrics_proto.set_tracks(ntrk); |
| 72 | } |
| 73 | |
Santiago Seifert | 79c34f2 | 2020-11-11 23:49:28 +0000 | [diff] [blame] | 74 | // android.media.mediaextractor.entry string |
| 75 | std::string entry_point_string; |
| 76 | if (item->getString("android.media.mediaextractor.entry", &entry_point_string)) { |
| 77 | stats::mediametrics::ExtractorData::EntryPoint entry_point; |
| 78 | if (entry_point_string == "sdk") { |
| 79 | entry_point = stats::mediametrics::ExtractorData_EntryPoint_SDK; |
| 80 | } else if (entry_point_string == "ndk-with-jvm") { |
| 81 | entry_point = stats::mediametrics::ExtractorData_EntryPoint_NDK_WITH_JVM; |
| 82 | } else if (entry_point_string == "ndk-no-jvm") { |
| 83 | entry_point = stats::mediametrics::ExtractorData_EntryPoint_NDK_NO_JVM; |
| 84 | } else { |
| 85 | entry_point = stats::mediametrics::ExtractorData_EntryPoint_OTHER; |
| 86 | } |
| 87 | metrics_proto.set_entry_point(entry_point); |
| 88 | } |
| 89 | |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 90 | std::string serialized; |
| 91 | if (!metrics_proto.SerializeToString(&serialized)) { |
| 92 | ALOGE("Failed to serialize extractor metrics"); |
| 93 | return false; |
| 94 | } |
| 95 | |
| 96 | if (enabled_statsd) { |
| 97 | android::util::BytesField bf_serialized( serialized.c_str(), serialized.size()); |
| 98 | (void)android::util::stats_write(android::util::MEDIAMETRICS_EXTRACTOR_REPORTED, |
| 99 | timestamp, pkgName.c_str(), pkgVersionCode, |
| 100 | mediaApexVersion, |
| 101 | bf_serialized); |
| 102 | |
| 103 | } else { |
| 104 | ALOGV("NOT sending: private data (len=%zu)", strlen(serialized.c_str())); |
| 105 | } |
| 106 | |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 107 | return true; |
| 108 | } |
| 109 | |
Andy Hung | 3ab1b32 | 2020-05-18 10:47:31 -0700 | [diff] [blame] | 110 | } // namespace android |