statsd_audiopolicy.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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_audiopolicy"
  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_audiopolicy(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::AudioPolicyData metrics_proto;
  45. // flesh out the protobuf we'll hand off with our data
  46. //
  47. //int32 char kAudioPolicyStatus[] = "android.media.audiopolicy.status";
  48. int32_t status = -1;
  49. if (item->getInt32("android.media.audiopolicy.status", &status)) {
  50. metrics_proto.set_status(status);
  51. }
  52. //string char kAudioPolicyRqstSrc[] = "android.media.audiopolicy.rqst.src";
  53. char *rqst_src = NULL;
  54. if (item->getCString("android.media.audiopolicy.rqst.src", &rqst_src)) {
  55. metrics_proto.set_request_source(rqst_src);
  56. }
  57. //string char kAudioPolicyRqstPkg[] = "android.media.audiopolicy.rqst.pkg";
  58. char *rqst_pkg = NULL;
  59. if (item->getCString("android.media.audiopolicy.rqst.pkg", &rqst_pkg)) {
  60. metrics_proto.set_request_package(rqst_pkg);
  61. }
  62. //int32 char kAudioPolicyRqstSession[] = "android.media.audiopolicy.rqst.session";
  63. int32_t rqst_session = -1;
  64. if (item->getInt32("android.media.audiopolicy.rqst.session", &rqst_session)) {
  65. metrics_proto.set_request_session(rqst_session);
  66. }
  67. //string char kAudioPolicyRqstDevice[] = "android.media.audiopolicy.rqst.device";
  68. char *rqst_device = NULL;
  69. if (item->getCString("android.media.audiopolicy.rqst.device", &rqst_device)) {
  70. metrics_proto.set_request_device(rqst_device);
  71. }
  72. //string char kAudioPolicyActiveSrc[] = "android.media.audiopolicy.active.src";
  73. char *active_src = NULL;
  74. if (item->getCString("android.media.audiopolicy.active.src", &active_src)) {
  75. metrics_proto.set_active_source(active_src);
  76. }
  77. //string char kAudioPolicyActivePkg[] = "android.media.audiopolicy.active.pkg";
  78. char *active_pkg = NULL;
  79. if (item->getCString("android.media.audiopolicy.active.pkg", &active_pkg)) {
  80. metrics_proto.set_active_package(active_pkg);
  81. }
  82. //int32 char kAudioPolicyActiveSession[] = "android.media.audiopolicy.active.session";
  83. int32_t active_session = -1;
  84. if (item->getInt32("android.media.audiopolicy.active.session", &active_session)) {
  85. metrics_proto.set_active_session(active_session);
  86. }
  87. //string char kAudioPolicyActiveDevice[] = "android.media.audiopolicy.active.device";
  88. char *active_device = NULL;
  89. if (item->getCString("android.media.audiopolicy.active.device", &active_device)) {
  90. metrics_proto.set_active_device(active_device);
  91. }
  92. std::string serialized;
  93. if (!metrics_proto.SerializeToString(&serialized)) {
  94. ALOGE("Failed to serialize audipolicy metrics");
  95. return false;
  96. }
  97. if (enabled_statsd) {
  98. android::util::BytesField bf_serialized( serialized.c_str(), serialized.size());
  99. (void)android::util::stats_write(android::util::MEDIAMETRICS_AUDIOPOLICY_REPORTED,
  100. timestamp, pkgName.c_str(), pkgVersionCode,
  101. mediaApexVersion,
  102. bf_serialized);
  103. } else {
  104. ALOGV("NOT sending: private data (len=%zu)", strlen(serialized.c_str()));
  105. }
  106. // must free the strings that we were given
  107. free(rqst_src);
  108. free(rqst_pkg);
  109. free(rqst_device);
  110. free(active_src);
  111. free(active_pkg);
  112. free(active_device);
  113. return true;
  114. }
  115. };