blob: 43631918b2fa013eed2391c25fede520bb427215 [file] [log] [blame]
Phil Burk828bea52017-01-03 17:22:09 -08001/*
2 * Copyright 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 Burk5204d312017-05-04 17:16:13 -070017#ifndef ANDROID_AAUDIO_RINGBUFFER_PARCELABLE_H
18#define ANDROID_AAUDIO_RINGBUFFER_PARCELABLE_H
Phil Burk828bea52017-01-03 17:22:09 -080019
jiabinfc791ee2023-02-15 19:43:40 +000020#include <map>
Phil Burk828bea52017-01-03 17:22:09 -080021#include <stdint.h>
22
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070023#include <aaudio/RingBuffer.h>
Phil Burk828bea52017-01-03 17:22:09 -080024#include <binder/Parcelable.h>
25
Phil Burk5ed503c2017-02-01 09:38:15 -080026#include "binding/AAudioServiceDefinitions.h"
Phil Burk828bea52017-01-03 17:22:09 -080027#include "binding/SharedRegionParcelable.h"
28
Phil Burk5ed503c2017-02-01 09:38:15 -080029namespace aaudio {
Phil Burk828bea52017-01-03 17:22:09 -080030
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070031class RingBufferParcelable {
Phil Burk828bea52017-01-03 17:22:09 -080032public:
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070033 RingBufferParcelable() = default;
34
35 // Construct based on a parcelable representation.
36 explicit RingBufferParcelable(const RingBuffer& parcelable);
Phil Burk828bea52017-01-03 17:22:09 -080037
38 // TODO This assumes that all three use the same SharedMemoryParcelable
39 void setupMemory(int32_t sharedMemoryIndex,
40 int32_t dataMemoryOffset,
41 int32_t dataSizeInBytes,
42 int32_t readCounterOffset,
43 int32_t writeCounterOffset,
44 int32_t counterSizeBytes);
45
46 void setupMemory(int32_t sharedMemoryIndex,
47 int32_t dataMemoryOffset,
48 int32_t dataSizeInBytes);
49
jiabinfc791ee2023-02-15 19:43:40 +000050 /**
51 * Set up memory for the RingBufferParcelable.
52 *
53 * This function will take three MemoryInfoTuple as parameters to set up memory. The
54 * MemoryInfoTuple contains the shared memory index, offset in the shared memory and size
55 * of the object. This will allow setting up the read counter, write counter and data memory
56 * that are located in different shared memory blocks.
57 *
58 * @param dataMemoryInfo
59 * @param readCounterInfo
60 * @param writeCounterInfo
61 */
62 void setupMemory(const SharedRegionParcelable::MemoryInfoTuple& dataMemoryInfo,
63 const SharedRegionParcelable::MemoryInfoTuple& readCounterInfo,
64 const SharedRegionParcelable::MemoryInfoTuple& writeCounterInfo);
65
jiabinf7f06152021-11-22 18:10:14 +000066 int32_t getBytesPerFrame() const;
Phil Burk828bea52017-01-03 17:22:09 -080067
68 void setBytesPerFrame(int32_t bytesPerFrame);
69
jiabinf7f06152021-11-22 18:10:14 +000070 int32_t getFramesPerBurst() const;
Phil Burk828bea52017-01-03 17:22:09 -080071
72 void setFramesPerBurst(int32_t framesPerBurst);
73
jiabinf7f06152021-11-22 18:10:14 +000074 int32_t getCapacityInFrames() const;
Phil Burk828bea52017-01-03 17:22:09 -080075
76 void setCapacityInFrames(int32_t capacityInFrames);
77
Phil Burkc0c70e32017-02-09 13:18:38 -080078 bool isFileDescriptorSafe(SharedMemoryParcelable *memoryParcels);
79
Phil Burk5ed503c2017-02-01 09:38:15 -080080 aaudio_result_t resolve(SharedMemoryParcelable *memoryParcels, RingBufferDescriptor *descriptor);
Phil Burk828bea52017-01-03 17:22:09 -080081
jiabinfc791ee2023-02-15 19:43:40 +000082 /**
83 * Update this ring buffer with the given ring buffer.
84 *
85 * @param parcelable the ring buffer to be used to update this ring buffer.
86 * @param memoryIndexMap a map from the shared memory indexes used by the given ring buffer
87 * to the shared memory indexes used by this ring buffer.
88 */
89 void updateMemory(const RingBufferParcelable& parcelable,
90 const std::map<int32_t, int32_t>& memoryIndexMap);
jiabinf7f06152021-11-22 18:10:14 +000091
jiabinfc791ee2023-02-15 19:43:40 +000092 int32_t getReadCounterSharedMemoryIndex() const {
93 return mReadCounterParcelable.getSharedMemoryIndex();
94 }
95 int32_t getWriteCounterSharedMemoryIndex() const {
96 return mWriteCounterParcelable.getSharedMemoryIndex();
97 }
98 int32_t getDataSharedMemoryIndex() const {
99 return mDataParcelable.getSharedMemoryIndex();
jiabinf7f06152021-11-22 18:10:14 +0000100 }
101
Phil Burk828bea52017-01-03 17:22:09 -0800102 void dump();
103
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700104 // Extract a parcelable representation of this object.
105 RingBuffer parcelable() const;
106
Phil Burk828bea52017-01-03 17:22:09 -0800107private:
108 SharedRegionParcelable mReadCounterParcelable;
109 SharedRegionParcelable mWriteCounterParcelable;
110 SharedRegionParcelable mDataParcelable;
111 int32_t mBytesPerFrame = 0; // index is in frames
112 int32_t mFramesPerBurst = 0; // for ISOCHRONOUS queues
113 int32_t mCapacityInFrames = 0; // zero if unused
114 RingbufferFlags mFlags = RingbufferFlags::NONE;
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700115
116 aaudio_result_t validate() const;
Phil Burk828bea52017-01-03 17:22:09 -0800117};
118
Phil Burk5ed503c2017-02-01 09:38:15 -0800119} /* namespace aaudio */
Phil Burk828bea52017-01-03 17:22:09 -0800120
Phil Burk5204d312017-05-04 17:16:13 -0700121#endif //ANDROID_AAUDIO_RINGBUFFER_PARCELABLE_H