blob: 4aceac5ea2de3a38ec846e462009921b4393f72c [file] [log] [blame]
Ray Essick20147322018-11-17 09:08:39 -08001/*
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 Essickf27e9872019-12-07 06:28:46 -080024#include <media/MediaMetricsItem.h>
Ray Essick20147322018-11-17 09:08:39 -080025#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
36mediametrics_handle_t mediametrics_create(mediametricskey_t key) {
Ray Essickf27e9872019-12-07 06:28:46 -080037 android::mediametrics::Item *item = android::mediametrics::Item::create(key);
Ray Essick20147322018-11-17 09:08:39 -080038 return (mediametrics_handle_t) item;
39}
40
41void mediametrics_delete(mediametrics_handle_t handle) {
Ray Essickf27e9872019-12-07 06:28:46 -080042 android::mediametrics::Item *item = (android::mediametrics::Item *) handle;
Ray Essick20147322018-11-17 09:08:39 -080043 if (item == NULL) return;
44 delete item;
45}
46
47mediametricskey_t mediametrics_getKey(mediametrics_handle_t handle) {
Ray Essickf27e9872019-12-07 06:28:46 -080048 android::mediametrics::Item *item = (android::mediametrics::Item *) handle;
Ray Essick20147322018-11-17 09:08:39 -080049 if (item == NULL) return NULL;
50 return strdup(item->getKey().c_str());
51}
52
53// nuplayer, et al use it when acting as proxies
54void mediametrics_setUid(mediametrics_handle_t handle, uid_t uid) {
Ray Essickf27e9872019-12-07 06:28:46 -080055 android::mediametrics::Item *item = (android::mediametrics::Item *) handle;
Ray Essick20147322018-11-17 09:08:39 -080056 if (item != NULL) item->setUid(uid);
57}
58
59// set attributes
60//
61
62void mediametrics_setInt32(mediametrics_handle_t handle, attr_t attr,
63 int32_t value) {
Ray Essickf27e9872019-12-07 06:28:46 -080064 android::mediametrics::Item *item = (android::mediametrics::Item *) handle;
Ray Essick20147322018-11-17 09:08:39 -080065 if (item != NULL) item->setInt32(attr, value);
66}
67
68void mediametrics_setInt64(mediametrics_handle_t handle, attr_t attr,
69 int64_t value) {
Ray Essickf27e9872019-12-07 06:28:46 -080070 android::mediametrics::Item *item = (android::mediametrics::Item *) handle;
Ray Essick20147322018-11-17 09:08:39 -080071 if (item != NULL) item->setInt64(attr, value);
72}
73
74void mediametrics_setDouble(mediametrics_handle_t handle, attr_t attr,
75 double value) {
Ray Essickf27e9872019-12-07 06:28:46 -080076 android::mediametrics::Item *item = (android::mediametrics::Item *) handle;
Ray Essick20147322018-11-17 09:08:39 -080077 if (item != NULL) item->setDouble(attr, value);
78}
79
80void mediametrics_setRate(mediametrics_handle_t handle, attr_t attr,
81 int64_t count, int64_t duration) {
Ray Essickf27e9872019-12-07 06:28:46 -080082 android::mediametrics::Item *item = (android::mediametrics::Item *) handle;
Ray Essick20147322018-11-17 09:08:39 -080083 if (item != NULL) item->setRate(attr, count, duration);
84}
85
86void mediametrics_setCString(mediametrics_handle_t handle, attr_t attr,
87 const char *value) {
Ray Essickf27e9872019-12-07 06:28:46 -080088 android::mediametrics::Item *item = (android::mediametrics::Item *) handle;
Ray Essick20147322018-11-17 09:08:39 -080089 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
95void mediametrics_addInt32(mediametrics_handle_t handle, attr_t attr,
96 int32_t value) {
Ray Essickf27e9872019-12-07 06:28:46 -080097 android::mediametrics::Item *item = (android::mediametrics::Item *) handle;
Ray Essick20147322018-11-17 09:08:39 -080098 if (item != NULL) item->addInt32(attr, value);
99}
100
101void mediametrics_addInt64(mediametrics_handle_t handle, attr_t attr,
102 int64_t value) {
Ray Essickf27e9872019-12-07 06:28:46 -0800103 android::mediametrics::Item *item = (android::mediametrics::Item *) handle;
Ray Essick20147322018-11-17 09:08:39 -0800104 if (item != NULL) item->addInt64(attr, value);
105}
106
107void mediametrics_addDouble(mediametrics_handle_t handle, attr_t attr,
108 double value) {
Ray Essickf27e9872019-12-07 06:28:46 -0800109 android::mediametrics::Item *item = (android::mediametrics::Item *) handle;
Ray Essick20147322018-11-17 09:08:39 -0800110 if (item != NULL) item->addDouble(attr, value);
111}
112
113void mediametrics_addRate(mediametrics_handle_t handle, attr_t attr,
114 int64_t count, int64_t duration) {
Ray Essickf27e9872019-12-07 06:28:46 -0800115 android::mediametrics::Item *item = (android::mediametrics::Item *) handle;
Ray Essick20147322018-11-17 09:08:39 -0800116 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
124bool mediametrics_getInt32(mediametrics_handle_t handle, attr_t attr,
125 int32_t * value) {
Ray Essickf27e9872019-12-07 06:28:46 -0800126 android::mediametrics::Item *item = (android::mediametrics::Item *) handle;
Ray Essick20147322018-11-17 09:08:39 -0800127 if (item == NULL) return false;
128 return item->getInt32(attr, value);
129}
130
131bool mediametrics_getInt64(mediametrics_handle_t handle, attr_t attr,
132 int64_t * value) {
Ray Essickf27e9872019-12-07 06:28:46 -0800133 android::mediametrics::Item *item = (android::mediametrics::Item *) handle;
Ray Essick20147322018-11-17 09:08:39 -0800134 if (item == NULL) return false;
135 return item->getInt64(attr, value);
136}
137
138bool mediametrics_getDouble(mediametrics_handle_t handle, attr_t attr,
139 double *value) {
Ray Essickf27e9872019-12-07 06:28:46 -0800140 android::mediametrics::Item *item = (android::mediametrics::Item *) handle;
Ray Essick20147322018-11-17 09:08:39 -0800141 if (item == NULL) return false;
142 return item->getDouble(attr, value);
143}
144
145bool mediametrics_getRate(mediametrics_handle_t handle, attr_t attr,
146 int64_t * count, int64_t * duration, double *rate) {
Ray Essickf27e9872019-12-07 06:28:46 -0800147 android::mediametrics::Item *item = (android::mediametrics::Item *) handle;
Ray Essick20147322018-11-17 09:08:39 -0800148 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
153bool mediametrics_getCString(mediametrics_handle_t handle, attr_t attr,
154 char **value) {
Ray Essickf27e9872019-12-07 06:28:46 -0800155 android::mediametrics::Item *item = (android::mediametrics::Item *) handle;
Ray Essick20147322018-11-17 09:08:39 -0800156 if (item == NULL) return false;
157
158 return item->getCString(attr, value);
159}
160
161// to release strings returned via getCString()
162void mediametrics_freeCString(char *value) {
163 free(value);
164}
165
166bool mediametrics_selfRecord(mediametrics_handle_t handle) {
Ray Essickf27e9872019-12-07 06:28:46 -0800167 android::mediametrics::Item *item = (android::mediametrics::Item *) handle;
Ray Essick20147322018-11-17 09:08:39 -0800168 if (item == NULL) return false;
169 return item->selfrecord();
170}
171
Ray Essickbf536ac2019-08-26 11:04:28 -0700172mediametrics_handle_t mediametrics_dup(mediametrics_handle_t handle) {
Ray Essickf27e9872019-12-07 06:28:46 -0800173 android::mediametrics::Item *item = (android::mediametrics::Item *) handle;
174 if (item == NULL) return android::mediametrics::Item::convert(item);
175 return android::mediametrics::Item::convert(item->dup());
Ray Essickbf536ac2019-08-26 11:04:28 -0700176}
Ray Essick20147322018-11-17 09:08:39 -0800177
178const char *mediametrics_readable(mediametrics_handle_t handle) {
Ray Essickf27e9872019-12-07 06:28:46 -0800179 android::mediametrics::Item *item = (android::mediametrics::Item *) handle;
Ray Essick20147322018-11-17 09:08:39 -0800180 if (item == NULL) return "";
181 return item->toCString();
182}
183
184int32_t mediametrics_count(mediametrics_handle_t handle) {
Ray Essickf27e9872019-12-07 06:28:46 -0800185 android::mediametrics::Item *item = (android::mediametrics::Item *) handle;
Ray Essick20147322018-11-17 09:08:39 -0800186 if (item == NULL) return 0;
187 return item->count();
188}
189
190bool mediametrics_isEnabled() {
191 // static, so doesn't need an instance
Ray Essickf27e9872019-12-07 06:28:46 -0800192 return android::mediametrics::Item::isEnabled();
Ray Essick20147322018-11-17 09:08:39 -0800193}
194
Ray Essickba8c4842019-01-18 11:35:33 -0800195bool mediametrics_getAttributes(mediametrics_handle_t handle, char **buffer, size_t *length) {
Ray Essickf27e9872019-12-07 06:28:46 -0800196 android::mediametrics::Item *item = (android::mediametrics::Item *) handle;
Ray Essickba8c4842019-01-18 11:35:33 -0800197 if (item == NULL) return false;
Andy Hung3253f2d2019-10-21 14:50:07 -0700198 return item->writeToByteString(buffer, length) == android::NO_ERROR;
Ray Essickba8c4842019-01-18 11:35:33 -0800199
Ray Essick20147322018-11-17 09:08:39 -0800200}