statsd_codec.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. //#define LOG_NDEBUG 0
  17. #define LOG_TAG "statsd_codec"
  18. #include <utils/Log.h>
  19. #include <dirent.h>
  20. #include <inttypes.h>
  21. #include <pthread.h>
  22. #include <pwd.h>
  23. #include <stdint.h>
  24. #include <string.h>
  25. #include <sys/stat.h>
  26. #include <sys/time.h>
  27. #include <sys/types.h>
  28. #include <unistd.h>
  29. #include <statslog.h>
  30. #include "MediaAnalyticsService.h"
  31. #include "frameworks/base/core/proto/android/stats/mediametrics/mediametrics.pb.h"
  32. #include "iface_statsd.h"
  33. namespace android {
  34. bool statsd_codec(MediaAnalyticsItem *item)
  35. {
  36. if (item == NULL) return false;
  37. // these go into the statsd wrapper
  38. nsecs_t timestamp = item->getTimestamp();
  39. std::string pkgName = item->getPkgName();
  40. int64_t pkgVersionCode = item->getPkgVersionCode();
  41. int64_t mediaApexVersion = 0;
  42. // the rest into our own proto
  43. //
  44. ::android::stats::mediametrics::CodecData metrics_proto;
  45. // flesh out the protobuf we'll hand off with our data
  46. //
  47. // android.media.mediacodec.codec string
  48. char *codec = NULL;
  49. if (item->getCString("android.media.mediacodec.codec", &codec)) {
  50. metrics_proto.set_codec(codec);
  51. }
  52. // android.media.mediacodec.mime string
  53. char *mime = NULL;
  54. if (item->getCString("android.media.mediacodec.mime", &mime)) {
  55. metrics_proto.set_mime(mime);
  56. }
  57. // android.media.mediacodec.mode string
  58. char *mode = NULL;
  59. if ( item->getCString("android.media.mediacodec.mode", &mode)) {
  60. metrics_proto.set_mode(mode);
  61. }
  62. // android.media.mediacodec.encoder int32
  63. int32_t encoder = -1;
  64. if ( item->getInt32("android.media.mediacodec.encoder", &encoder)) {
  65. metrics_proto.set_encoder(encoder);
  66. }
  67. // android.media.mediacodec.secure int32
  68. int32_t secure = -1;
  69. if ( item->getInt32("android.media.mediacodec.secure", &secure)) {
  70. metrics_proto.set_secure(secure);
  71. }
  72. // android.media.mediacodec.width int32
  73. int32_t width = -1;
  74. if ( item->getInt32("android.media.mediacodec.width", &width)) {
  75. metrics_proto.set_width(width);
  76. }
  77. // android.media.mediacodec.height int32
  78. int32_t height = -1;
  79. if ( item->getInt32("android.media.mediacodec.height", &height)) {
  80. metrics_proto.set_height(height);
  81. }
  82. // android.media.mediacodec.rotation-degrees int32
  83. int32_t rotation = -1;
  84. if ( item->getInt32("android.media.mediacodec.rotation-degrees", &rotation)) {
  85. metrics_proto.set_rotation(rotation);
  86. }
  87. // android.media.mediacodec.crypto int32 (although missing if not needed
  88. int32_t crypto = -1;
  89. if ( item->getInt32("android.media.mediacodec.crypto", &crypto)) {
  90. metrics_proto.set_crypto(crypto);
  91. }
  92. // android.media.mediacodec.profile int32
  93. int32_t profile = -1;
  94. if ( item->getInt32("android.media.mediacodec.profile", &profile)) {
  95. metrics_proto.set_profile(profile);
  96. }
  97. // android.media.mediacodec.level int32
  98. int32_t level = -1;
  99. if ( item->getInt32("android.media.mediacodec.level", &level)) {
  100. metrics_proto.set_level(level);
  101. }
  102. // android.media.mediacodec.maxwidth int32
  103. int32_t maxwidth = -1;
  104. if ( item->getInt32("android.media.mediacodec.maxwidth", &maxwidth)) {
  105. metrics_proto.set_max_width(maxwidth);
  106. }
  107. // android.media.mediacodec.maxheight int32
  108. int32_t maxheight = -1;
  109. if ( item->getInt32("android.media.mediacodec.maxheight", &maxheight)) {
  110. metrics_proto.set_max_height(maxheight);
  111. }
  112. // android.media.mediacodec.errcode int32
  113. int32_t errcode = -1;
  114. if ( item->getInt32("android.media.mediacodec.errcode", &errcode)) {
  115. metrics_proto.set_error_code(errcode);
  116. }
  117. // android.media.mediacodec.errstate string
  118. char *errstate = NULL;
  119. if ( item->getCString("android.media.mediacodec.errstate", &errstate)) {
  120. metrics_proto.set_error_state(errstate);
  121. }
  122. // android.media.mediacodec.latency.max int64
  123. int64_t latency_max = -1;
  124. if ( item->getInt64("android.media.mediacodec.latency.max", &latency_max)) {
  125. metrics_proto.set_latency_max(latency_max);
  126. }
  127. // android.media.mediacodec.latency.min int64
  128. int64_t latency_min = -1;
  129. if ( item->getInt64("android.media.mediacodec.latency.min", &latency_min)) {
  130. metrics_proto.set_latency_min(latency_min);
  131. }
  132. // android.media.mediacodec.latency.avg int64
  133. int64_t latency_avg = -1;
  134. if ( item->getInt64("android.media.mediacodec.latency.avg", &latency_avg)) {
  135. metrics_proto.set_latency_avg(latency_avg);
  136. }
  137. // android.media.mediacodec.latency.n int64
  138. int64_t latency_count = -1;
  139. if ( item->getInt64("android.media.mediacodec.latency.n", &latency_count)) {
  140. metrics_proto.set_latency_count(latency_count);
  141. }
  142. // android.media.mediacodec.latency.unknown int64
  143. int64_t latency_unknown = -1;
  144. if ( item->getInt64("android.media.mediacodec.latency.unknown", &latency_unknown)) {
  145. metrics_proto.set_latency_unknown(latency_unknown);
  146. }
  147. // android.media.mediacodec.latency.hist NOT EMITTED
  148. std::string serialized;
  149. if (!metrics_proto.SerializeToString(&serialized)) {
  150. ALOGE("Failed to serialize codec metrics");
  151. return false;
  152. }
  153. if (enabled_statsd) {
  154. android::util::BytesField bf_serialized( serialized.c_str(), serialized.size());
  155. (void)android::util::stats_write(android::util::MEDIAMETRICS_CODEC_REPORTED,
  156. timestamp, pkgName.c_str(), pkgVersionCode,
  157. mediaApexVersion,
  158. bf_serialized);
  159. } else {
  160. ALOGV("NOT sending: private data (len=%zu)", strlen(serialized.c_str()));
  161. }
  162. // must free the strings that we were given
  163. free(codec);
  164. free(mime);
  165. free(mode);
  166. free(errstate);
  167. return true;
  168. }
  169. };