• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 uint32_t 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(key_status_type);
131         });
132 
133     mEventCounter.ExportValues(
134         [&](const uint32_t event_type, const int64_t value) {
135             DrmFrameworkMetrics::Counter *counter =
136                 metrics.add_event_callback_counter();
137             counter->set_count(value);
138             counter->mutable_attributes()->set_event_type(event_type);
139         });
140 
141     mGetDeviceUniqueIdCounter.ExportValues(
142         [&](const status_t status, const int64_t value) {
143             DrmFrameworkMetrics::Counter *counter =
144                 metrics.add_get_device_unique_id_counter();
145             counter->set_count(value);
146             counter->mutable_attributes()->set_error_code(status);
147         });
148 
149     mGetKeyRequestTimeUs.ExportValues(
150         [&](const status_t status, const EventStatistics &stats) {
151             DrmFrameworkMetrics::DistributionMetric *metric =
152                 metrics.add_get_key_request_time_us();
153             metric->set_min(stats.min);
154             metric->set_max(stats.max);
155             metric->set_mean(stats.mean);
156             metric->set_operation_count(stats.count);
157             metric->set_variance(stats.sum_squared_deviation / stats.count);
158             metric->mutable_attributes()->set_error_code(status);
159         });
160 
161     mProvideKeyResponseTimeUs.ExportValues(
162         [&](const status_t status, const EventStatistics &stats) {
163             DrmFrameworkMetrics::DistributionMetric *metric =
164                 metrics.add_provide_key_response_time_us();
165             metric->set_min(stats.min);
166             metric->set_max(stats.max);
167             metric->set_mean(stats.mean);
168             metric->set_operation_count(stats.count);
169             metric->set_variance(stats.sum_squared_deviation / stats.count);
170             metric->mutable_attributes()->set_error_code(status);
171         });
172 
173     for (const auto &sessionLifespan : mSessionLifespans) {
174         auto *map = metrics.mutable_session_lifetimes();
175 
176         (*map)[sessionLifespan.first].set_start_time_ms(
177             sessionLifespan.second.first);
178         (*map)[sessionLifespan.first].set_end_time_ms(
179             sessionLifespan.second.second);
180     }
181 
182     if (!metrics.SerializeToString(serializedMetrics)) {
183         ALOGE("Failed to serialize metrics.");
184         return UNKNOWN_ERROR;
185     }
186 
187     return OK;
188 }
189 
GetSessionLifespans() const190 std::map<std::string, std::pair<int64_t, int64_t>> MediaDrmMetrics::GetSessionLifespans() const {
191   return mSessionLifespans;
192 }
193 
GetCurrentTimeMs()194 int64_t MediaDrmMetrics::GetCurrentTimeMs() {
195     struct timeval tv;
196     gettimeofday(&tv, NULL);
197     return ((int64_t)tv.tv_sec * 1000) + ((int64_t)tv.tv_usec / 1000);
198 }
199 
200 } // namespace android
201