Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [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 ANDROID_AAUDIO_AAUDIO_CLIENT_TRACKER_H |
| 18 | #define ANDROID_AAUDIO_AAUDIO_CLIENT_TRACKER_H |
| 19 | |
| 20 | #include <map> |
| 21 | #include <mutex> |
| 22 | #include <set> |
| 23 | |
Phil Burk | 0bd745e | 2020-10-17 18:20:01 +0000 | [diff] [blame] | 24 | #include <android-base/thread_annotations.h> |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 25 | #include <utils/Singleton.h> |
| 26 | |
| 27 | #include <aaudio/AAudio.h> |
Ytai Ben-Tsvi | 0412f73 | 2020-08-17 14:43:36 -0700 | [diff] [blame] | 28 | #include <aaudio/IAAudioClient.h> |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 29 | #include "AAudioService.h" |
| 30 | |
| 31 | namespace aaudio { |
| 32 | |
| 33 | class AAudioClientTracker : public android::Singleton<AAudioClientTracker>{ |
| 34 | public: |
| 35 | AAudioClientTracker(); |
| 36 | ~AAudioClientTracker() = default; |
| 37 | |
Phil Burk | 4501b35 | 2017-06-29 18:12:36 -0700 | [diff] [blame] | 38 | /** |
| 39 | * Returns information about the state of the this class. |
| 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 | * |
| 46 | * @return a string with useful information |
| 47 | */ |
| 48 | std::string dump() const; |
| 49 | |
Ytai Ben-Tsvi | 0412f73 | 2020-08-17 14:43:36 -0700 | [diff] [blame] | 50 | aaudio_result_t registerClient(pid_t pid, const android::sp<IAAudioClient>& client); |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 51 | |
| 52 | void unregisterClient(pid_t pid); |
| 53 | |
Phil Burk | 9169294 | 2017-06-30 12:23:05 -0700 | [diff] [blame] | 54 | int32_t getStreamCount(pid_t pid); |
| 55 | |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 56 | aaudio_result_t registerClientStream(pid_t pid, |
| 57 | android::sp<AAudioServiceStreamBase> serviceStream); |
| 58 | |
| 59 | aaudio_result_t unregisterClientStream(pid_t pid, |
| 60 | android::sp<AAudioServiceStreamBase> serviceStream); |
| 61 | |
Phil Burk | 836f9df | 2020-05-29 13:20:28 -0700 | [diff] [blame] | 62 | /** |
| 63 | * Specify whether a process is allowed to create an EXCLUSIVE MMAP stream. |
| 64 | * @param pid |
| 65 | * @param enabled |
| 66 | */ |
| 67 | void setExclusiveEnabled(pid_t pid, bool enabled); |
| 68 | |
| 69 | bool isExclusiveEnabled(pid_t pid); |
| 70 | |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 71 | android::AAudioService *getAAudioService() const { |
| 72 | return mAAudioService; |
| 73 | } |
| 74 | |
| 75 | void setAAudioService(android::AAudioService *aaudioService) { |
| 76 | mAAudioService = aaudioService; |
| 77 | } |
| 78 | |
| 79 | private: |
Phil Burk | 9169294 | 2017-06-30 12:23:05 -0700 | [diff] [blame] | 80 | |
| 81 | /** |
| 82 | * One per process. |
| 83 | */ |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 84 | class NotificationClient : public IBinder::DeathRecipient { |
| 85 | public: |
Steven Moreland | 37a6163 | 2019-12-04 14:06:50 -0800 | [diff] [blame] | 86 | NotificationClient(pid_t pid, const android::sp<IBinder>& binder); |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 87 | virtual ~NotificationClient(); |
| 88 | |
Phil Burk | 9169294 | 2017-06-30 12:23:05 -0700 | [diff] [blame] | 89 | int32_t getStreamCount(); |
| 90 | |
Phil Burk | 4501b35 | 2017-06-29 18:12:36 -0700 | [diff] [blame] | 91 | std::string dump() const; |
| 92 | |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 93 | aaudio_result_t registerClientStream(android::sp<AAudioServiceStreamBase> serviceStream); |
| 94 | |
| 95 | aaudio_result_t unregisterClientStream(android::sp<AAudioServiceStreamBase> serviceStream); |
| 96 | |
Phil Burk | 836f9df | 2020-05-29 13:20:28 -0700 | [diff] [blame] | 97 | void setExclusiveEnabled(bool enabled) { |
| 98 | mExclusiveEnabled = enabled; |
| 99 | } |
| 100 | |
| 101 | bool isExclusiveEnabled() { |
| 102 | return mExclusiveEnabled; |
| 103 | } |
| 104 | |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 105 | // IBinder::DeathRecipient |
| 106 | virtual void binderDied(const android::wp<IBinder>& who); |
| 107 | |
Phil Burk | 836f9df | 2020-05-29 13:20:28 -0700 | [diff] [blame] | 108 | private: |
Phil Burk | 4501b35 | 2017-06-29 18:12:36 -0700 | [diff] [blame] | 109 | mutable std::mutex mLock; |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 110 | const pid_t mProcessId; |
| 111 | std::set<android::sp<AAudioServiceStreamBase>> mStreams; |
Steven Moreland | 37a6163 | 2019-12-04 14:06:50 -0800 | [diff] [blame] | 112 | // hold onto binder to receive death notifications |
| 113 | android::sp<IBinder> mBinder; |
Phil Burk | 836f9df | 2020-05-29 13:20:28 -0700 | [diff] [blame] | 114 | bool mExclusiveEnabled = true; |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 115 | }; |
| 116 | |
Phil Burk | 836f9df | 2020-05-29 13:20:28 -0700 | [diff] [blame] | 117 | // This must be called under mLock |
Phil Burk | 0bd745e | 2020-10-17 18:20:01 +0000 | [diff] [blame] | 118 | android::sp<NotificationClient> getNotificationClient_l(pid_t pid) |
| 119 | REQUIRES(mLock); |
Phil Burk | 836f9df | 2020-05-29 13:20:28 -0700 | [diff] [blame] | 120 | |
Phil Burk | 4501b35 | 2017-06-29 18:12:36 -0700 | [diff] [blame] | 121 | mutable std::mutex mLock; |
Phil Burk | 0bd745e | 2020-10-17 18:20:01 +0000 | [diff] [blame] | 122 | std::map<pid_t, android::sp<NotificationClient>> mNotificationClients |
| 123 | GUARDED_BY(mLock); |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 124 | android::AAudioService *mAAudioService = nullptr; |
| 125 | }; |
| 126 | |
| 127 | } /* namespace aaudio */ |
| 128 | |
| 129 | #endif //ANDROID_AAUDIO_AAUDIO_CLIENT_TRACKER_H |