blob: e96a11373b286cc49566c350cf26556205e469ce [file] [log] [blame]
Chris Watkins99f31602015-03-20 13:06:33 -07001/*
2 * Copyright 2015 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_NDEBUG 0
18#define LOG_TAG "IDataSource"
19#include <utils/Log.h>
20#include <utils/Timers.h>
21
Marco Nelissendab79b32019-11-18 08:25:47 -080022#include <android/IDataSource.h>
Chris Watkins99f31602015-03-20 13:06:33 -070023#include <binder/IMemory.h>
24#include <binder/Parcel.h>
25#include <media/stagefright/foundation/ADebug.h>
26
27namespace android {
28
29enum {
30 GET_IMEMORY = IBinder::FIRST_CALL_TRANSACTION,
31 READ_AT,
32 GET_SIZE,
33 CLOSE,
Wei Jia10551fc2016-01-27 14:26:51 -080034 GET_FLAGS,
Marco Nelissen69d3d8a2016-03-07 13:20:01 -080035 TO_STRING,
Chris Watkins99f31602015-03-20 13:06:33 -070036};
37
38struct BpDataSource : public BpInterface<IDataSource> {
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -070039 explicit BpDataSource(const sp<IBinder>& impl)
40 : BpInterface<IDataSource>(impl) {}
Chris Watkins99f31602015-03-20 13:06:33 -070041
42 virtual sp<IMemory> getIMemory() {
43 Parcel data, reply;
44 data.writeInterfaceToken(IDataSource::getInterfaceDescriptor());
45 remote()->transact(GET_IMEMORY, data, &reply);
46 sp<IBinder> binder = reply.readStrongBinder();
47 return interface_cast<IMemory>(binder);
48 }
49
50 virtual ssize_t readAt(off64_t offset, size_t size) {
51 Parcel data, reply;
52 data.writeInterfaceToken(IDataSource::getInterfaceDescriptor());
53 data.writeInt64(offset);
54 data.writeInt64(size);
Wei Jiaff1fb4d2017-02-14 17:07:24 -080055 status_t err = remote()->transact(READ_AT, data, &reply);
56 if (err != OK) {
57 return err;
58 }
59 int64_t value = 0;
60 err = reply.readInt64(&value);
61 if (err != OK) {
62 return err;
63 }
64 return (ssize_t)value;
Chris Watkins99f31602015-03-20 13:06:33 -070065 }
66
67 virtual status_t getSize(off64_t* size) {
68 Parcel data, reply;
69 data.writeInterfaceToken(IDataSource::getInterfaceDescriptor());
70 remote()->transact(GET_SIZE, data, &reply);
71 status_t err = reply.readInt32();
72 *size = reply.readInt64();
73 return err;
74 }
75
76 virtual void close() {
77 Parcel data, reply;
78 data.writeInterfaceToken(IDataSource::getInterfaceDescriptor());
79 remote()->transact(CLOSE, data, &reply);
80 }
Wei Jia10551fc2016-01-27 14:26:51 -080081
82 virtual uint32_t getFlags() {
83 Parcel data, reply;
84 data.writeInterfaceToken(IDataSource::getInterfaceDescriptor());
85 remote()->transact(GET_FLAGS, data, &reply);
86 return reply.readUint32();
87 }
Marco Nelissen69d3d8a2016-03-07 13:20:01 -080088
89 virtual String8 toString() {
90 Parcel data, reply;
91 data.writeInterfaceToken(IDataSource::getInterfaceDescriptor());
92 remote()->transact(TO_STRING, data, &reply);
93 return reply.readString8();
94 }
Chris Watkins99f31602015-03-20 13:06:33 -070095};
96
97IMPLEMENT_META_INTERFACE(DataSource, "android.media.IDataSource");
98
99status_t BnDataSource::onTransact(
100 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
101 switch (code) {
102 case GET_IMEMORY: {
103 CHECK_INTERFACE(IDataSource, data, reply);
104 reply->writeStrongBinder(IInterface::asBinder(getIMemory()));
105 return NO_ERROR;
106 } break;
107 case READ_AT: {
108 CHECK_INTERFACE(IDataSource, data, reply);
109 off64_t offset = (off64_t) data.readInt64();
110 size_t size = (size_t) data.readInt64();
111 reply->writeInt64(readAt(offset, size));
112 return NO_ERROR;
113 } break;
114 case GET_SIZE: {
115 CHECK_INTERFACE(IDataSource, data, reply);
116 off64_t size;
117 status_t err = getSize(&size);
118 reply->writeInt32(err);
119 reply->writeInt64(size);
120 return NO_ERROR;
121 } break;
122 case CLOSE: {
123 CHECK_INTERFACE(IDataSource, data, reply);
124 close();
125 return NO_ERROR;
126 } break;
Wei Jia10551fc2016-01-27 14:26:51 -0800127 case GET_FLAGS: {
128 CHECK_INTERFACE(IDataSource, data, reply);
129 reply->writeUint32(getFlags());
130 return NO_ERROR;
131 } break;
Marco Nelissen69d3d8a2016-03-07 13:20:01 -0800132 case TO_STRING: {
133 CHECK_INTERFACE(IDataSource, data, reply);
134 reply->writeString8(toString());
135 return NO_ERROR;
136 } break;
137
Chris Watkins99f31602015-03-20 13:06:33 -0700138 default:
139 return BBinder::onTransact(code, data, reply, flags);
140 }
141}
142
143} // namespace android