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_audiorecord"
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 "ValidateId.h"
36 #include "frameworks/proto_logging/stats/message/mediametrics_message.pb.h"
37 #include "iface_statsd.h"
38
39 namespace android {
40
statsd_audiorecord(const std::shared_ptr<const mediametrics::Item> & item,const std::shared_ptr<mediametrics::StatsdLog> & statsdLog)41 bool statsd_audiorecord(const std::shared_ptr<const mediametrics::Item>& item,
42 const std::shared_ptr<mediametrics::StatsdLog>& statsdLog) {
43 if (item == nullptr) return false;
44
45 // these go into the statsd wrapper
46 const nsecs_t timestamp_nanos = MediaMetricsService::roundTime(item->getTimestamp());
47 const std::string package_name = item->getPkgName();
48 const int64_t package_version_code = item->getPkgVersionCode();
49 const int64_t media_apex_version = 0;
50
51 // the rest into our own proto
52 //
53 ::android::stats::mediametrics_message::AudioRecordData metrics_proto;
54
55 // flesh out the protobuf we'll hand off with our data
56 //
57 std::string encoding;
58 if (item->getString("android.media.audiorecord.encoding", &encoding)) {
59 metrics_proto.set_encoding(encoding);
60 }
61
62 std::string source;
63 if (item->getString("android.media.audiorecord.source", &source)) {
64 metrics_proto.set_source(source);
65 }
66
67 int32_t latency = -1;
68 if (item->getInt32("android.media.audiorecord.latency", &latency)) {
69 metrics_proto.set_latency(latency);
70 }
71
72 int32_t samplerate = -1;
73 if (item->getInt32("android.media.audiorecord.samplerate", &samplerate)) {
74 metrics_proto.set_samplerate(samplerate);
75 }
76
77 int32_t channels = -1;
78 if (item->getInt32("android.media.audiorecord.channels", &channels)) {
79 metrics_proto.set_channels(channels);
80 }
81
82 int64_t created_millis = -1;
83 // not currently sent from client.
84 if (item->getInt64("android.media.audiorecord.createdMs", &created_millis)) {
85 metrics_proto.set_created_millis(created_millis);
86 }
87
88 int64_t duration_millis = -1;
89 double durationMs = 0.;
90 if (item->getDouble("android.media.audiorecord.durationMs", &durationMs)) {
91 duration_millis = (int64_t)durationMs;
92 metrics_proto.set_duration_millis(duration_millis);
93 }
94
95 int32_t count = -1;
96 // not currently sent from client. (see start count instead).
97 if (item->getInt32("android.media.audiorecord.n", &count)) {
98 metrics_proto.set_count(count);
99 }
100
101 int32_t error_code = -1;
102 if (item->getInt32("android.media.audiorecord.errcode", &error_code)) {
103 metrics_proto.set_error_code(error_code);
104 } else if (item->getInt32("android.media.audiorecord.lastError.code", &error_code)) {
105 metrics_proto.set_error_code(error_code);
106 }
107
108 std::string error_function;
109 if (item->getString("android.media.audiorecord.errfunc", &error_function)) {
110 metrics_proto.set_error_function(error_function);
111 } else if (item->getString("android.media.audiorecord.lastError.at", &error_function)) {
112 metrics_proto.set_error_function(error_function);
113 }
114
115 int32_t port_id = -1;
116 if (item->getInt32("android.media.audiorecord.portId", &port_id)) {
117 metrics_proto.set_port_id(count);
118 }
119
120 int32_t frame_count = -1;
121 if (item->getInt32("android.media.audiorecord.frameCount", &frame_count)) {
122 metrics_proto.set_frame_count(frame_count);
123 }
124
125 std::string attributes;
126 if (item->getString("android.media.audiorecord.attributes", &attributes)) {
127 metrics_proto.set_attributes(attributes);
128 }
129
130 int64_t channel_mask = -1;
131 if (item->getInt64("android.media.audiorecord.channelMask", &channel_mask)) {
132 metrics_proto.set_channel_mask(channel_mask);
133 }
134
135 int64_t start_count = -1;
136 if (item->getInt64("android.media.audiorecord.startCount", &start_count)) {
137 metrics_proto.set_start_count(start_count);
138 }
139
140 std::string serialized;
141 if (!metrics_proto.SerializeToString(&serialized)) {
142 ALOGE("Failed to serialize audiorecord metrics");
143 return false;
144 }
145
146 // Android S
147 // log_session_id (string)
148 std::string logSessionId;
149 (void)item->getString("android.media.audiorecord.logSessionId", &logSessionId);
150 const auto log_session_id = mediametrics::ValidateId::get()->validateId(logSessionId);
151
152 android::util::BytesField bf_serialized( serialized.c_str(), serialized.size());
153 int result = android::util::stats_write(android::util::MEDIAMETRICS_AUDIORECORD_REPORTED,
154 timestamp_nanos, package_name.c_str(), package_version_code,
155 media_apex_version,
156 bf_serialized,
157 log_session_id.c_str());
158 std::stringstream log;
159 log << "result:" << result << " {"
160 << " mediametrics_audiorecord_reported:"
161 << android::util::MEDIAMETRICS_AUDIORECORD_REPORTED
162 << " timestamp_nanos:" << timestamp_nanos
163 << " package_name:" << package_name
164 << " package_version_code:" << package_version_code
165 << " media_apex_version:" << media_apex_version
166
167 << " encoding:" << encoding
168 << " source:" << source
169 << " latency:" << latency
170 << " samplerate:" << samplerate
171 << " channels:" << channels
172 << " created_millis:" << created_millis
173 << " duration_millis:" << duration_millis
174 << " count:" << count
175 << " error_code:" << error_code
176 << " error_function:" << error_function
177
178 << " port_id:" << port_id
179 << " frame_count:" << frame_count
180 << " attributes:" << attributes
181 << " channel_mask:" << channel_mask
182 << " start_count:" << start_count
183
184 << " log_session_id:" << log_session_id
185 << " }";
186 statsdLog->log(android::util::MEDIAMETRICS_AUDIORECORD_REPORTED, log.str());
187 return true;
188 }
189
190 } // namespace android
191