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_drm"
19 #include <utils/Log.h>
20 
21 #include <stdint.h>
22 #include <inttypes.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <sys/time.h>
26 #include <dirent.h>
27 #include <pthread.h>
28 #include <unistd.h>
29 
30 #include <string.h>
31 #include <pwd.h>
32 
33 #include "MediaMetricsService.h"
34 #include "iface_statsd.h"
35 
36 #include <statslog.h>
37 
38 #include <array>
39 #include <string>
40 
41 namespace android {
42 
43 // mediadrm
statsd_mediadrm(const mediametrics::Item * item)44 bool statsd_mediadrm(const mediametrics::Item *item)
45 {
46     if (item == nullptr) return false;
47 
48     const nsecs_t timestamp = MediaMetricsService::roundTime(item->getTimestamp());
49     std::string pkgName = item->getPkgName();
50     int64_t pkgVersionCode = item->getPkgVersionCode();
51     int64_t mediaApexVersion = 0;
52 
53     std::string vendor;
54     (void) item->getString("vendor", &vendor);
55     std::string description;
56     (void) item->getString("description", &description);
57     std::string serialized_metrics;
58     (void) item->getString("serialized_metrics", &serialized_metrics);
59 
60     if (enabled_statsd) {
61         android::util::BytesField bf_serialized(serialized_metrics.c_str(),
62                                                 serialized_metrics.size());
63         android::util::stats_write(android::util::MEDIAMETRICS_MEDIADRM_REPORTED,
64                                    timestamp, pkgName.c_str(), pkgVersionCode,
65                                    mediaApexVersion,
66                                    vendor.c_str(),
67                                    description.c_str(),
68                                    bf_serialized);
69     } else {
70         ALOGV("NOT sending: mediadrm private data (len=%zu)", serialized_metrics.size());
71     }
72 
73     return true;
74 }
75 
76 // widevineCDM
statsd_widevineCDM(const mediametrics::Item * item)77 bool statsd_widevineCDM(const mediametrics::Item *item)
78 {
79     if (item == nullptr) return false;
80 
81     const nsecs_t timestamp = MediaMetricsService::roundTime(item->getTimestamp());
82     std::string pkgName = item->getPkgName();
83     int64_t pkgVersionCode = item->getPkgVersionCode();
84     int64_t mediaApexVersion = 0;
85 
86     std::string serialized_metrics;
87     (void) item->getString("serialized_metrics", &serialized_metrics);
88 
89     if (enabled_statsd) {
90         android::util::BytesField bf_serialized(serialized_metrics.c_str(),
91                                                 serialized_metrics.size());
92         android::util::stats_write(android::util::MEDIAMETRICS_DRM_WIDEVINE_REPORTED,
93                                    timestamp, pkgName.c_str(), pkgVersionCode,
94                                    mediaApexVersion,
95                                    bf_serialized);
96     } else {
97         ALOGV("NOT sending: widevine private data (len=%zu)", serialized_metrics.size());
98     }
99 
100     return true;
101 }
102 
103 // drmmanager
statsd_drmmanager(const mediametrics::Item * item)104 bool statsd_drmmanager(const mediametrics::Item *item)
105 {
106     using namespace std::string_literals;
107     if (item == nullptr) return false;
108 
109     if (!enabled_statsd) {
110         ALOGV("NOT sending: drmmanager data");
111         return true;
112     }
113 
114     const nsecs_t timestamp = MediaMetricsService::roundTime(item->getTimestamp());
115     std::string pkgName = item->getPkgName();
116     int64_t pkgVersionCode = item->getPkgVersionCode();
117     int64_t mediaApexVersion = 0;
118 
119     std::string plugin_id;
120     (void) item->getString("plugin_id", &plugin_id);
121     std::string description;
122     (void) item->getString("description", &description);
123     int32_t method_id = -1;
124     (void) item->getInt32("method_id", &method_id);
125     std::string mime_types;
126     (void) item->getString("mime_types", &mime_types);
127 
128     // Corresponds to the 13 APIs tracked in the MediametricsDrmManagerReported statsd proto
129     // Please see also DrmManager::kMethodIdMap
130     std::array<int64_t, 13> methodCounts{};
131     for (size_t i = 0; i < methodCounts.size() ; i++) {
132         item->getInt64(("method"s + std::to_string(i)).c_str(), &methodCounts[i]);
133     }
134 
135     android::util::stats_write(android::util::MEDIAMETRICS_DRMMANAGER_REPORTED,
136                                timestamp, pkgName.c_str(), pkgVersionCode, mediaApexVersion,
137                                plugin_id.c_str(), description.c_str(),
138                                method_id, mime_types.c_str(),
139                                methodCounts[0], methodCounts[1], methodCounts[2],
140                                methodCounts[3], methodCounts[4], methodCounts[5],
141                                methodCounts[6], methodCounts[7], methodCounts[8],
142                                methodCounts[9], methodCounts[10], methodCounts[11],
143                                methodCounts[12]);
144 
145     return true;
146 }
147 
148 } // namespace android
149