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