1 /*
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 <android/IDataSource.h>
23 #include <binder/IMemory.h>
24 #include <binder/Parcel.h>
25 #include <media/stagefright/foundation/ADebug.h>
26 
27 namespace android {
28 
29 enum {
30     GET_IMEMORY = IBinder::FIRST_CALL_TRANSACTION,
31     READ_AT,
32     GET_SIZE,
33     CLOSE,
34     GET_FLAGS,
35     TO_STRING,
36 };
37 
38 struct BpDataSource : public BpInterface<IDataSource> {
BpDataSourceandroid::BpDataSource39     explicit BpDataSource(const sp<IBinder>& impl)
40         : BpInterface<IDataSource>(impl) {}
41 
getIMemoryandroid::BpDataSource42     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 
readAtandroid::BpDataSource50     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);
55         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;
65     }
66 
getSizeandroid::BpDataSource67     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 
closeandroid::BpDataSource76     virtual void close() {
77         Parcel data, reply;
78         data.writeInterfaceToken(IDataSource::getInterfaceDescriptor());
79         remote()->transact(CLOSE, data, &reply);
80     }
81 
getFlagsandroid::BpDataSource82     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     }
88 
toStringandroid::BpDataSource89     virtual String8 toString() {
90         Parcel data, reply;
91         data.writeInterfaceToken(IDataSource::getInterfaceDescriptor());
92         remote()->transact(TO_STRING, data, &reply);
93         return reply.readString8();
94     }
95 };
96 
97 IMPLEMENT_META_INTERFACE(DataSource, "android.media.IDataSource");
98 
onTransact(uint32_t code,const Parcel & data,Parcel * reply,uint32_t flags)99 status_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;
127         case GET_FLAGS: {
128             CHECK_INTERFACE(IDataSource, data, reply);
129             reply->writeUint32(getFlags());
130             return NO_ERROR;
131         } break;
132         case TO_STRING: {
133             CHECK_INTERFACE(IDataSource, data, reply);
134             reply->writeString8(toString());
135             return NO_ERROR;
136         } break;
137 
138         default:
139             return BBinder::onTransact(code, data, reply, flags);
140     }
141 }
142 
143 }  // namespace android
144