blob: ba178534913f161f6a90df3e3ceff89c2e0eda2f [file] [log] [blame]
Phil Burkc0c70e32017-02-09 13:18:38 -08001/*
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 <utils/Singleton.h>
23
24#include "binding/AAudioServiceMessage.h"
25#include "AAudioServiceEndpoint.h"
Phil Burk87c9f642017-05-17 07:22:39 -070026#include "AAudioServiceEndpointCapture.h"
Phil Burk15f7cab2017-08-04 09:13:31 -070027#include "AAudioServiceEndpointMMAP.h"
Phil Burk87c9f642017-05-17 07:22:39 -070028#include "AAudioServiceEndpointPlay.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080029
30namespace aaudio {
31
Phil Burk15f7cab2017-08-04 09:13:31 -070032class AAudioEndpointManager : public android::Singleton<AAudioEndpointManager> {
Phil Burkc0c70e32017-02-09 13:18:38 -080033public:
34 AAudioEndpointManager();
35 ~AAudioEndpointManager() = default;
36
37 /**
Phil Burk4501b352017-06-29 18:12:36 -070038 * Returns information about the state of the this class.
Andy Hung47c5e532017-06-26 18:28:00 -070039 *
40 * Will attempt to get the object lock, but will proceed
41 * even if it cannot.
42 *
43 * Each line of information ends with a newline.
44 *
Phil Burk4501b352017-06-29 18:12:36 -070045 * @return a string with useful information
Andy Hung47c5e532017-06-26 18:28:00 -070046 */
47 std::string dump() const;
48
49 /**
Phil Burk4e1af9f2018-01-03 15:54:35 -080050 * Find a service endpoint for the given deviceId, sessionId and direction.
Phil Burkec89b2e2017-06-20 15:05:06 -070051 * If an endpoint does not already exist then try to create one.
Phil Burkc0c70e32017-02-09 13:18:38 -080052 *
Phil Burk15f7cab2017-08-04 09:13:31 -070053 * @param audioService
54 * @param request
55 * @param sharingMode
56 * @return endpoint or null
Phil Burkc0c70e32017-02-09 13:18:38 -080057 */
Phil Burk15f7cab2017-08-04 09:13:31 -070058 android::sp<AAudioServiceEndpoint> openEndpoint(android::AAudioService &audioService,
Phil Burk15f97c92018-09-04 14:06:27 -070059 const aaudio::AAudioStreamRequest &request);
Phil Burkc0c70e32017-02-09 13:18:38 -080060
Phil Burk15f7cab2017-08-04 09:13:31 -070061 void closeEndpoint(android::sp<AAudioServiceEndpoint> serviceEndpoint);
Phil Burkc0c70e32017-02-09 13:18:38 -080062
63private:
Phil Burk15f7cab2017-08-04 09:13:31 -070064 android::sp<AAudioServiceEndpoint> openExclusiveEndpoint(android::AAudioService &aaudioService,
65 const aaudio::AAudioStreamRequest &request);
Phil Burkc0c70e32017-02-09 13:18:38 -080066
Phil Burk15f7cab2017-08-04 09:13:31 -070067 android::sp<AAudioServiceEndpoint> openSharedEndpoint(android::AAudioService &aaudioService,
68 const aaudio::AAudioStreamRequest &request);
Phil Burkc0c70e32017-02-09 13:18:38 -080069
Phil Burk15f7cab2017-08-04 09:13:31 -070070 android::sp<AAudioServiceEndpoint> findExclusiveEndpoint_l(
71 const AAudioStreamConfiguration& configuration);
72
73 android::sp<AAudioServiceEndpointShared> findSharedEndpoint_l(
74 const AAudioStreamConfiguration& configuration);
75
76 void closeExclusiveEndpoint(android::sp<AAudioServiceEndpoint> serviceEndpoint);
77 void closeSharedEndpoint(android::sp<AAudioServiceEndpoint> serviceEndpoint);
78
79 // Use separate locks because opening a Shared endpoint requires opening an Exclusive one.
80 // That could cause a recursive lock.
81 // Lock mSharedLock before mExclusiveLock.
82 // it is OK to only lock mExclusiveLock.
83 mutable std::mutex mSharedLock;
84 std::vector<android::sp<AAudioServiceEndpointShared>> mSharedStreams;
85
86 mutable std::mutex mExclusiveLock;
87 std::vector<android::sp<AAudioServiceEndpointMMAP>> mExclusiveStreams;
Phil Burkc0c70e32017-02-09 13:18:38 -080088
Phil Burk55e5eab2018-04-10 15:16:38 -070089 // Modified under a lock.
90 int32_t mExclusiveSearchCount = 0; // number of times we SEARCHED for an exclusive endpoint
91 int32_t mExclusiveFoundCount = 0; // number of times we FOUND an exclusive endpoint
92 int32_t mExclusiveOpenCount = 0; // number of times we OPENED an exclusive endpoint
93 int32_t mExclusiveCloseCount = 0; // number of times we CLOSED an exclusive endpoint
94 // Same as above but for SHARED endpoints.
95 int32_t mSharedSearchCount = 0;
96 int32_t mSharedFoundCount = 0;
97 int32_t mSharedOpenCount = 0;
98 int32_t mSharedCloseCount = 0;
Phil Burkc0c70e32017-02-09 13:18:38 -080099};
Phil Burkc0c70e32017-02-09 13:18:38 -0800100} /* namespace aaudio */
101
102#endif //AAUDIO_AAUDIO_ENDPOINT_MANAGER_H