1 /*
2  * Copyright (C) 2020 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_MEDIA_TRANSCODING_RESOURCE_POLICY_H
18 #define ANDROID_MEDIA_TRANSCODING_RESOURCE_POLICY_H
19 
20 #include <android/binder_auto_utils.h>
21 #include <media/ResourcePolicyInterface.h>
22 #include <utils/Condition.h>
23 
24 #include <mutex>
25 #include <set>
26 namespace aidl {
27 namespace android {
28 namespace media {
29 class IResourceObserverService;
30 }
31 }  // namespace android
32 }  // namespace aidl
33 
34 namespace android {
35 
36 using ::aidl::android::media::IResourceObserverService;
37 
38 class TranscodingResourcePolicy : public ResourcePolicyInterface {
39 public:
40     explicit TranscodingResourcePolicy();
41     ~TranscodingResourcePolicy();
42 
43     void setCallback(const std::shared_ptr<ResourcePolicyCallbackInterface>& cb) override;
44     void setPidResourceLost(pid_t pid) override;
45 
46 private:
47     struct ResourceObserver;
48     mutable std::mutex mRegisteredLock;
49     bool mRegistered GUARDED_BY(mRegisteredLock);
50     std::shared_ptr<IResourceObserverService> mService GUARDED_BY(mRegisteredLock);
51     std::shared_ptr<ResourceObserver> mObserver;
52     mutable std::mutex mCookieKeysLock;
53     std::set<uintptr_t> mCookieKeys;
54 
55     mutable std::mutex mCallbackLock;
56     std::weak_ptr<ResourcePolicyCallbackInterface> mResourcePolicyCallback
57             GUARDED_BY(mCallbackLock);
58     pid_t mResourceLostPid GUARDED_BY(mCallbackLock);
59 
60     ::ndk::ScopedAIBinder_DeathRecipient mDeathRecipient;
61 
62     static void BinderDiedCallback(void* cookie);
63 
64     void registerSelf();
65     // must delete the associated TranscodingResourcePolicyCookie any time this is called
66     void unregisterSelf();
67     void onResourceAvailable(pid_t pid);
68 };  // class TranscodingUidPolicy
69 
70 }  // namespace android
71 #endif  // ANDROID_MEDIA_TRANSCODING_RESOURCE_POLICY_H
72