blob: 79169bc240a81a9b278b9c4321528dbd11bf3d1b [file] [log] [blame]
Phil Burk2355edb2016-12-26 13:54:02 -08001/*
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 Burk5ed503c2017-02-01 09:38:15 -080017#ifndef AAUDIO_SHARED_RINGBUFFER_H
18#define AAUDIO_SHARED_RINGBUFFER_H
Phil Burk2355edb2016-12-26 13:54:02 -080019
Phil Burke72481c2017-08-08 13:20:45 -070020#include <android-base/unique_fd.h>
Phil Burk2355edb2016-12-26 13:54:02 -080021#include <stdint.h>
22#include <cutils/ashmem.h>
23#include <sys/mman.h>
24
25#include "fifo/FifoBuffer.h"
Phil Burk7f6b40d2017-02-09 13:18:38 -080026#include "binding/RingBufferParcelable.h"
27#include "binding/AudioEndpointParcelable.h"
Phil Burk2355edb2016-12-26 13:54:02 -080028
Phil Burk5ed503c2017-02-01 09:38:15 -080029namespace aaudio {
Phil Burk2355edb2016-12-26 13:54:02 -080030
31// Determine the placement of the counters and data in shared memory.
32#define SHARED_RINGBUFFER_READ_OFFSET 0
33#define SHARED_RINGBUFFER_WRITE_OFFSET sizeof(fifo_counter_t)
34#define SHARED_RINGBUFFER_DATA_OFFSET (SHARED_RINGBUFFER_WRITE_OFFSET + sizeof(fifo_counter_t))
35
36/**
37 * Atomic FIFO that uses shared memory.
38 */
39class SharedRingBuffer {
40public:
41 SharedRingBuffer() {}
42
43 virtual ~SharedRingBuffer();
44
Phil Burk7f6b40d2017-02-09 13:18:38 -080045 aaudio_result_t allocate(android::fifo_frames_t bytesPerFrame, android::fifo_frames_t capacityInFrames);
Phil Burk2355edb2016-12-26 13:54:02 -080046
47 void fillParcelable(AudioEndpointParcelable &endpointParcelable,
48 RingBufferParcelable &ringBufferParcelable);
49
Phil Burk7f6b40d2017-02-09 13:18:38 -080050 android::FifoBuffer * getFifoBuffer() {
Phil Burk2355edb2016-12-26 13:54:02 -080051 return mFifoBuffer;
52 }
53
54private:
Phil Burke72481c2017-08-08 13:20:45 -070055 android::base::unique_fd mFileDescriptor;
56 android::FifoBuffer *mFifoBuffer = nullptr;
57 uint8_t *mSharedMemory = nullptr;
58 int32_t mSharedMemorySizeInBytes = 0;
59 int32_t mDataMemorySizeInBytes = 0;
60 android::fifo_frames_t mCapacityInFrames = 0;
Phil Burk2355edb2016-12-26 13:54:02 -080061};
62
Phil Burk5ed503c2017-02-01 09:38:15 -080063} /* namespace aaudio */
Phil Burk2355edb2016-12-26 13:54:02 -080064
Phil Burk5ed503c2017-02-01 09:38:15 -080065#endif //AAUDIO_SHARED_RINGBUFFER_H