blob: a969c7db6e03ba4b347d3df71e139e68905d1be9 [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,
35};
36
37struct BpDataSource : public BpInterface<IDataSource> {
38 BpDataSource(const sp<IBinder>& impl) : BpInterface<IDataSource>(impl) {}
39
40 virtual sp<IMemory> getIMemory() {
41 Parcel data, reply;
42 data.writeInterfaceToken(IDataSource::getInterfaceDescriptor());
43 remote()->transact(GET_IMEMORY, data, &reply);
44 sp<IBinder> binder = reply.readStrongBinder();
45 return interface_cast<IMemory>(binder);
46 }
47
48 virtual ssize_t readAt(off64_t offset, size_t size) {
49 Parcel data, reply;
50 data.writeInterfaceToken(IDataSource::getInterfaceDescriptor());
51 data.writeInt64(offset);
52 data.writeInt64(size);
Wei Jia0addb442017-02-14 17:07:24 -080053 status_t err = remote()->transact(READ_AT, data, &reply);
54 if (err != OK) {
55 return err;
56 }
57 int64_t value = 0;
58 err = reply.readInt64(&value);
59 if (err != OK) {
60 return err;
61 }
62 return (ssize_t)value;
Chris Watkins99f31602015-03-20 13:06:33 -070063 }
64
65 virtual status_t getSize(off64_t* size) {
66 Parcel data, reply;
67 data.writeInterfaceToken(IDataSource::getInterfaceDescriptor());
68 remote()->transact(GET_SIZE, data, &reply);
69 status_t err = reply.readInt32();
70 *size = reply.readInt64();
71 return err;
72 }
73
74 virtual void close() {
75 Parcel data, reply;
76 data.writeInterfaceToken(IDataSource::getInterfaceDescriptor());
77 remote()->transact(CLOSE, data, &reply);
78 }
79};
80
81IMPLEMENT_META_INTERFACE(DataSource, "android.media.IDataSource");
82
83status_t BnDataSource::onTransact(
84 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
85 switch (code) {
86 case GET_IMEMORY: {
87 CHECK_INTERFACE(IDataSource, data, reply);
88 reply->writeStrongBinder(IInterface::asBinder(getIMemory()));
89 return NO_ERROR;
90 } break;
91 case READ_AT: {
92 CHECK_INTERFACE(IDataSource, data, reply);
93 off64_t offset = (off64_t) data.readInt64();
94 size_t size = (size_t) data.readInt64();
95 reply->writeInt64(readAt(offset, size));
96 return NO_ERROR;
97 } break;
98 case GET_SIZE: {
99 CHECK_INTERFACE(IDataSource, data, reply);
100 off64_t size;
101 status_t err = getSize(&size);
102 reply->writeInt32(err);
103 reply->writeInt64(size);
104 return NO_ERROR;
105 } break;
106 case CLOSE: {
107 CHECK_INTERFACE(IDataSource, data, reply);
108 close();
109 return NO_ERROR;
110 } break;
111 default:
112 return BBinder::onTransact(code, data, reply, flags);
113 }
114}
115
116} // namespace android