blob: b07bcefbe3ba6f7b0d8b3d3ac02b560323419f8d [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>
Phil Burk6e463ce2020-04-13 10:20:20 -070022#include <sys/types.h>
Phil Burk0bd745e2020-10-17 18:20:01 +000023
24#include <android-base/thread_annotations.h>
Phil Burkc0c70e32017-02-09 13:18:38 -080025#include <utils/Singleton.h>
26
27#include "binding/AAudioServiceMessage.h"
28#include "AAudioServiceEndpoint.h"
Phil Burk87c9f642017-05-17 07:22:39 -070029#include "AAudioServiceEndpointCapture.h"
Phil Burk15f7cab2017-08-04 09:13:31 -070030#include "AAudioServiceEndpointMMAP.h"
Phil Burk87c9f642017-05-17 07:22:39 -070031#include "AAudioServiceEndpointPlay.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080032
33namespace aaudio {
34
Phil Burk15f7cab2017-08-04 09:13:31 -070035class AAudioEndpointManager : public android::Singleton<AAudioEndpointManager> {
Phil Burkc0c70e32017-02-09 13:18:38 -080036public:
37 AAudioEndpointManager();
38 ~AAudioEndpointManager() = default;
39
40 /**
Phil Burk4501b352017-06-29 18:12:36 -070041 * Returns information about the state of the this class.
Andy Hung47c5e532017-06-26 18:28:00 -070042 *
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 Burk4501b352017-06-29 18:12:36 -070048 * @return a string with useful information
Andy Hung47c5e532017-06-26 18:28:00 -070049 */
50 std::string dump() const;
51
52 /**
Phil Burk4e1af9f2018-01-03 15:54:35 -080053 * Find a service endpoint for the given deviceId, sessionId and direction.
Phil Burkec89b2e2017-06-20 15:05:06 -070054 * If an endpoint does not already exist then try to create one.
Phil Burkc0c70e32017-02-09 13:18:38 -080055 *
Phil Burk15f7cab2017-08-04 09:13:31 -070056 * @param audioService
57 * @param request
58 * @param sharingMode
59 * @return endpoint or null
Phil Burkc0c70e32017-02-09 13:18:38 -080060 */
Phil Burk15f7cab2017-08-04 09:13:31 -070061 android::sp<AAudioServiceEndpoint> openEndpoint(android::AAudioService &audioService,
Phil Burk15f97c92018-09-04 14:06:27 -070062 const aaudio::AAudioStreamRequest &request);
Phil Burkc0c70e32017-02-09 13:18:38 -080063
Phil Burk15f7cab2017-08-04 09:13:31 -070064 void closeEndpoint(android::sp<AAudioServiceEndpoint> serviceEndpoint);
Phil Burkc0c70e32017-02-09 13:18:38 -080065
66private:
Phil Burk15f7cab2017-08-04 09:13:31 -070067 android::sp<AAudioServiceEndpoint> openExclusiveEndpoint(android::AAudioService &aaudioService,
Phil Burk6e463ce2020-04-13 10:20:20 -070068 const aaudio::AAudioStreamRequest &request,
69 sp<AAudioServiceEndpoint> &endpointToSteal);
Phil Burkc0c70e32017-02-09 13:18:38 -080070
Phil Burk15f7cab2017-08-04 09:13:31 -070071 android::sp<AAudioServiceEndpoint> openSharedEndpoint(android::AAudioService &aaudioService,
72 const aaudio::AAudioStreamRequest &request);
Phil Burkc0c70e32017-02-09 13:18:38 -080073
Phil Burk15f7cab2017-08-04 09:13:31 -070074 android::sp<AAudioServiceEndpoint> findExclusiveEndpoint_l(
Phil Burk0bd745e2020-10-17 18:20:01 +000075 const AAudioStreamConfiguration& configuration)
76 REQUIRES(mExclusiveLock);
Phil Burk15f7cab2017-08-04 09:13:31 -070077
78 android::sp<AAudioServiceEndpointShared> findSharedEndpoint_l(
Phil Burk0bd745e2020-10-17 18:20:01 +000079 const AAudioStreamConfiguration& configuration)
80 REQUIRES(mSharedLock);
Phil Burk15f7cab2017-08-04 09:13:31 -070081
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 Burk0bd745e2020-10-17 18:20:01 +000090 std::vector<android::sp<AAudioServiceEndpointShared>> mSharedStreams
91 GUARDED_BY(mSharedLock);
Phil Burk15f7cab2017-08-04 09:13:31 -070092
93 mutable std::mutex mExclusiveLock;
Phil Burk0bd745e2020-10-17 18:20:01 +000094 std::vector<android::sp<AAudioServiceEndpointMMAP>> mExclusiveStreams
95 GUARDED_BY(mExclusiveLock);
Phil Burkc0c70e32017-02-09 13:18:38 -080096
Phil Burk0bd745e2020-10-17 18:20:01 +000097 // 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 Burk6e463ce2020-04-13 10:20:20 -0700103
Phil Burk55e5eab2018-04-10 15:16:38 -0700104 // Same as above but for SHARED endpoints.
Phil Burk0bd745e2020-10-17 18:20:01 +0000105 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 Burk6e463ce2020-04-13 10:20:20 -0700109
110 // For easily disabling the stealing of exclusive streams.
111 static constexpr bool kStealingEnabled = true;
Phil Burkc0c70e32017-02-09 13:18:38 -0800112};
Phil Burkc0c70e32017-02-09 13:18:38 -0800113} /* namespace aaudio */
114
115#endif //AAUDIO_AAUDIO_ENDPOINT_MANAGER_H