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 "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_audiorecord(MediaAnalyticsItem * item)40 bool statsd_audiorecord(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::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(std::move(encoding));
60 }
61
62 std::string source;
63 if (item->getString("android.media.audiorecord.source", &source)) {
64 metrics_proto.set_source(std::move(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 createdMs = -1;
83 if (item->getInt64("android.media.audiorecord.createdMs", &createdMs)) {
84 metrics_proto.set_created_millis(createdMs);
85 }
86
87 int64_t durationMs = -1;
88 if (item->getInt64("android.media.audiorecord.durationMs", &durationMs)) {
89 metrics_proto.set_duration_millis(durationMs);
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 errcode = -1;
98 if (item->getInt32("android.media.audiorecord.errcode", &errcode)) {
99 metrics_proto.set_error_code(errcode);
100 } else if (item->getInt32("android.media.audiorecord.lastError.code", &errcode)) {
101 metrics_proto.set_error_code(errcode);
102 }
103
104 std::string errfunc;
105 if (item->getString("android.media.audiorecord.errfunc", &errfunc)) {
106 metrics_proto.set_error_function(std::move(errfunc));
107 } else if (item->getString("android.media.audiorecord.lastError.at", &errfunc)) {
108 metrics_proto.set_error_function(std::move(errfunc));
109 }
110
111 // portId (int32)
112 int32_t port_id = -1;
113 if (item->getInt32("android.media.audiorecord.portId", &port_id)) {
114 metrics_proto.set_port_id(count);
115 }
116 // frameCount (int32)
117 int32_t frameCount = -1;
118 if (item->getInt32("android.media.audiorecord.frameCount", &frameCount)) {
119 metrics_proto.set_frame_count(frameCount);
120 }
121 // attributes (string)
122 std::string attributes;
123 if (item->getString("android.media.audiorecord.attributes", &attributes)) {
124 metrics_proto.set_attributes(std::move(attributes));
125 }
126 // channelMask (int64)
127 int64_t channelMask = -1;
128 if (item->getInt64("android.media.audiorecord.channelMask", &channelMask)) {
129 metrics_proto.set_channel_mask(channelMask);
130 }
131 // startcount (int64)
132 int64_t startcount = -1;
133 if (item->getInt64("android.media.audiorecord.startcount", &startcount)) {
134 metrics_proto.set_start_count(startcount);
135 }
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 if (enabled_statsd) {
145 android::util::BytesField bf_serialized( serialized.c_str(), serialized.size());
146 (void)android::util::stats_write(android::util::MEDIAMETRICS_AUDIORECORD_REPORTED,
147 timestamp, pkgName.c_str(), pkgVersionCode,
148 mediaApexVersion,
149 bf_serialized);
150
151 } else {
152 ALOGV("NOT sending: private data (len=%zu)", strlen(serialized.c_str()));
153 }
154
155 return true;
156 }
157
158 };
159