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