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 #include <stats_event.h>
34
35 #include "cleaner.h"
36 #include "MediaMetricsService.h"
37 #include "StringUtils.h"
38 #include "frameworks/proto_logging/stats/message/mediametrics_message.pb.h"
39 #include "iface_statsd.h"
40
41 namespace android {
42
statsd_codec(const std::shared_ptr<const mediametrics::Item> & item,const std::shared_ptr<mediametrics::StatsdLog> & statsdLog)43 bool statsd_codec(const std::shared_ptr<const mediametrics::Item>& item,
44 const std::shared_ptr<mediametrics::StatsdLog>& statsdLog)
45 {
46 if (item == nullptr) return false;
47
48 AStatsEvent* event = AStatsEvent_obtain();
49 AStatsEvent_setAtomId(event, android::util::MEDIA_CODEC_REPORTED);
50
51 const nsecs_t timestamp_nanos = MediaMetricsService::roundTime(item->getTimestamp());
52 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);
62
63 // the rest into our own proto
64 //
65 ::android::stats::mediametrics_message::CodecData metrics_proto;
66
67 // flesh out the protobuf we'll hand off with our data
68 //
69 std::string codec;
70 if (item->getString("android.media.mediacodec.codec", &codec)) {
71 metrics_proto.set_codec(codec);
72 }
73 AStatsEvent_writeString(event, codec.c_str());
74
75 std::string mime;
76 if (item->getString("android.media.mediacodec.mime", &mime)) {
77 metrics_proto.set_mime(mime);
78 }
79 AStatsEvent_writeString(event, mime.c_str());
80
81 std::string mode;
82 if (item->getString("android.media.mediacodec.mode", &mode)) {
83 metrics_proto.set_mode(mode);
84 }
85 AStatsEvent_writeString(event, mode.c_str());
86
87 int32_t encoder = -1;
88 if (item->getInt32("android.media.mediacodec.encoder", &encoder)) {
89 metrics_proto.set_encoder(encoder);
90 }
91 AStatsEvent_writeInt32(event, encoder);
92
93 int32_t secure = -1;
94 if (item->getInt32("android.media.mediacodec.secure", &secure)) {
95 metrics_proto.set_secure(secure);
96 }
97 AStatsEvent_writeInt32(event, secure);
98
99 int32_t width = -1;
100 if (item->getInt32("android.media.mediacodec.width", &width)) {
101 metrics_proto.set_width(width);
102 }
103 AStatsEvent_writeInt32(event, width);
104
105 int32_t height = -1;
106 if (item->getInt32("android.media.mediacodec.height", &height)) {
107 metrics_proto.set_height(height);
108 }
109 AStatsEvent_writeInt32(event, height);
110
111 int32_t rotation = -1;
112 if (item->getInt32("android.media.mediacodec.rotation-degrees", &rotation)) {
113 metrics_proto.set_rotation(rotation);
114 }
115 AStatsEvent_writeInt32(event, rotation);
116
117 int32_t crypto = -1;
118 if (item->getInt32("android.media.mediacodec.crypto", &crypto)) {
119 metrics_proto.set_crypto(crypto);
120 }
121 AStatsEvent_writeInt32(event, crypto);
122
123 int32_t profile = -1;
124 if (item->getInt32("android.media.mediacodec.profile", &profile)) {
125 metrics_proto.set_profile(profile);
126 }
127 AStatsEvent_writeInt32(event, profile);
128
129 int32_t level = -1;
130 if (item->getInt32("android.media.mediacodec.level", &level)) {
131 metrics_proto.set_level(level);
132 }
133 AStatsEvent_writeInt32(event, level);
134
135
136 int32_t max_width = -1;
137 if ( item->getInt32("android.media.mediacodec.maxwidth", &max_width)) {
138 metrics_proto.set_max_width(max_width);
139 }
140 AStatsEvent_writeInt32(event, max_width);
141
142 int32_t max_height = -1;
143 if ( item->getInt32("android.media.mediacodec.maxheight", &max_height)) {
144 metrics_proto.set_max_height(max_height);
145 }
146 AStatsEvent_writeInt32(event, max_height);
147
148 int32_t error_code = -1;
149 if ( item->getInt32("android.media.mediacodec.errcode", &error_code)) {
150 metrics_proto.set_error_code(error_code);
151 }
152 AStatsEvent_writeInt32(event, error_code);
153
154 std::string error_state;
155 if ( item->getString("android.media.mediacodec.errstate", &error_state)) {
156 metrics_proto.set_error_state(error_state);
157 }
158 AStatsEvent_writeString(event, error_state.c_str());
159
160 int64_t latency_max = -1;
161 if (item->getInt64("android.media.mediacodec.latency.max", &latency_max)) {
162 metrics_proto.set_latency_max(latency_max);
163 }
164 AStatsEvent_writeInt64(event, latency_max);
165
166 int64_t latency_min = -1;
167 if (item->getInt64("android.media.mediacodec.latency.min", &latency_min)) {
168 metrics_proto.set_latency_min(latency_min);
169 }
170 AStatsEvent_writeInt64(event, latency_min);
171
172 int64_t latency_avg = -1;
173 if (item->getInt64("android.media.mediacodec.latency.avg", &latency_avg)) {
174 metrics_proto.set_latency_avg(latency_avg);
175 }
176 AStatsEvent_writeInt64(event, latency_avg);
177
178 int64_t latency_count = -1;
179 if (item->getInt64("android.media.mediacodec.latency.n", &latency_count)) {
180 metrics_proto.set_latency_count(latency_count);
181 }
182 AStatsEvent_writeInt64(event, latency_count);
183
184 int64_t latency_unknown = -1;
185 if (item->getInt64("android.media.mediacodec.latency.unknown", &latency_unknown)) {
186 metrics_proto.set_latency_unknown(latency_unknown);
187 }
188 AStatsEvent_writeInt64(event, latency_unknown);
189
190 int32_t queue_secure_input_buffer_error = -1;
191 if (item->getInt32("android.media.mediacodec.queueSecureInputBufferError",
192 &queue_secure_input_buffer_error)) {
193 metrics_proto.set_queue_secure_input_buffer_error(queue_secure_input_buffer_error);
194 }
195 AStatsEvent_writeInt32(event, queue_secure_input_buffer_error);
196
197 int32_t queue_input_buffer_error = -1;
198 if (item->getInt32("android.media.mediacodec.queueInputBufferError",
199 &queue_input_buffer_error)) {
200 metrics_proto.set_queue_input_buffer_error(queue_input_buffer_error);
201 }
202 AStatsEvent_writeInt32(event, queue_input_buffer_error);
203
204 std::string bitrate_mode;
205 if (item->getString("android.media.mediacodec.bitrate_mode", &bitrate_mode)) {
206 metrics_proto.set_bitrate_mode(bitrate_mode);
207 }
208 AStatsEvent_writeString(event, bitrate_mode.c_str());
209
210 int32_t bitrate = -1;
211 if (item->getInt32("android.media.mediacodec.bitrate", &bitrate)) {
212 metrics_proto.set_bitrate(bitrate);
213 }
214 AStatsEvent_writeInt32(event, bitrate);
215
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);
220 }
221 AStatsEvent_writeInt64(event, lifetime_millis);
222
223 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)) {
231 sessionId = mediametrics::stringutils::sanitizeLogSessionId(sessionId);
232 metrics_proto.set_log_session_id(sessionId);
233 }
234 AStatsEvent_writeString(event, codec.c_str());
235
236 int32_t channelCount = -1;
237 if (item->getInt32("android.media.mediacodec.channelCount", &channelCount)) {
238 metrics_proto.set_channel_count(channelCount);
239 }
240 AStatsEvent_writeInt32(event, channelCount);
241
242 int32_t sampleRate = -1;
243 if (item->getInt32("android.media.mediacodec.sampleRate", &sampleRate)) {
244 metrics_proto.set_sample_rate(sampleRate);
245 }
246 AStatsEvent_writeInt32(event, sampleRate);
247
248 // TODO PWG may want these fuzzed up a bit to obscure some precision
249 int64_t bytes = -1;
250 if (item->getInt64("android.media.mediacodec.vencode.bytes", &bytes)) {
251 metrics_proto.set_video_encode_bytes(bytes);
252 }
253 AStatsEvent_writeInt64(event, bytes);
254
255 int64_t frames = -1;
256 if (item->getInt64("android.media.mediacodec.vencode.frames", &frames)) {
257 metrics_proto.set_video_encode_frames(frames);
258 }
259 AStatsEvent_writeInt64(event, frames);
260
261 int64_t inputBytes = -1;
262 if (item->getInt64("android.media.mediacodec.video.input.bytes", &inputBytes)) {
263 metrics_proto.set_video_input_bytes(inputBytes);
264 }
265 AStatsEvent_writeInt64(event, inputBytes);
266
267 int64_t inputFrames = -1;
268 if (item->getInt64("android.media.mediacodec.video.input.frames", &inputFrames)) {
269 metrics_proto.set_video_input_frames(inputFrames);
270 }
271 AStatsEvent_writeInt64(event, inputFrames);
272
273 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
345 int32_t originalBitrate = -1;
346 if (item->getInt32("android.media.mediacodec.original.bitrate", &originalBitrate)) {
347 metrics_proto.set_original_bitrate(originalBitrate);
348 }
349 AStatsEvent_writeInt32(event, originalBitrate);
350
351 int32_t shapingEnhanced = -1;
352 if ( item->getInt32("android.media.mediacodec.shaped", &shapingEnhanced)) {
353 metrics_proto.set_shaping_enhanced(shapingEnhanced);
354 }
355 AStatsEvent_writeInt32(event, shapingEnhanced);
356
357 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 }
361 AStatsEvent_writeInt32(event, qpIMinOri);
362
363 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 }
367 AStatsEvent_writeInt32(event, qpIMaxOri);
368
369 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 }
373 AStatsEvent_writeInt32(event, qpPMinOri);
374
375 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 }
379 AStatsEvent_writeInt32(event, qpPMaxOri);
380
381 int32_t qpBMinOri = -1;
382 if ( item->getInt32("android.media.mediacodec.original-video-qp-b-min", &qpBMinOri)) {
383 metrics_proto.set_original_video_qp_b_min(qpBMinOri);
384 }
385 AStatsEvent_writeInt32(event, qpBMinOri);
386
387 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 }
391 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);
398
399 std::string serialized;
400 if (!metrics_proto.SerializeToString(&serialized)) {
401 ALOGE("Failed to serialize codec metrics");
402 return false;
403 }
404 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);
409
410 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
418
419 << " 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
429
430 << " 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
440
441 << " 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
445 << " original_bitrate:" << originalBitrate
446 << " lifetime_millis:" << lifetime_millis
447 << " playback_duration_seconds:" << playback_duration_sec
448 << " 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
473 << " }";
474 statsdLog->log(android::util::MEDIAMETRICS_CODEC_REPORTED, log.str());
475
476
477 return true;
478 }
479
480 } // namespace android
481