1 /*
2 * Copyright (C) 2022 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 #include "../includes/common.h"
18 #include <datasource/DataSourceFactory.h>
19 #include <dlfcn.h>
20 #include <gui/SurfaceComposerClient.h>
21 #include <media/IMediaHTTPService.h>
22 #include <media/stagefright/InterfaceUtils.h>
23 #include <media/stagefright/MediaCodecList.h>
24 #include <media/stagefright/MediaExtractorFactory.h>
25 #include <media/stagefright/SimpleDecodingSource.h>
26 #include <sys/mman.h>
27
28 typedef void *(*mmap_t)(void *, size_t, int, int, int, off_t);
29 mmap_t real_mmap = nullptr;
30
31 using namespace android;
32
33 bool testInProgress = false;
34 constexpr size_t kTargetBufferSize = 32768;
35 struct sigaction new_action, old_action;
sigsegv_handler(int signum,siginfo_t * info,void * context)36 void sigsegv_handler(int signum, siginfo_t *info, void *context) {
37 if (testInProgress && info->si_signo == SIGSEGV) {
38 (*old_action.sa_sigaction)(signum, info, context);
39 return;
40 }
41 exit(EXIT_FAILURE);
42 }
43
mmap(void * addr,size_t length,int prot,int flags,int fd,off_t offset)44 void *mmap(void *addr, size_t length, int prot, int flags, int fd,
45 off_t offset) {
46 real_mmap = (mmap_t)dlsym(RTLD_NEXT, "mmap");
47 if (!real_mmap) {
48 exit(EXIT_FAILURE);
49 }
50 if (length == kTargetBufferSize) {
51 char *tmp_ptr = (char *)real_mmap(addr, length + PAGE_SIZE, prot,
52 flags | MAP_ANONYMOUS, -1, offset);
53 mprotect(tmp_ptr + length, PAGE_SIZE, PROT_NONE);
54 return tmp_ptr;
55 }
56 return real_mmap(addr, length, prot, flags, fd, offset);
57 }
58
main(int argc,char ** argv)59 int main(int argc, char **argv) {
60 FAIL_CHECK(argc > 1);
61 sigemptyset(&new_action.sa_mask);
62 new_action.sa_flags = SA_SIGINFO;
63 new_action.sa_sigaction = sigsegv_handler;
64 sigaction(SIGSEGV, &new_action, &old_action);
65
66 sp<DataSource> dataSource = DataSourceFactory::getInstance()->CreateFromURI(
67 nullptr /* httpService */, argv[1]);
68 FAIL_CHECK(dataSource);
69
70 sp<IMediaExtractor> extractor = MediaExtractorFactory::Create(dataSource);
71 FAIL_CHECK(extractor);
72
73 sp<MediaSource> mediaSource =
74 CreateMediaSourceFromIMediaSource(extractor->getTrack(0));
75 FAIL_CHECK(mediaSource);
76
77 sp<MediaSource> rawSource = SimpleDecodingSource::Create(
78 mediaSource, MediaCodecList::kPreferSoftwareCodecs, nullptr, nullptr,
79 false);
80 FAIL_CHECK(rawSource);
81
82 status_t err = rawSource->start();
83 FAIL_CHECK(err == OK);
84
85 MediaSource::ReadOptions options = {};
86 MediaBufferBase *buffer = nullptr;
87
88 testInProgress = true;
89 rawSource->read(&buffer, &options);
90 testInProgress = false;
91 if (buffer) {
92 buffer->release();
93 buffer = nullptr;
94 }
95 options.clearSeekTo();
96 options.setSeekTo(0);
97 rawSource->stop();
98 return EXIT_SUCCESS;
99 }
100