1204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk/*
2204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk * Copyright (C) 2016 The Android Open Source Project
3204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk *
4204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk * Licensed under the Apache License, Version 2.0 (the "License");
5204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk * you may not use this file except in compliance with the License.
6204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk * You may obtain a copy of the License at
7204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk *
8204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk *      http://www.apache.org/licenses/LICENSE-2.0
9204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk *
10204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk * Unless required by applicable law or agreed to in writing, software
11204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk * distributed under the License is distributed on an "AS IS" BASIS,
12204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk * See the License for the specific language governing permissions and
14204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk * limitations under the License.
15204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk */
16204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk
175204d315c6c6f53188f8d1414dd1b55b6c90142bPhil Burk#ifndef ANDROID_AAUDIO_AUDIO_ENDPOINT_H
185204d315c6c6f53188f8d1414dd1b55b6c90142bPhil Burk#define ANDROID_AAUDIO_AUDIO_ENDPOINT_H
19204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk
205ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk#include <aaudio/AAudio.h>
21204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk
22c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk#include "binding/AAudioServiceMessage.h"
23c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk#include "binding/AudioEndpointParcelable.h"
24204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk#include "fifo/FifoBuffer.h"
25204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk
265ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burknamespace aaudio {
27204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk
28c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk#define ENDPOINT_DATA_QUEUE_SIZE_MIN   48
29204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk
30204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk/**
31204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk * A sink for audio.
32204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk * Used by the client code.
33204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk */
34204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burkclass AudioEndpoint {
35204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk
36204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burkpublic:
37204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    AudioEndpoint();
38204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    virtual ~AudioEndpoint();
39204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk
40204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    /**
41204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk     * Configure based on the EndPointDescriptor_t.
42204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk     */
435ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    aaudio_result_t configure(const EndpointDescriptor *pEndpointDescriptor);
44204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk
45204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    /**
46204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk     * Read from a command passed up from the Server.
47204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk     * @return 1 if command received, 0 for no command, or negative error.
48204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk     */
495ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    aaudio_result_t readUpCommand(AAudioServiceMessage *commandPtr);
50204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk
51204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    /**
52204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk     * Non-blocking write.
53204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk     * @return framesWritten or a negative error code.
54204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk     */
555ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    aaudio_result_t writeDataNow(const void *buffer, int32_t numFrames);
56204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk
5787c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk    void getEmptyFramesAvailable(android::WrappingBuffer *wrappingBuffer);
58c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk
594485d41bcded0eceec7ec97d50aa2b0e702397a0Phil Burk    int32_t getEmptyFramesAvailable();
6087c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk
6187c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk    void getFullFramesAvailable(android::WrappingBuffer *wrappingBuffer);
6287c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk
634485d41bcded0eceec7ec97d50aa2b0e702397a0Phil Burk    int32_t getFullFramesAvailable();
644485d41bcded0eceec7ec97d50aa2b0e702397a0Phil Burk
6587c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk    void advanceReadIndex(int32_t deltaFrames);
6687c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk
67c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    void advanceWriteIndex(int32_t deltaFrames);
68c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk
69204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    /**
70204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk     * Set the read index in the downData queue.
71204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk     * This is needed if the reader is not updating the index itself.
72204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk     */
7387c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk    void setDataReadCounter(android::fifo_counter_t framesRead);
7487c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk
7587c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk    android::fifo_counter_t getDataReadCounter();
7687c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk
7787c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk    void setDataWriteCounter(android::fifo_counter_t framesWritten);
78204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk
7987c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk    android::fifo_counter_t getDataWriteCounter();
80204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk
81204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    /**
82204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk     * The result is not valid until after configure() is called.
83204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk     *
84204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk     * @return true if the output buffer read position is not updated, eg. DMA
85204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk     */
8687c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk    bool isFreeRunning() const { return mFreeRunning; }
87204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk
883316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    int32_t setBufferSizeInFrames(int32_t requestedFrames,
893316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk                                  int32_t *actualFrames);
903316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    int32_t getBufferSizeInFrames() const;
91204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk
923316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    int32_t getBufferCapacityInFrames() const;
93204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk
94204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burkprivate:
95c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    android::FifoBuffer    *mUpCommandQueue;
9687c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk    android::FifoBuffer    *mDataQueue;
9787c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk    bool                    mFreeRunning;
98c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    android::fifo_counter_t mDataReadCounter; // only used if free-running
99c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    android::fifo_counter_t mDataWriteCounter; // only used if free-running
100204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk};
101204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk
1025ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk} // namespace aaudio
103204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk
1045204d315c6c6f53188f8d1414dd1b55b6c90142bPhil Burk#endif //ANDROID_AAUDIO_AUDIO_ENDPOINT_H
105