Home | History | Annotate | Download | only in libmediametrics
      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
     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 
     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 
     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
     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 
     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 
     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 
     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 
     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 
     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 
     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 
    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 
    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 
    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 
    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 
    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 
    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 
    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
    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()
    162 void mediametrics_freeCString(char *value) {
    163     free(value);
    164 }
    165 
    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 
    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 
    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 
    185 bool mediametrics_isEnabled() {
    186     // static, so doesn't need an instance
    187     return android::MediaAnalyticsItem::isEnabled();
    188 }
    189 
    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