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" |
Jeffrey Huang | 6adacdb | 2020-11-25 02:49:32 -0800 | [diff] [blame] | 36 | #include "frameworks/proto_logging/stats/enums/stats/mediametrics/mediametrics.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_codec(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 | // |
| 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)) { |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame^] | 61 | metrics_proto.set_codec(codec); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 62 | } |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame^] | 63 | |
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)) { |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame^] | 66 | metrics_proto.set_mime(mime); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 67 | } |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame^] | 68 | |
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)) { |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame^] | 71 | metrics_proto.set_mode(mode); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 72 | } |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame^] | 73 | |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 74 | int32_t encoder = -1; |
| 75 | if ( item->getInt32("android.media.mediacodec.encoder", &encoder)) { |
| 76 | metrics_proto.set_encoder(encoder); |
| 77 | } |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame^] | 78 | |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 79 | int32_t secure = -1; |
| 80 | if ( item->getInt32("android.media.mediacodec.secure", &secure)) { |
| 81 | metrics_proto.set_secure(secure); |
| 82 | } |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame^] | 83 | |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 84 | int32_t width = -1; |
| 85 | if ( item->getInt32("android.media.mediacodec.width", &width)) { |
| 86 | metrics_proto.set_width(width); |
| 87 | } |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame^] | 88 | |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 89 | int32_t height = -1; |
| 90 | if ( item->getInt32("android.media.mediacodec.height", &height)) { |
| 91 | metrics_proto.set_height(height); |
| 92 | } |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame^] | 93 | |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 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 | } |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame^] | 103 | |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 104 | int32_t profile = -1; |
| 105 | if ( item->getInt32("android.media.mediacodec.profile", &profile)) { |
| 106 | metrics_proto.set_profile(profile); |
| 107 | } |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame^] | 108 | |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 109 | int32_t level = -1; |
| 110 | if ( item->getInt32("android.media.mediacodec.level", &level)) { |
| 111 | metrics_proto.set_level(level); |
| 112 | } |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame^] | 113 | |
| 114 | int32_t max_width = -1; |
| 115 | if ( item->getInt32("android.media.mediacodec.maxwidth", &max_width)) { |
| 116 | metrics_proto.set_max_width(max_width); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 117 | } |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame^] | 118 | |
| 119 | int32_t max_height = -1; |
| 120 | if ( item->getInt32("android.media.mediacodec.maxheight", &max_height)) { |
| 121 | metrics_proto.set_max_height(max_height); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 122 | } |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame^] | 123 | |
| 124 | int32_t error_code = -1; |
| 125 | if ( item->getInt32("android.media.mediacodec.errcode", &error_code)) { |
| 126 | metrics_proto.set_error_code(error_code); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 127 | } |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame^] | 128 | |
| 129 | std::string error_state; |
| 130 | if ( item->getString("android.media.mediacodec.errstate", &error_state)) { |
| 131 | metrics_proto.set_error_state(error_state); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 132 | } |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame^] | 133 | |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 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 | } |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame^] | 138 | |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 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 | } |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame^] | 143 | |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 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 | } |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame^] | 148 | |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 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 | } |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame^] | 153 | |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 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 | } |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame^] | 158 | |
| 159 | int32_t queue_secure_input_buffer_error = -1; |
| 160 | if (item->getInt32("android.media.mediacodec.queueSecureInputBufferError", |
| 161 | &queue_secure_input_buffer_error)) { |
| 162 | metrics_proto.set_queue_secure_input_buffer_error(queue_secure_input_buffer_error); |
Edwin Wong | 4f10539 | 2020-02-12 14:55:00 -0800 | [diff] [blame] | 163 | } |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame^] | 164 | |
| 165 | int32_t queue_input_buffer_error = -1; |
| 166 | if (item->getInt32("android.media.mediacodec.queueInputBufferError", |
| 167 | &queue_input_buffer_error)) { |
| 168 | metrics_proto.set_queue_input_buffer_error(queue_input_buffer_error); |
Edwin Wong | 4f10539 | 2020-02-12 14:55:00 -0800 | [diff] [blame] | 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 | std::string bitrate_mode; |
| 173 | if (item->getString("android.media.mediacodec.bitrate_mode", &bitrate_mode)) { |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame^] | 174 | metrics_proto.set_bitrate_mode(bitrate_mode); |
Ray Essick | a21a3d3 | 2020-05-10 21:08:10 -0700 | [diff] [blame] | 175 | } |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame^] | 176 | |
Ray Essick | a21a3d3 | 2020-05-10 21:08:10 -0700 | [diff] [blame] | 177 | int32_t bitrate = -1; |
| 178 | if (item->getInt32("android.media.mediacodec.bitrate", &bitrate)) { |
| 179 | metrics_proto.set_bitrate(bitrate); |
| 180 | } |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame^] | 181 | |
| 182 | int64_t lifetime_millis = -1; |
| 183 | if (item->getInt64("android.media.mediacodec.lifetimeMs", &lifetime_millis)) { |
| 184 | lifetime_millis = mediametrics::bucket_time_minutes(lifetime_millis); |
| 185 | metrics_proto.set_lifetime_millis(lifetime_millis); |
Ray Essick | a21a3d3 | 2020-05-10 21:08:10 -0700 | [diff] [blame] | 186 | } |
Ray Essick | a21a3d3 | 2020-05-10 21:08:10 -0700 | [diff] [blame] | 187 | |
Ray Essick | 8791331 | 2021-03-02 10:45:54 -0800 | [diff] [blame] | 188 | // new for S; need to plumb through to westworld |
| 189 | // android.media.mediacodec.channelCount int32 |
| 190 | // android.media.mediacodec.sampleRate int32 |
| 191 | |
| 192 | // new for S; need to plumb through to westworld |
| 193 | // TODO PWG may want these fuzzed up a bit to obscure some precision |
| 194 | // android.media.mediacodec.vencode.bytes int64 |
| 195 | // android.media.mediacodec.vencode.frames int64 |
| 196 | // android.media.mediacodec.vencode.durationUs int64 |
| 197 | |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 198 | std::string serialized; |
| 199 | if (!metrics_proto.SerializeToString(&serialized)) { |
| 200 | ALOGE("Failed to serialize codec metrics"); |
| 201 | return false; |
| 202 | } |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame^] | 203 | android::util::BytesField bf_serialized( serialized.c_str(), serialized.size()); |
| 204 | int result = android::util::stats_write(android::util::MEDIAMETRICS_CODEC_REPORTED, |
| 205 | timestamp_nanos, package_name.c_str(), package_version_code, |
| 206 | media_apex_version, |
| 207 | bf_serialized); |
| 208 | std::stringstream log; |
| 209 | log << "result:" << result << " {" |
| 210 | << " mediametrics_codec_reported:" |
| 211 | << android::util::MEDIAMETRICS_CODEC_REPORTED |
| 212 | << " timestamp_nanos:" << timestamp_nanos |
| 213 | << " package_name:" << package_name |
| 214 | << " package_version_code:" << package_version_code |
| 215 | << " media_apex_version:" << media_apex_version |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 216 | |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame^] | 217 | << " codec:" << codec |
| 218 | << " mime:" << mime |
| 219 | << " mode:" << mode |
| 220 | << " encoder:" << encoder |
| 221 | << " secure:" << secure |
| 222 | << " width:" << width |
| 223 | << " height:" << height |
| 224 | << " rotation:" << rotation |
| 225 | << " crypto:" << crypto |
| 226 | << " profile:" << profile |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 227 | |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame^] | 228 | << " level:" << level |
| 229 | << " max_width:" << max_width |
| 230 | << " max_height:" << max_height |
| 231 | << " error_code:" << error_code |
| 232 | << " error_state:" << error_state |
| 233 | << " latency_max:" << latency_max |
| 234 | << " latency_min:" << latency_min |
| 235 | << " latency_avg:" << latency_avg |
| 236 | << " latency_count:" << latency_count |
| 237 | << " latency_unknown:" << latency_unknown |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 238 | |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame^] | 239 | << " queue_input_buffer_error:" << queue_input_buffer_error |
| 240 | << " queue_secure_input_buffer_error:" << queue_secure_input_buffer_error |
| 241 | << " bitrate_mode:" << bitrate_mode |
| 242 | << " bitrate:" << bitrate |
| 243 | << " lifetime_millis:" << lifetime_millis |
| 244 | // TODO: add when log_session_id is merged. |
| 245 | // << " log_session_id:" << log_session_id |
| 246 | << " }"; |
| 247 | statsdLog->log(android::util::MEDIAMETRICS_CODEC_REPORTED, log.str()); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 248 | return true; |
| 249 | } |
| 250 | |
Andy Hung | 3ab1b32 | 2020-05-18 10:47:31 -0700 | [diff] [blame] | 251 | } // namespace android |