blob: 3d4ab3402d6b81adc00f63c7770d66215413b58d [file] [log] [blame]
Phil Burk11e8d332017-05-24 09:59:02 -07001/*
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 Burk0bd745e2020-10-17 18:20:01 +000024#include <android-base/thread_annotations.h>
Phil Burk11e8d332017-05-24 09:59:02 -070025#include <utils/Singleton.h>
26
27#include <aaudio/AAudio.h>
Ytai Ben-Tsvi0412f732020-08-17 14:43:36 -070028#include <aaudio/IAAudioClient.h>
Phil Burk11e8d332017-05-24 09:59:02 -070029#include "AAudioService.h"
30
31namespace aaudio {
32
33class AAudioClientTracker : public android::Singleton<AAudioClientTracker>{
34public:
35 AAudioClientTracker();
36 ~AAudioClientTracker() = default;
37
Phil Burk4501b352017-06-29 18:12:36 -070038 /**
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-Tsvi0412f732020-08-17 14:43:36 -070050 aaudio_result_t registerClient(pid_t pid, const android::sp<IAAudioClient>& client);
Phil Burk11e8d332017-05-24 09:59:02 -070051
52 void unregisterClient(pid_t pid);
53
Phil Burk91692942017-06-30 12:23:05 -070054 int32_t getStreamCount(pid_t pid);
55
Phil Burk11e8d332017-05-24 09:59:02 -070056 aaudio_result_t registerClientStream(pid_t pid,
jiabin613e6ae2022-12-21 20:20:11 +000057 const android::sp<AAudioServiceStreamBase>& serviceStream);
Phil Burk11e8d332017-05-24 09:59:02 -070058
jiabin613e6ae2022-12-21 20:20:11 +000059 aaudio_result_t unregisterClientStream(
60 pid_t pid, const android::sp<AAudioServiceStreamBase>& serviceStream);
Phil Burk11e8d332017-05-24 09:59:02 -070061
Phil Burk836f9df2020-05-29 13:20:28 -070062 /**
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 Burk11e8d332017-05-24 09:59:02 -070071 android::AAudioService *getAAudioService() const {
72 return mAAudioService;
73 }
74
75 void setAAudioService(android::AAudioService *aaudioService) {
76 mAAudioService = aaudioService;
77 }
78
79private:
Phil Burk91692942017-06-30 12:23:05 -070080
81 /**
82 * One per process.
83 */
Phil Burk11e8d332017-05-24 09:59:02 -070084 class NotificationClient : public IBinder::DeathRecipient {
85 public:
Steven Moreland37a61632019-12-04 14:06:50 -080086 NotificationClient(pid_t pid, const android::sp<IBinder>& binder);
jiabin613e6ae2022-12-21 20:20:11 +000087 ~NotificationClient() override = default;
Phil Burk11e8d332017-05-24 09:59:02 -070088
Phil Burk91692942017-06-30 12:23:05 -070089 int32_t getStreamCount();
90
Phil Burk4501b352017-06-29 18:12:36 -070091 std::string dump() const;
92
jiabin613e6ae2022-12-21 20:20:11 +000093 aaudio_result_t registerClientStream(
94 const android::sp<AAudioServiceStreamBase>& serviceStream);
Phil Burk11e8d332017-05-24 09:59:02 -070095
jiabin613e6ae2022-12-21 20:20:11 +000096 aaudio_result_t unregisterClientStream(
97 const android::sp<AAudioServiceStreamBase>& serviceStream);
Phil Burk11e8d332017-05-24 09:59:02 -070098
Phil Burk836f9df2020-05-29 13:20:28 -070099 void setExclusiveEnabled(bool enabled) {
100 mExclusiveEnabled = enabled;
101 }
102
103 bool isExclusiveEnabled() {
104 return mExclusiveEnabled;
105 }
106
Phil Burk11e8d332017-05-24 09:59:02 -0700107 // IBinder::DeathRecipient
jiabin613e6ae2022-12-21 20:20:11 +0000108 void binderDied(const android::wp<IBinder>& who) override;
Phil Burk11e8d332017-05-24 09:59:02 -0700109
Phil Burk836f9df2020-05-29 13:20:28 -0700110 private:
Phil Burk4501b352017-06-29 18:12:36 -0700111 mutable std::mutex mLock;
Phil Burk11e8d332017-05-24 09:59:02 -0700112 const pid_t mProcessId;
jiabine1001322023-12-04 23:58:39 +0000113 std::set<android::sp<AAudioServiceStreamBase>> mStreams GUARDED_BY(mLock);
Steven Moreland37a61632019-12-04 14:06:50 -0800114 // hold onto binder to receive death notifications
115 android::sp<IBinder> mBinder;
Phil Burk836f9df2020-05-29 13:20:28 -0700116 bool mExclusiveEnabled = true;
Phil Burk11e8d332017-05-24 09:59:02 -0700117 };
118
Phil Burk836f9df2020-05-29 13:20:28 -0700119 // This must be called under mLock
Phil Burk0bd745e2020-10-17 18:20:01 +0000120 android::sp<NotificationClient> getNotificationClient_l(pid_t pid)
121 REQUIRES(mLock);
Phil Burk836f9df2020-05-29 13:20:28 -0700122
Phil Burk4501b352017-06-29 18:12:36 -0700123 mutable std::mutex mLock;
Phil Burk0bd745e2020-10-17 18:20:01 +0000124 std::map<pid_t, android::sp<NotificationClient>> mNotificationClients
125 GUARDED_BY(mLock);
Phil Burk11e8d332017-05-24 09:59:02 -0700126 android::AAudioService *mAAudioService = nullptr;
127};
128
129} /* namespace aaudio */
130
131#endif //ANDROID_AAUDIO_AAUDIO_CLIENT_TRACKER_H