blob: b5675e6e965de5e640108f562ce784a21f6e998a [file] [log] [blame]
Ray Essick3938dc62016-11-01 08:56:56 -07001/*
2 * Copyright (C) 2016 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
Ray Essickf27e9872019-12-07 06:28:46 -080017#define LOG_TAG "MediaMetrics"
Ray Essick3938dc62016-11-01 08:56:56 -070018
19#include <stdint.h>
20#include <inttypes.h>
21#include <sys/types.h>
22
23#include <binder/Parcel.h>
24#include <binder/IMemory.h>
25#include <binder/IPCThreadState.h>
Ray Essick3938dc62016-11-01 08:56:56 -070026
27#include <utils/Errors.h> // for status_t
28#include <utils/List.h>
29#include <utils/Log.h>
30#include <utils/String8.h>
31
Ray Essickf27e9872019-12-07 06:28:46 -080032#include <media/MediaMetricsItem.h>
33#include <media/IMediaMetricsService.h>
Ray Essick3938dc62016-11-01 08:56:56 -070034
Ray Essick3938dc62016-11-01 08:56:56 -070035namespace android {
36
Andy Hung1efc9c62019-12-03 13:43:33 -080037// TODO: Currently ONE_WAY transactions, make both ONE_WAY and synchronous options.
38
Ray Essick3938dc62016-11-01 08:56:56 -070039enum {
Andy Hung1efc9c62019-12-03 13:43:33 -080040 SUBMIT_ITEM = IBinder::FIRST_CALL_TRANSACTION,
41 SUBMIT_BUFFER,
Ray Essick3938dc62016-11-01 08:56:56 -070042};
43
Ray Essickf27e9872019-12-07 06:28:46 -080044class BpMediaMetricsService: public BpInterface<IMediaMetricsService>
Ray Essick3938dc62016-11-01 08:56:56 -070045{
46public:
Ray Essickf27e9872019-12-07 06:28:46 -080047 explicit BpMediaMetricsService(const sp<IBinder>& impl)
48 : BpInterface<IMediaMetricsService>(impl)
Ray Essick3938dc62016-11-01 08:56:56 -070049 {
50 }
51
Ray Essickf27e9872019-12-07 06:28:46 -080052 status_t submit(mediametrics::Item *item) override
Ray Essick3938dc62016-11-01 08:56:56 -070053 {
Andy Hunga87e69c2019-10-18 10:07:40 -070054 if (item == nullptr) {
55 return BAD_VALUE;
Ray Essick3938dc62016-11-01 08:56:56 -070056 }
Andy Hunga87e69c2019-10-18 10:07:40 -070057 ALOGV("%s: (ONEWAY) item=%s", __func__, item->toString().c_str());
Ray Essick3938dc62016-11-01 08:56:56 -070058
Andy Hunga87e69c2019-10-18 10:07:40 -070059 Parcel data;
Ray Essickf27e9872019-12-07 06:28:46 -080060 data.writeInterfaceToken(IMediaMetricsService::getInterfaceDescriptor());
Ray Essick3938dc62016-11-01 08:56:56 -070061
Andy Hung3253f2d2019-10-21 14:50:07 -070062 status_t status = item->writeToParcel(&data);
63 if (status != NO_ERROR) { // assume failure logged in item
64 return status;
65 }
66
67 status = remote()->transact(
Andy Hung1efc9c62019-12-03 13:43:33 -080068 SUBMIT_ITEM, data, nullptr /* reply */, IBinder::FLAG_ONEWAY);
69 ALOGW_IF(status != NO_ERROR, "%s: bad response from service for submit, status=%d",
70 __func__, status);
71 return status;
72 }
73
74 status_t submitBuffer(const char *buffer, size_t length) override
75 {
76 if (buffer == nullptr || length > INT32_MAX) {
77 return BAD_VALUE;
78 }
79 ALOGV("%s: (ONEWAY) length:%zu", __func__, length);
80
81 Parcel data;
Ray Essickf27e9872019-12-07 06:28:46 -080082 data.writeInterfaceToken(IMediaMetricsService::getInterfaceDescriptor());
Andy Hung1efc9c62019-12-03 13:43:33 -080083
84 status_t status = data.writeInt32(length)
85 ?: data.write((uint8_t*)buffer, length);
86 if (status != NO_ERROR) {
87 return status;
88 }
89
90 status = remote()->transact(
91 SUBMIT_BUFFER, data, nullptr /* reply */, IBinder::FLAG_ONEWAY);
Andy Hung3253f2d2019-10-21 14:50:07 -070092 ALOGW_IF(status != NO_ERROR, "%s: bad response from service for submit, status=%d",
93 __func__, status);
94 return status;
Ray Essick3938dc62016-11-01 08:56:56 -070095 }
Ray Essick3938dc62016-11-01 08:56:56 -070096};
97
Ray Essickf27e9872019-12-07 06:28:46 -080098IMPLEMENT_META_INTERFACE(MediaMetricsService, "android.media.IMediaMetricsService");
Ray Essick3938dc62016-11-01 08:56:56 -070099
100// ----------------------------------------------------------------------
101
Ray Essickf27e9872019-12-07 06:28:46 -0800102status_t BnMediaMetricsService::onTransact(
Ray Essick3938dc62016-11-01 08:56:56 -0700103 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
104{
Ray Essick3938dc62016-11-01 08:56:56 -0700105 switch (code) {
Andy Hung1efc9c62019-12-03 13:43:33 -0800106 case SUBMIT_ITEM: {
Ray Essickf27e9872019-12-07 06:28:46 -0800107 CHECK_INTERFACE(IMediaMetricsService, data, reply);
Ray Essick3938dc62016-11-01 08:56:56 -0700108
Ray Essickf27e9872019-12-07 06:28:46 -0800109 mediametrics::Item * const item = mediametrics::Item::create();
Andy Hung3253f2d2019-10-21 14:50:07 -0700110 status_t status = item->readFromParcel(data);
111 if (status != NO_ERROR) { // assume failure logged in item
112 return status;
Andy Hunga87e69c2019-10-18 10:07:40 -0700113 }
Andy Hung3253f2d2019-10-21 14:50:07 -0700114 status = submitInternal(item, true /* release */);
115 // assume failure logged by submitInternal
Andy Hunga87e69c2019-10-18 10:07:40 -0700116 return NO_ERROR;
Andy Hung1efc9c62019-12-03 13:43:33 -0800117 }
118 case SUBMIT_BUFFER: {
Ray Essickf27e9872019-12-07 06:28:46 -0800119 CHECK_INTERFACE(IMediaMetricsService, data, reply);
Andy Hung1efc9c62019-12-03 13:43:33 -0800120 int32_t length;
121 status_t status = data.readInt32(&length);
122 if (status != NO_ERROR || length <= 0) {
123 return BAD_VALUE;
124 }
125 const void *ptr = data.readInplace(length);
126 if (ptr == nullptr) {
127 return BAD_VALUE;
128 }
129 status = submitBuffer(static_cast<const char *>(ptr), length);
130 // assume failure logged by submitBuffer
131 return NO_ERROR;
132 }
Ray Essick3938dc62016-11-01 08:56:56 -0700133
Andy Hunga87e69c2019-10-18 10:07:40 -0700134 default:
135 return BBinder::onTransact(code, data, reply, flags);
Ray Essick3938dc62016-11-01 08:56:56 -0700136 }
137}
138
139// ----------------------------------------------------------------------------
140
141} // namespace android