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_audiotrack"
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_audiotrack(const std::shared_ptr<const mediametrics::Item> & item,const std::shared_ptr<mediametrics::StatsdLog> & statsdLog)41 bool statsd_audiotrack(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::AudioTrackData metrics_proto;
55
56 // flesh out the protobuf we'll hand off with our data
57 //
58
59 // Do not change this without changing AudioTrack.cpp collection.
60
61 // optional string streamType;
62 std::string stream_type;
63 if (item->getString("android.media.audiotrack.streamtype", &stream_type)) {
64 metrics_proto.set_stream_type(stream_type);
65 }
66
67 // optional string contentType;
68 std::string content_type;
69 if (item->getString("android.media.audiotrack.type", &content_type)) {
70 metrics_proto.set_content_type(content_type);
71 }
72
73 // optional string trackUsage;
74 std::string track_usage;
75 if (item->getString("android.media.audiotrack.usage", &track_usage)) {
76 metrics_proto.set_track_usage(track_usage);
77 }
78
79 // optional int32 sampleRate;
80 int32_t sample_rate = -1;
81 if (item->getInt32("android.media.audiotrack.sampleRate", &sample_rate)) {
82 metrics_proto.set_sample_rate(sample_rate);
83 }
84
85 // optional int64 channelMask;
86 int64_t channel_mask = -1;
87 if (item->getInt64("android.media.audiotrack.channelMask", &channel_mask)) {
88 metrics_proto.set_channel_mask(channel_mask);
89 }
90
91 // optional int32 underrunFrames;
92 int32_t underrun_frames = -1;
93 if (item->getInt32("android.media.audiotrack.underrunFrames", &underrun_frames)) {
94 metrics_proto.set_underrun_frames(underrun_frames);
95 }
96
97 // optional int32 glitch.startup;
98 int32_t startup_glitch = -1;
99 // Not currently sent from client.
100 if (item->getInt32("android.media.audiotrack.glitch.startup", &startup_glitch)) {
101 metrics_proto.set_startup_glitch(startup_glitch);
102 }
103
104 // portId (int32)
105 int32_t port_id = -1;
106 if (item->getInt32("android.media.audiotrack.portId", &port_id)) {
107 metrics_proto.set_port_id(port_id);
108 }
109 // encoding (string)
110 std::string encoding;
111 if (item->getString("android.media.audiotrack.encoding", &encoding)) {
112 metrics_proto.set_encoding(encoding);
113 }
114 // frameCount (int32)
115 int32_t frame_count = -1;
116 if (item->getInt32("android.media.audiotrack.frameCount", &frame_count)) {
117 metrics_proto.set_frame_count(frame_count);
118 }
119 // attributes (string)
120 std::string attributes;
121 if (item->getString("android.media.audiotrack.attributes", &attributes)) {
122 metrics_proto.set_attributes(attributes);
123 }
124
125 std::string serialized;
126 if (!metrics_proto.SerializeToString(&serialized)) {
127 ALOGE("Failed to serialize audiotrack metrics");
128 return false;
129 }
130
131 // Android S
132 // log_session_id (string)
133 std::string logSessionId;
134 (void)item->getString("android.media.audiotrack.logSessionId", &logSessionId);
135 const auto log_session_id = mediametrics::ValidateId::get()->validateId(logSessionId);
136
137 android::util::BytesField bf_serialized( serialized.c_str(), serialized.size());
138 int result = android::util::stats_write(android::util::MEDIAMETRICS_AUDIOTRACK_REPORTED,
139 timestamp_nanos, package_name.c_str(), package_version_code,
140 media_apex_version,
141 bf_serialized,
142 log_session_id.c_str());
143 std::stringstream log;
144 log << "result:" << result << " {"
145 << " mediametrics_audiotrack_reported:"
146 << android::util::MEDIAMETRICS_AUDIOTRACK_REPORTED
147 << " timestamp_nanos:" << timestamp_nanos
148 << " package_name:" << package_name
149 << " package_version_code:" << package_version_code
150 << " media_apex_version:" << media_apex_version
151
152 << " stream_type:" << stream_type
153 << " content_type:" << content_type
154 << " track_usage:" << track_usage
155 << " sample_rate:" << sample_rate
156 << " channel_mask:" << channel_mask
157 << " underrun_frames:" << underrun_frames
158 << " startup_glitch:" << startup_glitch
159 << " port_id:" << port_id
160 << " encoding:" << encoding
161 << " frame_count:" << frame_count
162
163 << " attributes:" << attributes
164
165 << " log_session_id:" << log_session_id
166 << " }";
167 statsdLog->log(android::util::MEDIAMETRICS_AUDIOTRACK_REPORTED, log.str());
168 return true;
169 }
170
171 } // namespace android
172