Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 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 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 17 | #define LOG_TAG "MediaMetrics" |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 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> |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 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 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 32 | #include <media/MediaMetricsItem.h> |
| 33 | #include <media/IMediaMetricsService.h> |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 34 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 35 | namespace android { |
| 36 | |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 37 | // TODO: Currently ONE_WAY transactions, make both ONE_WAY and synchronous options. |
| 38 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 39 | enum { |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 40 | SUBMIT_ITEM = IBinder::FIRST_CALL_TRANSACTION, |
| 41 | SUBMIT_BUFFER, |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 42 | }; |
| 43 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 44 | class BpMediaMetricsService: public BpInterface<IMediaMetricsService> |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 45 | { |
| 46 | public: |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 47 | explicit BpMediaMetricsService(const sp<IBinder>& impl) |
| 48 | : BpInterface<IMediaMetricsService>(impl) |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 49 | { |
| 50 | } |
| 51 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 52 | status_t submit(mediametrics::Item *item) override |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 53 | { |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 54 | if (item == nullptr) { |
| 55 | return BAD_VALUE; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 56 | } |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 57 | ALOGV("%s: (ONEWAY) item=%s", __func__, item->toString().c_str()); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 58 | |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 59 | Parcel data; |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 60 | data.writeInterfaceToken(IMediaMetricsService::getInterfaceDescriptor()); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 61 | |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 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( |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 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 | |
| 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 Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 82 | data.writeInterfaceToken(IMediaMetricsService::getInterfaceDescriptor()); |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 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); |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 92 | ALOGW_IF(status != NO_ERROR, "%s: bad response from service for submit, status=%d", |
| 93 | __func__, status); |
| 94 | return status; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 95 | } |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 96 | }; |
| 97 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 98 | IMPLEMENT_META_INTERFACE(MediaMetricsService, "android.media.IMediaMetricsService"); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 99 | |
| 100 | // ---------------------------------------------------------------------- |
| 101 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 102 | status_t BnMediaMetricsService::onTransact( |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 103 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 104 | { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 105 | switch (code) { |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 106 | case SUBMIT_ITEM: { |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 107 | CHECK_INTERFACE(IMediaMetricsService, data, reply); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 108 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 109 | mediametrics::Item * const item = mediametrics::Item::create(); |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 110 | status_t status = item->readFromParcel(data); |
| 111 | if (status != NO_ERROR) { // assume failure logged in item |
| 112 | return status; |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 113 | } |
Andy Hung | 3253f2d | 2019-10-21 14:50:07 -0700 | [diff] [blame] | 114 | status = submitInternal(item, true /* release */); |
| 115 | // assume failure logged by submitInternal |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 116 | return NO_ERROR; |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 117 | } |
| 118 | case SUBMIT_BUFFER: { |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 119 | CHECK_INTERFACE(IMediaMetricsService, data, reply); |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 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 | } |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 133 | |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 134 | default: |
| 135 | return BBinder::onTransact(code, data, reply, flags); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
| 139 | // ---------------------------------------------------------------------------- |
| 140 | |
| 141 | } // namespace android |