1 /*
2 * Copyright (C) 2018 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 #define LOG_TAG "DrmMetrics"
17 #include <iomanip>
18 #include <utility>
19
20 #include <android-base/macros.h>
21 #include <media/stagefright/foundation/base64.h>
22 #include <mediadrm/DrmMetrics.h>
23 #include <sys/time.h>
24 #include <utils/Log.h>
25 #include <utils/Timers.h>
26
27 #include "protos/metrics.pb.h"
28
29 using ::android::String16;
30 using ::android::String8;
31 using ::android::drm_metrics::DrmFrameworkMetrics;
32 using ::android::hardware::drm::V1_0::EventType;
33 using ::android::hardware::drm::V1_2::KeyStatusType;
34 using ::android::hardware::drm::V1_1::DrmMetricGroup;
35
36 namespace {
37
ToHexString(const android::Vector<uint8_t> & sessionId)38 std::string ToHexString(const android::Vector<uint8_t> &sessionId) {
39 std::ostringstream out;
40 out << std::hex << std::setfill('0');
41 for (size_t i = 0; i < sessionId.size(); i++) {
42 out << std::setw(2) << (int)(sessionId[i]);
43 }
44 return out.str();
45 }
46
47 } // namespace
48
49 namespace android {
50
MediaDrmMetrics()51 MediaDrmMetrics::MediaDrmMetrics()
52 : mOpenSessionCounter("drm.mediadrm.open_session", "status"),
53 mCloseSessionCounter("drm.mediadrm.close_session", "status"),
54 mGetKeyRequestTimeUs("drm.mediadrm.get_key_request", "status"),
55 mProvideKeyResponseTimeUs("drm.mediadrm.provide_key_response", "status"),
56 mGetProvisionRequestCounter("drm.mediadrm.get_provision_request",
57 "status"),
58 mProvideProvisionResponseCounter(
59 "drm.mediadrm.provide_provision_response", "status"),
60 mKeyStatusChangeCounter("drm.mediadrm.key_status_change",
61 "key_status_type"),
62 mEventCounter("drm.mediadrm.event", "event_type"),
63 mGetDeviceUniqueIdCounter("drm.mediadrm.get_device_unique_id", "status") {
64 }
65
SetSessionStart(const android::Vector<uint8_t> & sessionId)66 void MediaDrmMetrics::SetSessionStart(
67 const android::Vector<uint8_t> &sessionId) {
68 std::string sessionIdHex = ToHexString(sessionId);
69 mSessionLifespans[sessionIdHex] =
70 std::make_pair(GetCurrentTimeMs(), (int64_t)0);
71 }
72
SetSessionEnd(const android::Vector<uint8_t> & sessionId)73 void MediaDrmMetrics::SetSessionEnd(const android::Vector<uint8_t> &sessionId) {
74 std::string sessionIdHex = ToHexString(sessionId);
75 int64_t endTimeMs = GetCurrentTimeMs();
76 if (mSessionLifespans.find(sessionIdHex) != mSessionLifespans.end()) {
77 mSessionLifespans[sessionIdHex] =
78 std::make_pair(mSessionLifespans[sessionIdHex].first, endTimeMs);
79 } else {
80 mSessionLifespans[sessionIdHex] = std::make_pair((int64_t)0, endTimeMs);
81 }
82 }
83
GetSerializedMetrics(std::string * serializedMetrics)84 status_t MediaDrmMetrics::GetSerializedMetrics(std::string *serializedMetrics) {
85
86 if (!serializedMetrics) {
87 ALOGE("serializedMetrics was unexpectedly null.");
88 return UNEXPECTED_NULL;
89 }
90
91 DrmFrameworkMetrics metrics;
92
93 mOpenSessionCounter.ExportValues(
94 [&](const android::status_t status, const int64_t value) {
95 DrmFrameworkMetrics::Counter *counter =
96 metrics.add_open_session_counter();
97 counter->set_count(value);
98 counter->mutable_attributes()->set_error_code(status);
99 });
100
101 mCloseSessionCounter.ExportValues(
102 [&](const android::status_t status, const int64_t value) {
103 DrmFrameworkMetrics::Counter *counter =
104 metrics.add_close_session_counter();
105 counter->set_count(value);
106 counter->mutable_attributes()->set_error_code(status);
107 });
108
109 mGetProvisionRequestCounter.ExportValues(
110 [&](const android::status_t status, const int64_t value) {
111 DrmFrameworkMetrics::Counter *counter =
112 metrics.add_get_provisioning_request_counter();
113 counter->set_count(value);
114 counter->mutable_attributes()->set_error_code(status);
115 });
116
117 mProvideProvisionResponseCounter.ExportValues(
118 [&](const android::status_t status, const int64_t value) {
119 DrmFrameworkMetrics::Counter *counter =
120 metrics.add_provide_provisioning_response_counter();
121 counter->set_count(value);
122 counter->mutable_attributes()->set_error_code(status);
123 });
124
125 mKeyStatusChangeCounter.ExportValues(
126 [&](const KeyStatusType key_status_type, const int64_t value) {
127 DrmFrameworkMetrics::Counter *counter =
128 metrics.add_key_status_change_counter();
129 counter->set_count(value);
130 counter->mutable_attributes()->set_key_status_type(
131 (uint32_t)key_status_type);
132 });
133
134 mEventCounter.ExportValues(
135 [&](const EventType event_type, const int64_t value) {
136 DrmFrameworkMetrics::Counter *counter =
137 metrics.add_event_callback_counter();
138 counter->set_count(value);
139 counter->mutable_attributes()->set_event_type((uint32_t)event_type);
140 });
141
142 mGetDeviceUniqueIdCounter.ExportValues(
143 [&](const status_t status, const int64_t value) {
144 DrmFrameworkMetrics::Counter *counter =
145 metrics.add_get_device_unique_id_counter();
146 counter->set_count(value);
147 counter->mutable_attributes()->set_error_code(status);
148 });
149
150 mGetKeyRequestTimeUs.ExportValues(
151 [&](const status_t status, const EventStatistics &stats) {
152 DrmFrameworkMetrics::DistributionMetric *metric =
153 metrics.add_get_key_request_time_us();
154 metric->set_min(stats.min);
155 metric->set_max(stats.max);
156 metric->set_mean(stats.mean);
157 metric->set_operation_count(stats.count);
158 metric->set_variance(stats.sum_squared_deviation / stats.count);
159 metric->mutable_attributes()->set_error_code(status);
160 });
161
162 mProvideKeyResponseTimeUs.ExportValues(
163 [&](const status_t status, const EventStatistics &stats) {
164 DrmFrameworkMetrics::DistributionMetric *metric =
165 metrics.add_provide_key_response_time_us();
166 metric->set_min(stats.min);
167 metric->set_max(stats.max);
168 metric->set_mean(stats.mean);
169 metric->set_operation_count(stats.count);
170 metric->set_variance(stats.sum_squared_deviation / stats.count);
171 metric->mutable_attributes()->set_error_code(status);
172 });
173
174 for (const auto &sessionLifespan : mSessionLifespans) {
175 auto *map = metrics.mutable_session_lifetimes();
176
177 (*map)[sessionLifespan.first].set_start_time_ms(
178 sessionLifespan.second.first);
179 (*map)[sessionLifespan.first].set_end_time_ms(
180 sessionLifespan.second.second);
181 }
182
183 if (!metrics.SerializeToString(serializedMetrics)) {
184 ALOGE("Failed to serialize metrics.");
185 return UNKNOWN_ERROR;
186 }
187
188 return OK;
189 }
190
GetSessionLifespans() const191 std::map<std::string, std::pair<int64_t, int64_t>> MediaDrmMetrics::GetSessionLifespans() const {
192 return mSessionLifespans;
193 }
194
GetCurrentTimeMs()195 int64_t MediaDrmMetrics::GetCurrentTimeMs() {
196 struct timeval tv;
197 gettimeofday(&tv, NULL);
198 return ((int64_t)tv.tv_sec * 1000) + ((int64_t)tv.tv_usec / 1000);
199 }
200
201 } // namespace android
202