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
34 #include "MediaMetricsService.h"
35 #include "frameworks/proto_logging/stats/message/mediametrics_message.pb.h"
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 */
statsd_nuplayer(const std::shared_ptr<const mediametrics::Item> & item,const std::shared_ptr<mediametrics::StatsdLog> & statsdLog)44 bool statsd_nuplayer(const std::shared_ptr<const mediametrics::Item>& item,
45 const std::shared_ptr<mediametrics::StatsdLog>& statsdLog)
46 {
47 if (item == nullptr) return false;
48
49 // these go into the statsd wrapper
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;
54
55 // the rest into our own proto
56 //
57 ::android::stats::mediametrics_message::NuPlayerData metrics_proto;
58
59 // flesh out the protobuf we'll hand off with our data
60 //
61
62 // differentiate between nuplayer and nuplayer2
63 std::string whichPlayer = item->getKey();
64 metrics_proto.set_whichplayer(whichPlayer.c_str());
65
66 std::string video_mime;
67 if (item->getString("android.media.mediaplayer.video.mime", &video_mime)) {
68 metrics_proto.set_video_mime(video_mime);
69 }
70 std::string video_codec;
71 if (item->getString("android.media.mediaplayer.video.codec", &video_codec)) {
72 metrics_proto.set_video_codec(video_codec);
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 }
96 double framerate = -1.0;
97 if (item->getDouble("android.media.mediaplayer.fps", &framerate)) {
98 metrics_proto.set_framerate(framerate);
99 }
100
101 std::string audio_mime;
102 if (item->getString("android.media.mediaplayer.audio.mime", &audio_mime)) {
103 metrics_proto.set_audio_mime(audio_mime);
104 }
105 std::string audio_codec;
106 if (item->getString("android.media.mediaplayer.audio.codec", &audio_codec)) {
107 metrics_proto.set_audio_codec(audio_codec);
108 }
109
110 int64_t duration_millis = -1;
111 if (item->getInt64("android.media.mediaplayer.durationMs", &duration_millis)) {
112 metrics_proto.set_duration_millis(duration_millis);
113 }
114 int64_t playing_millis = -1;
115 if (item->getInt64("android.media.mediaplayer.playingMs", &playing_millis)) {
116 metrics_proto.set_playing_millis(playing_millis);
117 }
118
119 int32_t error = -1;
120 if (item->getInt32("android.media.mediaplayer.err", &error)) {
121 metrics_proto.set_error(error);
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 }
127 std::string error_state;
128 if (item->getString("android.media.mediaplayer.errstate", &error_state)) {
129 metrics_proto.set_error_state(error_state);
130 }
131
132 std::string data_source_type;
133 if (item->getString("android.media.mediaplayer.dataSource", &data_source_type)) {
134 metrics_proto.set_data_source_type(data_source_type);
135 }
136
137 int64_t rebuffering_millis = -1;
138 if (item->getInt64("android.media.mediaplayer.rebufferingMs", &rebuffering_millis)) {
139 metrics_proto.set_rebuffering_millis(rebuffering_millis);
140 }
141 int32_t rebuffers = -1;
142 if (item->getInt32("android.media.mediaplayer.rebuffers", &rebuffers)) {
143 metrics_proto.set_rebuffers(rebuffers);
144 }
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);
148 }
149
150 std::string serialized;
151 if (!metrics_proto.SerializeToString(&serialized)) {
152 ALOGE("Failed to serialize nuplayer metrics");
153 return false;
154 }
155
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);
161
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
170
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());
197 return true;
198 }
199
200 } // namespace android
201