blob: 61f0a68620f8d80234f40d99fe32662b9126c4ae [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
22#include <media/IDataSource.h>
23
24#include <binder/IMemory.h>
25#include <binder/Parcel.h>
26#include <media/stagefright/foundation/ADebug.h>
27
28namespace android {
29
30enum {
31 GET_IMEMORY = IBinder::FIRST_CALL_TRANSACTION,
32 READ_AT,
33 GET_SIZE,
34 CLOSE,
Wei Jia10551fc2016-01-27 14:26:51 -080035 GET_FLAGS,
Marco Nelissen69d3d8a2016-03-07 13:20:01 -080036 TO_STRING,
Chris Watkins99f31602015-03-20 13:06:33 -070037};
38
39struct BpDataSource : public BpInterface<IDataSource> {
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -070040 explicit BpDataSource(const sp<IBinder>& impl)
41 : BpInterface<IDataSource>(impl) {}
Chris Watkins99f31602015-03-20 13:06:33 -070042
43 virtual sp<IMemory> getIMemory() {
44 Parcel data, reply;
45 data.writeInterfaceToken(IDataSource::getInterfaceDescriptor());
46 remote()->transact(GET_IMEMORY, data, &reply);
47 sp<IBinder> binder = reply.readStrongBinder();
48 return interface_cast<IMemory>(binder);
49 }
50
51 virtual ssize_t readAt(off64_t offset, size_t size) {
52 Parcel data, reply;
53 data.writeInterfaceToken(IDataSource::getInterfaceDescriptor());
54 data.writeInt64(offset);
55 data.writeInt64(size);
Wei Jiaff1fb4d2017-02-14 17:07:24 -080056 status_t err = remote()->transact(READ_AT, data, &reply);
57 if (err != OK) {
58 return err;
59 }
60 int64_t value = 0;
61 err = reply.readInt64(&value);
62 if (err != OK) {
63 return err;
64 }
65 return (ssize_t)value;
Chris Watkins99f31602015-03-20 13:06:33 -070066 }
67
68 virtual status_t getSize(off64_t* size) {
69 Parcel data, reply;
70 data.writeInterfaceToken(IDataSource::getInterfaceDescriptor());
71 remote()->transact(GET_SIZE, data, &reply);
72 status_t err = reply.readInt32();
73 *size = reply.readInt64();
74 return err;
75 }
76
77 virtual void close() {
78 Parcel data, reply;
79 data.writeInterfaceToken(IDataSource::getInterfaceDescriptor());
80 remote()->transact(CLOSE, data, &reply);
81 }
Wei Jia10551fc2016-01-27 14:26:51 -080082
83 virtual uint32_t getFlags() {
84 Parcel data, reply;
85 data.writeInterfaceToken(IDataSource::getInterfaceDescriptor());
86 remote()->transact(GET_FLAGS, data, &reply);
87 return reply.readUint32();
88 }
Marco Nelissen69d3d8a2016-03-07 13:20:01 -080089
90 virtual String8 toString() {
91 Parcel data, reply;
92 data.writeInterfaceToken(IDataSource::getInterfaceDescriptor());
93 remote()->transact(TO_STRING, data, &reply);
94 return reply.readString8();
95 }
Chris Watkins99f31602015-03-20 13:06:33 -070096};
97
98IMPLEMENT_META_INTERFACE(DataSource, "android.media.IDataSource");
99
100status_t BnDataSource::onTransact(
101 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
102 switch (code) {
103 case GET_IMEMORY: {
104 CHECK_INTERFACE(IDataSource, data, reply);
105 reply->writeStrongBinder(IInterface::asBinder(getIMemory()));
106 return NO_ERROR;
107 } break;
108 case READ_AT: {
109 CHECK_INTERFACE(IDataSource, data, reply);
110 off64_t offset = (off64_t) data.readInt64();
111 size_t size = (size_t) data.readInt64();
112 reply->writeInt64(readAt(offset, size));
113 return NO_ERROR;
114 } break;
115 case GET_SIZE: {
116 CHECK_INTERFACE(IDataSource, data, reply);
117 off64_t size;
118 status_t err = getSize(&size);
119 reply->writeInt32(err);
120 reply->writeInt64(size);
121 return NO_ERROR;
122 } break;
123 case CLOSE: {
124 CHECK_INTERFACE(IDataSource, data, reply);
125 close();
126 return NO_ERROR;
127 } break;
Wei Jia10551fc2016-01-27 14:26:51 -0800128 case GET_FLAGS: {
129 CHECK_INTERFACE(IDataSource, data, reply);
130 reply->writeUint32(getFlags());
131 return NO_ERROR;
132 } break;
Marco Nelissen69d3d8a2016-03-07 13:20:01 -0800133 case TO_STRING: {
134 CHECK_INTERFACE(IDataSource, data, reply);
135 reply->writeString8(toString());
136 return NO_ERROR;
137 } break;
138
Chris Watkins99f31602015-03-20 13:06:33 -0700139 default:
140 return BBinder::onTransact(code, data, reply, flags);
141 }
142}
143
144} // namespace android