• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 //#define LOG_NDEBUG 0
18 #define LOG_TAG "TranscodingResourcePolicy"
19 
20 #include <aidl/android/media/BnResourceObserver.h>
21 #include <aidl/android/media/IResourceObserverService.h>
22 #include <android/binder_manager.h>
23 #include <android/binder_process.h>
24 #include <media/TranscodingResourcePolicy.h>
25 #include <utils/Log.h>
26 
27 namespace android {
28 
29 using Status = ::ndk::ScopedAStatus;
30 using ::aidl::android::media::BnResourceObserver;
31 using ::aidl::android::media::IResourceObserverService;
32 using ::aidl::android::media::MediaObservableEvent;
33 using ::aidl::android::media::MediaObservableFilter;
34 using ::aidl::android::media::MediaObservableParcel;
35 using ::aidl::android::media::MediaObservableType;
36 
toString(const MediaObservableParcel & observable)37 static std::string toString(const MediaObservableParcel& observable) {
38     return "{" + ::aidl::android::media::toString(observable.type) + ", " +
39            std::to_string(observable.value) + "}";
40 }
41 
42 struct TranscodingResourcePolicy::ResourceObserver : public BnResourceObserver {
ResourceObserverandroid::TranscodingResourcePolicy::ResourceObserver43     explicit ResourceObserver(TranscodingResourcePolicy* owner) : mOwner(owner) {}
44 
45     // IResourceObserver
onStatusChangedandroid::TranscodingResourcePolicy::ResourceObserver46     ::ndk::ScopedAStatus onStatusChanged(
47             MediaObservableEvent event, int32_t uid, int32_t pid,
48             const std::vector<MediaObservableParcel>& observables) override {
49         ALOGD("%s: %s, uid %d, pid %d, %s", __FUNCTION__,
50               ::aidl::android::media::toString(event).c_str(), uid, pid,
51               toString(observables[0]).c_str());
52 
53         // Only report kIdle event.
54         if (((uint64_t)event & (uint64_t)MediaObservableEvent::kIdle) != 0) {
55             for (auto& observable : observables) {
56                 if (observable.type == MediaObservableType::kVideoSecureCodec ||
57                     observable.type == MediaObservableType::kVideoNonSecureCodec) {
58                     mOwner->onResourceAvailable(pid);
59                     break;
60                 }
61             }
62         }
63         return ::ndk::ScopedAStatus::ok();
64     }
65 
66     TranscodingResourcePolicy* mOwner;
67 };
68 
69 // static
BinderDiedCallback(void * cookie)70 void TranscodingResourcePolicy::BinderDiedCallback(void* cookie) {
71     TranscodingResourcePolicy* owner = reinterpret_cast<TranscodingResourcePolicy*>(cookie);
72     if (owner != nullptr) {
73         owner->unregisterSelf();
74     }
75     // TODO(chz): retry to connecting to IResourceObserverService after failure.
76     // Also need to have back-up logic if IResourceObserverService is offline for
77     // Prolonged period of time. A possible alternative could be, during period where
78     // IResourceObserverService is not available, trigger onResourceAvailable() everytime
79     // when top uid changes (in hope that'll free up some codec instances that we could
80     // reclaim).
81 }
82 
TranscodingResourcePolicy()83 TranscodingResourcePolicy::TranscodingResourcePolicy()
84       : mRegistered(false),
85         mResourceLostPid(-1),
86         mDeathRecipient(AIBinder_DeathRecipient_new(BinderDiedCallback)) {
87     registerSelf();
88 }
89 
~TranscodingResourcePolicy()90 TranscodingResourcePolicy::~TranscodingResourcePolicy() {
91     unregisterSelf();
92 }
93 
registerSelf()94 void TranscodingResourcePolicy::registerSelf() {
95     ALOGI("TranscodingResourcePolicy: registerSelf");
96 
97     ::ndk::SpAIBinder binder(AServiceManager_getService("media.resource_observer"));
98 
99     std::scoped_lock lock{mRegisteredLock};
100 
101     if (mRegistered) {
102         return;
103     }
104 
105     // TODO(chz): retry to connecting to IResourceObserverService after failure.
106     mService = IResourceObserverService::fromBinder(binder);
107     if (mService == nullptr) {
108         ALOGE("Failed to get IResourceObserverService");
109         return;
110     }
111 
112     // Only register filters for codec resource available.
113     mObserver = ::ndk::SharedRefBase::make<ResourceObserver>(this);
114     std::vector<MediaObservableFilter> filters = {
115             {MediaObservableType::kVideoSecureCodec, MediaObservableEvent::kIdle},
116             {MediaObservableType::kVideoNonSecureCodec, MediaObservableEvent::kIdle}};
117 
118     Status status = mService->registerObserver(mObserver, filters);
119     if (!status.isOk()) {
120         ALOGE("failed to register: error %d", status.getServiceSpecificError());
121         mService = nullptr;
122         mObserver = nullptr;
123         return;
124     }
125 
126     AIBinder_linkToDeath(binder.get(), mDeathRecipient.get(), reinterpret_cast<void*>(this));
127 
128     ALOGD("@@@ registered observer");
129     mRegistered = true;
130 }
131 
unregisterSelf()132 void TranscodingResourcePolicy::unregisterSelf() {
133     ALOGI("TranscodingResourcePolicy: unregisterSelf");
134 
135     std::scoped_lock lock{mRegisteredLock};
136 
137     if (!mRegistered) {
138         return;
139     }
140 
141     ::ndk::SpAIBinder binder = mService->asBinder();
142     if (binder.get() != nullptr) {
143         Status status = mService->unregisterObserver(mObserver);
144         AIBinder_unlinkToDeath(binder.get(), mDeathRecipient.get(), reinterpret_cast<void*>(this));
145     }
146 
147     mService = nullptr;
148     mObserver = nullptr;
149     mRegistered = false;
150 }
151 
setCallback(const std::shared_ptr<ResourcePolicyCallbackInterface> & cb)152 void TranscodingResourcePolicy::setCallback(
153         const std::shared_ptr<ResourcePolicyCallbackInterface>& cb) {
154     std::scoped_lock lock{mCallbackLock};
155     mResourcePolicyCallback = cb;
156 }
157 
setPidResourceLost(pid_t pid)158 void TranscodingResourcePolicy::setPidResourceLost(pid_t pid) {
159     std::scoped_lock lock{mCallbackLock};
160     mResourceLostPid = pid;
161 }
162 
onResourceAvailable(pid_t pid)163 void TranscodingResourcePolicy::onResourceAvailable(pid_t pid) {
164     std::shared_ptr<ResourcePolicyCallbackInterface> cb;
165     {
166         std::scoped_lock lock{mCallbackLock};
167         // Only callback if codec resource is released from other processes.
168         if (mResourceLostPid != -1 && mResourceLostPid != pid) {
169             cb = mResourcePolicyCallback.lock();
170             mResourceLostPid = -1;
171         }
172     }
173 
174     if (cb != nullptr) {
175         cb->onResourceAvailable();
176     }
177 }
178 }  // namespace android
179