Chong Zhang | a9d45c7 | 2020-09-09 12:41:17 -0700 | [diff] [blame] | 1 | /** |
| 2 | * |
| 3 | * Copyright 2020, The Android Open Source Project |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | #define LOG_TAG "ResourceObserverService" |
| 20 | #include <utils/Log.h> |
| 21 | |
| 22 | #include <android/binder_manager.h> |
| 23 | #include <android/binder_process.h> |
| 24 | #include <binder/IServiceManager.h> |
| 25 | #include <utils/String16.h> |
| 26 | #include <aidl/android/media/MediaResourceParcel.h> |
| 27 | |
| 28 | #include "ResourceObserverService.h" |
| 29 | |
Chong Zhang | a9d45c7 | 2020-09-09 12:41:17 -0700 | [diff] [blame] | 30 | namespace android { |
| 31 | |
| 32 | using ::aidl::android::media::MediaResourceParcel; |
| 33 | using ::aidl::android::media::MediaObservableEvent; |
| 34 | |
| 35 | // MediaObservableEvent will be used as uint64_t flags. |
| 36 | static_assert(sizeof(MediaObservableEvent) == sizeof(uint64_t)); |
| 37 | |
| 38 | static std::vector<MediaObservableEvent> sEvents = { |
| 39 | MediaObservableEvent::kBusy, |
| 40 | MediaObservableEvent::kIdle, |
| 41 | }; |
| 42 | |
| 43 | static MediaObservableType getObservableType(const MediaResourceParcel& res) { |
| 44 | if (res.subType == MediaResourceSubType::kVideoCodec) { |
| 45 | if (res.type == MediaResourceType::kNonSecureCodec) { |
| 46 | return MediaObservableType::kVideoNonSecureCodec; |
| 47 | } |
| 48 | if (res.type == MediaResourceType::kSecureCodec) { |
| 49 | return MediaObservableType::kVideoSecureCodec; |
| 50 | } |
| 51 | } |
| 52 | return MediaObservableType::kInvalid; |
| 53 | } |
| 54 | |
| 55 | //static |
| 56 | std::mutex ResourceObserverService::sDeathRecipientLock; |
| 57 | //static |
| 58 | std::map<uintptr_t, std::shared_ptr<ResourceObserverService::DeathRecipient> > |
| 59 | ResourceObserverService::sDeathRecipientMap; |
| 60 | |
| 61 | struct ResourceObserverService::DeathRecipient { |
| 62 | DeathRecipient(ResourceObserverService* _service, |
| 63 | const std::shared_ptr<IResourceObserver>& _observer) |
| 64 | : service(_service), observer(_observer) {} |
| 65 | ~DeathRecipient() {} |
| 66 | |
| 67 | void binderDied() { |
| 68 | if (service != nullptr) { |
| 69 | service->unregisterObserver(observer); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | ResourceObserverService* service; |
| 74 | std::shared_ptr<IResourceObserver> observer; |
| 75 | }; |
| 76 | |
| 77 | // static |
| 78 | void ResourceObserverService::BinderDiedCallback(void* cookie) { |
| 79 | uintptr_t id = reinterpret_cast<uintptr_t>(cookie); |
| 80 | |
| 81 | ALOGW("Observer %lld is dead", (long long)id); |
| 82 | |
| 83 | std::shared_ptr<DeathRecipient> recipient; |
| 84 | |
| 85 | { |
| 86 | std::scoped_lock lock{sDeathRecipientLock}; |
| 87 | |
| 88 | auto it = sDeathRecipientMap.find(id); |
| 89 | if (it != sDeathRecipientMap.end()) { |
| 90 | recipient = it->second; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | if (recipient != nullptr) { |
| 95 | recipient->binderDied(); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | //static |
| 100 | std::shared_ptr<ResourceObserverService> ResourceObserverService::instantiate() { |
| 101 | std::shared_ptr<ResourceObserverService> observerService = |
| 102 | ::ndk::SharedRefBase::make<ResourceObserverService>(); |
| 103 | binder_status_t status = AServiceManager_addService(observerService->asBinder().get(), |
| 104 | ResourceObserverService::getServiceName()); |
| 105 | if (status != STATUS_OK) { |
| 106 | return nullptr; |
| 107 | } |
| 108 | return observerService; |
| 109 | } |
| 110 | |
| 111 | ResourceObserverService::ResourceObserverService() |
| 112 | : mDeathRecipient(AIBinder_DeathRecipient_new(BinderDiedCallback)) {} |
| 113 | |
| 114 | binder_status_t ResourceObserverService::dump( |
| 115 | int fd, const char** /*args*/, uint32_t /*numArgs*/) { |
| 116 | String8 result; |
| 117 | |
| 118 | if (checkCallingPermission(String16("android.permission.DUMP")) == false) { |
| 119 | result.format("Permission Denial: " |
| 120 | "can't dump ResourceManagerService from pid=%d, uid=%d\n", |
| 121 | AIBinder_getCallingPid(), |
| 122 | AIBinder_getCallingUid()); |
| 123 | write(fd, result.string(), result.size()); |
| 124 | return PERMISSION_DENIED; |
| 125 | } |
| 126 | |
| 127 | result.appendFormat("ResourceObserverService: %p\n", this); |
| 128 | result.appendFormat(" Registered Observers: %zu\n", mObserverInfoMap.size()); |
| 129 | |
| 130 | { |
| 131 | std::scoped_lock lock{mObserverLock}; |
| 132 | |
| 133 | for (auto &observer : mObserverInfoMap) { |
| 134 | result.appendFormat(" Observer %p:\n", observer.second.binder.get()); |
| 135 | for (auto &observable : observer.second.filters) { |
| 136 | String8 enabledEventsStr; |
| 137 | for (auto &event : sEvents) { |
| 138 | if (((uint64_t)observable.eventFilter & (uint64_t)event) != 0) { |
| 139 | if (!enabledEventsStr.isEmpty()) { |
| 140 | enabledEventsStr.append("|"); |
| 141 | } |
| 142 | enabledEventsStr.append(toString(event).c_str()); |
| 143 | } |
| 144 | } |
| 145 | result.appendFormat(" %s: %s\n", |
| 146 | toString(observable.type).c_str(), enabledEventsStr.c_str()); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | write(fd, result.string(), result.size()); |
| 152 | return OK; |
| 153 | } |
| 154 | |
| 155 | Status ResourceObserverService::registerObserver( |
| 156 | const std::shared_ptr<IResourceObserver>& in_observer, |
| 157 | const std::vector<MediaObservableFilter>& in_filters) { |
Chong Zhang | 43a6ba6 | 2020-10-08 13:43:02 -0700 | [diff] [blame] | 158 | if ((getpid() != AIBinder_getCallingPid()) && |
| 159 | checkCallingPermission( |
Chong Zhang | d9c4faf | 2020-09-25 11:54:12 -0700 | [diff] [blame] | 160 | String16("android.permission.REGISTER_MEDIA_RESOURCE_OBSERVER")) == false) { |
| 161 | ALOGE("Permission Denial: " |
| 162 | "can't registerObserver from pid=%d, uid=%d\n", |
| 163 | AIBinder_getCallingPid(), |
| 164 | AIBinder_getCallingUid()); |
| 165 | return Status::fromServiceSpecificError(PERMISSION_DENIED); |
| 166 | } |
Chong Zhang | a9d45c7 | 2020-09-09 12:41:17 -0700 | [diff] [blame] | 167 | |
| 168 | ::ndk::SpAIBinder binder = in_observer->asBinder(); |
| 169 | |
| 170 | { |
| 171 | std::scoped_lock lock{mObserverLock}; |
| 172 | |
| 173 | if (mObserverInfoMap.find((uintptr_t)binder.get()) != mObserverInfoMap.end()) { |
| 174 | return Status::fromServiceSpecificError(ALREADY_EXISTS); |
| 175 | } |
| 176 | |
| 177 | if (in_filters.empty()) { |
| 178 | return Status::fromServiceSpecificError(BAD_VALUE); |
| 179 | } |
| 180 | |
| 181 | // Add observer info. |
| 182 | mObserverInfoMap.emplace((uintptr_t)binder.get(), |
| 183 | ObserverInfo{binder, in_observer, in_filters}); |
| 184 | |
| 185 | // Add observer to observable->subscribers map. |
| 186 | for (auto &filter : in_filters) { |
| 187 | for (auto &event : sEvents) { |
| 188 | if (!((uint64_t)filter.eventFilter & (uint64_t)event)) { |
| 189 | continue; |
| 190 | } |
| 191 | MediaObservableFilter key{filter.type, event}; |
| 192 | mObservableToSubscribersMap[key].emplace((uintptr_t)binder.get(), in_observer); |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | // Add death binder and link. |
| 198 | uintptr_t cookie = (uintptr_t)binder.get(); |
| 199 | { |
| 200 | std::scoped_lock lock{sDeathRecipientLock}; |
| 201 | sDeathRecipientMap.emplace( |
| 202 | cookie, std::make_shared<DeathRecipient>(this, in_observer)); |
| 203 | } |
| 204 | |
| 205 | AIBinder_linkToDeath(binder.get(), mDeathRecipient.get(), |
| 206 | reinterpret_cast<void*>(cookie)); |
| 207 | |
| 208 | return Status::ok(); |
| 209 | } |
| 210 | |
| 211 | Status ResourceObserverService::unregisterObserver( |
| 212 | const std::shared_ptr<IResourceObserver>& in_observer) { |
Chong Zhang | 43a6ba6 | 2020-10-08 13:43:02 -0700 | [diff] [blame] | 213 | if ((getpid() != AIBinder_getCallingPid()) && |
| 214 | checkCallingPermission( |
Chong Zhang | d9c4faf | 2020-09-25 11:54:12 -0700 | [diff] [blame] | 215 | String16("android.permission.REGISTER_MEDIA_RESOURCE_OBSERVER")) == false) { |
| 216 | ALOGE("Permission Denial: " |
| 217 | "can't unregisterObserver from pid=%d, uid=%d\n", |
| 218 | AIBinder_getCallingPid(), |
| 219 | AIBinder_getCallingUid()); |
| 220 | return Status::fromServiceSpecificError(PERMISSION_DENIED); |
| 221 | } |
Chong Zhang | a9d45c7 | 2020-09-09 12:41:17 -0700 | [diff] [blame] | 222 | |
| 223 | ::ndk::SpAIBinder binder = in_observer->asBinder(); |
| 224 | |
| 225 | { |
| 226 | std::scoped_lock lock{mObserverLock}; |
| 227 | |
| 228 | auto it = mObserverInfoMap.find((uintptr_t)binder.get()); |
| 229 | if (it == mObserverInfoMap.end()) { |
| 230 | return Status::fromServiceSpecificError(NAME_NOT_FOUND); |
| 231 | } |
| 232 | |
| 233 | // Remove observer from observable->subscribers map. |
| 234 | for (auto &filter : it->second.filters) { |
| 235 | for (auto &event : sEvents) { |
| 236 | if (!((uint64_t)filter.eventFilter & (uint64_t)event)) { |
| 237 | continue; |
| 238 | } |
| 239 | MediaObservableFilter key{filter.type, event}; |
| 240 | mObservableToSubscribersMap[key].erase((uintptr_t)binder.get()); |
| 241 | |
| 242 | //Remove the entry if there's no more subscribers. |
| 243 | if (mObservableToSubscribersMap[key].empty()) { |
| 244 | mObservableToSubscribersMap.erase(key); |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | // Remove observer info. |
| 250 | mObserverInfoMap.erase(it); |
| 251 | } |
| 252 | |
| 253 | // Unlink and remove death binder. |
| 254 | uintptr_t cookie = (uintptr_t)binder.get(); |
| 255 | AIBinder_unlinkToDeath(binder.get(), mDeathRecipient.get(), |
| 256 | reinterpret_cast<void*>(cookie)); |
| 257 | |
| 258 | { |
| 259 | std::scoped_lock lock{sDeathRecipientLock}; |
| 260 | sDeathRecipientMap.erase(cookie); |
| 261 | } |
| 262 | |
| 263 | return Status::ok(); |
| 264 | } |
| 265 | |
| 266 | void ResourceObserverService::notifyObservers( |
| 267 | MediaObservableEvent event, int uid, int pid, const ResourceList &resources) { |
| 268 | struct CalleeInfo { |
| 269 | std::shared_ptr<IResourceObserver> observer; |
| 270 | std::vector<MediaObservableParcel> monitors; |
| 271 | }; |
| 272 | // Build a consolidated list of observers to call with their respective observables. |
| 273 | std::map<uintptr_t, CalleeInfo> calleeList; |
| 274 | |
| 275 | { |
| 276 | std::scoped_lock lock{mObserverLock}; |
| 277 | |
| 278 | for (auto &res : resources) { |
| 279 | // Skip if this resource doesn't map to any observable type. |
| 280 | MediaObservableType observableType = getObservableType(res.second); |
| 281 | if (observableType == MediaObservableType::kInvalid) { |
| 282 | continue; |
| 283 | } |
| 284 | MediaObservableFilter key{observableType, event}; |
| 285 | // Skip if no one subscribed to this observable. |
| 286 | auto observableIt = mObservableToSubscribersMap.find(key); |
| 287 | if (observableIt == mObservableToSubscribersMap.end()) { |
| 288 | continue; |
| 289 | } |
| 290 | // Loop through all subsribers. |
| 291 | for (auto &subscriber : observableIt->second) { |
| 292 | auto calleeIt = calleeList.find(subscriber.first); |
| 293 | if (calleeIt == calleeList.end()) { |
| 294 | calleeList.emplace(subscriber.first, CalleeInfo{ |
| 295 | subscriber.second, {{observableType, res.second.value}}}); |
| 296 | } else { |
| 297 | calleeIt->second.monitors.push_back({observableType, res.second.value}); |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | // Finally call the observers about the status change. |
| 304 | for (auto &calleeInfo : calleeList) { |
| 305 | calleeInfo.second.observer->onStatusChanged( |
| 306 | event, uid, pid, calleeInfo.second.monitors); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | void ResourceObserverService::onResourceAdded( |
| 311 | int uid, int pid, const ResourceList &resources) { |
| 312 | notifyObservers(MediaObservableEvent::kBusy, uid, pid, resources); |
| 313 | } |
| 314 | |
| 315 | void ResourceObserverService::onResourceRemoved( |
| 316 | int uid, int pid, const ResourceList &resources) { |
| 317 | notifyObservers(MediaObservableEvent::kIdle, uid, pid, resources); |
| 318 | } |
| 319 | |
| 320 | } // namespace android |