blob: 484d917ba27f8bdcf48978827213dcb74d38152d [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();
Phil Burk204a1632017-01-03 17:23:43 -080038
39 /**
40 * Configure based on the EndPointDescriptor_t.
41 */
Phil Burkfd34a932017-07-19 07:03:52 -070042 aaudio_result_t configure(const EndpointDescriptor *pEndpointDescriptor,
43 aaudio_direction_t direction);
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
Phil Burkfd34a932017-07-19 07:03:52 -070051 int32_t getEmptyFramesAvailable(android::WrappingBuffer *wrappingBuffer);
Phil Burkc0c70e32017-02-09 13:18:38 -080052
Phil Burk4485d412017-05-09 15:55:02 -070053 int32_t getEmptyFramesAvailable();
Phil Burk87c9f642017-05-17 07:22:39 -070054
Phil Burkfd34a932017-07-19 07:03:52 -070055 int32_t getFullFramesAvailable(android::WrappingBuffer *wrappingBuffer);
Phil Burk87c9f642017-05-17 07:22:39 -070056
Phil Burk4485d412017-05-09 15:55:02 -070057 int32_t getFullFramesAvailable();
58
Phil Burk87c9f642017-05-17 07:22:39 -070059 void advanceReadIndex(int32_t deltaFrames);
60
Phil Burkc0c70e32017-02-09 13:18:38 -080061 void advanceWriteIndex(int32_t deltaFrames);
62
Phil Burk204a1632017-01-03 17:23:43 -080063 /**
64 * Set the read index in the downData queue.
65 * This is needed if the reader is not updating the index itself.
66 */
Phil Burk87c9f642017-05-17 07:22:39 -070067 void setDataReadCounter(android::fifo_counter_t framesRead);
Phil Burk204a1632017-01-03 17:23:43 -080068
Phil Burk5edc4ea2020-04-17 08:15:42 -070069 android::fifo_counter_t getDataReadCounter() const;
Phil Burk87c9f642017-05-17 07:22:39 -070070
71 void setDataWriteCounter(android::fifo_counter_t framesWritten);
72
Phil Burk5edc4ea2020-04-17 08:15:42 -070073 android::fifo_counter_t getDataWriteCounter() const;
Phil Burk204a1632017-01-03 17:23:43 -080074
75 /**
76 * The result is not valid until after configure() is called.
77 *
78 * @return true if the output buffer read position is not updated, eg. DMA
79 */
Phil Burk87c9f642017-05-17 07:22:39 -070080 bool isFreeRunning() const { return mFreeRunning; }
Phil Burk204a1632017-01-03 17:23:43 -080081
Phil Burk3316d5e2017-02-15 11:23:01 -080082 int32_t setBufferSizeInFrames(int32_t requestedFrames,
83 int32_t *actualFrames);
84 int32_t getBufferSizeInFrames() const;
Phil Burk204a1632017-01-03 17:23:43 -080085
Phil Burk3316d5e2017-02-15 11:23:01 -080086 int32_t getBufferCapacityInFrames() const;
Phil Burk204a1632017-01-03 17:23:43 -080087
Phil Burkea04d972017-08-07 12:30:44 -070088 /**
89 * Write zeros to the data queue memory.
90 */
91 void eraseDataMemory();
92
Phil Burkec89b2e2017-06-20 15:05:06 -070093 void dump() const;
94
Phil Burk204a1632017-01-03 17:23:43 -080095private:
Phil Burk5edc4ea2020-04-17 08:15:42 -070096 std::unique_ptr<android::FifoBuffer> mUpCommandQueue;
97 std::unique_ptr<android::FifoBuffer> mDataQueue;
Phil Burk87c9f642017-05-17 07:22:39 -070098 bool mFreeRunning;
Phil Burkc0c70e32017-02-09 13:18:38 -080099 android::fifo_counter_t mDataReadCounter; // only used if free-running
100 android::fifo_counter_t mDataWriteCounter; // only used if free-running
Phil Burk204a1632017-01-03 17:23:43 -0800101};
102
Phil Burk5ed503c2017-02-01 09:38:15 -0800103} // namespace aaudio
Phil Burk204a1632017-01-03 17:23:43 -0800104
Phil Burk5204d312017-05-04 17:16:13 -0700105#endif //ANDROID_AAUDIO_AUDIO_ENDPOINT_H