blob: 258c4b0d083f848a58b7c2c7236287837caff3c2 [file] [log] [blame]
Adam Stonef0e618d2018-01-17 19:20:41 -08001/*
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
Adam Stonecea91ce2018-01-22 19:23:28 -080017#include <android-base/macros.h>
Adam Stonef0e618d2018-01-17 19:20:41 -080018#include <media/DrmMetrics.h>
19
Adam Stonecea91ce2018-01-22 19:23:28 -080020using ::android::hardware::drm::V1_0::EventType;
21using ::android::hardware::drm::V1_0::KeyStatusType;
22
Adam Stonef0e618d2018-01-17 19:20:41 -080023namespace {
24
25template<typename T>
Adam Stonecea91ce2018-01-22 19:23:28 -080026std::string GetAttributeName(T type);
27
28template<>
29std::string GetAttributeName<KeyStatusType>(KeyStatusType type) {
30 static const char* type_names[] = {
31 "USABLE", "EXPIRED", "OUTPUT_NOT_ALLOWED",
32 "STATUS_PENDING", "INTERNAL_ERROR" };
33 if (((size_t) type) > arraysize(type_names)) {
34 return "UNKNOWN_TYPE";
35 }
36 return type_names[(size_t) type];
37}
38
39template<>
40std::string GetAttributeName<EventType>(EventType type) {
41 static const char* type_names[] = {
42 "PROVISION_REQUIRED", "KEY_NEEDED", "KEY_EXPIRED",
43 "VENDOR_DEFINED", "SESSION_RECLAIMED" };
44 if (((size_t) type) > arraysize(type_names)) {
45 return "UNKNOWN_TYPE";
46 }
47 return type_names[(size_t) type];
48}
49
50template<typename T>
Adam Stonef0e618d2018-01-17 19:20:41 -080051void ExportCounterMetric(const android::CounterMetric<T>& counter,
52 android::MediaAnalyticsItem* item) {
Adam Stonecea91ce2018-01-22 19:23:28 -080053 if (!item) {
54 ALOGE("item was unexpectedly null.");
55 return;
56 }
Adam Stone4bea53c2018-01-24 21:44:58 -080057 std::string success_count_name = counter.metric_name() + ".ok.count";
58 std::string error_count_name = counter.metric_name() + ".error.count";
Adam Stonef0e618d2018-01-17 19:20:41 -080059 counter.ExportValues(
60 [&] (const android::status_t status, const int64_t value) {
61 if (status == android::OK) {
62 item->setInt64(success_count_name.c_str(), value);
63 } else {
64 int64_t total_errors(0);
65 item->getInt64(error_count_name.c_str(), &total_errors);
66 item->setInt64(error_count_name.c_str(), total_errors + value);
67 // TODO: Add support for exporting the list of error values.
68 // This probably needs to be added to MediaAnalyticsItem.
69 }
70 });
71}
72
73template<typename T>
Adam Stonecea91ce2018-01-22 19:23:28 -080074void ExportCounterMetricWithAttributeNames(
75 const android::CounterMetric<T>& counter,
76 android::MediaAnalyticsItem* item) {
77 if (!item) {
78 ALOGE("item was unexpectedly null.");
79 return;
80 }
81 counter.ExportValues(
82 [&] (const T& attribute, const int64_t value) {
83 std::string name = counter.metric_name()
Adam Stone4bea53c2018-01-24 21:44:58 -080084 + "." + GetAttributeName(attribute) + ".count";
Adam Stonecea91ce2018-01-22 19:23:28 -080085 item->setInt64(name.c_str(), value);
86 });
87}
88
89template<typename T>
Adam Stonef0e618d2018-01-17 19:20:41 -080090void ExportEventMetric(const android::EventMetric<T>& event,
91 android::MediaAnalyticsItem* item) {
Adam Stonecea91ce2018-01-22 19:23:28 -080092 if (!item) {
93 ALOGE("item was unexpectedly null.");
94 return;
95 }
Adam Stone4bea53c2018-01-24 21:44:58 -080096 std::string success_count_name = event.metric_name() + ".ok.count";
97 std::string error_count_name = event.metric_name() + ".error.count";
98 std::string timing_name = event.metric_name() + ".ok.average_time_micros";
Adam Stonef0e618d2018-01-17 19:20:41 -080099 event.ExportValues(
100 [&] (const android::status_t& status,
101 const android::EventStatistics& value) {
102 if (status == android::OK) {
103 item->setInt64(success_count_name.c_str(), value.count);
104 item->setInt64(timing_name.c_str(), value.mean);
105 } else {
106 int64_t total_errors(0);
107 item->getInt64(error_count_name.c_str(), &total_errors);
108 item->setInt64(error_count_name.c_str(),
109 total_errors + value.count);
110 // TODO: Add support for exporting the list of error values.
111 // This probably needs to be added to MediaAnalyticsItem.
112 }
113 });
114}
115
116} // namespace anonymous
117
118namespace android {
119
120MediaDrmMetrics::MediaDrmMetrics()
Adam Stone4bea53c2018-01-24 21:44:58 -0800121 : mOpenSessionCounter("drm.mediadrm.open_session", "status"),
122 mCloseSessionCounter("drm.mediadrm.close_session", "status"),
123 mGetKeyRequestTiming("drm.mediadrm.get_key_request", "status"),
124 mProvideKeyResponseTiming("drm.mediadrm.provide_key_response", "status"),
Adam Stonecea91ce2018-01-22 19:23:28 -0800125 mGetProvisionRequestCounter(
Adam Stone4bea53c2018-01-24 21:44:58 -0800126 "drm.mediadrm.get_provision_request", "status"),
Adam Stonecea91ce2018-01-22 19:23:28 -0800127 mProvideProvisionResponseCounter(
Adam Stone4bea53c2018-01-24 21:44:58 -0800128 "drm.mediadrm.provide_provision_response", "status"),
Adam Stonecea91ce2018-01-22 19:23:28 -0800129 mKeyStatusChangeCounter(
Adam Stone4bea53c2018-01-24 21:44:58 -0800130 "drm.mediadrm.key_status_change", "key_status_type"),
131 mEventCounter("drm.mediadrm.event", "event_type"),
Adam Stonecea91ce2018-01-22 19:23:28 -0800132 mGetDeviceUniqueIdCounter(
Adam Stone4bea53c2018-01-24 21:44:58 -0800133 "drm.mediadrm.get_device_unique_id", "status") {
Adam Stonef0e618d2018-01-17 19:20:41 -0800134}
135
136void MediaDrmMetrics::Export(MediaAnalyticsItem* item) {
Adam Stonecea91ce2018-01-22 19:23:28 -0800137 if (!item) {
138 ALOGE("item was unexpectedly null.");
139 return;
140 }
Adam Stonef0e618d2018-01-17 19:20:41 -0800141 ExportCounterMetric(mOpenSessionCounter, item);
Adam Stonecea91ce2018-01-22 19:23:28 -0800142 ExportCounterMetric(mCloseSessionCounter, item);
Adam Stonef0e618d2018-01-17 19:20:41 -0800143 ExportEventMetric(mGetKeyRequestTiming, item);
Adam Stonecea91ce2018-01-22 19:23:28 -0800144 ExportEventMetric(mProvideKeyResponseTiming, item);
145 ExportCounterMetric(mGetProvisionRequestCounter, item);
146 ExportCounterMetric(mProvideProvisionResponseCounter, item);
147 ExportCounterMetricWithAttributeNames(mKeyStatusChangeCounter, item);
148 ExportCounterMetricWithAttributeNames(mEventCounter, item);
149 ExportCounterMetric(mGetDeviceUniqueIdCounter, item);
Adam Stonef0e618d2018-01-17 19:20:41 -0800150}
151
152} // namespace android