blob: c271dbda8d707606e16dd6835031612d73fb0502 [file] [log] [blame]
Phil Burkc0c70e32017-02-09 13:18:38 -08001/*
2 * Copyright (C) 2017 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 AAUDIO_SERVICE_ENDPOINT_H
18#define AAUDIO_SERVICE_ENDPOINT_H
19
20#include <atomic>
21#include <functional>
22#include <mutex>
23#include <vector>
24
25#include "client/AudioStreamInternal.h"
Phil Burk87c9f642017-05-17 07:22:39 -070026#include "client/AudioStreamInternalPlay.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080027#include "binding/AAudioServiceMessage.h"
28#include "AAudioServiceStreamShared.h"
29#include "AAudioServiceStreamMMAP.h"
30#include "AAudioMixer.h"
31#include "AAudioService.h"
32
33namespace aaudio {
34
35class AAudioServiceEndpoint {
36public:
Phil Burk87c9f642017-05-17 07:22:39 -070037 virtual ~AAudioServiceEndpoint() = default;
Phil Burkc0c70e32017-02-09 13:18:38 -080038
Phil Burk87c9f642017-05-17 07:22:39 -070039 virtual aaudio_result_t open(int32_t deviceId);
Phil Burkc0c70e32017-02-09 13:18:38 -080040
Phil Burk87c9f642017-05-17 07:22:39 -070041 int32_t getSampleRate() const { return mStreamInternal->getSampleRate(); }
42 int32_t getSamplesPerFrame() const { return mStreamInternal->getSamplesPerFrame(); }
43 int32_t getFramesPerBurst() const { return mStreamInternal->getFramesPerBurst(); }
Phil Burkc0c70e32017-02-09 13:18:38 -080044
45 aaudio_result_t registerStream(AAudioServiceStreamShared *sharedStream);
46 aaudio_result_t unregisterStream(AAudioServiceStreamShared *sharedStream);
47 aaudio_result_t startStream(AAudioServiceStreamShared *sharedStream);
48 aaudio_result_t stopStream(AAudioServiceStreamShared *sharedStream);
49 aaudio_result_t close();
50
Phil Burkec89b2e2017-06-20 15:05:06 -070051 int32_t getRequestedDeviceId() const { return mRequestedDeviceId; }
Phil Burk87c9f642017-05-17 07:22:39 -070052 int32_t getDeviceId() const { return mStreamInternal->getDeviceId(); }
Phil Burkc0c70e32017-02-09 13:18:38 -080053
Phil Burk87c9f642017-05-17 07:22:39 -070054 aaudio_direction_t getDirection() const { return mStreamInternal->getDirection(); }
Phil Burkc0c70e32017-02-09 13:18:38 -080055
56 void disconnectRegisteredStreams();
57
Phil Burk87c9f642017-05-17 07:22:39 -070058 virtual void *callbackLoop() = 0;
Phil Burkc0c70e32017-02-09 13:18:38 -080059
Phil Burk71f35bb2017-04-13 16:05:07 -070060 // This should only be called from the AAudioEndpointManager under a mutex.
61 int32_t getReferenceCount() const {
62 return mReferenceCount;
63 }
64
65 // This should only be called from the AAudioEndpointManager under a mutex.
66 void setReferenceCount(int32_t count) {
67 mReferenceCount = count;
68 }
69
Phil Burk87c9f642017-05-17 07:22:39 -070070 virtual AudioStreamInternal *getStreamInternal() = 0;
Phil Burkc0c70e32017-02-09 13:18:38 -080071
72 std::atomic<bool> mCallbackEnabled;
73
74 std::mutex mLockStreams;
Phil Burk87c9f642017-05-17 07:22:39 -070075
Phil Burkc0c70e32017-02-09 13:18:38 -080076 std::vector<AAudioServiceStreamShared *> mRegisteredStreams;
77 std::vector<AAudioServiceStreamShared *> mRunningStreams;
Phil Burk71f35bb2017-04-13 16:05:07 -070078
Phil Burk87c9f642017-05-17 07:22:39 -070079private:
80 aaudio_result_t startSharingThread_l();
81 aaudio_result_t stopSharingThread();
82
83 AudioStreamInternal *mStreamInternal = nullptr;
84 int32_t mReferenceCount = 0;
Phil Burkec89b2e2017-06-20 15:05:06 -070085 int32_t mRequestedDeviceId = 0;
Phil Burkc0c70e32017-02-09 13:18:38 -080086};
87
88} /* namespace aaudio */
89
90
91#endif //AAUDIO_SERVICE_ENDPOINT_H