blob: 603d497106fc810d309a78dc28c59e606df14e92 [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 Burk4501b352017-06-29 18:12:36 -070039 std::string dump() const;
40
Eric Laurenta17ae742017-06-29 15:43:55 -070041 virtual aaudio_result_t open(const AAudioStreamConfiguration& configuration);
Phil Burkc0c70e32017-02-09 13:18:38 -080042
Phil Burk87c9f642017-05-17 07:22:39 -070043 int32_t getSampleRate() const { return mStreamInternal->getSampleRate(); }
44 int32_t getSamplesPerFrame() const { return mStreamInternal->getSamplesPerFrame(); }
45 int32_t getFramesPerBurst() const { return mStreamInternal->getFramesPerBurst(); }
Phil Burkc0c70e32017-02-09 13:18:38 -080046
Phil Burk11e8d332017-05-24 09:59:02 -070047 aaudio_result_t registerStream(android::sp<AAudioServiceStreamShared> sharedStream);
48 aaudio_result_t unregisterStream(android::sp<AAudioServiceStreamShared> sharedStream);
49 aaudio_result_t startStream(android::sp<AAudioServiceStreamShared> sharedStream);
50 aaudio_result_t stopStream(android::sp<AAudioServiceStreamShared> sharedStream);
Phil Burkc0c70e32017-02-09 13:18:38 -080051 aaudio_result_t close();
52
Phil Burkec89b2e2017-06-20 15:05:06 -070053 int32_t getRequestedDeviceId() const { return mRequestedDeviceId; }
Phil Burk87c9f642017-05-17 07:22:39 -070054 int32_t getDeviceId() const { return mStreamInternal->getDeviceId(); }
Phil Burkc0c70e32017-02-09 13:18:38 -080055
Phil Burk87c9f642017-05-17 07:22:39 -070056 aaudio_direction_t getDirection() const { return mStreamInternal->getDirection(); }
Phil Burkc0c70e32017-02-09 13:18:38 -080057
58 void disconnectRegisteredStreams();
59
Phil Burk87c9f642017-05-17 07:22:39 -070060 virtual void *callbackLoop() = 0;
Phil Burkc0c70e32017-02-09 13:18:38 -080061
Phil Burk71f35bb2017-04-13 16:05:07 -070062 // This should only be called from the AAudioEndpointManager under a mutex.
63 int32_t getReferenceCount() const {
64 return mReferenceCount;
65 }
66
67 // This should only be called from the AAudioEndpointManager under a mutex.
68 void setReferenceCount(int32_t count) {
69 mReferenceCount = count;
70 }
71
Phil Burk97350f92017-07-21 15:59:44 -070072 aaudio_result_t getTimestamp(int64_t *positionFrames, int64_t *timeNanos);
73
Eric Laurenta17ae742017-06-29 15:43:55 -070074 bool matches(const AAudioStreamConfiguration& configuration);
75
Phil Burk87c9f642017-05-17 07:22:39 -070076 virtual AudioStreamInternal *getStreamInternal() = 0;
Phil Burkc0c70e32017-02-09 13:18:38 -080077
Phil Burk940083c2017-07-17 17:00:02 -070078 std::atomic<bool> mCallbackEnabled{false};
Phil Burkc0c70e32017-02-09 13:18:38 -080079
Phil Burk4501b352017-06-29 18:12:36 -070080 mutable std::mutex mLockStreams;
Phil Burk87c9f642017-05-17 07:22:39 -070081
Phil Burk11e8d332017-05-24 09:59:02 -070082 std::vector<android::sp<AAudioServiceStreamShared>> mRegisteredStreams;
83
Phil Burk940083c2017-07-17 17:00:02 -070084 std::atomic<int> mRunningStreams{0};
Phil Burk71f35bb2017-04-13 16:05:07 -070085
Phil Burk87c9f642017-05-17 07:22:39 -070086private:
87 aaudio_result_t startSharingThread_l();
88 aaudio_result_t stopSharingThread();
89
90 AudioStreamInternal *mStreamInternal = nullptr;
91 int32_t mReferenceCount = 0;
Phil Burkec89b2e2017-06-20 15:05:06 -070092 int32_t mRequestedDeviceId = 0;
Phil Burkc0c70e32017-02-09 13:18:38 -080093};
94
95} /* namespace aaudio */
96
97
98#endif //AAUDIO_SERVICE_ENDPOINT_H