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