blob: 4dc7e68959714c0ad036d8c4371ff405c9eaa08a [file] [log] [blame]
Phil Burkc8f372c2017-03-28 11:32:39 -07001/*
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_ADAPTER_H
18#define AAUDIO_FIXED_BLOCK_ADAPTER_H
19
Phil Burk38ef26a2020-02-06 14:52:15 -080020#include <memory>
Phil Burkc8f372c2017-03-28 11:32:39 -070021#include <stdio.h>
22
23/**
24 * Interface for a class that needs fixed-size blocks.
25 */
26class FixedBlockProcessor {
27public:
28 virtual ~FixedBlockProcessor() = default;
29 virtual int32_t onProcessFixedBlock(uint8_t *buffer, int32_t numBytes) = 0;
30};
31
32/**
33 * Base class for a variable-to-fixed-size block adapter.
34 */
35class FixedBlockAdapter
36{
37public:
38 FixedBlockAdapter(FixedBlockProcessor &fixedBlockProcessor)
39 : mFixedBlockProcessor(fixedBlockProcessor) {}
40
Phil Burk38ef26a2020-02-06 14:52:15 -080041 virtual ~FixedBlockAdapter() = default;
Phil Burkc8f372c2017-03-28 11:32:39 -070042
43 /**
44 * Allocate internal resources needed for buffering data.
45 */
46 virtual int32_t open(int32_t bytesPerFixedBlock);
47
48 /**
49 * Note that if the fixed-sized blocks must be aligned, then the variable-sized blocks
50 * must have the same alignment.
51 * For example, if the fixed-size blocks must be a multiple of 8, then the variable-sized
52 * blocks must also be a multiple of 8.
53 *
54 * @param buffer
55 * @param numBytes
56 * @return zero if OK or a non-zero code
57 */
58 virtual int32_t processVariableBlock(uint8_t *buffer, int32_t numBytes) = 0;
59
60 /**
61 * Free internal resources.
62 */
63 int32_t close();
64
65protected:
66 FixedBlockProcessor &mFixedBlockProcessor;
Phil Burk38ef26a2020-02-06 14:52:15 -080067 std::unique_ptr<uint8_t[]> mStorage; // Store data here while assembling buffers.
Phil Burkc8f372c2017-03-28 11:32:39 -070068 int32_t mSize = 0; // Size in bytes of the fixed size buffer.
69 int32_t mPosition = 0; // Offset of the last byte read or written.
70};
71
72#endif /* AAUDIO_FIXED_BLOCK_ADAPTER_H */