1 /*
2 **
3 ** Copyright 2007, The Android Open Source Project
4 **
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
8 **
9 ** http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 */
17
18 #define LOG_TAG "IMediaExtractorService"
19 //#define LOG_NDEBUG 0
20
21 #include <utils/Log.h>
22 #include <stdint.h>
23 #include <sys/types.h>
24 #include <binder/Parcel.h>
25 #include <media/IMediaExtractorService.h>
26
27 namespace android {
28
29 enum {
30 MAKE_EXTRACTOR = IBinder::FIRST_CALL_TRANSACTION,
31 MAKE_IDATA_SOURCE_FD,
32 GET_SUPPORTED_TYPES,
33 };
34
35 class BpMediaExtractorService : public BpInterface<IMediaExtractorService>
36 {
37 public:
BpMediaExtractorService(const sp<IBinder> & impl)38 explicit BpMediaExtractorService(const sp<IBinder>& impl)
39 : BpInterface<IMediaExtractorService>(impl)
40 {
41 }
42
makeExtractor(const sp<IDataSource> & source,const char * mime)43 virtual sp<IMediaExtractor> makeExtractor(const sp<IDataSource> &source, const char *mime) {
44 Parcel data, reply;
45 data.writeInterfaceToken(IMediaExtractorService::getInterfaceDescriptor());
46 data.writeStrongBinder(IInterface::asBinder(source));
47 if (mime != NULL) {
48 data.writeCString(mime);
49 }
50 status_t ret = remote()->transact(MAKE_EXTRACTOR, data, &reply);
51 if (ret == NO_ERROR) {
52 return interface_cast<IMediaExtractor>(reply.readStrongBinder());
53 }
54 return NULL;
55 }
56
makeIDataSource(int fd,int64_t offset,int64_t length)57 virtual sp<IDataSource> makeIDataSource(int fd, int64_t offset, int64_t length)
58 {
59 Parcel data, reply;
60 data.writeInterfaceToken(IMediaExtractorService::getInterfaceDescriptor());
61 data.writeFileDescriptor(fd);
62 data.writeInt64(offset);
63 data.writeInt64(length);
64 status_t ret = remote()->transact(MAKE_IDATA_SOURCE_FD, data, &reply);
65 ALOGV("fd:%d offset:%lld length:%lld ret:%d",
66 fd, (long long)offset, (long long)length, ret);
67 if (ret == NO_ERROR) {
68 return interface_cast<IDataSource>(reply.readStrongBinder());
69 }
70 return nullptr;
71 }
72
getSupportedTypes()73 virtual std::unordered_set<std::string> getSupportedTypes() {
74 std::unordered_set<std::string> supportedTypes;
75 Parcel data, reply;
76 data.writeInterfaceToken(IMediaExtractorService::getInterfaceDescriptor());
77 status_t ret = remote()->transact(GET_SUPPORTED_TYPES, data, &reply);
78 if (ret == NO_ERROR) {
79 // process reply
80 while(true) {
81 const char *ext = reply.readCString();
82 if (!ext) {
83 break;
84 }
85 supportedTypes.insert(std::string(ext));
86 }
87 }
88 return supportedTypes;
89 }
90 };
91
92 IMPLEMENT_META_INTERFACE(MediaExtractorService, "android.media.IMediaExtractorService");
93
94 // ----------------------------------------------------------------------
95
onTransact(uint32_t code,const Parcel & data,Parcel * reply,uint32_t flags)96 status_t BnMediaExtractorService::onTransact(
97 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
98 {
99 switch (code) {
100
101 case MAKE_EXTRACTOR: {
102 CHECK_INTERFACE(IMediaExtractorService, data, reply);
103 sp<IBinder> b;
104 status_t ret = data.readStrongBinder(&b);
105 if (ret != NO_ERROR || b == NULL) {
106 ALOGE("Error reading source from parcel");
107 return ret;
108 }
109 // If we make an extractor through Binder, enabled shared memory
110 // for MediaBuffers for this process.
111 MediaBuffer::useSharedMemory();
112 sp<IDataSource> source = interface_cast<IDataSource>(b);
113 const char *mime = data.readCString();
114 sp<IMediaExtractor> ex = makeExtractor(source, mime);
115 reply->writeStrongBinder(IInterface::asBinder(ex));
116 return NO_ERROR;
117 }
118
119 case MAKE_IDATA_SOURCE_FD: {
120 CHECK_INTERFACE(IMediaExtractorService, data, reply);
121 const int fd = dup(data.readFileDescriptor()); // -1 fd checked in makeIDataSource
122 const int64_t offset = data.readInt64();
123 const int64_t length = data.readInt64();
124 ALOGV("fd %d offset%lld length:%lld", fd, (long long)offset, (long long)length);
125 sp<IDataSource> source = makeIDataSource(fd, offset, length);
126 reply->writeStrongBinder(IInterface::asBinder(source));
127 // The FileSource closes the descriptor, so if it is not created
128 // we need to close the descriptor explicitly.
129 if (source.get() == nullptr && fd != -1) {
130 close(fd);
131 }
132 return NO_ERROR;
133 }
134
135 case GET_SUPPORTED_TYPES:
136 {
137 CHECK_INTERFACE(IMediaExtractorService, data, reply);
138 std::unordered_set<std::string> supportedTypes = getSupportedTypes();
139 for (auto it = supportedTypes.begin(); it != supportedTypes.end(); ++it) {
140 reply->writeCString((*it).c_str());
141 }
142 return NO_ERROR;
143 }
144 default:
145 return BBinder::onTransact(code, data, reply, flags);
146 }
147 }
148
149 // ----------------------------------------------------------------------------
150
151 } // namespace android
152