Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 1 | /* |
| 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 Burk | 0bd745e | 2020-10-17 18:20:01 +0000 | [diff] [blame] | 25 | #include <android-base/thread_annotations.h> |
| 26 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 27 | #include "client/AudioStreamInternal.h" |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 28 | #include "client/AudioStreamInternalPlay.h" |
Phil Burk | 15f7cab | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 29 | #include "core/AAudioStreamParameters.h" |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 30 | #include "binding/AAudioServiceMessage.h" |
Phil Burk | 15f7cab | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 31 | #include "binding/AAudioStreamConfiguration.h" |
| 32 | |
| 33 | #include "AAudioServiceStreamBase.h" |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 34 | |
| 35 | namespace aaudio { |
| 36 | |
Phil Burk | 15f7cab | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 37 | /** |
| 38 | * AAudioServiceEndpoint is used by a subclass of AAudioServiceStreamBase |
| 39 | * to communicate with the underlying audio device or port. |
| 40 | */ |
| 41 | class AAudioServiceEndpoint |
| 42 | : public virtual android::RefBase |
| 43 | , public AAudioStreamParameters { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 44 | public: |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 45 | |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 46 | ~AAudioServiceEndpoint() override; |
Phil Burk | 4501b35 | 2017-06-29 18:12:36 -0700 | [diff] [blame] | 47 | |
Phil Burk | 15f7cab | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 48 | virtual std::string dump() const; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 49 | |
Phil Burk | 15f7cab | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 50 | virtual aaudio_result_t open(const aaudio::AAudioStreamRequest &request) = 0; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 51 | |
Phil Burk | 320910f | 2020-08-12 14:29:10 +0000 | [diff] [blame] | 52 | /* |
| 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 Burk | 15f7cab | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 57 | |
jiabin | e100132 | 2023-12-04 23:58:39 +0000 | [diff] [blame] | 58 | aaudio_result_t registerStream(const android::sp<AAudioServiceStreamBase>& stream) |
| 59 | EXCLUDES(mLockStreams); |
Phil Burk | 15f7cab | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 60 | |
jiabin | e100132 | 2023-12-04 23:58:39 +0000 | [diff] [blame] | 61 | aaudio_result_t unregisterStream(const android::sp<AAudioServiceStreamBase>& stream) |
| 62 | EXCLUDES(mLockStreams); |
Phil Burk | 15f7cab | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 63 | |
| 64 | virtual aaudio_result_t startStream(android::sp<AAudioServiceStreamBase> stream, |
| 65 | audio_port_handle_t *clientHandle) = 0; |
| 66 | |
| 67 | virtual aaudio_result_t stopStream(android::sp<AAudioServiceStreamBase> stream, |
| 68 | audio_port_handle_t clientHandle) = 0; |
| 69 | |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 70 | virtual aaudio_result_t startClient(const android::AudioClient& /*client*/, |
| 71 | const audio_attributes_t* /*attr*/, |
| 72 | audio_port_handle_t* /*clientHandle*/) { |
Phil Burk | 7ba4655 | 2019-04-15 08:58:08 -0700 | [diff] [blame] | 73 | ALOGD("AAudioServiceEndpoint::startClient(...) AAUDIO_ERROR_UNAVAILABLE"); |
Phil Burk | 15f7cab | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 74 | return AAUDIO_ERROR_UNAVAILABLE; |
| 75 | } |
| 76 | |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 77 | virtual aaudio_result_t stopClient(audio_port_handle_t /*clientHandle*/) { |
Phil Burk | 15f7cab | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 78 | ALOGD("AAudioServiceEndpoint::stopClient(...) AAUDIO_ERROR_UNAVAILABLE"); |
| 79 | return AAUDIO_ERROR_UNAVAILABLE; |
| 80 | } |
| 81 | |
jiabin | f7f0615 | 2021-11-22 18:10:14 +0000 | [diff] [blame] | 82 | virtual aaudio_result_t standby() { |
| 83 | ALOGD("AAudioServiceEndpoint::standby() AAUDIO_ERROR_UNAVAILABLE"); |
| 84 | return AAUDIO_ERROR_UNAVAILABLE; |
| 85 | } |
| 86 | |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 87 | virtual aaudio_result_t exitStandby(AudioEndpointParcelable* /*parcelable*/) { |
jiabin | f7f0615 | 2021-11-22 18:10:14 +0000 | [diff] [blame] | 88 | ALOGD("AAudioServiceEndpoint::exitStandby() AAUDIO_ERROR_UNAVAILABLE"); |
| 89 | return AAUDIO_ERROR_UNAVAILABLE; |
| 90 | } |
| 91 | |
Phil Burk | 15f7cab | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 92 | /** |
| 93 | * @param positionFrames |
| 94 | * @param timeNanos |
| 95 | * @return AAUDIO_OK or AAUDIO_ERROR_UNAVAILABLE or other negative error |
| 96 | */ |
| 97 | virtual aaudio_result_t getFreeRunningPosition(int64_t *positionFrames, int64_t *timeNanos) = 0; |
| 98 | |
Phil Burk | 56d34f2 | 2018-10-11 13:30:56 -0700 | [diff] [blame] | 99 | /** |
| 100 | * Set time that the associated frame was presented to the hardware. |
| 101 | * |
| 102 | * @param positionFrames receive position, input value is ignored |
| 103 | * @param timeNanos receive time, input value is ignored |
| 104 | * @return AAUDIO_OK or AAUDIO_ERROR_UNAVAILABLE or other negative error |
| 105 | */ |
Phil Burk | 15f7cab | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 106 | virtual aaudio_result_t getTimestamp(int64_t *positionFrames, int64_t *timeNanos) = 0; |
| 107 | |
| 108 | int32_t getFramesPerBurst() const { |
| 109 | return mFramesPerBurst; |
| 110 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 111 | |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 112 | int32_t getRequestedDeviceId() const { return mRequestedDeviceId; } |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 113 | |
Eric Laurent | a17ae74 | 2017-06-29 15:43:55 -0700 | [diff] [blame] | 114 | bool matches(const AAudioStreamConfiguration& configuration); |
| 115 | |
Phil Burk | 15f7cab | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 116 | // This should only be called from the AAudioEndpointManager under a mutex. |
| 117 | int32_t getOpenCount() const { |
| 118 | return mOpenCount; |
| 119 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 120 | |
Phil Burk | 15f7cab | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 121 | // This should only be called from the AAudioEndpointManager under a mutex. |
| 122 | void setOpenCount(int32_t count) { |
| 123 | mOpenCount = count; |
| 124 | } |
| 125 | |
Phil Burk | be0a5b6 | 2017-10-12 15:56:00 -0700 | [diff] [blame] | 126 | bool isConnected() const { |
| 127 | return mConnected; |
| 128 | } |
| 129 | |
Phil Burk | a987670 | 2020-04-20 18:16:15 -0700 | [diff] [blame] | 130 | static audio_attributes_t getAudioAttributesFrom(const AAudioStreamParameters *params); |
| 131 | |
Phil Burk | 6e463ce | 2020-04-13 10:20:20 -0700 | [diff] [blame] | 132 | // Stop, disconnect and release any streams registered on this endpoint. |
| 133 | void releaseRegisteredStreams(); |
| 134 | |
| 135 | bool isForSharing() const { |
| 136 | return mForSharing; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * |
| 141 | * @param flag true if this endpoint is to be shared between multiple streams |
| 142 | */ |
| 143 | void setForSharing(bool flag) { |
| 144 | mForSharing = flag; |
| 145 | } |
| 146 | |
Phil Burk | 15f7cab | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 147 | protected: |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 148 | |
| 149 | /** |
| 150 | * @param portHandle |
| 151 | * @return return true if a stream with the given portHandle is registered |
| 152 | */ |
jiabin | e100132 | 2023-12-04 23:58:39 +0000 | [diff] [blame] | 153 | bool isStreamRegistered(audio_port_handle_t portHandle) |
| 154 | EXCLUDES(mLockStreams); |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 155 | |
jiabin | e100132 | 2023-12-04 23:58:39 +0000 | [diff] [blame] | 156 | std::vector<android::sp<AAudioServiceStreamBase>> disconnectRegisteredStreams() |
| 157 | EXCLUDES(mLockStreams); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 158 | |
Phil Burk | 4501b35 | 2017-06-29 18:12:36 -0700 | [diff] [blame] | 159 | mutable std::mutex mLockStreams; |
Phil Burk | 0bd745e | 2020-10-17 18:20:01 +0000 | [diff] [blame] | 160 | std::vector<android::sp<AAudioServiceStreamBase>> mRegisteredStreams |
| 161 | GUARDED_BY(mLockStreams); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 162 | |
Phil Burk | a53ffa6 | 2018-10-10 16:21:37 -0700 | [diff] [blame] | 163 | SimpleDoubleBuffer<Timestamp> mAtomicEndpointTimestamp; |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 164 | |
Phil Burk | 15f7cab | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 165 | android::AudioClient mMmapClient; // set in open, used in open and startStream |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 166 | |
Phil Burk | 15f7cab | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 167 | int32_t mFramesPerBurst = 0; |
| 168 | int32_t mOpenCount = 0; |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 169 | int32_t mRequestedDeviceId = 0; |
Phil Burk | 15f7cab | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 170 | |
Phil Burk | 6e463ce | 2020-04-13 10:20:20 -0700 | [diff] [blame] | 171 | // True if this will be shared by one or more other streams. |
| 172 | bool mForSharing = false; |
| 173 | |
Phil Burk | be0a5b6 | 2017-10-12 15:56:00 -0700 | [diff] [blame] | 174 | std::atomic<bool> mConnected{true}; |
Phil Burk | 6e463ce | 2020-04-13 10:20:20 -0700 | [diff] [blame] | 175 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 176 | }; |
| 177 | |
| 178 | } /* namespace aaudio */ |
| 179 | |
| 180 | |
| 181 | #endif //AAUDIO_SERVICE_ENDPOINT_H |