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_AAUDIO_ENDPOINT_MANAGER_H |
| 18 | #define AAUDIO_AAUDIO_ENDPOINT_MANAGER_H |
| 19 | |
| 20 | #include <map> |
| 21 | #include <mutex> |
Phil Burk | 6e463ce | 2020-04-13 10:20:20 -0700 | [diff] [blame] | 22 | #include <sys/types.h> |
Phil Burk | 0bd745e | 2020-10-17 18:20:01 +0000 | [diff] [blame] | 23 | |
| 24 | #include <android-base/thread_annotations.h> |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 25 | #include <utils/Singleton.h> |
| 26 | |
| 27 | #include "binding/AAudioServiceMessage.h" |
| 28 | #include "AAudioServiceEndpoint.h" |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 29 | #include "AAudioServiceEndpointCapture.h" |
Phil Burk | 15f7cab | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 30 | #include "AAudioServiceEndpointMMAP.h" |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 31 | #include "AAudioServiceEndpointPlay.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 | class AAudioEndpointManager : public android::Singleton<AAudioEndpointManager> { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 36 | public: |
| 37 | AAudioEndpointManager(); |
| 38 | ~AAudioEndpointManager() = default; |
| 39 | |
| 40 | /** |
Phil Burk | 4501b35 | 2017-06-29 18:12:36 -0700 | [diff] [blame] | 41 | * Returns information about the state of the this class. |
Andy Hung | 47c5e53 | 2017-06-26 18:28:00 -0700 | [diff] [blame] | 42 | * |
| 43 | * Will attempt to get the object lock, but will proceed |
| 44 | * even if it cannot. |
| 45 | * |
| 46 | * Each line of information ends with a newline. |
| 47 | * |
Phil Burk | 4501b35 | 2017-06-29 18:12:36 -0700 | [diff] [blame] | 48 | * @return a string with useful information |
Andy Hung | 47c5e53 | 2017-06-26 18:28:00 -0700 | [diff] [blame] | 49 | */ |
| 50 | std::string dump() const; |
| 51 | |
| 52 | /** |
Phil Burk | 4e1af9f | 2018-01-03 15:54:35 -0800 | [diff] [blame] | 53 | * Find a service endpoint for the given deviceId, sessionId and direction. |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 54 | * If an endpoint does not already exist then try to create one. |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 55 | * |
Phil Burk | 15f7cab | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 56 | * @param audioService |
| 57 | * @param request |
| 58 | * @param sharingMode |
| 59 | * @return endpoint or null |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 60 | */ |
Phil Burk | 15f7cab | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 61 | android::sp<AAudioServiceEndpoint> openEndpoint(android::AAudioService &audioService, |
Phil Burk | 15f97c9 | 2018-09-04 14:06:27 -0700 | [diff] [blame] | 62 | const aaudio::AAudioStreamRequest &request); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 63 | |
Phil Burk | 15f7cab | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 64 | void closeEndpoint(android::sp<AAudioServiceEndpoint> serviceEndpoint); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 65 | |
| 66 | private: |
Phil Burk | 15f7cab | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 67 | android::sp<AAudioServiceEndpoint> openExclusiveEndpoint(android::AAudioService &aaudioService, |
Phil Burk | 6e463ce | 2020-04-13 10:20:20 -0700 | [diff] [blame] | 68 | const aaudio::AAudioStreamRequest &request, |
| 69 | sp<AAudioServiceEndpoint> &endpointToSteal); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 70 | |
Phil Burk | 15f7cab | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 71 | android::sp<AAudioServiceEndpoint> openSharedEndpoint(android::AAudioService &aaudioService, |
| 72 | const aaudio::AAudioStreamRequest &request); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 73 | |
Phil Burk | 15f7cab | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 74 | android::sp<AAudioServiceEndpoint> findExclusiveEndpoint_l( |
Phil Burk | 0bd745e | 2020-10-17 18:20:01 +0000 | [diff] [blame] | 75 | const AAudioStreamConfiguration& configuration) |
| 76 | REQUIRES(mExclusiveLock); |
Phil Burk | 15f7cab | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 77 | |
| 78 | android::sp<AAudioServiceEndpointShared> findSharedEndpoint_l( |
Phil Burk | 0bd745e | 2020-10-17 18:20:01 +0000 | [diff] [blame] | 79 | const AAudioStreamConfiguration& configuration) |
| 80 | REQUIRES(mSharedLock); |
Phil Burk | 15f7cab | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 81 | |
| 82 | void closeExclusiveEndpoint(android::sp<AAudioServiceEndpoint> serviceEndpoint); |
| 83 | void closeSharedEndpoint(android::sp<AAudioServiceEndpoint> serviceEndpoint); |
| 84 | |
| 85 | // Use separate locks because opening a Shared endpoint requires opening an Exclusive one. |
| 86 | // That could cause a recursive lock. |
| 87 | // Lock mSharedLock before mExclusiveLock. |
| 88 | // it is OK to only lock mExclusiveLock. |
| 89 | mutable std::mutex mSharedLock; |
Phil Burk | 0bd745e | 2020-10-17 18:20:01 +0000 | [diff] [blame] | 90 | std::vector<android::sp<AAudioServiceEndpointShared>> mSharedStreams |
| 91 | GUARDED_BY(mSharedLock); |
Phil Burk | 15f7cab | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 92 | |
| 93 | mutable std::mutex mExclusiveLock; |
Phil Burk | 0bd745e | 2020-10-17 18:20:01 +0000 | [diff] [blame] | 94 | std::vector<android::sp<AAudioServiceEndpointMMAP>> mExclusiveStreams |
| 95 | GUARDED_BY(mExclusiveLock); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 96 | |
Phil Burk | 0bd745e | 2020-10-17 18:20:01 +0000 | [diff] [blame] | 97 | // Counts related to an exclusive endpoint. |
| 98 | int32_t mExclusiveSearchCount GUARDED_BY(mExclusiveLock) = 0; // # SEARCHED |
| 99 | int32_t mExclusiveFoundCount GUARDED_BY(mExclusiveLock) = 0; // # FOUND |
| 100 | int32_t mExclusiveOpenCount GUARDED_BY(mExclusiveLock) = 0; // # OPENED |
| 101 | int32_t mExclusiveCloseCount GUARDED_BY(mExclusiveLock) = 0; // # CLOSED |
| 102 | int32_t mExclusiveStolenCount GUARDED_BY(mExclusiveLock) = 0; // # STOLEN |
Phil Burk | 6e463ce | 2020-04-13 10:20:20 -0700 | [diff] [blame] | 103 | |
Phil Burk | 55e5eab | 2018-04-10 15:16:38 -0700 | [diff] [blame] | 104 | // Same as above but for SHARED endpoints. |
Phil Burk | 0bd745e | 2020-10-17 18:20:01 +0000 | [diff] [blame] | 105 | int32_t mSharedSearchCount GUARDED_BY(mSharedLock) = 0; |
| 106 | int32_t mSharedFoundCount GUARDED_BY(mSharedLock) = 0; |
| 107 | int32_t mSharedOpenCount GUARDED_BY(mSharedLock) = 0; |
| 108 | int32_t mSharedCloseCount GUARDED_BY(mSharedLock) = 0; |
Phil Burk | 6e463ce | 2020-04-13 10:20:20 -0700 | [diff] [blame] | 109 | |
| 110 | // For easily disabling the stealing of exclusive streams. |
| 111 | static constexpr bool kStealingEnabled = true; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 112 | }; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 113 | } /* namespace aaudio */ |
| 114 | |
| 115 | #endif //AAUDIO_AAUDIO_ENDPOINT_MANAGER_H |