• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "MediaMetricsService.h"
35 #include "StringUtils.h"
36 #include "frameworks/proto_logging/stats/message/mediametrics_message.pb.h"
37 #include "iface_statsd.h"
38 
39 namespace android {
40 
statsd_recorder(const std::shared_ptr<const mediametrics::Item> & item,const std::shared_ptr<mediametrics::StatsdLog> & statsdLog)41 bool statsd_recorder(const std::shared_ptr<const mediametrics::Item>& item,
42         const std::shared_ptr<mediametrics::StatsdLog>& statsdLog)
43 {
44     if (item == nullptr) return false;
45 
46     // these go into the statsd wrapper
47     const nsecs_t timestamp_nanos = MediaMetricsService::roundTime(item->getTimestamp());
48     const std::string package_name = item->getPkgName();
49     const int64_t package_version_code = item->getPkgVersionCode();
50     const int64_t media_apex_version = 0;
51 
52     // the rest into our own proto
53     //
54     ::android::stats::mediametrics_message::RecorderData metrics_proto;
55 
56     // flesh out the protobuf we'll hand off with our data
57     //
58 
59     // string kRecorderLogSessionId = "android.media.mediarecorder.log-session-id";
60     std::string log_session_id;
61     if (item->getString("android.media.mediarecorder.log-session-id", &log_session_id)) {
62         log_session_id = mediametrics::stringutils::sanitizeLogSessionId(log_session_id);
63         metrics_proto.set_log_session_id(log_session_id);
64     }
65     // string kRecorderAudioMime = "android.media.mediarecorder.audio.mime";
66     std::string audio_mime;
67     if (item->getString("android.media.mediarecorder.audio.mime", &audio_mime)) {
68         metrics_proto.set_audio_mime(audio_mime);
69     }
70     // string kRecorderVideoMime = "android.media.mediarecorder.video.mime";
71     std::string video_mime;
72     if (item->getString("android.media.mediarecorder.video.mime", &video_mime)) {
73         metrics_proto.set_video_mime(video_mime);
74     }
75     // int32 kRecorderVideoProfile = "android.media.mediarecorder.video-encoder-profile";
76     int32_t video_profile = -1;
77     if (item->getInt32("android.media.mediarecorder.video-encoder-profile", &video_profile)) {
78         metrics_proto.set_video_profile(video_profile);
79     }
80     // int32 kRecorderVideoLevel = "android.media.mediarecorder.video-encoder-level";
81     int32_t video_level = -1;
82     if (item->getInt32("android.media.mediarecorder.video-encoder-level", &video_level)) {
83         metrics_proto.set_video_level(video_level);
84     }
85     // int32 kRecorderWidth = "android.media.mediarecorder.width";
86     int32_t width = -1;
87     if (item->getInt32("android.media.mediarecorder.width", &width)) {
88         metrics_proto.set_width(width);
89     }
90     // int32 kRecorderHeight = "android.media.mediarecorder.height";
91     int32_t height = -1;
92     if (item->getInt32("android.media.mediarecorder.height", &height)) {
93         metrics_proto.set_height(height);
94     }
95     // int32 kRecorderRotation = "android.media.mediarecorder.rotation";
96     int32_t rotation = -1;                      // default to 0?
97     if (item->getInt32("android.media.mediarecorder.rotation", &rotation)) {
98         metrics_proto.set_rotation(rotation);
99     }
100     // int32 kRecorderFrameRate = "android.media.mediarecorder.frame-rate";
101     int32_t framerate = -1;
102     if (item->getInt32("android.media.mediarecorder.frame-rate", &framerate)) {
103         metrics_proto.set_framerate(framerate);
104     }
105 
106     // int32 kRecorderCaptureFps = "android.media.mediarecorder.capture-fps";
107     int32_t capture_fps = -1;
108     if (item->getInt32("android.media.mediarecorder.capture-fps", &capture_fps)) {
109         metrics_proto.set_capture_fps(capture_fps);
110     }
111     // double kRecorderCaptureFpsEnable = "android.media.mediarecorder.capture-fpsenable";
112     double capture_fps_enable = -1;
113     if (item->getDouble("android.media.mediarecorder.capture-fpsenable", &capture_fps_enable)) {
114         metrics_proto.set_capture_fps_enable(capture_fps_enable);
115     }
116 
117     // int64 kRecorderDurationMs = "android.media.mediarecorder.durationMs";
118     int64_t duration_millis = -1;
119     if (item->getInt64("android.media.mediarecorder.durationMs", &duration_millis)) {
120         metrics_proto.set_duration_millis(duration_millis);
121     }
122     // int64 kRecorderPaused = "android.media.mediarecorder.pausedMs";
123     int64_t paused_millis = -1;
124     if (item->getInt64("android.media.mediarecorder.pausedMs", &paused_millis)) {
125         metrics_proto.set_paused_millis(paused_millis);
126     }
127     // int32 kRecorderNumPauses = "android.media.mediarecorder.NPauses";
128     int32_t paused_count = -1;
129     if (item->getInt32("android.media.mediarecorder.NPauses", &paused_count)) {
130         metrics_proto.set_paused_count(paused_count);
131     }
132 
133     // int32 kRecorderAudioBitrate = "android.media.mediarecorder.audio-bitrate";
134     int32_t audio_bitrate = -1;
135     if (item->getInt32("android.media.mediarecorder.audio-bitrate", &audio_bitrate)) {
136         metrics_proto.set_audio_bitrate(audio_bitrate);
137     }
138     // int32 kRecorderAudioChannels = "android.media.mediarecorder.audio-channels";
139     int32_t audio_channels = -1;
140     if (item->getInt32("android.media.mediarecorder.audio-channels", &audio_channels)) {
141         metrics_proto.set_audio_channels(audio_channels);
142     }
143     // int32 kRecorderAudioSampleRate = "android.media.mediarecorder.audio-samplerate";
144     int32_t audio_samplerate = -1;
145     if (item->getInt32("android.media.mediarecorder.audio-samplerate", &audio_samplerate)) {
146         metrics_proto.set_audio_samplerate(audio_samplerate);
147     }
148 
149     // int32 kRecorderMovieTimescale = "android.media.mediarecorder.movie-timescale";
150     int32_t movie_timescale = -1;
151     if (item->getInt32("android.media.mediarecorder.movie-timescale", &movie_timescale)) {
152         metrics_proto.set_movie_timescale(movie_timescale);
153     }
154     // int32 kRecorderAudioTimescale = "android.media.mediarecorder.audio-timescale";
155     int32_t audio_timescale = -1;
156     if (item->getInt32("android.media.mediarecorder.audio-timescale", &audio_timescale)) {
157         metrics_proto.set_audio_timescale(audio_timescale);
158     }
159     // int32 kRecorderVideoTimescale = "android.media.mediarecorder.video-timescale";
160     int32_t video_timescale = -1;
161     if (item->getInt32("android.media.mediarecorder.video-timescale", &video_timescale)) {
162         metrics_proto.set_video_timescale(video_timescale);
163     }
164 
165     // int32 kRecorderVideoBitrate = "android.media.mediarecorder.video-bitrate";
166     int32_t video_bitrate = -1;
167     if (item->getInt32("android.media.mediarecorder.video-bitrate", &video_bitrate)) {
168         metrics_proto.set_video_bitrate(video_bitrate);
169     }
170     // int32 kRecorderVideoIframeInterval = "android.media.mediarecorder.video-iframe-interval";
171     int32_t iframe_interval = -1;
172     if (item->getInt32("android.media.mediarecorder.video-iframe-interval", &iframe_interval)) {
173         metrics_proto.set_iframe_interval(iframe_interval);
174     }
175 
176     std::string serialized;
177     if (!metrics_proto.SerializeToString(&serialized)) {
178         ALOGE("Failed to serialize recorder metrics");
179         return false;
180     }
181 
182     android::util::BytesField bf_serialized( serialized.c_str(), serialized.size());
183     int result = android::util::stats_write(android::util::MEDIAMETRICS_RECORDER_REPORTED,
184         timestamp_nanos, package_name.c_str(), package_version_code,
185         media_apex_version,
186         bf_serialized);
187     std::stringstream log;
188     log << "result:" << result << " {"
189             << " mediametrics_recorder_reported:"
190             << android::util::MEDIAMETRICS_RECORDER_REPORTED
191             << " timestamp_nanos:" << timestamp_nanos
192             << " package_name:" << package_name
193             << " package_version_code:" << package_version_code
194             << " media_apex_version:" << media_apex_version
195 
196             << " audio_mime:" << audio_mime
197             << " video_mime:" << video_mime
198             << " video_profile:" << video_profile
199             << " video_level:" << video_level
200             << " width:" << width
201             << " height:" << height
202             << " rotation:" << rotation
203             << " framerate:" << framerate
204             << " capture_fps:" << capture_fps
205             << " capture_fps_enable:" << capture_fps_enable
206 
207             << " duration_millis:" << duration_millis
208             << " paused_millis:" << paused_millis
209             << " paused_count:" << paused_count
210             << " audio_bitrate:" << audio_bitrate
211             << " audio_channels:" << audio_channels
212             << " audio_samplerate:" << audio_samplerate
213             << " movie_timescale:" << movie_timescale
214             << " audio_timescale:" << audio_timescale
215             << " video_timescale:" << video_timescale
216             << " video_bitrate:" << video_bitrate
217 
218             << " iframe_interval:" << iframe_interval
219             << " log_session_id:" << log_session_id
220             << " }";
221     statsdLog->log(android::util::MEDIAMETRICS_RECORDER_REPORTED, log.str());
222     return true;
223 }
224 
225 } // namespace android
226