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 <memory> 21 #include <stdio.h> 22 23 /** 24 * Interface for a class that needs fixed-size blocks. 25 */ 26 class FixedBlockProcessor { 27 public: 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 */ 35 class FixedBlockAdapter 36 { 37 public: FixedBlockAdapter(FixedBlockProcessor & fixedBlockProcessor)38 explicit FixedBlockAdapter(FixedBlockProcessor &fixedBlockProcessor) 39 : mFixedBlockProcessor(fixedBlockProcessor) {} 40 41 virtual ~FixedBlockAdapter() = default; 42 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 65 protected: 66 FixedBlockProcessor &mFixedBlockProcessor; 67 std::unique_ptr<uint8_t[]> mStorage; // Store data here while assembling buffers. 68 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 */ 73