blob: 8a2158ff4d3eb0a8eeb527687ffc2cbf90d69f70 [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>
33
Ray Essick5a557292020-06-10 21:31:33 -070034#include "cleaner.h"
Ray Essick40e8e5e2019-12-05 20:19:40 -080035#include "MediaMetricsService.h"
Jeffrey Huang6adacdb2020-11-25 02:49:32 -080036#include "frameworks/proto_logging/stats/enums/stats/mediametrics/mediametrics.pb.h"
Ray Essick6ce27e52019-02-15 10:58:05 -080037#include "iface_statsd.h"
38
39namespace android {
40
Andy Hung5be90c82021-03-30 14:30:20 -070041bool statsd_codec(const std::shared_ptr<const mediametrics::Item>& item,
42 const std::shared_ptr<mediametrics::StatsdLog>& statsdLog)
Ray Essick6ce27e52019-02-15 10:58:05 -080043{
Andy Hung3ab1b322020-05-18 10:47:31 -070044 if (item == nullptr) return false;
Ray Essick6ce27e52019-02-15 10:58:05 -080045
46 // these go into the statsd wrapper
Andy Hung5be90c82021-03-30 14:30:20 -070047 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 Essick6ce27e52019-02-15 10:58:05 -080051
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 IV0d814432019-10-23 11:32:26 -070059 std::string codec;
60 if (item->getString("android.media.mediacodec.codec", &codec)) {
Andy Hung5be90c82021-03-30 14:30:20 -070061 metrics_proto.set_codec(codec);
Ray Essick6ce27e52019-02-15 10:58:05 -080062 }
Andy Hung5be90c82021-03-30 14:30:20 -070063
George Burgess IV0d814432019-10-23 11:32:26 -070064 std::string mime;
65 if (item->getString("android.media.mediacodec.mime", &mime)) {
Andy Hung5be90c82021-03-30 14:30:20 -070066 metrics_proto.set_mime(mime);
Ray Essick6ce27e52019-02-15 10:58:05 -080067 }
Andy Hung5be90c82021-03-30 14:30:20 -070068
George Burgess IV0d814432019-10-23 11:32:26 -070069 std::string mode;
70 if ( item->getString("android.media.mediacodec.mode", &mode)) {
Andy Hung5be90c82021-03-30 14:30:20 -070071 metrics_proto.set_mode(mode);
Ray Essick6ce27e52019-02-15 10:58:05 -080072 }
Andy Hung5be90c82021-03-30 14:30:20 -070073
Ray Essick6ce27e52019-02-15 10:58:05 -080074 int32_t encoder = -1;
75 if ( item->getInt32("android.media.mediacodec.encoder", &encoder)) {
76 metrics_proto.set_encoder(encoder);
77 }
Andy Hung5be90c82021-03-30 14:30:20 -070078
Ray Essick6ce27e52019-02-15 10:58:05 -080079 int32_t secure = -1;
80 if ( item->getInt32("android.media.mediacodec.secure", &secure)) {
81 metrics_proto.set_secure(secure);
82 }
Andy Hung5be90c82021-03-30 14:30:20 -070083
Ray Essick6ce27e52019-02-15 10:58:05 -080084 int32_t width = -1;
85 if ( item->getInt32("android.media.mediacodec.width", &width)) {
86 metrics_proto.set_width(width);
87 }
Andy Hung5be90c82021-03-30 14:30:20 -070088
Ray Essick6ce27e52019-02-15 10:58:05 -080089 int32_t height = -1;
90 if ( item->getInt32("android.media.mediacodec.height", &height)) {
91 metrics_proto.set_height(height);
92 }
Andy Hung5be90c82021-03-30 14:30:20 -070093
Ray Essick6ce27e52019-02-15 10:58:05 -080094 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 Hung5be90c82021-03-30 14:30:20 -0700103
Ray Essick6ce27e52019-02-15 10:58:05 -0800104 int32_t profile = -1;
105 if ( item->getInt32("android.media.mediacodec.profile", &profile)) {
106 metrics_proto.set_profile(profile);
107 }
Andy Hung5be90c82021-03-30 14:30:20 -0700108
Ray Essick6ce27e52019-02-15 10:58:05 -0800109 int32_t level = -1;
110 if ( item->getInt32("android.media.mediacodec.level", &level)) {
111 metrics_proto.set_level(level);
112 }
Andy Hung5be90c82021-03-30 14:30:20 -0700113
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 Essick6ce27e52019-02-15 10:58:05 -0800117 }
Andy Hung5be90c82021-03-30 14:30:20 -0700118
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 Essick6ce27e52019-02-15 10:58:05 -0800122 }
Andy Hung5be90c82021-03-30 14:30:20 -0700123
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 Essick6ce27e52019-02-15 10:58:05 -0800127 }
Andy Hung5be90c82021-03-30 14:30:20 -0700128
129 std::string error_state;
130 if ( item->getString("android.media.mediacodec.errstate", &error_state)) {
131 metrics_proto.set_error_state(error_state);
Ray Essick6ce27e52019-02-15 10:58:05 -0800132 }
Andy Hung5be90c82021-03-30 14:30:20 -0700133
Ray Essick6ce27e52019-02-15 10:58:05 -0800134 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 Hung5be90c82021-03-30 14:30:20 -0700138
Ray Essick6ce27e52019-02-15 10:58:05 -0800139 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 Hung5be90c82021-03-30 14:30:20 -0700143
Ray Essick6ce27e52019-02-15 10:58:05 -0800144 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 Hung5be90c82021-03-30 14:30:20 -0700148
Ray Essick6ce27e52019-02-15 10:58:05 -0800149 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 Hung5be90c82021-03-30 14:30:20 -0700153
Ray Essick6ce27e52019-02-15 10:58:05 -0800154 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 Hung5be90c82021-03-30 14:30:20 -0700158
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 Wong4f105392020-02-12 14:55:00 -0800163 }
Andy Hung5be90c82021-03-30 14:30:20 -0700164
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 Wong4f105392020-02-12 14:55:00 -0800169 }
Ray Essick6ce27e52019-02-15 10:58:05 -0800170 // android.media.mediacodec.latency.hist NOT EMITTED
171
Ray Essicka21a3d32020-05-10 21:08:10 -0700172 std::string bitrate_mode;
173 if (item->getString("android.media.mediacodec.bitrate_mode", &bitrate_mode)) {
Andy Hung5be90c82021-03-30 14:30:20 -0700174 metrics_proto.set_bitrate_mode(bitrate_mode);
Ray Essicka21a3d32020-05-10 21:08:10 -0700175 }
Andy Hung5be90c82021-03-30 14:30:20 -0700176
Ray Essicka21a3d32020-05-10 21:08:10 -0700177 int32_t bitrate = -1;
178 if (item->getInt32("android.media.mediacodec.bitrate", &bitrate)) {
179 metrics_proto.set_bitrate(bitrate);
180 }
Andy Hung5be90c82021-03-30 14:30:20 -0700181
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 Essicka21a3d32020-05-10 21:08:10 -0700186 }
Ray Essicka21a3d32020-05-10 21:08:10 -0700187
Ray Essick87913312021-03-02 10:45:54 -0800188 // 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 Essick6ce27e52019-02-15 10:58:05 -0800198 std::string serialized;
199 if (!metrics_proto.SerializeToString(&serialized)) {
200 ALOGE("Failed to serialize codec metrics");
201 return false;
202 }
Andy Hung5be90c82021-03-30 14:30:20 -0700203 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 Essick6ce27e52019-02-15 10:58:05 -0800216
Andy Hung5be90c82021-03-30 14:30:20 -0700217 << " 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 Essick6ce27e52019-02-15 10:58:05 -0800227
Andy Hung5be90c82021-03-30 14:30:20 -0700228 << " 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 Essick6ce27e52019-02-15 10:58:05 -0800238
Andy Hung5be90c82021-03-30 14:30:20 -0700239 << " 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 Essick6ce27e52019-02-15 10:58:05 -0800248 return true;
249}
250
Andy Hung3ab1b322020-05-18 10:47:31 -0700251} // namespace android