SharedRingBuffer.h revision 2355edbcacfcb6e852a8707d893aaca788d42fdc
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
172355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk#ifndef OBOE_SHARED_RINGBUFFER_H
182355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk#define OBOE_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"
252355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk#include "RingBufferParcelable.h"
262355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk#include "AudioEndpointParcelable.h"
272355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk
282355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burknamespace oboe {
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
442355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk    oboe_result_t allocate(fifo_frames_t bytesPerFrame, fifo_frames_t capacityInFrames);
452355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk
462355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk    void fillParcelable(AudioEndpointParcelable &endpointParcelable,
472355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk                        RingBufferParcelable &ringBufferParcelable);
482355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk
492355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk    FifoBuffer * getFifoBuffer() {
502355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk        return mFifoBuffer;
512355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk    }
522355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk
532355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burkprivate:
542355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk    int            mFileDescriptor = -1;
552355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk    FifoBuffer   * mFifoBuffer = nullptr;
562355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk    uint8_t      * mSharedMemory = nullptr;
572355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk    int32_t        mSharedMemorySizeInBytes = 0;
582355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk    int32_t        mDataMemorySizeInBytes = 0;
592355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk    fifo_frames_t  mCapacityInFrames = 0;
602355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk};
612355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk
622355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk} /* namespace oboe */
632355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk
642355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk#endif //OBOE_SHARED_RINGBUFFER_H
65