blob: 85814375d53467463850ee68ddc59cb8223cc5ee [file] [log] [blame]
Ray Essick6ce27e52019-02-15 10:58:05 -08001/*
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>
Brian Lindahlc935ee22021-06-08 09:57:07 +020033#include <stats_event.h>
Ray Essick6ce27e52019-02-15 10:58:05 -080034
Ray Essick5a557292020-06-10 21:31:33 -070035#include "cleaner.h"
Ray Essick40e8e5e2019-12-05 20:19:40 -080036#include "MediaMetricsService.h"
Andy Hungc9b6f8b2021-07-08 10:17:55 -070037#include "ValidateId.h"
Dichen Zhangb8f23c52021-03-22 00:56:29 -070038#include "frameworks/proto_logging/stats/message/mediametrics_message.pb.h"
Ray Essick6ce27e52019-02-15 10:58:05 -080039#include "iface_statsd.h"
40
41namespace android {
42
Andy Hung5be90c82021-03-30 14:30:20 -070043bool statsd_codec(const std::shared_ptr<const mediametrics::Item>& item,
44 const std::shared_ptr<mediametrics::StatsdLog>& statsdLog)
Ray Essick6ce27e52019-02-15 10:58:05 -080045{
Andy Hung3ab1b322020-05-18 10:47:31 -070046 if (item == nullptr) return false;
Ray Essick6ce27e52019-02-15 10:58:05 -080047
Brian Lindahlc935ee22021-06-08 09:57:07 +020048 AStatsEvent* event = AStatsEvent_obtain();
49 AStatsEvent_setAtomId(event, android::util::MEDIA_CODEC_REPORTED);
50
Andy Hung5be90c82021-03-30 14:30:20 -070051 const nsecs_t timestamp_nanos = MediaMetricsService::roundTime(item->getTimestamp());
Brian Lindahlc935ee22021-06-08 09:57:07 +020052 AStatsEvent_writeInt64(event, timestamp_nanos);
53
54 std::string package_name = item->getPkgName();
55 AStatsEvent_writeString(event, package_name.c_str());
56
57 int64_t package_version_code = item->getPkgVersionCode();
58 AStatsEvent_writeInt64(event, package_version_code);
59
60 int64_t media_apex_version = 0;
61 AStatsEvent_writeInt64(event, media_apex_version);
Ray Essick6ce27e52019-02-15 10:58:05 -080062
63 // the rest into our own proto
64 //
Dichen Zhangb8f23c52021-03-22 00:56:29 -070065 ::android::stats::mediametrics_message::CodecData metrics_proto;
Ray Essick6ce27e52019-02-15 10:58:05 -080066
67 // flesh out the protobuf we'll hand off with our data
68 //
George Burgess IV0d814432019-10-23 11:32:26 -070069 std::string codec;
70 if (item->getString("android.media.mediacodec.codec", &codec)) {
Andy Hung5be90c82021-03-30 14:30:20 -070071 metrics_proto.set_codec(codec);
Ray Essick6ce27e52019-02-15 10:58:05 -080072 }
Brian Lindahlc935ee22021-06-08 09:57:07 +020073 AStatsEvent_writeString(event, codec.c_str());
Andy Hung5be90c82021-03-30 14:30:20 -070074
George Burgess IV0d814432019-10-23 11:32:26 -070075 std::string mime;
76 if (item->getString("android.media.mediacodec.mime", &mime)) {
Andy Hung5be90c82021-03-30 14:30:20 -070077 metrics_proto.set_mime(mime);
Ray Essick6ce27e52019-02-15 10:58:05 -080078 }
Brian Lindahlc935ee22021-06-08 09:57:07 +020079 AStatsEvent_writeString(event, mime.c_str());
Andy Hung5be90c82021-03-30 14:30:20 -070080
George Burgess IV0d814432019-10-23 11:32:26 -070081 std::string mode;
Brian Lindahlc935ee22021-06-08 09:57:07 +020082 if (item->getString("android.media.mediacodec.mode", &mode)) {
Andy Hung5be90c82021-03-30 14:30:20 -070083 metrics_proto.set_mode(mode);
Ray Essick6ce27e52019-02-15 10:58:05 -080084 }
Brian Lindahlc935ee22021-06-08 09:57:07 +020085 AStatsEvent_writeString(event, mode.c_str());
Andy Hung5be90c82021-03-30 14:30:20 -070086
Ray Essick6ce27e52019-02-15 10:58:05 -080087 int32_t encoder = -1;
Brian Lindahlc935ee22021-06-08 09:57:07 +020088 if (item->getInt32("android.media.mediacodec.encoder", &encoder)) {
Ray Essick6ce27e52019-02-15 10:58:05 -080089 metrics_proto.set_encoder(encoder);
90 }
Brian Lindahlc935ee22021-06-08 09:57:07 +020091 AStatsEvent_writeInt32(event, encoder);
Andy Hung5be90c82021-03-30 14:30:20 -070092
Ray Essick6ce27e52019-02-15 10:58:05 -080093 int32_t secure = -1;
Brian Lindahlc935ee22021-06-08 09:57:07 +020094 if (item->getInt32("android.media.mediacodec.secure", &secure)) {
Ray Essick6ce27e52019-02-15 10:58:05 -080095 metrics_proto.set_secure(secure);
96 }
Brian Lindahlc935ee22021-06-08 09:57:07 +020097 AStatsEvent_writeInt32(event, secure);
Andy Hung5be90c82021-03-30 14:30:20 -070098
Ray Essick6ce27e52019-02-15 10:58:05 -080099 int32_t width = -1;
Brian Lindahlc935ee22021-06-08 09:57:07 +0200100 if (item->getInt32("android.media.mediacodec.width", &width)) {
Ray Essick6ce27e52019-02-15 10:58:05 -0800101 metrics_proto.set_width(width);
102 }
Brian Lindahlc935ee22021-06-08 09:57:07 +0200103 AStatsEvent_writeInt32(event, width);
Andy Hung5be90c82021-03-30 14:30:20 -0700104
Ray Essick6ce27e52019-02-15 10:58:05 -0800105 int32_t height = -1;
Brian Lindahlc935ee22021-06-08 09:57:07 +0200106 if (item->getInt32("android.media.mediacodec.height", &height)) {
Ray Essick6ce27e52019-02-15 10:58:05 -0800107 metrics_proto.set_height(height);
108 }
Brian Lindahlc935ee22021-06-08 09:57:07 +0200109 AStatsEvent_writeInt32(event, height);
Andy Hung5be90c82021-03-30 14:30:20 -0700110
Ray Essick6ce27e52019-02-15 10:58:05 -0800111 int32_t rotation = -1;
Brian Lindahlc935ee22021-06-08 09:57:07 +0200112 if (item->getInt32("android.media.mediacodec.rotation-degrees", &rotation)) {
Ray Essick6ce27e52019-02-15 10:58:05 -0800113 metrics_proto.set_rotation(rotation);
114 }
Brian Lindahlc935ee22021-06-08 09:57:07 +0200115 AStatsEvent_writeInt32(event, rotation);
116
Ray Essick6ce27e52019-02-15 10:58:05 -0800117 int32_t crypto = -1;
Brian Lindahlc935ee22021-06-08 09:57:07 +0200118 if (item->getInt32("android.media.mediacodec.crypto", &crypto)) {
Ray Essick6ce27e52019-02-15 10:58:05 -0800119 metrics_proto.set_crypto(crypto);
120 }
Brian Lindahlc935ee22021-06-08 09:57:07 +0200121 AStatsEvent_writeInt32(event, crypto);
Andy Hung5be90c82021-03-30 14:30:20 -0700122
Ray Essick6ce27e52019-02-15 10:58:05 -0800123 int32_t profile = -1;
Brian Lindahlc935ee22021-06-08 09:57:07 +0200124 if (item->getInt32("android.media.mediacodec.profile", &profile)) {
Ray Essick6ce27e52019-02-15 10:58:05 -0800125 metrics_proto.set_profile(profile);
126 }
Brian Lindahlc935ee22021-06-08 09:57:07 +0200127 AStatsEvent_writeInt32(event, profile);
Andy Hung5be90c82021-03-30 14:30:20 -0700128
Ray Essick6ce27e52019-02-15 10:58:05 -0800129 int32_t level = -1;
Brian Lindahlc935ee22021-06-08 09:57:07 +0200130 if (item->getInt32("android.media.mediacodec.level", &level)) {
Ray Essick6ce27e52019-02-15 10:58:05 -0800131 metrics_proto.set_level(level);
132 }
Brian Lindahlc935ee22021-06-08 09:57:07 +0200133 AStatsEvent_writeInt32(event, level);
134
Andy Hung5be90c82021-03-30 14:30:20 -0700135
136 int32_t max_width = -1;
137 if ( item->getInt32("android.media.mediacodec.maxwidth", &max_width)) {
138 metrics_proto.set_max_width(max_width);
Ray Essick6ce27e52019-02-15 10:58:05 -0800139 }
Brian Lindahlc935ee22021-06-08 09:57:07 +0200140 AStatsEvent_writeInt32(event, max_width);
Andy Hung5be90c82021-03-30 14:30:20 -0700141
142 int32_t max_height = -1;
143 if ( item->getInt32("android.media.mediacodec.maxheight", &max_height)) {
144 metrics_proto.set_max_height(max_height);
Ray Essick6ce27e52019-02-15 10:58:05 -0800145 }
Brian Lindahlc935ee22021-06-08 09:57:07 +0200146 AStatsEvent_writeInt32(event, max_height);
Andy Hung5be90c82021-03-30 14:30:20 -0700147
148 int32_t error_code = -1;
149 if ( item->getInt32("android.media.mediacodec.errcode", &error_code)) {
150 metrics_proto.set_error_code(error_code);
Ray Essick6ce27e52019-02-15 10:58:05 -0800151 }
Brian Lindahlc935ee22021-06-08 09:57:07 +0200152 AStatsEvent_writeInt32(event, error_code);
Andy Hung5be90c82021-03-30 14:30:20 -0700153
154 std::string error_state;
155 if ( item->getString("android.media.mediacodec.errstate", &error_state)) {
156 metrics_proto.set_error_state(error_state);
Ray Essick6ce27e52019-02-15 10:58:05 -0800157 }
Brian Lindahlc935ee22021-06-08 09:57:07 +0200158 AStatsEvent_writeString(event, error_state.c_str());
Andy Hung5be90c82021-03-30 14:30:20 -0700159
Ray Essick6ce27e52019-02-15 10:58:05 -0800160 int64_t latency_max = -1;
Brian Lindahlc935ee22021-06-08 09:57:07 +0200161 if (item->getInt64("android.media.mediacodec.latency.max", &latency_max)) {
Ray Essick6ce27e52019-02-15 10:58:05 -0800162 metrics_proto.set_latency_max(latency_max);
163 }
Brian Lindahlc935ee22021-06-08 09:57:07 +0200164 AStatsEvent_writeInt64(event, latency_max);
Andy Hung5be90c82021-03-30 14:30:20 -0700165
Ray Essick6ce27e52019-02-15 10:58:05 -0800166 int64_t latency_min = -1;
Brian Lindahlc935ee22021-06-08 09:57:07 +0200167 if (item->getInt64("android.media.mediacodec.latency.min", &latency_min)) {
Ray Essick6ce27e52019-02-15 10:58:05 -0800168 metrics_proto.set_latency_min(latency_min);
169 }
Brian Lindahlc935ee22021-06-08 09:57:07 +0200170 AStatsEvent_writeInt64(event, latency_min);
Andy Hung5be90c82021-03-30 14:30:20 -0700171
Ray Essick6ce27e52019-02-15 10:58:05 -0800172 int64_t latency_avg = -1;
Brian Lindahlc935ee22021-06-08 09:57:07 +0200173 if (item->getInt64("android.media.mediacodec.latency.avg", &latency_avg)) {
Ray Essick6ce27e52019-02-15 10:58:05 -0800174 metrics_proto.set_latency_avg(latency_avg);
175 }
Brian Lindahlc935ee22021-06-08 09:57:07 +0200176 AStatsEvent_writeInt64(event, latency_avg);
Andy Hung5be90c82021-03-30 14:30:20 -0700177
Ray Essick6ce27e52019-02-15 10:58:05 -0800178 int64_t latency_count = -1;
Brian Lindahlc935ee22021-06-08 09:57:07 +0200179 if (item->getInt64("android.media.mediacodec.latency.n", &latency_count)) {
Ray Essick6ce27e52019-02-15 10:58:05 -0800180 metrics_proto.set_latency_count(latency_count);
181 }
Brian Lindahlc935ee22021-06-08 09:57:07 +0200182 AStatsEvent_writeInt64(event, latency_count);
Andy Hung5be90c82021-03-30 14:30:20 -0700183
Ray Essick6ce27e52019-02-15 10:58:05 -0800184 int64_t latency_unknown = -1;
Brian Lindahlc935ee22021-06-08 09:57:07 +0200185 if (item->getInt64("android.media.mediacodec.latency.unknown", &latency_unknown)) {
Ray Essick6ce27e52019-02-15 10:58:05 -0800186 metrics_proto.set_latency_unknown(latency_unknown);
187 }
Brian Lindahlc935ee22021-06-08 09:57:07 +0200188 AStatsEvent_writeInt64(event, latency_unknown);
Andy Hung5be90c82021-03-30 14:30:20 -0700189
190 int32_t queue_secure_input_buffer_error = -1;
191 if (item->getInt32("android.media.mediacodec.queueSecureInputBufferError",
Brian Lindahlc935ee22021-06-08 09:57:07 +0200192 &queue_secure_input_buffer_error)) {
Andy Hung5be90c82021-03-30 14:30:20 -0700193 metrics_proto.set_queue_secure_input_buffer_error(queue_secure_input_buffer_error);
Edwin Wong4f105392020-02-12 14:55:00 -0800194 }
Brian Lindahlc935ee22021-06-08 09:57:07 +0200195 AStatsEvent_writeInt32(event, queue_secure_input_buffer_error);
Andy Hung5be90c82021-03-30 14:30:20 -0700196
197 int32_t queue_input_buffer_error = -1;
198 if (item->getInt32("android.media.mediacodec.queueInputBufferError",
Brian Lindahlc935ee22021-06-08 09:57:07 +0200199 &queue_input_buffer_error)) {
Andy Hung5be90c82021-03-30 14:30:20 -0700200 metrics_proto.set_queue_input_buffer_error(queue_input_buffer_error);
Edwin Wong4f105392020-02-12 14:55:00 -0800201 }
Brian Lindahlc935ee22021-06-08 09:57:07 +0200202 AStatsEvent_writeInt32(event, queue_input_buffer_error);
Ray Essick6ce27e52019-02-15 10:58:05 -0800203
Ray Essicka21a3d32020-05-10 21:08:10 -0700204 std::string bitrate_mode;
205 if (item->getString("android.media.mediacodec.bitrate_mode", &bitrate_mode)) {
Andy Hung5be90c82021-03-30 14:30:20 -0700206 metrics_proto.set_bitrate_mode(bitrate_mode);
Ray Essicka21a3d32020-05-10 21:08:10 -0700207 }
Brian Lindahlc935ee22021-06-08 09:57:07 +0200208 AStatsEvent_writeString(event, bitrate_mode.c_str());
Andy Hung5be90c82021-03-30 14:30:20 -0700209
Ray Essicka21a3d32020-05-10 21:08:10 -0700210 int32_t bitrate = -1;
211 if (item->getInt32("android.media.mediacodec.bitrate", &bitrate)) {
212 metrics_proto.set_bitrate(bitrate);
213 }
Brian Lindahlc935ee22021-06-08 09:57:07 +0200214 AStatsEvent_writeInt32(event, bitrate);
Andy Hung5be90c82021-03-30 14:30:20 -0700215
216 int64_t lifetime_millis = -1;
217 if (item->getInt64("android.media.mediacodec.lifetimeMs", &lifetime_millis)) {
218 lifetime_millis = mediametrics::bucket_time_minutes(lifetime_millis);
219 metrics_proto.set_lifetime_millis(lifetime_millis);
Ray Essicka21a3d32020-05-10 21:08:10 -0700220 }
Brian Lindahlc935ee22021-06-08 09:57:07 +0200221 AStatsEvent_writeInt64(event, lifetime_millis);
Ray Essicka21a3d32020-05-10 21:08:10 -0700222
Brian Lindahlc935ee22021-06-08 09:57:07 +0200223 int64_t playback_duration_sec = -1;
224 item->getInt64("android.media.mediacodec.playback-duration-sec", &playback_duration_sec);
225 // DO NOT record playback-duration in the metrics_proto - it should only
226 // exist in the flattened atom
227 AStatsEvent_writeInt64(event, playback_duration_sec);
228
229 std::string sessionId;
230 if (item->getString("android.media.mediacodec.log-session-id", &sessionId)) {
Andy Hungc9b6f8b2021-07-08 10:17:55 -0700231 sessionId = mediametrics::ValidateId::get()->validateId(sessionId);
Brian Lindahlc935ee22021-06-08 09:57:07 +0200232 metrics_proto.set_log_session_id(sessionId);
233 }
234 AStatsEvent_writeString(event, codec.c_str());
235
Dichen Zhang0de05752021-04-21 19:47:07 -0700236 int32_t channelCount = -1;
Brian Lindahlc935ee22021-06-08 09:57:07 +0200237 if (item->getInt32("android.media.mediacodec.channelCount", &channelCount)) {
Dichen Zhang0de05752021-04-21 19:47:07 -0700238 metrics_proto.set_channel_count(channelCount);
239 }
Brian Lindahlc935ee22021-06-08 09:57:07 +0200240 AStatsEvent_writeInt32(event, channelCount);
Ray Essick87913312021-03-02 10:45:54 -0800241
Dichen Zhang0de05752021-04-21 19:47:07 -0700242 int32_t sampleRate = -1;
Brian Lindahlc935ee22021-06-08 09:57:07 +0200243 if (item->getInt32("android.media.mediacodec.sampleRate", &sampleRate)) {
Dichen Zhang0de05752021-04-21 19:47:07 -0700244 metrics_proto.set_sample_rate(sampleRate);
245 }
Brian Lindahlc935ee22021-06-08 09:57:07 +0200246 AStatsEvent_writeInt32(event, sampleRate);
Dichen Zhang0de05752021-04-21 19:47:07 -0700247
Ray Essick87913312021-03-02 10:45:54 -0800248 // TODO PWG may want these fuzzed up a bit to obscure some precision
Dichen Zhang0de05752021-04-21 19:47:07 -0700249 int64_t bytes = -1;
Brian Lindahlc935ee22021-06-08 09:57:07 +0200250 if (item->getInt64("android.media.mediacodec.vencode.bytes", &bytes)) {
Dichen Zhang0de05752021-04-21 19:47:07 -0700251 metrics_proto.set_video_encode_bytes(bytes);
252 }
Brian Lindahlc935ee22021-06-08 09:57:07 +0200253 AStatsEvent_writeInt64(event, bytes);
Dichen Zhang0de05752021-04-21 19:47:07 -0700254
Dichen Zhang0de05752021-04-21 19:47:07 -0700255 int64_t frames = -1;
Brian Lindahlc935ee22021-06-08 09:57:07 +0200256 if (item->getInt64("android.media.mediacodec.vencode.frames", &frames)) {
Dichen Zhang0de05752021-04-21 19:47:07 -0700257 metrics_proto.set_video_encode_frames(frames);
258 }
Brian Lindahlc935ee22021-06-08 09:57:07 +0200259 AStatsEvent_writeInt64(event, frames);
Dichen Zhang0de05752021-04-21 19:47:07 -0700260
Dichen Zhang0de05752021-04-21 19:47:07 -0700261 int64_t inputBytes = -1;
Brian Lindahlc935ee22021-06-08 09:57:07 +0200262 if (item->getInt64("android.media.mediacodec.video.input.bytes", &inputBytes)) {
Dichen Zhang0de05752021-04-21 19:47:07 -0700263 metrics_proto.set_video_input_bytes(inputBytes);
264 }
Brian Lindahlc935ee22021-06-08 09:57:07 +0200265 AStatsEvent_writeInt64(event, inputBytes);
Dichen Zhang0de05752021-04-21 19:47:07 -0700266
Dichen Zhang0de05752021-04-21 19:47:07 -0700267 int64_t inputFrames = -1;
Brian Lindahlc935ee22021-06-08 09:57:07 +0200268 if (item->getInt64("android.media.mediacodec.video.input.frames", &inputFrames)) {
Dichen Zhang0de05752021-04-21 19:47:07 -0700269 metrics_proto.set_video_input_frames(inputFrames);
270 }
Brian Lindahlc935ee22021-06-08 09:57:07 +0200271 AStatsEvent_writeInt64(event, inputFrames);
Ray Essick87913312021-03-02 10:45:54 -0800272
Brian Lindahlc935ee22021-06-08 09:57:07 +0200273 int64_t durationUs = -1;
274 if (item->getInt64("android.media.mediacodec.vencode.durationUs", &durationUs)) {
275 metrics_proto.set_video_encode_duration_us(durationUs);
276 }
277 AStatsEvent_writeInt64(event, durationUs);
278
279 int32_t colorFormat = -1;
280 if (item->getInt32("android.media.mediacodec.color-format", &colorFormat)) {
281 metrics_proto.set_color_format(colorFormat);
282 }
283 AStatsEvent_writeInt32(event, colorFormat);
284
285 double frameRate = -1.0;
286 if (item->getDouble("android.media.mediacodec.frame-rate", &frameRate)) {
287 metrics_proto.set_frame_rate(frameRate);
288 }
289 AStatsEvent_writeFloat(event, (float) frameRate);
290
291 double captureRate = -1.0;
292 if (item->getDouble("android.media.mediacodec.capture-rate", &captureRate)) {
293 metrics_proto.set_capture_rate(captureRate);
294 }
295 AStatsEvent_writeFloat(event, (float) captureRate);
296
297 double operatingRate = -1.0;
298 if (item->getDouble("android.media.mediacodec.operating-rate", &operatingRate)) {
299 metrics_proto.set_operating_rate(operatingRate);
300 }
301 AStatsEvent_writeFloat(event, (float) operatingRate);
302
303 int32_t priority = -1;
304 if (item->getInt32("android.media.mediacodec.priority", &priority)) {
305 metrics_proto.set_priority(priority);
306 }
307 AStatsEvent_writeInt32(event, priority);
308
309 int32_t qpIMin = -1;
310 if (item->getInt32("android.media.mediacodec.video-qp-i-min", &qpIMin)) {
311 metrics_proto.set_video_qp_i_min(qpIMin);
312 }
313 AStatsEvent_writeInt32(event, qpIMin);
314
315 int32_t qpIMax = -1;
316 if (item->getInt32("android.media.mediacodec.video-qp-i-max", &qpIMax)) {
317 metrics_proto.set_video_qp_i_max(qpIMax);
318 }
319 AStatsEvent_writeInt32(event, qpIMax);
320
321 int32_t qpPMin = -1;
322 if (item->getInt32("android.media.mediacodec.video-qp-p-min", &qpPMin)) {
323 metrics_proto.set_video_qp_p_min(qpPMin);
324 }
325 AStatsEvent_writeInt32(event, qpPMin);
326
327 int32_t qpPMax = -1;
328 if (item->getInt32("android.media.mediacodec.video-qp-p-max", &qpPMax)) {
329 metrics_proto.set_video_qp_p_max(qpPMax);
330 }
331 AStatsEvent_writeInt32(event, qpPMax);
332
333 int32_t qpBMin = -1;
334 if (item->getInt32("android.media.mediacodec.video-qp-b-min", &qpBMin)) {
335 metrics_proto.set_video_qp_b_min(qpBMin);
336 }
337 AStatsEvent_writeInt32(event, qpBMin);
338
339 int32_t qpBMax = -1;
340 if (item->getInt32("android.media.mediacodec.video-qp-b-max", &qpBMax)) {
341 metrics_proto.set_video_qp_b_max(qpBMax);
342 }
343 AStatsEvent_writeInt32(event, qpBMax);
344
Dichen Zhange5bad782021-04-28 12:11:35 -0700345 int32_t originalBitrate = -1;
Brian Lindahlc935ee22021-06-08 09:57:07 +0200346 if (item->getInt32("android.media.mediacodec.original.bitrate", &originalBitrate)) {
Dichen Zhange5bad782021-04-28 12:11:35 -0700347 metrics_proto.set_original_bitrate(originalBitrate);
348 }
Brian Lindahlc935ee22021-06-08 09:57:07 +0200349 AStatsEvent_writeInt32(event, originalBitrate);
Dichen Zhange5bad782021-04-28 12:11:35 -0700350
Dichen Zhang57be6302021-05-18 18:20:31 -0700351 int32_t shapingEnhanced = -1;
352 if ( item->getInt32("android.media.mediacodec.shaped", &shapingEnhanced)) {
353 metrics_proto.set_shaping_enhanced(shapingEnhanced);
354 }
Brian Lindahlc935ee22021-06-08 09:57:07 +0200355 AStatsEvent_writeInt32(event, shapingEnhanced);
Dichen Zhang57be6302021-05-18 18:20:31 -0700356
Dichen Zhang57be6302021-05-18 18:20:31 -0700357 int32_t qpIMinOri = -1;
358 if ( item->getInt32("android.media.mediacodec.original-video-qp-i-min", &qpIMinOri)) {
359 metrics_proto.set_original_video_qp_i_min(qpIMinOri);
360 }
Brian Lindahlc935ee22021-06-08 09:57:07 +0200361 AStatsEvent_writeInt32(event, qpIMinOri);
Dichen Zhang57be6302021-05-18 18:20:31 -0700362
Dichen Zhang57be6302021-05-18 18:20:31 -0700363 int32_t qpIMaxOri = -1;
364 if ( item->getInt32("android.media.mediacodec.original-video-qp-i-max", &qpIMaxOri)) {
365 metrics_proto.set_original_video_qp_i_max(qpIMaxOri);
366 }
Brian Lindahlc935ee22021-06-08 09:57:07 +0200367 AStatsEvent_writeInt32(event, qpIMaxOri);
Dichen Zhang57be6302021-05-18 18:20:31 -0700368
Dichen Zhang57be6302021-05-18 18:20:31 -0700369 int32_t qpPMinOri = -1;
370 if ( item->getInt32("android.media.mediacodec.original-video-qp-p-min", &qpPMinOri)) {
371 metrics_proto.set_original_video_qp_p_min(qpPMinOri);
372 }
Brian Lindahlc935ee22021-06-08 09:57:07 +0200373 AStatsEvent_writeInt32(event, qpPMinOri);
Dichen Zhang57be6302021-05-18 18:20:31 -0700374
Dichen Zhang57be6302021-05-18 18:20:31 -0700375 int32_t qpPMaxOri = -1;
376 if ( item->getInt32("android.media.mediacodec.original-video-qp-p-max", &qpPMaxOri)) {
377 metrics_proto.set_original_video_qp_p_max(qpPMaxOri);
378 }
Brian Lindahlc935ee22021-06-08 09:57:07 +0200379 AStatsEvent_writeInt32(event, qpPMaxOri);
Dichen Zhang57be6302021-05-18 18:20:31 -0700380
Dichen Zhang57be6302021-05-18 18:20:31 -0700381 int32_t qpBMinOri = -1;
382 if ( item->getInt32("android.media.mediacodec.original-video-qp-b-min", &qpBMinOri)) {
Brian Lindahlc935ee22021-06-08 09:57:07 +0200383 metrics_proto.set_original_video_qp_b_min(qpBMinOri);
Dichen Zhang57be6302021-05-18 18:20:31 -0700384 }
Brian Lindahlc935ee22021-06-08 09:57:07 +0200385 AStatsEvent_writeInt32(event, qpBMinOri);
Dichen Zhang57be6302021-05-18 18:20:31 -0700386
Dichen Zhang57be6302021-05-18 18:20:31 -0700387 int32_t qpBMaxOri = -1;
388 if ( item->getInt32("android.media.mediacodec.original-video-qp-b-max", &qpBMaxOri)) {
389 metrics_proto.set_original_video_qp_b_max(qpBMaxOri);
390 }
Brian Lindahlc935ee22021-06-08 09:57:07 +0200391 AStatsEvent_writeInt32(event, qpBMaxOri);
392
393 int err = AStatsEvent_write(event);
394 if (err < 0) {
395 ALOGE("Failed to write codec metrics to statsd (%d)", err);
396 }
397 AStatsEvent_release(event);
Dichen Zhang57be6302021-05-18 18:20:31 -0700398
Ray Essick6ce27e52019-02-15 10:58:05 -0800399 std::string serialized;
400 if (!metrics_proto.SerializeToString(&serialized)) {
401 ALOGE("Failed to serialize codec metrics");
402 return false;
403 }
Andy Hung5be90c82021-03-30 14:30:20 -0700404 android::util::BytesField bf_serialized( serialized.c_str(), serialized.size());
405 int result = android::util::stats_write(android::util::MEDIAMETRICS_CODEC_REPORTED,
406 timestamp_nanos, package_name.c_str(), package_version_code,
407 media_apex_version,
408 bf_serialized);
Brian Lindahlc935ee22021-06-08 09:57:07 +0200409
Andy Hung5be90c82021-03-30 14:30:20 -0700410 std::stringstream log;
411 log << "result:" << result << " {"
412 << " mediametrics_codec_reported:"
413 << android::util::MEDIAMETRICS_CODEC_REPORTED
414 << " timestamp_nanos:" << timestamp_nanos
415 << " package_name:" << package_name
416 << " package_version_code:" << package_version_code
417 << " media_apex_version:" << media_apex_version
Ray Essick6ce27e52019-02-15 10:58:05 -0800418
Andy Hung5be90c82021-03-30 14:30:20 -0700419 << " codec:" << codec
420 << " mime:" << mime
421 << " mode:" << mode
422 << " encoder:" << encoder
423 << " secure:" << secure
424 << " width:" << width
425 << " height:" << height
426 << " rotation:" << rotation
427 << " crypto:" << crypto
428 << " profile:" << profile
Ray Essick6ce27e52019-02-15 10:58:05 -0800429
Andy Hung5be90c82021-03-30 14:30:20 -0700430 << " level:" << level
431 << " max_width:" << max_width
432 << " max_height:" << max_height
433 << " error_code:" << error_code
434 << " error_state:" << error_state
435 << " latency_max:" << latency_max
436 << " latency_min:" << latency_min
437 << " latency_avg:" << latency_avg
438 << " latency_count:" << latency_count
439 << " latency_unknown:" << latency_unknown
Ray Essick6ce27e52019-02-15 10:58:05 -0800440
Andy Hung5be90c82021-03-30 14:30:20 -0700441 << " queue_input_buffer_error:" << queue_input_buffer_error
442 << " queue_secure_input_buffer_error:" << queue_secure_input_buffer_error
443 << " bitrate_mode:" << bitrate_mode
444 << " bitrate:" << bitrate
Dichen Zhang57be6302021-05-18 18:20:31 -0700445 << " original_bitrate:" << originalBitrate
Andy Hung5be90c82021-03-30 14:30:20 -0700446 << " lifetime_millis:" << lifetime_millis
Brian Lindahlc935ee22021-06-08 09:57:07 +0200447 << " playback_duration_seconds:" << playback_duration_sec
Dichen Zhang57be6302021-05-18 18:20:31 -0700448 << " log_session_id:" << sessionId
449 << " channel_count:" << channelCount
450 << " sample_rate:" << sampleRate
451 << " encode_bytes:" << bytes
452 << " encode_frames:" << frames
453 << " encode_duration_us:" << durationUs
454 << " color_format:" << colorFormat
455 << " frame_rate:" << frameRate
456 << " capture_rate:" << captureRate
457 << " operating_rate:" << operatingRate
458 << " priority:" << priority
459 << " shaping_enhanced:" << shapingEnhanced
460
461 << " qp_i_min:" << qpIMin
462 << " qp_i_max:" << qpIMax
463 << " qp_p_min:" << qpPMin
464 << " qp_p_max:" << qpPMax
465 << " qp_b_min:" << qpBMin
466 << " qp_b_max:" << qpBMax
467 << " original_qp_i_min:" << qpIMinOri
468 << " original_qp_i_max:" << qpIMaxOri
469 << " original_qp_p_min:" << qpPMinOri
470 << " original_qp_p_max:" << qpPMaxOri
471 << " original_qp_b_min:" << qpBMinOri
472 << " original_qp_b_max:" << qpBMaxOri
Andy Hung5be90c82021-03-30 14:30:20 -0700473 << " }";
474 statsdLog->log(android::util::MEDIAMETRICS_CODEC_REPORTED, log.str());
Brian Lindahlc935ee22021-06-08 09:57:07 +0200475
476
Ray Essick6ce27e52019-02-15 10:58:05 -0800477 return true;
478}
479
Andy Hung3ab1b322020-05-18 10:47:31 -0700480} // namespace android