Adam Stone | f0e618d | 2018-01-17 19:20:41 -0800 | [diff] [blame] | 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 | |
Adam Stone | cea91ce | 2018-01-22 19:23:28 -0800 | [diff] [blame] | 17 | #include <android-base/macros.h> |
Adam Stone | f0e618d | 2018-01-17 19:20:41 -0800 | [diff] [blame] | 18 | #include <media/DrmMetrics.h> |
| 19 | |
Adam Stone | cea91ce | 2018-01-22 19:23:28 -0800 | [diff] [blame] | 20 | using ::android::hardware::drm::V1_0::EventType; |
| 21 | using ::android::hardware::drm::V1_0::KeyStatusType; |
| 22 | |
Adam Stone | f0e618d | 2018-01-17 19:20:41 -0800 | [diff] [blame] | 23 | namespace { |
| 24 | |
| 25 | template<typename T> |
Adam Stone | cea91ce | 2018-01-22 19:23:28 -0800 | [diff] [blame] | 26 | std::string GetAttributeName(T type); |
| 27 | |
| 28 | template<> |
| 29 | std::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 | |
| 39 | template<> |
| 40 | std::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 | |
| 50 | template<typename T> |
Adam Stone | f0e618d | 2018-01-17 19:20:41 -0800 | [diff] [blame] | 51 | void ExportCounterMetric(const android::CounterMetric<T>& counter, |
| 52 | android::MediaAnalyticsItem* item) { |
Adam Stone | cea91ce | 2018-01-22 19:23:28 -0800 | [diff] [blame] | 53 | if (!item) { |
| 54 | ALOGE("item was unexpectedly null."); |
| 55 | return; |
| 56 | } |
Adam Stone | 4bea53c | 2018-01-24 21:44:58 -0800 | [diff] [blame^] | 57 | std::string success_count_name = counter.metric_name() + ".ok.count"; |
| 58 | std::string error_count_name = counter.metric_name() + ".error.count"; |
Adam Stone | f0e618d | 2018-01-17 19:20:41 -0800 | [diff] [blame] | 59 | 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 | |
| 73 | template<typename T> |
Adam Stone | cea91ce | 2018-01-22 19:23:28 -0800 | [diff] [blame] | 74 | void 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 Stone | 4bea53c | 2018-01-24 21:44:58 -0800 | [diff] [blame^] | 84 | + "." + GetAttributeName(attribute) + ".count"; |
Adam Stone | cea91ce | 2018-01-22 19:23:28 -0800 | [diff] [blame] | 85 | item->setInt64(name.c_str(), value); |
| 86 | }); |
| 87 | } |
| 88 | |
| 89 | template<typename T> |
Adam Stone | f0e618d | 2018-01-17 19:20:41 -0800 | [diff] [blame] | 90 | void ExportEventMetric(const android::EventMetric<T>& event, |
| 91 | android::MediaAnalyticsItem* item) { |
Adam Stone | cea91ce | 2018-01-22 19:23:28 -0800 | [diff] [blame] | 92 | if (!item) { |
| 93 | ALOGE("item was unexpectedly null."); |
| 94 | return; |
| 95 | } |
Adam Stone | 4bea53c | 2018-01-24 21:44:58 -0800 | [diff] [blame^] | 96 | 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 Stone | f0e618d | 2018-01-17 19:20:41 -0800 | [diff] [blame] | 99 | 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 | |
| 118 | namespace android { |
| 119 | |
| 120 | MediaDrmMetrics::MediaDrmMetrics() |
Adam Stone | 4bea53c | 2018-01-24 21:44:58 -0800 | [diff] [blame^] | 121 | : 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 Stone | cea91ce | 2018-01-22 19:23:28 -0800 | [diff] [blame] | 125 | mGetProvisionRequestCounter( |
Adam Stone | 4bea53c | 2018-01-24 21:44:58 -0800 | [diff] [blame^] | 126 | "drm.mediadrm.get_provision_request", "status"), |
Adam Stone | cea91ce | 2018-01-22 19:23:28 -0800 | [diff] [blame] | 127 | mProvideProvisionResponseCounter( |
Adam Stone | 4bea53c | 2018-01-24 21:44:58 -0800 | [diff] [blame^] | 128 | "drm.mediadrm.provide_provision_response", "status"), |
Adam Stone | cea91ce | 2018-01-22 19:23:28 -0800 | [diff] [blame] | 129 | mKeyStatusChangeCounter( |
Adam Stone | 4bea53c | 2018-01-24 21:44:58 -0800 | [diff] [blame^] | 130 | "drm.mediadrm.key_status_change", "key_status_type"), |
| 131 | mEventCounter("drm.mediadrm.event", "event_type"), |
Adam Stone | cea91ce | 2018-01-22 19:23:28 -0800 | [diff] [blame] | 132 | mGetDeviceUniqueIdCounter( |
Adam Stone | 4bea53c | 2018-01-24 21:44:58 -0800 | [diff] [blame^] | 133 | "drm.mediadrm.get_device_unique_id", "status") { |
Adam Stone | f0e618d | 2018-01-17 19:20:41 -0800 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | void MediaDrmMetrics::Export(MediaAnalyticsItem* item) { |
Adam Stone | cea91ce | 2018-01-22 19:23:28 -0800 | [diff] [blame] | 137 | if (!item) { |
| 138 | ALOGE("item was unexpectedly null."); |
| 139 | return; |
| 140 | } |
Adam Stone | f0e618d | 2018-01-17 19:20:41 -0800 | [diff] [blame] | 141 | ExportCounterMetric(mOpenSessionCounter, item); |
Adam Stone | cea91ce | 2018-01-22 19:23:28 -0800 | [diff] [blame] | 142 | ExportCounterMetric(mCloseSessionCounter, item); |
Adam Stone | f0e618d | 2018-01-17 19:20:41 -0800 | [diff] [blame] | 143 | ExportEventMetric(mGetKeyRequestTiming, item); |
Adam Stone | cea91ce | 2018-01-22 19:23:28 -0800 | [diff] [blame] | 144 | ExportEventMetric(mProvideKeyResponseTiming, item); |
| 145 | ExportCounterMetric(mGetProvisionRequestCounter, item); |
| 146 | ExportCounterMetric(mProvideProvisionResponseCounter, item); |
| 147 | ExportCounterMetricWithAttributeNames(mKeyStatusChangeCounter, item); |
| 148 | ExportCounterMetricWithAttributeNames(mEventCounter, item); |
| 149 | ExportCounterMetric(mGetDeviceUniqueIdCounter, item); |
Adam Stone | f0e618d | 2018-01-17 19:20:41 -0800 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | } // namespace android |