Ray Essick | 2014732 | 2018-11-17 09:08:39 -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 | |
| 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) { |
Ray Essick | ba8c484 | 2019-01-18 11:35:33 -0800 | [diff] [blame] | 37 | android::MediaAnalyticsItem *item = android::MediaAnalyticsItem::create(key); |
Ray Essick | 2014732 | 2018-11-17 09:08:39 -0800 | [diff] [blame] | 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 | |
Ray Essick | fc0a473 | 2019-08-26 11:04:28 -0700 | [diff] [blame] | 172 | mediametrics_handle_t mediametrics_dup(mediametrics_handle_t handle) { |
| 173 | android::MediaAnalyticsItem *item = (android::MediaAnalyticsItem *) handle; |
| 174 | if (item == NULL) return android::MediaAnalyticsItem::convert(item); |
| 175 | return android::MediaAnalyticsItem::convert(item->dup()); |
| 176 | } |
Ray Essick | 2014732 | 2018-11-17 09:08:39 -0800 | [diff] [blame] | 177 | |
| 178 | const char *mediametrics_readable(mediametrics_handle_t handle) { |
| 179 | android::MediaAnalyticsItem *item = (android::MediaAnalyticsItem *) handle; |
| 180 | if (item == NULL) return ""; |
| 181 | return item->toCString(); |
| 182 | } |
| 183 | |
| 184 | int32_t mediametrics_count(mediametrics_handle_t handle) { |
| 185 | android::MediaAnalyticsItem *item = (android::MediaAnalyticsItem *) handle; |
| 186 | if (item == NULL) return 0; |
| 187 | return item->count(); |
| 188 | } |
| 189 | |
| 190 | bool mediametrics_isEnabled() { |
| 191 | // static, so doesn't need an instance |
| 192 | return android::MediaAnalyticsItem::isEnabled(); |
| 193 | } |
| 194 | |
Ray Essick | ba8c484 | 2019-01-18 11:35:33 -0800 | [diff] [blame] | 195 | bool mediametrics_getAttributes(mediametrics_handle_t handle, char **buffer, size_t *length) { |
Ray Essick | 2014732 | 2018-11-17 09:08:39 -0800 | [diff] [blame] | 196 | android::MediaAnalyticsItem *item = (android::MediaAnalyticsItem *) handle; |
Ray Essick | ba8c484 | 2019-01-18 11:35:33 -0800 | [diff] [blame] | 197 | if (item == NULL) return false; |
| 198 | return item->dumpAttributes(buffer, length); |
| 199 | |
Ray Essick | 2014732 | 2018-11-17 09:08:39 -0800 | [diff] [blame] | 200 | } |