blob: 39caf53004bc692b9ea80df88495e5ce89d4f38f [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>
Marco Nelissenb5894372019-01-10 14:44:13 -080022#include <time.h>
Marco Nelissenb2487f02015-09-01 13:23:23 -070023#include <sys/types.h>
24
Marco Nelissen69d3d8a2016-03-07 13:20:01 -080025#include <binder/IPCThreadState.h>
Marco Nelissenb2487f02015-09-01 13:23:23 -070026#include <binder/Parcel.h>
ray-cy.leeb596c342017-03-07 18:25:15 +080027#include <binder/PermissionCache.h>
Marco Nelissendab79b32019-11-18 08:25:47 -080028#include <android/IMediaExtractor.h>
Marco Nelissenb2487f02015-09-01 13:23:23 -070029#include <media/stagefright/MetaData.h>
30
31namespace android {
32
33enum {
34 COUNTTRACKS = IBinder::FIRST_CALL_TRANSACTION,
35 GETTRACK,
36 GETTRACKMETADATA,
37 GETMETADATA,
38 FLAGS,
Chong Zhang9dbe9a52017-01-03 11:35:15 -080039 SETMEDIACAS,
Ray Essickba13b7b2017-02-07 10:03:18 -080040 NAME,
Marco Nelissen2a243f02018-01-30 08:29:57 -080041 GETMETRICS
Marco Nelissenb2487f02015-09-01 13:23:23 -070042};
43
44class BpMediaExtractor : public BpInterface<IMediaExtractor> {
45public:
Chih-Hung Hsieh64a28702016-05-03 09:52:51 -070046 explicit BpMediaExtractor(const sp<IBinder>& impl)
Marco Nelissenb2487f02015-09-01 13:23:23 -070047 : BpInterface<IMediaExtractor>(impl)
48 {
49 }
50
51 virtual size_t countTracks() {
52 ALOGV("countTracks");
53 Parcel data, reply;
54 data.writeInterfaceToken(BpMediaExtractor::getInterfaceDescriptor());
55 status_t ret = remote()->transact(COUNTTRACKS, data, &reply);
56 size_t numTracks = 0;
57 if (ret == NO_ERROR) {
58 numTracks = reply.readUint32();
59 }
60 return numTracks;
61 }
62 virtual sp<IMediaSource> getTrack(size_t index) {
63 ALOGV("getTrack(%zu)", index);
64 Parcel data, reply;
65 data.writeInterfaceToken(BpMediaExtractor::getInterfaceDescriptor());
66 data.writeUint32(index);
67 status_t ret = remote()->transact(GETTRACK, data, &reply);
68 if (ret == NO_ERROR) {
69 return interface_cast<IMediaSource>(reply.readStrongBinder());
70 }
71 return NULL;
72 }
73
74 virtual sp<MetaData> getTrackMetaData(
75 size_t index, uint32_t flags) {
76 ALOGV("getTrackMetaData(%zu, %u)", index, flags);
77 Parcel data, reply;
78 data.writeInterfaceToken(BpMediaExtractor::getInterfaceDescriptor());
79 data.writeUint32(index);
80 data.writeUint32(flags);
81 status_t ret = remote()->transact(GETTRACKMETADATA, data, &reply);
82 if (ret == NO_ERROR) {
83 return MetaData::createFromParcel(reply);
84 }
85 return NULL;
86 }
87
88 virtual sp<MetaData> getMetaData() {
89 ALOGV("getMetaData");
90 Parcel data, reply;
91 data.writeInterfaceToken(BpMediaExtractor::getInterfaceDescriptor());
92 status_t ret = remote()->transact(GETMETADATA, data, &reply);
93 if (ret == NO_ERROR) {
94 return MetaData::createFromParcel(reply);
95 }
96 return NULL;
97 }
98
Ray Essickba13b7b2017-02-07 10:03:18 -080099 virtual status_t getMetrics(Parcel * reply) {
100 Parcel data;
101 data.writeInterfaceToken(BpMediaExtractor::getInterfaceDescriptor());
102 status_t ret = remote()->transact(GETMETRICS, data, reply);
103 if (ret == NO_ERROR) {
104 return OK;
105 }
106 return UNKNOWN_ERROR;
107 }
108
Marco Nelissenb2487f02015-09-01 13:23:23 -0700109 virtual uint32_t flags() const {
Marco Nelissene4f4b792020-01-17 13:15:18 -0800110 ALOGV("flags");
111 Parcel data, reply;
112 data.writeInterfaceToken(BpMediaExtractor::getInterfaceDescriptor());
113 status_t ret = remote()->transact(FLAGS, data, &reply);
114 int flgs = 0;
115 if (ret == NO_ERROR) {
116 flgs = reply.readUint32();
117 }
118 return flgs;
Marco Nelissenb2487f02015-09-01 13:23:23 -0700119 }
120
Chong Zhangd5a416a2017-05-16 11:16:34 -0700121 virtual status_t setMediaCas(const HInterfaceToken &casToken) {
Chong Zhang9dbe9a52017-01-03 11:35:15 -0800122 ALOGV("setMediaCas");
123
124 Parcel data, reply;
125 data.writeInterfaceToken(BpMediaExtractor::getInterfaceDescriptor());
Chong Zhangd5a416a2017-05-16 11:16:34 -0700126 data.writeByteVector(casToken);
Chong Zhang9dbe9a52017-01-03 11:35:15 -0800127
128 status_t err = remote()->transact(SETMEDIACAS, data, &reply);
129 if (err != NO_ERROR) {
130 return err;
131 }
132 return reply.readInt32();
133 }
134
Marco Nelissene4f4b792020-01-17 13:15:18 -0800135 virtual String8 name() {
136 Parcel data, reply;
137 data.writeInterfaceToken(BpMediaExtractor::getInterfaceDescriptor());
138 status_t ret = remote()->transact(NAME, data, &reply);
139 String8 nm;
140 if (ret == NO_ERROR) {
141 nm = reply.readString8();
142 }
143 return nm;
Marco Nelissenb2487f02015-09-01 13:23:23 -0700144 }
145};
146
147IMPLEMENT_META_INTERFACE(MediaExtractor, "android.media.IMediaExtractor");
148
149#undef LOG_TAG
150#define LOG_TAG "BnMediaExtractor"
151
152status_t BnMediaExtractor::onTransact(
153 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
154{
155 switch (code) {
156 case COUNTTRACKS: {
157 ALOGV("countTracks");
158 CHECK_INTERFACE(IMediaExtractor, data, reply);
159 size_t numTracks = countTracks();
160 if (numTracks > INT32_MAX) {
161 numTracks = 0;
162 }
163 reply->writeUint32(uint32_t(numTracks));
164 return NO_ERROR;
165 }
166 case GETTRACK: {
167 ALOGV("getTrack()");
168 CHECK_INTERFACE(IMediaExtractor, data, reply);
169 uint32_t idx;
170 if (data.readUint32(&idx) == NO_ERROR) {
Marco Nelissen69d3d8a2016-03-07 13:20:01 -0800171 const sp<IMediaSource> track = getTrack(size_t(idx));
172 registerMediaSource(this, track);
173 return reply->writeStrongBinder(IInterface::asBinder(track));
Marco Nelissenb2487f02015-09-01 13:23:23 -0700174 }
175 return UNKNOWN_ERROR;
176 }
177 case GETTRACKMETADATA: {
178 ALOGV("getTrackMetaData");
179 CHECK_INTERFACE(IMediaExtractor, data, reply);
180 uint32_t idx;
181 uint32_t flags;
182 if (data.readUint32(&idx) == NO_ERROR &&
183 data.readUint32(&flags) == NO_ERROR) {
184 sp<MetaData> meta = getTrackMetaData(idx, flags);
Marco Nelissen0d138242016-10-06 15:31:52 -0700185 if (meta == NULL) {
186 return UNKNOWN_ERROR;
187 }
Marco Nelissenb2487f02015-09-01 13:23:23 -0700188 meta->writeToParcel(*reply);
189 return NO_ERROR;
190 }
191 return UNKNOWN_ERROR;
192 }
193 case GETMETADATA: {
194 ALOGV("getMetaData");
195 CHECK_INTERFACE(IMediaExtractor, data, reply);
196 sp<MetaData> meta = getMetaData();
197 if (meta != NULL) {
198 meta->writeToParcel(*reply);
199 return NO_ERROR;
200 }
201 return UNKNOWN_ERROR;
202 }
Ray Essickba13b7b2017-02-07 10:03:18 -0800203 case GETMETRICS: {
204 CHECK_INTERFACE(IMediaExtractor, data, reply);
205 status_t ret = getMetrics(reply);
206 return ret;
207 }
Marco Nelissene4f4b792020-01-17 13:15:18 -0800208 case FLAGS: {
209 ALOGV("flags");
210 CHECK_INTERFACE(IMediaExtractor, data, reply);
211 reply->writeUint32(this->flags());
212 return NO_ERROR;
213 }
Chong Zhang9dbe9a52017-01-03 11:35:15 -0800214 case SETMEDIACAS: {
215 ALOGV("setMediaCas");
216 CHECK_INTERFACE(IMediaExtractor, data, reply);
217
Chong Zhangd5a416a2017-05-16 11:16:34 -0700218 HInterfaceToken casToken;
219 status_t err = data.readByteVector(&casToken);
Chong Zhang9dbe9a52017-01-03 11:35:15 -0800220 if (err != NO_ERROR) {
Chong Zhangd5a416a2017-05-16 11:16:34 -0700221 ALOGE("Error reading casToken from parcel");
Chong Zhang9dbe9a52017-01-03 11:35:15 -0800222 return err;
223 }
Chong Zhang9dbe9a52017-01-03 11:35:15 -0800224
Chong Zhangd5a416a2017-05-16 11:16:34 -0700225 reply->writeInt32(setMediaCas(casToken));
Chong Zhang9dbe9a52017-01-03 11:35:15 -0800226 return OK;
227 }
Marco Nelissene4f4b792020-01-17 13:15:18 -0800228 case NAME: {
229 ALOGV("name");
230 CHECK_INTERFACE(IMediaExtractor, data, reply);
231 String8 nm = name();
232 reply->writeString8(nm);
233 return NO_ERROR;
234 }
Marco Nelissenb2487f02015-09-01 13:23:23 -0700235 default:
236 return BBinder::onTransact(code, data, reply, flags);
237 }
238}
239
Marco Nelissen69d3d8a2016-03-07 13:20:01 -0800240typedef struct {
241 String8 mime;
242 String8 name;
243 String8 sourceDescription;
244 pid_t owner;
245 wp<IMediaExtractor> extractor;
246 Vector<wp<IMediaSource>> tracks;
247 Vector<String8> trackDescriptions;
248 String8 toString() const;
Marco Nelissenb5894372019-01-10 14:44:13 -0800249 time_t when;
Marco Nelissen69d3d8a2016-03-07 13:20:01 -0800250} ExtractorInstance;
251
252String8 ExtractorInstance::toString() const {
Marco Nelissenb5894372019-01-10 14:44:13 -0800253 String8 str;
254 char timeString[32];
255 strftime(timeString, sizeof(timeString), "%m-%d %T", localtime(&when));
256 str.append(timeString);
257 str.append(": ");
258 str.append(name);
Marco Nelissen69d3d8a2016-03-07 13:20:01 -0800259 str.append(" for mime ");
260 str.append(mime);
261 str.append(", source ");
262 str.append(sourceDescription);
263 str.append(String8::format(", pid %d: ", owner));
264 if (extractor.promote() == NULL) {
265 str.append("deleted\n");
266 } else {
267 str.append("active\n");
268 }
269 for (size_t i = 0; i < tracks.size(); i++) {
270 const String8 desc = trackDescriptions.itemAt(i);
271 str.appendFormat(" track {%s} ", desc.string());
Marco Nelissen460b7e82016-12-19 14:06:30 -0800272 wp<IMediaSource> wSource = tracks.itemAt(i);
273 if (wSource == NULL) {
274 str.append(": null\n");
Marco Nelissen69d3d8a2016-03-07 13:20:01 -0800275 } else {
Marco Nelissen460b7e82016-12-19 14:06:30 -0800276 const sp<IMediaSource> source = wSource.promote();
277 if (source == NULL) {
278 str.append(": deleted\n");
279 } else {
280 str.appendFormat(": active\n");
281 }
Marco Nelissen69d3d8a2016-03-07 13:20:01 -0800282 }
283 }
284 return str;
285}
286
Marco Nelissen8d27f5e2016-04-14 08:06:26 -0700287static Vector<ExtractorInstance> sExtractors;
288static Mutex sExtractorsLock;
Marco Nelissen69d3d8a2016-03-07 13:20:01 -0800289
290void registerMediaSource(
291 const sp<IMediaExtractor> &ex,
292 const sp<IMediaSource> &source) {
Marco Nelissen8d27f5e2016-04-14 08:06:26 -0700293 Mutex::Autolock lock(sExtractorsLock);
294 for (size_t i = 0; i < sExtractors.size(); i++) {
295 ExtractorInstance &instance = sExtractors.editItemAt(i);
Marco Nelissen69d3d8a2016-03-07 13:20:01 -0800296 sp<IMediaExtractor> extractor = instance.extractor.promote();
297 if (extractor != NULL && extractor == ex) {
298 if (instance.tracks.size() > 5) {
299 instance.tracks.resize(5);
Marco Nelissen460b7e82016-12-19 14:06:30 -0800300 instance.trackDescriptions.resize(5);
Marco Nelissen69d3d8a2016-03-07 13:20:01 -0800301 }
302 instance.tracks.push_front(source);
Marco Nelissen460b7e82016-12-19 14:06:30 -0800303 if (source != NULL) {
304 instance.trackDescriptions.push_front(source->getFormat()->toString());
305 } else {
306 instance.trackDescriptions.push_front(String8::empty());
307 }
Marco Nelissen69d3d8a2016-03-07 13:20:01 -0800308 break;
309 }
310 }
311}
312
313void registerMediaExtractor(
314 const sp<IMediaExtractor> &extractor,
Marco Nelissena3214692016-05-06 10:57:20 -0700315 const sp<DataSource> &source,
Marco Nelissen69d3d8a2016-03-07 13:20:01 -0800316 const char *mime) {
317 ExtractorInstance ex;
318 ex.mime = mime == NULL ? "NULL" : mime;
319 ex.name = extractor->name();
320 ex.sourceDescription = source->toString();
321 ex.owner = IPCThreadState::self()->getCallingPid();
322 ex.extractor = extractor;
Marco Nelissenb5894372019-01-10 14:44:13 -0800323 ex.when = time(NULL);
Marco Nelissen69d3d8a2016-03-07 13:20:01 -0800324
Marco Nelissen8d27f5e2016-04-14 08:06:26 -0700325 {
326 Mutex::Autolock lock(sExtractorsLock);
327 if (sExtractors.size() > 10) {
328 sExtractors.resize(10);
329 }
330 sExtractors.push_front(ex);
Marco Nelissen69d3d8a2016-03-07 13:20:01 -0800331 }
Marco Nelissen69d3d8a2016-03-07 13:20:01 -0800332}
333
334status_t dumpExtractors(int fd, const Vector<String16>&) {
335 String8 out;
ray-cy.leeb596c342017-03-07 18:25:15 +0800336 const IPCThreadState* ipc = IPCThreadState::self();
337 const int pid = ipc->getCallingPid();
338 const int uid = ipc->getCallingUid();
339 if (!PermissionCache::checkPermission(String16("android.permission.DUMP"), pid, uid)) {
340 out.appendFormat("Permission Denial: "
341 "can't dump MediaExtractor from pid=%d, uid=%d\n", pid, uid);
342 } else {
343 out.append("Recent extractors, most recent first:\n");
344 {
345 Mutex::Autolock lock(sExtractorsLock);
346 for (size_t i = 0; i < sExtractors.size(); i++) {
347 const ExtractorInstance &instance = sExtractors.itemAt(i);
348 out.append(" ");
349 out.append(instance.toString());
350 }
Marco Nelissen8d27f5e2016-04-14 08:06:26 -0700351 }
Marco Nelissen69d3d8a2016-03-07 13:20:01 -0800352 }
353 write(fd, out.string(), out.size());
354 return OK;
355}
356
Marco Nelissenb2487f02015-09-01 13:23:23 -0700357
358} // namespace android
359