1 /*
2 * Copyright (C) 2020 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 #ifndef IMEDIASOURCEFUZZIMPL_H
18 #define IMEDIASOURCEFUZZIMPL_H
19
20 #include <media/stagefright/MediaSource.h>
21
22 namespace android {
23
24 class IMediaSourceFuzzImpl : public IMediaSource {
25 public:
IMediaSourceFuzzImpl(FuzzedDataProvider * _fdp,size_t _max_buffer_size)26 IMediaSourceFuzzImpl(FuzzedDataProvider *_fdp, size_t _max_buffer_size) :
27 fdp(_fdp),
28 max_buffer_size(_max_buffer_size) {}
start(MetaData *)29 status_t start(MetaData*) override { return 0; }
stop()30 status_t stop() override { return 0; }
getFormat()31 sp<MetaData> getFormat() override { return nullptr; }
32 status_t read(MediaBufferBase**,
33 const MediaSource::ReadOptions*) override;
34 status_t readMultiple(Vector<MediaBufferBase*>*, uint32_t,
35 const MediaSource::ReadOptions*) override;
supportReadMultiple()36 bool supportReadMultiple() override { return true; }
supportNonblockingRead()37 bool supportNonblockingRead() override { return true; }
pause()38 status_t pause() override { return 0; }
39
40 protected:
onAsBinder()41 IBinder* onAsBinder() { return nullptr; }
42
43 private:
44 FuzzedDataProvider *fdp;
45 std::vector<std::shared_ptr<MediaBufferBase>> buffer_bases;
46 const size_t max_buffer_size;
47 };
48
49 // This class is simply to expose the destructor
50 class MediaBufferFuzzImpl : public MediaBuffer {
51 public:
MediaBufferFuzzImpl(void * data,size_t size)52 MediaBufferFuzzImpl(void *data, size_t size) : MediaBuffer(data, size) {}
~MediaBufferFuzzImpl()53 ~MediaBufferFuzzImpl() {}
54 };
55
read(MediaBufferBase ** buffer,const MediaSource::ReadOptions * options)56 status_t IMediaSourceFuzzImpl::read(MediaBufferBase **buffer,
57 const MediaSource::ReadOptions *options) {
58 Vector<MediaBufferBase*> buffers;
59 status_t ret = readMultiple(&buffers, 1, options);
60 *buffer = buffers.empty() ? nullptr : buffers[0];
61
62 return ret;
63 }
64
readMultiple(Vector<MediaBufferBase * > * buffers,uint32_t maxNumBuffers,const MediaSource::ReadOptions *)65 status_t IMediaSourceFuzzImpl::readMultiple(Vector<MediaBufferBase*>* buffers,
66 uint32_t maxNumBuffers, const MediaSource::ReadOptions*) {
67 uint32_t num_buffers =
68 fdp->ConsumeIntegralInRange<uint32_t>(0, maxNumBuffers);
69 for(uint32_t i = 0; i < num_buffers; i++) {
70 std::vector<uint8_t> buf = fdp->ConsumeBytes<uint8_t>(
71 fdp->ConsumeIntegralInRange<size_t>(0, max_buffer_size));
72
73 std::shared_ptr<MediaBufferBase> mbb(
74 new MediaBufferFuzzImpl(buf.data(), buf.size()));
75
76 buffer_bases.push_back(mbb);
77 buffers->push_back(mbb.get());
78 }
79
80 // STATUS_OK
81 return 0;
82 }
83
84 } // namespace android
85
86 #endif // IMEDIASOURCEFUZZIMPL_H
87
88