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