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