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