• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 ANDROID_LIBAUDIOPROCESSING_FUZZ_UTILS_H
18 #define ANDROID_LIBAUDIOPROCESSING_FUZZ_UTILS_H
19 
20 #include <media/AudioBufferProvider.h>
21 #include <system/audio.h>
22 
23 namespace android {
24 
25 class Provider : public AudioBufferProvider {
26   const void* mAddr;        // base address
27   const size_t mNumFrames;  // total frames
28   const size_t mFrameSize;  // size of each frame in bytes
29   size_t mNextFrame;        // index of next frame to provide
30   size_t mUnrel;            // number of frames not yet released
31  public:
Provider(const void * addr,size_t frames,size_t frameSize)32   Provider(const void* addr, size_t frames, size_t frameSize)
33       : mAddr(addr),
34         mNumFrames(frames),
35         mFrameSize(frameSize),
36         mNextFrame(0),
37         mUnrel(0) {}
getNextBuffer(Buffer * buffer)38   status_t getNextBuffer(Buffer* buffer) override {
39     if (buffer->frameCount > mNumFrames - mNextFrame) {
40       buffer->frameCount = mNumFrames - mNextFrame;
41     }
42     mUnrel = buffer->frameCount;
43     if (buffer->frameCount > 0) {
44       buffer->raw = (char*)mAddr + mFrameSize * mNextFrame;
45       return NO_ERROR;
46     } else {
47       buffer->raw = nullptr;
48       return NOT_ENOUGH_DATA;
49     }
50   }
releaseBuffer(Buffer * buffer)51   void releaseBuffer(Buffer* buffer) override {
52     if (buffer->frameCount > mUnrel) {
53       mNextFrame += mUnrel;
54       mUnrel = 0;
55     } else {
56       mNextFrame += buffer->frameCount;
57       mUnrel -= buffer->frameCount;
58     }
59     buffer->frameCount = 0;
60     buffer->raw = nullptr;
61   }
reset()62   void reset() { mNextFrame = 0; }
63 };
64 
65 } // namespace android
66 
67 #endif // ANDROID_LIBAUDIOPROCESSING_FUZZ_UTILS_H
68