1 /* 2 * Copyright (C) 2023 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 #pragma once 18 19 #include <android-base/unique_fd.h> 20 #include <cutils/ashmem.h> 21 #include <stdint.h> 22 #include <string> 23 #include <sys/mman.h> 24 25 #include "fifo/FifoBuffer.h" 26 #include "binding/RingBufferParcelable.h" 27 #include "binding/AudioEndpointParcelable.h" 28 29 namespace aaudio { 30 31 /** 32 * Wrap the shared memory with read and write counters. Provide a fifo buffer to access the 33 * wrapped shared memory. 34 */ 35 class SharedMemoryWrapper { 36 public: 37 explicit SharedMemoryWrapper(); 38 39 virtual ~SharedMemoryWrapper(); 40 getDataFileDescriptor()41 android::base::unique_fd& getDataFileDescriptor() { return mDataFd; } 42 43 aaudio_result_t setupFifoBuffer(android::fifo_frames_t bytesPerFrame, 44 android::fifo_frames_t capacityInFrames); 45 46 void reset(); 47 48 enum CounterFilling { 49 NONE = 0, 50 READ = 1, 51 WRITE = 2, 52 }; 53 /** 54 * Fill shared memory into parcelable. 55 * 56 * @param endpointParcelable container for ring buffers and shared memories 57 * @param ringBufferParcelable the ring buffer 58 * @param bytesPerFrame the bytes per frame of the data memory 59 * @param framesPerBurst the frame per burst of the data memory 60 * @param capacityInFrames the capacity in frames of the data memory 61 * @param counterFilling a bit mask to control if the counter from the wrapper should be filled 62 * or not. 63 */ 64 void fillParcelable(AudioEndpointParcelable* endpointParcelable, 65 RingBufferParcelable &ringBufferParcelable, 66 int32_t bytesPerFrame, 67 int32_t framesPerBurst, 68 int32_t capacityInFrames, 69 CounterFilling counterFilling = NONE); 70 getFifoBuffer()71 std::shared_ptr<android::FifoBuffer> getFifoBuffer() { 72 return mFifoBuffer; 73 } 74 75 private: 76 android::base::unique_fd mDataFd; 77 android::base::unique_fd mCounterFd; 78 uint8_t* mCounterMemoryAddress = nullptr; 79 android::fifo_counter_t* mReadCounterAddress = nullptr; 80 android::fifo_counter_t* mWriteCounterAddress = nullptr; 81 std::shared_ptr<android::FifoBufferIndirect> mFifoBuffer; 82 uint8_t* mSharedMemory = nullptr; 83 int32_t mSharedMemorySizeInBytes = 0; 84 }; 85 86 } /* namespace aaudio */ 87