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> 22 #include <sys/types.h> 23 24 #include <android-base/thread_annotations.h> 25 #include <utils/Singleton.h> 26 27 #include "binding/AAudioServiceMessage.h" 28 #include "AAudioServiceEndpoint.h" 29 #include "AAudioServiceEndpointCapture.h" 30 #include "AAudioServiceEndpointMMAP.h" 31 #include "AAudioServiceEndpointPlay.h" 32 33 namespace aaudio { 34 35 class AAudioEndpointManager : public android::Singleton<AAudioEndpointManager> { 36 public: 37 AAudioEndpointManager(); 38 ~AAudioEndpointManager() = default; 39 40 /** 41 * Returns information about the state of the this class. 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 * 48 * @return a string with useful information 49 */ 50 std::string dump() const; 51 52 /** 53 * Find a service endpoint for the given deviceId, sessionId and direction. 54 * If an endpoint does not already exist then try to create one. 55 * 56 * @param audioService 57 * @param request 58 * @param sharingMode 59 * @return endpoint or null 60 */ 61 android::sp<AAudioServiceEndpoint> openEndpoint(android::AAudioService &audioService, 62 const aaudio::AAudioStreamRequest &request); 63 64 void closeEndpoint(android::sp<AAudioServiceEndpoint> serviceEndpoint); 65 66 private: 67 android::sp<AAudioServiceEndpoint> openExclusiveEndpoint(android::AAudioService &aaudioService, 68 const aaudio::AAudioStreamRequest &request, 69 sp<AAudioServiceEndpoint> &endpointToSteal); 70 71 android::sp<AAudioServiceEndpoint> openSharedEndpoint(android::AAudioService &aaudioService, 72 const aaudio::AAudioStreamRequest &request); 73 74 android::sp<AAudioServiceEndpoint> findExclusiveEndpoint_l( 75 const AAudioStreamConfiguration& configuration) 76 REQUIRES(mExclusiveLock); 77 78 android::sp<AAudioServiceEndpointShared> findSharedEndpoint_l( 79 const AAudioStreamConfiguration& configuration) 80 REQUIRES(mSharedLock); 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; 90 std::vector<android::sp<AAudioServiceEndpointShared>> mSharedStreams 91 GUARDED_BY(mSharedLock); 92 93 mutable std::mutex mExclusiveLock; 94 std::vector<android::sp<AAudioServiceEndpointMMAP>> mExclusiveStreams 95 GUARDED_BY(mExclusiveLock); 96 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 103 104 // Same as above but for SHARED endpoints. 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; 109 110 // For easily disabling the stealing of exclusive streams. 111 static constexpr bool kStealingEnabled = true; 112 }; 113 } /* namespace aaudio */ 114 115 #endif //AAUDIO_AAUDIO_ENDPOINT_MANAGER_H 116