| hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 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 |  | 
| Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 20 | #include <aidl/android/media/ITranscodingClient.h> | 
| Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 21 | #include <aidl/android/media/ITranscodingClientCallback.h> | 
| hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 22 | #include <sys/types.h> | 
|  | 23 | #include <utils/Condition.h> | 
| hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 24 | #include <utils/String8.h> | 
|  | 25 | #include <utils/Vector.h> | 
|  | 26 |  | 
| Chong Zhang | 3fa408f | 2020-04-30 11:04:28 -0700 | [diff] [blame] | 27 | #include <map> | 
| hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 28 | #include <mutex> | 
|  | 29 | #include <unordered_map> | 
| Chong Zhang | 3fa408f | 2020-04-30 11:04:28 -0700 | [diff] [blame] | 30 | #include <unordered_set> | 
| hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 31 |  | 
| Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 32 | #include "SchedulerClientInterface.h" | 
|  | 33 |  | 
| hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 34 | namespace android { | 
|  | 35 |  | 
| Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 36 | using ::aidl::android::media::ITranscodingClient; | 
| Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 37 | using ::aidl::android::media::ITranscodingClientCallback; | 
| hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 38 |  | 
|  | 39 | /* | 
|  | 40 | * TranscodingClientManager manages all the transcoding clients across different processes. | 
|  | 41 | * | 
| Chong Zhang | 3fa408f | 2020-04-30 11:04:28 -0700 | [diff] [blame] | 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 | 
| hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 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 | */ | 
| Chong Zhang | 15c192a | 2020-05-05 16:24:00 -0700 | [diff] [blame] | 49 | class TranscodingClientManager : public std::enable_shared_from_this<TranscodingClientManager> { | 
| Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 50 | public: | 
| Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 51 | virtual ~TranscodingClientManager(); | 
| hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 52 |  | 
|  | 53 | /** | 
|  | 54 | * Adds a new client to the manager. | 
|  | 55 | * | 
| Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 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 | 
| Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 58 | * already been added, it will also return non-zero errorcode. | 
| hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 59 | * | 
| Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 60 | * @param callback client callback for the service to call this client. | 
| Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 61 | * @param pid client's process id. | 
|  | 62 | * @param uid client's user id. | 
|  | 63 | * @param clientName client's name. | 
|  | 64 | * @param opPackageName client's package name. | 
|  | 65 | * @param client output holding the ITranscodingClient interface for the client | 
|  | 66 | *        to use for subsequent communications with the service. | 
| hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 67 | * @return 0 if client is added successfully, non-zero errorcode otherwise. | 
|  | 68 | */ | 
| Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 69 | status_t addClient(const std::shared_ptr<ITranscodingClientCallback>& callback, pid_t pid, | 
|  | 70 | uid_t uid, const std::string& clientName, const std::string& opPackageName, | 
|  | 71 | std::shared_ptr<ITranscodingClient>* client); | 
| Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 72 |  | 
|  | 73 | /** | 
|  | 74 | * Gets the number of clients. | 
|  | 75 | */ | 
|  | 76 | size_t getNumOfClients() const; | 
|  | 77 |  | 
|  | 78 | /** | 
|  | 79 | * Dump all the client information to the fd. | 
|  | 80 | */ | 
|  | 81 | void dumpAllClients(int fd, const Vector<String16>& args); | 
|  | 82 |  | 
|  | 83 | private: | 
|  | 84 | friend class MediaTranscodingService; | 
|  | 85 | friend class TranscodingClientManagerTest; | 
| Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 86 | struct ClientImpl; | 
|  | 87 |  | 
| Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 88 | // Only allow MediaTranscodingService and unit tests to instantiate. | 
|  | 89 | TranscodingClientManager(const std::shared_ptr<SchedulerClientInterface>& scheduler); | 
| Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 90 |  | 
|  | 91 | /** | 
| hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 92 | * Removes an existing client from the manager. | 
|  | 93 | * | 
|  | 94 | * If the client does not exist, this will return non-zero errorcode. | 
|  | 95 | * | 
|  | 96 | * @param clientId id of the client to be removed.. | 
|  | 97 | * @return 0 if client is removed successfully, non-zero errorcode otherwise. | 
|  | 98 | */ | 
| Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 99 | status_t removeClient(ClientIdType clientId); | 
| hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 100 |  | 
| hkuang | 9c04b8d | 2020-01-22 10:03:21 -0800 | [diff] [blame] | 101 | static void BinderDiedCallback(void* cookie); | 
|  | 102 |  | 
| hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 103 | mutable std::mutex mLock; | 
| Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 104 | std::unordered_map<ClientIdType, std::shared_ptr<ClientImpl>> mClientIdToClientMap | 
| hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 105 | GUARDED_BY(mLock); | 
| Chong Zhang | 3fa408f | 2020-04-30 11:04:28 -0700 | [diff] [blame] | 106 | std::unordered_set<uintptr_t> mRegisteredCallbacks GUARDED_BY(mLock); | 
| hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 107 |  | 
| hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 108 | ::ndk::ScopedAIBinder_DeathRecipient mDeathRecipient; | 
| Chong Zhang | 6d58e4b | 2020-03-31 09:41:10 -0700 | [diff] [blame] | 109 |  | 
|  | 110 | std::shared_ptr<SchedulerClientInterface> mJobScheduler; | 
| Chong Zhang | 3fa408f | 2020-04-30 11:04:28 -0700 | [diff] [blame] | 111 |  | 
|  | 112 | static std::atomic<ClientIdType> sCookieCounter; | 
|  | 113 | static std::mutex sCookie2ClientLock; | 
|  | 114 | static std::map<ClientIdType, std::shared_ptr<ClientImpl>> sCookie2Client | 
|  | 115 | GUARDED_BY(sCookie2ClientLock); | 
| hkuang | 26587cb | 2020-01-16 10:36:08 -0800 | [diff] [blame] | 116 | }; | 
|  | 117 |  | 
|  | 118 | }  // namespace android | 
|  | 119 | #endif  // ANDROID_MEDIA_TRANSCODING_SERVICE_H |