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