blob: 51ccb5a393074508865a155e885df533b9196d4a [file] [log] [blame]
Marco Nelissenb2487f02015-09-01 13:23:23 -07001/*
2 * Copyright (C) 2009 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
Marco Nelissenb65990f2015-11-09 15:39:49 -080017//#define LOG_NDEBUG 0
Marco Nelissenb2487f02015-09-01 13:23:23 -070018#define LOG_TAG "BpMediaExtractor"
19#include <utils/Log.h>
20
21#include <stdint.h>
22#include <sys/types.h>
23
Marco Nelissen69d3d8a2016-03-07 13:20:01 -080024#include <binder/IPCThreadState.h>
Marco Nelissenb2487f02015-09-01 13:23:23 -070025#include <binder/Parcel.h>
ray-cy.leeb596c342017-03-07 18:25:15 +080026#include <binder/PermissionCache.h>
Marco Nelissenb2487f02015-09-01 13:23:23 -070027#include <media/IMediaExtractor.h>
28#include <media/stagefright/MetaData.h>
29
30namespace android {
31
32enum {
33 COUNTTRACKS = IBinder::FIRST_CALL_TRANSACTION,
34 GETTRACK,
35 GETTRACKMETADATA,
36 GETMETADATA,
37 FLAGS,
Marco Nelissenb2487f02015-09-01 13:23:23 -070038 GETDRMTRACKINFO,
Chong Zhang9dbe9a52017-01-03 11:35:15 -080039 SETMEDIACAS,
Marco Nelissenb2487f02015-09-01 13:23:23 -070040 SETUID,
Ray Essickba13b7b2017-02-07 10:03:18 -080041 NAME,
Marco Nelissen2a243f02018-01-30 08:29:57 -080042 GETMETRICS
Marco Nelissenb2487f02015-09-01 13:23:23 -070043};
44
45class BpMediaExtractor : public BpInterface<IMediaExtractor> {
46public:
Chih-Hung Hsieh64a28702016-05-03 09:52:51 -070047 explicit BpMediaExtractor(const sp<IBinder>& impl)
Marco Nelissenb2487f02015-09-01 13:23:23 -070048 : BpInterface<IMediaExtractor>(impl)
49 {
50 }
51
52 virtual size_t countTracks() {
53 ALOGV("countTracks");
54 Parcel data, reply;
55 data.writeInterfaceToken(BpMediaExtractor::getInterfaceDescriptor());
56 status_t ret = remote()->transact(COUNTTRACKS, data, &reply);
57 size_t numTracks = 0;
58 if (ret == NO_ERROR) {
59 numTracks = reply.readUint32();
60 }
61 return numTracks;
62 }
63 virtual sp<IMediaSource> getTrack(size_t index) {
64 ALOGV("getTrack(%zu)", index);
65 Parcel data, reply;
66 data.writeInterfaceToken(BpMediaExtractor::getInterfaceDescriptor());
67 data.writeUint32(index);
68 status_t ret = remote()->transact(GETTRACK, data, &reply);
69 if (ret == NO_ERROR) {
70 return interface_cast<IMediaSource>(reply.readStrongBinder());
71 }
72 return NULL;
73 }
74
75 virtual sp<MetaData> getTrackMetaData(
76 size_t index, uint32_t flags) {
77 ALOGV("getTrackMetaData(%zu, %u)", index, flags);
78 Parcel data, reply;
79 data.writeInterfaceToken(BpMediaExtractor::getInterfaceDescriptor());
80 data.writeUint32(index);
81 data.writeUint32(flags);
82 status_t ret = remote()->transact(GETTRACKMETADATA, data, &reply);
83 if (ret == NO_ERROR) {
84 return MetaData::createFromParcel(reply);
85 }
86 return NULL;
87 }
88
89 virtual sp<MetaData> getMetaData() {
90 ALOGV("getMetaData");
91 Parcel data, reply;
92 data.writeInterfaceToken(BpMediaExtractor::getInterfaceDescriptor());
93 status_t ret = remote()->transact(GETMETADATA, data, &reply);
94 if (ret == NO_ERROR) {
95 return MetaData::createFromParcel(reply);
96 }
97 return NULL;
98 }
99
Ray Essickba13b7b2017-02-07 10:03:18 -0800100 virtual status_t getMetrics(Parcel * reply) {
101 Parcel data;
102 data.writeInterfaceToken(BpMediaExtractor::getInterfaceDescriptor());
103 status_t ret = remote()->transact(GETMETRICS, data, reply);
104 if (ret == NO_ERROR) {
105 return OK;
106 }
107 return UNKNOWN_ERROR;
108 }
109
Marco Nelissenb2487f02015-09-01 13:23:23 -0700110 virtual uint32_t flags() const {
111 ALOGV("flags NOT IMPLEMENTED");
112 return 0;
113 }
114
Marco Nelissenb2487f02015-09-01 13:23:23 -0700115 virtual char* getDrmTrackInfo(size_t trackID __unused, int *len __unused) {
116 ALOGV("getDrmTrackInfo NOT IMPLEMENTED");
117 return NULL;
118 }
Chong Zhang9dbe9a52017-01-03 11:35:15 -0800119
Chong Zhangd5a416a2017-05-16 11:16:34 -0700120 virtual status_t setMediaCas(const HInterfaceToken &casToken) {
Chong Zhang9dbe9a52017-01-03 11:35:15 -0800121 ALOGV("setMediaCas");
122
123 Parcel data, reply;
124 data.writeInterfaceToken(BpMediaExtractor::getInterfaceDescriptor());
Chong Zhangd5a416a2017-05-16 11:16:34 -0700125 data.writeByteVector(casToken);
Chong Zhang9dbe9a52017-01-03 11:35:15 -0800126
127 status_t err = remote()->transact(SETMEDIACAS, data, &reply);
128 if (err != NO_ERROR) {
129 return err;
130 }
131 return reply.readInt32();
132 }
133
Marco Nelissenb2487f02015-09-01 13:23:23 -0700134 virtual void setUID(uid_t uid __unused) {
135 ALOGV("setUID NOT IMPLEMENTED");
136 }
137
138 virtual const char * name() {
139 ALOGV("name NOT IMPLEMENTED");
140 return NULL;
141 }
142};
143
144IMPLEMENT_META_INTERFACE(MediaExtractor, "android.media.IMediaExtractor");
145
146#undef LOG_TAG
147#define LOG_TAG "BnMediaExtractor"
148
149status_t BnMediaExtractor::onTransact(
150 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
151{
152 switch (code) {
153 case COUNTTRACKS: {
154 ALOGV("countTracks");
155 CHECK_INTERFACE(IMediaExtractor, data, reply);
156 size_t numTracks = countTracks();
157 if (numTracks > INT32_MAX) {
158 numTracks = 0;
159 }
160 reply->writeUint32(uint32_t(numTracks));
161 return NO_ERROR;
162 }
163 case GETTRACK: {
164 ALOGV("getTrack()");
165 CHECK_INTERFACE(IMediaExtractor, data, reply);
166 uint32_t idx;
167 if (data.readUint32(&idx) == NO_ERROR) {
Marco Nelissen69d3d8a2016-03-07 13:20:01 -0800168 const sp<IMediaSource> track = getTrack(size_t(idx));
169 registerMediaSource(this, track);
170 return reply->writeStrongBinder(IInterface::asBinder(track));
Marco Nelissenb2487f02015-09-01 13:23:23 -0700171 }
172 return UNKNOWN_ERROR;
173 }
174 case GETTRACKMETADATA: {
175 ALOGV("getTrackMetaData");
176 CHECK_INTERFACE(IMediaExtractor, data, reply);
177 uint32_t idx;
178 uint32_t flags;
179 if (data.readUint32(&idx) == NO_ERROR &&
180 data.readUint32(&flags) == NO_ERROR) {
181 sp<MetaData> meta = getTrackMetaData(idx, flags);
Marco Nelissen0d138242016-10-06 15:31:52 -0700182 if (meta == NULL) {
183 return UNKNOWN_ERROR;
184 }
Marco Nelissenb2487f02015-09-01 13:23:23 -0700185 meta->writeToParcel(*reply);
186 return NO_ERROR;
187 }
188 return UNKNOWN_ERROR;
189 }
190 case GETMETADATA: {
191 ALOGV("getMetaData");
192 CHECK_INTERFACE(IMediaExtractor, data, reply);
193 sp<MetaData> meta = getMetaData();
194 if (meta != NULL) {
195 meta->writeToParcel(*reply);
196 return NO_ERROR;
197 }
198 return UNKNOWN_ERROR;
199 }
Ray Essickba13b7b2017-02-07 10:03:18 -0800200 case GETMETRICS: {
201 CHECK_INTERFACE(IMediaExtractor, data, reply);
202 status_t ret = getMetrics(reply);
203 return ret;
204 }
Chong Zhang9dbe9a52017-01-03 11:35:15 -0800205 case SETMEDIACAS: {
206 ALOGV("setMediaCas");
207 CHECK_INTERFACE(IMediaExtractor, data, reply);
208
Chong Zhangd5a416a2017-05-16 11:16:34 -0700209 HInterfaceToken casToken;
210 status_t err = data.readByteVector(&casToken);
Chong Zhang9dbe9a52017-01-03 11:35:15 -0800211 if (err != NO_ERROR) {
Chong Zhangd5a416a2017-05-16 11:16:34 -0700212 ALOGE("Error reading casToken from parcel");
Chong Zhang9dbe9a52017-01-03 11:35:15 -0800213 return err;
214 }
Chong Zhang9dbe9a52017-01-03 11:35:15 -0800215
Chong Zhangd5a416a2017-05-16 11:16:34 -0700216 reply->writeInt32(setMediaCas(casToken));
Chong Zhang9dbe9a52017-01-03 11:35:15 -0800217 return OK;
218 }
Marco Nelissenb2487f02015-09-01 13:23:23 -0700219 default:
220 return BBinder::onTransact(code, data, reply, flags);
221 }
222}
223
Marco Nelissen69d3d8a2016-03-07 13:20:01 -0800224typedef struct {
225 String8 mime;
226 String8 name;
227 String8 sourceDescription;
228 pid_t owner;
229 wp<IMediaExtractor> extractor;
230 Vector<wp<IMediaSource>> tracks;
231 Vector<String8> trackDescriptions;
232 String8 toString() const;
233} ExtractorInstance;
234
235String8 ExtractorInstance::toString() const {
236 String8 str = name;
237 str.append(" for mime ");
238 str.append(mime);
239 str.append(", source ");
240 str.append(sourceDescription);
241 str.append(String8::format(", pid %d: ", owner));
242 if (extractor.promote() == NULL) {
243 str.append("deleted\n");
244 } else {
245 str.append("active\n");
246 }
247 for (size_t i = 0; i < tracks.size(); i++) {
248 const String8 desc = trackDescriptions.itemAt(i);
249 str.appendFormat(" track {%s} ", desc.string());
Marco Nelissen460b7e82016-12-19 14:06:30 -0800250 wp<IMediaSource> wSource = tracks.itemAt(i);
251 if (wSource == NULL) {
252 str.append(": null\n");
Marco Nelissen69d3d8a2016-03-07 13:20:01 -0800253 } else {
Marco Nelissen460b7e82016-12-19 14:06:30 -0800254 const sp<IMediaSource> source = wSource.promote();
255 if (source == NULL) {
256 str.append(": deleted\n");
257 } else {
258 str.appendFormat(": active\n");
259 }
Marco Nelissen69d3d8a2016-03-07 13:20:01 -0800260 }
261 }
262 return str;
263}
264
Marco Nelissen8d27f5e2016-04-14 08:06:26 -0700265static Vector<ExtractorInstance> sExtractors;
266static Mutex sExtractorsLock;
Marco Nelissen69d3d8a2016-03-07 13:20:01 -0800267
268void registerMediaSource(
269 const sp<IMediaExtractor> &ex,
270 const sp<IMediaSource> &source) {
Marco Nelissen8d27f5e2016-04-14 08:06:26 -0700271 Mutex::Autolock lock(sExtractorsLock);
272 for (size_t i = 0; i < sExtractors.size(); i++) {
273 ExtractorInstance &instance = sExtractors.editItemAt(i);
Marco Nelissen69d3d8a2016-03-07 13:20:01 -0800274 sp<IMediaExtractor> extractor = instance.extractor.promote();
275 if (extractor != NULL && extractor == ex) {
276 if (instance.tracks.size() > 5) {
277 instance.tracks.resize(5);
Marco Nelissen460b7e82016-12-19 14:06:30 -0800278 instance.trackDescriptions.resize(5);
Marco Nelissen69d3d8a2016-03-07 13:20:01 -0800279 }
280 instance.tracks.push_front(source);
Marco Nelissen460b7e82016-12-19 14:06:30 -0800281 if (source != NULL) {
282 instance.trackDescriptions.push_front(source->getFormat()->toString());
283 } else {
284 instance.trackDescriptions.push_front(String8::empty());
285 }
Marco Nelissen69d3d8a2016-03-07 13:20:01 -0800286 break;
287 }
288 }
289}
290
291void registerMediaExtractor(
292 const sp<IMediaExtractor> &extractor,
Marco Nelissena3214692016-05-06 10:57:20 -0700293 const sp<DataSource> &source,
Marco Nelissen69d3d8a2016-03-07 13:20:01 -0800294 const char *mime) {
295 ExtractorInstance ex;
296 ex.mime = mime == NULL ? "NULL" : mime;
297 ex.name = extractor->name();
298 ex.sourceDescription = source->toString();
299 ex.owner = IPCThreadState::self()->getCallingPid();
300 ex.extractor = extractor;
301
Marco Nelissen8d27f5e2016-04-14 08:06:26 -0700302 {
303 Mutex::Autolock lock(sExtractorsLock);
304 if (sExtractors.size() > 10) {
305 sExtractors.resize(10);
306 }
307 sExtractors.push_front(ex);
Marco Nelissen69d3d8a2016-03-07 13:20:01 -0800308 }
Marco Nelissen69d3d8a2016-03-07 13:20:01 -0800309}
310
311status_t dumpExtractors(int fd, const Vector<String16>&) {
312 String8 out;
ray-cy.leeb596c342017-03-07 18:25:15 +0800313 const IPCThreadState* ipc = IPCThreadState::self();
314 const int pid = ipc->getCallingPid();
315 const int uid = ipc->getCallingUid();
316 if (!PermissionCache::checkPermission(String16("android.permission.DUMP"), pid, uid)) {
317 out.appendFormat("Permission Denial: "
318 "can't dump MediaExtractor from pid=%d, uid=%d\n", pid, uid);
319 } else {
320 out.append("Recent extractors, most recent first:\n");
321 {
322 Mutex::Autolock lock(sExtractorsLock);
323 for (size_t i = 0; i < sExtractors.size(); i++) {
324 const ExtractorInstance &instance = sExtractors.itemAt(i);
325 out.append(" ");
326 out.append(instance.toString());
327 }
Marco Nelissen8d27f5e2016-04-14 08:06:26 -0700328 }
Marco Nelissen69d3d8a2016-03-07 13:20:01 -0800329 }
330 write(fd, out.string(), out.size());
331 return OK;
332}
333
Marco Nelissenb2487f02015-09-01 13:23:23 -0700334
335} // namespace android
336