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 17 #ifndef ANDROID_AAUDIO_AUDIO_ENDPOINT_H 18 #define ANDROID_AAUDIO_AUDIO_ENDPOINT_H 19 20 #include <mutex> 21 22 #include <aaudio/AAudio.h> 23 24 #include "binding/AAudioServiceMessage.h" 25 #include "binding/AudioEndpointParcelable.h" 26 #include "fifo/FifoBuffer.h" 27 28 namespace aaudio { 29 30 #define ENDPOINT_DATA_QUEUE_SIZE_MIN 48 31 32 /** 33 * A sink for audio. 34 * Used by the client code. 35 */ 36 class AudioEndpoint { 37 38 public: 39 AudioEndpoint() = default; 40 41 /** 42 * Configure based on the EndPointDescriptor_t. 43 */ 44 aaudio_result_t configure(const EndpointDescriptor *pEndpointDescriptor, 45 aaudio_direction_t direction); 46 47 aaudio_result_t configureDataQueue(const RingBufferDescriptor &descriptor, 48 aaudio_direction_t direction); 49 50 /** 51 * Read from a command passed up from the Server. 52 * @return 1 if command received, 0 for no command, or negative error. 53 */ 54 aaudio_result_t readUpCommand(AAudioServiceMessage *commandPtr); 55 56 int32_t getEmptyFramesAvailable(android::WrappingBuffer *wrappingBuffer); 57 58 int32_t getEmptyFramesAvailable(); 59 60 int32_t getFullFramesAvailable(android::WrappingBuffer *wrappingBuffer); 61 62 int32_t getFullFramesAvailable(); 63 64 android::fifo_frames_t read(void* buffer, android::fifo_frames_t numFrames); 65 66 android::fifo_frames_t write(void* buffer, android::fifo_frames_t numFrames); 67 68 void advanceReadIndex(int32_t deltaFrames); 69 70 void advanceWriteIndex(int32_t deltaFrames); 71 72 /** 73 * Set the read index in the downData queue. 74 * This is needed if the reader is not updating the index itself. 75 */ 76 void setDataReadCounter(android::fifo_counter_t framesRead); 77 78 android::fifo_counter_t getDataReadCounter() const; 79 80 void setDataWriteCounter(android::fifo_counter_t framesWritten); 81 82 android::fifo_counter_t getDataWriteCounter() const; 83 84 /** 85 * The result is not valid until after configure() is called. 86 * 87 * @return true if the output buffer read position is not updated, eg. DMA 88 */ isFreeRunning()89 bool isFreeRunning() const { return mFreeRunning; } 90 91 int32_t setBufferSizeInFrames(int32_t requestedFrames, 92 int32_t *actualFrames); 93 int32_t getBufferSizeInFrames() const; 94 95 int32_t getBufferCapacityInFrames() const; 96 setThreshold(int32_t frames)97 void setThreshold(int32_t frames) { 98 mDataQueue->setThreshold(frames); 99 } 100 getThreshold()101 int32_t getThreshold() { 102 return mDataQueue->getThreshold(); 103 } 104 105 /** 106 * Write zeros to the data queue memory. 107 */ 108 void eraseDataMemory(); 109 110 void eraseEmptyDataMemory(int32_t numFrames); 111 freeDataQueue()112 void freeDataQueue() { mDataQueue.reset(); } 113 114 void dump() const; 115 116 private: 117 std::unique_ptr<android::FifoBufferIndirect> mUpCommandQueue; 118 std::unique_ptr<android::FifoBufferIndirect> mDataQueue; 119 bool mFreeRunning{false}; 120 android::fifo_counter_t mDataReadCounter{0}; // only used if free-running 121 android::fifo_counter_t mDataWriteCounter{0}; // only used if free-running 122 }; 123 124 } // namespace aaudio 125 126 #endif //ANDROID_AAUDIO_AUDIO_ENDPOINT_H 127