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 | |
| 17 | #define LOG_TAG "MediaAnalytics" |
| 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 | #include <media/IHDCP.h> |
| 27 | #include <media/IMediaCodecList.h> |
| 28 | #include <media/IMediaHTTPService.h> |
| 29 | #include <media/IMediaPlayerService.h> |
| 30 | #include <media/IMediaRecorder.h> |
| 31 | #include <media/IOMX.h> |
| 32 | #include <media/IRemoteDisplay.h> |
| 33 | #include <media/IRemoteDisplayClient.h> |
| 34 | #include <media/IStreamSource.h> |
| 35 | |
| 36 | #include <utils/Errors.h> // for status_t |
| 37 | #include <utils/List.h> |
| 38 | #include <utils/Log.h> |
| 39 | #include <utils/String8.h> |
| 40 | |
| 41 | #include <media/MediaAnalyticsItem.h> |
| 42 | #include <media/IMediaAnalyticsService.h> |
| 43 | |
| 44 | #define DEBUGGING 0 |
| 45 | #define DEBUGGING_FLOW 0 |
| 46 | #define DEBUGGING_RETURNS 0 |
| 47 | |
| 48 | namespace android { |
| 49 | |
| 50 | enum { |
| 51 | GENERATE_UNIQUE_SESSIONID = IBinder::FIRST_CALL_TRANSACTION, |
| 52 | SUBMIT_ITEM, |
| 53 | GET_ITEM_LIST, |
| 54 | }; |
| 55 | |
| 56 | class BpMediaAnalyticsService: public BpInterface<IMediaAnalyticsService> |
| 57 | { |
| 58 | public: |
| 59 | explicit BpMediaAnalyticsService(const sp<IBinder>& impl) |
| 60 | : BpInterface<IMediaAnalyticsService>(impl) |
| 61 | { |
| 62 | } |
| 63 | |
| 64 | virtual MediaAnalyticsItem::SessionID_t generateUniqueSessionID() { |
| 65 | Parcel data, reply; |
| 66 | status_t err; |
| 67 | MediaAnalyticsItem::SessionID_t sessionid = |
| 68 | MediaAnalyticsItem::SessionIDInvalid; |
| 69 | |
| 70 | data.writeInterfaceToken(IMediaAnalyticsService::getInterfaceDescriptor()); |
| 71 | err = remote()->transact(GENERATE_UNIQUE_SESSIONID, data, &reply); |
| 72 | if (err != NO_ERROR) { |
| 73 | ALOGW("bad response from service"); |
| 74 | return MediaAnalyticsItem::SessionIDInvalid; |
| 75 | } |
| 76 | sessionid = reply.readInt64(); |
| 77 | if (DEBUGGING_RETURNS) { |
| 78 | ALOGD("the caller gets a sessionid of %" PRId64 " back", sessionid); |
| 79 | } |
| 80 | return sessionid; |
| 81 | } |
| 82 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 83 | virtual MediaAnalyticsItem::SessionID_t submit(MediaAnalyticsItem *item, bool forcenew) |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 84 | { |
| 85 | // have this record submit itself |
| 86 | // this will be a binder call with appropriate timing |
| 87 | // return value is the uuid that the system generated for it. |
| 88 | // the return value 0 and -1 are reserved. |
| 89 | // -1 to indicate that there was a problem recording... |
| 90 | |
| 91 | Parcel data, reply; |
| 92 | status_t err; |
| 93 | |
| 94 | if (item == NULL) { |
| 95 | return MediaAnalyticsItem::SessionIDInvalid; |
| 96 | } |
| 97 | |
| 98 | data.writeInterfaceToken(IMediaAnalyticsService::getInterfaceDescriptor()); |
| 99 | if(DEBUGGING_FLOW) { |
| 100 | ALOGD("client offers record: %s", item->toString().c_str()); |
| 101 | } |
| 102 | data.writeBool(forcenew); |
| 103 | item->writeToParcel(&data); |
| 104 | |
| 105 | err = remote()->transact(SUBMIT_ITEM, data, &reply); |
| 106 | if (err != NO_ERROR) { |
| 107 | return MediaAnalyticsItem::SessionIDInvalid; |
| 108 | } |
| 109 | |
| 110 | // get an answer out of 'reply' |
| 111 | int64_t sessionid = reply.readInt64(); |
| 112 | if (DEBUGGING_RETURNS) { |
| 113 | ALOGD("the caller gets sessionid=%" PRId64 "", sessionid); |
| 114 | } |
| 115 | return sessionid; |
| 116 | } |
| 117 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 118 | virtual List<MediaAnalyticsItem*> *getMediaAnalyticsItemList(bool finished, nsecs_t ts) |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 119 | { |
| 120 | return getMediaAnalyticsItemList(finished, ts, MediaAnalyticsItem::kKeyAny); |
| 121 | } |
| 122 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 123 | virtual List<MediaAnalyticsItem*> *getMediaAnalyticsItemList(bool finished, nsecs_t ts, MediaAnalyticsItem::Key key) |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 124 | { |
| 125 | Parcel data, reply; |
| 126 | status_t err; |
| 127 | |
| 128 | data.writeInterfaceToken(IMediaAnalyticsService::getInterfaceDescriptor()); |
| 129 | data.writeInt32(finished); |
| 130 | data.writeInt64(ts); |
| 131 | const char *str = key.c_str(); |
| 132 | if (key.empty()) { |
| 133 | str = MediaAnalyticsItem::kKeyNone.c_str(); |
| 134 | } |
| 135 | data.writeCString(str); |
| 136 | err = remote()->transact(GET_ITEM_LIST, data, &reply); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 137 | if (err != NO_ERROR) { |
| 138 | return NULL; |
| 139 | } |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 140 | |
| 141 | // read a count |
| 142 | int32_t count = reply.readInt32(); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 143 | List<MediaAnalyticsItem*> *list = NULL; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 144 | |
| 145 | if (count > 0) { |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 146 | list = new List<MediaAnalyticsItem*>(); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 147 | for (int i=0;i<count;i++) { |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 148 | MediaAnalyticsItem *item = new MediaAnalyticsItem(); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 149 | // XXX: watch for failures here |
| 150 | item->readFromParcel(reply); |
| 151 | list->push_back(item); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | return list; |
| 156 | } |
| 157 | }; |
| 158 | |
| 159 | IMPLEMENT_META_INTERFACE(MediaAnalyticsService, "android.media.IMediaAnalyticsService"); |
| 160 | |
| 161 | // ---------------------------------------------------------------------- |
| 162 | |
| 163 | status_t BnMediaAnalyticsService::onTransact( |
| 164 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 165 | { |
| 166 | |
| 167 | |
| 168 | // get calling pid/tid |
| 169 | IPCThreadState *ipc = IPCThreadState::self(); |
| 170 | int clientPid = ipc->getCallingPid(); |
| 171 | // permission checking |
| 172 | |
| 173 | if(DEBUGGING_FLOW) { |
| 174 | ALOGD("running in service, code %d, pid %d; called from pid %d", |
| 175 | code, getpid(), clientPid); |
| 176 | } |
| 177 | |
| 178 | switch (code) { |
| 179 | |
| 180 | case GENERATE_UNIQUE_SESSIONID: { |
| 181 | CHECK_INTERFACE(IMediaAnalyticsService, data, reply); |
| 182 | |
| 183 | MediaAnalyticsItem::SessionID_t sessionid = generateUniqueSessionID(); |
| 184 | reply->writeInt64(sessionid); |
| 185 | |
| 186 | return NO_ERROR; |
| 187 | } break; |
| 188 | |
| 189 | case SUBMIT_ITEM: { |
| 190 | CHECK_INTERFACE(IMediaAnalyticsService, data, reply); |
| 191 | |
| 192 | bool forcenew; |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 193 | MediaAnalyticsItem *item = new MediaAnalyticsItem; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 194 | |
| 195 | data.readBool(&forcenew); |
| 196 | item->readFromParcel(data); |
| 197 | |
| 198 | item->setPid(clientPid); |
| 199 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 200 | // submit() takes over ownership of 'item' |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 201 | MediaAnalyticsItem::SessionID_t sessionid = submit(item, forcenew); |
| 202 | reply->writeInt64(sessionid); |
| 203 | |
| 204 | return NO_ERROR; |
| 205 | } break; |
| 206 | |
| 207 | case GET_ITEM_LIST: { |
| 208 | CHECK_INTERFACE(IMediaPlayerService, data, reply); |
| 209 | // get the parameters |
| 210 | bool finished = data.readInt32(); |
| 211 | nsecs_t ts = data.readInt64(); |
| 212 | MediaAnalyticsItem::Key key = data.readCString(); |
| 213 | |
| 214 | // find the (0 or more) items |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 215 | List<MediaAnalyticsItem*> *list = getMediaAnalyticsItemList(finished, ts, key); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 216 | // encapsulate/serialize them |
| 217 | reply->writeInt32(list->size()); |
| 218 | if (list->size() > 0) { |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 219 | for (List<MediaAnalyticsItem*>::iterator it = list->begin(); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 220 | it != list->end(); it++) { |
| 221 | (*it)->writeToParcel(reply); |
| 222 | } |
| 223 | |
| 224 | |
| 225 | } |
| 226 | |
| 227 | // avoid leakiness; organized discarding of list and its contents |
| 228 | list->clear(); |
| 229 | delete list; |
| 230 | |
| 231 | return NO_ERROR; |
| 232 | } break; |
| 233 | |
| 234 | default: |
| 235 | return BBinder::onTransact(code, data, reply, flags); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | // ---------------------------------------------------------------------------- |
| 240 | |
| 241 | } // namespace android |