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 "MediaAnalyticsService.h"
35 #include "frameworks/base/core/proto/android/stats/mediametrics/mediametrics.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(MediaAnalyticsItem * item)44 bool statsd_nuplayer(MediaAnalyticsItem *item)
45 {
46     if (item == NULL) return false;
47 
48     // these go into the statsd wrapper
49     nsecs_t timestamp = item->getTimestamp();
50     std::string pkgName = item->getPkgName();
51     int64_t pkgVersionCode = item->getPkgVersionCode();
52     int64_t mediaApexVersion = 0;
53 
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
63     metrics_proto.set_whichplayer(item->getKey().c_str());
64 
65     std::string video_mime;
66     if (item->getString("android.media.mediaplayer.video.mime", &video_mime)) {
67         metrics_proto.set_video_mime(std::move(video_mime));
68     }
69     std::string video_codec;
70     if (item->getString("android.media.mediaplayer.video.codec", &video_codec)) {
71         metrics_proto.set_video_codec(std::move(video_codec));
72     }
73 
74     int32_t width = -1;
75     if (item->getInt32("android.media.mediaplayer.width", &width)) {
76         metrics_proto.set_width(width);
77     }
78     int32_t height = -1;
79     if (item->getInt32("android.media.mediaplayer.height", &height)) {
80         metrics_proto.set_height(height);
81     }
82 
83     int64_t frames = -1;
84     if (item->getInt64("android.media.mediaplayer.frames", &frames)) {
85         metrics_proto.set_frames(frames);
86     }
87     int64_t frames_dropped = -1;
88     if (item->getInt64("android.media.mediaplayer.dropped", &frames_dropped)) {
89         metrics_proto.set_frames_dropped(frames_dropped);
90     }
91     int64_t frames_dropped_startup = -1;
92     if (item->getInt64("android.media.mediaplayer.startupdropped", &frames_dropped_startup)) {
93         metrics_proto.set_frames_dropped_startup(frames_dropped_startup);
94     }
95     double fps = -1.0;
96     if (item->getDouble("android.media.mediaplayer.fps", &fps)) {
97         metrics_proto.set_framerate(fps);
98     }
99 
100     std::string audio_mime;
101     if (item->getString("android.media.mediaplayer.audio.mime", &audio_mime)) {
102         metrics_proto.set_audio_mime(std::move(audio_mime));
103     }
104     std::string audio_codec;
105     if (item->getString("android.media.mediaplayer.audio.codec", &audio_codec)) {
106         metrics_proto.set_audio_codec(std::move(audio_codec));
107     }
108 
109     int64_t duration_ms = -1;
110     if (item->getInt64("android.media.mediaplayer.durationMs", &duration_ms)) {
111         metrics_proto.set_duration_millis(duration_ms);
112     }
113     int64_t playing_ms = -1;
114     if (item->getInt64("android.media.mediaplayer.playingMs", &playing_ms)) {
115         metrics_proto.set_playing_millis(playing_ms);
116     }
117 
118     int32_t err = -1;
119     if (item->getInt32("android.media.mediaplayer.err", &err)) {
120         metrics_proto.set_error(err);
121     }
122     int32_t error_code = -1;
123     if (item->getInt32("android.media.mediaplayer.errcode", &error_code)) {
124         metrics_proto.set_error_code(error_code);
125     }
126     std::string error_state;
127     if (item->getString("android.media.mediaplayer.errstate", &error_state)) {
128         metrics_proto.set_error_state(std::move(error_state));
129     }
130 
131     std::string data_source_type;
132     if (item->getString("android.media.mediaplayer.dataSource", &data_source_type)) {
133         metrics_proto.set_data_source_type(std::move(data_source_type));
134     }
135 
136     int64_t rebufferingMs = -1;
137     if (item->getInt64("android.media.mediaplayer.rebufferingMs", &rebufferingMs)) {
138         metrics_proto.set_rebuffering_millis(rebufferingMs);
139     }
140     int32_t rebuffers = -1;
141     if (item->getInt32("android.media.mediaplayer.rebuffers", &rebuffers)) {
142         metrics_proto.set_rebuffers(rebuffers);
143     }
144     int32_t rebufferExit = -1;
145     if (item->getInt32("android.media.mediaplayer.rebufferExit", &rebufferExit)) {
146         metrics_proto.set_rebuffer_at_exit(rebufferExit);
147     }
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     if (enabled_statsd) {
157         android::util::BytesField bf_serialized( serialized.c_str(), serialized.size());
158         (void)android::util::stats_write(android::util::MEDIAMETRICS_NUPLAYER_REPORTED,
159                                    timestamp, pkgName.c_str(), pkgVersionCode,
160                                    mediaApexVersion,
161                                    bf_serialized);
162 
163     } else {
164         ALOGV("NOT sending: private data (len=%zu)", strlen(serialized.c_str()));
165     }
166 
167     return true;
168 }
169 
170 };
171