blob: 00ff467751f576049b74680f9a4388f91dfe12b1 [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
24#include <utils/Singleton.h>
25
26#include <aaudio/AAudio.h>
27#include "binding/IAAudioClient.h"
28#include "AAudioService.h"
29
30namespace aaudio {
31
32class AAudioClientTracker : public android::Singleton<AAudioClientTracker>{
33public:
34 AAudioClientTracker();
35 ~AAudioClientTracker() = default;
36
Phil Burk4501b352017-06-29 18:12:36 -070037 /**
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
Phil Burk11e8d332017-05-24 09:59:02 -070049 aaudio_result_t registerClient(pid_t pid, const android::sp<android::IAAudioClient>& client);
50
51 void unregisterClient(pid_t pid);
52
Phil Burk91692942017-06-30 12:23:05 -070053 int32_t getStreamCount(pid_t pid);
54
Phil Burk11e8d332017-05-24 09:59:02 -070055 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
61 android::AAudioService *getAAudioService() const {
62 return mAAudioService;
63 }
64
65 void setAAudioService(android::AAudioService *aaudioService) {
66 mAAudioService = aaudioService;
67 }
68
69private:
Phil Burk91692942017-06-30 12:23:05 -070070
71 /**
72 * One per process.
73 */
Phil Burk11e8d332017-05-24 09:59:02 -070074 class NotificationClient : public IBinder::DeathRecipient {
75 public:
Steven Moreland37a61632019-12-04 14:06:50 -080076 NotificationClient(pid_t pid, const android::sp<IBinder>& binder);
Phil Burk11e8d332017-05-24 09:59:02 -070077 virtual ~NotificationClient();
78
Phil Burk91692942017-06-30 12:23:05 -070079 int32_t getStreamCount();
80
Phil Burk4501b352017-06-29 18:12:36 -070081 std::string dump() const;
82
Phil Burk11e8d332017-05-24 09:59:02 -070083 aaudio_result_t registerClientStream(android::sp<AAudioServiceStreamBase> serviceStream);
84
85 aaudio_result_t unregisterClientStream(android::sp<AAudioServiceStreamBase> serviceStream);
86
87 // IBinder::DeathRecipient
88 virtual void binderDied(const android::wp<IBinder>& who);
89
90 protected:
Phil Burk4501b352017-06-29 18:12:36 -070091 mutable std::mutex mLock;
Phil Burk11e8d332017-05-24 09:59:02 -070092 const pid_t mProcessId;
93 std::set<android::sp<AAudioServiceStreamBase>> mStreams;
Steven Moreland37a61632019-12-04 14:06:50 -080094 // hold onto binder to receive death notifications
95 android::sp<IBinder> mBinder;
Phil Burk11e8d332017-05-24 09:59:02 -070096 };
97
Phil Burk4501b352017-06-29 18:12:36 -070098 mutable std::mutex mLock;
Phil Burk11e8d332017-05-24 09:59:02 -070099 std::map<pid_t, android::sp<NotificationClient>> mNotificationClients;
100 android::AAudioService *mAAudioService = nullptr;
101};
102
103} /* namespace aaudio */
104
105#endif //ANDROID_AAUDIO_AAUDIO_CLIENT_TRACKER_H