12355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk/*
22355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk * Copyright (C) 2016 The Android Open Source Project
32355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk *
42355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk * Licensed under the Apache License, Version 2.0 (the "License");
52355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk * you may not use this file except in compliance with the License.
62355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk * You may obtain a copy of the License at
72355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk *
82355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk *      http://www.apache.org/licenses/LICENSE-2.0
92355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk *
102355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk * Unless required by applicable law or agreed to in writing, software
112355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk * distributed under the License is distributed on an "AS IS" BASIS,
122355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
132355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk * See the License for the specific language governing permissions and
142355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk * limitations under the License.
152355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk */
162355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk
175ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk#ifndef AAUDIO_SHARED_RINGBUFFER_H
185ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk#define AAUDIO_SHARED_RINGBUFFER_H
192355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk
202355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk#include <stdint.h>
212355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk#include <cutils/ashmem.h>
222355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk#include <sys/mman.h>
232355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk
242355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk#include "fifo/FifoBuffer.h"
25c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk#include "binding/RingBufferParcelable.h"
26c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk#include "binding/AudioEndpointParcelable.h"
272355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk
285ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burknamespace aaudio {
292355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk
302355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk// Determine the placement of the counters and data in shared memory.
312355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk#define SHARED_RINGBUFFER_READ_OFFSET   0
322355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk#define SHARED_RINGBUFFER_WRITE_OFFSET  sizeof(fifo_counter_t)
332355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk#define SHARED_RINGBUFFER_DATA_OFFSET   (SHARED_RINGBUFFER_WRITE_OFFSET + sizeof(fifo_counter_t))
342355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk
352355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk/**
362355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk * Atomic FIFO that uses shared memory.
372355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk */
382355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burkclass SharedRingBuffer {
392355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burkpublic:
402355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk    SharedRingBuffer() {}
412355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk
422355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk    virtual ~SharedRingBuffer();
432355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk
44c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    aaudio_result_t allocate(android::fifo_frames_t bytesPerFrame, android::fifo_frames_t capacityInFrames);
452355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk
462355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk    void fillParcelable(AudioEndpointParcelable &endpointParcelable,
472355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk                        RingBufferParcelable &ringBufferParcelable);
482355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk
49c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    android::FifoBuffer * getFifoBuffer() {
502355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk        return mFifoBuffer;
512355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk    }
522355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk
532355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burkprivate:
54c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    int                    mFileDescriptor = -1;
55c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    android::FifoBuffer   *mFifoBuffer = nullptr;
56c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    uint8_t               *mSharedMemory = nullptr;
57c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    int32_t                mSharedMemorySizeInBytes = 0;
58c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    int32_t                mDataMemorySizeInBytes = 0;
59c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    android::fifo_frames_t mCapacityInFrames = 0;
602355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk};
612355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk
625ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk} /* namespace aaudio */
632355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk
645ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk#endif //AAUDIO_SHARED_RINGBUFFER_H
65