• 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 
17 #define LOG_TAG "MediaMetrics"
18 
19 #include <inttypes.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <sys/types.h>
23 
24 #include <media/MediaAnalyticsItem.h>
25 #include <media/MediaMetrics.h>
26 
27 //
28 // provide a C-ish interface that is easier to stabilize than the existing C++
29 // interface
30 //
31 // ALL functions returning a char * give responsibility for the allocated buffer
32 // to the caller. The caller is responsible to call free() on that pointer.
33 //
34 
35 // manage the overall record
mediametrics_create(mediametricskey_t key)36 mediametrics_handle_t mediametrics_create(mediametricskey_t key) {
37     android::MediaAnalyticsItem *item = android::MediaAnalyticsItem::create(key);
38     return (mediametrics_handle_t) item;
39 }
40 
mediametrics_delete(mediametrics_handle_t handle)41 void mediametrics_delete(mediametrics_handle_t handle) {
42     android::MediaAnalyticsItem *item = (android::MediaAnalyticsItem *) handle;
43     if (item == NULL) return;
44     delete item;
45 }
46 
mediametrics_getKey(mediametrics_handle_t handle)47 mediametricskey_t mediametrics_getKey(mediametrics_handle_t handle) {
48     android::MediaAnalyticsItem *item = (android::MediaAnalyticsItem *) handle;
49     if (item == NULL) return NULL;
50     return strdup(item->getKey().c_str());
51 }
52 
53 // nuplayer, et al use it when acting as proxies
mediametrics_setUid(mediametrics_handle_t handle,uid_t uid)54 void mediametrics_setUid(mediametrics_handle_t handle, uid_t uid) {
55     android::MediaAnalyticsItem *item = (android::MediaAnalyticsItem *) handle;
56     if (item != NULL) item->setUid(uid);
57 }
58 
59 // set attributes
60 //
61 
mediametrics_setInt32(mediametrics_handle_t handle,attr_t attr,int32_t value)62 void mediametrics_setInt32(mediametrics_handle_t handle, attr_t attr,
63                                 int32_t value) {
64     android::MediaAnalyticsItem *item = (android::MediaAnalyticsItem *) handle;
65     if (item != NULL) item->setInt32(attr, value);
66 }
67 
mediametrics_setInt64(mediametrics_handle_t handle,attr_t attr,int64_t value)68 void mediametrics_setInt64(mediametrics_handle_t handle, attr_t attr,
69                                 int64_t value) {
70     android::MediaAnalyticsItem *item = (android::MediaAnalyticsItem *) handle;
71     if (item != NULL) item->setInt64(attr, value);
72 }
73 
mediametrics_setDouble(mediametrics_handle_t handle,attr_t attr,double value)74 void mediametrics_setDouble(mediametrics_handle_t handle, attr_t attr,
75                                  double value) {
76     android::MediaAnalyticsItem *item = (android::MediaAnalyticsItem *) handle;
77     if (item != NULL) item->setDouble(attr, value);
78 }
79 
mediametrics_setRate(mediametrics_handle_t handle,attr_t attr,int64_t count,int64_t duration)80 void mediametrics_setRate(mediametrics_handle_t handle, attr_t attr,
81                                int64_t count, int64_t duration) {
82     android::MediaAnalyticsItem *item = (android::MediaAnalyticsItem *) handle;
83     if (item != NULL) item->setRate(attr, count, duration);
84 }
85 
mediametrics_setCString(mediametrics_handle_t handle,attr_t attr,const char * value)86 void mediametrics_setCString(mediametrics_handle_t handle, attr_t attr,
87                                  const char *value) {
88     android::MediaAnalyticsItem *item = (android::MediaAnalyticsItem *) handle;
89     if (item != NULL) item->setCString(attr, value);
90 }
91 
92 // fused get/add/set; if attr wasn't there, it's a simple set.
93 //
94 
mediametrics_addInt32(mediametrics_handle_t handle,attr_t attr,int32_t value)95 void mediametrics_addInt32(mediametrics_handle_t handle, attr_t attr,
96                                 int32_t value) {
97     android::MediaAnalyticsItem *item = (android::MediaAnalyticsItem *) handle;
98     if (item != NULL) item->addInt32(attr, value);
99 }
100 
mediametrics_addInt64(mediametrics_handle_t handle,attr_t attr,int64_t value)101 void mediametrics_addInt64(mediametrics_handle_t handle, attr_t attr,
102                                 int64_t value) {
103     android::MediaAnalyticsItem *item = (android::MediaAnalyticsItem *) handle;
104     if (item != NULL) item->addInt64(attr, value);
105 }
106 
mediametrics_addDouble(mediametrics_handle_t handle,attr_t attr,double value)107 void mediametrics_addDouble(mediametrics_handle_t handle, attr_t attr,
108                                  double value) {
109     android::MediaAnalyticsItem *item = (android::MediaAnalyticsItem *) handle;
110     if (item != NULL) item->addDouble(attr, value);
111 }
112 
mediametrics_addRate(mediametrics_handle_t handle,attr_t attr,int64_t count,int64_t duration)113 void mediametrics_addRate(mediametrics_handle_t handle, attr_t attr,
114                                int64_t count, int64_t duration) {
115     android::MediaAnalyticsItem *item = (android::MediaAnalyticsItem *) handle;
116     if (item != NULL) item->addRate(attr, count, duration);
117 }
118 
119 // find & extract values
120 // return indicates whether attr exists (and thus whether value filled in)
121 // NULL parameter value suppresses storage of value.
122 //
123 
mediametrics_getInt32(mediametrics_handle_t handle,attr_t attr,int32_t * value)124 bool mediametrics_getInt32(mediametrics_handle_t handle, attr_t attr,
125                                 int32_t * value) {
126     android::MediaAnalyticsItem *item = (android::MediaAnalyticsItem *) handle;
127     if (item == NULL) return false;
128     return item->getInt32(attr, value);
129 }
130 
mediametrics_getInt64(mediametrics_handle_t handle,attr_t attr,int64_t * value)131 bool mediametrics_getInt64(mediametrics_handle_t handle, attr_t attr,
132                                 int64_t * value) {
133     android::MediaAnalyticsItem *item = (android::MediaAnalyticsItem *) handle;
134     if (item == NULL) return false;
135     return item->getInt64(attr, value);
136 }
137 
mediametrics_getDouble(mediametrics_handle_t handle,attr_t attr,double * value)138 bool mediametrics_getDouble(mediametrics_handle_t handle, attr_t attr,
139                                  double *value) {
140     android::MediaAnalyticsItem *item = (android::MediaAnalyticsItem *) handle;
141     if (item == NULL) return false;
142     return item->getDouble(attr, value);
143 }
144 
mediametrics_getRate(mediametrics_handle_t handle,attr_t attr,int64_t * count,int64_t * duration,double * rate)145 bool mediametrics_getRate(mediametrics_handle_t handle, attr_t attr,
146                                int64_t * count, int64_t * duration, double *rate) {
147     android::MediaAnalyticsItem *item = (android::MediaAnalyticsItem *) handle;
148     if (item == NULL) return false;
149     return item->getRate(attr, count, duration, rate);
150 }
151 
152 // NB: caller owns the string that comes back, is responsible for freeing it
mediametrics_getCString(mediametrics_handle_t handle,attr_t attr,char ** value)153 bool mediametrics_getCString(mediametrics_handle_t handle, attr_t attr,
154                                  char **value) {
155     android::MediaAnalyticsItem *item = (android::MediaAnalyticsItem *) handle;
156     if (item == NULL) return false;
157 
158     return item->getCString(attr, value);
159 }
160 
161 // to release strings returned via getCString()
mediametrics_freeCString(char * value)162 void mediametrics_freeCString(char *value) {
163     free(value);
164 }
165 
mediametrics_selfRecord(mediametrics_handle_t handle)166 bool mediametrics_selfRecord(mediametrics_handle_t handle) {
167     android::MediaAnalyticsItem *item = (android::MediaAnalyticsItem *) handle;
168     if (item == NULL) return false;
169     return item->selfrecord();
170 }
171 
172 
mediametrics_readable(mediametrics_handle_t handle)173 const char *mediametrics_readable(mediametrics_handle_t handle) {
174     android::MediaAnalyticsItem *item = (android::MediaAnalyticsItem *) handle;
175     if (item == NULL) return "";
176     return item->toCString();
177 }
178 
mediametrics_count(mediametrics_handle_t handle)179 int32_t mediametrics_count(mediametrics_handle_t handle) {
180     android::MediaAnalyticsItem *item = (android::MediaAnalyticsItem *) handle;
181     if (item == NULL) return 0;
182     return item->count();
183 }
184 
mediametrics_isEnabled()185 bool mediametrics_isEnabled() {
186     // static, so doesn't need an instance
187     return android::MediaAnalyticsItem::isEnabled();
188 }
189 
mediametrics_getAttributes(mediametrics_handle_t handle,char ** buffer,size_t * length)190 bool mediametrics_getAttributes(mediametrics_handle_t handle, char **buffer, size_t *length) {
191     android::MediaAnalyticsItem *item = (android::MediaAnalyticsItem *) handle;
192     if (item == NULL) return false;
193     return item->dumpAttributes(buffer, length);
194 
195 }
196