statsd_nuplayer.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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_nuplayer"
  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. /*
  35. * handles nuplayer AND nuplayer2
  36. * checks for the union of what the two players generate
  37. */
  38. bool statsd_nuplayer(MediaAnalyticsItem *item)
  39. {
  40. if (item == NULL) return false;
  41. // these go into the statsd wrapper
  42. nsecs_t timestamp = item->getTimestamp();
  43. std::string pkgName = item->getPkgName();
  44. int64_t pkgVersionCode = item->getPkgVersionCode();
  45. int64_t mediaApexVersion = 0;
  46. // the rest into our own proto
  47. //
  48. ::android::stats::mediametrics::NuPlayerData metrics_proto;
  49. // flesh out the protobuf we'll hand off with our data
  50. //
  51. // differentiate between nuplayer and nuplayer2
  52. metrics_proto.set_whichplayer(item->getKey().c_str());
  53. char *video_mime = NULL;
  54. if (item->getCString("android.media.mediaplayer.video.mime", &video_mime)) {
  55. metrics_proto.set_video_mime(video_mime);
  56. }
  57. char *video_codec = NULL;
  58. if (item->getCString("android.media.mediaplayer.video.codec", &video_codec)) {
  59. metrics_proto.set_video_codec(video_codec);
  60. }
  61. int32_t width = -1;
  62. if (item->getInt32("android.media.mediaplayer.width", &width)) {
  63. metrics_proto.set_width(width);
  64. }
  65. int32_t height = -1;
  66. if (item->getInt32("android.media.mediaplayer.height", &height)) {
  67. metrics_proto.set_height(height);
  68. }
  69. int64_t frames = -1;
  70. if (item->getInt64("android.media.mediaplayer.frames", &frames)) {
  71. metrics_proto.set_frames(frames);
  72. }
  73. int64_t frames_dropped = -1;
  74. if (item->getInt64("android.media.mediaplayer.dropped", &frames_dropped)) {
  75. metrics_proto.set_frames_dropped(frames_dropped);
  76. }
  77. int64_t frames_dropped_startup = -1;
  78. if (item->getInt64("android.media.mediaplayer.startupdropped", &frames_dropped_startup)) {
  79. metrics_proto.set_frames_dropped_startup(frames_dropped_startup);
  80. }
  81. double fps = -1.0;
  82. if (item->getDouble("android.media.mediaplayer.fps", &fps)) {
  83. metrics_proto.set_framerate(fps);
  84. }
  85. char *audio_mime = NULL;
  86. if (item->getCString("android.media.mediaplayer.audio.mime", &audio_mime)) {
  87. metrics_proto.set_audio_mime(audio_mime);
  88. }
  89. char *audio_codec = NULL;
  90. if (item->getCString("android.media.mediaplayer.audio.codec", &audio_codec)) {
  91. metrics_proto.set_audio_codec(audio_codec);
  92. }
  93. int64_t duration_ms = -1;
  94. if (item->getInt64("android.media.mediaplayer.durationMs", &duration_ms)) {
  95. metrics_proto.set_duration_millis(duration_ms);
  96. }
  97. int64_t playing_ms = -1;
  98. if (item->getInt64("android.media.mediaplayer.playingMs", &playing_ms)) {
  99. metrics_proto.set_playing_millis(playing_ms);
  100. }
  101. int32_t err = -1;
  102. if (item->getInt32("android.media.mediaplayer.err", &err)) {
  103. metrics_proto.set_error(err);
  104. }
  105. int32_t error_code = -1;
  106. if (item->getInt32("android.media.mediaplayer.errcode", &error_code)) {
  107. metrics_proto.set_error_code(error_code);
  108. }
  109. char *error_state = NULL;
  110. if (item->getCString("android.media.mediaplayer.errstate", &error_state)) {
  111. metrics_proto.set_error_state(error_state);
  112. }
  113. char *data_source_type = NULL;
  114. if (item->getCString("android.media.mediaplayer.dataSource", &data_source_type)) {
  115. metrics_proto.set_data_source_type(data_source_type);
  116. }
  117. int64_t rebufferingMs = -1;
  118. if (item->getInt64("android.media.mediaplayer.rebufferingMs", &rebufferingMs)) {
  119. metrics_proto.set_rebuffering_millis(rebufferingMs);
  120. }
  121. int32_t rebuffers = -1;
  122. if (item->getInt32("android.media.mediaplayer.rebuffers", &rebuffers)) {
  123. metrics_proto.set_rebuffers(rebuffers);
  124. }
  125. int32_t rebufferExit = -1;
  126. if (item->getInt32("android.media.mediaplayer.rebufferExit", &rebufferExit)) {
  127. metrics_proto.set_rebuffer_at_exit(rebufferExit);
  128. }
  129. std::string serialized;
  130. if (!metrics_proto.SerializeToString(&serialized)) {
  131. ALOGE("Failed to serialize nuplayer metrics");
  132. return false;
  133. }
  134. if (enabled_statsd) {
  135. android::util::BytesField bf_serialized( serialized.c_str(), serialized.size());
  136. (void)android::util::stats_write(android::util::MEDIAMETRICS_NUPLAYER_REPORTED,
  137. timestamp, pkgName.c_str(), pkgVersionCode,
  138. mediaApexVersion,
  139. bf_serialized);
  140. } else {
  141. ALOGV("NOT sending: private data (len=%zu)", strlen(serialized.c_str()));
  142. }
  143. // must free the strings that we were given
  144. free(video_mime);
  145. free(video_codec);
  146. free(audio_mime);
  147. free(audio_codec);
  148. free(error_state);
  149. free(data_source_type);
  150. return true;
  151. }
  152. };