1 /*
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
17 #define LOG_TAG "MediaMetrics"
18
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>
26
27 #include <utils/Errors.h> // for status_t
28 #include <utils/List.h>
29 #include <utils/Log.h>
30 #include <utils/String8.h>
31
32 #include <media/MediaMetricsItem.h>
33 #include <media/IMediaMetricsService.h>
34
35 namespace android {
36
37 // TODO: Currently ONE_WAY transactions, make both ONE_WAY and synchronous options.
38
39 enum {
40 SUBMIT_ITEM = IBinder::FIRST_CALL_TRANSACTION,
41 SUBMIT_BUFFER,
42 };
43
44 class BpMediaMetricsService: public BpInterface<IMediaMetricsService>
45 {
46 public:
BpMediaMetricsService(const sp<IBinder> & impl)47 explicit BpMediaMetricsService(const sp<IBinder>& impl)
48 : BpInterface<IMediaMetricsService>(impl)
49 {
50 }
51
submit(mediametrics::Item * item)52 status_t submit(mediametrics::Item *item) override
53 {
54 if (item == nullptr) {
55 return BAD_VALUE;
56 }
57 ALOGV("%s: (ONEWAY) item=%s", __func__, item->toString().c_str());
58
59 Parcel data;
60 data.writeInterfaceToken(IMediaMetricsService::getInterfaceDescriptor());
61
62 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(
68 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
submitBuffer(const char * buffer,size_t length)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;
82 data.writeInterfaceToken(IMediaMetricsService::getInterfaceDescriptor());
83
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);
92 ALOGW_IF(status != NO_ERROR, "%s: bad response from service for submit, status=%d",
93 __func__, status);
94 return status;
95 }
96 };
97
98 IMPLEMENT_META_INTERFACE(MediaMetricsService, "android.media.IMediaMetricsService");
99
100 // ----------------------------------------------------------------------
101
onTransact(uint32_t code,const Parcel & data,Parcel * reply,uint32_t flags)102 status_t BnMediaMetricsService::onTransact(
103 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
104 {
105 switch (code) {
106 case SUBMIT_ITEM: {
107 CHECK_INTERFACE(IMediaMetricsService, data, reply);
108
109 mediametrics::Item * const item = mediametrics::Item::create();
110 status_t status = item->readFromParcel(data);
111 if (status != NO_ERROR) { // assume failure logged in item
112 return status;
113 }
114 status = submitInternal(item, true /* release */);
115 // assume failure logged by submitInternal
116 return NO_ERROR;
117 }
118 case SUBMIT_BUFFER: {
119 CHECK_INTERFACE(IMediaMetricsService, data, reply);
120 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 }
133
134 default:
135 return BBinder::onTransact(code, data, reply, flags);
136 }
137 }
138
139 // ----------------------------------------------------------------------------
140
141 } // namespace android
142