blob: a3c2f1a0e98e58f8d73e22a7b56996b1e7e4019c [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//
Ray Essickae88dd32019-12-17 20:14:05 -080034//
35
36using namespace android::mediametrics;
Ray Essick20147322018-11-17 09:08:39 -080037
38// manage the overall record
39mediametrics_handle_t mediametrics_create(mediametricskey_t key) {
Ray Essickae88dd32019-12-17 20:14:05 -080040 Item *item = Item::create(key);
Ray Essick20147322018-11-17 09:08:39 -080041 return (mediametrics_handle_t) item;
42}
43
44void mediametrics_delete(mediametrics_handle_t handle) {
Ray Essickae88dd32019-12-17 20:14:05 -080045 Item *item = (Item *) handle;
Ray Essick20147322018-11-17 09:08:39 -080046 if (item == NULL) return;
47 delete item;
48}
49
50mediametricskey_t mediametrics_getKey(mediametrics_handle_t handle) {
Ray Essickae88dd32019-12-17 20:14:05 -080051 Item *item = (Item *) handle;
Ray Essick20147322018-11-17 09:08:39 -080052 if (item == NULL) return NULL;
53 return strdup(item->getKey().c_str());
54}
55
56// nuplayer, et al use it when acting as proxies
57void mediametrics_setUid(mediametrics_handle_t handle, uid_t uid) {
Ray Essickae88dd32019-12-17 20:14:05 -080058 Item *item = (Item *) handle;
Ray Essick20147322018-11-17 09:08:39 -080059 if (item != NULL) item->setUid(uid);
60}
61
62// set attributes
63//
64
65void mediametrics_setInt32(mediametrics_handle_t handle, attr_t attr,
66 int32_t value) {
Ray Essickae88dd32019-12-17 20:14:05 -080067 Item *item = (Item *) handle;
Ray Essick20147322018-11-17 09:08:39 -080068 if (item != NULL) item->setInt32(attr, value);
69}
70
71void mediametrics_setInt64(mediametrics_handle_t handle, attr_t attr,
72 int64_t value) {
Ray Essickae88dd32019-12-17 20:14:05 -080073 Item *item = (Item *) handle;
Ray Essick20147322018-11-17 09:08:39 -080074 if (item != NULL) item->setInt64(attr, value);
75}
76
77void mediametrics_setDouble(mediametrics_handle_t handle, attr_t attr,
78 double value) {
Ray Essickae88dd32019-12-17 20:14:05 -080079 Item *item = (Item *) handle;
Ray Essick20147322018-11-17 09:08:39 -080080 if (item != NULL) item->setDouble(attr, value);
81}
82
83void mediametrics_setRate(mediametrics_handle_t handle, attr_t attr,
84 int64_t count, int64_t duration) {
Ray Essickae88dd32019-12-17 20:14:05 -080085 Item *item = (Item *) handle;
Ray Essick20147322018-11-17 09:08:39 -080086 if (item != NULL) item->setRate(attr, count, duration);
87}
88
89void mediametrics_setCString(mediametrics_handle_t handle, attr_t attr,
90 const char *value) {
Ray Essickae88dd32019-12-17 20:14:05 -080091 Item *item = (Item *) handle;
Ray Essick20147322018-11-17 09:08:39 -080092 if (item != NULL) item->setCString(attr, value);
93}
94
95// fused get/add/set; if attr wasn't there, it's a simple set.
96//
97
98void mediametrics_addInt32(mediametrics_handle_t handle, attr_t attr,
99 int32_t value) {
Ray Essickae88dd32019-12-17 20:14:05 -0800100 Item *item = (Item *) handle;
Ray Essick20147322018-11-17 09:08:39 -0800101 if (item != NULL) item->addInt32(attr, value);
102}
103
104void mediametrics_addInt64(mediametrics_handle_t handle, attr_t attr,
105 int64_t value) {
Ray Essickae88dd32019-12-17 20:14:05 -0800106 Item *item = (Item *) handle;
Ray Essick20147322018-11-17 09:08:39 -0800107 if (item != NULL) item->addInt64(attr, value);
108}
109
110void mediametrics_addDouble(mediametrics_handle_t handle, attr_t attr,
111 double value) {
Ray Essickae88dd32019-12-17 20:14:05 -0800112 Item *item = (Item *) handle;
Ray Essick20147322018-11-17 09:08:39 -0800113 if (item != NULL) item->addDouble(attr, value);
114}
115
116void mediametrics_addRate(mediametrics_handle_t handle, attr_t attr,
117 int64_t count, int64_t duration) {
Ray Essickae88dd32019-12-17 20:14:05 -0800118 Item *item = (Item *) handle;
Ray Essick20147322018-11-17 09:08:39 -0800119 if (item != NULL) item->addRate(attr, count, duration);
120}
121
122// find & extract values
123// return indicates whether attr exists (and thus whether value filled in)
124// NULL parameter value suppresses storage of value.
125//
126
127bool mediametrics_getInt32(mediametrics_handle_t handle, attr_t attr,
128 int32_t * value) {
Ray Essickae88dd32019-12-17 20:14:05 -0800129 Item *item = (Item *) handle;
Ray Essick20147322018-11-17 09:08:39 -0800130 if (item == NULL) return false;
131 return item->getInt32(attr, value);
132}
133
134bool mediametrics_getInt64(mediametrics_handle_t handle, attr_t attr,
135 int64_t * value) {
Ray Essickae88dd32019-12-17 20:14:05 -0800136 Item *item = (Item *) handle;
Ray Essick20147322018-11-17 09:08:39 -0800137 if (item == NULL) return false;
138 return item->getInt64(attr, value);
139}
140
141bool mediametrics_getDouble(mediametrics_handle_t handle, attr_t attr,
142 double *value) {
Ray Essickae88dd32019-12-17 20:14:05 -0800143 Item *item = (Item *) handle;
Ray Essick20147322018-11-17 09:08:39 -0800144 if (item == NULL) return false;
145 return item->getDouble(attr, value);
146}
147
148bool mediametrics_getRate(mediametrics_handle_t handle, attr_t attr,
149 int64_t * count, int64_t * duration, double *rate) {
Ray Essickae88dd32019-12-17 20:14:05 -0800150 Item *item = (Item *) handle;
Ray Essick20147322018-11-17 09:08:39 -0800151 if (item == NULL) return false;
152 return item->getRate(attr, count, duration, rate);
153}
154
155// NB: caller owns the string that comes back, is responsible for freeing it
156bool mediametrics_getCString(mediametrics_handle_t handle, attr_t attr,
157 char **value) {
Ray Essickae88dd32019-12-17 20:14:05 -0800158 Item *item = (Item *) handle;
Ray Essick20147322018-11-17 09:08:39 -0800159 if (item == NULL) return false;
160
161 return item->getCString(attr, value);
162}
163
164// to release strings returned via getCString()
165void mediametrics_freeCString(char *value) {
166 free(value);
167}
168
169bool mediametrics_selfRecord(mediametrics_handle_t handle) {
Ray Essickae88dd32019-12-17 20:14:05 -0800170 Item *item = (Item *) handle;
Ray Essick20147322018-11-17 09:08:39 -0800171 if (item == NULL) return false;
172 return item->selfrecord();
173}
174
Ray Essickbf536ac2019-08-26 11:04:28 -0700175mediametrics_handle_t mediametrics_dup(mediametrics_handle_t handle) {
Ray Essickae88dd32019-12-17 20:14:05 -0800176 Item *item = (Item *) handle;
177 if (item == NULL) return Item::convert(item);
178 return Item::convert(item->dup());
Ray Essickbf536ac2019-08-26 11:04:28 -0700179}
Ray Essick20147322018-11-17 09:08:39 -0800180
181const char *mediametrics_readable(mediametrics_handle_t handle) {
Ray Essickae88dd32019-12-17 20:14:05 -0800182 Item *item = (Item *) handle;
Ray Essick20147322018-11-17 09:08:39 -0800183 if (item == NULL) return "";
184 return item->toCString();
185}
186
187int32_t mediametrics_count(mediametrics_handle_t handle) {
Ray Essickae88dd32019-12-17 20:14:05 -0800188 Item *item = (Item *) handle;
Ray Essick20147322018-11-17 09:08:39 -0800189 if (item == NULL) return 0;
190 return item->count();
191}
192
193bool mediametrics_isEnabled() {
194 // static, so doesn't need an instance
Ray Essickae88dd32019-12-17 20:14:05 -0800195 return Item::isEnabled();
Ray Essick20147322018-11-17 09:08:39 -0800196}
197
Ray Essickba8c4842019-01-18 11:35:33 -0800198bool mediametrics_getAttributes(mediametrics_handle_t handle, char **buffer, size_t *length) {
Ray Essickae88dd32019-12-17 20:14:05 -0800199 Item *item = (Item *) handle;
Ray Essickba8c4842019-01-18 11:35:33 -0800200 if (item == NULL) return false;
Andy Hung3253f2d2019-10-21 14:50:07 -0700201 return item->writeToByteString(buffer, length) == android::NO_ERROR;
Ray Essickba8c4842019-01-18 11:35:33 -0800202
Ray Essick20147322018-11-17 09:08:39 -0800203}