xref: /aosp_15_r20/external/oboe/src/common/FixedBlockReader.h (revision 05767d913155b055644481607e6fa1e35e2fe72c)
1 /*
2  * Copyright (C) 2017 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 AAUDIO_FIXED_BLOCK_READER_H
18 #define AAUDIO_FIXED_BLOCK_READER_H
19 
20 #include <stdint.h>
21 
22 #include "FixedBlockAdapter.h"
23 
24 /**
25  * Read from a fixed-size block to a variable sized block.
26  *
27  * This can be used to convert a pull data flow from fixed sized buffers to variable sized buffers.
28  * An example would be an audio output callback that reads from the app.
29  */
30 class FixedBlockReader : public FixedBlockAdapter
31 {
32 public:
33     FixedBlockReader(FixedBlockProcessor &fixedBlockProcessor);
34 
35     virtual ~FixedBlockReader() = default;
36 
37     int32_t open(int32_t bytesPerFixedBlock) override;
38 
39     /**
40      * Read into a variable sized block.
41      *
42      * Note that if the fixed-sized blocks must be aligned, then the variable-sized blocks
43      * must have the same alignment.
44      * For example, if the fixed-size blocks must be a multiple of 8, then the variable-sized
45      * blocks must also be a multiple of 8.
46      *
47      * @param buffer
48      * @param numBytes
49      * @return Number of bytes read or a negative error code.
50      */
51     int32_t read(uint8_t *buffer, int32_t numBytes);
52 
53 private:
54     int32_t readFromStorage(uint8_t *buffer, int32_t numBytes);
55 
56     int32_t               mValid = 0;            // Number of valid bytes in mStorage.
57 };
58 
59 
60 #endif /* AAUDIO_FIXED_BLOCK_READER_H */
61