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 
17 #ifndef DRM_METRICS_H_
18 #define DRM_METRICS_H_
19 
20 #include <map>
21 
22 #include <android/hardware/drm/1.0/types.h>
23 #include <android/hardware/drm/1.1/types.h>
24 #include <binder/PersistableBundle.h>
25 #include <media/CounterMetric.h>
26 #include <media/EventMetric.h>
27 
28 namespace android {
29 
30 /**
31  * This class contains the definition of metrics captured within MediaDrm.
32  * It also contains a method for exporting all of the metrics to a
33  * PersistableBundle.
34  */
35 class MediaDrmMetrics {
36  public:
37   explicit MediaDrmMetrics();
~MediaDrmMetrics()38   virtual ~MediaDrmMetrics() {};
39   // Count of openSession calls.
40   CounterMetric<status_t> mOpenSessionCounter;
41   // Count of closeSession calls.
42   CounterMetric<status_t> mCloseSessionCounter;
43   // Count and timing of getKeyRequest calls.
44   EventMetric<status_t> mGetKeyRequestTimeUs;
45   // Count and timing of provideKeyResponse calls.
46   EventMetric<status_t> mProvideKeyResponseTimeUs;
47   // Count of getProvisionRequest calls.
48   CounterMetric<status_t> mGetProvisionRequestCounter;
49   // Count of provideProvisionResponse calls.
50   CounterMetric<status_t> mProvideProvisionResponseCounter;
51 
52   // Count of key status events broken out by status type.
53   CounterMetric<::android::hardware::drm::V1_0::KeyStatusType>
54       mKeyStatusChangeCounter;
55   // Count of events broken out by event type
56   CounterMetric<::android::hardware::drm::V1_0::EventType> mEventCounter;
57 
58   // Count getPropertyByteArray calls to retrieve the device unique id.
59   CounterMetric<status_t> mGetDeviceUniqueIdCounter;
60 
61   // Adds a session start time record.
62   void SetSessionStart(const Vector<uint8_t>& sessionId);
63 
64   // Adds a session end time record.
65   void SetSessionEnd(const Vector<uint8_t>& sessionId);
66 
67   // The app package name is the application package name that is using the
68   // instance. The app package name is held here for convenience. It is not
69   // serialized or exported with the metrics.
SetAppPackageName(const String8 & appPackageName)70   void SetAppPackageName(const String8& appPackageName) { mAppPackageName = appPackageName; }
GetAppPackageName()71   const String8& GetAppPackageName() { return mAppPackageName; }
72 
73   // Export the metrics to a PersistableBundle.
74   void Export(os::PersistableBundle* metricsBundle);
75 
76   // Get the serialized metrics. Metrics are formatted as a serialized
77   // DrmFrameworkMetrics proto. If there is a failure serializing the metrics,
78   // this returns an error. The parameter |serlializedMetrics| is owned by the
79   // caller and must not be null.
80   status_t GetSerializedMetrics(std::string* serializedMetrics);
81 
82   // Converts the DRM plugin metrics to a PersistableBundle. All of the metrics
83   // found in |pluginMetrics| are added to the |metricsBundle| parameter.
84   // |pluginBundle| is owned by the caller and must not be null.
85   //
86   // Each item in the pluginMetrics vector is added as a new PersistableBundle. E.g.
87   // DrmMetricGroup {
88   //   metrics[0] {
89   //     name: "buf_copy"
90   //     attributes[0] {
91   //       name: "size"
92   //       type: INT64_TYPE
93   //       int64Value: 1024
94   //     }
95   //     values[0] {
96   //       componentName: "operation_count"
97   //       type: INT64_TYPE
98   //       int64Value: 75
99   //     }
100   //     values[1] {
101   //       component_name: "average_time_seconds"
102   //       type: DOUBLE_TYPE
103   //       doubleValue: 0.00000042
104   //     }
105   //   }
106   // }
107   //
108   // becomes
109   //
110   // metricsBundle {
111   //   "0": (PersistableBundle) {
112   //     "attributes" : (PersistableBundle) {
113   //       "size" : (int64) 1024
114   //     }
115   //     "operation_count" : (int64) 75
116   //     "average_time_seconds" : (double) 0.00000042
117   //   }
118   //
119   static status_t HidlMetricsToBundle(
120           const hardware::hidl_vec<hardware::drm::V1_1::DrmMetricGroup>& pluginMetrics,
121           os::PersistableBundle* metricsBundle);
122 
123  protected:
124   // This is visible for testing only.
125   virtual int64_t GetCurrentTimeMs();
126 
127  private:
128   // Session lifetimes. A pair of values representing the milliseconds since
129   // epoch, UTC. The first value is the start time, the second is the end time.
130   std::map<std::string, std::pair<int64_t, int64_t>> mSessionLifespans;
131 
132   String8 mAppPackageName;
133 };
134 
135 }  // namespace android
136 
137 #endif  // DRM_METRICS_H_
138