Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 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_nuplayer" |
| 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 Essick | 40e8e5e | 2019-12-05 20:19:40 -0800 | [diff] [blame] | 34 | #include "MediaMetricsService.h" |
Jeffrey Huang | 6adacdb | 2020-11-25 02:49:32 -0800 | [diff] [blame] | 35 | #include "frameworks/proto_logging/stats/enums/stats/mediametrics/mediametrics.pb.h" |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 36 | #include "iface_statsd.h" |
| 37 | |
| 38 | namespace android { |
| 39 | |
| 40 | /* |
| 41 | * handles nuplayer AND nuplayer2 |
| 42 | * checks for the union of what the two players generate |
| 43 | */ |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame^] | 44 | bool statsd_nuplayer(const std::shared_ptr<const mediametrics::Item>& item, |
| 45 | const std::shared_ptr<mediametrics::StatsdLog>& statsdLog) |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 46 | { |
Andy Hung | 3ab1b32 | 2020-05-18 10:47:31 -0700 | [diff] [blame] | 47 | if (item == nullptr) return false; |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 48 | |
| 49 | // these go into the statsd wrapper |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame^] | 50 | const nsecs_t timestamp_nanos = MediaMetricsService::roundTime(item->getTimestamp()); |
| 51 | const std::string package_name = item->getPkgName(); |
| 52 | const int64_t package_version_code = item->getPkgVersionCode(); |
| 53 | const int64_t media_apex_version = 0; |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 54 | |
| 55 | // the rest into our own proto |
| 56 | // |
| 57 | ::android::stats::mediametrics::NuPlayerData metrics_proto; |
| 58 | |
| 59 | // flesh out the protobuf we'll hand off with our data |
| 60 | // |
| 61 | |
| 62 | // differentiate between nuplayer and nuplayer2 |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame^] | 63 | std::string whichPlayer = item->getKey(); |
| 64 | metrics_proto.set_whichplayer(whichPlayer.c_str()); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 65 | |
George Burgess IV | 0d81443 | 2019-10-23 11:32:26 -0700 | [diff] [blame] | 66 | std::string video_mime; |
| 67 | if (item->getString("android.media.mediaplayer.video.mime", &video_mime)) { |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame^] | 68 | metrics_proto.set_video_mime(video_mime); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 69 | } |
George Burgess IV | 0d81443 | 2019-10-23 11:32:26 -0700 | [diff] [blame] | 70 | std::string video_codec; |
| 71 | if (item->getString("android.media.mediaplayer.video.codec", &video_codec)) { |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame^] | 72 | metrics_proto.set_video_codec(video_codec); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | int32_t width = -1; |
| 76 | if (item->getInt32("android.media.mediaplayer.width", &width)) { |
| 77 | metrics_proto.set_width(width); |
| 78 | } |
| 79 | int32_t height = -1; |
| 80 | if (item->getInt32("android.media.mediaplayer.height", &height)) { |
| 81 | metrics_proto.set_height(height); |
| 82 | } |
| 83 | |
| 84 | int64_t frames = -1; |
| 85 | if (item->getInt64("android.media.mediaplayer.frames", &frames)) { |
| 86 | metrics_proto.set_frames(frames); |
| 87 | } |
| 88 | int64_t frames_dropped = -1; |
| 89 | if (item->getInt64("android.media.mediaplayer.dropped", &frames_dropped)) { |
| 90 | metrics_proto.set_frames_dropped(frames_dropped); |
| 91 | } |
| 92 | int64_t frames_dropped_startup = -1; |
| 93 | if (item->getInt64("android.media.mediaplayer.startupdropped", &frames_dropped_startup)) { |
| 94 | metrics_proto.set_frames_dropped_startup(frames_dropped_startup); |
| 95 | } |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame^] | 96 | double framerate = -1.0; |
| 97 | if (item->getDouble("android.media.mediaplayer.fps", &framerate)) { |
| 98 | metrics_proto.set_framerate(framerate); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 99 | } |
| 100 | |
George Burgess IV | 0d81443 | 2019-10-23 11:32:26 -0700 | [diff] [blame] | 101 | std::string audio_mime; |
| 102 | if (item->getString("android.media.mediaplayer.audio.mime", &audio_mime)) { |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame^] | 103 | metrics_proto.set_audio_mime(audio_mime); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 104 | } |
George Burgess IV | 0d81443 | 2019-10-23 11:32:26 -0700 | [diff] [blame] | 105 | std::string audio_codec; |
| 106 | if (item->getString("android.media.mediaplayer.audio.codec", &audio_codec)) { |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame^] | 107 | metrics_proto.set_audio_codec(audio_codec); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 108 | } |
| 109 | |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame^] | 110 | int64_t duration_millis = -1; |
| 111 | if (item->getInt64("android.media.mediaplayer.durationMs", &duration_millis)) { |
| 112 | metrics_proto.set_duration_millis(duration_millis); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 113 | } |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame^] | 114 | int64_t playing_millis = -1; |
| 115 | if (item->getInt64("android.media.mediaplayer.playingMs", &playing_millis)) { |
| 116 | metrics_proto.set_playing_millis(playing_millis); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 117 | } |
| 118 | |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame^] | 119 | int32_t error = -1; |
| 120 | if (item->getInt32("android.media.mediaplayer.err", &error)) { |
| 121 | metrics_proto.set_error(error); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 122 | } |
| 123 | int32_t error_code = -1; |
| 124 | if (item->getInt32("android.media.mediaplayer.errcode", &error_code)) { |
| 125 | metrics_proto.set_error_code(error_code); |
| 126 | } |
George Burgess IV | 0d81443 | 2019-10-23 11:32:26 -0700 | [diff] [blame] | 127 | std::string error_state; |
| 128 | if (item->getString("android.media.mediaplayer.errstate", &error_state)) { |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame^] | 129 | metrics_proto.set_error_state(error_state); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 130 | } |
| 131 | |
George Burgess IV | 0d81443 | 2019-10-23 11:32:26 -0700 | [diff] [blame] | 132 | std::string data_source_type; |
| 133 | if (item->getString("android.media.mediaplayer.dataSource", &data_source_type)) { |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame^] | 134 | metrics_proto.set_data_source_type(data_source_type); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 135 | } |
| 136 | |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame^] | 137 | int64_t rebuffering_millis = -1; |
| 138 | if (item->getInt64("android.media.mediaplayer.rebufferingMs", &rebuffering_millis)) { |
| 139 | metrics_proto.set_rebuffering_millis(rebuffering_millis); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 140 | } |
| 141 | int32_t rebuffers = -1; |
| 142 | if (item->getInt32("android.media.mediaplayer.rebuffers", &rebuffers)) { |
| 143 | metrics_proto.set_rebuffers(rebuffers); |
| 144 | } |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame^] | 145 | int32_t rebuffer_at_exit = -1; |
| 146 | if (item->getInt32("android.media.mediaplayer.rebufferExit", &rebuffer_at_exit)) { |
| 147 | metrics_proto.set_rebuffer_at_exit(rebuffer_at_exit); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 148 | } |
| 149 | |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 150 | std::string serialized; |
| 151 | if (!metrics_proto.SerializeToString(&serialized)) { |
| 152 | ALOGE("Failed to serialize nuplayer metrics"); |
| 153 | return false; |
| 154 | } |
| 155 | |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame^] | 156 | android::util::BytesField bf_serialized( serialized.c_str(), serialized.size()); |
| 157 | int result = android::util::stats_write(android::util::MEDIAMETRICS_NUPLAYER_REPORTED, |
| 158 | timestamp_nanos, package_name.c_str(), package_version_code, |
| 159 | media_apex_version, |
| 160 | bf_serialized); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 161 | |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame^] | 162 | std::stringstream log; |
| 163 | log << "result:" << result << " {" |
| 164 | << " mediametrics_nuplayer_reported:" |
| 165 | << android::util::MEDIAMETRICS_NUPLAYER_REPORTED |
| 166 | << " timestamp_nanos:" << timestamp_nanos |
| 167 | << " package_name:" << package_name |
| 168 | << " package_version_code:" << package_version_code |
| 169 | << " media_apex_version:" << media_apex_version |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 170 | |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame^] | 171 | << " whichPlayer:" << whichPlayer |
| 172 | << " video_mime:" << video_mime |
| 173 | << " video_codec:" << video_codec |
| 174 | << " width:" << width |
| 175 | << " height:" << height |
| 176 | << " frames:" << frames |
| 177 | << " frames_dropped:" << frames_dropped |
| 178 | << " framerate:" << framerate |
| 179 | << " audio_mime:" << audio_mime |
| 180 | << " audio_codec:" << media_apex_version |
| 181 | |
| 182 | << " duration_millis:" << duration_millis |
| 183 | << " playing_millis:" << playing_millis |
| 184 | << " error:" << error |
| 185 | << " error_code:" << error_code |
| 186 | << " error_state:" << error_state |
| 187 | << " data_source_type:" << data_source_type |
| 188 | << " rebuffering_millis:" << rebuffering_millis |
| 189 | << " rebuffers:" << rebuffers |
| 190 | << " rebuffer_at_exit:" << rebuffer_at_exit |
| 191 | << " frames_dropped_startup:" << frames_dropped_startup |
| 192 | |
| 193 | // TODO NuPlayer - add log_session_id |
| 194 | // << " log_session_id:" << log_session_id |
| 195 | << " }"; |
| 196 | statsdLog->log(android::util::MEDIAMETRICS_NUPLAYER_REPORTED, log.str()); |
Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 197 | return true; |
| 198 | } |
| 199 | |
Andy Hung | 3ab1b32 | 2020-05-18 10:47:31 -0700 | [diff] [blame] | 200 | } // namespace android |