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_CLIENT_MANAGER_H 18 #define ANDROID_MEDIA_TRANSCODING_CLIENT_MANAGER_H 19 20 #include <aidl/android/media/ITranscodingClient.h> 21 #include <aidl/android/media/ITranscodingClientCallback.h> 22 #include <sys/types.h> 23 #include <utils/Condition.h> 24 #include <utils/String8.h> 25 #include <utils/Vector.h> 26 27 #include <map> 28 #include <mutex> 29 #include <unordered_map> 30 #include <unordered_set> 31 32 #include "ControllerClientInterface.h" 33 34 namespace android { 35 36 using ::aidl::android::media::ITranscodingClient; 37 using ::aidl::android::media::ITranscodingClientCallback; 38 39 /* 40 * TranscodingClientManager manages all the transcoding clients across different processes. 41 * 42 * TranscodingClientManager manages all the clients's registration/unregistration and clients' 43 * information. It also bookkeeps all the clients' information. It also monitors the death of the 44 * clients. Upon client's death, it will remove the client from it. 45 * 46 * TODO(hkuang): Hook up with ResourceManager for resource management. 47 * TODO(hkuang): Hook up with MediaMetrics to log all the transactions. 48 */ 49 class TranscodingClientManager : public std::enable_shared_from_this<TranscodingClientManager> { 50 public: 51 virtual ~TranscodingClientManager(); 52 53 /** 54 * Adds a new client to the manager. 55 * 56 * The client must have valid callback, pid, uid, clientName and opPackageName. 57 * Otherwise, this will return a non-zero errorcode. If the client callback has 58 * already been added, it will also return non-zero errorcode. 59 * 60 * @param callback client callback for the service to call this client. 61 * @param clientName client's name. 62 * @param opPackageName client's package name. 63 * @param client output holding the ITranscodingClient interface for the client 64 * to use for subsequent communications with the service. 65 * @return 0 if client is added successfully, non-zero errorcode otherwise. 66 */ 67 status_t addClient(const std::shared_ptr<ITranscodingClientCallback>& callback, 68 const std::string& clientName, const std::string& opPackageName, 69 std::shared_ptr<ITranscodingClient>* client); 70 71 /** 72 * Gets the number of clients. 73 */ 74 size_t getNumOfClients() const; 75 76 /** 77 * Dump all the client information to the fd. 78 */ 79 void dumpAllClients(int fd, const Vector<String16>& args); 80 81 private: 82 friend class MediaTranscodingService; 83 friend class TranscodingClientManagerTest; 84 struct ClientImpl; 85 86 // Only allow MediaTranscodingService and unit tests to instantiate. 87 TranscodingClientManager(const std::shared_ptr<ControllerClientInterface>& controller); 88 89 // Checks if a user is trusted (and allowed to submit sessions on behalf of other uids) 90 bool isTrustedCaller(pid_t pid, uid_t uid); 91 92 /** 93 * Removes an existing client from the manager. 94 * 95 * If the client does not exist, this will return non-zero errorcode. 96 * 97 * @param clientId id of the client to be removed.. 98 * @return 0 if client is removed successfully, non-zero errorcode otherwise. 99 */ 100 status_t removeClient(ClientIdType clientId); 101 102 static void BinderDiedCallback(void* cookie); 103 104 mutable std::mutex mLock; 105 std::unordered_map<ClientIdType, std::shared_ptr<ClientImpl>> mClientIdToClientMap 106 GUARDED_BY(mLock); 107 std::unordered_set<uintptr_t> mRegisteredCallbacks GUARDED_BY(mLock); 108 109 ::ndk::ScopedAIBinder_DeathRecipient mDeathRecipient; 110 111 std::shared_ptr<ControllerClientInterface> mSessionController; 112 std::unordered_set<uid_t> mTrustedUids; 113 114 static std::atomic<ClientIdType> sCookieCounter; 115 static std::mutex sCookie2ClientLock; 116 static std::map<ClientIdType, std::shared_ptr<ClientImpl>> sCookie2Client 117 GUARDED_BY(sCookie2ClientLock); 118 }; 119 120 } // namespace android 121 #endif // ANDROID_MEDIA_TRANSCODING_SERVICE_H 122