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_audiopolicy"
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 "frameworks/proto_logging/stats/message/mediametrics_message.pb.h"
36 #include "iface_statsd.h"
37 
38 namespace android {
39 
statsd_audiopolicy(const std::shared_ptr<const mediametrics::Item> & item,const std::shared_ptr<mediametrics::StatsdLog> & statsdLog)40 bool statsd_audiopolicy(const std::shared_ptr<const mediametrics::Item>& item,
41        const std::shared_ptr<mediametrics::StatsdLog>& statsdLog)
42 {
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::AudioPolicyData metrics_proto;
54 
55     // flesh out the protobuf we'll hand off with our data
56     //
57     //int32 char kAudioPolicyStatus[] = "android.media.audiopolicy.status";
58     int32_t status = -1;
59     if (item->getInt32("android.media.audiopolicy.status", &status)) {
60         metrics_proto.set_status(status);
61     }
62     //string char kAudioPolicyRqstSrc[] = "android.media.audiopolicy.rqst.src";
63     std::string request_source;
64     if (item->getString("android.media.audiopolicy.rqst.src", &request_source)) {
65         metrics_proto.set_request_source(request_source);
66     }
67     //string char kAudioPolicyRqstPkg[] = "android.media.audiopolicy.rqst.pkg";
68     std::string request_package;
69     if (item->getString("android.media.audiopolicy.rqst.pkg", &request_package)) {
70         metrics_proto.set_request_package(request_package);
71     }
72     //int32 char kAudioPolicyRqstSession[] = "android.media.audiopolicy.rqst.session";
73     int32_t request_session = -1;
74     if (item->getInt32("android.media.audiopolicy.rqst.session", &request_session)) {
75         metrics_proto.set_request_session(request_session);
76     }
77     //string char kAudioPolicyRqstDevice[] = "android.media.audiopolicy.rqst.device";
78     std::string request_device;
79     if (item->getString("android.media.audiopolicy.rqst.device", &request_device)) {
80         metrics_proto.set_request_device(request_device);
81     }
82 
83     //string char kAudioPolicyActiveSrc[] = "android.media.audiopolicy.active.src";
84     std::string active_source;
85     if (item->getString("android.media.audiopolicy.active.src", &active_source)) {
86         metrics_proto.set_active_source(active_source);
87     }
88     //string char kAudioPolicyActivePkg[] = "android.media.audiopolicy.active.pkg";
89     std::string active_package;
90     if (item->getString("android.media.audiopolicy.active.pkg", &active_package)) {
91         metrics_proto.set_active_package(active_package);
92     }
93     //int32 char kAudioPolicyActiveSession[] = "android.media.audiopolicy.active.session";
94     int32_t active_session = -1;
95     if (item->getInt32("android.media.audiopolicy.active.session", &active_session)) {
96         metrics_proto.set_active_session(active_session);
97     }
98     //string char kAudioPolicyActiveDevice[] = "android.media.audiopolicy.active.device";
99     std::string active_device;
100     if (item->getString("android.media.audiopolicy.active.device", &active_device)) {
101         metrics_proto.set_active_device(active_device);
102     }
103 
104     std::string serialized;
105     if (!metrics_proto.SerializeToString(&serialized)) {
106         ALOGE("Failed to serialize audipolicy metrics");
107         return false;
108     }
109 
110     const stats::media_metrics::BytesField bf_serialized( serialized.c_str(), serialized.size());
111     const int result = stats::media_metrics::stats_write(
112         stats::media_metrics::MEDIAMETRICS_AUDIOPOLICY_REPORTED,
113         timestamp_nanos, package_name.c_str(), package_version_code,
114         media_apex_version,
115         bf_serialized);
116     std::stringstream log;
117     log << "result:" << result << " {"
118             << " mediametrics_audiopolicy_reported:"
119             << stats::media_metrics::MEDIAMETRICS_AUDIOPOLICY_REPORTED
120             << " timestamp_nanos:" << timestamp_nanos
121             << " package_name:" << package_name
122             << " package_version_code:" << package_version_code
123             << " media_apex_version:" << media_apex_version
124 
125             << " status:" << status
126             << " request_source:" << request_source
127             << " request_package:" << request_package
128             << " request_session:" << request_session
129             << " request_device:" << request_device
130             << " active_source:" << active_source
131             << " active_package:" << active_package
132             << " active_session:" << active_session
133             << " active_device:" << active_device
134             << " }";
135     statsdLog->log(stats::media_metrics::MEDIAMETRICS_AUDIOPOLICY_REPORTED, log.str());
136     return true;
137 }
138 
139 } // namespace android
140