• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(
62             android::AAudioService &audioService,
63             const aaudio::AAudioStreamRequest &request)
64             EXCLUDES(mExclusiveLock, mSharedLock);
65 
66     void closeEndpoint(const android::sp<AAudioServiceEndpoint>& serviceEndpoint)
67             EXCLUDES(mExclusiveLock, mSharedLock);;
68 
69 private:
70     android::sp<AAudioServiceEndpoint> openExclusiveEndpoint(
71             android::AAudioService &aaudioService,
72             const aaudio::AAudioStreamRequest &request,
73             sp<AAudioServiceEndpoint> &endpointToSteal)
74             EXCLUDES(mExclusiveLock);
75 
76     android::sp<AAudioServiceEndpoint> openSharedEndpoint(
77             android::AAudioService &aaudioService,
78             const aaudio::AAudioStreamRequest &request)
79             EXCLUDES(mSharedLock);
80 
81     android::sp<AAudioServiceEndpoint> findExclusiveEndpoint_l(
82             const AAudioStreamConfiguration& configuration)
83             REQUIRES(mExclusiveLock);
84 
85     android::sp<AAudioServiceEndpointShared> findSharedEndpoint_l(
86             const AAudioStreamConfiguration& configuration)
87             REQUIRES(mSharedLock)
88             EXCLUDES(mExclusiveLock);
89 
90     void closeExclusiveEndpoint(const android::sp<AAudioServiceEndpoint>& serviceEndpoint);
91     void closeSharedEndpoint(const android::sp<AAudioServiceEndpoint>& serviceEndpoint);
92 
93     // Use separate locks because opening a Shared endpoint requires opening an Exclusive one.
94     // That could cause a recursive lock.
95     // Lock mSharedLock before mExclusiveLock.
96     // it is OK to only lock mExclusiveLock.
97     mutable std::mutex                                     mSharedLock;
98     std::vector<android::sp<AAudioServiceEndpointShared>>  mSharedStreams
99             GUARDED_BY(mSharedLock);
100 
101     mutable std::mutex                                     mExclusiveLock;
102     std::vector<android::sp<AAudioServiceEndpointMMAP>>    mExclusiveStreams
103             GUARDED_BY(mExclusiveLock);
104 
105     // Counts related to an exclusive endpoint.
106     int32_t mExclusiveSearchCount GUARDED_BY(mExclusiveLock) = 0; // # SEARCHED
107     int32_t mExclusiveFoundCount  GUARDED_BY(mExclusiveLock) = 0; // # FOUND
108     int32_t mExclusiveOpenCount   GUARDED_BY(mExclusiveLock) = 0; // # OPENED
109     int32_t mExclusiveCloseCount  GUARDED_BY(mExclusiveLock) = 0; // # CLOSED
110     int32_t mExclusiveStolenCount GUARDED_BY(mExclusiveLock) = 0; // # STOLEN
111 
112     // Same as above but for SHARED endpoints.
113     int32_t mSharedSearchCount    GUARDED_BY(mSharedLock) = 0;
114     int32_t mSharedFoundCount     GUARDED_BY(mSharedLock) = 0;
115     int32_t mSharedOpenCount      GUARDED_BY(mSharedLock) = 0;
116     int32_t mSharedCloseCount     GUARDED_BY(mSharedLock) = 0;
117 
118     // For easily disabling the stealing of exclusive streams.
119     static constexpr bool kStealingEnabled = true;
120 };
121 } /* namespace aaudio */
122 
123 #endif //AAUDIO_AAUDIO_ENDPOINT_MANAGER_H
124