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 <stats_media_metrics.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         item->getInt32("android.media.audiorecord.lastError.code", &error_code)) {
104         metrics_proto.set_error_code(error_code);
105     }
106 
107     std::string error_function;
108     if (item->getString("android.media.audiorecord.errfunc", &error_function) ||
109         item->getString("android.media.audiorecord.lastError.at", &error_function)) {
110         metrics_proto.set_error_function(error_function);
111     }
112 
113     int32_t port_id = -1;
114     if (item->getInt32("android.media.audiorecord.portId", &port_id)) {
115         metrics_proto.set_port_id(count);
116     }
117 
118     int32_t frame_count = -1;
119     if (item->getInt32("android.media.audiorecord.frameCount", &frame_count)) {
120         metrics_proto.set_frame_count(frame_count);
121     }
122 
123     std::string attributes;
124     if (item->getString("android.media.audiorecord.attributes", &attributes)) {
125         metrics_proto.set_attributes(attributes);
126     }
127 
128     int64_t channel_mask = -1;
129     if (item->getInt64("android.media.audiorecord.channelMask", &channel_mask)) {
130         metrics_proto.set_channel_mask(channel_mask);
131     }
132 
133     int64_t start_count = -1;
134     if (item->getInt64("android.media.audiorecord.startCount", &start_count)) {
135         metrics_proto.set_start_count(start_count);
136     }
137 
138     std::string serialized;
139     if (!metrics_proto.SerializeToString(&serialized)) {
140         ALOGE("Failed to serialize audiorecord metrics");
141         return false;
142     }
143 
144     // Android S
145     // log_session_id (string)
146     std::string logSessionId;
147     (void)item->getString("android.media.audiorecord.logSessionId", &logSessionId);
148     const auto log_session_id = mediametrics::ValidateId::get()->validateId(logSessionId);
149 
150     const stats::media_metrics::BytesField bf_serialized( serialized.c_str(), serialized.size());
151     const int result = stats::media_metrics::stats_write(
152         stats::media_metrics::MEDIAMETRICS_AUDIORECORD_REPORTED,
153         timestamp_nanos, package_name.c_str(), package_version_code,
154         media_apex_version,
155         bf_serialized,
156         log_session_id.c_str());
157     std::stringstream log;
158     log << "result:" << result << " {"
159             << " mediametrics_audiorecord_reported:"
160             << stats::media_metrics::MEDIAMETRICS_AUDIORECORD_REPORTED
161             << " timestamp_nanos:" << timestamp_nanos
162             << " package_name:" << package_name
163             << " package_version_code:" << package_version_code
164             << " media_apex_version:" << media_apex_version
165 
166             << " encoding:" << encoding
167             << " source:" << source
168             << " latency:" << latency
169             << " samplerate:" << samplerate
170             << " channels:" << channels
171             << " created_millis:" << created_millis
172             << " duration_millis:" << duration_millis
173             << " count:" << count
174             << " error_code:" << error_code
175             << " error_function:" << error_function
176 
177             << " port_id:" << port_id
178             << " frame_count:" << frame_count
179             << " attributes:" << attributes
180             << " channel_mask:" << channel_mask
181             << " start_count:" << start_count
182 
183             << " log_session_id:" << log_session_id
184             << " }";
185     statsdLog->log(stats::media_metrics::MEDIAMETRICS_AUDIORECORD_REPORTED, log.str());
186     return true;
187 }
188 
189 } // namespace android
190