Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 17 | #ifndef AAUDIO_SHARED_RINGBUFFER_H |
| 18 | #define AAUDIO_SHARED_RINGBUFFER_H |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 19 | |
Phil Burk | e72481c | 2017-08-08 13:20:45 -0700 | [diff] [blame] | 20 | #include <android-base/unique_fd.h> |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 21 | #include <cutils/ashmem.h> |
Phil Burk | 8f4fe50 | 2020-07-15 23:54:50 +0000 | [diff] [blame^] | 22 | #include <stdint.h> |
| 23 | #include <string> |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 24 | #include <sys/mman.h> |
| 25 | |
| 26 | #include "fifo/FifoBuffer.h" |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 27 | #include "binding/RingBufferParcelable.h" |
| 28 | #include "binding/AudioEndpointParcelable.h" |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 29 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 30 | namespace aaudio { |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 31 | |
| 32 | // Determine the placement of the counters and data in shared memory. |
| 33 | #define SHARED_RINGBUFFER_READ_OFFSET 0 |
| 34 | #define SHARED_RINGBUFFER_WRITE_OFFSET sizeof(fifo_counter_t) |
| 35 | #define SHARED_RINGBUFFER_DATA_OFFSET (SHARED_RINGBUFFER_WRITE_OFFSET + sizeof(fifo_counter_t)) |
| 36 | |
| 37 | /** |
| 38 | * Atomic FIFO that uses shared memory. |
| 39 | */ |
| 40 | class SharedRingBuffer { |
| 41 | public: |
| 42 | SharedRingBuffer() {} |
| 43 | |
| 44 | virtual ~SharedRingBuffer(); |
| 45 | |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 46 | aaudio_result_t allocate(android::fifo_frames_t bytesPerFrame, android::fifo_frames_t capacityInFrames); |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 47 | |
| 48 | void fillParcelable(AudioEndpointParcelable &endpointParcelable, |
| 49 | RingBufferParcelable &ringBufferParcelable); |
| 50 | |
Phil Burk | 8f4fe50 | 2020-07-15 23:54:50 +0000 | [diff] [blame^] | 51 | /** |
| 52 | * Return available frames as a fraction of the capacity. |
| 53 | * @return fullness between 0.0 and 1.0 |
| 54 | */ |
| 55 | double getFractionalFullness() const; |
| 56 | |
| 57 | // dump: write# read# available |
| 58 | std::string dump() const; |
| 59 | |
| 60 | std::shared_ptr<android::FifoBuffer> getFifoBuffer() { |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 61 | return mFifoBuffer; |
| 62 | } |
| 63 | |
| 64 | private: |
Phil Burk | e72481c | 2017-08-08 13:20:45 -0700 | [diff] [blame] | 65 | android::base::unique_fd mFileDescriptor; |
Phil Burk | 8f4fe50 | 2020-07-15 23:54:50 +0000 | [diff] [blame^] | 66 | std::shared_ptr<android::FifoBufferIndirect> mFifoBuffer; |
| 67 | uint8_t *mSharedMemory = nullptr; // mmap |
Phil Burk | e72481c | 2017-08-08 13:20:45 -0700 | [diff] [blame] | 68 | int32_t mSharedMemorySizeInBytes = 0; |
Phil Burk | 8f4fe50 | 2020-07-15 23:54:50 +0000 | [diff] [blame^] | 69 | // size of memory used for data vs counters |
Phil Burk | e72481c | 2017-08-08 13:20:45 -0700 | [diff] [blame] | 70 | int32_t mDataMemorySizeInBytes = 0; |
| 71 | android::fifo_frames_t mCapacityInFrames = 0; |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 72 | }; |
| 73 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 74 | } /* namespace aaudio */ |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 75 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 76 | #endif //AAUDIO_SHARED_RINGBUFFER_H |