blob: 53ba0336c790050095c0326240247b3fa9c0eae3 [file] [log] [blame]
Phil Burk204a1632017-01-03 17:23:43 -08001/*
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
Phil Burk5204d312017-05-04 17:16:13 -070017#ifndef ANDROID_AAUDIO_AUDIO_ENDPOINT_H
18#define ANDROID_AAUDIO_AUDIO_ENDPOINT_H
Phil Burk204a1632017-01-03 17:23:43 -080019
Phil Burk5ed503c2017-02-01 09:38:15 -080020#include <aaudio/AAudio.h>
Phil Burk204a1632017-01-03 17:23:43 -080021
Phil Burkc0c70e32017-02-09 13:18:38 -080022#include "binding/AAudioServiceMessage.h"
23#include "binding/AudioEndpointParcelable.h"
Phil Burk204a1632017-01-03 17:23:43 -080024#include "fifo/FifoBuffer.h"
25
Phil Burk5ed503c2017-02-01 09:38:15 -080026namespace aaudio {
Phil Burk204a1632017-01-03 17:23:43 -080027
Phil Burkc0c70e32017-02-09 13:18:38 -080028#define ENDPOINT_DATA_QUEUE_SIZE_MIN 48
Phil Burk204a1632017-01-03 17:23:43 -080029
30/**
31 * A sink for audio.
32 * Used by the client code.
33 */
34class AudioEndpoint {
35
36public:
37 AudioEndpoint();
38 virtual ~AudioEndpoint();
39
40 /**
41 * Configure based on the EndPointDescriptor_t.
42 */
Phil Burk5ed503c2017-02-01 09:38:15 -080043 aaudio_result_t configure(const EndpointDescriptor *pEndpointDescriptor);
Phil Burk204a1632017-01-03 17:23:43 -080044
45 /**
46 * Read from a command passed up from the Server.
47 * @return 1 if command received, 0 for no command, or negative error.
48 */
Phil Burk5ed503c2017-02-01 09:38:15 -080049 aaudio_result_t readUpCommand(AAudioServiceMessage *commandPtr);
Phil Burk204a1632017-01-03 17:23:43 -080050
51 /**
52 * Non-blocking write.
53 * @return framesWritten or a negative error code.
54 */
Phil Burk5ed503c2017-02-01 09:38:15 -080055 aaudio_result_t writeDataNow(const void *buffer, int32_t numFrames);
Phil Burk204a1632017-01-03 17:23:43 -080056
Phil Burk87c9f642017-05-17 07:22:39 -070057 void getEmptyFramesAvailable(android::WrappingBuffer *wrappingBuffer);
Phil Burkc0c70e32017-02-09 13:18:38 -080058
Phil Burk4485d412017-05-09 15:55:02 -070059 int32_t getEmptyFramesAvailable();
Phil Burk87c9f642017-05-17 07:22:39 -070060
61 void getFullFramesAvailable(android::WrappingBuffer *wrappingBuffer);
62
Phil Burk4485d412017-05-09 15:55:02 -070063 int32_t getFullFramesAvailable();
64
Phil Burk87c9f642017-05-17 07:22:39 -070065 void advanceReadIndex(int32_t deltaFrames);
66
Phil Burkc0c70e32017-02-09 13:18:38 -080067 void advanceWriteIndex(int32_t deltaFrames);
68
Phil Burk204a1632017-01-03 17:23:43 -080069 /**
70 * Set the read index in the downData queue.
71 * This is needed if the reader is not updating the index itself.
72 */
Phil Burk87c9f642017-05-17 07:22:39 -070073 void setDataReadCounter(android::fifo_counter_t framesRead);
Phil Burk204a1632017-01-03 17:23:43 -080074
Phil Burk87c9f642017-05-17 07:22:39 -070075 android::fifo_counter_t getDataReadCounter();
76
77 void setDataWriteCounter(android::fifo_counter_t framesWritten);
78
79 android::fifo_counter_t getDataWriteCounter();
Phil Burk204a1632017-01-03 17:23:43 -080080
81 /**
82 * The result is not valid until after configure() is called.
83 *
84 * @return true if the output buffer read position is not updated, eg. DMA
85 */
Phil Burk87c9f642017-05-17 07:22:39 -070086 bool isFreeRunning() const { return mFreeRunning; }
Phil Burk204a1632017-01-03 17:23:43 -080087
Phil Burk3316d5e2017-02-15 11:23:01 -080088 int32_t setBufferSizeInFrames(int32_t requestedFrames,
89 int32_t *actualFrames);
90 int32_t getBufferSizeInFrames() const;
Phil Burk204a1632017-01-03 17:23:43 -080091
Phil Burk3316d5e2017-02-15 11:23:01 -080092 int32_t getBufferCapacityInFrames() const;
Phil Burk204a1632017-01-03 17:23:43 -080093
Phil Burk204a1632017-01-03 17:23:43 -080094private:
Phil Burkc0c70e32017-02-09 13:18:38 -080095 android::FifoBuffer *mUpCommandQueue;
Phil Burk87c9f642017-05-17 07:22:39 -070096 android::FifoBuffer *mDataQueue;
97 bool mFreeRunning;
Phil Burkc0c70e32017-02-09 13:18:38 -080098 android::fifo_counter_t mDataReadCounter; // only used if free-running
99 android::fifo_counter_t mDataWriteCounter; // only used if free-running
Phil Burk204a1632017-01-03 17:23:43 -0800100};
101
Phil Burk5ed503c2017-02-01 09:38:15 -0800102} // namespace aaudio
Phil Burk204a1632017-01-03 17:23:43 -0800103
Phil Burk5204d312017-05-04 17:16:13 -0700104#endif //ANDROID_AAUDIO_AUDIO_ENDPOINT_H