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 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 24 | #include <media/MediaMetricsItem.h> |
Ray Essick | 2014732 | 2018-11-17 09:08:39 -0800 | [diff] [blame] | 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 | // |
Ray Essick | ae88dd3 | 2019-12-17 20:14:05 -0800 | [diff] [blame] | 34 | // |
| 35 | |
| 36 | using namespace android::mediametrics; |
Ray Essick | 2014732 | 2018-11-17 09:08:39 -0800 | [diff] [blame] | 37 | |
| 38 | // manage the overall record |
| 39 | mediametrics_handle_t mediametrics_create(mediametricskey_t key) { |
Ray Essick | ae88dd3 | 2019-12-17 20:14:05 -0800 | [diff] [blame] | 40 | Item *item = Item::create(key); |
Ray Essick | 2014732 | 2018-11-17 09:08:39 -0800 | [diff] [blame] | 41 | return (mediametrics_handle_t) item; |
| 42 | } |
| 43 | |
| 44 | void mediametrics_delete(mediametrics_handle_t handle) { |
Ray Essick | ae88dd3 | 2019-12-17 20:14:05 -0800 | [diff] [blame] | 45 | Item *item = (Item *) handle; |
Ray Essick | 2014732 | 2018-11-17 09:08:39 -0800 | [diff] [blame] | 46 | if (item == NULL) return; |
| 47 | delete item; |
| 48 | } |
| 49 | |
| 50 | mediametricskey_t mediametrics_getKey(mediametrics_handle_t handle) { |
Ray Essick | ae88dd3 | 2019-12-17 20:14:05 -0800 | [diff] [blame] | 51 | Item *item = (Item *) handle; |
Ray Essick | 2014732 | 2018-11-17 09:08:39 -0800 | [diff] [blame] | 52 | if (item == NULL) return NULL; |
| 53 | return strdup(item->getKey().c_str()); |
| 54 | } |
| 55 | |
| 56 | // nuplayer, et al use it when acting as proxies |
| 57 | void mediametrics_setUid(mediametrics_handle_t handle, uid_t uid) { |
Ray Essick | ae88dd3 | 2019-12-17 20:14:05 -0800 | [diff] [blame] | 58 | Item *item = (Item *) handle; |
Ray Essick | 2014732 | 2018-11-17 09:08:39 -0800 | [diff] [blame] | 59 | if (item != NULL) item->setUid(uid); |
| 60 | } |
| 61 | |
| 62 | // set attributes |
| 63 | // |
| 64 | |
| 65 | void mediametrics_setInt32(mediametrics_handle_t handle, attr_t attr, |
| 66 | int32_t value) { |
Ray Essick | ae88dd3 | 2019-12-17 20:14:05 -0800 | [diff] [blame] | 67 | Item *item = (Item *) handle; |
Ray Essick | 2014732 | 2018-11-17 09:08:39 -0800 | [diff] [blame] | 68 | if (item != NULL) item->setInt32(attr, value); |
| 69 | } |
| 70 | |
| 71 | void mediametrics_setInt64(mediametrics_handle_t handle, attr_t attr, |
| 72 | int64_t value) { |
Ray Essick | ae88dd3 | 2019-12-17 20:14:05 -0800 | [diff] [blame] | 73 | Item *item = (Item *) handle; |
Ray Essick | 2014732 | 2018-11-17 09:08:39 -0800 | [diff] [blame] | 74 | if (item != NULL) item->setInt64(attr, value); |
| 75 | } |
| 76 | |
| 77 | void mediametrics_setDouble(mediametrics_handle_t handle, attr_t attr, |
| 78 | double value) { |
Ray Essick | ae88dd3 | 2019-12-17 20:14:05 -0800 | [diff] [blame] | 79 | Item *item = (Item *) handle; |
Ray Essick | 2014732 | 2018-11-17 09:08:39 -0800 | [diff] [blame] | 80 | if (item != NULL) item->setDouble(attr, value); |
| 81 | } |
| 82 | |
| 83 | void mediametrics_setRate(mediametrics_handle_t handle, attr_t attr, |
| 84 | int64_t count, int64_t duration) { |
Ray Essick | ae88dd3 | 2019-12-17 20:14:05 -0800 | [diff] [blame] | 85 | Item *item = (Item *) handle; |
Ray Essick | 2014732 | 2018-11-17 09:08:39 -0800 | [diff] [blame] | 86 | if (item != NULL) item->setRate(attr, count, duration); |
| 87 | } |
| 88 | |
Brian Lindahl | c4e9dee | 2023-05-01 15:21:44 -0600 | [diff] [blame] | 89 | void mediametrics_setString(mediametrics_handle_t handle, attr_t attr, |
Brian Lindahl | 5c0ba41 | 2023-08-28 13:24:22 -0600 | [diff] [blame] | 90 | const std::string &string) { |
Brian Lindahl | c4e9dee | 2023-05-01 15:21:44 -0600 | [diff] [blame] | 91 | mediametrics_setCString(handle, attr, string.c_str()); |
| 92 | } |
| 93 | |
Ray Essick | 2014732 | 2018-11-17 09:08:39 -0800 | [diff] [blame] | 94 | void mediametrics_setCString(mediametrics_handle_t handle, attr_t attr, |
| 95 | const char *value) { |
Ray Essick | ae88dd3 | 2019-12-17 20:14:05 -0800 | [diff] [blame] | 96 | Item *item = (Item *) handle; |
Ray Essick | 2014732 | 2018-11-17 09:08:39 -0800 | [diff] [blame] | 97 | if (item != NULL) item->setCString(attr, value); |
| 98 | } |
| 99 | |
| 100 | // fused get/add/set; if attr wasn't there, it's a simple set. |
| 101 | // |
| 102 | |
| 103 | void mediametrics_addInt32(mediametrics_handle_t handle, attr_t attr, |
| 104 | int32_t value) { |
Ray Essick | ae88dd3 | 2019-12-17 20:14:05 -0800 | [diff] [blame] | 105 | Item *item = (Item *) handle; |
Ray Essick | 2014732 | 2018-11-17 09:08:39 -0800 | [diff] [blame] | 106 | if (item != NULL) item->addInt32(attr, value); |
| 107 | } |
| 108 | |
| 109 | void mediametrics_addInt64(mediametrics_handle_t handle, attr_t attr, |
| 110 | int64_t value) { |
Ray Essick | ae88dd3 | 2019-12-17 20:14:05 -0800 | [diff] [blame] | 111 | Item *item = (Item *) handle; |
Ray Essick | 2014732 | 2018-11-17 09:08:39 -0800 | [diff] [blame] | 112 | if (item != NULL) item->addInt64(attr, value); |
| 113 | } |
| 114 | |
| 115 | void mediametrics_addDouble(mediametrics_handle_t handle, attr_t attr, |
| 116 | double value) { |
Ray Essick | ae88dd3 | 2019-12-17 20:14:05 -0800 | [diff] [blame] | 117 | Item *item = (Item *) handle; |
Ray Essick | 2014732 | 2018-11-17 09:08:39 -0800 | [diff] [blame] | 118 | if (item != NULL) item->addDouble(attr, value); |
| 119 | } |
| 120 | |
| 121 | void mediametrics_addRate(mediametrics_handle_t handle, attr_t attr, |
| 122 | int64_t count, int64_t duration) { |
Ray Essick | ae88dd3 | 2019-12-17 20:14:05 -0800 | [diff] [blame] | 123 | Item *item = (Item *) handle; |
Ray Essick | 2014732 | 2018-11-17 09:08:39 -0800 | [diff] [blame] | 124 | if (item != NULL) item->addRate(attr, count, duration); |
| 125 | } |
| 126 | |
| 127 | // find & extract values |
| 128 | // return indicates whether attr exists (and thus whether value filled in) |
| 129 | // NULL parameter value suppresses storage of value. |
| 130 | // |
| 131 | |
| 132 | bool mediametrics_getInt32(mediametrics_handle_t handle, attr_t attr, |
| 133 | int32_t * value) { |
Ray Essick | ae88dd3 | 2019-12-17 20:14:05 -0800 | [diff] [blame] | 134 | Item *item = (Item *) handle; |
Ray Essick | 2014732 | 2018-11-17 09:08:39 -0800 | [diff] [blame] | 135 | if (item == NULL) return false; |
| 136 | return item->getInt32(attr, value); |
| 137 | } |
| 138 | |
| 139 | bool mediametrics_getInt64(mediametrics_handle_t handle, attr_t attr, |
| 140 | int64_t * value) { |
Ray Essick | ae88dd3 | 2019-12-17 20:14:05 -0800 | [diff] [blame] | 141 | Item *item = (Item *) handle; |
Ray Essick | 2014732 | 2018-11-17 09:08:39 -0800 | [diff] [blame] | 142 | if (item == NULL) return false; |
| 143 | return item->getInt64(attr, value); |
| 144 | } |
| 145 | |
| 146 | bool mediametrics_getDouble(mediametrics_handle_t handle, attr_t attr, |
| 147 | double *value) { |
Ray Essick | ae88dd3 | 2019-12-17 20:14:05 -0800 | [diff] [blame] | 148 | Item *item = (Item *) handle; |
Ray Essick | 2014732 | 2018-11-17 09:08:39 -0800 | [diff] [blame] | 149 | if (item == NULL) return false; |
| 150 | return item->getDouble(attr, value); |
| 151 | } |
| 152 | |
| 153 | bool mediametrics_getRate(mediametrics_handle_t handle, attr_t attr, |
| 154 | int64_t * count, int64_t * duration, double *rate) { |
Ray Essick | ae88dd3 | 2019-12-17 20:14:05 -0800 | [diff] [blame] | 155 | Item *item = (Item *) handle; |
Ray Essick | 2014732 | 2018-11-17 09:08:39 -0800 | [diff] [blame] | 156 | if (item == NULL) return false; |
| 157 | return item->getRate(attr, count, duration, rate); |
| 158 | } |
| 159 | |
Brian Lindahl | c4e9dee | 2023-05-01 15:21:44 -0600 | [diff] [blame] | 160 | bool mediametrics_getString(mediametrics_handle_t handle, attr_t attr, |
| 161 | std::string *string) { |
| 162 | Item *item = (Item *) handle; |
| 163 | if (item == NULL) return false; |
| 164 | |
| 165 | return item->getString(attr, string); |
| 166 | } |
| 167 | |
Ray Essick | 2014732 | 2018-11-17 09:08:39 -0800 | [diff] [blame] | 168 | // NB: caller owns the string that comes back, is responsible for freeing it |
| 169 | bool mediametrics_getCString(mediametrics_handle_t handle, attr_t attr, |
| 170 | char **value) { |
Ray Essick | ae88dd3 | 2019-12-17 20:14:05 -0800 | [diff] [blame] | 171 | Item *item = (Item *) handle; |
Ray Essick | 2014732 | 2018-11-17 09:08:39 -0800 | [diff] [blame] | 172 | if (item == NULL) return false; |
| 173 | |
| 174 | return item->getCString(attr, value); |
| 175 | } |
| 176 | |
| 177 | // to release strings returned via getCString() |
| 178 | void mediametrics_freeCString(char *value) { |
| 179 | free(value); |
| 180 | } |
| 181 | |
| 182 | bool mediametrics_selfRecord(mediametrics_handle_t handle) { |
Ray Essick | ae88dd3 | 2019-12-17 20:14:05 -0800 | [diff] [blame] | 183 | Item *item = (Item *) handle; |
Ray Essick | 2014732 | 2018-11-17 09:08:39 -0800 | [diff] [blame] | 184 | if (item == NULL) return false; |
| 185 | return item->selfrecord(); |
| 186 | } |
| 187 | |
Ray Essick | bf536ac | 2019-08-26 11:04:28 -0700 | [diff] [blame] | 188 | mediametrics_handle_t mediametrics_dup(mediametrics_handle_t handle) { |
Ray Essick | ae88dd3 | 2019-12-17 20:14:05 -0800 | [diff] [blame] | 189 | Item *item = (Item *) handle; |
| 190 | if (item == NULL) return Item::convert(item); |
| 191 | return Item::convert(item->dup()); |
Ray Essick | bf536ac | 2019-08-26 11:04:28 -0700 | [diff] [blame] | 192 | } |
Ray Essick | 2014732 | 2018-11-17 09:08:39 -0800 | [diff] [blame] | 193 | |
| 194 | const char *mediametrics_readable(mediametrics_handle_t handle) { |
Ray Essick | ae88dd3 | 2019-12-17 20:14:05 -0800 | [diff] [blame] | 195 | Item *item = (Item *) handle; |
Ray Essick | 2014732 | 2018-11-17 09:08:39 -0800 | [diff] [blame] | 196 | if (item == NULL) return ""; |
| 197 | return item->toCString(); |
| 198 | } |
| 199 | |
| 200 | int32_t mediametrics_count(mediametrics_handle_t handle) { |
Ray Essick | ae88dd3 | 2019-12-17 20:14:05 -0800 | [diff] [blame] | 201 | Item *item = (Item *) handle; |
Ray Essick | 2014732 | 2018-11-17 09:08:39 -0800 | [diff] [blame] | 202 | if (item == NULL) return 0; |
| 203 | return item->count(); |
| 204 | } |
| 205 | |
| 206 | bool mediametrics_isEnabled() { |
| 207 | // static, so doesn't need an instance |
Ray Essick | ae88dd3 | 2019-12-17 20:14:05 -0800 | [diff] [blame] | 208 | return Item::isEnabled(); |
Ray Essick | 2014732 | 2018-11-17 09:08:39 -0800 | [diff] [blame] | 209 | } |
| 210 | |
Ray Essick | ba8c484 | 2019-01-18 11:35:33 -0800 | [diff] [blame] | 211 | bool mediametrics_getAttributes(mediametrics_handle_t handle, char **buffer, size_t *length) { |
Ray Essick | ae88dd3 | 2019-12-17 20:14:05 -0800 | [diff] [blame] | 212 | Item *item = (Item *) handle; |
Ray Essick | ba8c484 | 2019-01-18 11:35:33 -0800 | [diff] [blame] | 213 | if (item == NULL) return false; |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 214 | return item->writeToByteString(buffer, length) == android::NO_ERROR; |
Ray Essick | ba8c484 | 2019-01-18 11:35:33 -0800 | [diff] [blame] | 215 | |
Ray Essick | 2014732 | 2018-11-17 09:08:39 -0800 | [diff] [blame] | 216 | } |