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 <stdint.h> 22 #include <sys/types.h> 23 24 /** 25 * Interface for a class that needs fixed-size blocks. 26 */ 27 class FixedBlockProcessor { 28 public: 29 virtual ~FixedBlockProcessor() = default; 30 /** 31 * 32 * @param buffer Pointer to first byte of data. 33 * @param numBytes This will be a fixed size specified in FixedBlockAdapter::open(). 34 * @return Number of bytes processed or a negative error code. 35 */ 36 virtual int32_t onProcessFixedBlock(uint8_t *buffer, int32_t numBytes) = 0; 37 }; 38 39 /** 40 * Base class for a variable-to-fixed-size block adapter. 41 */ 42 class FixedBlockAdapter 43 { 44 public: FixedBlockAdapter(FixedBlockProcessor & fixedBlockProcessor)45 FixedBlockAdapter(FixedBlockProcessor &fixedBlockProcessor) 46 : mFixedBlockProcessor(fixedBlockProcessor) {} 47 48 virtual ~FixedBlockAdapter(); 49 50 /** 51 * Allocate internal resources needed for buffering data. 52 */ 53 virtual int32_t open(int32_t bytesPerFixedBlock); 54 55 /** 56 * Free internal resources. 57 */ 58 int32_t close(); 59 60 protected: 61 FixedBlockProcessor &mFixedBlockProcessor; 62 std::unique_ptr<uint8_t[]> mStorage; // Store data here while assembling buffers. 63 int32_t mSize = 0; // Size in bytes of the fixed size buffer. 64 int32_t mPosition = 0; // Offset of the last byte read or written. 65 }; 66 67 #endif /* AAUDIO_FIXED_BLOCK_ADAPTER_H */ 68