blob: 340cf19c0b7f2b6d04d91b07768a8ac8edd30339 [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
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
48namespace android {
49
50enum {
51 GENERATE_UNIQUE_SESSIONID = IBinder::FIRST_CALL_TRANSACTION,
52 SUBMIT_ITEM,
Ray Essick3938dc62016-11-01 08:56:56 -070053};
54
55class BpMediaAnalyticsService: public BpInterface<IMediaAnalyticsService>
56{
57public:
58 explicit BpMediaAnalyticsService(const sp<IBinder>& impl)
59 : BpInterface<IMediaAnalyticsService>(impl)
60 {
61 }
62
63 virtual MediaAnalyticsItem::SessionID_t generateUniqueSessionID() {
64 Parcel data, reply;
65 status_t err;
66 MediaAnalyticsItem::SessionID_t sessionid =
67 MediaAnalyticsItem::SessionIDInvalid;
68
69 data.writeInterfaceToken(IMediaAnalyticsService::getInterfaceDescriptor());
70 err = remote()->transact(GENERATE_UNIQUE_SESSIONID, data, &reply);
71 if (err != NO_ERROR) {
72 ALOGW("bad response from service");
73 return MediaAnalyticsItem::SessionIDInvalid;
74 }
75 sessionid = reply.readInt64();
76 if (DEBUGGING_RETURNS) {
77 ALOGD("the caller gets a sessionid of %" PRId64 " back", sessionid);
78 }
79 return sessionid;
80 }
81
Ray Essickb5fac8e2016-12-12 11:33:56 -080082 virtual MediaAnalyticsItem::SessionID_t submit(MediaAnalyticsItem *item, bool forcenew)
Ray Essick3938dc62016-11-01 08:56:56 -070083 {
84 // have this record submit itself
85 // this will be a binder call with appropriate timing
86 // return value is the uuid that the system generated for it.
87 // the return value 0 and -1 are reserved.
88 // -1 to indicate that there was a problem recording...
89
90 Parcel data, reply;
91 status_t err;
92
93 if (item == NULL) {
94 return MediaAnalyticsItem::SessionIDInvalid;
95 }
96
97 data.writeInterfaceToken(IMediaAnalyticsService::getInterfaceDescriptor());
98 if(DEBUGGING_FLOW) {
99 ALOGD("client offers record: %s", item->toString().c_str());
100 }
101 data.writeBool(forcenew);
102 item->writeToParcel(&data);
103
104 err = remote()->transact(SUBMIT_ITEM, data, &reply);
105 if (err != NO_ERROR) {
106 return MediaAnalyticsItem::SessionIDInvalid;
107 }
108
109 // get an answer out of 'reply'
110 int64_t sessionid = reply.readInt64();
111 if (DEBUGGING_RETURNS) {
112 ALOGD("the caller gets sessionid=%" PRId64 "", sessionid);
113 }
114 return sessionid;
115 }
116
Ray Essick3938dc62016-11-01 08:56:56 -0700117};
118
119IMPLEMENT_META_INTERFACE(MediaAnalyticsService, "android.media.IMediaAnalyticsService");
120
121// ----------------------------------------------------------------------
122
123status_t BnMediaAnalyticsService::onTransact(
124 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
125{
126
127
128 // get calling pid/tid
129 IPCThreadState *ipc = IPCThreadState::self();
130 int clientPid = ipc->getCallingPid();
131 // permission checking
132
133 if(DEBUGGING_FLOW) {
134 ALOGD("running in service, code %d, pid %d; called from pid %d",
135 code, getpid(), clientPid);
136 }
137
138 switch (code) {
139
140 case GENERATE_UNIQUE_SESSIONID: {
141 CHECK_INTERFACE(IMediaAnalyticsService, data, reply);
142
143 MediaAnalyticsItem::SessionID_t sessionid = generateUniqueSessionID();
144 reply->writeInt64(sessionid);
145
146 return NO_ERROR;
147 } break;
148
149 case SUBMIT_ITEM: {
150 CHECK_INTERFACE(IMediaAnalyticsService, data, reply);
151
152 bool forcenew;
Ray Essickb5fac8e2016-12-12 11:33:56 -0800153 MediaAnalyticsItem *item = new MediaAnalyticsItem;
Ray Essick3938dc62016-11-01 08:56:56 -0700154
155 data.readBool(&forcenew);
156 item->readFromParcel(data);
157
158 item->setPid(clientPid);
159
Ray Essickb5fac8e2016-12-12 11:33:56 -0800160 // submit() takes over ownership of 'item'
Ray Essick3938dc62016-11-01 08:56:56 -0700161 MediaAnalyticsItem::SessionID_t sessionid = submit(item, forcenew);
162 reply->writeInt64(sessionid);
163
164 return NO_ERROR;
165 } break;
166
Ray Essick3938dc62016-11-01 08:56:56 -0700167 default:
168 return BBinder::onTransact(code, data, reply, flags);
169 }
170}
171
172// ----------------------------------------------------------------------------
173
174} // namespace android