blob: dff571be6c5e51fb3ead7a88fcd2df28e1bd51c2 [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
Phil Burk0bd745e2020-10-17 18:20:01 +000025#include <android-base/thread_annotations.h>
26
Phil Burkc0c70e32017-02-09 13:18:38 -080027#include "client/AudioStreamInternal.h"
Phil Burk87c9f642017-05-17 07:22:39 -070028#include "client/AudioStreamInternalPlay.h"
Phil Burk15f7cab2017-08-04 09:13:31 -070029#include "core/AAudioStreamParameters.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080030#include "binding/AAudioServiceMessage.h"
Phil Burk15f7cab2017-08-04 09:13:31 -070031#include "binding/AAudioStreamConfiguration.h"
32
33#include "AAudioServiceStreamBase.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080034
35namespace aaudio {
36
Phil Burk15f7cab2017-08-04 09:13:31 -070037/**
38 * AAudioServiceEndpoint is used by a subclass of AAudioServiceStreamBase
39 * to communicate with the underlying audio device or port.
40 */
41class AAudioServiceEndpoint
42 : public virtual android::RefBase
43 , public AAudioStreamParameters {
Phil Burkc0c70e32017-02-09 13:18:38 -080044public:
Phil Burkc0c70e32017-02-09 13:18:38 -080045
jiabin613e6ae2022-12-21 20:20:11 +000046 ~AAudioServiceEndpoint() override;
Phil Burk4501b352017-06-29 18:12:36 -070047
Phil Burk15f7cab2017-08-04 09:13:31 -070048 virtual std::string dump() const;
Phil Burkc0c70e32017-02-09 13:18:38 -080049
Phil Burk15f7cab2017-08-04 09:13:31 -070050 virtual aaudio_result_t open(const aaudio::AAudioStreamRequest &request) = 0;
Phil Burkc0c70e32017-02-09 13:18:38 -080051
Phil Burk320910f2020-08-12 14:29:10 +000052 /*
53 * Perform any cleanup necessary before deleting the stream.
54 * This might include releasing and closing internal streams.
55 */
56 virtual void close() = 0;
Phil Burk15f7cab2017-08-04 09:13:31 -070057
jiabin613e6ae2022-12-21 20:20:11 +000058 aaudio_result_t registerStream(const android::sp<AAudioServiceStreamBase>& stream);
Phil Burk15f7cab2017-08-04 09:13:31 -070059
jiabin613e6ae2022-12-21 20:20:11 +000060 aaudio_result_t unregisterStream(const android::sp<AAudioServiceStreamBase>& stream);
Phil Burk15f7cab2017-08-04 09:13:31 -070061
62 virtual aaudio_result_t startStream(android::sp<AAudioServiceStreamBase> stream,
63 audio_port_handle_t *clientHandle) = 0;
64
65 virtual aaudio_result_t stopStream(android::sp<AAudioServiceStreamBase> stream,
66 audio_port_handle_t clientHandle) = 0;
67
jiabin613e6ae2022-12-21 20:20:11 +000068 virtual aaudio_result_t startClient(const android::AudioClient& /*client*/,
69 const audio_attributes_t* /*attr*/,
70 audio_port_handle_t* /*clientHandle*/) {
Phil Burk7ba46552019-04-15 08:58:08 -070071 ALOGD("AAudioServiceEndpoint::startClient(...) AAUDIO_ERROR_UNAVAILABLE");
Phil Burk15f7cab2017-08-04 09:13:31 -070072 return AAUDIO_ERROR_UNAVAILABLE;
73 }
74
jiabin613e6ae2022-12-21 20:20:11 +000075 virtual aaudio_result_t stopClient(audio_port_handle_t /*clientHandle*/) {
Phil Burk15f7cab2017-08-04 09:13:31 -070076 ALOGD("AAudioServiceEndpoint::stopClient(...) AAUDIO_ERROR_UNAVAILABLE");
77 return AAUDIO_ERROR_UNAVAILABLE;
78 }
79
jiabinf7f06152021-11-22 18:10:14 +000080 virtual aaudio_result_t standby() {
81 ALOGD("AAudioServiceEndpoint::standby() AAUDIO_ERROR_UNAVAILABLE");
82 return AAUDIO_ERROR_UNAVAILABLE;
83 }
84
jiabin613e6ae2022-12-21 20:20:11 +000085 virtual aaudio_result_t exitStandby(AudioEndpointParcelable* /*parcelable*/) {
jiabinf7f06152021-11-22 18:10:14 +000086 ALOGD("AAudioServiceEndpoint::exitStandby() AAUDIO_ERROR_UNAVAILABLE");
87 return AAUDIO_ERROR_UNAVAILABLE;
88 }
89
Phil Burk15f7cab2017-08-04 09:13:31 -070090 /**
91 * @param positionFrames
92 * @param timeNanos
93 * @return AAUDIO_OK or AAUDIO_ERROR_UNAVAILABLE or other negative error
94 */
95 virtual aaudio_result_t getFreeRunningPosition(int64_t *positionFrames, int64_t *timeNanos) = 0;
96
Phil Burk56d34f22018-10-11 13:30:56 -070097 /**
98 * Set time that the associated frame was presented to the hardware.
99 *
100 * @param positionFrames receive position, input value is ignored
101 * @param timeNanos receive time, input value is ignored
102 * @return AAUDIO_OK or AAUDIO_ERROR_UNAVAILABLE or other negative error
103 */
Phil Burk15f7cab2017-08-04 09:13:31 -0700104 virtual aaudio_result_t getTimestamp(int64_t *positionFrames, int64_t *timeNanos) = 0;
105
106 int32_t getFramesPerBurst() const {
107 return mFramesPerBurst;
108 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800109
Phil Burkec89b2e2017-06-20 15:05:06 -0700110 int32_t getRequestedDeviceId() const { return mRequestedDeviceId; }
Phil Burk97350f92017-07-21 15:59:44 -0700111
Eric Laurenta17ae742017-06-29 15:43:55 -0700112 bool matches(const AAudioStreamConfiguration& configuration);
113
Phil Burk15f7cab2017-08-04 09:13:31 -0700114 // This should only be called from the AAudioEndpointManager under a mutex.
115 int32_t getOpenCount() const {
116 return mOpenCount;
117 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800118
Phil Burk15f7cab2017-08-04 09:13:31 -0700119 // This should only be called from the AAudioEndpointManager under a mutex.
120 void setOpenCount(int32_t count) {
121 mOpenCount = count;
122 }
123
Phil Burkbe0a5b62017-10-12 15:56:00 -0700124 bool isConnected() const {
125 return mConnected;
126 }
127
Phil Burka9876702020-04-20 18:16:15 -0700128 static audio_attributes_t getAudioAttributesFrom(const AAudioStreamParameters *params);
129
Phil Burk6e463ce2020-04-13 10:20:20 -0700130 // Stop, disconnect and release any streams registered on this endpoint.
131 void releaseRegisteredStreams();
132
133 bool isForSharing() const {
134 return mForSharing;
135 }
136
137 /**
138 *
139 * @param flag true if this endpoint is to be shared between multiple streams
140 */
141 void setForSharing(bool flag) {
142 mForSharing = flag;
143 }
144
Phil Burk15f7cab2017-08-04 09:13:31 -0700145protected:
Phil Burkbbd52862018-04-13 11:37:42 -0700146
147 /**
148 * @param portHandle
149 * @return return true if a stream with the given portHandle is registered
150 */
151 bool isStreamRegistered(audio_port_handle_t portHandle);
152
Phil Burk6e463ce2020-04-13 10:20:20 -0700153 std::vector<android::sp<AAudioServiceStreamBase>> disconnectRegisteredStreams();
Phil Burkc0c70e32017-02-09 13:18:38 -0800154
Phil Burk4501b352017-06-29 18:12:36 -0700155 mutable std::mutex mLockStreams;
Phil Burk0bd745e2020-10-17 18:20:01 +0000156 std::vector<android::sp<AAudioServiceStreamBase>> mRegisteredStreams
157 GUARDED_BY(mLockStreams);
Phil Burk87c9f642017-05-17 07:22:39 -0700158
Phil Burka53ffa62018-10-10 16:21:37 -0700159 SimpleDoubleBuffer<Timestamp> mAtomicEndpointTimestamp;
Phil Burk11e8d332017-05-24 09:59:02 -0700160
Phil Burk15f7cab2017-08-04 09:13:31 -0700161 android::AudioClient mMmapClient; // set in open, used in open and startStream
Phil Burk71f35bb2017-04-13 16:05:07 -0700162
Phil Burk15f7cab2017-08-04 09:13:31 -0700163 int32_t mFramesPerBurst = 0;
164 int32_t mOpenCount = 0;
Phil Burkec89b2e2017-06-20 15:05:06 -0700165 int32_t mRequestedDeviceId = 0;
Phil Burk15f7cab2017-08-04 09:13:31 -0700166
Phil Burk6e463ce2020-04-13 10:20:20 -0700167 // True if this will be shared by one or more other streams.
168 bool mForSharing = false;
169
Phil Burkbe0a5b62017-10-12 15:56:00 -0700170 std::atomic<bool> mConnected{true};
Phil Burk6e463ce2020-04-13 10:20:20 -0700171
Phil Burkc0c70e32017-02-09 13:18:38 -0800172};
173
174} /* namespace aaudio */
175
176
177#endif //AAUDIO_SERVICE_ENDPOINT_H