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 | |
| 20 | #include <stdint.h> |
| 21 | #include <cutils/ashmem.h> |
| 22 | #include <sys/mman.h> |
| 23 | |
| 24 | #include "fifo/FifoBuffer.h" |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 25 | #include "binding/RingBufferParcelable.h" |
| 26 | #include "binding/AudioEndpointParcelable.h" |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 27 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 28 | namespace aaudio { |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 29 | |
| 30 | // Determine the placement of the counters and data in shared memory. |
| 31 | #define SHARED_RINGBUFFER_READ_OFFSET 0 |
| 32 | #define SHARED_RINGBUFFER_WRITE_OFFSET sizeof(fifo_counter_t) |
| 33 | #define SHARED_RINGBUFFER_DATA_OFFSET (SHARED_RINGBUFFER_WRITE_OFFSET + sizeof(fifo_counter_t)) |
| 34 | |
| 35 | /** |
| 36 | * Atomic FIFO that uses shared memory. |
| 37 | */ |
| 38 | class SharedRingBuffer { |
| 39 | public: |
| 40 | SharedRingBuffer() {} |
| 41 | |
| 42 | virtual ~SharedRingBuffer(); |
| 43 | |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 44 | 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] | 45 | |
| 46 | void fillParcelable(AudioEndpointParcelable &endpointParcelable, |
| 47 | RingBufferParcelable &ringBufferParcelable); |
| 48 | |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 49 | android::FifoBuffer * getFifoBuffer() { |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 50 | return mFifoBuffer; |
| 51 | } |
| 52 | |
| 53 | private: |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 54 | int mFileDescriptor = -1; |
| 55 | android::FifoBuffer *mFifoBuffer = nullptr; |
| 56 | uint8_t *mSharedMemory = nullptr; |
| 57 | int32_t mSharedMemorySizeInBytes = 0; |
| 58 | int32_t mDataMemorySizeInBytes = 0; |
| 59 | android::fifo_frames_t mCapacityInFrames = 0; |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 60 | }; |
| 61 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 62 | } /* namespace aaudio */ |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 63 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 64 | #endif //AAUDIO_SHARED_RINGBUFFER_H |