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_audiopolicy" |
| 19 | #include <utils/Log.h> |
| 20 | |
| 21 | #include <dirent.h> |
| 22 | #include <inttypes.h> |
| 23 | #include <pthread.h> |
| 24 | #include <pwd.h> |
| 25 | #include <stdint.h> |
| 26 | #include <string.h> |
| 27 | #include <sys/stat.h> |
| 28 | #include <sys/time.h> |
| 29 | #include <sys/types.h> |
| 30 | #include <unistd.h> |
| 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_audiopolicy(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::AudioPolicyData metrics_proto; |
| 54 | |
| 55 | // flesh out the protobuf we'll hand off with our data |
| 56 | // |
| 57 | //int32 char kAudioPolicyStatus[] = "android.media.audiopolicy.status"; |
| 58 | int32_t status = -1; |
| 59 | if (item->getInt32("android.media.audiopolicy.status", &status)) { |
| 60 | metrics_proto.set_status(status); |
| 61 | } |
| 62 | //string char kAudioPolicyRqstSrc[] = "android.media.audiopolicy.rqst.src"; |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame] | 63 | std::string request_source; |
| 64 | if (item->getString("android.media.audiopolicy.rqst.src", &request_source)) { |
| 65 | metrics_proto.set_request_source(request_source); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 66 | } |
| 67 | //string char kAudioPolicyRqstPkg[] = "android.media.audiopolicy.rqst.pkg"; |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame] | 68 | std::string request_package; |
| 69 | if (item->getString("android.media.audiopolicy.rqst.pkg", &request_package)) { |
| 70 | metrics_proto.set_request_package(request_package); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 71 | } |
| 72 | //int32 char kAudioPolicyRqstSession[] = "android.media.audiopolicy.rqst.session"; |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame] | 73 | int32_t request_session = -1; |
| 74 | if (item->getInt32("android.media.audiopolicy.rqst.session", &request_session)) { |
| 75 | metrics_proto.set_request_session(request_session); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 76 | } |
| 77 | //string char kAudioPolicyRqstDevice[] = "android.media.audiopolicy.rqst.device"; |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame] | 78 | std::string request_device; |
| 79 | if (item->getString("android.media.audiopolicy.rqst.device", &request_device)) { |
| 80 | metrics_proto.set_request_device(request_device); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | //string char kAudioPolicyActiveSrc[] = "android.media.audiopolicy.active.src"; |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame] | 84 | std::string active_source; |
| 85 | if (item->getString("android.media.audiopolicy.active.src", &active_source)) { |
| 86 | metrics_proto.set_active_source(active_source); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 87 | } |
| 88 | //string char kAudioPolicyActivePkg[] = "android.media.audiopolicy.active.pkg"; |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame] | 89 | std::string active_package; |
| 90 | if (item->getString("android.media.audiopolicy.active.pkg", &active_package)) { |
| 91 | metrics_proto.set_active_package(active_package); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 92 | } |
| 93 | //int32 char kAudioPolicyActiveSession[] = "android.media.audiopolicy.active.session"; |
| 94 | int32_t active_session = -1; |
| 95 | if (item->getInt32("android.media.audiopolicy.active.session", &active_session)) { |
| 96 | metrics_proto.set_active_session(active_session); |
| 97 | } |
| 98 | //string char kAudioPolicyActiveDevice[] = "android.media.audiopolicy.active.device"; |
George Burgess IV | 0d81443 | 2019-10-23 11:32:26 -0700 | [diff] [blame] | 99 | std::string active_device; |
| 100 | if (item->getString("android.media.audiopolicy.active.device", &active_device)) { |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame] | 101 | metrics_proto.set_active_device(active_device); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 104 | std::string serialized; |
| 105 | if (!metrics_proto.SerializeToString(&serialized)) { |
| 106 | ALOGE("Failed to serialize audipolicy metrics"); |
| 107 | return false; |
| 108 | } |
| 109 | |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame] | 110 | android::util::BytesField bf_serialized( serialized.c_str(), serialized.size()); |
| 111 | int result = android::util::stats_write(android::util::MEDIAMETRICS_AUDIOPOLICY_REPORTED, |
| 112 | timestamp_nanos, package_name.c_str(), package_version_code, |
| 113 | media_apex_version, |
| 114 | bf_serialized); |
| 115 | std::stringstream log; |
| 116 | log << "result:" << result << " {" |
| 117 | << " mediametrics_audiopolicy_reported:" |
| 118 | << android::util::MEDIAMETRICS_AUDIOPOLICY_REPORTED |
| 119 | << " timestamp_nanos:" << timestamp_nanos |
| 120 | << " package_name:" << package_name |
| 121 | << " package_version_code:" << package_version_code |
| 122 | << " media_apex_version:" << media_apex_version |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 123 | |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame] | 124 | << " status:" << status |
| 125 | << " request_source:" << request_source |
| 126 | << " request_package:" << request_package |
| 127 | << " request_session:" << request_session |
| 128 | << " request_device:" << request_device |
| 129 | << " active_source:" << active_source |
| 130 | << " active_package:" << active_package |
| 131 | << " active_session:" << active_session |
| 132 | << " active_device:" << active_device |
| 133 | << " }"; |
| 134 | statsdLog->log(android::util::MEDIAMETRICS_AUDIOPOLICY_REPORTED, log.str()); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 135 | return true; |
| 136 | } |
| 137 | |
Andy Hung | 3ab1b32 | 2020-05-18 10:47:31 -0700 | [diff] [blame] | 138 | } // namespace android |