1c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk/*
2c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk * Copyright (C) 2017 The Android Open Source Project
3c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk *
4c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk * Licensed under the Apache License, Version 2.0 (the "License");
5c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk * you may not use this file except in compliance with the License.
6c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk * You may obtain a copy of the License at
7c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk *
8c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk *      http://www.apache.org/licenses/LICENSE-2.0
9c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk *
10c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk * Unless required by applicable law or agreed to in writing, software
11c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk * distributed under the License is distributed on an "AS IS" BASIS,
12c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk * See the License for the specific language governing permissions and
14c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk * limitations under the License.
15c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk */
16c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk
17c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk#ifndef AAUDIO_SERVICE_ENDPOINT_H
18c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk#define AAUDIO_SERVICE_ENDPOINT_H
19c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk
20c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk#include <atomic>
21c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk#include <functional>
22c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk#include <mutex>
23c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk#include <vector>
24c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk
25c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk#include "client/AudioStreamInternal.h"
2687c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk#include "client/AudioStreamInternalPlay.h"
27c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk#include "binding/AAudioServiceMessage.h"
28c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk#include "AAudioServiceStreamShared.h"
29c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk#include "AAudioServiceStreamMMAP.h"
30c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk#include "AAudioMixer.h"
31c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk#include "AAudioService.h"
32c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk
33c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burknamespace aaudio {
34c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk
35c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burkclass AAudioServiceEndpoint {
36c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burkpublic:
3787c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk    virtual ~AAudioServiceEndpoint() = default;
38c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk
3987c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk    virtual aaudio_result_t open(int32_t deviceId);
40c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk
4187c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk    int32_t getSampleRate() const { return mStreamInternal->getSampleRate(); }
4287c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk    int32_t getSamplesPerFrame() const { return mStreamInternal->getSamplesPerFrame();  }
4387c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk    int32_t getFramesPerBurst() const { return mStreamInternal->getFramesPerBurst();  }
44c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk
45c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    aaudio_result_t registerStream(AAudioServiceStreamShared *sharedStream);
46c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    aaudio_result_t unregisterStream(AAudioServiceStreamShared *sharedStream);
47c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    aaudio_result_t startStream(AAudioServiceStreamShared *sharedStream);
48c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    aaudio_result_t stopStream(AAudioServiceStreamShared *sharedStream);
49c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    aaudio_result_t close();
50c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk
5187c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk    int32_t getDeviceId() const { return mStreamInternal->getDeviceId(); }
52c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk
5387c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk    aaudio_direction_t getDirection() const { return mStreamInternal->getDirection(); }
54c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk
55c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    void disconnectRegisteredStreams();
56c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk
5787c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk    virtual void *callbackLoop() = 0;
58c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk
5971f35bb687476694882a617ba4a810a0bb56fe23Phil Burk    // This should only be called from the AAudioEndpointManager under a mutex.
6071f35bb687476694882a617ba4a810a0bb56fe23Phil Burk    int32_t getReferenceCount() const {
6171f35bb687476694882a617ba4a810a0bb56fe23Phil Burk        return mReferenceCount;
6271f35bb687476694882a617ba4a810a0bb56fe23Phil Burk    }
6371f35bb687476694882a617ba4a810a0bb56fe23Phil Burk
6471f35bb687476694882a617ba4a810a0bb56fe23Phil Burk    // This should only be called from the AAudioEndpointManager under a mutex.
6571f35bb687476694882a617ba4a810a0bb56fe23Phil Burk    void setReferenceCount(int32_t count) {
6671f35bb687476694882a617ba4a810a0bb56fe23Phil Burk        mReferenceCount = count;
6771f35bb687476694882a617ba4a810a0bb56fe23Phil Burk    }
6871f35bb687476694882a617ba4a810a0bb56fe23Phil Burk
6987c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk    virtual AudioStreamInternal *getStreamInternal() = 0;
70c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk
71c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    std::atomic<bool>        mCallbackEnabled;
72c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk
73c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    std::mutex               mLockStreams;
7487c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk
75c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    std::vector<AAudioServiceStreamShared *> mRegisteredStreams;
76c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    std::vector<AAudioServiceStreamShared *> mRunningStreams;
7771f35bb687476694882a617ba4a810a0bb56fe23Phil Burk
7887c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burkprivate:
7987c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk    aaudio_result_t startSharingThread_l();
8087c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk    aaudio_result_t stopSharingThread();
8187c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk
8287c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk    AudioStreamInternal     *mStreamInternal = nullptr;
8387c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk    int32_t                  mReferenceCount = 0;
84c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk};
85c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk
86c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk} /* namespace aaudio */
87c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk
88c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk
89c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk#endif //AAUDIO_SERVICE_ENDPOINT_H
90