1e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk/*
2e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk * Copyright (C) 2017 The Android Open Source Project
3e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk *
4e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk * Licensed under the Apache License, Version 2.0 (the "License");
5e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk * you may not use this file except in compliance with the License.
6e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk * You may obtain a copy of the License at
7e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk *
8e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk *      http://www.apache.org/licenses/LICENSE-2.0
9e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk *
10e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk * Unless required by applicable law or agreed to in writing, software
11e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk * distributed under the License is distributed on an "AS IS" BASIS,
12e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk * See the License for the specific language governing permissions and
14e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk * limitations under the License.
15e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk */
16e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk
17e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk#ifndef AAUDIO_FIXED_BLOCK_ADAPTER_H
18e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk#define AAUDIO_FIXED_BLOCK_ADAPTER_H
19e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk
20e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk#include <stdio.h>
21e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk
22e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk/**
23e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk * Interface for a class that needs fixed-size blocks.
24e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk */
25e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burkclass FixedBlockProcessor {
26e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burkpublic:
27e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    virtual ~FixedBlockProcessor() = default;
28e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    virtual int32_t onProcessFixedBlock(uint8_t *buffer, int32_t numBytes) = 0;
29e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk};
30e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk
31e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk/**
32e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk * Base class for a variable-to-fixed-size block adapter.
33e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk */
34e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burkclass FixedBlockAdapter
35e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk{
36e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burkpublic:
37e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    FixedBlockAdapter(FixedBlockProcessor &fixedBlockProcessor)
38e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    : mFixedBlockProcessor(fixedBlockProcessor) {}
39e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk
40e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    virtual ~FixedBlockAdapter();
41e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk
42e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    /**
43e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk     * Allocate internal resources needed for buffering data.
44e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk     */
45e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    virtual int32_t open(int32_t bytesPerFixedBlock);
46e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk
47e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    /**
48e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk     * Note that if the fixed-sized blocks must be aligned, then the variable-sized blocks
49e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk     * must have the same alignment.
50e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk     * For example, if the fixed-size blocks must be a multiple of 8, then the variable-sized
51e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk     * blocks must also be a multiple of 8.
52e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk     *
53e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk     * @param buffer
54e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk     * @param numBytes
55e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk     * @return zero if OK or a non-zero code
56e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk     */
57e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    virtual int32_t processVariableBlock(uint8_t *buffer, int32_t numBytes) = 0;
58e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk
59e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    /**
60e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk     * Free internal resources.
61e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk     */
62e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    int32_t close();
63e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk
64e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burkprotected:
65e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    FixedBlockProcessor  &mFixedBlockProcessor;
66e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    uint8_t              *mStorage = nullptr;    // Store data here while assembling buffers.
67e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    int32_t               mSize = 0;             // Size in bytes of the fixed size buffer.
68e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    int32_t               mPosition = 0;         // Offset of the last byte read or written.
69e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk};
70e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk
71e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk#endif /* AAUDIO_FIXED_BLOCK_ADAPTER_H */
72