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_codec" |
| 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 | 5a55729 | 2020-06-10 21:31:33 -0700 | [diff] [blame] | 34 | #include "cleaner.h" |
Ray Essick | 40e8e5e | 2019-12-05 20:19:40 -0800 | [diff] [blame] | 35 | #include "MediaMetricsService.h" |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 36 | #include "frameworks/base/core/proto/android/stats/mediametrics/mediametrics.pb.h" |
| 37 | #include "iface_statsd.h" |
| 38 | |
| 39 | namespace android { |
| 40 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 41 | bool statsd_codec(const mediametrics::Item *item) |
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 |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 46 | const nsecs_t timestamp = MediaMetricsService::roundTime(item->getTimestamp()); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 47 | std::string pkgName = item->getPkgName(); |
| 48 | int64_t pkgVersionCode = item->getPkgVersionCode(); |
| 49 | int64_t mediaApexVersion = 0; |
| 50 | |
| 51 | |
| 52 | // the rest into our own proto |
| 53 | // |
| 54 | ::android::stats::mediametrics::CodecData metrics_proto; |
| 55 | |
| 56 | // flesh out the protobuf we'll hand off with our data |
| 57 | // |
| 58 | // android.media.mediacodec.codec string |
George Burgess IV | 0d81443 | 2019-10-23 11:32:26 -0700 | [diff] [blame] | 59 | std::string codec; |
| 60 | if (item->getString("android.media.mediacodec.codec", &codec)) { |
| 61 | metrics_proto.set_codec(std::move(codec)); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 62 | } |
| 63 | // android.media.mediacodec.mime string |
George Burgess IV | 0d81443 | 2019-10-23 11:32:26 -0700 | [diff] [blame] | 64 | std::string mime; |
| 65 | if (item->getString("android.media.mediacodec.mime", &mime)) { |
| 66 | metrics_proto.set_mime(std::move(mime)); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 67 | } |
| 68 | // android.media.mediacodec.mode string |
George Burgess IV | 0d81443 | 2019-10-23 11:32:26 -0700 | [diff] [blame] | 69 | std::string mode; |
| 70 | if ( item->getString("android.media.mediacodec.mode", &mode)) { |
| 71 | metrics_proto.set_mode(std::move(mode)); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 72 | } |
| 73 | // android.media.mediacodec.encoder int32 |
| 74 | int32_t encoder = -1; |
| 75 | if ( item->getInt32("android.media.mediacodec.encoder", &encoder)) { |
| 76 | metrics_proto.set_encoder(encoder); |
| 77 | } |
| 78 | // android.media.mediacodec.secure int32 |
| 79 | int32_t secure = -1; |
| 80 | if ( item->getInt32("android.media.mediacodec.secure", &secure)) { |
| 81 | metrics_proto.set_secure(secure); |
| 82 | } |
| 83 | // android.media.mediacodec.width int32 |
| 84 | int32_t width = -1; |
| 85 | if ( item->getInt32("android.media.mediacodec.width", &width)) { |
| 86 | metrics_proto.set_width(width); |
| 87 | } |
| 88 | // android.media.mediacodec.height int32 |
| 89 | int32_t height = -1; |
| 90 | if ( item->getInt32("android.media.mediacodec.height", &height)) { |
| 91 | metrics_proto.set_height(height); |
| 92 | } |
| 93 | // android.media.mediacodec.rotation-degrees int32 |
| 94 | int32_t rotation = -1; |
| 95 | if ( item->getInt32("android.media.mediacodec.rotation-degrees", &rotation)) { |
| 96 | metrics_proto.set_rotation(rotation); |
| 97 | } |
| 98 | // android.media.mediacodec.crypto int32 (although missing if not needed |
| 99 | int32_t crypto = -1; |
| 100 | if ( item->getInt32("android.media.mediacodec.crypto", &crypto)) { |
| 101 | metrics_proto.set_crypto(crypto); |
| 102 | } |
| 103 | // android.media.mediacodec.profile int32 |
| 104 | int32_t profile = -1; |
| 105 | if ( item->getInt32("android.media.mediacodec.profile", &profile)) { |
| 106 | metrics_proto.set_profile(profile); |
| 107 | } |
| 108 | // android.media.mediacodec.level int32 |
| 109 | int32_t level = -1; |
| 110 | if ( item->getInt32("android.media.mediacodec.level", &level)) { |
| 111 | metrics_proto.set_level(level); |
| 112 | } |
| 113 | // android.media.mediacodec.maxwidth int32 |
| 114 | int32_t maxwidth = -1; |
| 115 | if ( item->getInt32("android.media.mediacodec.maxwidth", &maxwidth)) { |
| 116 | metrics_proto.set_max_width(maxwidth); |
| 117 | } |
| 118 | // android.media.mediacodec.maxheight int32 |
| 119 | int32_t maxheight = -1; |
| 120 | if ( item->getInt32("android.media.mediacodec.maxheight", &maxheight)) { |
| 121 | metrics_proto.set_max_height(maxheight); |
| 122 | } |
| 123 | // android.media.mediacodec.errcode int32 |
| 124 | int32_t errcode = -1; |
| 125 | if ( item->getInt32("android.media.mediacodec.errcode", &errcode)) { |
| 126 | metrics_proto.set_error_code(errcode); |
| 127 | } |
| 128 | // android.media.mediacodec.errstate string |
George Burgess IV | 0d81443 | 2019-10-23 11:32:26 -0700 | [diff] [blame] | 129 | std::string errstate; |
| 130 | if ( item->getString("android.media.mediacodec.errstate", &errstate)) { |
| 131 | metrics_proto.set_error_state(std::move(errstate)); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 132 | } |
| 133 | // android.media.mediacodec.latency.max int64 |
| 134 | int64_t latency_max = -1; |
| 135 | if ( item->getInt64("android.media.mediacodec.latency.max", &latency_max)) { |
| 136 | metrics_proto.set_latency_max(latency_max); |
| 137 | } |
| 138 | // android.media.mediacodec.latency.min int64 |
| 139 | int64_t latency_min = -1; |
| 140 | if ( item->getInt64("android.media.mediacodec.latency.min", &latency_min)) { |
| 141 | metrics_proto.set_latency_min(latency_min); |
| 142 | } |
| 143 | // android.media.mediacodec.latency.avg int64 |
| 144 | int64_t latency_avg = -1; |
| 145 | if ( item->getInt64("android.media.mediacodec.latency.avg", &latency_avg)) { |
| 146 | metrics_proto.set_latency_avg(latency_avg); |
| 147 | } |
| 148 | // android.media.mediacodec.latency.n int64 |
| 149 | int64_t latency_count = -1; |
| 150 | if ( item->getInt64("android.media.mediacodec.latency.n", &latency_count)) { |
| 151 | metrics_proto.set_latency_count(latency_count); |
| 152 | } |
| 153 | // android.media.mediacodec.latency.unknown int64 |
| 154 | int64_t latency_unknown = -1; |
| 155 | if ( item->getInt64("android.media.mediacodec.latency.unknown", &latency_unknown)) { |
| 156 | metrics_proto.set_latency_unknown(latency_unknown); |
| 157 | } |
Edwin Wong | 4f10539 | 2020-02-12 14:55:00 -0800 | [diff] [blame] | 158 | // android.media.mediacodec.queueSecureInputBufferError int32 |
| 159 | if (int32_t queueSecureInputBufferError = -1; |
| 160 | item->getInt32("android.media.mediacodec.queueSecureInputBufferError", |
| 161 | &queueSecureInputBufferError)) { |
| 162 | metrics_proto.set_queue_secure_input_buffer_error(queueSecureInputBufferError); |
| 163 | } |
| 164 | // android.media.mediacodec.queueInputBufferError int32 |
| 165 | if (int32_t queueInputBufferError = -1; |
| 166 | item->getInt32("android.media.mediacodec.queueInputBufferError", |
| 167 | &queueInputBufferError)) { |
| 168 | metrics_proto.set_queue_input_buffer_error(queueInputBufferError); |
| 169 | } |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 170 | // android.media.mediacodec.latency.hist NOT EMITTED |
| 171 | |
Ray Essick | a21a3d3 | 2020-05-10 21:08:10 -0700 | [diff] [blame] | 172 | // android.media.mediacodec.bitrate_mode string |
| 173 | std::string bitrate_mode; |
| 174 | if (item->getString("android.media.mediacodec.bitrate_mode", &bitrate_mode)) { |
| 175 | metrics_proto.set_bitrate_mode(std::move(bitrate_mode)); |
| 176 | } |
| 177 | // android.media.mediacodec.bitrate int32 |
| 178 | int32_t bitrate = -1; |
| 179 | if (item->getInt32("android.media.mediacodec.bitrate", &bitrate)) { |
| 180 | metrics_proto.set_bitrate(bitrate); |
| 181 | } |
| 182 | // android.media.mediacodec.lifetimeMs int64 |
| 183 | int64_t lifetimeMs = -1; |
| 184 | if ( item->getInt64("android.media.mediacodec.lifetimeMs", &lifetimeMs)) { |
Ray Essick | 5a55729 | 2020-06-10 21:31:33 -0700 | [diff] [blame] | 185 | lifetimeMs = mediametrics::bucket_time_minutes(lifetimeMs); |
Ray Essick | a21a3d3 | 2020-05-10 21:08:10 -0700 | [diff] [blame] | 186 | metrics_proto.set_lifetime_millis(lifetimeMs); |
| 187 | } |
Ray Essick | a21a3d3 | 2020-05-10 21:08:10 -0700 | [diff] [blame] | 188 | |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 189 | std::string serialized; |
| 190 | if (!metrics_proto.SerializeToString(&serialized)) { |
| 191 | ALOGE("Failed to serialize codec metrics"); |
| 192 | return false; |
| 193 | } |
| 194 | |
| 195 | if (enabled_statsd) { |
| 196 | android::util::BytesField bf_serialized( serialized.c_str(), serialized.size()); |
| 197 | (void)android::util::stats_write(android::util::MEDIAMETRICS_CODEC_REPORTED, |
| 198 | timestamp, pkgName.c_str(), pkgVersionCode, |
| 199 | mediaApexVersion, |
| 200 | bf_serialized); |
| 201 | |
| 202 | } else { |
| 203 | ALOGV("NOT sending: private data (len=%zu)", strlen(serialized.c_str())); |
| 204 | } |
| 205 | |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 206 | return true; |
| 207 | } |
| 208 | |
Andy Hung | 3ab1b32 | 2020-05-18 10:47:31 -0700 | [diff] [blame] | 209 | } // namespace android |