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_recorder"
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 
statsd_recorder(MediaAnalyticsItem * item)40 bool statsd_recorder(MediaAnalyticsItem *item)
41 {
42     if (item == NULL) return false;
43 
44     // these go into the statsd wrapper
45     nsecs_t timestamp = item->getTimestamp();
46     std::string pkgName = item->getPkgName();
47     int64_t pkgVersionCode = item->getPkgVersionCode();
48     int64_t mediaApexVersion = 0;
49 
50 
51     // the rest into our own proto
52     //
53     ::android::stats::mediametrics::RecorderData metrics_proto;
54 
55     // flesh out the protobuf we'll hand off with our data
56     //
57 
58     // string kRecorderAudioMime = "android.media.mediarecorder.audio.mime";
59     std::string audio_mime;
60     if (item->getString("android.media.mediarecorder.audio.mime", &audio_mime)) {
61         metrics_proto.set_audio_mime(std::move(audio_mime));
62     }
63     // string kRecorderVideoMime = "android.media.mediarecorder.video.mime";
64     std::string video_mime;
65     if (item->getString("android.media.mediarecorder.video.mime", &video_mime)) {
66         metrics_proto.set_video_mime(std::move(video_mime));
67     }
68     // int32 kRecorderVideoProfile = "android.media.mediarecorder.video-encoder-profile";
69     int32_t videoProfile = -1;
70     if (item->getInt32("android.media.mediarecorder.video-encoder-profile", &videoProfile)) {
71         metrics_proto.set_video_profile(videoProfile);
72     }
73     // int32 kRecorderVideoLevel = "android.media.mediarecorder.video-encoder-level";
74     int32_t videoLevel = -1;
75     if (item->getInt32("android.media.mediarecorder.video-encoder-level", &videoLevel)) {
76         metrics_proto.set_video_level(videoLevel);
77     }
78     // int32 kRecorderWidth = "android.media.mediarecorder.width";
79     int32_t width = -1;
80     if (item->getInt32("android.media.mediarecorder.width", &width)) {
81         metrics_proto.set_width(width);
82     }
83     // int32 kRecorderHeight = "android.media.mediarecorder.height";
84     int32_t height = -1;
85     if (item->getInt32("android.media.mediarecorder.height", &height)) {
86         metrics_proto.set_height(height);
87     }
88     // int32 kRecorderRotation = "android.media.mediarecorder.rotation";
89     int32_t rotation = -1;                      // default to 0?
90     if (item->getInt32("android.media.mediarecorder.rotation", &rotation)) {
91         metrics_proto.set_rotation(rotation);
92     }
93     // int32 kRecorderFrameRate = "android.media.mediarecorder.frame-rate";
94     int32_t framerate = -1;
95     if (item->getInt32("android.media.mediarecorder.frame-rate", &framerate)) {
96         metrics_proto.set_framerate(framerate);
97     }
98 
99     // int32 kRecorderCaptureFps = "android.media.mediarecorder.capture-fps";
100     int32_t captureFps = -1;
101     if (item->getInt32("android.media.mediarecorder.capture-fps", &captureFps)) {
102         metrics_proto.set_capture_fps(captureFps);
103     }
104     // double kRecorderCaptureFpsEnable = "android.media.mediarecorder.capture-fpsenable";
105     double captureFpsEnable = -1;
106     if (item->getDouble("android.media.mediarecorder.capture-fpsenable", &captureFpsEnable)) {
107         metrics_proto.set_capture_fps_enable(captureFpsEnable);
108     }
109 
110     // int64 kRecorderDurationMs = "android.media.mediarecorder.durationMs";
111     int64_t durationMs = -1;
112     if (item->getInt64("android.media.mediarecorder.durationMs", &durationMs)) {
113         metrics_proto.set_duration_millis(durationMs);
114     }
115     // int64 kRecorderPaused = "android.media.mediarecorder.pausedMs";
116     int64_t pausedMs = -1;
117     if (item->getInt64("android.media.mediarecorder.pausedMs", &pausedMs)) {
118         metrics_proto.set_paused_millis(pausedMs);
119     }
120     // int32 kRecorderNumPauses = "android.media.mediarecorder.NPauses";
121     int32_t pausedCount = -1;
122     if (item->getInt32("android.media.mediarecorder.NPauses", &pausedCount)) {
123         metrics_proto.set_paused_count(pausedCount);
124     }
125 
126     // int32 kRecorderAudioBitrate = "android.media.mediarecorder.audio-bitrate";
127     int32_t audioBitrate = -1;
128     if (item->getInt32("android.media.mediarecorder.audio-bitrate", &audioBitrate)) {
129         metrics_proto.set_audio_bitrate(audioBitrate);
130     }
131     // int32 kRecorderAudioChannels = "android.media.mediarecorder.audio-channels";
132     int32_t audioChannels = -1;
133     if (item->getInt32("android.media.mediarecorder.audio-channels", &audioChannels)) {
134         metrics_proto.set_audio_channels(audioChannels);
135     }
136     // int32 kRecorderAudioSampleRate = "android.media.mediarecorder.audio-samplerate";
137     int32_t audioSampleRate = -1;
138     if (item->getInt32("android.media.mediarecorder.audio-samplerate", &audioSampleRate)) {
139         metrics_proto.set_audio_samplerate(audioSampleRate);
140     }
141 
142     // int32 kRecorderMovieTimescale = "android.media.mediarecorder.movie-timescale";
143     int32_t movieTimescale = -1;
144     if (item->getInt32("android.media.mediarecorder.movie-timescale", &movieTimescale)) {
145         metrics_proto.set_movie_timescale(movieTimescale);
146     }
147     // int32 kRecorderAudioTimescale = "android.media.mediarecorder.audio-timescale";
148     int32_t audioTimescale = -1;
149     if (item->getInt32("android.media.mediarecorder.audio-timescale", &audioTimescale)) {
150         metrics_proto.set_audio_timescale(audioTimescale);
151     }
152     // int32 kRecorderVideoTimescale = "android.media.mediarecorder.video-timescale";
153     int32_t videoTimescale = -1;
154     if (item->getInt32("android.media.mediarecorder.video-timescale", &videoTimescale)) {
155         metrics_proto.set_video_timescale(videoTimescale);
156     }
157 
158     // int32 kRecorderVideoBitrate = "android.media.mediarecorder.video-bitrate";
159     int32_t videoBitRate = -1;
160     if (item->getInt32("android.media.mediarecorder.video-bitrate", &videoBitRate)) {
161         metrics_proto.set_video_bitrate(videoBitRate);
162     }
163     // int32 kRecorderVideoIframeInterval = "android.media.mediarecorder.video-iframe-interval";
164     int32_t iFrameInterval = -1;
165     if (item->getInt32("android.media.mediarecorder.video-iframe-interval", &iFrameInterval)) {
166         metrics_proto.set_iframe_interval(iFrameInterval);
167     }
168 
169     std::string serialized;
170     if (!metrics_proto.SerializeToString(&serialized)) {
171         ALOGE("Failed to serialize recorder metrics");
172         return false;
173     }
174 
175     if (enabled_statsd) {
176         android::util::BytesField bf_serialized( serialized.c_str(), serialized.size());
177         (void)android::util::stats_write(android::util::MEDIAMETRICS_RECORDER_REPORTED,
178                                    timestamp, pkgName.c_str(), pkgVersionCode,
179                                    mediaApexVersion,
180                                    bf_serialized);
181 
182     } else {
183         ALOGV("NOT sending: private data (len=%zu)", strlen(serialized.c_str()));
184     }
185 
186     return true;
187 }
188 
189 };
190